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 767 by tim, Tue Sep 16 20:02:11 2003 UTC vs.
Revision 855 by mmeineke, Thu Nov 6 22:01:37 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
40 <  Nparticles = mpiSim->getTotAtoms();
41 < #else
42 <  Nparticles = theInfo->n_atoms;
43 < #endif
44 <
36 >  if( theInfo->useInitXSstate ){
37 >    // retrieve eta from simInfo if
38 >    data = info->getProperty(ETAVALUE_ID);
39 >    if(data){
40 >      etaValue = dynamic_cast<DoubleArrayData*>(data);
41 >      
42 >      if(etaValue){
43 >        etaArray = etaValue->getData();
44 >        eta = etaArray[0];
45 >        oldEta = eta;
46 >      }
47 >    }
48 >  }
49   }
50  
51   template<typename T> NPTi<T>::~NPTi() {
52 <  delete[] oldPos;
54 <  delete[] oldVel;
55 <  delete[] oldJi;
52 >  //nothing for now
53   }
54  
55 < template<typename T> void NPTi<T>::moveA() {
56 <
57 <  //new version of NPTi
58 <  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];
55 > template<typename T> void NPTi<T>::resetIntegrator() {
56 >  eta = 0.0;
57 >  T::resetIntegrator();
58 > }
59  
60 <  double rj[3];
61 <  double instaTemp, instaPress, instaVol;
62 <  double tt2, tb2, scaleFactor;
63 <  double COM[3];
60 > template<typename T> void NPTi<T>::evolveEtaA() {
61 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
62 >                 (p_convert*NkBT*tb2));
63 >  oldEta = eta;
64 > }
65  
66 <  tt2 = tauThermostat * tauThermostat;
74 <  tb2 = tauBarostat * tauBarostat;
66 > template<typename T> void NPTi<T>::evolveEtaB() {
67  
68 <  instaTemp = tStats->getTemperature();
69 <  instaPress = tStats->getPressure();
70 <  instaVol = tStats->getVolume();
71 <  
80 <  tStats->getCOM(COM);
81 <  
82 <  //evolve velocity half step
83 <  for( i=0; i<nAtoms; i++ ){
68 >  prevEta = eta;
69 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
70 >                 (p_convert*NkBT*tb2));
71 > }
72  
73 <    atoms[i]->getVel( vel );
74 <    atoms[i]->getFrc( frc );
73 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
74 >  int i;
75  
76 <    mass = atoms[i]->getMass();
76 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
77 > }
78  
79 <    for (j=0; j < 3; j++) {
80 <      // velocity half step  (use chi from previous step here):
92 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi + eta));
93 <  
94 <    }
79 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
80 >  int i;
81  
82 <    atoms[i]->setVel( vel );
83 <  
98 <    if( atoms[i]->isDirectional() ){
82 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
83 > }
84  
100      dAtom = (DirectionalAtom *)atoms[i];
85  
86 <      // get and convert the torque to body frame
87 <      
88 <      dAtom->getTrq( Tb );
105 <      dAtom->lab2Body( Tb );
106 <      
107 <      // get the angular momentum, and propagate a half step
86 > template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3],
87 >                                               int index, double sc[3]){
88 >  int j;
89  
90 <      dAtom->getJ( ji );
90 >  for(j=0; j<3; j++)
91 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
92  
93 <      for (j=0; j < 3; j++)
94 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
95 <      
114 <      // use the angular velocities to propagate the rotation matrix a
115 <      // full time step
93 >  for(j=0; j<3; j++)
94 >    sc[j] *= eta;
95 > }
96  
97 <      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 );
97 > template<typename T> void NPTi<T>::scaleSimBox( void ){
98  
99 <      // 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 <  }
99 >  double scaleFactor;
100  
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
179  }
180    
181
182  // Scale the box after all the positions have been moved:
183  
101    scaleFactor = exp(dt*eta);
102  
103    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 191 | Line 108 | template<typename T> void NPTi<T>::moveA() {
108               );
109      painCave.isFatal = 1;
110      simError();
111 <  } else {        
112 <    info->scaleBox(scaleFactor);      
113 <  }  
111 >  } else {
112 >    info->scaleBox(scaleFactor);
113 >  }
114  
115   }
116  
117 < template<typename T> void NPTi<T>::moveB( void ){
201 <  
202 <  //new version of NPTi
203 <  int i, j, k;
204 <  DirectionalAtom* dAtom;
205 <  double Tb[3], ji[3];
206 <  double vel[3], frc[3];
207 <  double mass;
117 > template<typename T> bool NPTi<T>::etaConverged() {
118  
119 <  double instTemp, instPress, instVol;
120 <  double tt2, tb2;
211 <  double oldChi, prevChi;
212 <  double oldEta, preEta;
213 <  
214 <  tt2 = tauThermostat * tauThermostat;
215 <  tb2 = tauBarostat * tauBarostat;
119 >  return ( fabs(prevEta - eta) <= etaTolerance );
120 > }
121  
122 + template<typename T> double NPTi<T>::getConservedQuantity(void){
123  
124 <  // Set things up for the iteration:
124 >  double conservedQuantity;
125 >  double Energy;
126 >  double thermostat_kinetic;
127 >  double thermostat_potential;
128 >  double barostat_kinetic;
129 >  double barostat_potential;
130  
131 <  oldChi = chi;
221 <  oldEta = eta;
131 >  Energy = tStats->getTotalE();
132  
133 <  for( i=0; i<nAtoms; i++ ){
133 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
134 >    (2.0 * eConvert);
135  
136 <    atoms[i]->getVel( vel );
136 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
137  
227    for (j=0; j < 3; j++)
228      oldVel[3*i + j]  = vel[j];
138  
139 <    if( atoms[i]->isDirectional() ){
139 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
140 >    (2.0 * eConvert);
141  
142 <      dAtom = (DirectionalAtom *)atoms[i];
142 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
143 >    eConvert;
144  
145 <      dAtom->getJ( ji );
145 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
146 >    barostat_kinetic + barostat_potential;
147  
148 <      for (j=0; j < 3; j++)
149 <        oldJi[3*i + j] = ji[j];
148 > //   cout.width(8);
149 > //   cout.precision(8);
150  
151 <    }
152 <  }
153 <
154 <  // do the iteration:
243 <
244 <  instVol = tStats->getVolume();
245 <  
246 <  for (k=0; k < 4; k++) {
247 <    
248 <    instTemp = tStats->getTemperature();
249 <    instPress = tStats->getPressure();
250 <
251 <    // evolve chi another half step using the temperature at t + dt/2
252 <
253 <    prevChi = chi;
254 <    chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) /
255 <      (tauThermostat*tauThermostat);
256 <
257 <    preEta = eta;
258 <    eta = oldEta + dt2 * ( instVol * (instPress - targetPressure) /
259 <       (p_convert*NkBT*tb2));
260 <
261 <  
262 <    for( i=0; i<nAtoms; i++ ){
263 <
264 <      atoms[i]->getFrc( frc );
265 <      atoms[i]->getVel(vel);
266 <      
267 <      mass = atoms[i]->getMass();
268 <      
269 <      // velocity half step
270 <      for (j=0; j < 3; j++)
271 <        vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*(chi + eta));
272 <      
273 <      atoms[i]->setVel( vel );
274 <      
275 <      if( atoms[i]->isDirectional() ){
276 <
277 <        dAtom = (DirectionalAtom *)atoms[i];
278 <  
279 <        // get and convert the torque to body frame      
280 <  
281 <        dAtom->getTrq( Tb );
282 <        dAtom->lab2Body( Tb );      
283 <            
284 <        for (j=0; j < 3; j++)
285 <          ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
286 <      
287 <          dAtom->setJ( ji );
288 <      }
289 <    }
290 <
291 <    if (fabs(prevChi - chi) <= chiTolerance && fabs(preEta -eta) <= etaTolerance)
292 <      break;
293 <  }
294 <
295 <  //calculate integral of chida
296 <  integralOfChidt += dt2*chi;
297 <
298 <
151 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
152 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
153 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
154 >  return conservedQuantity;
155   }
156  
157 < template<typename T> void NPTi<T>::resetIntegrator() {
158 <  chi = 0.0;
159 <  eta = 0.0;
160 < }
157 > template<typename T> string NPTi<T>::getAdditionalParameters(void){
158 >  string parameters;
159 >  const int BUFFERSIZE = 2000; // size of the read buffer
160 >  char buffer[BUFFERSIZE];
161  
162 < template<typename T> int NPTi<T>::readyCheck() {
162 >  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
163 >  parameters += buffer;
164  
165 <  //check parent's readyCheck() first
166 <  if (T::readyCheck() == -1)
310 <    return -1;
311 <
312 <  // First check to see if we have a target temperature.
313 <  // Not having one is fatal.
314 <  
315 <  if (!have_target_temp) {
316 <    sprintf( painCave.errMsg,
317 <             "NPTi error: You can't use the NPTi integrator\n"
318 <             "   without a targetTemp!\n"
319 <             );
320 <    painCave.isFatal = 1;
321 <    simError();
322 <    return -1;
323 <  }
165 >  sprintf(buffer,"\t%G\t0\t0;", eta);
166 >  parameters += buffer;
167  
168 <  if (!have_target_pressure) {
169 <    sprintf( painCave.errMsg,
327 <             "NPTi error: You can't use the NPTi integrator\n"
328 <             "   without a targetPressure!\n"
329 <             );
330 <    painCave.isFatal = 1;
331 <    simError();
332 <    return -1;
333 <  }
334 <  
335 <  // We must set tauThermostat.
336 <  
337 <  if (!have_tau_thermostat) {
338 <    sprintf( painCave.errMsg,
339 <             "NPTi error: If you use the NPTi\n"
340 <             "   integrator, you must set tauThermostat.\n");
341 <    painCave.isFatal = 1;
342 <    simError();
343 <    return -1;
344 <  }    
168 >  sprintf(buffer,"\t0\t%G\t0;", eta);
169 >  parameters += buffer;
170  
171 <  // We must set tauBarostat.
172 <  
348 <  if (!have_tau_barostat) {
349 <    sprintf( painCave.errMsg,
350 <             "NPTi error: If you use the NPTi\n"
351 <             "   integrator, you must set tauBarostat.\n");
352 <    painCave.isFatal = 1;
353 <    simError();
354 <    return -1;
355 <  }    
171 >  sprintf(buffer,"\t0\t0\t%G;", eta);
172 >  parameters += buffer;
173  
174 <  if (!have_chi_tolerance) {
358 <    sprintf( painCave.errMsg,
359 <             "NPTi warning: setting chi tolerance to 1e-6\n");
360 <    chiTolerance = 1e-6;
361 <    have_chi_tolerance = 1;
362 <    painCave.isFatal = 0;
363 <    simError();
364 <  }
174 >  return parameters;
175  
366    if (!have_eta_tolerance) {
367    sprintf( painCave.errMsg,
368             "NPTi warning: setting eta tolerance to 1e-6\n");
369    etaTolerance = 1e-6;
370    have_eta_tolerance = 1;
371    painCave.isFatal = 0;
372    simError();
373  }
374  // We need NkBT a lot, so just set it here:
375
376  NkBT = (double)Nparticles * kB * targetTemp;
377  fkBT = (double)info->ndf * kB * targetTemp;
378
379  return 1;
176   }
381
382 template<typename T> double NPTi<T>::getConservedQuantity(void){
383
384  double conservedQuantity;
385  double tb2;
386  double eta2;  
387  double E_NPT;
388  double U;
389  double TS;
390  double PV;
391  double extra;
392
393  U = tStats->getTotalE();
394
395  TS = fkBT *
396    (integralOfChidt + tauThermostat * tauThermostat * chi * chi / 2.0) / eConvert;
397
398  PV = (targetPressure * tStats->getVolume() / p_convert) / eConvert;
399
400  tb2 = tauBarostat * tauBarostat;
401  eta2 = eta * eta;
402
403
404  extra = ((double)info->ndfTrans * kB * targetTemp * tb2 * eta2 / 2.0) / eConvert;
405
406  cout.width(8);
407  cout.precision(8);
408
409  
410  cout << info->getTime() << "\t"
411       << chi << "\t"
412       << eta << "\t"
413       << U << "\t"
414       << TS << "\t"
415       << PV << "\t"
416       << extra << "\t"
417       << U+TS+PV+extra << endl;
418
419  conservedQuantity = U+TS+PV+extra;
420  return conservedQuantity;
421 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines