ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/integrators/Integrator.cpp
Revision: 1708
Committed: Thu Nov 4 16:20:55 2004 UTC (19 years, 8 months ago) by gezelter
File size: 18893 byte(s)
Log Message:
Don't remember what I did

File Contents

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