1 |
/* |
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 |
* 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 |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
40 |
*/ |
41 |
#include "selection/NameFinder.hpp" |
42 |
#include "utils/wildcards.hpp" |
43 |
#include "utils/StringTokenizer.hpp" |
44 |
#include "primitives/Molecule.hpp" |
45 |
namespace oopse { |
46 |
|
47 |
TreeNode::~TreeNode(){ |
48 |
std::map<std::string, TreeNode*>::iterator i; |
49 |
for ( i = children.begin(); i != children.end(); ++i) { |
50 |
i->second->~TreeNode(); |
51 |
} |
52 |
children.clear(); |
53 |
} |
54 |
|
55 |
|
56 |
NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ |
57 |
nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(); |
58 |
loadNames(); |
59 |
} |
60 |
|
61 |
|
62 |
NameFinder::~NameFinder(){ |
63 |
delete root_; |
64 |
} |
65 |
|
66 |
void NameFinder::loadNames() { |
67 |
|
68 |
std::map<std::string, TreeNode*>::iterator foundIter; |
69 |
SimInfo::MoleculeIterator mi; |
70 |
Molecule* mol; |
71 |
Molecule::AtomIterator ai; |
72 |
Atom* atom; |
73 |
Molecule::RigidBodyIterator rbIter; |
74 |
RigidBody* rb; |
75 |
|
76 |
root_ = new TreeNode; |
77 |
root_->bs.resize(nStuntDouble_); |
78 |
root_->bs.setAll(); // |
79 |
|
80 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
81 |
|
82 |
std::string molName = mol->getMoleculeName(); |
83 |
TreeNode* currentMolNode = createNode(root_, molName); |
84 |
|
85 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
86 |
std::string atomName = atom->getType(); |
87 |
TreeNode* currentAtomNode = createNode(currentMolNode, atomName); |
88 |
|
89 |
currentMolNode->bs.setBitOn(atom->getGlobalIndex()); |
90 |
currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
91 |
} |
92 |
|
93 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
94 |
std::string rbName = rb->getType(); |
95 |
TreeNode* currentRbNode = createNode(currentMolNode, rbName); |
96 |
|
97 |
currentMolNode->bs.setBitOn(rb->getGlobalIndex()); |
98 |
currentRbNode->bs.setBitOn(rb->getGlobalIndex()); |
99 |
|
100 |
//create nodes for atoms belong to this rigidbody |
101 |
for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
102 |
std::string rbAtomName = atom->getType(); |
103 |
TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; |
104 |
|
105 |
currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); |
106 |
} |
107 |
|
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
} |
113 |
|
114 |
TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { |
115 |
TreeNode* node; |
116 |
std::map<std::string, TreeNode*>::iterator foundIter; |
117 |
foundIter = parent->children.find(name); |
118 |
if ( foundIter == parent->children.end()) { |
119 |
node = new TreeNode; |
120 |
node->name = name; |
121 |
node->bs.resize(nStuntDouble_); |
122 |
parent->children.insert(std::make_pair(name, node)); |
123 |
}else { |
124 |
node = foundIter->second; |
125 |
} |
126 |
return node; |
127 |
} |
128 |
|
129 |
BitSet NameFinder::match(const std::string& name){ |
130 |
BitSet bs(nStuntDouble_); |
131 |
|
132 |
StringTokenizer tokenizer(name, "."); |
133 |
|
134 |
std::vector<std::string> names; |
135 |
while(tokenizer.hasMoreTokens()) { |
136 |
names.push_back(tokenizer.nextToken()); |
137 |
} |
138 |
|
139 |
int size = names.size(); |
140 |
switch(size) { |
141 |
case 1 : |
142 |
//could be molecule name, atom name and rigidbody name |
143 |
if (names[0] == "*"){ |
144 |
//if all molecules are selected, we don't need to do the matching, just set all of the bits |
145 |
bs.setAll(); |
146 |
} else{ |
147 |
matchMolecule(names[0], bs); |
148 |
matchStuntDouble("*", names[0], bs); |
149 |
} |
150 |
|
151 |
break; |
152 |
case 2: |
153 |
//could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) |
154 |
matchRigidAtoms("*", names[0], names[1], bs); |
155 |
matchStuntDouble(names[0], names[1], bs); |
156 |
|
157 |
break; |
158 |
case 3: |
159 |
//must be molecule.rigidbody.* |
160 |
matchRigidAtoms(names[0], names[1], names[2], bs); |
161 |
break; |
162 |
default: |
163 |
std::cerr << "invalid name: " << name << std::endl; |
164 |
break; |
165 |
} |
166 |
|
167 |
return bs; |
168 |
} |
169 |
|
170 |
void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { |
171 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
172 |
std::vector<TreeNode*>::iterator i; |
173 |
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
174 |
bs |= (*i)->bs; |
175 |
} |
176 |
} |
177 |
|
178 |
void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& bs){ |
179 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
180 |
std::vector<TreeNode*>::iterator i; |
181 |
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
182 |
std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName); |
183 |
std::vector<TreeNode*>::iterator j; |
184 |
for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { |
185 |
bs |= (*j)->bs; |
186 |
} |
187 |
} |
188 |
|
189 |
} |
190 |
|
191 |
void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){ |
192 |
std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); |
193 |
std::vector<TreeNode*>::iterator i; |
194 |
for( i = molNodes.begin(); i != molNodes.end(); ++i ) { |
195 |
std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); |
196 |
std::vector<TreeNode*>::iterator j; |
197 |
for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { |
198 |
std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); |
199 |
std::vector<TreeNode*>::iterator k; |
200 |
for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ |
201 |
bs |= (*k)->bs; |
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
} |
207 |
|
208 |
|
209 |
std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { |
210 |
std::vector<TreeNode*> matchedNodes; |
211 |
std::map<std::string, TreeNode*>::iterator i; |
212 |
for (i = node->children.begin(); i != node->children.end(); ++i) { |
213 |
if (isMatched( i->first, name)) { |
214 |
matchedNodes.push_back(i->second); |
215 |
} |
216 |
} |
217 |
|
218 |
return matchedNodes; |
219 |
} |
220 |
|
221 |
bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { |
222 |
return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); |
223 |
} |
224 |
|
225 |
} |