TrioCFD 1.9.9_beta
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 call to: Interprete::printOn(Sortie&)
41 *
42 * Prints the interpreter to an output stream
43 *
44 * @param (Sortie& os) an output stream
45 * @return (Sortie&) the modified output stream
46 */
48{
49 return Interprete::printOn(os);
50}
51
52
53/*! @brief Simple call to: Interprete::readOn(Entree&)
54 *
55 * @param (Entree& is) an input stream
56 * @return (Entree&) the modified input stream
57 */
59{
60 return Interprete::readOn(is);
61}
62
63
64/*! @brief Main function of the interpreter: solve a problem
65 *
66 * The problem is driven via the ICoCo interface.
67 * At each time step, the input fields "pression_sortie" and "vitesse_entree"
68 * are provided; these are respectively a ch_front_input and a ch_front_input_uniforme.
69 * Demonstrates the use of time advancement and input fields.
70 *
71 */
72
73// Usage in the data file: Pilote_ICoCo_1 pb
74// Used in the test case U_in_var_impl_ICoCo
75void main_pilote_icoco_1(Probleme_U& pb_to_solve)
76{
77 ProblemTrio pb;
78 TRUST_set_library_mode(false);
79
80 Nom pb_name=pb_to_solve.le_nom();
81 pb.initialize_pb(pb_to_solve);
82
83 bool stop=false; // Does the Problem want to stop ?
84 bool ok=true; // Is the time interval successfully solved ?
85
86 // Compute the first time step length
87 double dt=pb.computeTimeStep(stop);
88
89 // Loop on the time steps
90 statistics().start_timeloop();
91 while(!stop)
92 {
93 statistics().begin_count(STD_COUNTERS::timeloop);
94
95 ok=false; // Is the time interval successfully solved ?
96
97 // Loop on the time interval tries
98 while (!ok && !stop)
99 {
100
101 // Prepare the next time step
102 ok=pb.initTimeStep(dt);
103 if (!ok)
104 break;
105
106 // Example of field exchange
107 {
108 vector<string> sv=pb.getInputFieldsNames();
109 cout << "Input Fields for " << pb_name << " :" << endl;
110 for (unsigned i=0; i<sv.size(); i++)
111 std::cout << i << ". " << sv[i] << std::endl;
112
113 TrioField afield;
114 int nb_elems,nb_comp;
115
116 // Get the right geometry & nbcomp for "pression_sortie"
117 pb.getInputFieldTemplate("pression_sortie",afield);
118 // Allocate memory for the values (size=nb_elems*nb_comp)
119 afield.set_standalone();
120 // Fill the values
121 nb_elems=afield._nb_elems;
122 nb_comp=afield._nb_field_components;
123 assert(nb_comp==1);
124 for (int i=0; i<nb_elems; i++)
125 afield._field[i]=0;
126 // Give the field to the problem
127 pb.setInputField("pression_sortie",afield);
128
129 // Get the right geometry & nbcomp for "vitesse_entree"
130 pb.getInputFieldTemplate("vitesse_entree",afield);
131 // Allocate memory for the values (size=nb_elems*nb_comp)
132 afield.set_standalone();
133 // Fill the values
134 nb_elems=afield._nb_elems;
135 nb_comp=afield._nb_field_components;
136 assert(nb_elems==1); // ch_front_input_uniforme in the data file
137 double t=pb.presentTime();
138 t+=dt;
139 double vx=(t>100)?100:t;
140 for (int i=0; i<nb_elems; i++)
141 for (int j=0; j<afield._nb_field_components; j++)
142 afield._field[i*nb_comp+j]=(j==0)?vx:0;
143 // Give the field to the problem
144 pb.setInputField("vitesse_entree",afield);
145 }
146
147 // Solve the next time step
148 ok=pb.solveTimeStep();
149
150 if (!ok) // The resolution failed, try with a new time interval.
151 {
152 pb.abortTimeStep();
153 dt=pb.computeTimeStep(stop);
154 }
155
156 else // The resolution was successful, validate and go to the
157 // next time step.
158 pb.validateTimeStep();
159 }
160
161 if (!ok) // Impossible to solve the next time step, the Problem
162 break; // has given up
163
164 // Compute the next time step length
165 dt=pb.computeTimeStep(stop);
166
167 // Stop the resolution if the Problem is stationnary
168 if (pb.isStationary())
169 stop=true;
170 statistics().end_count(STD_COUNTERS::timeloop);
171 }
172 statistics().end_timeloop();
174 statistics().print_TU_files("Time loop statistics");
175 pb.terminate();
176
177}
178/*! @brief Main function of the interpreter: solve a problem
179 *
180 * The problem is driven via the ICoCo interface.
181 * At each time step, the input field "puissance" is provided; it is a Champ_input_P0.
182 * Demonstrates the use of time advancement and input fields.
183 *
184 */
185
186// Usage in the data file: Pilote_ICoCo_2 pb
187// Used in the test case ChDonXYZ_ICoCo
188void main_pilote_icoco_2(Probleme_U& pb_to_solve)
189{
190 ProblemTrio pb;
191 TRUST_set_library_mode(false);
192
193 Nom pb_name=pb_to_solve.le_nom();
194 pb.initialize_pb(pb_to_solve);
195
196 bool stop=false; // Does the Problem want to stop ?
197 bool ok=true; // Is the time interval successfully solved ?
198
199 // Compute the first time step length
200 double dt=pb.computeTimeStep(stop);
201
202 // Loop on the time steps
203 statistics().start_timeloop();
204 while(!stop)
205 {
206 statistics().begin_count(STD_COUNTERS::timeloop);
207
208 ok=false; // Is the time interval successfully solved ?
209
210 // Loop on the time interval tries
211 while (!ok && !stop)
212 {
213
214 // Prepare the next time step
215 ok=pb.initTimeStep(dt);
216 if (!ok)
217 break;
218
219 // Example of field exchange
220 {
221 vector<string> sv=pb.getInputFieldsNames();
222 cout << "Input Fields for " << pb_name << " :" << endl;
223 for (unsigned i=0; i<sv.size(); i++)
224 std::cout << i << ". " << sv[i] << std::endl;
225
226 TrioField afield;
227 int nb_elems;
228
229 // Get the right geometry & nbcomp for "puissance"
230 pb.getInputFieldTemplate("puissance",afield);
231 // Allocate memory for the values (size=nb_elems*nb_comp)
232 afield.set_standalone();
233 // Fill the values
234 nb_elems=afield._nb_elems;
235 //nb_comp=afield._nb_field_components;
236 assert(afield._nb_field_components==1);
237 assert(afield._space_dim==2);
238 for (int i=0; i<nb_elems; i++)
239 {
240 // Find gravity center of the element.
241 double x=0,y=0;
242 for (int j=0; j<afield._nodes_per_elem; j++)
243 {
244 int som=afield._connectivity[afield._nodes_per_elem*i+j];
245 x+=afield._coords[som*2];
246 y+=afield._coords[som*2+1];
247 }
248 x/=afield._nodes_per_elem;
249 y/=afield._nodes_per_elem;
250 afield._field[i]=(((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5))<0.3*0.3)*10;
251 }
252 // Give the field to the problem
253 pb.setInputField("puissance",afield);
254 }
255
256 // Solve the next time step
257 ok=pb.solveTimeStep();
258
259 if (!ok) // The resolution failed, try with a new time interval.
260 {
261 pb.abortTimeStep();
262 dt=pb.computeTimeStep(stop);
263 }
264
265 else // The resolution was successful, validate and go to the
266 // next time step.
267 pb.validateTimeStep();
268 }
269
270 if (!ok) // Impossible to solve the next time step, the Problem
271 break; // has given up
272
273 // Compute the next time step length
274 dt=pb.computeTimeStep(stop);
275
276 // Stop the resolution if the Problem is stationnary
277 if (pb.isStationary())
278 stop=true;
279 statistics().end_count(STD_COUNTERS::timeloop);
280 }
281 statistics().end_timeloop();
283 statistics().print_TU_files("Time loop statistics");
284 pb.terminate();
285}
286
287/**
288 * This one is used to test resetTime()
289 */
290void main_pilote_icoco_3(Probleme_U& pb_to_solve, int nb_pas_dt_reset)
291{
292 ProblemTrio pb;
293 TRUST_set_library_mode(false);
294
295 pb.initialize_pb(pb_to_solve);
296
297 bool stop=false; // Does the Problem want to stop ?
298 bool ok=true; // Is the time interval successfully solved ?
299
300 // Compute the first time step length
301 double dt=pb.computeTimeStep(stop);
302
303 int cnt = 1;
304 bool reset = false;
305 // Loop on the time steps
306 statistics().start_timeloop();
307 while(!stop)
308 {
309 statistics().begin_count(STD_COUNTERS::timeloop);
310 ok=false; // Is the time interval successfully solved ?
311
312 // Loop on the time interval tries
313 while (!ok && !stop)
314 {
315 // Prepare the next time step
316 ok=pb.initTimeStep(dt);
317 if (!ok)
318 break;
319
320 // Solve the next time step
321 ok=pb.solveTimeStep();
322
323 if (!ok) // The resolution failed, try with a new time interval.
324 {
325 pb.abortTimeStep();
326 dt=pb.computeTimeStep(stop);
327 }
328 else // The resolution was successful, validate and go to the
329 // next time step.
330 pb.validateTimeStep();
331 }
332
333 if (!ok) // Impossible to solve the next time step, the Problem
334 break; // has given up
335
336 if (cnt >= nb_pas_dt_reset && !reset)
337 {
338 Cerr << "ABOUT TO RESET TIME !!\n";
339 pb.resetTime(0);
340 Cerr << "RESET TIME DONE!!\n";
341 reset = true;
342 }
343
344 // Compute the next time step length
345 dt=pb.computeTimeStep(stop);
346
347 // Stop the resolution if the Problem is stationnary
348 if (pb.isStationary())
349 stop=true;
350
351 cnt ++;
352 statistics().end_count(STD_COUNTERS::timeloop);
353 }
354 statistics().end_timeloop();
356 statistics().print_TU_files("Time loop statistics");
357
358 pb.terminate();
359}
360
361/**
362 * This one is used to test setInputStringValue("SORTIE_ROOT_DIRECTORY", "./")
363 */
364void main_pilote_icoco_4(Probleme_U& pb_to_solve, int sortie_root_directory, int nstep_stabilized, int nb_pas_dt_reset)
365{
366 ProblemTrio pb;
367 TRUST_set_library_mode(false);
368
369 pb.initialize_pb(pb_to_solve);
370
371 bool stop=false; // Does the Problem want to stop ?
372 bool ok=true; // Is the time interval successfully solved ?
373
374 // Compute the first time step length
375 double dt=pb.computeTimeStep(stop);
376
377 assert(nstep_stabilized < nb_pas_dt_reset);
378 int cnt = 1;
379 bool reset = false;
380 // Loop on the time steps
381 statistics().start_timeloop();
382 while(!stop)
383 {
384 statistics().begin_count(STD_COUNTERS::timeloop);
385 ok=false; // Is the time interval successfully solved ?
386
387 // Loop on the time interval tries
388 while (!ok && !stop)
389 {
390 // Prepare the next time step
391 ok=pb.initTimeStep(dt);
392 if (!ok)
393 break;
394
395 // Solve the next time step
396 ok=pb.solveTimeStep();
397
398 if (!ok) // The resolution failed, try with a new time interval.
399 {
400 pb.abortTimeStep();
401 dt=pb.computeTimeStep(stop);
402 }
403 else // The resolution was successful, validate and go to the
404 // next time step.
405 pb.validateTimeStep();
406 }
407
408 if (!ok) // Impossible to solve the next time step, the Problem
409 break; // has given up
410
411 // stabilized transient step
412 if (cnt <= nstep_stabilized)
413 {
414 if (sortie_root_directory == 1)
415 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "./");
416 else if (sortie_root_directory == 2)
417 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "LATA");
418 pb.resetTime(0.);
419 }
420 // resetime directive
421 if (cnt >= nb_pas_dt_reset && !reset)
422 {
423 if (sortie_root_directory == 1)
424 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "./");
425 else if (sortie_root_directory == 2)
426 pb.setInputStringValue("SORTIE_ROOT_DIRECTORY", "LATA");
427 pb.resetTime(0);
428 reset = true;
429 }
430
431 // Compute the next time step length
432 dt=pb.computeTimeStep(stop);
433
434 // Stop the resolution if the Problem is stationnary
435 if (pb.isStationary())
436 stop=true;
437
438 cnt ++;
439 statistics().end_count(STD_COUNTERS::timeloop);
440 }
441 statistics().end_timeloop();
443 statistics().print_TU_files("Time loop statistics");
444
445 pb.terminate();
446}
447
448
449/*! @brief Main function of the interpreter: solve a problem
450 *
451 * The problem is driven via the ICoCo interface.
452 * The third time step is performed twice.
453 *
454 * @param (Entree& is) an input stream
455 * @return the modified input stream
456 */
457void main_abort( Probleme_U& pb_to_solve)
458{
459 // char* pb_name=nom1.getChar();
460 ProblemTrio pb;
461 TRUST_set_library_mode(false);
462 // pb.setDataFile(pb_name);
463
464 pb.initialize_pb(pb_to_solve);
465
466 bool stop=false; // Does the Problem want to stop ?
467 bool ok=true; // Is the time interval successfully solved ?
468 int n=0;
469 //loop on time step
470 statistics().start_timeloop();
471 while (!stop) // Loop on timesteps
472 {
473 statistics().begin_count(STD_COUNTERS::timeloop);
474 ok=false;
475
476 while (!ok && !stop) // Loop on timestep tries
477 {
478
479 //compute time step length
480 double dt=pb.computeTimeStep(stop);
481
482 if (stop)
483 break;
484
485 //prepare the new time step
486 pb.initTimeStep(dt);
487
488 //---------------------
489 //Solve next time step
490 //---------------------
491
492 {
493 TrioField afield;
494 int nb_elems;
495 pb.getInputFieldTemplate("temperature_bord",afield);
496 afield.set_standalone();
497 nb_elems=afield._nb_elems;
498 for (int i=0; i<nb_elems; i++)
499 afield._field[i]=600;
500 pb.setInputField("temperature_bord",afield);
501 }
502
503
504 bool ok2=pb.solveTimeStep();
505
506 n++;
507 if (!ok2 || n==3) // The resolution failed, try with a new time interval
508 {
509 Cerr<<" call of abortTimeStep" <<finl;
510 pb.abortTimeStep();
511 }
512 else // validate and go to the next time step
513 {
514 pb.validateTimeStep();
515 }
516 } // End loop on timestep size
517 statistics().end_count(STD_COUNTERS::timeloop);
518 } // End loop on timesteps
519 statistics().end_timeloop();
521 statistics().print_TU_files("Time loop statistics");
522 pb.terminate();
523}
524
526{
527 Param param(que_suis_je());
528 Nom nom1;
529 int nb_pas_dt_reset = 1000000000;
530 int nstep_stabilized = 0;
531 int sortie_root_directory = 0;
532 int methode = -1;
533 param.ajouter("pb_name",&nom1,Param::REQUIRED);
534 param.ajouter("main",&methode,Param::REQUIRED);
535 param.dictionnaire("abort_time_step",0);
536 param.dictionnaire("Pilote_ICoCo_1",1);
537 param.dictionnaire("Pilote_ICoCo_2",2);
538 param.dictionnaire("Pilote_ICoCo_3",3);
539 param.dictionnaire("Pilote_ICoCo_4",4);
540 param.ajouter("nb_pas_dt_reset",&nb_pas_dt_reset); // XD_ADD_P int
541 // XD_CONT number of time steps before a single call to resetTime(0) like the Cathare directive (default value
542 // XD_CONT 1000000000)
543 param.ajouter("nstep_stabilized",&nstep_stabilized); // XD_ADD_P int
544 // XD_CONT number of first iterations in which resetTime(0) is called as a Cathare stabilized transient (default value
545 // XD_CONT 0)
546 param.ajouter("sortie_root_directory",&sortie_root_directory); // XD_ADD_P int
547 // XD_CONT if enable, no save post before the resetTime(0) call (default value 0)
549 Probleme_U& pb_to_solve=ref_cast(Probleme_U,objet(nom1));
550 switch (methode)
551 {
552 case 0:
553 main_abort(pb_to_solve);
554 break;
555 case 1:
556 main_pilote_icoco_1(pb_to_solve);
557 break;
558 case 2:
559 main_pilote_icoco_2(pb_to_solve);
560 break;
561 case 3:
562 main_pilote_icoco_3(pb_to_solve, nb_pas_dt_reset);
563 break;
564 case 4:
565 main_pilote_icoco_4(pb_to_solve, sortie_root_directory, nstep_stabilized, nb_pas_dt_reset);
566 break;
567 default:
568 {
569 Cerr<<que_suis_je()<<" main number "<<methode<<" not implemented"<<finl;
570 exit();
571 }
572 }
573 return is;
574}
575
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.
Base class for "interpreter" objects.
Definition Interprete.h:38
static Objet_U & objet(const Nom &)
See Interprete_bloc::objet_global(). BM: the Interprete class is not the best place for this.
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
friend class Entree
Definition Objet_U.h:71
static bool disable_TU
Flag to disable or not the writing of the .TU files.
Definition Objet_U.h:120
const Nom & que_suis_je() const
Returns the string identifying the class.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
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
class Pilote_ICoCo Example of driving TRUST via ICoCo in C++
Entree & interpreter(Entree &) override
Probleme_U.
Definition Probleme_U.h:46
const Nom & le_nom() const override
Returns the name of the Objet_U. Virtual method to override: returns "neant" in this implementation.
Definition Probleme_U.h:109
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
Base class for output streams.
Definition Sortie.h:52