From 0b8e2bcbdccda2488b411ce223863491f1a104b6 Mon Sep 17 00:00:00 2001 From: Sarbian Date: Sun, 15 Nov 2026 21:00:32 +0100 Subject: [PATCH] PSG: a coast the optimizer shortened to nothing is no coast With a minimum coast of zero, PSG can plan a coast of no length when burning straight through is better. Solution.WillCoast still counted it, so guidance's coast-during hold kept terminal guidance off for a coast that never came: every re-solve put the zero-length coast ahead again, and the stage burned until it was empty, far past the target orbit. WillCoast and CoastFollows now skip a coast shorter than a millionth of the trajectory. The burns either side of it are flown as one, with terminal guidance at the end of the second. --- MechJebLib/PSG/Solution.cs | 16 ++++++++--- MechJebLibTest/PSGTests/SolutionTests.cs | 35 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/MechJebLib/PSG/Solution.cs b/MechJebLib/PSG/Solution.cs index ddb880bd6..61542cb2d 100644 --- a/MechJebLib/PSG/Solution.cs +++ b/MechJebLib/PSG/Solution.cs @@ -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() diff --git a/MechJebLibTest/PSGTests/SolutionTests.cs b/MechJebLibTest/PSGTests/SolutionTests.cs index 890cd6a63..03d8c94e5 100644 --- a/MechJebLibTest/PSGTests/SolutionTests.cs +++ b/MechJebLibTest/PSGTests/SolutionTests.cs @@ -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) { @@ -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() {