pyastropdl 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. pyastropdl/__init__.py +5 -0
  2. pyastropdl/core/__init__.py +3 -0
  3. pyastropdl/core/common/__init__.py +23 -0
  4. pyastropdl/core/common/constants.py +10 -0
  5. pyastropdl/core/common/interpolation.py +26 -0
  6. pyastropdl/core/common/root_finding.py +47 -0
  7. pyastropdl/core/simulation_engine/__init__.py +0 -0
  8. pyastropdl/core/simulation_engine/dynamic_g/__init__.py +27 -0
  9. pyastropdl/core/simulation_engine/dynamic_g/engine.py +216 -0
  10. pyastropdl/core/simulation_engine/dynamic_g/integrators.py +90 -0
  11. pyastropdl/core/simulation_engine/dynamic_g/physics.py +55 -0
  12. pyastropdl/core/simulation_engine/dynamic_g/residuals.py +73 -0
  13. pyastropdl/core/simulation_engine/dynamic_g/root_finding.py +62 -0
  14. pyastropdl/core/simulation_engine/static_g/__init__.py +26 -0
  15. pyastropdl/core/simulation_engine/static_g/engine.py +197 -0
  16. pyastropdl/core/simulation_engine/static_g/integrators.py +51 -0
  17. pyastropdl/core/simulation_engine/static_g/physics.py +48 -0
  18. pyastropdl/core/simulation_engine/static_g/residuals.py +72 -0
  19. pyastropdl/core/simulation_engine/static_g/root_finding.py +55 -0
  20. pyastropdl/core/solver.py +106 -0
  21. pyastropdl/core/solvers/__init__.py +3 -0
  22. pyastropdl/core/solvers/shooting_solver.py +79 -0
  23. pyastropdl/core/utils/__init__.py +55 -0
  24. pyastropdl/core/utils/paths.py +31 -0
  25. pyastropdl/core/utils/tools.py +31 -0
  26. pyastropdl/core/utils/types.py +41 -0
  27. pyastropdl/visualization/__init__.py +4 -0
  28. pyastropdl/visualization/animator.py +243 -0
  29. pyastropdl/visualization/components/__init__.py +6 -0
  30. pyastropdl/visualization/components/annotations.py +39 -0
  31. pyastropdl/visualization/components/hud.py +193 -0
  32. pyastropdl/visualization/components/plots.py +49 -0
  33. pyastropdl/visualization/components/ticks.py +71 -0
  34. pyastropdl/visualization/components/zoom.py +34 -0
  35. pyastropdl/visualization/plotter.py +255 -0
  36. pyastropdl/visualization/style/__init__.py +23 -0
  37. pyastropdl/visualization/style/axes.py +104 -0
  38. pyastropdl/visualization/style/colors.py +118 -0
  39. pyastropdl/visualization/style/fonts/Geist/static/Geist-Bold.ttf +0 -0
  40. pyastropdl/visualization/style/fonts/Geist/static/Geist-Medium.ttf +0 -0
  41. pyastropdl/visualization/style/fonts/Geist/static/Geist-Regular.ttf +0 -0
  42. pyastropdl/visualization/style/fonts/IBM_Plex_Mono/IBMPlexMono-Bold.ttf +0 -0
  43. pyastropdl/visualization/style/fonts/IBM_Plex_Mono/IBMPlexMono-Medium.ttf +0 -0
  44. pyastropdl/visualization/style/fonts/IBM_Plex_Mono/IBMPlexMono-Regular.ttf +0 -0
  45. pyastropdl/visualization/style/fonts/__init__.py +3 -0
  46. pyastropdl/visualization/style/fonts/fonts.py +141 -0
  47. pyastropdl/visualization/style/themes.py +64 -0
  48. pyastropdl-0.9.0.dist-info/METADATA +216 -0
  49. pyastropdl-0.9.0.dist-info/RECORD +52 -0
  50. pyastropdl-0.9.0.dist-info/WHEEL +5 -0
  51. pyastropdl-0.9.0.dist-info/licenses/LICENSE +21 -0
  52. pyastropdl-0.9.0.dist-info/top_level.txt +1 -0
