ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 733
Committed: Wed Aug 27 19:23:29 2003 UTC (20 years, 10 months ago) by tim
File size: 16232 byte(s)
Log Message:
fix bug of MPI_Allreduce in ZConstraint, the MPITYPE is set to MPI_DOUBLE,
however, the corret type is MPI_INT. Therefore, when we turn on the
optimization flag, it causes a seg fault

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
178 if (info->setTemp){
179 thermalize();
180 }
181
182 calcPot = 0;
183 calcStress = 0;
184 currSample = sampleTime;
185 currThermal = thermalTime;
186 currStatus = statusTime;
187
188 calcPot = 0;
189 calcStress = 0;
190 currSample = sampleTime + info->getTime();
191 currThermal = thermalTime+ info->getTime();
192 currStatus = statusTime + info->getTime();
193
194 dumpOut->writeDump(info->getTime());
195 statOut->writeStat(info->getTime());
196
197 readyCheck();
198
199 #ifdef IS_MPI
200 strcpy(checkPointMsg, "The integrator is ready to go.");
201 MPIcheckPoint();
202 #endif // is_mpi
203
204 while (info->getTime() < runTime){
205 if ((info->getTime() + dt) >= currStatus){
206 calcPot = 1;
207 calcStress = 1;
208 }
209
210 integrateStep(calcPot, calcStress);
211
212 info->incrTime(dt);
213
214 if (info->setTemp){
215 if (info->getTime() >= currThermal){
216 thermalize();
217 currThermal += thermalTime;
218 }
219 }
220
221 if (info->getTime() >= currSample){
222 dumpOut->writeDump(info->getTime());
223 currSample += sampleTime;
224 }
225
226 if (info->getTime() >= currStatus){
227 statOut->writeStat(info->getTime());
228 calcPot = 0;
229 calcStress = 0;
230 currStatus += statusTime;
231 }
232
233 #ifdef IS_MPI
234 strcpy(checkPointMsg, "successfully took a time step.");
235 MPIcheckPoint();
236 #endif // is_mpi
237 }
238
239 dumpOut->writeFinal(info->getTime());
240
241 delete dumpOut;
242 delete statOut;
243 }
244
245 template<typename T> void Integrator<T>::integrateStep(int calcPot,
246 int calcStress){
247 // Position full step, and velocity half step
248 preMove();
249
250 moveA();
251
252 if (nConstrained){
253 constrainA();
254 }
255
256
257 #ifdef IS_MPI
258 strcpy(checkPointMsg, "Succesful moveA\n");
259 MPIcheckPoint();
260 #endif // is_mpi
261
262
263 // calc forces
264
265 calcForce(calcPot, calcStress);
266
267 #ifdef IS_MPI
268 strcpy(checkPointMsg, "Succesful doForces\n");
269 MPIcheckPoint();
270 #endif // is_mpi
271
272
273 // finish the velocity half step
274
275 moveB();
276
277 if (nConstrained){
278 constrainB();
279 }
280
281 #ifdef IS_MPI
282 strcpy(checkPointMsg, "Succesful moveB\n");
283 MPIcheckPoint();
284 #endif // is_mpi
285 }
286
287
288 template<typename T> void Integrator<T>::moveA(void){
289 int i, j;
290 DirectionalAtom* dAtom;
291 double Tb[3], ji[3];
292 double A[3][3], I[3][3];
293 double angle;
294 double vel[3], pos[3], frc[3];
295 double mass;
296
297 for (i = 0; i < nAtoms; i++){
298 atoms[i]->getVel(vel);
299 atoms[i]->getPos(pos);
300 atoms[i]->getFrc(frc);
301
302 mass = atoms[i]->getMass();
303
304 for (j = 0; j < 3; j++){
305 // velocity half step
306 vel[j] += (dt2 * frc[j] / mass) * eConvert;
307 // position whole step
308 pos[j] += dt * vel[j];
309 }
310
311 atoms[i]->setVel(vel);
312 atoms[i]->setPos(pos);
313
314 if (atoms[i]->isDirectional()){
315 dAtom = (DirectionalAtom *) atoms[i];
316
317 // get and convert the torque to body frame
318
319 dAtom->getTrq(Tb);
320 dAtom->lab2Body(Tb);
321
322 // get the angular momentum, and propagate a half step
323
324 dAtom->getJ(ji);
325
326 for (j = 0; j < 3; j++)
327 ji[j] += (dt2 * Tb[j]) * eConvert;
328
329 // use the angular velocities to propagate the rotation matrix a
330 // full time step
331
332 dAtom->getA(A);
333 dAtom->getI(I);
334
335 // rotate about the x-axis
336 angle = dt2 * ji[0] / I[0][0];
337 this->rotate(1, 2, angle, ji, A);
338
339 // rotate about the y-axis
340 angle = dt2 * ji[1] / I[1][1];
341 this->rotate(2, 0, angle, ji, A);
342
343 // rotate about the z-axis
344 angle = dt * ji[2] / I[2][2];
345 this->rotate(0, 1, angle, ji, A);
346
347 // rotate about the y-axis
348 angle = dt2 * ji[1] / I[1][1];
349 this->rotate(2, 0, angle, ji, A);
350
351 // rotate about the x-axis
352 angle = dt2 * ji[0] / I[0][0];
353 this->rotate(1, 2, angle, ji, A);
354
355
356 dAtom->setJ(ji);
357 dAtom->setA(A);
358 }
359 }
360 }
361
362
363 template<typename T> void Integrator<T>::moveB(void){
364 int i, j;
365 DirectionalAtom* dAtom;
366 double Tb[3], ji[3];
367 double vel[3], frc[3];
368 double mass;
369
370 for (i = 0; i < nAtoms; i++){
371 atoms[i]->getVel(vel);
372 atoms[i]->getFrc(frc);
373
374 mass = atoms[i]->getMass();
375
376 // velocity half step
377 for (j = 0; j < 3; j++)
378 vel[j] += (dt2 * frc[j] / mass) * eConvert;
379
380 atoms[i]->setVel(vel);
381
382 if (atoms[i]->isDirectional()){
383 dAtom = (DirectionalAtom *) atoms[i];
384
385 // get and convert the torque to body frame
386
387 dAtom->getTrq(Tb);
388 dAtom->lab2Body(Tb);
389
390 // get the angular momentum, and propagate a half step
391
392 dAtom->getJ(ji);
393
394 for (j = 0; j < 3; j++)
395 ji[j] += (dt2 * Tb[j]) * eConvert;
396
397
398 dAtom->setJ(ji);
399 }
400 }
401 }
402
403 template<typename T> void Integrator<T>::preMove(void){
404 int i, j;
405 double pos[3];
406
407 if (nConstrained){
408 for (i = 0; i < nAtoms; i++){
409 atoms[i]->getPos(pos);
410
411 for (j = 0; j < 3; j++){
412 oldPos[3 * i + j] = pos[j];
413 }
414 }
415 }
416 }
417
418 template<typename T> void Integrator<T>::constrainA(){
419 int i, j, k;
420 int done;
421 double posA[3], posB[3];
422 double velA[3], velB[3];
423 double pab[3];
424 double rab[3];
425 int a, b, ax, ay, az, bx, by, bz;
426 double rma, rmb;
427 double dx, dy, dz;
428 double rpab;
429 double rabsq, pabsq, rpabsq;
430 double diffsq;
431 double gab;
432 int iteration;
433
434 for (i = 0; i < nAtoms; i++){
435 moving[i] = 0;
436 moved[i] = 1;
437 }
438
439 iteration = 0;
440 done = 0;
441 while (!done && (iteration < maxIteration)){
442 done = 1;
443 for (i = 0; i < nConstrained; i++){
444 a = constrainedA[i];
445 b = constrainedB[i];
446
447 ax = (a * 3) + 0;
448 ay = (a * 3) + 1;
449 az = (a * 3) + 2;
450
451 bx = (b * 3) + 0;
452 by = (b * 3) + 1;
453 bz = (b * 3) + 2;
454
455 if (moved[a] || moved[b]){
456 atoms[a]->getPos(posA);
457 atoms[b]->getPos(posB);
458
459 for (j = 0; j < 3; j++)
460 pab[j] = posA[j] - posB[j];
461
462 //periodic boundary condition
463
464 info->wrapVector(pab);
465
466 pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
467
468 rabsq = constrainedDsqr[i];
469 diffsq = rabsq - pabsq;
470
471 // the original rattle code from alan tidesley
472 if (fabs(diffsq) > (tol * rabsq * 2)){
473 rab[0] = oldPos[ax] - oldPos[bx];
474 rab[1] = oldPos[ay] - oldPos[by];
475 rab[2] = oldPos[az] - oldPos[bz];
476
477 info->wrapVector(rab);
478
479 rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
480
481 rpabsq = rpab * rpab;
482
483
484 if (rpabsq < (rabsq * -diffsq)){
485 #ifdef IS_MPI
486 a = atoms[a]->getGlobalIndex();
487 b = atoms[b]->getGlobalIndex();
488 #endif //is_mpi
489 sprintf(painCave.errMsg,
490 "Constraint failure in constrainA at atom %d and %d.\n", a,
491 b);
492 painCave.isFatal = 1;
493 simError();
494 }
495
496 rma = 1.0 / atoms[a]->getMass();
497 rmb = 1.0 / atoms[b]->getMass();
498
499 gab = diffsq / (2.0 * (rma + rmb) * rpab);
500
501 dx = rab[0] * gab;
502 dy = rab[1] * gab;
503 dz = rab[2] * gab;
504
505 posA[0] += rma * dx;
506 posA[1] += rma * dy;
507 posA[2] += rma * dz;
508
509 atoms[a]->setPos(posA);
510
511 posB[0] -= rmb * dx;
512 posB[1] -= rmb * dy;
513 posB[2] -= rmb * dz;
514
515 atoms[b]->setPos(posB);
516
517 dx = dx / dt;
518 dy = dy / dt;
519 dz = dz / dt;
520
521 atoms[a]->getVel(velA);
522
523 velA[0] += rma * dx;
524 velA[1] += rma * dy;
525 velA[2] += rma * dz;
526
527 atoms[a]->setVel(velA);
528
529 atoms[b]->getVel(velB);
530
531 velB[0] -= rmb * dx;
532 velB[1] -= rmb * dy;
533 velB[2] -= rmb * dz;
534
535 atoms[b]->setVel(velB);
536
537 moving[a] = 1;
538 moving[b] = 1;
539 done = 0;
540 }
541 }
542 }
543
544 for (i = 0; i < nAtoms; i++){
545 moved[i] = moving[i];
546 moving[i] = 0;
547 }
548
549 iteration++;
550 }
551
552 if (!done){
553 sprintf(painCave.errMsg,
554 "Constraint failure in constrainA, too many iterations: %d\n",
555 iteration);
556 painCave.isFatal = 1;
557 simError();
558 }
559 }
560
561 template<typename T> void Integrator<T>::constrainB(void){
562 int i, j, k;
563 int done;
564 double posA[3], posB[3];
565 double velA[3], velB[3];
566 double vxab, vyab, vzab;
567 double rab[3];
568 int a, b, ax, ay, az, bx, by, bz;
569 double rma, rmb;
570 double dx, dy, dz;
571 double rabsq, pabsq, rvab;
572 double diffsq;
573 double gab;
574 int iteration;
575
576 for (i = 0; i < nAtoms; i++){
577 moving[i] = 0;
578 moved[i] = 1;
579 }
580
581 done = 0;
582 iteration = 0;
583 while (!done && (iteration < maxIteration)){
584 done = 1;
585
586 for (i = 0; i < nConstrained; i++){
587 a = constrainedA[i];
588 b = constrainedB[i];
589
590 ax = (a * 3) + 0;
591 ay = (a * 3) + 1;
592 az = (a * 3) + 2;
593
594 bx = (b * 3) + 0;
595 by = (b * 3) + 1;
596 bz = (b * 3) + 2;
597
598 if (moved[a] || moved[b]){
599 atoms[a]->getVel(velA);
600 atoms[b]->getVel(velB);
601
602 vxab = velA[0] - velB[0];
603 vyab = velA[1] - velB[1];
604 vzab = velA[2] - velB[2];
605
606 atoms[a]->getPos(posA);
607 atoms[b]->getPos(posB);
608
609 for (j = 0; j < 3; j++)
610 rab[j] = posA[j] - posB[j];
611
612 info->wrapVector(rab);
613
614 rma = 1.0 / atoms[a]->getMass();
615 rmb = 1.0 / atoms[b]->getMass();
616
617 rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
618
619 gab = -rvab / ((rma + rmb) * constrainedDsqr[i]);
620
621 if (fabs(gab) > tol){
622 dx = rab[0] * gab;
623 dy = rab[1] * gab;
624 dz = rab[2] * gab;
625
626 velA[0] += rma * dx;
627 velA[1] += rma * dy;
628 velA[2] += rma * dz;
629
630 atoms[a]->setVel(velA);
631
632 velB[0] -= rmb * dx;
633 velB[1] -= rmb * dy;
634 velB[2] -= rmb * dz;
635
636 atoms[b]->setVel(velB);
637
638 moving[a] = 1;
639 moving[b] = 1;
640 done = 0;
641 }
642 }
643 }
644
645 for (i = 0; i < nAtoms; i++){
646 moved[i] = moving[i];
647 moving[i] = 0;
648 }
649
650 iteration++;
651 }
652
653 if (!done){
654 sprintf(painCave.errMsg,
655 "Constraint failure in constrainB, too many iterations: %d\n",
656 iteration);
657 painCave.isFatal = 1;
658 simError();
659 }
660 }
661
662 template<typename T> void Integrator<T>::rotate(int axes1, int axes2,
663 double angle, double ji[3],
664 double A[3][3]){
665 int i, j, k;
666 double sinAngle;
667 double cosAngle;
668 double angleSqr;
669 double angleSqrOver4;
670 double top, bottom;
671 double rot[3][3];
672 double tempA[3][3];
673 double tempJ[3];
674
675 // initialize the tempA
676
677 for (i = 0; i < 3; i++){
678 for (j = 0; j < 3; j++){
679 tempA[j][i] = A[i][j];
680 }
681 }
682
683 // initialize the tempJ
684
685 for (i = 0; i < 3; i++)
686 tempJ[i] = ji[i];
687
688 // initalize rot as a unit matrix
689
690 rot[0][0] = 1.0;
691 rot[0][1] = 0.0;
692 rot[0][2] = 0.0;
693
694 rot[1][0] = 0.0;
695 rot[1][1] = 1.0;
696 rot[1][2] = 0.0;
697
698 rot[2][0] = 0.0;
699 rot[2][1] = 0.0;
700 rot[2][2] = 1.0;
701
702 // use a small angle aproximation for sin and cosine
703
704 angleSqr = angle * angle;
705 angleSqrOver4 = angleSqr / 4.0;
706 top = 1.0 - angleSqrOver4;
707 bottom = 1.0 + angleSqrOver4;
708
709 cosAngle = top / bottom;
710 sinAngle = angle / bottom;
711
712 rot[axes1][axes1] = cosAngle;
713 rot[axes2][axes2] = cosAngle;
714
715 rot[axes1][axes2] = sinAngle;
716 rot[axes2][axes1] = -sinAngle;
717
718 // rotate the momentum acoording to: ji[] = rot[][] * ji[]
719
720 for (i = 0; i < 3; i++){
721 ji[i] = 0.0;
722 for (k = 0; k < 3; k++){
723 ji[i] += rot[i][k] * tempJ[k];
724 }
725 }
726
727 // rotate the Rotation matrix acording to:
728 // A[][] = A[][] * transpose(rot[][])
729
730
731 // NOte for as yet unknown reason, we are performing the
732 // calculation as:
733 // transpose(A[][]) = transpose(A[][]) * transpose(rot[][])
734
735 for (i = 0; i < 3; i++){
736 for (j = 0; j < 3; j++){
737 A[j][i] = 0.0;
738 for (k = 0; k < 3; k++){
739 A[j][i] += tempA[i][k] * rot[j][k];
740 }
741 }
742 }
743 }
744
745 template<typename T> void Integrator<T>::calcForce(int calcPot, int calcStress){
746 myFF->doForces(calcPot, calcStress);
747 }
748
749 template<typename T> void Integrator<T>::thermalize(){
750 tStats->velocitize();
751 }