ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new-templateless/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.
branches/new-templateless/OOPSE/libmdtools/NPTi.cpp (file contents), Revision 851 by mmeineke, Wed Nov 5 19:18:17 2003 UTC

# Line 1 | Line 1
1 < #include <cmath>
1 > #include <iostream>
2 > #include <math.h>
3 >
4 >
5   #include "Atom.hpp"
6   #include "SRI.hpp"
7   #include "AbstractClasses.hpp"
# Line 7 | Line 10
10   #include "Thermo.hpp"
11   #include "ReadWrite.hpp"
12   #include "Integrator.hpp"
13 < #include "simError.h"
13 > #include "simError.h"
14  
15 + #ifdef IS_MPI
16 + #include "mpiSimulation.hpp"
17 + #endif
18  
19   // Basic isotropic thermostating and barostating via the Melchionna
20   // modification of the Hoover algorithm:
21   //
22   //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
23 < //       Molec. Phys., 78, 533.
23 > //       Molec. Phys., 78, 533.
24   //
25   //           and
26 < //
26 > //
27   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
28  
29 < template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
30 <  T( theInfo, the_ff )
29 > NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff):
30 >  NPT( theInfo, the_ff )
31   {
32 <  chi = 0.0;
32 >  GenericData* data;
33 >  double *etaArray;
34 >  int test;
35 >
36    eta = 0.0;
37 <  have_tau_thermostat = 0;
38 <  have_tau_barostat = 0;
39 <  have_target_temp = 0;
40 <  have_target_pressure = 0;
37 >  oldEta = 0.0;
38 >
39 >  // retrieve eta array from simInfo if it exists
40 >  data = info->getProperty(ETAVALUE_ID);
41 >  if(data != NULL){
42 >    
43 >    test = data->getDarray(etaArray);
44 >    
45 >    if( test == 9 ){
46 >      
47 >      eta = etaArray[0];
48 >      delete[] etaArray;
49 >    }
50 >    else
51 >      std::cerr << "NPTi error: etaArray is not length 9 (actual = " << test
52 >                << ").\n"
53 >                << "            Simulation wil proceed with eta = 0;\n";
54 >  }
55   }
56  
57 < template<typename T> void NPTi<T>::moveA() {
58 <  
59 <  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];
57 > NPTi::~NPTi() {
58 >  //nothing for now
59 > }
60  
61 <  double rj[3];
62 <  double instaTemp, instaPress, instaVol;
63 <  double tt2, tb2, scaleFactor;
61 > void NPTi::resetIntegrator() {
62 >  eta = 0.0;
63 >  NPT::resetIntegrator();
64 > }
65  
66 <  tt2 = tauThermostat * tauThermostat;
67 <  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) /
66 > void NPTi::evolveEtaA() {
67 >  eta += dt2 * ( instaVol * (instaPress - targetPressure) /
68                   (p_convert*NkBT*tb2));
69 +  oldEta = eta;
70 + }
71  
72 <  for( i=0; i<nAtoms; i++ ){
61 <    atoms[i]->getVel( vel );
62 <    atoms[i]->getPos( pos );
63 <    atoms[i]->getFrc( frc );
72 > void NPTi::evolveEtaB() {
73  
74 <    mass = atoms[i]->getMass();
74 >  prevEta = eta;
75 >  eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) /
76 >                 (p_convert*NkBT*tb2));
77 > }
78  
79 <    for (j=0; j < 3; j++) {
80 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta));
69 <      rj[j] = pos[j];
70 <    }
79 > void NPTi::getVelScaleA(double sc[3], double vel[3]) {
80 >  int i;
81  
82 <    atoms[i]->setVel( vel );
82 >  for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta );
83 > }
84  
85 <    info->wrapVector(rj);
85 > void NPTi::getVelScaleB(double sc[3], int index ){
86 >  int i;
87  
88 <    for (j = 0; j < 3; j++)
89 <      pos[j] += dt * (vel[j] + eta*rj[j]);
88 >  for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta );
89 > }
90  
79    atoms[i]->setPos( pos );
91  
92 <    if( atoms[i]->isDirectional() ){
92 > void NPTi::getPosScale(double pos[3], double COM[3],
93 >                                               int index, double sc[3]){
94 >  int j;
95  
96 <      dAtom = (DirectionalAtom *)atoms[i];
97 <          
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
96 >  for(j=0; j<3; j++)
97 >    sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
98  
99 <      dAtom->getJ( ji );
99 >  for(j=0; j<3; j++)
100 >    sc[j] *= eta;
101 > }
102  
103 <      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
103 > void NPTi::scaleSimBox( void ){
104  
105 <      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 );
105 >  double scaleFactor;
106  
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  
107    scaleFactor = exp(dt*eta);
108  
109    if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) {
# Line 138 | Line 114 | template<typename T> void NPTi<T>::moveA() {
114               );
115      painCave.isFatal = 1;
116      simError();
117 <  } else {        
118 <    info->scaleBox(exp(dt*eta));      
117 >  } else {
118 >    info->scaleBox(scaleFactor);
119    }
120  
121   }
122  
123 < template<typename T> void NPTi<T>::moveB( void ){
123 > bool NPTi::etaConverged() {
124  
125 <  int i, j;
126 <  DirectionalAtom* dAtom;
151 <  double Tb[3], ji[3];
152 <  double vel[3], frc[3];
153 <  double mass;
125 >  return ( fabs(prevEta - eta) <= etaTolerance );
126 > }
127  
128 <  double instaTemp, instaPress, instaVol;
156 <  double tt2, tb2;
157 <  
158 <  tt2 = tauThermostat * tauThermostat;
159 <  tb2 = tauBarostat * tauBarostat;
128 > double NPTi::getConservedQuantity(void){
129  
130 <  instaTemp = tStats->getTemperature();
131 <  instaPress = tStats->getPressure();
132 <  instaVol = tStats->getVolume();
130 >  double conservedQuantity;
131 >  double Energy;
132 >  double thermostat_kinetic;
133 >  double thermostat_potential;
134 >  double barostat_kinetic;
135 >  double barostat_potential;
136  
137 <  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++ ){
137 >  Energy = tStats->getTotalE();
138  
139 <    atoms[i]->getVel( vel );
140 <    atoms[i]->getFrc( frc );
139 >  thermostat_kinetic = fkBT* tt2 * chi * chi /
140 >    (2.0 * eConvert);
141  
142 <    mass = atoms[i]->getMass();
142 >  thermostat_potential = fkBT* integralOfChidt / eConvert;
143  
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 );
144  
145 <    if( atoms[i]->isDirectional() ){
145 >  barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta /
146 >    (2.0 * eConvert);
147  
148 <      dAtom = (DirectionalAtom *)atoms[i];
148 >  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
149 >    eConvert;
150  
151 <      // get and convert the torque to body frame      
151 >  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential +
152 >    barostat_kinetic + barostat_potential;
153  
154 <      dAtom->getTrq( Tb );
155 <      dAtom->lab2Body( Tb );
154 > //   cout.width(8);
155 > //   cout.precision(8);
156  
157 <      // get the angular momentum, and propagate a half step
158 <
159 <      dAtom->getJ( ji );
160 <
195 <      for (j=0; j < 3; j++)
196 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);    
197 <
198 <      dAtom->setJ( ji );
199 <    }
200 <  }
157 > //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
158 > //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
159 > //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
160 >  return conservedQuantity;
161   }
162  
163 < template<typename T> void NPTi<T>::resetIntegrator() {
204 <  chi = 0.0;
205 <  eta = 0.0;
206 < }
163 > char* NPTi::getAdditionalParameters(void){
164  
165 < template<typename T> int NPTi<T>::readyCheck() {
165 >  sprintf(addParamBuffer,
166 >          "\t%G\t%G;"
167 >          "\t%G\t%0.0\t%0.0;"
168 >          "\t%0.0\t%G\t%0.0;"
169 >          "\t%0.0\t%0.0\t%G;",
170 >          chi, integralOfChidt,
171 >          eta, eta, eta
172 >          );
173  
174 <  //check parent's readyCheck() first
211 <  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 <  }
226 <
227 <  if (!have_target_pressure) {
228 <    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 <  }    
247 <
248 <  // We must set tauBarostat.
249 <  
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 <  }    
258 <
259 <  // We need NkBT a lot, so just set it here:
260 <
261 <  NkBT = (double)info->ndf * kB * targetTemp;
262 <
263 <  return 1;
174 >  return addParamBuffer;
175   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines