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