ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/NameFinder.cpp
Revision: 1874
Committed: Wed May 15 15:09:35 2013 UTC (11 years, 11 months ago) by gezelter
File size: 9011 byte(s)
Log Message:
Fixed a bunch of cppcheck warnings.

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, 234107 (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 NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){
58 nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies();
59 loadNames();
60 }
61
62 NameFinder::~NameFinder(){
63 delete root_;
64 }
65
66 void NameFinder::loadNames() {
67
68 SimInfo::MoleculeIterator mi;
69 Molecule* mol;
70 Molecule::AtomIterator ai;
71 Atom* atom;
72 Molecule::RigidBodyIterator rbIter;
73 RigidBody* rb;
74
75 root_ = new TreeNode;
76 root_->bs.resize(nStuntDouble_);
77 root_->bs.setAll(); //
78
79 for (mol = info_->beginMolecule(mi); mol != NULL;
80 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;
94 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());
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);;
105
106 currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex());
107 }
108 }
109 }
110 }
111
112 TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) {
113 TreeNode* node;
114 std::map<std::string, TreeNode*>::iterator foundIter;
115 foundIter = parent->children.find(name);
116 if ( foundIter == parent->children.end()) {
117 node = new TreeNode;
118 node->name = name;
119 node->bs.resize(nStuntDouble_);
120 parent->children.insert(std::make_pair(name, node));
121 }else {
122 node = foundIter->second;
123 }
124 return node;
125 }
126
127 OpenMDBitSet NameFinder::match(const std::string& name){
128 OpenMDBitSet bs(nStuntDouble_);
129
130 StringTokenizer tokenizer(name, ".");
131
132 std::vector<std::string> names;
133 while(tokenizer.hasMoreTokens()) {
134 names.push_back(tokenizer.nextToken());
135 }
136
137 int size = names.size();
138 switch(size) {
139 case 1 :
140 //could be molecule name, atom name and rigidbody name
141 matchMolecule(names[0], bs);
142 matchStuntDouble("*", names[0], bs);
143
144 break;
145 case 2:
146 //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody)
147
148 if (!isInteger(names[1])){
149 matchRigidAtoms("*", names[0], names[1], bs);
150 matchStuntDouble(names[0], names[1], bs);
151 } else {
152 int internalIndex = lexi_cast<int>(names[1]);
153 if (internalIndex < 0) {
154 std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl;
155 } else {
156 matchInternalIndex(names[0], internalIndex, bs);
157 }
158 }
159
160 break;
161 case 3:
162 //must be molecule.rigidbody.*
163 matchRigidAtoms(names[0], names[1], names[2], bs);
164 break;
165 default:
166 std::cerr << "invalid name: " << name << std::endl;
167 break;
168 }
169
170 return bs;
171 }
172
173 void NameFinder::matchMolecule(const std::string& molName, OpenMDBitSet& bs) {
174 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
175 std::vector<TreeNode*>::iterator i;
176 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
177 bs |= (*i)->bs;
178 }
179 }
180
181 void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, OpenMDBitSet& bs){
182 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
183 std::vector<TreeNode*>::iterator i;
184 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
185 std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName);
186 std::vector<TreeNode*>::iterator j;
187 for (j = sdNodes.begin(); j != sdNodes.end(); ++j) {
188 bs |= (*j)->bs;
189 }
190 }
191
192 }
193
194 void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, OpenMDBitSet& bs){
195 std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName);
196 std::vector<TreeNode*>::iterator i;
197 for( i = molNodes.begin(); i != molNodes.end(); ++i ) {
198 std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName);
199 std::vector<TreeNode*>::iterator j;
200 for (j = rbNodes.begin(); j != rbNodes.end(); ++j) {
201 std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName);
202 std::vector<TreeNode*>::iterator k;
203 for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){
204 bs |= (*k)->bs;
205 }
206 }
207 }
208
209 }
210
211
212 std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) {
213 std::vector<TreeNode*> matchedNodes;
214 std::map<std::string, TreeNode*>::iterator i;
215 for (i = node->children.begin(); i != node->children.end(); ++i) {
216 if (isMatched( i->first, name)) {
217 matchedNodes.push_back(i->second);
218 }
219 }
220
221 return matchedNodes;
222 }
223
224 bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) {
225 return Wildcard::wildcardfit(wildcard.c_str(), str.c_str()) > 0 ? true : false;
226 }
227
228
229 void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, OpenMDBitSet& bs){
230
231 SimInfo::MoleculeIterator mi;
232 Molecule* mol;
233
234 for (mol = info_->beginMolecule(mi); mol != NULL;
235 mol = info_->nextMolecule(mi)) {
236
237 if (isMatched(mol->getMoleculeName(), name) ) {
238 int natoms = mol->getNAtoms();
239 int nrigidbodies = mol->getNRigidBodies();
240 if (internalIndex >= natoms + nrigidbodies) {
241 continue;
242 } else if (internalIndex < natoms) {
243 bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex());
244 continue;
245 } else if ( internalIndex < natoms + nrigidbodies) {
246 bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex());
247 }
248 }
249 }
250 }
251
252 bool NameFinder::isInteger(const std::string &str) {
253 for(unsigned int i = 0; i < str.size(); ++i){
254 if (!std::isdigit(str[i])) {
255 return false;
256 }
257 }
258 return true;
259 }
260 }

Properties

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