def _mga_part_plot(self, x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams["legend.fontsize"] = 10 fig = plt.figure() ax = fig.gca(projection="3d", aspect="equal") ax.scatter(0, 0, 0, color="y") JR = 71492000.0 legs = len(x) / 4 seq = self.get_sequence() common_mu = seq[0].mu_central_body start_mjd2000 = self.t0.mjd2000 # 1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = x[3::4] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (legs + 1)) r_P = list([None] * (legs + 1)) v_P = list([None] * (legs + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(start_mjd2000 + sum(T[:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=JR) v_end_l = [a + b for a, b in zip(v_P[0], self.vinf_in)] # 4 - And we iterate on the legs for i in xrange(0, legs): # Fly-by v_out = fb_prop(v_end_l, v_P[i], x[1 + 4 * i] * seq[i - 1].radius, x[4 * i], seq[i].mu_self) # s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler( ax, r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color="b", legend=False, units=JR ) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, common_mu, False, False) plot_lambert(ax, l, sol=0, color="r", legend=False, units=JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] plt.show() return ax
def _compute_DV_DT_incipit(self,x): """ This method computes, for each leg, all the velocity increments coming from deep space manoeuvres and all the transfer times. Use: DV,DT = prob.compute_DV_DT(x) * x: trajectory encoding """ from PyKEP import epoch, lambert_problem, DAY2SEC, fb_prop, propagate_lagrangian from math import pi, acos,cos,sin,sqrt from scipy.linalg import norm #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] n_legs = len(x)/4 seq = self.get_sequence() common_mu = seq[0].mu_central_body #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (n_legs)) r_P = list([None] * (n_legs)) v_P = list([None] * (n_legs)) DV = list([None] * (n_legs)) for i,planet in enumerate(seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*seq[i-1].radius,x[4*i],seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) return (DV,T)
def _get_score_data_part(self,x): from PyKEP import epoch, lambert_problem, DAY2SEC, fb_prop, propagate_lagrangian from math import pi, acos,cos,sin,sqrt from scipy.linalg import norm from copy import deepcopy """ This method returns the data needed to compute the score of a trajectory. """ #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] nlegs = len(x)/4 seq = self.get_sequence() common_mu = seq[0].mu_central_body t0 = self.t0.mjd2000 vinf_in = deepcopy(self.vinf_in) #2 - We compute the epochs and ephemerides of the planetary encounters ep_list = list([None] * (nlegs+1)) t_P = list([None] * (nlegs+1)) r_P = list([None] * (nlegs+1)) v_P = list([None] * (nlegs+1)) DV = list([None] * nlegs) for i,planet in enumerate(seq): ep_list[i] = t0+sum(T[:i]) t_P[i] = epoch(t0+sum(T[:i])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) #init lists for fly-by parameters vinf_list = [] rp_list = [] beta_list = [] v_end_l = [a+b for a,b in zip(vinf_in, v_P[0])] #3 - And we proceed with each successive leg for i in xrange(nlegs): #Fly-by v_out = fb_prop(v_end_l,v_P[i],x[1+4*i]*seq[i].radius,x[4*i],seq[i].mu_self) vinf_list.append( [a-b for a,b in zip(v_end_l,v_P[i])] ) rp_list.append(x[1+4*i]*seq[i].radius) beta_list.append(x[4*i]) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i+1],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] vinf_list.append([a-b for a,b in zip(v_end_l,v_P[-1])]) rp_list.append(None) beta_list.append(None) return zip(ep_list, seq, vinf_list, rp_list, beta_list)
def _mga_1dsm_tof_plot(self, x): """ Plots the trajectory represented by the decision vector x """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') seq = self.get_sequence() # 2 - We plot the first leg r_P0, v_P0 = seq[0].eph(epoch(x[0])) plot_planet(ax, seq[0], t0=epoch(x[0]), color=( 0.8, 0.6, 0.8), legend=True, units = AU) r_P1, v_P1 = seq[1].eph(epoch(x[0] + x[5])) theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) v0 = [a + b for a, b in zip(v_P0, [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian( r_P0, v0, x[4] * x[5] * DAY2SEC, seq[0].mu_central_body) plot_kepler( ax, r_P0, v0, x[4] * x[5] * DAY2SEC, seq[0].mu_central_body, N=100, color='b', legend=False, units=AU) # Lambert arc to reach seq[1] dt = (1 - x[4]) * x[5] * DAY2SEC l = lambert_problem(r, r_P1, dt, seq[0].mu_central_body) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] vinf_in = [a - b for a, b in zip(v_end_l, v_P1)] _part_plot(x[6:], AU, ax, seq[1:], x[0] + x[5], vinf_in) return ax
def plot_kepler(r, v, t, mu, N=60, units=1, color='b', legend=False, ax=None): """ ax = plot_kepler(r, v, t, mu, N=60, units=1, color='b', legend=False, ax=None): - ax: 3D axis object created using fig.gca(projection='3d') - r: initial position (cartesian coordinates) - v: initial velocity (cartesian coordinates) - t: propagation time - mu: gravitational parameter - N: number of points to be plotted along one arc - units: the length unit to be used in the plot - color: matplotlib color to use to plot the line - legend when True it plots also the legend Plots the result of a keplerian propagation """ from PyKEP import propagate_lagrangian import matplotlib.pylab as plt from mpl_toolkits.mplot3d import Axes3D if ax is None: fig = plt.figure() axis = fig.gca(projection='3d') else: axis = ax # We define the integration time ... dt = t / (N - 1) # ... and calculate the cartesian components for r x = [0.0] * N y = [0.0] * N z = [0.0] * N # We calculate the spacecraft position at each dt for i in range(N): x[i] = r[0] / units y[i] = r[1] / units z[i] = r[2] / units r, v = propagate_lagrangian(r, v, dt, mu) # And we plot if legend: label = 'ballistic arc' else: label = None axis.plot(x, y, z, c=color, label=label) if legend: axis.legend() if ax is None: # show only if axis is not set plt.show() return axis
def planet_planet(start_planet, arrive_planet, tlaunch, tarrive, rev, N): # Create PyKEP epoch objects and calculate flight time t1 = epoch(tlaunch) t2 = epoch(tarrive) dt = (tarrive - tlaunch) * DAY2SEC OBJ1 = planet_ss(start_planet) OBJ2 = planet_ss(arrive_planet) # Calculate location of objects in flight path r1, v1 = OBJ1.eph(t1) r2, v2 = OBJ2.eph(t2) # Find trajectory l = lambert_problem(r1, r2, dt, MU_SUN) #extract relevant information from solution r = l.get_r1() v = l.get_v1()[0] mu = l.get_mu() #define the integration time dtn = dt / (N - 1) dtn_days = dtn * SEC2DAY #alocate the cartesian components for r t = np.array([0.0] * N) x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([0.0] * N) #calculate the spacecraft position at each dt for i in range(N): t[i] = tlaunch + dtn_days * i x[i] = r[0] / AU y[i] = r[1] / AU z[i] = r[2] / AU r, v = propagate_lagrangian(r, v, dtn, mu) #traj = [t, x, y, z] vin = l.get_v1()[rev] vout = l.get_v2()[rev] #dV=fb_vel(vin,vout,planet_ss(arrive_planet)) #dV=np.sqrt( np.square(vin[0]/vout[0])+np.square(vin[1]/vout[1])+np.square(vin[2]/vout[2])) #dV=np.sqrt( np.square(vin[0]-v1[0])+np.square(v1[1]-vin[1])+np.square(v1[2]-vin[2])) #dV=np.sqrt( np.square(v2[0]-vout[0])+np.square(v2[1]-vout[1])+np.square(v2[2]-vout[2])) #dV=np.sqrt( np.square(v1[0]/vin[0])+np.square(v1[1]/vin[1])+np.square(v1[2]/vin[2])) C3_launch = (np.sqrt(np.square(vin[0] - v1[0]) + np.square(vin[1] - v1[1]) + np.square(vin[2] - v1[2]))) ** 2 C3_arrive = (np.sqrt(np.square(vout[0] - v2[0]) + np.square(vout[1] - v2[1]) + np.square(vout[2] - v2[2]))) ** 2 C3 = np.sqrt((C3_arrive ** 2) + (C3_launch ** 2)) return C3
def _part_plot(x, units, axis, seq, start_mjd2000, vinf_in): """ Plots the trajectory represented by a decision vector x = [beta,rp,eta,T] * N associated to a sequence seq, a start_mjd2000 and an incoming vinf_in """ from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm legs = len(x) // 4 common_mu = seq[0].mu_central_body # 1 - we 'decode' the chromosome recording the various times of flight # (days) in the list T T = x[3::4] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (legs + 1)) r_P = list([None] * (legs + 1)) v_P = list([None] * (legs + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(start_mjd2000 + sum(T[:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(planet, t0=t_P[i], color=( 0.8, 0.6, 0.8), legend=True, units = units, ax=axis) v_end_l = [a + b for a, b in zip(v_P[0], vinf_in)] # 4 - And we iterate on the legs for i in range(0, legs): # Fly-by v_out = fb_prop(v_end_l, v_P[i], x[1 + 4 * i] * seq[i].radius, x[4 * i], seq[i].mu_self) # s/c propagation before the DSM r, v = propagate_lagrangian( r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler(r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color='b', legend=False, units=units, ax=axis) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, common_mu, False, False) plot_lambert( l, sol=0, color='r', legend=False, units=units, N=500, ax=axis) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0]
def positions_lambert(self, l, sol=0, units = 1.0, index = 0): """ Plots a particular solution to a Lambert's problem USAGE: plot_lambert(ax,l, N=60, sol=0, units = 'PyKEP.AU', legend = 'False') * l: PyKEP.lambert_problem object * sol: solution to the Lambert's problem we want to plot (must be in 0..Nmax*2) where Nmax is the maximum number of revolutions for which there exist a solution. * units: the length unit to be used in the plot """ from PyKEP import propagate_lagrangian, AU if sol > l.get_Nmax()*2: raise ValueError("sol must be in 0 .. NMax*2 \n * Nmax is the maximum number of revolutions for which there exist a solution to the Lambert's problem \n * You can compute Nmax calling the get_Nmax() method of the lambert_problem object") #We extract the relevant information from the Lambert's problem r = l.get_r1() v = l.get_v1()[sol] T = l.get_tof() mu = l.get_mu() #We define the integration time ... if T/86400. < 1: N = int(T) #...compute number of points... dt = T / (N-1.) else: N = int(2*T/86400.) #...compute number of points... dt = T / (N-1.) timetuple = [i*dt for i in range(N)] #... and alocate the cartesian components for r x = [0.0]*N y = [0.0]*N z = [0.0]*N #We calculate the spacecraft position at each dt for i in range(N): x[i] = r[0]/units y[i] = r[1]/units z[i] = r[2]/units r,v = propagate_lagrangian(r,v,dt,mu) self.trajectory_positions.append([index, x, y, z, timetuple])
def _objfun_impl(self,x): #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*self.seq[i-1].radius,x[4*i],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) return (sum(DV),)
def plot_kepler(ax,r,v,t,mu, N=60, units = 1, color = 'b', legend = False): """ Plots the result of a keplerian propagation USAGE: plot_kepler(ax,r,v,t,mu, N=60, units = 1, color = 'b', legend = False): * ax: 3D axis object created using fig.gca(projection='3d') * r: initial position (cartesian coordinates) * v: initial velocity (cartesian coordinates) * t: propagation time * mu: gravitational parameter * N: number of points to be plotted along one arc * units: the length unit to be used in the plot * color: matplotlib color to use to plot the line * legend when True it plots also the legend """ from PyKEP import propagate_lagrangian #We define the integration time ... dt = t / (N-1) #... and calculate the cartesian components for r x = [0.0]*N y = [0.0]*N z = [0.0]*N #We calculate the spacecraft position at each dt for i in range(N): x[i] = r[0]/units y[i] = r[1]/units z[i] = r[2]/units r,v = propagate_lagrangian(r,v,dt,mu) #And we plot if legend: label = 'ballistic arc' else: label = None ax.plot(x, y, z, c=color, label=label) if legend: ax.legend()
def positions_kepler(self, r,v,t,mu, units = 1, index = 0): """ Plots the result of a keplerian propagation USAGE: plot_kepler(ax,r,v,t,mu, N=60, units = 1, color = 'b', legend = False): * r: initial position (cartesian coordinates) * v: initial velocity (cartesian coordinates) * t: propagation time * mu: gravitational parameter * units: the length unit to be used in the plot """ from PyKEP import propagate_lagrangian #We define the integration time ... if t/86400. < 1: N = int(t) #...compute number of points... dt = t / (N-1.) else: N = int(2*t/86400.) #...compute number of points... dt = t / (N-1.) timetuple = [i*dt for i in range(N)] #... and calcuate the cartesian components for r x = [0.0]*N y = [0.0]*N z = [0.0]*N #We calculate the spacecraft position at each dt for i in range(N): x[i] = r[0]/units y[i] = r[1]/units z[i] = r[2]/units r,v = propagate_lagrangian(r,v,dt,mu) self.trajectory_positions.append([index, x, y, z, timetuple])
def pretty(self,x): """ Prints human readable information on the trajectory represented by the decision vector x Example:: prob.pretty(x) """ #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0]*(self.__n_legs)) for i in xrange(self.__n_legs): T[i] = (x[4+4*i]/sum(x[4::4]))*x[3] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) close_d = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] close_d[0] = closest_distance(r,v_beg_l, r_P[0], v_end_l, self.common_mu)[0] / JR #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) print "\nFirst Leg: 1000JR to " + self.seq[0].name print "\tDeparture: " + str(t_P[0]) + " (" + str(t_P[0].mjd2000) + " mjd2000) " print "\tDuration: " + str(T[0]) + "days" print "\tInitial Velocity Increment (m/s): " + str(DV[0]) print "\tArrival relative velocity at " + self.seq[0].name +" (m/s): " + str(norm([a-b for a,b in zip(v_end_l,v_P[0])])) print "\tClosest approach distance: " + str(close_d[0]) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[6+(i-1)*4]*self.seq[i-1].radius,x[5+(i-1)*4],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[7+(i-1)*4]*T[i]*DAY2SEC,self.common_mu) tmp, ra = closest_distance(r_P[i-1],v_out, r,v, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[7+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r,v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp/JR ra = ra/JR else: close_d[i] = tmp2/JR ra = ra2/JR #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) print "\nleg no. " + str(i+1) + ": " + self.seq[i-1].name + " to " + self.seq[i].name print "\tDuration (days): " + str(T[i]) print "\tFly-by epoch: " + str(t_P[i]) + " (" + str(t_P[i].mjd2000) + " mjd2000) " print "\tFly-by altitude (km): " + str((x[6+(i-1)*4]*self.seq[i-1].radius-self.seq[i-1].radius)/1000) print "\tDSM after (days): " + str(x[7+(i-1)*4]*T[i]) print "\tDSM magnitude (m/s): " + str(DV[i]) print "\tClosest approach distance: " + str(close_d[i]) print "\tApoapsis at closest distance: " + str(ra) print "\tV in (m/s): " + str(v_end_l) print "\tV out (m/s): " + str(v_out) print "\nArrival at " + self.seq[-1].name vel_inf = [a-b for a,b in zip(v_end_l,v_P[-1])] print "Arrival epoch: " + str(t_P[-1]) + " (" + str(t_P[-1].mjd2000) + " mjd2000) " print "Arrival Vinf (m/s): " + vel_inf.__repr__() + " - " + str(norm(vel_inf)) print "Total mission time (days): " + str(sum(T)) print "Total DV (m/s): " + str(sum(DV))
def plot(self,x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0,0,0, color='y') #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0]*(self.__n_legs)) for i in xrange(self.__n_legs): T[i] = (x[4+4*i]/sum(x[4::4]))*x[3] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8,0.6,0.8), legend=True, units = JR) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) plot_lambert(ax,l, sol = 0, color='k', legend=False, units = JR, N=500) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[6+(i-1)*4]*self.seq[i-1].radius,x[5+(i-1)*4],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+3]*T[i]*DAY2SEC,self.common_mu) plot_kepler(ax,r_P[i-1],v_out,x[7+(i-1)*4]*T[i]*DAY2SEC,self.common_mu,N = 500, color='b', legend=False, units = JR) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[7+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) plot_lambert(ax,l, sol = 0, color='r', legend=False, units = JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) plt.show()
def _mga_1dsm_tof_plot(self, x): """ Plots the trajectory represented by the decision vector x """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') seq = self.get_sequence() n = (len(seq) - 1) #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = x[5::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (n + 1)) r_P = list([None] * (n + 1)) v_P = list([None] * (n + 1)) DV = list([None] * (n + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=AU) #3 - We start with the first leg theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian(r_P[0], v0, x[4] * T[0] * DAY2SEC, seq[0].mu_central_body) plot_kepler(ax, r_P[0], v0, x[4] * T[0] * DAY2SEC, seq[0].mu_central_body, N=100, color='b', legend=False, units=AU) #Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, seq[0].mu_central_body) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at time nu1*T1 DV[0] = norm([a - b for a, b in zip(v_beg_l, v)]) #4 - And we proceed with each successive leg for i in range(1, n): #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * seq[i].radius, x[6 + (i - 1) * 4], seq[i].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, seq[0].mu_central_body) plot_kepler(ax, r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, seq[0].mu_central_body, N=100, color='b', legend=False, units=AU) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, seq[0].mu_central_body) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occurring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) return ax
def _leg_get_states(self): """ Returns the spacecraft states (t,r,v,m) at the leg grid points Examples:: times,r,v,m = PyKEP.sims_flanagan.leg.get_states() """ from PyKEP import propagate_lagrangian, AU, DAY2SEC, G0, propagate_taylor import numpy as np from scipy.linalg import norm from math import exp # We compute the number of segments for forward and backward propagation n_seg = len(self.get_throttles()) fwd_seg = (n_seg + 1) // 2 back_seg = n_seg // 2 # We extract information on the spacecraft sc = self.get_spacecraft() isp = sc.isp max_thrust = sc.thrust # And on the leg throttles = self.get_throttles() mu = self.get_mu() # time grid t_grid = [0.0] * (n_seg * 2 + 2) # Forward propagation # x,y,z contain the cartesian components of all points (grid+midpints) x = [0.0] * (fwd_seg * 2 + 1) y = [0.0] * (fwd_seg * 2 + 1) z = [0.0] * (fwd_seg * 2 + 1) vx = [0.0] * (fwd_seg * 2 + 1) vy = [0.0] * (fwd_seg * 2 + 1) vz = [0.0] * (fwd_seg * 2 + 1) mass = [0.0] * (fwd_seg * 2 + 1) state = self.get_xi() # Initial conditions r = state.r v = state.v m = state.m x[0], y[0], z[0] = r vx[0], vy[0], vz[0] = v mass[0] = m # We compute all points by propagation for i, t in enumerate(throttles[:fwd_seg]): t_grid[2 * i] = t.start.mjd2000 t_grid[2 * i + 1] = t.start.mjd2000 + \ (t.end.mjd2000 - t.start.mjd2000) / 2. dt = (t.end.mjd - t.start.mjd) * DAY2SEC alpha = min(norm(t.value), 1.0) # Keplerian propagation and dV application if self.high_fidelity is False: dV = [max_thrust / m * dt * dumb for dumb in t.value] r, v = propagate_lagrangian(r, v, dt / 2, mu) x[2 * i + 1], y[2 * i + 1], z[2 * i + 1] = r vx[2 * i + 1], vy[2 * i + 1], vz[2 * i + 1] = v mass[2 * i + 1] = m # v= v+dV v = [a + b for a, b in zip(v, dV)] r, v = propagate_lagrangian(r, v, dt / 2, mu) m *= exp(-norm(dV) / isp / G0) x[2 * i + 2], y[2 * i + 2], z[2 * i + 2] = r vx[2 * i + 2], vy[2 * i + 2], vz[2 * i + 2] = v mass[2 * i + 2] = m # Taylor propagation of constant thrust u else: u = [max_thrust * dumb for dumb in t.value] r, v, m = propagate_taylor( r, v, m, u, dt / 2, mu, isp * G0, -12, -12) x[2 * i + 1], y[2 * i + 1], z[2 * i + 1] = r vx[2 * i + 1], vy[2 * i + 1], vz[2 * i + 1] = v mass[2 * i + 1] = m r, v, m = propagate_taylor( r, v, m, u, dt / 2, mu, isp * G0, -12, -12) x[2 * i + 2], y[2 * i + 2], z[2 * i + 2] = r vx[2 * i + 2], vy[2 * i + 2], vz[2 * i + 2] = v mass[2 * i + 2] = m t_grid[2 * i + 2] = t.end.mjd2000 # Backward propagation # x,y,z will contain the cartesian components of x_back = [0.123] * (back_seg * 2 + 1) y_back = [0.123] * (back_seg * 2 + 1) z_back = [0.123] * (back_seg * 2 + 1) vx_back = [0.0] * (back_seg * 2 + 1) vy_back = [0.0] * (back_seg * 2 + 1) vz_back = [0.0] * (back_seg * 2 + 1) mass_back = [0.0] * (back_seg * 2 + 1) state = self.get_xf() # Final conditions r = state.r v = state.v m = state.m x_back[-1], y_back[-1], z_back[-1] = r vx_back[-1], vy_back[-1], vz_back[-1] = v mass_back[-1] = m for i, t in enumerate(throttles[-1:-back_seg - 1:-1]): t_grid[-2 * i - 2] = t.end.mjd2000 - \ (t.end .mjd2000 - t.start.mjd2000) / 2. t_grid[-2 * i - 1] = t.end.mjd2000 dt = (t.end.mjd - t.start.mjd) * DAY2SEC alpha = min(norm(t.value), 1.0) if self.high_fidelity is False: dV = [max_thrust / m * dt * dumb for dumb in t.value] r, v = propagate_lagrangian(r, v, -dt / 2, mu) x_back[-2 * i - 2], y_back[-2 * i - 2], z_back[-2 * i - 2] = r vx_back[-2 * i - 2], vy_back[-2 * i - 2], vz_back[-2 * i - 2] = v mass_back[-2 * i - 2] = m # v= v+dV v = [a - b for a, b in zip(v, dV)] r, v = propagate_lagrangian(r, v, -dt / 2, mu) m *= exp(norm(dV) / isp / G0) x_back[-2 * i - 3], y_back[-2 * i - 3], z_back[-2 * i - 3] = r vx_back[-2 * i - 3], vy_back[-2 * i - 3], vz_back[-2 * i - 3] = v mass_back[-2 * i - 3] = m else: u = [max_thrust * dumb for dumb in t.value] r, v, m = propagate_taylor( r, v, m, u, -dt / 2, mu, isp * G0, -12, -12) x_back[-2 * i - 2], y_back[-2 * i - 2], z_back[-2 * i - 2] = r vx_back[-2 * i - 2], vy_back[-2 * i - 2], vz_back[-2 * i - 2] = v mass_back[-2 * i - 2] = m r, v, m = propagate_taylor( r, v, m, u, -dt / 2, mu, isp * G0, -12, -12) x_back[-2 * i - 3], y_back[-2 * i - 3], z_back[-2 * i - 3] = r vx_back[-2 * i - 3], vy_back[-2 * i - 3], vz_back[-2 * i - 3] = v mass_back[-2 * i - 3] = m t_grid[-2 * i - 3] = t.start.mjd2000 x = x + x_back y = y + y_back z = z + z_back vx = vx + vx_back vy = vy + vy_back vz = vz + vz_back mass = mass + mass_back return t_grid, list(zip(x, y, z)), list(zip(vx, vy, vz)), mass
def plot_lambert(l, N=60, sol=0, units=1.0, color='b', legend=False, ax=None, alpha=1.): """ ax = plot_lambert(l, N=60, sol=0, units='PyKEP.AU', legend='False', ax=None, alpha=1.) - ax: 3D axis object created using fig.gca(projection='3d') - l: PyKEP.lambert_problem object - N: number of points to be plotted along one arc - sol: solution to the Lambert's problem we want to plot (must be in 0..Nmax*2) where Nmax is the maximum number of revolutions for which there exist a solution. - units: the length unit to be used in the plot - color: matplotlib color to use to plot the line - legend: when True it plots also the legend with info on the Lambert's solution chosen Plots a particular solution to a Lambert's problem Example:: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.gca(projection='3d') t1 = epoch(0) t2 = epoch(640) dt = (t2.mjd2000 - t1.mjd2000) * DAY2SEC pl = planet_ss('earth') plot_planet(pl, t0=t1, ax=ax, color='k') rE,vE = pl.eph(t1) pl = planet_ss('mars') plot_planet(pl, t0=t2, ax=ax, color='r') rM, vM = pl.eph(t2) l = lambert_problem(rE,rM,dt,MU_SUN) plot_lambert(l, ax=ax, color='b') plot_lambert(l, sol=1, ax=ax, color='g') plot_lambert(l, sol=2, ax=ax, color='g') plt.show() """ from PyKEP import propagate_lagrangian, AU import numpy as np import matplotlib.pylab as plt from mpl_toolkits.mplot3d import Axes3D if ax is None: fig = plt.figure() axis = fig.gca(projection='3d') else: axis = ax if sol > l.get_Nmax() * 2: raise ValueError("sol must be in 0 .. NMax*2 \n * Nmax is the maximum number of revolutions for which there exist a solution to the Lambert's problem \n * You can compute Nmax calling the get_Nmax() method of the lambert_problem object") # We extract the relevant information from the Lambert's problem r = l.get_r1() v = l.get_v1()[sol] T = l.get_tof() mu = l.get_mu() # We define the integration time ... dt = T / (N - 1) # ... and alocate the cartesian components for r x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([0.0] * N) # We calculate the spacecraft position at each dt for i in range(N): x[i] = r[0] / units y[i] = r[1] / units z[i] = r[2] / units r, v = propagate_lagrangian(r, v, dt, mu) # And we plot if legend: label = 'Lambert solution (' + str((sol + 1) / 2) + ' revs.)' else: label = None axis.plot(x, y, z, c=color, label=label, alpha=alpha) if legend: axis.legend() if ax is None: # show only if axis is not set plt.show() return axis
def getTraj_simple(start_planet, arrive_planet, tlaunch, tarrive, N): ''' Finds a trajectory between two objects orbiting the Sun USAGE: traj = getTraj(K1, K2, tlaunch, tarrive) K: array of object parameters. epoch: epoch of Keplerian orbital elements (JD) a: semimajor axis (AU) e: eccentricity (none) i: inclination (deg) om: longitude of the ascending node (deg) w: argument of perihelion (deg) ma: mean anomaly at epoch (deg) mass: mass of object (kg) r: radius of object (m) sr: safe radius to approach object (m) K1: [epoch1,a1,e1,i1,om1,w1,ma1,mass1,r1,sr1] K2: [epoch2,a2,e2,i2,om2,w2,ma2,mass2,r2,sr2] tlaunch: launch time (JD) tarrive: arrival time (JD) N: number of points in calculated trajectory ''' import numpy as np from PyKEP import epoch, DAY2SEC, SEC2DAY, AU, DEG2RAD, MU_SUN, planet, lambert_problem, propagate_lagrangian, fb_vel # Create PyKEP epoch objects and calculate flight time t1 = epoch(tlaunch) t2 = epoch(tarrive) dt = (tarrive - tlaunch) * DAY2SEC rev=0 #number of revolutions before intercept OBJ1 = planet.jpl_lp(start_planet) OBJ2 = planet.jpl_lp(arrive_planet) # Calculate location of objects in flight path r1, v1 = OBJ1.eph(t1) r2, v2 = OBJ2.eph(t2) #Find trajectory l = lambert_problem(r1, r2, dt, MU_SUN) #extract relevant information from solution r = l.get_r1() v = l.get_v1()[0] mu = l.get_mu() #define the integration time dtn = dt / (N - 1) dtn_days = dtn * SEC2DAY #alocate the cartesian components for r t = np.array([0.0] * N) x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([0.0] * N) #calculate the spacecraft position at each dt for i in range(N): t[i] = tlaunch + dtn_days * i x[i] = r[0] / AU y[i] = r[1] / AU z[i] = r[2] / AU r, v = propagate_lagrangian(r, v, dtn, mu) #traj = [t, x, y, z] vin=l.get_v1()[rev] vout=l.get_v2()[rev] dV=fb_vel(vin,vout,planet.jpl_lp(arrive_planet)) #dV=np.sqrt( np.square(vout[0])+np.square(vout[1])+np.square(vout[2]))-np.sqrt( np.square(vin[0])+np.square(vin[1])+np.square(vin[2])) return dV
def traj_planet_asteroid(source, dest, tlaunch, tarrive, rev, N): t1 = epoch(tlaunch) t2 = epoch(tarrive) dt = (tarrive - tlaunch) * DAY2SEC target = source['orbit'] ep = epoch(jd_to_mjd(tlaunch), epoch.epoch_type.MJD) a = target["a"] * AU e = target["e"] i = target["i"] * DEG2RAD om = target["om"] * DEG2RAD w = target["w"] * DEG2RAD ma = target["ma"] * DEG2RAD as_mu = 1E17 * 6.67384E-11 # maybe need to calculate actual mass from density and radius r = (10 / 2) * 1000 sr = r * 1.1 OBJ1 = planet(ep, (a, e, i, om, w, ma), MU_SUN, as_mu, r, sr) target = dest['orbit'] ep = epoch(jd_to_mjd(tarrive), epoch.epoch_type.MJD) a = target["a"] * AU e = target["e"] i = target["i"] * DEG2RAD om = target["om"] * DEG2RAD w = target["w"] * DEG2RAD ma = target["ma"] * DEG2RAD as_mu = 1E17 * 6.67384E-11 # maybe need to calculate actual mass from density and radius r = (10 / 2) * 1000 sr = r * 1.1 OBJ2 = planet(ep, (a, e, i, om, w, ma), MU_SUN, as_mu, r, sr) # Calculate location of objects in flight path r1, v1 = OBJ1.eph(t1) r2, v2 = OBJ2.eph(t2) #Find trajectory l = lambert_problem(r1, r2, dt, MU_SUN) #extract relevant information from solution r = l.get_r1() v = l.get_v1()[0] mu = l.get_mu() #define the integration time dtn = dt / (N - 1) dtn_days = dtn * SEC2DAY #alocate the cartesian components for r t = np.array([0.0] * N) x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([0.0] * N) #calculate the spacecraft position at each dt for i in range(N): t[i] = tlaunch + dtn_days * i x[i] = r[0] / AU y[i] = r[1] / AU z[i] = r[2] / AU r, v = propagate_lagrangian(r, v, dtn, mu) traj = [t.tolist(), x.tolist(), y.tolist(), z.tolist()] return traj
def pretty(self, x): """ Prints human readable information on the trajectory represented by the decision vector x Example:: prob.pretty(x) """ #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) close_d = list([None] * (self.__n_legs)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] close_d[0] = closest_distance(r, v_beg_l, r_P[0], v_end_l, self.common_mu)[0] / JR #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) print "\nFirst Leg: 1000JR to " + self.seq[0].name print "\tDeparture: " + str(t_P[0]) + " (" + str( t_P[0].mjd2000) + " mjd2000) " print "\tDuration: " + str(T[0]) + "days" print "\tInitial Velocity Increment (m/s): " + str(DV[0]) print "\tArrival relative velocity at " + self.seq[ 0].name + " (m/s): " + str( norm([a - b for a, b in zip(v_end_l, v_P[0])])) print "\tClosest approach distance: " + str(close_d[0]) #4 - And we proceed with each successive leg for i in xrange(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[1 + 4 * i] * self.seq[i - 1].radius, x[4 * i], self.seq[i - 1].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, self.common_mu) tmp, ra = closest_distance(r_P[i - 1], v_out, r, v, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r, v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp / JR ra = ra / JR else: close_d[i] = tmp2 / JR ra = ra2 / JR #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) print "\nleg no. " + str(i + 1) + ": " + self.seq[ i - 1].name + " to " + self.seq[i].name print "\tDuration (days): " + str(T[i]) print "\tFly-by epoch: " + str(t_P[i]) + " (" + str( t_P[i].mjd2000) + " mjd2000) " print "\tFly-by altitude (km): " + str( (x[4 * i + 1] * self.seq[i - 1].radius - self.seq[i - 1].radius) / 1000) print "\tDSM after (days): " + str(x[4 * i + 2] * T[i]) print "\tDSM magnitude (m/s): " + str(DV[i]) print "\tClosest approach distance: " + str(close_d[i]) print "\tApoapsis at closest distance: " + str(ra) print "\nArrival at " + self.seq[-1].name vel_inf = [a - b for a, b in zip(v_end_l, v_P[-1])] print "Arrival epoch: " + str(t_P[-1]) + " (" + str( t_P[-1].mjd2000) + " mjd2000) " print "Arrival Vinf (m/s): " + vel_inf.__repr__() + " - " + str( norm(vel_inf)) print "Total mission time (days): " + str(sum(T))
def plot(self, x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = x[3::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=JR) #3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, self.common_mu, False, False) plot_lambert(ax, l, sol=0, color='k', legend=False, units=JR, N=500) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[1 + 4 * i] * self.seq[i - 1].radius, x[4 * i], self.seq[i - 1].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, self.common_mu) plot_kepler(ax, r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, self.common_mu, N=500, color='b', legend=False, units=JR) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, self.common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) plt.show() return ax
def plot(self, x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0] * (self.__n_legs)) #a[-i] = x[-1-(i-1)*4] for i in xrange(self.__n_legs - 1): j = i + 1 T[-j] = (x[5] - sum(T[-(j - 1):])) * x[-1 - (j - 1) * 4] T[0] = x[5] - sum(T) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) DV = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=AU) #3 - We start with the first leg theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian(r_P[0], v0, x[4] * T[0] * DAY2SEC, self.common_mu) plot_kepler(ax, r_P[0], v0, x[4] * T[0] * DAY2SEC, self.common_mu, N=100, color='b', legend=False, units=AU) #Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, self.common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at time nu1*T1 DV[0] = norm([a - b for a, b in zip(v_beg_l, v)]) #4 - And we proceed with each successive leg for i in range(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * self.seq[i].radius, x[6 + (i - 1) * 4], self.seq[i].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu) plot_kepler(ax, r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu, N=100, color='b', legend=False, units=AU) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, self.common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU, N=1000) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) plt.show()
def _mga_part_plot(self, x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d', aspect='equal') ax.scatter(0, 0, 0, color='y') JR = 71492000.0 legs = len(x) / 4 seq = self.get_sequence() common_mu = seq[0].mu_central_body start_mjd2000 = self.t0.mjd2000 #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = x[3::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (legs + 1)) r_P = list([None] * (legs + 1)) v_P = list([None] * (legs + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(start_mjd2000 + sum(T[:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=JR) v_end_l = [a + b for a, b in zip(v_P[0], self.vinf_in)] #4 - And we iterate on the legs for i in xrange(0, legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[1 + 4 * i] * seq[i - 1].radius, x[4 * i], seq[i].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler(ax, r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color='b', legend=False, units=JR) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] plt.show() return ax
def _mga_incipit_plot(self, x, plot_leg_0=False): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d', aspect='equal') ax.scatter(0, 0, 0, color='y') JR = 71492000.0 legs = len(x) / 4 seq = self.get_sequence() common_mu = seq[0].mu_central_body #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = x[3::4] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * legs) r_P = list([None] * legs) v_P = list([None] * legs) DV = list([None] * legs) for i, planet in enumerate(seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=JR) #3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, common_mu, False, False) if (plot_leg_0): plot_lambert(ax, l, sol=0, color='k', legend=False, units=JR, N=500) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #4 - And we proceed with each successive leg for i in xrange(1, legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[1 + 4 * i] * seq[i - 1].radius, x[4 * i], seq[i - 1].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler(ax, r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color='b', legend=False, units=JR) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] plt.show() return ax
def stats(self,x): import matplotlib as mpl import matplotlib.pyplot as plt #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0]*(self.__n_legs)) for i in xrange(self.__n_legs): T[i] = (x[4+4*i]/sum(x[4::4]))*x[3] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) close_d = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] close_d[0] = closest_distance(r,v_beg_l, r_P[0], v_end_l, self.common_mu)[0] / JR #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[6+(i-1)*4]*self.seq[i-1].radius,x[5+(i-1)*4],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[7+(i-1)*4]*T[i]*DAY2SEC,self.common_mu) tmp, ra = closest_distance(r_P[i-1],v_out, r,v, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[7+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r,v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp/JR ra = ra/JR else: close_d[i] = tmp2/JR ra = ra2/JR #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) #print "Total mission time (days): " + str(sum(T)) #print "Total DV (m/s): " + str(sum(DV)) symbol_dict = { 'io' : 'yo', 'europa' : 'bo', 'ganymede' : 'ro', 'callisto' : 'ko' } #for i in xrange(0,self.__n_legs): # plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) #n = 0 ratio = list([0]*(self.__n_legs)) coeff = 0.3 for i in xrange(0,self.__n_legs): ratio[i] = (DV[i]/(T[i]*DAY2SEC)) if close_d[0] >= 2: if close_d[1] >= 2: if close_d[2] >= 2: if close_d[3] >= 2: if ratio[1] <= coeff*(0.1/2000): if ratio[2] <= coeff*(0.1/2000): if ratio[3] <= coeff*(0.1/2000): if ratio[0] <= coeff*(0.1/2000): plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) #for i in xrange(0,self.__n_legs): # if close_d[i] > 2: # plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) # else: # print "\n the closest distance is less than 2*Rj " #print "\n number of sequences that do not crash " + str(n) plt.show()
def _mga_1dsm_tof_plot_old(self, x): """ Plots the trajectory represented by the decision vector x """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') seq = self.get_sequence() n = (len(seq) - 1) # 1 - we 'decode' the chromosome recording the various times of flight # (days) in the list T T = x[5::4] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (n + 1)) r_P = list([None] * (n + 1)) v_P = list([None] * (n + 1)) DV = list([None] * (n + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=( 0.8, 0.6, 0.8), legend=True, units = AU) # 3 - We start with the first leg theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian( r_P[0], v0, x[4] * T[0] * DAY2SEC, seq[0].mu_central_body) plot_kepler( ax, r_P[0], v0, x[4] * T[0] * DAY2SEC, seq[0].mu_central_body, N=100, color='b', legend=False, units=AU) # Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, seq[0].mu_central_body) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # First DSM occuring at time nu1*T1 DV[0] = norm([a - b for a, b in zip(v_beg_l, v)]) # 4 - And we proceed with each successive leg for i in range(1, n): # Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * seq[i].radius, x[6 + (i - 1) * 4], seq[i].mu_self) # s/c propagation before the DSM r, v = propagate_lagrangian( r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, seq[0]. mu_central_body) plot_kepler(ax, r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, seq[0].mu_central_body, N=100, color='b', legend=False, units=AU) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, seq[0].mu_central_body) plot_lambert(ax, l, sol=0, color='r', legend=False, units=AU) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # DSM occurring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) return ax
def _objfun_impl(self,x): #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0]*(self.__n_legs)) #sum_alpha = 0 #for i in range(self.__n_legs-1): # sum_alpha = sum_alpha+x[4+4*i] #for i in xrange(self.__n_legs-1): # T[i] = (x[4+4*i]/sum_alpha)*x[3] for i in xrange(0,self.__n_legs): T[i] = (x[4+4*i]/sum(x[4::4]))*x[3] #print "\tDuration: " + str(T) + "days" #return(T,) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[6+(i-1)*4]*self.seq[i-1].radius,x[5+(i-1)*4],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[7+(i-1)*4]*T[i]*DAY2SEC,self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[7+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r,v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp/JR ra = ra/JR else: close_d[i] = tmp2/JR ra = ra2/JR #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) coeff = 0.3 for i in xrange(0,self.__n_legs): ratio[i] = (DV[i]/(T[i]*DAY2SEC)) DV_2rj[i] = DV[i] + max((2.0-close_d[i]),0.0)*1000 + max((ratio[i]-coeff*(0.1/2000)),0.0)*100 T_2rj[i] = T[i] + max((2.0-close_d[i]),0.0)*1000 + max((ratio[i]-coeff*(0.1/2000)),0.0)*100 #if self.f_dimension == 1: # return (sum(DV) #else: # return (sum(DV), sum(T)) if self.f_dimension == 1: return (sum(DV_2rj)) else: return (sum(DV_2rj), sum(T_2rj))
def _get_penalty_data(self,x): """ getTrajectory takes a genome x, and returns a Trajectory variable that is a list of all r, v, and time of flights Trajectory = [[r0, v0_out, r1, v1_in, tof, rp, ra, Trev, vinf], [r1, v1_out, r2, v2_in, tof, rp, ra, Trev, vinf], [...]] tof in days """ from PyKEP import epoch, lambert_problem, propagate_lagrangian, fb_prop, DAY2SEC; from math import pi, acos, cos, sin; import numpy as np; from _mass_penalty import get_rp_ra_Trev Trajectory = []; #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] # reconstruct properties that are known in _mga_incipit: self.seq = self.get_sequence(); self.__n_legs = len(self.seq); self.common_mu = self.seq[0].mu_central_body #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] Tr = [tuple(r), v_beg_l, r_P[0], v_end_l, T[0]*DAY2SEC]; rPvec = np.asarray(r_P[0]); vPvec = np.asarray(v_end_l); Tr = Tr + get_rp_ra_Trev(rPvec, vPvec); vinf = vPvec - np.asarray(v_P[0]); Tr = Tr + [vinf]; Trajectory.append(Tr); #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(np.linalg.norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,self.__n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*self.seq[i-1].radius,x[4*i],self.seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,self.common_mu) # append r, v, etc. to the Trajectory: Tr = [r_P[i-1], v_out, r, v, x[4*i+2]*T[i]*DAY2SEC]; rPvec = np.asarray(r); vPvec = np.asarray(v); Tr = Tr + get_rp_ra_Trev(rPvec, vPvec); vinf = []; Tr = Tr + [vinf]; Trajectory.append(Tr); #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # append r, v, etc. to the Trajectory: Tr = [r, v_beg_l, r_P[i], v_end_l, (1-x[4*i+2])*T[i]*DAY2SEC]; rPvec = np.asarray(r_P[i]); vPvec = np.asarray(v_end_l); Tr = Tr + get_rp_ra_Trev(rPvec, vPvec); vinf = vPvec - np.asarray(v_P[i]); Tr = Tr + [vinf]; Trajectory.append(Tr); #DSM occuring at time nu2*T2 DV[i] = np.linalg.norm([a-b for a,b in zip(v_beg_l,v)]) return Trajectory;
def plot_sf_leg(leg, N=5, units=1, color='b', legend=False, plot_line=True, plot_segments=True, ax=None): """ ax = plot_sf_leg(leg, N=5, units=1, color='b', legend=False, no_trajectory=False, ax=None): - ax: 3D axis object created using fig.gca(projection='3d') - leg: a PyKEP.sims_flanagan.leg - N: number of points to be plotted along one arc - units: the length unit to be used in the plot - color: matplotlib color to use to plot the trajectory and the grid points - legend when True it plots also the legend - plot_line: when True plots also the trajectory (between mid-points and grid points) Plots a Sims-Flanagan leg Example:: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.gca(projection='3d') t1 = epoch(0) pl = planet_ss('earth') rE,vE = pl.eph(t1) plot_planet(pl,t0=t1, units=AU, ax=ax) t2 = epoch(440) pl = planet_ss('mars') rM, vM = pl.eph(t2) plot_planet(pl,t0=t2, units=AU, ax=ax) sc = sims_flanagan.spacecraft(4500,0.5,2500) x0 = sims_flanagan.sc_state(rE,vE,sc.mass) xe = sims_flanagan.sc_state(rM,vM,sc.mass) l = sims_flanagan.leg(t1,x0,[1,0,0]*5,t2,xe,sc,MU_SUN) plot_sf_leg(l, units=AU, ax=ax) """ from PyKEP import propagate_lagrangian, AU, DAY2SEC, G0, propagate_taylor import numpy as np from scipy.linalg import norm from math import exp import matplotlib.pylab as plt from mpl_toolkits.mplot3d import Axes3D if ax is None: fig = plt.figure() axis = fig.gca(projection='3d') else: axis = ax # We compute the number of segments for forward and backward propagation n_seg = len(leg.get_throttles()) fwd_seg = (n_seg + 1) // 2 back_seg = n_seg // 2 # We extract information on the spacecraft sc = leg.get_spacecraft() isp = sc.isp max_thrust = sc.thrust # And on the leg throttles = leg.get_throttles() mu = leg.get_mu() # Forward propagation # x,y,z contain the cartesian components of all points (grid+midpints) x = [0.0] * (fwd_seg * 2 + 1) y = [0.0] * (fwd_seg * 2 + 1) z = [0.0] * (fwd_seg * 2 + 1) state = leg.get_xi() # Initial conditions r = state.r v = state.v m = state.m x[0] = r[0] / units y[0] = r[1] / units z[0] = r[2] / units # We compute all points by propagation for i, t in enumerate(throttles[:fwd_seg]): dt = (t.end.mjd - t.start.mjd) * DAY2SEC alpha = min(norm(t.value), 1.0) # Keplerian propagation and dV application if leg.high_fidelity is False: dV = [max_thrust / m * dt * dumb for dumb in t.value] if plot_line: plot_kepler(r, v, dt / 2, mu, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v = propagate_lagrangian(r, v, dt / 2, mu) x[2 * i + 1] = r[0] / units y[2 * i + 1] = r[1] / units z[2 * i + 1] = r[2] / units # v= v+dV v = [a + b for a, b in zip(v, dV)] if plot_line: plot_kepler(r, v, dt / 2, mu, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v = propagate_lagrangian(r, v, dt / 2, mu) x[2 * i + 2] = r[0] / units y[2 * i + 2] = r[1] / units z[2 * i + 2] = r[2] / units m *= exp(-norm(dV) / isp / G0) # Taylor propagation of constant thrust u else: u = [max_thrust * dumb for dumb in t.value] if plot_line: plot_taylor(r, v, m, u, dt / 2, mu, isp * G0, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v, m = propagate_taylor(r, v, m, u, dt / 2, mu, isp * G0, -12, -12) x[2 * i + 1] = r[0] / units y[2 * i + 1] = r[1] / units z[2 * i + 1] = r[2] / units if plot_line: plot_taylor(r, v, m, u, dt / 2, mu, isp * G0, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v, m = propagate_taylor(r, v, m, u, dt / 2, mu, isp * G0, -12, -12) x[2 * i + 2] = r[0] / units y[2 * i + 2] = r[1] / units z[2 * i + 2] = r[2] / units x_grid = x[::2] y_grid = y[::2] z_grid = z[::2] x_midpoint = x[1::2] y_midpoint = y[1::2] z_midpoint = z[1::2] if plot_segments: axis.scatter(x_grid[:-1], y_grid[:-1], z_grid[:-1], label='nodes', marker='o') axis.scatter(x_midpoint, y_midpoint, z_midpoint, label='mid-points', marker='x') axis.scatter(x_grid[-1], y_grid[-1], z_grid[-1], marker='^', c='y', label='mismatch point') # Backward propagation # x,y,z will contain the cartesian components of x = [0.0] * (back_seg * 2 + 1) y = [0.0] * (back_seg * 2 + 1) z = [0.0] * (back_seg * 2 + 1) state = leg.get_xf() # Final conditions r = state.r v = state.v m = state.m x[-1] = r[0] / units y[-1] = r[1] / units z[-1] = r[2] / units for i, t in enumerate(throttles[-1:-back_seg - 1:-1]): dt = (t.end.mjd - t.start.mjd) * DAY2SEC alpha = min(norm(t.value), 1.0) if leg.high_fidelity is False: dV = [max_thrust / m * dt * dumb for dumb in t.value] if plot_line: plot_kepler(r, v, -dt / 2, mu, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v = propagate_lagrangian(r, v, -dt / 2, mu) x[-2 * i - 2] = r[0] / units y[-2 * i - 2] = r[1] / units z[-2 * i - 2] = r[2] / units # v= v+dV v = [a - b for a, b in zip(v, dV)] if plot_line: plot_kepler(r, v, -dt / 2, mu, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v = propagate_lagrangian(r, v, -dt / 2, mu) x[-2 * i - 3] = r[0] / units y[-2 * i - 3] = r[1] / units z[-2 * i - 3] = r[2] / units m *= exp(norm(dV) / isp / G0) else: u = [max_thrust * dumb for dumb in t.value] if plot_line: plot_taylor(r, v, m, u, -dt / 2, mu, isp * G0, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v, m = propagate_taylor(r, v, m, u, -dt / 2, mu, isp * G0, -12, -12) x[-2 * i - 2] = r[0] / units y[-2 * i - 2] = r[1] / units z[-2 * i - 2] = r[2] / units if plot_line: plot_taylor(r, v, m, u, -dt / 2, mu, isp * G0, N=N, units=units, color=(alpha, 0, 1 - alpha), ax=axis) r, v, m = propagate_taylor(r, v, m, u, -dt / 2, mu, isp * G0, -12, -12) x[-2 * i - 3] = r[0] / units y[-2 * i - 3] = r[1] / units z[-2 * i - 3] = r[2] / units x_grid = x[::2] y_grid = y[::2] z_grid = z[::2] x_midpoint = x[1::2] y_midpoint = y[1::2] z_midpoint = z[1::2] if plot_segments: axis.scatter(x_grid[1:], y_grid[1:], z_grid[1:], marker='o') axis.scatter(x_midpoint, y_midpoint, z_midpoint, marker='x') axis.scatter(x_grid[0], y_grid[0], z_grid[0], marker='^', c='y') if legend: axis.legend() if ax is None: # show only if axis is not set plt.show() return axis
def pretty(self, x): """ Prints human readable information on the trajectory represented by the decision vector x Example:: prob.pretty(x) """ #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0] * (self.__n_legs)) #a[-i] = x[-1-(i-1)*4] for i in xrange(self.__n_legs - 1): j = i + 1 T[-j] = (x[5] - sum(T[-(j - 1):])) * x[-1 - (j - 1) * 4] T[0] = x[5] - sum(T) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) DV = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg print "First Leg: " + self.seq[0].name + " to " + self.seq[1].name theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) print "Departure: " + str(t_P[0]) + " (" + str( t_P[0].mjd2000) + " mjd2000) " print "Duration: " + str(T[0]) + "days" print "VINF: " + str(x[3] / 1000) + " km/sec" v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian(r_P[0], v0, x[4] * T[0] * DAY2SEC, self.common_mu) print "DSM after " + str(x[4] * T[0]) + " days" #Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at time nu1*T1 DV[0] = norm([a - b for a, b in zip(v_beg_l, v)]) print "DSM magnitude: " + str(DV[0]) + "m/s" #4 - And we proceed with each successive leg for i in range(1, self.__n_legs): print "\nleg no. " + str( i + 1) + ": " + self.seq[i].name + " to " + self.seq[i + 1].name print "Duration: " + str(T[i]) + "days" #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * self.seq[i].radius, x[6 + (i - 1) * 4], self.seq[i].mu_self) print "Fly-by epoch: " + str(t_P[i]) + " (" + str( t_P[i].mjd2000) + " mjd2000) " print "Fly-by radius: " + str(x[7 + (i - 1) * 4]) + " planetary radii" #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu) print "DSM after " + str(x[8 + (i - 1) * 4] * T[i]) + " days" #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) print "DSM magnitude: " + str(DV[i]) + "m/s" #Last Delta-v print "\nArrival at " + self.seq[-1].name DV[-1] = norm([a - b for a, b in zip(v_end_l, v_P[-1])]) print "Arrival epoch: " + str(t_P[-1]) + " (" + str( t_P[-1].mjd2000) + " mjd2000) " print "Arrival Vinf: " + str(DV[-1]) + "m/s" print "Total mission time: " + str(sum(T) / 365.25) + " years"
def lambertization(prob, x): """ Does all the lambert arc construction to get you DV and stuff """ # get sequence try: seq = prob.seq except AttributeError: seq = prob.get_sequence() # get common mu try: common_mu = prob.common_mu except AttributeError: common_mu = seq[0].mu_central_body # number of legs n_legs = len(seq) # time of flights (days) T = x[3::4] # Epochs and Ephemerides of the planetary encounters t_P = list([None] * (n_legs)) r_P = list([None] * (n_legs)) v_P = list([None] * (n_legs)) DV = list([None] * (n_legs)) close_d = list([None] * (n_legs)) for i,planet in enumerate(seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) # Lambert arc to 1st leg theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] close_d[0] = closest_distance(r,v_beg_l, r_P[0], v_end_l, common_mu)[0] / JR # save the initial conditions on 1000JR initial = ( [epoch(x[0]), t_P[0]], [r,r_P[0]], [v_beg_l, v_end_l] ) # First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) # creating new lists v_sc = [] # Proceed with succeeding legs for i in xrange(1, n_legs): # Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*seq[i-1].radius,x[4*i],seq[i-1].mu_self) # s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) tmp, ra = closest_distance(r_P[i-1],v_out, r,v, common_mu) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r,v_beg_l, r_P[i], v_end_l, common_mu) if tmp < tmp2: close_d[i] = tmp/JR ra = ra/JR else: close_d[i] = tmp2/JR ra = ra2/JR # DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) v_sc.append( (v_out, v_end_l) ) # v_inf_out, v_inf_in return seq, common_mu, n_legs, initial, T, t_P, r_P, v_sc, DV, close_d
def stats(self, x): import matplotlib as mpl import matplotlib.pyplot as plt #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0] * (self.__n_legs)) for i in xrange(self.__n_legs): T[i] = (x[4 + 4 * i] / sum(x[4::4])) * x[3] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) close_d = list([None] * (self.__n_legs)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] close_d[0] = closest_distance(r, v_beg_l, r_P[0], v_end_l, self.common_mu)[0] / JR #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[6 + (i - 1) * 4] * self.seq[i - 1].radius, x[5 + (i - 1) * 4], self.seq[i - 1].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i - 1], v_out, x[7 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu) tmp, ra = closest_distance(r_P[i - 1], v_out, r, v, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[7 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r, v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp / JR ra = ra / JR else: close_d[i] = tmp2 / JR ra = ra2 / JR #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) #print "Total mission time (days): " + str(sum(T)) #print "Total DV (m/s): " + str(sum(DV)) symbol_dict = { 'io': 'yo', 'europa': 'bo', 'ganymede': 'ro', 'callisto': 'ko' } #for i in xrange(0,self.__n_legs): # plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) #n = 0 ratio = list([0] * (self.__n_legs)) coeff = 0.3 for i in xrange(0, self.__n_legs): ratio[i] = (DV[i] / (T[i] * DAY2SEC)) if close_d[0] >= 2: if close_d[1] >= 2: if close_d[2] >= 2: if close_d[3] >= 2: if ratio[1] <= coeff * (0.1 / 2000): if ratio[2] <= coeff * (0.1 / 2000): if ratio[3] <= coeff * (0.1 / 2000): if ratio[0] <= coeff * (0.1 / 2000): plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) #for i in xrange(0,self.__n_legs): # if close_d[i] > 2: # plt.plot(sum(DV), sum(T), symbol_dict[self.seq[0].name]) # else: # print "\n the closest distance is less than 2*Rj " #print "\n number of sequences that do not crash " + str(n) plt.show()
def _get_states(self): """ Returns the spacecraft states (t,r,v,m) at the leg grid points Examples:: times,r,v,m = PyKEP.sims_flanagan.leg.get_states() """ from PyKEP import propagate_lagrangian, AU, DAY2SEC, G0, propagate_taylor import numpy as np from scipy.linalg import norm from math import exp #We compute the number of segments for forward and backward propagation n_seg = len(self.get_throttles()) fwd_seg = (n_seg+1)/2 back_seg = n_seg/2 #We extract information on the spacecraft sc = self.get_spacecraft() isp = sc.isp max_thrust = sc.thrust #And on the leg throttles = self.get_throttles() mu = self.get_mu() #time grid t_grid = [0.0]*(n_seg*2+2) #Forward propagation #x,y,z contain the cartesian components of all points (grid+midpints) x = [0.0]*(fwd_seg*2+1) y = [0.0]*(fwd_seg*2+1) z = [0.0]*(fwd_seg*2+1) vx = [0.0]*(fwd_seg*2+1) vy = [0.0]*(fwd_seg*2+1) vz = [0.0]*(fwd_seg*2+1) mass = [0.0]*(fwd_seg*2+1) state = self.get_xi() #Initial conditions r = state.r; v = state.v; m = state.m x[0],y[0],z[0] = r vx[0],vy[0],vz[0] = v mass[0] = m #We compute all points by propagation for i,t in enumerate(throttles[:fwd_seg]): t_grid[2*i] = t.start.mjd2000 t_grid[2*i+1] = t.start.mjd2000 + (t.end.mjd2000 - t.start.mjd2000)/2. dt = (t.end.mjd - t.start.mjd)*DAY2SEC alpha = min(norm(t.value),1.0) #Keplerian propagation and dV application if self.high_fidelity == False: dV = [max_thrust / m * dt * dumb for dumb in t.value] r,v = propagate_lagrangian(r,v,dt/2,mu) x[2*i+1],y[2*i+1],z[2*i+1] = r vx[2*i+1],vy[2*i+1],vz[2*i+1] = v mass[2*i+1] = m #v= v+dV v = [a+b for a,b in zip(v,dV)] r,v = propagate_lagrangian(r,v,dt/2,mu) m *= exp( -norm(dV)/isp/G0 ) x[2*i+2],y[2*i+2],z[2*i+2] = r vx[2*i+2],vy[2*i+2],vz[2*i+2] = v mass[2*i+2] = m #Taylor propagation of constant thrust u else: u = [max_thrust * dumb for dumb in t.value] r,v,m = propagate_taylor(r,v,m,u,dt/2,mu,isp*G0,-10,-10) x[2*i+1],y[2*i+1],z[2*i+1] = r vx[2*i+1],vy[2*i+1],vz[2*i+1] = v mass[2*i+1] = m r,v,m = propagate_taylor(r,v,m,u,dt/2,mu,isp*G0,-10,-10) x[2*i+2],y[2*i+2],z[2*i+2] = r vx[2*i+2],vy[2*i+2],vz[2*i+2] = v mass[2*i+2] = m t_grid[2*i+2] = t.end.mjd2000 #Backward propagation #x,y,z will contain the cartesian components of x_back = [0.123]*(back_seg*2+1) y_back = [0.123]*(back_seg*2+1) z_back = [0.123]*(back_seg*2+1) vx_back = [0.0]*(back_seg*2+1) vy_back = [0.0]*(back_seg*2+1) vz_back = [0.0]*(back_seg*2+1) mass_back = [0.0]*(back_seg*2+1) state = self.get_xf() #Final conditions r = state.r; v = state.v; m = state.m x_back[-1],y_back[-1],z_back[-1] = r vx_back[-1],vy_back[-1],vz_back[-1] = v mass_back[-1] = m for i,t in enumerate(throttles[-1:-back_seg-1:-1]): t_grid[-2*i-2] = t.end.mjd2000 -(t.end.mjd2000 - t.start.mjd2000)/2. t_grid[-2*i-1] = t.end.mjd2000 dt = (t.end.mjd - t.start.mjd)*DAY2SEC alpha = min(norm(t.value),1.0) if self.high_fidelity == False: dV = [max_thrust / m * dt * dumb for dumb in t.value] r,v = propagate_lagrangian(r,v,-dt/2,mu) x_back[-2*i-2],y_back[-2*i-2],z_back[-2*i-2] = r vx_back[-2*i-2],vy_back[-2*i-2],vz_back[-2*i-2] = v mass_back[-2*i-2] = m #v= v+dV v = [a-b for a,b in zip(v,dV)] r,v = propagate_lagrangian(r,v,-dt/2,mu) m *= exp( norm(dV)/isp/G0 ) x_back[-2*i-3],y_back[-2*i-3],z_back[-2*i-3] = r vx_back[-2*i-3],vy_back[-2*i-3],vz_back[-2*i-3] = v mass_back[-2*i-3] = m else: u = [max_thrust * dumb for dumb in t.value] r,v,m = propagate_taylor(r,v,m,u,-dt/2,mu,isp*G0,-10,-10) x_back[-2*i-2],y_back[-2*i-2],z_back[-2*i-2] = r vx_back[-2*i-2],vy_back[-2*i-2],vz_back[-2*i-2] = v mass_back[-2*i-2] = m r,v,m = propagate_taylor(r,v,m,u,-dt/2,mu,isp*G0,-10,-10) x_back[-2*i-3],y_back[-2*i-3],z_back[-2*i-3] = r vx_back[-2*i-3],vy_back[-2*i-3],vz_back[-2*i-3] = v mass_back[-2*i-3] = m t_grid[-2*i-3] = t.start.mjd2000 x = x+x_back y = y+y_back z = z+z_back vx = vx+vx_back vy = vy+vy_back vz = vz+vz_back mass = mass+mass_back return t_grid, zip(x,y,z), zip(vx,vy,vz), mass
def _objfun_impl(self, x): #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0] * (self.__n_legs)) for i in xrange(self.__n_legs - 1): j = i + 1 T[-j] = (x[5] - sum(T[-(j - 1):])) * x[-1 - (j - 1) * 4] T[0] = x[5] - sum(T) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) DV = list([0.0] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian(r_P[0], v0, x[4] * T[0] * DAY2SEC, self.common_mu) #Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at time nu1*T1 DV[0] = norm([a - b for a, b in zip(v_beg_l, v)]) #4 - And we proceed with each successive leg for i in range(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * self.seq[i].radius, x[6 + (i - 1) * 4], self.seq[i].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) #Last Delta-v if self.__add_vinf_arr: DV[-1] = norm([a - b for a, b in zip(v_end_l, v_P[-1])]) if self.__add_vinf_dep: DV[0] += x[3] if self.f_dimension == 1: return (sum(DV), ) else: return (sum(DV), sum(T))
def planet_asteroid(start_planet, target_name, tlaunch, tarrive, rev, N): # Create PyKEP epoch objects and calculate flight time t1 = epoch(tlaunch) t2 = epoch(tarrive) dt = (tarrive - tlaunch) * DAY2SEC import py.AsteroidDB as asteroidDB neo_db = asteroidDB.neo target = (item for item in neo_db if item["name"] == target_name).next() ep = epoch(target["epoch_mjd"], epoch.epoch_type.MJD) a = target["a"] * AU e = target["e"] i = target["i"] * DEG2RAD om = target["om"] * DEG2RAD w = target["w"] * DEG2RAD ma = target["ma"] * DEG2RAD as_mu = 1E17 * 6.67384E-11 # maybe need to calculate actual mass from density and radius r = (target["diameter"] / 2) * 1000 sr = r * 1.1 OBJ2 = planet(ep, (a, e, i, om, w, ma), MU_SUN, as_mu, r, sr) OBJ1 = planet_ss(start_planet) # Calculate location of objects in flight path r1, v1 = OBJ1.eph(t1) r2, v2 = OBJ2.eph(t2) # Find trajectory l = lambert_problem(r1, r2, dt, MU_SUN) #extract relevant information from solution r = l.get_r1() v = l.get_v1()[rev] mu = l.get_mu() #define the integration time dtn = dt / (N - 1) dtn_days = dtn * SEC2DAY #alocate the cartesian components for r t = np.array([0.0] * N) x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([0.0] * N) #calculate the spacecraft position at each dt for i in range(N): t[i] = tlaunch + dtn_days * i x[i] = r[0] / AU y[i] = r[1] / AU z[i] = r[2] / AU r, v = propagate_lagrangian(r, v, dtn, mu) #traj = [t, x, y, z] vin = l.get_v1()[rev] vout = l.get_v2()[rev] #dV=fb_vel(vin,vout,planet_ss(arrive_planet)) #dV=np.sqrt( np.square(vin[0]/vout[0])+np.square(vin[1]/vout[1])+np.square(vin[2]/vout[2])) #dV=np.sqrt( np.square(vin[0]-v1[0])+np.square(v1[1]-vin[1])+np.square(v1[2]-vin[2])) #dV=np.sqrt( np.square(v2[0]-vout[0])+np.square(v2[1]-vout[1])+np.square(v2[2]-vout[2])) #dV=np.sqrt( np.square(v1[0]/vin[0])+np.square(v1[1]/vin[1])+np.square(v1[2]/vin[2])) C3_launch = (np.sqrt(np.square(vin[0] - v1[0]) + np.square(vin[1] - v1[1]) + np.square(vin[2] - v1[2]))) ** 2 C3_arrive = (np.sqrt(np.square(vout[0] - v2[0]) + np.square(vout[1] - v2[1]) + np.square(vout[2] - v2[2]))) ** 2 C3 = np.sqrt((C3_arrive ** 2) + (C3_launch ** 2)) return C3
def _objfun_impl(self, x): #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = list([0] * (self.__n_legs)) #sum_alpha = 0 #for i in range(self.__n_legs-1): # sum_alpha = sum_alpha+x[4+4*i] #for i in xrange(self.__n_legs-1): # T[i] = (x[4+4*i]/sum_alpha)*x[3] for i in xrange(0, self.__n_legs): T[i] = (x[4 + 4 * i] / sum(x[4::4])) * x[3] #print "\tDuration: " + str(T) + "days" #return(T,) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs)) r_P = list([None] * (self.__n_legs)) v_P = list([None] * (self.__n_legs)) DV = list([None] * (self.__n_legs)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, self.common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1, self.__n_legs): #Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[6 + (i - 1) * 4] * self.seq[i - 1].radius, x[5 + (i - 1) * 4], self.seq[i - 1].mu_self) #s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i - 1], v_out, x[7 + (i - 1) * 4] * T[i] * DAY2SEC, self.common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[7 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, self.common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] tmp2, ra2 = closest_distance(r, v_beg_l, r_P[i], v_end_l, self.common_mu) if tmp < tmp2: close_d[i] = tmp / JR ra = ra / JR else: close_d[i] = tmp2 / JR ra = ra2 / JR #DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) coeff = 0.3 for i in xrange(0, self.__n_legs): ratio[i] = (DV[i] / (T[i] * DAY2SEC)) DV_2rj[i] = DV[i] + max((2.0 - close_d[i]), 0.0) * 1000 + max( (ratio[i] - coeff * (0.1 / 2000)), 0.0) * 100 T_2rj[i] = T[i] + max((2.0 - close_d[i]), 0.0) * 1000 + max( (ratio[i] - coeff * (0.1 / 2000)), 0.0) * 100 #if self.f_dimension == 1: # return (sum(DV) #else: # return (sum(DV), sum(T)) if self.f_dimension == 1: return (sum(DV_2rj)) else: return (sum(DV_2rj), sum(T_2rj))
def _mga_incipit_plot_old(self, x, plot_leg_0=False): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d', aspect='equal') ax.scatter(0, 0, 0, color='y') JR = 71492000.0 legs = len(x) / 4 seq = self.get_sequence() common_mu = seq[0].mu_central_body # 1 - we 'decode' the chromosome recording the various times of flight # (days) in the list T T = x[3::4] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * legs) r_P = list([None] * legs) v_P = list([None] * legs) DV = list([None] * legs) for i, planet in enumerate(seq): t_P[i] = epoch(x[0] + sum(T[:i + 1])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(ax, planet, t0=t_P[i], color=( 0.8, 0.6, 0.8), legend=True, units = JR) # 3 - We start with the first leg: a lambert arc theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 # phi close to zero is in the moon orbit plane injection r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] r = [JR * 1000 * d for d in r] l = lambert_problem(r, r_P[0], T[0] * DAY2SEC, common_mu, False, False) if (plot_leg_0): plot_lambert(ax, l, sol=0, color='k', legend=False, units=JR, N=500) # Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # 4 - And we proceed with each successive leg for i in range(1, legs): # Fly-by v_out = fb_prop(v_end_l, v_P[i - 1], x[1 + 4 * i] * seq[i - 1].radius, x[4 * i], seq[i - 1].mu_self) # s/c propagation before the DSM r, v = propagate_lagrangian( r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler(ax, r_P[i - 1], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color='b', legend=False, units=JR) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i], dt, common_mu, False, False) plot_lambert(ax, l, sol=0, color='r', legend=False, units=JR, N=500) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] plt.show() return ax
def _get_penalty_data_part(self,x): from PyKEP import epoch, lambert_problem, DAY2SEC, fb_prop, propagate_lagrangian from math import pi, acos,cos,sin,sqrt from scipy.linalg import norm from copy import deepcopy from _mass_penalty import get_rp_ra_Trev import numpy as np """ This method returns the data needed to compute the score of a trajectory. """ Trajectory = []; #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] nlegs = len(x)/4 seq = self.get_sequence() common_mu = seq[0].mu_central_body t0 = self.t0.mjd2000 vinf_in = deepcopy(self.vinf_in) #2 - We compute the epochs and ephemerides of the planetary encounters ep_list = list([None] * (nlegs+1)) t_P = list([None] * (nlegs+1)) r_P = list([None] * (nlegs+1)) v_P = list([None] * (nlegs+1)) DV = list([None] * nlegs) for i,planet in enumerate(seq): ep_list[i] = t0+sum(T[:i]) t_P[i] = epoch(t0+sum(T[:i])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) v_end_l = [a+b for a,b in zip(vinf_in, v_P[0])] #3 - And we proceed with each successive leg for i in xrange(nlegs): #Fly-by v_out = fb_prop(v_end_l,v_P[i],x[1+4*i]*seq[i].radius,x[4*i],seq[i].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) # append r, v, etc. to the Trajectory: Tr = [r_P[i-1], v_out, r, v, x[4*i+2]*T[i]*DAY2SEC]; rPvec = np.asarray(r); vPvec = np.asarray(v); Tr = Tr + get_rp_ra_Trev(rPvec, vPvec); vinf = []; Tr = Tr + [vinf]; Trajectory.append(Tr); #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i+1],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # append r, v, etc. to the Trajectory: Tr = [r, v_beg_l, r_P[i], v_end_l, (1-x[4*i+2])*T[i]*DAY2SEC]; rPvec = np.asarray(r_P[i]); vPvec = np.asarray(v_end_l); Tr = Tr + get_rp_ra_Trev(rPvec, vPvec); vinf = vPvec - np.asarray(v_P[i]); Tr = Tr + [vinf]; Trajectory.append(Tr); return Trajectory;
def AIO(self,x, doplot = True, doprint = True, rtrn_desc = True, dists_class = None): P = doprint plots_datas = list([None] * (self.__n)) PLDists = list([None] * (self.__n-1)) FBDates = list([None] * (self.__n-1)) #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0]*(self.__n)) #a[-i] = x[-1-(i-1)*4] for i in xrange(self.__n-1): j = i+1; T[-j] = (x[5] - sum(T[-(j-1):])) * x[-1-(j-1)*4] T[0] = x[5] - sum(T) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n+1)) r_P = list([None] * (self.__n+1)) v_P = list([None] * (self.__n+1)) DV = list([None] * (self.__n+1)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg if P: print "First Leg: " + self.seq[0].name + " to " + self.seq[1].name theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 Vinfx = x[3]*cos(phi)*cos(theta) Vinfy = x[3]*cos(phi)*sin(theta) Vinfz = x[3]*sin(phi) if P: print("Departure: " + str(t_P[0]) + " (" + str(t_P[0].mjd2000) + " mjd2000) \n" "Duration: " + str(T[0]) + " days\n" "VINF: " + str(x[3] / 1000) + " km/sec\n" "C3: " + str((x[3] / 1000)**2) + " km^2/s^2") v0 = [a+b for a,b in zip(v_P[0], [Vinfx,Vinfy,Vinfz])] r,v = propagate_lagrangian(r_P[0], v0, x[4]*T[0]*DAY2SEC, MU_SUN) if P: print "DSM after " + str(x[4]*T[0]) + " days" #Lambert arc to reach seq[1] dt = (1-x[4])*T[0]*DAY2SEC l = lambert_problem(r, r_P[1], dt, MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # Append data needed for potential plot generation plots_datas[0] = [v0, x[4]*T[0]*DAY2SEC, l] #First DSM occuring at time nu1*T1 DV[0] = norm([a-b for a,b in zip(v_beg_l,v)]) if P: print "DSM magnitude: " + str(DV[0]) + "m/s" #4 - And we proceed with each successive leg for i in range(1,self.__n): if P: print("\nleg no. " + str(i+1) + ": " + self.seq[i].name + " to " + self.seq[i+1].name + "\n" "Duration: " + str(T[i]) + "days") #Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7+(i-1)*4]*self.seq[i].radius, x[6+(i-1)*4], self.seq[i].mu_self) PLDists[i-1] = (x[7+(i-1)*4] -1)*self.seq[i].radius/1000. FBDates[i-1] = t_P[i] if P: print("Fly-by epoch: " + str(t_P[i]) + " (" + str(t_P[i].mjd2000) + " mjd2000) \n" "Fly-by radius: " + str(x[7+(i-1)*4]) + " planetary radii\n" "Fly-by distance: " + str( (x[7+(i-1)*4] -1)*self.seq[i].radius/1000.) + " km") #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i], v_out, x[8+(i-1)*4]*T[i]*DAY2SEC, MU_SUN) if P: print "DSM after " + str(x[8+(i-1)*4]*T[i]) + " days" #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[8+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r, r_P[i+1], dt, MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # Append data needed for potential plot generation plots_datas[i] = [v_out, x[8+(i-1)*4]*T[i]*DAY2SEC, l] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) if P: print "DSM magnitude: " + str(DV[i]) + "m/s" #Last Delta-v if P: print "\nArrival at " + self.seq[-1].name DV[-1] = norm([a-b for a,b in zip(v_end_l,v_P[-1])]) if P: print("Arrival Vinf: " + str(DV[-1]) + "m/s \n" "Total mission time: " + str(sum(T)/365.25) + " years (" + str(sum(T)) + " days) \n" "DSMs mag: " + str(sum(DV[:-1])) + "m/s \n" "Entry Vel: " + str(sqrt(2*(0.5*(DV[-1])**2 + 61933310.95))) + "m/s \n" "Entry epoch: " + str(epoch(t_P[0].mjd2000 + sum(T))) ) if doplot: import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure(figsize=(8, 8), dpi=80) ax = fig.gca(projection='3d') ax.scatter(0,0,0, color='y') for i,planet in enumerate(self.seq): plot_planet(ax, planet, t0=t_P[i], color=(0.8,0.6,0.8), legend=True, units = AU) for i, tpl in enumerate(plots_datas): plot_kepler(ax, r_P[i], tpl[0], tpl[1], MU_SUN ,N = 100, color='b', legend=False, units = AU) plot_lambert(ax, tpl[2], sol = 0, color='r', legend=False, units = AU) fig.tight_layout() plt.show() if dists_class is not None: for i, tpl in enumerate(plots_datas): dists_class.positions_kepler(r_P[i], tpl[0], tpl[1], MU_SUN ,index = 2*i) dists_class.positions_lambert(tpl[2], sol = 0, index = 2*(i+.5)) dists_class.set_launch_epoch(t_P[0].mjd2000) return dists_class if rtrn_desc: import numpy as np from math import atan2 desc = dict() for i in range(len(x)): desc[i] = x[i] theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 axtl = 23.43929*DEG2RAD # Earth axial tlit mactran = np.matrix([ [1, 0, 0], [0, cos(axtl), -sin(axtl)], [0, sin(axtl), cos(axtl)] ]) # Macierz przejscia z helio do ECI macdegs = np.matrix([ [cos(phi)*cos(theta)], [cos(phi)*sin(theta)], [sin(phi)] ]) aaa = mactran.dot(macdegs) theta_earth = atan2(aaa[1,0], aaa[0,0]) # Rektascensja asym phi_earth = asin(aaa[2,0]) # Deklinacja asym desc['RA'] = 360+RAD2DEG*theta_earth desc['DEC'] = phi_earth*RAD2DEG desc['Ldate'] = str(t_P[0]) desc['C3'] = (x[3] / 1000)**2 desc['dVmag'] = sum(DV[:-1]) desc['VinfRE'] = DV[-1] desc['VRE'] = (DV[-1]**2 + 2*61933310.95)**.5 desc['REDate'] = str(epoch(t_P[0].mjd2000 + sum(T))) for i in range(1,self.__n): desc[self.seq[i].name[0].capitalize()+'Dist'] = PLDists[i-1] desc[self.seq[i].name[0].capitalize()+'FBDate'] = str(FBDates[i-1]) return desc
def _get_lt_problem(self,x,n_seg=[10,10], high_fidelity=True): """ This method returns the equivalent low-thrust problem of an incipit """ from PyKEP import epoch, lambert_problem, DAY2SEC, fb_prop, propagate_lagrangian from PyGMO import population from math import pi, acos,cos,sin,sqrt, exp from scipy.linalg import norm retval = [] #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] n_legs = len(x)/4 seq = self.get_sequence() common_mu = seq[0].mu_central_body #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (n_legs)) r_P = list([None] * (n_legs)) v_P = list([None] * (n_legs)) DV = list([None] * (n_legs)) for i,planet in enumerate(seq): t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #We start appending in the lt chromosome (see mga_incipit_lt) retval.append(theta) retval.append(phi) #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #Start of the first lt leg encoding retval.append(T[0]) retval.append(exp(-DV[0]/9.80665/2000)*2000) #Tsiolkowsky retval.extend(v_beg_l) retval.extend([a-b for a,b in zip(v_end_l,v_P[0])]) #4 - And we proceed with each successive leg for i in xrange(1,n_legs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*seq[i-1].radius,x[4*i],seq[i-1].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l,v)]) #lt encoding of all legs retval.append(T[i]) retval.append(exp(-sum(DV[:i+1])/9.80665/2000)*2000) #Tsiolkowsky retval.extend([a-b for a,b in zip(v_out,v_P[i-1])]) if i != n_legs-1: retval.extend([a-b for a,b in zip(v_end_l,v_P[i])]) retval = retval + [0]*sum(n_seg)*3 prob = mga_incipit_lt(high_fidelity=high_fidelity,seq=seq, n_seg = n_seg,tf = epoch(x[0]+sum(T)), vf = [a-b for a,b in zip(v_end_l,v_P[i])]) # solves the problem of chemical trajectories wanting higher launch dv ub = list(prob.ub) lb = list(prob.lb) ub[4:7] = [5000,5000,5000] lb[4:7] = [-5000,-5000,-5000] prob.set_bounds(lb, ub) pop = population(prob) pop.push_back(retval) return (prob,pop)
def _objfun_impl(self,x): #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0]*(self.__n)) #a[-i] = x[-1-(i-1)*4] for i in xrange(self.__n-1): j = i+1; T[-j] = (x[5] - sum(T[-(j-1):])) * x[-1-(j-1)*4] T[0] = x[5] - sum(T) #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n+1)) r_P = list([None] * (self.__n+1)) v_P = list([None] * (self.__n+1)) DV = list([None] * (self.__n+1)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We start with the first leg theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 Vinfx = x[3]*cos(phi)*cos(theta) Vinfy = x[3]*cos(phi)*sin(theta) Vinfz = x[3]*sin(phi) v0 = [a+b for a,b in zip(v_P[0], [Vinfx,Vinfy,Vinfz])] r,v = propagate_lagrangian(r_P[0], v0, x[4]*T[0]*DAY2SEC, MU_SUN) #Lambert arc to reach seq[1] dt = (1-x[4])*T[0]*DAY2SEC l = lambert_problem(r,r_P[1],dt,MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #First DSM occuring at time nu1*T1 DV[0] = norm([a-b for a,b in zip(v_beg_l, v)]) #4 - And we proceed with each successive leg for i in range(1,self.__n): #Fly-by v_out = fb_prop(v_end_l, v_P[i] ,x[7+(i-1)*4]*self.seq[i].radius, x[6+(i-1)*4], self.seq[i].mu_self) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i], v_out, x[8+(i-1)*4]*T[i]*DAY2SEC, MU_SUN) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[8+(i-1)*4])*T[i]*DAY2SEC l = lambert_problem(r, r_P[i+1], dt, MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #DSM occuring at time nu2*T2 DV[i] = norm([a-b for a,b in zip(v_beg_l, v)]) #Last Delta-v DV[-1] = norm([a-b for a,b in zip(v_end_l,v_P[-1])]) #moje Eh56500 = 61933310.95 #Eh100000 = 61517435.56 entry_Vel = (DV[-1]**2 + 2*Eh56500)**0.5 entry_Vel2 = entry_Vel #axtl = 23.43929*DEG2RAD #DEC = abs(asin( sin(axtl)*cos(phi)*sin(theta) + cos(axtl)*sin(phi) ))*RAD2DEG # deklinacja asymptoty ucieczkowej sum_dv = sum(DV[:-1]) eff_C3 = (x[3])**2 if entry_Vel < self.entry_vel_barrier: entry_Vel2 = 0.0 del DV[-1] #~ if eff_C3 < self.C3_barrier: #~ #eff_C3 = 0 #~ pass if sum_dv < self.dsm_dv_barrier: sum_dv = 0+entry_Vel2 else: sum_dv = sum(DV) if self.f_dimension == 1: return (sum_dv,) else: return (sum_dv, eff_C3, entry_Vel2) #,
def ic_from_mga_1dsm(self, x): """ x_lt = prob.ic_from_mga_1dsm(x_mga) - x_mga: compatible trajectory as encoded by an mga_1dsm problem Returns an initial guess for the low-thrust trajectory, converting the mga_1dsm solution x_dsm. The user is responsible that x_mga makes sense (i.e. it is a viable mga_1dsm representation). The conversion is done by importing in the low-thrust encoding a) the launch date b) all the legs durations, c) the in and out relative velocities at each planet. All throttles are put to zero. Example:: x_lt= prob.ic_from_mga_1dsm(x_mga) """ from math import pi, cos, sin, acos from scipy.linalg import norm from PyKEP import propagate_lagrangian, lambert_problem, DAY2SEC, fb_prop retval = list([0.0] * self.dimension) # 1 - we 'decode' the chromosome recording the various times of flight (days) in the list T T = list([0] * (self.__n_legs)) for i in range(len(T)): T[i] = log(x[2 + 4 * i]) total = sum(T) T = [x[1] * time / total for time in T] retval[0] = x[0] for i in range(self.__n_legs): retval[1 + 8 * i] = T[i] retval[2 + 8 * i] = self.__sc.mass # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) DV = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) # 3 - We start with the first leg theta = 2 * pi * x[1] phi = acos(2 * x[2] - 1) - pi / 2 Vinfx = x[3] * cos(phi) * cos(theta) Vinfy = x[3] * cos(phi) * sin(theta) Vinfz = x[3] * sin(phi) retval[3:6] = [Vinfx, Vinfy, Vinfz] v0 = [a + b for a, b in zip(v_P[0], [Vinfx, Vinfy, Vinfz])] r, v = propagate_lagrangian(r_P[0], v0, x[4] * T[0] * DAY2SEC, MU_SUN) # Lambert arc to reach seq[1] dt = (1 - x[4]) * T[0] * DAY2SEC l = lambert_problem(r, r_P[1], dt, MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] retval[6:9] = [a - b for a, b in zip(v_end_l, v_P[1])] # 4 - And we proceed with each successive leg for i in range(1, self.__n_legs): # Fly-by v_out = fb_prop(v_end_l, v_P[i], x[7 + (i - 1) * 4] * self.seq[i].radius, x[6 + (i - 1) * 4], self.seq[i].mu_self) retval[3 + i * 8:6 + i * 8] = [a - b for a, b in zip(v_out, v_P[i])] # s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[8 + (i - 1) * 4] * T[i] * DAY2SEC, MU_SUN) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[8 + (i - 1) * 4]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, MU_SUN) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] # DSM occuring at time nu2*T2 DV[i] = norm([a - b for a, b in zip(v_beg_l, v)]) retval[6 + i * 8:9 + i * 8] = [a - b for a, b in zip(v_end_l, v_P[i + 1])] return retval
def _get_score_data_incipit(self,x): from PyKEP import epoch, lambert_problem, DAY2SEC, fb_prop, propagate_lagrangian from math import pi, acos,cos,sin,sqrt from scipy.linalg import norm """ This method returns the data needed to compute the score of a trajectory. """ #1 - we 'decode' the chromosome recording the various times of flight (days) in the list T for convenience T = x[3::4] nlegs = len(x)/4 seq = self.get_sequence() common_mu = seq[0].mu_central_body #2 - We compute the epochs and ephemerides of the planetary encounters ep_list = list([None] * nlegs) t_P = list([None] * nlegs) r_P = list([None] * nlegs) v_P = list([None] * nlegs) DV = list([None] * nlegs) for i,planet in enumerate(seq): ep_list[i] = x[0]+sum(T[:i+1]) t_P[i] = epoch(x[0]+sum(T[:i+1])) r_P[i],v_P[i] = seq[i].eph(t_P[i]) #3 - We start with the first leg: a lambert arc theta = 2*pi*x[1] phi = acos(2*x[2]-1)-pi/2 r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] l = lambert_problem(r,r_P[0],T[0]*DAY2SEC,common_mu, False, False) #Lambert arc to reach seq[1] v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] #init lists for fly-by parameters vinf_list = [] rp_list = [] beta_list = [] #First DSM occuring at the very beginning (will be cancelled by the optimizer) DV[0] = abs(norm(v_beg_l) - 3400) #4 - And we proceed with each successive leg for i in xrange(1,nlegs): #Fly-by v_out = fb_prop(v_end_l,v_P[i-1],x[1+4*i]*seq[i-1].radius,x[4*i],seq[i-1].mu_self) vinf_list.append( [a-b for a,b in zip(v_end_l,v_P[i-1])] ) rp_list.append(x[1+4*i]*seq[i-1].radius) beta_list.append(x[4*i]) #s/c propagation before the DSM r,v = propagate_lagrangian(r_P[i-1],v_out,x[4*i+2]*T[i]*DAY2SEC,common_mu) #Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1-x[4*i+2])*T[i]*DAY2SEC l = lambert_problem(r,r_P[i],dt,common_mu, False, False) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0] vinf_list.append([a-b for a,b in zip(v_end_l,v_P[-1])]) rp_list.append(None) beta_list.append(None) return zip(ep_list, seq, vinf_list, rp_list, beta_list)
def _part_plot(x, units, axis, seq, start_mjd2000, vinf_in): """ Plots the trajectory represented by a decision vector x = [beta,rp,eta,T] * N associated to a sequence seq, a start_mjd2000 and an incoming vinf_in """ from PyKEP.orbit_plots import plot_planet, plot_lambert, plot_kepler from PyKEP import epoch, propagate_lagrangian, lambert_problem, fb_prop, AU, MU_SUN, DAY2SEC from math import pi, acos, cos, sin from scipy.linalg import norm legs = len(x) / 4 common_mu = seq[0].mu_central_body # 1 - we 'decode' the chromosome recording the various times of flight # (days) in the list T T = x[3::4] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (legs + 1)) r_P = list([None] * (legs + 1)) v_P = list([None] * (legs + 1)) for i, planet in enumerate(seq): t_P[i] = epoch(start_mjd2000 + sum(T[:i])) r_P[i], v_P[i] = planet.eph(t_P[i]) plot_planet(planet, t0=t_P[i], color=(0.8, 0.6, 0.8), legend=True, units=units, ax=axis) v_end_l = [a + b for a, b in zip(v_P[0], vinf_in)] # 4 - And we iterate on the legs for i in range(0, legs): # Fly-by v_out = fb_prop(v_end_l, v_P[i], x[1 + 4 * i] * seq[i].radius, x[4 * i], seq[i].mu_self) # s/c propagation before the DSM r, v = propagate_lagrangian(r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu) plot_kepler(r_P[i], v_out, x[4 * i + 2] * T[i] * DAY2SEC, common_mu, N=500, color='b', legend=False, units=units, ax=axis) # Lambert arc to reach Earth during (1-nu2)*T2 (second segment) dt = (1 - x[4 * i + 2]) * T[i] * DAY2SEC l = lambert_problem(r, r_P[i + 1], dt, common_mu, False, False) plot_lambert(l, sol=0, color='r', legend=False, units=units, N=500, ax=axis) v_end_l = l.get_v2()[0] v_beg_l = l.get_v1()[0]