| 1 | tim | 283 | /* | 
| 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 | tim | 284 | #include "utils/wildcards.hpp" | 
| 43 | tim | 283 | namespace oopse { | 
| 44 |  |  |  | 
| 45 | tim | 284 | TreeNode::~TreeNode(){ | 
| 46 |  |  | std::map<std::string, TreeNode*>::iterator i; | 
| 47 |  |  | for ( i = children.begin(); i != children.end(); ++i) { | 
| 48 |  |  | i->second->~TreeNode(); | 
| 49 |  |  | } | 
| 50 |  |  | children.clear(); | 
| 51 |  |  | } | 
| 52 |  |  |  | 
| 53 |  |  |  | 
| 54 |  |  | NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ | 
| 55 |  |  | nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies() | 
| 56 | tim | 283 | loadNames(); | 
| 57 |  |  | } | 
| 58 |  |  |  | 
| 59 |  |  |  | 
| 60 | tim | 284 | NameFinder::~NameFinder(){ | 
| 61 |  |  | delete root_; | 
| 62 |  |  | } | 
| 63 | tim | 283 |  | 
| 64 |  |  | void NameFinder::loadNames() { | 
| 65 |  |  |  | 
| 66 | tim | 284 | std::map<std::string, TreeNode*>::iterator foundIter; | 
| 67 | tim | 283 | SimInfo::MoleculeIterator mi; | 
| 68 |  |  | Molecule* mol; | 
| 69 |  |  | Molecule::AtomIterator ai; | 
| 70 |  |  | Atom* atom; | 
| 71 |  |  | Molecule::RigidBodyIterator rbIter; | 
| 72 |  |  | RigidBody* rb; | 
| 73 | tim | 284 |  | 
| 74 |  |  | root_ = new TreeNode; | 
| 75 |  |  | root_->bs.resize(nStuntDobule_); | 
| 76 |  |  | root_->bs.setAll(); // | 
| 77 |  |  |  | 
| 78 | tim | 283 | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 79 | tim | 284 | TreeNode* currentMolNode; | 
| 80 |  |  | std::string molName = mol->getMoleculeName(); | 
| 81 |  |  |  | 
| 82 |  |  | foundIter = root_->children.find(molName); | 
| 83 |  |  | if ( foundIter  == root_->children.end()) { | 
| 84 |  |  | currentMolNode = new TreeNode; | 
| 85 |  |  | currentMolNode->name = molName; | 
| 86 |  |  | currentMolNode->bs.resize(nStuntDouble_); | 
| 87 |  |  | }else { | 
| 88 |  |  | currentMolNode = i->second; | 
| 89 |  |  | } | 
| 90 | tim | 283 |  | 
| 91 |  |  | for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { | 
| 92 | tim | 284 | std::string atomName = atom->getType(); | 
| 93 |  |  | TreeNode* currentAtomNode; | 
| 94 |  |  | foundIter = currentMolNode->children.find(molName); | 
| 95 |  |  | if (foundIter == currentMolNode->children.end()) { | 
| 96 |  |  | currentAtomNode = new TreeNode; | 
| 97 |  |  | currentAtomNode->name = atomName; | 
| 98 |  |  | currentAtomNode->bs.resize(nStuntDouble_); | 
| 99 |  |  | } else { | 
| 100 |  |  | currentAtomNode = foundIter->second; | 
| 101 |  |  | } | 
| 102 |  |  | currentMolNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 103 |  |  | currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 104 | tim | 283 | } | 
| 105 | tim | 284 |  | 
| 106 |  |  | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 107 |  |  | std::string rbName = atom->getType(); | 
| 108 |  |  | TreeNode* currentRbNode; | 
| 109 |  |  | foundIter = currentMolNode->children.find(molName); | 
| 110 |  |  | if (foundIter == currentMolNode->children.end()) { | 
| 111 |  |  | currentRbNode = new TreeNode; | 
| 112 |  |  | currentRbNode->name = rbName; | 
| 113 |  |  | currentRbNode->bs.resize(nStuntDouble_); | 
| 114 |  |  | } else { | 
| 115 |  |  | currentRbNode = foundIter->second; | 
| 116 |  |  | } | 
| 117 |  |  |  | 
| 118 |  |  | currentMolNode->bs.setBitOn(rb->getGlobalIndex()); | 
| 119 |  |  | currentRbNode->bs.setBitOn(rb->getGlobalIndex()); | 
| 120 |  |  |  | 
| 121 |  |  | //create nodes for atoms belong to this rigidbody | 
| 122 |  |  | for(atom = rb->beginAtom(ai); rb != NULL; atom = rb->nextAtom(ai)) { | 
| 123 |  |  | std::string rbAtomName = atom->getType(); | 
| 124 |  |  | TreeNode* currentRbAtomNode; | 
| 125 |  |  | foundIter = currentRbNode->children.find(molName); | 
| 126 |  |  | if (foundIter == currentRbNode->children.end()) { | 
| 127 |  |  | currentRbAtomNode = new TreeNode; | 
| 128 |  |  | currentRbAtomNode->name = rbAtomName; | 
| 129 |  |  | currentRbAtomNode->bs.resize(nStuntDouble_); | 
| 130 |  |  | } else { | 
| 131 |  |  | currentRbAtomNode = foundIter->second; | 
| 132 |  |  | } | 
| 133 |  |  | currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 134 |  |  | } | 
| 135 |  |  |  | 
| 136 |  |  | } | 
| 137 | tim | 283 |  | 
| 138 |  |  | } | 
| 139 | tim | 284 |  | 
| 140 |  |  | std::map<std::string, TreeNode*>::iterator i; | 
| 141 |  |  | for( i = root_->children.begin(); i != ; ++i){ | 
| 142 |  |  | i->bs = | 
| 143 |  |  | } | 
| 144 | tim | 283 | } | 
| 145 |  |  |  | 
| 146 |  |  | bool NameFinder::match(const std::string& name, BitSet& bs){ | 
| 147 |  |  |  | 
| 148 |  |  | bool error = true; | 
| 149 |  |  | StringTokenizer tokenizer(name, "."); | 
| 150 |  |  |  | 
| 151 |  |  | std::vector<std::string> names; | 
| 152 |  |  | while(tokenizer.hasMoreTokens()) { | 
| 153 |  |  | names.push_back(tokenizer.nextToken()); | 
| 154 |  |  | } | 
| 155 |  |  |  | 
| 156 |  |  | int size = names.size(); | 
| 157 |  |  | switch(size) { | 
| 158 |  |  | case 1 : | 
| 159 | tim | 284 | //could be molecule name, atom name and rigidbody name | 
| 160 |  |  | if (names[0] == "*"){ | 
| 161 |  |  | //if all molecules are selected, we don't need to do the matching, just set all of the bits | 
| 162 |  |  | bs.setAll(); | 
| 163 |  |  | } else{ | 
| 164 |  |  | matchMolecule(name[0]); | 
| 165 |  |  | matchStuntDouble("*", names[0]); | 
| 166 |  |  | } | 
| 167 |  |  |  | 
| 168 | tim | 283 | break; | 
| 169 |  |  | case 2: | 
| 170 | tim | 284 | //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) | 
| 171 |  |  | matchRigidAtoms("*", names[0], names[1], bs); | 
| 172 |  |  | matchStuntDouble(names[0], names[1]); | 
| 173 |  |  |  | 
| 174 | tim | 283 | break; | 
| 175 |  |  | case 3: | 
| 176 |  |  | //must be molecule.rigidbody.* | 
| 177 | tim | 284 | matchRigidAtoms(names[0], names[1], names[2], bs) | 
| 178 | tim | 283 | break; | 
| 179 |  |  | default: | 
| 180 |  |  | break; | 
| 181 |  |  | } | 
| 182 |  |  |  | 
| 183 |  |  | return matched; | 
| 184 |  |  | } | 
| 185 |  |  |  | 
| 186 | tim | 284 | void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { | 
| 187 |  |  | std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); | 
| 188 |  |  | std::vector<TreeNode*>::iterator i; | 
| 189 |  |  | for( i = molNodes.begin(); i != molNodes.end(); ++i ) { | 
| 190 |  |  | bs |= i->bs; | 
| 191 |  |  | } | 
| 192 | tim | 283 | } | 
| 193 | tim | 284 |  | 
| 194 |  |  | void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& 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*> sdNodes = getMatchedChildren(*i, sdName); | 
| 199 |  |  | std::vector<TreeNode*>::iterator j; | 
| 200 |  |  | for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { | 
| 201 |  |  | bs |= j->bs; | 
| 202 |  |  | } | 
| 203 |  |  | } | 
| 204 |  |  |  | 
| 205 |  |  | } | 
| 206 |  |  |  | 
| 207 |  |  | void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){ | 
| 208 |  |  | std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); | 
| 209 |  |  | std::vector<TreeNode*>::iterator i; | 
| 210 |  |  | for( i = molNodes.begin(); i != molNodes.end(); ++i ) { | 
| 211 |  |  | std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); | 
| 212 |  |  | std::vector<TreeNode*>::iterator j; | 
| 213 |  |  | for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { | 
| 214 |  |  | std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); | 
| 215 |  |  | std::vector<TreeNode*>::iterator k; | 
| 216 |  |  | for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ | 
| 217 |  |  | bs |= k->bs; | 
| 218 |  |  | } | 
| 219 |  |  | } | 
| 220 |  |  | } | 
| 221 |  |  |  | 
| 222 |  |  | } | 
| 223 |  |  |  | 
| 224 |  |  |  | 
| 225 |  |  | std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { | 
| 226 |  |  | std::vector<TreeNode*> matchedNodes; | 
| 227 |  |  | std::map<std::string, TreeNode*>::iterator i; | 
| 228 |  |  | for (i = node->children.begin(); i != node->children.end(); ++i) { | 
| 229 |  |  | if (isMatched( i->first, name)) { | 
| 230 |  |  | matchedNodes.push_back(i->second); | 
| 231 |  |  | } | 
| 232 |  |  | } | 
| 233 |  |  |  | 
| 234 |  |  | return matchedNodes; | 
| 235 |  |  | } | 
| 236 |  |  |  | 
| 237 |  |  | bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { | 
| 238 |  |  | return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); | 
| 239 |  |  | } | 
| 240 |  |  |  | 
| 241 |  |  | } |