Skip to content

Commit

Permalink
Merge pull request #78 from RCrockford/rf-residuals
Browse files Browse the repository at this point in the history
Added RF residuals to delta V calculations
  • Loading branch information
jrbudda committed Jul 9, 2024
2 parents d1837a9 + 32f41d3 commit bf4145b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
8 changes: 8 additions & 0 deletions KerbalEngineer/Editor/BuildAdvanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ private void DrawSettings()
SimManager.vectoredThrust = GUILayout.Toggle(SimManager.vectoredThrust, "ENABLED", buttonStyle, GUILayout.Width(100.0f * GuiDisplaySize.Offset));
GUILayout.EndHorizontal();

if (SimManager.hasInstalledRealFuels)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Simulate using residual predictions:", settingStyle);
SimManager.RFResiduals = GUILayout.Toggle(SimManager.RFResiduals, "ENABLED", buttonStyle, GUILayout.Width(100.0f * GuiDisplaySize.Offset));
GUILayout.EndHorizontal();
}

GUILayout.BeginHorizontal();
GUILayout.Label("Verbose Simulation Log:", settingStyle);
SimManager.logOutput = GUILayout.Toggle(SimManager.logOutput, "ENABLED", buttonStyle, GUILayout.Width(100.0f * GuiDisplaySize.Offset));
Expand Down
24 changes: 20 additions & 4 deletions KerbalEngineer/VesselSimulator/EngineSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class EngineSim

public double thrust = 0;

private double resourceMin;

// Add thrust vector to account for directional losses
public Vector3 thrustVec;

Expand Down Expand Up @@ -134,7 +136,15 @@ public static EngineSim New(PartSim theEngine,
engineSim.resourceFlowModes.Reset();
engineSim.appliedForces.Clear();


engineSim.resourceMin = SimManager.RESOURCE_MIN;
if (SimManager.RFResiduals)
{
var residualsField = engineMod.GetType().GetField("predictedMaximumResiduals");
if (residualsField != null)
{
engineSim.resourceMin = (double)residualsField.GetValue(engineMod);
}
}

double flowRate = 0.0;
if (engineSim.partSim.hasVessel)
Expand Down Expand Up @@ -397,10 +407,14 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
sourcePartSets.Add(type, sourcePartSet);
}

double thisResourceMin = resourceMin;
if (SimManager.RFResiduals)
thisResourceMin *= partSim.resourceCapacities[type]; // Residuals as percentages, otherwise just absolute "empty".

