Skip to content

Commit

Permalink
Zero residuals before doing RCS calcs
Browse files Browse the repository at this point in the history
ModuleRCS doesn't have residuals and this was causing infinite spinning.

Note that it makes me worried there's some sort of floating point round
off error bug inherent to residuals, because it should have at least
worked and gotten the wrong answer.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Aug 4, 2023
1 parent 6c988a4 commit dbe7998
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
11 changes: 10 additions & 1 deletion MechJeb2/MechJebLib/Simulations/FuelFlowSimulation.cs
Expand Up @@ -36,6 +36,7 @@ protected override bool Run(object? o)
while (vessel.CurrentStage >= 0) // FIXME: should stop mutating vessel.CurrentStage
{
SimulateStage(vessel);
ClearResiduals(vessel);
ComputeRcsMaxValues(vessel);
FinishSegment(vessel);
vessel.Stage();
Expand Down Expand Up @@ -96,10 +97,11 @@ private void SimulateStage(SimVessel vessel)
vessel.UpdateMass();
vessel.UpdateEngineStats();
vessel.UpdateActiveEngines();
UpdateResourceDrainsAndResiduals(vessel);

GetNextSegment(vessel);
ComputeRcsMinValues(vessel);

UpdateResourceDrainsAndResiduals(vessel);
double currentThrust = vessel.ThrustMagnitude;

for (int steps = MAXSTEPS; steps > 0; steps--)
Expand All @@ -113,6 +115,7 @@ private void SimulateStage(SimVessel vessel)
// prior > 0dV segment in the same kspStage we should add those together to reduce clutter.
if (Math.Abs(vessel.ThrustMagnitude - currentThrust) > 1e-12)
{
ClearResiduals(vessel);
ComputeRcsMaxValues(vessel);
FinishSegment(vessel);
GetNextSegment(vessel);
Expand Down Expand Up @@ -234,6 +237,12 @@ private void UpdateRCSDrainsInPart(SimPart p, double resourceConsumption, int re
p.AddRCSDrain(resourceId, resourceConsumption);
}

private void ClearResiduals(SimVessel vessel)
{
foreach (SimPart part in _partsWithResourceDrains)
part.ClearResiduals();
}

private void UpdateResourceDrainsAndResiduals(SimVessel vessel)
{
foreach (SimPart part in _partsWithResourceDrains)
Expand Down
15 changes: 15 additions & 0 deletions MechJeb2/MechJebLib/Simulations/SimPart.cs
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Text;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;

namespace MechJebLib.Simulations
{
Expand Down Expand Up @@ -131,6 +132,20 @@ public void UpdateResourceResidual(double residual, int resourceId)
Resources[resourceId] = resource;
}

public void ClearResiduals()
{
_resourceKeys.Clear();
foreach (int id in Resources.Keys)
_resourceKeys.Add(id);

foreach (int id in _resourceKeys)
{
SimResource resource = Resources[id];
resource.Residual = 0;
Resources[id] = resource;
}
}

public double ResidualThreshold(int resourceId) => Resources[resourceId].ResidualThreshold + ResourceRequestRemainingThreshold;

public void ClearResourceDrains() => _resourceDrains.Clear();
Expand Down
7 changes: 5 additions & 2 deletions MechJeb2/MechJebLib/Simulations/SimResource.cs
Expand Up @@ -26,14 +26,17 @@ public double Amount
public SimResource Drain(double resourceDrain)
{
_amount -= resourceDrain;
if (_amount < 0) _amount = 0;
if (_amount < 0)
_amount = 0;

return this;
}

public SimResource RCSDrain(double rcsDrain)
{
_rcsAmount -= rcsDrain;
if (Amount < 0) _rcsAmount = _amount;
if (Amount < 0)
_rcsAmount = _amount;

return this;
}
Expand Down

0 comments on commit dbe7998

Please sign in to comment.