-
Notifications
You must be signed in to change notification settings - Fork 0
/
CConfiguration.cpp
executable file
·586 lines (465 loc) · 21.3 KB
/
CConfiguration.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "headers/CConfiguration.h"
using namespace std;
const double _6root2 = 1.122462;
CConfiguration::CConfiguration(){
}
CConfiguration::CConfiguration(
double timestep, double potRange, double potStrength, double boxsize, double rodDistance, const bool potMod,
double psize, const bool posHisto, const bool steric, const bool ranU, bool hpi, double hpi_u, double hpi_k, bool ranRod, double n_rods){
_potRange = potRange;
_potStrength = potStrength;
_pradius = psize/2;
_boxsize = boxsize;
_resetpos = _boxsize/2;
_timestep = timestep;
_rodDistance = rodDistance;
_potMod = potMod;
_ranRod = ranRod;
_n_rods = n_rods;
_LJPot = (steric == false) && (psize != 0);
_ranU = ranU;
_poly = CPolymers();
_hpi = hpi;
_upot = 0;
_mu_sto = sqrt( 2 * _timestep ); //timestep for stochastic force
_hpi = hpi;
_hpi_u = hpi_u;
_hpi_k = hpi_k;
for (int i = 0; i < 3; i++){
_ppos[i] = _resetpos;
_startpos[i] = _resetpos;
_entryside[i] = 0;
_wallcrossings[i] = 0;
_boxnumberXYZ[i] = 0;
_prevpos[i] = _resetpos;
}
if (posHisto) initPosHisto();
// This is for inclusion of 2nd Order rods if k is 0.2b or larger
_min = -1, _max = 3;
if ( _ranU || _hpi || (_potRange < 2)){
_min = 0;
_max = 2;
}
if (_ranRod){
_min = 0;
_max = 1;
}
// seed = 0: use time, else any integer
// init random number generator
setRanNumberGen(0);
if (_ranRod){
initRodsVec();// SO FAR THE RODS ARE NOT INITIADED RANDOMLY; BUT SUCH THAT THE TRACER FITS FOR SURE FOR p <= 10
}
}
void CConfiguration::updateStartpos(){
//This function is used if the particle should keep moving after each run, and not start at _resetpos again, like in the first run
//This (hopefully) will give better averages without having to spend a lot of steps in the beginning of each run to get away from _resetpos
for (int i = 0; i < 3; i++){
_startpos[i] = _ppos[i] + _boxsize * _boxnumberXYZ[i];
}
}
void CConfiguration::resetposition(){
//Reset the position after every run.
for (int i = 0; i < 3; i++){
_entryside[i] = 0;
_startpos[i] = _resetpos;
_ppos[i] = _resetpos;
_boxnumberXYZ[i] = 0;
}
}
void CConfiguration::makeStep(){
//move the particle according to the forces and record trajectory like watched by outsider
for (int i = 0; i < 3; i++){
_prevpos[i] = _ppos[i];
_ppos[i] += _timestep * _f_mob[i] + _mu_sto * _f_sto[i];
}
}
void CConfiguration::checkBoxCrossing(){
//should the particle cross the confinement of the cube, let it appear on the opposite side of the box
int exitmarker = 0;
for (int i = 0; i < 3; i++){
exitmarker =0;
if (_ppos[i] < 0){
_ppos[i] += _boxsize;
_boxnumberXYZ[i] -= 1;
exitmarker = -1;
}
else if (_ppos[i] > _boxsize){
_ppos[i] -= _boxsize;
_boxnumberXYZ[i] += 1;
exitmarker = 1;
}
if (exitmarker!=0){
countWallCrossing(i, exitmarker);
if (_ranU) _poly.shiftPolySign(i, exitmarker);
if (_ranRod){
updateRodsVec(i, exitmarker);
//cout << "[["<<i<<"," << exitmarker <<"] ";
//prinRodPos(0); // cout print rod pos!
}
}
}
}
void CConfiguration::countWallCrossing(int crossaxis, int exitmarker){
if (_entryside[crossaxis] == exitmarker){ // case: entryside same as exitside
_wallcrossings[0] += 1;
}
else if (_entryside[crossaxis] == (-1.0 * exitmarker)){ // case: entryside opposite exitside
_wallcrossings[1] += 1;
}
else { // case: exiting "sideways", i.e. through one of the four other sides
_wallcrossings[2] += 1;
}
for (int i = 0; i < 3; i++){ // mark new entryside
_entryside[i] = 0;
}
_entryside[crossaxis] = -1 * exitmarker;
}
void CConfiguration::calcStochasticForces(){
// the variate generator uses m_igen (int rand number generator),
// samples from normal distribution with variance 1 (later sqrt(2) is multiplied)
boost::variate_generator<boost::mt19937&, boost::normal_distribution<double> > ran_gen(
*m_igen, boost::normal_distribution<double>(0, 1));
for (int i = 0; i < 3; i++) {
_f_sto[i] = ran_gen();
}
}
void CConfiguration::calcMobilityForces(){
//calculate mobility forces from potential Epot - Unified version that includes 2ndOrder if k is larger than or equal 0.2 b , except if ranPot is activated.
double r_abs = 0;
double r_i = 0, r_k = 0;
double utmp = 0, frtmp = 0; //temporary "hilfsvariables"
double Epot = 0;
double z1, z2;
int hlpr = 1;
if (_ranU){
z1 = 1/4 * _boxsize;
z2 = _boxsize - z1; //z is in cylindrical coordinates. This indicates above/below which value the exp potential is modifed for random signs.
}
//reset mobility forces to zero
for (int l = 0; l < 3; l++) {
_f_mob[l] = 0;
}
const double r_c = _6root2 * _pradius; //cutoff for Lennard-Jones calculation (at minimum)
for (int i = 0; i < 3; i++){
int k = i + 1; //k always one direction "further", i.e. if i = 0 = x-direction, then k = 1 = y-direction
if ( k == 3 ) k = 0;
int plane = 3 - (i+k); //this is the current plane of the cylindrical coordinates
int n = 0; // reset counter for index of next rod in plane n = 0, 1, 2, 3 -> only needed for ranPot
if (_ranRod) hlpr =_rodvec[plane].size();
for (int nk = _min; nk < _max; nk++){
for (int ni = _min; ni < _max; ni++){
// if ranRod: hlpr = _rodvec[i].size(), else hlpr == 1
for (int irod=0;irod<hlpr;irod++){
if (!_ranRod){
r_i = _ppos[i] - ni*_boxsize;
r_k = _ppos[k] - nk*_boxsize;
//this is needed if we dont want the rods to cross each other to create a strong potential well
if (plane == 0){
r_i -= _rodDistance;
}
else if (plane == 1){
r_k -= _rodDistance;
r_i -= _rodDistance;
}
}
else{
r_i = _ppos[i] - _rodvec[plane][irod].coord[i];
r_k = _ppos[k] - _rodvec[plane][irod].coord[k];
assert ((_rodvec[plane][irod].coord[plane] == 0.) && "WARNING: Bad poly coordinate!\n"); //TODO debug
}
r_abs = sqrt(r_i * r_i + r_k * r_k); //distance to the rods
//if (r_abs < 0.9*_pradius) cout << "Small rho distace: " << r_abs << endl;
if (_potMod) calculateExpPotentialMOD(r_abs, utmp, frtmp, plane);
else calculateExpPotential(r_abs, utmp, frtmp);
if (_hpi) calculateExpHPI(r_abs, utmp, frtmp);
if (_ranU){
utmp = utmp * _poly.get_sign(plane, n);
frtmp = frtmp * _poly.get_sign(plane, n);
if (_ppos[plane] > z2){
if (! _poly.samesign(1, plane, n)){
_f_mob[plane] += utmp * 4 / _boxsize; //this takes care of the derivative of the potential modification and resulting force
modifyPot(utmp, frtmp, _boxsize - _ppos[plane]);
}
}
else if (_ppos[plane] < z1){
if (! _poly.samesign(-1, plane, n)){
_f_mob[plane] -= utmp * 4 / _boxsize; //this takes care of the derivative of the potential modification and resulting force
modifyPot(utmp, frtmp, _ppos[plane]);
}
}
n++; //index of next rod in curent plane
}
if (_LJPot && ( r_abs < r_c || _hpi )) calcLJPot(r_abs, utmp, frtmp);
Epot += utmp;
_f_mob[i] += frtmp * r_i;
_f_mob[k] += frtmp * r_k;
}
}
}
}
_upot = Epot;
}
void CConfiguration::saveXYZTraj(string name, const int& move, string a_w){
FILE *f = fopen(name.c_str(), a_w.c_str());
fprintf(f, "%d\n%s (%8.3f %8.3f %8.3f) t=%d \n", 1, "sim_name", _boxsize, _boxsize, _boxsize, move);
// polymer particles
fprintf(f, "%3s%9.3f%9.3f%9.3f \n","H", _ppos[0]+ _boxsize *_boxnumberXYZ[0], _ppos[1]+ _boxsize *_boxnumberXYZ[1], _ppos[2]+ _boxsize *_boxnumberXYZ[2]); // absolute position
// fprintf(f, "%3s%9.3f%9.3f%9.3f \n","H", _ppos[0], _ppos[1], _ppos[2]); // relative position in box
fclose(f);
}
void CConfiguration::setRanNumberGen(double seed){
if (seed == 0) {
m_igen = new boost::mt19937(static_cast<unsigned int>(time(NULL)));
cout << "random seed is time!" << endl;
} else {
m_igen = new boost::mt19937(static_cast<unsigned int>(seed));
cout << "random seed is " << seed << endl;
}
}
double CConfiguration::getPosVariance(){
//function to return the variance, to save it as an instant value
double var = 0;
for (int m = 0; m < 3; m++){ //calculate (r_m - r_0)^2|_i
var += pow((_ppos[m] + _boxsize *_boxnumberXYZ[m] - _startpos[m]) , 2);
}
return var;
}
double CConfiguration::get1DPosVariance(int dim){
//function to return the variance of just one dimension x = 0, y = 1 or z = 2
return pow((_ppos[dim] + _boxsize *_boxnumberXYZ[dim] - _startpos[dim]) , 2);
}
bool CConfiguration::checkFirstPassage(double mfpPos, int dim){ //TODO
//function returns true if in the last step the momentary "way point" (_mfpIndex * xinterval) has been crossed in dim-direction
// where dim = 0, 1, 2 -> x, y, z. Otherwise it returns false
if ((_ppos[dim] + _boxsize*_boxnumberXYZ[dim] - _startpos[dim]) > (mfpPos) ) return true;
else return false;
}
void CConfiguration::moveBack(){
//moves particle back to previous position
for (int i = 0; i < 3; i++) {_ppos[i] = _prevpos[i];}
}
//****************************POTENTIALS**********************************************************
void CConfiguration::calculateExpPotential(const double r, double& U, double& Fr){
//function to calculate an exponential Potential U = U_0 * exp(-1 * r * k)
// k is the interaction range. U_0 is the strength of the potential
//which is attractive if direction = -1, and repulsive if direction = 1
//The potential is weighted with kT!
U = _potStrength * exp(-1 * r / _potRange);
Fr = U / (_potRange * r); //This is the force divided by the distance to the rod!
}
void CConfiguration::calculateExpHPI(const double r, double& U, double& Fr){
double u = _hpi_u * exp( - r / _hpi_k);
U += u;
Fr += u / (_hpi_k * r);
}
/*void CConfiguration::calculateDHPotential(const double r, double& U, double& Fr){
//function to calculate an exponential Potential U = U_0 * exp(-1 * r * k)
// k is the interaction range. U_0 is the strength of the potential
//which is attractive if direction = -1, and repulsive if direction = 1
//The potential is weighted with kT!
U = _potStrength * exp(-1 * r / _potRange) / r;
Fr = U * (1/_potRange + 1/r) / r; //This is the force divided by the distance to the rod!
}
*/
void CConfiguration::calculateExpPotentialMOD(const double r, double& U, double& Fr, int plane){
//function to calculate an exponential Potential U = U_0 * exp(-1 * r * k)
// k is the interaction range. U_0 is the strength of the potential
//which is attractive if direction = -1, and repulsive if direction = 1
//The potential is weighted with kT!
//index, is the dimension (x, y or z) which is normal to the plane that the potential is being calculated in.
// U = _potStrength * exp(-1 * r / _potRange) * ( 1 - 2 / 3 * pow((2*_ppos[3-index]/_boxsize - 1), 2));
U = _potStrength * exp( -r / _potRange) * (1 - abs(2 * _ppos[plane]/_boxsize - 1) * 0.66667);
Fr = U / (_potRange * r); //This is the force divided by the distance to the rod!
//DEBYE!!!
// U = _potStrength * exp(-1 * r / _potRange) / r;
// Fr = U * (1/_potRange + 1/r) / r; //This is the force divided by the distance to the rod!
// BESSEL
// U = 2 * _potStrength * boost::math::cyl_bessel_k(0, r / _potRange);
// Fr = 2 * _potStrength * boost::math::cyl_bessel_k(1, r / _potRange) / (_potRange * r);
}
void CConfiguration::modifyPot(double& U, double& Fr, double dist){
//function to modify the potential according to the distance along the polymer axis to the next neighbor,
//in case the next neighboring polymer part is of opposite sign
U = U * 4 * dist/_boxsize;
Fr = Fr * 4 * dist/_boxsize;
}
//****************************STERIC HINDRANCE****************************************************//
bool CConfiguration::testOverlap(){
//Function to check, whether the diffusing particle of size psize is overlapping with any one of the rods (edges of the box)
//most if borrowed from moveParticleAndWatch()
bool overlaps = false;
double r_i = 0, r_k = 0;
double r_abs = 0;
double L1[] = {0, _boxsize, 0, _boxsize}; //these two arrays are only needed for the iteration.
double L2[] = {0, 0, _boxsize, _boxsize};
for (int i = 0; i < 2; i++){
for (int k = i+1; k < 3; k++){
for (int n = 0; n < 4; n++){
r_i = _ppos[i] - L1[n];
r_k = _ppos[k] - L2[n];
//this is needed if we dont want the rods to cross each other to create a strong potential well
if (k == 2){
r_i -= _rodDistance;
if (i == 0) r_k -= _rodDistance;
}
r_abs = sqrt(r_i * r_i + r_k * r_k); //distance to the rods
if (r_abs < _pradius) overlaps = true;
}
}
}
return overlaps;
}
void CConfiguration::calcLJPot(const double r, double& U, double& Fr){
//Function to calculate the Lennard-Jones Potential
double por6 = pow((_pradius / r ), 6); //por6 stands for "p over r to the power of 6" . The 2 comes from the fact, that I need the particle radius, not the particle size
U += 4 * ( por6*por6 - por6 + 0.25 );
Fr += 24 / ( r * r ) * ( 2 * por6*por6 - por6 );
}
//****************************POS HISTOGRAM****************************************************//
void CConfiguration::initPosHisto(){
_posHistoM.resize(100);
for (int i = 0; i < 100; i++){
_posHistoM[i].resize(100);
for (int j = 0; j < 100; j++){
_posHistoM[i][j].resize(100, 0); //initialize all the 100*100*100 matrix elements to zero!
}
}
}
void CConfiguration::addHistoValue(){
//adds a value to the position histogram
int x = _ppos[0] / _boxsize * 100; //CAREFUL: THIS CAN'T BE DONE AT A POINT WHERE X MIGHT BE ZERO!!!
int y = _ppos[1] / _boxsize * 100;
int z = _ppos[2] / _boxsize * 100;
if ((x < 0) || (y < 0) || (z < 0) || (x > 99) || (y > 99) || (z > 99)){
cout << "The Position Histogram function 'conf.addHisto()' is in a bad place, since there is a negative position _ppos[]" << endl;
cout << "The current position is: " << _ppos[0] << " " << _ppos[1] << " " << _ppos[2] << endl;
}
_posHistoM[x][y][z] += 1;
}
void CConfiguration::printHistoMatrix(string folder){
//function to print the positionHistogram to a file called posHistoMatrix.txt
//The elements of the matrix are M[x][y][z]. First the z is counted from 1 to 100 in one row, then the y follows, then, after the 'X', the 100 x elements.
ofstream matrixfile;
matrixfile.open((folder + "/InstantValues/posHistoMatrix.txt").c_str());
int maxval = 0;
for (int i = 0; i < 100; i++){
for (int j = 0; j < 100; j++){
for (int k = 0; k < 100; k++){
matrixfile << _posHistoM[i][j][k] << " ";
if (maxval < _posHistoM[i][j][k] ) maxval = _posHistoM[i][j][k];
}
matrixfile << endl;
}
matrixfile << "X" << endl;
}
matrixfile << "MaxVal " << maxval; //THis does not affect the grid files, since data is only copied to them when a "X" line comes!
matrixfile.close();
}
//****************************OLD CODE****************************************************
void CConfiguration::calc_1RODONLY_MobilityForces(){ //see OldCode
}
void CConfiguration::calc_ZRODONLY_MobilityForces(){ //see OldCode
}
void CConfiguration::calc_YZRODONLY_MobilityForces(){ //see OldCode
}
double CConfiguration::getDisplacement(){
double d = 0;
for (int i = 0; i < 3; i++){
d += pow((_timestep * _f_mob[i] + _mu_sto * _f_sto[i]), 2);
}
return sqrt(d);
}
/*
void CConfiguration::calcMobilityForces(){
//calculate mobility forces from potential Epot
double r_abs = 0;
double r_i = 0, r_k = 0;
double L1[] = {0, _boxsize, 0, _boxsize}; //these two arrays are only needed for the iteration.
double L2[] = {0, 0, _boxsize, _boxsize};
double utmp = 0, frtmp = 0; //temporary "hilfsvariables"
double Epot = 0;
double z1, z2;
if (_ranU){
z1 = 1/4 * _boxsize;
z2 = _boxsize - z1; //z is in cylindrical coordinates. This indicates above/below which value the exp potential is modifed for random signs.
}
//reset mobility forces to zero
for (int l = 0; l < 3; l++) {
_f_mob[l] = 0;
}
const double r_c = _6root2 * _pradius; //cutoff for Lennard-Jones calculation (at minimum)
for (int i = 0; i < 3; i++){
int k = i + 1; //k always one direction "further", i.e. if i = 0 = x-direction, then k = 1 = y-direction
if ( k == 3 ) k = 0;
int plane = 3 - (i+k); //this is the current plane of the cylindrical coordinates
for (int n = 0; n < 4; n++){
r_i = _ppos[i] - L1[n];
r_k = _ppos[k] - L2[n];
//this is needed if we dont want the rods to cross each other to create a strong potential well
if (plane == 0){
r_i -= _rodDistance;
}
else if (plane == 1){
r_k -= _rodDistance;
r_i -= _rodDistance;
}
r_abs = sqrt(r_i * r_i + r_k * r_k); //distance to the rods
if (_potMod) calculateExpPotentialMOD(r_abs, utmp, frtmp, plane);
else calculateExpPotential(r_abs, utmp, frtmp);
if (_ranU){
utmp = utmp * _poly.get_sign(plane, n);
frtmp = frtmp * _poly.get_sign(plane, n);
if (_ppos[plane] > z2){
if (! _poly.samesign(1, plane, n)){
modifyPot(utmp, frtmp, _boxsize - _ppos[plane]);
_f_mob[plane] += utmp * 4 / _boxsize; //this takes care of the derivative of the potential modification and resulting force
}
}
else if (_ppos[plane] < z1){
if (! _poly.samesign(-1, plane, n)){
modifyPot(utmp, frtmp, _ppos[plane]);
_f_mob[plane] -= utmp * 4 / _boxsize; //this takes care of the derivative of the potential modification and resulting force
}
}
}
if (_LJPot && ( r_abs < r_c )) calcLJPot(r_abs, utmp, frtmp);
Epot += utmp;
_f_mob[i] += frtmp * r_i;
_f_mob[k] += frtmp * r_k;
}
}
_upot = Epot;
}
void CConfiguration::calcMobilityForces(){
//calculate mobility forces from potential Epot
double r_abs = 0;
double r_i = 0, r_k = 0;
double utmp = 0, frtmp = 0; //temporary "hilfsvariables"
double Epot = 0;
//reset mobility forces to zero
for (int l = 0; l < 3; l++) {
_f_mob[l] = 0;
}
const double r_c = _6root2 * _pradius; //cutoff for Lennard-Jones calculation (at minimum)
for (int i = 0; i < 3; i++){
int k = i + 1; //k always one direction "further", i.e. if i = 0 = x-direction, then k = 1 = y-direction
if ( k == 3 ) k = 0;
int plane = 3 - (i+k); //this is the current plane of the cylindrical coordinates
for (int ni = -1; ni < 3; ni++){
for (int nk = -1; nk < 3; nk++){
r_i = _ppos[i] - ni*_boxsize;
r_k = _ppos[k] - nk*_boxsize;
r_abs = sqrt(r_i * r_i + r_k * r_k); //distance to the rods
if (_potMod) calculateExpPotentialMOD(r_abs, utmp, frtmp, plane);
else calculateExpPotential(r_abs, utmp, frtmp);
if (_LJPot && ( r_abs < r_c )) calcLJPot(r_abs, utmp, frtmp);
Epot += utmp;
_f_mob[i] += frtmp * r_i;
_f_mob[k] += frtmp * r_k;
}
}
}
_upot = Epot;
}
*/