ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-1.0/libmdtools/Integrator.cpp
Revision: 1334
Committed: Fri Jul 16 18:58:03 2004 UTC (19 years, 11 months ago) by gezelter
File size: 18661 byte(s)
Log Message:
Initial import of OOPSE-1.0 source tree

File Contents

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