TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Source_base.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 <Schema_Temps_base.h>
17#include <Equation_base.h>
18#include <Probleme_base.h>
19#include <Process.h>
20#include <Source_base.h>
21#include <TRUSTTrav.h>
22#include <TRUSTTabs.h>
23#include <SFichier.h>
24#include <map>
25
26Implemente_base(Source_base,"Source_base",Objet_U);
27// XD source_base objet_u source_base INHERITS_BRACE Basic class of source terms introduced in the equation.
28
29/*! @brief DOES NOTHING - to override in derived classes.
30 *
31 * Prints the source to an output stream.
32 *
33 * @param (Sortie& os) the output stream
34 * @return (Sortie&) the modified output stream
35 */
37{
38 return os;
39}
40
41/*! @brief DOES NOTHING - to override in derived classes.
42 *
43 * Reading of a source term from an input stream.
44 *
45 * @param (Entree& is) the input stream
46 * @return (Entree&) the modified input stream
47 */
49{
50 return is;
51}
52
53/*! @brief DOES NOTHING - to override in derived classes.
54 *
55 * Time update of the source term.
56 * @param (double) the time step of the update
57 */
59{
60 Cerr << finl;
61 Cerr << "You must to overload the method Source_base::mettre_a_jour() because there's a big" << finl;
62 Cerr << "chance that the source term " << que_suis_je() << " must be updated," << finl;
63 Cerr << "especially if your source has a field, you must update it..." << finl << finl;
64 exit();
65}
66
67/*! @brief DOES NOTHING - to override in derived classes.
68 *
69 * Reset current time.
70 * @param (double) new current time to be set.
71 */
73{
74 Cerr << finl;
75 Cerr << "You must to overload the method Source_base::resetTime() because there's big" << finl;
76 Cerr << "chance that the source term " << que_suis_je() << " must be reset," << finl;
77 Cerr << "especially if your source has a field, you must reset its current time..." << finl << finl;
78 exit();
79}
80
81/*! @brief Updates internal references of the Source_base object.
82 *
83 * Calls 2 protected pure virtual methods:
84 * Source_base::associer_domaines(const Domaine_dis_base& ,const Domaine_Cl_dis_base&)
85 * Source_base::associer_pb(const Probleme_base&)
86 *
87 */
89{
90 const Equation_base& eqn = equation();
91 const Domaine_dis_base& zdis= eqn.domaine_dis();
92 const Domaine_Cl_dis_base& zcldis = eqn.domaine_Cl_dis();
93 associer_domaines(zdis, zcldis);
94 associer_pb(eqn.probleme());
95 // Initialize the bilan_ array:
96 bilan_.resize(eqn.inconnue().nb_comp());
97 bilan_=0;
98 for (auto& itr : col_names_)
99 col_width_ = std::max(col_width_, itr.longueur());
100}
101
102/*! @brief This method (or the method of the derived class) is called by Sources::associer_champ_rho for each source in the list
103 *
104 * (for example, at the initialization of a front-tracking calculation).
105 * This method must be re-implemented in the derived classes
106 * used in problems with variable rho.
107 *
108 * The ajouter method calculates the following term:
109 * INTEGRAL (source term)
110 * over interleaved volume
111 *
112 * In problems where rho is variable, "source term" homogeneous to rho*v.
113 * Otherwise, "source term" is homogeneous to v.
114 *
115 */
117{
118 Cerr << "In Source_base::associer_champ_rho" << finl;
119 Cerr << " field : " << champ_rho.le_nom() << finl;
120 Cerr << " Source of type : " << que_suis_je() << finl;
121 Cerr << " This source does not support the association of a field rho." << finl;
122 Cerr << " (method associer_champ_rho must be coded for this source term)." << finl;
123 assert(0);
124 exit();
125}
126
127/*! @brief If the source understands the keyword "mot", it fills the reference to ch_ref and returns 1, otherwise returns 0 (see Source_Translation for example)
128 *
129 * )
130 *
131 */
133{
134 // The base class understands no keywords
135 return 0;
136}
137
139{
140 return champs_compris_.get_champ(nom);
141}
142
143bool Source_base::has_champ(const Motcle& nom, OBS_PTR(Champ_base) &ref_champ) const
144{
145 return champs_compris_.has_champ(nom, ref_champ);
146}
147
148bool Source_base::has_champ(const Motcle& nom) const
149{
150 return champs_compris_.has_champ(nom);
151}
152
154{
155 if (opt == DESCRIPTION)
156 Cerr << que_suis_je() << " : " << champs_compris_.liste_noms_compris() << finl;
157 else
158 nom.add(champs_compris_.liste_noms_compris());
159}
160
161/*! @brief DOES NOTHING - to override in derived classes.
162 *
163 * Time update of the source term.
164 *
165 * @param (double) the time step of the update
166 */
167static std::map<std::string, int> counters; // Todo provisional
169{
170 if (out_=="??")
171 {
172 //Cerr << "No balance printed for " << que_suis_je() << finl;
173 }
174 else
175 {
176 int nb_compo=bilan_.size();
177 if (nb_compo==0)
178 {
179 Cerr << "No balance printed for " << que_suis_je() << finl;
180 Cerr << "cause bilan_ array is not filled." << finl;
181 }
182 else
183 {
184 int flag=je_suis_maitre();
185 ouvrir_fichier(Flux,"",flag);
186 const Probleme_base& pb=equation().probleme();
187 const Schema_Temps_base& sch=pb.schema_temps();
188 double temps=sch.temps_courant();
189
191 Flux.add_col(temps);
192 DoubleVect bilan_p(bilan_);
193 mp_sum_for_each_item(bilan_p);
194 /*
195 for(int k=0; k<nb_compo; k++)
196 bilan_(k)=Process::mp_sum(bilan_(k)); */
197 // Replaced by:
198 // mp_sum_for_each_item(bilan_); // Fixed bug: double line!
199
201 {
202 for(int k=0; k<nb_compo; k++)
203 Flux.add_col(bilan_p(k));
204 Flux << finl;
205 }
206 }
207 }
208 return 1;
209}
210/*! @brief Sizing of the implicit matrix of source terms.
211 *
212 * By default does nothing.
213 *
214 */
219
221{
222 if (has_interface_blocs()) dimensionner_blocs({{ "vitesse", &mat }});
223}
224
225DoubleTab& Source_base::ajouter(DoubleTab& secmem) const
226{
227 if (has_interface_blocs()) ajouter_blocs({}, secmem, {});
228 else Process::exit(que_suis_je() + " : ajouter() not coded!");
229 return secmem;
230}
231
232DoubleTab& Source_base::calculer(DoubleTab& secmem) const
233{
234 secmem = 0;
235 return ajouter(secmem);
236}
237
238/*! @brief contribution to the implicit matrix of source terms, by default no contribution
239 *
240 */
241void Source_base::contribuer_a_avec(const DoubleTab&, Matrice_Morse& mat) const
242{
243 if (!has_interface_blocs()) return;
244 DoubleTrav secmem(equation().inconnue().valeurs()); //sera jete
245 ajouter_blocs({{ equation().inconnue().le_nom().getString(), &mat}}, secmem, {});
246}
247
248/*! @brief contribution to the right-hand side of source terms implicitly, by default error
249 *
250 * method presented for consistency with Operateur_base
251 *
252 */
254{
255 Cerr<<"Source_base::contribuer_au_second_membre(DoubleTab& ) const uncoded"<<finl;
256 exit();
257}
258
259/* by default error */
260void Source_base::dimensionner_blocs(matrices_t matrices, const tabs_t& semi_impl) const
261{
262 Process::exit(que_suis_je() + " : dimensionner_blocs() not coded!");
263}
264void Source_base::ajouter_blocs(matrices_t matrices, DoubleTab& secmem, const tabs_t& semi_impl) const
265{
266 Process::exit(que_suis_je() + " : ajouter_blocs() not coded!");
267}
268
269/*! @brief Unlike the mettre_a_jour methods, the initializer methods of sources cannot depend on the outside
270 *
271 * (itself may not be initialized)
272 * By default, mettre_a_jour
273 *
274 */
276{
277 mettre_a_jour(temps);
278 return 1;
279}
280
281
282/*! @brief Opening/creation of a file for printing a source term. To override in derived classes.
283 *
284 * @throws method to override
285 */
286void Source_base::ouvrir_fichier(SFichier& os,const Nom& type, const int flag) const
287{
288 // null flag means we don't open the file
289 if (flag==0)
290 return ;
291 // Todo provisional:
292 counters[type.getString()]++;
293 if (counters[type.getString()]>1 && type!="")
294 {
295 Cerr << "Code should be rewritten to have only one call to Source_base::ouvrir_fichier for " << type << " source and not " << counters[type.getString()] << " times." << finl;
297 }
298 const Probleme_base& pb=equation().probleme();
299 const Schema_Temps_base& sch=pb.schema_temps();
300 const int precision = sch.precision_impr(), wcol = std::max(col_width_, sch.wcol()), gnuplot_header = sch.gnuplot_header();
301 os.set_col_width(wcol);
302
303 Nom nomfichier(out_);
304 if (type!="") nomfichier+=(Nom)"_"+type;
305 nomfichier+=".out";
306
307 // Create the file on the first printing with the header
308 if (sch.nb_impr()==1 && !pb.reprise_effectuee())
309 {
310 os.ouvrir(nomfichier);
311 SFichier& fic=os;
312 //Nom espace="\t\t";
313 fic << (Nom)"# Printing of the source term "+que_suis_je()+" of the equation "+equation().que_suis_je()+" of the problem "+equation().probleme().le_nom() << finl;
314 fic << "# " << description() << finl;
315 if (!gnuplot_header) fic << "#";
316 os.set_col_width(wcol - !gnuplot_header);
317 fic.add_col("Time");
318 os.set_col_width(wcol);
319 for (int i = 0; i < col_names_.size(); i++)
320 fic.add_col(col_names_[i]);
321 fic << finl;
322 }
323 // Otherwise open it
324 else
325 {
326 os.ouvrir(nomfichier,ios::app);
327 }
328 os.precision(precision);
329 os.setf(ios::scientific);
330}
331
333{
335 out_+="_";
336 out_+=equation().probleme().le_nom()+"_"+nom;
337}
338
class Champ_base This class is the base of the fields hierarchy.
Definition Champ_base.h:43
class Domaine_Cl_dis_base Domaine_Cl_dis_base objects represent discretized boundary conditions
class Domaine_dis_base This class is the base of the hierarchy of discretized domains.
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
class Equation_base The role of an equation is the calculation of one or more fields....
virtual const Champ_Inc_base & inconnue() const =0
virtual Domaine_Cl_dis_base & domaine_Cl_dis()
Returns the discretized boundary condition domain associated with the equation.
Probleme_base & probleme()
Returns the problem associated with the equation.
Domaine_dis_base & domaine_dis()
Returns the discretized domain associated with the equation.
const Nom & le_nom() const override
Returns the name of the field.
virtual int nb_comp() const
Definition Field_base.h:56
Matrice_Morse class - Represents a (sparse) matrix M, not necessarily square,.
const Equation_base & equation() const
Returns the reference to the equation pointed to by MorEqn::mon_equation.
Definition MorEqn.h:62
OBS_PTR(Equation_base) mon_equation
A character string (Nom) in uppercase.
Definition Motcle.h:26
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
const std::string & getString() const
Definition Nom.h:92
An array of character strings (VECT(Nom)).
Definition Noms.h:26
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
friend class Sortie
Definition Objet_U.h:70
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
static const Nom & nom_du_cas()
Returns a constant reference to the case name. This method is static.
Definition Objet_U.cpp:145
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
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
class Probleme_base It is a Probleme_U that is not a coupling.
bool & reprise_effectuee()
const Schema_Temps_base & schema_temps() const
Returns the time scheme associated with the problem.
static void mp_sum_for_each_item(TRUSTArray< _TYPE_ > &x, int n=-1)
Definition Process.cpp:194
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
static int je_suis_maitre()
Returns 1 if on the master processor of the current group (i.e. me() == 0), 0 otherwise.
Definition Process.cpp:82
SFichier is to the C++ ofstream class what Sortie is to the C++ ostream class.
Definition SFichier.h:29
class Schema_Temps_base
int nb_impr() const
Returns the number of outputs performed.
double temps_courant() const
Returns the current time.
virtual int ouvrir(const char *name, IOS_OPEN_MODE mode=ios::out)
void precision(int pre) override
void setf(IOS_FORMAT code) override
Base class for output streams.
Definition Sortie.h:52
void set_col_width(int w)
Definition Sortie.h:66
virtual int add_col(const double ob)
Definition Sortie.cpp:83
Source_base A Source_base object is a term appearing on the right-hand side of an.
Definition Source_base.h:42
void set_fichier(const Nom &)
virtual void mettre_a_jour(double temps)
DOES NOTHING - to override in derived classes.
virtual void associer_pb(const Probleme_base &)=0
Champs_compris champs_compris_
const Champ_base & get_champ(const Motcle &nom) const override
virtual void associer_domaines(const Domaine_dis_base &, const Domaine_Cl_dis_base &)=0
bool has_champ(const Motcle &nom, OBS_PTR(Champ_base) &ref_champ) const override
const Nom description() const
Definition Source_base.h:86
virtual int impr(Sortie &os) const
virtual void resetTime(double t)
DOES NOTHING - to override in derived classes.
virtual void associer_champ_rho(const Champ_base &champ_rho)
This method (or the method of the derived class) is called by Sources::associer_champ_rho for each so...
virtual DoubleTab & calculer(DoubleTab &) const
virtual void ouvrir_fichier(SFichier &os, const Nom &, const int flag=1) const
Opening/creation of a file for printing a source term. To override in derived classes.
virtual void contribuer_au_second_membre(DoubleTab &) const
contribution to the right-hand side of source terms implicitly, by default error
void get_noms_champs_postraitables(Noms &nom, Option opt=NONE) const override
virtual int initialiser(double temps)
Unlike the mettre_a_jour methods, the initializer methods of sources cannot depend on the outside.
virtual void contribuer_a_avec(const DoubleTab &, Matrice_Morse &) const
contribution to the implicit matrix of source terms, by default no contribution
virtual void dimensionner_blocs(matrices_t matrices, const tabs_t &semi_impl={ }) const
virtual void dimensionner_bloc_vitesse(Matrice_Morse &) const
virtual void completer()
Updates internal references of the Source_base object.
virtual int has_interface_blocs() const
Definition Source_base.h:68
SFichier Flux
virtual DoubleTab & ajouter(DoubleTab &) const
DoubleVect bilan_
virtual int a_pour_Champ_Fonc(const Motcle &mot, OBS_PTR(Champ_base) &ch_ref) const
If the source understands the keyword "mot", it fills the reference to ch_ref and returns 1,...
virtual void dimensionner(Matrice_Morse &) const
Sizing of the implicit matrix of source terms.
virtual void ajouter_blocs(matrices_t matrices, DoubleTab &secmem, const tabs_t &semi_impl={ }) const