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