ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/selection/NameFinder.cpp
Revision: 1987
Committed: Mon Feb 7 22:36:32 2005 UTC (19 years, 5 months ago) by tim
File size: 9771 byte(s)
Log Message:
support select internal index selection, for example, select DMPC.3

File Contents

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

Properties

Name Value
svn:executable *