OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
LangevinHullForceModifier.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/LangevinHullForceModifier.hpp"
49
50#include <cmath>
51#include <fstream>
52#include <iostream>
53#include <map>
54#include <memory>
55#include <random>
56#include <string>
57
58#ifdef IS_MPI
59#include <mpi.h>
60#endif
61
62#include "brains/ForceModifier.hpp"
63#include "math/AlphaHull.hpp"
64#include "math/CholeskyDecomposition.hpp"
65#include "math/ConvexHull.hpp"
66#include "math/Triangle.hpp"
67#include "utils/Constants.hpp"
68
69using namespace std;
70
71namespace OpenMD {
72
73 LangevinHullForceModifier::LangevinHullForceModifier(SimInfo* info) :
74 ForceModifier {info} {
75 simParams_ = info->getSimParams();
76 veloMunge = std::make_unique<Velocitizer>(info);
77
78 // Create Hull, Convex Hull for now, other options later.
79
80 stringToEnumMap_["Convex"] = hullConvex;
81 stringToEnumMap_["AlphaShape"] = hullAlphaShape;
82 stringToEnumMap_["Unknown"] = hullUnknown;
83
84 const std::string ht = simParams_->getHULL_Method();
85
86 std::map<std::string, HullTypeEnum>::iterator iter;
87 iter = stringToEnumMap_.find(ht);
88 hullType_ = (iter == stringToEnumMap_.end()) ?
89 LangevinHullForceModifier::hullUnknown :
90 iter->second;
91
92 switch (hullType_) {
93 case hullConvex:
94 surfaceMesh_ = new ConvexHull();
95 break;
96 case hullAlphaShape:
97 surfaceMesh_ = new AlphaHull(simParams_->getAlpha());
98 break;
99 case hullUnknown:
100 default:
101 snprintf(
102 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
103 "LangevinHullForceModifier: Unknown Hull_Method was requested!\n");
104 painCave.isFatal = 1;
105 simError();
106 break;
107 }
108
109 doThermalCoupling_ = true;
110 doPressureCoupling_ = true;
111
112 /* Check that the simulation has target pressure and target
113 temperature set */
114 if (!simParams_->haveTargetTemp()) {
115 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
116 "LangevinHullForceModifier: no targetTemp (K) was set.\n"
117 "\tOpenMD is turning off the thermal coupling to the bath.\n");
118 painCave.isFatal = 0;
119 painCave.severity = OPENMD_INFO;
120 simError();
121 doThermalCoupling_ = false;
122 } else {
123 targetTemp_ = simParams_->getTargetTemp();
124
125 if (!simParams_->haveViscosity()) {
126 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
127 "LangevinHullForceModifier: no viscosity was set.\n"
128 "\tOpenMD is turning off the thermal coupling to the bath.\n");
129 painCave.isFatal = 0;
130 painCave.severity = OPENMD_INFO;
131 simError();
132 doThermalCoupling_ = false;
133 } else {
134 viscosity_ = simParams_->getViscosity();
135 if (fabs(viscosity_) < 1e-6) {
136 snprintf(
137 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
138 "LangevinHullForceModifier: The bath viscosity was set lower\n"
139 "\tthan 1e-6 poise. OpenMD is turning off the thermal\n"
140 "\tcoupling to the bath.\n");
141 painCave.isFatal = 0;
142 painCave.severity = OPENMD_INFO;
143 simError();
144 doThermalCoupling_ = false;
145 }
146 }
147 }
148 if (!simParams_->haveTargetPressure()) {
149 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
150 "LangevinHullForceModifier: no targetPressure (atm) was set.\n"
151 "\tOpenMD is turning off the pressure coupling to the bath.\n");
152 painCave.isFatal = 0;
153 painCave.severity = OPENMD_INFO;
154 simError();
155 doPressureCoupling_ = false;
156 } else {
157 // Convert pressure from atm -> amu/(fs^2*Ang)
158 targetPressure_ =
159 simParams_->getTargetPressure() / Constants::pressureConvert;
160 }
161 if (simParams_->getUsePeriodicBoundaryConditions()) {
162 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
163 "LangevinHullForceModifier: You can't use the Langevin Hull\n"
164 "\tintegrator with periodic boundary conditions turned on!\n");
165 painCave.isFatal = 1;
166 simError();
167 }
168
169 dt_ = simParams_->getDt();
170
171#ifdef IS_MPI
172 if (worldRank == 0) {
173#endif
174 if (doThermalCoupling_) {
175 randNumGen_ = info->getRandomNumberGenerator();
176
177 RealType stdDev = std::sqrt(2.0 * Constants::kb * targetTemp_ / dt_);
178
179 forceDistribution_ = std::normal_distribution<RealType>(0.0, stdDev);
180 }
181#ifdef IS_MPI
182 }
183#endif
184
185 // Build a vector of integrable objects to determine if the are
186 // surface atoms
187 Molecule* mol;
188 StuntDouble* sd;
189 SimInfo::MoleculeIterator i;
190 Molecule::IntegrableObjectIterator j;
191
192 for (mol = info_->beginMolecule(i); mol != NULL;
193 mol = info_->nextMolecule(i)) {
194 for (sd = mol->beginIntegrableObject(j); sd != NULL;
195 sd = mol->nextIntegrableObject(j)) {
196 localSites_.push_back(sd);
197 }
198 }
199
200 // We need to make an initial guess at the bounding box in order
201 // to compute long range forces in ForceMatrixDecomposition:
202
203 // Compute surface Mesh
204 surfaceMesh_->computeHull(localSites_);
205 }
206
207 LangevinHullForceModifier::~LangevinHullForceModifier() {
208 delete surfaceMesh_;
209 }
210
211 void LangevinHullForceModifier::modifyForces() {
212 int nTriangles, thisFacet;
213 RealType thisArea;
214 std::vector<Triangle> sMesh;
215 Triangle thisTriangle;
216 std::vector<Triangle>::iterator face;
217 std::vector<StuntDouble*> vertexSDs;
218 std::vector<StuntDouble*>::iterator vertex;
219
220 Vector3d unitNormal, centroid, facetVel;
221 Vector3d langevinForce, vertexForce;
222 Vector3d extPressure, randomForce, dragForce;
223
224 Mat3x3d hydroTensor, S;
225 std::vector<Vector3d> randNums;
226
227 // Compute surface Mesh
228 surfaceMesh_->computeHull(localSites_);
229 // Get number of surface stunt doubles
230 sMesh = surfaceMesh_->getMesh();
231 nTriangles = sMesh.size();
232
233 if (doThermalCoupling_) {
234 // Generate all of the necessary random forces
235 randNums = genTriangleForces(nTriangles);
236 }
237
238 // Loop over the mesh faces
239 thisFacet = 0;
240 for (face = sMesh.begin(); face != sMesh.end(); ++face) {
241 thisTriangle = *face;
242 vertexSDs = thisTriangle.getVertices();
243 thisArea = thisTriangle.getArea();
244 unitNormal = thisTriangle.getUnitNormal();
245 centroid = thisTriangle.getCentroid();
246 facetVel = thisTriangle.getFacetVelocity();
247
248 langevinForce = V3Zero;
249
250 if (doPressureCoupling_) {
251 extPressure = -unitNormal * (targetPressure_ * thisArea) /
252 Constants::energyConvert;
253 langevinForce += extPressure;
254 }
255
256 if (doThermalCoupling_) {
257 hydroTensor = thisTriangle.computeHydrodynamicTensor(viscosity_);
258 hydroTensor *= Constants::viscoConvert;
259 CholeskyDecomposition(hydroTensor, S);
260 randomForce = S * randNums[thisFacet++];
261 dragForce = -hydroTensor * facetVel;
262 langevinForce += randomForce + dragForce;
263 }
264
265 // Apply triangle force to stuntdouble vertices
266 for (vertex = vertexSDs.begin(); vertex != vertexSDs.end(); ++vertex) {
267 if ((*vertex) != NULL) {
268 vertexForce = langevinForce / RealType(3.0);
269 (*vertex)->addFrc(vertexForce);
270 }
271 }
272 }
273
274 if (simParams_->getConserveLinearMomentum()) veloMunge->removeComDrift();
275 if (simParams_->getConserveAngularMomentum())
276 veloMunge->removeAngularDrift();
277
278 Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
279 currSnapshot->setVolume(surfaceMesh_->getVolume());
280 currSnapshot->setHullVolume(surfaceMesh_->getVolume());
281 }
282
283 std::vector<Vector3d> LangevinHullForceModifier::genTriangleForces(
284 int nTriangles) {
285 // zero fill the random vector before starting:
286 std::vector<Vector3d> gaussRand(nTriangles);
287 std::fill(gaussRand.begin(), gaussRand.end(), V3Zero);
288
289#ifdef IS_MPI
290 if (worldRank == 0) {
291#endif
292 for (int i = 0; i < nTriangles; i++) {
293 gaussRand[i][0] = forceDistribution_(*randNumGen_);
294 gaussRand[i][1] = forceDistribution_(*randNumGen_);
295 gaussRand[i][2] = forceDistribution_(*randNumGen_);
296 }
297#ifdef IS_MPI
298 }
299 // push these out to the other processors
300 // Same command on all nodes:
301 MPI_Bcast(gaussRand[0].getArrayPointer(), nTriangles * 3, MPI_REALTYPE, 0,
302 MPI_COMM_WORLD);
303#endif
304
305 return gaussRand;
306 }
307} // namespace OpenMD
Abstract class for external ForceModifier classes.
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.