Open end working

This commit is contained in:
2026-05-07 12:55:57 +02:00
parent bc0df51ddb
commit 685b48b577
7 changed files with 355 additions and 330 deletions

View File

@@ -4,38 +4,38 @@ namespace FluidSim.Core
{
/// <summary>
/// Compressible flow through an orifice, modelled as an isentropic nozzle.
/// Supports choked and unchoked flow, forward and reverse.
/// The caller provides the upstream stagnation state (pUp, rhoUp, TUp),
/// downstream pressure, orifice area, discharge coefficient, and gas properties.
/// Returns the face state and mass flow from upstream to downstream.
/// </summary>
public static class IsentropicOrifice
{
/// <summary>
/// Compute mass flow and face primitive state for an orifice.
/// </summary>
/// <param name="pUp">Upstream stagnation pressure (Pa).</param>
/// <param name="rhoUp">Upstream stagnation density (kg/m³).</param>
/// <param name="gamma">Ratio of specific heats.</param>
/// <param name="R">Specific gas constant (J/kg·K).</param>
/// <param name="pDown">Downstream static pressure (Pa).</param>
/// <param name="area">Effective orifice area (m²).</param>
/// <param name="Cd">Discharge coefficient (default 0.62).</param>
/// <param name="mdot">Mass flow rate (kg/s), positive from upstream to downstream.</param>
/// <param name="rhoFace">Face density (kg/m³).</param>
/// <param name="uFace">Face velocity (m/s).</param>
/// <param name="pFace">Face pressure (Pa).</param>
public static void Compute(double pUp, double rhoUp, double TUp, double gamma, double R,
double pDown, double area, double Cd,
out double mdot, out double rhoFace, out double uFace, out double pFace)
public static void Compute(
double pUp, double rhoUp, double TUp, // upstream stagnation
double pDown, // downstream back pressure
double gamma, double R, double area, double Cd,
out double mdot, out double rhoFace, out double uFace, out double pFace)
{
// mdot is positive from upstream to downstream.
double pr = Math.Max(pDown / pUp, 1e-6);
double prCrit = Math.Pow(2.0 / (gamma + 1.0), gamma / (gamma - 1.0));
if (pr < prCrit) pr = prCrit;
mdot = 0; rhoFace = rhoUp; uFace = 0; pFace = pUp;
double M = Math.Sqrt((2.0 / (gamma - 1.0)) * (Math.Pow(pr, -(gamma - 1.0) / gamma) - 1.0));
uFace = M * Math.Sqrt(gamma * R * TUp);
if (area <= 0 || pUp <= 0 || rhoUp <= 0 || TUp <= 0)
return;
double pr = pDown / pUp;
if (pr < 1e-6) pr = 1e-6;
double prCrit = Math.Pow(2.0 / (gamma + 1.0), gamma / (gamma - 1.0));
if (pr < prCrit) pr = prCrit; // choked flow
double exponent = (gamma - 1.0) / gamma;
double M = Math.Sqrt((2.0 / (gamma - 1.0)) * (Math.Pow(pr, -exponent) - 1.0));
if (double.IsNaN(M)) M = 0;
double aUp = Math.Sqrt(gamma * R * TUp);
uFace = M * aUp;
rhoFace = rhoUp * Math.Pow(pr, 1.0 / gamma);
pFace = pUp * pr;
mdot = rhoFace * uFace * area * Cd; // mass flow from upstream to downstream
mdot = rhoFace * uFace * area * Cd; // positive from upstream to downstream
}
}
}

View File

@@ -3,19 +3,15 @@ using FluidSim.Components;
namespace FluidSim.Core
{
/// <summary>
/// Characteristic openend boundary condition.
/// For subsonic outflow the outgoing Riemann invariant is conserved,
/// and the ghost pressure is set to the prescribed ambient value.
/// </summary>
public class OpenEndLink
{
public Pipe1D Pipe { get; }
public bool IsLeftEnd { get; }
public double AmbientPressure { get; set; } = 101325.0;
public double Gamma { get; set; } = 1.4;
public double GasConstant { get; set; } = 287.0;
public double AmbientTemperature { get; set; } = 300.0;
// Last resolved state (for audio / monitoring)
public double LastMassFlowRate { get; private set; }
public double LastFaceDensity { get; private set; }
public double LastFaceVelocity { get; private set; }
@@ -27,9 +23,6 @@ namespace FluidSim.Core
IsLeftEnd = isLeftEnd;
}
/// <summary>
/// Compute the ghost state and mass flow for one substep.
/// </summary>
public void Resolve(double dtSub)
{
(double rhoInt, double uInt, double pInt) = IsLeftEnd
@@ -40,80 +33,61 @@ namespace FluidSim.Core
double gm1 = gamma - 1.0;
double cInt = Math.Sqrt(gamma * pInt / Math.Max(rhoInt, 1e-12));
double pAmb = AmbientPressure;
double rhoAmb = pAmb / (GasConstant * AmbientTemperature);
double aAmb = Math.Sqrt(gamma * pAmb / rhoAmb);
double rhoGhost, uGhost, pGhost;
double mdot;
if (IsLeftEnd)
// ----- Supersonic outflow: extrapolate interior -----
bool supersonicOut = IsLeftEnd ? (uInt <= -cInt) : (uInt >= cInt);
if (supersonicOut)
{
// Left end: outgoing invariant is J- = u - 2c/(γ-1)
rhoGhost = rhoInt;
uGhost = uInt;
pGhost = pInt;
}
else
{
// Riemann invariants
double J_plus = uInt + 2.0 * cInt / gm1;
double J_minus = uInt - 2.0 * cInt / gm1;
if (uInt <= -cInt) // supersonic inflow (all info from outside)
{
// Simple reservoir model use ambient density and temperature 300 K
rhoGhost = pAmb / (287.0 * 300.0);
uGhost = uInt; // keep interior velocity (should be supersonic inward)
pGhost = pAmb;
}
else if (uInt < 0) // subsonic inflow
{
double rhoAmb = pAmb / (287.0 * 300.0);
double cAmb = Math.Sqrt(gamma * pAmb / rhoAmb);
uGhost = J_minus + 2.0 * cAmb / gm1;
rhoGhost = rhoAmb;
pGhost = pAmb;
}
else // subsonic outflow (uInt >= 0)
{
double s = pInt / Math.Pow(rhoInt, gamma);
rhoGhost = Math.Pow(pAmb / s, 1.0 / gamma);
double cGhost = Math.Sqrt(gamma * pAmb / rhoGhost);
uGhost = J_minus + 2.0 * cGhost / gm1;
if (uGhost < 0) uGhost = 0;
pGhost = pAmb;
}
}
else // Right end
{
// Right end: outgoing invariant is J+ = u + 2c/(γ-1)
double J_plus = uInt + 2.0 * cInt / gm1;
// Trial subsonic outflow ghost state
double s = pInt / Math.Pow(rhoInt, gamma);
double rhoOut = Math.Pow(pAmb / s, 1.0 / gamma);
double cOut = Math.Sqrt(gamma * pAmb / rhoOut);
double uOut = IsLeftEnd
? (J_minus + 2.0 * cOut / gm1)
: (J_plus - 2.0 * cOut / gm1);
if (uInt >= cInt) // supersonic outflow
bool outflowPossible = IsLeftEnd ? (uOut <= 0) : (uOut >= 0);
if (outflowPossible)
{
rhoGhost = rhoInt;
uGhost = uInt;
pGhost = pInt;
}
else if (uInt >= 0) // subsonic outflow
{
double s = pInt / Math.Pow(rhoInt, gamma);
rhoGhost = Math.Pow(pAmb / s, 1.0 / gamma);
double cGhost = Math.Sqrt(gamma * pAmb / rhoGhost);
uGhost = J_plus - 2.0 * cGhost / gm1;
if (uGhost < 0) uGhost = 0;
// Subsonic outflow
pGhost = pAmb;
rhoGhost = rhoOut;
uGhost = uOut;
}
else // subsonic inflow (uInt < 0)
else
{
double rhoAmb = pAmb / (287.0 * 300.0);
double cAmb = Math.Sqrt(gamma * pAmb / rhoAmb);
uGhost = J_plus - 2.0 * cAmb / gm1;
// Subsonic inflow (ambient reservoir model)
pGhost = pAmb;
rhoGhost = rhoAmb;
pGhost = pAmb;
uGhost = IsLeftEnd
? (J_minus + 2.0 * aAmb / gm1)
: (J_plus - 2.0 * aAmb / gm1);
}
}
// Apply ghost to pipe
if (IsLeftEnd)
Pipe.SetGhostLeft(rhoGhost, uGhost, pGhost);
else
Pipe.SetGhostRight(rhoGhost, uGhost, pGhost);
// Mass flow (positive = out of pipe)
double area = Pipe.Area;
mdot = rhoGhost * uGhost * area;
if (IsLeftEnd) mdot = -mdot; // positive u into pipe, so out of pipe is negative u
double mdot = rhoGhost * uGhost * area;
if (IsLeftEnd) mdot = -mdot;
LastMassFlowRate = mdot;
LastFaceDensity = rhoGhost;
LastFaceVelocity = uGhost;

View File

@@ -6,7 +6,8 @@ namespace FluidSim.Core
{
/// <summary>
/// Connects a port (volume or atmosphere) to one end of a pipe via an orifice.
/// The area can be dynamic (Func<double>).
/// Uses the isentropic nozzle model for the steadystate relationship,
/// and includes acoustic inertance for dynamic (Helmholtz) behaviour.
/// </summary>
public class OrificeLink
{
@@ -15,105 +16,131 @@ namespace FluidSim.Core
public bool IsPipeLeftEnd { get; }
public Func<double> AreaProvider { get; set; }
public double DischargeCoefficient { get; set; } = 0.62;
public double Gamma { get; set; } = 1.4;
public double GasConstant { get; set; } = 287.0;
// Last resolved state (for audio/monitoring)
// Acoustic length (wall thickness + end correction) controls the resonance frequency
public double EffectiveLength { get; set; } = 0.001; // 1 mm
// Whether to include inertance; if false, uses the steadystate nozzle model directly
public bool UseInertance { get; set; } = true;
// Current mass flow (kg/s, positive = volume → pipe)
private double _mdot;
public double LastMassFlowRate { get; private set; }
public double LastFaceDensity { get; private set; }
public double LastFaceVelocity { get; private set; }
public double LastFacePressure { get; private set; }
public OrificeLink(Port volumePort, Pipe1D pipe, bool isPipeLeftEnd, Func<double> areaProvider)
public OrificeLink(Port? volumePort, Pipe1D pipe, bool isPipeLeftEnd, Func<double> areaProvider)
{
VolumePort = volumePort ?? throw new ArgumentNullException(nameof(volumePort));
VolumePort = volumePort; // null is allowed
Pipe = pipe ?? throw new ArgumentNullException(nameof(pipe));
IsPipeLeftEnd = isPipeLeftEnd;
AreaProvider = areaProvider ?? throw new ArgumentNullException(nameof(areaProvider));
_mdot = 0.0;
}
/// <summary>
/// Resolve the coupling for one substep. Computes nozzle flow (isentropic)
/// and sets the pipe ghost cell and the port flow rates.
/// </summary>
public void Resolve(double dtSub)
{
double area = AreaProvider();
if (area < 1e-12)
// Closed wall or missing volume port => reflective boundary
if (area < 1e-12 || VolumePort == null)
{
SetClosedWall();
return;
}
// Retrieve volume state
double volP = VolumePort.Pressure;
// Gather volume state
double volP = VolumePort.Pressure;
double volRho = VolumePort.Density;
double volT = VolumePort.Temperature;
double volH = VolumePort.SpecificEnthalpy;
double volT = VolumePort.Temperature;
double volH = VolumePort.SpecificEnthalpy;
// Retrieve pipe interior state at the connected end
// Gather pipe interior state at the connected end
(double pipeRho, double pipeU, double pipeP) = IsPipeLeftEnd
? Pipe.GetInteriorStateLeft()
: Pipe.GetInteriorStateRight();
// Determine upstream/downstream: if volume pressure > pipe pressure, flow is out of volume (negative into volume).
bool flowOutOfVolume = volP > pipeP;
double pUp, rhoUp, TUp, pDown;
if (flowOutOfVolume)
double pipeT = pipeP / Math.Max(pipeRho * 287.0, 1e-12);
double gamma = 1.4;
double R = 287.0;
// ---- Steadystate mass flow from isentropic nozzle ----
double mdotSS; // positive = volume → pipe
double rhoFace, uFace, pFace;
if (volP >= pipeP)
{
pUp = volP; rhoUp = volRho; TUp = volT; pDown = pipeP;
IsentropicOrifice.Compute(volP, volRho, volT, pipeP, gamma, R, area, DischargeCoefficient,
out double mdotUpToDown, out rhoFace, out uFace, out pFace);
mdotSS = mdotUpToDown; // volume → pipe
}
else
{
// Pipe is upstream
pUp = pipeP; rhoUp = pipeRho; TUp = pipeP / (pipeRho * GasConstant); // temperature from pipe
pDown = volP;
IsentropicOrifice.Compute(pipeP, pipeRho, pipeT, volP, gamma, R, area, DischargeCoefficient,
out double mdotUpToDown, out rhoFace, out uFace, out pFace);
mdotSS = -mdotUpToDown; // pipe → volume → negative for volume→pipe convention
}
// Compute isentropic nozzle flow
IsentropicOrifice.Compute(pUp, rhoUp, TUp, Gamma, GasConstant, pDown, area, DischargeCoefficient,
out double mdotUpstreamToDown, out double rhoFace, out double uFace, out double pFace);
// mdotUpstreamToDown is positive from upstream to downstream.
// Convert to mass flow into volume (positive mdot = into volume).
double mdotVolume;
if (flowOutOfVolume)
mdotVolume = -mdotUpstreamToDown; // out of volume is negative
// ---- Inertance ODE (optional) ----
if (UseInertance)
{
double rhoUp = _mdot >= 0 ? volRho : pipeRho;
double inertance = rhoUp * EffectiveLength / area;
double dp = volP - pipeP;
double resistance = Math.Abs(dp) / Math.Max(Math.Abs(mdotSS), 1e-12);
double dmdot_dt = (dp - resistance * _mdot) / inertance;
_mdot += dmdot_dt * dtSub;
}
else
mdotVolume = mdotUpstreamToDown; // into volume is positive
{
_mdot = mdotSS;
}
// Clamp mass flow to available mass in volume (if it is a Volume0D)
// Clamp outflow to available mass (if finite volume)
if (VolumePort.Owner is Volume0D vol)
{
double maxMdot = vol.Mass / dtSub;
if (mdotVolume > maxMdot) mdotVolume = maxMdot;
if (mdotVolume < -maxMdot) mdotVolume = -maxMdot;
double maxOut = vol.Mass / dtSub;
if (_mdot > maxOut) _mdot = maxOut;
}
// Apply ghost state to pipe
// ---- Ghost state ----
// Density = upstream density (consistent with current flow direction)
rhoFace = _mdot >= 0 ? volRho : pipeRho;
// Pressure = downstream pressure (consistent with nozzle exit)
pFace = _mdot >= 0 ? pipeP : volP;
// Velocity magnitude derived from actual mass flow
double mdotMag = Math.Abs(_mdot);
uFace = mdotMag / (rhoFace * area);
if (IsPipeLeftEnd)
uFace = _mdot >= 0 ? uFace : -uFace; // left end: positive u = into pipe
else
uFace = _mdot >= 0 ? -uFace : uFace; // right end: positive u = out of pipe
// Apply ghost to pipe
if (IsPipeLeftEnd)
Pipe.SetGhostLeft(rhoFace, uFace, pFace);
else
Pipe.SetGhostRight(rhoFace, uFace, pFace);
// Store results
LastMassFlowRate = mdotVolume;
LastFaceDensity = rhoFace;
// ---- Store results ----
double mdotIntoVolume = -_mdot; // positive = into volume
LastMassFlowRate = mdotIntoVolume;
LastFaceDensity = rhoFace;
LastFaceVelocity = uFace;
LastFacePressure = pFace;
// Set port flow rates for volume integration
VolumePort.MassFlowRate = mdotVolume;
if (mdotVolume >= 0)
VolumePort.MassFlowRate = mdotIntoVolume;
// Enthalpy for volume integration
if (mdotIntoVolume >= 0) // inflow → pipe enthalpy
{
// Inflow: enthalpy comes from upstream (pipe)
double pPipe = pipeP;
double rhoPipe = pipeRho;
VolumePort.SpecificEnthalpy = Gamma / (Gamma - 1.0) * pPipe / rhoPipe;
double hPipe = gamma / (gamma - 1.0) * pipeP / Math.Max(pipeRho, 1e-12);
VolumePort.SpecificEnthalpy = hPipe;
}
else
else // outflow → volume's own enthalpy
{
// Outflow: volume's own specific enthalpy
VolumePort.SpecificEnthalpy = volH;
}
}
@@ -130,11 +157,12 @@ namespace FluidSim.Core
Pipe.SetGhostRight(rInt, -uInt, pInt);
LastMassFlowRate = 0.0;
LastFaceDensity = rInt;
LastFaceDensity = rInt;
LastFaceVelocity = 0.0;
LastFacePressure = pInt;
VolumePort.MassFlowRate = 0.0;
// Keep specific enthalpy as is (not used)
// Don't touch VolumePort if it's null
if (VolumePort != null)
VolumePort.MassFlowRate = 0.0;
}
}
}

View File

@@ -1,18 +1,20 @@
using System;
using FluidSim.Interfaces;
using FluidSim.Core;
namespace FluidSim.Core
{
public class SoundProcessor
{
private readonly double dt;
private readonly double scaleFactor; // 1 / (4π r) and a user gain
private readonly double scaleFactor; // 1 / (4π r)
private double prevMassFlowOut;
// Simple lowpass for derivative smoothing (≈ 23 ms)
private double smoothDMdt;
private readonly double alpha;
// New: lowpass the mass flow signal before derivative
private double flowLP;
private readonly double lpAlpha;
public float Gain { get; set; } = 1.0f;
public SoundProcessor(int sampleRate, double listenerDistanceMeters = 1.0)
@@ -20,29 +22,34 @@ namespace FluidSim.Core
dt = 1.0 / sampleRate;
scaleFactor = 1.0 / (4.0 * Math.PI * listenerDistanceMeters);
// Smoothing time constant ~ 2 ms, blocks singlesample spikes
double tau = 0.002;
// Smoothing time constant for the derivative: 10 ms (much smoother)
double tau = 0.010; // 10 ms
alpha = Math.Exp(-dt / tau);
// Lowpass time constant for the mass flow: 5 ms (kneecap highfreq directly)
double tauLP = 0.005;
lpAlpha = Math.Exp(-dt / tauLP);
}
public float Process(Port port)
public float Process(OpenEndLink openEnd)
{
// Outflow mass flow (positive = leaving pipe)
double flowOut = -port.MassFlowRate;
double flowOut = openEnd.LastMassFlowRate;
// Derivative
double rawDerivative = (flowOut - prevMassFlowOut) / dt;
prevMassFlowOut = flowOut;
// Lowpass the mass flow signal
flowLP = lpAlpha * flowLP + (1.0 - lpAlpha) * flowOut;
// Smooth the derivative to kill isolated spikes
// Derivative of the smoothed mass flow
double rawDerivative = (flowLP - prevMassFlowOut) / dt;
prevMassFlowOut = flowLP;
// Smooth the derivative
smoothDMdt = alpha * smoothDMdt + (1.0 - alpha) * rawDerivative;
// Farfield monopole pressure
double pressure = smoothDMdt * scaleFactor * Gain;
// Soft clip to ±1 for audio output (safe limit)
float sample = (float)Math.Tanh(pressure);
return sample;
// Soft clip to ±1 (should rarely trigger now)
return (float)Math.Tanh(pressure);
}
}
}