Getting used to programming collaboratively in python can be quite hard, as there are lots of tooling options to choose from. Therefore, the first exercise is to just be able to run the following command:
$ plan-a-trip-to-mars
Hello, World!
This is plan-a-trip-to-mars, version 1.0.0
To do this, follow the installation instructions below.
Let us try to make a change to the code. Open the file
./src/plan_a_trip_to_mars/simulation.py
, and edit it such that the default behaviour
is to save the animations. This is set in the __init__
method of the Sim
class.
Open the file ./src/plan_a_trip_to_mars/scenarios.py
. The python class BigScenario
works as a template or parent for all the other classes that inherit it. Using the
other classes (Simpl
, Mayhem
and Jerk
) as guiding, create a new class for a Mars
orbit, for example named MarsTransfer
.
Figure out which methods (a function tied to a class) are needed for the new
MarsTransfer
class to work.
As a starting point you should be able to run the simulation with three bodies: the Sun, Earth and Mars. Their positions and velocities are not important yet, just make sure they exist in the simulation.
Now, adjust the positions and velocities of the three bodies (Sun, Earth, Mars) so that the Earth and Mars move in circular orbits around the Sun.
Use the simulation to obtain a reasonable value for the amount of days between two Mars oppositions, that is, the amount of days that passes between two occasions that Earth and Mars are aligned with the Sun.
Tip
The base class BigScenario
has a method named do_at_each_time_step()
. You may make
use of this in order to get an estimate of the opposition time. Think about where the
do_at_each_time_step()
method should be implemented. Is it used by any other
scenario class?
Calculate the velocity needed for a rocket to travel from Earth to Mars on a Hohmann transfer orbit, i.e., the relative velocity with respect to Earth.
Using the program, obtain a reasonable number for the amount of days it takes for the rocket to complete the transfer orbit, i.e., the amount of days until it arrives at Mars. You should use your answer from problem 4 to place the Earth and Mars in a good position for the transfer orbit.
How much must the velocity of the rocket increase upon arrival at Mars if it were to
follow the same orbit? Give the rocket a kick
on the day of arrival and confirm that
the rocket and Mars move as a pair.
If the rocket missed on the timing of the transfer orbit, but is on Mars's orbit just some distance behind, how would you adjust its velocity so that it overtakes Mars? Explain.
A rocket sits on a circular trajectory when a sudden impact in the radial direction sends it on a parabolic trajectory. Find the velocity needed to get it on a parabolic trajectory, and the closest point the parabolic trajectory will have to the centre object. Simulate the path it takes.
The project is packaged with Pixi and Uv, and additionally solved for Conda. All three installation alternatives are available for all three major platforms: Linux, OSX and Windows. You will need to use one of them to install this project.
Uv is a python-only tool for handling your project, and extremely fast.
Install Uv via their website.
Pixi is similar to anaconda (just younger/more modern) and I have tested that it works with for example Spyder.
Install Pixi via their website.
Conda has been around for a long time and has widespread support. The files that specify the environments are generated using Pixi, as
environment.linux-64.yml
environment.osx-64.yml
environment.osx-arm64.yml
environment.win-64.yml
Install Conda via their website.
With one of the above tools installed (Uv/Pixi/Conda), you will now need to clone the
repository and cd
into it. This is done using git. (Alternatively, you may download
the repository in a zip-file from GitHub.) Open a console, typically an application
called Terminal is available for this purpose, and run the following commands:
# If you typically have all coding projects or other things you work on in a designated
# folder called `my-projects-folder`, you should first change directory `cd` to that:
cd ~/my-projects-folder
# We then clone the repo. Use the URL of your own repo.
git clone https://github.com/flottflyt/plan-a-trip-to-mars.git
# Now, `cd` into the newly cloned directory.
cd plan_a_trip_to_mars || exit
Now you can install the project with either Uv, Pixi or Conda:
# uv
uv sync
# pixi
pixi install
# conda (replace the file name to match your operating system platform)
conda env create --name name-of-my-env --file environment.linux-64.yml
You will now be able to run the python code! This can be done in two ways with slightly different syntax depending on if you used Uv, Pixi or Conda to install the project.
# uv
uv run plan-a-trip-to-mars
uv run python ./src/plan_a_trip_to_mars/simulation.py
# pixi
pixi run -e spyder plan-a-trip-to-mars
pixi run -e spyder python ./src/plan_a_trip_to_mars/simulation.py
# conda
conda run -n name-of-my-env plan-a-trip-to-mars
conda run -n name-of-my-env python ./src/plan_a_trip_to_mars/simulation.py
Tip
You can open the project in Spyder by running the following command in a terminal:
pixi run -e spyder spyder
Note on virtual environments
When working on a python project, the best practice is to work inside a virtual environment. This can be confusing to begin with, but the pros massively outweighs the cons. Many programs exist that creates and manages virtual environments, and both Pixi and Uv will do this automatically for you!
Many good alternatives for working with python using virtual environments exist. Pick your favourite and learn how to use it.
Five scenario constants exists, defined inside a single object SimulationConstants
:
size
: The length of the sides of the simulation, in metres.total_time
: The total time of the simulation, in units ofspi
. That is, changing thespi
will change the unit of the total time (e.g. seconds to hours). This value decides how many iterations the simulation will use.fps
: The frame rate of the animation. After the simulation has been calculated, only everyn
-th iteration is used (forfps=n
). Useful if you need high temporal resolution, but a faster simulation.time_scale
: The clock shown in the animation is divided bytime_scale
, effectively changing the time unit.unit
: Add a time unit to the simulation clock.
The spi
decides how many seconds pass per iteration (seconds-per-iteration). By
default, everything is calculated using SI units, meaning seconds for time. This quickly
become computationally expensive when you want to simulate a solar system. Setting the
spi
to 3600
will instead update all positions, velocities, etc. every hour. Be
careful to also change the timing of events; the time of a rocket's kick
is now
specified in hours.