TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Parser.h
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Parser_included
17#define Parser_included
18
19#include <UnaryFunction.h>
20#include <TRUST_Deriv.h>
21#include <TRUST_List.h>
22#include <Constante.h>
23#include <algorithm>
24#include <Stack.h>
25#include <math.h>
26#include <string>
27#include <Noms.h>
28#include <TRUSTArray.h>
29
30class StringTokenizer;
31// FUNCTION start at 1, cause 0 is needed
32enum class FUNCTION { SIN=1, ASIN, COS, ACOS, TAN, ATAN, LN, EXP, SQRT, ENT, ERF, RND, COSH, SINH, TANH, ATANH, NOT, ABS, SGN };
33/*! @brief Representation des donnees de la classe Parser
34 *
35 * @sa =
36 */
37
38class Parser
39{
40public :
41
42 /**
43 * Initialise le parser avec une chaine "0" : ne sert a rien !!
44 */
45 Parser();
46 Parser(const Parser&);
47 virtual ~Parser();
48 void set(const Parser&);
49
50 /**
51 * Construit un objet Parser avec une chaine specifiee et un nb max de variables a indiquer avec la methode addVar.
52 */
53 Parser(std::string&,int n =1);
54
55 /**
56 *
57 */
58 void init_parser();
59
60 /**
61 * Construit l'arbre correspondant a la chaine de caracteres. Cet arbre doit etre construit une seule fois et la chaine de caractere est evaluee en parcourant cet arbre par la methode eval() autant de fois qu'on le souhaite.
62 */
63 virtual void parseString();
64
65 /**
66 * Sert a evaluer l'expression mathematique correspondante a la chaine de caracteres. Poru cela il faut avant toute chose construire l'arbre par la methode parseString().
67 */
68 inline double eval() { return eval(PNodes[0]); }
69
70 /**
71 * Fixe la valeur de la variable representee par une chaine sv.
72 */
73 inline void setVar(const char* sv, double val) { setVar(searchVar(sv),val); }
74
75 /**
76 * Fixe la valeur de la variable representee par v.
77 */
78 inline void setVar(const std::string& v, double val) { setVar(searchVar(v),val); }
79
80 /**
81 * Fixe la valeur de la variable de numero specifie. Ce numero correspondt a l'ordre de l'ajout des variables par la methode addVar().
82 */
83 inline void setVar(int i, double val)
84 {
85 assert(i>-1 && i<ivar);
86 les_var[i] = val;
87 }
88
89 /**
90 * Fixe le nombre max de variables a indiquer avec la methode addVar.
91 */
92 virtual void setNbVar(int nvar);
93
94 /**
95 * permet d'ajouter une variable en specifiant sa chaine representative (par ex. : x, y1 etc...)
96 */
97 void addVar(const char *);
98
99 inline int getmaxVar() { return maxvar; }
100 inline int getNbVar() { return ivar; }
101 inline std::string& getString() { return str; }
102 inline void setString(const std::string& s)
103 {
104 str = s;
105 }
106 void addCst(const Constante& cst);
107
108 /**
109 * Fixe le temps initial et la periode de la fonction impulsion
110 */
111 inline void setImpulsion(double tinit, double periode)
112 {
113 impuls_t0 = tinit;
114 impuls_T = periode;
115 impuls_tn = tinit-periode;
117 }
118
119protected:
120 int test_op_binaire(int type);
121
122 static int precedence(int);
123 inline double eval(const PNodePod& node)
124 {
125 double x,y;
126 switch(node.type)
127 {
128 case 1 :
129 x = (node.left != -1 ? eval(PNodes[node.left]) : 0);
130 y = (node.right != -1 ? eval(PNodes[node.right]) : 0);
131 return evalOp(node, x, y); // PNode_type::OP
132 case 2 :
133 return node.nvalue; // PNode_type::VALUE
134 case 3 :
135 x = node.value<=0 ? eval(PNodes[node.left]) : 0;
136 return evalFunc(node, x);// PNode_type::FUNCTION
137 case 4 :
138 return les_var[node.value]; // PNode_type::VAR
139 default:
140 Process::exit("method eval : Unknown type for this node !!!");
141 return 0;
142 }
143 }
144 KOKKOS_INLINE_FUNCTION double evalOp(const PNodePod& node, double x, double y);
145 KOKKOS_INLINE_FUNCTION double evalFunc(const PNodePod& node, double x);
146 void parserState0(StringTokenizer*,PSTACK(PNode)* ,STACK(int)*);
147 void parserState1(StringTokenizer*,PSTACK(PNode)* ,STACK(int)*);
148 void parserState2(StringTokenizer*,PSTACK(PNode)* ,STACK(int)*);
149 inline int searchVar(const std::string& s) { return searchVar(s.c_str()); }
150 inline int searchVar(const char*);
151 int searchCst(const std::string& v);
152 int searchFunc(const std::string& v);
153 int state;
155 double impuls_T;
156 double impuls_t0;
157 double impuls_tn;
159
160 PNode* root; // Liste chainee de PNode
161 std::vector<PNodePod> PNodes; // Vecteur de PNodePod
162 std::string str;
163 ArrOfDouble les_var;
165 LIST(Constante) les_cst;
166 std::map<std::string, int> map_function_;
168};
169
170inline int Parser::searchVar(const char * sv)
171{
172 std::string s(sv);
173 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
174 return les_var_names.rang(s.c_str());
175}
176
177KOKKOS_INLINE_FUNCTION double Parser::evalFunc(const PNodePod& node, double x)
178{
179 /* OC : Nouvelle version : */
180 if (node.value<=0)
181 {
182 int unary_function = -node.value-1; // OC attention, dans node->value c est l'oppose de l'indice de la func dans la liste
183 // afin de distinguer operateur binaire (>0) et fonctions unaires (<0>
184 // Il est donc necessaire de prendre -node->value ici pour referencer un element de la liste
185 // De plus, on rajoute +1 car le zero ne doit pas etre utiliser pour les fonctions
186 switch (unary_function)
187 {
188 case static_cast<int>(FUNCTION::SIN):
189 return sin(x);
190 case static_cast<int>(FUNCTION::ASIN):
191 return asin(x);
192 case static_cast<int>(FUNCTION::COS):
193 return cos(x);
194 case static_cast<int>(FUNCTION::ACOS):
195 return acos(x);
196 case static_cast<int>(FUNCTION::TAN):
197 return tan(x);
198 case static_cast<int>(FUNCTION::ATAN):
199 return atan(x);
200 case static_cast<int>(FUNCTION::LN):
201 if (x <= 0)
202 Process::Kokkos_exit("Negative value x for LN(x) function used.\nCheck your data file.");
203 return log(x);
204 case static_cast<int>(FUNCTION::EXP):
205 return exp(x);
206 case static_cast<int>(FUNCTION::SQRT):
207 if (x < 0)
208 Process::Kokkos_exit("Negative value x for SQRT(x) function used.\nCheck your data file.");
209 return sqrt(x);
210 case static_cast<int>(FUNCTION::ENT):
211 return (int) x;
212 case static_cast<int>(FUNCTION::ERF):
213#ifndef MICROSOFT
214 return erf(x);
215#else
216 Process::exit("erf(x) fonction not implemented on Windows version.");
217 return eval(x);
218#endif
219 case static_cast<int>(FUNCTION::RND):
220#ifdef TRUST_USE_GPU
221 // ToDo Kokkos: cuRAND and hipRAND
222 Process::Kokkos_exit("Rnd function is not available yet for TRUST GPU version.");
223 return 0;
224#else
225 return x*drand48();
226#endif
227 case static_cast<int>(FUNCTION::COSH):
228 return cosh(x);
229 case static_cast<int>(FUNCTION::SINH):
230 return sinh(x);
231 case static_cast<int>(FUNCTION::TANH):
232 return tanh(x);
233 case static_cast<int>(FUNCTION::ATANH):
234 return atanh(x);
235 case static_cast<int>(FUNCTION::NOT):
236 if (x == 0) return 1;
237 else return 0;
238 case static_cast<int>(FUNCTION::ABS):
239 return std::fabs(x);
240 case static_cast<int>(FUNCTION::SGN):
241 return (x > 0) - (x < 0);
242 default:
243 Process::Kokkos_exit("method evalFunc : Unknown function for this node !!!");
244 return 0;
245 }
246 }
247 else
248 {
249 Process::Kokkos_exit("method evalFunc : Unknown func !!!");
250 return -1;
251 }
252}
253
254// Ne pas inliner car sinon Parser::eval(PNode* node) plus souvent appelee encore
255// ne sera peut etre pas inlinee...
256KOKKOS_INLINE_FUNCTION
257double Parser::evalOp(const PNodePod& node, double x, double y)
258{
259 switch (node.value)
260 {
261 case 0: // ADD
262 return x + y;
263 case 1: // SUBTRACT
264 return x - y;
265 case 2: // MULTIPLY
266 return x * y;
267 case 3: // DIVIDE
268 if (y==0)
269 {
270 Process::Kokkos_exit("Error in the Parser: x/y calculated with y equals 0. You are using a formulae with a division per 0.");
271 }
272 return x/y;
273 case 4: // POWER
274 if (y != (int)(y) && x<0)
275 {
276#ifdef TRUST_USE_GPU
277 Process::Kokkos_exit("Error in the Parser: x^y calculated with negative value for x !");
278#else
279 Cerr << "Error in the Parser: x^y calculated with negative value for x (x = " << x << ") and y real y (y = " << y << " )" << finl;
281#endif
282 }
283 return pow(x,y);
284 case 5: // LT
285 return (x<y)?1:0;
286 case 6: // GT
287 return (x>y)?1:0;
288 case 7: // LE
289 return (x<=y)?1:0;
290 case 8: // GE
291 return (x>=y)?1:0;
292 case 9: // MOD
293 return ((int)(x))%((int)(y));
294 case 10: // MAX
295 return (x>y)?x:y;
296 case 11: // MIN
297 return (x<y)?x:y;
298 case 12: // AND
299 return x && y;
300 case 13: // OR
301 return x || y;
302 case 14: // EQ
303 return (x == y);
304 case 15: // NEQ
305 return (x != y);
306 default:
307#ifdef TRUST_USE_GPU
308 Process::Kokkos_exit("Method evalOp : Unknown operation during expression parsing!");
309#else
310 Cerr << "Method evalOp : Unknown op " << (int)node.value << "!!!" << finl;
312#endif
313 return 0;
314 }
315}
316#endif
Definit une constante dans le jeu de donnees.
Definition Constante.h:27
Un tableau de chaine de caracteres (VECT(Nom)).
Definition Noms.h:26
int right
Definition PNode.h:50
int value
Definition PNode.h:52
double nvalue
Definition PNode.h:53
int left
Definition PNode.h:49
int type
Definition PNode.h:51
Definition PNode.h:21
void addVar(const char *)
Definition Parser.cpp:565
Noms les_var_names
Definition Parser.h:164
Parser()
Definition Parser.cpp:25
int ivar
Definition Parser.h:167
virtual void setNbVar(int nvar)
Definition Parser.cpp:116
int getmaxVar()
Definition Parser.h:99
std::vector< PNodePod > PNodes
Definition Parser.h:161
double impuls_t0
Definition Parser.h:156
ArrOfDouble les_var
Definition Parser.h:163
double eval(const PNodePod &node)
Definition Parser.h:123
Constante c_pi
Definition Parser.h:154
virtual ~Parser()
Definition Parser.cpp:111
LIST(Constante) les_cst
KOKKOS_INLINE_FUNCTION double evalFunc(const PNodePod &node, double x)
Definition Parser.h:177
std::string str
Definition Parser.h:162
int searchVar(const std::string &s)
Definition Parser.h:149
int maxvar
Definition Parser.h:167
void init_parser()
Definition Parser.cpp:73
void setImpulsion(double tinit, double periode)
Definition Parser.h:111
void setVar(const char *sv, double val)
Definition Parser.h:73
double impuls_T
Definition Parser.h:155
std::string & getString()
Definition Parser.h:101
PNode * root
Definition Parser.h:160
void parserState1(StringTokenizer *, PSTACK(PNode) *, STACK(int) *)
Definition Parser.cpp:343
void parserState0(StringTokenizer *, PSTACK(PNode) *, STACK(int) *)
Definition Parser.cpp:255
double impuls_tn
Definition Parser.h:157
void setVar(int i, double val)
Definition Parser.h:83
double impuls_tempo
Definition Parser.h:158
void set(const Parser &)
Definition Parser.cpp:53
double eval()
Definition Parser.h:68
int state
Definition Parser.h:153
int searchFunc(const std::string &v)
Definition Parser.cpp:607
void setString(const std::string &s)
Definition Parser.h:102
int test_op_binaire(int type)
Definition Parser.cpp:411
int getNbVar()
Definition Parser.h:100
std::map< std::string, int > map_function_
Definition Parser.h:166
void setVar(const std::string &v, double val)
Definition Parser.h:78
static int precedence(int)
Definition Parser.cpp:213
virtual void parseString()
Definition Parser.cpp:124
int searchCst(const std::string &v)
Definition Parser.cpp:591
KOKKOS_INLINE_FUNCTION double evalOp(const PNodePod &node, double x, double y)
Definition Parser.h:257
void addCst(const Constante &cst)
Definition Parser.cpp:618
void parserState2(StringTokenizer *, PSTACK(PNode) *, STACK(int) *)
Definition Parser.cpp:416
static KOKKOS_INLINE_FUNCTION void Kokkos_exit(const char *)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.h:173
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455