pyastropdl/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from pyastropdl.core.solver import Solver
2
+ from pyastropdl.visualization.animator import Animator
3
+ from pyastropdl.visualization.plotter import Plotter
4
+
5
+ __all__ = ["Animator", "Plotter", "Solver"]
@@ -0,0 +1,3 @@
1
+ from .solver import Solver
2
+
3
+ __all__ = ["Solver"]
@@ -0,0 +1,23 @@
1
+ from .constants import (
2
+ G_EARTH,
3
+ G_MOON,
4
+ GM_MOON,
5
+ MAX_ITERATIONS_STEPS,
6
+ MAX_RESIDUALS_STEPS,
7
+ MAX_SIMULATION_STEPS,
8
+ R_MOON,
9
+ )
10
+ from .interpolation import lerp_inplace
11
+ from .root_finding import halley_method
12
+
13
+ __all__ = [
14
+ "GM_MOON",
15
+ "G_EARTH",
16
+ "G_MOON",
17
+ "MAX_ITERATIONS_STEPS",
18
+ "MAX_RESIDUALS_STEPS",
19
+ "MAX_SIMULATION_STEPS",
20
+ "R_MOON",
21
+ "halley_method",
22
+ "lerp_inplace"
23
+ ]
@@ -0,0 +1,10 @@
1
+
2
+ MAX_SIMULATION_STEPS = 250_000
3
+ MAX_RESIDUALS_STEPS = 250_000
4
+ MAX_ITERATIONS_STEPS = 100
5
+
6
+ G_MOON = 1.6234
7
+ G_EARTH = 9.80665
8
+
9
+ GM_MOON = 4.902800118e12
10
+ R_MOON = 1737400.0
@@ -0,0 +1,26 @@
1
+ from numba import njit
2
+
3
+ from pyastropdl.core.utils.types import StateVector, f64_1d, i64, void
4
+
5
+
6
+ @njit(void(f64_1d, f64_1d, f64_1d, i64), cache=True, fastmath=True)
7
+ def lerp_inplace(
8
+ curr_state: StateVector,
9
+ prev_state: StateVector,
10
+ end_state: StateVector,
11
+ mode: int
12
+ ) -> None:
13
+
14
+ # mode: 0 - velocity, 1 - altitude
15
+ theta = 0.0
16
+
17
+ match mode:
18
+ case 0:
19
+ dv = curr_state[1] - prev_state[1]
20
+ theta = (end_state[0] - prev_state[1]) / dv if abs(dv) > 1e-9 else 0.0
21
+ case 1:
22
+ dh = curr_state[2] - prev_state[2]
23
+ theta = (end_state[1] - prev_state[2]) / dh if abs(dh) > 1e-9 else 0.0
24
+
25
+ for i in range(4):
26
+ curr_state[i] = prev_state[i] + theta * (curr_state[i] - prev_state[i])
@@ -0,0 +1,47 @@
1
+ import numpy as np
2
+ from numpy import nan
3
+
4
+
5
+ def halley_method(
6
+ func,
7
+ func_args: tuple,
8
+ t_bounds: tuple,
9
+ eps: float = 1e-5,
10
+ max_iter: int = 100,
11
+ ) -> float:
12
+
13
+ t_min, t_max = t_bounds
14
+ ti = t_max
15
+
16
+ for _ in range(max_iter):
17
+
18
+ f_val = func(ti, func_args)
19
+ if abs(f_val) <= eps:
20
+ return ti
21
+
22
+ f_left = func(ti - eps, func_args)
23
+ f_right = func(ti + eps, func_args)
24
+
25
+ f_dx = (f_right - f_left) / (2.0 * eps)
26
+ f_ddx = (f_right - 2.0 * f_val + f_left) / (eps**2)
27
+
28
+ denominator = 2.0 * (f_dx**2) - f_val * f_ddx
29
+
30
+
31
+ if abs(denominator) < 1e-12:
32
+ return nan
33
+
34
+ ti += -(2.0 * f_val * f_dx) / denominator
35
+
36
+ if np.isnan(ti):
37
+ return nan
38
+
39
+ if ti >= t_max:
40
+ ti = t_max - eps
41
+ t_max -= 0.1
42
+ elif ti <= t_min:
43
+ ti = t_min + eps
44
+ t_min += 0.1
45
+
46
+ return nan
47
+
File without changes
@@ -0,0 +1,27 @@
1
+ from .engine import (
2
+ get_ff_phase,
3
+ get_start_cond,
4
+ get_terminal_state,
5
+ get_time_boundary,
6
+ get_trajectory,
7
+ )
8
+ from .integrators import rk4_bp_inplace, rk4_ffp_inplace
9
+ from .physics import braking_ode, free_fall_ode, get_g
10
+ from .residuals import altitude_residual, residual_func
11
+ from .root_finding import halley_method
12
+
13
+ __all__ = [
14
+ "altitude_residual",
15
+ "braking_ode",
16
+ "free_fall_ode",
17
+ "get_ff_phase",
18
+ "get_g",
19
+ "get_start_cond",
20
+ "get_terminal_state",
21
+ "get_time_boundary",
22
+ "get_trajectory",
23
+ "halley_method",
24
+ "residual_func",
25
+ "rk4_bp_inplace",
26
+ "rk4_ffp_inplace"
27
+ ]
@@ -0,0 +1,216 @@
1
+ import numpy as np
2
+ from numba import njit
3
+
4
+ from pyastropdl.core.common.constants import MAX_SIMULATION_STEPS
5
+ from pyastropdl.core.common.interpolation import lerp_inplace
6
+ from pyastropdl.core.utils import (
7
+ FloatArray,
8
+ StateVector,
9
+ f64,
10
+ f64_1d,
11
+ f64_1d_bool,
12
+ f64_2d,
13
+ f64_2d_bool,
14
+ f64_tuple2,
15
+ f64_tuple3,
16
+ i64,
17
+ )
18
+
19
+ from .integrators import rk4_bp_inplace, rk4_ffp_inplace
20
+ from .physics import get_g
21
+
22
+
23
+ @njit(f64_1d_bool(f64, f64_2d, f64), cache=True, fastmath=True)
24
+ def get_start_cond(
25
+ ts: float,
26
+ free_fall_phase: FloatArray,
27
+ dt: float
28
+ ) -> tuple[StateVector, bool]:
29
+
30
+ max_cols = free_fall_phase.shape[1]
31
+ n = int(ts // dt)
32
+
33
+ res = np.empty(3, dtype=np.float64)
34
+
35
+ if n < 0 or n >= max_cols - 1:
36
+ return (res, False)
37
+
38
+ theta = (ts - n * dt) / dt
39
+
40
+ res[0] = free_fall_phase[0, n] + theta * (free_fall_phase[0, n + 1] - free_fall_phase[0, n])
41
+ res[1] = free_fall_phase[1, n] + theta * (free_fall_phase[1, n + 1] - free_fall_phase[1, n])
42
+ res[2] = free_fall_phase[2, n] + theta * (free_fall_phase[2, n + 1] - free_fall_phase[2, n])
43
+
44
+ return (res, True)
45
+
46
+
47
+ @njit(f64_2d_bool(f64_1d, f64), cache=True, fastmath=True)
48
+ def get_ff_phase(
49
+ init_state: StateVector,
50
+ dt: float
51
+ ) -> tuple[FloatArray, bool]:
52
+
53
+ free_fall_phase = np.zeros((MAX_SIMULATION_STEPS, 4), dtype=np.float64).T
54
+
55
+ curr_state = np.empty(4, dtype=np.float64)
56
+ prev_state = np.empty(4, dtype=np.float64)
57
+
58
+ curr_state[:3] = init_state[:3]
59
+ curr_state[3] = 0.0
60
+
61
+ free_fall_phase[:, 0] = curr_state
62
+
63
+ for curr_shift in range(1, MAX_SIMULATION_STEPS):
64
+ prev_state, curr_state = curr_state, prev_state
65
+
66
+ g = get_g(prev_state[2])
67
+ rk4_ffp_inplace(curr_state, prev_state, g, dt)
68
+
69
+ if curr_state[2] * prev_state[2] <= 0:
70
+ end_state = np.array([0, 0], dtype=np.float64)
71
+ lerp_inplace(
72
+ curr_state, prev_state, end_state, 1
73
+ )
74
+ free_fall_phase[:, curr_shift] = curr_state
75
+ return (free_fall_phase[:, : curr_shift + 1], True)
76
+
77
+ free_fall_phase[:, curr_shift] = curr_state
78
+
79
+ free_fall_phase[:, curr_shift] = curr_state
80
+ return (free_fall_phase[:, : curr_shift + 1], False)
81
+
82
+
83
+ @njit(f64_1d_bool(f64, f64_2d, f64_1d, f64_tuple3, i64, f64), cache=True, fastmath=True)
84
+ def get_terminal_state(
85
+ ts: float,
86
+ free_fall_phase:FloatArray,
87
+ end_state: StateVector,
88
+ phys_params: tuple,
89
+ mode: int,
90
+ dt: float
91
+ ) -> tuple[StateVector, bool]:
92
+
93
+ drym = phys_params[2]
94
+
95
+ curr_state = np.empty(4, dtype=np.float64)
96
+ prev_state = np.empty(4, dtype=np.float64)
97
+
98
+ start_cond, valid = get_start_cond(ts, free_fall_phase, dt)
99
+ if not valid:
100
+ return (curr_state, False)
101
+
102
+ curr_state[:3] = start_cond
103
+ curr_state[3] = ts
104
+
105
+ for _ in range(MAX_SIMULATION_STEPS):
106
+ prev_state, curr_state = curr_state, prev_state
107
+
108
+ g = get_g(prev_state[2])
109
+ rk4_bp_inplace(curr_state, prev_state, phys_params, g, dt)
110
+
111
+ if mode == 0:
112
+ if (curr_state[1] - end_state[0]) * (prev_state[1] - end_state[0]) <= 0:
113
+ lerp_inplace(curr_state, prev_state, end_state, mode)
114
+ return (curr_state, True)
115
+ elif mode == 1:
116
+ if (curr_state[2] - end_state[1]) * (prev_state[2] - end_state[1]) <= 0:
117
+ lerp_inplace(curr_state, prev_state, end_state, mode)
118
+ return (curr_state, True)
119
+
120
+ elif curr_state[2] < 0 and curr_state[0] <= drym:
121
+ return (curr_state, False)
122
+
123
+ return (curr_state, False)
124
+
125
+
126
+ @njit(f64_2d_bool(f64, f64_2d, f64_1d, f64_tuple3, i64, f64), cache=True, fastmath=True)
127
+ def get_trajectory(
128
+ ts: float,
129
+ free_fall_phase: FloatArray,
130
+ end_state: StateVector,
131
+ phys_params: tuple,
132
+ mode: int,
133
+ dt: float
134
+ ) -> tuple[FloatArray, bool]:
135
+
136
+ drym = phys_params[2]
137
+
138
+ state_history = np.zeros((MAX_SIMULATION_STEPS, 4), dtype=np.float64).T
139
+
140
+ curr_state = np.empty(4, dtype=np.float64)
141
+ prev_state = np.empty(4, dtype=np.float64)
142
+
143
+ start_cond, valid = get_start_cond(ts, free_fall_phase, dt)
144
+ if not valid:
145
+ return (state_history[:, :1], False)
146
+
147
+ curr_state[:3] = start_cond
148
+ curr_state[3] = ts
149
+
150
+ state_history[:, 0] = curr_state
151
+
152
+ for curr_shift in range(1, MAX_SIMULATION_STEPS):
153
+ prev_state, curr_state = curr_state, prev_state
154
+
155
+ g = get_g(prev_state[2])
156
+ rk4_bp_inplace(curr_state, prev_state, phys_params, g, dt)
157
+
158
+ if mode == 0:
159
+ if (curr_state[1] - end_state[0]) * (prev_state[1] - end_state[0]) <= 0:
160
+ lerp_inplace(curr_state, prev_state, end_state, mode)
161
+ state_history[:, curr_shift] = curr_state
162
+ return (state_history[:, : curr_shift + 1], True)
163
+ elif mode == 1:
164
+ if (curr_state[2] - end_state[1]) * (prev_state[2] - end_state[1]) <= 0:
165
+ lerp_inplace(curr_state, prev_state, end_state, mode)
166
+ state_history[:, curr_shift] = curr_state
167
+ return (state_history[:, : curr_shift + 1], True)
168
+
169
+ elif curr_state[2] < 0 and curr_state[0] <= drym:
170
+ state_history[:, curr_shift] = curr_state
171
+ return (state_history[:, : curr_shift + 1], False)
172
+
173
+ state_history[:, curr_shift] = curr_state
174
+
175
+ state_history[:, curr_shift] = curr_state
176
+ return (state_history[:, : curr_shift + 1], False)
177
+
178
+
179
+ @njit(f64_tuple2(f64_1d, f64_1d, f64_tuple3, f64_2d, f64), cache=True)
180
+ def get_time_boundary(
181
+ init_state: StateVector,
182
+ end_state: StateVector,
183
+ phys_params: tuple,
184
+ free_fall_phase: FloatArray,
185
+ dt: float
186
+ ) -> tuple[float, float]:
187
+
188
+ mask = free_fall_phase[1, :] <= 0
189
+ idx = np.flatnonzero(mask)
190
+
191
+ if idx.size == 0:
192
+ return (np.nan, np.nan)
193
+
194
+ t_min = free_fall_phase[3, idx[0]]
195
+ impact_time = free_fall_phase[3, idx[-1]]
196
+ t_max = impact_time
197
+
198
+ terminal_state, valid = get_terminal_state(
199
+ impact_time, free_fall_phase, end_state, phys_params, 0, dt
200
+ )
201
+
202
+ if not valid:
203
+ v0, h0 = init_state[1], init_state[2]
204
+ start_v, end_v = free_fall_phase[1, idx[-1]], terminal_state[1]
205
+ dv = end_v - start_v
206
+ g = get_g(h0 / 2)
207
+ t_max = (v0 + dv) / g
208
+
209
+ terminal_state, valid = get_terminal_state(
210
+ t_max, free_fall_phase, end_state, phys_params, 0, dt
211
+ )
212
+
213
+ if not valid or terminal_state[2] > 0:
214
+ return (t_min, np.nan)
215
+
216
+ return (t_min, t_max)
@@ -0,0 +1,90 @@
1
+ import numpy as np
2
+ from numba import njit
3
+
4
+ from pyastropdl.core.utils.types import StateVector, f64, f64_1d, f64_tuple3, void
5
+
6
+ from .physics import braking_ode, free_fall_ode
7
+
8
+
9
+ @njit(void(f64_1d, f64_1d, f64_tuple3, f64, f64), cache=True, fastmath=True)
10
+ def rk4_bp_inplace(
11
+ curr_state: StateVector,
12
+ prev_state: StateVector,
13
+ phys_params: tuple,
14
+ g: np.float64,
15
+ dt: float,
16
+ ) -> None:
17
+
18
+ # phys_params mapping: (thrust, k, drym) = (phys_params[0], phys_params[1], phys_params[2])
19
+ # state mapping: (m, v, h) = (_state[0], _state[1], _state[2])
20
+
21
+ m_dot = phys_params[0] * phys_params[1]
22
+ fuel_remaining = prev_state[0] - phys_params[2]
23
+
24
+ if 0.0 < fuel_remaining < m_dot * dt:
25
+ dt = fuel_remaining / m_dot
26
+
27
+ dt_half = dt * 0.5
28
+
29
+ k10, k11, k12 = braking_ode(prev_state, phys_params, g)
30
+
31
+ curr_state[0] = prev_state[0] + dt_half * k10
32
+ curr_state[1] = prev_state[1] + dt_half * k11
33
+ curr_state[2] = prev_state[2] + dt_half * k12
34
+ k20, k21, k22 = braking_ode(curr_state, phys_params, g)
35
+
36
+ curr_state[0] = prev_state[0] + dt_half * k20
37
+ curr_state[1] = prev_state[1] + dt_half * k21
38
+ curr_state[2] = prev_state[2] + dt_half * k22
39
+ k30, k31, k32 = braking_ode(curr_state, phys_params, g)
40
+
41
+ curr_state[0] = prev_state[0] + dt * k30
42
+ curr_state[1] = prev_state[1] + dt * k31
43
+ curr_state[2] = prev_state[2] + dt * k32
44
+ k40, k41, k42 = braking_ode(curr_state, phys_params, g)
45
+
46
+ inv_six = 1.0/6.0
47
+
48
+ curr_state[0] = prev_state[0] + dt * (k10 + 2.0 * k20 + 2.0 * k30 + k40) * inv_six
49
+ curr_state[1] = prev_state[1] + dt * (k11 + 2.0 * k21 + 2.0 * k31 + k41) * inv_six
50
+ curr_state[2] = prev_state[2] + dt * (k12 + 2.0 * k22 + 2.0 * k32 + k42) * inv_six
51
+ curr_state[3] = prev_state[3] + dt
52
+
53
+
54
+ @njit(void(f64_1d, f64_1d, f64, f64), cache=True, fastmath=True)
55
+ def rk4_ffp_inplace(
56
+ curr_state: StateVector,
57
+ prev_state: StateVector,
58
+ g: np.float64,
59
+ dt: float,
60
+ ) -> None:
61
+
62
+ # state mapping: (m, v, h) = (_state[0], _state[1], _state[2])
63
+
64
+ dt_half = dt * 0.5
65
+
66
+ k10, k11, k12 = free_fall_ode(prev_state, g)
67
+
68
+ curr_state[0] = prev_state[0] + dt_half * k10
69
+ curr_state[1] = prev_state[1] + dt_half * k11
70
+ curr_state[2] = prev_state[2] + dt_half * k12
71
+ k20, k21, k22 = free_fall_ode(curr_state, g)
72
+
73
+ curr_state[0] = prev_state[0] + dt_half * k20
74
+ curr_state[1] = prev_state[1] + dt_half * k21
75
+ curr_state[2] = prev_state[2] + dt_half * k22
76
+ k30, k31, k32 = free_fall_ode(curr_state, g)
77
+
78
+ curr_state[0] = prev_state[0] + dt * k30
79
+ curr_state[1] = prev_state[1] + dt * k31
80
+ curr_state[2] = prev_state[2] + dt * k32
81
+ k40, k41, k42 = free_fall_ode(curr_state, g)
82
+
83
+ inv_six = 1.0/6.0
84
+
85
+ curr_state[0] = prev_state[0] + dt * (k10 + 2.0 * k20 + 2.0 * k30 + k40) * inv_six
86
+ curr_state[1] = prev_state[1] + dt * (k11 + 2.0 * k21 + 2.0 * k31 + k41) * inv_six
87
+ curr_state[2] = prev_state[2] + dt * (k12 + 2.0 * k22 + 2.0 * k32 + k42) * inv_six
88
+ curr_state[3] = prev_state[3] + dt
89
+
90
+
@@ -0,0 +1,55 @@
1
+ import numpy as np
2
+ from numba import njit
3
+
4
+ from pyastropdl.core.common.constants import GM_MOON, R_MOON
5
+ from pyastropdl.core.utils.types import StateVector, f64, f64_1d, f64_tuple3
6
+
7
+
8
+ @njit(f64(f64), cache=True, fastmath=True)
9
+ def get_g(h: np.float64 = 0.0) -> np.float64:
10
+
11
+ if h <= 0.0:
12
+ return GM_MOON / (R_MOON * R_MOON)
13
+
14
+ r_total = R_MOON + h
15
+ return GM_MOON / (r_total * r_total)
16
+
17
+
18
+ @njit(f64_tuple3(f64_1d, f64), cache=True, inline="always")
19
+ def free_fall_ode(
20
+ state: StateVector, g: np.float64
21
+ ) -> tuple[float, float, float]:
22
+
23
+ # state mapping: (m, v, h) = (state[0], state[1], state[2])
24
+
25
+ # free fall differential system state mapping:
26
+ # dm_dt = 0.0
27
+ # dv_dt = -g
28
+ # dh_dt = v
29
+
30
+ return (0.0, -g, state[1])
31
+
32
+
33
+ @njit(
34
+ f64_tuple3(f64_1d, f64_tuple3, f64), cache=True, fastmath=True, inline="always"
35
+ )
36
+ def braking_ode(
37
+ state: StateVector, phys_params: tuple, g: np.float64
38
+ ) -> tuple[float, float, float]:
39
+
40
+ # phys_params mapping: (thrust, k, drym) = (phys_params[0], phys_params[1], phys_params[2])
41
+ # state mapping: (m, v, h) = (state[0], state[1], state[2])
42
+
43
+ if state[0] > phys_params[2]:
44
+ # dh_dt = v -> state[1]
45
+ # dv_dt = (thrust / m) - g -> (phys_params[0] / state[0]) - g
46
+ # dm_dt = -thrust * k -> -phys_params[0] * phys_params[1]
47
+ return (
48
+ -phys_params[0] * phys_params[1],
49
+ (phys_params[0] / state[0]) - g,
50
+ state[1],
51
+ )
52
+ else:
53
+ return (0.0, -g, state[1])
54
+
55
+
@@ -0,0 +1,73 @@
1
+ import numpy as np
2
+ from numba import njit
3
+
4
+ from pyastropdl.core.common.constants import MAX_RESIDUALS_STEPS
5
+ from pyastropdl.core.utils.types import (
6
+ FloatArray,
7
+ StateVector,
8
+ f64,
9
+ f64_1d,
10
+ f64_2d,
11
+ f64_tuple3,
12
+ i64,
13
+ )
14
+
15
+ from .engine import get_terminal_state
16
+
17
+
18
+ @njit(f64_2d(f64_2d, f64_1d, f64_tuple3, f64, i64), cache=True)
19
+ def residual_func(
20
+ free_fall_phase: FloatArray,
21
+ end_state: StateVector,
22
+ phys_params: tuple,
23
+ dt: float,
24
+ mode: int,
25
+ ) -> FloatArray:
26
+
27
+ # mode: 0 - altitude residual (h_terminal - h_target)
28
+ # mode: 1 - velocity residual (v_terminal - v_target)
29
+
30
+ func_state = np.empty((MAX_RESIDUALS_STEPS, 2), dtype=np.float64).T
31
+ func_state[0, 0] = np.nan
32
+ has_started = False
33
+ curr_ts: float = 0.0
34
+ curr_shift: int = 0
35
+
36
+ for _ in range(MAX_RESIDUALS_STEPS):
37
+ terminal_state, valid = get_terminal_state(
38
+ curr_ts, free_fall_phase, end_state, phys_params, mode, dt
39
+ )
40
+
41
+ if not valid:
42
+ if has_started:
43
+ break
44
+
45
+ else:
46
+ residual = terminal_state[2 - mode] - end_state[1 - mode]
47
+ has_started = True
48
+ func_state[0, curr_shift] = curr_ts
49
+ func_state[1, curr_shift] = residual
50
+ curr_shift += 1
51
+
52
+ curr_ts += dt
53
+
54
+ return func_state[:, :curr_shift]
55
+
56
+
57
+ @njit(f64(f64, f64_2d, f64_1d, f64_tuple3, f64), cache=True)
58
+ def altitude_residual(
59
+ x: float,
60
+ free_fall_phase: FloatArray,
61
+ end_state: StateVector,
62
+ phys_params: tuple[float, float, float],
63
+ dt: float,
64
+ ) -> float:
65
+
66
+ terminal_state, valid = get_terminal_state(
67
+ x, free_fall_phase, end_state, phys_params, mode=0, dt=dt
68
+ )
69
+
70
+ if not valid:
71
+ return np.nan
72
+
73
+ return terminal_state[2] - end_state[1]
@@ -0,0 +1,62 @@
1
+ import numpy as np
2
+ from numba import njit
3
+ from numpy import nan
4
+
5
+ from pyastropdl.core.common.constants import MAX_ITERATIONS_STEPS
6
+ from pyastropdl.core.utils.types import (
7
+ StateVector,
8
+ f64,
9
+ f64_1d,
10
+ f64_2d,
11
+ f64_tuple2,
12
+ f64_tuple3,
13
+ )
14
+
15
+ from .residuals import altitude_residual
16
+
17
+
18
+ @njit(f64(f64_2d, f64_1d, f64_tuple3, f64, f64_tuple2, f64), cache=True)
19
+ def halley_method(
20
+ free_fall_phase: StateVector,
21
+ end_state: StateVector,
22
+ phys_params: tuple[float, float, float],
23
+ dt: float,
24
+ t_bounds: tuple[float, float],
25
+ eps: float = 1e-5,
26
+ ) -> float:
27
+
28
+ t_min, t_max = t_bounds
29
+ ti = t_max
30
+
31
+ for _ in range(MAX_ITERATIONS_STEPS):
32
+
33
+ f_val = altitude_residual(ti, free_fall_phase, end_state, phys_params, dt)
34
+ if abs(f_val) <= eps:
35
+ return ti
36
+
37
+ f_left = altitude_residual(ti - eps, free_fall_phase, end_state, phys_params, dt)
38
+ f_right = altitude_residual(ti + eps, free_fall_phase, end_state, phys_params, dt)
39
+
40
+ f_dx = (f_right - f_left) / (2.0 * eps)
41
+ f_ddx = (f_right - 2.0 * f_val + f_left) / (eps**2)
42
+
43
+ denominator = 2.0 * (f_dx**2) - f_val * f_ddx
44
+
45
+
46
+ if abs(denominator) < 1e-12:
47
+ return nan
48
+
49
+ ti += -(2.0 * f_val * f_dx) / denominator
50
+
51
+ if np.isnan(ti):
52
+ return nan
53
+
54
+ if ti >= t_max:
55
+ ti = t_max - eps
56
+ t_max -= 0.1
57
+ elif ti <= t_min:
58
+ ti = t_min + eps
59
+ t_min += 0.1
60
+
61
+ return nan
62
+
@@ -0,0 +1,26 @@
1
+ from .engine import (
2
+ get_ff_phase,
3
+ get_terminal_state,
4
+ get_theo_time_boundary,
5
+ get_time_boundary,
6
+ get_trajectory,
7
+ )
8
+ from .integrators import rk4_bp_inplace
9
+ from .physics import braking_ode, get_free_fall_state, get_g
10
+ from .residuals import altitude_residual, residual_func
11
+ from .root_finding import halley_method
12
+
13
+ __all__ = [
14
+ "altitude_residual",
15
+ "braking_ode",
16
+ "get_ff_phase",
17
+ "get_free_fall_state",
18
+ "get_g",
19
+ "get_terminal_state",
20
+ "get_theo_time_boundary",
21
+ "get_time_boundary",
22
+ "get_trajectory",
23
+ "halley_method",
24
+ "residual_func",
25
+ "rk4_bp_inplace"
26
+ ]