Skip to main content

get_energy

get_energy(sim_data, G=None, softening=None)

Total kinetic, potential and mechanical energy at every time step.

Sums the kinetic energy of every body and the gravitational potential energy of every pair of bodies. In an isolated system the total is constant, so any change in the energy column over a run is numerical error from the integrator. Use it to judge whether a time step is small enough.

Parameters

Name Type Description Default
sim_data DataFrame The output of simulate_system(): a pandas or polars DataFrame with columns id, mass, x, y, z, vx, vy, vz and time, in SI units. required
G float The gravitational constant in m^3 kg^-1 s^-2. The default None uses the value recorded in the run (see run_settings()), or gravitational_constant if none is recorded. None
softening float The softening length in meters, which should match the one the run used. The default None uses the value recorded in the run, or 0 if none is recorded. None

Returns

Name Type Description
DataFrame The same kind of DataFrame it was given (pandas or polars), with one row per time step, sorted by time, and these columns:
• time: time in seconds.
• kinetic: total kinetic energy in joules.
• potential: total gravitational potential energy in joules. Zero or negative.
• energy: kinetic + potential, in joules.

Raises

Name Type Description
TypeError If sim_data is not a pandas or polars DataFrame with the columns simulate_system() produces.

See Also

Notes

The energies are

\[K = \sum_i \tfrac{1}{2} m_i v_i^2, \qquad U = -\sum_{i<j} \frac{G m_i m_j}{\sqrt{r_{ij}^2 + \epsilon^2}}\]

where \(r_{ij}\) is the distance between bodies \(i\) and \(j\) and \(\epsilon\) is the softening length. A bound system has negative total energy.

Kinetic energy depends on the reference frame. The total is conserved in the simulation’s own frame and in the barycentric frame, but not in a frame centered on an accelerating body.

A polars DataFrame only remembers its run settings while it is the exact object pyorbiter returned. After a filter or with_columns, pass G and softening yourself if the run did not use the defaults.

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)
)
orb.get_energy(sim).head()
time kinetic potential energy
0 0.0 2.739716e+33 -5.389428e+33 -2.649712e+33
1 86400.0 2.739702e+33 -5.389414e+33 -2.649712e+33
2 172800.0 2.739659e+33 -5.389372e+33 -2.649712e+33
3 259200.0 2.739589e+33 -5.389301e+33 -2.649712e+33
4 345600.0 2.739491e+33 -5.389203e+33 -2.649712e+33