Attempting to generate sound, and set cells automatically

This commit is contained in:
max
2026-05-02 23:26:52 +02:00
parent 2c338ad7d9
commit a262410616
16 changed files with 442 additions and 285 deletions

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using FluidSim.Audio;
using FluidSim.Components;
using FluidSim.Interfaces;
@@ -10,17 +10,51 @@ namespace FluidSim.Core
private readonly List<Pipe1D> _pipes = new();
private readonly List<Connection> _connections = new();
public float LastSample { get; private set; }
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()
{
// 1. Volumes publish state to their ports
// 1. Publish volume states to their own ports
foreach (var v in _volumes)
v.PushStateToPort();
// 2. Set volume states as boundary conditions on pipes
// 2. Handle direct volumetovolume connections
foreach (var conn in _connections)
{
if (IsVolumePort(conn.PortA) && IsVolumePort(conn.PortB))
{
Volume0D volA = _volumes.Find(v => v.Port == conn.PortA);
Volume0D volB = _volumes.Find(v => v.Port == conn.PortB);
if (volA == null || volB == null) continue;
double pA = volA.Pressure, rhoA = volA.Density;
double pB = volB.Pressure, rhoB = volB.Density;
double mdot = OrificeBoundary.MassFlow(pA, rhoA, pB, rhoB, conn);
if (mdot > 0) // A → B
{
volA.Port.MassFlowRate = -mdot;
volB.Port.MassFlowRate = mdot;
volB.Port.SpecificEnthalpy = volA.SpecificEnthalpy; // fluid from A
volA.Port.SpecificEnthalpy = volA.SpecificEnthalpy; // outflow carries its own enthalpy
}
else // B → A (mdot negative)
{
volA.Port.MassFlowRate = -mdot; // positive
volB.Port.MassFlowRate = mdot; // negative
volA.Port.SpecificEnthalpy = volB.SpecificEnthalpy; // fluid from B
volB.Port.SpecificEnthalpy = volB.SpecificEnthalpy; // outflow carries its own
}
}
}
// 3. Pipevolume boundary conditions unchanged
foreach (var conn in _connections)
{
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
@@ -29,11 +63,11 @@ namespace FluidSim.Core
SetVolumeBC(conn.PortB, conn.PortA);
}
// 3. Run pipe simulations
// 4. Run pipe simulations
foreach (var p in _pipes)
p.Simulate();
// 4. Transfer pipeport flows to volume ports
// 5. Transfer pipetovolume flows unchanged
foreach (var conn in _connections)
{
if (IsPipePort(conn.PortA) && IsVolumePort(conn.PortB))
@@ -42,9 +76,21 @@ namespace FluidSim.Core
TransferPipeToVolume(conn.PortB, conn.PortA);
}
// 5. Integrate volumes
// 6. Integrate volumes
foreach (var v in _volumes)
v.Integrate();
// 7. COMPUTE AUDIO SAMPLE from all sound connections
double sample = 0f;
foreach (var conn in _connections)
{
if (conn is SoundConnection)
{
// Both ports have the same absolute mass flow; either works.
sample += SoundProcessor.ComputeSample(conn);
}
}
LastSample = (float)Math.Tanh(sample);
}
bool IsVolumePort(Port p) => _volumes.Exists(v => v.Port == p);