def mikkola_coe(k, p, ecc, inc, raan, argp, nu, tof): a = p / (1 - ecc**2) n = np.sqrt(k / np.abs(a)**3) # Solve for specific geometrical case if ecc < 1.0: # Equation (9a) alpha = (1 - ecc) / (4 * ecc + 1 / 2) M0 = E_to_M(nu_to_E(nu, ecc), ecc) else: alpha = (ecc - 1) / (4 * ecc + 1 / 2) M0 = F_to_M(nu_to_F(nu, ecc), ecc) M = M0 + n * tof beta = M / 2 / (4 * ecc + 1 / 2) # Equation (9b) if beta >= 0: z = (beta + np.sqrt(beta**2 + alpha**3))**(1 / 3) else: z = (beta - np.sqrt(beta**2 + alpha**3))**(1 / 3) s = z - alpha / z # Apply initial correction if ecc < 1.0: ds = -0.078 * s**5 / (1 + ecc) else: ds = 0.071 * s**5 / (1 + 0.45 * s**2) / (1 + 4 * s**2) / ecc s += ds # Solving for the true anomaly if ecc < 1.0: E = M + ecc * (3 * s - 4 * s**3) f = E - ecc * np.sin(E) - M f1 = 1.0 - ecc * np.cos(E) f2 = ecc * np.sin(E) f3 = ecc * np.cos(E) f4 = -f2 f5 = -f3 else: E = 3 * np.log(s + np.sqrt(1 + s**2)) f = -E + ecc * np.sinh(E) - M f1 = -1.0 + ecc * np.cosh(E) f2 = ecc * np.sinh(E) f3 = ecc * np.cosh(E) f4 = f2 f5 = f3 # Apply Taylor expansion u1 = -f / f1 u2 = -f / (f1 + 0.5 * f2 * u1) u3 = -f / (f1 + 0.5 * f2 * u2 + (1.0 / 6.0) * f3 * u2**2) u4 = -f / (f1 + 0.5 * f2 * u3 + (1.0 / 6.0) * f3 * u3**2 + (1.0 / 24.0) * f4 * (u3**3)) u5 = -f / (f1 + f2 * u4 / 2 + f3 * (u4 * u4) / 6.0 + f4 * (u4 * u4 * u4) / 24.0 + f5 * (u4 * u4 * u4 * u4) / 120.0) E += u5 if ecc < 1.0: nu = E_to_nu(E, ecc) else: if ecc == 1.0: # Parabolic nu = D_to_nu(E) else: # Hyperbolic nu = F_to_nu(E, ecc) return nu
def nu_from_delta_t(delta_t, ecc, k=1.0, q=1.0, delta=1e-2): """True anomaly for given elapsed time since periapsis. Parameters ---------- delta_t : float Time elapsed since periapsis. ecc : float Eccentricity. k : float Gravitational parameter. q : float Periapsis distance. delta : float Parameter that controls the size of the near parabolic region. Returns ------- nu : float True anomaly. """ if ecc < 1 - delta: # Strong elliptic n = np.sqrt(k * (1 - ecc)**3 / q**3) M = n * delta_t # This might represent several revolutions, # so we wrap the true anomaly E = M_to_E((M + np.pi) % (2 * np.pi) - np.pi, ecc) nu = E_to_nu(E, ecc) elif 1 - delta <= ecc < 1: E_delta = np.arccos((1 - delta) / ecc) # We compute M assuming we are in the strong elliptic case # and verify later n = np.sqrt(k * (1 - ecc)**3 / q**3) M = n * delta_t # We check against abs(M) because E_delta could also be negative if E_to_M(E_delta, ecc) <= abs(M): # Strong elliptic, proceed # This might represent several revolutions, # so we wrap the true anomaly E = M_to_E((M + np.pi) % (2 * np.pi) - np.pi, ecc) nu = E_to_nu(E, ecc) else: # Near parabolic, recompute M n = np.sqrt(k / (2 * q**3)) M = n * delta_t D = M_to_D_near_parabolic(M, ecc) nu = D_to_nu(D) elif ecc == 1: # Parabolic n = np.sqrt(k / (2 * q**3)) M = n * delta_t D = M_to_D(M) nu = D_to_nu(D) elif 1 < ecc <= 1 + delta: F_delta = np.arccosh((1 + delta) / ecc) # We compute M assuming we are in the strong hyperbolic case # and verify later n = np.sqrt(k * (ecc - 1)**3 / q**3) M = n * delta_t # We check against abs(M) because F_delta could also be negative if F_to_M(F_delta, ecc) <= abs(M): # Strong hyperbolic, proceed F = M_to_F(M, ecc) nu = F_to_nu(F, ecc) else: # Near parabolic, recompute M n = np.sqrt(k / (2 * q**3)) M = n * delta_t D = M_to_D_near_parabolic(M, ecc) nu = D_to_nu(D) # elif 1 + delta < ecc: else: # Strong hyperbolic n = np.sqrt(k * (ecc - 1)**3 / q**3) M = n * delta_t F = M_to_F(M, ecc) nu = F_to_nu(F, ecc) return nu
def rv2coe(k, r, v, tol=1e-8): r"""Converts from vectors to classical orbital elements. 1. First the angular momentum is computed: .. math:: \vec{h} = \vec{r} \times \vec{v} 2. With it the eccentricity can be solved: .. math:: \begin{align} \vec{e} &= \frac{1}{\mu}\left [ \left ( v^{2} - \frac{\mu}{r}\right ) \vec{r} - (\vec{r} \cdot \vec{v})\vec{v} \right ] \\ e &= \sqrt{\vec{e}\cdot\vec{e}} \\ \end{align} 3. The node vector line is solved: .. math:: \begin{align} \vec{N} &= \vec{k} \times \vec{h} \\ N &= \sqrt{\vec{N}\cdot\vec{N}} \end{align} 4. The rigth ascension node is computed: .. math:: \Omega = \left\{ \begin{array}{lcc} cos^{-1}{\left ( \frac{N_{x}}{N} \right )} & if & N_{y} \geq 0 \\ \\ 360^{o} -cos^{-1}{\left ( \frac{N_{x}}{N} \right )} & if & N_{y} < 0 \\ \end{array} \right. 5. The argument of perigee: .. math:: \omega = \left\{ \begin{array}{lcc} cos^{-1}{\left ( \frac{\vec{N}\vec{e}}{Ne} \right )} & if & e_{z} \geq 0 \\ \\ 360^{o} -cos^{-1}{\left ( \frac{\vec{N}\vec{e}}{Ne} \right )} & if & e_{z} < 0 \\ \end{array} \right. 6. And finally the true anomaly: .. math:: \nu = \left\{ \begin{array}{lcc} cos^{-1}{\left ( \frac{\vec{e}\vec{r}}{er} \right )} & if & v_{r} \geq 0 \\ \\ 360^{o} -cos^{-1}{\left ( \frac{\vec{e}\vec{r}}{er} \right )} & if & v_{r} < 0 \\ \end{array} \right. Parameters ---------- k : float Standard gravitational parameter (km^3 / s^2) r : array Position vector (km) v : array Velocity vector (km / s) tol : float, optional Tolerance for eccentricity and inclination checks, default to 1e-8 Returns ------- p : float Semi-latus rectum of parameter (km) ecc: float Eccentricity inc: float Inclination (rad) raan: float Right ascension of the ascending nod (rad) argp: float Argument of Perigee (rad) nu: float True Anomaly (rad) Examples -------- >>> from poliastro.constants import GM_earth >>> from astropy import units as u >>> k = GM_earth.to(u.km ** 3 / u.s ** 2).value # Earth gravitational parameter >>> r = np.array([-6045., -3490., 2500.]) >>> v = np.array([-3.457, 6.618, 2.533]) >>> p, ecc, inc, raan, argp, nu = rv2coe(k, r, v) >>> print("p:", p, "[km]") p: 8530.47436396927 [km] >>> print("ecc:", ecc) ecc: 0.17121118195416898 >>> print("inc:", np.rad2deg(inc), "[deg]") inc: 153.2492285182475 [deg] >>> print("raan:", np.rad2deg(raan), "[deg]") raan: 255.27928533439618 [deg] >>> print("argp:", np.rad2deg(argp), "[deg]") argp: 20.068139973005366 [deg] >>> print("nu:", np.rad2deg(nu), "[deg]") nu: 28.445804984192122 [deg] Note ---- This example is a real exercise from Orbital Mechanics for Engineering students by Howard D.Curtis. This exercise is 4.3 of 3rd. Edition, page 200. """ h = cross(r, v) n = cross([0, 0, 1], h) e = ((v.dot(v) - k / (norm(r))) * r - r.dot(v) * v) / k ecc = norm(e) p = h.dot(h) / k inc = np.arccos(h[2] / norm(h)) circular = ecc < tol equatorial = abs(inc) < tol if equatorial and not circular: raan = 0 argp = np.arctan2(e[1], e[0]) % (2 * np.pi) # Longitude of periapsis nu = np.arctan2(h.dot(cross(e, r)) / norm(h), r.dot(e)) elif not equatorial and circular: raan = np.arctan2(n[1], n[0]) % (2 * np.pi) argp = 0 # Argument of latitude nu = np.arctan2(r.dot(cross(h, n)) / norm(h), r.dot(n)) elif equatorial and circular: raan = 0 argp = 0 nu = np.arctan2(r[1], r[0]) % (2 * np.pi) # True longitude else: a = p / (1 - (ecc**2)) ka = k * a if a > 0: e_se = r.dot(v) / sqrt(ka) e_ce = norm(r) * v.dot(v) / k - 1 nu = E_to_nu(np.arctan2(e_se, e_ce), ecc) else: e_sh = r.dot(v) / sqrt(-ka) e_ch = norm(r) * (norm(v)**2) / k - 1 nu = F_to_nu(np.log((e_ch + e_sh) / (e_ch - e_sh)) / 2, ecc) raan = np.arctan2(n[1], n[0]) % (2 * np.pi) px = r.dot(n) py = r.dot(cross(h, n)) / norm(h) argp = (np.arctan2(py, px) - nu) % (2 * np.pi) nu = (nu + np.pi) % (2 * np.pi) - np.pi return p, ecc, inc, raan, argp, nu
def mikkola(k, r0, v0, tof, rtol=None): """ Raw algorithm for Mikkola's Kepler solver. Parameters ---------- k : ~astropy.units.Quantity Standard gravitational parameter of the attractor. r : ~astropy.units.Quantity Position vector. v : ~astropy.units.Quantity Velocity vector. tofs : ~astropy.units.Quantity Array of times to propagate. rtol: float This method does not require of tolerance since it is non iterative. Returns ------- rr : ~astropy.units.Quantity Propagated position vectors. vv : ~astropy.units.Quantity Note ---- Original paper: https://doi.org/10.1007/BF01235850 """ # Solving for the classical elements p, ecc, inc, raan, argp, nu = rv2coe(k, r0, v0) M0 = nu_to_M(nu, ecc, delta=0) a = p / (1 - ecc**2) n = np.sqrt(k / np.abs(a)**3) M = M0 + n * tof # Solve for specific geometrical case if ecc < 1.0: # Equation (9a) alpha = (1 - ecc) / (4 * ecc + 1 / 2) else: alpha = (ecc - 1) / (4 * ecc + 1 / 2) beta = M / 2 / (4 * ecc + 1 / 2) # Equation (9b) if beta >= 0: z = (beta + np.sqrt(beta**2 + alpha**3))**(1 / 3) else: z = (beta - np.sqrt(beta**2 + alpha**3))**(1 / 3) s = z - alpha / z # Apply initial correction if ecc < 1.0: ds = -0.078 * s**5 / (1 + ecc) else: ds = 0.071 * s**5 / (1 + 0.45 * s**2) / (1 + 4 * s**2) / ecc s += ds # Solving for the true anomaly if ecc < 1.0: E = M + ecc * (3 * s - 4 * s**3) f = E - ecc * np.sin(E) - M f1 = 1.0 - ecc * np.cos(E) f2 = ecc * np.sin(E) f3 = ecc * np.cos(E) f4 = -f2 f5 = -f3 else: E = 3 * np.log(s + np.sqrt(1 + s**2)) f = -E + ecc * np.sinh(E) - M f1 = -1.0 + ecc * np.cosh(E) f2 = ecc * np.sinh(E) f3 = ecc * np.cosh(E) f4 = f2 f5 = f3 # Apply Taylor expansion u1 = -f / f1 u2 = -f / (f1 + 0.5 * f2 * u1) u3 = -f / (f1 + 0.5 * f2 * u2 + (1.0 / 6.0) * f3 * u2**2) u4 = -f / (f1 + 0.5 * f2 * u3 + (1.0 / 6.0) * f3 * u3**2 + (1.0 / 24.0) * f4 * (u3**3)) u5 = -f / (f1 + f2 * u4 / 2 + f3 * (u4 * u4) / 6.0 + f4 * (u4 * u4 * u4) / 24.0 + f5 * (u4 * u4 * u4 * u4) / 120.0) E += u5 if ecc < 1.0: nu = E_to_nu(E, ecc) else: if ecc == 1.0: # Parabolic nu = D_to_nu(E) else: # Hyperbolic nu = F_to_nu(E, ecc) return coe2rv(k, p, ecc, inc, raan, argp, nu)