Tutorial ======== This tutorial takes about 10 minutes. You will 1. run a ready-made three-body system, 2. write your own input file for a star with two planets, 3. compare the integrators, 4. do the same from Python. It assumes you have :doc:`installed ` the package. Step 1: Run a first simulation ------------------------------ Make an empty folder, and in it create a file called ``three_body.json``: .. code-block:: json { "G": 1.0, "dt": 0.01, "final_time": 5, "bodies": [ {"name": "P1", "mass": 1.0, "position": [0.8083106230, 0.0], "velocity": [0.0, 0.9901979166]}, {"name": "P2", "mass": 1.0, "position": [-0.4954148566, 0.0], "velocity": [0.0, -2.7171431768]}, {"name": "P3", "mass": 1.0, "position": [-0.3128957664, 0.0], "velocity": [0.0, 1.7269452602]} ] } (This file is also in the ``examples/`` folder of the repository.) Run it: .. code-block:: bash nbody run three_body.json You should see: .. code-block:: text Simulating 3 bodies with RK4 up to t=5.0 (dt=0.01)... Relative energy drift: 8.64e-05 Rendering 251 frames to three_body.gif ... Done: three_body.gif Open ``three_body.gif``. It should look like the animation on the :doc:`home page `. .. note:: **What is the energy drift?** In an isolated system the total energy is conserved. Any change comes from numerical error, so the drift is a quick check of accuracy. Below about ``1e-3`` is fine. If it is larger, use a smaller ``dt``. Step 2: Write your own system ----------------------------- Now a star with two planets. Create ``two_planets.json``: .. code-block:: json { "G": 1.0, "dt": 0.01, "final_time": 30, "bodies": [ {"name": "Star", "mass": 1.0, "position": [0, 0], "velocity": [0, 0]}, {"name": "Inner", "mass": 0.001, "position": [1, 0], "velocity": [0, 1.0]}, {"name": "Outer", "mass": 0.01, "position": [0, 2], "velocity": [-0.707, 0]} ] } **Where do the velocities come from?** A body on a circular orbit of radius :math:`r` around a mass :math:`M` moves at :math:`v = \sqrt{GM/r}`. With :math:`G = M = 1`: * inner planet, :math:`r = 1`: :math:`v = 1`; * outer planet, :math:`r = 2`: :math:`v = \sqrt{1/2} \approx 0.707`. The velocity points perpendicular to the line joining the planet and the star. That is why the inner planet moves along ``y`` and the outer one along ``-x``. First, check the file. This finds typos without running anything: .. code-block:: bash nbody check two_planets.json .. code-block:: text OK: 3 bodies (Star, Inner, Outer), 2D, G=1.0, dt=0.01, final_time=30.0 Then run it. ``--every 5`` keeps one frame in five, so the GIF stays small: .. code-block:: bash nbody run two_planets.json --every 5 .. tip:: Change the inner planet's velocity to ``[0, 1.2]``. The orbit becomes an ellipse. At ``[0, 1.5]`` the planet escapes. Step 3: Compare integrators --------------------------- Three integration schemes are built in. Try them on the same system with a fairly large time step: .. code-block:: bash nbody run two_planets.json --method euler --dt 0.05 -o euler.gif nbody run two_planets.json --method rk2 --dt 0.05 -o rk2.gif nbody run two_planets.json --method rk4 --dt 0.05 -o rk4.gif Compare the energy drift printed for each one, and look at the orbits. With Euler the inner planet spirals outward. RK4 keeps the orbits closed. Step 4: Use it from Python -------------------------- Everything the command does is also available as functions, which is handy in Jupyter notebooks or to analyse the results: .. code-block:: python import matplotlib.pyplot as plt from celestial_rhapsody import load_system, simulate, animate system = load_system("two_planets.json") trajectory = simulate(system, method="rk4", save_every=5) # positions has shape (frames, bodies, 2) print(trajectory.positions.shape) # Plot the energy to check the accuracy plt.plot(trajectory.times, trajectory.energy) plt.xlabel("time") plt.ylabel("total energy") plt.savefig("energy.png") # Make the animation, with trails 100 frames long animate(trajectory, "two_planets.gif", trail=100) You can also build a system directly in code: .. code-block:: python from celestial_rhapsody import System, simulate system = System( names=["Star", "Planet"], masses=[1.0, 0.001], positions=[[0, 0], [1, 0]], velocities=[[0, 0], [0, 1]], G=1.0, dt=0.01, final_time=10, ) trajectory = simulate(system) Next steps ---------- * Every field of the input file, and the older ``.txt`` format: :doc:`input_format` * All command options: :doc:`cli` * Every function: :doc:`api`