ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 841
Committed: Wed Oct 29 17:55:28 2003 UTC (20 years, 8 months ago) by mmeineke
File size: 16945 byte(s)
Log Message:
som efixes to the way rcut is setup, as well as additional debugging comments.

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