This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathperturbations.go
155 lines (142 loc) · 4.89 KB
/
perturbations.go
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
package smd
import (
"math"
"math/rand"
"time"
"github.com/gonum/matrix/mat64"
"github.com/gonum/stat/distmv"
)
// Perturbations defines how to handle perturbations during the propagation.
type Perturbations struct {
Jn uint8 // Factors to be used (only up to 4 supported)
PerturbingBody *CelestialObject // The 3rd body which is perturbating the spacecraft.
AutoThirdBody bool // Automatically determine what is the 3rd body based on distance and mass
Drag bool // Set to true to use the Spacecraft's Drag for everything including STM computation
Noise OrbitNoise
Arbitrary func(o Orbit) []float64 // Additional arbitrary pertubation.
}
func (p Perturbations) isEmpty() bool {
return p.Jn <= 1 && p.PerturbingBody == nil && p.AutoThirdBody && p.Arbitrary == nil
}
// STMSize returns the size of the STM
func (p Perturbations) STMSize() (r, c int) {
if p.Drag {
return 7, 7
}
return 6, 6
}
// Perturb returns the perturbing state vector based on the kind of propagation being used.
// For example, if using Cartesian, it'll return the impact on the R vector. If Gaussian, it'll
// return the impact on Ω, ω, ν (later for ν...).
func (p Perturbations) Perturb(o Orbit, dt time.Time, sc Spacecraft) []float64 {
pert := make([]float64, 7)
if p.isEmpty() {
return pert
}
if p.Jn > 1 && !o.Origin.Equals(Sun) {
// Ignore any Jn about the Sun
R := o.R()
x := R[0]
y := R[1]
z := R[2]
z2 := math.Pow(R[2], 2)
z3 := math.Pow(R[2], 3)
r2 := math.Pow(R[0], 2) + math.Pow(R[1], 2) + z2
r252 := math.Pow(r2, 5/2.)
r272 := math.Pow(r2, 7/2.)
// J2 (computed via SageMath: https://cloud.sagemath.com/projects/1fb6b227-1832-4f82-a05c-7e45614c00a2/files/j2perts.sagews)
accJ2 := (3 / 2.) * o.Origin.J(2) * math.Pow(o.Origin.Radius, 2) * o.Origin.μ
pert[3] += accJ2 * (5*x*z2/r272 - x/r252)
pert[4] += accJ2 * (5*y*z2/r272 - y/r252)
pert[5] += accJ2 * (5*z3/r272 - 3*z/r252)
if p.Jn >= 3 {
// J3 (computed via SageMath: https://cloud.sagemath.com/#projects/1fb6b227-1832-4f82-a05c-7e45614c00a2/files/j3perts.sagews)
r292 := math.Pow(r2, 9/2.)
z4 := math.Pow(R[2], 4)
accJ3 := o.Origin.J(3) * math.Pow(o.Origin.Radius, 3) * o.Origin.μ
pert[3] += (5 / 2.) * accJ3 * (7*x*z3/r292 - 3*x*z/r272)
pert[4] += (5 / 2.) * accJ3 * (7*y*z3/r292 - 3*y*z/r272)
pert[5] += 0.5 * accJ3 * (35*z4/r292 - 30*z2/r272 + 3/r252)
}
}
var RSunToEarth, RSunToSC, REarthToSC []float64
if p.Drag || p.PerturbingBody != nil {
REarthToSC = o.R()
RSunToEarth = MxV33(R1(Deg2rad(-Earth.tilt)), o.Origin.HelioOrbit(dt).R())
RSunToSC = make([]float64, 3)
for i := 0; i < 3; i++ {
RSunToSC[i] = RSunToEarth[i] + REarthToSC[i]
}
}
if p.Drag {
// If Drag, SRP is *also* turned on.
// TODO: Drag, there is only SRP here.
Cr := sc.Drag
S := 0.01e-6 // TODO: Idem for the Area to mass ratio
Phi := 1357.
// Build the vectors.
celerity := 2.997925e+05
srpCst := (Phi * AU * AU * S / celerity) * Cr / math.Pow(Norm(RSunToSC), 3)
for i := 0; i < 3; i++ {
pert[i+3] += -srpCst * RSunToSC[i]
}
}
if p.PerturbingBody != nil && !p.PerturbingBody.Equals(o.Origin) {
if !p.PerturbingBody.Equals(Sun) {
panic("only the Sun as a perturbing body is currently supported")
}
RSunToEarthNorm3 := math.Pow(Norm(RSunToEarth), 3)
RSunToSCNorm3 := math.Pow(Norm(RSunToSC), 3)
for i := 0; i < 3; i++ {
pert[i+3] += Sun.μ * (RSunToEarth[i]/RSunToEarthNorm3 - RSunToSC[i]/RSunToSCNorm3)
}
}
if p.Arbitrary != nil {
// Add the arbitrary perturbations
arbs := p.Arbitrary(o)
for i := 0; i < 7; i++ {
pert[i] += arbs[i]
}
}
if p.Noise != (OrbitNoise{}) {
orbitNoise := p.Noise.Generate()
for i := 0; i < 6; i++ {
pert[i] += orbitNoise[i]
}
}
return pert
}
// OrbitNoise defines a new orbit noise applied as a perturbations.
// Use case is for generating datasets for filtering.
type OrbitNoise struct {
probability float64
position *distmv.Normal
velocity *distmv.Normal
}
func (n OrbitNoise) Generate() (rtn []float64) {
rtn = make([]float64, 6)
if randFloat := rand.Float64(); n.probability < randFloat {
return
}
position := n.position.Rand(nil)
velocity := n.velocity.Rand(nil)
for i := 0; i < 3; i++ {
rtn[i] = position[i]
rtn[i+3] = velocity[i]
}
return
}
func NewOrbitNoise(probability, sigmaPosition, sigmaVelocity float64) OrbitNoise {
posMatrix := mat64.NewSymDense(3, []float64{sigmaPosition, 0, 0, 0, sigmaPosition, 0, 0, 0, sigmaPosition})
velMatrix := mat64.NewSymDense(3, []float64{sigmaVelocity, 0, 0, 0, sigmaVelocity, 0, 0, 0, sigmaVelocity})
seed := rand.New(rand.NewSource(time.Now().UnixNano()))
position, ok := distmv.NewNormal(make([]float64, 3), posMatrix, seed)
if !ok {
panic("process noise invalid")
}
velocity, ok := distmv.NewNormal(make([]float64, 3), velMatrix, seed)
if !ok {
panic("measurement noise invalid")
}
return OrbitNoise{probability, position, velocity}
}