1 /*============================================================================
2  * User functions for input of calculation parameters.
3  *============================================================================*/
4 
5 /* VERS */
6 
7 /*
8   This file is part of Code_Saturne, a general-purpose CFD tool.
9 
10   Copyright (C) 1998-2021 EDF S.A.
11 
12   This program is free software; you can redistribute it and/or modify it under
13   the terms of the GNU General Public License as published by the Free Software
14   Foundation; either version 2 of the License, or (at your option) any later
15   version.
16 
17   This program is distributed in the hope that it will be useful, but WITHOUT
18   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20   details.
21 
22   You should have received a copy of the GNU General Public License along with
23   this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24   Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 
27 /*----------------------------------------------------------------------------*/
28 
29 #include "cs_defs.h"
30 
31 /*----------------------------------------------------------------------------
32  * Standard C library headers
33  *----------------------------------------------------------------------------*/
34 
35 #include <assert.h>
36 #include <math.h>
37 #include <string.h>
38 
39 #if defined(HAVE_MPI)
40 #include <mpi.h>
41 #endif
42 
43 /*----------------------------------------------------------------------------
44  * PLE library headers
45  *----------------------------------------------------------------------------*/
46 
47 #include <ple_coupling.h>
48 
49 /*----------------------------------------------------------------------------
50  * Local headers
51  *----------------------------------------------------------------------------*/
52 
53 #include "cs_headers.h"
54 
55 /*----------------------------------------------------------------------------*/
56 
57 BEGIN_C_DECLS
58 
59 /*----------------------------------------------------------------------------*/
60 /*!
61  * \file cs_user_parameters-cdo-solidification.c
62  *
63  * \brief User functions for setting a calculation using the solidification
64  *        module with CDO schemes
65  *
66  * See \ref parameters for examples.
67  */
68 /*----------------------------------------------------------------------------*/
69 
70 /*============================================================================
71  * Private function prototypes
72  *============================================================================*/
73 
74 /*============================================================================
75  * User function definitions
76  *============================================================================*/
77 
78 /*----------------------------------------------------------------------------*/
79 /*!
80  * \brief Select physical model options, including user fields.
81  *
82  * This function is called at the earliest stages of the data setup,
83  * so field ids are not available yet.
84  */
85 /*----------------------------------------------------------------------------*/
86 
87 void
cs_user_model(void)88 cs_user_model(void)
89 {
90   cs_domain_t  *domain = cs_glob_domain;
91 
92   /* ======================
93      Boundary of the domain
94      ====================== */
95 
96   cs_boundary_t  *bdy = domain->boundaries;
97 
98   /* Choose a boundary by default */
99 
100   cs_boundary_set_default(bdy, CS_BOUNDARY_SYMMETRY);
101 
102   /* Add new boundaries */
103 
104   cs_boundary_add(bdy, CS_BOUNDARY_WALL, "left");
105   cs_boundary_add(bdy, CS_BOUNDARY_WALL, "right");
106   cs_boundary_add(bdy, CS_BOUNDARY_WALL, "top");
107   cs_boundary_add(bdy, CS_BOUNDARY_WALL, "bottom");
108 
109   /* Activate CDO/HHO module so that main additional structure are built */
110 
111   cs_domain_set_cdo_mode(domain, CS_DOMAIN_CDO_MODE_ONLY);
112 
113   /* ===============================
114      Define the solidification model
115      =============================== */
116 
117   /* 1. Activate the solidification module */
118   /* ------------------------------------- */
119 
120   /*! [param_cdo_activate_solidification] */
121   {
122     /* For the solidification module:
123        cs_solidification_activate(solidification_model_type,
124                                   solid_option_flag,
125                                   solid_post_flag,
126                                   boundaries,
127                                   navsto_model,
128                                   navsto_model_flag,
129                                   navsto_coupling_type,
130                                   navsto_post_flag);
131 
132        If a flag is set to 0, then there is no option to add.
133        To add options to a flag:
134        flag = option1 | option2 | option3 | ...
135     */
136 
137     cs_flag_t  solid_option_flag = 0;
138 
139     cs_flag_t  solid_post_flag = CS_SOLIDIFICATION_POST_SOLIDIFICATION_RATE;
140 
141     cs_flag_t  navsto_model_flag = 0;
142 
143     cs_flag_t  navsto_post_flag = 0     |
144       CS_NAVSTO_POST_VELOCITY_DIVERGENCE |
145       CS_NAVSTO_POST_MASS_DENSITY;
146 
147     /* Activate the solidification module with a binary alloy model (the
148        Navier-Stokes and the thermal modules are also activated in back-end) */
149 
150     cs_solidification_activate(/* Main solidification model */
151                                CS_SOLIDIFICATION_MODEL_BINARY_ALLOY,
152                                /* Solidification options */
153                                solid_option_flag,
154                                /* Solidification automatic post options */
155                                solid_post_flag,
156                                /* NavSto parameters */
157                                domain->boundaries,
158                                CS_NAVSTO_MODEL_INCOMPRESSIBLE_NAVIER_STOKES,
159                                navsto_model_flag,
160                                CS_NAVSTO_COUPLING_MONOLITHIC,
161                                navsto_post_flag);
162 
163   }
164   /*! [param_cdo_activate_solidification] */
165 
166   /*! [param_cdo_solidification_set_voller] */
167   {
168     /* Physical data for the settings a binay alloy model */
169 
170     cs_real_t  T0 = 0.5, beta_t = 0.01;
171     cs_real_t  t_solidus = -0.1, t_liquidus = 0.1;
172     cs_real_t  latent_heat = 5, s_das = 0.33541;
173 
174     /* Set the parameters for the Voller & Prakash model */
175 
176     cs_solidification_set_voller_model(/* Boussinesq approximation */
177                                        beta_t,
178                                        T0,
179                                        /* Phase diagram */
180                                        t_solidus,
181                                        t_liquidus,
182                                        /* Physical constants */
183                                        latent_heat,
184                                        s_das);
185 
186 
187     /* If the flag options CS_SOLIDIFICATION_NO_VELOCITY_FIELD has been set,
188        then one can used a simplified version of the function */
189 
190     cs_solidification_set_voller_model_no_velocity(/* Phase diagram */
191                                                    t_solidus,
192                                                    t_liquidus,
193                                                    /* Physical constants */
194                                                    latent_heat);
195   }
196   /*! [param_cdo_solidification_set_voller] */
197 
198   /*! [param_cdo_solidification_set_binary_alloy] */
199   {
200     /* Physical data for the settings a binay alloy model */
201 
202     cs_real_t  T0 = 0.5, beta_t = 0.01;
203     cs_real_t  conc0 = 1.0, beta_c = 0.01;
204     cs_real_t  ml = -0.1, kp = 0.1;
205     cs_real_t  t_eutec = -0.1, t_melt = 0.2;
206     cs_real_t  diff_val = 0;
207     cs_real_t  latent_heat = 5;
208     cs_real_t  s_das = 0.33541;
209 
210     /* Set the parameters for the binary alloy model */
211 
212     cs_solidification_set_binary_alloy_model("C_solute", "C_bulk",
213                                              /* Boussinesq approximation */
214                                              beta_t,
215                                              T0,
216                                              beta_c,
217                                              conc0,
218                                              /* Phase diagram */
219                                              kp,
220                                              ml,
221                                              t_eutec,
222                                              t_melt,
223                                              /* Solute transport equation */
224                                              diff_val,
225                                              /* Physical constants */
226                                              latent_heat,
227                                              s_das);
228 
229   }
230   /*! [param_cdo_solidification_set_binary_alloy] */
231 
232 }
233 
234 /*----------------------------------------------------------------------------*/
235 /*!
236  * \brief Define or modify general numerical and physical user parameters.
237  *
238  * At the calling point of this function, most model-related variables
239  * and other fields have been defined, so specific settings related to those
240  * fields may be set here.
241  */
242 /*----------------------------------------------------------------------------*/
243 
244 void
cs_user_parameters(cs_domain_t * domain)245 cs_user_parameters(cs_domain_t    *domain)
246 {
247   CS_UNUSED(domain);
248 
249   /*! [param_cdo_solidification_set_strategy] */
250   {
251     cs_solidification_set_strategy(CS_SOLIDIFICATION_STRATEGY_PATH);
252   }
253   /*! [param_cdo_solidification_set_strategy] */
254 
255   /*! [param_cdo_solidification_nl_voller_advanced] */
256   {
257     cs_solidification_voller_t
258       *model_struct = cs_solidification_get_voller_struct();
259 
260     int  n_iter_max = 20;
261     double  rel_tolerance = 1e-3;
262 
263     /* Drive the convergence of the non-linear algorithm to update the thermal
264      * source term. */
265 
266     model_struct->nl_algo->param.n_max_algo_iter = n_iter_max;
267     model_struct->nl_algo->param.rtol = rel_tolerance;
268   }
269   /*! [param_cdo_solidification_nl_voller_advanced] */
270 
271   /*! [param_cdo_solidification_binary_advanced] */
272   {
273     cs_solidification_binary_alloy_t
274       *model_struct = cs_solidification_get_binary_alloy_struct();
275 
276     int  n_iter_max = 10;
277     double  delta_eps = 1e-3;
278 
279     /* Drive the convergence of the coupled system (solute transport and thermal
280      * equation) with respect to the following criteria (taken from Voller and
281      * Swaminathan'91)
282      *   max_{c\in C} |Temp^(k+1) - Temp^(k)| < delta_tolerance
283      *   max_{c\in C} |Cbulk^(k+1) - Cbulk*^(k)| < delta_tolerance
284      *   n_iter < n_iter_max
285      */
286 
287     model_struct->n_iter_max = n_iter_max;
288     model_struct->delta_tolerance = delta_eps;
289   }
290   /*! [param_cdo_solidification_binary_advanced] */
291 }
292 
293 /*----------------------------------------------------------------------------*/
294 /*!
295  * \brief  Specify the elements such as properties, advection fields,
296  *         user-defined equations and modules which have been previously added.
297  *
298  * \param[in, out]   domain    pointer to a cs_domain_t structure
299 */
300 /*----------------------------------------------------------------------------*/
301 
302 void
cs_user_finalize_setup(cs_domain_t * domain)303 cs_user_finalize_setup(cs_domain_t   *domain)
304 {
305   CS_UNUSED(domain);
306 
307   /*! [param_cdo_solidification_properties] */
308   {
309     /* All the following properties are isotropic and set on all the mesh cells
310        (this implies NULL for the second argument). If the property is
311        piecewise constant, then replace NULL by the name of a volum zone. */
312 
313     /* Mass density (kg.m^-3) */
314 
315     cs_property_t  *rho = cs_property_by_name(CS_PROPERTY_MASS_DENSITY);
316     cs_real_t  rho0 = 1;
317     cs_property_def_iso_by_value(rho, NULL, rho0);
318 
319     /* Laminar dynamic viscosity (Pa.s) */
320 
321     cs_property_t  *mu = cs_property_by_name(CS_NAVSTO_LAM_VISCOSITY);
322     cs_real_t  mu0 = 1;
323     cs_property_def_iso_by_value(mu, NULL, mu0);
324 
325     /* Thermal heat capacity */
326 
327     cs_property_t  *cp = cs_property_by_name(CS_THERMAL_CP_NAME);
328     cs_real_t  cp0 = 1.;
329     cs_property_def_iso_by_value(cp, NULL, cp0);
330 
331     /* Thermal conductivity */
332 
333     cs_property_t  *lambda = cs_property_by_name(CS_THERMAL_LAMBDA_NAME);
334     cs_real_t  lambda0 = 0.001;
335     cs_property_def_iso_by_value(lambda, NULL, lambda0);
336   }
337   /*! [param_cdo_solidification_properties] */
338 
339 
340   /*! [param_cdo_solidification_thermal_eq] */
341   {
342     cs_real_t  t_ref = 0.5;
343 
344     cs_equation_param_t  *th_eqp = cs_equation_param_by_name(CS_THERMAL_EQNAME);
345 
346     /* Set the initial value for the temperature */
347 
348     cs_equation_add_ic_by_value(th_eqp, NULL, &t_ref);
349 
350     /* Set the value of the boundary conditions.
351      *
352      * The other boundary zones are associated to the default boundary (by
353      * default, the thermal equation is associated to a no flux (Homogeneous
354      * Neumann boundary condition) */
355 
356     cs_real_t  Th = t_ref, Tc = -t_ref;
357     cs_equation_add_bc_by_value(th_eqp, CS_PARAM_BC_DIRICHLET, "left", &Tc);
358     cs_equation_add_bc_by_value(th_eqp, CS_PARAM_BC_DIRICHLET, "right", &Th);
359   }
360   /*! [param_cdo_solidification_thermal_eq] */
361 
362 }
363 /*----------------------------------------------------------------------------*/
364 
365 END_C_DECLS
366