Chiller system for Kerbal Space Program
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Chiller
{
public class ModuleChiller : PartModule
{
/// <summary>
/// Maximum electric power this Chiller unit can draw, in KSP Electric Charge per second. Since this mod assumes 1 EC = 1 kW.s, the value indicated here can be considered to be in kilowatts.
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float MaxPower = 10;
/// <summary>
/// Coefficient Of Performance (heat removed from the chilled part, divided by the electric power consumed)
/// Note that the electric power also ends up heating the radiator panel (in addition to the heat taken from the cooled part)
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float COP = 4;
/// <summary>
/// Target temperature for the part on which the Chiller is attached, in Kelvins. If the temperature of that part is above the set-point, the Chiller turns on to cool it.
/// </summary>
[KSPField(isPersistant=true, guiActive=true, guiName="Set-point (K)")]
[UI_FloatRange(minValue = 10, maxValue = 400)]
public float SetPoint = 100;
[KSPField(isPersistant = false, guiActive = false)]
public float SetPointThreshold = 5;
[KSPField(isPersistant = false, guiActive = true)]
public string Status = "Offline";
[KSPField(isPersistant = false, guiActive = true, guiName="Heat transfer")]
public string HeatTransferStatus = "Offline";
[KSPField(isPersistant = false, guiActive = true, guiName = "Temperature")]
public string CurrentTemperature = "";
[KSPField(isPersistant = false, guiActive = true, guiName = "Electric consumption")]
public string ElectricStatus = "";
private bool WasCooling = false;
private float CurrentHeatTransfer = 0;
public void FixedUpdate()
{
var cooledPart = part.parent;
bool isActive = part.FindModuleImplementing<ModuleActiveRadiator>().IsCooling;
Status = "Stand-by";
CurrentTemperature = cooledPart.temperature.ToString("0.00") + " K";
ElectricStatus = "";
bool cooling = false;
float energyToTransfer = 0;
if (isActive)
{
if (cooledPart.temperature > SetPoint + (WasCooling ? 0 : SetPointThreshold))
{
float maxRadiatorTemperature = (float)part.maxTemp * (float)part.radiatorHeadroom;
if ((float)part.temperature >= maxRadiatorTemperature - (WasCooling ? 0 : SetPointThreshold))
{
Status = "Radiator too hot";
}
else
{
float neededCoolingEnergy = ((float)cooledPart.temperature - SetPoint) * (float)cooledPart.thermalMass / TimeWarp.fixedDeltaTime;
float availableHeatEnergy = (maxRadiatorTemperature - (float)part.temperature) * (float)part.thermalMass * (COP / (COP + 1)) / TimeWarp.fixedDeltaTime;
energyToTransfer = Mathf.Min(neededCoolingEnergy, availableHeatEnergy, MaxPower * COP);
}
}
}
else
{
Status = "Off";
}
if (energyToTransfer > CurrentHeatTransfer)
{
// slow transfer startup, to simulate progressive refrigerant compression as the pump is started
float c = 1.0f / (1.0f + TimeWarp.fixedDeltaTime);
CurrentHeatTransfer = CurrentHeatTransfer * c + energyToTransfer * (1.0f - c);
}
else
{
// immediate reactivity when slowing down to avoid overshooting
CurrentHeatTransfer = energyToTransfer;
}
if(energyToTransfer > 0)
{
float requiredElectricCharge = CurrentHeatTransfer * TimeWarp.fixedDeltaTime / COP;
var electricCharge = vessel.GetActiveResources().SingleOrDefault(r => r.info.name == "ElectricCharge");
float totalAvailableElectricEnergy = electricCharge == null ? 0.0f : (float)electricCharge.amount;
// we don't use electric charge at all if battery is low, to avoid disabling more critical systems, such as command modules etc.
float electricChargeMargin = 10;
float availableElectricEnergy;
if (totalAvailableElectricEnergy > requiredElectricCharge + electricChargeMargin)
{
availableElectricEnergy = part.RequestResource(electricCharge.info.id, Math.Min(requiredElectricCharge, totalAvailableElectricEnergy - electricChargeMargin));
}
else
{
availableElectricEnergy = 0;
}
if (availableElectricEnergy < requiredElectricCharge * 0.9f)
{
Status = "Not enough power";
CurrentHeatTransfer = availableElectricEnergy * COP;
}
else
{
Status = "Cooling";
}
cooledPart.AddThermalFlux(-CurrentHeatTransfer * 2);
part.AddThermalFlux(CurrentHeatTransfer * 2 * (COP + 1) / COP);
ElectricStatus = availableElectricEnergy.ToString("0.00") + " kW";
cooling = true;
}
HeatTransferStatus = CurrentHeatTransfer.ToString("0.00") + " kW (" + (CurrentHeatTransfer / (float)cooledPart.thermalMass).ToString("0.00") + " K/s)";
WasCooling = cooling;
}
}
}