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