Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions MechJebLib/PSG/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,24 +324,32 @@ public double TgoForKSPStage(double t, int kspStage)

public bool Coast(double t) => Phases[IndexFor(t)].Coast;

// the fraction of the whole trajectory under which a coast counts as none
private const double ZERO_COAST = 1e-6;

// A coast phase with a length. With a minimum coast of zero the optimizer may shorten the coast to nothing, when burning
// straight through is better: the burns either side of it are then one burn, and guidance must fly them as one (no
// coast to wait for, and terminal guidance at the end of the second).
private bool FlownCoast(int i) => Phases[i].Coast && _tmax[i] - _tmin[i] > ZERO_COAST * (Tf - T0);

// Specialized API to determine if we still have the coast in our future or not
// (or if we're in a coast right now)
// (or if we're in a coast right now). A coast of no length doesn't count.
public bool WillCoast(double t)
{
for (int i = IndexFor(t); i < Phases.Count; i++)
if (Phases[i].Coast)
if (FlownCoast(i))
return true;

return false;
}

// true if we're in a coast, or the phase after the current one is a coast. Unlike WillCoast this is false for a burn
// which is followed by other burns before the coast.
// which is followed by other burns before the coast. A coast of no length doesn't count.
public bool CoastFollows(double t)
{
int idx = IndexFor(t);

return Phases[idx].Coast || (idx + 1 < Phases.Count && Phases[idx + 1].Coast);
return FlownCoast(idx) || (idx + 1 < Phases.Count && FlownCoast(idx + 1));
}

public int CoastKSPStage()
Expand Down
35 changes: 35 additions & 0 deletions MechJebLibTest/PSGTests/SolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,22 @@ private static Solution CoastFirst()
new Seg(burn, 0.5, 1, 512, 256, _yHat, _yHat, 1, 1));
}

// a coast-during that the optimizer shortened to nothing: the burn before it (4096-4160 s), the coast of no length at 4160 s,
// and the precise shutdown burn after it (4160-4224 s), all in KSP stage 1
private static Solution ZeroCoastDuring()
{
Phase burn1 = NewBurn(1, 1, 1024, 768, 300, 64);
Phase coast = NewCoast(1, 1, 768);
Phase burn2 = NewBurn(1, 1, 768, 512, 300, 64);
burn2.PreciseShutdown = true;
burn2.TerminalStage = true;

return Build(
new Seg(burn1, 0, 0.5, 1024, 768, _xHat, _xHat, 1, 1),
new Seg(coast, 0.5, 0.5, 768, 768, _xHat, _xHat, 0, 0),
new Seg(burn2, 0.5, 1, 768, 512, _xHat, _xHat, 1, 1));
}

// a single burn that ends in the state (rf, vf), in m and m/s
private static Solution EndingAt(V3 rf, V3 vf)
{
Expand Down Expand Up @@ -977,6 +993,25 @@ public void CoastAndUnguidedQueries()
}
}

// a coast the optimizer shortened to nothing is no coast: guidance flies the burns either side of it as one burn, and
// its coast-during hold mustn't keep terminal guidance off for a coast that never comes
[Fact]
public void ZeroLengthCoastIsNoCoast()
{
using Solution solution = ZeroCoastDuring();

foreach (double t in new double[] { T0, 4128, 4159.9, 4160, 4192, 4224 })
{
solution.Coast(t).ShouldBeFalse();
solution.WillCoast(t).ShouldBeFalse();
solution.CoastFollows(t).ShouldBeFalse();
}

// the stage's burn time left counts both burns, so terminal guidance comes at the end of the second
solution.TgoForKSPStage(4128, 1).ShouldEqual(96, 1e-9);
solution.TerminalKSPStage().ShouldEqual(1);
}

[Fact]
public void StageQueries()
{
Expand Down
Loading