- Simulate Interplanetary Space Travel in Python
- Phillip Heita
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Z Zhaojin
- Proofread by: jaredliw, Chor
Interplanetary space travel using Python simulations
Simulate the transfer of a virtual spacecraft from Earth to Mars using Python.
For the past few decades, the Hohmann transfer has been the preferred method for all lunar and interplanetary space travel. This article will use Python to simulate the transfer of a virtual spacecraft from Earth to Mars.
This article discusses an approach to interstellar space travel and a simulated implementation using Python.
What is the Hohmann transfer orbit
A Homan transfer orbit is how a spacecraft moves between two orbits (for example, Earth and Mars) with the least amount of energy, and is also considered the most efficient transfer orbit. A Hohmann transfer orbit involves accelerating a spacecraft into an elliptical orbit with its perihelion (the point in hohmann’s orbit closest to the Sun) on the inner planet and aphelion (the point in Hohmann’s orbit farthest from the Sun) on the outer planet’s orbit.
A relatively new application of Homan’s transfer is the Mars Science Laboratory (MSL) mission to Mars, which took about nine months. An important consideration for the Homan transfer orbit is the timing of the launch so that Mars will be in that exact position when the spacecraft arrives.
The key steps of The Homan transfer orbit include calculating the orbital period of the spacecraft. In the Earth-to-Mars scenario, that would take about 520 days. The spacecraft has to travel half its orbit, so a trip would be about 260 days. Using the distance the spacecraft traveled and Mars’ orbital period (about 687 days), we found that the best time to launch a spacecraft is when Mars is 44 degrees ahead of Earth in its orbit, as shown in the image above. An excellent way to see this is to check out the Planet Transfer Calculator, a solar system simulator that calculates transfers between planets, moons and stars.
Use Python emulation
This section describes the code that simulates hohmann transitions. First, import some basic libraries for numerical calculations and plotting.
import numpy as np
import matplotlib asMPL MPL. Use (the 'PDF')import matplotlib.pyplot as plt
from matplotlib import animation
Copy the code
The code below defines the critical Angle at which Mars should lead in its orbit, which is 44 degrees. On average, Earth is 150 million kilometers from the Sun, a distance we call 1 Astronomical Unit (AU), while Mars is about 1.52 AU. To plot the orbits of Earth and Mars, we use the Circle() function of Matplotlib and give them radii of 1 and 1.52 respectively.
# Map the orbits of Earth and Mars
alpha = 44 # Degree (should lead the Angle)
Earth = plt.Circle((0.0), radius= 1.0,fill=False, color = "blue") Mars = PLT. Circle ((0.0), radius= 1.52,fill=FalseColor = 'brown')Copy the code
Moving parts, namely blue Earth, brown Mars and red spacecraft, are defined in the following way:
# Move Earth, Mars, and spacecraft
patch_E = plt.Circle((0.0.0.0),radius=0.04,fill=TruePatch_M = PLT, color = "blue"). The Circle ((0.0.0.0),radius=0.03,fill=True, color = 'brown') patch_H = PLT. Circle ((0.0.0.0),radius=0.01,fill=TrueColor = "red")Copy the code
Then we define two basic functions. The first function creates the base frame of the animation, the initial position of all the moving parts, and this function takes only one parameter, the number of frames I. The animate function includes all the necessary orbital equations that describe various motions. To learn more about these two functions, consider this tutorial.
def init() :
patch_E.center = (0.0.0.0)
ax.add_patch(patch_E)
patch_M.center = (0.0.0.0)
ax.add_patch(patch_M)
patch_H.center = (0.0.0.0)
ax.add_patch(patch_H)
return patch_E,patch_M,patch_H
def animate(i) :
# the earth
x_E, y_E = patch_E.center
x_E = np.cos((2*np.pi/365.2)*i)
y_E = np.sin((2*np.pi/365.2)*i)
patch_E.center = (x_E, y_E)
# Mars
x_M, y_M = patch_M.center
x_M = 1.52*np.cos((2*np.pi/686.98)*i+(np.pi*alpha/180.))
y_M = 1.52*np.sin((2*np.pi/686.98)*i+(np.pi*alpha/180.))
patch_M.center = (x_M,y_M)
# spacecraft
Period = 516.0
x_H = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period)*i))*np.cos((2*np.pi/Period)*i)
y_H = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period)*i))*np.sin((2*np.pi/Period)*i)
patch_H.center = (x_H,y_H)
return patch_E,patch_M,patch_H
Copy the code
To create animations, you use the Writer() function to set the movie file format, which controls the number of frames per second (FPS) and bit rate. In the code below, we also set up the drawing layout, namely the font and label sizes. To better guide the line of sight, we traced the spacecraft’s path in gray.
# Set the file format of the movie
# plt.rcparams [' savefig. Bbox '] = 'tight' # seriously garble video!!Writer = animation. Writers [' ffmpeg '] Writer = Writer(FPS =60, metadata=dict(artist = 'Me'), bitrate =1800) plt.rc(' font ', family= 'serif', serif= 'Times') plt.rc(' text', usetex=True) PLT. Rc (' xtick ', labelsize =8) PLT. Rc (ytick, labelsize =8) PLT. Rc (axes, labelsize =8)
Set the path and guide the line of sight
Period = 516.
x_H_B = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*75)))*np.cos((2*np.pi/Period*75))
y_H_B = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*75)))*np.sin((2*np.pi/Period*75))
x_H_C = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*150)))*np.cos((2*np.pi/Period*150))
y_H_C = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*150)))*np.sin((2*np.pi/Period*150))
x_H_D = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*200)))*np.cos((2*np.pi/Period*200))
y_H_D = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*200)))*np.sin((2*np.pi/Period*200))
x_H_M = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*250)))*np.cos((2*np.pi/Period*250))
y_H_M = 1.26* (1. — 0.21**2)/(1. + 0.21*np.cos((2*np.pi/Period*250)))*np.sin((2*np.pi/Period*250))
Copy the code
Finally, use the following code to put the entire animation together. The main content of this code is to set up the diagram, the various axes, and the planets defined earlier. The key lines are calling animation.funcanimation () and saving the animation.
fig, ax = plt.subplots(figsize=(10.8))
# fig.subplots_adjust(left=.15, bottom=.16, right=.99, top=.97)
ax.plot(0.0, color = orange, marker = 'o', graphics.linestyle = ', 'markersize =16, markerfacecolor = "yellow", ax label = "Sun"). The plot ([], [], color = 'blue', graphics.linestyle = ', 'marker =' o ', label = "Earth") Ax. The plot ([], [], color = brown, graphics.linestyle = ', 'marker =' o ', label = 'Mars') Ax. The plot ([], [], color = "red", graphics.linestyle = ', 'marker =' o ', label = "spacecraft") ax. The plot (x_H_B y_H_B, color = "dimgray", marker = p ', 'markerfacecolor = "dimgray", graphics.linestyle = ", ax label = "path"). The plot (x_H_C y_H_C, color = "dimgray", marker = 'p', markerfacecolor ax = "dimgray"). The plot (x_H_D y_H_D, color = "dimgray", marker = 'p', markerfacecolor = "dimgray") Ax. The plot (x_H_M y_H_M, color = "dimgray", marker = 'p', markerfacecolor = "dimgray") ax. Add_patch (Earth) ax. Add_patch (Mars) Ax. Set_xlabel (' X (AU), fontsize =12) ax. Set_ylabel (Y (AU), fontsize =12) ax. Legend (fontsize = loc = 'best'12)
anim = animation.FuncAnimation(fig, animate,init_func=init,frames=260,interval=40,blit=True) PLT. Axis (" three ")# Real time zoom drawingPLT. Savefig (' Hohmann. PDF) anim. Save (" Hohmann. Mp4, writer = writer) PLT. The show ()Copy the code
What happens if the launch is off by 10 degrees
As expected, changing the launch Angle from 44 degrees to 54 degrees caused the spacecraft to deviate considerably from its Mars intercept point.
That’s all for this article, and I hope you found it interesting! If you’ve also created any cool animations in Python, feel free to share them with me.
Thank you for reading!
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.