| 1 | --- |
| 2 | jupytext: |
| 3 | text_representation: |
| 4 | extension: .md |
| 5 | format_name: myst |
| 6 | format_version: 0.13 |
| 7 | jupytext_version: 1.14.1 |
| 8 | kernelspec: |
| 9 | display_name: Python 3 (ipykernel) |
| 10 | language: python |
| 11 | name: python3 |
| 12 | --- |
| 13 | |
| 14 | # Natural and artificial perturbations |
| 15 | |
| 16 | ```{code-cell} ipython3 |
| 17 | from matplotlib import pyplot as plt |
| 18 | import numpy as np |
| 19 | |
| 20 | from astropy.coordinates import solar_system_ephemeris |
| 21 | from astropy.time import Time, TimeDelta |
| 22 | from astropy import units as u |
| 23 | |
| 24 | from boinor.bodies import Earth, Moon |
| 25 | from boinor.constants import rho0_earth, H0_earth |
| 26 | |
| 27 | from boinor.core.elements import rv2coe |
| 28 | from boinor.core.perturbations import ( |
| 29 | atmospheric_drag_exponential, |
| 30 | third_body, |
| 31 | J2_perturbation, |
| 32 | ) |
| 33 | from boinor.core.propagation import func_twobody |
| 34 | from boinor.ephem import build_ephem_interpolant |
| 35 | from boinor.plotting import OrbitPlotter |
| 36 | from boinor.plotting.orbit.backends import Plotly3D |
| 37 | from boinor.twobody import Orbit |
| 38 | from boinor.twobody.propagation import CowellPropagator |
| 39 | from boinor.twobody.sampling import EpochsArray |
| 40 | from boinor.util import norm, time_range |
| 41 | ``` |
| 42 | |
| 43 | ## Atmospheric drag ## |
| 44 | The boinor package now has several commonly used natural perturbations. One of them is atmospheric drag! See how one can monitor decay of the near-Earth orbit over time using our new module `boinor.twobody.perturbations`! |
| 45 | |
| 46 | ```{code-cell} ipython3 |
| 47 | R = Earth.R.to(u.km).value |
| 48 | k = Earth.k.to(u.km**3 / u.s**2).value |
| 49 | |
| 50 | orbit = Orbit.circular( |
| 51 | Earth, 250 * u.km, epoch=Time(0.0, format="jd", scale="tdb") |
| 52 | ) |
| 53 | |
| 54 | # parameters of a body |
| 55 | C_D = 2.2 # dimentionless (any value would do) |
| 56 | A_over_m = ((np.pi / 4.0) * (u.m**2) / (100 * u.kg)).to_value( |
| 57 | u.km**2 / u.kg |
| 58 | ) # km^2/kg |
| 59 | B = C_D * A_over_m |
| 60 | |
| 61 | # parameters of the atmosphere |
| 62 | rho0 = rho0_earth.to(u.kg / u.km**3).value # kg/km^3 |
| 63 | H0 = H0_earth.to(u.km).value |
| 64 | |
| 65 | tofs = TimeDelta(np.linspace(0 * u.h, 100000 * u.s, num=2000)) |
| 66 | |
| 67 | |
| 68 | def f(t0, state, k): |
| 69 | du_kep = func_twobody(t0, state, k) |
| 70 | ax, ay, az = atmospheric_drag_exponential( |
| 71 | t0, |
| 72 | state, |
| 73 | k, |
| 74 | R=R, |
| 75 | C_D=C_D, |
| 76 | A_over_m=A_over_m, |
| 77 | H0=H0, |
| 78 | rho0=rho0, |
| 79 | ) |
| 80 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 81 | |
| 82 | return du_kep + du_ad |
| 83 | |
| 84 | |
| 85 | rr, _ = orbit.to_ephem( |
| 86 | EpochsArray(orbit.epoch + tofs, method=CowellPropagator(f=f)), |
| 87 | ).rv() |
| 88 | ``` |
| 89 | |
| 90 | ```{code-cell} ipython3 |
| 91 | plt.ylabel("h(t)") |
| 92 | plt.xlabel("t, days") |
| 93 | plt.plot(tofs.value, norm(rr, axis=1) - Earth.R); |
| 94 | ``` |
| 95 | |
| 96 | ## Orbital Decay |
| 97 | |
| 98 | If atmospheric drag causes the orbit to fully decay, additional code |
| 99 | is needed to stop the integration when the satellite reaches the |
| 100 | surface. |
| 101 | |
| 102 | Please note that you will likely want to use a more sophisticated |
| 103 | atmosphere model than the one in `atmospheric_drag` for these sorts |
| 104 | of computations. |
| 105 | |
| 106 | ```{code-cell} ipython3 |
| 107 | from boinor.twobody.events import LithobrakeEvent |
| 108 | |
| 109 | orbit = Orbit.circular( |
| 110 | Earth, 230 * u.km, epoch=Time(0.0, format="jd", scale="tdb") |
| 111 | ) |
| 112 | tofs = TimeDelta(np.linspace(0 * u.h, 100 * u.d, num=2000)) |
| 113 | |
| 114 | lithobrake_event = LithobrakeEvent(R) |
| 115 | events = [lithobrake_event] |
| 116 | |
| 117 | rr, _ = orbit.to_ephem( |
| 118 | EpochsArray( |
| 119 | orbit.epoch + tofs, method=CowellPropagator(f=f, events=events) |
| 120 | ), |
| 121 | ).rv() |
| 122 | |
| 123 | print( |
| 124 | "orbital decay seen after", lithobrake_event.last_t.to(u.d).value, "days" |
| 125 | ) |
| 126 | ``` |
| 127 | |
| 128 | ```{code-cell} ipython3 |
| 129 | :tags: [nbsphinx-thumbnail] |
| 130 | |
| 131 | plt.ylabel("h(t)") |
| 132 | plt.xlabel("t, days") |
| 133 | plt.plot(tofs[: len(rr)].value, norm(rr, axis=1) - Earth.R); |
| 134 | ``` |
| 135 | |
| 136 | ## Evolution of RAAN due to the J2 perturbation |
| 137 | |
| 138 | We can also see how the J2 perturbation changes RAAN over time! |
| 139 | |
| 140 | ```{code-cell} ipython3 |
| 141 | r0 = np.array([-2384.46, 5729.01, 3050.46]) * u.km |
| 142 | v0 = np.array([-7.36138, -2.98997, 1.64354]) * u.km / u.s |
| 143 | |
| 144 | orbit = Orbit.from_vectors(Earth, r0, v0) |
| 145 | |
| 146 | tofs = TimeDelta(np.linspace(0, 48.0 * u.h, num=2000)) |
| 147 | |
| 148 | |
| 149 | def f(t0, state, k): |
| 150 | du_kep = func_twobody(t0, state, k) |
| 151 | ax, ay, az = J2_perturbation( |
| 152 | t0, state, k, J2=Earth.J2.value, R=Earth.R.to(u.km).value |
| 153 | ) |
| 154 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 155 | |
| 156 | return du_kep + du_ad |
| 157 | |
| 158 | |
| 159 | rr, vv = orbit.to_ephem( |
| 160 | EpochsArray(orbit.epoch + tofs, method=CowellPropagator(f=f)), |
| 161 | ).rv() |
| 162 | |
| 163 | # This will be easier to compute when this is solved: |
| 164 | # https://github.com/boinor/boinor/issues/380 |
| 165 | raans = [ |
| 166 | rv2coe(k, r, v)[3] |
| 167 | for r, v in zip(rr.to_value(u.km), vv.to_value(u.km / u.s)) |
| 168 | ] |
| 169 | ``` |
| 170 | |
| 171 | ```{code-cell} ipython3 |
| 172 | plt.ylabel("RAAN(t)") |
| 173 | plt.xlabel("t, h") |
| 174 | plt.plot(tofs.value, raans); |
| 175 | ``` |
| 176 | |
| 177 | ## 3rd body |
| 178 | |
| 179 | Apart from time-independent perturbations such as atmospheric drag, J2/J3, we have time-dependent perturbations. Let's see how the Moon changes the orbit of GEO satellite over time! |
| 180 | |
| 181 | ```{code-cell} ipython3 |
| 182 | # database keeping positions of bodies in Solar system over time |
| 183 | solar_system_ephemeris.set("de432s") |
| 184 | |
| 185 | epoch = Time( |
| 186 | 2454283.0, format="jd", scale="tdb" |
| 187 | ) # setting the exact event date is important |
| 188 | |
| 189 | # create interpolant of 3rd body coordinates (calling in on every iteration will be just too slow) |
| 190 | epochs_moon = time_range(epoch,num_values=214,end=epoch + 60*u.day) |
| 191 | body_r = build_ephem_interpolant( |
| 192 | Moon, |
| 193 | epochs_moon |
| 194 | ) |
| 195 | |
| 196 | initial = Orbit.from_classical( |
| 197 | Earth, |
| 198 | 42164.0 * u.km, |
| 199 | 0.0001 * u.one, |
| 200 | 1 * u.deg, |
| 201 | 0.0 * u.deg, |
| 202 | 0.0 * u.deg, |
| 203 | 0.0 * u.rad, |
| 204 | epoch=epoch, |
| 205 | ) |
| 206 | |
| 207 | tofs = TimeDelta(np.linspace(0, 60 * u.day, num=1000)) |
| 208 | |
| 209 | |
| 210 | def f(t0, state, k): |
| 211 | du_kep = func_twobody(t0, state, k) |
| 212 | ax, ay, az = third_body( |
| 213 | t0, |
| 214 | state, |
| 215 | k, |
| 216 | k_third=400 * Moon.k.to(u.km**3 / u.s**2).value, |
| 217 | perturbation_body=body_r, |
| 218 | ) |
| 219 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 220 | |
| 221 | return du_kep + du_ad |
| 222 | |
| 223 | |
| 224 | # multiply Moon gravity by 400 so that effect is visible :) |
| 225 | ephem = initial.to_ephem( |
| 226 | EpochsArray(initial.epoch + tofs, method=CowellPropagator(rtol=1e-6, f=f)), |
| 227 | ) |
| 228 | ``` |
| 229 | |
| 230 | ```{code-cell} ipython3 |
| 231 | frame = OrbitPlotter(backend=Plotly3D()) |
| 232 | |
| 233 | frame.set_attractor(Earth) |
| 234 | frame.plot_ephem(ephem, label="orbit influenced by Moon") |
| 235 | ``` |
| 236 | |
| 237 | ## Applying thrust |
| 238 | |
| 239 | Apart from natural perturbations, there are artificial thrusts aimed at intentional change of orbit parameters. One of such changes is simultaneous change of eccentricity and inclination: |
| 240 | |
| 241 | ```{code-cell} ipython3 |
| 242 | from boinor.twobody.thrust import change_ecc_inc |
| 243 | |
| 244 | ecc_0, ecc_f = 0.4, 0.0 |
| 245 | a = 42164 # km |
| 246 | inc_0 = 0.0 # rad, baseline |
| 247 | inc_f = 20.0 * u.deg |
| 248 | argp = 0.0 # rad, the method is efficient for 0 and 180 |
| 249 | f = 2.4e-6 * (u.km / u.s**2) |
| 250 | |
| 251 | k = Earth.k.to(u.km**3 / u.s**2).value |
| 252 | orb0 = Orbit.from_classical( |
| 253 | Earth, |
| 254 | a * u.km, |
| 255 | ecc_0 * u.one, |
| 256 | inc_0 * u.deg, |
| 257 | 0 * u.deg, |
| 258 | argp * u.deg, |
| 259 | 0 * u.deg, |
| 260 | epoch=Time(0, format="jd", scale="tdb"), |
| 261 | ) |
| 262 | |
| 263 | a_d, _, t_f = change_ecc_inc(orb0, ecc_f, inc_f, f) |
| 264 | |
| 265 | |
| 266 | def f(t0, state, k): |
| 267 | du_kep = func_twobody(t0, state, k) |
| 268 | ax, ay, az = a_d( |
| 269 | t0, |
| 270 | state, |
| 271 | k, |
| 272 | ) |
| 273 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 274 | |
| 275 | return du_kep + du_ad |
| 276 | |
| 277 | |
| 278 | tofs = TimeDelta(np.linspace(0, t_f, num=1000)) |
| 279 | |
| 280 | ephem2 = orb0.to_ephem( |
| 281 | EpochsArray(orb0.epoch + tofs, method=CowellPropagator(rtol=1e-6, f=f)), |
| 282 | ) |
| 283 | ``` |
| 284 | |
| 285 | ```{code-cell} ipython3 |
| 286 | frame = OrbitPlotter(backend=Plotly3D()) |
| 287 | |
| 288 | frame.set_attractor(Earth) |
| 289 | frame.plot_ephem(ephem2, label="orbit with artificial thrust") |
| 290 | ``` |
| 291 | |
| 292 | ## Combining multiple perturbations |
| 293 | |
| 294 | It might be of interest to determine what effect multiple perturbations have on a single object. In order to add multiple perturbations we can create a custom function that adds them up: |
| 295 | |
| 296 | ```{code-cell} ipython3 |
| 297 | from numba import njit as jit |
| 298 | |
| 299 | # Add @jit for speed! |
| 300 | @jit |
| 301 | def a_d(t0, state, k, J2, R, C_D, A_over_m, H0, rho0): |
| 302 | return J2_perturbation(t0, state, k, J2, R) + atmospheric_drag_exponential( |
| 303 | t0, state, k, R, C_D, A_over_m, H0, rho0 |
| 304 | ) |
| 305 | ``` |
| 306 | |
| 307 | ```{code-cell} ipython3 |
| 308 | # propagation times of flight and orbit |
| 309 | tofs = TimeDelta(np.linspace(0, 10 * u.day, num=10 * 500)) |
| 310 | orbit = Orbit.circular(Earth, 250 * u.km) # recall orbit from drag example |
| 311 | |
| 312 | |
| 313 | def f(t0, state, k): |
| 314 | du_kep = func_twobody(t0, state, k) |
| 315 | ax, ay, az = a_d( |
| 316 | t0, |
| 317 | state, |
| 318 | k, |
| 319 | R=R, |
| 320 | C_D=C_D, |
| 321 | A_over_m=A_over_m, |
| 322 | H0=H0, |
| 323 | rho0=rho0, |
| 324 | J2=Earth.J2.value, |
| 325 | ) |
| 326 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 327 | |
| 328 | return du_kep + du_ad |
| 329 | |
| 330 | |
| 331 | # propagate with J2 and atmospheric drag |
| 332 | rr3, _ = orbit.to_ephem( |
| 333 | EpochsArray(orbit.epoch + tofs, method=CowellPropagator(f=f)), |
| 334 | ).rv() |
| 335 | |
| 336 | |
| 337 | def f(t0, state, k): |
| 338 | du_kep = func_twobody(t0, state, k) |
| 339 | ax, ay, az = atmospheric_drag_exponential( |
| 340 | t0, |
| 341 | state, |
| 342 | k, |
| 343 | R=R, |
| 344 | C_D=C_D, |
| 345 | A_over_m=A_over_m, |
| 346 | H0=H0, |
| 347 | rho0=rho0, |
| 348 | ) |
| 349 | du_ad = np.array([0, 0, 0, ax, ay, az]) |
| 350 | |
| 351 | return du_kep + du_ad |
| 352 | |
| 353 | |
| 354 | # propagate with only atmospheric drag |
| 355 | rr4, _ = orbit.to_ephem( |
| 356 | EpochsArray(orbit.epoch + tofs, method=CowellPropagator(f=f)), |
| 357 | ).rv() |
| 358 | ``` |
| 359 | |
| 360 | ```{code-cell} ipython3 |
| 361 | fig, (axes1, axes2) = plt.subplots(nrows=2, sharex=True, figsize=(15, 6)) |
| 362 | |
| 363 | axes1.plot(tofs.value, norm(rr3, axis=1) - Earth.R) |
| 364 | axes1.set_ylabel("h(t)") |
| 365 | axes1.set_xlabel("t, days") |
| 366 | axes1.set_ylim([225, 251]) |
| 367 | |
| 368 | axes2.plot(tofs.value, norm(rr4, axis=1) - Earth.R) |
| 369 | axes2.set_ylabel("h(t)") |
| 370 | axes2.set_xlabel("t, days") |
| 371 | axes2.set_ylim([225, 251]) |
| 372 | ``` |
| 373 | |
| 374 | The first plot shows the altitude of the orbit changing due to both atmospheric drag and the J2 effect, the second plot shows only the effect of atmospheric drag. |