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

Comparing trunk/OOPSE/libmdtools/NVT.cpp (file contents):
Revision 658 by tim, Thu Jul 31 15:35:07 2003 UTC vs.
Revision 1097 by gezelter, Mon Apr 12 20:32:20 2004 UTC

# Line 1 | Line 1
1 + #include <math.h>
2 +
3   #include "Atom.hpp"
4   #include "SRI.hpp"
5   #include "AbstractClasses.hpp"
# Line 6 | Line 8
8   #include "Thermo.hpp"
9   #include "ReadWrite.hpp"
10   #include "Integrator.hpp"
11 < #include "simError.h"
11 > #include "simError.h"
12  
13  
14   // Basic thermostating via Hoover, Phys.Rev.A, 1985, Vol. 31 (5) 1695-1697
# Line 14 | Line 16 | template<typename T> NVT<T>::NVT ( SimInfo *theInfo, F
16   template<typename T> NVT<T>::NVT ( SimInfo *theInfo, ForceFields* the_ff):
17    T( theInfo, the_ff )
18   {
19 +  GenericData* data;
20 +  DoubleData * chiValue;
21 +  DoubleData * integralOfChidtValue;
22 +
23 +  chiValue = NULL;
24 +  integralOfChidtValue = NULL;
25 +
26    chi = 0.0;
27    have_tau_thermostat = 0;
28    have_target_temp = 0;
29 +  have_chi_tolerance = 0;
30 +  integralOfChidt = 0.0;
31 +
32 +
33 +  if( theInfo->useInitXSstate ){
34 +
35 +    // retrieve chi and integralOfChidt from simInfo
36 +    data = info->getProperty(CHIVALUE_ID);
37 +    if(data){
38 +      chiValue = dynamic_cast<DoubleData*>(data);
39 +    }
40 +    
41 +    data = info->getProperty(INTEGRALOFCHIDT_ID);
42 +    if(data){
43 +      integralOfChidtValue = dynamic_cast<DoubleData*>(data);
44 +    }
45 +    
46 +    // chi and integralOfChidt should appear by pair
47 +    if(chiValue && integralOfChidtValue){
48 +      chi = chiValue->getData();
49 +      integralOfChidt = integralOfChidtValue->getData();
50 +    }
51 +  }
52 +
53 +
54 +  std::cerr << "building oldVel with \t" << integrableObjects.size() << "\n";
55 +  oldVel = new double[3*integrableObjects.size()];
56 +  oldJi = new double[3*integrableObjects.size()];
57   }
58  
59 + template<typename T> NVT<T>::~NVT() {
60 +  delete[] oldVel;
61 +  delete[] oldJi;
62 + }
63 +
64   template<typename T> void NVT<T>::moveA() {
65 <  
65 >
66    int i, j;
67    DirectionalAtom* dAtom;
68    double Tb[3], ji[3];
69 <  double A[3][3], I[3][3];
28 <  double angle, mass;
69 >  double mass;
70    double vel[3], pos[3], frc[3];
71  
72    double instTemp;
73  
74 +  // We need the temperature at time = t for the chi update below:
75 +
76    instTemp = tStats->getTemperature();
77  
78 <  // first evolve chi a half step
36 <  
37 <  chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat);
78 >  for( i=0; i < integrableObjects.size(); i++ ){
79  
80 <  for( i=0; i<nAtoms; i++ ){
80 >    integrableObjects[i]->getVel( vel );
81 >    integrableObjects[i]->getPos( pos );
82 >    integrableObjects[i]->getFrc( frc );
83  
84 <    atoms[i]->getVel( vel );
42 <    atoms[i]->getPos( pos );
43 <    atoms[i]->getFrc( frc );
84 >    mass = integrableObjects[i]->getMass();
85  
45    mass = atoms[i]->getMass();
46
86      for (j=0; j < 3; j++) {
87 <      // velocity half step
87 >      // velocity half step  (use chi from previous step here):
88        vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi);
89        // position whole step
90        pos[j] += dt * vel[j];
91      }
92  
93 <    atoms[i]->setVel( vel );
94 <    atoms[i]->setPos( pos );
56 <  
57 <    if( atoms[i]->isDirectional() ){
93 >    integrableObjects[i]->setVel( vel );
94 >    integrableObjects[i]->setPos( pos );
95  
96 <      dAtom = (DirectionalAtom *)atoms[i];
97 <          
96 >    if( integrableObjects[i]->isDirectional() ){
97 >
98        // get and convert the torque to body frame
99 <      
100 <      dAtom->getTrq( Tb );
101 <      dAtom->lab2Body( Tb );
102 <      
99 >
100 >      integrableObjects[i]->getTrq( Tb );
101 >      integrableObjects[i]->lab2Body( Tb );
102 >
103        // get the angular momentum, and propagate a half step
104  
105 <      dAtom->getJ( ji );
105 >      integrableObjects[i]->getJ( ji );
106  
107 <      for (j=0; j < 3; j++)
107 >      for (j=0; j < 3; j++)
108          ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
72      
73      // use the angular velocities to propagate the rotation matrix a
74      // full time step
109  
110 <      dAtom->getA(A);
77 <      dAtom->getI(I);
78 <    
79 <      // rotate about the x-axis      
80 <      angle = dt2 * ji[0] / I[0][0];
81 <      this->rotate( 1, 2, angle, ji, A );
110 >      this->rotationPropagation( integrableObjects[i], ji );
111  
112 <      // rotate about the y-axis
113 <      angle = dt2 * ji[1] / I[1][1];
85 <      this->rotate( 2, 0, angle, ji, A );
86 <      
87 <      // rotate about the z-axis
88 <      angle = dt * ji[2] / I[2][2];
89 <      this->rotate( 0, 1, angle, ji, A);
90 <      
91 <      // rotate about the y-axis
92 <      angle = dt2 * ji[1] / I[1][1];
93 <      this->rotate( 2, 0, angle, ji, A );
94 <      
95 <       // rotate about the x-axis
96 <      angle = dt2 * ji[0] / I[0][0];
97 <      this->rotate( 1, 2, angle, ji, A );
98 <      
99 <      dAtom->setJ( ji );
100 <      dAtom->setA( A  );    
101 <    }    
112 >      integrableObjects[i]->setJ( ji );
113 >    }
114    }
115 +
116 +  if (nConstrained){
117 +    constrainA();
118 +  }
119 +
120 +  // Finally, evolve chi a half step (just like a velocity) using
121 +  // temperature at time t, not time t+dt/2
122 +
123 +  chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat);
124 +  integralOfChidt += chi*dt2;
125 +
126   }
127  
128   template<typename T> void NVT<T>::moveB( void ){
129 <  int i, j;
107 <  DirectionalAtom* dAtom;
129 >  int i, j, k;
130    double Tb[3], ji[3];
131    double vel[3], frc[3];
132    double mass;
111
133    double instTemp;
134 <  
114 <  instTemp = tStats->getTemperature();
115 <  chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat);
116 <  
117 <  for( i=0; i<nAtoms; i++ ){
134 >  double oldChi, prevChi;
135  
136 <    atoms[i]->getVel( vel );
120 <    atoms[i]->getFrc( frc );
136 >  // Set things up for the iteration:
137  
138 <    mass = atoms[i]->getMass();
138 >  oldChi = chi;
139  
140 <    // velocity half step
125 <    for (j=0; j < 3; j++)
126 <      vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi);
127 <    
128 <    atoms[i]->setVel( vel );
140 >  for( i=0; i < integrableObjects.size(); i++ ){
141  
142 <    if( atoms[i]->isDirectional() ){
142 >    integrableObjects[i]->getVel( vel );
143  
144 <      dAtom = (DirectionalAtom *)atoms[i];
144 >    for (j=0; j < 3; j++)
145 >      oldVel[3*i + j]  = vel[j];
146  
147 <      // get and convert the torque to body frame      
147 >    if( integrableObjects[i]->isDirectional() ){
148  
149 <      dAtom->getTrq( Tb );
137 <      dAtom->lab2Body( Tb );
149 >      integrableObjects[i]->getJ( ji );
150  
151 <      // get the angular momentum, and propagate a half step
151 >      for (j=0; j < 3; j++)
152 >        oldJi[3*i + j] = ji[j];
153  
154 <      dAtom->getJ( ji );
154 >    }
155 >  }
156  
157 <      for (j=0; j < 3; j++)
144 <        ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
145 <      
157 >  // do the iteration:
158  
159 <      dAtom->setJ( ji );
159 >  for (k=0; k < 4; k++) {
160 >
161 >    instTemp = tStats->getTemperature();
162 >
163 >    // evolve chi another half step using the temperature at t + dt/2
164 >
165 >    prevChi = chi;
166 >    chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) /
167 >      (tauThermostat*tauThermostat);
168 >
169 >    for( i=0; i < integrableObjects.size(); i++ ){
170 >
171 >      integrableObjects[i]->getFrc( frc );
172 >      integrableObjects[i]->getVel(vel);
173 >
174 >      mass = integrableObjects[i]->getMass();
175 >
176 >      // velocity half step
177 >      for (j=0; j < 3; j++)
178 >        vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi);
179 >
180 >      integrableObjects[i]->setVel( vel );
181 >
182 >      if( integrableObjects[i]->isDirectional() ){
183 >
184 >        // get and convert the torque to body frame
185 >
186 >        integrableObjects[i]->getTrq( Tb );
187 >        integrableObjects[i]->lab2Body( Tb );
188 >
189 >        for (j=0; j < 3; j++)
190 >          ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
191 >
192 >        integrableObjects[i]->setJ( ji );
193 >      }
194      }
195 +
196 +    if (nConstrained){
197 +      constrainB();
198 +    }
199 +
200 +    if (fabs(prevChi - chi) <= chiTolerance) break;
201    }
202 +
203 +  integralOfChidt += dt2*chi;
204   }
205  
206 + template<typename T> void NVT<T>::resetIntegrator( void ){
207 +
208 +  chi = 0.0;
209 +  integralOfChidt = 0.0;
210 + }
211 +
212   template<typename T> int NVT<T>::readyCheck() {
213  
214    //check parent's readyCheck() first
215    if (T::readyCheck() == -1)
216      return -1;
217 <  
218 <  // First check to see if we have a target temperature.
219 <  // Not having one is fatal.
220 <  
217 >
218 >  // First check to see if we have a target temperature.
219 >  // Not having one is fatal.
220 >
221    if (!have_target_temp) {
222      sprintf( painCave.errMsg,
223               "NVT error: You can't use the NVT integrator without a targetTemp!\n"
# Line 166 | Line 226 | template<typename T> int NVT<T>::readyCheck() {
226      simError();
227      return -1;
228    }
229 <  
229 >
230    // We must set tauThermostat.
231 <  
231 >
232    if (!have_tau_thermostat) {
233      sprintf( painCave.errMsg,
234               "NVT error: If you use the constant temperature\n"
# Line 176 | Line 236 | template<typename T> int NVT<T>::readyCheck() {
236      painCave.isFatal = 1;
237      simError();
238      return -1;
239 <  }    
239 >  }
240 >
241 >  if (!have_chi_tolerance) {
242 >    sprintf( painCave.errMsg,
243 >             "NVT warning: setting chi tolerance to 1e-6\n");
244 >    chiTolerance = 1e-6;
245 >    have_chi_tolerance = 1;
246 >    painCave.isFatal = 0;
247 >    simError();
248 >  }
249 >
250    return 1;
251 +
252   }
253 +
254 + template<typename T> double NVT<T>::getConservedQuantity(void){
255 +
256 +  double conservedQuantity;
257 +  double fkBT;
258 +  double Energy;
259 +  double thermostat_kinetic;
260 +  double thermostat_potential;
261 +
262 +  fkBT = (double)(info->getNDF()    ) * kB * targetTemp;
263 +
264 +  Energy = tStats->getTotalE();
265 +
266 +  thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi /
267 +    (2.0 * eConvert);
268 +
269 +  thermostat_potential = fkBT * integralOfChidt / eConvert;
270 +
271 +  conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
272 +
273 +  return conservedQuantity;
274 + }
275 +
276 + template<typename T> string NVT<T>::getAdditionalParameters(void){
277 +  string parameters;
278 +  const int BUFFERSIZE = 2000; // size of the read buffer
279 +  char buffer[BUFFERSIZE];
280 +
281 +  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
282 +  parameters += buffer;
283 +
284 +  return parameters;
285 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines