OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
NameFinder.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#include "selection/NameFinder.hpp"
48
51#include "utils/StringUtils.hpp"
52#include "utils/simError.h"
53#include "utils/wildcards.hpp"
54
55namespace OpenMD {
56
57 NameFinder::NameFinder(SimInfo* info) : info_(info) {
58 nObjects_.push_back(info_->getNGlobalAtoms() +
59 info_->getNGlobalRigidBodies());
60 nObjects_.push_back(info_->getNGlobalBonds());
61 nObjects_.push_back(info_->getNGlobalBends());
62 nObjects_.push_back(info_->getNGlobalTorsions());
63 nObjects_.push_back(info_->getNGlobalInversions());
64 nObjects_.push_back(info_->getNGlobalMolecules());
65
66 loadNames();
67 }
68
69 void NameFinder::loadNames() {
70 SimInfo::MoleculeIterator mi;
71 Molecule::AtomIterator ai;
72 Molecule::RigidBodyIterator rbIter;
73 Molecule::BondIterator bondIter;
74 Molecule::BendIterator bendIter;
75 Molecule::TorsionIterator torsionIter;
76 Molecule::InversionIterator inversionIter;
77
78 Molecule* mol;
79 Atom* atom;
80 RigidBody* rb;
81 Bond* bond;
82 Bend* bend;
83 Torsion* torsion;
84 Inversion* inversion;
85
86 root_ = std::make_shared<TreeNode>();
87 root_->bs.resize(nObjects_);
88 root_->bs.setAll(); //
89
90 for (mol = info_->beginMolecule(mi); mol != NULL;
91 mol = info_->nextMolecule(mi)) {
92 std::string molName = mol->getType();
93 TreeNodePtr molNode = createNode(root_, molName);
94 molNode->bs.bitsets_[MOLECULE].setBitOn(mol->getGlobalIndex());
95
96 for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
97 std::string atomName = atom->getType();
98 TreeNodePtr atomNode = createNode(molNode, atomName);
99
100 molNode->bs.bitsets_[STUNTDOUBLE].setBitOn(atom->getGlobalIndex());
101 atomNode->bs.bitsets_[STUNTDOUBLE].setBitOn(atom->getGlobalIndex());
102 }
103
104 for (rb = mol->beginRigidBody(rbIter); rb != NULL;
105 rb = mol->nextRigidBody(rbIter)) {
106 std::string rbName = rb->getType();
107 TreeNodePtr rbNode = createNode(molNode, rbName);
108
109 molNode->bs.bitsets_[STUNTDOUBLE].setBitOn(rb->getGlobalIndex());
110 rbNode->bs.bitsets_[STUNTDOUBLE].setBitOn(rb->getGlobalIndex());
111
112 // COMMENTED OUT because rigid bodies are IntegrableObjects
113 // (e.g. they are independently mobile, so selecting their
114 // member atoms will give some odd results if we are computing
115 // degrees of freedom elsewhere.
116
117 // //create nodes for atoms belong to this rigidbody
118 // for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai))
119 // {
120 // std::string rbAtomName = atom->getType();
121 // TreeNodePtr rbAtomNode = createNode(rbNode, rbAtomName);
122
123 // rbAtomNode->bs.bitsets_[STUNTDOUBLE].setBitOn(atom->getGlobalIndex());
124 // }
125 }
126
127 for (bond = mol->beginBond(bondIter); bond != NULL;
128 bond = mol->nextBond(bondIter)) {
129 std::string bondName = bond->getName();
130 TreeNodePtr bondNode = createNode(molNode, bondName);
131
132 molNode->bs.bitsets_[BOND].setBitOn(bond->getGlobalIndex());
133 bondNode->bs.bitsets_[BOND].setBitOn(bond->getGlobalIndex());
134
135 std::vector<Atom*> atoms = bond->getAtoms();
136 std::vector<Atom*>::iterator ai;
137
138 for (ai = atoms.begin(); ai != atoms.end(); ++ai) {
139 std::string atomName = (*ai)->getType();
140 TreeNodePtr atomNode = createNode(bondNode, atomName);
141 atomNode->bs.bitsets_[STUNTDOUBLE].setBitOn((*ai)->getGlobalIndex());
142 }
143 }
144 for (bend = mol->beginBend(bendIter); bend != NULL;
145 bend = mol->nextBend(bendIter)) {
146 std::string bendName = bend->getName();
147 TreeNodePtr bendNode = createNode(molNode, bendName);
148
149 molNode->bs.bitsets_[BEND].setBitOn(bend->getGlobalIndex());
150 bendNode->bs.bitsets_[BEND].setBitOn(bend->getGlobalIndex());
151
152 std::vector<Atom*> atoms = bend->getAtoms();
153 std::vector<Atom*>::iterator ai;
154
155 for (ai = atoms.begin(); ai != atoms.end(); ++ai) {
156 std::string atomName = (*ai)->getType();
157 TreeNodePtr atomNode = createNode(bendNode, atomName);
158 atomNode->bs.bitsets_[STUNTDOUBLE].setBitOn((*ai)->getGlobalIndex());
159 }
160 }
161 for (torsion = mol->beginTorsion(torsionIter); torsion != NULL;
162 torsion = mol->nextTorsion(torsionIter)) {
163 std::string torsionName = torsion->getName();
164 TreeNodePtr torsionNode = createNode(molNode, torsionName);
165
166 molNode->bs.bitsets_[TORSION].setBitOn(torsion->getGlobalIndex());
167 torsionNode->bs.bitsets_[TORSION].setBitOn(torsion->getGlobalIndex());
168
169 std::vector<Atom*> atoms = torsion->getAtoms();
170 std::vector<Atom*>::iterator ai;
171
172 for (ai = atoms.begin(); ai != atoms.end(); ++ai) {
173 std::string atomName = (*ai)->getType();
174 TreeNodePtr atomNode = createNode(torsionNode, atomName);
175 atomNode->bs.bitsets_[STUNTDOUBLE].setBitOn((*ai)->getGlobalIndex());
176 }
177 }
178 for (inversion = mol->beginInversion(inversionIter); inversion != NULL;
179 inversion = mol->nextInversion(inversionIter)) {
180 std::string inversionName = inversion->getName();
181 TreeNodePtr inversionNode = createNode(molNode, inversionName);
182
183 molNode->bs.bitsets_[INVERSION].setBitOn(inversion->getGlobalIndex());
184 inversionNode->bs.bitsets_[INVERSION].setBitOn(
185 inversion->getGlobalIndex());
186 std::vector<Atom*> atoms = inversion->getAtoms();
187 std::vector<Atom*>::iterator ai;
188
189 for (ai = atoms.begin(); ai != atoms.end(); ++ai) {
190 std::string atomName = (*ai)->getType();
191 TreeNodePtr atomNode = createNode(inversionNode, atomName);
192 atomNode->bs.bitsets_[STUNTDOUBLE].setBitOn((*ai)->getGlobalIndex());
193 }
194 }
195 }
196 }
197
198 TreeNodePtr NameFinder::createNode(TreeNodePtr parent,
199 const std::string& name) {
200 TreeNodePtr node;
201 std::map<std::string, TreeNodePtr>::iterator foundIter;
202 foundIter = parent->children.find(name);
203 if (foundIter == parent->children.end()) {
204 node = std::make_shared<TreeNode>();
205 node->name = name;
206 node->bs.resize(nObjects_);
207 parent->children.insert(std::make_pair(name, node));
208 } else {
209 node = foundIter->second;
210 }
211 return node;
212 }
213
214 SelectionSet NameFinder::match(const std::string& name) {
215 SelectionSet bs(nObjects_);
216
217 StringTokenizer tokenizer(name, ".");
218
219 std::vector<std::string> names;
220 while (tokenizer.hasMoreTokens()) {
221 names.push_back(tokenizer.nextToken());
222 }
223
224 int size = names.size();
225
226 switch (size) {
227 case 1:
228 // could be molecule name, atom name and rigidbody name
229 matchMolecule(names[0], bs);
230 matchStuntDouble("*", names[0], bs);
231 matchBond("*", names[0], bs);
232 matchBend("*", names[0], bs);
233 matchTorsion("*", names[0], bs);
234 matchInversion("*", names[0], bs);
235
236 break;
237 case 2:
238 // could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms
239 // belong to rigidbody)
240
241 if (!isInteger(names[1])) {
242 matchRigidAtoms("*", names[0], names[1], bs);
243 matchStuntDouble(names[0], names[1], bs);
244 } else {
245 int internalIndex = lexi_cast<int>(names[1]);
246 if (internalIndex < 0) {
247 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
248 "NameFinder : Name %s.%s is an invalid name.\n",
249 names[0].c_str(), names[1].c_str());
250 painCave.severity = OPENMD_WARNING;
251 painCave.isFatal = 0;
252 simError();
253 } else {
254 matchInternalIndex(names[0], internalIndex, bs);
255 }
256 }
257
258 break;
259 case 3:
260 // must be molecule.rigidbody.*
261 matchRigidAtoms(names[0], names[1], names[2], bs);
262 break;
263 default:
264 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
265 "NameFinder : Invalid Name %s.\n", name.c_str());
266 painCave.severity = OPENMD_WARNING;
267 painCave.isFatal = 0;
268 simError();
269 break;
270 }
271 return bs;
272 }
273
274 void NameFinder::matchMolecule(const std::string& molName, SelectionSet& bs) {
275 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
276 std::vector<TreeNodePtr>::iterator i;
277 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
278 bs |= (*i)->bs;
279 }
280 }
281
282 void NameFinder::matchStuntDouble(const std::string& molName,
283 const std::string& sdName,
284 SelectionSet& bs) {
285 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
286 std::vector<TreeNodePtr>::iterator i;
287 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
288 std::vector<TreeNodePtr> sdNodes = getMatchedChildren(*i, sdName);
289 std::vector<TreeNodePtr>::iterator j;
290 for (j = sdNodes.begin(); j != sdNodes.end(); ++j) {
291 bs |= (*j)->bs;
292 }
293 }
294 }
295
296 void NameFinder::matchBond(const std::string& molName,
297 const std::string& bondName, SelectionSet& bs) {
298 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
299 std::vector<TreeNodePtr>::iterator i;
300 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
301 std::vector<TreeNodePtr> bondNodes = getMatchedChildren(*i, bondName);
302 std::vector<TreeNodePtr>::iterator j;
303 for (j = bondNodes.begin(); j != bondNodes.end(); ++j) {
304 bs |= (*j)->bs;
305 std::vector<TreeNodePtr> bondAtomNodes = getAllChildren(*j);
306 std::vector<TreeNodePtr>::iterator k;
307 for (k = bondAtomNodes.begin(); k != bondAtomNodes.end(); ++k) {
308 bs |= (*k)->bs;
309 }
310 }
311 }
312 }
313
314 void NameFinder::matchBend(const std::string& molName,
315 const std::string& bendName, SelectionSet& bs) {
316 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
317 std::vector<TreeNodePtr>::iterator i;
318 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
319 std::vector<TreeNodePtr> bendNodes = getMatchedChildren(*i, bendName);
320 std::vector<TreeNodePtr>::iterator j;
321 for (j = bendNodes.begin(); j != bendNodes.end(); ++j) {
322 std::vector<TreeNodePtr> bendAtomNodes = getAllChildren(*j);
323 std::vector<TreeNodePtr>::iterator k;
324 for (k = bendAtomNodes.begin(); k != bendAtomNodes.end(); ++k) {
325 bs |= (*k)->bs;
326 }
327 }
328 }
329 }
330 void NameFinder::matchTorsion(const std::string& molName,
331 const std::string& torsionName,
332 SelectionSet& bs) {
333 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
334 std::vector<TreeNodePtr>::iterator i;
335 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
336 std::vector<TreeNodePtr> torsionNodes =
337 getMatchedChildren(*i, torsionName);
338 std::vector<TreeNodePtr>::iterator j;
339 for (j = torsionNodes.begin(); j != torsionNodes.end(); ++j) {
340 std::vector<TreeNodePtr> torsionAtomNodes = getAllChildren(*j);
341 std::vector<TreeNodePtr>::iterator k;
342 for (k = torsionAtomNodes.begin(); k != torsionAtomNodes.end(); ++k) {
343 bs |= (*k)->bs;
344 }
345 }
346 }
347 }
348 void NameFinder::matchInversion(const std::string& molName,
349 const std::string& inversionName,
350 SelectionSet& bs) {
351 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
352 std::vector<TreeNodePtr>::iterator i;
353 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
354 std::vector<TreeNodePtr> inversionNodes =
355 getMatchedChildren(*i, inversionName);
356 std::vector<TreeNodePtr>::iterator j;
357 for (j = inversionNodes.begin(); j != inversionNodes.end(); ++j) {
358 std::vector<TreeNodePtr> inversionAtomNodes = getAllChildren(*j);
359 std::vector<TreeNodePtr>::iterator k;
360 for (k = inversionAtomNodes.begin(); k != inversionAtomNodes.end();
361 ++k) {
362 bs |= (*k)->bs;
363 }
364 }
365 }
366 }
367
368 void NameFinder::matchRigidAtoms(const std::string& molName,
369 const std::string& rbName,
370 const std::string& rbAtomName,
371 SelectionSet& bs) {
372 std::vector<TreeNodePtr> molNodes = getMatchedChildren(root_, molName);
373 std::vector<TreeNodePtr>::iterator i;
374 for (i = molNodes.begin(); i != molNodes.end(); ++i) {
375 std::vector<TreeNodePtr> rbNodes = getMatchedChildren(*i, rbName);
376 std::vector<TreeNodePtr>::iterator j;
377 for (j = rbNodes.begin(); j != rbNodes.end(); ++j) {
378 std::vector<TreeNodePtr> rbAtomNodes =
379 getMatchedChildren(*j, rbAtomName);
380 std::vector<TreeNodePtr>::iterator k;
381 for (k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k) {
382 bs |= (*k)->bs;
383 }
384 }
385 }
386 }
387
388 std::vector<TreeNodePtr> NameFinder::getAllChildren(TreeNodePtr node) {
389 std::vector<TreeNodePtr> childNodes;
390 std::map<std::string, TreeNodePtr>::iterator i;
391 for (i = node->children.begin(); i != node->children.end(); ++i) {
392 childNodes.push_back(i->second);
393 }
394 return childNodes;
395 }
396
397 std::vector<TreeNodePtr> NameFinder::getMatchedChildren(
398 TreeNodePtr node, const std::string& name) {
399 std::vector<TreeNodePtr> matchedNodes;
400 std::map<std::string, TreeNodePtr>::iterator i;
401 for (i = node->children.begin(); i != node->children.end(); ++i) {
402 if (isMatched(i->first, name)) { matchedNodes.push_back(i->second); }
403 }
404
405 return matchedNodes;
406 }
407
408 bool NameFinder::isMatched(const std::string& str,
409 const std::string& wildcard) {
410 return Wildcard::wildcardfit(wildcard.c_str(), str.c_str()) > 0 ? true :
411 false;
412 }
413
414 void NameFinder::matchInternalIndex(const std::string& name,
415 int internalIndex, SelectionSet& bs) {
416 SimInfo::MoleculeIterator mi;
417 Molecule* mol;
418
419 for (mol = info_->beginMolecule(mi); mol != NULL;
420 mol = info_->nextMolecule(mi)) {
421 if (isMatched(mol->getType(), name)) {
422 int natoms = mol->getNAtoms();
423 int nrigidbodies = mol->getNRigidBodies();
424 if (internalIndex >= natoms + nrigidbodies) {
425 continue;
426 } else if (internalIndex < natoms) {
427 bs.bitsets_[STUNTDOUBLE].setBitOn(
428 mol->getAtomAt(internalIndex)->getGlobalIndex());
429 continue;
430 } else if (internalIndex < natoms + nrigidbodies) {
431 bs.bitsets_[STUNTDOUBLE].setBitOn(
432 mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex());
433 }
434 }
435 }
436 }
437
438 bool NameFinder::isInteger(const std::string& str) {
439 for (unsigned int i = 0; i < str.size(); ++i) {
440 if (!std::isdigit(str[i])) { return false; }
441 }
442 return true;
443 }
444} // namespace OpenMD
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.
@ INVERSION
Inversions.
@ STUNTDOUBLE
StuntDoubles (Atoms & RigidBodies).
@ TORSION
Torsions.
@ BEND
Bends.
@ BOND
Bonds.
@ MOLECULE
Molecules.