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 746 by mmeineke, Thu Sep 4 21:48:35 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   // 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 <  have_tau_thermostat = 0;
35 <  have_tau_barostat = 0;
36 <  have_target_temp = 0;
37 <  have_target_pressure = 0;
34 >  oldEta = 0.0;
35 >
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> void NPTi<T>::moveA() {
52 <  
53 <  int i, j;
37 <  DirectionalAtom* dAtom;
38 <  double Tb[3], ji[3];
39 <  double A[3][3], I[3][3];
40 <  double angle, mass;
41 <  double vel[3], pos[3], frc[3];
51 > template<typename T> NPTi<T>::~NPTi() {
52 >  //nothing for now
53 > }
54  
55 <  double rj[3];
56 <  double instaTemp, instaPress, instaVol;
57 <  double tt2, tb2, scaleFactor;
55 > template<typename T> void NPTi<T>::resetIntegrator() {
56 >  eta = 0.0;
57 >  T::resetIntegrator();
58 > }
59  
60 <  tt2 = tauThermostat * tauThermostat;
61 <  tb2 = tauBarostat * tauBarostat;
49 <
50 <  instaTemp = tStats->getTemperature();
51 <  instaPress = tStats->getPressure();
52 <  instaVol = tStats->getVolume();
53 <  
54 <   // first evolve chi a half step
55 <  
56 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
57 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
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 <  for( i=0; i<nAtoms; i++ ){
61 <    atoms[i]->getVel( vel );
62 <    atoms[i]->getPos( pos );
63 <    atoms[i]->getFrc( frc );
66 > template<typename T> void NPTi<T>::evolveEtaB() {
67  
68 <    mass = atoms[i]->getMass();
68 >  prevEta = eta;
69 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
70 >                 (p_convert*NkBT*tb2));
71 > }
72  
73 <    for (j=0; j < 3; j++) {
74 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
69 <      rj[j] = pos[j];
70 <    }
73 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
74 >  int i;
75  
76 <    atoms[i]->setVel( vel );
76 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
77 > }
78  
79 <    info->wrapVector(rj);
79 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
80 >  int i;
81  
82 <    for (j = 0; j < 3; j++)
83 <      pos[j] += dt * (vel[j] + eta*rj[j]);
82 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
83 > }
84  
79    atoms[i]->setPos( pos );
85  
86 <    if( atoms[i]->isDirectional() ){
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 = (DirectionalAtom *)atoms[i];
91 <          
85 <      // get and convert the torque to body frame
86 <      
87 <      dAtom->getTrq( Tb );
88 <      dAtom->lab2Body( Tb );
89 <      
90 <      // get the angular momentum, and propagate a half step
90 >  for(j=0; j<3; j++)
91 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
92  
93 <      dAtom->getJ( ji );
93 >  for(j=0; j<3; j++)
94 >    sc[j] *= eta;
95 > }
96  
97 <      for (j=0; j < 3; j++)
95 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
96 <      
97 <      // use the angular velocities to propagate the rotation matrix a
98 <      // full time step
97 > template<typename T> void NPTi<T>::scaleSimBox( void ){
98  
99 <      dAtom->getA(A);
101 <      dAtom->getI(I);
102 <    
103 <      // rotate about the x-axis      
104 <      angle = dt2 * ji[0] / I[0][0];
105 <      this->rotate( 1, 2, angle, ji, A );
99 >  double scaleFactor;
100  
107      // rotate about the y-axis
108      angle = dt2 * ji[1] / I[1][1];
109      this->rotate( 2, 0, angle, ji, A );
110      
111      // rotate about the z-axis
112      angle = dt * ji[2] / I[2][2];
113      this->rotate( 0, 1, angle, ji, A);
114      
115      // rotate about the y-axis
116      angle = dt2 * ji[1] / I[1][1];
117      this->rotate( 2, 0, angle, ji, A );
118      
119       // rotate about the x-axis
120      angle = dt2 * ji[0] / I[0][0];
121      this->rotate( 1, 2, angle, ji, A );
122      
123      dAtom->setJ( ji );
124      dAtom->setA( A  );    
125    }                
126
127  }
128
129  // Scale the box after all the positions have been moved:
130  
101    scaleFactor = exp(dt*eta);
102  
103    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 138 | Line 108 | template<typename T> void NPTi<T>::moveA() {
108               );
109      painCave.isFatal = 1;
110      simError();
111 <  } else {        
112 <    info->scaleBox(exp(dt*eta));      
111 >  } else {
112 >    info->scaleBox(scaleFactor);
113    }
114  
115   }
116  
117 < template<typename T> void NPTi<T>::moveB( void ){
117 > template<typename T> bool NPTi<T>::etaConverged() {
118  
119 <  int i, j;
120 <  DirectionalAtom* dAtom;
151 <  double Tb[3], ji[3];
152 <  double vel[3], frc[3];
153 <  double mass;
119 >  return ( fabs(prevEta - eta) <= etaTolerance );
120 > }
121  
122 <  double instaTemp, instaPress, instaVol;
156 <  double tt2, tb2;
157 <  
158 <  tt2 = tauThermostat * tauThermostat;
159 <  tb2 = tauBarostat * tauBarostat;
122 > template<typename T> double NPTi<T>::getConservedQuantity(void){
123  
124 <  instaTemp = tStats->getTemperature();
125 <  instaPress = tStats->getPressure();
126 <  instaVol = tStats->getVolume();
124 >  double conservedQuantity;
125 >  double Energy;
126 >  double thermostat_kinetic;
127 >  double thermostat_potential;
128 >  double barostat_kinetic;
129 >  double barostat_potential;
130  
131 <  chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
166 <  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
167 <                 (p_convert*NkBT*tb2));
168 <  
169 <  for( i=0; i<nAtoms; i++ ){
131 >  Energy = tStats->getTotalE();
132  
133 <    atoms[i]->getVel( vel );
134 <    atoms[i]->getFrc( frc );
133 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
134 >    (2.0 * eConvert);
135  
136 <    mass = atoms[i]->getMass();
136 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
137  
176    // velocity half step
177    for (j=0; j < 3; j++)
178      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
179    
180    atoms[i]->setVel( vel );
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 <      // get and convert the torque to body frame      
145 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
146 >    barostat_kinetic + barostat_potential;
147  
148 <      dAtom->getTrq( Tb );
149 <      dAtom->lab2Body( Tb );
148 > //   cout.width(8);
149 > //   cout.precision(8);
150  
151 <      // get the angular momentum, and propagate a half step
152 <
153 <      dAtom->getJ( ji );
154 <
195 <      for (j=0; j < 3; j++)
196 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);    
197 <
198 <      dAtom->setJ( ji );
199 <    }
200 <  }
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)
212 <    return -1;
213 <
214 <  // First check to see if we have a target temperature.
215 <  // Not having one is fatal.
216 <  
217 <  if (!have_target_temp) {
218 <    sprintf( painCave.errMsg,
219 <             "NPTi error: You can't use the NPTi integrator\n"
220 <             "   without a targetTemp!\n"
221 <             );
222 <    painCave.isFatal = 1;
223 <    simError();
224 <    return -1;
225 <  }
165 >  sprintf(buffer,"\t%G\t0\t0;", eta);
166 >  parameters += buffer;
167  
168 <  if (!have_target_pressure) {
169 <    sprintf( painCave.errMsg,
229 <             "NPTi error: You can't use the NPTi integrator\n"
230 <             "   without a targetPressure!\n"
231 <             );
232 <    painCave.isFatal = 1;
233 <    simError();
234 <    return -1;
235 <  }
236 <  
237 <  // We must set tauThermostat.
238 <  
239 <  if (!have_tau_thermostat) {
240 <    sprintf( painCave.errMsg,
241 <             "NPTi error: If you use the NPTi\n"
242 <             "   integrator, you must set tauThermostat.\n");
243 <    painCave.isFatal = 1;
244 <    simError();
245 <    return -1;
246 <  }    
168 >  sprintf(buffer,"\t0\t%G\t0;", eta);
169 >  parameters += buffer;
170  
171 <  // We must set tauBarostat.
172 <  
250 <  if (!have_tau_barostat) {
251 <    sprintf( painCave.errMsg,
252 <             "NPTi error: If you use the NPTi\n"
253 <             "   integrator, you must set tauBarostat.\n");
254 <    painCave.isFatal = 1;
255 <    simError();
256 <    return -1;
257 <  }    
171 >  sprintf(buffer,"\t0\t0\t%G;", eta);
172 >  parameters += buffer;
173  
174 <  // We need NkBT a lot, so just set it here:
174 >  return parameters;
175  
261  NkBT = (double)info->ndf * kB * targetTemp;
262
263  return 1;
176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines