orifice confirmed working
This commit is contained in:
@@ -3,14 +3,19 @@ using FluidSim.Components;
|
||||
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Characteristic open‑end boundary condition after Jones (1978).
|
||||
/// For all subsonic flow (outflow and inflow), the ghost state is derived
|
||||
/// from the isentropic expansion to ambient pressure, using the pipe's entropy,
|
||||
/// and the outgoing Riemann invariant. This avoids a density jump at flow reversal.
|
||||
/// Supersonic outflow extrapolates the interior state.
|
||||
/// </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;
|
||||
|
||||
public double LastMassFlowRate { get; private set; }
|
||||
public double LastFaceDensity { get; private set; }
|
||||
@@ -33,61 +38,63 @@ 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);
|
||||
|
||||
// Riemann invariants
|
||||
double J_plus = uInt + 2.0 * cInt / gm1;
|
||||
double J_minus = uInt - 2.0 * cInt / gm1;
|
||||
|
||||
double rhoGhost, uGhost, pGhost;
|
||||
|
||||
// ----- Supersonic outflow: extrapolate interior -----
|
||||
bool supersonicOut = IsLeftEnd ? (uInt <= -cInt) : (uInt >= cInt);
|
||||
if (supersonicOut)
|
||||
// ---- Subsonic branch (used for both outflow and inflow) ----
|
||||
// Isentropic expansion to ambient pressure using pipe's entropy
|
||||
double s = pInt / Math.Pow(rhoInt, gamma); // entropy constant
|
||||
double rhoIso = Math.Pow(pAmb / s, 1.0 / gamma);
|
||||
double cIso = Math.Sqrt(gamma * pAmb / Math.Max(rhoIso, 1e-12));
|
||||
double uIso = IsLeftEnd
|
||||
? (J_minus + 2.0 * cIso / gm1)
|
||||
: (J_plus - 2.0 * cIso / gm1);
|
||||
|
||||
// Check for supersonic outflow: if the isentropic velocity exceeds the speed of sound,
|
||||
// the flow is supersonic and we extrapolate the interior state.
|
||||
bool supersonic = IsLeftEnd
|
||||
? (uInt <= -cInt) // left end: outflow is when u < -c
|
||||
: (uInt >= cInt); // right end: outflow is when u > c
|
||||
|
||||
// Extra check: if the isentropic velocity is supersonic in the outflow direction,
|
||||
// also treat as supersonic (this can happen when the interior pressure is very high).
|
||||
if (!supersonic)
|
||||
{
|
||||
if (IsLeftEnd)
|
||||
supersonic = uIso <= -cIso;
|
||||
else
|
||||
supersonic = uIso >= cIso;
|
||||
}
|
||||
|
||||
if (supersonic)
|
||||
{
|
||||
// Supersonic outflow – extrapolate interior
|
||||
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;
|
||||
|
||||
// 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);
|
||||
|
||||
bool outflowPossible = IsLeftEnd ? (uOut <= 0) : (uOut >= 0);
|
||||
|
||||
if (outflowPossible)
|
||||
{
|
||||
// Subsonic outflow
|
||||
pGhost = pAmb;
|
||||
rhoGhost = rhoOut;
|
||||
uGhost = uOut;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Subsonic inflow (ambient reservoir model)
|
||||
pGhost = pAmb;
|
||||
rhoGhost = rhoAmb;
|
||||
uGhost = IsLeftEnd
|
||||
? (J_minus + 2.0 * aAmb / gm1)
|
||||
: (J_plus - 2.0 * aAmb / gm1);
|
||||
}
|
||||
// Subsonic flow – use the isentropic state
|
||||
rhoGhost = rhoIso;
|
||||
uGhost = uIso;
|
||||
pGhost = pAmb;
|
||||
}
|
||||
|
||||
// Apply ghost to pipe
|
||||
if (IsLeftEnd)
|
||||
Pipe.SetGhostLeft(rhoGhost, uGhost, pGhost);
|
||||
else
|
||||
Pipe.SetGhostRight(rhoGhost, uGhost, pGhost);
|
||||
|
||||
// Mass flow out of the pipe (positive = leaving)
|
||||
double area = Pipe.Area;
|
||||
double mdot = rhoGhost * uGhost * area;
|
||||
if (IsLeftEnd) mdot = -mdot;
|
||||
if (IsLeftEnd) mdot = -mdot; // left end: positive u is into pipe, so out is -u
|
||||
LastMassFlowRate = mdot;
|
||||
LastFaceDensity = rhoGhost;
|
||||
LastFaceVelocity = uGhost;
|
||||
|
||||
@@ -6,10 +6,6 @@ using FluidSim.Interfaces;
|
||||
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Top‑level solver that owns all components and couplings,
|
||||
/// orchestrates sub‑stepping, and exposes states for audio.
|
||||
/// </summary>
|
||||
public class Solver
|
||||
{
|
||||
private readonly List<IComponent> _components = new();
|
||||
@@ -19,6 +15,9 @@ namespace FluidSim.Core
|
||||
|
||||
private double _dt;
|
||||
|
||||
/// <summary>CFL target for sub‑stepping (0.3‑0.8). Lower values are safer for shocks.</summary>
|
||||
public double CflTarget { get; set; } = 0.8;
|
||||
|
||||
public void SetTimeStep(double dt) => _dt = dt;
|
||||
|
||||
public void AddComponent(IComponent component) => _components.Add(component);
|
||||
@@ -26,57 +25,38 @@ namespace FluidSim.Core
|
||||
public void AddJunction(Junction junction) => _junctions.Add(junction);
|
||||
public void AddOpenEndLink(OpenEndLink link) => _openEndLinks.Add(link);
|
||||
|
||||
// Convenience: first pipe’s port B mass flow (often the exhaust)
|
||||
public double ExhaustMassFlow
|
||||
{
|
||||
get
|
||||
{
|
||||
var pipes = _components.OfType<Pipe1D>().ToList();
|
||||
if (pipes.Count > 0)
|
||||
return Math.Abs(pipes[0].PortB.MassFlowRate);
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Advance the whole system by one global time step.
|
||||
/// </summary>
|
||||
public void Step()
|
||||
{
|
||||
var pipes = _components.OfType<Pipe1D>().ToList();
|
||||
if (pipes.Count == 0) return;
|
||||
|
||||
// 1. Determine sub‑step count (max CFL over all pipes)
|
||||
int nSub = 1;
|
||||
foreach (var p in pipes)
|
||||
nSub = Math.Max(nSub, p.GetRequiredSubSteps(_dt));
|
||||
nSub = Math.Max(nSub, p.GetRequiredSubSteps(_dt, CflTarget));
|
||||
double dtSub = _dt / nSub;
|
||||
|
||||
// 2. Sub‑step loop
|
||||
const int maxSubSteps = 10000;
|
||||
if (nSub > maxSubSteps)
|
||||
{
|
||||
Console.WriteLine($"Warning: required sub‑steps {nSub} exceeds limit. Simulation stopped.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int sub = 0; sub < nSub; sub++)
|
||||
{
|
||||
// a) Resolve all orifice links (volume ↔ pipe)
|
||||
foreach (var link in _orificeLinks)
|
||||
link.Resolve(dtSub);
|
||||
|
||||
// b) Resolve all open‑end links (pipe → atmosphere)
|
||||
foreach (var link in _openEndLinks)
|
||||
link.Resolve(dtSub);
|
||||
|
||||
// c) Resolve all junctions (pipe ↔ pipe)
|
||||
foreach (var junc in _junctions)
|
||||
junc.Resolve(dtSub);
|
||||
|
||||
// d) Advance all pipes
|
||||
foreach (var p in pipes)
|
||||
p.SimulateSingleStep(dtSub);
|
||||
}
|
||||
|
||||
// 3. Clear ghost flags
|
||||
foreach (var p in pipes)
|
||||
p.ClearGhostFlags();
|
||||
|
||||
// 4. Integrate non‑pipe components (volumes, atmosphere, etc.)
|
||||
foreach (var comp in _components)
|
||||
comp.UpdateState(_dt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user