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