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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines