ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NVT.cpp
Revision: 853
Committed: Thu Nov 6 19:11:38 2003 UTC (20 years, 8 months ago) by mmeineke
File size: 6154 byte(s)
Log Message:
did a merge by hand from the new-templateless branch to the main trunk.

bug Fixes include:
  * fixed the switching function from ortho to non-ortho box.
         !!!!! THis was responsible for all of the sudden deaths we saw.
  * some formating in the string when we write out the extended system state.
  * added NPT.cpp to the makefile.in

File Contents

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