MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DuctLoss.cpp
Go to the documentation of this file.
1#include "DuctLoss.hpp"
2
4
5namespace {
6
7 constexpr double air_viscosity = 1.84e-5;
8 constexpr double heat_capacity_ratio = 1.4;
9 constexpr double prandtl_number = 0.71;
10
11 [[nodiscard]] bool positive_finite(double value) noexcept
12 {
13 return std::isfinite(value) && value > 0.0;
14 }
15
16 [[nodiscard]] bool not_negative_finite(double value) noexcept
17 {
18 return std::isfinite(value) && value >= 0.0;
19 }
20
21}
22
23double viscothermal_attenuation(double radius, double frequency, double sound_speed, double air_density) noexcept
24{
25 if (!positive_finite(radius) || !positive_finite(frequency)
26 || !positive_finite(sound_speed) || !positive_finite(air_density)) {
27 return 0.0;
28 }
29
30 const double omega = 2.0 * std::numbers::pi * frequency;
31 const double thermal = (heat_capacity_ratio - 1.0) / std::sqrt(prandtl_number);
32 return std::sqrt(omega * air_viscosity / (2.0 * air_density)) * (1.0 + thermal) / (radius * sound_speed);
33}
34
35std::pair<double, double> viscothermal_rates(
36 double radius, double sound_speed, double air_density, double low_frequency, double high_frequency) noexcept
37{
38 if (!positive_finite(low_frequency) || !positive_finite(high_frequency) || !(low_frequency < high_frequency)) {
39 return { 0.0, 0.0 };
40 }
41
42 const double low = viscothermal_attenuation(radius, low_frequency, sound_speed, air_density);
43 const double high = viscothermal_attenuation(radius, high_frequency, sound_speed, air_density);
44 if (!(low > 0.0) || !(high > 0.0)) {
45 return { 0.0, 0.0 };
46 }
47
48 const double slope = (high - low) / (high_frequency * high_frequency - low_frequency * low_frequency);
49 const double flat = low - slope * low_frequency * low_frequency;
50
51 const double two_pi = 2.0 * std::numbers::pi;
52 const double friction = 2.0 * sound_speed * std::max(flat, 0.0);
53 const double viscosity = slope * 2.0 * sound_speed * sound_speed * sound_speed / (two_pi * two_pi);
54 return { friction, viscosity };
55}
56
57bool DuctLoss::configure(const DuctGrid& grid, double friction, double viscosity)
58{
59 const size_t n = grid.intervals;
60 if (n < 2 || grid.velocity_gain.size() != n || !positive_finite(grid.time_step) || !positive_finite(grid.spacing)
61 || !not_negative_finite(friction) || !not_negative_finite(viscosity)) {
62 return false;
63 }
64
65 const double k = grid.time_step;
66 const double diffusion = viscosity * k / (grid.spacing * grid.spacing);
67
68 m_mass.resize(n);
69 m_edge.resize(n - 1);
70 m_upper.resize(n);
71 m_inverse_pivot.resize(n);
72
73 for (size_t l = 0; l < n; ++l) {
74 m_mass[l] = k / grid.velocity_gain[l];
75 }
76 for (size_t l = 0; l + 1 < n; ++l) {
77 m_edge[l] = diffusion * 0.5 * (m_mass[l] + m_mass[l + 1]);
78 }
79
80 const double ghost_left = 2.0 * m_edge.front();
81 const double ghost_right = 2.0 * m_edge.back();
82
83 double upper_before = 0.0;
84 for (size_t l = 0; l < n; ++l) {
85 const double left = l > 0 ? m_edge[l - 1] : ghost_left;
86 const double right = l + 1 < n ? m_edge[l] : ghost_right;
87 const double diagonal = m_mass[l] * (1.0 + k * friction) + left + right;
88
89 const double pivot = diagonal - (l > 0 ? left * upper_before : 0.0);
90 m_inverse_pivot[l] = 1.0 / pivot;
91 m_upper[l] = l + 1 < n ? m_edge[l] * m_inverse_pivot[l] : 0.0;
92 upper_before = m_upper[l];
93 }
94
97 m_active = friction > 0.0 || viscosity > 0.0;
98 return true;
99}
100
101void DuctLoss::apply(std::span<double> velocity) const noexcept
102{
103 const size_t n = m_mass.size();
104 if (!m_active || n == 0 || velocity.size() != n) {
105 return;
106 }
107
108 velocity[0] = m_mass[0] * velocity[0] * m_inverse_pivot[0];
109 for (size_t l = 1; l < n; ++l) {
110 velocity[l] = (m_mass[l] * velocity[l] + m_edge[l - 1] * velocity[l - 1]) * m_inverse_pivot[l];
111 }
112 for (size_t l = n - 1; l > 0; --l) {
113 velocity[l - 1] += m_upper[l - 1] * velocity[l];
114 }
115}
116
117} // namespace MayaFlux::Kinesis::Acoustics
Wall losses of a duct as friction and viscosity on the volume velocity.
double friction() const noexcept
Definition DuctLoss.hpp:91
double viscosity() const noexcept
Definition DuctLoss.hpp:92
std::vector< double > m_inverse_pivot
Definition DuctLoss.hpp:98
bool configure(const DuctGrid &grid, double friction, double viscosity)
Factor the loss for a grid.
Definition DuctLoss.cpp:57
void apply(std::span< double > velocity) const noexcept
Apply the loss to the velocities of a sample, in place.
Definition DuctLoss.cpp:101
double viscothermal_attenuation(double radius, double frequency, double sound_speed, double air_density) noexcept
Plane wave attenuation of a circular tube from its wall boundary layers.
Definition DuctLoss.cpp:23
std::pair< double, double > viscothermal_rates(double radius, double sound_speed, double air_density, double low_frequency, double high_frequency) noexcept
Friction and viscosity that match the tube's attenuation at two frequencies.
Definition DuctLoss.cpp:35
T velocity(const Memory::HistoryBuffer< T > &history, double dt) noexcept
First difference: rate of change of position.
std::vector< double > velocity_gain
Definition DuctGrid.hpp:75
Prepared grid and update gains of one bore.
Definition DuctGrid.hpp:66