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