1 |
#include <stdlib.h> |
2 |
#include <math.h> |
3 |
|
4 |
#include "Atom.hpp" |
5 |
#include "SRI.hpp" |
6 |
#include "AbstractClasses.hpp" |
7 |
#include "SimInfo.hpp" |
8 |
#include "ForceFields.hpp" |
9 |
#include "Thermo.hpp" |
10 |
#include "ReadWrite.hpp" |
11 |
#include "Integrator.hpp" |
12 |
#include "simError.h" |
13 |
|
14 |
#ifdef IS_MPI |
15 |
#include "mpiSimulation.hpp" |
16 |
#endif |
17 |
|
18 |
|
19 |
// Basic isotropic thermostating and barostating via the Melchionna |
20 |
// modification of the Hoover algorithm: |
21 |
// |
22 |
// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, |
23 |
// Molec. Phys., 78, 533. |
24 |
// |
25 |
// and |
26 |
// |
27 |
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
28 |
|
29 |
NPT::NPT ( SimInfo *theInfo, ForceFields* the_ff): |
30 |
Integrator( theInfo, the_ff ) |
31 |
{ |
32 |
GenericData* data; |
33 |
|
34 |
chi = 0.0; |
35 |
integralOfChidt = 0.0; |
36 |
have_tau_thermostat = 0; |
37 |
have_tau_barostat = 0; |
38 |
have_target_temp = 0; |
39 |
have_target_pressure = 0; |
40 |
have_chi_tolerance = 0; |
41 |
have_eta_tolerance = 0; |
42 |
have_pos_iter_tolerance = 0; |
43 |
|
44 |
// retrieve chi and integralOfChidt from simInfo |
45 |
data = info->getProperty(CHIVALUE_ID); |
46 |
if(data != NULL ){ |
47 |
chi = data->getDval(); |
48 |
} |
49 |
|
50 |
data = info->getProperty(INTEGRALOFCHIDT_ID); |
51 |
if(data != NULL ){ |
52 |
integralOfChidt = data->getDval(); |
53 |
} |
54 |
|
55 |
oldPos = new double[3*nAtoms]; |
56 |
oldVel = new double[3*nAtoms]; |
57 |
oldJi = new double[3*nAtoms]; |
58 |
#ifdef IS_MPI |
59 |
Nparticles = mpiSim->getTotAtoms(); |
60 |
#else |
61 |
Nparticles = theInfo->n_atoms; |
62 |
#endif |
63 |
|
64 |
} |
65 |
|
66 |
NPT::~NPT() { |
67 |
delete[] oldPos; |
68 |
delete[] oldVel; |
69 |
delete[] oldJi; |
70 |
} |
71 |
|
72 |
void NPT::moveA() { |
73 |
|
74 |
//new version of NPT |
75 |
int i, j, k; |
76 |
DirectionalAtom* dAtom; |
77 |
double Tb[3], ji[3]; |
78 |
double mass; |
79 |
double vel[3], pos[3], frc[3]; |
80 |
double sc[3]; |
81 |
double COM[3]; |
82 |
|
83 |
instaTemp = tStats->getTemperature(); |
84 |
|
85 |
tStats->getPressureTensor( press ); |
86 |
instaPress = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0; |
87 |
|
88 |
instaVol = tStats->getVolume(); |
89 |
|
90 |
tStats->getCOM(COM); |
91 |
|
92 |
//evolve velocity half step |
93 |
for( i=0; i<nAtoms; i++ ){ |
94 |
|
95 |
atoms[i]->getVel( vel ); |
96 |
atoms[i]->getFrc( frc ); |
97 |
|
98 |
mass = atoms[i]->getMass(); |
99 |
|
100 |
getVelScaleA( sc, vel ); |
101 |
|
102 |
for (j=0; j < 3; j++) { |
103 |
|
104 |
// velocity half step (use chi from previous step here): |
105 |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - sc[j]); |
106 |
|
107 |
} |
108 |
|
109 |
atoms[i]->setVel( vel ); |
110 |
|
111 |
if( atoms[i]->isDirectional() ){ |
112 |
|
113 |
dAtom = (DirectionalAtom *)atoms[i]; |
114 |
|
115 |
// get and convert the torque to body frame |
116 |
|
117 |
dAtom->getTrq( Tb ); |
118 |
dAtom->lab2Body( Tb ); |
119 |
|
120 |
// get the angular momentum, and propagate a half step |
121 |
|
122 |
dAtom->getJ( ji ); |
123 |
|
124 |
for (j=0; j < 3; j++) |
125 |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
126 |
|
127 |
this->rotationPropagation( dAtom, ji ); |
128 |
|
129 |
dAtom->setJ( ji ); |
130 |
} |
131 |
} |
132 |
|
133 |
// evolve chi and eta half step |
134 |
|
135 |
evolveChiA(); |
136 |
evolveEtaA(); |
137 |
|
138 |
//calculate the integral of chidt |
139 |
integralOfChidt += dt2*chi; |
140 |
|
141 |
//save the old positions |
142 |
for(i = 0; i < nAtoms; i++){ |
143 |
atoms[i]->getPos(pos); |
144 |
for(j = 0; j < 3; j++) |
145 |
oldPos[i*3 + j] = pos[j]; |
146 |
} |
147 |
|
148 |
//the first estimation of r(t+dt) is equal to r(t) |
149 |
|
150 |
for(k = 0; k < 5; k ++){ |
151 |
|
152 |
for(i =0 ; i < nAtoms; i++){ |
153 |
|
154 |
atoms[i]->getVel(vel); |
155 |
atoms[i]->getPos(pos); |
156 |
|
157 |
this->getPosScale( pos, COM, i, sc ); |
158 |
|
159 |
for(j = 0; j < 3; j++) |
160 |
pos[j] = oldPos[i*3 + j] + dt*(vel[j] + sc[j]); |
161 |
|
162 |
atoms[i]->setPos( pos ); |
163 |
} |
164 |
|
165 |
if (nConstrained){ |
166 |
constrainA(); |
167 |
} |
168 |
} |
169 |
|
170 |
|
171 |
// Scale the box after all the positions have been moved: |
172 |
|
173 |
this->scaleSimBox(); |
174 |
} |
175 |
|
176 |
void NPT::moveB( void ){ |
177 |
|
178 |
//new version of NPT |
179 |
int i, j, k; |
180 |
DirectionalAtom* dAtom; |
181 |
double Tb[3], ji[3], sc[3]; |
182 |
double vel[3], frc[3], qVel[3]; |
183 |
double mass; |
184 |
|
185 |
// Set things up for the iteration: |
186 |
|
187 |
for( i=0; i<nAtoms; i++ ){ |
188 |
|
189 |
atoms[i]->getVel( vel ); |
190 |
|
191 |
for (j=0; j < 3; j++) |
192 |
oldVel[3*i + j] = vel[j]; |
193 |
|
194 |
if( atoms[i]->isDirectional() ){ |
195 |
|
196 |
dAtom = (DirectionalAtom *)atoms[i]; |
197 |
|
198 |
dAtom->getJ( ji ); |
199 |
|
200 |
for (j=0; j < 3; j++) |
201 |
oldJi[3*i + j] = ji[j]; |
202 |
|
203 |
} |
204 |
} |
205 |
|
206 |
// do the iteration: |
207 |
|
208 |
instaVol = tStats->getVolume(); |
209 |
|
210 |
for (k=0; k < 4; k++) { |
211 |
|
212 |
atoms[0]->getVel(vel); |
213 |
|
214 |
instaTemp = tStats->getTemperature(); |
215 |
|
216 |
instaPress = tStats->getPressure(); |
217 |
|
218 |
// evolve chi another half step using the temperature at t + dt/2 |
219 |
|
220 |
this->evolveChiB(); |
221 |
this->evolveEtaB(); |
222 |
|
223 |
for( i=0; i<nAtoms; i++ ){ |
224 |
|
225 |
atoms[i]->getFrc( frc ); |
226 |
atoms[i]->getVel(vel); |
227 |
|
228 |
mass = atoms[i]->getMass(); |
229 |
|
230 |
getVelScaleB( sc, i ); |
231 |
|
232 |
for(j=0;j<3;j++) |
233 |
qVel[j] = vel[j]; |
234 |
|
235 |
// velocity half step |
236 |
for (j=0; j < 3; j++) |
237 |
vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - sc[j]); |
238 |
|
239 |
atoms[i]->setVel( vel ); |
240 |
|
241 |
if( atoms[i]->isDirectional() ){ |
242 |
|
243 |
dAtom = (DirectionalAtom *)atoms[i]; |
244 |
|
245 |
// get and convert the torque to body frame |
246 |
|
247 |
dAtom->getTrq( Tb ); |
248 |
dAtom->lab2Body( Tb ); |
249 |
|
250 |
for (j=0; j < 3; j++) |
251 |
ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); |
252 |
|
253 |
dAtom->setJ( ji ); |
254 |
} |
255 |
} |
256 |
|
257 |
if (nConstrained){ |
258 |
constrainB(); |
259 |
} |
260 |
|
261 |
if ( this->chiConverged() && this->etaConverged() ) break; |
262 |
} |
263 |
|
264 |
//calculate integral of chida |
265 |
integralOfChidt += dt2*chi; |
266 |
|
267 |
|
268 |
} |
269 |
|
270 |
void NPT::resetIntegrator() { |
271 |
chi = 0.0; |
272 |
Integrator::resetIntegrator(); |
273 |
} |
274 |
|
275 |
void NPT::evolveChiA() { |
276 |
|
277 |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
278 |
oldChi = chi; |
279 |
} |
280 |
|
281 |
void NPT::evolveChiB() { |
282 |
|
283 |
prevChi = chi; |
284 |
chi = oldChi + dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
285 |
} |
286 |
|
287 |
bool NPT::chiConverged() { |
288 |
|
289 |
return ( fabs( prevChi - chi ) <= chiTolerance ); |
290 |
} |
291 |
|
292 |
int NPT::readyCheck() { |
293 |
|
294 |
//check parent's readyCheck() first |
295 |
if (Integrator::readyCheck() == -1) |
296 |
return -1; |
297 |
|
298 |
// First check to see if we have a target temperature. |
299 |
// Not having one is fatal. |
300 |
|
301 |
if (!have_target_temp) { |
302 |
sprintf( painCave.errMsg, |
303 |
"NPT error: You can't use the NPT integrator\n" |
304 |
" without a targetTemp!\n" |
305 |
); |
306 |
painCave.isFatal = 1; |
307 |
simError(); |
308 |
return -1; |
309 |
} |
310 |
|
311 |
if (!have_target_pressure) { |
312 |
sprintf( painCave.errMsg, |
313 |
"NPT error: You can't use the NPT integrator\n" |
314 |
" without a targetPressure!\n" |
315 |
); |
316 |
painCave.isFatal = 1; |
317 |
simError(); |
318 |
return -1; |
319 |
} |
320 |
|
321 |
// We must set tauThermostat. |
322 |
|
323 |
if (!have_tau_thermostat) { |
324 |
sprintf( painCave.errMsg, |
325 |
"NPT error: If you use the NPT\n" |
326 |
" integrator, you must set tauThermostat.\n"); |
327 |
painCave.isFatal = 1; |
328 |
simError(); |
329 |
return -1; |
330 |
} |
331 |
|
332 |
// We must set tauBarostat. |
333 |
|
334 |
if (!have_tau_barostat) { |
335 |
sprintf( painCave.errMsg, |
336 |
"NPT error: If you use the NPT\n" |
337 |
" integrator, you must set tauBarostat.\n"); |
338 |
painCave.isFatal = 1; |
339 |
simError(); |
340 |
return -1; |
341 |
} |
342 |
|
343 |
if (!have_chi_tolerance) { |
344 |
sprintf( painCave.errMsg, |
345 |
"NPT warning: setting chi tolerance to 1e-6\n"); |
346 |
chiTolerance = 1e-6; |
347 |
have_chi_tolerance = 1; |
348 |
painCave.isFatal = 0; |
349 |
simError(); |
350 |
} |
351 |
|
352 |
if (!have_eta_tolerance) { |
353 |
sprintf( painCave.errMsg, |
354 |
"NPT warning: setting eta tolerance to 1e-6\n"); |
355 |
etaTolerance = 1e-6; |
356 |
have_eta_tolerance = 1; |
357 |
painCave.isFatal = 0; |
358 |
simError(); |
359 |
} |
360 |
|
361 |
// We need NkBT a lot, so just set it here: This is the RAW number |
362 |
// of particles, so no subtraction or addition of constraints or |
363 |
// orientational degrees of freedom: |
364 |
|
365 |
NkBT = (double)Nparticles * kB * targetTemp; |
366 |
|
367 |
// fkBT is used because the thermostat operates on more degrees of freedom |
368 |
// than the barostat (when there are particles with orientational degrees |
369 |
// of freedom). ndf = 3 * (n_atoms + n_oriented -1) - n_constraint - nZcons |
370 |
|
371 |
fkBT = (double)info->ndf * kB * targetTemp; |
372 |
|
373 |
tt2 = tauThermostat * tauThermostat; |
374 |
tb2 = tauBarostat * tauBarostat; |
375 |
|
376 |
return 1; |
377 |
} |