ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
Revision: 600
Committed: Mon Jul 14 22:38:13 2003 UTC (20 years, 11 months ago) by gezelter
File size: 15099 byte(s)
Log Message:
Fixes for get and set routines in Atom and DirectionalAtom

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