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 572 by mmeineke, Wed Jul 2 21:26:55 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){
78 <        
79 <        dummy_plug = theArray[j]->get_constraint();
80 <        temp_con[nConstrained].set_a( dummy_plug->get_a() );
81 <        temp_con[nConstrained].set_b( dummy_plug->get_b() );
82 <        temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() );
83 <        
84 <        nConstrained++;
84 <        constrained = 0;
76 >
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 >        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);
174  
176
177  tStats   = new Thermo( info );
178  statOut  = new StatWriter( info );
179  dumpOut  = new DumpWriter( info );
180
175    atoms = info->atoms;
182  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  
207  readyCheck();
211  
212   #ifdef IS_MPI
213 <  strcpy( checkPointMsg,
211 <          "The integrator is ready to go." );
213 >  strcpy(checkPointMsg, "The integrator is ready to go.");
214    MPIcheckPoint();
215   #endif // is_mpi
216  
217 <
218 <  pos  = Atom::getPosArray();
219 <  vel  = Atom::getVelArray();
218 <  frc  = Atom::getFrcArray();
219 <  trq  = Atom::getTrqArray();
220 <  Amat = Atom::getAmatArray();
221 <
222 <  while( currTime < runTime ){
223 <
224 <    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;
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,
254 <            "successfully took a time step." );
269 >    strcpy(checkPointMsg, "successfully took a time step.");
270      MPIcheckPoint();
271   #endif // is_mpi
257
272    }
273  
260  dumpOut->writeFinal(currTime);
261
274    delete dumpOut;
275    delete statOut;
276   }
277  
278 < void Integrator::integrateStep( int calcPot, int calcStress ){
279 <
268 <
269 <      
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();
274  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");
305 +  MPIcheckPoint();
306 + #endif // is_mpi
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");
315 +  MPIcheckPoint();
316 + #endif // is_mpi
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");
334 >  MPIcheckPoint();
335 > #endif // is_mpi
336   }
337  
338  
339 < void Integrator::moveA( void ){
340 <  
290 <  int i,j,k;
291 <  int atomIndex, aMatIndex;
339 > template<typename T> void Integrator<T>::moveA(void){
340 >  size_t i, j;
341    DirectionalAtom* dAtom;
342 <  double Tb[3];
343 <  double ji[3];
344 <  double angle;
342 >  double Tb[3], ji[3];
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 (j = 0; j < 3; j++){
354 +      // velocity half step
355 +      vel[j] += (dt2 * frc[j] / mass) * eConvert;
356 +      // position whole step
357 +      pos[j] += dt * vel[j];
358 +    }
359  
360 +    integrableObjects[i]->setVel(vel);
361 +    integrableObjects[i]->setPos(pos);
362  
363 <  for( i=0; i<nAtoms; i++ ){
300 <    atomIndex = i * 3;
301 <    aMatIndex = i * 9;
363 >    if (integrableObjects[i]->isDirectional()){
364  
365 <    // velocity half step
304 <    for( j=atomIndex; j<(atomIndex+3); j++ )
305 <      vel[j] += ( dt2 * frc[j] / atoms[i]->getMass() ) * eConvert;
365 >      // get and convert the torque to body frame
366  
367 <    // position whole step    
368 <    for( j=atomIndex; j<(atomIndex+3); j++ ) pos[j] += dt * vel[j];
309 <    
310 <    if( atoms[i]->isDirectional() ){
367 >      integrableObjects[i]->getTrq(Tb);
368 >      integrableObjects[i]->lab2Body(Tb);
369  
312      dAtom = (DirectionalAtom *)atoms[i];
313          
314      // get and convert the torque to body frame
315      
316      Tb[0] = dAtom->getTx();
317      Tb[1] = dAtom->getTy();
318      Tb[2] = dAtom->getTz();
319      
320      dAtom->lab2Body( Tb );
321      
370        // get the angular momentum, and propagate a half step
371 <      
372 <      ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * eConvert;
373 <      ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * eConvert;
374 <      ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * eConvert;
375 <      
376 <      // use the angular velocities to propagate the rotation matrix a
377 <      // full time step
378 <      
379 <      // rotate about the x-axis      
332 <      angle = dt2 * ji[0] / dAtom->getIxx();
333 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
334 <      
335 <      // rotate about the y-axis
336 <      angle = dt2 * ji[1] / dAtom->getIyy();
337 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
338 <      
339 <      // rotate about the z-axis
340 <      angle = dt * ji[2] / dAtom->getIzz();
341 <      this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] );
342 <      
343 <      // rotate about the y-axis
344 <      angle = dt2 * ji[1] / dAtom->getIyy();
345 <      this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] );
346 <      
347 <       // rotate about the x-axis
348 <      angle = dt2 * ji[0] / dAtom->getIxx();
349 <      this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] );
350 <      
351 <      dAtom->setJx( ji[0] );
352 <      dAtom->setJy( ji[1] );
353 <      dAtom->setJz( ji[2] );
371 >
372 >      integrableObjects[i]->getJ(ji);
373 >
374 >      for (j = 0; j < 3; j++)
375 >        ji[j] += (dt2 * Tb[j]) * eConvert;
376 >
377 >      this->rotationPropagation( integrableObjects[i], ji );
378 >
379 >      integrableObjects[i]->setJ(ji);
380      }
355    
381    }
382 +
383 +  if (nConstrained){
384 +    constrainA();
385 +  }
386   }
387  
388  
389 < void Integrator::moveB( void ){
390 <  int i,j,k;
391 <  int atomIndex;
392 <  DirectionalAtom* dAtom;
393 <  double Tb[3];
365 <  double ji[3];
389 > template<typename T> void Integrator<T>::moveB(void){
390 >  int i, j;
391 >  double Tb[3], ji[3];
392 >  double vel[3], frc[3];
393 >  double mass;
394  
395 <  for( i=0; i<nAtoms; i++ ){
396 <    atomIndex = i * 3;
395 >  for (i = 0; i < integrableObjects.size(); i++){
396 >    integrableObjects[i]->getVel(vel);
397 >    integrableObjects[i]->getFrc(frc);
398  
399 +    mass = integrableObjects[i]->getMass();
400 +
401      // velocity half step
402 <    for( j=atomIndex; j<(atomIndex+3); j++ )
403 <      vel[j] += ( dt2 * frc[j] / atoms[i]->getMass() ) * eConvert;
402 >    for (j = 0; j < 3; j++)
403 >      vel[j] += (dt2 * frc[j] / mass) * eConvert;
404  
405 <    if( atoms[i]->isDirectional() ){
406 <      
407 <      dAtom = (DirectionalAtom *)atoms[i];
408 <      
405 >    integrableObjects[i]->setVel(vel);
406 >
407 >    if (integrableObjects[i]->isDirectional()){
408 >
409        // get and convert the torque to body frame
410 <      
411 <      Tb[0] = dAtom->getTx();
412 <      Tb[1] = dAtom->getTy();
413 <      Tb[2] = dAtom->getTz();
414 <      
415 <      dAtom->lab2Body( Tb );
416 <      
417 <      // get the angular momentum, and complete the angular momentum
418 <      // half step
419 <      
420 <      ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * eConvert;
421 <      ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * eConvert;
422 <      ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * eConvert;
392 <      
393 <      dAtom->setJx( ji[0] );
394 <      dAtom->setJy( ji[1] );
395 <      dAtom->setJz( ji[2] );
410 >
411 >      integrableObjects[i]->getTrq(Tb);
412 >      integrableObjects[i]->lab2Body(Tb);
413 >
414 >      // get the angular momentum, and propagate a half step
415 >
416 >      integrableObjects[i]->getJ(ji);
417 >
418 >      for (j = 0; j < 3; j++)
419 >        ji[j] += (dt2 * Tb[j]) * eConvert;
420 >
421 >
422 >      integrableObjects[i]->setJ(ji);
423      }
424    }
425  
426 +  if (nConstrained){
427 +    constrainB();
428 +  }
429   }
430  
431 < void Integrator::preMove( void ){
432 <  int i;
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*3); i++) oldPos[i] = pos[i];
439 >      for (j = 0; j < 3; j++){
440 >        oldPos[3 * i + j] = pos[j];
441 >      }
442 >    }
443    }
444 < }  
444 > }
445  
446 < void Integrator::constrainA(){
447 <
412 <  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];
451    double pab[3];
452    double rab[3];
453    int a, b, ax, ay, az, bx, by, bz;
# Line 422 | Line 459 | void Integrator::constrainA(){
459    double gab;
460    int iteration;
461  
462 <
426 <  
427 <  for( i=0; i<nAtoms; i++){
428 <    
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 )){
436 <
469 >  while (!done && (iteration < maxIteration)){
470      done = 1;
471 <    for(i=0; i<nConstrained; i++){
439 <
471 >    for (i = 0; i < nConstrained; i++){
472        a = constrainedA[i];
473        b = constrainedB[i];
442      
443      ax = (a*3) + 0;
444      ay = (a*3) + 1;
445      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 <        pab[0] = pos[ax] - pos[bx];
454 <        pab[1] = pos[ay] - pos[by];
455 <        pab[2] = pos[az] - pos[bz];
479 >      bx = (b * 3) + 0;
480 >      by = (b * 3) + 1;
481 >      bz = (b * 3) + 2;
482  
483 <        //periodic boundary condition
483 >      if (moved[a] || moved[b]){
484 >        atoms[a]->getPos(posA);
485 >        atoms[b]->getPos(posB);
486  
487 <        info->wrapVector( pab );
487 >        for (j = 0; j < 3; j++)
488 >          pab[j] = posA[j] - posB[j];
489  
490 <        pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
490 >        //periodic boundary condition
491  
492 <        rabsq = constrainedDsqr[i];
464 <        diffsq = rabsq - pabsq;
492 >        info->wrapVector(pab);
493  
494 <        // 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];
494 >        pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2];
495  
496 <          info->wrapVector( rab );
496 >        rabsq = constrainedDsqr[i];
497 >        diffsq = rabsq - pabsq;
498  
499 <          rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
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 <          rpabsq = rpab * rpab;
505 >          info->wrapVector(rab);
506  
507 +          rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2];
508  
509 <          if (rpabsq < (rabsq * -diffsq)){
509 >          rpabsq = rpab * rpab;
510  
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 <          pos[ax] += rma * dx;
534 <          pos[ay] += rma * dy;
535 <          pos[az] += rma * dz;
533 >          posA[0] += rma * dx;
534 >          posA[1] += rma * dy;
535 >          posA[2] += rma * dz;
536  
537 <          pos[bx] -= rmb * dx;
506 <          pos[by] -= rmb * dy;
507 <          pos[bz] -= rmb * dz;
537 >          atoms[a]->setPos(posA);
538  
539 +          posB[0] -= rmb * dx;
540 +          posB[1] -= rmb * dy;
541 +          posB[2] -= rmb * dz;
542 +
543 +          atoms[b]->setPos(posB);
544 +
545            dx = dx / dt;
546            dy = dy / dt;
547            dz = dz / dt;
548  
549 <          vel[ax] += rma * dx;
514 <          vel[ay] += rma * dy;
515 <          vel[az] += rma * dz;
549 >          atoms[a]->getVel(velA);
550  
551 <          vel[bx] -= rmb * dx;
552 <          vel[by] -= rmb * dy;
553 <          vel[bz] -= rmb * dz;
551 >          velA[0] += rma * dx;
552 >          velA[1] += rma * dy;
553 >          velA[2] += rma * dz;
554  
555 <          moving[a] = 1;
556 <          moving[b] = 1;
557 <          done = 0;
558 <        }
555 >          atoms[a]->setVel(velA);
556 >
557 >          atoms[b]->getVel(velB);
558 >
559 >          velB[0] -= rmb * dx;
560 >          velB[1] -= rmb * dy;
561 >          velB[2] -= rmb * dz;
562 >
563 >          atoms[b]->setVel(velB);
564 >
565 >          moving[a] = 1;
566 >          moving[b] = 1;
567 >          done = 0;
568 >        }
569        }
570      }
571 <    
572 <    for(i=0; i<nAtoms; i++){
529 <      
571 >
572 >    for (i = 0; i < nAtoms; i++){
573        moved[i] = moving[i];
574        moving[i] = 0;
575      }
# Line 534 | 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",
541 <             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 <  
550 <  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];
595    double vxab, vyab, vzab;
596    double rab[3];
597    int a, b, ax, ay, az, bx, by, bz;
598    double rma, rmb;
599    double dx, dy, dz;
600 <  double rabsq, pabsq, rvab;
558 <  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 ) ){
570 <
611 >  while (!done && (iteration < maxIteration)){
612      done = 1;
613  
614 <    for(i=0; i<nConstrained; i++){
574 <      
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] ){
627 <        
628 <        vxab = vel[ax] - vel[bx];
589 <        vyab = vel[ay] - vel[by];
590 <        vzab = vel[az] - vel[bz];
626 >      if (moved[a] || moved[b]){
627 >        atoms[a]->getVel(velA);
628 >        atoms[b]->getVel(velB);
629  
630 <        rab[0] = pos[ax] - pos[bx];
631 <        rab[1] = pos[ay] - pos[by];
632 <        rab[2] = pos[az] - pos[bz];
595 <        
596 <        info->wrapVector( rab );
597 <        
598 <        rma = 1.0 / atoms[a]->getMass();
599 <        rmb = 1.0 / atoms[b]->getMass();
630 >        vxab = velA[0] - velB[0];
631 >        vyab = velA[1] - velB[1];
632 >        vzab = velA[2] - velB[2];
633  
634 <        rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
635 <          
603 <        gab = -rvab / ( ( rma + rmb ) * constrainedDsqr[i] );
634 >        atoms[a]->getPos(posA);
635 >        atoms[b]->getPos(posB);
636  
637 <        if (fabs(gab) > tol) {
638 <          
607 <          dx = rab[0] * gab;
608 <          dy = rab[1] * gab;
609 <          dz = rab[2] * gab;
610 <          
611 <          vel[ax] += rma * dx;
612 <          vel[ay] += rma * dy;
613 <          vel[az] += rma * dz;
637 >        for (j = 0; j < 3; j++)
638 >          rab[j] = posA[j] - posB[j];
639  
640 <          vel[bx] -= rmb * dx;
641 <          vel[by] -= rmb * dy;
642 <          vel[bz] -= rmb * dz;
643 <          
644 <          moving[a] = 1;
645 <          moving[b] = 1;
646 <          done = 0;
647 <        }
640 >        info->wrapVector(rab);
641 >
642 >        rma = 1.0 / atoms[a]->getMass();
643 >        rmb = 1.0 / atoms[b]->getMass();
644 >
645 >        rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab;
646 >
647 >        gab = -rvab / ((rma + rmb) * constrainedDsqr[i]);
648 >
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    }
680  
681 <  if( !done ){
682 <
683 <  
684 <    sprintf( painCave.errMsg,
638 <             "Constraint failure in constrainB, too many iterations: %d\n",
639 <             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 <  }
643 <
687 >  }
688   }
689  
690 + template<typename T> void Integrator<T>::rotationPropagation
691 + ( StuntDouble* sd, double ji[3] ){
692  
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 < void Integrator::rotate( int axes1, int axes2, double angle, double ji[3],
715 <                         double A[9] ){
714 >    angle = dt2 * ji[j] / I[j][j];
715 >    this->rotate( k, i, angle, ji, A );
716  
717 <  int i,j,k;
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 664 | 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++){
759 <      tempA[j][i] = A[3*i + 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 683 | 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 703 | 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 721 | 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++){
817 <      A[3*j + i] = 0.0;
818 <      for(k=0; k<3; k++){
819 <        A[3*j + i] += tempA[i][k] * rot[j][k];
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];
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