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

Comparing trunk/OOPSE/libmdtools/NPTi.cpp (file contents):
Revision 769 by tim, Fri Sep 19 14:22:29 2003 UTC vs.
Revision 847 by mmeineke, Fri Oct 31 18:28:52 2003 UTC

# Line 1 | Line 1
1 < #include <cmath>
1 > #include <math.h>
2   #include "Atom.hpp"
3   #include "SRI.hpp"
4   #include "AbstractClasses.hpp"
# Line 7 | Line 7
7   #include "Thermo.hpp"
8   #include "ReadWrite.hpp"
9   #include "Integrator.hpp"
10 < #include "simError.h"
10 > #include "simError.h"
11  
12   #ifdef IS_MPI
13   #include "mpiSimulation.hpp"
14   #endif
15  
16
16   // Basic isotropic thermostating and barostating via the Melchionna
17   // modification of the Hoover algorithm:
18   //
19   //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
20 < //       Molec. Phys., 78, 533.
20 > //       Molec. Phys., 78, 533.
21   //
22   //           and
23 < //
23 > //
24   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
25  
26   template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
27    T( theInfo, the_ff )
28   {
29 <  chi = 0.0;
29 >  GenericData* data;
30 >  DoubleArrayData * etaValue;
31 >  vector<double> etaArray;
32 >
33    eta = 0.0;
34 <  integralOfChidt = 0.0;
33 <  have_tau_thermostat = 0;
34 <  have_tau_barostat = 0;
35 <  have_target_temp = 0;
36 <  have_target_pressure = 0;
37 <  have_chi_tolerance = 0;
38 <  have_eta_tolerance = 0;
39 <  have_pos_iter_tolerance = 0;
34 >  oldEta = 0.0;
35  
36 <  oldPos = new double[3*nAtoms];
37 <  oldVel = new double[3*nAtoms];
38 <  oldJi = new double[3*nAtoms];
39 < #ifdef IS_MPI
45 <  Nparticles = mpiSim->getTotAtoms();
46 < #else
47 <  Nparticles = theInfo->n_atoms;
48 < #endif
36 >  // retrieve eta from simInfo if
37 >  data = info->getProperty(ETAVALUE_ID);
38 >  if(data){
39 >    etaValue = dynamic_cast<DoubleArrayData*>(data);
40  
41 +    if(etaValue){
42 +      etaArray = etaValue->getData();
43 +      eta = etaArray[0];
44 +      oldEta = eta;
45 +    }
46 +  }
47 +
48   }
49  
50   template<typename T> NPTi<T>::~NPTi() {
51 <  delete[] oldPos;
54 <  delete[] oldVel;
55 <  delete[] oldJi;
51 >  //nothing for now
52   }
53  
54 < template<typename T> void NPTi<T>::moveA() {
55 <
56 <  //new version of NPTi
57 <  int i, j, k;
62 <  DirectionalAtom* dAtom;
63 <  double Tb[3], ji[3];
64 <  double A[3][3], I[3][3];
65 <  double angle, mass;
66 <  double vel[3], pos[3], frc[3];
54 > template<typename T> void NPTi<T>::resetIntegrator() {
55 >  eta = 0.0;
56 >  T::resetIntegrator();
57 > }
58  
59 <  double rj[3];
60 <  double instaTemp, instaPress, instaVol;
61 <  double tt2, tb2, scaleFactor;
62 <  double COM[3];
59 > template<typename T> void NPTi<T>::evolveEtaA() {
60 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
61 >                 (p_convert*NkBT*tb2));
62 >  oldEta = eta;
63 > }
64  
65 <  tt2 = tauThermostat * tauThermostat;
74 <  tb2 = tauBarostat * tauBarostat;
65 > template<typename T> void NPTi<T>::evolveEtaB() {
66  
67 <  instaTemp = tStats->getTemperature();
68 <  instaPress = tStats->getPressure();
69 <  instaVol = tStats->getVolume();
70 <  
80 <  tStats->getCOM(COM);
81 <  
82 <  //evolve velocity half step
83 <  for( i=0; i<nAtoms; i++ ){
67 >  prevEta = eta;
68 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
69 >                 (p_convert*NkBT*tb2));
70 > }
71  
72 <    atoms[i]->getVel( vel );
73 <    atoms[i]->getFrc( frc );
72 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
73 >  int i;
74  
75 <    mass = atoms[i]->getMass();
75 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
76 > }
77  
78 <    for (j=0; j < 3; j++) {
79 <      // velocity half step  (use chi from previous step here):
92 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi + eta));
93 <  
94 <    }
78 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
79 >  int i;
80  
81 <    atoms[i]->setVel( vel );
82 <  
98 <    if( atoms[i]->isDirectional() ){
81 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
82 > }
83  
100      dAtom = (DirectionalAtom *)atoms[i];
84  
85 <      // get and convert the torque to body frame
86 <      
87 <      dAtom->getTrq( Tb );
105 <      dAtom->lab2Body( Tb );
106 <      
107 <      // get the angular momentum, and propagate a half step
85 > template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3],
86 >                                               int index, double sc[3]){
87 >  int j;
88  
89 <      dAtom->getJ( ji );
89 >  for(j=0; j<3; j++)
90 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
91  
92 <      for (j=0; j < 3; j++)
93 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
94 <      
114 <      // use the angular velocities to propagate the rotation matrix a
115 <      // full time step
92 >  for(j=0; j<3; j++)
93 >    sc[j] *= eta;
94 > }
95  
96 <      dAtom->getA(A);
118 <      dAtom->getI(I);
119 <    
120 <      // rotate about the x-axis      
121 <      angle = dt2 * ji[0] / I[0][0];
122 <      this->rotate( 1, 2, angle, ji, A );
96 > template<typename T> void NPTi<T>::scaleSimBox( void ){
97  
98 <      // rotate about the y-axis
125 <      angle = dt2 * ji[1] / I[1][1];
126 <      this->rotate( 2, 0, angle, ji, A );
127 <      
128 <      // rotate about the z-axis
129 <      angle = dt * ji[2] / I[2][2];
130 <      this->rotate( 0, 1, angle, ji, A);
131 <      
132 <      // rotate about the y-axis
133 <      angle = dt2 * ji[1] / I[1][1];
134 <      this->rotate( 2, 0, angle, ji, A );
135 <      
136 <       // rotate about the x-axis
137 <      angle = dt2 * ji[0] / I[0][0];
138 <      this->rotate( 1, 2, angle, ji, A );
139 <      
140 <      dAtom->setJ( ji );
141 <      dAtom->setA( A  );    
142 <    }    
143 <  }
98 >  double scaleFactor;
99  
145  // evolve chi and eta  half step
146  
147  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
148  eta += dt2 * ( instaVol * (instaPress - targetPressure) / (p_convert*NkBT*tb2));
149
150  //calculate the integral of chidt
151  integralOfChidt += dt2*chi;
152
153  //save the old positions
154  for(i = 0; i < nAtoms; i++){
155    atoms[i]->getPos(pos);
156    for(j = 0; j < 3; j++)
157      oldPos[i*3 + j] = pos[j];
158  }
159  
160  //the first estimation of r(t+dt) is equal to  r(t)
161    
162  for(k = 0; k < 4; k ++){
163
164    for(i =0 ; i < nAtoms; i++){
165
166      atoms[i]->getVel(vel);
167      atoms[i]->getPos(pos);
168
169      for(j = 0; j < 3; j++)
170        rj[j] = (oldPos[i*3 + j] + pos[j])/2 - COM[j];    
171      
172      for(j = 0; j < 3; j++)
173        pos[j] = oldPos[i*3 + j] + dt*(vel[j] + eta*rj[j]);
174
175      atoms[i]->setPos( pos );
176    }
177    
178    if (nConstrained){
179      constrainA();
180    }
181  }
182    
183
184  // Scale the box after all the positions have been moved:
185  
100    scaleFactor = exp(dt*eta);
101  
102    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 193 | Line 107 | template<typename T> void NPTi<T>::moveA() {
107               );
108      painCave.isFatal = 1;
109      simError();
110 <  } else {        
111 <    info->scaleBox(scaleFactor);      
112 <  }  
110 >  } else {
111 >    info->scaleBox(scaleFactor);
112 >  }
113  
114   }
115  
116 < template<typename T> void NPTi<T>::moveB( void ){
203 <  
204 <  //new version of NPTi
205 <  int i, j, k;
206 <  DirectionalAtom* dAtom;
207 <  double Tb[3], ji[3];
208 <  double vel[3], frc[3];
209 <  double mass;
210 <
211 <  double instTemp, instPress, instVol;
212 <  double tt2, tb2;
213 <  double oldChi, prevChi;
214 <  double oldEta, preEta;
215 <  
216 <  tt2 = tauThermostat * tauThermostat;
217 <  tb2 = tauBarostat * tauBarostat;
218 <
219 <
220 <  // Set things up for the iteration:
221 <
222 <  oldChi = chi;
223 <  oldEta = eta;
224 <
225 <  for( i=0; i<nAtoms; i++ ){
116 > template<typename T> bool NPTi<T>::etaConverged() {
117  
118 <    atoms[i]->getVel( vel );
228 <
229 <    for (j=0; j < 3; j++)
230 <      oldVel[3*i + j]  = vel[j];
231 <
232 <    if( atoms[i]->isDirectional() ){
233 <
234 <      dAtom = (DirectionalAtom *)atoms[i];
235 <
236 <      dAtom->getJ( ji );
237 <
238 <      for (j=0; j < 3; j++)
239 <        oldJi[3*i + j] = ji[j];
240 <
241 <    }
242 <  }
243 <
244 <  // do the iteration:
245 <
246 <  instVol = tStats->getVolume();
247 <  
248 <  for (k=0; k < 4; k++) {
249 <    
250 <    instTemp = tStats->getTemperature();
251 <    instPress = tStats->getPressure();
252 <
253 <    // evolve chi another half step using the temperature at t + dt/2
254 <
255 <    prevChi = chi;
256 <    chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) /
257 <      (tauThermostat*tauThermostat);
258 <
259 <    preEta = eta;
260 <    eta = oldEta + dt2 * ( instVol * (instPress - targetPressure) /
261 <       (p_convert*NkBT*tb2));
262 <
263 <  
264 <    for( i=0; i<nAtoms; i++ ){
265 <
266 <      atoms[i]->getFrc( frc );
267 <      atoms[i]->getVel(vel);
268 <      
269 <      mass = atoms[i]->getMass();
270 <      
271 <      // velocity half step
272 <      for (j=0; j < 3; j++)
273 <        vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*(chi + eta));
274 <      
275 <      atoms[i]->setVel( vel );
276 <      
277 <      if( atoms[i]->isDirectional() ){
278 <
279 <        dAtom = (DirectionalAtom *)atoms[i];
280 <  
281 <        // get and convert the torque to body frame      
282 <  
283 <        dAtom->getTrq( Tb );
284 <        dAtom->lab2Body( Tb );      
285 <            
286 <        for (j=0; j < 3; j++)
287 <          ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
288 <      
289 <          dAtom->setJ( ji );
290 <      }
291 <    }
292 <    
293 <    if (nConstrained){
294 <      constrainB();
295 <    }    
296 <    
297 <    if (fabs(prevChi - chi) <=
298 <        chiTolerance && fabs(preEta -eta) <= etaTolerance)
299 <      break;
300 <  }
301 <
302 <  //calculate integral of chida
303 <  integralOfChidt += dt2*chi;
304 <
305 <
118 >  return ( fabs(prevEta - eta) <= etaTolerance );
119   }
120  
308 template<typename T> void NPTi<T>::resetIntegrator() {
309  chi = 0.0;
310  eta = 0.0;
311 }
312
313 template<typename T> int NPTi<T>::readyCheck() {
314
315  //check parent's readyCheck() first
316  if (T::readyCheck() == -1)
317    return -1;
318
319  // First check to see if we have a target temperature.
320  // Not having one is fatal.
321  
322  if (!have_target_temp) {
323    sprintf( painCave.errMsg,
324             "NPTi error: You can't use the NPTi integrator\n"
325             "   without a targetTemp!\n"
326             );
327    painCave.isFatal = 1;
328    simError();
329    return -1;
330  }
331
332  if (!have_target_pressure) {
333    sprintf( painCave.errMsg,
334             "NPTi error: You can't use the NPTi integrator\n"
335             "   without a targetPressure!\n"
336             );
337    painCave.isFatal = 1;
338    simError();
339    return -1;
340  }
341  
342  // We must set tauThermostat.
343  
344  if (!have_tau_thermostat) {
345    sprintf( painCave.errMsg,
346             "NPTi error: If you use the NPTi\n"
347             "   integrator, you must set tauThermostat.\n");
348    painCave.isFatal = 1;
349    simError();
350    return -1;
351  }    
352
353  // We must set tauBarostat.
354  
355  if (!have_tau_barostat) {
356    sprintf( painCave.errMsg,
357             "NPTi error: If you use the NPTi\n"
358             "   integrator, you must set tauBarostat.\n");
359    painCave.isFatal = 1;
360    simError();
361    return -1;
362  }    
363
364  if (!have_chi_tolerance) {
365    sprintf( painCave.errMsg,
366             "NPTi warning: setting chi tolerance to 1e-6\n");
367    chiTolerance = 1e-6;
368    have_chi_tolerance = 1;
369    painCave.isFatal = 0;
370    simError();
371  }
372
373    if (!have_eta_tolerance) {
374    sprintf( painCave.errMsg,
375             "NPTi warning: setting eta tolerance to 1e-6\n");
376    etaTolerance = 1e-6;
377    have_eta_tolerance = 1;
378    painCave.isFatal = 0;
379    simError();
380  }
381  // We need NkBT a lot, so just set it here:
382
383  NkBT = (double)Nparticles * kB * targetTemp;
384  fkBT = (double)info->ndf * kB * targetTemp;
385
386  return 1;
387 }
388
121   template<typename T> double NPTi<T>::getConservedQuantity(void){
122  
123    double conservedQuantity;
392  double LkBT;
393  double fkBT;
394  double f1kBT;
395  double f2kBT;
396  double NkBT;
124    double Energy;
125    double thermostat_kinetic;
126    double thermostat_potential;
127    double barostat_kinetic;
128    double barostat_potential;
402  double tb2;
403  double eta2;  
129  
405  LkBT = (double)(info->getNDF() + 4) * kB * targetTemp;  // 3N + 1
406  fkBT = (double)(info->getNDF()    ) * kB * targetTemp;  // 3N - 3
407  f1kBT = (double)(info->getNDF()+ 1) * kB * targetTemp;  // 3N - 3 + 1
408  NkBT = (double)(info->getNDF() + 3) * kB * targetTemp;  // 3N
409  f2kBT = (double)(info->getNDF()+ 2) * kB * targetTemp;  // 3N - 3 + 1
410
130    Energy = tStats->getTotalE();
131  
132 <  thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi /
132 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
133      (2.0 * eConvert);
134  
135    thermostat_potential = fkBT* integralOfChidt / eConvert;
136  
137  
138 <  barostat_kinetic = fkBT * tauBarostat * tauBarostat * eta * eta /
138 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
139      (2.0 * eConvert);
140 <  
141 <  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
140 >
141 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
142      eConvert;
143  
144    conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
145      barostat_kinetic + barostat_potential;
427  
428  cout.width(8);
429  cout.precision(8);
146  
147 <  cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
148 <      "\t" << thermostat_potential << "\t" << barostat_kinetic <<
433 <      "\t" << barostat_potential << "\t" << conservedQuantity << endl;
147 > //   cout.width(8);
148 > //   cout.precision(8);
149  
150 <  return conservedQuantity;
150 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
151 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
152 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
153 >  return conservedQuantity;
154   }
155 +
156 + template<typename T> string NPTi<T>::getAdditionalParameters(void){
157 +  string parameters;
158 +  const int BUFFERSIZE = 2000; // size of the read buffer
159 +  char buffer[BUFFERSIZE];
160 +
161 +  sprintf(buffer,"\t%lf\t%lf;", chi, integralOfChidt);
162 +  parameters += buffer;
163 +
164 +  sprintf(buffer,"\t%lf\t0\t0;", eta);
165 +  parameters += buffer;
166 +
167 +  sprintf(buffer,"\t0\t%lf\t0;", eta);
168 +  parameters += buffer;
169 +
170 +  sprintf(buffer,"\t0\t0\t%lf;", eta);
171 +  parameters += buffer;
172 +
173 +  return parameters;
174 +
175 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines