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