OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NPrT.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/NPrT.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 NPrT::NPrT(SimInfo* info) : NPT(info) {
59 Globals* simParams = info_->getSimParams();
60 if (!simParams->haveSurfaceTension()) {
61 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
62 "If you use the NPT integrator, you must set tauBarostat.\n");
63 painCave.severity = OPENMD_ERROR;
64 painCave.isFatal = 1;
65 simError();
66 } else {
67 surfaceTension_ = simParams->getSurfaceTension() *
68 Constants::surfaceTensionConvert *
69 Constants::energyConvert;
70
71 // Default value of privilegedAxis is "z"
72 if (simParams->getPrivilegedAxis() == "x")
73 axis_ = 0;
74 else if (simParams->getPrivilegedAxis() == "y")
75 axis_ = 1;
76 else if (simParams->getPrivilegedAxis() == "z")
77 axis_ = 2;
78
79 // Compute complementary axes to the privileged axis
80 axis1_ = (axis_ + 1) % 3;
81 axis2_ = (axis_ + 2) % 3;
82 }
83 }
84 void NPrT::evolveEtaA() {
85 Mat3x3d hmat = snap->getHmat();
86 RealType hz = hmat(axis_, axis_);
87 RealType Axy = hmat(axis1_, axis1_) * hmat(axis2_, axis2_);
88 RealType sx = -hz * (press(axis1_, axis1_) -
89 targetPressure / Constants::pressureConvert);
90 RealType sy = -hz * (press(axis2_, axis2_) -
91 targetPressure / Constants::pressureConvert);
92 eta(axis1_, axis1_) -= dt2 * Axy * (sx - surfaceTension_) / (NkBT * tb2);
93 eta(axis2_, axis2_) -= dt2 * Axy * (sy - surfaceTension_) / (NkBT * tb2);
94 eta(axis_, axis_) +=
95 dt2 * instaVol *
96 (press(axis_, axis_) - targetPressure / Constants::pressureConvert) /
97 (NkBT * tb2);
98 oldEta_ = eta;
99 }
100
101 void NPrT::evolveEtaB() {
102 Mat3x3d hmat = snap->getHmat();
103 RealType hz = hmat(axis_, axis_);
104 RealType Axy = hmat(axis1_, axis1_) * hmat(axis2_, axis2_);
105 prevEta_ = eta;
106 RealType sx = -hz * (press(axis1_, axis1_) -
107 targetPressure / Constants::pressureConvert);
108 RealType sy = -hz * (press(axis2_, axis2_) -
109 targetPressure / Constants::pressureConvert);
110 eta(axis1_, axis1_) = oldEta_(axis1_, axis1_) -
111 dt2 * Axy * (sx - surfaceTension_) / (NkBT * tb2);
112 eta(axis2_, axis2_) = oldEta_(axis2_, axis2_) -
113 dt2 * Axy * (sy - surfaceTension_) / (NkBT * tb2);
114 eta(axis_, axis_) = oldEta_(axis_, axis_) +
115 dt2 * instaVol *
116 (press(axis_, axis_) -
117 targetPressure / Constants::pressureConvert) /
118 (NkBT * tb2);
119 }
120
121 void NPrT::calcVelScale() {
122 for (int i = 0; i < 3; i++) {
123 for (int j = 0; j < 3; j++) {
124 vScale_(i, j) = eta(i, j);
125
126 if (i == j) { vScale_(i, j) += thermostat.first; }
127 }
128 }
129 }
130
131 void NPrT::getVelScaleA(Vector3d& sc, const Vector3d& vel) {
132 sc = vScale_ * vel;
133 }
134
135 void NPrT::getVelScaleB(Vector3d& sc, int index) {
136 sc = vScale_ * oldVel[index];
137 }
138
139 void NPrT::getPosScale(const Vector3d& pos, const Vector3d& COM, int index,
140 Vector3d& sc) {
141 /**@todo */
142 Vector3d rj = (oldPos[index] + pos) / (RealType)2.0 - COM;
143 sc = eta * rj;
144 }
145
146 void NPrT::scaleSimBox() {
147 Mat3x3d scaleMat;
148
149 scaleMat(axis1_, axis1_) = exp(dt * eta(axis1_, axis1_));
150 scaleMat(axis2_, axis2_) = exp(dt * eta(axis2_, axis2_));
151 scaleMat(axis_, axis_) = exp(dt * eta(axis_, axis_));
152 Mat3x3d hmat = snap->getHmat();
153 hmat = hmat * scaleMat;
154 snap->setHmat(hmat);
155 }
156
157 bool NPrT::etaConverged() {
158 int i;
159 RealType diffEta, sumEta;
160
161 sumEta = 0;
162 for (i = 0; i < 3; i++) {
163 sumEta += pow(prevEta_(i, i) - eta(i, i), 2);
164 }
165
166 diffEta = sqrt(sumEta / 3.0);
167
168 return (diffEta <= etaTolerance);
169 }
170
171 RealType NPrT::calcConservedQuantity() {
172 thermostat = snap->getThermostat();
173 loadEta();
174
175 // We need NkBT a lot, so just set it here: This is the RAW number
176 // of integrableObjects, so no subtraction or addition of constraints or
177 // orientational degrees of freedom:
178 NkBT = info_->getNGlobalIntegrableObjects() * Constants::kB * targetTemp;
179
180 // fkBT is used because the thermostat operates on more degrees of freedom
181 // than the barostat (when there are particles with orientational degrees
182 // of freedom).
183 fkBT = info_->getNdf() * Constants::kB * targetTemp;
184
185 RealType totalEnergy = thermo.getTotalEnergy();
186
187 RealType thermostat_kinetic = fkBT * tt2 * thermostat.first *
188 thermostat.first /
189 (2.0 * Constants::energyConvert);
190
191 RealType thermostat_potential =
192 fkBT * thermostat.second / Constants::energyConvert;
193
194 SquareMatrix<RealType, 3> tmp = eta.transpose() * eta;
195 RealType trEta = tmp.trace();
196
197 RealType barostat_kinetic =
198 NkBT * tb2 * trEta / (2.0 * Constants::energyConvert);
199
200 RealType barostat_potential =
201 (targetPressure * thermo.getVolume() / Constants::pressureConvert) /
202 Constants::energyConvert;
203
204 Mat3x3d hmat = snap->getHmat();
205 RealType area = hmat(axis1_, axis1_) * hmat(axis2_, axis2_);
206
207 RealType conservedQuantity =
208 totalEnergy + thermostat_kinetic + thermostat_potential +
209 barostat_kinetic + barostat_potential -
210 surfaceTension_ * area / Constants::energyConvert;
211
212 return conservedQuantity;
213 }
214
215 void NPrT::loadEta() {
216 eta = snap->getBarostat();
217
218 // if (!eta.isDiagonal()) {
219 // snprintf( painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
220 // "NPrT error: the diagonal elements of eta matrix are not the
221 // same or etaMat is not a diagonal matrix");
222 // painCave.isFatal = 1;
223 // simError();
224 //}
225 }
226
227 void NPrT::saveEta() { snap->setBarostat(eta); }
228
229} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.