Solution precision fixes (#25199)

* Add test for two chemistry issues

1. rounding issue with reaction processing when making chloral hydrate
2. reliable assert trip due to the ValidateSolution() heat capacity issue.

* Fix FixedPoint2 arithmetic

Fix internal floating point arithmetic in places where it could be avoided.

Fix incorrect rounding mode used in other places (it should always floor, like regular int arithmetic).

I had to add an explicit epsilon value for float -> FixedPoint2 because something like 1.05 is actually like 1.04999 and that'd cause it to be rounded down to 1.04.

This fixes reaction reagent processing in cases where the reagent inputs can't cleanly divide. Previously, when making 30u chloral hydrate by adding the chlorine in 10u increments you'd end up with 0.04 chlorine left over. This was caused by division in the reaction code rounding up in some cases. Changing division here to always round down fixes it.

* Attempt to fix heat capacity precision assert issues.

Fixes #22126

First, we just increase the tolerance of the assert. It was way too low.

Second, actually put a cap on float drift from one-off _heatCapacity changes.

* Fix float -> FixedPoint2 epsilon for negative number, fix tests.

* Fix DamageableTest

* Oh yeah I need to call CleanReturnAsync
This commit is contained in:
Pieter-Jan Briers
2024-02-17 00:54:27 +01:00
committed by GitHub
parent c7870882f6
commit 33611b7094
6 changed files with 232 additions and 49 deletions

View File

@@ -92,6 +92,12 @@ namespace Content.Shared.Chemistry.Components
/// </summary>
[ViewVariables] private bool _heatCapacityDirty = true;
[ViewVariables(VVAccess.ReadWrite)]
private int _heatCapacityUpdateCounter;
// This value is arbitrary btw.
private const int HeatCapacityUpdateInterval = 15;
public void UpdateHeatCapacity(IPrototypeManager? protoMan)
{
IoCManager.Resolve(ref protoMan);
@@ -103,6 +109,8 @@ namespace Content.Shared.Chemistry.Components
_heatCapacity += (float) quantity *
protoMan.Index<ReagentPrototype>(reagent.Prototype).SpecificHeat;
}
_heatCapacityUpdateCounter = 0;
}
public float GetHeatCapacity(IPrototypeManager? protoMan)
@@ -112,6 +120,15 @@ namespace Content.Shared.Chemistry.Components
return _heatCapacity;
}
public void CheckRecalculateHeatCapacity()
{
// For performance, we have a few ways for heat capacity to get modified without a full recalculation.
// To avoid these drifting too much due to float error, we mark it as dirty after N such operations,
// so it will be recalculated.
if (++_heatCapacityUpdateCounter >= HeatCapacityUpdateInterval)
_heatCapacityDirty = true;
}
public float GetThermalEnergy(IPrototypeManager? protoMan)
{
return GetHeatCapacity(protoMan) * Temperature;
@@ -165,6 +182,7 @@ namespace Content.Shared.Chemistry.Components
Temperature = solution.Temperature;
_heatCapacity = solution._heatCapacity;
_heatCapacityDirty = solution._heatCapacityDirty;
_heatCapacityUpdateCounter = solution._heatCapacityUpdateCounter;
ValidateSolution();
}
@@ -193,7 +211,7 @@ namespace Content.Shared.Chemistry.Components
var cur = _heatCapacity;
_heatCapacityDirty = true;
UpdateHeatCapacity(null);
DebugTools.Assert(MathHelper.CloseTo(_heatCapacity, cur));
DebugTools.Assert(MathHelper.CloseTo(_heatCapacity, cur, tolerance: 0.01));
}
#endif
}
@@ -380,7 +398,9 @@ namespace Content.Shared.Chemistry.Components
public void AddReagent(ReagentPrototype proto, ReagentId reagentId, FixedPoint2 quantity)
{
AddReagent(reagentId, quantity, false);
_heatCapacity += quantity.Float() * proto.SpecificHeat;
CheckRecalculateHeatCapacity();
}
public void AddReagent(ReagentQuantity reagentQuantity)
@@ -419,6 +439,7 @@ namespace Content.Shared.Chemistry.Components
_heatCapacity *= scale;
Volume *= scale;
CheckRecalculateHeatCapacity();
for (int i = 0; i < Contents.Count; i++)
{
@@ -752,6 +773,7 @@ namespace Content.Shared.Chemistry.Components
}
_heatCapacity += otherSolution._heatCapacity;
CheckRecalculateHeatCapacity();
if (closeTemps)
_heatCapacityDirty |= otherSolution._heatCapacityDirty;
else