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 |
template<typename T> NVT<T>::NVT ( SimInfo *theInfo, ForceFields* the_ff): |
15 |
T( theInfo, the_ff ) |
16 |
{ |
17 |
chi = 0.0; |
18 |
have_tau_thermostat = 0; |
19 |
have_target_temp = 0; |
20 |
have_chi_tolerance = 0; |
21 |
integralOfChidt = 0.0; |
22 |
|
23 |
oldVel = new double[3*nAtoms]; |
24 |
oldJi = new double[3*nAtoms]; |
25 |
} |
26 |
|
27 |
template<typename T> NVT<T>::~NVT() { |
28 |
delete[] oldVel; |
29 |
delete[] oldJi; |
30 |
} |
31 |
|
32 |
template<typename T> void NVT<T>::moveA() { |
33 |
|
34 |
int i, j; |
35 |
DirectionalAtom* dAtom; |
36 |
double Tb[3], ji[3]; |
37 |
double A[3][3], I[3][3]; |
38 |
double angle, mass; |
39 |
double vel[3], pos[3], frc[3]; |
40 |
|
41 |
double instTemp; |
42 |
|
43 |
// We need the temperature at time = t for the chi update below: |
44 |
|
45 |
instTemp = tStats->getTemperature(); |
46 |
|
47 |
for( i=0; i<nAtoms; i++ ){ |
48 |
|
49 |
atoms[i]->getVel( vel ); |
50 |
atoms[i]->getPos( pos ); |
51 |
atoms[i]->getFrc( frc ); |
52 |
|
53 |
mass = atoms[i]->getMass(); |
54 |
|
55 |
for (j=0; j < 3; j++) { |
56 |
// velocity half step (use chi from previous step here): |
57 |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi); |
58 |
// position whole step |
59 |
pos[j] += dt * vel[j]; |
60 |
} |
61 |
|
62 |
atoms[i]->setVel( vel ); |
63 |
atoms[i]->setPos( pos ); |
64 |
|
65 |
if( atoms[i]->isDirectional() ){ |
66 |
|
67 |
dAtom = (DirectionalAtom *)atoms[i]; |
68 |
|
69 |
// get and convert the torque to body frame |
70 |
|
71 |
dAtom->getTrq( Tb ); |
72 |
dAtom->lab2Body( Tb ); |
73 |
|
74 |
// get the angular momentum, and propagate a half step |
75 |
|
76 |
dAtom->getJ( ji ); |
77 |
|
78 |
for (j=0; j < 3; j++) |
79 |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
80 |
|
81 |
// use the angular velocities to propagate the rotation matrix a |
82 |
// full time step |
83 |
|
84 |
dAtom->getA(A); |
85 |
dAtom->getI(I); |
86 |
|
87 |
// rotate about the x-axis |
88 |
angle = dt2 * ji[0] / I[0][0]; |
89 |
this->rotate( 1, 2, 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 z-axis |
96 |
angle = dt * ji[2] / I[2][2]; |
97 |
this->rotate( 0, 1, angle, ji, A); |
98 |
|
99 |
// rotate about the y-axis |
100 |
angle = dt2 * ji[1] / I[1][1]; |
101 |
this->rotate( 2, 0, angle, ji, A ); |
102 |
|
103 |
// rotate about the x-axis |
104 |
angle = dt2 * ji[0] / I[0][0]; |
105 |
this->rotate( 1, 2, angle, ji, A ); |
106 |
|
107 |
dAtom->setJ( ji ); |
108 |
dAtom->setA( A ); |
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 E_NVT; |
259 |
|
260 |
//HNVE |
261 |
conservedQuantity = tStats->getTotalE(); |
262 |
//HNVE |
263 |
|
264 |
E_NVT = (info->getNDF() * kB * targetTemp * |
265 |
(integralOfChidt + tauThermostat * tauThermostat * chi * chi / 2.0 )) / eConvert; |
266 |
|
267 |
conservedQuantity += E_NVT; |
268 |
|
269 |
//cerr << info->getTime() << "\t" << chi << "\t" << integralOfChidt << "\t" << E_NVT << endl; |
270 |
|
271 |
return conservedQuantity; |
272 |
} |