45#include "rnemd/SPFForceManager.hpp"
57#include "brains/Snapshot.hpp"
58#include "brains/Thermo.hpp"
61#include "nonbonded/NonBondedInteraction.hpp"
65namespace OpenMD::RNEMD {
67 SPFForceManager::SPFForceManager(
SimInfo* info) :
68 ForceManager {info}, potentialSource_ {}, potentialSink_ {} {
69 thermo_ = std::make_unique<Thermo>(info);
70 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
72 k_ = info_->getSimParams()->getRNEMDParameters()->getSPFScalingPower();
74 int nAtoms = info_->getNAtoms();
75 int nRigidBodies = info_->getNRigidBodies();
76 int nCutoffGroups = info_->getNCutoffGroups();
78 int atomStorageLayout = info_->getSnapshotManager()->getAtomStorageLayout();
80 info_->getSnapshotManager()->getRigidBodyStorageLayout();
82 info_->getSnapshotManager()->getCutoffGroupStorageLayout();
84 bool usePBC = info_->getSimParams()->getUsePeriodicBoundaryConditions();
86 temporarySourceSnapshot_ =
87 new Snapshot(nAtoms, nRigidBodies, nCutoffGroups, atomStorageLayout,
88 rbStorageLayout, cgStorageLayout, usePBC);
90 temporarySinkSnapshot_ =
91 new Snapshot(nAtoms, nRigidBodies, nCutoffGroups, atomStorageLayout,
92 rbStorageLayout, cgStorageLayout, usePBC);
95 SPFForceManager::~SPFForceManager() {
96 delete temporarySourceSnapshot_;
97 delete temporarySinkSnapshot_;
100 void SPFForceManager::calcForces() {
102 ForceManager::calcForces();
103 potentialSource_ = currentSnapshot_->getPotentialEnergy();
105 if (hasSelectedMolecule_) {
106 Vector3d prevSourceCom {}, currentSourceCom {}, delta {};
109 if (selectedMolecule_) {
110 prevSourceCom = selectedMolecule_->getPrevCom();
111 currentSourceCom = selectedMolecule_->getCom();
113 delta = currentSourceCom - prevSourceCom;
116 if (temporarySourceSnapshot_ && currentSnapshot_) {
117 *temporarySourceSnapshot_ = *currentSnapshot_;
120 sourceNeighborList_ = neighborList_;
121 sourcePoint_ = point_;
122 sourceSavedPositions_ = savedPositions_;
124 neighborList_ = sinkNeighborList_;
126 savedPositions_ = sinkSavedPositions_;
128 currentSnapshot_->clearDerivedProperties();
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;
139 if (selectedMolecule_) {
140 currentSnapshot_->getSPFData()->pos += delta;
141 selectedMolecule_->setCom(currentSnapshot_->getSPFData()->pos);
145 int globalSelectedID = currentSnapshot_->getSPFData()->globalID;
147 MPI_Bcast(¤tSnapshot_->getSPFData()->pos[0], 3, MPI_REALTYPE,
148 info_->getMolToProc(globalSelectedID), MPI_COMM_WORLD);
152 ForceManager::calcForces();
153 potentialSink_ = currentSnapshot_->getPotentialEnergy();
155 if (temporarySinkSnapshot_ && currentSnapshot_) {
156 *temporarySinkSnapshot_ = *currentSnapshot_;
159 sinkNeighborList_ = neighborList_;
161 sinkSavedPositions_ = savedPositions_;
163 neighborList_ = sourceNeighborList_;
164 point_ = sourcePoint_;
165 savedPositions_ = sourceSavedPositions_;
167 currentSnapshot_->clearDerivedProperties();
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;
177 combineForcesAndTorques();
179 updateVirialTensor();
182 if (selectedMolecule_) { selectedMolecule_->setCom(currentSourceCom); }
185 *temporarySourceSnapshot_ = *currentSnapshot_;
186 *temporarySinkSnapshot_ = *currentSnapshot_;
187 potentialSink_ = currentSnapshot_->getPotentialEnergy();
191 void SPFForceManager::setSelectedMolecule(
Molecule* selectedMolecule) {
192 if (selectedMolecule) {
193 selectedMolecule_ = selectedMolecule;
195 selectedMolecule_ =
nullptr;
199 bool SPFForceManager::updateLambda(RealType& particleTarget,
200 RealType& deltaLambda) {
201 bool updateSelectedMolecule {
false};
203 if (hasSelectedMolecule_) {
204 currentSnapshot_->getSPFData()->lambda += std::fabs(particleTarget);
206 if (f_lambda(currentSnapshot_->getSPFData()->lambda +
207 std::fabs(particleTarget)) > 1.0 &&
208 f_lambda(currentSnapshot_->getSPFData()->lambda) < 1.0) {
210 particleTarget - (f_lambda(currentSnapshot_->getSPFData()->lambda +
211 std::fabs(particleTarget)) -
214 deltaLambda = particleTarget;
217 currentSnapshot_->clearDerivedProperties();
219 combineForcesAndTorques();
221 updateVirialTensor();
223 if (f_lambda(currentSnapshot_->getSPFData()->lambda) > 1.0 ||
224 std::fabs(f_lambda(currentSnapshot_->getSPFData()->lambda) - 1.0) <
226 currentSnapshot_->getSPFData()->lambda = 0.0;
227 currentSnapshot_->getSPFData()->globalID = -1;
230 if (selectedMolecule_) {
231 selectedMolecule_->setCom(currentSnapshot_->getSPFData()->pos);
232 selectedMolecule_ =
nullptr;
235 neighborList_ = sinkNeighborList_;
237 savedPositions_ = sinkSavedPositions_;
239 hasSelectedMolecule_ =
false;
240 updateSelectedMolecule =
true;
244 return updateSelectedMolecule;
247 void SPFForceManager::combineForcesAndTorques() {
249 SimInfo::MoleculeIterator mi;
251 Molecule::IntegrableObjectIterator ii;
254 RealType result = f_lambda(currentSnapshot_->getSPFData()->lambda);
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)) {
262 temporarySinkSnapshot_, 1.0 - result,
268 void SPFForceManager::updatePotentials() {
269 updateLongRangePotentials();
270 updateShortRangePotentials();
271 updateSelfPotentials();
272 updateExcludedPotentials();
273 updateRestraintPotentials();
274 if (doPotentialSelection_) updateSelectionPotentials();
277 void SPFForceManager::updateLongRangePotentials() {
278 potVec longRangePotentials =
279 linearCombination(temporarySourceSnapshot_->getLongRangePotentials(),
280 temporarySinkSnapshot_->getLongRangePotentials());
281 currentSnapshot_->setLongRangePotentials(longRangePotentials);
283 RealType reciprocalPotential =
284 linearCombination(temporarySourceSnapshot_->getReciprocalPotential(),
285 temporarySinkSnapshot_->getReciprocalPotential());
286 currentSnapshot_->setReciprocalPotential(reciprocalPotential);
288 RealType surfacePotential =
289 linearCombination(temporarySourceSnapshot_->getSurfacePotential(),
290 temporarySinkSnapshot_->getSurfacePotential());
291 currentSnapshot_->setSurfacePotential(surfacePotential);
294 void SPFForceManager::updateShortRangePotentials() {
295 RealType bondPotential =
296 linearCombination(temporarySourceSnapshot_->getBondPotential(),
297 temporarySinkSnapshot_->getBondPotential());
298 currentSnapshot_->setBondPotential(bondPotential);
300 RealType bendPotential =
301 linearCombination(temporarySourceSnapshot_->getBendPotential(),
302 temporarySinkSnapshot_->getBendPotential());
303 currentSnapshot_->setBendPotential(bendPotential);
305 RealType torsionPotential =
306 linearCombination(temporarySourceSnapshot_->getTorsionPotential(),
307 temporarySinkSnapshot_->getTorsionPotential());
308 currentSnapshot_->setTorsionPotential(torsionPotential);
310 RealType inversionPotential =
311 linearCombination(temporarySourceSnapshot_->getInversionPotential(),
312 temporarySinkSnapshot_->getInversionPotential());
313 currentSnapshot_->setInversionPotential(inversionPotential);
316 void SPFForceManager::updateSelfPotentials() {
318 linearCombination(temporarySourceSnapshot_->getSelfPotentials(),
319 temporarySinkSnapshot_->getSelfPotentials());
320 currentSnapshot_->setSelfPotentials(selfPotentials);
323 void SPFForceManager::updateExcludedPotentials() {
324 potVec excludedPotentials =
325 linearCombination(temporarySourceSnapshot_->getExcludedPotentials(),
326 temporarySinkSnapshot_->getExcludedPotentials());
327 currentSnapshot_->setExcludedPotentials(excludedPotentials);
330 void SPFForceManager::updateRestraintPotentials() {
331 RealType restraintPotential =
332 linearCombination(temporarySourceSnapshot_->getRestraintPotential(),
333 temporarySinkSnapshot_->getRestraintPotential());
334 currentSnapshot_->setRestraintPotential(restraintPotential);
337 void SPFForceManager::updateSelectionPotentials() {
338 potVec selectionPotentials =
339 linearCombination(temporarySourceSnapshot_->getSelectionPotentials(),
340 temporarySinkSnapshot_->getSelectionPotentials());
341 currentSnapshot_->setSelectionPotentials(selectionPotentials);
344 void SPFForceManager::updateVirialTensor() {
346 linearCombination(temporarySourceSnapshot_->getVirialTensor(),
347 temporarySinkSnapshot_->getVirialTensor());
348 currentSnapshot_->setVirialTensor(virialTensor);
351 linearCombination(thermo_->getPressureTensor(temporarySourceSnapshot_),
352 thermo_->getPressureTensor(temporarySinkSnapshot_));
353 currentSnapshot_->setPressureTensor(pressureTensor);
356 linearCombination(thermo_->getPressure(temporarySourceSnapshot_),
357 thermo_->getPressure(temporarySinkSnapshot_));
358 currentSnapshot_->setPressure(pressure);
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...
The Snapshot class is a repository storing dynamic data during a Simulation.
"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.