ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/selection/SelectionEvaluator.cpp
Revision: 2102
Committed: Thu Mar 10 16:14:07 2005 UTC (19 years, 4 months ago) by tim
File size: 12785 byte(s)
Log Message:
minor change

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