Skip to content

Commit

Permalink
Refactor ChangeOrbitalElements slightly
Browse files Browse the repository at this point in the history
Makes for a slightly better public interface and eliminates the
case statement to check for errors

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jun 12, 2023
1 parent 747cacf commit 5cd9811
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
2 changes: 2 additions & 0 deletions MechJeb2.sln.DotSettings
Expand Up @@ -6,6 +6,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BCI/@EntryIndexedValue">BCI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BS/@EntryIndexedValue">BS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DP/@EntryIndexedValue">DP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ECC/@EntryIndexedValue">ECC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ECI/@EntryIndexedValue">ECI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ENU/@EntryIndexedValue">ENU</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FPA/@EntryIndexedValue">FPA</s:String>
Expand All @@ -22,4 +23,5 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RK/@EntryIndexedValue">RK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RO/@EntryIndexedValue">RO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SI/@EntryIndexedValue">SI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SMA/@EntryIndexedValue">SMA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SOI/@EntryIndexedValue">SOI</s:String></wpf:ResourceDictionary>
56 changes: 34 additions & 22 deletions MechJeb2/MechJebLib/Maneuvers/ChangeOrbitalElement.cs
Expand Up @@ -14,7 +14,7 @@ namespace MechJebLib.Maneuvers
{
public static class ChangeOrbitalElement
{
public enum Type { PERIAPSIS, APOAPSIS, SMA, ECC }
private enum Type { PERIAPSIS, APOAPSIS, SMA, ECC }

private struct Args
{
Expand Down Expand Up @@ -53,7 +53,7 @@ private static void NLPFunction(double[] x, double[] fi, object obj)
}
}

public static V3 DeltaV(double mu, V3 r, V3 v, double value, Type type)
private static V3 DeltaV(double mu, V3 r, V3 v, double value, Type type)
{
if (!mu.IsFinite())
throw new ArgumentException("bad mu in ChangeOrbitalElement");
Expand All @@ -62,26 +62,6 @@ public static V3 DeltaV(double mu, V3 r, V3 v, double value, Type type)
if (!v.IsFinite())
throw new ArgumentException("bad v in ChangeOrbitalElement");

switch (type)
{
case Type.PERIAPSIS:
if (value < 0 || (value > r.magnitude) | !value.IsFinite())
throw new ArgumentException($"Bad periapsis in ChangeOrbitalElement = {value}");
break;
case Type.APOAPSIS:
if (!value.IsFinite() || (value > 0 && value < r.magnitude))
throw new ArgumentException($"Bad apoapsis in ChangeOrbitalElement = {value}");
break;
case Type.SMA:
if (!value.IsFinite())
throw new ArgumentException($"Bad SMA in ChangeOrbitalElement = {value}");
break;
case Type.ECC:
if (!value.IsFinite() || value < 0)
throw new ArgumentException($"Bad Ecc in ChangeOrbitalElement = {value}");
break;
}

const double DIFFSTEP = 1e-7;
const double EPSX = 1e-15;
const int MAXITS = 1000;
Expand Down Expand Up @@ -131,5 +111,37 @@ public static V3 DeltaV(double mu, V3 r, V3 v, double value, Type type)

return rot * new V3(x[0], x[1], 0) * scale.VelocityScale;
}

public static V3 ChangePeriapsis(double mu, V3 r, V3 v, double peR)
{
if (peR < 0 || (peR > r.magnitude) | !peR.IsFinite())
throw new ArgumentException($"Bad periapsis in ChangeOrbitalElement = {peR}");

return DeltaV(mu, r, v, peR, Type.PERIAPSIS);
}

public static V3 ChangeApoapsis(double mu, V3 r, V3 v, double apR)
{
if (!apR.IsFinite() || (apR > 0 && apR < r.magnitude))
throw new ArgumentException($"Bad apoapsis in ChangeOrbitalElement = {apR}");

return DeltaV(mu, r, v, apR, Type.APOAPSIS);
}

public static V3 ChangeSMA(double mu, V3 r, V3 v, double sma)
{
if (!sma.IsFinite())
throw new ArgumentException($"Bad SMA in ChangeOrbitalElement = {sma}");

return DeltaV(mu, r, v, sma, Type.SMA);
}

public static V3 ChangeECC(double mu, V3 r, V3 v, double ecc)
{
if (!ecc.IsFinite() || ecc < 0)
throw new ArgumentException($"Bad Ecc in ChangeOrbitalElement = {ecc}");

return DeltaV(mu, r, v, ecc, Type.ECC);
}
}
}
2 changes: 1 addition & 1 deletion MechJeb2/MechJebLib/Maneuvers/ReturnFromMoon.cs
Expand Up @@ -129,7 +129,7 @@ private static void NLPFunction(double[] x, double[] fi, object obj)
if (ecc < 1)
{
// do the planet first which is analogous to heliocentric in a transfer
V3 v1 = ChangeOrbitalElement.DeltaV(centralMu, moonR0, moonV0, peR, ChangeOrbitalElement.Type.PERIAPSIS);
V3 v1 = ChangeOrbitalElement.ChangePeriapsis(centralMu, moonR0, moonV0, peR);

// then do the source moon SOI
V3 vneg, vpos, rburn;
Expand Down
24 changes: 12 additions & 12 deletions MechJeb2/OrbitalManeuverCalculator.cs
Expand Up @@ -64,7 +64,7 @@ public static Vector3d DeltaVToChangePeriapsis(Orbit o, double ut, double newPeR

(V3 r, V3 v) = o.RightHandedStateVectorsAtUT(ut);

V3 dv = ChangeOrbitalElement.DeltaV(o.referenceBody.gravParameter, r, v, newPeR, ChangeOrbitalElement.Type.PERIAPSIS);
V3 dv = ChangeOrbitalElement.ChangePeriapsis(o.referenceBody.gravParameter, r, v, newPeR);

return dv.V3ToWorld();
}
Expand All @@ -80,7 +80,7 @@ public static Vector3d DeltaVToChangeApoapsis(Orbit o, double ut, double newApR)

(V3 r, V3 v) = o.RightHandedStateVectorsAtUT(ut);

V3 dv = ChangeOrbitalElement.DeltaV(o.referenceBody.gravParameter, r, v, newApR, ChangeOrbitalElement.Type.APOAPSIS);
V3 dv = ChangeOrbitalElement.ChangeApoapsis(o.referenceBody.gravParameter, r, v, newApR);

return dv.V3ToWorld();
}
Expand All @@ -92,7 +92,16 @@ public static Vector3d DeltaVToChangeEccentricity(Orbit o, double ut, double new

(V3 r, V3 v) = o.RightHandedStateVectorsAtUT(ut);

V3 dv = ChangeOrbitalElement.DeltaV(o.referenceBody.gravParameter, r, v, newEcc, ChangeOrbitalElement.Type.ECC);
V3 dv = ChangeOrbitalElement.ChangeECC(o.referenceBody.gravParameter, r, v, newEcc);

return dv.V3ToWorld();
}

