TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Pilote_ICoCo.cpp
1/****************************************************************************
2 * Copyright (c) 2024, 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 <Pilote_ICoCo.h>
17#include <ICoCoTrioField.h>
18#include <ProblemTrio.h>
19#include <Param.h>
20#include <Probleme_U.h>
21/*
22 #include <vector>
23 #include <string>
24 #include <iostream>
25 */
26//using namespace std;
27using std::string;
28using std::vector;
30using ICoCo::TrioField;
31
32extern void TRUST_set_library_mode(bool);
33
34Implemente_instanciable(Pilote_ICoCo,"Pilote_ICoCo",Interprete);
35// XD pilote_icoco interprete pilote_icoco BRACE not_set
36// XD attr pb_name chaine pb_name REQ not_set
37// XD attr main chaine main REQ not_set
38
39
40/*! @brief Simple appel a: Interprete::printOn(Sortie&)
41 *
42 * Imprime l'interprete sur un flot de sortie
43 *
44 * @param (Sortie& os) un flot de sortie
45 * @return (Sortie&) le flot de sortie modifie
46 */
48{
49 return Interprete::printOn(os);
50}
51
52
53/*! @brief Simple appel a: Interprete::readOn(Entree&)
54 *
55 * @param (Entree& is) un flot d'entree
56 * @return (Entree&) le flot d'entree modifie
57 */
59{
60 return Interprete::readOn(is);
61}
62
63
64/*! @brief Fonction principale de l'interprete: resoudre un probleme
65 *
66 * Le probleme est pilote via l'interface ICoCo
67 * On lui fournit a chaque pas de temps les champs
68 * d'entree "pression_sortie" et "vitesse_entree"
69 * qui sont respectivement un ch_front_input et un ch_front_input_uniforme
70 * Montre l'utilisation de l'avance en temps et des input fields
71 *
72 */
73
74// Utilisation dans le JDD : Pilote_ICoCo_1 pb
75// Utilise dans le cas test U_in_var_impl_ICoCo
76void main_pilote_icoco_1(Probleme_U& pb_to_solve)
77{
78 ProblemTrio pb;
79 TRUST_set_library_mode(false);
80
81 Nom pb_name=pb_to_solve.le_nom();
82 pb.initialize_pb(pb_to_solve);
83
84 bool stop=false; // Does the Problem want to stop ?
85 bool ok=true; // Is the time interval successfully solved ?
86
87 // Compute the first time step length
88 double dt=pb.computeTimeStep(stop);
89
90 // Loop on the time steps
91 statistics().start_timeloop();
92 while(!stop)
93 {
94 statistics().begin_count(STD_COUNTERS::timeloop);
95
96 ok=false; // Is the time interval successfully solved ?
97
98 // Loop on the time interval tries
99 while (!ok && !stop)
100 {
101
102 // Prepare the next time step
103 ok=pb.initTimeStep(dt);
104 if (!ok)
105 break;
106
107 // Example of field exchange
108 {
109 vector<string> sv=pb.getInputFieldsNames();
110 cout << "Input Fields for " << pb_name << " :" << endl;
111 for (unsigned i=0; i<sv.size(); i++)
112 std::cout << i << ". " << sv[i] << std::endl;
113
114 TrioField afield;
115 int nb_elems,nb_comp;
116
117 // Get the right geometry & nbcomp for "pression_sortie"
118 pb.getInputFieldTemplate("pression_sortie",afield);
119 // Allocate memory for the values (size=nb_elems*nb_comp)
120 afield.set_standalone();
121 // Fill the values
122 nb_elems=afield._nb_elems;
123 nb_comp=afield._nb_field_components;
124 assert(nb_comp==1);
125 for (int i=0; i<nb_elems; i++)
126 afield._field[i]=0;
127 // Give the field to the problem
128 pb.setInputField("pression_sortie",afield);
129
130 // Get the right geometry & nbcomp for "vitesse_entree"
131 pb.getInputFieldTemplate("vitesse_entree",afield);
132 // Allocate memory for the values (size=nb_elems*nb_comp)
133 afield.set_standalone();
134 // Fill the values
135 nb_elems=afield._nb_elems;
136 nb_comp=afield._nb_field_components;
137 assert(nb_elems==1); // ch_front_input_uniforme in the data file
138 double t=pb.presentTime();
139 t+=dt;
140 double vx=(t>100)?100:t;
141 for (int i=0; i<nb_elems; i++)
142 for (int j=0; j<afield._nb_field_components; j++)
143 afield._field[i*nb_comp+j]=(j==0)?vx:0;
144 // Give the field to the problem
145 pb.setInputField("vitesse_entree",afield);
146 }
147
148 // Solve the next time step
149 ok=pb.solveTimeStep();
150
151 if (!ok) // The resolution failed, try with a new time interval.
152 {
153 pb.abortTimeStep();
154 dt=pb.computeTimeStep(stop);
155 }
156
157 else // The resolution was successful, validate and go to the
158 // next time step.
159 pb.validateTimeStep();
160 }
161
162 if (!ok) // Impossible to solve the next time step, the Problem
163 break; // has given up
164
165 // Compute the next time step length
166 dt=pb.computeTimeStep(stop);
167
168 // Stop the resolution if the Problem is stationnary
169 if (pb.isStationary())
170 stop=true;
171 statistics().end_count(STD_COUNTERS::timeloop);
172 }
173 statistics().end_timeloop();
175 statistics().print_TU_files("Time loop statistics");
176 pb.terminate();
177
178}
179/*! @brief Fonction principale de l'interprete: resoudre un probleme
180 *
181 * Le probleme est pilote via l'interface ICoCo.
182 * On lui fournit a chaque pas de temps le champ
183 * d'entree "puissance" qui est un Champ_input_P0
184 * Montre l'utilisation de l'avance en temps et des input fields
185 *
186 */
187
188// Utilisation dans le JDD : Pilote_ICoCo_2 pb
189// Utilise dans le cas test ChDonXYZ_ICoCo
190void main_pilote_icoco_2(Probleme_U& pb_to_solve)
191{
192 ProblemTrio pb;
193 TRUST_set_library_mode(false);
194
195 Nom pb_name=pb_to_solve.le_nom();
196 pb.initialize_pb(pb_to_solve);
197
198 bool stop=false; // Does the Problem want to stop ?
199 bool ok=true; // Is the time interval successfully solved ?
200
201 // Compute the first time step length
202 double dt=pb.computeTimeStep(stop);
203
204 // Loop on the time steps
205 statistics().start_timeloop();
206 while(!stop)
207 {
208 statistics().begin_count(STD_COUNTERS::timeloop);
209
210 ok=false; // Is the time interval successfully solved ?
211
212 // Loop on the time interval tries
213 while (!ok && !stop)
214 {
215
216 // Prepare the next time step
217 ok=pb.initTimeStep(dt);
218 if (!ok)
219 break;
220
221 // Example of field exchange
222 {
223 vector<string> sv=pb.getInputFieldsNames();
224 cout << "Input Fields for " << pb_name << " :" << endl;
225 for (unsigned i=0; i<sv.size(); i++)
226 std::cout << i << ". " << sv[i] << std::endl;
227
228 TrioField afield;
229 int nb_elems;
230
231 // Get the right geometry & nbcomp for "puissance"
232 pb.getInputFieldTemplate("puissance",afield);
233 // Allocate memory for the values (size=nb_elems*nb_comp)
234 afield.set_standalone();
235 // Fill the values
236 nb_elems=afield._nb_elems;
237 //nb_comp=afield._nb_field_components;
238 assert(afield._nb_field_components==1);
239 assert(afield._space_dim==2);
240 for (int i=0; i<nb_elems; i++)
241 {
242 // Find gravity center of the element.
243 double x=0,y=0;
244 for (int j=0; j<afield._nodes_per_elem; j++)
245 {
246 int som=afield._connectivity[afield._nodes_per_elem*i+j];
247 x+=afield._coords[som*2];
248 y+=afield._coords[som*2+1];
249 }
250 x/=afield._nodes_per_elem;
251 y/=afield._nodes_per_elem;
252 afield._field[i]=(((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5))<0.3*0.3)*10;
253 }
254 // Give the field to the problem
255 pb.setInputField("puissance",afield);
256 }
257
258 // Solve the next time step
259 ok=pb.solveTimeStep();
260
261 if (!ok) // The resolution failed, try with a new time interval.
262 {
263 pb.abortTimeStep();
264 dt=pb.computeTimeStep(stop);
265 }
266
267 else // The resolution was successful, validate and go to the
268 // next time step.
269 pb.validateTimeStep();
270 }
271
272 if (!ok) // Impossible to solve the next time step, the Problem
273 break; // has given up
274
275 // Compute the next time step length
276 dt=pb.computeTimeStep(stop);
277
278 // Stop the resolution if the Problem is stationnary
279 if (pb.isStationary())
280 stop=true;
281 statistics().end_count(STD_COUNTERS::timeloop);
282 }
283 statistics().end_timeloop();
285 statistics().print_TU_files("Time loop statistics");
286 pb.terminate();
287}
288
289/**
290 * This one is used to test resetTime()
291 */
292void main_pilote_icoco_3(Probleme_U& pb_to_solve, int nb_pas_dt_reset)
293{
294 ProblemTrio pb;
295 TRUST_set_library_mode(false);
296
297 pb.initialize_pb(pb_to_solve);
298
299 bool stop=false; // Does the Problem want to stop ?
300 bool ok=true; // Is the time interval successfully solved ?
301
302 // Compute the first time step length
303 double dt=pb.computeTimeStep(stop);
304
305 int cnt = 1;
306 bool reset = false;
307 // Loop on the time steps
308 statistics().start_timeloop();
309 while(!stop)
310 {
311 statistics().begin_count(STD_COUNTERS::timeloop);
312 ok=false; // Is the time interval successfully solved ?
313
314 // Loop on the time interval tries
315 while (!ok && !stop)
316 {
317 // Prepare the next time step
318 ok=pb.initTimeStep(dt);
319 if (!ok)
320 break;
321
322 // Solve the next time step
323 ok=pb.solveTimeStep();
324
325 if (!ok) // The resolution failed, try with a new time interval.
326 {
327 pb.abortTimeStep();
328 dt=pb.computeTimeStep(stop);
329 }
330 else // The resolution was successful, validate and go to the
331 // next time step.
332 pb.validateTimeStep();
333 }
334
335 if (!ok) // Impossible to solve the next time step, the Problem
336 break; // has given up
337
338 if (cnt >= nb_pas_dt_reset && !reset)
339 {
340 Cerr << "ABOUT TO RESET TIME !!\n";
341 pb.resetTime(0);
342 Cerr << "RESET TIME DONE!!\n";
343 reset = true;
344 }
345
346 // Compute the next time step length
347 dt=pb.computeTimeStep(stop);
348
349 // Stop the resolution if the Problem is stationnary
350 if (pb.isStationary())
351 stop=true;
352
353 cnt ++;
354 statistics().end_count(STD_COUNTERS::timeloop);
355 }
356 statistics().end_timeloop();
358 statistics().print_TU_files("Time loop statistics");
359
360 pb.terminate();
361}
362
363/**
364 * This one is used to test setInputStringValue("SORTIE_ROOT_DIRECTORY", "./")
365 */
366void main_pilote_icoco_4(Probleme_U& pb_to_solve, int sortie_root_directory, int nstep_stabilized, int nb_pas_dt_reset)
367{
368 ProblemTrio pb;
369 TRUST_set_library_mode(false);
370
371 pb.initialize_pb(pb_to_solve);
372
373 bool stop=false; // Does the Problem want to stop ?
374 bool ok=true; // Is the time interval successfully solved ?
375
376 // Compute the first time step length
377 double dt=pb.computeTimeStep(stop);
378
379 assert(nstep_stabilized < nb_pas_dt_reset);
380 int cnt = 1;
381 bool reset = false;
382 // Loop on the time steps
383 statistics().start_timeloop();
384 while(!stop)
385 {
386 statistics().begin_count(STD_COUNTERS::timeloop);
387 ok=false; // Is the time interval successfully solved ?
388
389 // Loop on the time interval tries
390 while (!ok && !stop)
391 {
392 // Prepare the next time step
393 ok=pb.initTimeStep(dt);
394 if (!ok)
395 break;
396
397 // Solve the next time step
398 ok=pb.solveTimeStep();
399
400 if (!ok) // The resolution failed, try with a new time interval.
401 {
402 pb.abortTimeStep();
403 dt=pb.computeTimeStep(stop);
404 }
405 else // The resolution was successful, validate and go to the
406 // next time step.
407 pb.validateTimeStep();
408 }
409
410 if (!ok) // Impossible to solve the next time step, the Problem
411 break; // has given up
412
413 // stabilized transient step
414 if (cnt <= nstep_stabilized)
415 {
416 if (sortie_root_directory == 1)
417 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "./");
418 else if (sortie_root_directory == 2)
419 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "LATA");
420 pb.resetTime(0.);
421 }
422 // resetime directive
423 if (cnt >= nb_pas_dt_reset && !reset)
424 {
425 if (sortie_root_directory == 1)
426 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "./");
427 else if (sortie_root_directory == 2)
428 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "LATA");
429 pb.resetTime(0);
430 reset = true;
431 }
432
433 // Compute the next time step length
434 dt=pb.computeTimeStep(stop);
435
436 // Stop the resolution if the Problem is stationnary
437 if (pb.isStationary())
438 stop=true;
439
440 cnt ++;
441 statistics().end_count(STD_COUNTERS::timeloop);
442 }
443 statistics().end_timeloop();
445 statistics().print_TU_files("Time loop statistics");
446
447 pb.terminate();
448}
449
450
451/*! @brief Fonction principale de l'interprete: resoudre un probleme
452 *
453 * Le probleme est pilote via l'interface ICoCo
454 * Le troisieme pas de temps est effectue deux fois
455 *
456 * @param (Entree& is) un flot d'entree
457 * @return le flot d'entree modifie
458 */
459void main_abort( Probleme_U& pb_to_solve)
460{
461 // char* pb_name=nom1.getChar();
462 ProblemTrio pb;
463 TRUST_set_library_mode(false);
464 // pb.setDataFile(pb_name);
465
466 pb.initialize_pb(pb_to_solve);
467
468 bool stop=false; // Does the Problem want to stop ?
469 bool ok=true; // Is the time interval successfully solved ?
470 int n=0;
471 //loop on time step
472 statistics().start_timeloop();
473 while (!stop) // Loop on timesteps
474 {
475 statistics().begin_count(STD_COUNTERS::timeloop);
476 ok=false;
477
478 while (!ok && !stop) // Loop on timestep tries
479 {
480
481 //compute time step length
482 double dt=pb.computeTimeStep(stop);
483
484 if (stop)
485 break;
486
487 //prepare the new time step
488 pb.initTimeStep(dt);
489
490 //---------------------
491 //Solve next time step
492 //---------------------
493
494 {
495 TrioField afield;
496 int nb_elems;
497 pb.getInputFieldTemplate("temperature_bord",afield);
498 afield.set_standalone();
499 nb_elems=afield._nb_elems;
500 for (int i=0; i<nb_elems; i++)
501 afield._field[i]=600;
502 pb.setInputField("temperature_bord",afield);
503 }
504
505
506 bool ok2=pb.solveTimeStep();
507
508 n++;
509 if (!ok2 || n==3) // The resolution failed, try with a new time interval
510 {
511 Cerr<<" call of abortTimeStep" <<finl;
512 pb.abortTimeStep();
513 }
514 else // validate and go to the next time step
515 {
516 pb.validateTimeStep();
517 }
518 } // End loop on timestep size
519 statistics().end_count(STD_COUNTERS::timeloop);
520 } // End loop on timesteps
521 statistics().end_timeloop();
523 statistics().print_TU_files("Time loop statistics");
524 pb.terminate();
525}
526
528{
529 Param param(que_suis_je());
530 Nom nom1;
531 int nb_pas_dt_reset = 1000000000;
532 int nstep_stabilized = 0;
533 int sortie_root_directory = 0;
534 int methode = -1;
535 param.ajouter("pb_name",&nom1,Param::REQUIRED);
536 param.ajouter("main",&methode,Param::REQUIRED);
537 param.dictionnaire("abort_time_step",0);
538 param.dictionnaire("Pilote_ICoCo_1",1);
539 param.dictionnaire("Pilote_ICoCo_2",2);
540 param.dictionnaire("Pilote_ICoCo_3",3);
541 param.dictionnaire("Pilote_ICoCo_4",4);
542 param.ajouter("nb_pas_dt_reset",&nb_pas_dt_reset); // XD_ADD_P int
543 // XD_CONT number of time steps before a single call to resetTime(0) like the Cathare directive (default value
544 // XD_CONT 1000000000)
545 param.ajouter("nstep_stabilized",&nstep_stabilized); // XD_ADD_P int
546 // XD_CONT number of first iterations in which resetTime(0) is called as a Cathare stabilized transient (default value
547 // XD_CONT 0)
548 param.ajouter("sortie_root_directory",&sortie_root_directory); // XD_ADD_P int
549 // XD_CONT if enable, no save post before the resetTime(0) call (default value 0)
551 Probleme_U& pb_to_solve=ref_cast(Probleme_U,objet(nom1));
552 switch (methode)
553 {
554 case 0:
555 main_abort(pb_to_solve);
556 break;
557 case 1:
558 main_pilote_icoco_1(pb_to_solve);
559 break;
560 case 2:
561 main_pilote_icoco_2(pb_to_solve);
562 break;
563 case 3:
564 main_pilote_icoco_3(pb_to_solve, nb_pas_dt_reset);
565 break;
566 case 4:
567 main_pilote_icoco_4(pb_to_solve, sortie_root_directory, nstep_stabilized, nb_pas_dt_reset);
568 break;
569 default:
570 {
571 Cerr<<que_suis_je()<<" main number "<<methode<<" not implemented"<<finl;
572 exit();
573 }
574 }
575 return is;
576}
577
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
class ProblemTrio
Definition ProblemTrio.h:46
void terminate() override
This method is called once at the end, after any other one.
double computeTimeStep(bool &stop) const override
Compute the value the Problem would like for the next time step.
std::vector< std::string > getInputFieldsNames() const override
This method is used to find the names of input fields understood by the Problem.
void validateTimeStep() override
Validates the calculated unknown by moving the present time at the end of the time step.
double presentTime() const override
Returns the present time.
void getInputFieldTemplate(const std::string &name, TrioField &afield) const override
This method is used to get a template of a field expected for the given name.
void resetTime(double time) override
Reset the current time of the Problem to a given value.
void setInputField(const std::string &name, const TrioField &afield) override
This method is used to provide the Problem with an input field.
bool initialize_pb(Probleme_U &pb)
bool initTimeStep(double dt) override
This method allocates and initializes the unknown and given fields for the future time step.
void setInputStringValue(const std::string &name, const std::string &val) override
bool solveTimeStep() override
Calculates the unknown fields for the next time step.
bool isStationary() const override
Tells if the Problem unknowns have changed during the last time step.
void abortTimeStep() override
Aborts the resolution of the current time step.
Classe de base des objets "interprete".
Definition Interprete.h:38
static Objet_U & objet(const Nom &)
Voir Interprete_bloc::objet_global() BM: la classe Interprete n'est pas le meilleur endroit pour cett...
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
friend class Entree
Definition Objet_U.h:76
static bool disable_TU
Flag to disable or not the writing of the .TU files.
Definition Objet_U.h:125
const Nom & que_suis_je() const
renvoie la chaine identifiant la classe.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
Helper class to factorize the readOn method of Objet_U classes.
Definition Param.h:112
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
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
@ REQUIRED
Definition Param.h:115
int lire_avec_accolades_depuis(Entree &is)
Parse the parameter block { ... } from is.
Definition Param.cpp:32
classe Pilote_ICoCo Exemple de pilotage de Trio via ICoCo en C++
Entree & interpreter(Entree &) override
classe Probleme_U
Definition Probleme_U.h:46
const Nom & le_nom() const override
Donne le nom de l'Objet_U Methode a surcharger : renvoie "neant" dans cette implementation.
Definition Probleme_U.h:109
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