-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHO.cpp~
More file actions
450 lines (380 loc) · 13.7 KB
/
HO.cpp~
File metadata and controls
450 lines (380 loc) · 13.7 KB
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
#include <time.h>
#include <random>
#include <utility>
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
//#define pb push_back;
double getForce(double position, double mass)
{
double omega = 1;
return -mass*omega*omega*position;
}
class HarmonicPotential
{
public:
double mass;
double omega;
HarmonicPotential(double m, double o)
{
mass = m;
omega = o;
}
double getPotential(double pos)
{
return 0.5 * mass * omega * omega * pos * pos;
}
double getForce(double pos)
{
return -mass * omega * omega * pos;
}
};
/*
double combine(double pos, double lambda, HarmonicPotential* p1, HarmonicPotential* p2)
{
return (1-lambda)*p1->getPotential(pos) + lambda*p2->getPotential(pos);
}
*/
/*
class TwoStatePotential
{
public:
double getPotential(double lambda, TwoStatePotential::*pot_A, TwoStatePotential::*pot_B, double x);
void harmonic(double omega, double mass, double pos);
};
TwoStatePotential::harmonic(double omega, double mass, double pos)
{
return 0.5*mass*omega*omega*pos*pos;
}
OBTwoStatePotential::getPotential(double lambda, TwoStatePotential::*pot_A, TwoStatePotential::*pot_B, double x)
{
}
*/
class Bead
{
public:
int dim; //dimension of the system
double* position;
double* velocity;
double mass;
Bead(std::vector<double>::iterator pos, int d, std::vector<double>::iterator vel, double m)
{
dim = d;
position = new double[dim];
velocity = new double[dim];
for (int i=0;i<dim;i++)
{
position[i] = pos[i];
velocity[i] = vel[i];
}
mass = m;
}
/*
void updateForces()
{
for (int i=0;i<dim;i++)
force[i] = getForce(position[i],mass);
}
*/
void printMe()
{
std::cout << "bead position: ";
for (int i=0;i<dim;i++)
std::cout << position[i] << " ";
std::cout << std::endl;
std::cout << "bead velocity: ";
for (int i=0;i<dim;i++)
std::cout << velocity[i] << " ";
std::cout << std::endl;
}
};
class RP
{
public:
int dim; //dimension of the system
std::vector<Bead> beads;
int P; //number of beads
RP(int d, int numBeads, std::vector<double>* positions, std::vector<double>* velocities, std::vector<double>* masses)
{
P = numBeads;
dim = d;
// std::cout << "numbeads is " << numBeads << std::endl;
for (int i =0; i < numBeads; i++)
{
// std::cout << "here" << std::endl;
// std::cout << *(positions+i*dim) << " " << dim << " " << *(velocities+i*dim) << " " << masses[i] << std::endl;
// Bead* b = new Bead(positions+i*dim, dim, velocities+i*dim, masses[i]);
// b->printMe();
beads.push_back(Bead(positions->begin()+i*dim, dim, velocities->begin()+i*dim, masses->at(i)));
}
// std::cout << "supposedly initialized all the beads" << std::endl;
// beads.at(0).printMe();
}
/*
void updateForces()
{
for (std::vector<Bead>::iterator it = beads.begin(); it < beads.end(); it++)
it->updateForces();
}
*/
void printMe()
{
std::cout << "DETAILS OF THE RING POLYMER::" << std::endl;
std::cout << "POSITIONS: VELOCITIES:" << std::endl;
// for (int i = 0; i < P; i++)
// std::cout << beads[i].position << " " << beads[i].velocity << std::endl;
}
};
double integrate(std::vector<std::pair<double,double> >* func)
{
std::cout << "starting integration" << std::endl;
double delta = func->at(1).first - func->at(0).first; //assuming that the domain varies linearly and constantly
std::cout << delta << std::endl;
std::vector<std::pair<double,double> > derivative;
derivative.push_back(std::pair<double,double>(func->at(0).first, (func->at(1).second - func->at(0).second)/(delta)));
// for (std::vector<std::pair<double,double> >::iterator it = func->begin(); it+2 < func->end(); it++)
//std::cout << it->first << " " << it->second << std::endl;
double tmp = 0;
for (std::vector<std::pair<double,double> >::iterator it = func->begin(); it+2 < func->end(); it++)
{
// std::cout << ((it+2)->second - it->second)/(2*delta) << std::endl;
derivative.push_back(std::pair<double,double>((it+1)->first, ((it+2)->second - it->second)/(2*delta)));
}
derivative.push_back(std::pair<double,double>((func->end()-1)->first, ((func->end()-1)->second - (func->end()-2)->second)/(delta)));
// std::cout << "second to last : " << (func->end()-1)->second << std::endl;
// std::cout << "last : " << func->end()->second << std::endl;
// std::cout << "This is what I got for the derivative : " << std::endl;
// for (std::vector<std::pair<double,double> >::iterator it = derivative.begin(); it < derivative.end(); it++)
// std::cout << it->first << " " << it->second << std::endl;
// std::cout << " end of derivative" << std::endl;
std::cout << "computed the derivative. starting FE integration" << std::endl;
double ans = 0;
for (std::vector<std::pair<double,double> >::iterator it = derivative.begin(); it+1 < derivative.end(); it++)
{
ans += delta * ( (it+1)->second + it->second) / 2;
// std::cout << "between " << it->first << " and " << (it+1)->first << " we get " << ans << std::endl;
}
return ans;
}
int main()
{
//initialize the ring polymer
int dim = 1;
int P = 3;
// double* positions = new double[dim*P];
// double* velocities = new double[dim*P];
// double* masses = new double[dim];
// positions[0] = 1;
// positions[1] = 2;
// positions[2] = 3;
// velocities[0] = 0;
// velocities[1] = 0;
// velocities[2] = 0;
// masses[0] = 1;
// RP myRP(dim,P,positions,velocities,masses);
double time = 2;
double deltaT = 0.01;
//open the output files:
std::ofstream fpos("positions.dat");
std::ofstream fvel("velocities.dat");
std::ofstream fene("energy.dat");
//create the two potentials by first initializing their classes:
HarmonicPotential hp1(1,2);
HarmonicPotential hp2(1,3);
/*
//create function pointers to the two potential functions:
double (*pot_A)(double);
pot_A = &hp1.getPotential;
double (*pot_B)(double);
pot_B = &hp2.getPotential;
//create function pointers to the two force functions:
double (*force_A)(double);
force_A = &hp1.getForce;
double (*force_B)(double);
force_B = &hp2.getForce;
*/
/*
std::cout << "TESTING FUNCTION OF FUNCTION FUNCTIONALITY" << std::endl;
for (double lambda = 0; lambda <= 1; lambda+=0.1)
{
std::cout << combine(1,lambda, &hp1, &hp2) << std::endl;
}
std::cout << "DONE TESTING FUNCTION OF FUNCTION FUNCTIONALITY" << std::endl;
*/
//For each instance of the parameter, create a boltzmann distribution of polymers:
int numPolymers = 10000;
std::vector<RP> ensemble;
//create a boltzmann distribution random number generator:
std::default_random_engine generator;
std::normal_distribution<double> distribution(0,0.25);
//initialize many polymers with positions and velocities randomly from this distribution
//make sure that the center of mass (COM) momentum of each polymer is 0
double com = 0;
std::cout << " **** STARTING RP INIT ****" << std::endl;
for (int i = 0; i < numPolymers; i++)
{
com = 0;
std::vector<double> positions;
std::vector<double> velocities;
std::vector<double> masses;
for (int curBead = 0; curBead < P; curBead++)
{
masses.push_back(1);
for (int curDim = 0; curDim < dim; curDim++)
{
positions.push_back(distribution(generator));
double cur_v = distribution(generator);
velocities.push_back(cur_v);
com += cur_v; //WARNING: ASSUMES THAT THE MASS IS 1!!!!!!!!!!!
}
}
//subtract the COM momentum:
for (int curBead = 0; curBead < P; curBead++)
for (int curDim = 0; curDim < dim; curDim++)
velocities[curBead*dim+curDim] -= com/(dim*P); //ONLY WORKS WHEN MASS IS 1!!!!!!!!!!!!
//create a polymer with these initial conditions and store in the ensemble
ensemble.push_back(RP(dim,P,&positions,&velocities,&masses));
}
//output the initial distribution of velocities and positions:
std::ofstream dist("dist.dat");
for (int curRP = 0; curRP < numPolymers; curRP++)
{
for (int curBead = 0; curBead < P; curBead++)
{
for (int curDim = 0; curDim < dim; curDim++)
{
dist << ensemble[curRP].beads[curBead].position[curDim] << " " << ensemble[curRP].beads[curBead].velocity[curDim] << std::endl;
}
}
}
dist.close();
// srand (time(NULL));
//we have all of the ring polymers initialized -- now propogate them in time so that they equilibrate.
std::vector<double> curForces;
std::vector<double> oldForces;
double kinetic, potential;
bool first = true;
double tmp;
std::ofstream fpot("fpot.dat");
double energy_test = 0;
bool testing = true;
std::vector<std::pair<double, double> > integrand; //will store the values (lambda, potential energy)
//as we vary lambda, the potential changes and we need to wait for the system to equlibrate.
//after equlibrating we can sample the potential energy
int numSamples = 0;
double pot = 0;
// Smoothly vary the potential-flipping parameter:
for (double lambda = 0; lambda <= 1; lambda += 0.02)
{
std::cout << " ************** CURRENT LAMBDA ************* " << lambda << std::endl;
for (double curTime = 0; curTime < time; curTime+=deltaT)
{
kinetic=0;
potential=0;
pot = 0;
numSamples = 0;
for(std::vector<RP>::iterator myRP = ensemble.begin(); myRP < ensemble.end(); myRP++)
{
//update forces:
for (int curBead = 0; curBead < myRP->P; curBead++)
for (int j=0;j<dim;j++)
{
if (first)
{
tmp = (1-lambda)*hp1.getForce(myRP->beads[curBead].position[j]) + lambda*hp2.getForce(myRP->beads[curBead].position[j]);
curForces.push_back(tmp);
oldForces.push_back(tmp);
first = false;
} else
{
tmp = (1-lambda)*hp1.getForce(myRP->beads[curBead].position[j]) + lambda*hp2.getForce(myRP->beads[curBead].position[j]);
curForces[curBead*dim+j] = tmp;
oldForces[curBead*dim+j] = tmp;
}
}
//update positions
for (int curBead = 0; curBead < myRP->P; curBead++)
for (int j=0;j<dim;j++)
myRP->beads[curBead].position[j] += deltaT * myRP->beads[curBead].mass * myRP->beads[curBead].velocity[j]
+ deltaT * deltaT * 0.5 / myRP->beads[curBead].mass * curForces[curBead*dim+j];
//update current forces while keeping the old ones
for (int curBead = 0; curBead < myRP->P; curBead++)
for (int j=0;j<dim;j++)
curForces[curBead*dim+j] = (1-lambda)*hp1.getForce(myRP->beads[curBead].position[j]) + lambda*hp2.getForce(myRP->beads[curBead].position[j]);
//update velocities
for (int curBead = 0; curBead < myRP->P; curBead++)
for (int j=0;j<dim;j++)
myRP->beads[curBead].velocity[j] += deltaT * 0.5 / myRP->beads[curBead].mass * (oldForces[curBead*dim+j]+curForces[curBead*dim+j]);
//compute current potential and kinetic energies
for (int curBead = 0; curBead < myRP->P; curBead++)
for (int j=0;j<dim;j++)
{
kinetic += 0.5 * myRP->beads[curBead].mass * myRP->beads[curBead].velocity[j] * myRP->beads[curBead].velocity[j];
potential += ( (1-lambda)*hp1.getPotential(myRP->beads[curBead].position[j]) + lambda*hp2.getPotential(myRP->beads[curBead].position[j]) );
}
/*
if (testing)
{
energy_test = 0;
// if (lambda <0.05)
{
energy_test += 0.5 * ensemble[0].beads[0].mass * ensemble[0].beads[0].velocity[0] * ensemble[0].beads[0].velocity[0];
energy_test += ( (1-lambda)*hp1.getPotential(ensemble[0].beads[0].position[0]) + lambda*hp1.getPotential(ensemble[0].beads[0].position[0]) );
std::cout << curTime + lambda*1000 << " " << energy_test << " " << ensemble[0].beads[0].position[0] << " " <<ensemble[0].beads[0].velocity[0] << std::endl;
}
testing = false;
}
*/
} //end of VV loop
fpos << curTime+time*lambda*50 << " ";
fvel << curTime+time*lambda*50 << " ";
//record the position and velocity of just one of the polymers:
for (std::vector<Bead>::iterator it = ensemble[1].beads.begin(); it < ensemble[1].beads.end(); it++)
{
for (int j=0;j<dim;j++)
{
fpos << it->position[j] << " ";
fvel << it->velocity[j] << " ";
}
}
fpos << std::endl;
fvel << std::endl;
fene << curTime+time*lambda*50 << " " << kinetic << " " << potential << std::endl;
// sample the potential energy with some probability (here, 0.2):
if (curTime > time/2)
std::cout << " ABOUT TO SAMPLE POTENTIAL " << time/2 << std::endl;
if (rand()%10 <= 5 && curTime > time/2) // with probability 0.2
{
pot += potential;
numSamples++;
}
} //end of time loop
integrand.push_back(std::pair<double,double>(lambda,pot));
fpot << lambda << " " << pot/numSamples << " " << numSamples << std::endl;
} // end of lambda loop
fpos.close();
fvel.close();
fene.close();
fpot.close();
std::cout << "FREE ENERGY DIFFERENCE IS : " << std::endl;
for (std::vector<std::pair<double,double> >::iterator it = integrand.begin(); it < integrand.end(); it++)
std::cout << it->first << " " << it->second << std::endl;
std::vector<std::pair<double,double> > test;
/*
test.push_back(std::pair<double,double>(0.0,0));
test.push_back(std::pair<double,double>(0.5,.25));
test.push_back(std::pair<double,double>(1.0,1));
test.push_back(std::pair<double,double>(1.5,2.25));
test.push_back(std::pair<double,double>(2.0,4));
test.push_back(std::pair<double,double>(2.5,6.25));
test.push_back(std::pair<double,double>(3.0,9));
test.push_back(std::pair<double,double>(3.5,12.25));
test.push_back(std::pair<double,double>(4.0,16));
test.push_back(std::pair<double,double>(4.5,20.25));
test.push_back(std::pair<double,double>(5.0,25));
*/
std::cout << integrate(&integrand) << std::endl;
};