Added boundary states for correct resonances
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using FluidSim.Components;
|
||||
using FluidSim.Interfaces;
|
||||
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using FluidSim.Components;
|
||||
using FluidSim.Interfaces;
|
||||
using FluidSim.Utils;
|
||||
|
||||
namespace FluidSim.Core
|
||||
@@ -7,76 +8,64 @@ namespace FluidSim.Core
|
||||
public static class Simulation
|
||||
{
|
||||
private static Solver solver;
|
||||
private static Volume0D volA, volB;
|
||||
private static Pipe1D pipe;
|
||||
private static Connection connA, connB;
|
||||
private static int stepCount;
|
||||
private static double time;
|
||||
private static double dt;
|
||||
private static float sample;
|
||||
private static double ambientPressure = 1.0 * Units.atm;
|
||||
|
||||
public static void Initialize(int sampleRate)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
|
||||
double V = 5.0 * Units.L;
|
||||
volA = new Volume0D(V, 2.0 * Units.atm, Units.Celsius(20), sampleRate);
|
||||
volB = new Volume0D(V, 1.0 * Units.atm, Units.Celsius(20), sampleRate);
|
||||
double length = 0.2;
|
||||
double radius = 5 * Units.mm;
|
||||
double area = Units.AreaFromDiameter(radius);
|
||||
|
||||
double length = 150 * Units.mm;
|
||||
double diameter = 25 * Units.mm;
|
||||
double area = Units.AreaFromDiameter(25, Units.mm);
|
||||
|
||||
pipe = new Pipe1D(length, area, sampleRate);
|
||||
pipe.SetUniformState(volA.Density, 0.0, volA.Pressure);
|
||||
pipe.FrictionFactor = 0.02;
|
||||
|
||||
// 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 };
|
||||
pipe = new Pipe1D(length, area, sampleRate, forcedCellCount: 80);
|
||||
pipe.SetUniformState(1.225, 0.0, ambientPressure);
|
||||
pipe.FrictionFactor = 0.0;
|
||||
|
||||
solver = new Solver();
|
||||
solver.AddVolume(volA);
|
||||
solver.AddVolume(volB);
|
||||
solver.SetTimeStep(dt);
|
||||
solver.AddPipe(pipe);
|
||||
solver.AddConnection(connA);
|
||||
solver.AddConnection(connB);
|
||||
solver.SetPipeBoundary(pipe, isLeft: true, BoundaryType.OpenEnd, ambientPressure);
|
||||
solver.SetPipeBoundary(pipe, isLeft: false, BoundaryType.ClosedEnd);
|
||||
|
||||
// Excite the pipe with an initial pressure pulse near the open end
|
||||
int pulseCells = 5;
|
||||
double pulsePressure = 4 * ambientPressure;
|
||||
for (int i = 0; i < pulseCells; i++)
|
||||
pipe.SetCellState(i, 1.225, 0.0, pulsePressure);
|
||||
}
|
||||
|
||||
public static float Process()
|
||||
{
|
||||
solver.Step();
|
||||
sample = solver.Step();
|
||||
time += dt;
|
||||
stepCount++;
|
||||
|
||||
// Override the audio sample with mid-pipe pressure deviation
|
||||
double pMid = pipe.GetPressureAtFraction(0.5);
|
||||
sample = (float)((pMid - ambientPressure) / ambientPressure);
|
||||
|
||||
Log();
|
||||
return 0f;
|
||||
return sample;
|
||||
}
|
||||
|
||||
public static void Log()
|
||||
{
|
||||
bool logPipe = true;
|
||||
|
||||
if ((stepCount <= 10 || (stepCount <= 1000 && stepCount % 100 == 0)) || stepCount % 1000 == 0 && stepCount < 10000)
|
||||
if (stepCount % 10 == 0 && stepCount < 1000)
|
||||
{
|
||||
// Summary line
|
||||
double pMid = pipe.GetPressureAtFraction(0.5);
|
||||
double pOpen = pipe.GetCellPressure(0);
|
||||
double pClosed = pipe.GetCellPressure(pipe.GetCellCount() - 1);
|
||||
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");
|
||||
|
||||
// Per‑cell state
|
||||
if (logPipe && stepCount <= 1000)
|
||||
{
|
||||
int n = pipe.GetCellCount();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double rho = pipe.GetCellDensity(i);
|
||||
double p = pipe.GetCellPressure(i);
|
||||
double u = pipe.GetCellVelocity(i);
|
||||
Console.WriteLine(
|
||||
$" Cell {i,2}: ρ={rho,8:F4} kg/m³, p={p,10:F2} Pa, u={u,8:F3} m/s");
|
||||
}
|
||||
}
|
||||
$"Sample: = {sample:F3}, " +
|
||||
$"P_mid = {pMid:F2} Pa ({pMid / ambientPressure:F4} atm), " +
|
||||
$"P_open = {pOpen:F2} Pa, P_closed = {pClosed:F2} Pa");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
143
Core/Solver.cs
143
Core/Solver.cs
@@ -10,70 +10,163 @@ namespace FluidSim.Core
|
||||
private readonly List<Pipe1D> _pipes = new();
|
||||
private readonly List<Connection> _connections = new();
|
||||
|
||||
private double _dt; // global time step
|
||||
|
||||
public void AddVolume(Volume0D v) => _volumes.Add(v);
|
||||
public void AddPipe(Pipe1D p) => _pipes.Add(p);
|
||||
public void AddConnection(Connection c) => _connections.Add(c);
|
||||
|
||||
public void Step()
|
||||
/// <summary>Set the global time step (called from Simulation).</summary>
|
||||
public void SetTimeStep(double dt) => _dt = dt;
|
||||
|
||||
/// <summary>
|
||||
/// Convenient method to set the boundary type of a pipe end.
|
||||
/// </summary>
|
||||
public void SetPipeBoundary(Pipe1D pipe, bool isLeft, BoundaryType type, double ambientPressure = 101325.0)
|
||||
{
|
||||
// 1. Volumes publish state to their ports
|
||||
if (isLeft)
|
||||
{
|
||||
pipe.SetLeftBoundaryType(type);
|
||||
if (type == BoundaryType.OpenEnd)
|
||||
pipe.SetLeftAmbientPressure(ambientPressure);
|
||||
}
|
||||
else
|
||||
{
|
||||
pipe.SetRightBoundaryType(type);
|
||||
if (type == BoundaryType.OpenEnd)
|
||||
pipe.SetRightAmbientPressure(ambientPressure);
|
||||
}
|
||||
}
|
||||
|
||||
public float Step()
|
||||
{
|
||||
// 1. Volumes publish state to ports (only needed if any volume exists)
|
||||
foreach (var v in _volumes)
|
||||
v.PushStateToPort();
|
||||
|
||||
// 2. Set volume states as boundary conditions on pipes
|
||||
// 2. Set initial pipe boundary conditions ONLY for volume‑coupled ends
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
SetVolumeBC(conn.PortA, conn.PortB);
|
||||
{
|
||||
var pipe = GetPipe(conn.PortA);
|
||||
bool isLeft = pipe.PortA == conn.PortA;
|
||||
BoundaryType bc = isLeft ? pipe.LeftBCType : pipe.RightBCType;
|
||||
if (bc == BoundaryType.VolumeCoupling)
|
||||
SetVolumeBC(conn.PortA, conn.PortB);
|
||||
}
|
||||
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
|
||||
SetVolumeBC(conn.PortB, conn.PortA);
|
||||
{
|
||||
var pipe = GetPipe(conn.PortB);
|
||||
bool isLeft = pipe.PortB == conn.PortB;
|
||||
BoundaryType bc = isLeft ? pipe.LeftBCType : pipe.RightBCType;
|
||||
if (bc == BoundaryType.VolumeCoupling)
|
||||
SetVolumeBC(conn.PortB, conn.PortA);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Run pipe simulations
|
||||
// 3. Determine number of sub‑steps
|
||||
int nSub = 1;
|
||||
foreach (var p in _pipes)
|
||||
p.Simulate();
|
||||
nSub = Math.Max(nSub, p.GetRequiredSubSteps(_dt));
|
||||
double dtSub = _dt / nSub;
|
||||
|
||||
// 4. Transfer pipe‑port flows to volume ports
|
||||
foreach (var conn in _connections)
|
||||
// 4. Sub‑step loop
|
||||
for (int sub = 0; sub < nSub; sub++)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
TransferPipeToVolume(conn.PortA, conn.PortB);
|
||||
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
|
||||
TransferPipeToVolume(conn.PortB, conn.PortA);
|
||||
foreach (var p in _pipes)
|
||||
p.SimulateSingleStep(dtSub);
|
||||
|
||||
// Transfer flows only for volume‑coupled connections
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
{
|
||||
var pipe = GetPipe(conn.PortA);
|
||||
bool isLeft = pipe.PortA == conn.PortA;
|
||||
if (pipe.LeftBCType == BoundaryType.VolumeCoupling || pipe.RightBCType == BoundaryType.VolumeCoupling)
|
||||
TransferAndIntegrate(conn.PortA, conn.PortB, dtSub);
|
||||
}
|
||||
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
|
||||
{
|
||||
var pipe = GetPipe(conn.PortB);
|
||||
bool isLeft = pipe.PortB == conn.PortB;
|
||||
if (pipe.LeftBCType == BoundaryType.VolumeCoupling || pipe.RightBCType == BoundaryType.VolumeCoupling)
|
||||
TransferAndIntegrate(conn.PortB, conn.PortA, dtSub);
|
||||
}
|
||||
}
|
||||
|
||||
// Update BCs for volume‑coupled ends between sub‑steps
|
||||
if (sub < nSub - 1)
|
||||
{
|
||||
foreach (var v in _volumes)
|
||||
v.PushStateToPort();
|
||||
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
|
||||
{
|
||||
var pipe = GetPipe(conn.PortA);
|
||||
bool isLeft = pipe.PortA == conn.PortA;
|
||||
if ((isLeft && pipe.LeftBCType == BoundaryType.VolumeCoupling) ||
|
||||
(!isLeft && pipe.RightBCType == BoundaryType.VolumeCoupling))
|
||||
SetVolumeBC(conn.PortA, conn.PortB);
|
||||
}
|
||||
else if (IsVolumePort(conn.PortA) && IsPipePort(conn.PortB))
|
||||
{
|
||||
var pipe = GetPipe(conn.PortB);
|
||||
bool isLeft = pipe.PortB == conn.PortB;
|
||||
if ((isLeft && pipe.LeftBCType == BoundaryType.VolumeCoupling) ||
|
||||
(!isLeft && pipe.RightBCType == BoundaryType.VolumeCoupling))
|
||||
SetVolumeBC(conn.PortB, conn.PortA);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Integrate volumes
|
||||
foreach (var v in _volumes)
|
||||
v.Integrate();
|
||||
// 5. Audio samples from SoundConnections (if any)
|
||||
var audioSamples = new List<float>();
|
||||
foreach (var conn in _connections)
|
||||
{
|
||||
if (conn is SoundConnection sc)
|
||||
audioSamples.Add(sc.GetAudioSample());
|
||||
}
|
||||
|
||||
// 6. Clear volume BC flags
|
||||
foreach (var p in _pipes)
|
||||
p.ClearBC();
|
||||
|
||||
return SoundProcessor.MixAndClip(audioSamples.ToArray());
|
||||
}
|
||||
|
||||
bool IsVolumePort(Port p) => _volumes.Exists(v => v.Port == p);
|
||||
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);
|
||||
private bool IsVolumePort(Port p) => _volumes.Exists(v => v.Port == p);
|
||||
private bool IsPipePort(Port p) => _pipes.Exists(pp => pp.PortA == p || pp.PortB == p);
|
||||
private Pipe1D GetPipe(Port p) => _pipes.Find(pp => pp.PortA == p || pp.PortB == p);
|
||||
private Volume0D GetVolume(Port p) => _volumes.Find(v => v.Port == p);
|
||||
|
||||
void SetVolumeBC(Port pipePort, Port volPort)
|
||||
private void SetVolumeBC(Port pipePort, Port volPort)
|
||||
{
|
||||
Pipe1D pipe = GetPipe(pipePort);
|
||||
var pipe = GetPipe(pipePort);
|
||||
if (pipe == null) return;
|
||||
bool isLeft = pipe.PortA == pipePort;
|
||||
|
||||
if (isLeft)
|
||||
pipe.SetLeftVolumeState(volPort.Density, volPort.Pressure);
|
||||
else
|
||||
pipe.SetRightVolumeState(volPort.Density, volPort.Pressure);
|
||||
}
|
||||
|
||||
void TransferPipeToVolume(Port pipePort, Port volPort)
|
||||
private void TransferAndIntegrate(Port pipePort, Port volPort, double dtSub)
|
||||
{
|
||||
double mdot = pipePort.MassFlowRate;
|
||||
volPort.MassFlowRate = -mdot;
|
||||
|
||||
if (mdot < 0) // pipe → volume
|
||||
{
|
||||
// pipePort.SpecificEnthalpy is already total (h + ½u²)
|
||||
volPort.SpecificEnthalpy = pipePort.SpecificEnthalpy;
|
||||
}
|
||||
// else: volume → pipe, volume’s own static enthalpy is used (already set)
|
||||
// else: volume’s own enthalpy (set by PushStateToPort) is used
|
||||
|
||||
GetVolume(volPort)?.Integrate(dtSub);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Core/SoundProcessor.cs
Normal file
23
Core/SoundProcessor.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace FluidSim.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Mixes multiple audio samples and applies a soft‑clipping tanh.
|
||||
/// </summary>
|
||||
public static class SoundProcessor
|
||||
{
|
||||
/// <summary>Overall gain applied after mixing (before tanh).</summary>
|
||||
public static float MasterGain { get; set; } = 0.01f;
|
||||
|
||||
/// <summary>
|
||||
/// Mixes an array of raw audio samples and returns a single sample in [‑1, 1].
|
||||
/// </summary>
|
||||
public static float MixAndClip(params float[] samples)
|
||||
{
|
||||
float sum = 0f;
|
||||
foreach (float s in samples)
|
||||
sum += s;
|
||||
sum *= MasterGain;
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user