6 |
|
* redistribute this software in source and binary code form, provided |
7 |
|
* that the following conditions are met: |
8 |
|
* |
9 |
< |
* 1. Acknowledgement of the program authors must be made in any |
10 |
< |
* publication of scientific results based in part on use of the |
11 |
< |
* program. An acceptable form of acknowledgement is citation of |
12 |
< |
* the article in which the program was described (Matthew |
13 |
< |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
< |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
< |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
< |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
< |
* |
18 |
< |
* 2. Redistributions of source code must retain the above copyright |
9 |
> |
* 1. Redistributions of source code must retain the above copyright |
10 |
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
* |
12 |
< |
* 3. Redistributions in binary form must reproduce the above copyright |
12 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
* documentation and/or other materials provided with the |
15 |
|
* distribution. |
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 |
+ |
* |
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 |
+ |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
|
*/ |
41 |
|
#include "selection/NameFinder.hpp" |
42 |
|
#include "utils/wildcards.hpp" |
43 |
|
#include "utils/StringTokenizer.hpp" |
44 |
|
#include "primitives/Molecule.hpp" |
45 |
|
#include "utils/StringUtils.hpp" |
46 |
< |
namespace oopse { |
46 |
> |
namespace OpenMD { |
47 |
|
|
48 |
< |
TreeNode::~TreeNode(){ |
48 |
> |
TreeNode::~TreeNode(){ |
49 |
|
std::map<std::string, TreeNode*>::iterator i; |
50 |
|
for ( i = children.begin(); i != children.end(); ++i) { |
51 |
< |
i->second->~TreeNode(); |
51 |
> |
i->second->~TreeNode(); |
52 |
|
} |
53 |
|
children.clear(); |
54 |
< |
} |
54 |
> |
} |
55 |
|
|
56 |
|
|
57 |
< |
NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ |
57 |
> |
NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ |
58 |
|
nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(); |
59 |
|
loadNames(); |
60 |
< |
} |
60 |
> |
} |
61 |
|
|
62 |
|
|
63 |
< |
NameFinder::~NameFinder(){ |
63 |
> |
NameFinder::~NameFinder(){ |
64 |
|
delete root_; |
65 |
< |
} |
65 |
> |
} |
66 |
|
|
67 |
< |
void NameFinder::loadNames() { |
67 |
> |
void NameFinder::loadNames() { |
68 |
|
|
69 |
|
std::map<std::string, TreeNode*>::iterator foundIter; |
70 |
|
SimInfo::MoleculeIterator mi; |
80 |
|
|
81 |
|
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
82 |
|
|
83 |
< |
std::string molName = mol->getMoleculeName(); |
84 |
< |
TreeNode* currentMolNode = createNode(root_, molName); |
83 |
> |
std::string molName = mol->getMoleculeName(); |
84 |
> |
TreeNode* currentMolNode = createNode(root_, molName); |
85 |
|
|
86 |
< |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
87 |
< |
std::string atomName = atom->getType(); |
88 |
< |
TreeNode* currentAtomNode = createNode(currentMolNode, atomName); |
86 |
> |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
87 |
> |
std::string atomName = atom->getType(); |
88 |
> |
TreeNode* currentAtomNode = createNode(currentMolNode, atomName); |
89 |
|
|
90 |
< |
currentMolNode->bs.setBitOn(atom->getGlobalIndex()); |
91 |
< |
currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
92 |
< |
} |
90 |
> |
currentMolNode->bs.setBitOn(atom->getGlobalIndex()); |
91 |
> |
currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
92 |
> |
} |
93 |
|
|
94 |
< |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
95 |
< |
std::string rbName = rb->getType(); |
96 |
< |
TreeNode* currentRbNode = createNode(currentMolNode, rbName); |
94 |
> |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
95 |
> |
std::string rbName = rb->getType(); |
96 |
> |
TreeNode* currentRbNode = createNode(currentMolNode, rbName); |
97 |
|
|
98 |
< |
currentMolNode->bs.setBitOn(rb->getGlobalIndex()); |
99 |
< |
currentRbNode->bs.setBitOn(rb->getGlobalIndex()); |
98 |
> |
currentMolNode->bs.setBitOn(rb->getGlobalIndex()); |
99 |
> |
currentRbNode->bs.setBitOn(rb->getGlobalIndex()); |
100 |
|
|
101 |
< |
//create nodes for atoms belong to this rigidbody |
102 |
< |
for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
103 |
< |
std::string rbAtomName = atom->getType(); |
104 |
< |
TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; |
101 |
> |
//create nodes for atoms belong to this rigidbody |
102 |
> |
for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
103 |
> |
std::string rbAtomName = atom->getType(); |
104 |
> |
TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; |
105 |
|
|
106 |
< |
currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
107 |
< |
} |
106 |
> |
currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
107 |
> |
} |
108 |
|
|
109 |
< |
} |
109 |
> |
} |
110 |
|
|
111 |
|
} |
112 |
|
|
113 |
< |
} |
113 |
> |
} |
114 |
|
|
115 |
< |
TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { |
115 |
> |
TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { |
116 |
|
TreeNode* node; |
117 |
|
std::map<std::string, TreeNode*>::iterator foundIter; |
118 |
|
foundIter = parent->children.find(name); |
119 |
|
if ( foundIter == parent->children.end()) { |
120 |
< |
node = new TreeNode; |
121 |
< |
node->name = name; |
122 |
< |
node->bs.resize(nStuntDouble_); |
123 |
< |
parent->children.insert(std::make_pair(name, node)); |
120 |
> |
node = new TreeNode; |
121 |
> |
node->name = name; |
122 |
> |
node->bs.resize(nStuntDouble_); |
123 |
> |
parent->children.insert(std::make_pair(name, node)); |
124 |
|
}else { |
125 |
< |
node = foundIter->second; |
125 |
> |
node = foundIter->second; |
126 |
|
} |
127 |
|
return node; |
128 |
< |
} |
128 |
> |
} |
129 |
|
|
130 |
< |
BitSet NameFinder::match(const std::string& name){ |
131 |
< |
BitSet bs(nStuntDouble_); |
130 |
> |
OpenMDBitSet NameFinder::match(const std::string& name){ |
131 |
> |
OpenMDBitSet bs(nStuntDouble_); |
132 |
|
|
133 |
|
StringTokenizer tokenizer(name, "."); |
134 |
|
|
135 |
|
std::vector<std::string> names; |
136 |
|
while(tokenizer.hasMoreTokens()) { |
137 |
< |
names.push_back(tokenizer.nextToken()); |
137 |
> |
names.push_back(tokenizer.nextToken()); |
138 |
|
} |
139 |
|
|
140 |
|
int size = names.size(); |
141 |
|
switch(size) { |
142 |
< |
case 1 : |
143 |
< |
//could be molecule name, atom name and rigidbody name |
144 |
< |
if (names[0] == "*"){ |
145 |
< |
//if all molecules are selected, we don't need to do the matching, just set all of the bits |
146 |
< |
bs.setAll(); |
147 |
< |
} else{ |
148 |
< |
matchMolecule(names[0], bs); |
149 |
< |
matchStuntDouble("*", names[0], bs); |
150 |
< |
} |
142 |
> |
case 1 : |
143 |
> |
//could be molecule name, atom name and rigidbody name |
144 |
> |
matchMolecule(names[0], bs); |
145 |
> |
matchStuntDouble("*", names[0], bs); |
146 |
|
|
147 |
< |
break; |
148 |
< |
case 2: |
149 |
< |
//could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) |
147 |
> |
break; |
148 |
> |
case 2: |
149 |
> |
//could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) |
150 |
|
|
151 |
< |
if (!isInteger(names[1])){ |
152 |
< |
matchRigidAtoms("*", names[0], names[1], bs); |
153 |
< |
matchStuntDouble(names[0], names[1], bs); |
154 |
< |
} else { |
155 |
< |
int internalIndex = lexi_cast<int>(names[1]); |
156 |
< |
if (internalIndex < 0) { |
157 |
< |
std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; |
158 |
< |
} else { |
159 |
< |
matchInternalIndex(names[0], internalIndex, bs); |
160 |
< |
} |
161 |
< |
} |
151 |
> |
if (!isInteger(names[1])){ |
152 |
> |
matchRigidAtoms("*", names[0], names[1], bs); |
153 |
> |
matchStuntDouble(names[0], names[1], bs); |
154 |
> |
} else { |
155 |
> |
int internalIndex = lexi_cast<int>(names[1]); |
156 |
> |
if (internalIndex < 0) { |
157 |
> |
std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; |
158 |
> |
} else { |
159 |
> |
matchInternalIndex(names[0], internalIndex, bs); |
160 |
> |
} |
161 |
> |
} |
162 |
|
|
163 |
< |
break; |
164 |
< |
case 3: |
165 |
< |
//must be molecule.rigidbody.* |
166 |
< |
matchRigidAtoms(names[0], names[1], names[2], bs); |
167 |
< |
break; |
168 |
< |
default: |
169 |
< |
std::cerr << "invalid name: " << name << std::endl; |
170 |
< |
break; |
163 |
> |
break; |
164 |
> |
case 3: |
165 |
> |
//must be molecule.rigidbody.* |
166 |
> |
matchRigidAtoms(names[0], names[1], names[2], bs); |
167 |
> |
break; |
168 |
> |
default: |
169 |
> |
std::cerr << "invalid name: " << name << std::endl; |
170 |
> |
break; |
171 |
|
} |
172 |
|
|
173 |
|
return bs; |
174 |
< |
} |
174 |
> |
} |
175 |
|
|
176 |
< |
void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { |
176 |
> |
void NameFinder::matchMolecule(const std::string& molName, OpenMDBitSet& bs) { |
177 |
|
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
178 |
|
std::vector<TreeNode*>::iterator i; |
179 |
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
180 |
< |
bs |= (*i)->bs; |
180 |
> |
bs |= (*i)->bs; |
181 |
|
} |
182 |
< |
} |
182 |
> |
} |
183 |
|
|
184 |
< |
void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& bs){ |
184 |
> |
void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, OpenMDBitSet& bs){ |
185 |
|
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
186 |
|
std::vector<TreeNode*>::iterator i; |
187 |
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
188 |
< |
std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName); |
189 |
< |
std::vector<TreeNode*>::iterator j; |
190 |
< |
for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { |
191 |
< |
bs |= (*j)->bs; |
192 |
< |
} |
188 |
> |
std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName); |
189 |
> |
std::vector<TreeNode*>::iterator j; |
190 |
> |
for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { |
191 |
> |
bs |= (*j)->bs; |
192 |
> |
} |
193 |
|
} |
194 |
|
|
195 |
< |
} |
195 |
> |
} |
196 |
|
|
197 |
< |
void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){ |
197 |
> |
void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, OpenMDBitSet& bs){ |
198 |
|
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
199 |
|
std::vector<TreeNode*>::iterator i; |
200 |
|
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
201 |
< |
std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); |
202 |
< |
std::vector<TreeNode*>::iterator j; |
203 |
< |
for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { |
204 |
< |
std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); |
205 |
< |
std::vector<TreeNode*>::iterator k; |
206 |
< |
for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ |
207 |
< |
bs |= (*k)->bs; |
208 |
< |
} |
209 |
< |
} |
201 |
> |
std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); |
202 |
> |
std::vector<TreeNode*>::iterator j; |
203 |
> |
for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { |
204 |
> |
std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); |
205 |
> |
std::vector<TreeNode*>::iterator k; |
206 |
> |
for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ |
207 |
> |
bs |= (*k)->bs; |
208 |
> |
} |
209 |
> |
} |
210 |
|
} |
211 |
|
|
212 |
< |
} |
212 |
> |
} |
213 |
|
|
214 |
|
|
215 |
< |
std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { |
215 |
> |
std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { |
216 |
|
std::vector<TreeNode*> matchedNodes; |
217 |
|
std::map<std::string, TreeNode*>::iterator i; |
218 |
|
for (i = node->children.begin(); i != node->children.end(); ++i) { |
219 |
< |
if (isMatched( i->first, name)) { |
220 |
< |
matchedNodes.push_back(i->second); |
221 |
< |
} |
219 |
> |
if (isMatched( i->first, name)) { |
220 |
> |
matchedNodes.push_back(i->second); |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
return matchedNodes; |
225 |
< |
} |
225 |
> |
} |
226 |
|
|
227 |
< |
bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { |
227 |
> |
bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { |
228 |
|
return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); |
229 |
< |
} |
229 |
> |
} |
230 |
|
|
231 |
|
|
232 |
< |
void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, BitSet& bs){ |
232 |
> |
void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, OpenMDBitSet& bs){ |
233 |
|
|
234 |
|
std::map<std::string, TreeNode*>::iterator foundIter; |
235 |
|
SimInfo::MoleculeIterator mi; |
237 |
|
|
238 |
|
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
239 |
|
|
240 |
< |
if (isMatched(mol->getMoleculeName(), name) ) { |
241 |
< |
int natoms = mol->getNAtoms(); |
242 |
< |
int nrigidbodies = mol->getNRigidBodies(); |
243 |
< |
if (internalIndex >= natoms + nrigidbodies) { |
244 |
< |
continue; |
245 |
< |
} else if (internalIndex < natoms) { |
246 |
< |
bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); |
247 |
< |
continue; |
248 |
< |
} else if ( internalIndex < natoms + nrigidbodies) { |
249 |
< |
bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); |
250 |
< |
} |
251 |
< |
} |
240 |
> |
if (isMatched(mol->getMoleculeName(), name) ) { |
241 |
> |
int natoms = mol->getNAtoms(); |
242 |
> |
int nrigidbodies = mol->getNRigidBodies(); |
243 |
> |
if (internalIndex >= natoms + nrigidbodies) { |
244 |
> |
continue; |
245 |
> |
} else if (internalIndex < natoms) { |
246 |
> |
bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); |
247 |
> |
continue; |
248 |
> |
} else if ( internalIndex < natoms + nrigidbodies) { |
249 |
> |
bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); |
250 |
> |
} |
251 |
> |
} |
252 |
|
|
253 |
|
} |
254 |
|
|
255 |
< |
} |
255 |
> |
} |
256 |
|
|
257 |
< |
bool NameFinder::isInteger(const std::string str) { |
257 |
> |
bool NameFinder::isInteger(const std::string str) { |
258 |
|
for(int i =0; i < str.size(); ++i){ |
259 |
< |
if (!std::isdigit(str[i])) { |
260 |
< |
return false; |
261 |
< |
} |
259 |
> |
if (!std::isdigit(str[i])) { |
260 |
> |
return false; |
261 |
> |
} |
262 |
|
} |
263 |
|
|
264 |
|
return true; |
265 |
< |
} |
265 |
> |
} |
266 |
|
|
267 |
|
} |