Skip to main content

save_system

save_system(system, path)

Save a system to a JSON file.

Writes the system’s gravitational constant and its body table to a file that load_system() can read back. Use it to keep a set of starting conditions, or to share one with someone else. The file is plain JSON, so it can be opened in a text editor. Also available as the chained method system.save(path).

Parameters

Name Type Description Default
system OrbitSystem The system to save. required
path str or path - like The file to write, for example "my_system.json". It is overwritten if it exists. required

Returns

Name Type Description
OrbitSystem The same system, unchanged, so the call can sit in the middle of a chain.

Raises

Name Type Description
TypeError If system is not an OrbitSystem.

See Also

Notes

The file holds four entries: format (the text "pyorbiter.orbit_system"), version (1), G, and bodies, a list with one record per body giving id, mass, x, y, z, vx, vy and vz. Only the starting state is saved, not the results of a simulation.

Examples

Save a system to a temporary folder and load it again.

import os
import tempfile

import pyorbiter as orb

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

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "earth.json")
    orb.save_system(system, 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