def find_vector(): RA = [[18, 41, 48.08], [18, 14, 30.53], [17, 54, 29.36]] DEC = [[36, 15, 14.1], [36, 9, 46.0], [34, 29, 32.2]] for i in range(3): RA[i] = od.HMStoDeg(RA[i]) * pi / 180 DEC[i] = od.DMStoDeg(DEC[i]) * pi / 180 rho_hat1 = [ cos(RA[0]) * cos(DEC[0]), sin(RA[0]) * cos(DEC[0]), sin(DEC[0]) ] # calculating rho from RA and Dec rho_hat2 = [ cos(RA[1]) * cos(DEC[1]), sin(RA[1]) * cos(DEC[1]), sin(DEC[1]) ] rho_hat3 = [ cos(RA[2]) * cos(DEC[2]), sin(RA[2]) * cos(DEC[2]), sin(DEC[2]) ] rho1 = [0.07410335127715940, -.4015611046271995, 0.2994573000142365] # exact rho values rho2 = [0.02816887198319436, -0.4437310883473226, 0.3249882273840305] rho3 = [-0.01187334735101580, -0.4953819999141603, 0.3404906007681184] rho_hat_1_ex = [ rho1[0] / od.mag(rho1), rho1[1] / od.mag(rho1), rho1[2] / od.mag(rho1) ] # rho hat calculation from exact values rho_hat_2_ex = [ rho2[0] / od.mag(rho2), rho2[1] / od.mag(rho2), rho2[2] / od.mag(rho2) ] rho_hat_3_ex = [ rho3[0] / od.mag(rho3), rho3[1] / od.mag(rho3), rho3[2] / od.mag(rho3) ] error_1x = (rho_hat1[0] - rho_hat_1_ex[0])**2 # error by component error_1y = (rho_hat1[1] - rho_hat_1_ex[1])**2 error_1z = (rho_hat1[2] - rho_hat_1_ex[2])**2 error_2x = (rho_hat2[0] - rho_hat_2_ex[0])**2 error_2y = (rho_hat2[1] - rho_hat_2_ex[1])**2 error_2z = (rho_hat2[1] - rho_hat_2_ex[1])**2 error_3x = (rho_hat3[0] - rho_hat_3_ex[0])**2 error_3y = (rho_hat3[1] - rho_hat_3_ex[1])**2 error_3z = (rho_hat3[2] - rho_hat_3_ex[2])**2 error_1 = error_1x - error_1y - error_1z # cummulative error error_2 = error_2x - error_2y - error_2z error_3 = error_3x - error_3y - error_3z print('Error for rho 1: ', error_1) print('Error for rho 2: ', error_2) print('Error for rho 3: ', error_3)
def f(tau, tau2, r2, r2Dot): res = 1 - (tau - tau2)**2 / (2 * odlib.mag(r2)**3) res += odlib.dot(r2, r2Dot) / (2 * odlib.mag(r2)**5) * (tau - tau2)**3 O4 = 3 * (odlib.dot(r2Dot, r2Dot) / odlib.mag(r2)**2 - 1 / odlib.mag(r2)**3) O4 += -15 * (odlib.dot(r2, r2Dot) / odlib.mag(r2)**2)**2 + 1 / odlib.mag(r2)**3 O4 *= ((tau - tau2)**4 / (24 * odlib.mag(r2)**3)) return res + O4
def orbitalElements(r, rDot): rMag = odlib.mag(r) vSqr = odlib.dot(rDot, rDot) a = 1 / (2 / rMag - vSqr) h = odlib.cross(r, rDot) hMag = odlib.mag(h) e = sqrt(1 - hMag**2 / a) i = acos(h[2] / hMag) cosOmega = -h[1] / (hMag * sin(i)) sinOmega = h[0] / (hMag * sin(i)) omega = odlib.sinCosToAngle(sinOmega, cosOmega) sinNu = (a * (1 - e**2) / hMag * (odlib.dot(r, rDot) / rMag)) / e cosNu = (a * (1 - e**2) / rMag - 1) / e nu = odlib.sinCosToAngle(sinNu, cosNu) cosU = (r[0] * cosOmega + r[1] * sinOmega) / rMag sinU = r[2] / (rMag * sin(i)) u = odlib.sinCosToAngle(sinU, cosU) lowerOmega = u - nu E = acos((1 - rMag / a) / e) M = E - e * sin(E) return [a, e, i, omega, lowerOmega, M]
r2_dot, r2_dot_old) > 10**-30 and iteration < 1000: # calculating rho vector r = [] for i in range(3): D[0, i] = od.dot(od.cross(R[i], rho_hat[1]), rho_hat[2]) D[1, i] = od.dot(od.cross(rho_hat[0], R[i]), rho_hat[2]) D[2, i] = od.dot(rho_hat[0], od.cross(rho_hat[1], R[i])) rho[i] = (a1 * D[i, 0] + a2 * D[i, 1] + a3 * D[i, 2]) / (a[i] * D0) r.append(rho[i] * rho_hat[i] - R[i]) r = np.array(r) if iteration == 0: r2_dot = (r[2] - r[0]) / T0 else: r2_dot = b1 * r[0] + b3 * r[2] r2_mag = od.mag(r[1]) # time correction c = 173.145 # c in au / day T0 = k * ((t3 - rho[2] / c) - (t1 - rho[0] / c)) T1 = k * ((t1 - rho[0] / c) - (t2 - rho[1] / c)) T3 = k * ((t3 - rho[2] / c) - (t2 - rho[1] / c)) a1 = T3 / T0 a2 = -1 a3 = -T1 / T0 a = [a1, a2, a3] # defining f and g series def f(tau): series_pt1 = 1-(tau**2)/(2*r2_mag**3) + \
rvec1 = od.co_rot(pos, -W1, 'z') rvec2 = od.co_rot(rvec1, -inc1, 'x') rvec_ec = od.co_rot(rvec2, -Ome1, 'z') rvec = od.co_rot(rvec_ec, -23.4367505323, 'x') # vector of sun to earth R = [-6.574011197988899E-01, 7.092445968434601E-01, 3.074588261788798E-01] # calculating rho and rho hat rho = [rvec[0] + R[0], rvec[1] + R[1], rvec[2] + R[2]] rho_h = [rho[0] / od.mag(rho), rho[1] / od.mag(rho), rho[2] / od.mag(rho)] # calculating RA and Dec dec = asin(rho_h[2]) cosA = rho_h[0] / cos(dec) sinA = rho_h[1] / cos(dec) # convert into degrees and isolate for RA ra = od.rads(sinA, cosA) * 180 / pi dec = dec * 180 / pi ra = od.RAdecimalToHMS(ra) dec = od.DECdecimalToDMS(dec)
E = E - (M - realM) / derivM return E n = sqrt(1 / a**3) T = t2 - M2 / (n * odlib.k) M = M2 + n * odlib.k * (t - t2) foundE = newtonFindE(e, M) r = [a * cos(foundE) - a * e, a * sqrt(1 - e**2) * sin(foundE), 0] r = odlib.rotateVectorZ(r, -w) r = odlib.rotateVectorX(r, -i) r = odlib.rotateVectorZ(r, -omega) r = np.array(odlib.rotateVectorX(r, odlib.degreesToRadians(-eclipticObliquity))) print(r) rho = R + r rhoHat = rho / odlib.mag(rho) dec = asin(rhoHat[2]) cosRA = rhoHat[0] / cos(dec) sinRA = rhoHat[1] / cos(dec) RA = odlib.sinCosToAngle(sinRA, cosRA) print("Predicted RA:\t[17, 42, 21.12]\nPredicted DEC:\t[31, 52, 26.7]") print( f"Calculated RA: \t{odlib.RAdecimalToHMS(RA*180/pi)}\nCalculated DEC:\t{odlib.DECdecimalToDMS(dec*180/pi)}" )
def g(tau, tau2, r2, r2Dot): res = tau - tau2 - (tau - tau2)**3 / (6 * odlib.mag(r2)**3) res += (odlib.dot(r2, r2Dot) * (tau - tau2)**4) / (4 * odlib.mag(r2)**5) return res
def od(t1, t2, t3, ra1, dec1, ra2, dec2, ra3, dec3, R): tau3 = odlib.timeToGaussian(t3, t2) tau1 = odlib.timeToGaussian(t1, t2) tau0 = tau3 - tau1 realR2Dot = (realR3 - realR1) / tau0 if radecAsDecimal: rhoHat1 = getRhoHat(ra1, dec1) rhoHat2 = getRhoHat(ra2, dec2) rhoHat3 = getRhoHat(ra3, dec3) else: rhoHat1 = getRhoHat(*ra1, *dec1) rhoHat2 = getRhoHat(*ra2, *dec2) rhoHat3 = getRhoHat(*ra3, *dec3) a = [tau3 / tau0, -1, -tau1 / tau0] rhoHats = np.array([rhoHat1, rhoHat2, rhoHat3]) rho = rhoHats.copy() D0 = odlib.tri_product(rhoHat1, rhoHat2, rhoHat3) assert (D0 != 0) D1 = odlib.tri_product(rhoHat3, R[0], rhoHat2) D2 = odlib.tri_product(rhoHat3, R[1], rhoHat2) D3 = odlib.tri_product(rhoHat3, R[2], rhoHat2) rho[0] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[0] * D0) D1 = odlib.tri_product(rhoHat3, rhoHat1, R[0]) D2 = odlib.tri_product(rhoHat3, rhoHat1, R[1]) D3 = odlib.tri_product(rhoHat3, rhoHat1, R[2]) rho[1] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[1] * D0) D1 = odlib.tri_product(rhoHat1, rhoHat2, R[0]) D2 = odlib.tri_product(rhoHat1, rhoHat2, R[1]) D3 = odlib.tri_product(rhoHat1, rhoHat2, R[2]) rho[2] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[2] * D0) r = rho - R r2Dot = (r[2] - r[0]) / tau0 tauAt1 = odlib.timeToGaussian(t1) tauAt2 = odlib.timeToGaussian(t2) tauAt3 = odlib.timeToGaussian(t3) def f(tau, tau2, r2, r2Dot): res = 1 - (tau - tau2)**2 / (2 * odlib.mag(r2)**3) res += odlib.dot(r2, r2Dot) / (2 * odlib.mag(r2)**5) * (tau - tau2)**3 O4 = 3 * (odlib.dot(r2Dot, r2Dot) / odlib.mag(r2)**2 - 1 / odlib.mag(r2)**3) O4 += -15 * (odlib.dot(r2, r2Dot) / odlib.mag(r2)**2)**2 + 1 / odlib.mag(r2)**3 O4 *= ((tau - tau2)**4 / (24 * odlib.mag(r2)**3)) return res + O4 def g(tau, tau2, r2, r2Dot): res = tau - tau2 - (tau - tau2)**3 / (6 * odlib.mag(r2)**3) res += (odlib.dot(r2, r2Dot) * (tau - tau2)**4) / (4 * odlib.mag(r2)**5) return res tiny = 1E-9 maxIterations = 200 iteration = 0 # Minus 100 so the loop doesn't exit immediately r2Old = r[1] - 100 r2DotOld = r2Dot while (np.any(abs(r[1] - r2Old) > tiny) or np.any(abs(r2Dot - r2DotOld) > tiny)) and iteration < maxIterations: r2Old = r[1].copy() r2DotOld = r2Dot.copy() if useLightspeedCorrection: # Lightspeed correction correctedT1 = t1 - odlib.mag(rho[0]) / c correctedT2 = t2 - odlib.mag(rho[1]) / c correctedT3 = t3 - odlib.mag(rho[2]) / c tauAt1 = odlib.timeToGaussian(correctedT1) tauAt2 = odlib.timeToGaussian(correctedT2) tauAt3 = odlib.timeToGaussian(correctedT3) rho = rhoHats.copy() D0 = odlib.tri_product(rhoHat1, rhoHat2, rhoHat3) assert (D0 != 0) D1 = odlib.tri_product(rhoHat3, R[0], rhoHat2) D2 = odlib.tri_product(rhoHat3, R[1], rhoHat2) D3 = odlib.tri_product(rhoHat3, R[2], rhoHat2) rho[0] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[0] * D0) D1 = odlib.tri_product(rhoHat3, rhoHat1, R[0]) D2 = odlib.tri_product(rhoHat3, rhoHat1, R[1]) D3 = odlib.tri_product(rhoHat3, rhoHat1, R[2]) rho[1] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[1] * D0) D1 = odlib.tri_product(rhoHat1, rhoHat2, R[0]) D2 = odlib.tri_product(rhoHat1, rhoHat2, R[1]) D3 = odlib.tri_product(rhoHat1, rhoHat2, R[2]) rho[2] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[2] * D0) r = rho - R f1 = f(tauAt1, tauAt2, r[1], r2Dot) f3 = f(tauAt3, tauAt2, r[1], r2Dot) g1 = g(tauAt1, tauAt2, r[1], r2Dot) g3 = g(tauAt3, tauAt2, r[1], r2Dot) a[0] = g3 / (f1 * g3 - f3 * g1) a[2] = g1 / (f3 * g1 - f1 * g3) r[1] = a[0] * r[0] + a[2] * r[2] r2Dot = f3 / (g1 * f3 - g3 * f1) * r[0] + f1 / (g3 * f1 - g1 * f3) * r[2] iteration += 1 assert (iteration < maxIterations) # print(r) r[1] = odlib.rotateVector(r[1], 0, odlib.degreesToRadians(eclipticObliquity), 0) r2Dot = odlib.rotateVector(r2Dot, 0, odlib.degreesToRadians(eclipticObliquity), 0) elements = orbitalElements(r[1], r2Dot) elements[2] = odlib.radiansToDegrees(elements[2]) elements[3] = odlib.radiansToDegrees(elements[3]) elements[4] = odlib.radiansToDegrees(elements[4]) elements[5] = odlib.radiansToDegrees(elements[5]) while elements[4] < 0: elements[4] += 360 while elements[4] > 360: elements[4] -= 360 return elements
def rhoHatFromRho(rho): rhoMag = odlib.mag(rho) return [rho[0] / rhoMag, rho[1] / rhoMag, rho[2] / rhoMag]
def orbit_ele(x_pos1, y_pos1, z_pos1, x_pos2, y_pos2, z_pos2, J1, J2): k = 0.01720209847 r_dot = [(x_pos2 - x_pos1) / (k * (J2 - J1)), (y_pos2 - y_pos1) / (k * (J2 - J1)), (z_pos2 - z_pos1) / (k * (J2 - J1))] r = [x_pos1, y_pos1, z_pos1] r_mag = od.mag(r) h = od.cross(r, r_dot) h_x = h[0] h_y = h[1] h_z = h[2] h_mag = od.mag(h) v2 = od.dot(r_dot, r_dot) a = 1 / ((2 / r_mag) - v2) ecc = (1 - (h_mag)**2 / a)**.5 inc = acos(h_z / h_mag) cO = -h_y / (h_mag * sin(inc)) # cos of Omega sO = h_x / (h_mag * sin(inc)) # sin of Omega Ome = od.rads(sO, cO) cU = (x_pos1 * cO + y_pos1 * sO) / r_mag # cos of U sU = z_pos1 / (r_mag * sin(inc)) # sin of U U = od.rads(sU, cU) cV = (1 / ecc) * ((a * (1 - ecc**2)) / r_mag - 1) # cos of V sV = (1 / ecc) * (((a * (1 - ecc**2)) / h_mag) * (od.dot(r, r_dot)) / r_mag) V = od.rads(sV, cV) w = U - V E = acos((1 / ecc) * (1 - r_mag / a)) M = E - ecc * sin(E) inc = inc * 180 / pi U = U * 180 / pi V = V * 180 / pi Ome = Ome * 180 / pi w = w * 180 / pi if w < 0: w = 360 + w E = E * 180 / pi M = M * 180 / pi experi = [a, ecc, inc, Ome, V, w, M] expect = [ 1.056800057682216, 3.442331106521022 * 10**-1, 2.515525601713781 * 10, 236.2379793903064, 1.589559274581728 * 10**2, 255.5046093427637, 1.404194621765141 * 10**2 ] per_error = [] for i in range(len(experi)): per_error.append(abs(experi[i] - expect[i]) / expect[i] * 100) print('Semi-major axis: ', a, ', Expected: ', expect[0], ', % error: ', per_error[0]) print('Eccentricity: ', ecc, ', Expected: ', expect[1], ', % error: ', per_error[1]) print('Inclination (degrees): ', inc, ', Expected: ', expect[2], ', % error: ', per_error[2]) print('Longitude of the Ascending Node (degrees): ', Ome, ', Expected: ', expect[3], ', % error: ', per_error[3]) print('True Anomoly (degrees): ', V, ', Expected: ', expect[4], ', % error: ', per_error[4]) print('Argument of Perihelion, (degrees): ', w, ', Expected: ', expect[5], ', % error: ', per_error[5]) print('Mean Anomaly (degrees): ', M, ', Expected: ', expect[6], ', % error: ', per_error[6])
res = tau - tau2 - (tau - tau2)**3 / (6 * odlib.mag(r2)**3) res += (odlib.dot(r2, r2Dot) * (tau - tau2)**4) / (4 * odlib.mag(r2)**5) return res tiny = 1E-9 maxIterations = 200 iteration = 0 # Minus 100 so the loop doesn't exit immediately r2Old = r[1] - 100 r2DotOld = r2Dot while (np.any(abs(r[1] - r2Old) > tiny) or np.any(abs(r2Dot - r2DotOld) > tiny)) and iteration < maxIterations: r2Old = r[1].copy() r2DotOld = r2Dot.copy() # Lightspeed correction correctedT1 = t1 - odlib.mag(rho[0]) / c correctedT2 = t2 - odlib.mag(rho[1]) / c correctedT3 = t3 - odlib.mag(rho[2]) / c tauAt1 = odlib.timeToGaussian(correctedT1) tauAt2 = odlib.timeToGaussian(correctedT2) tauAt3 = odlib.timeToGaussian(correctedT3) rho = rhoHats.copy() D0 = odlib.tri_product(rhoHat1, rhoHat2, rhoHat3) assert(D0 != 0) D1 = odlib.tri_product(rhoHat3, R[0], rhoHat2) D2 = odlib.tri_product(rhoHat3, R[1], rhoHat2) D3 = odlib.tri_product(rhoHat3, R[2], rhoHat2) rho[0] *= (a[0] * D1 + a[1] * D2 + a[2] * D3) / (a[0] * D0)