switch ((ResourceFlowMode)this.resourceFlowModes[type])
{
case ResourceFlowMode.NO_FLOW:
if (partSim.resources[type] > SimManager.RESOURCE_MIN && partSim.resourceFlowStates[type] != 0)
if (partSim.resources[type] > thisResourceMin && partSim.resourceFlowStates[type] != 0)
{
sourcePartSet.Add(partSim);
}
Expand All @@ -411,7 +425,7 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
for (int i = 0; i < allParts.Count; i++)
{
PartSim aPartSim = allParts[i];
if (aPartSim.resources[type] > SimManager.RESOURCE_MIN && aPartSim.resourceFlowStates[type] != 0)
if (aPartSim.resources[type] > thisResourceMin && aPartSim.resourceFlowStates[type] != 0)
{
sourcePartSet.Add(aPartSim);
}
Expand All @@ -434,7 +448,7 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
var aPartSim = allParts[i];
//if (log != null) log.Append(aPartSim.name, ":" + aPartSim.partId, " contains ", aPartSim.resources[type])
// .AppendLine((aPartSim.resourceFlowStates[type] == 0) ? " (disabled)" : "");
if (aPartSim.resources[type] <= SimManager.RESOURCE_MIN || aPartSim.resourceFlowStates[type] == 0)
if (aPartSim.resources[type] <= thisResourceMin || aPartSim.resourceFlowStates[type] == 0)
{
continue;
}
Expand Down Expand Up @@ -544,6 +558,8 @@ public bool SetResourceDrains(LogMsg log, List<PartSim> allParts, List<PartSim>
.AppendLine(" to ", partSim.name, ":", partSim.partId);

partSim.resourceDrains.Add(type, amount);
if (SimManager.RFResiduals)
partSim.residuals.AddMax(type, partSim.resourceCapacities[type] * resourceMin);
drainingParts.Add(partSim);
}
}
Expand Down
11 changes: 10 additions & 1 deletion KerbalEngineer/VesselSimulator/PartSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public class PartSim {
public ResourceContainer resourceDrains = new ResourceContainer();
public ResourceContainer resourceFlowStates = new ResourceContainer();
public ResourceContainer resources = new ResourceContainer();
// For RF residual calcs
public ResourceContainer resourceCapacities = new ResourceContainer();
public ResourceContainer residuals = new ResourceContainer();
public double startMass = 0d;
public double crewMassOffset = 0d;
public String vesselName;
Expand All @@ -91,6 +94,8 @@ private static void Reset(PartSim partSim) {
partSim.resourceDrains.Reset();
partSim.resourceFlowStates.Reset();
partSim.resources.Reset();
partSim.resourceCapacities.Reset();
partSim.residuals.Reset();
partSim.parent = null;
partSim.baseCost = 0d;
partSim.baseMass = 0d;
Expand Down Expand Up @@ -180,6 +185,7 @@ public static PartSim New(Part p, int id, double atmosphere, LogMsg log) {
if (log != null) log.AppendLine(resource.resourceName, " = ", resource.amount);

partSim.resources.Add(resource.info.id, resource.amount);
partSim.resourceCapacities.Add(resource.info.id, resource.maxAmount);
partSim.resourceFlowStates.Add(resource.info.id, resource.flowState ? 1 : 0);
} else {
if (log != null) log.AppendLine(resource.resourceName, " is NaN. Skipping.");
Expand Down Expand Up @@ -814,7 +820,10 @@ public double TimeToDrainResource(LogMsg log) {
int type = resourceDrains.Types[i];

if (resourceDrains[type] > 0) {
time = Math.Min(time, resources[type] / resourceDrains[type]);
double amount = resources[type];
if (residuals.HasType(type))
amount -= residuals[type];
time = Math.Min(time, amount / resourceDrains[type]);
//if (log != null) log.AppendLine("type = " + ResourceContainer.GetResourceName(type) + " amount = " + resources[type] + " rate = " + resourceDrains[type] + " time = " + time);
}
}
Expand Down
13 changes: 13 additions & 0 deletions KerbalEngineer/VesselSimulator/ResourceContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public void Add(int type, double amount)
}
}

public void AddMax(int type, double amount)
{
if (this.resources.ContainsKey(type))
{
this.resources[type] = Math.Max(this.resources[type], amount);
}
else
{
this.resources.Add(type, amount);
this.types.Add(type);
}
}

public void Reset()
{
this.resources.Clear();
Expand Down
18 changes: 18 additions & 0 deletions KerbalEngineer/VesselSimulator/SimManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public class SimManager

public static double Mach { get; set; }

private static bool useRFResiduals = false;
public static bool RFResiduals
{
get
{
return useRFResiduals;
}
set
{
if (hasInstalledRealFuels)
useRFResiduals = value;
else
useRFResiduals = false;
}
}

public static String failMessage { get; private set; }

#endregion
Expand Down Expand Up @@ -150,6 +166,8 @@ private static void CheckForMods()
hasInstalledKIDS = true;
}
}

useRFResiduals = hasInstalledRealFuels;
log.Flush();
}

Expand Down
1 change: 1 addition & 0 deletions KerbalEngineer/VesselSimulator/Simulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ private void UpdateResourceDrains() {
// Reset the resource drains of all draining parts
foreach (PartSim partSim in drainingParts) {
partSim.resourceDrains.Reset();
partSim.residuals.Reset();
}

// Empty the draining parts set
Expand Down

0 comments on commit bf4145b

Please sign in to comment.