ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 1144
Committed: Sat May 1 18:52:38 2004 UTC (20 years, 2 months ago) by tim
File size: 18210 byte(s)
Log Message:
C++ pass groupList to fortran

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