Make thermomachines more thermodynamically sound (#18984)
Co-authored-by: Ilya246 <ilyukarno@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using Content.Shared.Atmos.Piping.Unary.Components;
|
using Content.Shared.Atmos;
|
||||||
|
using Content.Shared.Atmos.Piping.Unary.Components;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Robust.Client.GameObjects;
|
using Robust.Client.GameObjects;
|
||||||
|
|
||||||
@@ -19,6 +20,9 @@ namespace Content.Client.Atmos.UI
|
|||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
private float _maxTemp = 0.0f;
|
private float _maxTemp = 0.0f;
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
|
private bool _isHeater = true;
|
||||||
|
|
||||||
public GasThermomachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
public GasThermomachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -50,7 +54,12 @@ namespace Content.Client.Atmos.UI
|
|||||||
|
|
||||||
private void OnTemperatureChanged(float value)
|
private void OnTemperatureChanged(float value)
|
||||||
{
|
{
|
||||||
var actual = Math.Clamp(value, _minTemp, _maxTemp);
|
var actual = 0f;
|
||||||
|
if (_isHeater)
|
||||||
|
actual = Math.Min(value, _maxTemp);
|
||||||
|
else
|
||||||
|
actual = Math.Max(value, _minTemp);
|
||||||
|
actual = Math.Max(actual, Atmospherics.TCMB);
|
||||||
if (!MathHelper.CloseTo(actual, value, 0.09))
|
if (!MathHelper.CloseTo(actual, value, 0.09))
|
||||||
{
|
{
|
||||||
_window?.SetTemperature(actual);
|
_window?.SetTemperature(actual);
|
||||||
@@ -72,14 +81,14 @@ namespace Content.Client.Atmos.UI
|
|||||||
|
|
||||||
_minTemp = cast.MinTemperature;
|
_minTemp = cast.MinTemperature;
|
||||||
_maxTemp = cast.MaxTemperature;
|
_maxTemp = cast.MaxTemperature;
|
||||||
|
_isHeater = cast.IsHeater;
|
||||||
|
|
||||||
_window.SetTemperature(cast.Temperature);
|
_window.SetTemperature(cast.Temperature);
|
||||||
_window.SetActive(cast.Enabled);
|
_window.SetActive(cast.Enabled);
|
||||||
_window.Title = cast.Mode switch
|
_window.Title = _isHeater switch
|
||||||
{
|
{
|
||||||
ThermoMachineMode.Freezer => Loc.GetString("comp-gas-thermomachine-ui-title-freezer"),
|
false => Loc.GetString("comp-gas-thermomachine-ui-title-freezer"),
|
||||||
ThermoMachineMode.Heater => Loc.GetString("comp-gas-thermomachine-ui-title-heater"),
|
true => Loc.GetString("comp-gas-thermomachine-ui-title-heater")
|
||||||
_ => string.Empty
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace Content.Server.Atmos.Piping.Unary.Components
|
|||||||
public string InletName = "pipe";
|
public string InletName = "pipe";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current maximum temperature, calculated from <see cref="BaseHeatCapacity"/> and the quality of matter
|
/// Current electrical power consumption, in watts. Increasing power increases the ability of the
|
||||||
/// bins. The heat capacity effectively determines the rate at which the thermo machine can add or remove
|
/// thermomachine to heat or cool air.
|
||||||
/// heat from a pipenet.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables(VVAccess.ReadWrite)]
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
public float HeatCapacity = 10000;
|
public float HeatCapacity = 10000;
|
||||||
@@ -30,12 +29,31 @@ namespace Content.Server.Atmos.Piping.Unary.Components
|
|||||||
[ViewVariables(VVAccess.ReadWrite)]
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
public float TargetTemperature = Atmospherics.T20C;
|
public float TargetTemperature = Atmospherics.T20C;
|
||||||
|
|
||||||
[DataField("mode")]
|
/// <summary>
|
||||||
public ThermoMachineMode Mode = ThermoMachineMode.Freezer;
|
/// Tolerance for temperature setpoint hysteresis.
|
||||||
|
/// </summary>
|
||||||
|
[ViewVariables(VVAccess.ReadOnly)]
|
||||||
|
public float TemperatureTolerance = 2f;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implements setpoint hysteresis to prevent heater from rapidly cycling on and off at setpoint.
|
||||||
|
/// If true, add Sign(Cp)*TemperatureTolerance to the temperature setpoint.
|
||||||
|
/// </summary>
|
||||||
|
[ViewVariables(VVAccess.ReadOnly)]
|
||||||
|
public bool HysteresisState = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Coefficient of performance. Output power / input power.
|
||||||
|
/// Positive for heaters, negative for freezers.
|
||||||
|
/// </summary>
|
||||||
|
[DataField("coefficientOfPerformance")]
|
||||||
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
|
public float Cp = 0.9f; // output power / input power, positive is heat
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current minimum temperature, calculated from <see cref="InitialMinTemperature"/> and <see
|
/// Current minimum temperature, calculated from <see cref="InitialMinTemperature"/> and <see
|
||||||
/// cref="MinTemperatureDelta"/>.
|
/// cref="MinTemperatureDelta"/>.
|
||||||
|
/// Ignored if heater.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables(VVAccess.ReadWrite)]
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
public float MinTemperature;
|
public float MinTemperature;
|
||||||
@@ -43,6 +61,7 @@ namespace Content.Server.Atmos.Piping.Unary.Components
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Current maximum temperature, calculated from <see cref="InitialMaxTemperature"/> and <see
|
/// Current maximum temperature, calculated from <see cref="InitialMaxTemperature"/> and <see
|
||||||
/// cref="MaxTemperatureDelta"/>.
|
/// cref="MaxTemperatureDelta"/>.
|
||||||
|
/// Ignored if freezer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables(VVAccess.ReadWrite)]
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
public float MaxTemperature;
|
public float MaxTemperature;
|
||||||
|
|||||||
@@ -49,48 +49,74 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|||||||
|
|
||||||
private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent thermoMachine, AtmosDeviceUpdateEvent args)
|
private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent thermoMachine, AtmosDeviceUpdateEvent args)
|
||||||
{
|
{
|
||||||
|
if (!(_power.IsPowered(uid) && TryComp<ApcPowerReceiverComponent>(uid, out var receiver))
|
||||||
if (!(_power.IsPowered(uid))
|
|| !TryComp<NodeContainerComponent>(uid, out var nodeContainer)
|
||||||
|| !TryComp(uid, out NodeContainerComponent? nodeContainer)
|
|
||||||
|| !_nodeContainer.TryGetNode(nodeContainer, thermoMachine.InletName, out PipeNode? inlet))
|
|| !_nodeContainer.TryGetNode(nodeContainer, thermoMachine.InletName, out PipeNode? inlet))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var airHeatCapacity = _atmosphereSystem.GetHeatCapacity(inlet.Air);
|
float sign = Math.Sign(thermoMachine.Cp); // 1 if heater, -1 if freezer
|
||||||
var combinedHeatCapacity = airHeatCapacity + thermoMachine.HeatCapacity;
|
float targetTemp = thermoMachine.TargetTemperature;
|
||||||
|
float highTemp = targetTemp + sign * thermoMachine.TemperatureTolerance;
|
||||||
|
float temp = inlet.Air.Temperature;
|
||||||
|
|
||||||
var startEnergy = inlet.Air.Temperature * airHeatCapacity;
|
if (sign * temp >= sign * highTemp) // upper bound
|
||||||
|
thermoMachine.HysteresisState = false; // turn off
|
||||||
|
else if (sign * temp < sign * targetTemp) // lower bound
|
||||||
|
thermoMachine.HysteresisState = true; // turn on
|
||||||
|
|
||||||
if (!MathHelper.CloseTo(combinedHeatCapacity, 0, 0.001f))
|
if (thermoMachine.HysteresisState)
|
||||||
|
targetTemp = highTemp; // when on, target upper hysteresis bound
|
||||||
|
|
||||||
|
if (!thermoMachine.HysteresisState) // Hysteresis is the same as "Should this be on?"
|
||||||
{
|
{
|
||||||
var combinedEnergy = thermoMachine.HeatCapacity * thermoMachine.TargetTemperature + airHeatCapacity * inlet.Air.Temperature;
|
// Turn dynamic load back on when power has been adjusted to not cause lights to
|
||||||
inlet.Air.Temperature = combinedEnergy / combinedHeatCapacity;
|
// blink every time this heater comes on.
|
||||||
|
//receiver.Load = 0f;
|
||||||
thermoMachine.LastEnergyDelta = inlet.Air.Temperature * airHeatCapacity - startEnergy;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Multiply power in by coefficient of performance, add that heat to gas
|
||||||
|
float dQ = thermoMachine.HeatCapacity * thermoMachine.Cp * args.dt;
|
||||||
|
|
||||||
|
// Clamps the heat transferred to not overshoot
|
||||||
|
float Cin = _atmosphereSystem.GetHeatCapacity(inlet.Air);
|
||||||
|
float dT = targetTemp - temp;
|
||||||
|
float dQLim = dT * Cin;
|
||||||
|
float scale = 1f;
|
||||||
|
if (Math.Abs(dQ) > Math.Abs(dQLim))
|
||||||
|
{
|
||||||
|
scale = dQLim / dQ; // reduce power consumption
|
||||||
|
thermoMachine.HysteresisState = false; // turn off
|
||||||
|
}
|
||||||
|
float dQActual = dQ * scale;
|
||||||
|
_atmosphereSystem.AddHeat(inlet.Air, dQActual);
|
||||||
|
receiver.Load = thermoMachine.HeatCapacity;// * scale; // we're not ready for dynamic load yet, see note above
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsHeater(GasThermoMachineComponent comp)
|
||||||
|
{
|
||||||
|
return comp.Cp >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnGasThermoRefreshParts(EntityUid uid, GasThermoMachineComponent thermoMachine, RefreshPartsEvent args)
|
private void OnGasThermoRefreshParts(EntityUid uid, GasThermoMachineComponent thermoMachine, RefreshPartsEvent args)
|
||||||
{
|
{
|
||||||
var heatCapacityPartRating = args.PartRatings[thermoMachine.MachinePartHeatCapacity];
|
var heatCapacityPartRating = args.PartRatings[thermoMachine.MachinePartHeatCapacity];
|
||||||
var temperatureRangePartRating = args.PartRatings[thermoMachine.MachinePartTemperature];
|
|
||||||
|
|
||||||
thermoMachine.HeatCapacity = thermoMachine.BaseHeatCapacity * MathF.Pow(heatCapacityPartRating, 2);
|
thermoMachine.HeatCapacity = thermoMachine.BaseHeatCapacity * MathF.Pow(heatCapacityPartRating, 2);
|
||||||
|
|
||||||
switch (thermoMachine.Mode)
|
var temperatureRangePartRating = args.PartRatings[thermoMachine.MachinePartTemperature];
|
||||||
|
if (IsHeater(thermoMachine))
|
||||||
{
|
{
|
||||||
// 593.15K with stock parts.
|
// 593.15K with stock parts.
|
||||||
case ThermoMachineMode.Heater:
|
thermoMachine.MaxTemperature = thermoMachine.BaseMaxTemperature + thermoMachine.MaxTemperatureDelta * temperatureRangePartRating;
|
||||||
thermoMachine.MaxTemperature = thermoMachine.BaseMaxTemperature + thermoMachine.MaxTemperatureDelta * temperatureRangePartRating;
|
thermoMachine.MinTemperature = Atmospherics.T20C;
|
||||||
thermoMachine.MinTemperature = Atmospherics.T20C;
|
}
|
||||||
break;
|
else {
|
||||||
// 73.15K with stock parts.
|
// 73.15K with stock parts.
|
||||||
case ThermoMachineMode.Freezer:
|
thermoMachine.MinTemperature = MathF.Max(
|
||||||
thermoMachine.MinTemperature = MathF.Max(
|
thermoMachine.BaseMinTemperature - thermoMachine.MinTemperatureDelta * temperatureRangePartRating, Atmospherics.TCMB);
|
||||||
thermoMachine.BaseMinTemperature - thermoMachine.MinTemperatureDelta * temperatureRangePartRating, Atmospherics.TCMB);
|
thermoMachine.MaxTemperature = Atmospherics.T20C;
|
||||||
thermoMachine.MaxTemperature = Atmospherics.T20C;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DirtyUI(uid, thermoMachine);
|
DirtyUI(uid, thermoMachine);
|
||||||
@@ -98,14 +124,13 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|||||||
|
|
||||||
private void OnGasThermoUpgradeExamine(EntityUid uid, GasThermoMachineComponent thermoMachine, UpgradeExamineEvent args)
|
private void OnGasThermoUpgradeExamine(EntityUid uid, GasThermoMachineComponent thermoMachine, UpgradeExamineEvent args)
|
||||||
{
|
{
|
||||||
switch (thermoMachine.Mode)
|
if (IsHeater(thermoMachine))
|
||||||
{
|
{
|
||||||
case ThermoMachineMode.Heater:
|
args.AddPercentageUpgrade("gas-thermo-component-upgrade-heating", thermoMachine.MaxTemperature / (thermoMachine.BaseMaxTemperature + thermoMachine.MaxTemperatureDelta));
|
||||||
args.AddPercentageUpgrade("gas-thermo-component-upgrade-heating", thermoMachine.MaxTemperature / (thermoMachine.BaseMaxTemperature + thermoMachine.MaxTemperatureDelta));
|
}
|
||||||
break;
|
else
|
||||||
case ThermoMachineMode.Freezer:
|
{
|
||||||
args.AddPercentageUpgrade("gas-thermo-component-upgrade-cooling", thermoMachine.MinTemperature / (thermoMachine.BaseMinTemperature - thermoMachine.MinTemperatureDelta));
|
args.AddPercentageUpgrade("gas-thermo-component-upgrade-cooling", thermoMachine.MinTemperature / (thermoMachine.BaseMinTemperature - thermoMachine.MinTemperatureDelta));
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
args.AddPercentageUpgrade("gas-thermo-component-upgrade-heat-capacity", thermoMachine.HeatCapacity / thermoMachine.BaseHeatCapacity);
|
args.AddPercentageUpgrade("gas-thermo-component-upgrade-heat-capacity", thermoMachine.HeatCapacity / thermoMachine.BaseHeatCapacity);
|
||||||
}
|
}
|
||||||
@@ -118,9 +143,11 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|||||||
|
|
||||||
private void OnChangeTemperature(EntityUid uid, GasThermoMachineComponent thermoMachine, GasThermomachineChangeTemperatureMessage args)
|
private void OnChangeTemperature(EntityUid uid, GasThermoMachineComponent thermoMachine, GasThermomachineChangeTemperatureMessage args)
|
||||||
{
|
{
|
||||||
thermoMachine.TargetTemperature =
|
if (IsHeater(thermoMachine))
|
||||||
Math.Clamp(args.Temperature, thermoMachine.MinTemperature, thermoMachine.MaxTemperature);
|
thermoMachine.TargetTemperature = MathF.Min(args.Temperature, thermoMachine.MaxTemperature);
|
||||||
|
else
|
||||||
|
thermoMachine.TargetTemperature = MathF.Max(args.Temperature, thermoMachine.MinTemperature);
|
||||||
|
thermoMachine.TargetTemperature = MathF.Max(thermoMachine.TargetTemperature, Atmospherics.TCMB);
|
||||||
DirtyUI(uid, thermoMachine);
|
DirtyUI(uid, thermoMachine);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +161,7 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
_userInterfaceSystem.TrySetUiState(uid, ThermomachineUiKey.Key,
|
_userInterfaceSystem.TrySetUiState(uid, ThermomachineUiKey.Key,
|
||||||
new GasThermomachineBoundUserInterfaceState(thermoMachine.MinTemperature, thermoMachine.MaxTemperature, thermoMachine.TargetTemperature, !powerReceiver.PowerDisabled, thermoMachine.Mode), null, ui);
|
new GasThermomachineBoundUserInterfaceState(thermoMachine.MinTemperature, thermoMachine.MaxTemperature, thermoMachine.TargetTemperature, !powerReceiver.PowerDisabled, IsHeater(thermoMachine)), null, ui);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnExamined(EntityUid uid, GasThermoMachineComponent thermoMachine, ExaminedEvent args)
|
private void OnExamined(EntityUid uid, GasThermoMachineComponent thermoMachine, ExaminedEvent args)
|
||||||
@@ -143,8 +170,8 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (Loc.TryGetString("gas-thermomachine-system-examined", out var str,
|
if (Loc.TryGetString("gas-thermomachine-system-examined", out var str,
|
||||||
("machineName", thermoMachine.Mode == ThermoMachineMode.Freezer ? "freezer" : "heater"),
|
("machineName", !IsHeater(thermoMachine) ? "freezer" : "heater"),
|
||||||
("tempColor", thermoMachine.Mode == ThermoMachineMode.Freezer ? "deepskyblue" : "red"),
|
("tempColor", !IsHeater(thermoMachine) ? "deepskyblue" : "red"),
|
||||||
("temp", Math.Round(thermoMachine.TargetTemperature,2))
|
("temp", Math.Round(thermoMachine.TargetTemperature,2))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,6 @@ public enum ThermomachineUiKey
|
|||||||
Key
|
Key
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
[NetSerializable]
|
|
||||||
public enum ThermoMachineMode : byte
|
|
||||||
{
|
|
||||||
Freezer = 0,
|
|
||||||
Heater = 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
[NetSerializable]
|
[NetSerializable]
|
||||||
public sealed class GasThermomachineToggleMessage : BoundUserInterfaceMessage
|
public sealed class GasThermomachineToggleMessage : BoundUserInterfaceMessage
|
||||||
@@ -46,14 +38,14 @@ public sealed class GasThermomachineBoundUserInterfaceState : BoundUserInterface
|
|||||||
public float MaxTemperature { get; }
|
public float MaxTemperature { get; }
|
||||||
public float Temperature { get; }
|
public float Temperature { get; }
|
||||||
public bool Enabled { get; }
|
public bool Enabled { get; }
|
||||||
public ThermoMachineMode Mode { get; }
|
public bool IsHeater { get; }
|
||||||
|
|
||||||
public GasThermomachineBoundUserInterfaceState(float minTemperature, float maxTemperature, float temperature, bool enabled, ThermoMachineMode mode)
|
public GasThermomachineBoundUserInterfaceState(float minTemperature, float maxTemperature, float temperature, bool enabled, bool isHeater)
|
||||||
{
|
{
|
||||||
MinTemperature = minTemperature;
|
MinTemperature = minTemperature;
|
||||||
MaxTemperature = maxTemperature;
|
MaxTemperature = maxTemperature;
|
||||||
Temperature = temperature;
|
Temperature = temperature;
|
||||||
Enabled = enabled;
|
Enabled = enabled;
|
||||||
Mode = mode;
|
IsHeater = isHeater;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -284,7 +284,7 @@
|
|||||||
True: { state: freezerOn }
|
True: { state: freezerOn }
|
||||||
False: { state: freezerOff }
|
False: { state: freezerOff }
|
||||||
- type: GasThermoMachine
|
- type: GasThermoMachine
|
||||||
mode: Freezer
|
coefficientOfPerformance: -3.9
|
||||||
- type: ApcPowerReceiver
|
- type: ApcPowerReceiver
|
||||||
powerDisabled: true #starts off
|
powerDisabled: true #starts off
|
||||||
- type: Machine
|
- type: Machine
|
||||||
@@ -327,7 +327,7 @@
|
|||||||
True: { state: heaterOn }
|
True: { state: heaterOn }
|
||||||
False: { state: heaterOff }
|
False: { state: heaterOff }
|
||||||
- type: GasThermoMachine
|
- type: GasThermoMachine
|
||||||
mode: Heater
|
coefficientOfPerformance: 0.95
|
||||||
- type: ApcPowerReceiver
|
- type: ApcPowerReceiver
|
||||||
powerDisabled: true #starts off
|
powerDisabled: true #starts off
|
||||||
- type: Machine
|
- type: Machine
|
||||||
|
|||||||
Reference in New Issue
Block a user