Engine working
This commit is contained in:
@@ -3,6 +3,7 @@ using SFML.Graphics;
|
||||
using SFML.System;
|
||||
using FluidSim.Components;
|
||||
using FluidSim.Core;
|
||||
using FluidSim.Utils;
|
||||
|
||||
namespace FluidSim.Tests
|
||||
{
|
||||
@@ -10,67 +11,81 @@ namespace FluidSim.Tests
|
||||
{
|
||||
// Engine
|
||||
private Cylinder cylinder;
|
||||
private Crankshaft crankshaft;
|
||||
|
||||
// Intake side
|
||||
private Pipe1D intakePipeBeforeThrottle; // pipe from ambient to plenum
|
||||
private Volume0D intakePlenum; // plenum (100 mL)
|
||||
private Pipe1D intakeRunner; // pipe from plenum to cylinder
|
||||
private Pipe1D intakePipeBeforeThrottle;
|
||||
private Volume0D intakePlenum; // 5 mL
|
||||
private Pipe1D intakeRunner;
|
||||
|
||||
// Exhaust side
|
||||
private Pipe1D exhaustPipe;
|
||||
|
||||
// Links
|
||||
private OpenEndLink intakeOpenEnd; // ambient → left end of first pipe
|
||||
private OrificeLink throttleOrifice; // first pipe right end → plenum inlet (variable area)
|
||||
private OrificeLink plenumToRunner; // plenum outlet → runner left end (fixed area)
|
||||
private OrificeLink intakeValve; // runner right end → cylinder intake port
|
||||
|
||||
private OpenEndLink intakeOpenEnd;
|
||||
private OrificeLink throttleOrifice;
|
||||
private OrificeLink plenumToRunner;
|
||||
private OrificeLink intakeValve;
|
||||
private OrificeLink exhaustValve;
|
||||
private OpenEndLink exhaustOpenEnd;
|
||||
|
||||
private Solver solver;
|
||||
private SoundProcessor soundProcessor;
|
||||
private SoundProcessor exhaustSoundProcessor;
|
||||
private SoundProcessor intakeSoundProcessor;
|
||||
private OutdoorExhaustReverb reverb;
|
||||
private double dt;
|
||||
private int stepCount;
|
||||
|
||||
public double ThrottleArea { get; set; } = 0.0; // controlled externally
|
||||
// ---------- Throttle control ----------
|
||||
public double Throttle { get; set; } = 0.0;
|
||||
public double MaxThrottleArea { get; set; } = 6 * Units.cm2; // 2 cm²
|
||||
|
||||
public override void Initialize(int sampleRate)
|
||||
{
|
||||
dt = 1.0 / sampleRate;
|
||||
soundProcessor = new SoundProcessor(sampleRate, 1) { Gain = 1f };
|
||||
|
||||
solver = new Solver();
|
||||
solver.SetTimeStep(dt);
|
||||
solver.CflTarget = 0.9;
|
||||
|
||||
// ---- Cylinder (no valve overlap to avoid backflow) ----
|
||||
double bore = 0.056, stroke = 0.050, conRod = 0.110, compRatio = 10.0;
|
||||
// ---- Crankshaft (external, passed to cylinder) ----
|
||||
crankshaft = new Crankshaft(1000);
|
||||
crankshaft.Inertia = 0.05;
|
||||
crankshaft.FrictionConstant = 2;
|
||||
crankshaft.FrictionViscous = 0.05;
|
||||
|
||||
// ---- Cylinder ----
|
||||
double bore = 0.056, stroke = 0.057, conRod = 0.110, compRatio = 9.2;
|
||||
double ivo = 370.0, ivc = 580.0, evo = 120.0, evc = 350.0;
|
||||
cylinder = new Cylinder(bore, stroke, conRod, compRatio, ivo, ivc, evo, evc, 1000)
|
||||
cylinder = new Cylinder(bore, stroke, conRod, compRatio, ivo, ivc, evo, evc, crankshaft)
|
||||
{
|
||||
MaxIntakeArea = 0.00037,
|
||||
MaxExhaustArea = 0.00037,
|
||||
MaxIntakeArea = 3.7 * Units.cm2,
|
||||
MaxExhaustArea = 3.7 * Units.cm2,
|
||||
};
|
||||
solver.AddComponent(cylinder);
|
||||
|
||||
double pipeArea = 0.00037; // 3.7 cm²
|
||||
double pipeDiameter = 2 * Units.cm;
|
||||
double pipeArea = Units.AreaFromDiameter(pipeDiameter);
|
||||
|
||||
exhaustSoundProcessor = new SoundProcessor(sampleRate, 1, pipeDiameter) { Gain = 0.05f };
|
||||
intakeSoundProcessor = new SoundProcessor(sampleRate, 1, pipeDiameter) { Gain = 0.05f };
|
||||
reverb = new OutdoorExhaustReverb(sampleRate);
|
||||
|
||||
// ---- Pipes ----
|
||||
intakePipeBeforeThrottle = new Pipe1D(0.15, pipeArea, 5); // short pipe before throttle
|
||||
intakeRunner = new Pipe1D(0.10, pipeArea, 5); // runner after plenum
|
||||
intakePipeBeforeThrottle = new Pipe1D(0.15, pipeArea, 5);
|
||||
intakeRunner = new Pipe1D(0.1, pipeArea, 5);
|
||||
exhaustPipe = new Pipe1D(1.00, pipeArea, 80);
|
||||
solver.AddComponent(intakePipeBeforeThrottle);
|
||||
solver.AddComponent(intakeRunner);
|
||||
solver.AddComponent(exhaustPipe);
|
||||
|
||||
// ---- Plenum (100 mL) ----
|
||||
intakePlenum = new Volume0D(0.0001, 101325.0, 300.0); // 0.0001 m³
|
||||
var plenumInlet = intakePlenum.CreatePort(); // from throttle
|
||||
var plenumOutlet = intakePlenum.CreatePort(); // to runner
|
||||
// ---- Plenum (5 mL) ----
|
||||
intakePlenum = new Volume0D(5 * Units.mL, 101325.0, 300.0);
|
||||
var plenumInlet = intakePlenum.CreatePort();
|
||||
var plenumOutlet = intakePlenum.CreatePort();
|
||||
solver.AddComponent(intakePlenum);
|
||||
|
||||
// ---- Intake open end (ambient → left end of first pipe) ----
|
||||
// ---- Intake open end ----
|
||||
intakeOpenEnd = new OpenEndLink(intakePipeBeforeThrottle, isLeftEnd: true)
|
||||
{
|
||||
AmbientPressure = 101325.0,
|
||||
@@ -78,16 +93,16 @@ namespace FluidSim.Tests
|
||||
};
|
||||
solver.AddOpenEndLink(intakeOpenEnd);
|
||||
|
||||
// ---- Throttle orifice (first pipe right end → plenum inlet) ----
|
||||
// ---- Throttle orifice (variable area) ----
|
||||
throttleOrifice = new OrificeLink(plenumInlet, intakePipeBeforeThrottle, isPipeLeftEnd: false,
|
||||
areaProvider: () => ThrottleArea)
|
||||
areaProvider: () => MaxThrottleArea * Math.Clamp(Throttle, 0.001, 1))
|
||||
{
|
||||
DischargeCoefficient = 0.1, // realistic throttle Cd
|
||||
DischargeCoefficient = 0.1,
|
||||
UseInertance = false
|
||||
};
|
||||
solver.AddOrificeLink(throttleOrifice);
|
||||
|
||||
// ---- Plenum → runner (fixed area = pipe area) ----
|
||||
// ---- Plenum to runner (fixed area) ----
|
||||
plenumToRunner = new OrificeLink(plenumOutlet, intakeRunner, isPipeLeftEnd: true,
|
||||
areaProvider: () => pipeArea)
|
||||
{
|
||||
@@ -96,7 +111,7 @@ namespace FluidSim.Tests
|
||||
};
|
||||
solver.AddOrificeLink(plenumToRunner);
|
||||
|
||||
// ---- Intake valve (runner right end → cylinder intake port) ----
|
||||
// ---- Intake valve ----
|
||||
intakeValve = new OrificeLink(cylinder.IntakePort, intakeRunner, isPipeLeftEnd: false,
|
||||
areaProvider: () => cylinder.IntakeValveArea)
|
||||
{
|
||||
@@ -130,16 +145,12 @@ namespace FluidSim.Tests
|
||||
|
||||
public override float Process()
|
||||
{
|
||||
// 1. Advance crankshaft & pre‑step
|
||||
cylinder.Crankshaft.Step(dt);
|
||||
cylinder.PreStep(dt);
|
||||
|
||||
// 2. Run solver
|
||||
solver.Step();
|
||||
stepCount++;
|
||||
|
||||
// 3. Log every 200 steps
|
||||
if (stepCount % 200 == 0)
|
||||
if (stepCount % 10000 == 0)
|
||||
{
|
||||
double crankDeg = cylinder.Crankshaft.CrankAngle * 180.0 / Math.PI % 720.0;
|
||||
double cylP = cylinder.Pressure / 1e5;
|
||||
@@ -149,14 +160,17 @@ namespace FluidSim.Tests
|
||||
double mdotE = exhaustValve.LastMassFlowRate;
|
||||
double pipeR = exhaustPipe.GetCellPressure(exhaustPipe.CellCount - 1) / 1e5;
|
||||
double plenumP = intakePlenum.Pressure / 1e5;
|
||||
double actualArea = MaxThrottleArea * Throttle;
|
||||
|
||||
Console.WriteLine($"Step {stepCount}: Angle={crankDeg:F1}°, " +
|
||||
$"CylP={cylP:F2} bar, T={cylT:F0} K, mass={cylMass:F1} mg, " +
|
||||
$"mdotI={mdotI:E4} kg/s, mdotE={mdotE:E4} kg/s, PipeR={pipeR:F2} bar");
|
||||
Console.WriteLine($"Throttle area = {ThrottleArea * 1e6:F2} mm², Plenum P = {plenumP:F3} bar");
|
||||
Console.WriteLine($"Throttle = {Throttle * 100:F0}% area = {actualArea * 1e6:F2} mm², Plenum P = {plenumP:F3} bar");
|
||||
}
|
||||
|
||||
return soundProcessor.Process(exhaustOpenEnd);
|
||||
float exhaustDry = exhaustSoundProcessor.Process(exhaustOpenEnd);
|
||||
float intakeDry = intakeSoundProcessor.Process(intakeOpenEnd);
|
||||
return reverb.Process(intakeDry + exhaustDry);
|
||||
}
|
||||
|
||||
public override void Draw(RenderWindow target)
|
||||
@@ -164,22 +178,21 @@ namespace FluidSim.Tests
|
||||
float winW = target.GetView().Size.X;
|
||||
float winH = target.GetView().Size.Y;
|
||||
|
||||
// Fixed vertical centres for intake and exhaust
|
||||
float intakeY = winH / 2f - 40f;
|
||||
float exhaustY = winH / 2f + 80f;
|
||||
|
||||
// ---- 1. Open end (ambient air source) ----
|
||||
// Open end marker
|
||||
float openEndX = 40f;
|
||||
var openEndMark = new CircleShape(5f) { FillColor = Color.Cyan };
|
||||
openEndMark.Position = new Vector2f(openEndX - 5f, intakeY - 5f);
|
||||
target.Draw(openEndMark);
|
||||
|
||||
// ---- 2. First intake pipe (ambient → throttle) ----
|
||||
// First intake pipe
|
||||
float pipe1StartX = openEndX;
|
||||
float pipe1EndX = pipe1StartX + 120f;
|
||||
DrawPipe(target, intakePipeBeforeThrottle, intakeY, pipe1StartX, pipe1EndX);
|
||||
|
||||
// ---- 3. Throttle (symbolic restriction) ----
|
||||
// Throttle symbol
|
||||
float throttleX = pipe1EndX + 5f;
|
||||
var throttleRect = new RectangleShape(new Vector2f(8f, 30f))
|
||||
{
|
||||
@@ -188,25 +201,25 @@ namespace FluidSim.Tests
|
||||
};
|
||||
target.Draw(throttleRect);
|
||||
|
||||
// ---- 4. Plenum ----
|
||||
// Plenum
|
||||
float plenW = 60f, plenH = 80f;
|
||||
float plenLeftX = throttleX + 10f;
|
||||
float plenCenterX = plenLeftX + plenW / 2f;
|
||||
float plenTopY = intakeY - plenH / 2f;
|
||||
DrawVolume(target, intakePlenum, plenCenterX, plenTopY, plenW, plenH);
|
||||
|
||||
// ---- 5. Runner pipe (plenum → cylinder) ----
|
||||
// Runner pipe
|
||||
float runnerStartX = plenLeftX + plenW + 5f;
|
||||
float runnerEndX = runnerStartX + 100f;
|
||||
DrawPipe(target, intakeRunner, intakeY, runnerStartX, runnerEndX);
|
||||
|
||||
// ---- 6. Cylinder ----
|
||||
float cylCX = runnerEndX + 50f; // center X
|
||||
float cylTopY = intakeY - 120f; // top of cylinder (so it sits above the pipe)
|
||||
// Cylinder
|
||||
float cylCX = runnerEndX + 50f;
|
||||
float cylTopY = intakeY - 120f;
|
||||
float cylW = 80f, cylMaxH = 240f;
|
||||
DrawCylinder(target, cylinder, cylCX, cylTopY, cylW, cylMaxH);
|
||||
|
||||
// ---- 7. Exhaust pipe (cylinder → open end) ----
|
||||
// Exhaust pipe
|
||||
float exhStartX = cylCX + cylW / 2f + 20f;
|
||||
float exhEndX = winW - 60f;
|
||||
DrawPipe(target, exhaustPipe, exhaustY, exhStartX, exhEndX);
|
||||
|
||||
Reference in New Issue
Block a user