Helmholtz testing (no decay bug)
This commit is contained in:
@@ -1,54 +1,52 @@
|
||||
// Components/Crankshaft.cs
|
||||
using System;
|
||||
|
||||
namespace FluidSim.Components
|
||||
{
|
||||
public class Crankshaft
|
||||
{
|
||||
public double AngularVelocity { get; set; } // rad/s
|
||||
public double CrankAngle { get; set; } // rad, 0 … 4π (four‑stroke cycle)
|
||||
public double PreviousAngle { get; set; } // ← now has public setter
|
||||
public float AngularVelocity; // rad/s
|
||||
public float CrankAngle; // rad, 0 … 4π
|
||||
public float PreviousAngle;
|
||||
|
||||
public double Inertia { get; set; } = 0.2;
|
||||
public double FrictionConstant { get; set; } = 0.0; // N·m
|
||||
public double FrictionViscous { get; set; } = 0.000; // N·m per rad/s
|
||||
public float Inertia = 0.2f;
|
||||
public float FrictionConstant; // N·m
|
||||
public float FrictionViscous; // N·m per rad/s
|
||||
|
||||
private double externalTorque;
|
||||
private float externalTorque;
|
||||
|
||||
public Crankshaft(double initialRPM = 400.0)
|
||||
public Crankshaft(float initialRPM = 400f)
|
||||
{
|
||||
AngularVelocity = initialRPM * 2.0 * Math.PI / 60.0;
|
||||
CrankAngle = 0.0;
|
||||
PreviousAngle = 0.0;
|
||||
AngularVelocity = initialRPM * 2f * MathF.PI / 60f;
|
||||
CrankAngle = 0f;
|
||||
PreviousAngle = 0f;
|
||||
}
|
||||
|
||||
public void AddTorque(double torque) => externalTorque += torque;
|
||||
public void AddTorque(float torque) => externalTorque += torque;
|
||||
|
||||
public void Step(double dt)
|
||||
public void Step(float dt)
|
||||
{
|
||||
// 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;
|
||||
if (float.IsNaN(AngularVelocity) || float.IsInfinity(AngularVelocity))
|
||||
AngularVelocity = 0f;
|
||||
if (float.IsNaN(externalTorque) || float.IsInfinity(externalTorque))
|
||||
externalTorque = 0f;
|
||||
|
||||
PreviousAngle = CrankAngle;
|
||||
|
||||
double friction = FrictionConstant * Math.Sign(AngularVelocity) + FrictionViscous * AngularVelocity;
|
||||
double netTorque = externalTorque - friction;
|
||||
double alpha = netTorque / Inertia;
|
||||
float friction = FrictionConstant * MathF.Sign(AngularVelocity)
|
||||
+ FrictionViscous * AngularVelocity;
|
||||
float netTorque = externalTorque - friction;
|
||||
float alpha = netTorque / Inertia;
|
||||
AngularVelocity += alpha * dt;
|
||||
|
||||
if (AngularVelocity < 0) AngularVelocity = 0;
|
||||
if (AngularVelocity < 0f) AngularVelocity = 0f;
|
||||
|
||||
CrankAngle += AngularVelocity * dt;
|
||||
if (CrankAngle >= 4f * MathF.PI)
|
||||
CrankAngle -= 4f * MathF.PI;
|
||||
else if (CrankAngle < 0f)
|
||||
CrankAngle += 4f * MathF.PI;
|
||||
|
||||
if (CrankAngle >= 4.0 * Math.PI)
|
||||
CrankAngle -= 4.0 * Math.PI;
|
||||
else if (CrankAngle < 0)
|
||||
CrankAngle += 4.0 * Math.PI;
|
||||
|
||||
externalTorque = 0.0;
|
||||
externalTorque = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user