/
ChangeOrbitalElement.cs
221 lines (182 loc) · 8.2 KB
/
ChangeOrbitalElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
using System;
using MechJebLib.Functions;
using MechJebLib.Primitives;
using MechJebLib.Utils;
using static System.Math;
#nullable enable
namespace MechJebLib.Maneuvers
{
public static class ChangeOrbitalElement
{
private enum Type { PERIAPSIS, APOAPSIS, SMA, ECC }
private struct Args
{
public V3 P;
public V3 Q;
public double Value;
public Type Type;
}
private static void NLPFunction2(double[] x, double[] fi, double[,] jac, object obj)
{
var dv0 = new DualV3(x[0], x[1], 0, 1, 0, 0);
var dv1 = new DualV3(x[0], x[1], 0, 0, 1, 0);
var args = (Args)obj;
V3 p = args.P;
V3 q = args.Q;
double value = args.Value;
Type type = args.Type;
Dual sqM0 = dv0.sqrMagnitude;
Dual sqM1 = dv1.sqrMagnitude;
fi[0] = sqM0.M;
jac[0, 0] = sqM0.D;
jac[0, 1] = sqM1.D;
switch (type)
{
case Type.PERIAPSIS:
Dual peR0 = Astro.PeriapsisFromStateVectors(1.0, p, q + dv0) - value;
Dual peR1 = Astro.PeriapsisFromStateVectors(1.0, p, q + dv1) - value;
fi[1] = peR0.M;
jac[1, 0] = peR0.D;
jac[1, 1] = peR1.D;
break;
case Type.APOAPSIS:
Dual apR0 = 1.0 / Astro.ApoapsisFromStateVectors(1.0, p, q + dv0) - 1.0 / value;
Dual apR1 = 1.0 / Astro.ApoapsisFromStateVectors(1.0, p, q + dv1) - 1.0 / value;
fi[1] = apR0.M;
jac[1, 0] = apR0.D;
jac[1, 1] = apR1.D;
break;
case Type.SMA:
Dual sma0 = 1.0 / Astro.SmaFromStateVectors(1.0, p, q + dv0) - 1.0 / value;
Dual sma1 = 1.0 / Astro.SmaFromStateVectors(1.0, p, q + dv1) - 1.0 / value;
fi[1] = sma0.M;
jac[1, 0] = sma0.D;
jac[1, 1] = sma1.D;
break;
case Type.ECC:
Dual ecc0 = Astro.EccFromStateVectors(1.0, p, q + dv0) - value;
Dual ecc1 = Astro.EccFromStateVectors(1.0, p, q + dv1) - value;
fi[1] = ecc0.M;
jac[1, 0] = ecc0.D;
jac[1, 1] = ecc1.D;
break;
}
}
private static void NLPFunction(double[] x, double[] fi, object obj)
{
var dv = new V3(x[0], x[1], 0);
var args = (Args)obj;
V3 p = args.P;
V3 q = args.Q;
double value = args.Value;
Type type = args.Type;
fi[0] = dv.sqrMagnitude;
switch (type)
{
case Type.PERIAPSIS:
fi[1] = Astro.PeriapsisFromStateVectors(1.0, p, q + dv) - value;
break;
case Type.APOAPSIS:
fi[1] = 1.0 / Astro.ApoapsisFromStateVectors(1.0, p, q + dv) - 1.0 / value;
break;
case Type.SMA:
fi[1] = 1.0 / Astro.SmaFromStateVectors(1.0, p, q + dv) - 1.0 / value;
break;
case Type.ECC:
double ecc = Astro.EccFromStateVectors(1.0, p, q + dv);
fi[1] = ecc - value;
break;
}
}
private static V3 DeltaV(double mu, V3 r, V3 v, double value, Type type, bool optguard = false)
{
if (!Statics.IsFinite(mu))
throw new ArgumentException("bad mu in ChangeOrbitalElement");
if (!r.IsFinite())
throw new ArgumentException("bad r in ChangeOrbitalElement");
if (!v.IsFinite())
throw new ArgumentException("bad v in ChangeOrbitalElement");
const double DIFFSTEP = 1e-7;
const double EPSX = 1e-7;
const int MAXITS = 1000;
const int NVARIABLES = 2;
const int NEQUALITYCONSTRAINTS = 1;
const int NINEQUALITYCONSTRAINTS = 0;
double[] x = new double[NVARIABLES];
var scale = Scale.Create(mu, r.magnitude);
(V3 p, V3 q, Q3 rot) = Astro.PerifocalFromStateVectors(mu, r, v);
p /= scale.LengthScale;
q /= scale.VelocityScale;
if (type == Type.ECC)
{
// changing the ECC is actually a global optimization problem due to basins around parabolic ecc == 1.0
double boost = Sign(value - 1.0) * 0.1 + Sqrt(2);
V3 dv = Simple.DeltaVRelativeToCircularVelocity(1.0, p, q, boost);
x[0] = dv.x;
x[1] = dv.y;
}
else
{
x[0] = 0; // maneuver vy
x[1] = 0; // maneuver vy
}
var args = new Args { P = p, Q = q, Value = type == Type.ECC ? value : value / scale.LengthScale, Type = type };
alglib.minnlccreate(NVARIABLES, x, out alglib.minnlcstate state);
alglib.minnlcsetalgosqp(state);
alglib.minnlcsetcond(state, EPSX, MAXITS);
alglib.minnlcsetnlc(state, NEQUALITYCONSTRAINTS, NINEQUALITYCONSTRAINTS);
if (optguard)
{
alglib.minnlcoptguardsmoothness(state);
alglib.minnlcoptguardgradient(state, DIFFSTEP);
}
alglib.minnlcoptimize(state, NLPFunction2, null, args);
alglib.minnlcresults(state, out double[] x2, out alglib.minnlcreport rep);
if (rep.terminationtype < 0)
throw new Exception(
$"ChangeOrbitalElement.DeltaV({mu}, {r}, {v}, {value}, {type}): SQP solver terminated abnormally: {rep.terminationtype}"
);
if (optguard)
{
alglib.minnlcoptguardresults(state, out alglib.optguardreport ogrep);
if (ogrep.badgradsuspected || ogrep.nonc0suspected || ogrep.nonc1suspected)
throw new Exception("alglib optguard caught an error, i should report better on errors now");
}
return rot * new V3(x2[0], x2[1], 0) * scale.VelocityScale;
}
public static V3 ChangePeriapsis(double mu, V3 r, V3 v, double peR, bool optguard = false)
{
if (peR < 0 || (peR > r.magnitude) | !Statics.IsFinite(peR))
throw new ArgumentException($"Bad periapsis in ChangeOrbitalElement = {peR}");
return DeltaV(mu, r, v, peR, Type.PERIAPSIS, optguard);
}
public static V3 ChangeApoapsis(double mu, V3 r, V3 v, double apR, bool optguard = false)
{
if (!Statics.IsFinite(apR) || (apR > 0 && apR < r.magnitude))
throw new ArgumentException($"Bad apoapsis in ChangeOrbitalElement = {apR}");
return DeltaV(mu, r, v, apR, Type.APOAPSIS, optguard);
}
public static V3 ChangeApsis(double mu, V3 r, V3 v, double valueR, bool optguard = false)
{
if (!Statics.IsFinite(valueR))
throw new ArgumentException($"Bad apoapsis in ChangeOrbitalElement = {valueR}");
return valueR <= r.magnitude ? DeltaV(mu, r, v, valueR, Type.PERIAPSIS, optguard) : DeltaV(mu, r, v, valueR, Type.APOAPSIS, optguard);
}
public static V3 ChangeSMA(double mu, V3 r, V3 v, double sma, bool optguard = false)
{
if (!Statics.IsFinite(sma))
throw new ArgumentException($"Bad SMA in ChangeOrbitalElement = {sma}");
return DeltaV(mu, r, v, sma, Type.SMA, optguard);
}
public static V3 ChangeECC(double mu, V3 r, V3 v, double ecc, bool optguard = false)
{
if (!Statics.IsFinite(ecc) || ecc < 0)
throw new ArgumentException($"Bad Ecc in ChangeOrbitalElement = {ecc}");
return DeltaV(mu, r, v, ecc, Type.ECC, optguard);
}
}
}