Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
tracer_diffusivity_model.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2022-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_tracer_diffusivity_model_h
5#define lethe_tracer_diffusivity_model_h
6
8
9#include <algorithm>
10
11/**
12 * @brief Abstract class that allows to calculates the
13 * tracer diffusivity.
14 */
16{
17public:
18 /**
19 * @brief Instantiates and returns a pointer to a TracerDiffusivityModel object by casting it to
20 * the proper child class
21 *
22 * @param material_properties Parameters for a material
23 */
24 static std::shared_ptr<TracerDiffusivityModel>
25 model_cast(const Parameters::Material &material_properties);
26};
27
28
29/**
30 * @brief Constant tracer diffusivity.
31 */
33{
34public:
35 /**
36 * @brief Default constructor
37 */
38 ConstantTracerDiffusivity(const double p_tracer_diffusivity)
39 : tracer_diffusivity(p_tracer_diffusivity)
40 {}
41
42 /**
43 * @brief value Calculates the tracer diffusivity
44 * @param fields_value Value of the various fields on which the property may depend.
45 * @return value of the physical property calculated with the fields_value
46 */
47 double
48 value(const std::map<field, double> & /*fields_value*/) override
49 {
50 return tracer_diffusivity;
51 };
52
53 /**
54 * @brief vector_value Calculates the vector of tracer diffusivity.
55 * @param field_vectors Vectors of the fields on which the diffusivity may depend.
56 * @param property_vector Vectors of the tracer diffusivity values
57 */
58 void
59 vector_value(const std::map<field, std::vector<double>> & /*field_vectors*/,
60 std::vector<double> &property_vector) override
61 {
62 std::ranges::fill(property_vector, tracer_diffusivity);
63 }
64
65 /**
66 * @brief jacobian Calculates the jacobian (the partial derivative) of the diffusivity with respect to a field
67 * @param field_values Value of the various fields on which the property may depend.
68 * @param id Indicator of the field with respect to which the jacobian
69 * should be calculated.
70 * @return value of the partial derivative of the diffusivity with respect to the field.
71 */
72
73 double
74 jacobian(const std::map<field, double> & /*field_values*/,
75 field /*id*/) override
76 {
77 return 0;
78 };
79
80 /**
81 * @brief vector_jacobian Calculates the derivative of the tracer diffusivity with respect to a field.
82 * @param field_vectors Vector for the values of the fields used to evaluate the property.
83 * @param id Identifier of the field with respect to which a derivative should be calculated.
84 * @param jacobian vector of the value of the derivative of the tracer diffusivity with respect to the field id.
85 */
86
87 void
89 const std::map<field, std::vector<double>> & /*field_vectors*/,
90 const field /*id*/,
91 std::vector<double> &jacobian_vector) override
92 {
93 std::ranges::fill(jacobian_vector, 0);
94 };
95
96private:
97 const double tracer_diffusivity;
98};
99
100/**
101 * @brief Diffusivity that depends on the tanh of the level set
102 */
104{
105public:
106 /**
107 * @brief Constructor of the level set-dependent diffusivity model.
108 *
109 * @param[in] p_tracer_diffusivity_outside Diffusivity outside the solid
110 * @param[in] p_tracer_diffusivity_inside Diffusivity inside the solid
111 * @param[in] p_thickness Thickness of the tanh function used to smooth the
112 * property jump
113 */
114 TanhLevelsetTracerDiffusivity(const double p_tracer_diffusivity_outside,
115 const double p_tracer_diffusivity_inside,
116 const double p_thickness)
117 : tracer_diffusivity_outside(p_tracer_diffusivity_outside)
118 , tracer_diffusivity_inside(p_tracer_diffusivity_inside)
119 , thickness(p_thickness)
121 {
122 this->model_depends_on[field::levelset] = true;
123 }
124
125 /**
126 * @brief Compute the diffusivity.
127 *
128 * @param[in] field_values Values of the various fields on which the property
129 * may depend. In this case, the diffusivity depends on the level set.
130 * The map stores a single value per field.
131 *
132 * @return Value of the diffusivity computed with the @p field_values.
133 */
134 double
135 value(const std::map<field, double> &field_values) override
136 {
137 Assert(field_values.find(field::levelset) != field_values.end(),
138 PhysicialPropertyModelFieldUndefined("TanhLevelsetTracerDiffusivity",
139 "levelset"));
140 double levelset = field_values.at(field::levelset);
141
143 delta_diffusivity * (0.5 + 0.5 * tanh(levelset / thickness));
144 }
145
146 /**
147 * @brief Compute a vector of diffusivity.
148 *
149 * @param[in] field_vectors Vectors of the fields on which the diffusivity
150 * may depend. In this case, the diffusivity depends on the level set. The map
151 * stores a vector of values per field.
152 *
153 * @param[out] property_vector Vectors of computed diffusivities.
154 */
155 void
156 vector_value(const std::map<field, std::vector<double>> &field_vectors,
157 std::vector<double> &property_vector) override
158 {
159 Assert(field_vectors.find(field::levelset) != field_vectors.end(),
160 PhysicialPropertyModelFieldUndefined("TanhLevelsetTracerDiffusivity",
161 "levelset"));
162
163 const std::vector<double> &levelset_vec = field_vectors.at(field::levelset);
164
165 const unsigned int n_values = property_vector.size();
166
167 Assert(n_values == levelset_vec.size(),
168 SizeOfFields(n_values, levelset_vec.size()));
169
170 for (unsigned int i = 0; i < n_values; ++i)
171 {
172 const double levelset = levelset_vec[i];
173 property_vector[i] =
175 delta_diffusivity * (0.5 + 0.5 * tanh(levelset / thickness));
176 }
177 }
178
179 /**
180 * @brief Compute the jacobian (the partial derivative) of the diffusivity
181 * with respect to a specified field.
182 *
183 * @param[in] field_values Values of the various fields on which the specific
184 * heat may depend. The map stores a single value per field.
185 *
186 * @param[in] id Indicator of the field with respect to which the jacobian
187 * should be computed.
188 *
189 * @return Value of the derivative of the diffusivity with respect to the
190 * specified field.
191 */
192 double
193 jacobian(const std::map<field, double> &field_values, field id) override
194 {
195 if (id == field::levelset)
196 {
197 Assert(field_values.find(field::levelset) != field_values.end(),
198 PhysicialPropertyModelFieldUndefined(
199 "TanhLevelsetTracerDiffusivity", "levelset"));
200 return numerical_jacobian(field_values, field::levelset);
201 }
202 else
203 return 0;
204 };
205
206 /**
207 * @brief Compute the derivative of the diffusivity with respect to a field.
208 *
209 * @param[in] field_vectors Vector of values of the fields used to evaluate
210 * the diffusivity. The map stores a vector of values per field.
211 *
212 * @param[in] id Identifier of the field with respect to which a derivative
213 * should be computed.
214 *
215 * @param[out] jacobian Vector of computed derivative values of the
216 * diffusivity with respect to the field of the specified @p id.
217 */
218 void
219 vector_jacobian(const std::map<field, std::vector<double>> &field_vectors,
220 const field id,
221 std::vector<double> &jacobian_vector) override
222 {
223 if (id == field::levelset)
224 {
225 Assert(field_vectors.find(field::levelset) != field_vectors.end(),
226 PhysicialPropertyModelFieldUndefined(
227 "TanhLevelsetTracerDiffusivity", "levelset"));
228 vector_numerical_jacobian(field_vectors, id, jacobian_vector);
229 }
230 else
231 std::ranges::fill(jacobian_vector, 0.);
232 };
233
234
235private:
238 const double thickness;
239 const double delta_diffusivity;
240};
241
242/**
243 * @brief Diffusivity that depends on the Gaussian of the level set
244 */
246{
247public:
248 /**
249 * @brief Constructor of the Gaussian level set-dependent diffusivity model.
250 *
251 * @param[in] p_tracer_diffusivity_interface Diffusivity at the fluid-solid
252 * interface the solid
253 * @param[in] p_tracer_diffusivity_bulk Diffusivity in the bulk of the phases
254 * @param[in] p_thickness Thickness of the Gaussian function used to smooth
255 * the property jump
256 */
257 GaussianLevelsetTracerDiffusivity(const double p_tracer_diffusivity_interface,
258 const double p_tracer_diffusivity_bulk,
259 const double p_thickness)
260 : tracer_diffusivity_interface(p_tracer_diffusivity_interface)
261 , tracer_diffusivity_bulk(p_tracer_diffusivity_bulk)
263 , squared_thickness(pow(p_thickness, 2))
264 {
265 this->model_depends_on[field::levelset] = true;
266 }
267
268 /**
269 * @brief Compute the diffusivity.
270 *
271 * @param[in] field_values Values of the various fields on which the property
272 * may depend. In this case, the diffusivity depends on the level set.
273 * The map stores a single value per field.
274 *
275 * @return Value of the diffusivity computed with the @p field_values.
276 */
277 double
278 value(const std::map<field, double> &field_values) override
279 {
280 Assert(field_values.find(field::levelset) != field_values.end(),
281 PhysicialPropertyModelFieldUndefined(
282 "GaussianLevelsetTracerDiffusivity", "levelset"));
283 double levelset = field_values.at(field::levelset);
284
286 delta_diffusivity * exp(-(pow(levelset, 2)) / squared_thickness);
287 }
288
289 /**
290 * @brief Compute a vector of diffusivity.
291 *
292 * @param[in] field_vectors Vectors of the fields on which the diffusivity
293 * may depend. In this case, the diffusivity depends on the level set. The map
294 * stores a vector of values per field.
295 *
296 * @param[out] property_vector Vectors of computed diffusivities.
297 */
298 void
299 vector_value(const std::map<field, std::vector<double>> &field_vectors,
300 std::vector<double> &property_vector) override
301 {
302 Assert(field_vectors.find(field::levelset) != field_vectors.end(),
303 PhysicialPropertyModelFieldUndefined(
304 "GaussianLevelsetTracerDiffusivity", "levelset"));
305
306 const std::vector<double> &levelset_vec = field_vectors.at(field::levelset);
307
308 const unsigned int n_values = property_vector.size();
309
310 Assert(n_values == levelset_vec.size(),
311 SizeOfFields(n_values, levelset_vec.size()));
312
313 for (unsigned int i = 0; i < n_values; ++i)
314 {
315 const double levelset = levelset_vec[i];
316 property_vector[i] =
318 delta_diffusivity * exp(-(pow(levelset, 2)) / squared_thickness);
319 ;
320 }
321 }
322
323 /**
324 * @brief Compute the jacobian (the partial derivative) of the diffusivity
325 * with respect to a specified field.
326 *
327 * @param[in] field_values Values of the various fields on which the specific
328 * heat may depend. The map stores a single value per field.
329 *
330 * @param[in] id Indicator of the field with respect to which the jacobian
331 * should be computed.
332 *
333 * @return Value of the derivative of the diffusivity with respect to the
334 * specified field.
335 */
336 double
337 jacobian(const std::map<field, double> &field_values, field id) override
338 {
339 if (id == field::levelset)
340 {
341 Assert(field_values.find(field::levelset) != field_values.end(),
342 PhysicialPropertyModelFieldUndefined(
343 "GaussianLevelsetTracerDiffusivity", "levelset"));
344
345 const double levelset = field_values.at(field::levelset);
346 const double exponential =
347 std::exp(-(levelset * levelset) / squared_thickness);
348
349 return delta_diffusivity * exponential *
350 (-2.0 * levelset / squared_thickness);
351 }
352 else
353 return 0.0;
354 }
355
356 /**
357 * @brief Compute the derivative of the diffusivity with respect to a field.
358 *
359 * @param[in] field_vectors Vector of values of the fields used to evaluate
360 * the diffusivity. The map stores a vector of values per field.
361 *
362 * @param[in] id Identifier of the field with respect to which a derivative
363 * should be computed.
364 *
365 * @param[out] jacobian Vector of computed derivative values of the
366 * diffusivity with respect to the field of the specified @p id.
367 */
368 void
369 vector_jacobian(const std::map<field, std::vector<double>> &field_vectors,
370 const field id,
371 std::vector<double> &jacobian_vector) override
372 {
373 if (id == field::levelset)
374 {
375 Assert(field_vectors.find(field::levelset) != field_vectors.end(),
376 PhysicialPropertyModelFieldUndefined(
377 "GaussianLevelsetTracerDiffusivity", "levelset"));
378
379 const std::vector<double> &levelset_vec =
380 field_vectors.at(field::levelset);
381 const unsigned int n = jacobian_vector.size();
382
383 for (unsigned int i = 0; i < n; ++i)
384 {
385 const double levelset = levelset_vec[i];
386 const double exponential =
387 std::exp(-pow(levelset, 2) / squared_thickness);
388 jacobian_vector[i] = delta_diffusivity * exponential *
389 (-2.0 * levelset / squared_thickness);
390 }
391 }
392 else
393 std::ranges::fill(jacobian_vector, 0.0);
394 }
395
396private:
399 const double delta_diffusivity;
400 const double squared_thickness;
401};
402
403#endif
void vector_value(const std::map< field, std::vector< double > > &, std::vector< double > &property_vector) override
vector_value Calculates the vector of tracer diffusivity.
double jacobian(const std::map< field, double > &, field) override
jacobian Calculates the jacobian (the partial derivative) of the diffusivity with respect to a field
void vector_jacobian(const std::map< field, std::vector< double > > &, const field, std::vector< double > &jacobian_vector) override
vector_jacobian Calculates the derivative of the tracer diffusivity with respect to a field.
ConstantTracerDiffusivity(const double p_tracer_diffusivity)
Default constructor.
double value(const std::map< field, double > &) override
value Calculates the tracer diffusivity
void vector_jacobian(const std::map< field, std::vector< double > > &field_vectors, const field id, std::vector< double > &jacobian_vector) override
Compute the derivative of the diffusivity with respect to a field.
double value(const std::map< field, double > &field_values) override
Compute the diffusivity.
GaussianLevelsetTracerDiffusivity(const double p_tracer_diffusivity_interface, const double p_tracer_diffusivity_bulk, const double p_thickness)
Constructor of the Gaussian level set-dependent diffusivity model.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Compute a vector of diffusivity.
double jacobian(const std::map< field, double > &field_values, field id) override
Compute the jacobian (the partial derivative) of the diffusivity with respect to a specified field.
Material - Class that defines the physical property of a material. Generally a material will be a flu...
Definition parameters.h:594
void vector_numerical_jacobian(const std::map< field, std::vector< double > > &field_vectors, const field id, std::vector< double > &jacobian_vector)
vector_numerical_jacobian Calculates the vector of jacobian through forward finite difference (Euler)...
double numerical_jacobian(const std::map< field, double > &field_values, const field id)
numerical_jacobian Calculates the jacobian through a forward finite difference (Euler) approximation....
PhysicalPropertyModel()
Default constructor. Set the model_depends_on to false for all variables.
std::map< field, bool > model_depends_on
TanhLevelsetTracerDiffusivity(const double p_tracer_diffusivity_outside, const double p_tracer_diffusivity_inside, const double p_thickness)
Constructor of the level set-dependent diffusivity model.
void vector_jacobian(const std::map< field, std::vector< double > > &field_vectors, const field id, std::vector< double > &jacobian_vector) override
Compute the derivative of the diffusivity with respect to a field.
double jacobian(const std::map< field, double > &field_values, field id) override
Compute the jacobian (the partial derivative) of the diffusivity with respect to a specified field.
double value(const std::map< field, double > &field_values) override
Compute the diffusivity.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Compute a vector of diffusivity.
Abstract class that allows to calculates the tracer diffusivity.
static std::shared_ptr< TracerDiffusivityModel > model_cast(const Parameters::Material &material_properties)
Instantiates and returns a pointer to a TracerDiffusivityModel object by casting it to the proper chi...