ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/selection/SelectionEvaluator.cpp
Revision: 1972
Committed: Fri Feb 4 22:39:26 2005 UTC (19 years, 4 months ago) by tim
File size: 11674 byte(s)
Log Message:
half of the selection utility is working need to debug within keyword and atomproperty keyword

File Contents

# User Rev Content
1 tim 1961 /*
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    
42 tim 1967 #include <stack>
43 tim 1961 #include "selection/SelectionEvaluator.hpp"
44 tim 1967 #include "primitives/Atom.hpp"
45     #include "primitives/DirectionalAtom.hpp"
46     #include "primitives/RigidBody.hpp"
47     #include "primitives/Molecule.hpp"
48    
49 tim 1961 namespace oopse {
50    
51    
52 tim 1972 SelectionEvaluator::SelectionEvaluator(SimInfo* si) : info(si), finder(info), isLoaded_(false){
53    
54 tim 1967 }
55    
56 tim 1962 bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) {
57 tim 1972 clearDefinitionsAndLoadPredefined();
58 tim 1962 this->filename = filename;
59     this->script = script;
60     if (! compiler.compile(filename, script)) {
61     error = true;
62     errorMessage = compiler.getErrorMessage();
63 tim 1972 std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl;
64 tim 1962 return false;
65     }
66    
67     pc = 0;
68     aatoken = compiler.getAatokenCompiled();
69     linenumbers = compiler.getLineNumbers();
70     lineIndices = compiler.getLineIndices();
71 tim 1972
72     std::vector<std::vector<Token> >::const_iterator i;
73    
74     isDynamic_ = false;
75     for (i = aatoken.begin(); i != aatoken.end(); ++i) {
76     if (containDynamicToken(*i)) {
77     isDynamic_ = true;
78     break;
79     }
80     }
81    
82     isLoaded_ = true;
83 tim 1962 return true;
84 tim 1961 }
85 tim 1962
86     void SelectionEvaluator::clearState() {
87 tim 1972 //for (int i = scriptLevelMax; --i >= 0; )
88     // stack[i].clear();
89     //scriptLevel = 0;
90 tim 1962 error = false;
91 tim 1965 errorMessage = "";
92 tim 1962 }
93    
94     bool SelectionEvaluator::loadScriptString(const std::string& script) {
95     clearState();
96 tim 1965 return loadScript("", script);
97 tim 1962 }
98    
99     bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
100     clearState();
101     return loadScriptFileInternal(filename);
102     }
103    
104 tim 1967 bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) {
105 tim 1972 ifstream ifs(filename.c_str());
106     if (!ifs.is_open()) {
107     return false;
108     }
109    
110     const int bufferSize = 65535;
111     char buffer[bufferSize];
112     std::string script;
113     while(ifs.getline(buffer, bufferSize)) {
114     script += buffer;
115     }
116     return loadScript(filename, script);
117 tim 1966 }
118    
119 tim 1972 void SelectionEvaluator::instructionDispatchLoop(BitSet& bs){
120    
121 tim 1965 while ( pc < aatoken.size()) {
122 tim 1962 statement = aatoken[pc++];
123 tim 1965 statementLength = statement.size();
124 tim 1962 Token token = statement[0];
125     switch (token.tok) {
126 tim 1965 case Token::define:
127 tim 1962 define();
128     break;
129 tim 1965 case Token::select:
130 tim 1972 select(bs);
131 tim 1962 break;
132     default:
133     unrecognizedCommand(token);
134     return;
135     }
136     }
137 tim 1972
138 tim 1962 }
139    
140 tim 1967 BitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) {
141 tim 1962 BitSet bs;
142 tim 1967 std::stack<BitSet> stack;
143    
144 tim 1972 for (int pc = pcStart; pc < code.size(); ++pc) {
145 tim 1962 Token instruction = code[pc];
146 tim 1966
147 tim 1962 switch (instruction.tok) {
148 tim 1965 case Token::expressionBegin:
149 tim 1962 break;
150 tim 1965 case Token::expressionEnd:
151 tim 1966 break;
152 tim 1965 case Token::all:
153 tim 1967 bs = BitSet(nStuntDouble);
154     bs.setAll();
155     stack.push(bs);
156 tim 1962 break;
157 tim 1965 case Token::none:
158 tim 1967 bs = BitSet(nStuntDouble);
159     stack.push(bs);
160 tim 1962 break;
161 tim 1965 case Token::opOr:
162 tim 1967 bs = stack.top();
163     stack.pop();
164     stack.top() |= bs;
165 tim 1962 break;
166 tim 1965 case Token::opAnd:
167 tim 1967 bs = stack.top();
168     stack.pop();
169     stack.top() &= bs;
170 tim 1962 break;
171 tim 1965 case Token::opNot:
172 tim 1967 stack.top().flip();
173 tim 1962 break;
174 tim 1965 case Token::within:
175 tim 1967
176     withinInstruction(instruction, stack.top());
177 tim 1962 break;
178 tim 1967 //case Token::selected:
179     // stack.push(getSelectionSet());
180     // break;
181 tim 1965 case Token::name:
182 tim 1967 stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value)));
183 tim 1962 break;
184 tim 1965 break;
185     case Token::identifier:
186 tim 1967 stack.push(lookupValue(boost::any_cast<std::string>(instruction.value)));
187 tim 1965 break;
188     case Token::opLT:
189     case Token::opLE:
190     case Token::opGE:
191     case Token::opGT:
192     case Token::opEQ:
193     case Token::opNE:
194 tim 1967 stack.push(comparatorInstruction(instruction));
195 tim 1962 break;
196     default:
197     unrecognizedExpression();
198     }
199     }
200 tim 1967 if (stack.size() != 1)
201 tim 1962 evalError("atom expression compiler error - stack over/underflow");
202 tim 1967
203     return stack.top();
204 tim 1962 }
205    
206    
207    
208 tim 1967 BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) {
209 tim 1962 int comparator = instruction.tok;
210     int property = instruction.intValue;
211 tim 1967 float comparisonValue = boost::any_cast<float>(instruction.value);
212     float propertyValue;
213     BitSet bs(nStuntDouble);
214    
215     SimInfo::MoleculeIterator mi;
216     Molecule* mol;
217     Molecule::AtomIterator ai;
218     Atom* atom;
219     Molecule::RigidBodyIterator rbIter;
220     RigidBody* rb;
221    
222     for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
223 tim 1965
224 tim 1967 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
225     compareProperty(atom, bs, property, comparator, comparisonValue);
226     }
227    
228     //change the positions of atoms which belong to the rigidbodies
229     for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
230     compareProperty(rb, bs, property, comparator, comparisonValue);
231     }
232 tim 1962 }
233    
234 tim 1967 return bs;
235     }
236 tim 1962
237 tim 1967 void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) {
238     double propertyValue;
239     switch (property) {
240     case Token::mass:
241     propertyValue = sd->getMass();
242     break;
243     case Token::charge:
244     return;
245     //break;
246     case Token::dipole:
247     return;
248     //break;
249     default:
250     unrecognizedAtomProperty(property);
251     }
252    
253     bool match = false;
254     switch (comparator) {
255     case Token::opLT:
256     match = propertyValue < comparisonValue;
257     break;
258     case Token::opLE:
259     match = propertyValue <= comparisonValue;
260     break;
261     case Token::opGE:
262     match = propertyValue >= comparisonValue;
263     break;
264     case Token::opGT:
265     match = propertyValue > comparisonValue;
266     break;
267     case Token::opEQ:
268     match = propertyValue == comparisonValue;
269     break;
270     case Token::opNE:
271     match = propertyValue != comparisonValue;
272     break;
273     }
274     if (match)
275     bs.setBitOn(sd->getGlobalIndex());
276    
277     }
278    
279     void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){
280    
281 tim 1965 boost::any withinSpec = instruction.value;
282 tim 1966 if (withinSpec.type() == typeid(float)){
283 tim 1967 //
284 tim 1962 return;
285     }
286 tim 1966
287 tim 1967 evalError("Unrecognized within parameter");
288 tim 1962 }
289    
290 tim 1967 void SelectionEvaluator::define() {
291 tim 1966 assert(statement.size() >= 3);
292    
293     std::string variable = boost::any_cast<std::string>(statement[1].value);
294 tim 1967
295 tim 1965 variables.insert(std::make_pair(variable, expression(statement, 2)));
296 tim 1962 }
297    
298 tim 1967
299 tim 1966 /** @todo */
300     void SelectionEvaluator::predefine(const std::string& script) {
301 tim 1962
302 tim 1966 if (compiler.compile("#predefine", script)) {
303     std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled();
304     if (aatoken.size() != 1) {
305     evalError("predefinition does not have exactly 1 command:"
306     + script);
307     return;
308     }
309     std::vector<Token> statement = aatoken[0];
310     if (statement.size() > 2) {
311     int tok = statement[1].tok;
312     if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) {
313 tim 1967 std::string variable = boost::any_cast<std::string>(statement[1].value);
314 tim 1966 variables.insert(std::make_pair(variable, statement));
315    
316     } else {
317     evalError("invalid variable name:" + script);
318     }
319     }else {
320     evalError("bad predefinition length:" + script);
321     }
322    
323    
324     } else {
325     evalError("predefined set compile error:" + script +
326     "\ncompile error:" + compiler.getErrorMessage());
327     }
328    
329     }
330    
331 tim 1972 void SelectionEvaluator::select(BitSet& bs){
332     bs = expression(statement, 1);
333 tim 1966 }
334    
335     BitSet SelectionEvaluator::lookupValue(const std::string& variable){
336    
337     std::map<std::string, boost::any>::iterator i = variables.find(variable);
338    
339     if (i != variables.end()) {
340     if (i->second.type() == typeid(BitSet)) {
341     return boost::any_cast<BitSet>(i->second);
342     } else if (i->second.type() == typeid(std::vector<Token>)){
343 tim 1967 BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2);
344 tim 1966 i->second = bs; /**@todo fixme */
345     return bs;
346     }
347 tim 1967 } else {
348     unrecognizedIdentifier(variable);
349 tim 1966 }
350     }
351    
352 tim 1967 BitSet SelectionEvaluator::nameInstruction(const std::string& name){
353    
354 tim 1972 return finder.match(name);
355    
356 tim 1967 }
357    
358 tim 1972 bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){
359     std::vector<Token>::const_iterator i;
360     for (i = tokens.begin(); i != tokens.end(); ++i) {
361     if (i->tok & Token::dynamic) {
362     return true;
363     }
364     }
365 tim 1967
366 tim 1972 return false;
367     }
368    
369     void SelectionEvaluator::clearDefinitionsAndLoadPredefined() {
370     variables.clear();
371     //load predefine
372     //predefine();
373 tim 1966 }
374 tim 1972
375     BitSet SelectionEvaluator::evaluate() {
376     BitSet bs(nStuntDouble);
377     if (isLoaded_) {
378     instructionDispatchLoop(bs);
379     }
380    
381     return bs;
382     }
383    
384     //BitSet SelectionEvaluator::evaluate(int frameNo) {
385     //
386     //}
387    
388     }