AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
Tokenizer_muparser.hpp
Go to the documentation of this file.
1/*
2 * This file is part of AQUAgpusph, a free CFD program based on SPH.
3 * Copyright (C) 2012 Jose Luis Cercos Pita <jl.cercos@upm.es>
4 *
5 * AQUAgpusph is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * AQUAgpusph is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with AQUAgpusph. If not, see <http://www.gnu.org/licenses/>.
17 */
18
23
24#ifndef TOKENIZER_H_INCLUDED
25#define TOKENIZER_H_INCLUDED
26
27#include <map>
28#include <string>
29#include <muParser.h>
30#include <mutex>
33
34namespace Aqua {
35
45{
46 public:
49
52
64 template<typename T>
65 bool registerVariable(const std::string name, T value)
66 {
67 const std::lock_guard<std::mutex> lock(this->mutex);
68 bool overwritten = false;
69 if (isVariable(name))
70 overwritten = true;
71 p.DefineConst(name, (mu::value_type)value);
72 return overwritten;
73 }
74
79 std::vector<std::string> exprVariables(const std::string eq);
80
86 template<typename T=float>
87 T solve(const std::string eq)
88 {
89 const std::lock_guard<std::mutex> lock(this->mutex);
90 T res;
91
92 // First try straight number conversions
93 size_t sz;
94 try {
95 res = narrow_cast<T>((int64_t)std::stoll(eq, &sz));
96 if (sz == eq.size()) {
97 return res;
98 }
99 } catch (...) {
100 }
101 try {
102 std::string::size_type sz;
103 res = narrow_cast<T>(std::stod(eq, &sz));
104 if (sz == eq.size()) {
105 return res;
106 }
107 } catch (...) {
108 }
109
110 // No way, let's evaluate it as an expression
111 mu::value_type res_org;
112 p.SetExpr(eq);
113 try {
114 res_org = p.Eval();
115 res = narrow_cast<T>(res_org);
116 } catch (mu::Parser::exception_type& e) {
117 std::ostringstream msg;
118 msg << "Error evaluating \"" << e.GetExpr() << "\"" << std::endl;
119 msg << "input = \"" << eq << "\"" << std::endl;
120 LOG(L_WARNING, msg.str());
121 msg.str("");
122 msg << "\t" << e.GetMsg() << std::endl;
123 LOG0(L_DEBUG, msg.str());
124 msg.str("");
125 msg << "\tToken " << e.GetToken() << " in position " << e.GetPos()
126 << std::endl;
127 LOG0(L_DEBUG, msg.str());
128 throw;
129 } catch(std::out_of_range&) {
130 LOG(L_ERROR, std::string("Error evaluating \"") + eq + "\":\n");
131 LOG0(L_DEBUG, "\n");
132 LOG0(L_DEBUG, std::string("The result ") +
133 std::to_string(res_org) + " overflows the type \"" +
134 typeid(res).name() + "\"\n");
135 LOG0(L_DEBUG, "\n");
136 throw;
137 }
138
139 return res;
140 }
141
142 protected:
148 bool isVariable(const std::string name);
149
155 template<typename T=float>
156 T variable(const std::string name)
157 {
158 if (!isVariable(name))
159 return 0.f;
160 mu::valmap_type cmap = p.GetConst();
161 return narrow_cast<T>(cmap[name.c_str()]);
162 }
163
164 private:
166 mu::Parser q;
168 mu::Parser p;
169
171 static std::mutex mutex;
172}; // class Tokenizer
173
174} // Aqua::
175
176#endif // TOKENIZER_H_INCLUDED
Set of auxiliar functions.
Terminal output, with Log automatic copying. (See Aqua::InputOutput::Logger for details)
#define LOG0(level, log)
Definition Logger.hpp:64
#define LOG(level, log)
Definition Logger.hpp:55
#define T
Definition Sort.hcl.in:83
bool isVariable(const std::string name)
Checks if a variable has been registered.
Definition Tokenizer_muparser.cpp:113
T variable(const std::string name)
Returns a variable value.
Definition Tokenizer_muparser.hpp:156
T solve(const std::string eq)
Solve a math expression.
Definition Tokenizer_muparser.hpp:87
bool registerVariable(const std::string name, T value)
Register a variable.
Definition Tokenizer_muparser.hpp:65
Tokenizer_muparser()
Constructor.
Definition Tokenizer_muparser.cpp:53
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
Tdst narrow_cast(Torg v)
Cast a value, checking that it is not overflowing.
Definition AuxiliarMethods.hpp:414
@ L_ERROR
Definition Logger.hpp:75
@ L_WARNING
Definition Logger.hpp:74
@ L_DEBUG
Definition Logger.hpp:72
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition sphPrerequisites.hpp:65