ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/SelectionEvaluator.cpp
Revision: 1627
Committed: Tue Sep 13 22:05:04 2011 UTC (13 years, 7 months ago) by gezelter
File size: 14135 byte(s)
Log Message:
Splitting out ifstrstream into a header and an implementation.  This
means that much of the code that depends on it can be compiled only
once and the parallel I/O is concentrated into a few files.  To do
this, a number of files that relied on basic_ifstrstream to load the
mpi header had to be modified to manage their own headers.


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     * [4] Vardeman & Gezelter, in progress (2009).
40 tim 277 */
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 gezelter 1627 #include "io/ifstrstream.hpp"
49 tim 283
50 gezelter 1390 namespace OpenMD {
51 tim 277
52    
53 gezelter 507 SelectionEvaluator::SelectionEvaluator(SimInfo* si)
54 gezelter 1412 : info(si), nameFinder(info), distanceFinder(info), hullFinder(info),
55     indexFinder(info),
56 cli2 1364 isLoaded_(false){
57     nStuntDouble = info->getNGlobalAtoms() + info->getNGlobalRigidBodies();
58 gezelter 507 }
59 tim 283
60 cli2 1364 bool SelectionEvaluator::loadScript(const std::string& filename,
61     const std::string& script) {
62 tim 288 clearDefinitionsAndLoadPredefined();
63 tim 278 this->filename = filename;
64     this->script = script;
65     if (! compiler.compile(filename, script)) {
66 gezelter 507 error = true;
67     errorMessage = compiler.getErrorMessage();
68 cli2 1364
69     sprintf( painCave.errMsg,
70     "SelectionCompiler Error: %s\n", errorMessage.c_str());
71 gezelter 1390 painCave.severity = OPENMD_ERROR;
72 cli2 1364 painCave.isFatal = 1;
73     simError();
74 gezelter 507 return false;
75 tim 278 }
76    
77     pc = 0;
78     aatoken = compiler.getAatokenCompiled();
79     linenumbers = compiler.getLineNumbers();
80     lineIndices = compiler.getLineIndices();
81 tim 288
82     std::vector<std::vector<Token> >::const_iterator i;
83    
84     isDynamic_ = false;
85     for (i = aatoken.begin(); i != aatoken.end(); ++i) {
86 gezelter 507 if (containDynamicToken(*i)) {
87     isDynamic_ = true;
88     break;
89     }
90 tim 288 }
91    
92     isLoaded_ = true;
93 tim 278 return true;
94 gezelter 507 }
95 tim 278
96 gezelter 507 void SelectionEvaluator::clearState() {
97 tim 278 error = false;
98 tim 281 errorMessage = "";
99 gezelter 507 }
100 tim 278
101 gezelter 507 bool SelectionEvaluator::loadScriptString(const std::string& script) {
102 tim 278 clearState();
103 tim 281 return loadScript("", script);
104 gezelter 507 }
105 tim 278
106 gezelter 507 bool SelectionEvaluator::loadScriptFile(const std::string& filename) {
107 tim 278 clearState();
108     return loadScriptFileInternal(filename);
109 gezelter 507 }
110 tim 278
111 cli2 1364 bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) {
112     ifstrstream ifs(filename.c_str());
113 tim 288 if (!ifs.is_open()) {
114 gezelter 507 return false;
115 tim 288 }
116 cli2 1364
117 tim 288 const int bufferSize = 65535;
118     char buffer[bufferSize];
119     std::string script;
120     while(ifs.getline(buffer, bufferSize)) {
121 gezelter 507 script += buffer;
122 tim 288 }
123     return loadScript(filename, script);
124 gezelter 507 }
125 cli2 1364
126 gezelter 1390 void SelectionEvaluator::instructionDispatchLoop(OpenMDBitSet& bs){
127 tim 288
128 tim 281 while ( pc < aatoken.size()) {
129 gezelter 507 statement = aatoken[pc++];
130     statementLength = statement.size();
131     Token token = statement[0];
132     switch (token.tok) {
133     case Token::define:
134     define();
135     break;
136     case Token::select:
137     select(bs);
138     break;
139     default:
140     unrecognizedCommand(token);
141     return;
142     }
143 tim 278 }
144 tim 288
145 gezelter 507 }
146 tim 278
147 gezelter 1390 OpenMDBitSet SelectionEvaluator::expression(const std::vector<Token>& code,
148 cli2 1364 int pcStart) {
149 gezelter 1390 OpenMDBitSet bs;
150     std::stack<OpenMDBitSet> stack;
151 cli2 1364
152 tim 288 for (int pc = pcStart; pc < code.size(); ++pc) {
153 tim 278 Token instruction = code[pc];
154 tim 282
155 tim 278 switch (instruction.tok) {
156 tim 281 case Token::expressionBegin:
157 tim 278 break;
158 tim 281 case Token::expressionEnd:
159 tim 282 break;
160 tim 281 case Token::all:
161 gezelter 1390 bs = OpenMDBitSet(nStuntDouble);
162 tim 283 bs.setAll();
163     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     float propertyValue;
225 gezelter 1390 OpenMDBitSet bs(nStuntDouble);
226 tim 295 bs.clearAll();
227 tim 283
228     SimInfo::MoleculeIterator mi;
229     Molecule* mol;
230     Molecule::AtomIterator ai;
231     Atom* atom;
232     Molecule::RigidBodyIterator rbIter;
233     RigidBody* rb;
234 tim 281
235 cli2 1364 for (mol = info->beginMolecule(mi); mol != NULL;
236     mol = info->nextMolecule(mi)) {
237    
238 gezelter 507 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
239     compareProperty(atom, bs, property, comparator, comparisonValue);
240     }
241 cli2 1364
242     for (rb = mol->beginRigidBody(rbIter); rb != NULL;
243     rb = mol->nextRigidBody(rbIter)) {
244     compareProperty(rb, bs, property, comparator, comparisonValue);
245     }
246 tim 278 }
247    
248 tim 283 return bs;
249 gezelter 507 }
250 tim 278
251 gezelter 1390 void SelectionEvaluator::compareProperty(StuntDouble* sd, OpenMDBitSet& bs,
252 cli2 1364 int property, int comparator,
253     float comparisonValue) {
254 tim 963 RealType propertyValue = 0.0;
255 gezelter 507 switch (property) {
256     case Token::mass:
257     propertyValue = sd->getMass();
258     break;
259     case Token::charge:
260     if (sd->isAtom()){
261     Atom* atom = static_cast<Atom*>(sd);
262     propertyValue = getCharge(atom);
263     } else if (sd->isRigidBody()) {
264     RigidBody* rb = static_cast<RigidBody*>(sd);
265     RigidBody::AtomIterator ai;
266     Atom* atom;
267     for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) {
268     propertyValue+= getCharge(atom);
269     }
270     }
271     break;
272 cli2 1360 case Token::x:
273     propertyValue = sd->getPos().x();
274     break;
275     case Token::y:
276     propertyValue = sd->getPos().y();
277     break;
278     case Token::z:
279     propertyValue = sd->getPos().z();
280     break;
281 gezelter 507 default:
282     unrecognizedAtomProperty(property);
283     }
284 tim 283
285 gezelter 507 bool match = false;
286     switch (comparator) {
287     case Token::opLT:
288     match = propertyValue < comparisonValue;
289     break;
290     case Token::opLE:
291     match = propertyValue <= comparisonValue;
292     break;
293     case Token::opGE:
294     match = propertyValue >= comparisonValue;
295     break;
296     case Token::opGT:
297     match = propertyValue > comparisonValue;
298     break;
299     case Token::opEQ:
300     match = propertyValue == comparisonValue;
301     break;
302     case Token::opNE:
303     match = propertyValue != comparisonValue;
304     break;
305     }
306     if (match)
307     bs.setBitOn(sd->getGlobalIndex());
308 cli2 1364
309 gezelter 507 }
310 tim 283
311 cli2 1364 void SelectionEvaluator::withinInstruction(const Token& instruction,
312 gezelter 1390 OpenMDBitSet& bs){
313 tim 295
314 tim 281 boost::any withinSpec = instruction.value;
315 tim 295 float distance;
316 tim 282 if (withinSpec.type() == typeid(float)){
317 gezelter 507 distance = boost::any_cast<float>(withinSpec);
318 tim 295 } else if (withinSpec.type() == typeid(int)) {
319 gezelter 507 distance = boost::any_cast<int>(withinSpec);
320 tim 295 } else {
321 gezelter 507 evalError("casting error in withinInstruction");
322     bs.clearAll();
323 tim 278 }
324 tim 282
325 tim 295 bs = distanceFinder.find(bs, distance);
326 gezelter 507 }
327 cli2 1364
328 gezelter 507 void SelectionEvaluator::define() {
329 tim 282 assert(statement.size() >= 3);
330 cli2 1364
331 tim 282 std::string variable = boost::any_cast<std::string>(statement[1].value);
332 cli2 1364
333     variables.insert(VariablesType::value_type(variable,
334     expression(statement, 2)));
335 gezelter 507 }
336 cli2 1364
337 tim 278
338 gezelter 507 /** @todo */
339     void SelectionEvaluator::predefine(const std::string& script) {
340 cli2 1364
341 tim 282 if (compiler.compile("#predefine", script)) {
342 gezelter 507 std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled();
343     if (aatoken.size() != 1) {
344     evalError("predefinition does not have exactly 1 command:"
345     + script);
346     return;
347     }
348     std::vector<Token> statement = aatoken[0];
349     if (statement.size() > 2) {
350     int tok = statement[1].tok;
351 cli2 1364 if (tok == Token::identifier ||
352     (tok & Token::predefinedset) == Token::predefinedset) {
353 gezelter 507 std::string variable = boost::any_cast<std::string>(statement[1].value);
354     variables.insert(VariablesType::value_type(variable, statement));
355 cli2 1364
356 gezelter 507 } else {
357     evalError("invalid variable name:" + script);
358     }
359     }else {
360     evalError("bad predefinition length:" + script);
361 cli2 1364 }
362 tim 282
363     } else {
364 gezelter 507 evalError("predefined set compile error:" + script +
365     "\ncompile error:" + compiler.getErrorMessage());
366 tim 282 }
367 gezelter 507 }
368 tim 282
369 gezelter 1390 void SelectionEvaluator::select(OpenMDBitSet& bs){
370 tim 288 bs = expression(statement, 1);
371 gezelter 507 }
372 cli2 1364
373 gezelter 1390 OpenMDBitSet SelectionEvaluator::lookupValue(const std::string& variable){
374 cli2 1364
375 gezelter 1390 OpenMDBitSet bs(nStuntDouble);
376 tim 282 std::map<std::string, boost::any>::iterator i = variables.find(variable);
377 tim 295
378 tim 282 if (i != variables.end()) {
379 gezelter 1390 if (i->second.type() == typeid(OpenMDBitSet)) {
380     return boost::any_cast<OpenMDBitSet>(i->second);
381 gezelter 507 } else if (i->second.type() == typeid(std::vector<Token>)){
382     bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2);
383     i->second = bs; /**@todo fixme */
384     return bs;
385     }
386 tim 283 } else {
387 gezelter 507 unrecognizedIdentifier(variable);
388 tim 282 }
389 cli2 1364
390 tim 295 return bs;
391 gezelter 507 }
392 cli2 1364
393 gezelter 1390 OpenMDBitSet SelectionEvaluator::nameInstruction(const std::string& name){
394 cli2 1364 return nameFinder.match(name);
395 gezelter 507 }
396 tim 283
397 gezelter 507 bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){
398 tim 288 std::vector<Token>::const_iterator i;
399     for (i = tokens.begin(); i != tokens.end(); ++i) {
400 gezelter 507 if (i->tok & Token::dynamic) {
401     return true;
402     }
403 tim 288 }
404 cli2 1364
405 tim 288 return false;
406 gezelter 507 }
407 tim 288
408 gezelter 507 void SelectionEvaluator::clearDefinitionsAndLoadPredefined() {
409 tim 288 variables.clear();
410     //load predefine
411     //predefine();
412 gezelter 507 }
413 tim 288
414 gezelter 1390 OpenMDBitSet SelectionEvaluator::evaluate() {
415     OpenMDBitSet bs(nStuntDouble);
416 tim 288 if (isLoaded_) {
417 gezelter 507 pc = 0;
418     instructionDispatchLoop(bs);
419 tim 288 }
420    
421     return bs;
422 gezelter 507 }
423 tim 295
424 gezelter 1390 OpenMDBitSet SelectionEvaluator::indexInstruction(const boost::any& value) {
425     OpenMDBitSet bs(nStuntDouble);
426 tim 295
427     if (value.type() == typeid(int)) {
428 gezelter 507 int index = boost::any_cast<int>(value);
429     if (index < 0 || index >= bs.size()) {
430     invalidIndex(index);
431     } else {
432     bs = indexFinder.find(index);
433     }
434 tim 295 } else if (value.type() == typeid(std::pair<int, int>)) {
435 gezelter 507 std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value);
436     assert(indexRange.first <= indexRange.second);
437     if (indexRange.first < 0 || indexRange.second >= bs.size()) {
438     invalidIndexRange(indexRange);
439     }else {
440     bs = indexFinder.find(indexRange.first, indexRange.second);
441     }
442 tim 295 }
443    
444     return bs;
445 gezelter 507 }
446 tim 295
447 tim 432
448 gezelter 1412 OpenMDBitSet SelectionEvaluator::hull() {
449     OpenMDBitSet bs(nStuntDouble);
450    
451     bs = hullFinder.findHull();
452    
453     return bs;
454     }
455    
456    
457    
458 tim 963 RealType SelectionEvaluator::getCharge(Atom* atom) {
459     RealType charge =0.0;
460 tim 432 AtomType* atomType = atom->getAtomType();
461     if (atomType->isCharge()) {
462 gezelter 507 GenericData* data = atomType->getPropertyByName("Charge");
463     if (data != NULL) {
464     DoubleGenericData* doubleData= dynamic_cast<DoubleGenericData*>(data);
465 tim 432
466 gezelter 507 if (doubleData != NULL) {
467     charge = doubleData->getData();
468 tim 432
469 gezelter 507 } else {
470     sprintf( painCave.errMsg,
471     "Can not cast GenericData to DoubleGenericData\n");
472 gezelter 1390 painCave.severity = OPENMD_ERROR;
473 gezelter 507 painCave.isFatal = 1;
474     simError();
475     }
476     }
477 tim 432 }
478    
479     return charge;
480 gezelter 507 }
481 tim 432
482     }

Properties

Name Value
svn:keywords Author Id Revision Date