7 [[nodiscard]]
bool positive_finite(
double value)
noexcept
9 return std::isfinite(value) && value > 0.0;
12 [[nodiscard]]
bool channel_valid(
const ReedChannel& reed)
noexcept
14 return positive_finite(reed.width) && positive_finite(reed.rest_opening)
15 && positive_finite(reed.closing_pressure) && positive_finite(reed.air_density);
22 return reed.width * reed.rest_opening * std::sqrt(2.0 / reed.air_density);
27 if (!channel_valid(reed)) {
31 const double opening = 1.0 - difference / reed.closing_pressure;
35 return std::copysign(
reed_flow_coefficient(reed) * opening * std::sqrt(std::abs(difference)), difference);
46 double end_pressure_gain,
47 double mouth_pressure,
49 double downstream_flow)
noexcept
54 const auto wall = [&](
RootStatus status) -> std::tuple<double, double, double, RootStatus> {
59 if (!channel_valid(reed) || !std::isfinite(target) || !positive_finite(gain)) {
63 const double closing = reed.closing_pressure;
64 if (target >= closing) {
68 const double magnitude = std::abs(target);
69 if (magnitude == 0.0) {
74 const double sign = target > 0.0 ? 1.0 : -1.0;
76 const double plain_root = 2.0 * magnitude / (a + std::sqrt(a * a + 4.0 * magnitude));
78 const double lower = sign > 0.0 ? plain_root : 0.0;
79 const double upper = sign > 0.0 ? std::sqrt(magnitude) : plain_root;
81 const auto equation = [&](
double s) ->
RootSample {
82 const double cubic = a * s * s * s / closing;
84 s * s + a * s - sign * cubic - magnitude,
85 2.0 * s + a - 3.0 * sign * a * s * s / closing
89 const double inflection = closing / (3.0 * a);
90 double start = warm_start;
91 if (sign < 0.0 || upper <= inflection) {
93 }
else if (lower >= inflection) {
99 const auto [root, status] =
bracketed_newton(equation, lower, upper, start, options);
102 const double difference = sign * root * root;
117 std::array<double, 2>& motion,
118 double end_pressure_gain,
119 double mouth_pressure,
121 double downstream_flow)
noexcept
124 const double target =
flow_coupling_target(mouth_pressure, end_pressure, gain, downstream_flow);
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 };
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);
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);
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;
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;
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);
156 motion = { std::max(next, -reed.rest_opening), motion[0] };
157 return {
centred_end_pressure(mouth_pressure, difference, end_pressure), flow + pushed, difference };
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.
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|.
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.
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.
bool reed_coupling_is_unique(const ReedChannel &reed, double gain) noexcept
Whether a coupling gain keeps the solution unique.
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.
Stopping rule of a root search.