Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
norm_r0 = np.dot(r0, r0)**.5
norm_r = np.dot(r, r)**.5
cos_dnu = np.dot(r0, r) / (norm_r0 * norm_r)
A = t_m * (norm_r * norm_r0 * (1 + cos_dnu))**.5
if A == 0.0:
raise RuntimeError("Cannot compute orbit, phase angle is 180 degrees")
psi = 0.0
psi_low = -4 * np.pi
psi_up = 4 * np.pi
count = 0
while count < numiter:
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
if A > 0.0 and y < 0.0:
# Readjust xi_low until y > 0.0
# Translated directly from Vallado
while y < 0.0:
psi_low = psi
psi = (0.8 * (1.0 / c3(psi)) *
(1.0 - (norm_r0 + norm_r) * np.sqrt(c2(psi)) / A))
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
xi = np.sqrt(y / c2(psi))
tof_new = (xi**3 * c3(psi) + A * np.sqrt(y)) / np.sqrt(k)
# Convergence check
if np.abs((tof_new - tof) / tof) < rtol:
break
else:
xi_new = (np.sign(tof) * (-1 / alpha)**.5 *
np.log((-2 * k * alpha * tof) /
(dot_r0v0 + np.sign(tof) *
np.sqrt(-k / alpha) * (1 - norm_r0 * alpha))))
else:
# Parabolic orbit
# (Conservative initial guess)
xi_new = sqrt_mu * tof / norm_r0
# Newton-Raphson iteration on the Kepler equation
count = 0
while count < numiter:
xi = xi_new
psi = xi * xi * alpha
c2_psi = c2(psi)
c3_psi = c3(psi)
norm_r = (xi * xi * c2_psi +
dot_r0v0 / sqrt_mu * xi * (1 - psi * c3_psi) +
norm_r0 * (1 - psi * c2_psi))
xi_new = xi + (sqrt_mu * tof - xi * xi * xi * c3_psi -
dot_r0v0 / sqrt_mu * xi * xi * c2_psi -
norm_r0 * xi * (1 - psi * c3_psi)) / norm_r
if abs(xi_new - xi) < 1e-7:
break
else:
count += 1
else:
raise RuntimeError("Maximum number of iterations reached")
# Compute Lagrange coefficients
f = 1 - xi**2 / norm_r0 * c2_psi
g = tof - xi**3 / sqrt_mu * c3_psi
psi_up = 4 * np.pi
count = 0
while count < numiter:
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
if A > 0.0 and y < 0.0:
# Readjust xi_low until y > 0.0
# Translated directly from Vallado
while y < 0.0:
psi_low = psi
psi = (0.8 * (1.0 / c3(psi)) *
(1.0 - (norm_r0 + norm_r) * np.sqrt(c2(psi)) / A))
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
xi = np.sqrt(y / c2(psi))
tof_new = (xi**3 * c3(psi) + A * np.sqrt(y)) / np.sqrt(k)
# Convergence check
if np.abs((tof_new - tof) / tof) < rtol:
break
else:
count += 1
# Bisection check
if tof_new <= tof:
psi_low = psi
else:
psi_up = psi
psi = (psi_up + psi_low) / 2
else:
raise RuntimeError("Maximum number of iterations reached")
f = 1 - y / norm_r0
# compute preliminaries
r0 = np.sqrt((x**2)+(y**2)+(z**2))
v20 = (u**2)+(v**2)+(w**2)
vr0 = (x*u+y*v+z*w)/r0
alpha0 = (2.0/r0)-(v20/mu)
# compute initial estimate for xi
xi = np.sqrt(mu)*np.abs(alpha0)*dt
i = 0
ratio_i = 1.0
while np.abs(ratio_i)>atol and i
def lagrangeg_(tau, xi, z, mu):
"""Compute current value of Lagrange's g function.
Args:
tau (float): time interval
xi (float): universal Kepler anomaly
z (float): xi**2/alpha
r (float): radial distance
Returns:
float: Lagrange's g function value
"""
return tau-(xi**3)*c3(z)/np.sqrt(mu)
if A == 0.0:
raise RuntimeError("Cannot compute orbit, phase angle is 180 degrees")
psi = 0.0
psi_low = -4 * np.pi
psi_up = 4 * np.pi
count = 0
while count < numiter:
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
if A > 0.0 and y < 0.0:
# Readjust xi_low until y > 0.0
# Translated directly from Vallado
while y < 0.0:
psi_low = psi
psi = (0.8 * (1.0 / c3(psi)) *
(1.0 - (norm_r0 + norm_r) * np.sqrt(c2(psi)) / A))
y = norm_r0 + norm_r + A * (psi * c3(psi) - 1) / c2(psi)**.5
xi = np.sqrt(y / c2(psi))
tof_new = (xi**3 * c3(psi) + A * np.sqrt(y)) / np.sqrt(k)
# Convergence check
if np.abs((tof_new - tof) / tof) < rtol:
break
else:
count += 1
# Bisection check
if tof_new <= tof:
psi_low = psi
else:
psi_up = psi