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
create_system: Start an empty system to add bodies to.
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:
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:
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 orbsim = 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 pdimport pyorbiter as orbsystem = 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)