ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 725
Committed: Tue Aug 26 20:29:26 2003 UTC (20 years, 10 months ago) by tim
File size: 16269 byte(s)
Log Message:
*** empty log message ***

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