1 |
tim |
283 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
tim |
283 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
tim |
283 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
gezelter |
1665 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
tim |
283 |
*/ |
42 |
|
|
#include "selection/NameFinder.hpp" |
43 |
tim |
284 |
#include "utils/wildcards.hpp" |
44 |
tim |
287 |
#include "utils/StringTokenizer.hpp" |
45 |
|
|
#include "primitives/Molecule.hpp" |
46 |
tim |
303 |
#include "utils/StringUtils.hpp" |
47 |
gezelter |
1390 |
namespace OpenMD { |
48 |
tim |
283 |
|
49 |
gezelter |
507 |
TreeNode::~TreeNode(){ |
50 |
tim |
284 |
std::map<std::string, TreeNode*>::iterator i; |
51 |
|
|
for ( i = children.begin(); i != children.end(); ++i) { |
52 |
gezelter |
507 |
i->second->~TreeNode(); |
53 |
tim |
284 |
} |
54 |
|
|
children.clear(); |
55 |
gezelter |
507 |
} |
56 |
tim |
284 |
|
57 |
|
|
|
58 |
gezelter |
507 |
NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ |
59 |
tim |
287 |
nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(); |
60 |
tim |
283 |
loadNames(); |
61 |
gezelter |
507 |
} |
62 |
tim |
283 |
|
63 |
|
|
|
64 |
gezelter |
507 |
NameFinder::~NameFinder(){ |
65 |
tim |
284 |
delete root_; |
66 |
gezelter |
507 |
} |
67 |
tim |
283 |
|
68 |
gezelter |
507 |
void NameFinder::loadNames() { |
69 |
tim |
283 |
|
70 |
tim |
284 |
std::map<std::string, TreeNode*>::iterator foundIter; |
71 |
tim |
283 |
SimInfo::MoleculeIterator mi; |
72 |
|
|
Molecule* mol; |
73 |
|
|
Molecule::AtomIterator ai; |
74 |
|
|
Atom* atom; |
75 |
|
|
Molecule::RigidBodyIterator rbIter; |
76 |
|
|
RigidBody* rb; |
77 |
tim |
284 |
|
78 |
|
|
root_ = new TreeNode; |
79 |
tim |
287 |
root_->bs.resize(nStuntDouble_); |
80 |
tim |
284 |
root_->bs.setAll(); // |
81 |
|
|
|
82 |
tim |
283 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
83 |
tim |
288 |
|
84 |
gezelter |
507 |
std::string molName = mol->getMoleculeName(); |
85 |
|
|
TreeNode* currentMolNode = createNode(root_, molName); |
86 |
tim |
283 |
|
87 |
gezelter |
507 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
88 |
|
|
std::string atomName = atom->getType(); |
89 |
|
|
TreeNode* currentAtomNode = createNode(currentMolNode, atomName); |
90 |
tim |
288 |
|
91 |
gezelter |
507 |
currentMolNode->bs.setBitOn(atom->getGlobalIndex()); |
92 |
|
|
currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
93 |
|
|
} |
94 |
tim |
284 |
|
95 |
gezelter |
507 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
96 |
|
|
std::string rbName = rb->getType(); |
97 |
|
|
TreeNode* currentRbNode = createNode(currentMolNode, rbName); |
98 |
tim |
284 |
|
99 |
gezelter |
507 |
currentMolNode->bs.setBitOn(rb->getGlobalIndex()); |
100 |
|
|
currentRbNode->bs.setBitOn(rb->getGlobalIndex()); |
101 |
tim |
284 |
|
102 |
gezelter |
507 |
//create nodes for atoms belong to this rigidbody |
103 |
|
|
for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
104 |
|
|
std::string rbAtomName = atom->getType(); |
105 |
|
|
TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; |
106 |
tim |
288 |
|
107 |
gezelter |
507 |
currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
108 |
|
|
} |
109 |
tim |
284 |
|
110 |
gezelter |
507 |
} |
111 |
tim |
283 |
|
112 |
|
|
} |
113 |
tim |
284 |
|
114 |
gezelter |
507 |
} |
115 |
tim |
283 |
|
116 |
gezelter |
507 |
TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { |
117 |
tim |
288 |
TreeNode* node; |
118 |
|
|
std::map<std::string, TreeNode*>::iterator foundIter; |
119 |
|
|
foundIter = parent->children.find(name); |
120 |
|
|
if ( foundIter == parent->children.end()) { |
121 |
gezelter |
507 |
node = new TreeNode; |
122 |
|
|
node->name = name; |
123 |
|
|
node->bs.resize(nStuntDouble_); |
124 |
|
|
parent->children.insert(std::make_pair(name, node)); |
125 |
tim |
288 |
}else { |
126 |
gezelter |
507 |
node = foundIter->second; |
127 |
tim |
288 |
} |
128 |
|
|
return node; |
129 |
gezelter |
507 |
} |
130 |
tim |
283 |
|
131 |
gezelter |
1390 |
OpenMDBitSet NameFinder::match(const std::string& name){ |
132 |
|
|
OpenMDBitSet bs(nStuntDouble_); |
133 |
tim |
288 |
|
134 |
tim |
283 |
StringTokenizer tokenizer(name, "."); |
135 |
|
|
|
136 |
|
|
std::vector<std::string> names; |
137 |
|
|
while(tokenizer.hasMoreTokens()) { |
138 |
gezelter |
507 |
names.push_back(tokenizer.nextToken()); |
139 |
tim |
283 |
} |
140 |
|
|
|
141 |
|
|
int size = names.size(); |
142 |
|
|
switch(size) { |
143 |
gezelter |
507 |
case 1 : |
144 |
|
|
//could be molecule name, atom name and rigidbody name |
145 |
|
|
matchMolecule(names[0], bs); |
146 |
|
|
matchStuntDouble("*", names[0], bs); |
147 |
tim |
284 |
|
148 |
gezelter |
507 |
break; |
149 |
|
|
case 2: |
150 |
|
|
//could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) |
151 |
tim |
303 |
|
152 |
gezelter |
507 |
if (!isInteger(names[1])){ |
153 |
|
|
matchRigidAtoms("*", names[0], names[1], bs); |
154 |
|
|
matchStuntDouble(names[0], names[1], bs); |
155 |
|
|
} else { |
156 |
|
|
int internalIndex = lexi_cast<int>(names[1]); |
157 |
|
|
if (internalIndex < 0) { |
158 |
|
|
std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; |
159 |
|
|
} else { |
160 |
|
|
matchInternalIndex(names[0], internalIndex, bs); |
161 |
|
|
} |
162 |
|
|
} |
163 |
tim |
284 |
|
164 |
gezelter |
507 |
break; |
165 |
|
|
case 3: |
166 |
|
|
//must be molecule.rigidbody.* |
167 |
|
|
matchRigidAtoms(names[0], names[1], names[2], bs); |
168 |
|
|
break; |
169 |
|
|
default: |
170 |
|
|
std::cerr << "invalid name: " << name << std::endl; |
171 |
|
|
break; |
172 |
tim |
283 |
} |
173 |
|
|
|
174 |
tim |
288 |
return bs; |
175 |
gezelter |
507 |
} |
176 |
tim |
283 |
|
177 |
gezelter |
1390 |
void NameFinder::matchMolecule(const std::string& molName, OpenMDBitSet& bs) { |
178 |
tim |
284 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
179 |
|
|
std::vector<TreeNode*>::iterator i; |
180 |
|
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
181 |
gezelter |
507 |
bs |= (*i)->bs; |
182 |
tim |
284 |
} |
183 |
gezelter |
507 |
} |
184 |
tim |
284 |
|
185 |
gezelter |
1390 |
void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, OpenMDBitSet& bs){ |
186 |
tim |
284 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
187 |
|
|
std::vector<TreeNode*>::iterator i; |
188 |
|
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
189 |
gezelter |
507 |
std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName); |
190 |
|
|
std::vector<TreeNode*>::iterator j; |
191 |
|
|
for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { |
192 |
|
|
bs |= (*j)->bs; |
193 |
|
|
} |
194 |
tim |
284 |
} |
195 |
|
|
|
196 |
gezelter |
507 |
} |
197 |
tim |
284 |
|
198 |
gezelter |
1390 |
void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, OpenMDBitSet& bs){ |
199 |
tim |
284 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
200 |
|
|
std::vector<TreeNode*>::iterator i; |
201 |
|
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
202 |
gezelter |
507 |
std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); |
203 |
|
|
std::vector<TreeNode*>::iterator j; |
204 |
|
|
for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { |
205 |
|
|
std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); |
206 |
|
|
std::vector<TreeNode*>::iterator k; |
207 |
|
|
for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ |
208 |
|
|
bs |= (*k)->bs; |
209 |
|
|
} |
210 |
|
|
} |
211 |
tim |
284 |
} |
212 |
|
|
|
213 |
gezelter |
507 |
} |
214 |
tim |
284 |
|
215 |
|
|
|
216 |
gezelter |
507 |
std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { |
217 |
tim |
284 |
std::vector<TreeNode*> matchedNodes; |
218 |
|
|
std::map<std::string, TreeNode*>::iterator i; |
219 |
|
|
for (i = node->children.begin(); i != node->children.end(); ++i) { |
220 |
gezelter |
507 |
if (isMatched( i->first, name)) { |
221 |
|
|
matchedNodes.push_back(i->second); |
222 |
|
|
} |
223 |
tim |
284 |
} |
224 |
|
|
|
225 |
|
|
return matchedNodes; |
226 |
gezelter |
507 |
} |
227 |
tim |
284 |
|
228 |
gezelter |
507 |
bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { |
229 |
tim |
284 |
return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); |
230 |
gezelter |
507 |
} |
231 |
tim |
284 |
|
232 |
tim |
303 |
|
233 |
gezelter |
1390 |
void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, OpenMDBitSet& bs){ |
234 |
tim |
303 |
|
235 |
|
|
std::map<std::string, TreeNode*>::iterator foundIter; |
236 |
|
|
SimInfo::MoleculeIterator mi; |
237 |
|
|
Molecule* mol; |
238 |
|
|
|
239 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
240 |
|
|
|
241 |
gezelter |
507 |
if (isMatched(mol->getMoleculeName(), name) ) { |
242 |
|
|
int natoms = mol->getNAtoms(); |
243 |
|
|
int nrigidbodies = mol->getNRigidBodies(); |
244 |
|
|
if (internalIndex >= natoms + nrigidbodies) { |
245 |
|
|
continue; |
246 |
|
|
} else if (internalIndex < natoms) { |
247 |
|
|
bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); |
248 |
|
|
continue; |
249 |
|
|
} else if ( internalIndex < natoms + nrigidbodies) { |
250 |
|
|
bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); |
251 |
|
|
} |
252 |
|
|
} |
253 |
tim |
303 |
|
254 |
|
|
} |
255 |
|
|
|
256 |
gezelter |
507 |
} |
257 |
tim |
303 |
|
258 |
gezelter |
507 |
bool NameFinder::isInteger(const std::string str) { |
259 |
tim |
303 |
for(int i =0; i < str.size(); ++i){ |
260 |
gezelter |
507 |
if (!std::isdigit(str[i])) { |
261 |
|
|
return false; |
262 |
|
|
} |
263 |
tim |
303 |
} |
264 |
|
|
|
265 |
|
|
return true; |
266 |
gezelter |
507 |
} |
267 |
tim |
303 |
|
268 |
|
|
} |