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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines