Open end working
This commit is contained in:
@@ -4,10 +4,8 @@ using FluidSim.Interfaces;
|
||||
namespace FluidSim.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// 1‑D compressible Euler pipe (finite‑volume, HLLC flux).
|
||||
/// Boundary conditions are set externally via SetGhostLeft/Right.
|
||||
/// Enforces that ghosts are always valid before stepping.
|
||||
/// Uses exponential damping and Newtonian energy relaxation.
|
||||
/// 1‑D compressible Euler pipe with Lax‑Friedrichs finite‑volume scheme.
|
||||
/// Ghost states are set externally via SetGhostLeft/Right; they are always required.
|
||||
/// </summary>
|
||||
public class Pipe1D : IComponent
|
||||
{
|
||||
@@ -17,7 +15,6 @@ namespace FluidSim.Components
|
||||
public double DampingMultiplier { get; set; } = 1.0;
|
||||
public double EnergyRelaxationRate { get; set; } = 0.0; // 1/s
|
||||
|
||||
// Ambient pressure for the energy relaxation term (default 101325 Pa)
|
||||
private double _ambientPressure = 101325.0;
|
||||
public double AmbientPressure
|
||||
{
|
||||
@@ -29,37 +26,21 @@ namespace FluidSim.Components
|
||||
}
|
||||
}
|
||||
|
||||
// Geometry
|
||||
private readonly int _n; // number of real cells
|
||||
private readonly double _dx; // cell size (m)
|
||||
private readonly double _diameter; // m
|
||||
private readonly int _n;
|
||||
private readonly double _dx;
|
||||
private readonly double _diameter;
|
||||
private readonly double _gamma = 1.4;
|
||||
|
||||
// Conserved variables [0 .. _n-1]
|
||||
private double[] _rho;
|
||||
private double[] _rhou;
|
||||
private double[] _E;
|
||||
private double[] _rho, _rhou, _E;
|
||||
private double[] _fluxM, _fluxP, _fluxE; // flux at cell faces (0.._n)
|
||||
|
||||
// Face fluxes [0 .. _n]
|
||||
private double[] _fluxM;
|
||||
private double[] _fluxP;
|
||||
private double[] _fluxE;
|
||||
|
||||
// Ghost cells (set externally)
|
||||
private double _rhoGhostL, _uGhostL, _pGhostL;
|
||||
private double _rhoGhostR, _uGhostR, _pGhostR;
|
||||
private bool _ghostLValid, _ghostRValid;
|
||||
|
||||
// Pre‑computed damping coefficient
|
||||
private double _laminarCoeff;
|
||||
private double _ambientEnergyReference; // internal energy density at ambient pressure
|
||||
private double _ambientEnergyReference;
|
||||
|
||||
/// <summary>
|
||||
/// Initialise a pipe with a given cell count.
|
||||
/// </summary>
|
||||
/// <param name="length">Pipe length (m).</param>
|
||||
/// <param name="area">Cross‑sectional area (m²).</param>
|
||||
/// <param name="cellCount">Number of finite‑volume cells (≥ 4).</param>
|
||||
public Pipe1D(double length, double area, int cellCount)
|
||||
{
|
||||
if (cellCount < 4) throw new ArgumentException("cellCount must be at least 4");
|
||||
@@ -77,48 +58,32 @@ namespace FluidSim.Components
|
||||
_fluxP = new double[_n + 1];
|
||||
_fluxE = new double[_n + 1];
|
||||
|
||||
// Laminar damping coefficient for air at 20°C (multiplied by DampingMultiplier each step)
|
||||
double mu_air = 1.8e-5;
|
||||
double radius = _diameter * 0.5;
|
||||
_laminarCoeff = 8.0 * mu_air / (radius * radius);
|
||||
|
||||
// Ambient energy reference (internal energy per unit volume at 101325 Pa)
|
||||
_ambientEnergyReference = 101325.0 / (_gamma - 1.0);
|
||||
|
||||
PortA = new Port { Owner = this };
|
||||
PortB = new Port { Owner = this };
|
||||
|
||||
// Initial state = still air at ambient conditions
|
||||
SetUniformState(1.225, 0.0, 101325.0);
|
||||
}
|
||||
|
||||
IReadOnlyList<Port> IComponent.Ports => new[] { PortA, PortB };
|
||||
|
||||
// No integration needed for pipes – state is advanced via sub‑steps
|
||||
public void UpdateState(double dt) { }
|
||||
|
||||
// ---------- Ghost cell interface ----------
|
||||
// ---------- Ghost interface ----------
|
||||
public void SetGhostLeft(double rho, double u, double p)
|
||||
{
|
||||
_rhoGhostL = rho;
|
||||
_uGhostL = u;
|
||||
_pGhostL = p;
|
||||
_ghostLValid = true;
|
||||
_rhoGhostL = rho; _uGhostL = u; _pGhostL = p; _ghostLValid = true;
|
||||
}
|
||||
|
||||
public void SetGhostRight(double rho, double u, double p)
|
||||
{
|
||||
_rhoGhostR = rho;
|
||||
_uGhostR = u;
|
||||
_pGhostR = p;
|
||||
_ghostRValid = true;
|
||||
}
|
||||
|
||||
public void ClearGhostFlags()
|
||||
{
|
||||
_ghostLValid = false;
|
||||
_ghostRValid = false;
|
||||
_rhoGhostR = rho; _uGhostR = u; _pGhostR = p; _ghostRValid = true;
|
||||
}
|
||||
public void ClearGhostFlags() { _ghostLValid = false; _ghostRValid = false; }
|
||||
|
||||
public (double rho, double u, double p) GetInteriorStateLeft()
|
||||
{
|
||||
@@ -127,7 +92,6 @@ namespace FluidSim.Components
|
||||
double p = PressureScalar(0);
|
||||
return (rho, u, p);
|
||||
}
|
||||
|
||||
public (double rho, double u, double p) GetInteriorStateRight()
|
||||
{
|
||||
double rho = Math.Max(_rho[_n - 1], 1e-12);
|
||||
@@ -135,18 +99,11 @@ namespace FluidSim.Components
|
||||
double p = PressureScalar(_n - 1);
|
||||
return (rho, u, p);
|
||||
}
|
||||
|
||||
public int CellCount => _n;
|
||||
|
||||
public double GetCellDensity(int i) => _rho[i];
|
||||
public double GetCellVelocity(int i)
|
||||
{
|
||||
double rho = Math.Max(_rho[i], 1e-12);
|
||||
return _rhou[i] / rho;
|
||||
}
|
||||
public double GetCellVelocity(int i) => _rhou[i] / Math.Max(_rho[i], 1e-12);
|
||||
public double GetCellPressure(int i) => PressureScalar(i);
|
||||
|
||||
// ---------- Sub‑stepping ----------
|
||||
public int GetRequiredSubSteps(double dtGlobal, double cflTarget = 0.8)
|
||||
{
|
||||
double maxW = 0.0;
|
||||
@@ -163,38 +120,65 @@ namespace FluidSim.Components
|
||||
return Math.Max(1, (int)Math.Ceiling(dtGlobal * maxW / (cflTarget * _dx)));
|
||||
}
|
||||
|
||||
// ---------- Main simulation step (per sub‑step) ----------
|
||||
// ---------- Main step (per sub‑step) ----------
|
||||
public void SimulateSingleStep(double dtSub)
|
||||
{
|
||||
// Enforce that both ends have been provided with ghost states
|
||||
if (!_ghostLValid || !_ghostRValid)
|
||||
throw new InvalidOperationException("Pipe boundary ghosts not set before SimulateSingleStep.");
|
||||
throw new InvalidOperationException("Ghost cells not set before SimulateSingleStep.");
|
||||
|
||||
double dt = dtSub;
|
||||
int n = _n;
|
||||
|
||||
// Left boundary face (index 0)
|
||||
HLLCFlux(_rhoGhostL, _uGhostL, _pGhostL, _rho[0], _rhou[0] / _rho[0], PressureScalar(0),
|
||||
out _fluxM[0], out _fluxP[0], out _fluxE[0]);
|
||||
// ---- Compute fluxes at all faces using Lax‑Friedrichs ----
|
||||
// Left face (0): between ghostL and cell 0
|
||||
double rL = Math.Max(_rhoGhostL, 1e-12);
|
||||
double pL = _pGhostL;
|
||||
double uL = _uGhostL;
|
||||
double eL = pL / ((_gamma - 1.0) * rL) + 0.5 * uL * uL;
|
||||
|
||||
double rR = Math.Max(_rho[0], 1e-12);
|
||||
double pR = PressureScalar(0);
|
||||
double uR = _rhou[0] / rR;
|
||||
double eR = pR / ((_gamma - 1.0) * rR) + 0.5 * uR * uR;
|
||||
|
||||
LaxFriedrichsFlux(rL, uL, pL, eL, rR, uR, pR, eR,
|
||||
out _fluxM[0], out _fluxP[0], out _fluxE[0]);
|
||||
|
||||
// Internal faces (1 .. n-1)
|
||||
for (int f = 1; f < n; f++)
|
||||
{
|
||||
double rhoL = Math.Max(_rho[f - 1], 1e-12);
|
||||
double uL = _rhou[f - 1] / rhoL;
|
||||
double pL = PressureScalar(f - 1);
|
||||
double rhoR = Math.Max(_rho[f], 1e-12);
|
||||
double uR = _rhou[f] / rhoR;
|
||||
double pR = PressureScalar(f);
|
||||
HLLCFlux(rhoL, uL, pL, rhoR, uR, pR, out _fluxM[f], out _fluxP[f], out _fluxE[f]);
|
||||
int iL = f - 1;
|
||||
int iR = f;
|
||||
|
||||
rL = Math.Max(_rho[iL], 1e-12);
|
||||
pL = PressureScalar(iL);
|
||||
uL = _rhou[iL] / rL;
|
||||
eL = pL / ((_gamma - 1.0) * rL) + 0.5 * uL * uL;
|
||||
|
||||
rR = Math.Max(_rho[iR], 1e-12);
|
||||
pR = PressureScalar(iR);
|
||||
uR = _rhou[iR] / rR;
|
||||
eR = pR / ((_gamma - 1.0) * rR) + 0.5 * uR * uR;
|
||||
|
||||
LaxFriedrichsFlux(rL, uL, pL, eL, rR, uR, pR, eR,
|
||||
out _fluxM[f], out _fluxP[f], out _fluxE[f]);
|
||||
}
|
||||
|
||||
// Right boundary face (index n)
|
||||
HLLCFlux(_rho[_n - 1], _rhou[_n - 1] / _rho[_n - 1], PressureScalar(_n - 1),
|
||||
_rhoGhostR, _uGhostR, _pGhostR,
|
||||
out _fluxM[n], out _fluxP[n], out _fluxE[n]);
|
||||
// Right face (n): between cell n-1 and ghostR
|
||||
rL = Math.Max(_rho[n - 1], 1e-12);
|
||||
pL = PressureScalar(n - 1);
|
||||
uL = _rhou[n - 1] / rL;
|
||||
eL = pL / ((_gamma - 1.0) * rL) + 0.5 * uL * uL;
|
||||
|
||||
// Cell update
|
||||
rR = Math.Max(_rhoGhostR, 1e-12);
|
||||
pR = _pGhostR;
|
||||
uR = _uGhostR;
|
||||
eR = pR / ((_gamma - 1.0) * rR) + 0.5 * uR * uR;
|
||||
|
||||
LaxFriedrichsFlux(rL, uL, pL, eL, rR, uR, pR, eR,
|
||||
out _fluxM[n], out _fluxP[n], out _fluxE[n]);
|
||||
|
||||
// ---- Cell update ----
|
||||
double dt_dx = dt / _dx;
|
||||
double coeff = _laminarCoeff * DampingMultiplier;
|
||||
double relaxRate = EnergyRelaxationRate;
|
||||
@@ -213,15 +197,12 @@ namespace FluidSim.Components
|
||||
double newRu = ru - dt_dx * dP;
|
||||
double newE = E - dt_dx * dE_flux;
|
||||
|
||||
// Wall friction damping (laminar)
|
||||
double dampingFactor = Math.Exp(-coeff / Math.Max(r, 1e-12) * dt);
|
||||
newRu *= dampingFactor;
|
||||
|
||||
// Newtonian cooling toward ambient energy
|
||||
double relaxFactor = Math.Exp(-relaxRate * dt);
|
||||
newE = _ambientEnergyReference + (newE - _ambientEnergyReference) * relaxFactor;
|
||||
|
||||
// Clamps – minimum density 1e-12, minimum pressure 100 Pa
|
||||
newR = Math.Max(newR, 1e-12);
|
||||
double kin = 0.5 * newRu * newRu / Math.Max(newR, 1e-12);
|
||||
double eMin = 100.0 / (_gamma - 1.0) + kin;
|
||||
@@ -232,77 +213,54 @@ namespace FluidSim.Components
|
||||
_E[i] = newE;
|
||||
}
|
||||
|
||||
// Update port states to reflect the current interior state (for audio / monitoring)
|
||||
// Update port states
|
||||
(double rhoA, double uA, double pA) = GetInteriorStateLeft();
|
||||
PortA.Pressure = pA;
|
||||
PortA.Density = rhoA;
|
||||
PortA.Pressure = pA; PortA.Density = rhoA;
|
||||
PortA.Temperature = pA / (rhoA * 287.0);
|
||||
PortA.SpecificEnthalpy = _gamma / (_gamma - 1.0) * pA / rhoA;
|
||||
|
||||
(double rhoB, double uB, double pB) = GetInteriorStateRight();
|
||||
PortB.Pressure = pB;
|
||||
PortB.Density = rhoB;
|
||||
PortB.Pressure = pB; PortB.Density = rhoB;
|
||||
PortB.Temperature = pB / (rhoB * 287.0);
|
||||
PortB.SpecificEnthalpy = _gamma / (_gamma - 1.0) * pB / rhoB;
|
||||
}
|
||||
|
||||
// ---------- Private helpers ----------
|
||||
// ---------- Lax‑Friedrichs flux ----------
|
||||
private void LaxFriedrichsFlux(double rL, double uL, double pL, double eL,
|
||||
double rR, double uR, double pR, double eR,
|
||||
out double fm, out double fp, out double fe)
|
||||
{
|
||||
// Primitive states
|
||||
double rhoL = rL, rhoR = rR;
|
||||
double EL = rhoL * eL; // total energy per volume = rho * (specific total energy)
|
||||
double ER = rhoR * eR;
|
||||
|
||||
// Conserved vectors U = (ρ, ρu, E)
|
||||
// Flux F = (ρu, ρu²+p, (E+p)u)
|
||||
double Fm_L = rhoL * uL;
|
||||
double Fp_L = rhoL * uL * uL + pL;
|
||||
double Fe_L = (EL + pL) * uL;
|
||||
|
||||
double Fm_R = rhoR * uR;
|
||||
double Fp_R = rhoR * uR * uR + pR;
|
||||
double Fe_R = (ER + pR) * uR;
|
||||
|
||||
// Lax‑Friedrichs dissipation coefficient α = max(|u|+c) over whole domain, but here we use local max to be simple:
|
||||
double cL = Math.Sqrt(_gamma * pL / rL);
|
||||
double cR = Math.Sqrt(_gamma * pR / rR);
|
||||
double alpha = Math.Max(Math.Abs(uL) + cL, Math.Abs(uR) + cR);
|
||||
|
||||
fm = 0.5 * (Fm_L + Fm_R) - 0.5 * alpha * (rhoR - rhoL);
|
||||
fp = 0.5 * (Fp_L + Fp_R) - 0.5 * alpha * (rhoR * uR - rhoL * uL);
|
||||
fe = 0.5 * (Fe_L + Fe_R) - 0.5 * alpha * (ER - EL);
|
||||
}
|
||||
|
||||
private double PressureScalar(int i)
|
||||
{
|
||||
double rho = Math.Max(_rho[i], 1e-12);
|
||||
return (_gamma - 1.0) * (_E[i] - 0.5 * _rhou[i] * _rhou[i] / rho);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HLLC approximate Riemann solver (Toro, 1997).
|
||||
/// Computes the numerical flux at a face given left and right states.
|
||||
/// </summary>
|
||||
private void HLLCFlux(double rL, double uL, double pL, double rR, double uR, double pR,
|
||||
out double fm, out double fp, out double fe)
|
||||
{
|
||||
double cL = Math.Sqrt(_gamma * pL / rL);
|
||||
double cR = Math.Sqrt(_gamma * pR / rR);
|
||||
double EL = pL / ((_gamma - 1.0) * rL) + 0.5 * uL * uL; // specific total energy
|
||||
double ER = pR / ((_gamma - 1.0) * rR) + 0.5 * uR * uR;
|
||||
|
||||
// Wave speed estimates (Davis, 1988)
|
||||
double SL = Math.Min(uL - cL, uR - cR);
|
||||
double SR = Math.Max(uL + cL, uR + cR);
|
||||
|
||||
double denom = rL * (SL - uL) - rR * (SR - uR);
|
||||
double Ss = (pR - pL + rL * uL * (SL - uL) - rR * uR * (SR - uR)) / denom;
|
||||
|
||||
double Fm_L = rL * uL;
|
||||
double Fp_L = rL * uL * uL + pL;
|
||||
double Fe_L = (rL * EL + pL) * uL;
|
||||
|
||||
double Fm_R = rR * uR;
|
||||
double Fp_R = rR * uR * uR + pR;
|
||||
double Fe_R = (rR * ER + pR) * uR;
|
||||
|
||||
if (SL >= 0) { fm = Fm_L; fp = Fp_L; fe = Fe_L; }
|
||||
else if (SR <= 0) { fm = Fm_R; fp = Fp_R; fe = Fe_R; }
|
||||
else if (Ss >= 0)
|
||||
{
|
||||
double rsL = rL * (SL - uL) / (SL - Ss);
|
||||
double ps = pL + rL * (SL - uL) * (Ss - uL);
|
||||
double EsL = EL + (Ss - uL) * (Ss + pL / (rL * (SL - uL)));
|
||||
fm = rsL * Ss;
|
||||
fp = rsL * Ss * Ss + ps;
|
||||
fe = (rsL * EsL + ps) * Ss;
|
||||
}
|
||||
else
|
||||
{
|
||||
double rsR = rR * (SR - uR) / (SR - Ss);
|
||||
double ps = pR + rR * (SR - uR) * (Ss - uR);
|
||||
double EsR = ER + (Ss - uR) * (Ss + pR / (rR * (SR - uR)));
|
||||
fm = rsR * Ss;
|
||||
fp = rsR * Ss * Ss + ps;
|
||||
fe = (rsR * EsR + ps) * Ss;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Initialise all cells to a uniform state (rho, u, p).</summary>
|
||||
public void SetUniformState(double rho, double u, double p)
|
||||
{
|
||||
double e = p / ((_gamma - 1.0) * rho);
|
||||
@@ -314,5 +272,24 @@ namespace FluidSim.Components
|
||||
_E[i] = E;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCellState(int i, double rho, double u, double p)
|
||||
{
|
||||
if (i < 0 || i >= _n) return;
|
||||
double e = p / ((_gamma - 1.0) * rho);
|
||||
double E = rho * e + 0.5 * rho * u * u;
|
||||
_rho[i] = rho;
|
||||
_rhou[i] = rho * u;
|
||||
_E[i] = E;
|
||||
}
|
||||
|
||||
public void SetCellPressure(int i, double p)
|
||||
{
|
||||
if (i < 0 || i >= _n) return;
|
||||
double rho = _rho[i];
|
||||
double u = _rhou[i] / rho;
|
||||
double e = p / ((_gamma - 1.0) * rho);
|
||||
_E[i] = rho * e + 0.5 * rho * u * u;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user