Skip to main content

simulate_system

simulate_system(
    system,
    time_step=seconds_per_hour,
    duration=seconds_per_year,
    method='verlet',
    softening=0,
    backend=None,
)

Integrate a system forward in time.

Propagates every body in an OrbitSystem under the mutual gravity of all the others and records the state at each time step. The result is a tidy DataFrame with one row per body per time step, ready for the analysis and plotting functions. The default integrator is velocity Verlet, which keeps the total energy bounded and gives stable orbits over long runs.

system.simulate(...) is the same function as a method, for use at the end of a chain.

Parameters

Name Type Description Default
system OrbitSystem The system to simulate, built with create_system and the add_ methods, or loaded with load_solar_system. It is not modified. required
time_step float Time between recorded states, in seconds. This is also the integration step: there are no hidden substeps. The default is one hour (orb.seconds_per_hour, 3600 s). For planets around a star, one day (orb.seconds_per_day) is usually enough. For the Moon or tighter orbits, use an hour or less. seconds_per_hour
duration float Total simulated time in seconds. The default is one year (orb.seconds_per_year, 31557600 s). States are recorded at times 0, time_step, 2 time_step, and so on, up to the last whole step that fits in duration. A duration of 0 records only the initial state. seconds_per_year
method str The integrator: "verlet" (velocity Verlet, the default), "euler_cromer" (semi-implicit Euler), or "euler" (explicit Euler). See Notes. 'verlet'
softening float Softening length in meters. The squared separation r^2 between each pair of bodies is replaced by r^2 + softening^2 in the force, which prevents a numerical blow-up when two bodies pass very close to each other. The default 0 means no softening. A value like 1e4 (10 km) is reasonable for planetary systems. 0
backend str or None The kind of DataFrame to return: "pandas" or "polars". The default None uses the library chosen with set_backend, which is pandas unless you changed it. None

Returns

Name Type Description
DataFrame A pandas DataFrame by default, or a polars DataFrame. It has one row per body per recorded time, ordered by time and then by the order the bodies were added, with columns:
• id: name of the body.
• mass: mass in kilograms.
• x, y, z: position in meters.
• vx, vy, vz: velocity in meters per second.
• time: time since the start of the run, in seconds. The run’s G, softening, method and time_step are recorded with the result. Read them back with run_settings. For a pandas DataFrame they are also in sim.attrs. The analysis functions and continue_simulation pick them up from there.

Raises

Name Type Description
TypeError If system is not an OrbitSystem.
ValueError If method is not one of the three integrators, if time_step is not positive, if duration is negative, or if backend is not "pandas" or "polars".
ImportError If the polars backend is requested and polars is not installed.

See Also

Notes

Each step advances positions \(\mathbf{r}\) and velocities \(\mathbf{v}\) by a time \(\Delta t\) using the accelerations \(\mathbf{a}\) from Newtonian gravity. The three integrators differ in the order of the updates.

Velocity Verlet ("verlet") moves the positions with the current acceleration, recomputes the acceleration at the new positions, and then updates the velocities with the average of the old and new accelerations:

\[ \mathbf{r}_{n+1} = \mathbf{r}_n + \mathbf{v}_n \Delta t + \tfrac{1}{2} \mathbf{a}_n \Delta t^2, \qquad \mathbf{v}_{n+1} = \mathbf{v}_n + \tfrac{1}{2} (\mathbf{a}_n + \mathbf{a}_{n+1}) \Delta t \]

It is second order in \(\Delta t\) and symplectic, so the energy error oscillates within a bound instead of growing. Use it unless you have a reason not to.

Euler-Cromer ("euler_cromer") updates the velocities first and then moves the positions with the new velocities:

\[ \mathbf{v}_{n+1} = \mathbf{v}_n + \mathbf{a}_n \Delta t, \qquad \mathbf{r}_{n+1} = \mathbf{r}_n + \mathbf{v}_{n+1} \Delta t \]

It is first order but also symplectic, so the energy error stays bounded, with a larger error than Verlet at the same step.

Euler ("euler") moves the positions with the old velocities and then updates the velocities:

\[ \mathbf{r}_{n+1} = \mathbf{r}_n + \mathbf{v}_n \Delta t, \qquad \mathbf{v}_{n+1} = \mathbf{v}_n + \mathbf{a}_n \Delta t \]

It is first order and not symplectic. The energy grows steadily and orbits spiral outward. It is included for teaching, to show what goes wrong.

All three use one force calculation per step, so they cost the same. The force calculation covers every pair of bodies, so the run time grows with the square of the number of bodies.

Softening changes the force law, so a softened run is a slightly different physical system. get_energy uses the softening recorded with the run, so the energy it reports is the one that run conserves.

Examples

The Earth around the Sun for one year, in daily steps.

import pyorbiter as orb

sim = orb.create_system().add_sun().add_planet("Earth", parent="Sun").simulate(
    time_step=orb.seconds_per_day, duration=orb.seconds_per_year)
sim.head()
id mass x y z vx vy vz time
0 Sun 1.989000e+30 0.000000e+00 0.000000e+00 0.0 0.000000 0.000000 0.0 0.0
1 Earth 5.972000e+24 -3.296559e+10 1.433603e+11 0.0 -29520.187051 -6788.143346 0.0 0.0
2 Sun 1.989000e+30 -1.540745e+01 6.700373e+01 0.0 -0.000370 0.001548 0.0 86400.0
3 Earth 5.972000e+24 -3.551100e+10 1.427515e+11 0.0 -29396.816453 -7303.615723 0.0 86400.0
4 Sun 1.989000e+30 -6.400891e+01 2.674448e+02 0.0 -0.000768 0.003089 0.0 172800.0

The same run with each integrator, comparing the relative change in total energy between the first and last step. Verlet and Euler-Cromer hold the energy. Explicit Euler does not.

import pandas as pd
import pyorbiter as orb

system = orb.create_system().add_sun().add_planet("Earth", parent="Sun")

rows = []
for method in ["verlet", "euler_cromer", "euler"]:
    sim = orb.simulate_system(system, time_step=orb.seconds_per_day,
                              duration=orb.seconds_per_year, method=method)
    energy = orb.get_energy(sim)["energy"]
    rows.append({"method": method,
                 "relative_energy_change": (energy.iloc[-1] - energy.iloc[0]) / abs(energy.iloc[0])})
pd.DataFrame(rows)
method relative_energy_change
0 verlet 2.076659e-11
1 euler_cromer -1.383286e-06
2 euler 1.520419e-01