ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 892
Committed: Mon Dec 22 21:27:04 2003 UTC (20 years, 6 months ago) by chuckv
File size: 17393 byte(s)
Log Message:
Fixes to profile code.

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