forked from LaradjiSoftMatter/SoftMold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDbondsub.cpp
421 lines (358 loc) · 12.5 KB
/
MDbondsub.cpp
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
/**
* \brief This simply runs a system through the timesteps. It can utilize OpenMP.
* It is currently setup to use the implicit solvent molecular dynamic model used by Laradji's Computational Soft Matter lab.
* The particular system being studied is a phospholipid system.
* Various additions, such as cytoskeletons and nanoparticles, have been studied as well.
* Multicomponent lipid systems have been studied.
*/
//no solvent within frames
#define noSolventFrames
//For molecular dynamics forces and potentials
#include "include/MD.h"
//For the molecular dynamics variables
#include "include/system.h"
//extractData must be located after system
//It is a header dependent on the system in the include directory, but is not part of the program
#include "dataExtraction.h"
#include <ctime>
#define resizeRate 1
int main(int argc, char* argv[])
{
if(argc!=2)
{
//so simple, this can't possibly mess it up
std::cout << "usage: command name\n";
return 0;
}
char *name=argv[1];
//the variables for the simulation, remember that for some reason constructor isn't explicit when nothing is in it
Blob<double> System;
//load variables, then initialize them, Script requires some functions from Blob
Script<double, Blob <double> > fileIO(name,std::ios::in,&System);
fileIO.read();
fileIO.close();
//initialize the variables, objects, algorithms, data collection, etc...
Verlet<double> integrate(System.getPositions(), System.getAccelerations(), System.getVelocities(), System.readNParticles(),
System.readSize(), System.readDeltaT(), System.readPeriodic());
Langevin<double> thermostat(System.getAccelerations(), System.getVelocities(), System.readNParticles(), System.readGamma(),
System.readDeltaT(), System.readSeed());
//for data
dataExtraction<double, Blob <double> > dataCollection(&System,name);
//for xyz frames
position <double> *p=System.getPositions();
int xyzNParticles=System.readNParticles();
xyzFormat<double> xyzFile(p, xyzNParticles);
//for random size fluctuations
MTRand randNum(System.readSeed());
//these two do pretty much the same thing, but CellOpt is much much much faster
CellOpt<double, Potential<double>, Force <double> > pairInteractions(System.getPositions(), System.getAccelerations(),
System.getTwoBodyFconst(), System.getTwoBodyUconst(), System.readNParticles(), System.readNTypes(), System.readSize(),
System.readPeriodic(), System.readCutoff());
//Cell<double> neighbors(System.p, System.nParticles, System.cutoff, System.size);
//Map and count the volumes
int *excludeType=new int[System.readNTypes()];
for(int i=1;i<System.readNTypes();i++)
excludeType[i-1]=i;
VolumeExtraction<double> volumize(System.getPositions(), System.readNParticles(),\
System.readSize(), System.readCutoff(), excludeType, System.readNTypes()-1, System.readSeed());
delete excludeType;
//Other initializations
std::string framesFilename("frames_");
framesFilename+=name;
framesFilename+=".xyz";
int nSolvent=0;
if(System.readRemoveSolvent()>0)
for(int i=0;i<System.readNParticles();i++)
#ifndef SOLVENT_FLAG
if(p[i].type==SOLVENT)
nSolvent++;
#else
if(p[i].type==SOLVENT_FLAG)
nSolvent++;
#endif
//This whole section builds counter interactions for BOND structures
/*
int nBondMol=0;
for(int i=0;i<System.readNMolecules();i++)
if(System.getMolecule()[i].readType()==BOND)
nBondMol++;
CellOpt<double, CounterPotential<double>, CounterForce<double> > *bondCounterInteractions=NULL;
int **bondList=NULL;
if(nBondMol!=0)
{
bondList=new int*[nBondMol];//each list
bondCounterInteractions= new CellOpt<double, CounterPotential<double>, CounterForce<double> > [nBondMol];
}
for(int i=0,listIndex=0;i<System.readNMolecules();i++)
{
if(System.getMolecule()[i].readType()==BOND)
{
int nBondList=(System.getMolecule()[i].readNBond())*2;
bondList[listIndex]=new int[nBondList];
for(int j=0,bondIndex=0;j<System.getMolecule()[i].readNBond();j++)
{
bondList[listIndex][bondIndex++]=System.getMolecule()[i].getBonds()[j].s[0];
bondList[listIndex][bondIndex++]=System.getMolecule()[i].getBonds()[j].s[1];
}
std::sort(&bondList[listIndex][0],&bondList[listIndex][nBondList]);
int *end=std::unique(&bondList[listIndex][0],&bondList[listIndex][nBondList]);
//Supposedly this works with pointer arithmetic?
nBondList=(end-bondList[listI2.3607759e+08ndex]);
bondCounterInteractions[listIndex].initialize(System.getPositions(), System.getAccelerations(),
System.getTwoBodyFconst(), System.getTwoBodyUconst(), System.readNParticles(),
System.readNTypes(), System.readSize(), System.readPeriodic(), System.readCutoff(),
nBondList,bondList[listIndex]);
listIndex++;
}
}
*/
//End of building bond counter interactions,
// the actual execution is below, but commented out
for(int k=0;k<System.readNParticles();k++)
System.getAccelerations()[k]=0;
thermostat.compute(System.readInitialTemp());
//this is how you call the pair interactions, build then compute...
//Cuda works for these
//Force is computed here regardless of previous state.
pairInteractions.build();
pairInteractions.computeForce();
//Cuda doesn't work for this loop yet
for(int k=0;k<System.readNMolecules();k++)
{
switch(System.getMolecule()[k].readType())
{
case BOND:
{
System.doBondForce(k);
break;
}
case BEND:
{
System.doBendForce(k);
break;
}
case CHAIN:
{
System.doChainForce(k);
break;
}
default:
{
//does nothing
break;
}
}
}
//counter-force
//for(int k=0;k<nBondMol;k++)
//{
// bondCounterInteractions[k].build();
// bondCounterInteractions[k].computeForce();
//}
//this corrects an issue where an extra data point is added when the system is restarted
if(System.readInitialTime()==0)
{
dataCollection.initialize();
xyzFile.open(framesFilename,std::ios::out | std::ios::app);
xyzFile.store();
xyzFile.close();
}
else
{
//Surprise! This is done because a previously run configuration doesn't do this upon exit
integrate.second();
}
//using integer indexing, the 0.0000001 fixes an accuracy issue with the gnu c++ compiler.
//Don't believe it affects anything else...
int endInt=int(System.readFinalTime()/System.readDeltaT()+0.0000001);//end
int startInt=int(System.readInitialTime()/System.readDeltaT()+0.0000001);//start
int storeint=int(System.readStoreInterval()/System.readDeltaT()+0.0000001);//when to store
int measureint=int(System.readMeasureInterval()/System.readDeltaT()+0.0000001);//when to measure
double tempStep=(System.readStoreInterval()*(System.readInitialTemp()-System.readFinalTemp()))/(System.readFinalTime()-System.readInitialTime());
std::cout << "starting main loop: \n";
time_t current=time(NULL);
//the molecular dynamics loop, the "running" of the system
for(int i=startInt;i<=endInt;i++)
{
threeVector<double> *acc=System.getAccelerations();
System.setInitialTime((double)i*System.readDeltaT());
integrate.first();
for(int k=0;k<System.readNParticles();k++)
{
//This version is really slow!
//System.getAccelerations()[k]=0;
//Direct access is much faster
acc[k].x=0;
acc[k].y=0;
acc[k].z=0;
}
//The system is stored here because force is updated here, but velocities are updated next.
//It causes a problem when it reenters the loop from a previously run configuration.
if(i%storeint==0 && i!=startInt)
{
System.setInitialTemp(System.readInitialTemp()+tempStep);
fileIO.open(name,std::ios::out);
fileIO.write();
fileIO.close();
xyzFile.open(framesFilename,std::ios::out | std::ios::app);
xyzFile.store();
xyzFile.close();
}
//Cuda works for these
thermostat.compute(System.readInitialTemp());
//Build linked lists
pairInteractions.build();
pairInteractions.computeForce();
//Cuda doesn't work for this loop yet
for(int k=0;k<System.readNMolecules();k++)
{
switch(System.getMolecule()[k].readType())
{
case BOND:
{
System.doBondForce(k);
break;
}
case BEND:
{
System.doBendForce(k);
break;
}
case CHAIN:
{
System.doChainForce(k);
break;
}
default:
{
//does nothing
break;
}
}
}
//counter-force
//for(int k=0;k<nBondMol;k++)
//{
// bondCounterInteractions[k].build();
// bondCounterInteractions[k].computeForce();
//}
integrate.second();
//this just needs to be placed before measure, it just moves inner solvent particles to outer
if(i<System.readRemoveSolvent()*nSolvent)
{
//These 2 lines are very slow, there might be a better way
volumize.build();
volumize.moveToOuter(volumize.grabInner());
}
//Measurements are done here
if(i%measureint==0 && i!=startInt)
{
//double last=current;
time_t last=current;
//current=omp_get_wtime();
current=time(NULL);
//time since last storage step, good for benchmarking
std::cout << System.readInitialTime() << '\t' << current-last << std::endl;
dataCollection.compute();
current=time(NULL);
}
//short section to resize system, note that it only works when deltaLXY is something other than 0, it flags execution.
//This needs to be put in it's own object.
if(i%resizeRate==0 && i!=0 && System.readDeltaLXY()!=0)
{
threeVector<double> size=System.readSize();
threeVector<double> oldSize=System.readSize();
threeVector<double> fluctuation;
fluctuation.x=System.readDeltaLXY()*(2.0*randNum.rand53()-1.0);
fluctuation.y=System.readDeltaLXY()*(2.0*randNum.rand53()-1.0);
//constrain z to maintain volume, but vary projected area
//Note to anyone who sees this later, this method is very anistropic.
// If you have any issue, try lowering deltaLXY or exclude any new types
// from the interaction.
fluctuation.z=(size.x*size.y)/((size.x+fluctuation.x)*(size.y+fluctuation.y));
//System.readDeltaLXY()*(2.0*randNum.rand53()-1.0);
size.x+=fluctuation.x;
size.y+=fluctuation.y;
size.z*=fluctuation.z;
threeVector<double> aSize=size;
aSize.x/=oldSize.x;
aSize.y/=oldSize.y;
aSize.z/=oldSize.z;
double oldVolume=oldSize.x*oldSize.y*oldSize.z;
double newVolume=size.x*size.y*size.z;
double dPotential=pairInteractions.computeDPotential(aSize);
for(int k=0;k<System.readNMolecules();k++)
{
//pick a structure by type
switch(System.getMolecule()[k].readType())
{
case BOND:
{
dPotential+=System.doBondDPotential(k,aSize);
break;
}
case BEND:
{
dPotential+=System.doBendDPotential(k,aSize);
break;
}
case CHAIN:
{
dPotential+=System.doChainDPotential(k,aSize);
break;
}
default:
{
//does nothing
break;
}
}
}
//The next line was the old way of doing this, the one below it is the new way.
//Combined with the changes in cellOpt and systemMD, this is about 15% faster than the old one.
//double D=(oldPotential-newPotential)/System.readInitialTemp();//this is a really small number sometimes
double D=(-dPotential)/System.readInitialTemp();//this is a really small number sometimes
//If volume isn't changed, then this doesn't contribute.
//D-=(double(System.readNParticles())*log(oldVolume/newVolume));
D=exp(-D);
double randNumber=randNum.rand53();
//accept change
if(D<=randNum.rand53())
{
for(int k=0;k<System.readNParticles();k++)
{
//This is kind of a template for excluding a type if needed
//Don't forget to do the same thing to the above section as well
//if(Sys.getP()[k].type!=excludedType)
//{
p[k].x*=(size.x/oldSize.x);
p[k].y*=(size.y/oldSize.y);
p[k].z*=(size.z/oldSize.z);
//}
}
pairInteractions.resize(size);
integrate.resize(size);
System.setSize(size);
}
}
//someMeasureInterval would be an integer, like every 10 steps rather than 10 tau
//if(i%someMeasureInterval && i!=0)
//{
// //you would put the measure here
//}
//you could potentially add more measures here
}
//Clear any memory allocated in main:
/*
if(nBondMol!=0)
{
delete[] bondCounterInteractions;
for(int i=0;i<nBondMol;i++)
{
delete bondList[i];
}
delete bondList;
}
*/
return 0;
}