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 558 by mmeineke, Thu Jun 19 19:21:23 2003 UTC vs.
Revision 725 by tim, Tue Aug 26 20:29:26 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines