This commit is contained in:
2026-05-05 19:39:11 +02:00
parent 608dabff12
commit d6b1d214f5
11 changed files with 493 additions and 277 deletions

View File

@@ -7,7 +7,7 @@ namespace FluidSim.Components
{
public double AngularVelocity { get; set; } // rad/s
public double CrankAngle { get; set; } // rad, 0 … 4π (fourstroke cycle)
public double PreviousAngle { get; private set; } // for TDC detection
public double PreviousAngle { get; set; } // ← now has public setter
public double Inertia { get; set; } = 0.2;
public double FrictionConstant { get; set; } = 2.0; // N·m
@@ -15,7 +15,6 @@ namespace FluidSim.Components
private double externalTorque;
/// <param name="initialRPM">Idle speed before any combustion torque is applied.</param>
public Crankshaft(double initialRPM = 400.0)
{
AngularVelocity = initialRPM * 2.0 * Math.PI / 60.0;
@@ -27,20 +26,23 @@ namespace FluidSim.Components
public void Step(double dt)
{
// Save previous angle
// Catch NaN before it propagates
if (double.IsNaN(AngularVelocity) || double.IsInfinity(AngularVelocity))
AngularVelocity = 0.0;
if (double.IsNaN(externalTorque) || double.IsInfinity(externalTorque))
externalTorque = 0.0;
PreviousAngle = CrankAngle;
// Friction
double friction = FrictionConstant * Math.Sign(AngularVelocity) + FrictionViscous * AngularVelocity;
double netTorque = externalTorque - friction;
double alpha = netTorque / Inertia;
AngularVelocity += alpha * dt;
if (AngularVelocity < 0) AngularVelocity = 0; // stall
if (AngularVelocity < 0) AngularVelocity = 0;
CrankAngle += AngularVelocity * dt;
// Wrap to [0, 4π)
if (CrankAngle >= 4.0 * Math.PI)
CrankAngle -= 4.0 * Math.PI;
else if (CrankAngle < 0)