ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/NameFinder.cpp
(Generate patch)

Comparing:
trunk/src/selection/NameFinder.cpp (file contents), Revision 452 by tim, Tue Apr 5 23:09:48 2005 UTC vs.
branches/development/src/selection/NameFinder.cpp (file contents), Revision 1803 by gezelter, Wed Oct 3 14:20:07 2012 UTC

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

Comparing:
trunk/src/selection/NameFinder.cpp (property svn:keywords), Revision 452 by tim, Tue Apr 5 23:09:48 2005 UTC vs.
branches/development/src/selection/NameFinder.cpp (property svn:keywords), Revision 1803 by gezelter, Wed Oct 3 14:20:07 2012 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines