Fix radiator transfer rate in high-pressure environments (#18858)

Co-authored-by: Kevin Zheng <kevinz5000@gmail.com>
This commit is contained in:
Ilya246
2023-08-08 12:16:23 +04:00
committed by GitHub
parent 075ff1c775
commit 376a71100d

View File

@@ -58,7 +58,20 @@ public sealed class HeatExchangerSystem : EntitySystem
// Positive dN flows from inlet to outlet
var dt = (float)(_gameTiming.CurTime - device.LastProcess).TotalSeconds;
var dP = inlet.Air.Pressure - outlet.Air.Pressure;
var dN = comp.G*dP*dt;
// What we want is dN/dt = G*dP (first-order constant-coefficient differential equation w.r.t. P).
// However, by approximating dN = G*dP*dt using Forward Euler dN can be larger than all of the gas
// available for sufficiently large dP. However, we know that dN cannot exceed:
//
// dNMax = (n2T2/V2 - n1T1/V1)/(T2/V1 + T2/V2) = Δp/R/T2/(1/V1 + 1/V2)
//
// Because that is the amount of gas that needs to be transferred to equalize pressures [citation needed].
// So we just limit abs(dN) < abs(dNMax).
//
// Also, by factoring out dP we can avoid taking any abs().
// transferLimit below is equal to dNMax/dP
var transferLimit = 1/Atmospherics.R/outlet.Air.Temperature/(1/inlet.Air.Volume + 1/outlet.Air.Volume);
var dN = dP*Math.Min(comp.G*dt, transferLimit);
GasMixture xfer;
if (dN > 0)