-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompressibleNewtonianFluid.hpp
419 lines (371 loc) · 20 KB
/
CompressibleNewtonianFluid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright © 2020-2024 Alexandre Coderre-Chabot
//
// This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
// models, and units of measure for scientific computing.
//
// Physical Quantities is hosted at:
// https://github.com/acodcha/phq
//
// Physical Quantities is licensed under the MIT License:
// https://mit-license.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// - The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef PHQ_CONSTITUTIVE_MODEL_COMPRESSIBLE_NEWTONIAN_FLUID_HPP
#define PHQ_CONSTITUTIVE_MODEL_COMPRESSIBLE_NEWTONIAN_FLUID_HPP
#include <cstddef>
#include <functional>
#include <ostream>
#include <string>
#include "../Base.hpp"
#include "../BulkDynamicViscosity.hpp"
#include "../ConstitutiveModel.hpp"
#include "../DynamicViscosity.hpp"
#include "../Strain.hpp"
#include "../StrainRate.hpp"
#include "../Stress.hpp"
#include "../SymmetricDyad.hpp"
#include "../Unit/Frequency.hpp"
#include "../Unit/Pressure.hpp"
namespace PhQ {
/// \brief Constitutive model for a compressible Newtonian fluid. This is the simplest constitutive
/// model for a compressible fluid. It is similar to the model for an incompressible Newtonian
/// fluid, but also includes the effect of the volumetric component of the strain rate tensor in
/// addition to its deviatoric component.
template <typename NumericType = double>
class ConstitutiveModel::CompressibleNewtonianFluid : public ConstitutiveModel {
public:
/// \brief Default constructor. Constructs a compressible Newtonian fluid constitutive model with
/// an uninitialized dynamic viscosity and bulk dynamic viscosity.
CompressibleNewtonianFluid() : ConstitutiveModel() {}
/// \brief Constructor. Constructs a compressible Newtonian fluid constitutive model from a given
/// dynamic viscosity. Initializes the bulk dynamic viscosity to zero.
explicit constexpr CompressibleNewtonianFluid(
const DynamicViscosity<NumericType>& dynamic_viscosity)
: ConstitutiveModel(), dynamic_viscosity(dynamic_viscosity),
bulk_dynamic_viscosity(PhQ::BulkDynamicViscosity<NumericType>::Zero()) {}
/// \brief Constructor. Constructs a compressible Newtonian fluid constitutive model from a given
/// dynamic viscosity and bulk dynamic viscosity.
constexpr CompressibleNewtonianFluid(
const DynamicViscosity<NumericType>& dynamic_viscosity,
const BulkDynamicViscosity<NumericType>& bulk_dynamic_viscosity)
: ConstitutiveModel(), dynamic_viscosity(dynamic_viscosity),
bulk_dynamic_viscosity(bulk_dynamic_viscosity) {}
/// \brief Destructor. Destroys this compressible Newtonian fluid constitutive model.
~CompressibleNewtonianFluid() noexcept override = default;
/// \brief Copy constructor. Constructs a compressible Newtonian fluid constitutive model by
/// copying another one.
constexpr CompressibleNewtonianFluid(const CompressibleNewtonianFluid& other) = default;
/// \brief Move constructor. Constructs a compressible Newtonian fluid constitutive model by
/// moving another one.
constexpr CompressibleNewtonianFluid(CompressibleNewtonianFluid&& other) noexcept = default;
/// \brief Copy assignment operator. Assigns this compressible Newtonian fluid constitutive model
/// by copying another one.
CompressibleNewtonianFluid& operator=(const CompressibleNewtonianFluid& other) = default;
/// \brief Move assignment operator. Assigns this compressible Newtonian fluid constitutive model
/// by moving another one.
CompressibleNewtonianFluid& operator=(CompressibleNewtonianFluid&& other) noexcept = default;
/// \brief Dynamic viscosity of this compressible Newtonian fluid constitutive model.
[[nodiscard]] inline constexpr const PhQ::DynamicViscosity<NumericType>&
DynamicViscosity() const noexcept {
return dynamic_viscosity;
}
/// \brief Bulk dynamic viscosity of this compressible Newtonian fluid constitutive model.
[[nodiscard]] inline constexpr const PhQ::BulkDynamicViscosity<NumericType>&
BulkDynamicViscosity() const noexcept {
return bulk_dynamic_viscosity;
}
/// \brief Returns this constitutive model's type.
[[nodiscard]] inline ConstitutiveModel::Type GetType() const noexcept override {
return ConstitutiveModel::Type::CompressibleNewtonianFluid;
}
/// \brief Returns the stress resulting from a given strain and strain rate. Since this is a
/// compressible Newtonian fluid constitutive model, the strain does not contribute to the stress
/// and is ignored.
[[nodiscard]] inline PhQ::Stress<float> Stress(
const PhQ::Strain<float>& /*strain*/,
const PhQ::StrainRate<float>& strain_rate) const override {
return this->Stress(strain_rate);
}
/// \brief Returns the stress resulting from a given strain and strain rate. Since this is a
/// compressible Newtonian fluid constitutive model, the strain does not contribute to the stress
/// and is ignored.
[[nodiscard]] inline PhQ::Stress<double> Stress(
const PhQ::Strain<double>& /*strain*/,
const PhQ::StrainRate<double>& strain_rate) const override {
return this->Stress(strain_rate);
}
/// \brief Returns the stress resulting from a given strain and strain rate. Since this is a
/// compressible Newtonian fluid constitutive model, the strain does not contribute to the stress
/// and is ignored.
[[nodiscard]] inline PhQ::Stress<long double> Stress(
const PhQ::Strain<long double>& /*strain*/,
const PhQ::StrainRate<long double>& strain_rate) const override {
return this->Stress(strain_rate);
}
/// \brief Returns the stress resulting from a given strain. Since this is a compressible
/// Newtonian fluid constitutive model, the strain does not contribute to the stress, so this
/// always returns a stress of zero.
[[nodiscard]] inline PhQ::Stress<float> Stress(
const PhQ::Strain<float>& /*strain*/) const override {
return PhQ::Stress<float>::Zero();
}
/// \brief Returns the stress resulting from a given strain. Since this is a compressible
/// Newtonian fluid constitutive model, the strain does not contribute to the stress, so this
/// always returns a stress of zero.
[[nodiscard]] inline PhQ::Stress<double> Stress(
const PhQ::Strain<double>& /*strain*/) const override {
return PhQ::Stress<double>::Zero();
}
/// \brief Returns the stress resulting from a given strain. Since this is a compressible
/// Newtonian fluid constitutive model, the strain does not contribute to the stress, so this
/// always returns a stress of zero.
[[nodiscard]] inline PhQ::Stress<long double> Stress(
const PhQ::Strain<long double>& /*strain*/) const override {
return PhQ::Stress<long double>::Zero();
}
/// \brief Returns the stress resulting from a given strain rate.
[[nodiscard]] inline PhQ::Stress<float> Stress(
const PhQ::StrainRate<float>& strain_rate) const override {
// stress = a * strain_rate + b * trace(strain_rate) * identity_matrix
// a = 2 * dynamic_viscosity
// b = bulk_dynamic_viscosity
const float a{static_cast<float>(2) * static_cast<float>(dynamic_viscosity.Value())};
const float b{static_cast<float>(bulk_dynamic_viscosity.Value())
* static_cast<float>(strain_rate.Value().Trace())};
return PhQ::Stress<float>{
SymmetricDyad<float>{a * static_cast<SymmetricDyad<float>>(strain_rate.Value())}
+ SymmetricDyad<float>{b, static_cast<float>(0), static_cast<float>(0), b,
static_cast<float>(0), b},
Standard<Unit::Pressure>
};
}
/// \brief Returns the stress resulting from a given strain rate.
[[nodiscard]] inline PhQ::Stress<double> Stress(
const PhQ::StrainRate<double>& strain_rate) const override {
// stress = a * strain_rate + b * trace(strain_rate) * identity_matrix
// a = 2 * dynamic_viscosity
// b = bulk_dynamic_viscosity
const double a{static_cast<double>(2) * static_cast<double>(dynamic_viscosity.Value())};
const double b{static_cast<double>(bulk_dynamic_viscosity.Value())
* static_cast<double>(strain_rate.Value().Trace())};
return PhQ::Stress<double>{
SymmetricDyad<double>{a * static_cast<SymmetricDyad<double>>(strain_rate.Value())}
+ SymmetricDyad<double>{b, static_cast<double>(0), static_cast<double>(0), b,
static_cast<double>(0), b},
Standard<Unit::Pressure>
};
}
/// \brief Returns the stress resulting from a given strain rate.
[[nodiscard]] inline PhQ::Stress<long double> Stress(
const PhQ::StrainRate<long double>& strain_rate) const override {
// stress = a * strain_rate + b * trace(strain_rate) * identity_matrix
// a = 2 * dynamic_viscosity
// b = bulk_dynamic_viscosity
const long double a{
static_cast<long double>(2) * static_cast<long double>(dynamic_viscosity.Value())};
const long double b{static_cast<long double>(bulk_dynamic_viscosity.Value())
* static_cast<long double>(strain_rate.Value().Trace())};
return PhQ::Stress<long double>{
SymmetricDyad<long double>{a * static_cast<SymmetricDyad<long double>>(strain_rate.Value())}
+ SymmetricDyad<long double>{b, static_cast<long double>(0), static_cast<long double>(0),
b, static_cast<long double>(0), b},
Standard<Unit::Pressure>
};
}
/// \brief Returns the strain resulting from a given stress. Since this is a compressible
/// Newtonian fluid constitutive model, stress does not depend on strain, so this always returns a
/// strain of zero.
[[nodiscard]] inline PhQ::Strain<float> Strain(
const PhQ::Stress<float>& /*stress*/) const override {
return PhQ::Strain<float>::Zero();
}
/// \brief Returns the strain resulting from a given stress. Since this is a compressible
/// Newtonian fluid constitutive model, stress does not depend on strain, so this always returns a
/// strain of zero.
[[nodiscard]] inline PhQ::Strain<double> Strain(
const PhQ::Stress<double>& /*stress*/) const override {
return PhQ::Strain<double>::Zero();
}
/// \brief Returns the strain resulting from a given stress. Since this is a compressible
/// Newtonian fluid constitutive model, stress does not depend on strain, so this always returns a
/// strain of zero.
[[nodiscard]] inline PhQ::Strain<long double> Strain(
const PhQ::Stress<long double>& /*stress*/) const override {
return PhQ::Strain<long double>::Zero();
}
/// \brief Returns the strain rate resulting from a given stress.
[[nodiscard]] inline PhQ::StrainRate<float> StrainRate(
const PhQ::Stress<float>& stress) const override {
// strain_rate = a * stress + b * trace(stress) * identity_matrix
// a = 1 / (2 * dynamic_viscosity)
// b = -1 * bulk_dynamic_viscosity /
// (2 * dynamic_viscosity * (2 * dynamic_viscosity + 3 * bulk_dynamic_viscosity))
const float a{static_cast<float>(1)
/ (static_cast<float>(2) * static_cast<float>(dynamic_viscosity.Value()))};
const float b{
static_cast<float>(-bulk_dynamic_viscosity.Value())
/ (static_cast<float>(2) * static_cast<float>(dynamic_viscosity.Value())
* (static_cast<float>(2) * static_cast<float>(dynamic_viscosity.Value())
+ static_cast<float>(3) * static_cast<float>(bulk_dynamic_viscosity.Value())))};
const float c{b * static_cast<float>(stress.Value().Trace())};
return PhQ::StrainRate<float>{
a * static_cast<SymmetricDyad<float>>(stress.Value())
+ SymmetricDyad<float>{c, static_cast<float>(0), static_cast<float>(0), c,
static_cast<float>(0), c},
Standard<Unit::Frequency>
};
}
/// \brief Returns the strain rate resulting from a given stress.
[[nodiscard]] inline PhQ::StrainRate<double> StrainRate(
const PhQ::Stress<double>& stress) const override {
// strain_rate = a * stress + b * trace(stress) * identity_matrix
// a = 1 / (2 * dynamic_viscosity)
// b = -1 * bulk_dynamic_viscosity /
// (2 * dynamic_viscosity * (2 * dynamic_viscosity + 3 * bulk_dynamic_viscosity))
const double a{static_cast<double>(1)
/ (static_cast<double>(2) * static_cast<double>(dynamic_viscosity.Value()))};
const double b{
static_cast<double>(-bulk_dynamic_viscosity.Value())
/ (static_cast<double>(2) * static_cast<double>(dynamic_viscosity.Value())
* (static_cast<double>(2) * static_cast<double>(dynamic_viscosity.Value())
+ static_cast<double>(3) * static_cast<double>(bulk_dynamic_viscosity.Value())))};
const double c{b * static_cast<double>(stress.Value().Trace())};
return PhQ::StrainRate<double>{
a * static_cast<SymmetricDyad<double>>(stress.Value())
+ SymmetricDyad<double>{c, static_cast<double>(0), static_cast<double>(0), c,
static_cast<double>(0), c},
Standard<Unit::Frequency>
};
}
/// \brief Returns the strain rate resulting from a given stress.
[[nodiscard]] inline PhQ::StrainRate<long double> StrainRate(
const PhQ::Stress<long double>& stress) const override {
// strain_rate = a * stress + b * trace(stress) * identity_matrix
// a = 1 / (2 * dynamic_viscosity)
// b = -1 * bulk_dynamic_viscosity /
// (2 * dynamic_viscosity * (2 * dynamic_viscosity + 3 * bulk_dynamic_viscosity))
const long double a{
static_cast<long double>(1)
/ (static_cast<long double>(2) * static_cast<long double>(dynamic_viscosity.Value()))};
const long double b{
static_cast<long double>(-bulk_dynamic_viscosity.Value())
/ (static_cast<long double>(2) * static_cast<long double>(dynamic_viscosity.Value())
* (static_cast<long double>(2) * static_cast<long double>(dynamic_viscosity.Value())
+ static_cast<long double>(3)
* static_cast<long double>(bulk_dynamic_viscosity.Value())))};
const long double c{b * static_cast<long double>(stress.Value().Trace())};
return PhQ::StrainRate<long double>{
a * static_cast<SymmetricDyad<long double>>(stress.Value())
+ SymmetricDyad<long double>{c, static_cast<long double>(0), static_cast<long double>(0),
c, static_cast<long double>(0), c},
Standard<Unit::Frequency>
};
}
/// \brief Prints this compressible Newtonian fluid constitutive model as a string.
[[nodiscard]] inline std::string Print() const override {
return {"Type = " + std::string{Abbreviation(this->GetType())}
+ ", Dynamic Viscosity = " + dynamic_viscosity.Print()
+ ", Bulk Dynamic Viscosity = " + bulk_dynamic_viscosity.Print()};
}
/// \brief Serializes this compressible Newtonian fluid constitutive model as a JSON message.
[[nodiscard]] inline std::string JSON() const override {
return {R"({"type":")" + SnakeCase(Abbreviation(this->GetType())) + R"(","dynamic_viscosity":)"
+ dynamic_viscosity.JSON()
+ ",\"bulk_dynamic_viscosity\":" + bulk_dynamic_viscosity.JSON() + "}"};
}
/// \brief Serializes this compressible Newtonian fluid constitutive model as an XML message.
[[nodiscard]] inline std::string XML() const override {
return {"<type>" + SnakeCase(Abbreviation(this->GetType())) + "</type><dynamic_viscosity>"
+ dynamic_viscosity.XML() + "</dynamic_viscosity><bulk_dynamic_viscosity>"
+ bulk_dynamic_viscosity.XML() + "</bulk_dynamic_viscosity>"};
}
/// \brief Serializes this compressible Newtonian fluid constitutive model as a YAML message.
[[nodiscard]] inline std::string YAML() const override {
return {"{type:\"" + SnakeCase(Abbreviation(this->GetType()))
+ "\",dynamic_viscosity:" + dynamic_viscosity.YAML()
+ ",bulk_dynamic_viscosity:" + bulk_dynamic_viscosity.YAML() + "}"};
}
private:
/// \brief Dynamic viscosity of this compressible Newtonian fluid constitutive model.
PhQ::DynamicViscosity<NumericType> dynamic_viscosity;
/// \brief Bulk dynamic viscosity of this compressible Newtonian fluid constitutive model.
PhQ::BulkDynamicViscosity<NumericType> bulk_dynamic_viscosity;
};
template <typename NumericType>
inline constexpr bool operator==(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
return left.DynamicViscosity() == right.DynamicViscosity()
&& left.BulkDynamicViscosity() == right.BulkDynamicViscosity();
}
template <typename NumericType>
inline constexpr bool operator!=(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
return left.DynamicViscosity() != right.DynamicViscosity()
|| left.BulkDynamicViscosity() != right.BulkDynamicViscosity();
}
template <typename NumericType>
inline constexpr bool operator<(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
if (left.DynamicViscosity() != right.DynamicViscosity()) {
return left.DynamicViscosity() < right.DynamicViscosity();
}
return left.BulkDynamicViscosity() < right.BulkDynamicViscosity();
}
template <typename NumericType>
inline constexpr bool operator>(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
if (left.DynamicViscosity() != right.DynamicViscosity()) {
return left.DynamicViscosity() > right.DynamicViscosity();
}
return left.BulkDynamicViscosity() > right.BulkDynamicViscosity();
}
template <typename NumericType>
inline constexpr bool operator<=(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
return !(left > right);
}
template <typename NumericType>
inline constexpr bool operator>=(
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& left,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& right) noexcept {
return !(left < right);
}
template <typename NumericType>
inline std::ostream& operator<<(
std::ostream& stream,
const typename ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& model) {
stream << model.Print();
return stream;
}
} // namespace PhQ
namespace std {
template <typename NumericType>
struct hash<typename PhQ::ConstitutiveModel::CompressibleNewtonianFluid<NumericType>> {
size_t operator()(
const typename PhQ::ConstitutiveModel::CompressibleNewtonianFluid<NumericType>& model) const {
size_t result{17};
result = static_cast<size_t>(31) * result
+ hash<PhQ::DynamicViscosity<NumericType>>()(model.DynamicViscosity());
result = static_cast<size_t>(31) * result
+ hash<PhQ::BulkDynamicViscosity<NumericType>>()(model.BulkDynamicViscosity());
return result;
}
};
} // namespace std
#endif // PHQ_CONSTITUTIVE_MODEL_COMPRESSIBLE_NEWTONIAN_FLUID_HPP