OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ZconsVisitor.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 "visitors/ZconsVisitor.hpp"
49
50#include <cmath>
51#include <memory>
52
54#include "types/ZconsStamp.hpp"
55#include "utils/StringUtils.hpp"
56
57namespace OpenMD {
58
60 BaseVisitor(), zconsReader_(NULL), info_(info) {
61 visitorName = "ZConsVisitor";
62 currSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
63 Globals* simParam = info_->getSimParams();
64
65 if (simParam->haveZconsTime()) {
66 zconsTime_ = simParam->getZconsTime();
67 } else {
68 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
69 "ZConstraint error: If you use a ZConstraint,\n"
70 "\tyou must set zconsTime.\n");
71 painCave.isFatal = 1;
72 simError();
73 }
74
75 if (simParam->haveZconsTol()) {
76 zconsTol_ = simParam->getZconsTol();
77 } else {
78 zconsTol_ = 0.01;
79 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
80 "ZConstraint Warning: Tolerance for z-constraint method is not "
81 "specified.\n"
82 "\tOpenMD will use a default value of %f.\n"
83 "\tTo set the tolerance, use the zconsTol variable.\n",
84 zconsTol_);
85 painCave.isFatal = 0;
86 simError();
87 }
88
89 int nZconstraints = simParam->getNZconsStamps();
90 std::vector<ZConsStamp*> stamp = simParam->getZconsStamps();
91 for (int i = 0; i < nZconstraints; i++) {
92 int zmolIndex = stamp[i]->getMolIndex();
93 zmolStates_.insert(std::make_pair(zmolIndex, zsMoving));
94 }
95
96 // fill zatomToZmol_ array
97 /** @todo only works for single version now*/
98 std::map<int, ZConsState>::iterator j;
99 for (j = zmolStates_.begin(); j != zmolStates_.end(); ++j) {
100 Molecule* mol = info_->getMoleculeByGlobalIndex(j->first);
101 assert(mol != NULL);
102 Molecule::AtomIterator ai;
103 Atom* at;
104 for (at = mol->beginAtom(ai); at != NULL; at = mol->nextAtom(ai)) {
105 zatomToZmol_.insert(
106 std::make_pair(at->getGlobalIndex(), mol->getGlobalIndex()));
107 }
108 }
109
110 zconsFilename_ = getPrefix(info_->getFinalConfigFileName()) + ".fz";
111
112 zconsReader_ = new ZConsReader(info);
113
114 if (zconsReader_->hasNextFrame()) zconsReader_->readNextFrame();
115 }
116
117 ZConsVisitor::~ZConsVisitor() { delete zconsReader_; }
118
119 void ZConsVisitor::visit(Atom* atom) {
120 std::string prefix;
121 if (isZconstraint(atom->getGlobalIndex(), prefix))
122 internalVisit(atom, prefix);
123 }
124
125 void ZConsVisitor::visit(DirectionalAtom* datom) {
126 std::string prefix;
127
128 if (isZconstraint(datom->getGlobalIndex(), prefix))
129 internalVisit(datom, prefix);
130 }
131
132 void ZConsVisitor::visit(RigidBody* rb) {
133 std::string prefix;
134 std::vector<Atom*> atoms;
135
136 atoms = rb->getAtoms();
137
138 if (isZconstraint(atoms[0]->getGlobalIndex(), prefix))
139 internalVisit(rb, prefix);
140 }
141
142 void ZConsVisitor::update() {
143 Vector3d com;
144 std::map<int, ZConsState>::iterator i;
145 for (i = zmolStates_.begin(); i != zmolStates_.end(); ++i) {
146 i->second = zsMoving;
147 }
148
149 readZconsFile(currSnapshot_->getTime());
150
151 const std::vector<ZconsData>& fixedZmolData =
152 zconsReader_->getFixedZMolData();
153 std::vector<ZconsData>::const_iterator j;
154 for (j = fixedZmolData.begin(); j != fixedZmolData.end(); ++j) {
155 std::map<int, ZConsState>::iterator k = zmolStates_.find(j->zmolIndex);
156 assert(k != zmolStates_.end());
157 k->second = zsFixed;
158 }
159 }
160
161 void ZConsVisitor::readZconsFile(RealType time) {
162 RealType tempTime;
163 while (zconsReader_->hasNextFrame()) {
164 tempTime = zconsReader_->getCurTime();
165 if (tempTime >= time) { return; }
166
167 zconsReader_->readNextFrame();
168 }
169 }
170
171 void ZConsVisitor::internalVisit(StuntDouble* sd, const std::string& prefix) {
172 std::shared_ptr<GenericData> data;
173 std::shared_ptr<AtomData> atomData;
174 std::shared_ptr<AtomInfo> atomInfo;
175 std::vector<std::shared_ptr<AtomInfo>>::iterator iter;
176
177 // if there is not atom data, just skip it
178 data = sd->getPropertyByName("ATOMDATA");
179 if (data != nullptr) {
180 atomData = std::dynamic_pointer_cast<AtomData>(data);
181 if (atomData == nullptr) return;
182 } else
183 return;
184
185 for (atomInfo = atomData->beginAtomInfo(iter); atomInfo;
186 atomInfo = atomData->nextAtomInfo(iter))
187 (atomInfo->atomTypeName).insert(0, prefix);
188 }
189
190 bool ZConsVisitor::isZconstraint(int atomIndex, std::string& prefix) {
191 std::string prefixString[] = {"ZF", "ZM"};
192 std::map<int, int>::iterator i = zatomToZmol_.find(atomIndex);
193 if (i == zatomToZmol_.end()) {
194 prefix = "";
195 return false;
196 } else {
197 std::map<int, ZConsState>::iterator j = zmolStates_.find(i->second);
198 assert(j != zmolStates_.end());
199 prefix = prefixString[j->second];
200 return true;
201 }
202 }
203
204 const std::string ZConsVisitor::toString() {
205 char buffer[65535];
206 std::string result;
207
208 snprintf(
209 buffer, 65535,
210 "------------------------------------------------------------------\n");
211 result += buffer;
212
213 snprintf(buffer, 65535, "Visitor name: %s\n", visitorName.c_str());
214 result += buffer;
215
216 snprintf(buffer, 65535, "number of zconstraint molecule: %d\n",
217 (int)zmolStates_.size());
218 result += buffer;
219
220 snprintf(buffer, 65535, "zconstraint tolerance = %lf\n", zconsTol_);
221 result += buffer;
222
223 snprintf(buffer, 65535, "zconstraint sample time = %lf\n", zconsTime_);
224 result += buffer;
225
226 snprintf(buffer, 65535, "zconstraint output filename = %s\n",
227 zconsFilename_.c_str());
228 result += buffer;
229
230 std::map<int, ZConsState>::iterator i;
231 int j = 0;
232 for (i = zmolStates_.begin(); i != zmolStates_.end(); ++i) {
233 snprintf(buffer, 65535, "zconstraint molecule[%d] = %d\n", j++, i->first);
234 result += buffer;
235 }
236
237 snprintf(
238 buffer, 65535,
239 "------------------------------------------------------------------\n");
240 result += buffer;
241
242 return result;
243 }
244
245} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
ZConsVisitor(SimInfo *info)
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)