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