OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ParamConstraint.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 IO_PARAMCONSTRAINT_HPP
46#define IO_PARAMCONSTRAINT_HPP
47
48#include <sstream>
49
50#include "utils/CaseConversion.hpp"
52
53namespace OpenMD {
54 /**
55 * This class allows to recognize constraint predicates, so that they can be
56 * combined using composition operators. Every constraint predicate must be
57 * derived from this class
58 */
59 template<typename Derived>
61 std::string getConstraintDescription() { return description_; }
62
63 protected:
64 std::string description_;
65 };
66
67 struct NotEmptyConstraint : public ParamConstraintFacade<NotEmptyConstraint> {
68 NotEmptyConstraint() { description_ = "nonempty"; }
69 bool operator()(const std::string& data) const { return !data.empty(); }
70 };
71
72 struct ZeroConstraint : public ParamConstraintFacade<ZeroConstraint> {
73 ZeroConstraint() { this->description_ = "zero"; }
74 template<typename DataType>
75 bool operator()(DataType data) const {
76 return data == 0;
77 }
78 };
79
80 struct NonZeroConstraint : public ParamConstraintFacade<NonZeroConstraint> {
81 NonZeroConstraint() { this->description_ = "nonzero"; }
82
83 template<typename DataType>
84 bool operator()(DataType data) const {
85 return data != 0;
86 }
87 };
88
89 struct PositiveConstraint : public ParamConstraintFacade<PositiveConstraint> {
90 PositiveConstraint() { this->description_ = "positive"; }
91 template<typename DataType>
92 bool operator()(DataType data) const {
93 return data > 0;
94 }
95 };
96
98 public ParamConstraintFacade<NonPositiveConstraint> {
99 NonPositiveConstraint() { this->description_ = "nonpositive"; }
100 template<typename DataType>
101 bool operator()(DataType data) const {
102 return data <= 0;
103 }
104 };
105
106 struct NegativeConstraint : public ParamConstraintFacade<NegativeConstraint> {
107 NegativeConstraint() { this->description_ = "negative"; }
108 template<typename DataType>
109 bool operator()(DataType data) const {
110 return data < 0;
111 }
112 };
113
115 public ParamConstraintFacade<NonNegativeConstraint> {
116 NonNegativeConstraint() { this->description_ = "nonnegative"; }
117 template<typename DataType>
118 bool operator()(DataType data) const {
119 return data >= 0;
120 }
121 };
122
123 struct EvenConstraint : public ParamConstraintFacade<EvenConstraint> {
124 EvenConstraint() { this->description_ = "even"; }
125 template<typename DataType>
126 bool operator()(DataType data) const {
127 return data % 2 == 0;
128 }
129 };
130
131 template<typename T>
133 public ParamConstraintFacade<LessThanConstraint<T>> {
134 LessThanConstraint(T rhs) : rhs_(rhs) {
135 std::stringstream iss;
136 iss << "less than " << rhs;
137 this->description_ = iss.str();
138 }
139 template<typename DataType>
140 bool operator()(DataType data) const {
141 return data < rhs_;
142 }
143
144 private:
145 T rhs_;
146 };
147
148 template<typename T>
150 public ParamConstraintFacade<LessThanOrEqualToConstraint<T>> {
151 LessThanOrEqualToConstraint(T rhs) : rhs_(rhs) {
152 std::stringstream iss;
153 iss << "less than or equal to" << rhs;
154 this->description_ = iss.str();
155 }
156
157 template<typename DataType>
158 bool operator()(DataType data) const {
159 return data <= rhs_;
160 }
161
162 private:
163 T rhs_;
164 };
165
166 template<typename T>
167 struct EqualConstraint : public ParamConstraintFacade<EqualConstraint<T>> {
168 EqualConstraint(T rhs) : rhs_(rhs) {
169 std::stringstream iss;
170 iss << "equal to" << rhs;
171 this->description_ = iss.str();
172 }
173 template<typename DataType>
174 bool operator()(DataType data) const {
175 return data == rhs_;
176 }
177
178 private:
179 T rhs_;
180 };
181
183 public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
184 EqualIgnoreCaseConstraint(std::string rhs) :
185 rhs_(OpenMD::toUpperCopy(rhs)) {
186 std::stringstream iss;
187 iss << "equal to (case insensitive) " << rhs;
188 this->description_ = iss.str();
189 }
190
191 bool operator()(std::string data) const {
192 return OpenMD::toUpperCopy(data) == rhs_;
193 }
194
195 private:
196 std::string rhs_;
197 };
198
200 public ParamConstraintFacade<EqualIgnoreCaseConstraint> {
201 ContainsConstraint(std::string rhs) : rhs_(OpenMD::toUpperCopy(rhs)) {
202 std::stringstream iss;
203 iss << "contains " << rhs;
204 this->description_ = iss.str();
205 }
206
207 bool operator()(std::string data) const {
208 OpenMD::StringTokenizer tokenizer(OpenMD::toUpperCopy(data),
209 " ,;|\t\n\r");
210 while (tokenizer.hasMoreTokens()) {
211 if (tokenizer.nextToken() == rhs_) { return true; }
212 }
213
214 return false;
215 }
216
217 private:
218 std::string rhs_;
219 };
220
221 template<typename T>
223 public ParamConstraintFacade<GreaterThanConstraint<T>> {
224 GreaterThanConstraint(T rhs) : rhs_(rhs) {
225 std::stringstream iss;
226 iss << "greater than" << rhs;
227 this->description_ = iss.str();
228 }
229 template<typename DataType>
230 bool operator()(DataType data) const {
231 return data > rhs_;
232 }
233
234 private:
235 T rhs_;
236 };
237
238 template<typename T>
240 public ParamConstraintFacade<GreaterThanOrEqualTo<T>> {
241 GreaterThanOrEqualTo(T rhs) : rhs_(rhs) {
242 std::stringstream iss;
243 iss << "greater than or equal to" << rhs;
244 this->description_ = iss.str();
245 }
246 template<typename DataType>
247 bool operator()(DataType data) const {
248 return data >= rhs_;
249 }
250
251 private:
252 T rhs_;
253 };
254
255 // class_and composition predicate
256 template<typename Cons1T, typename Cons2T>
258 public ParamConstraintFacade<AndParamConstraint<Cons1T, Cons2T>> {
259 public:
260 AndParamConstraint(Cons1T cons1, Cons2T cons2) :
261 cons1_(cons1), cons2_(cons2) {
262 std::stringstream iss;
263 iss << "(" << cons1_.getConstraintDescription() << " and "
264 << cons2_.getConstraintDescription() << ")";
265 this->description_ = iss.str();
266 }
267
268 template<typename DataType>
269 bool operator()(DataType data) const {
270 return cons1_(data) && cons2_(data);
271 }
272
273 private:
274 Cons1T cons1_;
275 Cons2T cons2_;
276 };
277
278 template<typename Cons1T, typename Cons2T>
280 public ParamConstraintFacade<OrParamConstraint<Cons1T, Cons2T>> {
281 public:
282 OrParamConstraint(Cons1T cons1, Cons2T cons2) :
283 cons1_(cons1), cons2_(cons2) {
284 std::stringstream iss;
285 iss << cons1_.getConstraintDescription() << " or "
286 << cons2_.getConstraintDescription() << "";
287 this->description_ = iss.str();
288 }
289
290 template<typename DataType>
291 bool operator()(DataType data) const {
292 return cons1_(data) || cons2_(data);
293 }
294
295 private:
296 Cons1T cons1_;
297 Cons2T cons2_;
298 };
299
300 template<typename ConsT>
302 public ParamConstraintFacade<NotParamConstraint<ConsT>> {
303 public:
304 NotParamConstraint(ConsT cons) : cons_(cons) {
305 std::stringstream iss;
306 iss << "(not" << cons_.getConstraintDescription() << ")";
307 this->description_ = iss.str();
308 }
309
310 template<typename DataType>
311 bool operator()(DataType data) const {
312 return !cons_(data);
313 }
314
315 private:
316 ConsT cons_;
317 };
318
319 template<typename Cons1T, typename Cons2T>
320 inline AndParamConstraint<Cons1T, Cons2T> operator&&(
322 const ParamConstraintFacade<Cons2T>& cons2) {
324 *static_cast<const Cons1T*>(&cons1),
325 *static_cast<const Cons2T*>(&cons2));
326 }
327
328 template<typename Cons1T, typename Cons2T>
329 inline OrParamConstraint<Cons1T, Cons2T> operator||(
330 const ParamConstraintFacade<Cons1T>& cons1,
331 const ParamConstraintFacade<Cons2T>& cons2) {
332 return OrParamConstraint<Cons1T, Cons2T>(
333 *static_cast<const Cons1T*>(&cons1),
334 *static_cast<const Cons2T*>(&cons2));
335 }
336
337 template<typename ConsT>
338 inline NotParamConstraint<ConsT> operator!(
339 const ParamConstraintFacade<ConsT>& cons) {
340 return NotParamConstraint<ConsT>(*static_cast<const ConsT*>(&cons));
341 }
342
343 NotEmptyConstraint isNotEmpty();
344 ZeroConstraint isZero();
345
346 ParamConstraintFacade<NonZeroConstraint> isNonZero();
347 PositiveConstraint isPositive();
348 NonPositiveConstraint isNonPositive();
349
350 NegativeConstraint isNegative();
351
352 NonNegativeConstraint isNonNegative();
353 EvenConstraint isEven();
354
355 template<typename T>
356 inline LessThanConstraint<T> isLessThan(T& v) {
357 return LessThanConstraint<T>(v);
358 }
359
360 template<typename T>
361 inline LessThanOrEqualToConstraint<T> isLessThanOrEqualTo(T& v) {
362 return LessThanOrEqualToConstraint<T>(v);
363 }
364
365 template<typename T>
366 inline EqualConstraint<T> isEqual(T& v) {
367 return EqualConstraint<T>(v);
368 }
369
370 template<typename T>
371 inline GreaterThanConstraint<T> isGreaterThan(T& v) {
372 return GreaterThanConstraint<T>(v);
373 }
374
375 template<typename T>
376 inline GreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T& v) {
377 return GreaterThanOrEqualTo<T>(v);
378 }
379
380 EqualIgnoreCaseConstraint isEqualIgnoreCase(std::string str);
381} // namespace OpenMD
382
383#endif
The string tokenizer class allows an application to break a string into tokens The set of delimiters ...
std::string nextToken()
Returns the next token from this string tokenizer.
bool hasMoreTokens()
Tests if there are more tokens available from this tokenizer's string.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
This class allows to recognize constraint predicates, so that they can be combined using composition ...