1 |
/* |
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. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
*/ |
42 |
|
43 |
#include <stack> |
44 |
#include "selection/SelectionEvaluator.hpp" |
45 |
#include "primitives/Atom.hpp" |
46 |
#include "primitives/DirectionalAtom.hpp" |
47 |
#include "primitives/RigidBody.hpp" |
48 |
#include "primitives/Molecule.hpp" |
49 |
#include "io/ifstrstream.hpp" |
50 |
|
51 |
namespace OpenMD { |
52 |
|
53 |
|
54 |
SelectionEvaluator::SelectionEvaluator(SimInfo* si) |
55 |
: info(si), nameFinder(info), distanceFinder(info), hullFinder(info), |
56 |
indexFinder(info), |
57 |
isLoaded_(false){ |
58 |
nStuntDouble = info->getNGlobalAtoms() + info->getNGlobalRigidBodies(); |
59 |
} |
60 |
|
61 |
bool SelectionEvaluator::loadScript(const std::string& filename, |
62 |
const std::string& script) { |
63 |
clearDefinitionsAndLoadPredefined(); |
64 |
this->filename = filename; |
65 |
this->script = script; |
66 |
if (! compiler.compile(filename, script)) { |
67 |
error = true; |
68 |
errorMessage = compiler.getErrorMessage(); |
69 |
|
70 |
sprintf( painCave.errMsg, |
71 |
"SelectionCompiler Error: %s\n", errorMessage.c_str()); |
72 |
painCave.severity = OPENMD_ERROR; |
73 |
painCave.isFatal = 1; |
74 |
simError(); |
75 |
return false; |
76 |
} |
77 |
|
78 |
pc = 0; |
79 |
aatoken = compiler.getAatokenCompiled(); |
80 |
linenumbers = compiler.getLineNumbers(); |
81 |
lineIndices = compiler.getLineIndices(); |
82 |
|
83 |
std::vector<std::vector<Token> >::const_iterator i; |
84 |
|
85 |
isDynamic_ = false; |
86 |
for (i = aatoken.begin(); i != aatoken.end(); ++i) { |
87 |
if (containDynamicToken(*i)) { |
88 |
isDynamic_ = true; |
89 |
break; |
90 |
} |
91 |
} |
92 |
|
93 |
isLoaded_ = true; |
94 |
return true; |
95 |
} |
96 |
|
97 |
void SelectionEvaluator::clearState() { |
98 |
error = false; |
99 |
errorMessage = ""; |
100 |
} |
101 |
|
102 |
bool SelectionEvaluator::loadScriptString(const std::string& script) { |
103 |
clearState(); |
104 |
return loadScript("", script); |
105 |
} |
106 |
|
107 |
bool SelectionEvaluator::loadScriptFile(const std::string& filename) { |
108 |
clearState(); |
109 |
return loadScriptFileInternal(filename); |
110 |
} |
111 |
|
112 |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
113 |
ifstrstream ifs(filename.c_str()); |
114 |
if (!ifs.is_open()) { |
115 |
return false; |
116 |
} |
117 |
|
118 |
const int bufferSize = 65535; |
119 |
char buffer[bufferSize]; |
120 |
std::string script; |
121 |
while(ifs.getline(buffer, bufferSize)) { |
122 |
script += buffer; |
123 |
} |
124 |
return loadScript(filename, script); |
125 |
} |
126 |
|
127 |
void SelectionEvaluator::instructionDispatchLoop(OpenMDBitSet& bs){ |
128 |
|
129 |
while ( pc < aatoken.size()) { |
130 |
statement = aatoken[pc++]; |
131 |
statementLength = statement.size(); |
132 |
Token token = statement[0]; |
133 |
switch (token.tok) { |
134 |
case Token::define: |
135 |
define(); |
136 |
break; |
137 |
case Token::select: |
138 |
select(bs); |
139 |
break; |
140 |
default: |
141 |
unrecognizedCommand(token); |
142 |
return; |
143 |
} |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
OpenMDBitSet SelectionEvaluator::expression(const std::vector<Token>& code, |
149 |
int pcStart) { |
150 |
OpenMDBitSet bs; |
151 |
std::stack<OpenMDBitSet> stack; |
152 |
|
153 |
for (unsigned int pc = pcStart; pc < code.size(); ++pc) { |
154 |
Token instruction = code[pc]; |
155 |
|
156 |
switch (instruction.tok) { |
157 |
case Token::expressionBegin: |
158 |
break; |
159 |
case Token::expressionEnd: |
160 |
break; |
161 |
case Token::all: |
162 |
bs = allInstruction(); |
163 |
stack.push(bs); |
164 |
break; |
165 |
case Token::none: |
166 |
bs = OpenMDBitSet(nStuntDouble); |
167 |
stack.push(bs); |
168 |
break; |
169 |
case Token::opOr: |
170 |
bs = stack.top(); |
171 |
stack.pop(); |
172 |
stack.top() |= bs; |
173 |
break; |
174 |
case Token::opAnd: |
175 |
bs = stack.top(); |
176 |
stack.pop(); |
177 |
stack.top() &= bs; |
178 |
break; |
179 |
case Token::opNot: |
180 |
stack.top().flip(); |
181 |
break; |
182 |
case Token::within: |
183 |
withinInstruction(instruction, stack.top()); |
184 |
break; |
185 |
case Token::hull: |
186 |
stack.push(hull()); |
187 |
break; |
188 |
//case Token::selected: |
189 |
// stack.push(getSelectionSet()); |
190 |
// break; |
191 |
case Token::name: |
192 |
stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
193 |
break; |
194 |
case Token::index: |
195 |
stack.push(indexInstruction(instruction.value)); |
196 |
break; |
197 |
case Token::identifier: |
198 |
stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
199 |
break; |
200 |
case Token::opLT: |
201 |
case Token::opLE: |
202 |
case Token::opGE: |
203 |
case Token::opGT: |
204 |
case Token::opEQ: |
205 |
case Token::opNE: |
206 |
stack.push(comparatorInstruction(instruction)); |
207 |
break; |
208 |
default: |
209 |
unrecognizedExpression(); |
210 |
} |
211 |
} |
212 |
if (stack.size() != 1) |
213 |
evalError("atom expression compiler error - stack over/underflow"); |
214 |
|
215 |
return stack.top(); |
216 |
} |
217 |
|
218 |
|
219 |
|
220 |
OpenMDBitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { |
221 |
int comparator = instruction.tok; |
222 |
int property = instruction.intValue; |
223 |
float comparisonValue = boost::any_cast<float>(instruction.value); |
224 |
OpenMDBitSet bs(nStuntDouble); |
225 |
bs.clearAll(); |
226 |
|
227 |
SimInfo::MoleculeIterator mi; |
228 |
Molecule* mol; |
229 |
Molecule::AtomIterator ai; |
230 |
Atom* atom; |
231 |
Molecule::RigidBodyIterator rbIter; |
232 |
RigidBody* rb; |
233 |
|
234 |
for (mol = info->beginMolecule(mi); mol != NULL; |
235 |
mol = info->nextMolecule(mi)) { |
236 |
|
237 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
238 |
compareProperty(atom, bs, property, comparator, comparisonValue); |
239 |
} |
240 |
|
241 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
242 |
rb = mol->nextRigidBody(rbIter)) { |
243 |
compareProperty(rb, bs, property, comparator, comparisonValue); |
244 |
} |
245 |
} |
246 |
|
247 |
return bs; |
248 |
} |
249 |
|
250 |
void SelectionEvaluator::compareProperty(StuntDouble* sd, OpenMDBitSet& bs, |
251 |
int property, int comparator, |
252 |
float comparisonValue) { |
253 |
RealType propertyValue = 0.0; |
254 |
switch (property) { |
255 |
case Token::mass: |
256 |
propertyValue = sd->getMass(); |
257 |
break; |
258 |
case Token::charge: |
259 |
if (sd->isAtom()){ |
260 |
Atom* atom = static_cast<Atom*>(sd); |
261 |
propertyValue = getCharge(atom); |
262 |
} else if (sd->isRigidBody()) { |
263 |
RigidBody* rb = static_cast<RigidBody*>(sd); |
264 |
RigidBody::AtomIterator ai; |
265 |
Atom* atom; |
266 |
for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
267 |
propertyValue+= getCharge(atom); |
268 |
} |
269 |
} |
270 |
break; |
271 |
case Token::x: |
272 |
propertyValue = sd->getPos().x(); |
273 |
break; |
274 |
case Token::y: |
275 |
propertyValue = sd->getPos().y(); |
276 |
break; |
277 |
case Token::z: |
278 |
propertyValue = sd->getPos().z(); |
279 |
break; |
280 |
case Token::r: |
281 |
propertyValue = sd->getPos().length(); |
282 |
break; |
283 |
default: |
284 |
unrecognizedAtomProperty(property); |
285 |
} |
286 |
|
287 |
bool match = false; |
288 |
switch (comparator) { |
289 |
case Token::opLT: |
290 |
match = propertyValue < comparisonValue; |
291 |
break; |
292 |
case Token::opLE: |
293 |
match = propertyValue <= comparisonValue; |
294 |
break; |
295 |
case Token::opGE: |
296 |
match = propertyValue >= comparisonValue; |
297 |
break; |
298 |
case Token::opGT: |
299 |
match = propertyValue > comparisonValue; |
300 |
break; |
301 |
case Token::opEQ: |
302 |
match = propertyValue == comparisonValue; |
303 |
break; |
304 |
case Token::opNE: |
305 |
match = propertyValue != comparisonValue; |
306 |
break; |
307 |
} |
308 |
if (match) |
309 |
bs.setBitOn(sd->getGlobalIndex()); |
310 |
|
311 |
} |
312 |
|
313 |
void SelectionEvaluator::withinInstruction(const Token& instruction, |
314 |
OpenMDBitSet& bs){ |
315 |
|
316 |
boost::any withinSpec = instruction.value; |
317 |
float distance; |
318 |
if (withinSpec.type() == typeid(float)){ |
319 |
distance = boost::any_cast<float>(withinSpec); |
320 |
} else if (withinSpec.type() == typeid(int)) { |
321 |
distance = boost::any_cast<int>(withinSpec); |
322 |
} else { |
323 |
evalError("casting error in withinInstruction"); |
324 |
bs.clearAll(); |
325 |
} |
326 |
|
327 |
bs = distanceFinder.find(bs, distance); |
328 |
} |
329 |
|
330 |
void SelectionEvaluator::define() { |
331 |
assert(statement.size() >= 3); |
332 |
|
333 |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
334 |
|
335 |
variables.insert(VariablesType::value_type(variable, |
336 |
expression(statement, 2))); |
337 |
} |
338 |
|
339 |
|
340 |
/** @todo */ |
341 |
void SelectionEvaluator::predefine(const std::string& script) { |
342 |
|
343 |
if (compiler.compile("#predefine", script)) { |
344 |
std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
345 |
if (aatoken.size() != 1) { |
346 |
evalError("predefinition does not have exactly 1 command:" |
347 |
+ script); |
348 |
return; |
349 |
} |
350 |
std::vector<Token> statement = aatoken[0]; |
351 |
if (statement.size() > 2) { |
352 |
int tok = statement[1].tok; |
353 |
if (tok == Token::identifier || |
354 |
(tok & Token::predefinedset) == Token::predefinedset) { |
355 |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
356 |
variables.insert(VariablesType::value_type(variable, statement)); |
357 |
|
358 |
} else { |
359 |
evalError("invalid variable name:" + script); |
360 |
} |
361 |
}else { |
362 |
evalError("bad predefinition length:" + script); |
363 |
} |
364 |
|
365 |
} else { |
366 |
evalError("predefined set compile error:" + script + |
367 |
"\ncompile error:" + compiler.getErrorMessage()); |
368 |
} |
369 |
} |
370 |
|
371 |
void SelectionEvaluator::select(OpenMDBitSet& bs){ |
372 |
bs = expression(statement, 1); |
373 |
} |
374 |
|
375 |
OpenMDBitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
376 |
|
377 |
OpenMDBitSet bs(nStuntDouble); |
378 |
std::map<std::string, boost::any>::iterator i = variables.find(variable); |
379 |
|
380 |
if (i != variables.end()) { |
381 |
if (i->second.type() == typeid(OpenMDBitSet)) { |
382 |
return boost::any_cast<OpenMDBitSet>(i->second); |
383 |
} else if (i->second.type() == typeid(std::vector<Token>)){ |
384 |
bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); |
385 |
i->second = bs; /**@todo fixme */ |
386 |
return bs; |
387 |
} |
388 |
} else { |
389 |
unrecognizedIdentifier(variable); |
390 |
} |
391 |
|
392 |
return bs; |
393 |
} |
394 |
|
395 |
OpenMDBitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
396 |
return nameFinder.match(name); |
397 |
} |
398 |
|
399 |
bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){ |
400 |
std::vector<Token>::const_iterator i; |
401 |
for (i = tokens.begin(); i != tokens.end(); ++i) { |
402 |
if (i->tok & Token::dynamic) { |
403 |
return true; |
404 |
} |
405 |
} |
406 |
|
407 |
return false; |
408 |
} |
409 |
|
410 |
void SelectionEvaluator::clearDefinitionsAndLoadPredefined() { |
411 |
variables.clear(); |
412 |
//load predefine |
413 |
//predefine(); |
414 |
} |
415 |
|
416 |
OpenMDBitSet SelectionEvaluator::evaluate() { |
417 |
OpenMDBitSet bs(nStuntDouble); |
418 |
if (isLoaded_) { |
419 |
pc = 0; |
420 |
instructionDispatchLoop(bs); |
421 |
} |
422 |
|
423 |
return bs; |
424 |
} |
425 |
|
426 |
OpenMDBitSet SelectionEvaluator::indexInstruction(const boost::any& value) { |
427 |
OpenMDBitSet bs(nStuntDouble); |
428 |
|
429 |
if (value.type() == typeid(int)) { |
430 |
int index = boost::any_cast<int>(value); |
431 |
if (index < 0 || index >= bs.size()) { |
432 |
invalidIndex(index); |
433 |
} else { |
434 |
bs = indexFinder.find(index); |
435 |
} |
436 |
} else if (value.type() == typeid(std::pair<int, int>)) { |
437 |
std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value); |
438 |
assert(indexRange.first <= indexRange.second); |
439 |
if (indexRange.first < 0 || indexRange.second >= bs.size()) { |
440 |
invalidIndexRange(indexRange); |
441 |
}else { |
442 |
bs = indexFinder.find(indexRange.first, indexRange.second); |
443 |
} |
444 |
} |
445 |
|
446 |
return bs; |
447 |
} |
448 |
|
449 |
OpenMDBitSet SelectionEvaluator::allInstruction() { |
450 |
OpenMDBitSet bs(nStuntDouble); |
451 |
|
452 |
SimInfo::MoleculeIterator mi; |
453 |
Molecule* mol; |
454 |
Molecule::AtomIterator ai; |
455 |
Atom* atom; |
456 |
Molecule::RigidBodyIterator rbIter; |
457 |
RigidBody* rb; |
458 |
|
459 |
// Doing the loop insures that we're actually on this processor. |
460 |
|
461 |
for (mol = info->beginMolecule(mi); mol != NULL; |
462 |
mol = info->nextMolecule(mi)) { |
463 |
|
464 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
465 |
bs.setBitOn(atom->getGlobalIndex()); |
466 |
} |
467 |
|
468 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
469 |
rb = mol->nextRigidBody(rbIter)) { |
470 |
bs.setBitOn(rb->getGlobalIndex()); |
471 |
} |
472 |
} |
473 |
|
474 |
return bs; |
475 |
} |
476 |
|
477 |
OpenMDBitSet SelectionEvaluator::hull() { |
478 |
OpenMDBitSet bs(nStuntDouble); |
479 |
|
480 |
bs = hullFinder.findHull(); |
481 |
|
482 |
return bs; |
483 |
} |
484 |
|
485 |
RealType SelectionEvaluator::getCharge(Atom* atom) { |
486 |
RealType charge =0.0; |
487 |
AtomType* atomType = atom->getAtomType(); |
488 |
if (atomType->isCharge()) { |
489 |
GenericData* data = atomType->getPropertyByName("Charge"); |
490 |
if (data != NULL) { |
491 |
DoubleGenericData* doubleData= dynamic_cast<DoubleGenericData*>(data); |
492 |
|
493 |
if (doubleData != NULL) { |
494 |
charge = doubleData->getData(); |
495 |
|
496 |
} else { |
497 |
sprintf( painCave.errMsg, |
498 |
"Can not cast GenericData to DoubleGenericData\n"); |
499 |
painCave.severity = OPENMD_ERROR; |
500 |
painCave.isFatal = 1; |
501 |
simError(); |
502 |
} |
503 |
} |
504 |
} |
505 |
|
506 |
return charge; |
507 |
} |
508 |
|
509 |
} |