Known bad point (unphysical energy loss)
This commit is contained in:
@@ -55,14 +55,19 @@ namespace FluidSim.Core
|
||||
bool isLeftBoundary,
|
||||
out double massFlux, out double momFlux, out double energyFlux)
|
||||
{
|
||||
// mass flow from pipe to volume (positive = pipe → volume)
|
||||
double mdot = MassFlow(pPipe, rhoPipe, pVol, rhoVol, conn);
|
||||
// ----- Compute STAGNATION pressures -----
|
||||
double pStagPipe = pPipe + 0.5 * rhoPipe * uPipe * uPipe;
|
||||
double pStagVol = pVol + 0.5 * rhoVol * uVol * uVol; // uVol is always 0 for your volumes
|
||||
|
||||
// Mass flow driven by stagnation pressure difference (positive = pipe→volume)
|
||||
double mdot = MassFlow(pStagPipe, rhoPipe, pStagVol, rhoVol, conn);
|
||||
|
||||
// Limit mass flow to the amount that can leave/enter the pipe cell
|
||||
double maxMdot = rhoPipe * pipeArea * 343.0;
|
||||
if (Math.Abs(mdot) > maxMdot) mdot = Math.Sign(mdot) * maxMdot;
|
||||
|
||||
bool flowLeavesPipe = mdot > 0;
|
||||
bool flowLeavesPipe = mdot > 0; // pipe → volume
|
||||
|
||||
double uFace, pFace, rhoFace;
|
||||
double massFluxPerArea;
|
||||
|
||||
@@ -83,7 +88,7 @@ namespace FluidSim.Core
|
||||
{ uFace = uVol; pFace = pVol; rhoFace = rhoVol; }
|
||||
}
|
||||
|
||||
// Total enthalpy of the injected fluid (corrected: mass flux × total enthalpy)
|
||||
// Total enthalpy of the injected fluid
|
||||
double specificEnthalpy = (1.4 / (1.4 - 1.0)) * pFace / Math.Max(rhoFace, 1e-12);
|
||||
double totalEnthalpy = specificEnthalpy + 0.5 * uFace * uFace;
|
||||
|
||||
|
||||
@@ -6,58 +6,46 @@ namespace FluidSim.Core
|
||||
{
|
||||
public static class Simulation
|
||||
{
|
||||
private static Solver solver;
|
||||
private static Volume0D volA, volB;
|
||||
private static Pipe1D pipe;
|
||||
private static Connection leftConn, rightConn; // dummy connections for orifice params
|
||||
private static Connection connA, connB;
|
||||
private static int stepCount;
|
||||
private static double time;
|
||||
private static double dt;
|
||||
private static int stepCount;
|
||||
|
||||
public static void Initialize(int sampleRate)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
|
||||
double V = 5.0 * Units.L;
|
||||
volA = new Volume0D(V, 1.1 * Units.atm, Units.Celsius(20), sampleRate);
|
||||
volB = new Volume0D(V, 1.0 * Units.atm, Units.Celsius(20), sampleRate);
|
||||
|
||||
double length = 150 * Units.mm;
|
||||
double diameter = 25 * Units.mm;
|
||||
double area = Units.AreaFromDiameter(25, Units.mm);
|
||||
int nCells = 10;
|
||||
|
||||
pipe = new Pipe1D(length, area, nCells, sampleRate);
|
||||
pipe.SetUniformState(1.2, 0.0, 1.0 * Units.atm); // start at 1 atm
|
||||
pipe.SetUniformState(volA.Density, 0.0, volA.Pressure);
|
||||
pipe.FrictionFactor = 0.02;
|
||||
|
||||
// Dummy connections – only used for orifice parameters
|
||||
leftConn = new Connection(null, null) { Area = area, DischargeCoefficient = 1.0, Gamma = 1.4 };
|
||||
rightConn = new Connection(null, null) { Area = area, DischargeCoefficient = 1.0, Gamma = 1.4 };
|
||||
// Connections with orifice area equal to pipe area (flange joint)
|
||||
connA = new Connection(volA.Port, pipe.PortA) { Area = area, DischargeCoefficient = 1.0, Gamma = 1.4 };
|
||||
connB = new Connection(pipe.PortB, volB.Port) { Area = area, DischargeCoefficient = 1.0, Gamma = 1.4 };
|
||||
|
||||
solver = new Solver();
|
||||
solver.AddVolume(volA);
|
||||
solver.AddVolume(volB);
|
||||
solver.AddPipe(pipe);
|
||||
solver.AddConnection(connA);
|
||||
solver.AddConnection(connB);
|
||||
}
|
||||
|
||||
public static float Process()
|
||||
{
|
||||
// Fixed boundary reservoirs
|
||||
double pLeft = 1.1 * Units.atm;
|
||||
double rhoLeft = 1.2;
|
||||
double uLeft = 0.0;
|
||||
|
||||
double pRight = 1.0 * Units.atm;
|
||||
double rhoRight = 1.2;
|
||||
double uRight = 0.0;
|
||||
|
||||
// Compute boundary fluxes via orifice model
|
||||
OrificeBoundary.PipeVolumeFlux(
|
||||
pipe.GetLeftPressure(), pipe.GetLeftDensity(), 0.0,
|
||||
pLeft, rhoLeft, uLeft,
|
||||
leftConn, pipe.Area, true,
|
||||
out double leftMassFlux, out double leftMomFlux, out double leftEnergyFlux);
|
||||
|
||||
OrificeBoundary.PipeVolumeFlux(
|
||||
pipe.GetRightPressure(), pipe.GetRightDensity(), 0.0,
|
||||
pRight, rhoRight, uRight,
|
||||
rightConn, pipe.Area, false,
|
||||
out double rightMassFlux, out double rightMomFlux, out double rightEnergyFlux);
|
||||
|
||||
pipe.SetLeftBoundaryFlux(leftMassFlux, leftMomFlux, leftEnergyFlux);
|
||||
pipe.SetRightBoundaryFlux(rightMassFlux, rightMomFlux, rightEnergyFlux);
|
||||
pipe.Simulate();
|
||||
|
||||
solver.Step();
|
||||
time += dt;
|
||||
stepCount++;
|
||||
Log();
|
||||
@@ -66,20 +54,13 @@ namespace FluidSim.Core
|
||||
|
||||
public static void Log()
|
||||
{
|
||||
if (stepCount <= 20 || stepCount % 50 == 0)
|
||||
if (stepCount <= 50 || stepCount % 200 == 0)
|
||||
{
|
||||
Console.WriteLine($"Step {stepCount:D4} t = {time * 1e3:F3} ms");
|
||||
for (int i = 0; i < pipe.GetCellCount(); i++)
|
||||
{
|
||||
double rho = pipe.GetCellDensity(i);
|
||||
double p = pipe.GetCellPressure(i);
|
||||
double u = pipe.GetCellVelocity(i);
|
||||
Console.WriteLine($" Cell {i}: ρ={rho:F4} kg/m³ p={p / 1e5:F6} bar u={u:F3} m/s");
|
||||
}
|
||||
double leftFlow = pipe.PortA.MassFlowRate;
|
||||
double rightFlow = pipe.PortB.MassFlowRate;
|
||||
Console.WriteLine($" Left flow = {leftFlow * 1e3:F4} g/s Right flow = {rightFlow * 1e3:F4} g/s");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(
|
||||
$"t = {time * 1e3:F3} ms Step {stepCount:D4}: " +
|
||||
$"PA = {volA.Pressure / 1e5:F6} bar, " +
|
||||
$"PB = {volB.Pressure / 1e5:F6} bar, " +
|
||||
$"FlowA = {pipe.PortA.MassFlowRate * 1e3:F2} g/s");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace FluidSim.Core
|
||||
foreach (var v in _volumes)
|
||||
v.PushStateToPort();
|
||||
|
||||
// 2. Apply orifice boundaries to pipes
|
||||
// 2. Compute boundary fluxes (orifice model)
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
@@ -31,20 +31,20 @@ namespace FluidSim.Core
|
||||
VolumeToVolume(conn);
|
||||
}
|
||||
|
||||
// 3. Pipes simulate
|
||||
// 3. Pipe simulation step
|
||||
foreach (var p in _pipes)
|
||||
p.Simulate();
|
||||
|
||||
// 4. Transfer pipe flows to connected volumes
|
||||
// 4. Transfer pipe‑port data to volumes
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
Transfer(conn.PortA, conn.PortB);
|
||||
TransferPipeToVolume(conn.PortA, conn.PortB);
|
||||
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
|
||||
Transfer(conn.PortB, conn.PortA);
|
||||
TransferPipeToVolume(conn.PortB, conn.PortA);
|
||||
}
|
||||
|
||||
// 5. Volumes integrate
|
||||
// 5. Integrate volumes
|
||||
foreach (var v in _volumes)
|
||||
v.Integrate();
|
||||
}
|
||||
@@ -61,34 +61,56 @@ namespace FluidSim.Core
|
||||
|
||||
double pP = isLeft ? pipe.GetLeftPressure() : pipe.GetRightPressure();
|
||||
double rhoP = isLeft ? pipe.GetLeftDensity() : pipe.GetRightDensity();
|
||||
double uP = 0.0;
|
||||
double pV = volPort.Pressure, rhoV = volPort.Density, uV = 0.0;
|
||||
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);
|
||||
|
||||
OrificeBoundary.PipeVolumeFlux(pP, rhoP, uP, pV, rhoV, uV, conn, pipe.Area,
|
||||
isLeft, out double mf, out double pf, out double ef);
|
||||
if (isLeft)
|
||||
pipe.SetLeftBoundaryFlux(mf, pf, ef);
|
||||
pipe.SetLeftBoundaryFlux(massFlux, momFlux, energyFlux);
|
||||
else
|
||||
pipe.SetRightBoundaryFlux(mf, pf, ef);
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
void Transfer(Port pipePort, Port volPort)
|
||||
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;
|
||||
volPort.SpecificEnthalpy = pipePort.SpecificEnthalpy;
|
||||
|
||||
if (mdot < 0) // pipe → volume
|
||||
{
|
||||
// ★ pipePort.SpecificEnthalpy now contains TOTAL enthalpy
|
||||
volPort.SpecificEnthalpy = pipePort.SpecificEnthalpy;
|
||||
}
|
||||
// else: fluid goes volume → pipe → volume owns its own (static) enthalpy,
|
||||
// which is already correct.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user