TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Param.cpp
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#include <TRUSTArray.h>
17#include <Parser_U.h>
18#include <Param.h>
19
20Param::Param(const char * name):proprietaire_(name) { }
21
22/*! @brief Read the is input with the parameters (defined with the Param object) inside: { .
23 *
24 * .. } Example, to read: { a 1 b 2 }
25 * Param param(que_suis_je());
26 * int a,b;
27 * param.ajouter("a",&a);
28 * param.ajouter("b",&b);
29 * param.lire_avec_accolades_depuis(is);
30 *
31 */
33{
34 int ok = read(is);
35 ok = check();
36 if (!ok)
38 return 1;
39}
40
42{
43 Motcle bidon;
44 for (auto &itr : list_parametre_a_lire_)
45 {
46 Objet_a_lire& obj = itr;
47 if (obj.is_optional())
48 {
49 Cerr << proprietaire_ << " has optional param. not implemented for lire_sans_accolade" << finl;
51 }
52 obj.read(bidon, is);
53 Motcle mot = obj.get_name();
54 list_parametre_lu_.add(mot);
55 }
56 int ok = check();
57 if (!ok)
59 return 1;
60
61}
62int Param::read(Entree& is, int with_acco)
63{
64 assert(with_acco == 1);
65 Motcle motlu;
66 Motcle accolade_ouverte("{");
67 Motcle accolade_fermee("}");
68 is >> motlu;
69 if (motlu != accolade_ouverte)
70 {
71 Cerr << "Error while reading the parameters of " << proprietaire_ << finl;
72 Cerr << "We expected : " << accolade_ouverte << " and not " << motlu << finl;
73 motlu.exit();
74 }
75 is >> motlu;
76 while (motlu != accolade_fermee)
77 {
78 bool found = false;
79 Motcle mot(motlu);
80
81 for (auto &itr : list_parametre_a_lire_)
82 if (!found)
83 {
84 Objet_a_lire& obj = itr;
85
86 if (obj.comprend_name(mot))
87 {
88 obj.read(mot, is);
89 list_parametre_lu_.add(mot);
90 found = true;
91 break;
92 }
93 }
94 if (!found)
95 {
96 Cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << finl;
97 Cerr << motlu << " is not understood by " << proprietaire_ << finl;
98 Cerr << "The understood keywords are : ";
99 for (int i = 0; i < list_parametre_a_lire_.size(); i++)
100 Cerr << list_parametre_a_lire_(i).get_names_message() << " ";
101 Cerr << finl;
102 // Evolution des options du mot cle Transformation
103 // On previent les utilisateurs
104 if (proprietaire_ == "Transformation")
105 {
106 if (motlu == "fonction")
107 {
108 Cerr << "Syntax has changed after the 1.6.3 version and fonction keyword is obsoleted." << finl;
109 Cerr << "Have a look at the TRUST documentation about Transformation keyword." << finl;
110 Cerr << "You should replace 'fonction' by 'methode formule expression nb_comp'" << finl;
111 Cerr << "where nb_comp is the number of components of your formulae following the old keyword 'fonction'" << finl;
113 }
114 if (motlu == "vecteur")
115 {
116 Cerr << "Syntax has changed after the 1.6.3 version." << finl;
117 Cerr << "Have a look at the TRUST documentation about Transformation keyword" << finl;
118 Cerr << "You should replace 'vecteur' by 'methode vecteur expression'" << finl;
120 }
121 if (motlu == "produit_scalaire")
122 {
123 Cerr << "Syntax has changed after the 1.6.3 version." << finl;
124 Cerr << "Have a look at the TRUST documentation about Transformation keyword" << finl;
125 Cerr << "You should replace 'produit_scalaire' by 'methode produit_scalaire'" << finl;
127 }
128 }
130 }
131 is >> motlu;
132 }
133 return 1;
134}
135void Param::print(Sortie& s) const
136{
137
138 //Motcle motlu;
139 Motcle accolade_ouverte("{");
140 Motcle accolade_fermee("}");
141 s << accolade_ouverte << finl;
142
143 for (int i = 0; i < list_parametre_a_lire_.size(); i++)
144 {
145 const Objet_a_lire& obj = list_parametre_a_lire_(i);
146 obj.print(s);
147 s << finl;
148 }
149 s << accolade_fermee << finl;
150}
151
153{
154
155 switch(type)
156 {
157 case INTEGER:
158 if (dictionnaire_noms.size() == 0)
159 {
160 s << get_name() << " " << (*int_a_lire);
161 }
162 else
163 {
164 Cerr << " dico print not coded for this case " << get_name() << finl;
166 }
167 break;
168 case DOUBLE:
169 s << get_name() << " " << (*double_a_lire);
170 break;
171 case FLAG:
172 if (*flag_a_lire)
173 s << get_name();
174 break;
175 case NON_STD:
176 s << get_name() << " non standard !!!!";
177 break;
178 default:
179 Cerr << "print not coded for this case " << get_name() << finl;
181 break;
182 }
183}
185{
186 {
187 // ok on a fini de lire est ce que tous les attributs non optionnels ont ete lus ?
188 int err = 0;
189 for (auto &itr : list_parametre_a_lire_)
190 {
191 Objet_a_lire& obj = itr;
192 if (!obj.is_optional())
193 {
194 Motcle name_var(obj.get_name());
195 if (!list_parametre_lu_.contient(name_var))
196 {
197 Cerr << obj.get_name() << " of " << proprietaire_ << " has not been read while it is required." << finl;
198 err = 1;
199 }
200 }
201 }
202 if (err)
203 return 0;
204 //Process::exit();
205 }
206 // tests des autres conditions
207 int val = 1;
208 LIST(Nom) list_mot;
209 int size = list_parametre_a_lire_.size();
210 for (int i = 0; i < size; i++)
211 {
212 Nom mot_lu("is_read_");
213 mot_lu += list_parametre_a_lire_(i).get_name();
214 list_mot.add(mot_lu);
215 }
216 for (int i = 0; i < size; i++)
217 {
218 const Objet_a_lire& obj = list_parametre_a_lire_(i);
219 if (obj.is_type_simple())
220 {
221 Nom mot_lu("value_of_");
222 mot_lu += list_parametre_a_lire_(i).get_name();
223 list_mot.add(mot_lu);
224 }
225 }
226 int size_agrandi = list_mot.size();
227 int nb_condition = list_conditions_.size();
228 for (int cond = 0; cond < nb_condition; cond++)
229 {
230 Nom condition = list_conditions_(cond);
231 Parser_U parser;
232 parser.setNbVar(size_agrandi);
233 for (int j = 0; j < size_agrandi; j++)
234 parser.addVar(list_mot(j));
235 parser.setString(condition);
236 parser.parseString();
237 for (int j = 0; j < size; j++)
238 parser.setVar(j, 0);
239 for (int lu = 0; lu < list_parametre_lu_.size(); lu++)
240 {
241 Nom mot_lu("is_read_");
242 mot_lu += list_parametre_lu_(lu);
243 parser.setVar(mot_lu, 1);
244 }
245 for (int inc = size; inc < size_agrandi; inc++)
246 {
247 Nom mot_lu(list_mot(inc));
248 double xx = get_value(mot_lu);
249 parser.setVar(mot_lu, xx);
250 }
251 if (parser.eval() == 0)
252 {
253 val = 0;
254 Cerr << " Condition " << list_nom_conditions_(cond) << " not verified: " << condition << " it's equal to " << parser.eval() << finl;
255 Cerr << list_message_erreur_conditions_(cond) << finl;
256 }
257
258 }
259 return val;
260}
261
262// retourne une liste de mot venant de nom.split('|')
263LIST(Nom) split_mot(const Nom& nom)
264{
265 LIST(Nom) res;
266 Nom n0(nom);
267 // on cherche le dernier |
268 const char *marq = strchr(n0, '|');
269 while (marq)
270 {
271 Nom n1(n0);
272 n1.prefix(marq);
273 res.add(n1);
274
275 marq++;
276 Nom n1b(n0);
277 n1b.prefix(marq);
278 Nom n2(n0);
279 n2.suffix(n1b);
280 n0 = n2;
281 marq = strchr(n0, '|');
282 }
283 res.add(n0);
284 return res;
285}
286
287/*! @brief Si le dernier parametre ajoute est de type "int", associe a ce parametre une ou plusieurs chaines de caracteres et pour chacune une valeur numerique.
288 *
289 * Il faut appeler dictionnaire pour chaque motcle autorise pour ce parametre
290 * Le parametre lu doit alors etre un motcle parmi ceux du dictionnaire de noms autorises.
291 *
292 */
293void Param::dictionnaire(const char *nom_option, int valeur)
294{
295 const int sz = list_parametre_a_lire_.size();
296 assert(sz > 0);
297 Objet_a_lire& last = list_parametre_a_lire_[sz - 1];
298 last.add_dict(nom_option, valeur);
299}
300Param& Param::dictionnaire_param(const char *nom_option, int valeur)
301{
302 const int sz = list_parametre_a_lire_.size();
303 assert(sz > 0);
304 Objet_a_lire& last = list_parametre_a_lire_[sz - 1];
305 Nom name(proprietaire_);
306 name += "/";
307 name += Nom(nom_option);
308 return last.add_dict(nom_option, valeur, name).valeur();
309}
310
311/*! @brief renvoit Objet_a_lire correspondant au mot le rajoute au besoin a la liste
312 *
313 */
315{
316 LIST(Nom) split_noms = split_mot(Nom(mot));
317
318 for (const auto &itr1 : split_noms)
319 {
320 Motcle name_var(itr1);
321 for (auto &itr2 : list_parametre_a_lire_)
322 {
323 if (itr2.comprend_name(name_var))
324 {
325 Cerr << name_var << " was already a keyword of " << proprietaire_ << finl;
326 Cerr << "the old one will be ignored" << finl;
328
329 return itr2;
330 }
331 }
332 }
333 Objet_a_lire toto;
334 Objet_a_lire& obj = list_parametre_a_lire_.add(toto);
335 obj.set_name(split_noms);
336 return obj;
337}
338
339void Param::supprimer(const char *mot)
340{
341 LIST(Nom) split_noms = split_mot(Nom(mot));
342 Motcle motcle(split_noms(0));
343 for (auto &itr : list_parametre_a_lire_)
344 {
345 Objet_a_lire& obj = itr;
346 if (obj.comprend_name(motcle))
347 {
348 list_parametre_a_lire_.suppr(obj);
349 return;
350 }
351 }
352 Cerr << "Error in Param::supprimer parametre " << mot << " not found in " << proprietaire_ << finl;
354}
355
356Objet_a_lire::Nature convert_nature(Param::Nature nat)
357{
358 if (nat == Param::REQUIRED)
360 else
362}
363
364void Param::ajouter(const char *mot, const int *quoi, Param::Nature nat)
365{
367 obj.set_nature(convert_nature(nat));
368 obj.set_entier(const_cast<int*>(quoi));
369}
370
371#if INT_is_64_ == 2
372void Param::ajouter(const char *mot, const trustIdType *quoi, Param::Nature nat)
373{
375 obj.set_nature(convert_nature(nat));
376 obj.set_tid(const_cast<trustIdType*>(quoi));
377}
378#endif
379
380void Param::ajouter(const char *mot, const double *quoi, Param::Nature nat)
381{
383 obj.set_nature(convert_nature(nat));
384 obj.set_double(const_cast<double*>(quoi));
385}
386
387void Param::ajouter(const char *mot, const std::string *quoi, Param::Nature nat)
388{
390 obj.set_nature(convert_nature(nat));
391 obj.set_string(const_cast<std::string*>(quoi));
392}
393
394void Param::ajouter(const char *mot, const Objet_U *quoi, Param::Nature nat)
395{
397 obj.set_nature(convert_nature(nat));
398 obj.set_objet(const_cast<Objet_U*>(quoi));
399}
400
402{
404 obj.set_nature(convert_nature(nat));
405 Nom name(proprietaire_);
406 name += "/";
407 name += obj.get_name();
408 return obj.create_param(name);
409}
410
411void Param::ajouter_arr_size_predefinie(const char *mot, const ArrOfInt *quoi, Param::Nature nat)
412{
414 obj.set_nature(convert_nature(nat));
415 obj.set_arrofint(const_cast<ArrOfInt*>(quoi));
416}
417void Param::ajouter_arr_size_predefinie(const char *mot, const ArrOfDouble *quoi, Param::Nature nat)
418{
420 obj.set_nature(convert_nature(nat));
421 obj.set_arrofdouble(const_cast<ArrOfDouble*>(quoi));
422}
423
424
425
426
427void Param::ajouter(const char *mot, const std::vector<int>* quoi,Param::Nature nat, int size)
428{
429
431 obj.set_nature(convert_nature(nat));
432 obj.set_vec_expected_size(size);
433 obj.set_vec_int(const_cast<std::vector<int>*>(quoi));
434}
435
436void Param::ajouter(const char *mot, const std::vector<double>* quoi,Param::Nature nat, int size)
437{
439 obj.set_nature(convert_nature(nat));
440 obj.set_vec_expected_size(size);
441 obj.set_vec_dbl(const_cast<std::vector<double>*>(quoi));
442}
443
444void Param::ajouter(const char *mot, const std::vector<std::string>* quoi,Param::Nature nat, int size)
445{
447 obj.set_nature(convert_nature(nat));
448 obj.set_vec_expected_size(size);
449 obj.set_vec_str(const_cast<std::vector<std::string>*>(quoi));
450}
451
452void Param::ajouter(const char *mot, const std::map<std::string, int>* quoi,Param::Nature nat)
453{
454
456 obj.set_nature(convert_nature(nat));
457 obj.set_map_int(const_cast<std::map<std::string, int>*>(quoi));
458}
459
460void Param::ajouter(const char *mot, const std::map<std::string, double>* quoi,Param::Nature nat)
461{
463 obj.set_nature(convert_nature(nat));
464 obj.set_map_dbl(const_cast<std::map<std::string, double>*>(quoi));
465}
466
467void Param::ajouter(const char *mot, const std::map<std::string, std::string>* quoi,Param::Nature nat)
468{
470 obj.set_nature(convert_nature(nat));
471 obj.set_map_str(const_cast<std::map<std::string, std::string>*>(quoi));
472}
473
474void Param::ajouter_flag(const char *mot, const bool *quoi)
475{
476
477 if (*quoi)
478 {
479 Cerr << "ERROR: flag '" << mot << "' is set to true before being passed to Param::ajouter_flag." << finl;
480 Cerr << " Flags should be set to false by default. Then, the value will be set to true if the keyword '"<<mot<<"' is present in the .data file." << finl;
481 Cerr << " Change the default value of the flag to 'false' to fix this problem. (This error may come from the parameter not being initialized)" << finl;
483 }
484
486 obj.set_nature(convert_nature(Param::OPTIONAL));
487 obj.set_flag(const_cast<bool*>(quoi));
488}
489void Param::ajouter_non_std(const char *mot, const Objet_U *quoi, Param::Nature nat)
490{
492 obj.set_nature(convert_nature(nat));
493 obj.set_non_std(const_cast<Objet_U*>(quoi));
494}
495
496void Param::ajouter_condition(const char *condition, const char *message, const char *name)
497{
498 list_conditions_.add(Nom(condition));
499 list_message_erreur_conditions_.add(Nom(message));
500 Nom nom;
501 if (name == 0)
502 {
503 nom = "condition_";
504 nom += Nom(list_conditions_.size());
505 }
506 else
507 nom = Nom(name);
508 list_nom_conditions_.add(nom);
509}
510void Param::supprimer_condition(const char *name)
511{
512 Nom nom(name);
513 int i = list_nom_conditions_.rang(nom);
514 if (i < 0)
515 {
516 Cerr << "Error in Param::supprimer_condition, condition " << nom << " not found in " << proprietaire_ << finl;
518 }
519
520 list_conditions_.suppr(list_conditions_(i));
521 list_message_erreur_conditions_.suppr(list_message_erreur_conditions_(i));
522 list_nom_conditions_.suppr(list_nom_conditions_(i));
523}
524
525double Param::get_value(const Nom& mot_lu) const
526{
527 int size = list_parametre_a_lire_.size();
528 Motcle val("value_of_");
529 for (int i = 0; i < size; i++)
530 {
531 const Objet_a_lire& obj = list_parametre_a_lire_(i);
532 if (obj.is_type_simple())
533 {
534 Motcle nom(val);
535 nom += obj.get_name();
536 if (nom == mot_lu)
537 {
538 return obj.get_value();
539 }
540 }
541 }
542 Cerr << mot_lu << " not found in Param::get_value" << finl;
543 Cerr << "the understood keywords are : ";
544 for (int i = 0; i < list_parametre_a_lire_.size(); i++)
545 if (list_parametre_a_lire_(i).is_type_simple())
546 Cerr << " value_of_" << list_parametre_a_lire_(i).get_name();
547 Cerr << finl;
548 mot_lu.exit();
549 return -1.;
550}
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Une chaine de caractere (Nom) en majuscules.
Definition Motcle.h:26
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
friend class Sortie
Definition Objet_U.h:75
void set_objet(Objet_U *)
bool is_optional() const
void set_map_int(std::map< std::string, int > *)
void set_tid(trustIdType *)
void set_double(double *)
void read(const Motcle &keyword, Entree &is)
void set_vec_int(std::vector< int > *)
void set_vec_dbl(std::vector< double > *)
void print(Sortie &s) const
Definition Param.cpp:152
Objet_a_lire::Type type
int comprend_name(Motcle &mot) const
void set_map_str(std::map< std::string, std::string > *)
void set_arrofint(ArrOfInt *)
void set_flag(bool *)
void set_nature(Objet_a_lire::Nature n)
double get_value() const
bool * flag_a_lire
void set_map_dbl(std::map< std::string, double > *)
void set_vec_expected_size(int s)
Motcles dictionnaire_noms
void set_arrofdouble(ArrOfDouble *)
void set_entier(int *)
ptrParam & add_dict(const char *, int, const char *=0)
void set_non_std(Objet_U *)
void set_vec_str(std::vector< std::string > *)
bool is_type_simple() const
void set_name(const LIST(Nom)&n)
const Nom & get_name() const
Param & create_param(const char *)
void set_string(std::string *)
void supprimer_condition(const char *name)
Remove a previously-registered condition from this Param.
Definition Param.cpp:510
Param(const char *owner_name)
Build a Param that will parse the parameters of an object named owner_name.
Definition Param.cpp:20
const LIST(Nom) &get_list_mots_lus() const
List of keywords that have actually been read from the input.
Definition Param.h:599
void ajouter_flag(const char *keyword, const bool *value)
Register a boolean flag whose mere presence switches it to true.
Definition Param.cpp:474
Param & dictionnaire_param(const char *option_name, int value)
Same as dictionnaire, but also attaches a nested Param block that will be read when this option is se...
Definition Param.cpp:300
Objet_a_lire & create_or_get_objet_a_lire(const char *keyword)
Look up (or create) the Objet_a_lire associated with a keyword string.
Definition Param.cpp:314
void ajouter_arr_size_predefinie(const char *keyword, const ArrOfInt *value, Param::Nature nat=Param::OPTIONAL)
Register an ArrOfInt whose size has already been fixed.
Definition Param.cpp:411
void print(Sortie &s) const
Print the current state of the registered parameters as a { ... } block.
Definition Param.cpp:135
void dictionnaire(const char *option_name, int value)
Add an (option name, integer value) entry to the dictionary attached to a previously registered integ...
Definition Param.cpp:293
int lire_sans_accolade(Entree &is)
Read all required keywords in the order they were registered, without enclosing braces.
Definition Param.cpp:41
void ajouter_condition(const char *condition, const char *message, const char *name=0)
Declare a post-read logical condition that must hold on the parameter values.
Definition Param.cpp:496
int read(Entree &is, int with_acco=1)
Low-level entry point used by lire_avec_accolades_depuis.
Definition Param.cpp:62
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
int check()
Validate that every required keyword was read and every condition holds.
Definition Param.cpp:184
Nature
Whether a registered parameter must be present in the input or may be omitted.
Definition Param.h:115
@ OPTIONAL
Definition Param.h:115
@ REQUIRED
Definition Param.h:115
void ajouter_non_std(const char *keyword, const Objet_U *value, Param::Nature nat=Param::OPTIONAL)
Register a keyword handled by Objet_U::lire_motcle_non_standard.
Definition Param.cpp:489
int lire_avec_accolades_depuis(Entree &is)
Parse the parameter block { ... } from is.
Definition Param.cpp:32
void supprimer(const char *keyword)
Remove a previously-registered keyword from this Param.
Definition Param.cpp:339
Param()
Default-constructed Param with no owner; only for derived classes.
Param & ajouter_param(const char *keyword, Param::Nature nat=Param::OPTIONAL)
Register a nested Param block and return a reference to it so it can be populated in turn.
Definition Param.cpp:401
double get_value(const Nom &mot_lu) const
Retrieve the value of a simple-type parameter by its value_of_<keyword> name.
Definition Param.cpp:525
classe Parser_U Version de la classe Parser, derivant de Objet_U.
Definition Parser_U.h:32
void setVar(const char *sv, double val)
Definition Parser_U.h:149
void setString(const std::string &s)
Definition Parser_U.h:194
void setNbVar(int nvar)
Definition Parser_U.h:174
void parseString()
Definition Parser_U.h:116
double eval()
Definition Parser_U.h:125
void addVar(const char *v)
Definition Parser_U.h:183
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52
Param & valeur()
Definition ptrParam.cpp:37