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->setChi(0.0); |
73 |
currSnapshot->setIntegralOfChiDt(0.0); |
74 |
currSnapshot->setEta(Mat3x3d(0.0)); |
75 |
} |
76 |
|
77 |
if (!simParams->haveTargetTemp()) { |
78 |
sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp!\n"); |
79 |
painCave.isFatal = 1; |
80 |
painCave.severity = OPENMD_ERROR; |
81 |
simError(); |
82 |
} else { |
83 |
targetTemp = simParams->getTargetTemp(); |
84 |
} |
85 |
|
86 |
// We must set tauThermostat |
87 |
if (!simParams->haveTauThermostat()) { |
88 |
sprintf(painCave.errMsg, "If you use the constant temperature\n" |
89 |
"\tintegrator, you must set tauThermostat.\n"); |
90 |
|
91 |
painCave.severity = OPENMD_ERROR; |
92 |
painCave.isFatal = 1; |
93 |
simError(); |
94 |
} else { |
95 |
tauThermostat = simParams->getTauThermostat(); |
96 |
} |
97 |
|
98 |
if (!simParams->haveTargetPressure()) { |
99 |
sprintf(painCave.errMsg, "NPT error: You can't use the NPT integrator\n" |
100 |
" without a targetPressure!\n"); |
101 |
|
102 |
painCave.isFatal = 1; |
103 |
simError(); |
104 |
} else { |
105 |
targetPressure = simParams->getTargetPressure(); |
106 |
} |
107 |
|
108 |
if (!simParams->haveTauBarostat()) { |
109 |
sprintf(painCave.errMsg, |
110 |
"If you use the NPT integrator, you must set tauBarostat.\n"); |
111 |
painCave.severity = OPENMD_ERROR; |
112 |
painCave.isFatal = 1; |
113 |
simError(); |
114 |
} else { |
115 |
tauBarostat = simParams->getTauBarostat(); |
116 |
} |
117 |
|
118 |
tt2 = tauThermostat * tauThermostat; |
119 |
tb2 = tauBarostat * tauBarostat; |
120 |
|
121 |
updateSizes(); |
122 |
} |
123 |
|
124 |
NPT::~NPT() { |
125 |
} |
126 |
|
127 |
void NPT::doUpdateSizes() { |
128 |
|
129 |
oldPos.resize(info_->getNIntegrableObjects()); |
130 |
oldVel.resize(info_->getNIntegrableObjects()); |
131 |
oldJi.resize(info_->getNIntegrableObjects()); |
132 |
|
133 |
} |
134 |
|
135 |
void NPT::moveA() { |
136 |
SimInfo::MoleculeIterator i; |
137 |
Molecule::IntegrableObjectIterator j; |
138 |
Molecule* mol; |
139 |
StuntDouble* integrableObject; |
140 |
Vector3d Tb, ji; |
141 |
RealType mass; |
142 |
Vector3d vel; |
143 |
Vector3d pos; |
144 |
Vector3d frc; |
145 |
Vector3d sc; |
146 |
int index; |
147 |
|
148 |
chi= currentSnapshot_->getChi(); |
149 |
integralOfChidt = currentSnapshot_->getIntegralOfChiDt(); |
150 |
loadEta(); |
151 |
|
152 |
instaTemp =thermo.getTemperature(); |
153 |
press = thermo.getPressureTensor(); |
154 |
instaPress = PhysicalConstants::pressureConvert* (press(0, 0) + press(1, 1) + press(2, 2)) / 3.0; |
155 |
instaVol =thermo.getVolume(); |
156 |
|
157 |
Vector3d COM = info_->getCom(); |
158 |
|
159 |
//evolve velocity half step |
160 |
|
161 |
calcVelScale(); |
162 |
|
163 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
164 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
165 |
integrableObject = mol->nextIntegrableObject(j)) { |
166 |
|
167 |
vel = integrableObject->getVel(); |
168 |
frc = integrableObject->getFrc(); |
169 |
|
170 |
mass = integrableObject->getMass(); |
171 |
|
172 |
getVelScaleA(sc, vel); |
173 |
|
174 |
// velocity half step (use chi from previous step here): |
175 |
//vel[j] += dt2 * ((frc[j] / mass) * PhysicalConstants::energyConvert - sc[j]); |
176 |
vel += dt2*PhysicalConstants::energyConvert/mass* frc - dt2*sc; |
177 |
integrableObject->setVel(vel); |
178 |
|
179 |
if (integrableObject->isDirectional()) { |
180 |
|
181 |
// get and convert the torque to body frame |
182 |
|
183 |
Tb = integrableObject->lab2Body(integrableObject->getTrq()); |
184 |
|
185 |
// get the angular momentum, and propagate a half step |
186 |
|
187 |
ji = integrableObject->getJ(); |
188 |
|
189 |
//ji[j] += dt2 * (Tb[j] * PhysicalConstants::energyConvert - ji[j]*chi); |
190 |
ji += dt2*PhysicalConstants::energyConvert * Tb - dt2*chi* ji; |
191 |
|
192 |
rotAlgo_->rotate(integrableObject, ji, dt); |
193 |
|
194 |
integrableObject->setJ(ji); |
195 |
} |
196 |
|
197 |
} |
198 |
} |
199 |
// evolve chi and eta half step |
200 |
|
201 |
chi += dt2 * (instaTemp / targetTemp - 1.0) / tt2; |
202 |
|
203 |
evolveEtaA(); |
204 |
|
205 |
//calculate the integral of chidt |
206 |
integralOfChidt += dt2 * chi; |
207 |
|
208 |
flucQ_->moveA(); |
209 |
|
210 |
|
211 |
index = 0; |
212 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
213 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
214 |
integrableObject = mol->nextIntegrableObject(j)) { |
215 |
oldPos[index++] = integrableObject->getPos(); |
216 |
} |
217 |
} |
218 |
|
219 |
//the first estimation of r(t+dt) is equal to r(t) |
220 |
|
221 |
for(int k = 0; k < maxIterNum_; k++) { |
222 |
index = 0; |
223 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
224 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
225 |
integrableObject = mol->nextIntegrableObject(j)) { |
226 |
|
227 |
vel = integrableObject->getVel(); |
228 |
pos = integrableObject->getPos(); |
229 |
|
230 |
this->getPosScale(pos, COM, index, sc); |
231 |
|
232 |
pos = oldPos[index] + dt * (vel + sc); |
233 |
integrableObject->setPos(pos); |
234 |
|
235 |
++index; |
236 |
} |
237 |
} |
238 |
|
239 |
rattle_->constraintA(); |
240 |
} |
241 |
|
242 |
// Scale the box after all the positions have been moved: |
243 |
|
244 |
this->scaleSimBox(); |
245 |
|
246 |
currentSnapshot_->setChi(chi); |
247 |
currentSnapshot_->setIntegralOfChiDt(integralOfChidt); |
248 |
|
249 |
saveEta(); |
250 |
} |
251 |
|
252 |
void NPT::moveB(void) { |
253 |
SimInfo::MoleculeIterator i; |
254 |
Molecule::IntegrableObjectIterator j; |
255 |
Molecule* mol; |
256 |
StuntDouble* integrableObject; |
257 |
int index; |
258 |
Vector3d Tb; |
259 |
Vector3d ji; |
260 |
Vector3d sc; |
261 |
Vector3d vel; |
262 |
Vector3d frc; |
263 |
RealType mass; |
264 |
|
265 |
|
266 |
chi= currentSnapshot_->getChi(); |
267 |
integralOfChidt = currentSnapshot_->getIntegralOfChiDt(); |
268 |
RealType oldChi = chi; |
269 |
RealType prevChi; |
270 |
|
271 |
loadEta(); |
272 |
|
273 |
//save velocity and angular momentum |
274 |
index = 0; |
275 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
276 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
277 |
integrableObject = mol->nextIntegrableObject(j)) { |
278 |
|
279 |
oldVel[index] = integrableObject->getVel(); |
280 |
oldJi[index] = integrableObject->getJ(); |
281 |
++index; |
282 |
} |
283 |
} |
284 |
|
285 |
// do the iteration: |
286 |
instaVol =thermo.getVolume(); |
287 |
|
288 |
for(int k = 0; k < maxIterNum_; k++) { |
289 |
instaTemp =thermo.getTemperature(); |
290 |
instaPress =thermo.getPressure(); |
291 |
|
292 |
// evolve chi another half step using the temperature at t + dt/2 |
293 |
prevChi = chi; |
294 |
chi = oldChi + dt2 * (instaTemp / targetTemp - 1.0) / tt2; |
295 |
|
296 |
//evolve eta |
297 |
this->evolveEtaB(); |
298 |
this->calcVelScale(); |
299 |
|
300 |
index = 0; |
301 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
302 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
303 |
integrableObject = mol->nextIntegrableObject(j)) { |
304 |
|
305 |
frc = integrableObject->getFrc(); |
306 |
vel = integrableObject->getVel(); |
307 |
|
308 |
mass = integrableObject->getMass(); |
309 |
|
310 |
getVelScaleB(sc, index); |
311 |
|
312 |
// velocity half step |
313 |
//vel[j] = oldVel[3 * i + j] + dt2 *((frc[j] / mass) * PhysicalConstants::energyConvert - sc[j]); |
314 |
vel = oldVel[index] + dt2*PhysicalConstants::energyConvert/mass* frc - dt2*sc; |
315 |
integrableObject->setVel(vel); |
316 |
|
317 |
if (integrableObject->isDirectional()) { |
318 |
// get and convert the torque to body frame |
319 |
Tb = integrableObject->lab2Body(integrableObject->getTrq()); |
320 |
|
321 |
//ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * PhysicalConstants::energyConvert - oldJi[3*i+j]*chi); |
322 |
ji = oldJi[index] + dt2*PhysicalConstants::energyConvert*Tb - dt2*chi*oldJi[index]; |
323 |
integrableObject->setJ(ji); |
324 |
} |
325 |
|
326 |
++index; |
327 |
} |
328 |
} |
329 |
|
330 |
rattle_->constraintB(); |
331 |
|
332 |
if ((fabs(prevChi - chi) <= chiTolerance) && this->etaConverged()) |
333 |
break; |
334 |
} |
335 |
|
336 |
//calculate integral of chidt |
337 |
integralOfChidt += dt2 * chi; |
338 |
|
339 |
currentSnapshot_->setChi(chi); |
340 |
currentSnapshot_->setIntegralOfChiDt(integralOfChidt); |
341 |
|
342 |
flucQ_->moveB(); |
343 |
saveEta(); |
344 |
} |
345 |
|
346 |
void NPT::resetIntegrator(){ |
347 |
currentSnapshot_->setChi(0.0); |
348 |
currentSnapshot_->setIntegralOfChiDt(0.0); |
349 |
resetEta(); |
350 |
} |
351 |
|
352 |
|
353 |
void NPT::resetEta() { |
354 |
Mat3x3d etaMat(0.0); |
355 |
currentSnapshot_->setEta(etaMat); |
356 |
} |
357 |
|
358 |
} |