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 658 by tim, Thu Jul 31 15:35:07 2003 UTC vs.
Revision 853 by mmeineke, Thu Nov 6 19:11:38 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;
29 <  have_tau_barostat = 0;
30 <  have_target_temp = 0;
31 <  have_target_pressure = 0;
32 < }
34 >  oldEta = 0.0;
35  
36 < template<typename T> void NPTi<T>::moveA() {
37 <  
38 <  int i, j;
39 <  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];
36 >  // retrieve eta from simInfo if
37 >  data = info->getProperty(ETAVALUE_ID);
38 >  if(data){
39 >    etaValue = dynamic_cast<DoubleArrayData*>(data);
40  
41 <  double rj[3];
42 <  double instaTemp, instaPress, instaVol;
43 <  double tt2, tb2, scaleFactor;
41 >    if(etaValue){
42 >      etaArray = etaValue->getData();
43 >      eta = etaArray[0];
44 >      oldEta = eta;
45 >    }
46 >  }
47  
48 <  tt2 = tauThermostat * tauThermostat;
48 <  tb2 = tauBarostat * tauBarostat;
48 > }
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) /
58 <                 (p_convert*NkBT*tb2));
50 > template<typename T> NPTi<T>::~NPTi() {
51 >  //nothing for now
52 > }
53  
54 <  for( i=0; i<nAtoms; i++ ){
55 <    atoms[i]->getVel( vel );
56 <    atoms[i]->getPos( pos );
57 <    atoms[i]->getFrc( frc );
54 > template<typename T> void NPTi<T>::resetIntegrator() {
55 >  eta = 0.0;
56 >  T::resetIntegrator();
57 > }
58  
59 <    mass = atoms[i]->getMass();
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 <    for (j=0; j < 3; j++) {
68 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
69 <      rj[j] = pos[j];
70 <    }
65 > template<typename T> void NPTi<T>::evolveEtaB() {
66  
67 <    atoms[i]->setVel( vel );
67 >  prevEta = eta;
68 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
69 >                 (p_convert*NkBT*tb2));
70 > }
71  
72 <    info->wrapVector(rj);
72 > template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) {
73 >  int i;
74  
75 <    for (j = 0; j < 3; j++)
76 <      pos[j] += dt * (vel[j] + eta*rj[j]);
75 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
76 > }
77  
78 <    atoms[i]->setPos( pos );
78 > template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){
79 >  int i;
80  
81 <    if( atoms[i]->isDirectional() ){
81 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
82 > }
83  
83      dAtom = (DirectionalAtom *)atoms[i];
84          
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
84  
85 <      dAtom->getJ( ji );
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 <      for (j=0; j < 3; j++)
90 <        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
89 >  for(j=0; j<3; j++)
90 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
91  
92 <      dAtom->getA(A);
93 <      dAtom->getI(I);
94 <    
103 <      // rotate about the x-axis      
104 <      angle = dt2 * ji[0] / I[0][0];
105 <      this->rotate( 1, 2, angle, ji, A );
92 >  for(j=0; j<3; j++)
93 >    sc[j] *= eta;
94 > }
95  
96 <      // 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 <    }                
96 > template<typename T> void NPTi<T>::scaleSimBox( void ){
97  
98 <  }
98 >  double scaleFactor;
99  
129  // Scale the box after all the positions have been moved:
130  
100    scaleFactor = exp(dt*eta);
101  
102    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 138 | Line 107 | template<typename T> void NPTi<T>::moveA() {
107               );
108      painCave.isFatal = 1;
109      simError();
110 <  } else {        
111 <    info->scaleBox(exp(dt*eta));      
110 >  } else {
111 >    info->scaleBox(scaleFactor);
112    }
113  
114   }
115  
116 < template<typename T> void NPTi<T>::moveB( void ){
116 > template<typename T> bool NPTi<T>::etaConverged() {
117  
118 <  int i, j;
119 <  DirectionalAtom* dAtom;
151 <  double Tb[3], ji[3];
152 <  double vel[3], frc[3];
153 <  double mass;
118 >  return ( fabs(prevEta - eta) <= etaTolerance );
119 > }
120  
121 <  double instaTemp, instaPress, instaVol;
156 <  double tt2, tb2;
157 <  
158 <  tt2 = tauThermostat * tauThermostat;
159 <  tb2 = tauBarostat * tauBarostat;
121 > template<typename T> double NPTi<T>::getConservedQuantity(void){
122  
123 <  instaTemp = tStats->getTemperature();
124 <  instaPress = tStats->getPressure();
125 <  instaVol = tStats->getVolume();
123 >  double conservedQuantity;
124 >  double Energy;
125 >  double thermostat_kinetic;
126 >  double thermostat_potential;
127 >  double barostat_kinetic;
128 >  double barostat_potential;
129  
130 <  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++ ){
130 >  Energy = tStats->getTotalE();
131  
132 <    atoms[i]->getVel( vel );
133 <    atoms[i]->getFrc( frc );
132 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
133 >    (2.0 * eConvert);
134  
135 <    mass = atoms[i]->getMass();
135 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
136  
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 );
137  
138 <    if( atoms[i]->isDirectional() ){
138 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
139 >    (2.0 * eConvert);
140  
141 <      dAtom = (DirectionalAtom *)atoms[i];
141 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
142 >    eConvert;
143  
144 <      // get and convert the torque to body frame      
144 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
145 >    barostat_kinetic + barostat_potential;
146  
147 <      dAtom->getTrq( Tb );
148 <      dAtom->lab2Body( Tb );
147 > //   cout.width(8);
148 > //   cout.precision(8);
149  
150 <      // get the angular momentum, and propagate a half step
151 <
152 <      dAtom->getJ( ji );
153 <
195 <      for (j=0; j < 3; j++)
196 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);    
197 <
198 <      dAtom->setJ( ji );
199 <    }
200 <  }
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> int NPTi<T>::readyCheck() {
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 <  //check parent's readyCheck() first
162 <  if (T::readyCheck() == -1)
207 <    return -1;
208 <
209 <  // First check to see if we have a target temperature.
210 <  // Not having one is fatal.
211 <  
212 <  if (!have_target_temp) {
213 <    sprintf( painCave.errMsg,
214 <             "NPTi error: You can't use the NPTi integrator\n"
215 <             "   without a targetTemp!\n"
216 <             );
217 <    painCave.isFatal = 1;
218 <    simError();
219 <    return -1;
220 <  }
161 >  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
162 >  parameters += buffer;
163  
164 <  if (!have_target_pressure) {
165 <    sprintf( painCave.errMsg,
224 <             "NPTi error: You can't use the NPTi integrator\n"
225 <             "   without a targetPressure!\n"
226 <             );
227 <    painCave.isFatal = 1;
228 <    simError();
229 <    return -1;
230 <  }
231 <  
232 <  // We must set tauThermostat.
233 <  
234 <  if (!have_tau_thermostat) {
235 <    sprintf( painCave.errMsg,
236 <             "NPTi error: If you use the NPTi\n"
237 <             "   integrator, you must set tauThermostat.\n");
238 <    painCave.isFatal = 1;
239 <    simError();
240 <    return -1;
241 <  }    
164 >  sprintf(buffer,"\t%G\t0\t0;", eta);
165 >  parameters += buffer;
166  
167 <  // We must set tauBarostat.
168 <  
245 <  if (!have_tau_barostat) {
246 <    sprintf( painCave.errMsg,
247 <             "NPTi error: If you use the NPTi\n"
248 <             "   integrator, you must set tauBarostat.\n");
249 <    painCave.isFatal = 1;
250 <    simError();
251 <    return -1;
252 <  }    
167 >  sprintf(buffer,"\t0\t%G\t0;", eta);
168 >  parameters += buffer;
169  
170 <  // We need NkBT a lot, so just set it here:
170 >  sprintf(buffer,"\t0\t0\t%G;", eta);
171 >  parameters += buffer;
172  
173 <  NkBT = (double)info->ndf * kB * targetTemp;
173 >  return parameters;
174  
258  return 1;
175   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines