ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 726
Committed: Tue Aug 26 20:37:30 2003 UTC (20 years, 10 months ago) by tim
File size: 16256 byte(s)
Log Message:
set default force substraction policy to PolicyByMass

File Contents

# Content
1 #include <iostream>
2 #include <cstdlib>
3 #include <cmath>
4
5 #ifdef IS_MPI
6 #include "mpiSimulation.hpp"
7 #include <unistd.h>
8 #endif //is_mpi
9
10 #include "Integrator.hpp"
11 #include "simError.h"
12
13
14 template<typename T> Integrator<T>::Integrator(SimInfo* theInfo,
15 ForceFields* the_ff){
16 info = theInfo;
17 myFF = the_ff;
18 isFirst = 1;
19
20 molecules = info->molecules;
21 nMols = info->n_mol;
22
23 // give a little love back to the SimInfo object
24
25 if (info->the_integrator != NULL){
26 delete info->the_integrator;
27 }
28 info->the_integrator = this;
29
30 nAtoms = info->n_atoms;
31
32 // check for constraints
33
34 constrainedA = NULL;
35 constrainedB = NULL;
36 constrainedDsqr = NULL;
37 moving = NULL;
38 moved = NULL;
39 oldPos = NULL;
40
41 nConstrained = 0;
42
43 checkConstraints();
44 }
45
46 template<typename T> Integrator<T>::~Integrator(){
47 if (nConstrained){
48 delete[] constrainedA;
49 delete[] constrainedB;
50 delete[] constrainedDsqr;
51 delete[] moving;
52 delete[] moved;
53 delete[] oldPos;
54 }
55 }
56
57 template<typename T> void Integrator<T>::checkConstraints(void){
58 isConstrained = 0;
59
60 Constraint* temp_con;
61 Constraint* dummy_plug;
62 temp_con = new Constraint[info->n_SRI];
63 nConstrained = 0;
64 int constrained = 0;
65
66 SRI** theArray;
67 for (int i = 0; i < nMols; i++){
68 theArray = (SRI * *) molecules[i].getMyBonds();
69 for (int j = 0; j < molecules[i].getNBonds(); j++){
70 constrained = theArray[j]->is_constrained();
71
72 if (constrained){
73 dummy_plug = theArray[j]->get_constraint();
74 temp_con[nConstrained].set_a(dummy_plug->get_a());
75 temp_con[nConstrained].set_b(dummy_plug->get_b());
76 temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr());
77
78 nConstrained++;
79 constrained = 0;
80 }
81 }
82
83 theArray = (SRI * *) molecules[i].getMyBends();
84 for (int j = 0; j < molecules[i].getNBends(); j++){
85 constrained = theArray[j]->is_constrained();
86
87 if (constrained){
88 dummy_plug = theArray[j]->get_constraint();
89 temp_con[nConstrained].set_a(dummy_plug->get_a());
90 temp_con[nConstrained].set_b(dummy_plug->get_b());
91 temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr());
92
93 nConstrained++;
94 constrained = 0;
95 }
96 }
97
98 theArray = (SRI * *) molecules[i].getMyTorsions();
99 for (int j = 0; j < molecules[i].getNTorsions(); j++){
100 constrained = theArray[j]->is_constrained();
101
102 if (constrained){
103 dummy_plug = theArray[j]->get_constraint();
104 temp_con[nConstrained].set_a(dummy_plug->get_a());
105 temp_con[nConstrained].set_b(dummy_plug->get_b());
106 temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr());
107
108 nConstrained++;
109 constrained = 0;
110 }
111 }
112 }
113
114 if (nConstrained > 0){
115 isConstrained = 1;
116
117 if (constrainedA != NULL)
118 delete[] constrainedA;
119 if (constrainedB != NULL)
120 delete[] constrainedB;
121 if (constrainedDsqr != NULL)
122 delete[] constrainedDsqr;
123
124 constrainedA = new int[nConstrained];
125 constrainedB = new int[nConstrained];
126 constrainedDsqr = new double[nConstrained];
127
128 for (int i = 0; i < nConstrained; i++){
129 constrainedA[i] = temp_con[i].get_a();
130 constrainedB[i] = temp_con[i].get_b();
131 constrainedDsqr[i] = temp_con[i].get_dsqr();
132 }
133
134
135 // save oldAtoms to check for lode balanceing later on.
136
137 oldAtoms = nAtoms;
138
139 moving = new int[nAtoms];
140 moved = new int[nAtoms];
141
142 oldPos = new double[nAtoms * 3];
143 }
144
145 delete[] temp_con;
146 }
147
148
149 template<typename T> void Integrator<T>::integrate(void){
150 int i, j; // loop counters
151
152 double runTime = info->run_time;
153 double sampleTime = info->sampleTime;
154 double statusTime = info->statusTime;
155 double thermalTime = info->thermalTime;
156
157 double currSample;
158 double currThermal;
159 double currStatus;
160
161 int calcPot, calcStress;
162 int isError;
163
164 tStats = new Thermo(info);
165 statOut = new StatWriter(info);
166 dumpOut = new DumpWriter(info);
167
168 atoms = info->atoms;
169 DirectionalAtom* dAtom;
170
171 dt = info->dt;
172 dt2 = 0.5 * dt;
173
174 // initialize the forces before the first step
175
176 calcForce(1, 1);
177 // myFF->doForces(1,1);
178
179 if (info->setTemp){
180 thermalize();
181 }
182
183 calcPot = 0;
184 calcStress = 0;
185 currSample = sampleTime;
186 currThermal = thermalTime;
187 currStatus = statusTime;
188
189 calcPot = 0;
190 calcStress = 0;
191 currSample = sampleTime + info->getTime();
192 currThermal = thermalTime+ info->getTime();
193 currStatus = statusTime + info->getTime();
194
195 dumpOut->writeDump(info->getTime());
196 statOut->writeStat(info->getTime());
197
198 readyCheck();
199
200 #ifdef IS_MPI
201 strcpy(checkPointMsg, "The integrator is ready to go.");
202 MPIcheckPoint();
203 #endif // is_mpi
204
205 while (info->getTime() < runTime){
206 if ((info->getTime() + dt) >= currStatus){
207 calcPot = 1;
208 calcStress = 1;
209 }
210
211 integrateStep(calcPot, calcStress);
212
213 info->incrTime(dt);
214
215 if (info->setTemp){
216 if (info->getTime() >= currThermal){
217 thermalize();
218 currThermal += thermalTime;
219 }
220 }
221
222 if (info->getTime() >= currSample){
223 dumpOut->writeDump(info->getTime());
224 currSample += sampleTime;
225 }
226
227 if (info->getTime() >= currStatus){
228 statOut->writeStat(info->getTime());
229 calcPot = 0;
230 calcStress = 0;
231 currStatus += statusTime;
232 }
233
234 #ifdef IS_MPI
235 strcpy(checkPointMsg, "successfully took a time step.");
236 MPIcheckPoint();
237 #endif // is_mpi
238 }
239
240 dumpOut->writeFinal(info->getTime());
241
242 delete dumpOut;
243 delete statOut;
244 }
245
246 template<typename T> void Integrator<T>::integrateStep(int calcPot,
247 int calcStress){
248 // Position full step, and velocity half step
249 preMove();
250
251 moveA();
252
253 if (nConstrained){
254 constrainA();
255 }
256
257
258 #ifdef IS_MPI
259 strcpy(checkPointMsg, "Succesful moveA\n");
260 MPIcheckPoint();
261 #endif // is_mpi
262
263
264 // calc forces
265
266 calcForce(calcPot, calcStress);
267
268 #ifdef IS_MPI
269 strcpy(checkPointMsg, "Succesful doForces\n");
270 MPIcheckPoint();
271 #endif // is_mpi
272
273
274 // finish the velocity half step
275
276 moveB();
277
278 if (nConstrained){
279 constrainB();
280 }
281
282 #ifdef IS_MPI
283 strcpy(checkPointMsg, "Succesful moveB\n");
284 MPIcheckPoint();
285 #endif // is_mpi
286 }
287
288
289 template<typename T> void Integrator<T>::moveA(void){
290 int i, j;
291 DirectionalAtom* dAtom;
292 double Tb[3], ji[3];
293 double A[3][3], I[3][3];
294 double angle;
295 double vel[3], pos[3], frc[3];
296 double mass;
297
298 for (i = 0; i < nAtoms; i++){
299 atoms[i]->getVel(vel);
300 atoms[i]->getPos(pos);
301 atoms[i]->getFrc(frc);
302
303 mass = atoms[i]->getMass();
304
305 for (j = 0; j < 3; j++){
306 // velocity half step
307 vel[j] += (dt2 * frc[j] / mass) * eConvert;
308 // position whole step
309 pos[j] += dt * vel[j];
310 }
311
312 atoms[i]->setVel(vel);
313 atoms[i]->setPos(pos);
314
315 if (atoms[i]->isDirectional()){
316 dAtom = (DirectionalAtom *) atoms[i];
317
318 // get and convert the torque to body frame
319
320 dAtom->getTrq(Tb);
321 dAtom->lab2Body(Tb);
322
323 // get the angular momentum, and propagate a half step
324
325 dAtom->getJ(ji);
326
327 for (j = 0; j < 3; j++)
328 ji[j] += (dt2 * Tb[j]) * eConvert;
329
330 // use the angular velocities to propagate the rotation matrix a
331 // full time step
332
333 dAtom->getA(A);
334 dAtom->getI(I);
335
336 // rotate about the x-axis
337 angle = dt2 * ji[0] / I[0][0];
338 this->rotate(1, 2, angle, ji, A);
339
340 // rotate about the y-axis
341 angle = dt2 * ji[1] / I[1][1];
342 this->rotate(2, 0, angle, ji, A);
343
344 // rotate about the z-axis
345 angle = dt * ji[2] / I[2][2];
346 this->rotate(0, 1, angle, ji, A);
347
348 // rotate about the y-axis
349 angle = dt2 * ji[1] / I[1][1];
350 this->rotate(2, 0, angle, ji, A);
351
352 // rotate about the x-axis
353 angle = dt2 * ji[0] / I[0][0];
354 this->rotate(1, 2, angle, ji, A);
355
356
357 dAtom->setJ(ji);
358 dAtom->setA(A);
359 }
360 }
361 }
362
363
364 template<typename T> void Integrator<T>::moveB(void){
365 int i, j;
366 DirectionalAtom* dAtom;
367 double Tb[3], ji[3];
368 double vel[3], frc[3];
369 double mass;
370
371 for (i = 0; i < nAtoms; i++){
372 atoms[i]->getVel(vel);
373 atoms[i]->getFrc(frc);
374
375 mass = atoms[i]->getMass();
376
377 // velocity half step
378 for (j = 0; j < 3; j++)
379 vel[j] += (dt2 * frc[j] / mass) * eConvert;
380
381 atoms[i]->setVel(vel);
382
383 if (atoms[i]->isDirectional()){
384 dAtom = (DirectionalAtom *) atoms[i];
385
386 // get and convert the torque to body frame
387
388 dAtom->getTrq(Tb);
389 dAtom->lab2Body(Tb);
390
391 // get the angular momentum, and propagate a half step
392
393 dAtom->getJ(ji);
394
395 for (j = 0; j < 3; j++)
396 ji[j] += (dt2 * Tb[j]) * eConvert;
397
398
399 dAtom->setJ(ji);
400 }
401 }
402 }
403
404 template<typename T> void Integrator<T>::preMove(void){
405 int i, j;
406 double pos[3];
407
408 if (nConstrained){
409 for (i = 0; i < nAtoms; i++){
410 atoms[i]->getPos(pos);
411
412 for (j = 0; j < 3; j++){
413 oldPos[3 * i + j] = pos[j];
414 }
415 }
416 }
417 }
418
419 template<typename T> void Integrator<T>::constrainA(){
420 int i, j, k;
421 int done;
422 double posA[3], posB[3];
423 double velA[3], velB[3];
424 double pab[3];
425 double rab[3];
426 int a, b, ax, ay, az, bx, by, bz;
427 double rma, rmb;
428 double dx, dy, dz;
429 double rpab;
430 double rabsq, pabsq, rpabsq;
431 double diffsq;
432 double gab;
433 int iteration;
434
435 for (i = 0; i < nAtoms; i++){
436 moving[i] = 0;
437 moved[i] = 1;
438 }
439
440 iteration = 0;
441 done = 0;
442 while (!done && (iteration < maxIteration)){
443 done = 1;
444 for (i = 0; i < nConstrained; i++){
445 a = constrainedA[i];
446 b = constrainedB[i];
447
448 ax = (a * 3) + 0;
449 ay = (a * 3) + 1;
450 az = (a * 3) + 2;
451
452 bx = (b * 3) + 0;
453 by = (b * 3) + 1;
454 bz = (b * 3) + 2;
455
456 if (moved[a] || moved[b]){
457 atoms[a]->getPos(posA);
458 atoms[b]->getPos(posB);
459
460 for (j = 0; j < 3; j++)
461 pab[j] = posA[j] - posB[j];
462
463 //periodic boundary condition
464
465 info->wrapVector(pab);
466
467 pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
468
469 rabsq = constrainedDsqr[i];
470 diffsq = rabsq - pabsq;
471
472 // the original rattle code from alan tidesley
473 if (fabs(diffsq) > (tol * rabsq * 2)){
474 rab[0] = oldPos[ax] - oldPos[bx];
475 rab[1] = oldPos[ay] - oldPos[by];
476 rab[2] = oldPos[az] - oldPos[bz];
477
478 info->wrapVector(rab);
479
480 rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
481
482 rpabsq = rpab * rpab;
483
484
485 if (rpabsq < (rabsq * -diffsq)){
486 #ifdef IS_MPI
487 a = atoms[a]->getGlobalIndex();
488 b = atoms[b]->getGlobalIndex();
489 #endif //is_mpi
490 sprintf(painCave.errMsg,
491 "Constraint failure in constrainA at atom %d and %d.\n", a,
492 b);
493 painCave.isFatal = 1;
494 simError();
495 }
496
497 rma = 1.0 / atoms[a]->getMass();
498 rmb = 1.0 / atoms[b]->getMass();
499
500 gab = diffsq / (2.0 * (rma + rmb) * rpab);
501
502 dx = rab[0] * gab;
503 dy = rab[1] * gab;
504 dz = rab[2] * gab;
505
506 posA[0] += rma * dx;
507 posA[1] += rma * dy;
508 posA[2] += rma * dz;
509
510 atoms[a]->setPos(posA);
511
512 posB[0] -= rmb * dx;
513 posB[1] -= rmb * dy;
514 posB[2] -= rmb * dz;
515
516 atoms[b]->setPos(posB);
517
518 dx = dx / dt;
519 dy = dy / dt;
520 dz = dz / dt;
521
522 atoms[a]->getVel(velA);
523
524 velA[0] += rma * dx;
525 velA[1] += rma * dy;
526 velA[2] += rma * dz;
527
528 atoms[a]->setVel(velA);
529
530 atoms[b]->getVel(velB);
531
532 velB[0] -= rmb * dx;
533 velB[1] -= rmb * dy;
534 velB[2] -= rmb * dz;
535
536 atoms[b]->setVel(velB);
537
538 moving[a] = 1;
539 moving[b] = 1;
540 done = 0;
541 }
542 }
543 }
544
545 for (i = 0; i < nAtoms; i++){
546 moved[i] = moving[i];
547 moving[i] = 0;
548 }
549
550 iteration++;
551 }
552
553 if (!done){
554 sprintf(painCave.errMsg,
555 "Constraint failure in constrainA, too many iterations: %d\n",
556 iteration);
557 painCave.isFatal = 1;
558 simError();
559 }
560 }
561
562 template<typename T> void Integrator<T>::constrainB(void){
563 int i, j, k;
564 int done;
565 double posA[3], posB[3];
566 double velA[3], velB[3];
567 double vxab, vyab, vzab;
568 double rab[3];
569 int a, b, ax, ay, az, bx, by, bz;
570 double rma, rmb;
571 double dx, dy, dz;
572 double rabsq, pabsq, rvab;
573 double diffsq;
574 double gab;
575 int iteration;
576
577 for (i = 0; i < nAtoms; i++){
578 moving[i] = 0;
579 moved[i] = 1;
580 }
581
582 done = 0;
583 iteration = 0;
584 while (!done && (iteration < maxIteration)){
585 done = 1;
586
587 for (i = 0; i < nConstrained; i++){
588 a = constrainedA[i];
589 b = constrainedB[i];
590
591 ax = (a * 3) + 0;
592 ay = (a * 3) + 1;
593 az = (a * 3) + 2;
594
595 bx = (b * 3) + 0;
596 by = (b * 3) + 1;
597 bz = (b * 3) + 2;
598
599 if (moved[a] || moved[b]){
600 atoms[a]->getVel(velA);
601 atoms[b]->getVel(velB);
602
603 vxab = velA[0] - velB[0];
604 vyab = velA[1] - velB[1];
605 vzab = velA[2] - velB[2];
606
607 atoms[a]->getPos(posA);
608 atoms[b]->getPos(posB);
609
610 for (j = 0; j < 3; j++)
611 rab[j] = posA[j] - posB[j];
612
613 info->wrapVector(rab);
614
615 rma = 1.0 / atoms[a]->getMass();
616 rmb = 1.0 / atoms[b]->getMass();
617
618 rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
619
620 gab = -rvab / ((rma + rmb) * constrainedDsqr[i]);
621
622 if (fabs(gab) > tol){
623 dx = rab[0] * gab;
624 dy = rab[1] * gab;
625 dz = rab[2] * gab;
626
627 velA[0] += rma * dx;
628 velA[1] += rma * dy;
629 velA[2] += rma * dz;
630
631 atoms[a]->setVel(velA);
632
633 velB[0] -= rmb * dx;
634 velB[1] -= rmb * dy;
635 velB[2] -= rmb * dz;
636
637 atoms[b]->setVel(velB);
638
639 moving[a] = 1;
640 moving[b] = 1;
641 done = 0;
642 }
643 }
644 }
645
646 for (i = 0; i < nAtoms; i++){
647 moved[i] = moving[i];
648 moving[i] = 0;
649 }
650
651 iteration++;
652 }
653
654 if (!done){
655 sprintf(painCave.errMsg,
656 "Constraint failure in constrainB, too many iterations: %d\n",
657 iteration);
658 painCave.isFatal = 1;
659 simError();
660 }
661 }
662
663 template<typename T> void Integrator<T>::rotate(int axes1, int axes2,
664 double angle, double ji[3],
665 double A[3][3]){
666 int i, j, k;
667 double sinAngle;
668 double cosAngle;
669 double angleSqr;
670 double angleSqrOver4;
671 double top, bottom;
672 double rot[3][3];
673 double tempA[3][3];
674 double tempJ[3];
675
676 // initialize the tempA
677
678 for (i = 0; i < 3; i++){
679 for (j = 0; j < 3; j++){
680 tempA[j][i] = A[i][j];
681 }
682 }
683
684 // initialize the tempJ
685
686 for (i = 0; i < 3; i++)
687 tempJ[i] = ji[i];
688
689 // initalize rot as a unit matrix
690
691 rot[0][0] = 1.0;
692 rot[0][1] = 0.0;
693 rot[0][2] = 0.0;
694
695 rot[1][0] = 0.0;
696 rot[1][1] = 1.0;
697 rot[1][2] = 0.0;
698
699 rot[2][0] = 0.0;
700 rot[2][1] = 0.0;
701 rot[2][2] = 1.0;
702
703 // use a small angle aproximation for sin and cosine
704
705 angleSqr = angle * angle;
706 angleSqrOver4 = angleSqr / 4.0;
707 top = 1.0 - angleSqrOver4;
708 bottom = 1.0 + angleSqrOver4;
709
710 cosAngle = top / bottom;
711 sinAngle = angle / bottom;
712
713 rot[axes1][axes1] = cosAngle;
714 rot[axes2][axes2] = cosAngle;
715
716 rot[axes1][axes2] = sinAngle;
717 rot[axes2][axes1] = -sinAngle;
718
719 // rotate the momentum acoording to: ji[] = rot[][] * ji[]
720
721 for (i = 0; i < 3; i++){
722 ji[i] = 0.0;
723 for (k = 0; k < 3; k++){
724 ji[i] += rot[i][k] * tempJ[k];
725 }
726 }
727
728 // rotate the Rotation matrix acording to:
729 // A[][] = A[][] * transpose(rot[][])
730
731
732 // NOte for as yet unknown reason, we are performing the
733 // calculation as:
734 // transpose(A[][]) = transpose(A[][]) * transpose(rot[][])
735
736 for (i = 0; i < 3; i++){
737 for (j = 0; j < 3; j++){
738 A[j][i] = 0.0;
739 for (k = 0; k < 3; k++){
740 A[j][i] += tempA[i][k] * rot[j][k];
741 }
742 }
743 }
744 }
745
746 template<typename T> void Integrator<T>::calcForce(int calcPot, int calcStress){
747 myFF->doForces(calcPot, calcStress);
748 }
749
750 template<typename T> void Integrator<T>::thermalize(){
751 tStats->velocitize();
752 }