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 |
// Finally, evolve chi a half step (just like a velocity) using |
113 |
// temperature at time t, not time t+dt/2 |
114 |
|
115 |
chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat); |
116 |
integralOfChidt += chi*dt2; |
117 |
|
118 |
} |
119 |
|
120 |
template<typename T> void NVT<T>::moveB( void ){ |
121 |
int i, j, k; |
122 |
DirectionalAtom* dAtom; |
123 |
double Tb[3], ji[3]; |
124 |
double vel[3], frc[3]; |
125 |
double mass; |
126 |
double instTemp; |
127 |
double oldChi, prevChi; |
128 |
|
129 |
// Set things up for the iteration: |
130 |
|
131 |
oldChi = chi; |
132 |
|
133 |
for( i=0; i<nAtoms; i++ ){ |
134 |
|
135 |
atoms[i]->getVel( vel ); |
136 |
|
137 |
for (j=0; j < 3; j++) |
138 |
oldVel[3*i + j] = vel[j]; |
139 |
|
140 |
if( atoms[i]->isDirectional() ){ |
141 |
|
142 |
dAtom = (DirectionalAtom *)atoms[i]; |
143 |
|
144 |
dAtom->getJ( ji ); |
145 |
|
146 |
for (j=0; j < 3; j++) |
147 |
oldJi[3*i + j] = ji[j]; |
148 |
|
149 |
} |
150 |
} |
151 |
|
152 |
// do the iteration: |
153 |
|
154 |
for (k=0; k < 4; k++) { |
155 |
|
156 |
instTemp = tStats->getTemperature(); |
157 |
|
158 |
// evolve chi another half step using the temperature at t + dt/2 |
159 |
|
160 |
prevChi = chi; |
161 |
chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) / |
162 |
(tauThermostat*tauThermostat); |
163 |
|
164 |
for( i=0; i<nAtoms; i++ ){ |
165 |
|
166 |
atoms[i]->getFrc( frc ); |
167 |
atoms[i]->getVel(vel); |
168 |
|
169 |
mass = atoms[i]->getMass(); |
170 |
|
171 |
// velocity half step |
172 |
for (j=0; j < 3; j++) |
173 |
vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi); |
174 |
|
175 |
atoms[i]->setVel( vel ); |
176 |
|
177 |
if( atoms[i]->isDirectional() ){ |
178 |
|
179 |
dAtom = (DirectionalAtom *)atoms[i]; |
180 |
|
181 |
// get and convert the torque to body frame |
182 |
|
183 |
dAtom->getTrq( Tb ); |
184 |
dAtom->lab2Body( Tb ); |
185 |
|
186 |
for (j=0; j < 3; j++) |
187 |
ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); |
188 |
|
189 |
dAtom->setJ( ji ); |
190 |
} |
191 |
} |
192 |
|
193 |
if (fabs(prevChi - chi) <= chiTolerance) break; |
194 |
} |
195 |
|
196 |
integralOfChidt += dt2*chi; |
197 |
} |
198 |
|
199 |
template<typename T> void NVT<T>::resetIntegrator( void ){ |
200 |
|
201 |
chi = 0.0; |
202 |
integralOfChidt = 0.0; |
203 |
} |
204 |
|
205 |
template<typename T> int NVT<T>::readyCheck() { |
206 |
|
207 |
//check parent's readyCheck() first |
208 |
if (T::readyCheck() == -1) |
209 |
return -1; |
210 |
|
211 |
// First check to see if we have a target temperature. |
212 |
// Not having one is fatal. |
213 |
|
214 |
if (!have_target_temp) { |
215 |
sprintf( painCave.errMsg, |
216 |
"NVT error: You can't use the NVT integrator without a targetTemp!\n" |
217 |
); |
218 |
painCave.isFatal = 1; |
219 |
simError(); |
220 |
return -1; |
221 |
} |
222 |
|
223 |
// We must set tauThermostat. |
224 |
|
225 |
if (!have_tau_thermostat) { |
226 |
sprintf( painCave.errMsg, |
227 |
"NVT error: If you use the constant temperature\n" |
228 |
" integrator, you must set tauThermostat.\n"); |
229 |
painCave.isFatal = 1; |
230 |
simError(); |
231 |
return -1; |
232 |
} |
233 |
|
234 |
if (!have_chi_tolerance) { |
235 |
sprintf( painCave.errMsg, |
236 |
"NVT warning: setting chi tolerance to 1e-6\n"); |
237 |
chiTolerance = 1e-6; |
238 |
have_chi_tolerance = 1; |
239 |
painCave.isFatal = 0; |
240 |
simError(); |
241 |
} |
242 |
|
243 |
return 1; |
244 |
|
245 |
} |
246 |
|
247 |
template<typename T> double NVT<T>::getConservedQuantity(void){ |
248 |
|
249 |
double conservedQuantity; |
250 |
double E_NVT; |
251 |
|
252 |
//HNVE |
253 |
conservedQuantity = tStats->getTotalE(); |
254 |
//HNVE |
255 |
|
256 |
E_NVT = (info->getNDF() * kB * targetTemp * |
257 |
(integralOfChidt + tauThermostat * tauThermostat * chi * chi / 2.0 )) / eConvert; |
258 |
|
259 |
conservedQuantity += E_NVT; |
260 |
|
261 |
//cerr << info->getTime() << "\t" << chi << "\t" << integralOfChidt << "\t" << E_NVT << endl; |
262 |
|
263 |
return conservedQuantity; |
264 |
} |