ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/NameFinder.cpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
File size: 9102 byte(s)
Log Message:
updated copyright notices

File Contents

# Content
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. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
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 *
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] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 */
42 #include "selection/NameFinder.hpp"
43 #include "utils/wildcards.hpp"
44 #include "utils/StringTokenizer.hpp"
45 #include "primitives/Molecule.hpp"
46 #include "utils/StringUtils.hpp"
47 namespace OpenMD {
48
49 TreeNode::~TreeNode(){
50 std::map<std::string, TreeNode*>::iterator i;
51 for ( i = children.begin(); i != children.end(); ++i) {
52 i->second->~TreeNode();
53 }
54 children.clear();
55 }
56
57
58 NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){
59 nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies();
60 loadNames();
61 }
62
63
64 NameFinder::~NameFinder(){
65 delete root_;
66 }
67
68 void NameFinder::loadNames() {
69
70 std::map<std::string, TreeNode*>::iterator foundIter;
71 SimInfo::MoleculeIterator mi;
72 Molecule* mol;
73 Molecule::AtomIterator ai;
74 Atom* atom;
75 Molecule::RigidBodyIterator rbIter;
76 RigidBody* rb;
77
78 root_ = new TreeNode;
79 root_->bs.resize(nStuntDouble_);
80 root_->bs.setAll(); //
81
82 for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
83
84 std::string molName = mol->getMoleculeName();
85 TreeNode* currentMolNode = createNode(root_, molName);
86
87 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
88 std::string atomName = atom->getType();
89 TreeNode* currentAtomNode = createNode(currentMolNode, atomName);
90
91 currentMolNode->bs.setBitOn(atom->getGlobalIndex());
92 currentAtomNode->bs.setBitOn(atom->getGlobalIndex());
93 }
94
95 for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
96 std::string rbName = rb->getType();
97 TreeNode* currentRbNode = createNode(currentMolNode, rbName);
98
99 currentMolNode->bs.setBitOn(rb->getGlobalIndex());
100 currentRbNode->bs.setBitOn(rb->getGlobalIndex());
101
102 //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
107 currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex());
108 }
109
110 }
111
112 }
113
114 }
115
116 TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) {
117 TreeNode* node;
118 std::map<std::string, TreeNode*>::iterator foundIter;
119 foundIter = parent->children.find(name);
120 if ( foundIter == parent->children.end()) {
121 node = new TreeNode;
122 node->name = name;
123 node->bs.resize(nStuntDouble_);
124 parent->children.insert(std::make_pair(name, node));
125 }else {
126 node = foundIter->second;
127 }
128 return node;
129 }
130
131 OpenMDBitSet NameFinder::match(const std::string& name){
132 OpenMDBitSet bs(nStuntDouble_);
133
134 StringTokenizer tokenizer(name, ".");
135
136 std::vector<std::string> names;
137 while(tokenizer.hasMoreTokens()) {
138 names.push_back(tokenizer.nextToken());
139 }
140
141 int size = names.size();
142 switch(size) {
143 case 1 :
144 //could be molecule name, atom name and rigidbody name
145 matchMolecule(names[0], bs);
146 matchStuntDouble("*", names[0], bs);
147
148 break;
149 case 2:
150 //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody)
151
152 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
164 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 }
173
174 return bs;
175 }
176
177 void NameFinder::matchMolecule(const std::string& molName, OpenMDBitSet& bs) {
178 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
179 std::vector<TreeNode*>::iterator i;
180 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
181 bs |= (*i)->bs;
182 }
183 }
184
185 void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, OpenMDBitSet& bs){
186 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
187 std::vector<TreeNode*>::iterator i;
188 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
189 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 }
195
196 }
197
198 void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, OpenMDBitSet& bs){
199 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
200 std::vector<TreeNode*>::iterator i;
201 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
202 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 }
212
213 }
214
215
216 std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) {
217 std::vector<TreeNode*> matchedNodes;
218 std::map<std::string, TreeNode*>::iterator i;
219 for (i = node->children.begin(); i != node->children.end(); ++i) {
220 if (isMatched( i->first, name)) {
221 matchedNodes.push_back(i->second);
222 }
223 }
224
225 return matchedNodes;
226 }
227
228 bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) {
229 return Wildcard::wildcardfit (wildcard.c_str(), str.c_str());
230 }
231
232
233 void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, OpenMDBitSet& bs){
234
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 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
254 }
255
256 }
257
258 bool NameFinder::isInteger(const std::string str) {
259 for(int i =0; i < str.size(); ++i){
260 if (!std::isdigit(str[i])) {
261 return false;
262 }
263 }
264
265 return true;
266 }
267
268 }

Properties

Name Value
svn:executable *
svn:keywords Author Id Revision Date