OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#ifndef SELECTION_SELECTIONEVALUATOR_HPP
49#define SELECTION_SELECTIONEVALUATOR_HPP
50
51#include <any>
52#include <fstream>
53#include <map>
54#include <string>
55#include <vector>
56
57#include "brains/SimInfo.hpp"
59#include "selection/DistanceFinder.hpp"
60#include "selection/HullFinder.hpp"
61#include "selection/IndexFinder.hpp"
62#include "selection/NameFinder.hpp"
63#include "selection/SelectionCompiler.hpp"
64#include "selection/SelectionSet.hpp"
65#include "selection/SelectionToken.hpp"
66#include "utils/StringUtils.hpp"
67
68namespace OpenMD {
69
70 /**
71 * @class SelectionEvaluator SelectionEvaluator.hpp
72 * "selection/SelectionEvaluator"
73 * @brief Evalute the tokens compiled by SelectionCompiler and return a
74 * OpenMDBitSet
75 */
76 class SelectionEvaluator {
77 public:
78 SelectionEvaluator(SimInfo* info);
79
80 bool loadScriptString(const std::string& script);
81 bool loadScriptFile(const std::string& filename);
82
83 SelectionSet evaluate();
84 SelectionSet evaluate(int frame);
85
86 /**
87 * Tests if the result from evaluation of script is dynamic.
88 */
89 bool isDynamic() { return isDynamic_; }
90
91 bool hadRuntimeError() const { return error; }
92
93 std::string getErrorMessage() const { return errorMessage; }
94
95 int getLinenumber() { return linenumbers[pc]; }
96
97 std::string getLine() {
98 std::size_t ichBegin = lineIndices[pc];
99 std::size_t ichEnd;
100 if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos &&
101 (ichEnd = script.find('\n', ichBegin)) == std::string::npos) {
102 ichEnd = script.size();
103 }
104 return script.substr(ichBegin, ichEnd);
105 }
106 bool hasSurfaceArea() { return hasSurfaceArea_; }
107 RealType getSurfaceArea() {
108 if (hasSurfaceArea_) {
109 return surfaceArea_;
110 } else {
111 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
112 "SelectionEvaluator Error: %s\n", "No Surface Area For You!");
113 painCave.severity = OPENMD_ERROR;
114 painCave.isFatal = 1;
115 simError();
116 return 0.0;
117 }
118 }
119 bool hasVolume() { return hasVolume_; }
120 RealType getVolume() {
121 if (hasVolume_) {
122 return volume_;
123 } else {
124 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
125 "SelectionEvaluator Error: %s\n", "No Volume For You!");
126 painCave.severity = OPENMD_ERROR;
127 painCave.isFatal = 1;
128 simError();
129 return 0.0;
130 }
131 }
132
133 private:
134 void clearState();
135
136 bool loadScript(const std::string& filename, const std::string& script);
137
138 bool loadScriptFileInternal(const std::string& filename);
139
140 SelectionSet createSelectionSets();
141 void clearDefinitionsAndLoadPredefined();
142
143 void define();
144 void select(SelectionSet& bs);
145 void select(SelectionSet& bs, int frame);
146 void predefine(const std::string& script);
147
148 void instructionDispatchLoop(SelectionSet& bs);
149 void instructionDispatchLoop(SelectionSet& bs, int frame);
150
151 void withinInstruction(const Token& instruction, SelectionSet& bs);
152 void withinInstruction(const Token& instruction, SelectionSet& bs,
153 int frame);
154
155 SelectionSet alphaHullInstruction(const Token& instruction);
156 SelectionSet alphaHullInstruction(const Token& instruction, int frame);
157
158 SelectionSet allInstruction();
159
160 SelectionSet comparatorInstruction(const Token& instruction);
161 SelectionSet comparatorInstruction(const Token& instruction, int frame);
162 void compareProperty(StuntDouble* sd, SelectionSet& bs, int property,
163 int comparator, float comparisonValue);
164 void compareProperty(StuntDouble* sd, SelectionSet& bs, int property,
165 int comparator, float comparisonValue, int frame);
166 void compareProperty(Molecule* mol, SelectionSet& bs, int property,
167 int comparator, float comparisonValue);
168 void compareProperty(Molecule* mol, SelectionSet& bs, int property,
169 int comparator, float comparisonValue, int frame);
170 SelectionSet nameInstruction(const std::string& name);
171 SelectionSet indexInstruction(const std::any& value);
172 SelectionSet expression(const std::vector<Token>& tokens, int pc);
173 SelectionSet expression(const std::vector<Token>& tokens, int pc,
174 int frame);
175
176 SelectionSet lookupValue(const std::string& variable);
177
178 SelectionSet hull();
179 SelectionSet hull(int frame);
180
181 void evalError(const std::string& message) {
182 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
183 "SelectionEvaluator Error: %s\n", message.c_str());
184 painCave.severity = OPENMD_ERROR;
185 painCave.isFatal = 1;
186 simError();
187 }
188
189 void unrecognizedCommand(const Token& token) {
190 evalError("unrecognized command:" +
191 std::any_cast<std::string>(token.value));
192 }
193
194 void unrecognizedExpression() { evalError("unrecognized expression"); }
195
196 void unrecognizedAtomProperty(int) {
197 evalError("unrecognized atom property");
198 }
199
200 void unrecognizedMoleculeProperty(int) {
201 evalError("unrecognized molecule property");
202 }
203
204 void unrecognizedIdentifier(const std::string& identifier) {
205 evalError("unrecognized identifier:" + identifier);
206 }
207
208 void invalidIndexRange(std::pair<int, int> range) {
209 evalError("invalid index range: [" + toString(range.first) + ", " +
210 toString(range.second) + ")");
211 }
212
213 void invalidIndex(int index) {
214 evalError("invalid index : " + toString(index));
215 }
216
217 bool containDynamicToken(const std::vector<Token>& tokens);
218
219 RealType getCharge(Atom* atom);
220 RealType getCharge(Atom* atom, int frame);
221
222 SelectionCompiler compiler;
223
224 // const static int scriptLevelMax = 10;
225 // int scriptLevel;
226
227 // Context stack[scriptLevelMax];
228
229 std::string filename;
230 std::string script;
231 std::vector<int> linenumbers;
232 std::vector<int> lineIndices;
233 std::vector<std::vector<Token>> aatoken;
234 unsigned int pc; // program counter
235
236 bool error {false};
237 std::string errorMessage;
238
239 std::vector<Token> statement;
240 int statementLength;
241
242 SimInfo* info {nullptr};
243 NameFinder nameFinder;
244 DistanceFinder distanceFinder;
245 HullFinder hullFinder;
246 AlphaHullFinder alphaHullFinder;
247 IndexFinder indexFinder;
248 vector<int> nObjects;
249
250 using VariablesType = std::map<std::string, std::any>;
251 VariablesType variables;
252
253 bool isDynamic_ {false};
254 bool isLoaded_ {false};
255 bool hasSurfaceArea_ {false};
256 RealType surfaceArea_;
257 bool hasVolume_ {false};
258 RealType volume_;
259 };
260} // namespace OpenMD
261
262#endif
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:96
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.