| 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 | 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 | 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; | 
| 81 | mol = info_->nextMolecule(mi)) { | 
| 82 |  | 
| 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); | 
| 89 |  | 
| 90 | currentMolNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 91 | currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 92 | } | 
| 93 |  | 
| 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()); | 
| 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 | 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)); | 
| 122 | }else { | 
| 123 | node = foundIter->second; | 
| 124 | } | 
| 125 | return node; | 
| 126 | } | 
| 127 |  | 
| 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()); | 
| 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); | 
| 144 |  | 
| 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 | } | 
| 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; | 
| 169 | } | 
| 170 |  | 
| 171 | return bs; | 
| 172 | } | 
| 173 |  | 
| 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; | 
| 179 | } | 
| 180 | } | 
| 181 |  | 
| 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 | } | 
| 191 | } | 
| 192 |  | 
| 193 | } | 
| 194 |  | 
| 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 | } | 
| 208 | } | 
| 209 |  | 
| 210 | } | 
| 211 |  | 
| 212 |  | 
| 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 | } | 
| 220 | } | 
| 221 |  | 
| 222 | return matchedNodes; | 
| 223 | } | 
| 224 |  | 
| 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, 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; | 
| 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 | } | 
| 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 | } | 
| 262 | } |