orifice confirmed working

This commit is contained in:
2026-05-07 13:28:41 +02:00
parent 685b48b577
commit f79cf6b7eb
5 changed files with 143 additions and 150 deletions

View File

@@ -4,23 +4,17 @@ using FluidSim.Interfaces;
namespace FluidSim.Components
{
/// <summary>
/// Zerodimensional control volume with arbitrary number of ports.
/// Integrates mass and energy fluxes from all ports.
/// Safeguards keep a tiny amount of gas to avoid negative states.
/// </summary>
public class Volume0D : IComponent
{
public List<Port> Ports { get; } = new List<Port>();
public double Mass { get; private set; }
public double InternalEnergy { get; private set; }
public double Mass { get; set; } // made public setter
public double InternalEnergy { get; set; } // made public setter
public double Volume { get; set; }
public double Dvdt { get; set; }
public double Gamma { get; set; } = 1.4;
public double GasConstant { get; set; } = 287.0;
// Ambient pressure used for emergency refill default 101325 Pa
public double AmbientPressure { get; set; } = 101325.0;
// Derived quantities
@@ -42,11 +36,9 @@ namespace FluidSim.Components
InternalEnergy = (initialPressure * Volume) / (Gamma - 1.0);
}
/// <summary>Add a new port and initialise it to the volume's current state.</summary>
public Port CreatePort()
{
var port = new Port { Owner = this };
// Set the port state immediately to avoid a mismatch before the first integration
port.Pressure = Pressure;
port.Density = Density;
port.Temperature = Temperature;
@@ -56,9 +48,18 @@ namespace FluidSim.Components
}
/// <summary>
/// Integrate over dt using the MassFlowRate and SpecificEnthalpy
/// that have been set on each port during the coupling resolution phase.
/// Set the pressure to a specific value while keeping the current temperature constant.
/// Updates Mass and InternalEnergy accordingly.
/// </summary>
public void SetPressure(double pressure)
{
double V = Math.Max(Volume, 1e-12);
double currentT = Temperature; // current temperature before changes
double rho = pressure / (GasConstant * currentT);
Mass = rho * V;
InternalEnergy = pressure * V / (Gamma - 1.0);
}
public void UpdateState(double dt)
{
double totalMdot = 0.0;
@@ -67,17 +68,15 @@ namespace FluidSim.Components
foreach (var port in Ports)
{
totalMdot += port.MassFlowRate;
// mdot * h gives energy flow: positive mdot = inflow, negative = outflow
totalEdot += port.MassFlowRate * port.SpecificEnthalpy;
}
double dm = totalMdot * dt;
double dE = totalEdot * dt - Pressure * Dvdt * dt; // piston work
double dE = totalEdot * dt - Pressure * Dvdt * dt;
Mass += dm;
InternalEnergy += dE;
// Safeguards: keep at least 1 µg of gas at a small pressure
double V = Math.Max(Volume, 1e-12);
if (Mass < 1e-9)
{
@@ -89,18 +88,16 @@ namespace FluidSim.Components
InternalEnergy = AmbientPressure * V / (Gamma - 1.0);
}
// Final nonnegative clamps (should not be needed after above)
if (Mass < 0.0) Mass = 1e-9;
if (InternalEnergy < 0.0) InternalEnergy = AmbientPressure * V / (Gamma - 1.0);
// Push updated state back to all ports
double p = Pressure, rho = Density, T = Temperature, h = SpecificEnthalpy;
foreach (var port in Ports)
{
port.Pressure = p;
port.Density = rho;
port.Temperature = T;
port.SpecificEnthalpy = h; // will be overwritten by couplings for inflow, but this is the default
port.SpecificEnthalpy = h;
}
}