Energy conservation fixed

This commit is contained in:
max
2026-05-02 18:05:14 +02:00
parent 650b993c2b
commit ab50b808fb
2 changed files with 107 additions and 121 deletions

View File

@@ -16,26 +16,24 @@ namespace FluidSim.Core
public void Step()
{
// 1. Volumes publish state
// 1. Volumes publish state to their ports
foreach (var v in _volumes)
v.PushStateToPort();
// 2. Compute boundary fluxes (orifice model)
// 2. Set volume states as boundary conditions on pipes
foreach (var conn in _connections)
{
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
ApplyOrifice(conn, conn.PortA, conn.PortB);
SetVolumeBC(conn.PortA, conn.PortB);
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
ApplyOrifice(conn, conn.PortB, conn.PortA);
else if (IsVolumePort(conn.PortA) && IsVolumePort(conn.PortB))
VolumeToVolume(conn);
SetVolumeBC(conn.PortB, conn.PortA);
}
// 3. Pipe simulation step
// 3. Run pipe simulations
foreach (var p in _pipes)
p.Simulate();
// 4. Transfer pipeport data to volumes
// 4. Transfer pipeport flows to volume ports
foreach (var conn in _connections)
{
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
@@ -53,64 +51,29 @@ namespace FluidSim.Core
bool IsPipePort(Port p) => _pipes.Exists(pp => pp.PortA == p || pp.PortB == p);
Pipe1D GetPipe(Port p) => _pipes.Find(pp => pp.PortA == p || pp.PortB == p);
void ApplyOrifice(Connection conn, Port pipePort, Port volPort)
void SetVolumeBC(Port pipePort, Port volPort)
{
Pipe1D pipe = GetPipe(pipePort);
if (pipe == null) return;
bool isLeft = pipe.PortA == pipePort;
double pP = isLeft ? pipe.GetLeftPressure() : pipe.GetRightPressure();
double rhoP = isLeft ? pipe.GetLeftDensity() : pipe.GetRightDensity();
double uP = isLeft ? pipe.GetCellVelocity(0)
: pipe.GetCellVelocity(pipe.GetCellCount() - 1);
double pV = volPort.Pressure;
double rhoV = volPort.Density;
double uV = 0.0; // volume has zero organized velocity
OrificeBoundary.PipeVolumeFlux(
pP, rhoP, uP,
pV, rhoV, uV,
conn, pipe.Area, isLeft,
out double massFlux, out double momFlux, out double energyFlux);
if (isLeft)
pipe.SetLeftBoundaryFlux(massFlux, momFlux, energyFlux);
pipe.SetLeftVolumeState(volPort.Density, volPort.Pressure);
else
pipe.SetRightBoundaryFlux(massFlux, momFlux, energyFlux);
}
void VolumeToVolume(Connection conn)
{
double mdot = OrificeBoundary.MassFlow(
conn.PortA.Pressure, conn.PortA.Density,
conn.PortB.Pressure, conn.PortB.Density, conn);
conn.PortA.MassFlowRate = -mdot;
conn.PortB.MassFlowRate = mdot;
if (mdot > 0)
conn.PortB.SpecificEnthalpy = conn.PortA.SpecificEnthalpy;
else if (mdot < 0)
conn.PortA.SpecificEnthalpy = conn.PortB.SpecificEnthalpy;
pipe.SetRightVolumeState(volPort.Density, volPort.Pressure);
}
void TransferPipeToVolume(Port pipePort, Port volPort)
{
double mdot = pipePort.MassFlowRate;
// mdot > 0 → fluid enters pipe from volume
// mdot < 0 → fluid leaves pipe and enters volume
// Volume mass flow sign is opposite (positive into volume)
volPort.MassFlowRate = -mdot;
if (mdot < 0) // pipe → volume
{
// pipePort.SpecificEnthalpy now contains TOTAL enthalpy
// pipePort.SpecificEnthalpy is already total (h + ½u²)
volPort.SpecificEnthalpy = pipePort.SpecificEnthalpy;
}
// else: fluid goes volume → pipe volume owns its own (static) enthalpy,
// which is already correct.
// else: volume → pipe, volumes own static enthalpy is used (already set)
}
}
}