OpenMD 3.1
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 appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "integrators/NPTf.hpp"
46
47#include "brains/SimInfo.hpp"
48#include "brains/Thermo.hpp"
49#include "integrators/IntegratorCreator.hpp"
51#include "utils/Constants.hpp"
52#include "utils/simError.h"
53
54namespace OpenMD {
55
56 // Basic non-isotropic thermostating and barostating via the Melchionna
57 // modification of the Hoover algorithm:
58 //
59 // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
60 // Molec. Phys., 78, 533.
61 //
62 // and
63 //
64 // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
65
66 void NPTf::evolveEtaA() {
67 int i, j;
68
69 for (i = 0; i < 3; i++) {
70 for (j = 0; j < 3; j++) {
71 if (i == j) {
72 eta(i, j) +=
73 dt2 * instaVol *
74 (press(i, j) - targetPressure / Constants::pressureConvert) /
75 (NkBT * tb2);
76 } else {
77 eta(i, j) += dt2 * instaVol * press(i, j) / (NkBT * tb2);
78 }
79 }
80 }
81
82 for (i = 0; i < 3; i++) {
83 for (j = 0; j < 3; j++) {
84 oldEta(i, j) = eta(i, j);
85 }
86 }
87 }
88
89 void NPTf::evolveEtaB() {
90 int i;
91 int j;
92
93 for (i = 0; i < 3; i++) {
94 for (j = 0; j < 3; j++) {
95 prevEta(i, j) = eta(i, j);
96 }
97 }
98
99 for (i = 0; i < 3; i++) {
100 for (j = 0; j < 3; j++) {
101 if (i == j) {
102 eta(i, j) =
103 oldEta(i, j) +
104 dt2 * instaVol *
105 (press(i, j) - targetPressure / Constants::pressureConvert) /
106 (NkBT * tb2);
107 } else {
108 eta(i, j) =
109 oldEta(i, j) + dt2 * instaVol * press(i, j) / (NkBT * tb2);
110 }
111 }
112 }
113 }
114
115 void NPTf::calcVelScale() {
116 for (int i = 0; i < 3; i++) {
117 for (int j = 0; j < 3; j++) {
118 vScale(i, j) = eta(i, j);
119
120 if (i == j) { vScale(i, j) += thermostat.first; }
121 }
122 }
123 }
124
125 void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel) {
126 sc = vScale * vel;
127 }
128
129 void NPTf::getVelScaleB(Vector3d& sc, int index) {
130 sc = vScale * oldVel[index];
131 }
132
133 void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index,
134 Vector3d& sc) {
135 /**@todo */
136 Vector3d rj = (oldPos[index] + pos) / (RealType)2.0 - COM;
137 sc = eta * rj;
138 }
139
140 void NPTf::scaleSimBox() {
141 int i;
142 int j;
143 int k;
144 Mat3x3d scaleMat;
145 RealType eta2ij;
146 RealType bigScale, smallScale, offDiagMax;
147 Mat3x3d hm;
148 Mat3x3d hmnew;
149
150 // Scale the box after all the positions have been moved:
151
152 // Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat)
153 // Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2)
154
155 bigScale = 1.0;
156 smallScale = 1.0;
157 offDiagMax = 0.0;
158
159 for (i = 0; i < 3; i++) {
160 for (j = 0; j < 3; j++) {
161 // Calculate the matrix Product of the eta array (we only need
162 // the ij element right now):
163
164 eta2ij = 0.0;
165 for (k = 0; k < 3; k++) {
166 eta2ij += eta(i, k) * eta(k, j);
167 }
168
169 scaleMat(i, j) = 0.0;
170 // identity matrix (see above):
171 if (i == j) scaleMat(i, j) = 1.0;
172 // Taylor expansion for the exponential truncated at second order:
173 scaleMat(i, j) += dt * eta(i, j) + 0.5 * dt * dt * eta2ij;
174
175 if (i != j)
176 if (fabs(scaleMat(i, j)) > offDiagMax)
177 offDiagMax = fabs(scaleMat(i, j));
178 }
179
180 if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
181 if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
182 }
183
184 if ((bigScale > 1.01) || (smallScale < 0.99)) {
185 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
186 "NPTf error: Attempting a Box scaling of more than 1 percent.\n"
187 " Check your tauBarostat, as it is probably too small!\n\n"
188 " scaleMat = [%lf\t%lf\t%lf]\n"
189 " [%lf\t%lf\t%lf]\n"
190 " [%lf\t%lf\t%lf]\n"
191 " eta = [%lf\t%lf\t%lf]\n"
192 " [%lf\t%lf\t%lf]\n"
193 " [%lf\t%lf\t%lf]\n",
194 scaleMat(0, 0), scaleMat(0, 1), scaleMat(0, 2), scaleMat(1, 0),
195 scaleMat(1, 1), scaleMat(1, 2), scaleMat(2, 0), scaleMat(2, 1),
196 scaleMat(2, 2), eta(0, 0), eta(0, 1), eta(0, 2), eta(1, 0),
197 eta(1, 1), eta(1, 2), eta(2, 0), eta(2, 1), eta(2, 2));
198 painCave.isFatal = 1;
199 simError();
200 } else if (offDiagMax > 0.01) {
201 snprintf(
202 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
203 "NPTf error: Attempting an off-diagonal Box scaling of more than 1 "
204 "percent.\n"
205 " Check your tauBarostat, as it is probably too small!\n\n"
206 " scaleMat = [%lf\t%lf\t%lf]\n"
207 " [%lf\t%lf\t%lf]\n"
208 " [%lf\t%lf\t%lf]\n"
209 " eta = [%lf\t%lf\t%lf]\n"
210 " [%lf\t%lf\t%lf]\n"
211 " [%lf\t%lf\t%lf]\n",
212 scaleMat(0, 0), scaleMat(0, 1), scaleMat(0, 2), scaleMat(1, 0),
213 scaleMat(1, 1), scaleMat(1, 2), scaleMat(2, 0), scaleMat(2, 1),
214 scaleMat(2, 2), eta(0, 0), eta(0, 1), eta(0, 2), eta(1, 0), eta(1, 1),
215 eta(1, 2), eta(2, 0), eta(2, 1), eta(2, 2));
216 painCave.isFatal = 1;
217 simError();
218 } else {
219 Mat3x3d hmat = snap->getHmat();
220 hmat = hmat * scaleMat;
221 snap->setHmat(hmat);
222 }
223 }
224
225 bool NPTf::etaConverged() {
226 int i;
227 RealType diffEta, sumEta;
228
229 sumEta = 0;
230 for (i = 0; i < 3; i++) {
231 sumEta += pow(prevEta(i, i) - eta(i, i), 2);
232 }
233
234 diffEta = sqrt(sumEta / 3.0);
235
236 return (diffEta <= etaTolerance);
237 }
238
239 RealType NPTf::calcConservedQuantity() {
240 thermostat = snap->getThermostat();
241 loadEta();
242
243 // We need NkBT a lot, so just set it here: This is the RAW number
244 // of integrableObjects, so no subtraction or addition of constraints or
245 // orientational degrees of freedom:
246 NkBT = info_->getNGlobalIntegrableObjects() * Constants::kB * targetTemp;
247
248 // fkBT is used because the thermostat operates on more degrees of freedom
249 // than the barostat (when there are particles with orientational degrees
250 // of freedom).
251 fkBT = info_->getNdf() * Constants::kB * targetTemp;
252
253 RealType conservedQuantity;
254 RealType totalEnergy;
255 RealType thermostat_kinetic;
256 RealType thermostat_potential;
257 RealType barostat_kinetic;
258 RealType barostat_potential;
259 RealType trEta;
260
261 totalEnergy = thermo.getTotalEnergy();
262
263 thermostat_kinetic = fkBT * tt2 * thermostat.first * thermostat.first /
264 (2.0 * Constants::energyConvert);
265
266 thermostat_potential = fkBT * thermostat.second / Constants::energyConvert;
267
268 SquareMatrix<RealType, 3> tmp = eta.transpose() * eta;
269 trEta = tmp.trace();
270
271 barostat_kinetic = NkBT * tb2 * trEta / (2.0 * Constants::energyConvert);
272
273 barostat_potential =
274 (targetPressure * thermo.getVolume() / Constants::pressureConvert) /
275 Constants::energyConvert;
276
277 conservedQuantity = totalEnergy + thermostat_kinetic +
278 thermostat_potential + barostat_kinetic +
279 barostat_potential;
280
281 return conservedQuantity;
282 }
283
284 void NPTf::loadEta() {
285 eta = snap->getBarostat();
286
287 // if (!eta.isDiagonal()) {
288 // snprintf( painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
289 // "NPTf error: the diagonal elements of eta matrix are not the
290 // same or etaMat is not a diagonal matrix");
291 // painCave.isFatal = 1;
292 // simError();
293 //}
294 }
295
296 void NPTf::saveEta() { snap->setBarostat(eta); }
297
298} // namespace OpenMD
int getNdf()
Returns the number of degrees of freedom.
Definition SimInfo.hpp:220
int getNGlobalIntegrableObjects()
Returns the total number of integrable objects (total number of rigid bodies plus the total number of...
Definition SimInfo.hpp:139
Mat3x3d getHmat()
Returns the H-Matrix.
Definition Snapshot.cpp:214
void setHmat(const Mat3x3d &m)
Sets the H-Matrix.
Definition Snapshot.cpp:217
A square matrix class.
Real trace() const
Returns the trace of this matrix.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.