Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
density_model.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_density_model_h
5#define lethe_density_model_h
6
8
9#include <algorithm>
10
11/**
12 * @brief Abstract class for calculating the density of materials.
13 */
15{
16public:
17 /**
18 * @brief Instantiate and return a pointer to a DensityModel object by
19 * casting it to the proper child class.
20 *
21 * @param[in] material_properties Property parameters of a material (fluid or
22 * solid).
23 *
24 * @return Casted DensityModel object.
25 */
26 static std::shared_ptr<DensityModel>
27 model_cast(const Parameters::Material &material_properties);
28
29
30 /**
31 * @brief Get the value of the compressibility factor used in the
32 * density model.
33 *
34 * @return Value of the compressibility factor.
35 */
36 virtual double
37 get_psi() const = 0;
38
39 /**
40 * @brief Get the value of the reference state density
41 * used in the density model.
42 *
43 * @return Value of the reference state density.
44 */
45 virtual double
46 get_density_ref() const = 0;
47
48 /**
49 * @brief Check if the model is a constant density model.
50 *
51 * @return Boolean indicating if the model corresponds to a constant
52 * density model.
53 */
54 bool
59
60protected:
61 /// Boolean indicating if the model corresponds to a constant density model
63};
64
65
66/**
67 * @brief Constant density model.
68 */
70{
71public:
72 /**
73 * @brief Constructor of the constant density model.
74 *
75 * @param[in] p_density Constant density value.
76 */
77 DensityConstant(const double p_density)
78 : density(p_density)
79 {
80 this->constant_density_model = true;
81 }
82
83 /**
84 * @brief Compute density value.
85 *
86 * @param[in] field_values Value of the various fields on which the property
87 * may depend. The map stores a single value per field.
88 *
89 * @note Here, the @p field_values parameter is ignored since the density
90 * remains constant.
91 *
92 * @return Density value.
93 */
94 double
95 value(const std::map<field, double> &field_values) override
96 {
97 (void)field_values;
98 return density;
99 }
100
101 /**
102 * @brief Compute a vector of density values.
103 *
104 * @param[in] field_vectors Vectors of the fields on which the density may
105 * depend. The map stores a vector of values per field.
106 *
107 * @param[out] property_vector Vector of computed density values.
108 *
109 * @note Here, the @p field_vectors parameter is ignored since the density
110 * remains constant.
111 */
112 void
113 vector_value(const std::map<field, std::vector<double>> &field_vectors,
114 std::vector<double> &property_vector) override
115 {
116 (void)field_vectors;
117 std::ranges::fill(property_vector, density);
118 }
119
120 /**
121 * @brief Compute the jacobian (the partial derivative) of the density with
122 * respect to a specified field.
123 *
124 * @param[in] field_values Values of the various fields on which the density
125 * may depend. The map stores a single value per field.
126 *
127 * @param[in] id Indicator of the field with respect to which the jacobian
128 * should be computed.
129 *
130 * @note Here, the @p field_values and @p id parameters are ignored since the
131 * density remains constant.
132 *
133 * @return Value of the derivative of the density with respect to the
134 * specified field. Since the density remains constant, this function returns
135 * zero.
136 */
137 double
138 jacobian(const std::map<field, double> &field_values, const field id) override
139 {
140 (void)field_values;
141 (void)id;
142 return 0.0;
143 }
144
145 /**
146 * @brief Computes the derivative of the density with respect to specified
147 * fields.
148 *
149 * @param[in] field_vectors Vector of values of the fields used to evaluate
150 * the density. The map stores a vector of values per field.
151 *
152 * @param[in] id Identifier of the field with respect to which a derivative
153 * should be computed.
154 *
155 * @param[out] jacobian_vector Vector of computed derivative values of the
156 * density
157 * with respect to the field of the specified @p id. In this case, it returns
158 * a vector of zeros since the density remains constant.
159 *
160 * @note Here, the @p field_vectors and @p id parameters are ignored since the
161 * density remains constant.
162 *
163 */
164 void
165 vector_jacobian(const std::map<field, std::vector<double>> &field_vectors,
166 const field id,
167 std::vector<double> &jacobian_vector) override
168 {
169 (void)field_vectors;
170 (void)id;
171 std::ranges::fill(jacobian_vector, 0.0);
172 }
173
174 /**
175 * @brief Get the value of the compressibility factor used in the density
176 * model.
177 *
178 * @return Value of the compressibility factor which in this case is null.
179 */
180 double
181 get_psi() const override
182 {
183 return 0.0;
184 }
185
186 /**
187 * @brief Get the value of the reference state density used in the density
188 * model.
189 *
190 * @return Value of the reference state density, here the constant density
191 * value.
192 */
193 double
194 get_density_ref() const override
195 {
196 return density;
197 }
198
199private:
200 /// Density of the material.
201 const double density;
202};
203
204
205/**
206 * @brief Isothermal ideal gas density.
207 *
208 * Isothermal ideal gas density assumes that the fluid's density varies
209 * according the following state equation:
210 *
211 * \f$\rho = \rho_\text{ref} + \psi p = \rho_\text{ref} + \frac{1}{R T} \ p\f$
212 *
213 * where \f$\rho_\text{ref}\f$ is the density of the fluid at the reference
214 * state, \f$\psi = \frac{1}{R T}\f$ is the compressibility factor derived from
215 * the ideal gas law with \f$R= \frac{R_u}{M}\f$ the specific gas constant
216 * (universal gas constant (\f$R_u\f$) divided by the molar mass of the gas
217 * (\f$M\f$)) and \f$T\f$ the temperature of the gas, finally, \f$p\f$ is the
218 * differential pressure between the reference state and the current state.
219 * The model is used for weakly compressible flows when temperature
220 * fluctuations' influence on density can be neglected.
221 */
223{
224public:
225 /**
226 * @brief Constructor of the isothermal ideal gas density model.
227 *
228 * @param[in] p_density_ref Value of the density of the gas at reference
229 * state.
230 *
231 * @param[in] p_R Value of the specific gas constant \f$\left(R=\frac{R_u}{M}
232 * \right)\f$ with \f$R_u\f$ the universal gas constant and \f$M\f$ the molar
233 * mass of the gas.
234 *
235 * @param[in] p_T Value of the temperature of the gas at reference state.
236 */
237 DensityIsothermalIdealGas(const double p_density_ref,
238 const double p_R,
239 const double p_T)
240 : density_ref(p_density_ref)
241 , psi(1. / (p_R * p_T))
242 {
243 this->model_depends_on[field::pressure] = true;
244 }
245
246 /**
247 * @brief Compute the density of the isothermal ideal gas.
248 *
249 * @param[in] field_values Values of the various fields on which the property
250 * may depend. In this case, the density depends on pressure. The map stores a
251 * single value per field.
252 *
253 * @return Value of the density computed with the @p field_values.
254 */
255 double
256 value(const std::map<field, double> &field_values) override
257 {
258 Assert(field_values.find(field::pressure) != field_values.end(),
259 PhysicialPropertyModelFieldUndefined("DensityIsothermalIdealGas",
260 "pressure"));
261 const double pressure = field_values.at(field::pressure);
262 return density_ref + psi * pressure;
263 }
264
265 /**
266 * @brief Compute a vector of density values for an isothermal ideal gas.
267 *
268 * @param[in] field_vectors Vectors of the fields on which the density may
269 * depend. In this case, the density depends on pressure. The map stores a
270 * vector of values per field.
271 *
272 * @param[out] property_vector Vectors of computed density values.
273 */
274 void
275 vector_value(const std::map<field, std::vector<double>> &field_vectors,
276 std::vector<double> &property_vector) override
277 {
278 Assert(field_vectors.find(field::pressure) != field_vectors.end(),
279 PhysicialPropertyModelFieldUndefined("DensityIsothermalIdealGas",
280 "pressure"));
281 const std::vector<double> &pressure = field_vectors.at(field::pressure);
282 for (unsigned int i = 0; i < property_vector.size(); ++i)
283 property_vector[i] = density_ref + psi * pressure[i];
284 }
285
286 /**
287 * @brief Compute the jacobian (the partial derivative) of the density with
288 * respect to a specified field.
289 *
290 * @param[in] field_values Values of the various fields on which the density
291 * may depend. The map stores a single value per field.
292 *
293 * @param[in] id Indicator of the field with respect to which the jacobian
294 * should be computed.
295 *
296 * @return Value of the derivative of the density with respect to the
297 * specified field.
298 */
299 double
300 jacobian(const std::map<field, double> &field_values, const field id) override
301 {
302 (void)field_values;
303 if (id == field::pressure)
304 return psi;
305 else
306 return 0;
307 }
308
309 /**
310 * @brief Compute the derivative of the density with respect to a field for
311 * an isothermal ideal gas.
312 *
313 * @param[in] field_vectors Vector of values of the fields used to evaluate
314 * the density. The map stores a vector of values per field.
315 *
316 * @param[in] id Identifier of the field with respect to which a derivative
317 * should be computed.
318 *
319 * @param[out] jacobian_vector Vector of computed derivative values of the
320 * density
321 * with respect to the field of the specified @p id.
322 */
323 void
324 vector_jacobian(const std::map<field, std::vector<double>> &field_vectors,
325 const field id,
326 std::vector<double> &jacobian_vector) override
327 {
328 (void)field_vectors;
329 if (id == field::pressure)
330 std::ranges::fill(jacobian_vector, psi);
331 else
332 std::ranges::fill(jacobian_vector, 0);
333 }
334
335 /**
336 * @brief Get the value of the compressibility factor used in the density
337 * model.
338 *
339 * @return Value of the isothermal ideal gas compressibility factor.
340 */
341 double
342 get_psi() const override
343 {
344 return psi;
345 }
346
347 /**
348 * @brief Get the value of the reference state density used in the density
349 * model.
350 *
351 * @return Value of the density of the isothermal ideal gas at reference state
352 */
353 double
354 get_density_ref() const override
355 {
356 return density_ref;
357 }
358
359private:
360 /// Density of the isothermal ideal gas at reference state
361 const double density_ref;
362 /// Compressibility factor of the isothermal ideal gas at reference state
363 const double psi;
364};
365
366#endif
void vector_jacobian(const std::map< field, std::vector< double > > &field_vectors, const field id, std::vector< double > &jacobian_vector) override
Computes the derivative of the density with respect to specified fields.
double value(const std::map< field, double > &field_values) override
Compute density value.
const double density
Density of the material.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Compute a vector of density values.
double jacobian(const std::map< field, double > &field_values, const field id) override
Compute the jacobian (the partial derivative) of the density with respect to a specified field.
double get_psi() const override
Get the value of the compressibility factor used in the density model.
DensityConstant(const double p_density)
Constructor of the constant density model.
double get_density_ref() const override
Get the value of the reference state density used in the density model.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Compute a vector of density values for an isothermal ideal gas.
DensityIsothermalIdealGas(const double p_density_ref, const double p_R, const double p_T)
Constructor of the isothermal ideal gas density model.
double value(const std::map< field, double > &field_values) override
Compute the density of the isothermal ideal gas.
const double psi
Compressibility factor of the isothermal ideal gas at reference state.
double get_psi() const override
Get the value of the compressibility factor used in the density model.
const double density_ref
Density of the isothermal ideal gas at reference state.
double get_density_ref() const override
Get the value of the reference state density used in the density 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 density with respect to a field for an isothermal ideal gas.
double jacobian(const std::map< field, double > &field_values, const field id) override
Compute the jacobian (the partial derivative) of the density with respect to a specified field.
Abstract class for calculating the density of materials.
bool is_constant_density_model() const
Check if the model is a constant density model.
bool constant_density_model
Boolean indicating if the model corresponds to a constant density model.
static std::shared_ptr< DensityModel > model_cast(const Parameters::Material &material_properties)
Instantiate and return a pointer to a DensityModel object by casting it to the proper child class.
virtual double get_density_ref() const =0
Get the value of the reference state density used in the density model.
virtual double get_psi() const =0
Get the value of the compressibility factor used in the density model.
Material - Class that defines the physical property of a material. Generally a material will be a flu...
Definition parameters.h:594
PhysicalPropertyModel()
Default constructor. Set the model_depends_on to false for all variables.
std::map< field, bool > model_depends_on
@ pressure
Pressure scalar field from fluid dynamics.