MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
PressureFlowSolve.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @file PressureFlowSolve.hpp
7 * @brief Same-sample coupling of an end cell to a flow that depends on the
8 * pressure across it.
9 *
10 * An end cell with an exciter or a load at its edge has a pressure that
11 * depends on the flow, and a flow that depends on the pressure. Time
12 * centred, the two meet in one scalar equation in the pressure difference
13 * d across the coupling:
14 *
15 * d + gain * flow(d) = target
16 *
17 * where gain is half the end cell's pressure gain, target gathers the
18 * driving pressure, the cell's pressure and the flow arriving from the bore,
19 * and the end pressure afterwards is 2 (driving - d) - end. Solving it in
20 * the same sample leaves no one sample delay in the coupling, and being
21 * centred in time keeps the end second order and free of added damping.
22 *
23 * Where the left side is increasing in d the solution is unique. Where the
24 * flow law falls with the difference, as a reed does past a third of its
25 * closing pressure, uniqueness holds only while gain times the steepest fall
26 * stays under one; past that the coupling can have several solutions and
27 * the one a search finds follows its starting guess.
28 */
29
31
32/** @brief Flow through a coupling, then its slope against the pressure difference. */
33using FlowSample = std::pair<double, double>;
34
35/**
36 * @brief Gain of a time centred coupling at an end cell.
37 * @param end_pressure_gain rho c^2 k / V of the end cell, from DuctGrid
38 */
39[[nodiscard]] constexpr double flow_coupling_gain(double end_pressure_gain) noexcept
40{
41 return 0.5 * end_pressure_gain;
42}
43
44/**
45 * @brief Right side of the coupling equation.
46 * @param driving_pressure Pressure on the far side of the coupling at the half step
47 * @param end_pressure Pressure of the end cell at the start of the step
48 * @param gain From flow_coupling_gain
49 * @param downstream_flow Flow from the end cell into the next half section
50 * of the bore, positive away from the coupling
51 */
52[[nodiscard]] constexpr double flow_coupling_target(
53 double driving_pressure, double end_pressure, double gain, double downstream_flow) noexcept
54{
55 return driving_pressure - end_pressure + gain * downstream_flow;
56}
57
58/**
59 * @brief Pressure of the end cell after the step, once the difference is solved.
60 */
61[[nodiscard]] constexpr double centred_end_pressure(
62 double driving_pressure, double pressure_difference, double end_pressure) noexcept
63{
64 return 2.0 * (driving_pressure - pressure_difference) - end_pressure;
65}
66
67/**
68 * @brief Solve a coupling for any flow law.
69 * @param law Callable returning a FlowSample for a pressure difference
70 * @param gain From flow_coupling_gain
71 * @param target From flow_coupling_target
72 * @param lower Lower end of a bracket holding the solution
73 * @param upper Upper end of the bracket
74 * @param guess Starting difference, usually the previous sample's
75 * @param options Stopping rule, see bracketed_newton
76 * @return The pressure difference, the flow there, and the search status
77 *
78 * A flow whose magnitude never exceeds F brackets the solution in
79 * [target - gain F, target + gain F]. The bracket must contain a sign change
80 * of d + gain * flow(d) - target, which the search checks unless
81 * options.check_bracket is off.
82 */
83template <typename FlowLaw>
84 requires std::invocable<FlowLaw&, double>
85 && std::convertible_to<std::invoke_result_t<FlowLaw&, double>, FlowSample>
86[[nodiscard]] std::tuple<double, double, RootStatus> solve_pressure_flow(
87 FlowLaw law, double gain, double target, double lower, double upper, double guess,
88 const RootOptions& options = {})
89{
90 const auto equation = [&](double difference) -> RootSample {
91 const auto [flow, slope] = static_cast<FlowSample>(law(difference));
92 return { difference + gain * flow - target, 1.0 + gain * slope };
93 };
94
95 const auto [difference, status] = bracketed_newton(equation, lower, upper, guess, options);
96 return { difference, static_cast<FlowSample>(law(difference)).first, status };
97}
98
99} // namespace MayaFlux::Kinesis::Acoustics
Roots of scalar functions held inside a bracket.
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.
constexpr double flow_coupling_gain(double end_pressure_gain) noexcept
Gain of a time centred coupling at an end cell.
std::tuple< double, double, RootStatus > solve_pressure_flow(FlowLaw law, double gain, double target, double lower, double upper, double guess, const RootOptions &options={})
Solve a coupling for any flow law.
std::pair< double, double > FlowSample
Flow through a coupling, then its slope against the pressure difference.
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.
Stopping rule of a root search.