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 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
tim |
277 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
tim |
277 |
* 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 |
gezelter |
1390 |
* |
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 |
gezelter |
1879 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
gezelter |
1782 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
tim |
277 |
*/ |
42 |
|
|
|
43 |
tim |
283 |
#include <stack> |
44 |
tim |
277 |
#include "selection/SelectionEvaluator.hpp" |
45 |
tim |
283 |
#include "primitives/Atom.hpp" |
46 |
|
|
#include "primitives/DirectionalAtom.hpp" |
47 |
|
|
#include "primitives/RigidBody.hpp" |
48 |
|
|
#include "primitives/Molecule.hpp" |
49 |
gezelter |
1782 |
#include "io/ifstrstream.hpp" |
50 |
gezelter |
1816 |
#include "types/FixedChargeAdapter.hpp" |
51 |
|
|
#include "types/FluctuatingChargeAdapter.hpp" |
52 |
tim |
283 |
|
53 |
gezelter |
1816 |
|
54 |
gezelter |
1390 |
namespace OpenMD { |
55 |
tim |
277 |
|
56 |
|
|
|
57 |
gezelter |
507 |
SelectionEvaluator::SelectionEvaluator(SimInfo* si) |
58 |
gezelter |
1412 |
: info(si), nameFinder(info), distanceFinder(info), hullFinder(info), |
59 |
gezelter |
1903 |
indexFinder(info), hasSurfaceArea_(false), |
60 |
cli2 |
1364 |
isLoaded_(false){ |
61 |
gezelter |
1953 |
nObjects.push_back(info->getNGlobalAtoms() + info->getNGlobalRigidBodies()); |
62 |
|
|
nObjects.push_back(info->getNGlobalBonds()); |
63 |
|
|
nObjects.push_back(info->getNGlobalBends()); |
64 |
|
|
nObjects.push_back(info->getNGlobalTorsions()); |
65 |
|
|
nObjects.push_back(info->getNGlobalInversions()); |
66 |
gezelter |
2052 |
nObjects.push_back(info->getNGlobalMolecules()); |
67 |
gezelter |
1953 |
} |
68 |
|
|
|
69 |
cli2 |
1364 |
bool SelectionEvaluator::loadScript(const std::string& filename, |
70 |
|
|
const std::string& script) { |
71 |
tim |
288 |
clearDefinitionsAndLoadPredefined(); |
72 |
tim |
278 |
this->filename = filename; |
73 |
|
|
this->script = script; |
74 |
|
|
if (! compiler.compile(filename, script)) { |
75 |
gezelter |
507 |
error = true; |
76 |
|
|
errorMessage = compiler.getErrorMessage(); |
77 |
cli2 |
1364 |
|
78 |
|
|
sprintf( painCave.errMsg, |
79 |
|
|
"SelectionCompiler Error: %s\n", errorMessage.c_str()); |
80 |
gezelter |
1390 |
painCave.severity = OPENMD_ERROR; |
81 |
cli2 |
1364 |
painCave.isFatal = 1; |
82 |
|
|
simError(); |
83 |
gezelter |
507 |
return false; |
84 |
tim |
278 |
} |
85 |
|
|
|
86 |
|
|
pc = 0; |
87 |
|
|
aatoken = compiler.getAatokenCompiled(); |
88 |
|
|
linenumbers = compiler.getLineNumbers(); |
89 |
|
|
lineIndices = compiler.getLineIndices(); |
90 |
tim |
288 |
|
91 |
|
|
std::vector<std::vector<Token> >::const_iterator i; |
92 |
|
|
|
93 |
|
|
isDynamic_ = false; |
94 |
|
|
for (i = aatoken.begin(); i != aatoken.end(); ++i) { |
95 |
gezelter |
507 |
if (containDynamicToken(*i)) { |
96 |
|
|
isDynamic_ = true; |
97 |
|
|
break; |
98 |
|
|
} |
99 |
tim |
288 |
} |
100 |
|
|
|
101 |
|
|
isLoaded_ = true; |
102 |
tim |
278 |
return true; |
103 |
gezelter |
507 |
} |
104 |
tim |
278 |
|
105 |
gezelter |
507 |
void SelectionEvaluator::clearState() { |
106 |
tim |
278 |
error = false; |
107 |
tim |
281 |
errorMessage = ""; |
108 |
gezelter |
507 |
} |
109 |
tim |
278 |
|
110 |
gezelter |
507 |
bool SelectionEvaluator::loadScriptString(const std::string& script) { |
111 |
tim |
278 |
clearState(); |
112 |
tim |
281 |
return loadScript("", script); |
113 |
gezelter |
507 |
} |
114 |
tim |
278 |
|
115 |
gezelter |
507 |
bool SelectionEvaluator::loadScriptFile(const std::string& filename) { |
116 |
tim |
278 |
clearState(); |
117 |
|
|
return loadScriptFileInternal(filename); |
118 |
gezelter |
507 |
} |
119 |
tim |
278 |
|
120 |
cli2 |
1364 |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
121 |
|
|
ifstrstream ifs(filename.c_str()); |
122 |
tim |
288 |
if (!ifs.is_open()) { |
123 |
gezelter |
507 |
return false; |
124 |
tim |
288 |
} |
125 |
cli2 |
1364 |
|
126 |
tim |
288 |
const int bufferSize = 65535; |
127 |
|
|
char buffer[bufferSize]; |
128 |
|
|
std::string script; |
129 |
|
|
while(ifs.getline(buffer, bufferSize)) { |
130 |
gezelter |
507 |
script += buffer; |
131 |
tim |
288 |
} |
132 |
|
|
return loadScript(filename, script); |
133 |
gezelter |
507 |
} |
134 |
cli2 |
1364 |
|
135 |
gezelter |
1953 |
void SelectionEvaluator::instructionDispatchLoop(SelectionSet& bs){ |
136 |
tim |
288 |
|
137 |
tim |
281 |
while ( pc < aatoken.size()) { |
138 |
gezelter |
507 |
statement = aatoken[pc++]; |
139 |
|
|
statementLength = statement.size(); |
140 |
|
|
Token token = statement[0]; |
141 |
|
|
switch (token.tok) { |
142 |
|
|
case Token::define: |
143 |
|
|
define(); |
144 |
|
|
break; |
145 |
|
|
case Token::select: |
146 |
|
|
select(bs); |
147 |
|
|
break; |
148 |
|
|
default: |
149 |
|
|
unrecognizedCommand(token); |
150 |
|
|
return; |
151 |
|
|
} |
152 |
tim |
278 |
} |
153 |
tim |
288 |
|
154 |
gezelter |
507 |
} |
155 |
tim |
278 |
|
156 |
gezelter |
1953 |
void SelectionEvaluator::instructionDispatchLoop(SelectionSet& bs, int frame){ |
157 |
gezelter |
1816 |
|
158 |
|
|
while ( pc < aatoken.size()) { |
159 |
|
|
statement = aatoken[pc++]; |
160 |
|
|
statementLength = statement.size(); |
161 |
|
|
Token token = statement[0]; |
162 |
|
|
switch (token.tok) { |
163 |
|
|
case Token::define: |
164 |
|
|
define(); |
165 |
|
|
break; |
166 |
|
|
case Token::select: |
167 |
|
|
select(bs, frame); |
168 |
|
|
break; |
169 |
|
|
default: |
170 |
|
|
unrecognizedCommand(token); |
171 |
|
|
return; |
172 |
|
|
} |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
} |
176 |
|
|
|
177 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::expression(const std::vector<Token>& code, |
178 |
cli2 |
1364 |
int pcStart) { |
179 |
gezelter |
1953 |
SelectionSet bs = createSelectionSets(); |
180 |
|
|
std::stack<SelectionSet> stack; |
181 |
|
|
vector<int> bsSize = bs.size(); |
182 |
cli2 |
1364 |
|
183 |
gezelter |
1782 |
for (unsigned int pc = pcStart; pc < code.size(); ++pc) { |
184 |
tim |
278 |
Token instruction = code[pc]; |
185 |
tim |
282 |
|
186 |
tim |
278 |
switch (instruction.tok) { |
187 |
tim |
281 |
case Token::expressionBegin: |
188 |
tim |
278 |
break; |
189 |
tim |
281 |
case Token::expressionEnd: |
190 |
tim |
282 |
break; |
191 |
tim |
281 |
case Token::all: |
192 |
gezelter |
1801 |
bs = allInstruction(); |
193 |
gezelter |
1953 |
stack.push(bs); |
194 |
tim |
278 |
break; |
195 |
tim |
281 |
case Token::none: |
196 |
gezelter |
1953 |
bs = createSelectionSets(); |
197 |
|
|
stack.push(bs); |
198 |
tim |
278 |
break; |
199 |
tim |
281 |
case Token::opOr: |
200 |
gezelter |
1953 |
bs= stack.top(); |
201 |
|
|
stack.pop(); |
202 |
|
|
stack.top() |= bs; |
203 |
tim |
278 |
break; |
204 |
tim |
281 |
case Token::opAnd: |
205 |
gezelter |
1953 |
bs = stack.top(); |
206 |
|
|
stack.pop(); |
207 |
|
|
stack.top() &= bs; |
208 |
tim |
278 |
break; |
209 |
tim |
281 |
case Token::opNot: |
210 |
gezelter |
1953 |
stack.top().flip(); |
211 |
tim |
278 |
break; |
212 |
tim |
281 |
case Token::within: |
213 |
tim |
283 |
withinInstruction(instruction, stack.top()); |
214 |
tim |
278 |
break; |
215 |
gezelter |
1412 |
case Token::hull: |
216 |
gezelter |
1953 |
stack.push(hull()); |
217 |
gezelter |
1412 |
break; |
218 |
gezelter |
507 |
//case Token::selected: |
219 |
|
|
// stack.push(getSelectionSet()); |
220 |
|
|
// break; |
221 |
tim |
281 |
case Token::name: |
222 |
gezelter |
1953 |
stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
223 |
tim |
278 |
break; |
224 |
tim |
295 |
case Token::index: |
225 |
gezelter |
1953 |
stack.push(indexInstruction(instruction.value)); |
226 |
tim |
281 |
break; |
227 |
|
|
case Token::identifier: |
228 |
gezelter |
1953 |
stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
229 |
tim |
281 |
break; |
230 |
|
|
case Token::opLT: |
231 |
|
|
case Token::opLE: |
232 |
|
|
case Token::opGE: |
233 |
|
|
case Token::opGT: |
234 |
|
|
case Token::opEQ: |
235 |
|
|
case Token::opNE: |
236 |
tim |
283 |
stack.push(comparatorInstruction(instruction)); |
237 |
tim |
278 |
break; |
238 |
|
|
default: |
239 |
|
|
unrecognizedExpression(); |
240 |
|
|
} |
241 |
|
|
} |
242 |
tim |
283 |
if (stack.size() != 1) |
243 |
tim |
278 |
evalError("atom expression compiler error - stack over/underflow"); |
244 |
tim |
283 |
|
245 |
|
|
return stack.top(); |
246 |
tim |
278 |
} |
247 |
|
|
|
248 |
|
|
|
249 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::expression(const std::vector<Token>& code, |
250 |
gezelter |
1816 |
int pcStart, int frame) { |
251 |
gezelter |
1953 |
SelectionSet bs = createSelectionSets(); |
252 |
|
|
std::stack<SelectionSet> stack; |
253 |
gezelter |
1816 |
|
254 |
|
|
for (unsigned int pc = pcStart; pc < code.size(); ++pc) { |
255 |
|
|
Token instruction = code[pc]; |
256 |
tim |
278 |
|
257 |
gezelter |
1816 |
switch (instruction.tok) { |
258 |
|
|
case Token::expressionBegin: |
259 |
|
|
break; |
260 |
|
|
case Token::expressionEnd: |
261 |
|
|
break; |
262 |
|
|
case Token::all: |
263 |
|
|
bs = allInstruction(); |
264 |
|
|
stack.push(bs); |
265 |
|
|
break; |
266 |
|
|
case Token::none: |
267 |
gezelter |
1953 |
bs = SelectionSet(nObjects); |
268 |
gezelter |
1816 |
stack.push(bs); |
269 |
|
|
break; |
270 |
|
|
case Token::opOr: |
271 |
|
|
bs = stack.top(); |
272 |
|
|
stack.pop(); |
273 |
|
|
stack.top() |= bs; |
274 |
|
|
break; |
275 |
|
|
case Token::opAnd: |
276 |
|
|
bs = stack.top(); |
277 |
|
|
stack.pop(); |
278 |
|
|
stack.top() &= bs; |
279 |
|
|
break; |
280 |
|
|
case Token::opNot: |
281 |
|
|
stack.top().flip(); |
282 |
|
|
break; |
283 |
|
|
case Token::within: |
284 |
|
|
withinInstruction(instruction, stack.top(), frame); |
285 |
|
|
break; |
286 |
|
|
case Token::hull: |
287 |
|
|
stack.push(hull(frame)); |
288 |
|
|
break; |
289 |
|
|
//case Token::selected: |
290 |
|
|
// stack.push(getSelectionSet()); |
291 |
|
|
// break; |
292 |
|
|
case Token::name: |
293 |
|
|
stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
294 |
|
|
break; |
295 |
|
|
case Token::index: |
296 |
|
|
stack.push(indexInstruction(instruction.value)); |
297 |
|
|
break; |
298 |
|
|
case Token::identifier: |
299 |
|
|
stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
300 |
|
|
break; |
301 |
|
|
case Token::opLT: |
302 |
|
|
case Token::opLE: |
303 |
|
|
case Token::opGE: |
304 |
|
|
case Token::opGT: |
305 |
|
|
case Token::opEQ: |
306 |
|
|
case Token::opNE: |
307 |
|
|
stack.push(comparatorInstruction(instruction, frame)); |
308 |
|
|
break; |
309 |
|
|
default: |
310 |
|
|
unrecognizedExpression(); |
311 |
|
|
} |
312 |
|
|
} |
313 |
|
|
if (stack.size() != 1) |
314 |
|
|
evalError("atom expression compiler error - stack over/underflow"); |
315 |
|
|
|
316 |
|
|
return stack.top(); |
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { |
322 |
tim |
278 |
int comparator = instruction.tok; |
323 |
|
|
int property = instruction.intValue; |
324 |
tim |
283 |
float comparisonValue = boost::any_cast<float>(instruction.value); |
325 |
gezelter |
1953 |
SelectionSet bs = createSelectionSets(); |
326 |
tim |
295 |
bs.clearAll(); |
327 |
tim |
283 |
|
328 |
|
|
SimInfo::MoleculeIterator mi; |
329 |
|
|
Molecule* mol; |
330 |
|
|
Molecule::AtomIterator ai; |
331 |
|
|
Atom* atom; |
332 |
|
|
Molecule::RigidBodyIterator rbIter; |
333 |
|
|
RigidBody* rb; |
334 |
tim |
281 |
|
335 |
cli2 |
1364 |
for (mol = info->beginMolecule(mi); mol != NULL; |
336 |
|
|
mol = info->nextMolecule(mi)) { |
337 |
|
|
|
338 |
gezelter |
507 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
339 |
|
|
compareProperty(atom, bs, property, comparator, comparisonValue); |
340 |
|
|
} |
341 |
cli2 |
1364 |
|
342 |
|
|
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
343 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
344 |
|
|
compareProperty(rb, bs, property, comparator, comparisonValue); |
345 |
|
|
} |
346 |
tim |
278 |
} |
347 |
|
|
|
348 |
tim |
283 |
return bs; |
349 |
gezelter |
507 |
} |
350 |
tim |
278 |
|
351 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::comparatorInstruction(const Token& instruction, int frame) { |
352 |
gezelter |
1816 |
int comparator = instruction.tok; |
353 |
|
|
int property = instruction.intValue; |
354 |
|
|
float comparisonValue = boost::any_cast<float>(instruction.value); |
355 |
gezelter |
1953 |
SelectionSet bs = createSelectionSets(); |
356 |
gezelter |
1816 |
bs.clearAll(); |
357 |
|
|
|
358 |
|
|
SimInfo::MoleculeIterator mi; |
359 |
|
|
Molecule* mol; |
360 |
|
|
Molecule::AtomIterator ai; |
361 |
|
|
Atom* atom; |
362 |
|
|
Molecule::RigidBodyIterator rbIter; |
363 |
|
|
RigidBody* rb; |
364 |
|
|
|
365 |
|
|
for (mol = info->beginMolecule(mi); mol != NULL; |
366 |
|
|
mol = info->nextMolecule(mi)) { |
367 |
|
|
|
368 |
|
|
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
369 |
|
|
compareProperty(atom, bs, property, comparator, comparisonValue, frame); |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
373 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
374 |
|
|
compareProperty(rb, bs, property, comparator, comparisonValue, frame); |
375 |
|
|
} |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
return bs; |
379 |
|
|
} |
380 |
|
|
|
381 |
gezelter |
1953 |
void SelectionEvaluator::compareProperty(StuntDouble* sd, SelectionSet& bs, |
382 |
cli2 |
1364 |
int property, int comparator, |
383 |
|
|
float comparisonValue) { |
384 |
tim |
963 |
RealType propertyValue = 0.0; |
385 |
gezelter |
1879 |
Vector3d pos; |
386 |
gezelter |
1931 |
|
387 |
gezelter |
507 |
switch (property) { |
388 |
|
|
case Token::mass: |
389 |
|
|
propertyValue = sd->getMass(); |
390 |
|
|
break; |
391 |
|
|
case Token::charge: |
392 |
|
|
if (sd->isAtom()){ |
393 |
|
|
Atom* atom = static_cast<Atom*>(sd); |
394 |
|
|
propertyValue = getCharge(atom); |
395 |
|
|
} else if (sd->isRigidBody()) { |
396 |
|
|
RigidBody* rb = static_cast<RigidBody*>(sd); |
397 |
|
|
RigidBody::AtomIterator ai; |
398 |
|
|
Atom* atom; |
399 |
|
|
for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
400 |
|
|
propertyValue+= getCharge(atom); |
401 |
|
|
} |
402 |
|
|
} |
403 |
|
|
break; |
404 |
cli2 |
1360 |
case Token::x: |
405 |
|
|
propertyValue = sd->getPos().x(); |
406 |
|
|
break; |
407 |
|
|
case Token::y: |
408 |
|
|
propertyValue = sd->getPos().y(); |
409 |
|
|
break; |
410 |
|
|
case Token::z: |
411 |
|
|
propertyValue = sd->getPos().z(); |
412 |
|
|
break; |
413 |
gezelter |
1879 |
case Token::wrappedX: |
414 |
|
|
pos = sd->getPos(); |
415 |
|
|
info->getSnapshotManager()->getCurrentSnapshot()->wrapVector(pos); |
416 |
|
|
propertyValue = pos.x(); |
417 |
|
|
break; |
418 |
|
|
case Token::wrappedY: |
419 |
|
|
pos = sd->getPos(); |
420 |
|
|
info->getSnapshotManager()->getCurrentSnapshot()->wrapVector(pos); |
421 |
|
|
propertyValue = pos.y(); |
422 |
|
|
break; |
423 |
|
|
case Token::wrappedZ: |
424 |
|
|
pos = sd->getPos(); |
425 |
|
|
info->getSnapshotManager()->getCurrentSnapshot()->wrapVector(pos); |
426 |
|
|
propertyValue = pos.z(); |
427 |
|
|
break; |
428 |
gezelter |
1513 |
case Token::r: |
429 |
|
|
propertyValue = sd->getPos().length(); |
430 |
|
|
break; |
431 |
gezelter |
507 |
default: |
432 |
|
|
unrecognizedAtomProperty(property); |
433 |
|
|
} |
434 |
tim |
283 |
|
435 |
gezelter |
507 |
bool match = false; |
436 |
|
|
switch (comparator) { |
437 |
|
|
case Token::opLT: |
438 |
|
|
match = propertyValue < comparisonValue; |
439 |
|
|
break; |
440 |
|
|
case Token::opLE: |
441 |
|
|
match = propertyValue <= comparisonValue; |
442 |
|
|
break; |
443 |
|
|
case Token::opGE: |
444 |
|
|
match = propertyValue >= comparisonValue; |
445 |
|
|
break; |
446 |
|
|
case Token::opGT: |
447 |
|
|
match = propertyValue > comparisonValue; |
448 |
|
|
break; |
449 |
|
|
case Token::opEQ: |
450 |
|
|
match = propertyValue == comparisonValue; |
451 |
|
|
break; |
452 |
|
|
case Token::opNE: |
453 |
|
|
match = propertyValue != comparisonValue; |
454 |
|
|
break; |
455 |
|
|
} |
456 |
gezelter |
1931 |
|
457 |
gezelter |
1816 |
if (match) |
458 |
gezelter |
1953 |
bs.bitsets_[STUNTDOUBLE].setBitOn(sd->getGlobalIndex()); |
459 |
gezelter |
1816 |
|
460 |
cli2 |
1364 |
|
461 |
gezelter |
507 |
} |
462 |
tim |
283 |
|
463 |
gezelter |
1953 |
void SelectionEvaluator::compareProperty(StuntDouble* sd, SelectionSet& bs, |
464 |
gezelter |
1816 |
int property, int comparator, |
465 |
|
|
float comparisonValue, int frame) { |
466 |
|
|
RealType propertyValue = 0.0; |
467 |
gezelter |
1879 |
Vector3d pos; |
468 |
gezelter |
1816 |
switch (property) { |
469 |
|
|
case Token::mass: |
470 |
|
|
propertyValue = sd->getMass(); |
471 |
|
|
break; |
472 |
|
|
case Token::charge: |
473 |
|
|
if (sd->isAtom()){ |
474 |
|
|
Atom* atom = static_cast<Atom*>(sd); |
475 |
|
|
propertyValue = getCharge(atom,frame); |
476 |
|
|
} else if (sd->isRigidBody()) { |
477 |
|
|
RigidBody* rb = static_cast<RigidBody*>(sd); |
478 |
|
|
RigidBody::AtomIterator ai; |
479 |
|
|
Atom* atom; |
480 |
|
|
for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
481 |
|
|
propertyValue+= getCharge(atom,frame); |
482 |
|
|
} |
483 |
|
|
} |
484 |
|
|
break; |
485 |
|
|
case Token::x: |
486 |
|
|
propertyValue = sd->getPos(frame).x(); |
487 |
|
|
break; |
488 |
|
|
case Token::y: |
489 |
|
|
propertyValue = sd->getPos(frame).y(); |
490 |
|
|
break; |
491 |
|
|
case Token::z: |
492 |
|
|
propertyValue = sd->getPos(frame).z(); |
493 |
|
|
break; |
494 |
gezelter |
1879 |
case Token::wrappedX: |
495 |
|
|
pos = sd->getPos(frame); |
496 |
|
|
info->getSnapshotManager()->getSnapshot(frame)->wrapVector(pos); |
497 |
|
|
propertyValue = pos.x(); |
498 |
|
|
break; |
499 |
|
|
case Token::wrappedY: |
500 |
|
|
pos = sd->getPos(frame); |
501 |
|
|
info->getSnapshotManager()->getSnapshot(frame)->wrapVector(pos); |
502 |
|
|
propertyValue = pos.y(); |
503 |
|
|
break; |
504 |
|
|
case Token::wrappedZ: |
505 |
|
|
pos = sd->getPos(frame); |
506 |
|
|
info->getSnapshotManager()->getSnapshot(frame)->wrapVector(pos); |
507 |
|
|
propertyValue = pos.z(); |
508 |
|
|
break; |
509 |
|
|
|
510 |
gezelter |
1816 |
case Token::r: |
511 |
|
|
propertyValue = sd->getPos(frame).length(); |
512 |
|
|
break; |
513 |
|
|
default: |
514 |
|
|
unrecognizedAtomProperty(property); |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
bool match = false; |
518 |
|
|
switch (comparator) { |
519 |
|
|
case Token::opLT: |
520 |
|
|
match = propertyValue < comparisonValue; |
521 |
|
|
break; |
522 |
|
|
case Token::opLE: |
523 |
|
|
match = propertyValue <= comparisonValue; |
524 |
|
|
break; |
525 |
|
|
case Token::opGE: |
526 |
|
|
match = propertyValue >= comparisonValue; |
527 |
|
|
break; |
528 |
|
|
case Token::opGT: |
529 |
|
|
match = propertyValue > comparisonValue; |
530 |
|
|
break; |
531 |
|
|
case Token::opEQ: |
532 |
|
|
match = propertyValue == comparisonValue; |
533 |
|
|
break; |
534 |
|
|
case Token::opNE: |
535 |
|
|
match = propertyValue != comparisonValue; |
536 |
|
|
break; |
537 |
|
|
} |
538 |
|
|
if (match) |
539 |
gezelter |
1953 |
bs.bitsets_[STUNTDOUBLE].setBitOn(sd->getGlobalIndex()); |
540 |
gezelter |
1816 |
|
541 |
|
|
|
542 |
|
|
} |
543 |
|
|
|
544 |
cli2 |
1364 |
void SelectionEvaluator::withinInstruction(const Token& instruction, |
545 |
gezelter |
1953 |
SelectionSet& bs){ |
546 |
tim |
295 |
|
547 |
tim |
281 |
boost::any withinSpec = instruction.value; |
548 |
tim |
295 |
float distance; |
549 |
tim |
282 |
if (withinSpec.type() == typeid(float)){ |
550 |
gezelter |
507 |
distance = boost::any_cast<float>(withinSpec); |
551 |
tim |
295 |
} else if (withinSpec.type() == typeid(int)) { |
552 |
gezelter |
507 |
distance = boost::any_cast<int>(withinSpec); |
553 |
tim |
295 |
} else { |
554 |
gezelter |
507 |
evalError("casting error in withinInstruction"); |
555 |
|
|
bs.clearAll(); |
556 |
tim |
278 |
} |
557 |
tim |
282 |
|
558 |
tim |
295 |
bs = distanceFinder.find(bs, distance); |
559 |
gezelter |
507 |
} |
560 |
gezelter |
1816 |
|
561 |
|
|
void SelectionEvaluator::withinInstruction(const Token& instruction, |
562 |
gezelter |
1953 |
SelectionSet& bs, int frame){ |
563 |
gezelter |
1816 |
|
564 |
|
|
boost::any withinSpec = instruction.value; |
565 |
|
|
float distance; |
566 |
|
|
if (withinSpec.type() == typeid(float)){ |
567 |
|
|
distance = boost::any_cast<float>(withinSpec); |
568 |
|
|
} else if (withinSpec.type() == typeid(int)) { |
569 |
|
|
distance = boost::any_cast<int>(withinSpec); |
570 |
|
|
} else { |
571 |
|
|
evalError("casting error in withinInstruction"); |
572 |
|
|
bs.clearAll(); |
573 |
|
|
} |
574 |
|
|
|
575 |
|
|
bs = distanceFinder.find(bs, distance, frame); |
576 |
|
|
} |
577 |
cli2 |
1364 |
|
578 |
gezelter |
507 |
void SelectionEvaluator::define() { |
579 |
tim |
282 |
assert(statement.size() >= 3); |
580 |
cli2 |
1364 |
|
581 |
tim |
282 |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
582 |
cli2 |
1364 |
|
583 |
|
|
variables.insert(VariablesType::value_type(variable, |
584 |
|
|
expression(statement, 2))); |
585 |
gezelter |
507 |
} |
586 |
cli2 |
1364 |
|
587 |
tim |
278 |
|
588 |
gezelter |
507 |
/** @todo */ |
589 |
|
|
void SelectionEvaluator::predefine(const std::string& script) { |
590 |
cli2 |
1364 |
|
591 |
tim |
282 |
if (compiler.compile("#predefine", script)) { |
592 |
gezelter |
507 |
std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
593 |
|
|
if (aatoken.size() != 1) { |
594 |
|
|
evalError("predefinition does not have exactly 1 command:" |
595 |
|
|
+ script); |
596 |
|
|
return; |
597 |
|
|
} |
598 |
|
|
std::vector<Token> statement = aatoken[0]; |
599 |
|
|
if (statement.size() > 2) { |
600 |
|
|
int tok = statement[1].tok; |
601 |
cli2 |
1364 |
if (tok == Token::identifier || |
602 |
|
|
(tok & Token::predefinedset) == Token::predefinedset) { |
603 |
gezelter |
507 |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
604 |
|
|
variables.insert(VariablesType::value_type(variable, statement)); |
605 |
cli2 |
1364 |
|
606 |
gezelter |
507 |
} else { |
607 |
|
|
evalError("invalid variable name:" + script); |
608 |
|
|
} |
609 |
|
|
}else { |
610 |
|
|
evalError("bad predefinition length:" + script); |
611 |
cli2 |
1364 |
} |
612 |
tim |
282 |
|
613 |
|
|
} else { |
614 |
gezelter |
507 |
evalError("predefined set compile error:" + script + |
615 |
|
|
"\ncompile error:" + compiler.getErrorMessage()); |
616 |
tim |
282 |
} |
617 |
gezelter |
507 |
} |
618 |
tim |
282 |
|
619 |
gezelter |
1953 |
void SelectionEvaluator::select(SelectionSet& bs){ |
620 |
tim |
288 |
bs = expression(statement, 1); |
621 |
gezelter |
507 |
} |
622 |
gezelter |
1816 |
|
623 |
gezelter |
1953 |
void SelectionEvaluator::select(SelectionSet& bs, int frame){ |
624 |
gezelter |
1816 |
bs = expression(statement, 1, frame); |
625 |
|
|
} |
626 |
cli2 |
1364 |
|
627 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::lookupValue(const std::string& variable){ |
628 |
cli2 |
1364 |
|
629 |
gezelter |
1953 |
SelectionSet bs = createSelectionSets(); |
630 |
tim |
282 |
std::map<std::string, boost::any>::iterator i = variables.find(variable); |
631 |
tim |
295 |
|
632 |
tim |
282 |
if (i != variables.end()) { |
633 |
gezelter |
1953 |
if (i->second.type() == typeid(SelectionSet)) { |
634 |
|
|
return boost::any_cast<SelectionSet>(i->second); |
635 |
gezelter |
507 |
} else if (i->second.type() == typeid(std::vector<Token>)){ |
636 |
|
|
bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); |
637 |
|
|
i->second = bs; /**@todo fixme */ |
638 |
|
|
return bs; |
639 |
|
|
} |
640 |
tim |
283 |
} else { |
641 |
gezelter |
507 |
unrecognizedIdentifier(variable); |
642 |
tim |
282 |
} |
643 |
cli2 |
1364 |
|
644 |
tim |
295 |
return bs; |
645 |
gezelter |
507 |
} |
646 |
cli2 |
1364 |
|
647 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::nameInstruction(const std::string& name){ |
648 |
cli2 |
1364 |
return nameFinder.match(name); |
649 |
gezelter |
507 |
} |
650 |
tim |
283 |
|
651 |
gezelter |
507 |
bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){ |
652 |
tim |
288 |
std::vector<Token>::const_iterator i; |
653 |
|
|
for (i = tokens.begin(); i != tokens.end(); ++i) { |
654 |
gezelter |
507 |
if (i->tok & Token::dynamic) { |
655 |
|
|
return true; |
656 |
|
|
} |
657 |
tim |
288 |
} |
658 |
cli2 |
1364 |
|
659 |
tim |
288 |
return false; |
660 |
gezelter |
507 |
} |
661 |
tim |
288 |
|
662 |
gezelter |
507 |
void SelectionEvaluator::clearDefinitionsAndLoadPredefined() { |
663 |
tim |
288 |
variables.clear(); |
664 |
|
|
//load predefine |
665 |
|
|
//predefine(); |
666 |
gezelter |
507 |
} |
667 |
tim |
288 |
|
668 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::createSelectionSets() { |
669 |
|
|
SelectionSet ss(nObjects); |
670 |
|
|
return ss; |
671 |
|
|
} |
672 |
|
|
|
673 |
|
|
SelectionSet SelectionEvaluator::evaluate() { |
674 |
|
|
SelectionSet bs = createSelectionSets(); |
675 |
tim |
288 |
if (isLoaded_) { |
676 |
gezelter |
507 |
pc = 0; |
677 |
|
|
instructionDispatchLoop(bs); |
678 |
tim |
288 |
} |
679 |
gezelter |
1816 |
return bs; |
680 |
|
|
} |
681 |
tim |
288 |
|
682 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::evaluate(int frame) { |
683 |
|
|
SelectionSet bs = createSelectionSets(); |
684 |
gezelter |
1816 |
if (isLoaded_) { |
685 |
|
|
pc = 0; |
686 |
|
|
instructionDispatchLoop(bs, frame); |
687 |
|
|
} |
688 |
tim |
288 |
return bs; |
689 |
gezelter |
507 |
} |
690 |
tim |
295 |
|
691 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::indexInstruction(const boost::any& value) { |
692 |
|
|
SelectionSet bs = createSelectionSets(); |
693 |
tim |
295 |
|
694 |
|
|
if (value.type() == typeid(int)) { |
695 |
gezelter |
507 |
int index = boost::any_cast<int>(value); |
696 |
gezelter |
1953 |
if (index < 0 || index >= bs.bitsets_[STUNTDOUBLE].size()) { |
697 |
gezelter |
507 |
invalidIndex(index); |
698 |
|
|
} else { |
699 |
|
|
bs = indexFinder.find(index); |
700 |
|
|
} |
701 |
tim |
295 |
} else if (value.type() == typeid(std::pair<int, int>)) { |
702 |
gezelter |
507 |
std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value); |
703 |
|
|
assert(indexRange.first <= indexRange.second); |
704 |
gezelter |
1953 |
if (indexRange.first < 0 || |
705 |
|
|
indexRange.second >= bs.bitsets_[STUNTDOUBLE].size()) { |
706 |
gezelter |
507 |
invalidIndexRange(indexRange); |
707 |
|
|
}else { |
708 |
|
|
bs = indexFinder.find(indexRange.first, indexRange.second); |
709 |
|
|
} |
710 |
tim |
295 |
} |
711 |
|
|
|
712 |
|
|
return bs; |
713 |
gezelter |
507 |
} |
714 |
tim |
295 |
|
715 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::allInstruction() { |
716 |
|
|
SelectionSet ss = createSelectionSets(); |
717 |
tim |
432 |
|
718 |
gezelter |
1801 |
SimInfo::MoleculeIterator mi; |
719 |
gezelter |
1953 |
Molecule::AtomIterator ai; |
720 |
|
|
Molecule::RigidBodyIterator rbIter; |
721 |
|
|
Molecule::BondIterator bondIter; |
722 |
|
|
Molecule::BendIterator bendIter; |
723 |
|
|
Molecule::TorsionIterator torsionIter; |
724 |
|
|
Molecule::InversionIterator inversionIter; |
725 |
|
|
|
726 |
gezelter |
1801 |
Molecule* mol; |
727 |
|
|
Atom* atom; |
728 |
|
|
RigidBody* rb; |
729 |
gezelter |
1953 |
Bond* bond; |
730 |
|
|
Bend* bend; |
731 |
|
|
Torsion* torsion; |
732 |
|
|
Inversion* inversion; |
733 |
gezelter |
1801 |
|
734 |
|
|
// Doing the loop insures that we're actually on this processor. |
735 |
|
|
|
736 |
|
|
for (mol = info->beginMolecule(mi); mol != NULL; |
737 |
|
|
mol = info->nextMolecule(mi)) { |
738 |
|
|
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
739 |
gezelter |
1953 |
ss.bitsets_[STUNTDOUBLE].setBitOn(atom->getGlobalIndex()); |
740 |
|
|
} |
741 |
gezelter |
1801 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
742 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
743 |
gezelter |
1953 |
ss.bitsets_[STUNTDOUBLE].setBitOn(rb->getGlobalIndex()); |
744 |
gezelter |
1801 |
} |
745 |
gezelter |
1953 |
for (bond = mol->beginBond(bondIter); bond != NULL; |
746 |
|
|
bond = mol->nextBond(bondIter)) { |
747 |
|
|
ss.bitsets_[BOND].setBitOn(bond->getGlobalIndex()); |
748 |
|
|
} |
749 |
|
|
for (bend = mol->beginBend(bendIter); bend != NULL; |
750 |
|
|
bend = mol->nextBend(bendIter)) { |
751 |
|
|
ss.bitsets_[BEND].setBitOn(bend->getGlobalIndex()); |
752 |
|
|
} |
753 |
|
|
for (torsion = mol->beginTorsion(torsionIter); torsion != NULL; |
754 |
|
|
torsion = mol->nextTorsion(torsionIter)) { |
755 |
|
|
ss.bitsets_[TORSION].setBitOn(torsion->getGlobalIndex()); |
756 |
|
|
} |
757 |
|
|
for (inversion = mol->beginInversion(inversionIter); inversion != NULL; |
758 |
|
|
inversion = mol->nextInversion(inversionIter)) { |
759 |
|
|
ss.bitsets_[INVERSION].setBitOn(inversion->getGlobalIndex()); |
760 |
gezelter |
2052 |
} |
761 |
|
|
ss.bitsets_[MOLECULE].setBitOn(mol->getGlobalIndex()); |
762 |
gezelter |
1801 |
} |
763 |
|
|
|
764 |
gezelter |
1953 |
return ss; |
765 |
gezelter |
1801 |
} |
766 |
|
|
|
767 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::hull() { |
768 |
|
|
SelectionSet bs = createSelectionSets(); |
769 |
gezelter |
1412 |
|
770 |
|
|
bs = hullFinder.findHull(); |
771 |
gezelter |
1903 |
surfaceArea_ = hullFinder.getSurfaceArea(); |
772 |
|
|
hasSurfaceArea_ = true; |
773 |
gezelter |
1412 |
return bs; |
774 |
|
|
} |
775 |
|
|
|
776 |
gezelter |
1816 |
|
777 |
gezelter |
1953 |
SelectionSet SelectionEvaluator::hull(int frame) { |
778 |
|
|
SelectionSet bs = createSelectionSets(); |
779 |
gezelter |
1816 |
|
780 |
|
|
bs = hullFinder.findHull(frame); |
781 |
gezelter |
1903 |
|
782 |
gezelter |
1816 |
return bs; |
783 |
|
|
} |
784 |
|
|
|
785 |
tim |
963 |
RealType SelectionEvaluator::getCharge(Atom* atom) { |
786 |
gezelter |
1816 |
RealType charge = 0.0; |
787 |
tim |
432 |
AtomType* atomType = atom->getAtomType(); |
788 |
|
|
|
789 |
gezelter |
1816 |
FixedChargeAdapter fca = FixedChargeAdapter(atomType); |
790 |
|
|
if ( fca.isFixedCharge() ) { |
791 |
|
|
charge = fca.getCharge(); |
792 |
|
|
} |
793 |
|
|
|
794 |
|
|
FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType); |
795 |
|
|
if ( fqa.isFluctuatingCharge() ) { |
796 |
|
|
charge += atom->getFlucQPos(); |
797 |
|
|
} |
798 |
|
|
return charge; |
799 |
|
|
} |
800 |
tim |
432 |
|
801 |
gezelter |
1816 |
RealType SelectionEvaluator::getCharge(Atom* atom, int frame) { |
802 |
|
|
RealType charge = 0.0; |
803 |
|
|
AtomType* atomType = atom->getAtomType(); |
804 |
|
|
|
805 |
|
|
FixedChargeAdapter fca = FixedChargeAdapter(atomType); |
806 |
|
|
if ( fca.isFixedCharge() ) { |
807 |
|
|
charge = fca.getCharge(); |
808 |
tim |
432 |
} |
809 |
gezelter |
1816 |
|
810 |
|
|
FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter(atomType); |
811 |
|
|
if ( fqa.isFluctuatingCharge() ) { |
812 |
|
|
charge += atom->getFlucQPos(frame); |
813 |
|
|
} |
814 |
tim |
432 |
return charge; |
815 |
gezelter |
507 |
} |
816 |
tim |
432 |
|
817 |
|
|
} |