seemingly working, added display text

This commit is contained in:
2026-05-07 16:37:12 +02:00
parent f79cf6b7eb
commit 14f5ba925f
10 changed files with 288 additions and 161 deletions

View File

@@ -19,34 +19,37 @@ namespace FluidSim.Tests
// ---------- Shared drawing helpers ----------
protected const double AmbientPressure = 101325.0;
protected const double AmbientTemperature = 300.0; // K
/// <summary>Blue (low) → Green (ambient) → Red (high).</summary>
protected Color PressureColor(double pressure)
/// <summary>Map temperature [0K … 2000K] to a color: blue (0K) → green (300K) → red (2000K).</summary>
protected Color TemperatureColor(double temperature)
{
double range = AmbientPressure * 0.05; // ±5% gives full colour swing
double t = (pressure - AmbientPressure) / range;
t = Math.Clamp(t, -1.0, 1.0);
// Clamp to the range we want to display
double t = Math.Clamp(temperature, 0.0, 2000.0);
byte r, g, b;
if (t < 0)
if (t < AmbientTemperature)
{
double factor = -t;
// Blue → Green
double factor = t / AmbientTemperature; // 0 at 0K, 1 at 300K
r = 0;
g = (byte)(255 * (1 - factor));
b = (byte)(255 * factor);
g = (byte)(255 * factor);
b = (byte)(255 * (1.0 - factor));
}
else
{
double factor = t;
// Green → Red
double factor = (t - AmbientTemperature) / (2000.0 - AmbientTemperature); // 0 at 300K, 1 at 2000K
r = (byte)(255 * factor);
g = (byte)(255 * (1 - factor));
g = (byte)(255 * (1.0 - factor));
b = 0;
}
return new Color(r, g, b);
}
/// <summary>
/// Draws the pipe as a smooth trianglestrip whose radius varies with cell pressure.
/// Draws the pipe as a smooth trianglestrip whose radius varies with cell pressure (for visibility),
/// but colored by temperature.
/// </summary>
protected void DrawPipe(RenderWindow target, Pipe1D pipe, float pipeCenterY, float pipeStartX, float pipeEndX)
{
@@ -57,8 +60,8 @@ namespace FluidSim.Tests
float dx = pipeLengthPx / (n - 1); // spacing between cell centres
float baseRadius = 25f;
float rangeFactor = 1f;
float scaleFactor = 5f;
float rangeFactor = 2f;
float scaleFactor = 2f;
// ----- smoothstep helper -----
static float SmoothStep(float edge0, float edge1, float x)
@@ -67,12 +70,19 @@ namespace FluidSim.Tests
return t * t * (3f - 2f * t);
}
// ----- Precompute cell positions and radii -----
// ----- Precompute cell positions, radii, and temperatures -----
var centers = new float[n];
var radii = new float[n];
var temperatures = new double[n];
double R_gas = 287.0;
for (int i = 0; i < n; i++)
{
double p = pipe.GetCellPressure(i);
double rho = pipe.GetCellDensity(i);
double T = p / Math.Max(rho * R_gas, 1e-12); // ideal gas
temperatures[i] = T;
float deviation = (float)Math.Tanh((p - AmbientPressure) / AmbientPressure / rangeFactor);
radii[i] = baseRadius * (1f + deviation * scaleFactor);
if (radii[i] < 2f) radii[i] = 2f;
@@ -89,8 +99,7 @@ namespace FluidSim.Tests
{
float x = centers[i];
float r = radii[i];
double p = pipe.GetCellPressure(i);
Color col = PressureColor(p);
Color col = TemperatureColor(temperatures[i]);
stripVertices[idx++] = new Vertex(new Vector2f(x, pipeCenterY - r), col);
stripVertices[idx++] = new Vertex(new Vector2f(x, pipeCenterY + r), col);
@@ -103,8 +112,8 @@ namespace FluidSim.Tests
float st = SmoothStep(0f, 1f, t);
float xi = centers[i] + (centers[i + 1] - centers[i]) * t;
float ri = radii[i] + (radii[i + 1] - radii[i]) * st;
double pi = pipe.GetCellPressure(i) * (1 - t) + pipe.GetCellPressure(i + 1) * t;
Color coli = PressureColor(pi);
double Ti = temperatures[i] + (temperatures[i + 1] - temperatures[i]) * st; // linear interpolation
Color coli = TemperatureColor(Ti);
stripVertices[idx++] = new Vertex(new Vector2f(xi, pipeCenterY - ri), coli);
stripVertices[idx++] = new Vertex(new Vector2f(xi, pipeCenterY + ri), coli);