Python API¶
This page is generated from the docstrings in the source code.
The most common functions can be imported straight from the package:
from celestial_rhapsody import load_system, simulate, animate
Reading input¶
Reading the input file that describes the system to simulate.
Two formats are supported:
JSON (
.json), the recommended format;the original INI-style text format (
.txt) with[Global]and[Planet]sections.
Use load_system() for either one. It picks the reader from the file extension.
- class celestial_rhapsody.config.System(names, masses, positions, velocities, G=1.0, dt=0.01, final_time=5.0, softening=0.0)[source]¶
A set of bodies plus the simulation settings.
- Parameters:
names (List[str])
masses (ndarray)
positions (ndarray)
velocities (ndarray)
G (float)
dt (float)
final_time (float)
softening (float)
- names¶
One label per body.
- Type:
List[str]
- masses¶
Masses, shape
(n,).- Type:
numpy.ndarray
- positions¶
Initial positions, shape
(n, dim)withdim2 or 3.- Type:
numpy.ndarray
- velocities¶
Initial velocities, shape
(n, dim).- Type:
numpy.ndarray
- G¶
Gravitational constant.
- Type:
float
- dt¶
Integration time step.
- Type:
float
- final_time¶
Time at which the simulation stops.
- Type:
float
- softening¶
Optional softening length (0 means pure Newtonian gravity).
- Type:
float
- celestial_rhapsody.config.read_config(file_name)[source]¶
Read the INI-style text config file (the original project format).
The file starts with a
[Global]section (G,dt,final_time) followed by one[Planet]section per body (name,mass,x_0,y_0,vx_0,vy_0). Values are read in order, so keep the order of the lines as in the examples.- Parameters:
file_name (str | Path) – Path of the config file.
- Raises:
RuntimeError – If the file cannot be opened or a line cannot be parsed.
- Returns:
A list with the global values
[G, dt, final_time]and a list of planets, each[name, mass, x_0, y_0, vx_0, vy_0].- Return type:
Tuple[list, list]
- celestial_rhapsody.config.system_from_dict(data)[source]¶
Build a
Systemfrom a dictionary with the JSON layout.See the Input format page of the documentation for all fields.
- Parameters:
data (dict)
- Return type:
Simulation¶
Newtonian gravity and the time integrators (Euler, RK2, RK4).
- class celestial_rhapsody.solvers.Trajectory(times, positions, velocities, energy, names, masses)[source]¶
Result of
simulate().- Parameters:
times (ndarray)
positions (ndarray)
velocities (ndarray)
energy (ndarray)
names (List[str])
masses (ndarray)
- times¶
Times of the saved frames, shape
(n_frames,).- Type:
numpy.ndarray
- positions¶
Shape
(n_frames, n_bodies, dim).- Type:
numpy.ndarray
- velocities¶
Shape
(n_frames, n_bodies, dim).- Type:
numpy.ndarray
- energy¶
Total energy at each saved frame. It should stay nearly constant; a large drift means
dtis too big.- Type:
numpy.ndarray
- names¶
Body labels.
- Type:
List[str]
- masses¶
Body masses.
- Type:
numpy.ndarray
- property energy_drift: float¶
Relative change of total energy between first and last frame.
- celestial_rhapsody.solvers.accelerations(positions, masses, G=1.0, softening=0.0)[source]¶
Gravitational acceleration of every body due to all the others.
\[\mathbf{a}_i = -G \sum_{k \neq i} m_k \frac{\mathbf{r}_i - \mathbf{r}_k}{\left(|\mathbf{r}_i - \mathbf{r}_k|^2 + \epsilon^2\right)^{3/2}}\]- Parameters:
positions (ndarray) – Shape
(n, dim).masses (ndarray) – Shape
(n,).G (float) – Gravitational constant.
softening (float) – Softening length \(\epsilon\).
- Returns:
Accelerations, shape
(n, dim).- Return type:
ndarray
- celestial_rhapsody.solvers.total_energy(positions, velocities, masses, G=1.0, softening=0.0)[source]¶
Kinetic plus gravitational potential energy.
- Parameters:
positions (ndarray)
velocities (ndarray)
masses (ndarray)
G (float)
softening (float)
- Return type:
float
- celestial_rhapsody.solvers.euler_step(x, v, dt, acc)[source]¶
Forward Euler (1st order). Simple but drifts quickly; for teaching only.
- Parameters:
x (ndarray)
v (ndarray)
dt (float)
acc (Callable[[ndarray], ndarray])
- Return type:
Tuple[ndarray, ndarray]
- celestial_rhapsody.solvers.rk2_step(x, v, dt, acc)[source]¶
Midpoint Runge-Kutta (2nd order).
- Parameters:
x (ndarray)
v (ndarray)
dt (float)
acc (Callable[[ndarray], ndarray])
- Return type:
Tuple[ndarray, ndarray]
- celestial_rhapsody.solvers.rk4_step(x, v, dt, acc)[source]¶
Classic Runge-Kutta (4th order). The default.
- Parameters:
x (ndarray)
v (ndarray)
dt (float)
acc (Callable[[ndarray], ndarray])
- Return type:
Tuple[ndarray, ndarray]
- celestial_rhapsody.solvers.simulate(system, method='rk4', save_every=1)[source]¶
Integrate the system from
t = 0tosystem.final_time.The last step is shortened if needed so the run ends exactly at
final_time.- Parameters:
system (System) – Bodies and settings, usually from
celestial_rhapsody.load_system().method (str) –
"rk4"(default),"rk2"or"euler".save_every (int) – Keep one frame every
save_everysteps. The final state is always kept.
- Returns:
A
Trajectory.- Return type:
Animation¶
Animation of a Trajectory (GIF or MP4).
- celestial_rhapsody.animation.animate(trajectory, output='movie.gif', fps=30, trail=None, title=None, dpi=100)[source]¶
Save an animation of the orbits in the x-y plane.
The format comes from the file extension:
.gifworks everywhere,.mp4needs ffmpeg.- Parameters:
trajectory (Trajectory) – Output of
celestial_rhapsody.simulate().output (str | Path) – File to write, e.g.
"orbits.gif".fps (int) – Frames per second.
trail (int | None) – Number of past frames drawn behind each body.
None(default) draws the whole path;0draws none.title (str | None) – Optional plot title.
dpi (int) – Resolution.
- Returns:
Path of the written file.
- Return type:
Path