Open end working

This commit is contained in:
2026-05-07 12:55:57 +02:00
parent bc0df51ddb
commit 685b48b577
7 changed files with 355 additions and 330 deletions

View File

@@ -9,40 +9,41 @@ namespace FluidSim.Tests
public class TestScenario : Scenario
{
private Solver solver;
private Volume0D volume;
private Pipe1D pipe;
private Atmosphere atmosphere;
private OrificeLink orificeLink;
private OpenEndLink openEndLink;
private OrificeLink closedEnd; // left end closed wall
private OpenEndLink openEndLink; // right end atmosphere
private SoundProcessor soundProcessor;
private OutdoorExhaustReverb reverb;
private int stepCount;
private double simTime; // elapsed simulation time (seconds)
private double pulseInterval = 0.1; // seconds between pulses
private double nextPulseTime;
private double dt;
public override void Initialize(int sampleRate)
{
double dt = 1.0 / sampleRate;
dt = 1.0 / sampleRate;
soundProcessor = new SoundProcessor(sampleRate, 1);
soundProcessor.Gain = 10;
reverb = new OutdoorExhaustReverb(sampleRate);
solver = new Solver();
solver.SetTimeStep(dt);
volume = new Volume0D(1e-3, 150000.0, 300.0);
solver.AddComponent(volume);
pipe = new Pipe1D(2.0, 1e-4, 20);
// Pipe: 2 m long, 1 cm² area, 200 cells
pipe = new Pipe1D(length: 2, area: 1e-4, cellCount: 100);
solver.AddComponent(pipe);
atmosphere = new Atmosphere();
solver.AddComponent(atmosphere);
// Initially pipe at ambient conditions
pipe.SetUniformState(1.225, 0.0, 101325.0);
// Volume → left pipe end (orifice)
var volPort = volume.CreatePort();
orificeLink = new OrificeLink(volPort, pipe, isPipeLeftEnd: true, areaProvider: () => 1e-5)
{
DischargeCoefficient = 0.62,
Gamma = volume.Gamma,
GasConstant = volume.GasConstant
};
solver.AddOrificeLink(orificeLink);
// Left end: closed wall (area = 0 → reflective)
closedEnd = new OrificeLink(null, pipe, isPipeLeftEnd: true, areaProvider: () => 0.0);
solver.AddOrificeLink(closedEnd);
// Right pipe end → atmosphere (characteristic openend)
// Right end: open to atmosphere
openEndLink = new OpenEndLink(pipe, isLeftEnd: false)
{
AmbientPressure = 101325.0,
@@ -51,45 +52,82 @@ namespace FluidSim.Tests
solver.AddOpenEndLink(openEndLink);
stepCount = 0;
Console.WriteLine("TestScenario initialized with sampleRate = " + sampleRate);
simTime = 0.0;
nextPulseTime = pulseInterval; // first pulse at 100 ms
Console.WriteLine("Pulse reflection test closed left, open right");
Console.WriteLine("Pulse injected every 100 ms at left end (cell 0)");
}
public override float Process()
{
solver.Step();
stepCount++;
simTime += dt;
if (stepCount % 100 == 0)
// ---- Inject a pressure pulse at the closed end every 100 ms ----
if (simTime >= nextPulseTime)
{
double volPressure = volume.Pressure;
double volMass = volume.Mass;
double pipeLeftPressure = pipe.GetCellPressure(0);
double pipeRightPressure = pipe.GetCellPressure(pipe.CellCount - 1);
double mdotOrifice = orificeLink.LastMassFlowRate;
double mdotOpen = openEndLink.LastMassFlowRate;
// Apply a Gaussian pulse to the first few cells
double ambientPressure = 101325.0;
double pulseAmplitude = 20 * ambientPressure; // 0.5 atm overpressure
double pulseWidth = 0.05; // m (spread over a few cells)
int n = pipe.CellCount;
double dx = 2.0 / n;
Console.WriteLine($"Step {stepCount}:");
Console.WriteLine($" Vol Pressure = {volPressure:F1} Pa, Mass = {volMass:E4} kg");
Console.WriteLine($" Pipe left P = {pipeLeftPressure:F1} Pa, right P = {pipeRightPressure:F1} Pa");
Console.WriteLine($" Orifice mdot = {mdotOrifice:E4} kg/s, Openend mdot = {mdotOpen:E4} kg/s");
Console.WriteLine();
// Only modify cells within 2*pulseWidth from the left end
int maxCell = Math.Min(5, n - 1); // at most the first 5 cells
for (int i = 0; i <= maxCell; i++)
{
double x = (i + 0.5) * dx;
double P = pulseAmplitude * Math.Exp(-x * x / (pulseWidth * pulseWidth));
double currentP = pipe.GetCellPressure(i);
double newP = P;
// Update pressure, keeping density and velocity unchanged
// We recompute total energy accordingly
double rho = pipe.GetCellDensity(i);
double u = pipe.GetCellVelocity(i);
double e = newP / ((1.4 - 1.0) * rho);
double E = rho * e + 0.5 * rho * u * u;
pipe.SetCellState(i, rho, u, newP);
}
Console.WriteLine($"Pulse injected at t = {simTime:F3} s");
nextPulseTime += pulseInterval;
}
// Audio sample from the openend mass flow
return (float)openEndLink.LastMassFlowRate;
// Audio from openend mass flow
float sample = soundProcessor.Process(openEndLink);
// Log every 200 steps
if (stepCount % 1000 == 0)
{
int leftIdx = 0;
int midIdx = pipe.CellCount / 2;
int rightIdx = pipe.CellCount - 1;
double pL = pipe.GetCellPressure(leftIdx);
double pM = pipe.GetCellPressure(midIdx);
double pR = pipe.GetCellPressure(rightIdx);
Console.WriteLine($"Step {stepCount}: P_left={pL:F1} Pa, P_mid={pM:F1} Pa, P_right={pR:F1} Pa");
}
if (double.IsNaN(pipe.GetCellPressure(0)))
{
Console.WriteLine("NaN detected stopping simulation.");
return 0f;
}
return reverb.Process(sample);
}
public override void Draw(RenderWindow target)
{
float winWidth = target.GetView().Size.X;
float winHeight = target.GetView().Size.Y;
float pipeCenterY = winHeight / 2f;
float margin = 60f;
float pipeStartX = margin;
float pipeEndX = winWidth - margin;
// Use the shared pipe drawing from the base class
DrawPipe(target, pipe, pipeCenterY, pipeStartX, pipeEndX);
}
}