public static Vector3d DeltaVForSemiMajorAxis(Orbit o, double ut, double newSMA)
{
(V3 r, V3 v) = o.RightHandedStateVectorsAtUT(ut);

V3 dv = ChangeOrbitalElement.ChangeSMA(o.referenceBody.gravParameter, r, v, newSMA);

return dv.V3ToWorld();
}
Expand Down Expand Up @@ -958,15 +967,6 @@ public static Vector3d DeltaVToShiftLAN(Orbit o, double UT, double newLAN)
return desiredHorizontalVelocity - actualHorizontalVelocity;
}

public static Vector3d DeltaVForSemiMajorAxis(Orbit o, double ut, double newSMA)
{
(V3 r, V3 v) = o.RightHandedStateVectorsAtUT(ut);

V3 dv = ChangeOrbitalElement.DeltaV(o.referenceBody.gravParameter, r, v, newSMA, ChangeOrbitalElement.Type.SMA);

return dv.V3ToWorld();
}

public static Vector3d DeltaVToShiftNodeLongitude(Orbit o, double UT, double newNodeLong)
{
// Get the location underneath the burn location at the current moment.
Expand Down
12 changes: 6 additions & 6 deletions MechJebLibTest/Maths/FunctionsTests.cs
Expand Up @@ -805,21 +805,21 @@ private void ChangeOrbitalElementTest()
newR *= rscale;
double mu = rscale * vscale * vscale;

V3 dv = ChangeOrbitalElement.DeltaV(mu, r, v, newR, ChangeOrbitalElement.Type.PERIAPSIS);
V3 dv = ChangeOrbitalElement.ChangePeriapsis(mu, r, v, newR);
MechJebLib.Core.Maths.PeriapsisFromStateVectors(mu, r, v + dv).ShouldEqual(newR, 1e-9);

// validate this API works left handed.
V3 dv2 = ChangeOrbitalElement.DeltaV(mu, r.xzy, v.xzy, newR, ChangeOrbitalElement.Type.PERIAPSIS);
V3 dv2 = ChangeOrbitalElement.ChangePeriapsis(mu, r.xzy, v.xzy, newR);
dv2.ShouldEqual(dv.xzy, 1e-3);

// now test apoapsis changing to elliptical
newR = random.NextDouble() * rscale * 1e9 + r.magnitude;
V3 dv3 = ChangeOrbitalElement.DeltaV(mu, r, v, newR, ChangeOrbitalElement.Type.APOAPSIS);
V3 dv3 = ChangeOrbitalElement.ChangeApoapsis(mu, r, v, newR);
MechJebLib.Core.Maths.ApoapsisFromStateVectors(mu, r, v + dv3).ShouldEqual(newR, 1e-5);

// now test apoapsis changing to hyperbolic
newR = -(random.NextDouble() * 1e9 + 1e3) * rscale;
V3 dv4 = ChangeOrbitalElement.DeltaV(mu, r, v, newR, ChangeOrbitalElement.Type.APOAPSIS);
V3 dv4 = ChangeOrbitalElement.ChangeApoapsis(mu, r, v, newR);
MechJebLib.Core.Maths.ApoapsisFromStateVectors(mu, r, v + dv4).ShouldEqual(newR, 1e-5);

// now test changing the SMA.
Expand All @@ -830,12 +830,12 @@ private void ChangeOrbitalElementTest()
v = new V3(23370.6359939819, 12558.3695536628, 17864.1347044172);
newR = 35354091.544774853;
*/
V3 dv5 = ChangeOrbitalElement.DeltaV(mu, r, v, newR, ChangeOrbitalElement.Type.SMA);
V3 dv5 = ChangeOrbitalElement.ChangeSMA(mu, r, v, newR);
MechJebLib.Core.Maths.SmaFromStateVectors(mu, r, v + dv5).ShouldEqual(newR, 1e-9);

// now test changing the Ecc
double newEcc = random.NextDouble() * 5 + 2.5;
V3 dv6 = ChangeOrbitalElement.DeltaV(mu, r, v, newEcc, ChangeOrbitalElement.Type.ECC);
V3 dv6 = ChangeOrbitalElement.ChangeECC(mu, r, v, newEcc);
(_, double ecc) = MechJebLib.Core.Maths.SmaEccFromStateVectors(mu, r, v + dv6);
ecc.ShouldEqual(newEcc, 1e-9);
}
Expand Down

0 comments on commit 5cd9811

Please sign in to comment.