ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/tags/mpi_start/mdtools/md_code/Symplectic.cpp
Revision: 116
Committed: Mon Sep 23 15:13:12 2002 UTC (21 years, 9 months ago)
File size: 15016 byte(s)
Log Message:
This commit was manufactured by cvs2svn to create tag 'mpi_start'.

File Contents

# Content
1 #include <iostream>
2 #include <cstdlib>
3
4 #include "Integrator.hpp"
5 #include "Thermo.hpp"
6 #include "ReadWrite.hpp"
7
8
9 extern "C"{
10
11 void v_constrain_a_( double &dt, int &n_atoms, double* mass,
12 double* Rx, double* Ry, double* Rz,
13 double* Vx, double* Vy, double* Vz,
14 double* Fx, double* Fy, double* Fz,
15 int &n_constrained, double *constr_sqr,
16 int* constr_i, int* constr_j,
17 double &box_x, double &box_y, double &box_z );
18
19 void v_constrain_b_( double &dt, int &n_atoms, double* mass,
20 double* Rx, double* Ry, double* Rz,
21 double* Vx, double* Vy, double* Vz,
22 double* Fx, double* Fy, double* Fz,
23 double &Kinetic,
24 int &n_constrained, double *constr_sqr,
25 int* constr_i, int* constr_j,
26 double &box_x, double &box_y, double &box_z );
27 }
28
29
30
31
32 Symplectic::Symplectic( SimInfo* the_entry_plug ){
33 entry_plug = the_entry_plug;
34 isFirst = 1;
35
36 srInteractions = entry_plug->sr_interactions;
37 longRange = entry_plug->longRange;
38 nSRI = entry_plug->n_SRI;
39
40 // give a little love back to the SimInfo object
41
42 if( entry_plug->the_integrator != NULL ) delete entry_plug->the_integrator;
43 entry_plug->the_integrator = this;
44
45 // grab the masses
46
47 mass = new double[entry_plug->n_atoms];
48 for(int i = 0; i < entry_plug->n_atoms; i++){
49 mass[i] = entry_plug->atoms[i]->getMass();
50 }
51
52
53 // check for constraints
54
55 is_constrained = 0;
56
57 Constraint *temp_con;
58 Constraint *dummy_plug;
59 temp_con = new Constraint[nSRI];
60 n_constrained = 0;
61 int constrained = 0;
62
63 for(int i = 0; i < nSRI; i++){
64
65 constrained = srInteractions[i]->is_constrained();
66
67 if(constrained){
68
69 dummy_plug = srInteractions[i]->get_constraint();
70 temp_con[n_constrained].set_a( dummy_plug->get_a() );
71 temp_con[n_constrained].set_b( dummy_plug->get_b() );
72 temp_con[n_constrained].set_dsqr( dummy_plug->get_dsqr() );
73
74 n_constrained++;
75 constrained = 0;
76 }
77 }
78
79 if(n_constrained > 0){
80
81 is_constrained = 1;
82 constrained_i = new int[n_constrained];
83 constrained_j = new int[n_constrained];
84 constrained_dsqr = new double[n_constrained];
85
86 for( int i = 0; i < n_constrained; i++){
87
88 /* add 1 to the index for the fortran arrays. */
89
90 constrained_i[i] = temp_con[i].get_a() + 1;
91 constrained_j[i] = temp_con[i].get_b() + 1;
92 constrained_dsqr[i] = temp_con[i].get_dsqr();
93 }
94 }
95
96 delete[] temp_con;
97 }
98
99 Symplectic::~Symplectic() {
100
101 if( n_constrained ){
102 delete[] constrained_i;
103 delete[] constrained_j;
104 delete[] constrained_dsqr;
105 }
106
107 }
108
109
110 void Symplectic::integrate( void ){
111
112 const double e_convert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2
113
114 int i, j; // loop counters
115 int nAtoms = entry_plug->n_atoms; // the number of atoms
116 double kE = 0.0; // the kinetic energy
117 double rot_kE;
118 double trans_kE;
119 int tl; // the time loop conter
120 double dt2; // half the dt
121
122 double vx, vy, vz; // the velocities
123 double vx2, vy2, vz2; // the square of the velocities
124 double rx, ry, rz; // the postitions
125
126 double ji[3]; // the body frame angular momentum
127 double jx2, jy2, jz2; // the square of the angular momentums
128 double Tb[3]; // torque in the body frame
129 double angle; // the angle through which to rotate the rotation matrix
130 double A[3][3]; // the rotation matrix
131
132 int time;
133
134 double dt = entry_plug->dt;
135 double runTime = entry_plug->run_time;
136 double sampleTime = entry_plug->sampleTime;
137 double statusTime = entry_plug->statusTime;
138 double thermalTime = entry_plug->thermalTime;
139
140 int n_loops = (int)( runTime / dt );
141 int sample_n = (int)( sampleTime / dt );
142 int status_n = (int)( statusTime / dt );
143 int vel_n = (int)( thermalTime / dt );
144
145 Thermo *tStats = new Thermo( entry_plug );
146
147 StatWriter* e_out = new StatWriter( entry_plug );
148 DumpWriter* dump_out = new DumpWriter( entry_plug );
149
150
151 Atom** atoms = entry_plug->atoms;
152 DirectionalAtom* dAtom;
153 dt2 = 0.5 * dt;
154
155 // initialize the forces the before the first step
156
157
158 for(i = 0; i < nAtoms; i++){
159 atoms[i]->zeroForces();
160 }
161
162 for(i = 0; i < nSRI; i++){
163
164 srInteractions[i]->calc_forces();
165 }
166
167 longRange->calc_forces();
168
169 if( entry_plug->setTemp ){
170
171 tStats->velocitize();
172 }
173
174 dump_out->writeDump( 0.0 );
175 e_out->writeStat( 0.0 );
176
177 if( n_constrained ){
178
179 double *Rx = new double[nAtoms];
180 double *Ry = new double[nAtoms];
181 double *Rz = new double[nAtoms];
182
183 double *Vx = new double[nAtoms];
184 double *Vy = new double[nAtoms];
185 double *Vz = new double[nAtoms];
186
187 double *Fx = new double[nAtoms];
188 double *Fy = new double[nAtoms];
189 double *Fz = new double[nAtoms];
190
191
192 for( tl=0; tl < n_loops; tl++ ){
193
194 for( j=0; j<nAtoms; j++ ){
195
196 Rx[j] = atoms[j]->getX();
197 Ry[j] = atoms[j]->getY();
198 Rz[j] = atoms[j]->getZ();
199
200 Vx[j] = atoms[j]->get_vx();
201 Vy[j] = atoms[j]->get_vy();
202 Vz[j] = atoms[j]->get_vz();
203
204 Fx[j] = atoms[j]->getFx();
205 Fy[j] = atoms[j]->getFy();
206 Fz[j] = atoms[j]->getFz();
207
208 }
209
210 v_constrain_a_( dt, nAtoms, mass, Rx, Ry, Rz, Vx, Vy, Vz,
211 Fx, Fy, Fz,
212 n_constrained, constrained_dsqr,
213 constrained_i, constrained_j,
214 entry_plug->box_x,
215 entry_plug->box_y,
216 entry_plug->box_z );
217
218 for( j=0; j<nAtoms; j++ ){
219
220 atoms[j]->setX(Rx[j]);
221 atoms[j]->setY(Ry[j]);
222 atoms[j]->setZ(Rz[j]);
223
224 atoms[j]->set_vx(Vx[j]);
225 atoms[j]->set_vy(Vy[j]);
226 atoms[j]->set_vz(Vz[j]);
227 }
228
229
230 for( i=0; i<nAtoms; i++ ){
231 if( atoms[i]->isDirectional() ){
232
233 dAtom = (DirectionalAtom *)atoms[i];
234
235 // get and convert the torque to body frame
236
237 Tb[0] = dAtom->getTx();
238 Tb[1] = dAtom->getTy();
239 Tb[2] = dAtom->getTz();
240
241 dAtom->lab2Body( Tb );
242
243 // get the angular momentum, and propagate a half step
244
245 ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * e_convert;
246 ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * e_convert;
247 ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * e_convert;
248
249 // get the atom's rotation matrix
250
251 A[0][0] = dAtom->getAxx();
252 A[0][1] = dAtom->getAxy();
253 A[0][2] = dAtom->getAxz();
254
255 A[1][0] = dAtom->getAyx();
256 A[1][1] = dAtom->getAyy();
257 A[1][2] = dAtom->getAyz();
258
259 A[2][0] = dAtom->getAzx();
260 A[2][1] = dAtom->getAzy();
261 A[2][2] = dAtom->getAzz();
262
263
264 // use the angular velocities to propagate the rotation matrix a
265 // full time step
266
267
268 angle = dt2 * ji[0] / dAtom->getIxx();
269 this->rotate( 1, 2, angle, ji, A ); // rotate about the x-axis
270
271 angle = dt2 * ji[1] / dAtom->getIyy();
272 this->rotate( 2, 0, angle, ji, A ); // rotate about the y-axis
273
274 angle = dt * ji[2] / dAtom->getIzz();
275 this->rotate( 0, 1, angle, ji, A ); // rotate about the z-axis
276
277 angle = dt2 * ji[1] / dAtom->getIyy();
278 this->rotate( 2, 0, angle, ji, A ); // rotate about the y-axis
279
280 angle = dt2 * ji[0] / dAtom->getIxx();
281 this->rotate( 1, 2, angle, ji, A ); // rotate about the x-axis
282
283
284 dAtom->setA( A );
285 dAtom->setJx( ji[0] );
286 dAtom->setJy( ji[1] );
287 dAtom->setJz( ji[2] );
288 }
289 }
290
291 // calculate the forces
292
293 for(j = 0; j < nAtoms; j++){
294 atoms[j]->zeroForces();
295 }
296
297 for(j = 0; j < nSRI; j++){
298 srInteractions[j]->calc_forces();
299 }
300
301 longRange->calc_forces();
302
303 // move b
304
305 for( j=0; j<nAtoms; j++ ){
306
307 Rx[j] = atoms[j]->getX();
308 Ry[j] = atoms[j]->getY();
309 Rz[j] = atoms[j]->getZ();
310
311 Vx[j] = atoms[j]->get_vx();
312 Vy[j] = atoms[j]->get_vy();
313 Vz[j] = atoms[j]->get_vz();
314
315 Fx[j] = atoms[j]->getFx();
316 Fy[j] = atoms[j]->getFy();
317 Fz[j] = atoms[j]->getFz();
318 }
319
320 v_constrain_b_( dt, nAtoms, mass, Rx, Ry, Rz, Vx, Vy, Vz,
321 Fx, Fy, Fz,
322 kE, n_constrained, constrained_dsqr,
323 constrained_i, constrained_j,
324 entry_plug->box_x,
325 entry_plug->box_y,
326 entry_plug->box_z );
327
328 for( j=0; j<nAtoms; j++ ){
329
330 atoms[j]->setX(Rx[j]);
331 atoms[j]->setY(Ry[j]);
332 atoms[j]->setZ(Rz[j]);
333
334 atoms[j]->set_vx(Vx[j]);
335 atoms[j]->set_vy(Vy[j]);
336 atoms[j]->set_vz(Vz[j]);
337 }
338
339 for( i=0; i< nAtoms; i++ ){
340
341 if( atoms[i]->isDirectional() ){
342
343 dAtom = (DirectionalAtom *)atoms[i];
344
345 // get and convert the torque to body frame
346
347 Tb[0] = dAtom->getTx();
348 Tb[1] = dAtom->getTy();
349 Tb[2] = dAtom->getTz();
350
351 dAtom->lab2Body( Tb );
352
353 // get the angular momentum, and complete the angular momentum
354 // half step
355
356 ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * e_convert;
357 ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * e_convert;
358 ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * e_convert;
359
360 dAtom->setJx( ji[0] );
361 dAtom->setJy( ji[1] );
362 dAtom->setJz( ji[2] );
363 }
364 }
365
366 time = tl + 1;
367
368 if( entry_plug->setTemp ){
369 if( !(time % vel_n) ) tStats->velocitize();
370 }
371 if( !(time % sample_n) ) dump_out->writeDump( time * dt );
372 if( !(time % status_n) ) e_out->writeStat( time * dt );
373 }
374 }
375 else{
376
377 for( tl=0; tl<n_loops; tl++ ){
378
379 kE = 0.0;
380 rot_kE= 0.0;
381 trans_kE = 0.0;
382
383 for( i=0; i<nAtoms; i++ ){
384
385 // velocity half step
386
387 vx = atoms[i]->get_vx() +
388 ( dt2 * atoms[i]->getFx() / atoms[i]->getMass() ) * e_convert;
389 vy = atoms[i]->get_vy() +
390 ( dt2 * atoms[i]->getFy() / atoms[i]->getMass() ) * e_convert;
391 vz = atoms[i]->get_vz() +
392 ( dt2 * atoms[i]->getFz() / atoms[i]->getMass() ) * e_convert;
393
394 // position whole step
395
396 rx = atoms[i]->getX() + dt * vx;
397 ry = atoms[i]->getY() + dt * vy;
398 rz = atoms[i]->getZ() + dt * vz;
399
400 atoms[i]->setX( rx );
401 atoms[i]->setY( ry );
402 atoms[i]->setZ( rz );
403
404 atoms[i]->set_vx( vx );
405 atoms[i]->set_vy( vy );
406 atoms[i]->set_vz( vz );
407
408 if( atoms[i]->isDirectional() ){
409
410 dAtom = (DirectionalAtom *)atoms[i];
411
412 // get and convert the torque to body frame
413
414 Tb[0] = dAtom->getTx();
415 Tb[1] = dAtom->getTy();
416 Tb[2] = dAtom->getTz();
417
418 dAtom->lab2Body( Tb );
419
420 // get the angular momentum, and propagate a half step
421
422 ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * e_convert;
423 ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * e_convert;
424 ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * e_convert;
425
426 // get the atom's rotation matrix
427
428 A[0][0] = dAtom->getAxx();
429 A[0][1] = dAtom->getAxy();
430 A[0][2] = dAtom->getAxz();
431
432 A[1][0] = dAtom->getAyx();
433 A[1][1] = dAtom->getAyy();
434 A[1][2] = dAtom->getAyz();
435
436 A[2][0] = dAtom->getAzx();
437 A[2][1] = dAtom->getAzy();
438 A[2][2] = dAtom->getAzz();
439
440
441 // use the angular velocities to propagate the rotation matrix a
442 // full time step
443
444
445 angle = dt2 * ji[0] / dAtom->getIxx();
446 this->rotate( 1, 2, angle, ji, A ); // rotate about the x-axis
447
448 angle = dt2 * ji[1] / dAtom->getIyy();
449 this->rotate( 2, 0, angle, ji, A ); // rotate about the y-axis
450
451 angle = dt * ji[2] / dAtom->getIzz();
452 this->rotate( 0, 1, angle, ji, A ); // rotate about the z-axis
453
454 angle = dt2 * ji[1] / dAtom->getIyy();
455 this->rotate( 2, 0, angle, ji, A ); // rotate about the y-axis
456
457 angle = dt2 * ji[0] / dAtom->getIxx();
458 this->rotate( 1, 2, angle, ji, A ); // rotate about the x-axis
459
460
461 dAtom->setA( A );
462 dAtom->setJx( ji[0] );
463 dAtom->setJy( ji[1] );
464 dAtom->setJz( ji[2] );
465 }
466 }
467
468 // calculate the forces
469
470 for(j = 0; j < nAtoms; j++){
471 atoms[j]->zeroForces();
472 }
473
474 for(j = 0; j < nSRI; j++){
475 srInteractions[j]->calc_forces();
476 }
477
478 longRange->calc_forces();
479
480 for( i=0; i< nAtoms; i++ ){
481
482 // complete the velocity half step
483
484 vx = atoms[i]->get_vx() +
485 ( dt2 * atoms[i]->getFx() / atoms[i]->getMass() ) * e_convert;
486 vy = atoms[i]->get_vy() +
487 ( dt2 * atoms[i]->getFy() / atoms[i]->getMass() ) * e_convert;
488 vz = atoms[i]->get_vz() +
489 ( dt2 * atoms[i]->getFz() / atoms[i]->getMass() ) * e_convert;
490
491 atoms[i]->set_vx( vx );
492 atoms[i]->set_vy( vy );
493 atoms[i]->set_vz( vz );
494
495 vx2 = vx * vx;
496 vy2 = vy * vy;
497 vz2 = vz * vz;
498
499 if( atoms[i]->isDirectional() ){
500
501 dAtom = (DirectionalAtom *)atoms[i];
502
503 // get and convert the torque to body frame
504
505 Tb[0] = dAtom->getTx();
506 Tb[1] = dAtom->getTy();
507 Tb[2] = dAtom->getTz();
508
509 dAtom->lab2Body( Tb );
510
511 // get the angular momentum, and complete the angular momentum
512 // half step
513
514 ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * e_convert;
515 ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * e_convert;
516 ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * e_convert;
517
518 jx2 = ji[0] * ji[0];
519 jy2 = ji[1] * ji[1];
520 jz2 = ji[2] * ji[2];
521
522 rot_kE += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy())
523 + (jz2 / dAtom->getIzz());
524
525 dAtom->setJx( ji[0] );
526 dAtom->setJy( ji[1] );
527 dAtom->setJz( ji[2] );
528 }
529 }
530
531 time = tl + 1;
532
533 if( entry_plug->setTemp ){
534 if( !(time % vel_n) ) tStats->velocitize();
535 }
536 if( !(time % sample_n) ) dump_out->writeDump( time * dt );
537 if( !(time % status_n) ) e_out->writeStat( time * dt );
538 }
539 }
540
541 dump_out->writeFinal();
542
543 delete dump_out;
544 delete e_out;
545 }
546
547 void Symplectic::rotate( int axes1, int axes2, double angle, double ji[3],
548 double A[3][3] ){
549
550 int i,j,k;
551 double sinAngle;
552 double cosAngle;
553 double angleSqr;
554 double angleSqrOver4;
555 double top, bottom;
556 double rot[3][3];
557 double tempA[3][3];
558 double tempJ[3];
559
560 // initialize the tempA
561
562 for(i=0; i<3; i++){
563 for(j=0; j<3; j++){
564 tempA[i][j] = A[i][j];
565 }
566 }
567
568 // initialize the tempJ
569
570 for( i=0; i<3; i++) tempJ[i] = ji[i];
571
572 // initalize rot as a unit matrix
573
574 rot[0][0] = 1.0;
575 rot[0][1] = 0.0;
576 rot[0][2] = 0.0;
577
578 rot[1][0] = 0.0;
579 rot[1][1] = 1.0;
580 rot[1][2] = 0.0;
581
582 rot[2][0] = 0.0;
583 rot[2][1] = 0.0;
584 rot[2][2] = 1.0;
585
586 // use a small angle aproximation for sin and cosine
587
588 angleSqr = angle * angle;
589 angleSqrOver4 = angleSqr / 4.0;
590 top = 1.0 - angleSqrOver4;
591 bottom = 1.0 + angleSqrOver4;
592
593 cosAngle = top / bottom;
594 sinAngle = angle / bottom;
595
596 rot[axes1][axes1] = cosAngle;
597 rot[axes2][axes2] = cosAngle;
598
599 rot[axes1][axes2] = sinAngle;
600 rot[axes2][axes1] = -sinAngle;
601
602 // rotate the momentum acoording to: ji[] = rot[][] * ji[]
603
604 for(i=0; i<3; i++){
605 ji[i] = 0.0;
606 for(k=0; k<3; k++){
607 ji[i] += rot[i][k] * tempJ[k];
608 }
609 }
610
611 // rotate the Rotation matrix acording to:
612 // A[][] = A[][] * transpose(rot[][])
613
614
615 // NOte for as yet unknown reason, we are setting the performing the
616 // calculation as:
617 // transpose(A[][]) = transpose(A[][]) * transpose(rot[][])
618
619 for(i=0; i<3; i++){
620 for(j=0; j<3; j++){
621 A[j][i] = 0.0;
622 for(k=0; k<3; k++){
623 A[j][i] += tempA[k][i] * rot[j][k];
624 }
625 }
626 }
627 }