1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
*/ |
42 |
|
43 |
#include <math.h> |
44 |
|
45 |
#include "brains/SimInfo.hpp" |
46 |
#include "brains/Thermo.hpp" |
47 |
#include "integrators/NPT.hpp" |
48 |
#include "math/SquareMatrix3.hpp" |
49 |
#include "primitives/Molecule.hpp" |
50 |
#include "utils/PhysicalConstants.hpp" |
51 |
#include "utils/simError.h" |
52 |
|
53 |
// Basic isotropic thermostating and barostating via the Melchionna |
54 |
// modification of the Hoover algorithm: |
55 |
// |
56 |
// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, |
57 |
// Molec. Phys., 78, 533. |
58 |
// |
59 |
// and |
60 |
// |
61 |
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
62 |
|
63 |
namespace OpenMD { |
64 |
|
65 |
NPT::NPT(SimInfo* info) : |
66 |
VelocityVerletIntegrator(info), chiTolerance(1e-6), etaTolerance(1e-6), maxIterNum_(4) { |
67 |
|
68 |
Globals* simParams = info_->getSimParams(); |
69 |
|
70 |
if (!simParams->getUseIntialExtendedSystemState()) { |
71 |
Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); |
72 |
currSnapshot->setThermostat(make_pair(0.0, 0.0)); |
73 |
currSnapshot->setBarostat(Mat3x3d(0.0)); |
74 |
} |
75 |
|
76 |
if (!simParams->haveTargetTemp()) { |
77 |
sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp!\n"); |
78 |
painCave.isFatal = 1; |
79 |
painCave.severity = OPENMD_ERROR; |
80 |
simError(); |
81 |
} else { |
82 |
targetTemp = simParams->getTargetTemp(); |
83 |
} |
84 |
|
85 |
// We must set tauThermostat |
86 |
if (!simParams->haveTauThermostat()) { |
87 |
sprintf(painCave.errMsg, "If you use the constant temperature\n" |
88 |
"\tintegrator, you must set tauThermostat.\n"); |
89 |
|
90 |
painCave.severity = OPENMD_ERROR; |
91 |
painCave.isFatal = 1; |
92 |
simError(); |
93 |
} else { |
94 |
tauThermostat = simParams->getTauThermostat(); |
95 |
} |
96 |
|
97 |
if (!simParams->haveTargetPressure()) { |
98 |
sprintf(painCave.errMsg, "NPT error: You can't use the NPT integrator\n" |
99 |
" without a targetPressure!\n"); |
100 |
|
101 |
painCave.isFatal = 1; |
102 |
simError(); |
103 |
} else { |
104 |
targetPressure = simParams->getTargetPressure(); |
105 |
} |
106 |
|
107 |
if (!simParams->haveTauBarostat()) { |
108 |
sprintf(painCave.errMsg, |
109 |
"If you use the NPT integrator, you must set tauBarostat.\n"); |
110 |
painCave.severity = OPENMD_ERROR; |
111 |
painCave.isFatal = 1; |
112 |
simError(); |
113 |
} else { |
114 |
tauBarostat = simParams->getTauBarostat(); |
115 |
} |
116 |
|
117 |
tt2 = tauThermostat * tauThermostat; |
118 |
tb2 = tauBarostat * tauBarostat; |
119 |
|
120 |
updateSizes(); |
121 |
} |
122 |
|
123 |
NPT::~NPT() { |
124 |
} |
125 |
|
126 |
void NPT::doUpdateSizes() { |
127 |
|
128 |
oldPos.resize(info_->getNIntegrableObjects()); |
129 |
oldVel.resize(info_->getNIntegrableObjects()); |
130 |
oldJi.resize(info_->getNIntegrableObjects()); |
131 |
|
132 |
} |
133 |
|
134 |
void NPT::moveA() { |
135 |
SimInfo::MoleculeIterator i; |
136 |
Molecule::IntegrableObjectIterator j; |
137 |
Molecule* mol; |
138 |
StuntDouble* sd; |
139 |
Vector3d Tb, ji; |
140 |
RealType mass; |
141 |
Vector3d vel; |
142 |
Vector3d pos; |
143 |
Vector3d frc; |
144 |
Vector3d sc; |
145 |
int index; |
146 |
|
147 |
thermostat = snap->getThermostat(); |
148 |
loadEta(); |
149 |
|
150 |
instaTemp =thermo.getTemperature(); |
151 |
press = thermo.getPressureTensor(); |
152 |
instaPress = PhysicalConstants::pressureConvert* (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0; |
153 |
instaVol =thermo.getVolume(); |
154 |
|
155 |
Vector3d COM = thermo.getCom(); |
156 |
|
157 |
//evolve velocity half step |
158 |
|
159 |
calcVelScale(); |
160 |
|
161 |
for (mol = info_->beginMolecule(i); mol != NULL; |
162 |
mol = info_->nextMolecule(i)) { |
163 |
|
164 |
for (sd = mol->beginIntegrableObject(j); sd != NULL; |
165 |
sd = mol->nextIntegrableObject(j)) { |
166 |
|
167 |
vel = sd->getVel(); |
168 |
frc = sd->getFrc(); |
169 |
|
170 |
mass = sd->getMass(); |
171 |
|
172 |
getVelScaleA(sc, vel); |
173 |
|
174 |
// velocity half step (use chi from previous step here): |
175 |
|
176 |
vel += dt2*PhysicalConstants::energyConvert/mass* frc - dt2*sc; |
177 |
sd->setVel(vel); |
178 |
|
179 |
if (sd->isDirectional()) { |
180 |
|
181 |
// get and convert the torque to body frame |
182 |
|
183 |
Tb = sd->lab2Body(sd->getTrq()); |
184 |
|
185 |
// get the angular momentum, and propagate a half step |
186 |
|
187 |
ji = sd->getJ(); |
188 |
|
189 |
ji += dt2*PhysicalConstants::energyConvert * Tb |
190 |
- dt2*thermostat.first* ji; |
191 |
|
192 |
rotAlgo_->rotate(sd, ji, dt); |
193 |
|
194 |
sd->setJ(ji); |
195 |
} |
196 |
|
197 |
} |
198 |
} |
199 |
// evolve chi and eta half step |
200 |
|
201 |
thermostat.first += dt2 * (instaTemp / targetTemp - 1.0) / tt2; |
202 |
|
203 |
evolveEtaA(); |
204 |
|
205 |
//calculate the integral of chidt |
206 |
thermostat.second += dt2 * thermostat.first; |
207 |
|
208 |
flucQ_->moveA(); |
209 |
|
210 |
|
211 |
index = 0; |
212 |
for (mol = info_->beginMolecule(i); mol != NULL; |
213 |
mol = info_->nextMolecule(i)) { |
214 |
|
215 |
for (sd = mol->beginIntegrableObject(j); sd != NULL; |
216 |
sd = mol->nextIntegrableObject(j)) { |
217 |
|
218 |
oldPos[index++] = sd->getPos(); |
219 |
|
220 |
} |
221 |
} |
222 |
|
223 |
//the first estimation of r(t+dt) is equal to r(t) |
224 |
|
225 |
for(int k = 0; k < maxIterNum_; k++) { |
226 |
index = 0; |
227 |
for (mol = info_->beginMolecule(i); mol != NULL; |
228 |
mol = info_->nextMolecule(i)) { |
229 |
|
230 |
for (sd = mol->beginIntegrableObject(j); sd != NULL; |
231 |
sd = mol->nextIntegrableObject(j)) { |
232 |
|
233 |
vel = sd->getVel(); |
234 |
pos = sd->getPos(); |
235 |
|
236 |
this->getPosScale(pos, COM, index, sc); |
237 |
|
238 |
pos = oldPos[index] + dt * (vel + sc); |
239 |
sd->setPos(pos); |
240 |
|
241 |
++index; |
242 |
} |
243 |
} |
244 |
|
245 |
rattle_->constraintA(); |
246 |
} |
247 |
|
248 |
// Scale the box after all the positions have been moved: |
249 |
|
250 |
this->scaleSimBox(); |
251 |
|
252 |
snap->setThermostat(thermostat); |
253 |
|
254 |
saveEta(); |
255 |
} |
256 |
|
257 |
void NPT::moveB(void) { |
258 |
SimInfo::MoleculeIterator i; |
259 |
Molecule::IntegrableObjectIterator j; |
260 |
Molecule* mol; |
261 |
StuntDouble* sd; |
262 |
int index; |
263 |
Vector3d Tb; |
264 |
Vector3d ji; |
265 |
Vector3d sc; |
266 |
Vector3d vel; |
267 |
Vector3d frc; |
268 |
RealType mass; |
269 |
|
270 |
thermostat = snap->getThermostat(); |
271 |
RealType oldChi = thermostat.first; |
272 |
RealType prevChi; |
273 |
|
274 |
loadEta(); |
275 |
|
276 |
//save velocity and angular momentum |
277 |
index = 0; |
278 |
for (mol = info_->beginMolecule(i); mol != NULL; |
279 |
mol = info_->nextMolecule(i)) { |
280 |
|
281 |
for (sd = mol->beginIntegrableObject(j); sd != NULL; |
282 |
sd = mol->nextIntegrableObject(j)) { |
283 |
|
284 |
oldVel[index] = sd->getVel(); |
285 |
|
286 |
if (sd->isDirectional()) |
287 |
oldJi[index] = sd->getJ(); |
288 |
|
289 |
++index; |
290 |
} |
291 |
} |
292 |
|
293 |
// do the iteration: |
294 |
instaVol =thermo.getVolume(); |
295 |
|
296 |
for(int k = 0; k < maxIterNum_; k++) { |
297 |
instaTemp =thermo.getTemperature(); |
298 |
instaPress =thermo.getPressure(); |
299 |
|
300 |
// evolve chi another half step using the temperature at t + dt/2 |
301 |
prevChi = thermostat.first; |
302 |
thermostat.first = oldChi + dt2 * (instaTemp / targetTemp - 1.0) / tt2; |
303 |
|
304 |
//evolve eta |
305 |
this->evolveEtaB(); |
306 |
this->calcVelScale(); |
307 |
|
308 |
index = 0; |
309 |
for (mol = info_->beginMolecule(i); mol != NULL; |
310 |
mol = info_->nextMolecule(i)) { |
311 |
|
312 |
for (sd = mol->beginIntegrableObject(j); sd != NULL; |
313 |
sd = mol->nextIntegrableObject(j)) { |
314 |
|
315 |
frc = sd->getFrc(); |
316 |
vel = sd->getVel(); |
317 |
|
318 |
mass = sd->getMass(); |
319 |
|
320 |
getVelScaleB(sc, index); |
321 |
|
322 |
// velocity half step |
323 |
vel = oldVel[index] |
324 |
+ dt2*PhysicalConstants::energyConvert/mass* frc |
325 |
- dt2*sc; |
326 |
|
327 |
sd->setVel(vel); |
328 |
|
329 |
if (sd->isDirectional()) { |
330 |
// get and convert the torque to body frame |
331 |
Tb = sd->lab2Body(sd->getTrq()); |
332 |
|
333 |
ji = oldJi[index] |
334 |
+ dt2*PhysicalConstants::energyConvert*Tb |
335 |
- dt2*thermostat.first*oldJi[index]; |
336 |
|
337 |
sd->setJ(ji); |
338 |
} |
339 |
|
340 |
++index; |
341 |
} |
342 |
} |
343 |
|
344 |
rattle_->constraintB(); |
345 |
|
346 |
if ((fabs(prevChi - thermostat.first) <= chiTolerance) && |
347 |
this->etaConverged()) |
348 |
break; |
349 |
} |
350 |
|
351 |
//calculate integral of chidt |
352 |
thermostat.second += dt2 * thermostat.first; |
353 |
|
354 |
snap->setThermostat(thermostat); |
355 |
|
356 |
flucQ_->moveB(); |
357 |
saveEta(); |
358 |
} |
359 |
|
360 |
void NPT::resetIntegrator(){ |
361 |
snap->setThermostat(make_pair(0.0, 0.0)); |
362 |
resetEta(); |
363 |
} |
364 |
|
365 |
void NPT::resetEta() { |
366 |
Mat3x3d etaMat(0.0); |
367 |
snap->setBarostat(etaMat); |
368 |
} |
369 |
} |