OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SelectionEvaluator.hpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#ifndef SELECTION_SELECTIONEVALUATOR_HPP
46#define SELECTION_SELECTIONEVALUATOR_HPP
47
48#include <any>
49#include <fstream>
50#include <map>
51#include <string>
52#include <vector>
53
54#include "brains/SimInfo.hpp"
56#include "selection/DistanceFinder.hpp"
57#include "selection/HullFinder.hpp"
58#include "selection/IndexFinder.hpp"
59#include "selection/NameFinder.hpp"
60#include "selection/SelectionCompiler.hpp"
61#include "selection/SelectionSet.hpp"
62#include "selection/SelectionToken.hpp"
63#include "utils/StringUtils.hpp"
64
65namespace OpenMD {
66
67 /**
68 * @class SelectionEvaluator SelectionEvaluator.hpp
69 * "selection/SelectionEvaluator"
70 * @brief Evalute the tokens compiled by SelectionCompiler and return a
71 * OpenMDBitSet
72 */
74 public:
76
77 bool loadScriptString(const std::string& script);
78 bool loadScriptFile(const std::string& filename);
79
80 SelectionSet evaluate();
81 SelectionSet evaluate(int frame);
82
83 /**
84 * Tests if the result from evaluation of script is dynamic.
85 */
86 bool isDynamic() { return isDynamic_; }
87
88 bool hadRuntimeError() const { return error; }
89
90 std::string getErrorMessage() const { return errorMessage; }
91
92 int getLinenumber() { return linenumbers[pc]; }
93
94 std::string getLine() {
95 std::size_t ichBegin = lineIndices[pc];
96 std::size_t ichEnd;
97 if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos &&
98 (ichEnd = script.find('\n', ichBegin)) == std::string::npos) {
99 ichEnd = script.size();
100 }
101 return script.substr(ichBegin, ichEnd);
102 }
103 bool hasSurfaceArea() { return hasSurfaceArea_; }
104 RealType getSurfaceArea() {
105 if (hasSurfaceArea_) {
106 return surfaceArea_;
107 } else {
108 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
109 "SelectionEvaluator Error: %s\n", "No Surface Area For You!");
110 painCave.severity = OPENMD_ERROR;
111 painCave.isFatal = 1;
112 simError();
113 return 0.0;
114 }
115 }
116 bool hasVolume() { return hasVolume_; }
117 RealType getVolume() {
118 if (hasVolume_) {
119 return volume_;
120 } else {
121 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
122 "SelectionEvaluator Error: %s\n", "No Volume For You!");
123 painCave.severity = OPENMD_ERROR;
124 painCave.isFatal = 1;
125 simError();
126 return 0.0;
127 }
128 }
129
130 private:
131 void clearState();
132
133 bool loadScript(const std::string& filename, const std::string& script);
134
135 bool loadScriptFileInternal(const std::string& filename);
136
137 SelectionSet createSelectionSets();
138 void clearDefinitionsAndLoadPredefined();
139
140 void define();
141 void select(SelectionSet& bs);
142 void select(SelectionSet& bs, int frame);
143 void predefine(const std::string& script);
144
145 void instructionDispatchLoop(SelectionSet& bs);
146 void instructionDispatchLoop(SelectionSet& bs, int frame);
147
148 void withinInstruction(const Token& instruction, SelectionSet& bs);
149 void withinInstruction(const Token& instruction, SelectionSet& bs,
150 int frame);
151
152 SelectionSet alphaHullInstruction(const Token& instruction);
153 SelectionSet alphaHullInstruction(const Token& instruction, int frame);
154
155 SelectionSet allInstruction();
156
157 SelectionSet comparatorInstruction(const Token& instruction);
158 SelectionSet comparatorInstruction(const Token& instruction, int frame);
159 void compareProperty(StuntDouble* sd, SelectionSet& bs, int property,
160 int comparator, float comparisonValue);
161 void compareProperty(StuntDouble* sd, SelectionSet& bs, int property,
162 int comparator, float comparisonValue, int frame);
163 void compareProperty(Molecule* mol, SelectionSet& bs, int property,
164 int comparator, float comparisonValue);
165 void compareProperty(Molecule* mol, SelectionSet& bs, int property,
166 int comparator, float comparisonValue, int frame);
167 SelectionSet nameInstruction(const std::string& name);
168 SelectionSet indexInstruction(const std::any& value);
169 SelectionSet expression(const std::vector<Token>& tokens, int pc);
170 SelectionSet expression(const std::vector<Token>& tokens, int pc,
171 int frame);
172
173 SelectionSet lookupValue(const std::string& variable);
174
175 SelectionSet hull();
176 SelectionSet hull(int frame);
177
178 void evalError(const std::string& message) {
179 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
180 "SelectionEvaluator Error: %s\n", message.c_str());
181 painCave.severity = OPENMD_ERROR;
182 painCave.isFatal = 1;
183 simError();
184 }
185
186 void unrecognizedCommand(const Token& token) {
187 evalError("unrecognized command:" +
188 std::any_cast<std::string>(token.value));
189 }
190
191 void unrecognizedExpression() { evalError("unrecognized expression"); }
192
193 void unrecognizedAtomProperty(int) {
194 evalError("unrecognized atom property");
195 }
196
197 void unrecognizedMoleculeProperty(int) {
198 evalError("unrecognized molecule property");
199 }
200
201 void unrecognizedIdentifier(const std::string& identifier) {
202 evalError("unrecognized identifier:" + identifier);
203 }
204
205 void invalidIndexRange(std::pair<int, int> range) {
206 evalError("invalid index range: [" + toString(range.first) + ", " +
207 toString(range.second) + ")");
208 }
209
210 void invalidIndex(int index) {
211 evalError("invalid index : " + toString(index));
212 }
213
214 bool containDynamicToken(const std::vector<Token>& tokens);
215
216 RealType getCharge(Atom* atom);
217 RealType getCharge(Atom* atom, int frame);
218
219 SelectionCompiler compiler;
220
221 // const static int scriptLevelMax = 10;
222 // int scriptLevel;
223
224 // Context stack[scriptLevelMax];
225
226 std::string filename;
227 std::string script;
228 std::vector<int> linenumbers;
229 std::vector<int> lineIndices;
230 std::vector<std::vector<Token>> aatoken;
231 unsigned int pc; // program counter
232
233 bool error {false};
234 std::string errorMessage;
235
236 std::vector<Token> statement;
237 int statementLength;
238
239 SimInfo* info {nullptr};
240 NameFinder nameFinder;
241 DistanceFinder distanceFinder;
242 HullFinder hullFinder;
243 AlphaHullFinder alphaHullFinder;
244 IndexFinder indexFinder;
245 vector<int> nObjects;
246
247 using VariablesType = std::map<std::string, std::any>;
248 VariablesType variables;
249
250 bool isDynamic_ {false};
251 bool isLoaded_ {false};
252 bool hasSurfaceArea_ {false};
253 RealType surfaceArea_;
254 bool hasVolume_ {false};
255 RealType volume_;
256 };
257} // namespace OpenMD
258
259#endif
"selection/SelectionEvaluator"
bool isDynamic()
Tests if the result from evaluation of script is dynamic.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:93
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string toString(const T &v)
Convert a variable to a string.