orifice confirmed working
This commit is contained in:
@@ -3,60 +3,74 @@ using SFML.Graphics;
|
||||
using SFML.System;
|
||||
using FluidSim.Components;
|
||||
using FluidSim.Core;
|
||||
using FluidSim.Utils;
|
||||
|
||||
namespace FluidSim.Tests
|
||||
{
|
||||
public class TestScenario : Scenario
|
||||
{
|
||||
private Solver solver;
|
||||
private Volume0D volume;
|
||||
private Pipe1D pipe;
|
||||
private OrificeLink closedEnd; // left end – closed wall
|
||||
private OpenEndLink openEndLink; // right end – atmosphere
|
||||
private OrificeLink orifice; // volume → pipe left
|
||||
private OpenEndLink openEnd; // pipe right → 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;
|
||||
|
||||
// Pressure reset scheduling
|
||||
private double simTime;
|
||||
private double dt;
|
||||
private double resetInterval = 0.2; // seconds between resets
|
||||
private double nextResetTime;
|
||||
private double targetPressure = 10 * Units.atm;
|
||||
private double rampDuration = 0.002; // 2 ms ramp
|
||||
private double rampStartTime;
|
||||
private double rampStartPressure; // pressure when ramp started
|
||||
private bool ramping;
|
||||
|
||||
public override void Initialize(int sampleRate)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
|
||||
soundProcessor = new SoundProcessor(sampleRate, 1);
|
||||
soundProcessor.Gain = 10;
|
||||
|
||||
reverb = new OutdoorExhaustReverb(sampleRate);
|
||||
soundProcessor.Gain = 2.0f; // lower gain to avoid clipping
|
||||
|
||||
solver = new Solver();
|
||||
solver.SetTimeStep(dt);
|
||||
solver.CflTarget = 0.4;
|
||||
|
||||
// Pipe: 2 m long, 1 cm² area, 200 cells
|
||||
pipe = new Pipe1D(length: 2, area: 1e-4, cellCount: 100);
|
||||
volume = new Volume0D(1e-3, targetPressure, 300.0);
|
||||
solver.AddComponent(volume);
|
||||
|
||||
pipe = new Pipe1D(1.0, 1e-4, 200);
|
||||
pipe.EnergyRelaxationRate = 10;
|
||||
solver.AddComponent(pipe);
|
||||
|
||||
// Initially pipe at ambient conditions
|
||||
pipe.SetUniformState(1.225, 0.0, 101325.0);
|
||||
var volPort = volume.CreatePort();
|
||||
double orificeArea = 1e-5;
|
||||
orifice = new OrificeLink(volPort, pipe, isPipeLeftEnd: true,
|
||||
areaProvider: () => orificeArea)
|
||||
{
|
||||
DischargeCoefficient = 0.62,
|
||||
UseInertance = true,
|
||||
EffectiveLength = 0.001
|
||||
};
|
||||
solver.AddOrificeLink(orifice);
|
||||
|
||||
// Left end: closed wall (area = 0 → reflective)
|
||||
closedEnd = new OrificeLink(null, pipe, isPipeLeftEnd: true, areaProvider: () => 0.0);
|
||||
solver.AddOrificeLink(closedEnd);
|
||||
|
||||
// Right end: open to atmosphere
|
||||
openEndLink = new OpenEndLink(pipe, isLeftEnd: false)
|
||||
openEnd = new OpenEndLink(pipe, isLeftEnd: false)
|
||||
{
|
||||
AmbientPressure = 101325.0,
|
||||
Gamma = 1.4
|
||||
};
|
||||
solver.AddOpenEndLink(openEndLink);
|
||||
solver.AddOpenEndLink(openEnd);
|
||||
|
||||
stepCount = 0;
|
||||
simTime = 0.0;
|
||||
nextPulseTime = pulseInterval; // first pulse at 100 ms
|
||||
nextResetTime = resetInterval;
|
||||
ramping = false;
|
||||
|
||||
Console.WriteLine("Pulse reflection test – closed left, open right");
|
||||
Console.WriteLine("Pulse injected every 100 ms at left end (cell 0)");
|
||||
Console.WriteLine("Pressure reset test with smooth ramp");
|
||||
Console.WriteLine($"Volume 1L, reset to {targetPressure} Pa every {resetInterval*1000} ms, ramp {rampDuration*1000} ms");
|
||||
}
|
||||
|
||||
public override float Process()
|
||||
@@ -65,59 +79,54 @@ namespace FluidSim.Tests
|
||||
stepCount++;
|
||||
simTime += dt;
|
||||
|
||||
// ---- Inject a pressure pulse at the closed end every 100 ms ----
|
||||
if (simTime >= nextPulseTime)
|
||||
// ---- Smooth pressure ramp ----
|
||||
if (ramping)
|
||||
{
|
||||
// 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;
|
||||
|
||||
// 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 elapsed = simTime - rampStartTime;
|
||||
if (elapsed >= rampDuration)
|
||||
{
|
||||
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);
|
||||
// Ramp finished, set exactly to target
|
||||
volume.SetPressure(targetPressure);
|
||||
ramping = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
double frac = elapsed / rampDuration;
|
||||
double currentTarget = rampStartPressure + (targetPressure - rampStartPressure) * frac;
|
||||
volume.SetPressure(currentTarget);
|
||||
}
|
||||
Console.WriteLine($"Pulse injected at t = {simTime:F3} s");
|
||||
nextPulseTime += pulseInterval;
|
||||
}
|
||||
|
||||
// Audio from open‑end mass flow
|
||||
float sample = soundProcessor.Process(openEndLink);
|
||||
|
||||
// Log every 200 steps
|
||||
if (stepCount % 1000 == 0)
|
||||
// ---- Trigger a new reset ----
|
||||
if (!ramping && simTime >= nextResetTime)
|
||||
{
|
||||
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);
|
||||
rampStartPressure = volume.Pressure; // current pressure before reset
|
||||
rampStartTime = simTime;
|
||||
ramping = true;
|
||||
nextResetTime += resetInterval;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Step {stepCount}: P_left={pL:F1} Pa, P_mid={pM:F1} Pa, P_right={pR:F1} Pa");
|
||||
// Log every 500 steps
|
||||
if (stepCount % 500 == 0)
|
||||
{
|
||||
double volP = volume.Pressure;
|
||||
double pipeL = pipe.GetCellPressure(0);
|
||||
double pipeR = pipe.GetCellPressure(pipe.CellCount - 1);
|
||||
double mdotOrif = orifice.LastMassFlowRate;
|
||||
double mdotOpen = openEnd.LastMassFlowRate;
|
||||
|
||||
Console.WriteLine($"Step {stepCount}: " +
|
||||
$"VolP={volP:F1} Pa, PipeL={pipeL:F1}, PipeR={pipeR:F1}, " +
|
||||
$"mdot_orif={mdotOrif:E4} kg/s, mdot_open={mdotOpen:E4} kg/s");
|
||||
}
|
||||
|
||||
if (double.IsNaN(pipe.GetCellPressure(0)))
|
||||
{
|
||||
Console.WriteLine("NaN detected – stopping simulation.");
|
||||
Console.WriteLine("NaN detected – stopping.");
|
||||
return 0f;
|
||||
}
|
||||
|
||||
return reverb.Process(sample);
|
||||
return soundProcessor.Process(openEnd);
|
||||
}
|
||||
|
||||
public override void Draw(RenderWindow target)
|
||||
|
||||
Reference in New Issue
Block a user