1 |
#include <cmath> |
2 |
#include "Atom.hpp" |
3 |
#include "Molecule.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 isotropic thermostating and barostating via the Melchionna |
15 |
// modification of the Hoover algorithm: |
16 |
// |
17 |
// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, |
18 |
// Molec. Phys., 78, 533. |
19 |
// |
20 |
// and |
21 |
// |
22 |
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
23 |
|
24 |
// The NPTzm variant scales the molecular center-of-mass coordinates |
25 |
// instead of the atomic coordinates |
26 |
|
27 |
template<typename T> NPTzm<T>::NPTzm ( SimInfo *theInfo, ForceFields* the_ff): |
28 |
T( theInfo, the_ff ) |
29 |
{ |
30 |
chi = 0.0; |
31 |
eta = 0.0; |
32 |
etaZ = 0.0; |
33 |
have_tau_thermostat = 0; |
34 |
have_tau_barostat = 0; |
35 |
have_target_temp = 0; |
36 |
have_target_pressure = 0; |
37 |
} |
38 |
|
39 |
template<typename T> void NPTzm<T>::moveA() { |
40 |
|
41 |
int i, j, k; |
42 |
DirectionalAtom* dAtom; |
43 |
double Tb[3], ji[3]; |
44 |
double A[3][3], I[3][3]; |
45 |
double angle, mass; |
46 |
double vel[3], pos[3], frc[3]; |
47 |
|
48 |
double rj[3]; |
49 |
double instaTemp, instaPress, instaPressZ, instaVol; |
50 |
double tt2, tb2, scaleFactor, scaleFactorZ, scaleFactorXY, bigFactor, etaXY; |
51 |
double hm[3][3], hmnew[3][3], scaleMat[3][3]; |
52 |
double scF3, scFxy2; |
53 |
|
54 |
|
55 |
int nInMol; |
56 |
double rc[3]; |
57 |
|
58 |
nMols = info->n_mol; |
59 |
myMolecules = info->molecules; |
60 |
|
61 |
tt2 = tauThermostat * tauThermostat; |
62 |
tb2 = tauBarostat * tauBarostat; |
63 |
|
64 |
instaTemp = tStats->getTemperature(); |
65 |
instaPress = tStats->getPressure(); |
66 |
instaPressZ = tStats->getPressureZ(); |
67 |
instaVol = tStats->getVolume(); |
68 |
|
69 |
// first evolve chi a half step |
70 |
|
71 |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
72 |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
73 |
(p_convert*NkBT*tb2)); |
74 |
etaZ += dt2 * ( instaVol * (instaPressZ - targetPressure) / |
75 |
(p_convert*NkBT*tb2)); |
76 |
|
77 |
scaleFactorZ = exp(dt*etaZ); |
78 |
scaleFactor = exp(dt*eta); |
79 |
|
80 |
scF3 = scaleFactor * scaleFactor * scaleFactor; |
81 |
scFxy2 = scF3 / scaleFactorZ; |
82 |
scaleFactorXY = sqrt( scFxy2 ); |
83 |
|
84 |
etaXY = log( scaleFactorXY ) / dt; |
85 |
|
86 |
for( i = 0; i < nMols; i++) { |
87 |
|
88 |
myMolecules[i].getCOM(rc); |
89 |
|
90 |
nInMol = myMolecules[i].getNAtoms(); |
91 |
myAtoms = myMolecules[i].getMyAtoms(); |
92 |
|
93 |
// find the minimum image coordinates of the molecular centers of mass: |
94 |
|
95 |
info->wrapVector(rc); |
96 |
|
97 |
for (j = 0; j < nInMol; j++) { |
98 |
|
99 |
if(myAtoms[j] != NULL) { |
100 |
|
101 |
myAtoms[j]->getVel( vel ); |
102 |
myAtoms[j]->getPos( pos ); |
103 |
myAtoms[j]->getFrc( frc ); |
104 |
|
105 |
mass = myAtoms[j]->getMass(); |
106 |
|
107 |
for (k=0; k < 2; k++) |
108 |
vel[k] += dt2 * ((frc[k] / mass ) * eConvert - vel[k]*(chi+etaXY)); |
109 |
vel[2] += dt2 * ((frc[2] / mass ) * eConvert - vel[2]*(chi+etaZ)); |
110 |
|
111 |
myAtoms[j]->setVel( vel ); |
112 |
|
113 |
for (k = 0; k < 2; k++) |
114 |
pos[k] += dt * (vel[k] + etaXY*rc[k]); |
115 |
pos[2] += dt * (vel[2] + etaZ*rc[2]); |
116 |
|
117 |
myAtoms[j]->setPos( pos ); |
118 |
|
119 |
if( myAtoms[j]->isDirectional() ){ |
120 |
|
121 |
dAtom = (DirectionalAtom *)myAtoms[j]; |
122 |
|
123 |
// get and convert the torque to body frame |
124 |
|
125 |
dAtom->getTrq( Tb ); |
126 |
dAtom->lab2Body( Tb ); |
127 |
|
128 |
// get the angular momentum, and propagate a half step |
129 |
|
130 |
dAtom->getJ( ji ); |
131 |
|
132 |
for (k=0; k < 3; k++) |
133 |
ji[k] += dt2 * (Tb[k] * eConvert - ji[k]*chi); |
134 |
|
135 |
// use the angular velocities to propagate the rotation matrix a |
136 |
// full time step |
137 |
|
138 |
dAtom->getA(A); |
139 |
dAtom->getI(I); |
140 |
|
141 |
// rotate about the x-axis |
142 |
angle = dt2 * ji[0] / I[0][0]; |
143 |
this->rotate( 1, 2, angle, ji, A ); |
144 |
|
145 |
// rotate about the y-axis |
146 |
angle = dt2 * ji[1] / I[1][1]; |
147 |
this->rotate( 2, 0, angle, ji, A ); |
148 |
|
149 |
// rotate about the z-axis |
150 |
angle = dt * ji[2] / I[2][2]; |
151 |
this->rotate( 0, 1, angle, ji, A); |
152 |
|
153 |
// rotate about the y-axis |
154 |
angle = dt2 * ji[1] / I[1][1]; |
155 |
this->rotate( 2, 0, angle, ji, A ); |
156 |
|
157 |
// rotate about the x-axis |
158 |
angle = dt2 * ji[0] / I[0][0]; |
159 |
this->rotate( 1, 2, angle, ji, A ); |
160 |
|
161 |
dAtom->setJ( ji ); |
162 |
dAtom->setA( A ); |
163 |
} |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
// Scale the box after all the positions have been moved: |
169 |
|
170 |
|
171 |
|
172 |
bigFactor = abs( 1.0 - scaleFactorZ ); |
173 |
if( abs(1.0 - scaleFactor) > bigFactor ) |
174 |
bigFactor = abs(1.0 - scaleFactor); |
175 |
|
176 |
if (bigFactor > 0.1) { |
177 |
sprintf( painCave.errMsg, |
178 |
"NPTzm error: Attempting a Box scaling of more than 10 percent" |
179 |
" check your tauBarostat, as it is probably too small!\n" |
180 |
" eta = %lf, scaleFactor = %lf\n" |
181 |
" etaZ = %lf, scaleFactorZ = %lf\n", |
182 |
eta, scaleFactor, |
183 |
etaZ, scaleFactorZ |
184 |
); |
185 |
painCave.isFatal = 1; |
186 |
simError(); |
187 |
} else { |
188 |
|
189 |
for(i=0;i<3;i++) |
190 |
for(j=0;j<3;j++) |
191 |
scaleBox[i][j] = 0.0; |
192 |
|
193 |
|
194 |
|
195 |
scaleBox[0][0] = scaleFactorXY; |
196 |
scaleBox[1][1] = scaleFactorXY; |
197 |
scaleBox[2][2] = scaleFactorZ; |
198 |
|
199 |
info->getBoxM( hm ); |
200 |
info->matMul3( hm, scaleBox, hmnew ); |
201 |
|
202 |
info->setBoxM( hmnew ); |
203 |
} |
204 |
} |
205 |
|
206 |
template<typename T> void NPTzm<T>::moveB( void ){ |
207 |
int i, j; |
208 |
DirectionalAtom* dAtom; |
209 |
double Tb[3], ji[3]; |
210 |
double vel[3], frc[3]; |
211 |
double mass; |
212 |
double scaleFactor, scaleFactorZ, scaleFactorXY, bigFactor, etaXY; |
213 |
double instaTemp, instaPress, instaPressZ, instaVol, etaXY; |
214 |
double scF3, scFxy2; |
215 |
|
216 |
double tt2, tb2; |
217 |
|
218 |
tt2 = tauThermostat * tauThermostat; |
219 |
tb2 = tauBarostat * tauBarostat; |
220 |
|
221 |
instaTemp = tStats->getTemperature(); |
222 |
instaPress = tStats->getPressure(); |
223 |
instaPressZ = tStats->getPressureZ(); |
224 |
instaVol = tStats->getVolume(); |
225 |
|
226 |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
227 |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
228 |
(p_convert*NkBT*tb2)); |
229 |
etaZ += dt2 * ( instaVol * (instaPressZ - targetPressure) / |
230 |
(p_convert*NkBT*tb2)); |
231 |
|
232 |
scaleFactorZ = exp(dt*etaZ); |
233 |
scaleFactor = exp(dt*eta); |
234 |
|
235 |
scF3 = scaleFactor * scaleFactor * scaleFactor; |
236 |
scFxy2 = scF3 / scaleFactorZ; |
237 |
scaleFactorXY = sqrt( scFxy2 ); |
238 |
|
239 |
etaXY = log( scaleFactorXY ) / dt; |
240 |
|
241 |
for( i=0; i<nAtoms; i++ ){ |
242 |
|
243 |
atoms[i]->getVel( vel ); |
244 |
atoms[i]->getFrc( frc ); |
245 |
|
246 |
mass = atoms[i]->getMass(); |
247 |
|
248 |
// velocity half step |
249 |
for (j=0; j < 2; j++) |
250 |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+etaXY)); |
251 |
vel[2] += dt2 * ((frc[2] / mass ) * eConvert - vel[2]*(chi+etaZ)); |
252 |
|
253 |
atoms[i]->setVel( vel ); |
254 |
|
255 |
if( atoms[i]->isDirectional() ){ |
256 |
|
257 |
dAtom = (DirectionalAtom *)atoms[i]; |
258 |
|
259 |
// get and convert the torque to body frame |
260 |
|
261 |
dAtom->getTrq( Tb ); |
262 |
dAtom->lab2Body( Tb ); |
263 |
|
264 |
// get the angular momentum, and propagate a half step |
265 |
|
266 |
dAtom->getJ( ji ); |
267 |
|
268 |
for (j=0; j < 3; j++) |
269 |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
270 |
|
271 |
dAtom->setJ( ji ); |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
template<typename T> void NPTzm<T>::resetIntegrator() { |
277 |
chi = 0.0; |
278 |
eta = 0.0; |
279 |
etaZ = 0.0; |
280 |
} |
281 |
|
282 |
template<typename T> int NPTzm<T>::readyCheck() { |
283 |
|
284 |
//check parent's readyCheck() first |
285 |
if (T::readyCheck() == -1) |
286 |
return -1; |
287 |
|
288 |
// First check to see if we have a target temperature. |
289 |
// Not having one is fatal. |
290 |
|
291 |
if (!have_target_temp) { |
292 |
sprintf( painCave.errMsg, |
293 |
"NPTzm error: You can't use the NPTzm integrator\n" |
294 |
" without a targetTemp!\n" |
295 |
); |
296 |
painCave.isFatal = 1; |
297 |
simError(); |
298 |
return -1; |
299 |
} |
300 |
|
301 |
if (!have_target_pressure) { |
302 |
sprintf( painCave.errMsg, |
303 |
"NPTzm error: You can't use the NPTzm integrator\n" |
304 |
" without a targetPressure!\n" |
305 |
); |
306 |
painCave.isFatal = 1; |
307 |
simError(); |
308 |
return -1; |
309 |
} |
310 |
|
311 |
// We must set tauThermostat. |
312 |
|
313 |
if (!have_tau_thermostat) { |
314 |
sprintf( painCave.errMsg, |
315 |
"NPTzm error: If you use the NPTzm\n" |
316 |
" integrator, you must set tauThermostat.\n"); |
317 |
painCave.isFatal = 1; |
318 |
simError(); |
319 |
return -1; |
320 |
} |
321 |
|
322 |
// We must set tauBarostat. |
323 |
|
324 |
if (!have_tau_barostat) { |
325 |
sprintf( painCave.errMsg, |
326 |
"NPTzm error: If you use the NPTzm\n" |
327 |
" integrator, you must set tauBarostat.\n"); |
328 |
painCave.isFatal = 1; |
329 |
simError(); |
330 |
return -1; |
331 |
} |
332 |
|
333 |
// We need NkBT a lot, so just set it here: |
334 |
|
335 |
NkBT = (double)info->ndf * kB * targetTemp; |
336 |
|
337 |
return 1; |
338 |
} |