Skip to main content

OrbitSystem

OrbitSystem(G=gravitational_constant, bodies=None)

A set of bodies and the gravitational constant that binds them.

An OrbitSystem holds the starting state of a simulation: a table with one row per body (name, mass, position, velocity) and the value of G. You normally start one with create_system() rather than calling the class directly, then build it up with the add_* methods.

A system is never changed in place. Every add_* method and remove_body returns a new system and leaves the original untouched, so calls chain with dots, from an empty system through to a finished simulation. Printing a system shows G and the body table.

Parameters

Name Type Description Default
G float The gravitational constant in m^3 kg^-1 s^-2. The default is the measured value, orb.gravitational_constant (6.6743e-11). Use G=0 to turn gravity off. gravitational_constant
bodies DataFrame A body table with columns id, mass, x, y, z, vx, vy, vz (pandas or polars). The default, None, starts the system with no bodies. You rarely need this: add bodies with the methods instead, which validate their input. None

Attributes

Name Type Description
G float The gravitational constant the system was created with.
bodies DataFrame The body table, one row per body, with columns id, mass, x, y, z, vx, vy, vz. A copy; the same as get_bodies() with no arguments.

See Also

Examples

Build a system by chaining methods, then print it.

import pyorbiter as orb

system = (
    orb.create_system()
    .add_sun()
    .add_planet("Earth", parent="Sun")
    .add_planet("Moon", parent="Earth")
)
system
───────────────────────────────── orbit_system ─────────────────────────────────
G: 6.6743e-11 (standard)
Bodies: 3

   id         mass             x            y             z            vx           vy        vz
  Sun 1.989000e+30  0.000000e+00 0.000000e+00  0.000000e+00      0.000000     0.000000  0.000000
Earth 5.972000e+24 -3.296559e+10 1.433603e+11  0.000000e+00 -29520.187051 -6788.143346  0.000000
 Moon 7.342000e+22 -3.292356e+10 1.437205e+11 -2.175733e+07 -30585.853990 -6659.462622 71.933632

Each step returns a new system, so an earlier one can be reused.

import pyorbiter as orb

sun_only = orb.create_system().add_sun()
with_mars = sun_only.add_planet("Mars", parent="Sun")
len(sun_only), len(with_mars)
(1, 2)

Methods

Name Description
simulate Integrate the system forward in time.
save Write the system to a JSON file.
export_bodies Write the body table to a CSV file.

simulate

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

Integrate the system forward in time.

The chained form of simulate_system(), which has the full description of each argument and of the integration methods.

Parameters

Name Type Description Default
time_step float Seconds between recorded states. Default one hour (orb.seconds_per_hour). seconds_per_hour
duration float Total simulated time in seconds. Default one year (orb.seconds_per_year). seconds_per_year
method str The integrator: "verlet" (the default), "euler_cromer" or "euler". 'verlet'
softening float Softening length in meters. Default 0, which is plain Newtonian gravity. 0
backend str "pandas" or "polars". The default, None, uses the backend chosen with set_backend(). None

Returns

Name Type Description
DataFrame A pandas DataFrame by default, or a polars DataFrame. One row per body per recorded time, with columns id, mass (kg), x, y, z (m), vx, vy, vz (m/s) and time (s).

See Also

Examples

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

save

save(path)

Write the system to a JSON file.

The chained form of save_system(). Read the file back with load_system().

Parameters

Name Type Description Default
path str or path - like The file to write. It is overwritten if it exists. required

Returns

Name Type Description
OrbitSystem The same system, so the chain can continue.

See Also

Examples

import os
import tempfile

import pyorbiter as orb

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "earth.json")
    orb.create_system().add_sun().add_planet("Earth", parent="Sun").save(path)
    restored = orb.load_system(path)

restored
───────────────────────────────── orbit_system ─────────────────────────────────
G: 6.6743e-11 (standard)
Bodies: 2

   id         mass             x            y   z            vx           vy  vz
  Sun 1.989000e+30  0.000000e+00 0.000000e+00 0.0      0.000000     0.000000 0.0
Earth 5.972000e+24 -3.296559e+10 1.433603e+11 0.0 -29520.187051 -6788.143346 0.0

export_bodies

export_bodies(path)

Write the body table to a CSV file.

The chained form of export_bodies().

Parameters

Name Type Description Default
path str or path - like The CSV file to write. It is overwritten if it exists. required

Returns

Name Type Description
OrbitSystem The same system, so the chain can continue.

See Also

  • export_bodies: The same thing as a function, documented in full.
  • OrbitSystem.save: Save the whole system, including G.

Examples

import os
import tempfile

import pyorbiter as orb

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "bodies.csv")
    orb.create_system().add_sun().add_planet("Earth", parent="Sun").export_bodies(path)
    with open(path) as fh:
        text = fh.read()

print(text)
id,mass,x,y,z,vx,vy,vz
Sun,1.989e+30,0.0,0.0,0.0,0.0,0.0,0.0
Earth,5.972e+24,-32965585120.94338,143360295955.52682,0.0,-29520.18705135016,-6788.143345695552,0.0