ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/Integrator.cpp (file contents):
Revision 637 by gezelter, Thu Jul 17 21:50:01 2003 UTC vs.
Revision 1178 by gezelter, Thu May 13 21:08:05 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines