ECS Atmos Part 2: Moves a lot of Gas Mixture methods to AtmosphereSystem. (#4218)

This commit is contained in:
Vera Aguilera Puerto
2021-06-23 11:35:30 +02:00
committed by GitHub
parent e16c23a747
commit 263c9ef974
34 changed files with 461 additions and 464 deletions

View File

@@ -22,26 +22,23 @@ namespace Content.Server.Atmos
[DataDefinition]
public class GasMixture : IEquatable<GasMixture>, ICloneable, ISerializationHooks
{
private AtmosphereSystem? _atmosphereSystem;
public static GasMixture SpaceGas => new() {Volume = 2500f, Immutable = true, Temperature = Atmospherics.TCMB};
public static GasMixture SpaceGas => new() {Volume = Atmospherics.CellVolume, Temperature = Atmospherics.TCMB, Immutable = true};
// This must always have a length that is a multiple of 4 for SIMD acceleration.
[DataField("moles")] [ViewVariables] private float[] _moles = new float[Atmospherics.AdjustedNumberOfGases];
[DataField("moles")] [ViewVariables]
public float[] Moles = new float[Atmospherics.AdjustedNumberOfGases];
[DataField("molesArchived")] [ViewVariables]
private float[] _molesArchived = new float[Atmospherics.AdjustedNumberOfGases];
public float[] MolesArchived = new float[Atmospherics.AdjustedNumberOfGases];
[DataField("temperature")] [ViewVariables]
private float _temperature = Atmospherics.TCMB;
public IReadOnlyList<float> Gases => _moles;
[DataField("immutable")] [ViewVariables]
public bool Immutable { get; private set; }
[DataField("lastShare")] [ViewVariables]
public float LastShare { get; private set; }
public float LastShare { get; set; }
[ViewVariables]
public readonly Dictionary<GasReaction, float> ReactionResults = new()
@@ -50,39 +47,11 @@ namespace Content.Server.Atmos
{ GasReaction.Fire, 0f }
};
[ViewVariables]
public float HeatCapacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
_atmosphereSystem ??= EntitySystem.Get<AtmosphereSystem>();
Span<float> tmp = stackalloc float[_moles.Length];
NumericsHelpers.Multiply(_moles, _atmosphereSystem.GasSpecificHeats, tmp);
return MathF.Max(NumericsHelpers.HorizontalAdd(tmp), Atmospherics.MinimumHeatCapacity);
}
}
[ViewVariables]
public float HeatCapacityArchived
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
_atmosphereSystem ??= EntitySystem.Get<AtmosphereSystem>();
Span<float> tmp = stackalloc float[_moles.Length];
NumericsHelpers.Multiply(_molesArchived, _atmosphereSystem.GasSpecificHeats, tmp);
return MathF.Max(NumericsHelpers.HorizontalAdd(tmp), Atmospherics.MinimumHeatCapacity);
}
}
[ViewVariables]
public float TotalMoles
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => NumericsHelpers.HorizontalAdd(_moles);
get => NumericsHelpers.HorizontalAdd(Moles);
}
[ViewVariables]
@@ -106,25 +75,17 @@ namespace Content.Server.Atmos
}
}
[ViewVariables]
public float ThermalEnergy => Temperature * HeatCapacity;
[DataField("temperatureArchived")] [ViewVariables]
public float TemperatureArchived { get; private set; }
[DataField("volume")] [ViewVariables]
public float Volume { get; set; }
public GasMixture() : this(null)
public GasMixture()
{
}
public GasMixture(AtmosphereSystem? atmosphereSystem)
{
_atmosphereSystem = atmosphereSystem;
}
public GasMixture(float volume, AtmosphereSystem? atmosphereSystem = null): this(atmosphereSystem)
public GasMixture(float volume = 0f)
{
if (volume < 0)
volume = 0;
@@ -140,31 +101,14 @@ namespace Content.Server.Atmos
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Archive()
{
_moles.AsSpan().CopyTo(_molesArchived.AsSpan());
Moles.AsSpan().CopyTo(MolesArchived.AsSpan());
TemperatureArchived = Temperature;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Merge(GasMixture giver)
{
if (Immutable) return;
if (MathF.Abs(Temperature - giver.Temperature) > Atmospherics.MinimumTemperatureDeltaToConsider)
{
var combinedHeatCapacity = HeatCapacity + giver.HeatCapacity;
if (combinedHeatCapacity > 0f)
{
Temperature = (giver.Temperature * giver.HeatCapacity + Temperature * HeatCapacity) / combinedHeatCapacity;
}
}
NumericsHelpers.Add(_moles, giver._moles);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetMoles(int gasId)
{
return _moles[gasId];
return Moles[gasId];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -180,7 +124,7 @@ namespace Content.Server.Atmos
throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(quantity));
if (!Immutable)
_moles[gasId] = quantity;
Moles[gasId] = quantity;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -197,9 +141,9 @@ namespace Content.Server.Atmos
if (float.IsInfinity(quantity) || float.IsNaN(quantity))
throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(quantity));
_moles[gasId] += quantity;
Moles[gasId] += quantity;
var moles = _moles[gasId];
var moles = Moles[gasId];
if (float.IsInfinity(moles) || float.IsNaN(moles) || float.IsNegative(moles))
throw new Exception($"Invalid mole quantity \"{moles}\" in gas Id {gasId} after adjusting moles with \"{quantity}\"!");
@@ -224,28 +168,28 @@ namespace Content.Server.Atmos
switch (ratio)
{
case <= 0:
return new GasMixture(Volume, _atmosphereSystem){Temperature = Temperature};
return new GasMixture(Volume){Temperature = Temperature};
case > 1:
ratio = 1;
break;
}
var removed = new GasMixture(_atmosphereSystem) {Volume = Volume, Temperature = Temperature};
var removed = new GasMixture(Volume) { Temperature = Temperature };
_moles.CopyTo(removed._moles.AsSpan());
NumericsHelpers.Multiply(removed._moles, ratio);
Moles.CopyTo(removed.Moles.AsSpan());
NumericsHelpers.Multiply(removed.Moles, ratio);
if (!Immutable)
NumericsHelpers.Sub(_moles, removed._moles);
NumericsHelpers.Sub(Moles, removed.Moles);
for (var i = 0; i < _moles.Length; i++)
for (var i = 0; i < Moles.Length; i++)
{
var moles = _moles[i];
var otherMoles = removed._moles[i];
var moles = Moles[i];
var otherMoles = removed.Moles[i];
if (moles < Atmospherics.GasMinMoles || float.IsNaN(moles))
_moles[i] = 0;
Moles[i] = 0;
if (otherMoles < Atmospherics.GasMinMoles || float.IsNaN(otherMoles))
removed._moles[i] = 0;
removed.Moles[i] = 0;
}
return removed;
@@ -255,139 +199,10 @@ namespace Content.Server.Atmos
public void CopyFromMutable(GasMixture sample)
{
if (Immutable) return;
sample._moles.CopyTo(_moles, 0);
sample.Moles.CopyTo(Moles, 0);
Temperature = sample.Temperature;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Share(GasMixture sharer, int atmosAdjacentTurfs)
{
_atmosphereSystem ??= EntitySystem.Get<AtmosphereSystem>();
var temperatureDelta = TemperatureArchived - sharer.TemperatureArchived;
var absTemperatureDelta = Math.Abs(temperatureDelta);
var oldHeatCapacity = 0f;
var oldSharerHeatCapacity = 0f;
if (absTemperatureDelta > Atmospherics.MinimumTemperatureDeltaToConsider)
{
oldHeatCapacity = HeatCapacity;
oldSharerHeatCapacity = sharer.HeatCapacity;
}
var heatCapacityToSharer = 0f;
var heatCapacitySharerToThis = 0f;
var movedMoles = 0f;
var absMovedMoles = 0f;
for(var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var thisValue = _moles[i];
var sharerValue = sharer._moles[i];
var delta = (thisValue - sharerValue) / (atmosAdjacentTurfs + 1);
if (!(MathF.Abs(delta) >= Atmospherics.GasMinMoles)) continue;
if (absTemperatureDelta > Atmospherics.MinimumTemperatureDeltaToConsider)
{
var gasHeatCapacity = delta * _atmosphereSystem.GasSpecificHeats[i];
if (delta > 0)
{
heatCapacityToSharer += gasHeatCapacity;
}
else
{
heatCapacitySharerToThis -= gasHeatCapacity;
}
}
if (!Immutable) _moles[i] -= delta;
if (!sharer.Immutable) sharer._moles[i] += delta;
movedMoles += delta;
absMovedMoles += MathF.Abs(delta);
}
LastShare = absMovedMoles;
if (absTemperatureDelta > Atmospherics.MinimumTemperatureDeltaToConsider)
{
var newHeatCapacity = oldHeatCapacity + heatCapacitySharerToThis - heatCapacityToSharer;
var newSharerHeatCapacity = oldSharerHeatCapacity + heatCapacityToSharer - heatCapacitySharerToThis;
// Transfer of thermal energy (via changed heat capacity) between self and sharer.
if (!Immutable && newHeatCapacity > Atmospherics.MinimumHeatCapacity)
{
Temperature = ((oldHeatCapacity * Temperature) - (heatCapacityToSharer * TemperatureArchived) + (heatCapacitySharerToThis * sharer.TemperatureArchived)) / newHeatCapacity;
}
if (!sharer.Immutable && newSharerHeatCapacity > Atmospherics.MinimumHeatCapacity)
{
sharer.Temperature = ((oldSharerHeatCapacity * sharer.Temperature) - (heatCapacitySharerToThis * sharer.TemperatureArchived) + (heatCapacityToSharer*TemperatureArchived)) / newSharerHeatCapacity;
}
// Thermal energy of the system (self and sharer) is unchanged.
if (MathF.Abs(oldSharerHeatCapacity) > Atmospherics.MinimumHeatCapacity)
{
if (MathF.Abs(newSharerHeatCapacity / oldSharerHeatCapacity - 1) < 0.1)
{
TemperatureShare(sharer, Atmospherics.OpenHeatTransferCoefficient);
}
}
}
if (!(temperatureDelta > Atmospherics.MinimumTemperatureToMove) &&
!(MathF.Abs(movedMoles) > Atmospherics.MinimumMolesDeltaToMove)) return 0f;
var moles = TotalMoles;
var theirMoles = sharer.TotalMoles;
return (TemperatureArchived * (moles + movedMoles)) - (sharer.TemperatureArchived * (theirMoles - movedMoles)) * Atmospherics.R / Volume;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float TemperatureShare(GasMixture sharer, float conductionCoefficient)
{
var temperatureDelta = TemperatureArchived - sharer.TemperatureArchived;
if (MathF.Abs(temperatureDelta) > Atmospherics.MinimumTemperatureDeltaToConsider)
{
var heatCapacity = HeatCapacityArchived;
var sharerHeatCapacity = sharer.HeatCapacityArchived;
if (sharerHeatCapacity > Atmospherics.MinimumHeatCapacity && heatCapacity > Atmospherics.MinimumHeatCapacity)
{
var heat = conductionCoefficient * temperatureDelta * (heatCapacity * sharerHeatCapacity / (heatCapacity + sharerHeatCapacity));
if (!Immutable)
Temperature = MathF.Abs(MathF.Max(Temperature - heat / heatCapacity, Atmospherics.TCMB));
if (!sharer.Immutable)
sharer.Temperature = MathF.Abs(MathF.Max(sharer.Temperature + heat / sharerHeatCapacity, Atmospherics.TCMB));
}
}
return sharer.Temperature;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float TemperatureShare(float conductionCoefficient, float sharerTemperature, float sharerHeatCapacity)
{
var temperatureDelta = TemperatureArchived - sharerTemperature;
if (MathF.Abs(temperatureDelta) > Atmospherics.MinimumTemperatureDeltaToConsider)
{
var heatCapacity = HeatCapacityArchived;
if (sharerHeatCapacity > Atmospherics.MinimumHeatCapacity && heatCapacity > Atmospherics.MinimumHeatCapacity)
{
var heat = conductionCoefficient * temperatureDelta * (heatCapacity * sharerHeatCapacity / (heatCapacity + sharerHeatCapacity));
if (!Immutable)
Temperature = MathF.Abs(MathF.Max(Temperature - heat / heatCapacity, Atmospherics.TCMB));
sharerTemperature = MathF.Abs(MathF.Max(sharerTemperature + heat / sharerHeatCapacity, Atmospherics.TCMB));
}
}
return sharerTemperature;
}
public enum GasCompareResult
{
NoExchange = -2,
@@ -404,8 +219,8 @@ namespace Content.Server.Atmos
for(var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var gasMoles = _moles[i];
var delta = MathF.Abs(gasMoles - sample._moles[i]);
var gasMoles = Moles[i];
var delta = MathF.Abs(gasMoles - sample.Moles[i]);
if (delta > Atmospherics.MinimumMolesDeltaToMove && (delta > gasMoles * Atmospherics.MinimumAirRatioToMove))
return (GasCompareResult)i; // We can move gases!
moles += gasMoles;
@@ -422,125 +237,25 @@ namespace Content.Server.Atmos
return GasCompareResult.NoExchange;
}
/// <summary>
/// Pump gas from this mixture to the output mixture.
/// Amount depends on target pressure.
/// </summary>
/// <param name="outputAir">The mixture to pump the gas to</param>
/// <param name="targetPressure">The target pressure to reach</param>
/// <returns>Whether we could pump air to the output or not</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool PumpGasTo(GasMixture outputAir, float targetPressure)
{
var outputStartingPressure = outputAir.Pressure;
var pressureDelta = targetPressure - outputStartingPressure;
if (pressureDelta < 0.01)
// No need to pump gas, we've reached the target.
return false;
if (!(TotalMoles > 0) || !(Temperature > 0)) return false;
// We calculate the necessary moles to transfer with the ideal gas law.
var transferMoles = pressureDelta * outputAir.Volume / (Temperature * Atmospherics.R);
// And now we transfer the gas.
var removed = Remove(transferMoles);
outputAir.Merge(removed);
return true;
}
/// <summary>
/// Releases gas from this mixture to the output mixture.
/// If the output mixture is null, then this is being released into space.
/// It can't transfer air to a mixture with higher pressure.
/// </summary>
/// <param name="outputAir"></param>
/// <param name="targetPressure"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ReleaseGasTo(GasMixture? outputAir, float targetPressure)
{
var outputStartingPressure = outputAir?.Pressure ?? 0;
var inputStartingPressure = Pressure;
if (outputStartingPressure >= MathF.Min(targetPressure, inputStartingPressure - 10))
// No need to pump gas if the target is already reached or input pressure is too low.
// Need at least 10 kPa difference to overcome friction in the mechanism.
return false;
if (!(TotalMoles > 0) || !(Temperature > 0)) return false;
// We calculate the necessary moles to transfer with the ideal gas law.
var pressureDelta = MathF.Min(targetPressure - outputStartingPressure, (inputStartingPressure - outputStartingPressure) / 2f);
var transferMoles = pressureDelta * (outputAir?.Volume ?? Atmospherics.CellVolume) / (Temperature * Atmospherics.R);
// And now we transfer the gas.
var removed = Remove(transferMoles);
outputAir?.Merge(removed);
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReactionResult React(IGasMixtureHolder holder)
{
_atmosphereSystem ??= EntitySystem.Get<AtmosphereSystem>();
var reaction = ReactionResult.NoReaction;
var temperature = Temperature;
var energy = ThermalEnergy;
foreach (var prototype in _atmosphereSystem.GasReactions)
{
if (energy < prototype.MinimumEnergyRequirement ||
temperature < prototype.MinimumTemperatureRequirement ||
temperature > prototype.MaximumTemperatureRequirement)
continue;
var doReaction = true;
for (var i = 0; i < prototype.MinimumRequirements.Length; i++)
{
if(i > Atmospherics.TotalNumberOfGases)
throw new IndexOutOfRangeException("Reaction Gas Minimum Requirements Array Prototype exceeds total number of gases!");
var req = prototype.MinimumRequirements[i];
if (!(GetMoles(i) < req)) continue;
doReaction = false;
break;
}
if (!doReaction)
continue;
reaction = prototype.React(this, holder, _atmosphereSystem.GridTileLookupSystem);
if(reaction.HasFlag(ReactionResult.StopReactions))
break;
}
return reaction;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
if (Immutable) return;
Array.Clear(_moles, 0, Atmospherics.TotalNumberOfGases);
Array.Clear(Moles, 0, Atmospherics.TotalNumberOfGases);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Multiply(float multiplier)
{
if (Immutable) return;
NumericsHelpers.Multiply(_moles, multiplier);
NumericsHelpers.Multiply(Moles, multiplier);
}
void ISerializationHooks.AfterDeserialization()
{
// The arrays MUST have a specific length.
Array.Resize(ref _moles, Atmospherics.AdjustedNumberOfGases);
Array.Resize(ref _molesArchived, Atmospherics.AdjustedNumberOfGases);
Array.Resize(ref Moles, Atmospherics.AdjustedNumberOfGases);
Array.Resize(ref MolesArchived, Atmospherics.AdjustedNumberOfGases);
}
public override bool Equals(object? obj)
@@ -554,8 +269,8 @@ namespace Content.Server.Atmos
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return _moles.SequenceEqual(other._moles)
&& _molesArchived.SequenceEqual(other._molesArchived)
return Moles.SequenceEqual(other.Moles)
&& MolesArchived.SequenceEqual(other.MolesArchived)
&& _temperature.Equals(other._temperature)
&& ReactionResults.SequenceEqual(other.ReactionResults)
&& Immutable == other.Immutable
@@ -570,8 +285,8 @@ namespace Content.Server.Atmos
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var moles = _moles[i];
var molesArchived = _molesArchived[i];
var moles = Moles[i];
var molesArchived = MolesArchived[i];
hashCode.Add(moles);
hashCode.Add(molesArchived);
}
@@ -587,10 +302,10 @@ namespace Content.Server.Atmos
public object Clone()
{
var newMixture = new GasMixture(_atmosphereSystem)
var newMixture = new GasMixture()
{
_moles = (float[])_moles.Clone(),
_molesArchived = (float[])_molesArchived.Clone(),
Moles = (float[])Moles.Clone(),
MolesArchived = (float[])MolesArchived.Clone(),
_temperature = _temperature,
Immutable = Immutable,
LastShare = LastShare,
@@ -599,18 +314,5 @@ namespace Content.Server.Atmos
};
return newMixture;
}
public void ScrubInto(GasMixture destination, IReadOnlyCollection<Gas> filterGases)
{
var buffer = new GasMixture(Volume){Temperature = Temperature};
foreach (var gas in filterGases)
{
buffer.AdjustMoles(gas, GetMoles(gas));
SetMoles(gas, 0f);
}
destination.Merge(buffer);
}
}
}