-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotential.cc
471 lines (407 loc) · 14.6 KB
/
potential.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <dlib/optimization.h>
#include <vector>
#include <iostream>
#include <libconfig.h++>
#include <cstring>
#include "geometry.h"
#include "structure.h"
#include "parameter.h"
#include "stop_strategy.h"
#include "potential.h"
#include "iop.h"
#include "globals.h"
using namespace std;
/*----------------------------------Energy----------------------------------------------*/
double pairPotential::calcEnergy (const column_vector &v)
{
vector<coord3d> coordinates;
for (long i = 0; i < v.size() / 3; ++i)
{
coord3d sphere(v(3 * i), v(3 * i + 1), v(3 * i + 2));
coordinates.push_back(sphere);
}
structure S(0, coordinates, false);
double f(0);
for (int i = 0; i < S.nAtoms(); ++i) {
for (int j = i + 1; j < S.nAtoms(); ++j) {
f += this->E (coord3d::dist (S[i],S[j]));
}
}
return f;
}
/*----------------------------------Energy from structure-------------------------------*/
double pairPotential::calcEnergy (structure &S)
{
double f(0);
for (int i = 0; i < S.nAtoms(); ++i) {
for (int j = i + 1; j < S.nAtoms(); ++j) {
f += this->E (coord3d::dist (S[i],S[j]));
}
}
return f;
}
/*----------------------------------Gradient--------------------------------------------*/
const column_vector pairPotential::calcGradient (const column_vector &v)
{
vector<coord3d> coordinates;
for (long i = 0; i < v.size() / 3; ++i)
{
coord3d sphere(v(3 * i), v(3 * i + 1), v(3 * i + 2));
coordinates.push_back(sphere);
}
structure S(0, coordinates, false);
vector<coord3d> gradients (S.nAtoms(), coord3d());
for (int i = 0; i < S.nAtoms(); i++)
{
for (int j = i + 1; j < S.nAtoms(); j++)
{
coord3d distanceVector = S[i]-S[j];
double gradValue = this->dE_dr (coord3d::dist(S[i],S[j]));
coord3d twoBodyGradient = distanceVector / distanceVector.norm() * gradValue;
gradients[i] += twoBodyGradient;
gradients[j] -= twoBodyGradient;
}
}
column_vector df (gradients.size() * 3);
for (vector<coord3d>::size_type i = 0; i< gradients.size(); i++)
{
for (int j = 0; j < 3; j++)
{
df(3 * i + j) = gradients[i][j];
}
}
return df;
}
/*----------------------------------Gradient from structure-----------------------------*/
const column_vector pairPotential::calcGradient (structure &S)
{
vector<coord3d> gradients (S.nAtoms(), coord3d());
for (int i = 0; i < S.nAtoms(); i++)
{
for (int j = i + 1; j < S.nAtoms(); j++)
{
coord3d distanceVector = S[i]-S[j];
double gradValue = this->dE_dr (coord3d::dist(S[i],S[j]));
coord3d twoBodyGradient = distanceVector / distanceVector.norm() * gradValue;
gradients[i] += twoBodyGradient;
gradients[j] -= twoBodyGradient;
}
}
column_vector df (gradients.size() * 3);
for (vector<coord3d>::size_type i = 0; i< gradients.size(); i++)
{
for (int j = 0; j < 3; j++)
{
df(3 * i + j) = gradients[i][j];
}
}
return df;
}
/*----------------------------------Hessian---------------------------------------------*/
vector< vector<double> > pairPotential::calcHessian (structure &S)
{
vector< vector<double> > hessianMatrix (S.nAtoms() * 3, vector<double> (S.nAtoms() * 3, 0));
for (int i = 0; i < S.nAtoms(); i++)
{
for (int j = i + 1; j < S.nAtoms(); j++)
{
const coord3d vecr = S[i] - S[j];
const double r = coord3d::dist(S[i], S[j]);
//calculate first and second derivative values
double dE_dr = this->dE_dr(r);
double d2E_dr2 = this->d2E_dr2(r);
//calculate derivatives of r
coord3d dvecr_dr = coord3d::dnorm(vecr);
vector<double> d2rvecr_dr2(9, double());
coord3d::ddnorm(vecr, d2rvecr_dr2);
//calculation of hessian elements
//loop over all 6 coordinates of 1 atom pair
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
//calculate the value first, which will always only differ by sign
const double hessianValue = dE_dr * d2rvecr_dr2[3 * k + l]
+ d2E_dr2 * dvecr_dr[k] * dvecr_dr[l];
/*write hessian
this is basically a 2 atom hessian, where the diagonal quadrants are the same and the
remaining quadrants are of the opposite sign
each quadrant can have contributions from different atom pairs
eg atom pair 1/2 and 1/3 both have non zero second derivatives with respect to the
coordinates of atom 1*/
hessianMatrix[3 * i + k][3 * i + l] += hessianValue;
hessianMatrix[3 * i + k][3 * j + l] -= hessianValue;
hessianMatrix[3 * j + k][3 * i + l] -= hessianValue;
hessianMatrix[3 * j + k][3 * j + l] += hessianValue;
}
}
}
}
return hessianMatrix;
}
pair<double,vector<double> > pairPotential::getLowestEvec (vector< pair< double,vector<double> > > &V)
{
sort(V.begin(),V.end());
for (auto& i : V)
{
if (i.first > 0.001 || i.first < -0.001)
{
return i;
break;
}
}
throw "This should never ever happen! Unreachable reached.";
}
/*----------------------------------Optimization----------------------------------------*/
structure pairPotential::optimize (ostream &min, structure &S)
{
min.precision(16);
column_vector x = S.getFlattenedCoordinates();
auto f = [this] (column_vector v) -> const double { return this->calcEnergy(v); };
auto df = [this] (column_vector v) -> column_vector { return this->calcGradient(v); };
switch (_algo_switch)
{
case 1:
{
try
{
dlib::find_min( dlib::bfgs_search_strategy(),
dlib::stop_strategy(_stop_crit, _nsteps).be_verbose(min),
f, df, x, -(S.nAtoms()) * 1000);
}
catch (std::exception &e)
{
//cerr << "Structure " << S.getNumber() << ": " << e.what() << endl;
structure newS(S.getNumber(), S.getCoordinates());
return newS;
}
break;
}
case 2:
{
try
{
dlib::find_min( dlib::cg_search_strategy(),
dlib::stop_strategy(_stop_crit, _nsteps).be_verbose(min),
f, df, x, -(S.nAtoms()) * 1000);
}
catch (std::exception &e)
{
//cerr << "Structure " << S.getNumber() << ": " << e.what() << endl;
structure newS(S.getNumber(), S.getCoordinates());
return newS;
}
break;
}
default:
{
cerr << "Bad input of algorithm name" << endl;
structure newS(S.getNumber(), S.getCoordinates());
return newS;
}
}
vector<coord3d> newCoordinates = structure::unflattenCoordinates(x);
//for (long i = 0; i < x.size() / 3; i++)
//{
// coord3d sphere (x(3 * i), x(3 * i + 1), x(3 * i + 2));
// newCoordinates.push_back(sphere);
//}
structure newS(S.getNumber(), newCoordinates);
double finalEnergy = this->calcEnergy(x);
column_vector finalGradient = this->calcGradient(x);
min << "-----------------------------------------------" << endl;
min << "E: " << finalEnergy << endl;
min << "g: " << scientific << dlib::length(finalGradient) << endl;
newS.setEnergy(finalEnergy);
newS.setConverged();
return newS;
}
/*--------------------------------------------------------------------------------------*/
// LJ potential derived class
/*--------------------------------------------------------------------------------------*/
double LJ::E (double distance)
{
return (_epsilon / (_exp1/_exp2 - 1)) * ( (pow (_rm / distance, _exp1)) - (_exp1/_exp2) * (pow (_rm / distance, _exp2)) );
}
double LJ::dE_dr (double distance)
{
return - (( _epsilon / (_rm * (_exp1/_exp2 - 1)) ) * ( _exp1 * (pow (_rm / distance, _exp1 + 1)) - _exp1 * (pow (_rm / distance, _exp2 + 1)) ));
}
double LJ::d2E_dr2 (double distance)
{
return _epsilon / (pow (_rm, 2) * (_exp1/_exp2-1)) * ( (pow (_exp1, 2) + _exp1) * pow (_rm / distance, _exp1 + 2) - (_exp1 * _exp2 + _exp1) * pow (_rm / distance, _exp2 + 2) );
}
LJ *LJ::readPotential ()
{
libconfig::Config cfg;
try {
cfg.readFile("settings");
}
catch(const libconfig::FileIOException &fioex) {
cerr << "\tI/O error while reading file." << endl;
}
catch(const libconfig::ParseException &pex) {
cerr << "\tParse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << endl;
}
double epsilon, rm, exp1, exp2;
int algo_switch, nsteps;
double stop_crit;
string algo;
cfg.lookupValue("potential.epsilon", epsilon);
cfg.lookupValue("potential.rm", rm);
cfg.lookupValue("potential.exp1", exp1);
cfg.lookupValue("potential.exp2", exp2);
cfg.lookupValue("opt.nsteps", nsteps);
cfg.lookupValue("opt.convergence", stop_crit);
cfg.lookupValue("opt.name", algo);
if (algo == "BFGS") algo_switch = 1;
if (algo == "CG") algo_switch = 2;
LJ *potential = new LJ(epsilon, rm, exp1, exp2, algo_switch, stop_crit, nsteps);
return potential;
}
/*--------------------------------------------------------------------------------------*/
// extended LJ potential derived class
/*--------------------------------------------------------------------------------------*/
double ELJ::E (double distance)
{
double en(0);
for (vector<double>::size_type i = 0; i < _c.size(); i++)
{
en += _c[i] * pow(1 / distance, i);
}
return en;
}
double ELJ::dE_dr (double distance)
{
double grad(0);
for (vector<double>::size_type i = 0; i < _c.size(); i++)
{
grad -= i * _c[i] * pow(1 / distance, i + 1);
}
return grad;
}
double ELJ::d2E_dr2 (double distance)
{
double hess(0);
for (vector<double>::size_type i = 0; i < _c.size(); i++)
{
hess += i * (i + 1) * _c[i] * pow(1 / distance, i + 2);
}
return hess;
}
ELJ *ELJ::readPotential ()
{
ifstream input;
vector<double> c(30);
if (!fexists("ext"))
{
cerr << "No extended LJ parameters found" << endl;
}
input.open("ext");
int n;
double c_value;
while (input >> n >> c_value)
{
c[n] = c_value;
}
libconfig::Config cfg;
try {
cfg.readFile("settings");
}
catch(const libconfig::FileIOException &fioex) {
cerr << "\tI/O error while reading file." << endl;
}
catch(const libconfig::ParseException &pex) {
cerr << "\tParse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << endl;
}
int algo_switch, nsteps;
double stop_crit;
string algo;
cfg.lookupValue("opt.nsteps", nsteps);
cfg.lookupValue("opt.convergence", stop_crit);
cfg.lookupValue("opt.name", algo);
if (algo == "BFGS") algo_switch = 1;
if (algo == "CG") algo_switch = 2;
ELJ *potential = new ELJ(c, algo_switch, stop_crit, nsteps);
return potential;
}
/*--------------------------------------------------------------------------------------*/
// LJ potential with range cutoff derived class
/*--------------------------------------------------------------------------------------*/
double RangeLJ::E (double distance)
{
if (distance > (_rm * _range)) return 0;
return (_epsilon / (_exp1/_exp2 - 1)) * ( (pow (_rm / distance, _exp1)) - (_exp1/_exp2) * (pow (_rm / distance, _exp2)) );
}
double RangeLJ::dE_dr (double distance)
{
if (distance > (_rm * _range)) return 0;
return - (( _epsilon / (_rm * (_exp1/_exp2 - 1)) ) * ( _exp1 * (pow (_rm / distance, _exp1 + 1)) - _exp1 * (pow (_rm / distance, _exp2 + 1)) ));
}
double RangeLJ::d2E_dr2 (double distance)
{
if (distance > (_rm * _range)) return 0;
return _epsilon / (pow (_rm, 2) * (_exp1/_exp2-1)) * ( (pow (_exp1, 2) + _exp1) * pow (_rm / distance, _exp1 + 2) - (_exp1 * _exp2 + _exp1) * pow (_rm / distance, _exp2 + 2) );
}
RangeLJ *RangeLJ::readPotential ()
{
libconfig::Config cfg;
try {
cfg.readFile("settings");
}
catch(const libconfig::FileIOException &fioex) {
cerr << "\tI/O error while reading file." << endl;
}
catch(const libconfig::ParseException &pex) {
cerr << "\tParse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << endl;
}
double epsilon, rm, exp1, exp2, range;
int algo_switch, nsteps;
double stop_crit;
string algo;
cfg.lookupValue("potential.epsilon", epsilon);
cfg.lookupValue("potential.rm", rm);
cfg.lookupValue("potential.exp1", exp1);
cfg.lookupValue("potential.exp2", exp2);
cfg.lookupValue("potential.range", range);
cfg.lookupValue("opt.nsteps", nsteps);
cfg.lookupValue("opt.convergence", stop_crit);
cfg.lookupValue("opt.name", algo);
if (algo == "BFGS") algo_switch = 1;
if (algo == "CG") algo_switch = 2;
RangeLJ *potential = new RangeLJ(epsilon, rm, exp1, exp2, range, algo_switch, stop_crit, nsteps);
return potential;
}
/*--------------------------------------------------------------------------------------*/
// main function for testing
/*--------------------------------------------------------------------------------------*/
//int main ()
//{
// vector<coord3d> coords;
// coords.push_back(coord3d(0,0,0));
// coords.push_back(coord3d(1,0,0));
// coords.push_back(coord3d(1,0.87,0));
// structure test(1,coords);
//
// column_vector x(test.nAtoms() * 3);
// for (int i = 0; i < test.nAtoms(); ++i)
// {
// for (int j = 0; j<=2; ++j)
// {
// x(3 * i + j) = (test)[i][j];
// }
// }
// pairPotential pp;
// LJ len;
// cout << len.calcEnergy(x) << endl;
// const column_vector grad = len.calcGradient(x);
// cout << grad << endl;
//
//
// //len.optimize<LJ>(std::cout, test);
// //pp.optimize<pairPotential>(std::cout, test);
//
// len.calcHessian(test);
//
//return 0;
//}