Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
magnetic_permeability_model.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_magnetic_permeability_model_h
5#define lethe_magnetic_permeability_model_h
6
7#include <core/parameters.h>
9
10#include <algorithm>
11
12/**
13 * @brief Abstract class that allows to calculate the
14 * magnetic permeability on each quadrature point.
15 */
17{
18public:
19 /**
20 * @brief Instantiates and returns a pointer to a MagneticPermeabilityModel object by casting it to
21 * the proper child class for the real part of the permeability.
22 *
23 * @param[in] material_properties Parameters for a material
24 */
25 static std::shared_ptr<MagneticPermeabilityModel>
26 model_cast_real(const Parameters::Material &material_properties);
27
28 /**
29 * @brief Instantiates and returns a pointer to a MagneticPermeabilityModel object by casting it to
30 * the proper child class for the imaginary part of the permeability.
31 *
32 * @param[in] material_properties Parameters for a material
33 */
34 static std::shared_ptr<MagneticPermeabilityModel>
35 model_cast_imag(const Parameters::Material &material_properties);
36};
37
38
39/**
40 * @brief Constant magnetic permeability.
41 */
43{
44public:
45 /**
46 * @brief Default constructor
47 */
48 ConstantMagneticPermeability(const double p_magnetic_permeability)
49 : magnetic_permeability(p_magnetic_permeability)
50 {}
51
52 /**
53 * @brief Calculates the value of the magnetic permeability
54 * @param[in] field_values Values of the various fields on which the magnetic
55 * permeability depends.
56 * @return Value of the magnetic permeability calculated with the `field_values`.
57 */
58 double
59 value([[maybe_unused]] const std::map<field, double> &field_values) override
60 {
62 };
63
64 /**
65 * @brief Calculates, in a vector, values of magnetic permeabilities
66 * @param[in] field_vectors Vector of properties on which the magnetic
67 * permeabilities depend
68 * @param[in,out] property_vector Values of the magnetic permeabilities
69 */
70 void
72 [[maybe_unused]] const std::map<field, std::vector<double>> &field_vectors,
73 std::vector<double> &property_vector) override
74 {
75 property_vector.assign(property_vector.size(), magnetic_permeability);
76 }
77
78 /**
79 * @brief Calculates the jacobian (the partial derivative) of the magnetic permeability with respect to a field
80 * @param[in] field_values Value of the various fields on which the property
81 * may depend.
82 * @param[in] id Identifier of the field with respect to which the jacobian
83 * should be calculated
84 * @return Value of the partial derivative of the magnetic permeability with respect to the specified field.
85 */
86
87 double
88 jacobian(const std::map<field, double> & /*field_values*/,
89 field /*id*/) override
90 {
91 return 0;
92 };
93
94 /**
95 * @brief Calculates, in a vector, the derivatives of the magnetic permeability with respect to a field
96 * @param[in] field_vectors Vector for the values of the fields used to
97 * evaluate the property
98 * @param[in] id Identifier of the field with respect to which a derivative
99 * should be calculated
100 * @param[in,out] jacobian_vector Vector of values of the derivatives of the
101 * magnetic permeability with respect to the specified field
102 */
103
104 void
106 const std::map<field, std::vector<double>> & /*field_vectors*/,
107 [[maybe_unused]] const field id,
108 std::vector<double> &jacobian_vector) override
109 {
110 std::ranges::fill(jacobian_vector, 0);
111 };
112
113private:
115};
116
117/**
118 * @brief Polynomial magnetic permeability.
119 */
121{
122public:
123 /**
124 * @brief Default constructor
125 */
127 const std::vector<double> &p_magnetic_permeability_polynomial_coefficients)
129 p_magnetic_permeability_polynomial_coefficients)
130 {
132 }
133
134 /**
135 * @brief Calculates the value of the magnetic permeability
136 * @param[in] field_values Values of the various fields on which the magnetic
137 * permeability depends.
138 * @return Value of the magnetic permeability calculated with the `field_values`.
139 */
140 double
141 value(const std::map<field, double> &field_values) override
142 {
143 AssertThrow(field_values.contains(field::temperature),
144 PhysicialPropertyModelFieldUndefined(
145 "PolynomialMagneticPermeability", "temperature"));
146 const double temperature = field_values.at(field::temperature);
147
148 // Compute the polynomial power using Horner's method for efficiency, i.e.
149 // P(x) = (...((a_n * x + a_{n-1}) * x + a_{n-2}) * x + ... + a_1) * x + a_0
150 double magnetic_permeability = 0.0;
151 for (const auto &coefficient :
153 {
155 coefficient + temperature * magnetic_permeability;
156 }
157
159 }
160
161 /**
162 * @brief Calculates, in a vector, values of magnetic permeabilities
163 * @param[in] field_vectors Vector of properties on which the magnetic
164 * permeabilities depend
165 * @param[in,out] property_vector Values of the magnetic permeabilities
166 */
167 void
168 vector_value(const std::map<field, std::vector<double>> &field_vectors,
169 std::vector<double> &property_vector) override
170 {
171 AssertThrow(field_vectors.find(field::temperature) != field_vectors.end(),
172 PhysicialPropertyModelFieldUndefined(
173 "PolynomialMagneticPermeability", "temperature"));
174
175 const std::vector<double> &temperature =
176 field_vectors.at(field::temperature);
177 for (unsigned int i = 0; i < property_vector.size(); ++i)
178 {
179 // Compute the polynomial power using Horner's method for efficiency,
180 // i.e. P(x) = (...((a_n * x + a_{n-1}) * x + a_{n-2}) * x + ... + a_1)
181 // * x + a_0
182 double magnetic_permeability = 0.0;
183 for (const auto &coefficient :
185 {
187 coefficient + temperature[i] * magnetic_permeability;
188 }
189 property_vector[i] = magnetic_permeability;
190 }
191 }
192
193 /**
194 * @brief Calculates the jacobian (the partial derivative) of the magnetic permeability with respect to a field
195 * @param[in] field_values Value of the various fields on which the property
196 * may depend.
197 * @param[in] id Identifier of the field with respect to which the jacobian
198 * should be calculated
199 * @return Value of the partial derivative of the magnetic permeability with respect to the specified field.
200 *
201 * @remark The jacobian is only implemented with respect to the temperature.
202 */
203 double
204 jacobian(const std::map<field, double> &field_values, const field id) override
205 {
206 // The derivative can only be taken with respect to the temperature field,
207 // since the polynomial is a function of temperature.
208 if (id == field::temperature)
209 {
210 AssertThrow(field_values.find(field::temperature) != field_values.end(),
211 PhysicialPropertyModelFieldUndefined(
212 "PolynomialMagneticPermeability", "temperature"));
213
214 // Use a Horner accumulator to evaluate the derivative directly.
215 unsigned int polynomial_order =
217 double magnetic_permeability_derivative =
219
220 double temperature = field_values.at(field::temperature);
221
222 for (unsigned int i = 1; i < polynomial_order; ++i)
223 {
224 magnetic_permeability_derivative =
225 magnetic_permeability_derivative * temperature +
226 (polynomial_order - i) *
228 }
229
230 return magnetic_permeability_derivative;
231 }
232 else
233 return 0;
234 }
235
236 /**
237 * @brief Calculates, in a vector, the derivatives of the magnetic permeability with respect to a field
238 * @param[in] field_vectors Vector for the values of the fields used to
239 * evaluate the property
240 * @param[in] id Identifier of the field with respect to which a derivative
241 * should be calculated
242 * @param[in,out] jacobian_vector Vector of values of the derivatives of the
243 * magnetic permeability with respect to the specified field
244 *
245 * @remark The vector_jacobian is only implemented with respect to the
246 * temperature.
247 */
248 void
249 vector_jacobian(const std::map<field, std::vector<double>> &field_vectors,
250 const field id,
251 std::vector<double> &jacobian_vector) override
252 {
253 if (id == field::temperature)
254 {
255 AssertThrow(
256 field_vectors.find(field::temperature) != field_vectors.end(),
257 PhysicialPropertyModelFieldUndefined("PolynomialMagneticPermeability",
258 "temperature"));
259
260 const std::vector<double> &temperature =
261 field_vectors.at(field::temperature);
262 for (unsigned int i = 0; i < jacobian_vector.size(); ++i)
263 {
264 // Use a Horner accumulator to evaluate the derivative directly.
265 unsigned int polynomial_order =
267 double magnetic_permeability_derivative =
268 polynomial_order *
270 for (unsigned int j = 1; j < polynomial_order; ++j)
271 {
272 magnetic_permeability_derivative =
273 magnetic_permeability_derivative * temperature[i] +
274 (polynomial_order - j) *
276 }
277
278 jacobian_vector[i] = magnetic_permeability_derivative;
279 }
280 }
281 else
282 {
283 std::ranges::fill(jacobian_vector, 0);
284 }
285 }
286
287private:
289};
290
291#endif
ConstantMagneticPermeability(const double p_magnetic_permeability)
Default constructor.
void vector_jacobian(const std::map< field, std::vector< double > > &, const field id, std::vector< double > &jacobian_vector) override
Calculates, in a vector, the derivatives of the magnetic permeability with respect to a field.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Calculates, in a vector, values of magnetic permeabilities.
double value(const std::map< field, double > &field_values) override
Calculates the value of the magnetic permeability.
double jacobian(const std::map< field, double > &, field) override
Calculates the jacobian (the partial derivative) of the magnetic permeability with respect to a field...
Abstract class that allows to calculate the magnetic permeability on each quadrature point.
static std::shared_ptr< MagneticPermeabilityModel > model_cast_imag(const Parameters::Material &material_properties)
Instantiates and returns a pointer to a MagneticPermeabilityModel object by casting it to the proper ...
static std::shared_ptr< MagneticPermeabilityModel > model_cast_real(const Parameters::Material &material_properties)
Instantiates and returns a pointer to a MagneticPermeabilityModel object by casting it to the proper ...
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
double jacobian(const std::map< field, double > &field_values, const field id) override
Calculates the jacobian (the partial derivative) of the magnetic permeability with respect to a field...
void vector_jacobian(const std::map< field, std::vector< double > > &field_vectors, const field id, std::vector< double > &jacobian_vector) override
Calculates, in a vector, the derivatives of the magnetic permeability with respect to a field.
void vector_value(const std::map< field, std::vector< double > > &field_vectors, std::vector< double > &property_vector) override
Calculates, in a vector, values of magnetic permeabilities.
PolynomialMagneticPermeability(const std::vector< double > &p_magnetic_permeability_polynomial_coefficients)
Default constructor.
const std::vector< double > magnetic_permeability_polynomial_coefficients
double value(const std::map< field, double > &field_values) override
Calculates the value of the magnetic permeability.
@ temperature
Temperature scalar field from heat transfer.