General testing

This commit is contained in:
2026-05-05 10:32:30 +02:00
parent ff4c4aef23
commit d963032e74
11 changed files with 794 additions and 448 deletions

View File

@@ -1,21 +1,17 @@
using System;
using FluidSim.Interfaces;
using FluidSim.Utils;
namespace FluidSim.Components
{
public class Volume0D
{
public Port Port { get; private set; }
public double Mass { get; private set; }
public double InternalEnergy { get; private set; }
public double Mass { get; set; }
public double InternalEnergy { get; set; }
public double Gamma { get; set; } = 1.4;
public double GasConstant { get; set; } = 287.0;
public double Volume { get; set; }
public double dVdt { get; set; }
public double Dvdt { get; set; }
private double _dt;
@@ -24,6 +20,9 @@ namespace FluidSim.Components
public double Temperature => Pressure / (Density * GasConstant);
public double SpecificEnthalpy => Gamma / (Gamma - 1.0) * Pressure / Density;
public double MassFlowRateIn { get; set; }
public double SpecificEnthalpyIn { get; set; }
public Volume0D(double initialVolume, double initialPressure,
double initialTemperature, int sampleRate,
double gasConstant = 287.0, double gamma = 1.4)
@@ -31,54 +30,38 @@ namespace FluidSim.Components
GasConstant = gasConstant;
Gamma = gamma;
Volume = initialVolume;
dVdt = 0.0;
Dvdt = 0.0;
_dt = 1.0 / sampleRate;
double rho0 = initialPressure / (GasConstant * initialTemperature);
Mass = rho0 * Volume;
InternalEnergy = (initialPressure * Volume) / (Gamma - 1.0);
Port = new Port();
PushStateToPort();
}
public void PushStateToPort()
{
Port.Pressure = Pressure;
Port.Density = Density;
Port.Temperature = Temperature;
Port.SpecificEnthalpy = SpecificEnthalpy;
}
// Original integrate (uses the constructors sample rate)
public void Integrate()
{
Integrate(_dt);
}
public void SetPressure(double newPressure)
{
InternalEnergy = newPressure * Volume / (Gamma - 1.0);
// Mass stays the same, so density is unchanged
}
// New overload: integrate with a custom time step (for substeps)
public void Integrate(double dtOverride)
{
double mdot = Port.MassFlowRate;
double h_in = Port.SpecificEnthalpy;
double dm = mdot * dtOverride;
double dE = (mdot * h_in) * dtOverride - Pressure * dVdt * dtOverride;
double dm = MassFlowRateIn * dtOverride;
double dE = (MassFlowRateIn * SpecificEnthalpyIn) * dtOverride - Pressure * Dvdt * dtOverride;
Mass += dm;
InternalEnergy += dE;
// Hard physical bounds prevent NaN and unphysical states
if (Mass < 1e-12) Mass = 1e-12;
if (InternalEnergy < 1e-12) InternalEnergy = 1e-12;
// Safety: if mass becomes extremely small, reset internal energy to zero
if (Mass < 1e-12)
{
Mass = 0.0;
InternalEnergy = 0.0;
}
else if (InternalEnergy < 1e-12)
{
InternalEnergy = 0.0;
}
PushStateToPort();
// Avoid negative mass/energy
if (Mass < 0.0) Mass = 0.0;
if (InternalEnergy < 0.0) InternalEnergy = 0.0;
}
public void Integrate() => Integrate(_dt);
}
}