ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/SelectionEvaluator.cpp
Revision: 1803
Committed: Wed Oct 3 14:20:07 2012 UTC (12 years, 6 months ago) by gezelter
File size: 14964 byte(s)
Log Message:
Merging trunk changes back to development branch

File Contents

# User Rev Content
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     * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).
39 gezelter 1665 * [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 1627 #include "io/ifstrstream.hpp"
50 tim 283
51 gezelter 1390 namespace OpenMD {
52 tim 277
53    
54 gezelter 507 SelectionEvaluator::SelectionEvaluator(SimInfo* si)
55 gezelter 1412 : info(si), nameFinder(info), distanceFinder(info), hullFinder(info),
56     indexFinder(info),
57 cli2 1364 isLoaded_(false){
58     nStuntDouble = info->getNGlobalAtoms() + info->getNGlobalRigidBodies();
59 gezelter 507 }
60 tim 283
61 cli2 1364 bool SelectionEvaluator::loadScript(const std::string& filename,
62     const std::string& script) {
63 tim 288 clearDefinitionsAndLoadPredefined();
64 tim 278 this->filename = filename;
65     this->script = script;
66     if (! compiler.compile(filename, script)) {
67 gezelter 507 error = true;
68     errorMessage = compiler.getErrorMessage();
69 cli2 1364
70     sprintf( painCave.errMsg,
71     "SelectionCompiler Error: %s\n", errorMessage.c_str());
72 gezelter 1390 painCave.severity = OPENMD_ERROR;
73 cli2 1364 painCave.isFatal = 1;
74     simError();
75 gezelter 507 return false;
76 tim 278 }
77    
78     pc = 0;
79     aatoken = compiler.getAatokenCompiled();
80     linenumbers = compiler.getLineNumbers();
81     lineIndices = compiler.getLineIndices();
82 tim 288
83     std::vector<std::vector<Token> >::const_iterator i;
84    
85     isDynamic_ = false;
86     for (i = aatoken.begin(); i != aatoken.end(); ++i) {
87 gezelter 507 if (containDynamicToken(*i)) {
88     isDynamic_ = true;
89     break;
90     }
91 tim 288 }
92    
93     isLoaded_ = true;
94 tim 278 return true;
95 gezelter 507 }
96 tim 278
97 gezelter 507 void SelectionEvaluator::clearState() {
98 tim 278 error = false;
99 tim 281 errorMessage = "";
100 gezelter 507 }
101 tim 278
102 gezelter 507 bool SelectionEvaluator::loadScriptString(const std::string& script) {
103 tim 278 clearState();
104 tim 281 return loadScript("", script);
105 gezelter 507 }
106 tim 278
107 gezelter 507 bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
108 tim 278 clearState();
109     return loadScriptFileInternal(filename);
110 gezelter 507 }
111 tim 278
112 cli2 1364 bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) {
113     ifstrstream ifs(filename.c_str());
114 tim 288 if (!ifs.is_open()) {
115 gezelter 507 return false;
116 tim 288 }
117 cli2 1364
118 tim 288 const int bufferSize = 65535;
119     char buffer[bufferSize];
120     std::string script;
121     while(ifs.getline(buffer, bufferSize)) {
122 gezelter 507 script += buffer;
123 tim 288 }
124     return loadScript(filename, script);
125 gezelter 507 }
126 cli2 1364
127 gezelter 1390 void SelectionEvaluator::instructionDispatchLoop(OpenMDBitSet& bs){
128 tim 288
129 tim 281 while ( pc < aatoken.size()) {
130 gezelter 507 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 tim 278 }
145 tim 288
146 gezelter 507 }
147 tim 278
148 gezelter 1390 OpenMDBitSet SelectionEvaluator::expression(const std::vector<Token>& code,
149 cli2 1364 int pcStart) {
150 gezelter 1390 OpenMDBitSet bs;
151     std::stack<OpenMDBitSet> stack;
152 cli2 1364
153 gezelter 1767 for (unsigned int pc = pcStart; pc < code.size(); ++pc) {
154 tim 278 Token instruction = code[pc];
155 tim 282
156 tim 278 switch (instruction.tok) {
157 tim 281 case Token::expressionBegin:
158 tim 278 break;
159 tim 281 case Token::expressionEnd:
160 tim 282 break;
161 tim 281 case Token::all:
162 gezelter 1803 bs = allInstruction();
163 tim 283 stack.push(bs);
164 tim 278 break;
165 tim 281 case Token::none:
166 gezelter 1390 bs = OpenMDBitSet(nStuntDouble);
167 tim 283 stack.push(bs);
168 tim 278 break;
169 tim 281 case Token::opOr:
170 tim 283 bs = stack.top();
171     stack.pop();
172     stack.top() |= bs;
173 tim 278 break;
174 tim 281 case Token::opAnd:
175 tim 283 bs = stack.top();
176     stack.pop();
177     stack.top() &= bs;
178 tim 278 break;
179 tim 281 case Token::opNot:
180 tim 283 stack.top().flip();
181 tim 278 break;
182 tim 281 case Token::within:
183 tim 283 withinInstruction(instruction, stack.top());
184 tim 278 break;
185 gezelter 1412 case Token::hull:
186     stack.push(hull());
187     break;
188 gezelter 507 //case Token::selected:
189     // stack.push(getSelectionSet());
190     // break;
191 tim 281 case Token::name:
192 tim 283 stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value)));
193 tim 278 break;
194 tim 295 case Token::index:
195     stack.push(indexInstruction(instruction.value));
196 tim 281 break;
197     case Token::identifier:
198 tim 283 stack.push(lookupValue(boost::any_cast<std::string>(instruction.value)));
199 tim 281 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 tim 283 stack.push(comparatorInstruction(instruction));
207 tim 278 break;
208     default:
209     unrecognizedExpression();
210     }
211     }
212 tim 283 if (stack.size() != 1)
213 tim 278 evalError("atom expression compiler error - stack over/underflow");
214 tim 283
215     return stack.top();
216 tim 278 }
217    
218    
219    
220 gezelter 1390 OpenMDBitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) {
221 tim 278 int comparator = instruction.tok;
222     int property = instruction.intValue;
223 tim 283 float comparisonValue = boost::any_cast<float>(instruction.value);
224 gezelter 1390 OpenMDBitSet bs(nStuntDouble);
225 tim 295 bs.clearAll();
226 tim 283
227     SimInfo::MoleculeIterator mi;
228     Molecule* mol;
229     Molecule::AtomIterator ai;
230     Atom* atom;
231     Molecule::RigidBodyIterator rbIter;
232     RigidBody* rb;
233 tim 281
234 cli2 1364 for (mol = info->beginMolecule(mi); mol != NULL;
235     mol = info->nextMolecule(mi)) {
236    
237 gezelter 507 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
238     compareProperty(atom, bs, property, comparator, comparisonValue);
239     }
240 cli2 1364
241     for (rb = mol->beginRigidBody(rbIter); rb != NULL;
242     rb = mol->nextRigidBody(rbIter)) {
243     compareProperty(rb, bs, property, comparator, comparisonValue);
244     }
245 tim 278 }
246    
247 tim 283 return bs;
248 gezelter 507 }
249 tim 278
250 gezelter 1390 void SelectionEvaluator::compareProperty(StuntDouble* sd, OpenMDBitSet& bs,
251 cli2 1364 int property, int comparator,
252     float comparisonValue) {
253 tim 963 RealType propertyValue = 0.0;
254 gezelter 507 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 cli2 1360 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 gezelter 1629 case Token::r:
281     propertyValue = sd->getPos().length();
282     break;
283 gezelter 507 default:
284     unrecognizedAtomProperty(property);
285     }
286 tim 283
287 gezelter 507 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 cli2 1364
311 gezelter 507 }
312 tim 283
313 cli2 1364 void SelectionEvaluator::withinInstruction(const Token& instruction,
314 gezelter 1390 OpenMDBitSet& bs){
315 tim 295
316 tim 281 boost::any withinSpec = instruction.value;
317 tim 295 float distance;
318 tim 282 if (withinSpec.type() == typeid(float)){
319 gezelter 507 distance = boost::any_cast<float>(withinSpec);
320 tim 295 } else if (withinSpec.type() == typeid(int)) {
321 gezelter 507 distance = boost::any_cast<int>(withinSpec);
322 tim 295 } else {
323 gezelter 507 evalError("casting error in withinInstruction");
324     bs.clearAll();
325 tim 278 }
326 tim 282
327 tim 295 bs = distanceFinder.find(bs, distance);
328 gezelter 507 }
329 cli2 1364
330 gezelter 507 void SelectionEvaluator::define() {
331 tim 282 assert(statement.size() >= 3);
332 cli2 1364
333 tim 282 std::string variable = boost::any_cast<std::string>(statement[1].value);
334 cli2 1364
335     variables.insert(VariablesType::value_type(variable,
336     expression(statement, 2)));
337 gezelter 507 }
338 cli2 1364
339 tim 278
340 gezelter 507 /** @todo */
341     void SelectionEvaluator::predefine(const std::string& script) {
342 cli2 1364
343 tim 282 if (compiler.compile("#predefine", script)) {
344 gezelter 507 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 cli2 1364 if (tok == Token::identifier ||
354     (tok & Token::predefinedset) == Token::predefinedset) {
355 gezelter 507 std::string variable = boost::any_cast<std::string>(statement[1].value);
356     variables.insert(VariablesType::value_type(variable, statement));
357 cli2 1364
358 gezelter 507 } else {
359     evalError("invalid variable name:" + script);
360     }
361     }else {
362     evalError("bad predefinition length:" + script);
363 cli2 1364 }
364 tim 282
365     } else {
366 gezelter 507 evalError("predefined set compile error:" + script +
367     "\ncompile error:" + compiler.getErrorMessage());
368 tim 282 }
369 gezelter 507 }
370 tim 282
371 gezelter 1390 void SelectionEvaluator::select(OpenMDBitSet& bs){
372 tim 288 bs = expression(statement, 1);
373 gezelter 507 }
374 cli2 1364
375 gezelter 1390 OpenMDBitSet SelectionEvaluator::lookupValue(const std::string& variable){
376 cli2 1364
377 gezelter 1390 OpenMDBitSet bs(nStuntDouble);
378 tim 282 std::map<std::string, boost::any>::iterator i = variables.find(variable);
379 tim 295
380 tim 282 if (i != variables.end()) {
381 gezelter 1390 if (i->second.type() == typeid(OpenMDBitSet)) {
382     return boost::any_cast<OpenMDBitSet>(i->second);
383 gezelter 507 } 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 tim 283 } else {
389 gezelter 507 unrecognizedIdentifier(variable);
390 tim 282 }
391 cli2 1364
392 tim 295 return bs;
393 gezelter 507 }
394 cli2 1364
395 gezelter 1390 OpenMDBitSet SelectionEvaluator::nameInstruction(const std::string& name){
396 cli2 1364 return nameFinder.match(name);
397 gezelter 507 }
398 tim 283
399 gezelter 507 bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){
400 tim 288 std::vector<Token>::const_iterator i;
401     for (i = tokens.begin(); i != tokens.end(); ++i) {
402 gezelter 507 if (i->tok & Token::dynamic) {
403     return true;
404     }
405 tim 288 }
406 cli2 1364
407 tim 288 return false;
408 gezelter 507 }
409 tim 288
410 gezelter 507 void SelectionEvaluator::clearDefinitionsAndLoadPredefined() {
411 tim 288 variables.clear();
412     //load predefine
413     //predefine();
414 gezelter 507 }
415 tim 288
416 gezelter 1390 OpenMDBitSet SelectionEvaluator::evaluate() {
417     OpenMDBitSet bs(nStuntDouble);
418 tim 288 if (isLoaded_) {
419 gezelter 507 pc = 0;
420     instructionDispatchLoop(bs);
421 tim 288 }
422    
423     return bs;
424 gezelter 507 }
425 tim 295
426 gezelter 1390 OpenMDBitSet SelectionEvaluator::indexInstruction(const boost::any& value) {
427     OpenMDBitSet bs(nStuntDouble);
428 tim 295
429     if (value.type() == typeid(int)) {
430 gezelter 507 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 tim 295 } else if (value.type() == typeid(std::pair<int, int>)) {
437 gezelter 507 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 tim 295 }
445    
446     return bs;
447 gezelter 507 }
448 tim 295
449 gezelter 1803 OpenMDBitSet SelectionEvaluator::allInstruction() {
450     OpenMDBitSet bs(nStuntDouble);
451 tim 432
452 gezelter 1803 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 gezelter 1412 OpenMDBitSet SelectionEvaluator::hull() {
478     OpenMDBitSet bs(nStuntDouble);
479    
480     bs = hullFinder.findHull();
481    
482     return bs;
483     }
484    
485 tim 963 RealType SelectionEvaluator::getCharge(Atom* atom) {
486     RealType charge =0.0;
487 tim 432 AtomType* atomType = atom->getAtomType();
488     if (atomType->isCharge()) {
489 gezelter 507 GenericData* data = atomType->getPropertyByName("Charge");
490     if (data != NULL) {
491     DoubleGenericData* doubleData= dynamic_cast<DoubleGenericData*>(data);
492 tim 432
493 gezelter 507 if (doubleData != NULL) {
494     charge = doubleData->getData();
495 tim 432
496 gezelter 507 } else {
497     sprintf( painCave.errMsg,
498     "Can not cast GenericData to DoubleGenericData\n");
499 gezelter 1390 painCave.severity = OPENMD_ERROR;
500 gezelter 507 painCave.isFatal = 1;
501     simError();
502     }
503     }
504 tim 432 }
505    
506     return charge;
507 gezelter 507 }
508 tim 432
509     }

Properties

Name Value
svn:keywords Author Id Revision Date