OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SPFForceManager.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 "rnemd/SPFForceManager.hpp"
46
47#include <config.h>
48
49#include <vector>
50
51#ifdef IS_MPI
52#include <mpi.h>
53#endif
54
56#include "brains/SimInfo.hpp"
57#include "brains/Snapshot.hpp"
58#include "brains/Thermo.hpp"
60#include "math/Vector3.hpp"
61#include "nonbonded/NonBondedInteraction.hpp"
64
65namespace OpenMD::RNEMD {
66
67 SPFForceManager::SPFForceManager(SimInfo* info) :
68 ForceManager {info}, potentialSource_ {}, potentialSink_ {} {
69 thermo_ = std::make_unique<Thermo>(info);
70 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
71
72 k_ = info_->getSimParams()->getRNEMDParameters()->getSPFScalingPower();
73
74 int nAtoms = info_->getNAtoms();
75 int nRigidBodies = info_->getNRigidBodies();
76 int nCutoffGroups = info_->getNCutoffGroups();
77
78 int atomStorageLayout = info_->getSnapshotManager()->getAtomStorageLayout();
79 int rbStorageLayout =
80 info_->getSnapshotManager()->getRigidBodyStorageLayout();
81 int cgStorageLayout =
82 info_->getSnapshotManager()->getCutoffGroupStorageLayout();
83
84 bool usePBC = info_->getSimParams()->getUsePeriodicBoundaryConditions();
85
86 temporarySourceSnapshot_ =
87 new Snapshot(nAtoms, nRigidBodies, nCutoffGroups, atomStorageLayout,
88 rbStorageLayout, cgStorageLayout, usePBC);
89
90 temporarySinkSnapshot_ =
91 new Snapshot(nAtoms, nRigidBodies, nCutoffGroups, atomStorageLayout,
92 rbStorageLayout, cgStorageLayout, usePBC);
93 }
94
95 SPFForceManager::~SPFForceManager() {
96 delete temporarySourceSnapshot_;
97 delete temporarySinkSnapshot_;
98 }
99
100 void SPFForceManager::calcForces() {
101 // Current snapshot with selected molecule in source slab
102 ForceManager::calcForces();
103 potentialSource_ = currentSnapshot_->getPotentialEnergy();
104
105 if (hasSelectedMolecule_) {
106 Vector3d prevSourceCom {}, currentSourceCom {}, delta {};
107
108 // Only the processor with the selected molecule should do this step:
109 if (selectedMolecule_) {
110 prevSourceCom = selectedMolecule_->getPrevCom();
111 currentSourceCom = selectedMolecule_->getCom();
112
113 delta = currentSourceCom - prevSourceCom;
114 }
115
116 if (temporarySourceSnapshot_ && currentSnapshot_) {
117 *temporarySourceSnapshot_ = *currentSnapshot_;
118
119 // Save source Verlet neighbor list information:
120 sourceNeighborList_ = neighborList_;
121 sourcePoint_ = point_;
122 sourceSavedPositions_ = savedPositions_;
123 // Use sink Verlet neighbor list information:
124 neighborList_ = sinkNeighborList_;
125 point_ = sinkPoint_;
126 savedPositions_ = sinkSavedPositions_;
127
128 currentSnapshot_->clearDerivedProperties();
129 } else {
130 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
131 "Either temporarySourceSnapshot or currentSnapshot "
132 "has a null value.\n");
133 painCave.isFatal = 1;
134 painCave.severity = OPENMD_ERROR;
135 simError();
136 }
137
138 // Only the processor with the selected molecule should do this step:
139 if (selectedMolecule_) {
140 currentSnapshot_->getSPFData()->pos += delta;
141 selectedMolecule_->setCom(currentSnapshot_->getSPFData()->pos);
142 }
143
144#ifdef IS_MPI
145 int globalSelectedID = currentSnapshot_->getSPFData()->globalID;
146
147 MPI_Bcast(&currentSnapshot_->getSPFData()->pos[0], 3, MPI_REALTYPE,
148 info_->getMolToProc(globalSelectedID), MPI_COMM_WORLD);
149#endif
150
151 // Current snapshot with selected molecule in sink slab
152 ForceManager::calcForces();
153 potentialSink_ = currentSnapshot_->getPotentialEnergy();
154
155 if (temporarySinkSnapshot_ && currentSnapshot_) {
156 *temporarySinkSnapshot_ = *currentSnapshot_;
157
158 // Save source Verlet neighbor list information:
159 sinkNeighborList_ = neighborList_;
160 sinkPoint_ = point_;
161 sinkSavedPositions_ = savedPositions_;
162 // Use source Verlet neighbor list information:
163 neighborList_ = sourceNeighborList_;
164 point_ = sourcePoint_;
165 savedPositions_ = sourceSavedPositions_;
166
167 currentSnapshot_->clearDerivedProperties();
168 } else {
169 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
170 "Either temporarySinkSnapshot or currentSnapshot "
171 "has a null value.\n");
172 painCave.isFatal = 1;
173 painCave.severity = OPENMD_ERROR;
174 simError();
175 }
176
177 combineForcesAndTorques();
178 updatePotentials();
179 updateVirialTensor();
180
181 // Only the processor with the selected molecule should do this step:
182 if (selectedMolecule_) { selectedMolecule_->setCom(currentSourceCom); }
183
184 } else {
185 *temporarySourceSnapshot_ = *currentSnapshot_;
186 *temporarySinkSnapshot_ = *currentSnapshot_;
187 potentialSink_ = currentSnapshot_->getPotentialEnergy();
188 }
189 }
190
191 void SPFForceManager::setSelectedMolecule(Molecule* selectedMolecule) {
192 if (selectedMolecule) {
193 selectedMolecule_ = selectedMolecule;
194 } else {
195 selectedMolecule_ = nullptr;
196 }
197 }
198
199 bool SPFForceManager::updateLambda(RealType& particleTarget,
200 RealType& deltaLambda) {
201 bool updateSelectedMolecule {false};
202
203 if (hasSelectedMolecule_) {
204 currentSnapshot_->getSPFData()->lambda += std::fabs(particleTarget);
205
206 if (f_lambda(currentSnapshot_->getSPFData()->lambda +
207 std::fabs(particleTarget)) > 1.0 &&
208 f_lambda(currentSnapshot_->getSPFData()->lambda) < 1.0) {
209 deltaLambda =
210 particleTarget - (f_lambda(currentSnapshot_->getSPFData()->lambda +
211 std::fabs(particleTarget)) -
212 1.0);
213 } else {
214 deltaLambda = particleTarget;
215 }
216
217 currentSnapshot_->clearDerivedProperties();
218
219 combineForcesAndTorques();
220 updatePotentials();
221 updateVirialTensor();
222
223 if (f_lambda(currentSnapshot_->getSPFData()->lambda) > 1.0 ||
224 std::fabs(f_lambda(currentSnapshot_->getSPFData()->lambda) - 1.0) <
225 1e-6) {
226 currentSnapshot_->getSPFData()->lambda = 0.0;
227 currentSnapshot_->getSPFData()->globalID = -1;
228
229 // Only the processor with the selected molecule should do this step:
230 if (selectedMolecule_) {
231 selectedMolecule_->setCom(currentSnapshot_->getSPFData()->pos);
232 selectedMolecule_ = nullptr;
233 }
234
235 neighborList_ = sinkNeighborList_;
236 point_ = sinkPoint_;
237 savedPositions_ = sinkSavedPositions_;
238
239 hasSelectedMolecule_ = false;
240 updateSelectedMolecule = true;
241 }
242 }
243
244 return updateSelectedMolecule;
245 }
246
247 void SPFForceManager::combineForcesAndTorques() {
248 // Calculate lambda-averaged forces on all atoms and potentials:
249 SimInfo::MoleculeIterator mi;
250 Molecule* mol;
251 Molecule::IntegrableObjectIterator ii;
252 StuntDouble* sd;
253
254 RealType result = f_lambda(currentSnapshot_->getSPFData()->lambda);
255
256 // Now scale forces and torques of all the sds
257 for (mol = info_->beginMolecule(mi); mol != NULL;
258 mol = info_->nextMolecule(mi)) {
259 for (sd = mol->beginIntegrableObject(ii); sd != NULL;
260 sd = mol->nextIntegrableObject(ii)) {
261 sd->combineForcesAndTorques(temporarySourceSnapshot_,
262 temporarySinkSnapshot_, 1.0 - result,
263 result);
264 }
265 }
266 }
267
268 void SPFForceManager::updatePotentials() {
269 updateLongRangePotentials();
270 updateShortRangePotentials();
271 updateSelfPotentials();
272 updateExcludedPotentials();
273 updateRestraintPotentials();
274 if (doPotentialSelection_) updateSelectionPotentials();
275 }
276
277 void SPFForceManager::updateLongRangePotentials() {
278 potVec longRangePotentials =
279 linearCombination(temporarySourceSnapshot_->getLongRangePotentials(),
280 temporarySinkSnapshot_->getLongRangePotentials());
281 currentSnapshot_->setLongRangePotentials(longRangePotentials);
282
283 RealType reciprocalPotential =
284 linearCombination(temporarySourceSnapshot_->getReciprocalPotential(),
285 temporarySinkSnapshot_->getReciprocalPotential());
286 currentSnapshot_->setReciprocalPotential(reciprocalPotential);
287
288 RealType surfacePotential =
289 linearCombination(temporarySourceSnapshot_->getSurfacePotential(),
290 temporarySinkSnapshot_->getSurfacePotential());
291 currentSnapshot_->setSurfacePotential(surfacePotential);
292 }
293
294 void SPFForceManager::updateShortRangePotentials() {
295 RealType bondPotential =
296 linearCombination(temporarySourceSnapshot_->getBondPotential(),
297 temporarySinkSnapshot_->getBondPotential());
298 currentSnapshot_->setBondPotential(bondPotential);
299
300 RealType bendPotential =
301 linearCombination(temporarySourceSnapshot_->getBendPotential(),
302 temporarySinkSnapshot_->getBendPotential());
303 currentSnapshot_->setBendPotential(bendPotential);
304
305 RealType torsionPotential =
306 linearCombination(temporarySourceSnapshot_->getTorsionPotential(),
307 temporarySinkSnapshot_->getTorsionPotential());
308 currentSnapshot_->setTorsionPotential(torsionPotential);
309
310 RealType inversionPotential =
311 linearCombination(temporarySourceSnapshot_->getInversionPotential(),
312 temporarySinkSnapshot_->getInversionPotential());
313 currentSnapshot_->setInversionPotential(inversionPotential);
314 }
315
316 void SPFForceManager::updateSelfPotentials() {
317 potVec selfPotentials =
318 linearCombination(temporarySourceSnapshot_->getSelfPotentials(),
319 temporarySinkSnapshot_->getSelfPotentials());
320 currentSnapshot_->setSelfPotentials(selfPotentials);
321 }
322
323 void SPFForceManager::updateExcludedPotentials() {
324 potVec excludedPotentials =
325 linearCombination(temporarySourceSnapshot_->getExcludedPotentials(),
326 temporarySinkSnapshot_->getExcludedPotentials());
327 currentSnapshot_->setExcludedPotentials(excludedPotentials);
328 }
329
330 void SPFForceManager::updateRestraintPotentials() {
331 RealType restraintPotential =
332 linearCombination(temporarySourceSnapshot_->getRestraintPotential(),
333 temporarySinkSnapshot_->getRestraintPotential());
334 currentSnapshot_->setRestraintPotential(restraintPotential);
335 }
336
337 void SPFForceManager::updateSelectionPotentials() {
338 potVec selectionPotentials =
339 linearCombination(temporarySourceSnapshot_->getSelectionPotentials(),
340 temporarySinkSnapshot_->getSelectionPotentials());
341 currentSnapshot_->setSelectionPotentials(selectionPotentials);
342 }
343
344 void SPFForceManager::updateVirialTensor() {
345 Mat3x3d virialTensor =
346 linearCombination(temporarySourceSnapshot_->getVirialTensor(),
347 temporarySinkSnapshot_->getVirialTensor());
348 currentSnapshot_->setVirialTensor(virialTensor);
349
350 Mat3x3d pressureTensor =
351 linearCombination(thermo_->getPressureTensor(temporarySourceSnapshot_),
352 thermo_->getPressureTensor(temporarySinkSnapshot_));
353 currentSnapshot_->setPressureTensor(pressureTensor);
354
355 RealType pressure =
356 linearCombination(thermo_->getPressure(temporarySourceSnapshot_),
357 thermo_->getPressure(temporarySinkSnapshot_));
358 currentSnapshot_->setPressure(pressure);
359 }
360} // namespace OpenMD::RNEMD
ForceManager is responsible for calculating both the short range (bonded) interactions and long range...
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:93
The Snapshot class is a repository storing dynamic data during a Simulation.
Definition Snapshot.hpp:147
"Don't move, or you're dead! Stand up! Captain, we've got them!"
void combineForcesAndTorques(Snapshot *snapA, Snapshot *snapB, RealType multA, RealType multB)
Linearly combines the properties from two different snapshots.