Files
FluidSim/Core/Simulation.cs
2026-05-02 16:58:40 +02:00

86 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using FluidSim.Components;
using FluidSim.Utils;
namespace FluidSim.Core
{
public static class Simulation
{
private static Pipe1D pipe;
private static Connection leftConn, rightConn; // dummy connections for orifice params
private static double time;
private static double dt;
private static int stepCount;
public static void Initialize(int sampleRate)
{
dt = 1.0 / 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.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 };
}
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();
time += dt;
stepCount++;
Log();
return 0f;
}
public static void Log()
{
if (stepCount <= 20 || stepCount % 50 == 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();
}
}
}
}