MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Reed.cpp
Go to the documentation of this file.
1#include "Reed.hpp"
2
4
5namespace {
6
7 [[nodiscard]] bool positive_finite(double value) noexcept
8 {
9 return std::isfinite(value) && value > 0.0;
10 }
11
12 [[nodiscard]] bool channel_valid(const ReedChannel& reed) noexcept
13 {
14 return positive_finite(reed.width) && positive_finite(reed.rest_opening)
15 && positive_finite(reed.closing_pressure) && positive_finite(reed.air_density);
16 }
17
18}
19
20double reed_flow_coefficient(const ReedChannel& reed) noexcept
21{
22 return reed.width * reed.rest_opening * std::sqrt(2.0 / reed.air_density);
23}
24
25double reed_flow(const ReedChannel& reed, double difference) noexcept
26{
27 if (!channel_valid(reed)) {
28 return 0.0;
29 }
30
31 const double opening = 1.0 - difference / reed.closing_pressure;
32 if (opening <= 0.0) {
33 return 0.0;
34 }
35 return std::copysign(reed_flow_coefficient(reed) * opening * std::sqrt(std::abs(difference)), difference);
36}
37
38bool reed_coupling_is_unique(const ReedChannel& reed, double gain) noexcept
39{
40 return channel_valid(reed) && gain * reed_flow_coefficient(reed) < std::sqrt(reed.closing_pressure);
41}
42
43std::tuple<double, double, double, RootStatus> reed_mouth_step(
44 const ReedChannel& reed,
45 double& warm_start,
46 double end_pressure_gain,
47 double mouth_pressure,
48 double end_pressure,
49 double downstream_flow) noexcept
50{
51 const double gain = flow_coupling_gain(end_pressure_gain);
52 const double target = flow_coupling_target(mouth_pressure, end_pressure, gain, downstream_flow);
53
54 const auto wall = [&](RootStatus status) -> std::tuple<double, double, double, RootStatus> {
55 warm_start = 0.0;
56 return { centred_end_pressure(mouth_pressure, target, end_pressure), 0.0, target, status };
57 };
58
59 if (!channel_valid(reed) || !std::isfinite(target) || !positive_finite(gain)) {
60 return wall(RootStatus::NOT_FINITE);
61 }
62
63 const double closing = reed.closing_pressure;
64 if (target >= closing) {
65 return wall(RootStatus::CONVERGED);
66 }
67
68 const double magnitude = std::abs(target);
69 if (magnitude == 0.0) {
70 warm_start = 0.0;
71 return { centred_end_pressure(mouth_pressure, 0.0, end_pressure), 0.0, 0.0, RootStatus::CONVERGED };
72 }
73
74 const double sign = target > 0.0 ? 1.0 : -1.0;
75 const double a = gain * reed_flow_coefficient(reed);
76 const double plain_root = 2.0 * magnitude / (a + std::sqrt(a * a + 4.0 * magnitude));
77
78 const double lower = sign > 0.0 ? plain_root : 0.0;
79 const double upper = sign > 0.0 ? std::sqrt(magnitude) : plain_root;
80
81 const auto equation = [&](double s) -> RootSample {
82 const double cubic = a * s * s * s / closing;
83 return {
84 s * s + a * s - sign * cubic - magnitude,
85 2.0 * s + a - 3.0 * sign * a * s * s / closing
86 };
87 };
88
89 const double inflection = closing / (3.0 * a);
90 double start = warm_start;
91 if (sign < 0.0 || upper <= inflection) {
92 start = upper;
93 } else if (lower >= inflection) {
94 start = lower;
95 }
96
97 RootOptions options;
98 options.check_bracket = false;
99 const auto [root, status] = bracketed_newton(equation, lower, upper, start, options);
100 warm_start = root;
101
102 const double difference = sign * root * root;
103 return {
104 centred_end_pressure(mouth_pressure, difference, end_pressure),
105 reed_flow(reed, difference),
106 difference,
107 status
108 };
109}
110
111std::tuple<double, double, double> dynamic_reed_mouth_step(
112 const ReedChannel& reed,
113 double resonance,
114 double damping,
115 double reed_area,
116 double time_step,
117 std::array<double, 2>& motion,
118 double end_pressure_gain,
119 double mouth_pressure,
120 double end_pressure,
121 double downstream_flow) noexcept
122{
123 const double gain = flow_coupling_gain(end_pressure_gain);
124 const double target = flow_coupling_target(mouth_pressure, end_pressure, gain, downstream_flow);
125
126 if (!channel_valid(reed) || !positive_finite(resonance) || !positive_finite(time_step)
127 || !std::isfinite(damping) || damping < 0.0 || !std::isfinite(reed_area) || reed_area < 0.0
128 || !std::isfinite(target) || !positive_finite(gain)) {
129 motion = { 0.0, 0.0 };
130 return { centred_end_pressure(mouth_pressure, target, end_pressure), 0.0, target };
131 }
132
133 const double pushed = -reed_area * (motion[0] - motion[1]) / time_step;
134 const double known = flow_coupling_target(mouth_pressure, end_pressure, gain, downstream_flow - pushed);
135
136 const double opening = std::max(0.0, reed.rest_opening + motion[0]);
137 const double coefficient = reed.width * opening * std::sqrt(2.0 / reed.air_density);
138 const double reach = gain * coefficient;
139 const double magnitude = std::abs(known);
140
141 const double root = magnitude > 0.0 ? 2.0 * magnitude / (reach + std::sqrt(reach * reach + 4.0 * magnitude)) : 0.0;
142 const double sign = known >= 0.0 ? 1.0 : -1.0;
143 const double difference = sign * root * root;
144 const double flow = sign * coefficient * root;
145
146 const double omega = 2.0 * std::numbers::pi * resonance;
147 const double stiffness = omega * omega;
148 const double inverse_square = 1.0 / (time_step * time_step);
149 const double half_damping = damping / (2.0 * time_step);
150 const double force = -stiffness * reed.rest_opening * difference / reed.closing_pressure;
151
152 const double next = (force + 2.0 * inverse_square * motion[0]
153 - (inverse_square - half_damping + 0.5 * stiffness) * motion[1])
154 / (inverse_square + half_damping + 0.5 * stiffness);
155
156 motion = { std::max(next, -reed.rest_opening), motion[0] };
157 return { centred_end_pressure(mouth_pressure, difference, end_pressure), flow + pushed, difference };
158}
159
160} // namespace MayaFlux::Kinesis::Acoustics
Quasi-static single reed and its same-sample coupling to the bore.
constexpr double centred_end_pressure(double driving_pressure, double pressure_difference, double end_pressure) noexcept
Pressure of the end cell after the step, once the difference is solved.
constexpr double flow_coupling_target(double driving_pressure, double end_pressure, double gain, double downstream_flow) noexcept
Right side of the coupling equation.
std::tuple< double, double, double, RootStatus > reed_mouth_step(const ReedChannel &reed, double &warm_start, double end_pressure_gain, double mouth_pressure, double end_pressure, double downstream_flow) noexcept
Couple the reed to the first cell of the bore for one sample.
Definition Reed.cpp:43
double reed_flow_coefficient(const ReedChannel &reed) noexcept
w H sqrt(2 / rho), the flow per unit of (1 - d / pM) sign(d) sqrt|d|.
Definition Reed.cpp:20
std::tuple< double, double, double > dynamic_reed_mouth_step(const ReedChannel &reed, double resonance, double damping, double reed_area, double time_step, std::array< double, 2 > &motion, double end_pressure_gain, double mouth_pressure, double end_pressure, double downstream_flow) noexcept
Couple a reed with its own dynamics to the first cell of the bore.
Definition Reed.cpp:111
constexpr double flow_coupling_gain(double end_pressure_gain) noexcept
Gain of a time centred coupling at an end cell.
double reed_flow(const ReedChannel &reed, double difference) noexcept
Volume velocity through the channel for a pressure difference.
Definition Reed.cpp:25
bool reed_coupling_is_unique(const ReedChannel &reed, double gain) noexcept
Whether a coupling gain keeps the solution unique.
Definition Reed.cpp:38
RootStatus
How a root search ended.
RootResult bracketed_newton(Function function, double lower, double upper, double guess, const RootOptions &options={})
Root of an increasing function by Newton steps kept inside a bracket.
std::pair< double, double > RootSample
Value and slope of a function at one point.
Geometry and constants of the reed channel.
Definition Reed.hpp:36
Stopping rule of a root search.