OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NPTf.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "integrators/NPTf.hpp"
49
50#include "brains/SimInfo.hpp"
51#include "brains/Thermo.hpp"
52#include "integrators/IntegratorCreator.hpp"
54#include "utils/Constants.hpp"
55#include "utils/simError.h"
56
57namespace OpenMD {
58
59 // Basic non-isotropic thermostating and barostating via the Melchionna
60 // modification of the Hoover algorithm:
61 //
62 // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
63 // Molec. Phys., 78, 533.
64 //
65 // and
66 //
67 // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
68
69 void NPTf::evolveEtaA() {
70 int i, j;
71
72 for (i = 0; i < 3; i++) {
73 for (j = 0; j < 3; j++) {
74 if (i == j) {
75 eta(i, j) +=
76 dt2 * instaVol *
77 (press(i, j) - targetPressure / Constants::pressureConvert) /
78 (NkBT * tb2);
79 } else {
80 eta(i, j) += dt2 * instaVol * press(i, j) / (NkBT * tb2);
81 }
82 }
83 }
84
85 for (i = 0; i < 3; i++) {
86 for (j = 0; j < 3; j++) {
87 oldEta(i, j) = eta(i, j);
88 }
89 }
90 }
91
92 void NPTf::evolveEtaB() {
93 int i;
94 int j;
95
96 for (i = 0; i < 3; i++) {
97 for (j = 0; j < 3; j++) {
98 prevEta(i, j) = eta(i, j);
99 }
100 }
101
102 for (i = 0; i < 3; i++) {
103 for (j = 0; j < 3; j++) {
104 if (i == j) {
105 eta(i, j) =
106 oldEta(i, j) +
107 dt2 * instaVol *
108 (press(i, j) - targetPressure / Constants::pressureConvert) /
109 (NkBT * tb2);
110 } else {
111 eta(i, j) =
112 oldEta(i, j) + dt2 * instaVol * press(i, j) / (NkBT * tb2);
113 }
114 }
115 }
116 }
117
118 void NPTf::calcVelScale() {
119 for (int i = 0; i < 3; i++) {
120 for (int j = 0; j < 3; j++) {
121 vScale(i, j) = eta(i, j);
122
123 if (i == j) { vScale(i, j) += thermostat.first; }
124 }
125 }
126 }
127
128 void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel) {
129 sc = vScale * vel;
130 }
131
132 void NPTf::getVelScaleB(Vector3d& sc, int index) {
133 sc = vScale * oldVel[index];
134 }
135
136 void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index,
137 Vector3d& sc) {
138 /**@todo */
139 Vector3d rj = (oldPos[index] + pos) / (RealType)2.0 - COM;
140 sc = eta * rj;
141 }
142
143 void NPTf::scaleSimBox() {
144 int i;
145 int j;
146 int k;
147 Mat3x3d scaleMat;
148 RealType eta2ij;
149 RealType bigScale, smallScale, offDiagMax;
150 Mat3x3d hm;
151 Mat3x3d hmnew;
152
153 // Scale the box after all the positions have been moved:
154
155 // Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat)
156 // Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2)
157
158 bigScale = 1.0;
159 smallScale = 1.0;
160 offDiagMax = 0.0;
161
162 for (i = 0; i < 3; i++) {
163 for (j = 0; j < 3; j++) {
164 // Calculate the matrix Product of the eta array (we only need
165 // the ij element right now):
166
167 eta2ij = 0.0;
168 for (k = 0; k < 3; k++) {
169 eta2ij += eta(i, k) * eta(k, j);
170 }
171
172 scaleMat(i, j) = 0.0;
173 // identity matrix (see above):
174 if (i == j) scaleMat(i, j) = 1.0;
175 // Taylor expansion for the exponential truncated at second order:
176 scaleMat(i, j) += dt * eta(i, j) + 0.5 * dt * dt * eta2ij;
177
178 if (i != j)
179 if (fabs(scaleMat(i, j)) > offDiagMax)
180 offDiagMax = fabs(scaleMat(i, j));
181 }
182
183 if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
184 if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
185 }
186
187 if ((bigScale > 1.01) || (smallScale < 0.99)) {
188 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
189 "NPTf error: Attempting a Box scaling of more than 1 percent.\n"
190 " Check your tauBarostat, as it is probably too small!\n\n"
191 " scaleMat = [%lf\t%lf\t%lf]\n"
192 " [%lf\t%lf\t%lf]\n"
193 " [%lf\t%lf\t%lf]\n"
194 " eta = [%lf\t%lf\t%lf]\n"
195 " [%lf\t%lf\t%lf]\n"
196 " [%lf\t%lf\t%lf]\n",
197 scaleMat(0, 0), scaleMat(0, 1), scaleMat(0, 2), scaleMat(1, 0),
198 scaleMat(1, 1), scaleMat(1, 2), scaleMat(2, 0), scaleMat(2, 1),
199 scaleMat(2, 2), eta(0, 0), eta(0, 1), eta(0, 2), eta(1, 0),
200 eta(1, 1), eta(1, 2), eta(2, 0), eta(2, 1), eta(2, 2));
201 painCave.isFatal = 1;
202 simError();
203 } else if (offDiagMax > 0.01) {
204 snprintf(
205 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
206 "NPTf error: Attempting an off-diagonal Box scaling of more than 1 "
207 "percent.\n"
208 " Check your tauBarostat, as it is probably too small!\n\n"
209 " scaleMat = [%lf\t%lf\t%lf]\n"
210 " [%lf\t%lf\t%lf]\n"
211 " [%lf\t%lf\t%lf]\n"
212 " eta = [%lf\t%lf\t%lf]\n"
213 " [%lf\t%lf\t%lf]\n"
214 " [%lf\t%lf\t%lf]\n",
215 scaleMat(0, 0), scaleMat(0, 1), scaleMat(0, 2), scaleMat(1, 0),
216 scaleMat(1, 1), scaleMat(1, 2), scaleMat(2, 0), scaleMat(2, 1),
217 scaleMat(2, 2), eta(0, 0), eta(0, 1), eta(0, 2), eta(1, 0), eta(1, 1),
218 eta(1, 2), eta(2, 0), eta(2, 1), eta(2, 2));
219 painCave.isFatal = 1;
220 simError();
221 } else {
222 Mat3x3d hmat = snap->getHmat();
223 hmat = hmat * scaleMat;
224 snap->setHmat(hmat);
225 }
226 }
227
228 bool NPTf::etaConverged() {
229 int i;
230 RealType diffEta, sumEta;
231
232 sumEta = 0;
233 for (i = 0; i < 3; i++) {
234 sumEta += pow(prevEta(i, i) - eta(i, i), 2);
235 }
236
237 diffEta = sqrt(sumEta / 3.0);
238
239 return (diffEta <= etaTolerance);
240 }
241
242 RealType NPTf::calcConservedQuantity() {
243 thermostat = snap->getThermostat();
244 loadEta();
245
246 // We need NkBT a lot, so just set it here: This is the RAW number
247 // of integrableObjects, so no subtraction or addition of constraints or
248 // orientational degrees of freedom:
249 NkBT = info_->getNGlobalIntegrableObjects() * Constants::kB * targetTemp;
250
251 // fkBT is used because the thermostat operates on more degrees of freedom
252 // than the barostat (when there are particles with orientational degrees
253 // of freedom).
254 fkBT = info_->getNdf() * Constants::kB * targetTemp;
255
256 RealType conservedQuantity;
257 RealType totalEnergy;
258 RealType thermostat_kinetic;
259 RealType thermostat_potential;
260 RealType barostat_kinetic;
261 RealType barostat_potential;
262 RealType trEta;
263
264 totalEnergy = thermo.getTotalEnergy();
265
266 thermostat_kinetic = fkBT * tt2 * thermostat.first * thermostat.first /
267 (2.0 * Constants::energyConvert);
268
269 thermostat_potential = fkBT * thermostat.second / Constants::energyConvert;
270
271 SquareMatrix<RealType, 3> tmp = eta.transpose() * eta;
272 trEta = tmp.trace();
273
274 barostat_kinetic = NkBT * tb2 * trEta / (2.0 * Constants::energyConvert);
275
276 barostat_potential =
277 (targetPressure * thermo.getVolume() / Constants::pressureConvert) /
278 Constants::energyConvert;
279
280 conservedQuantity = totalEnergy + thermostat_kinetic +
281 thermostat_potential + barostat_kinetic +
282 barostat_potential;
283
284 return conservedQuantity;
285 }
286
287 void NPTf::loadEta() {
288 eta = snap->getBarostat();
289
290 // if (!eta.isDiagonal()) {
291 // snprintf( painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
292 // "NPTf error: the diagonal elements of eta matrix are not the
293 // same or etaMat is not a diagonal matrix");
294 // painCave.isFatal = 1;
295 // simError();
296 //}
297 }
298
299 void NPTf::saveEta() { snap->setBarostat(eta); }
300
301} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.