ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 1178
Committed: Thu May 13 21:08:05 2004 UTC (20 years, 11 months ago) by gezelter
File size: 18195 byte(s)
Log Message:
fixes for skip list

File Contents

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