Skip to main content

conserved_quantities

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

Energy, momentum and angular momentum at every step, with relative errors.

Combines get_energy(), get_momentum() and get_angular_momentum() into one table and adds three error columns that measure how far each quantity has moved from its starting value. An isolated system conserves all three exactly, so the errors measure the integrator, not the physics. This is the quickest check that a time step is small enough and that a method suits the problem.

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. Use the simulation as it came out, not a frame shifted onto a body. 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, potential, energy: energies in joules, as in get_energy().
• px, py, pz: total linear momentum in kg m/s.
• Lx, Ly, Lz: total angular momentum about the origin in kg m^2/s.
• energy_error: (energy - E0) / abs(E0), where E0 is the energy at the first time step. Signed and dimensionless.
• momentum_error: the length of the change in the momentum vector since the first step, divided by the sum of every body’s mass * speed at the first step. Never negative, dimensionless.
• angular_momentum_error: the length of the change in the angular momentum vector since the first step, divided by the length of the angular momentum vector at the first step. Never negative, dimensionless. An error column is all NaN when the quantity it is divided by is zero or not finite.

Raises

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

See Also

Notes

In symbols, with a subscript 0 for the first time step:

\[\frac{E - E_0}{|E_0|}, \qquad \frac{|\mathbf{P} - \mathbf{P}_0|}{\sum_i m_i |\mathbf{v}_{i,0}|}, \qquad \frac{|\mathbf{L} - \mathbf{L}_0|}{|\mathbf{L}_0|}\]

for the energy, momentum and angular momentum errors in that order.

The momentum error is scaled by the sum of the individual momenta, not by \(|\mathbf{P}_0|\), because the total momentum is often zero or close to it and would make a poor yardstick.

Run this on the simulation as it came out of simulate_system(). A frame centered on a body is not inertial, and momentum and angular momentum are not conserved in it.

Examples

One year of the Earth’s orbit with the default velocity Verlet method. The error columns are at the right.

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.conserved_quantities(sim).tail()
time kinetic potential energy px py pz Lx Ly Lz energy_error momentum_error angular_momentum_error
361 31190400.0 2.739465e+33 -5.389178e+33 -2.649712e+33 -1.762946e+29 -4.053879e+28 0.0 0.0 0.0 2.661002e+40 7.722291e-09 7.293793e-16 0.000000e+00
362 31276800.0 2.739570e+33 -5.389282e+33 -2.649712e+33 -1.762946e+29 -4.053879e+28 0.0 0.0 0.0 2.661002e+40 4.496462e-09 7.293793e-16 1.817249e-16
363 31363200.0 2.739646e+33 -5.389359e+33 -2.649712e+33 -1.762946e+29 -4.053879e+28 0.0 0.0 0.0 2.661002e+40 2.136954e-09 7.293793e-16 1.817249e-16
364 31449600.0 2.739695e+33 -5.389407e+33 -2.649712e+33 -1.762946e+29 -4.053879e+28 0.0 0.0 0.0 2.661002e+40 6.448303e-10 6.807540e-16 1.817249e-16
365 31536000.0 2.739715e+33 -5.389427e+33 -2.649712e+33 -1.762946e+29 -4.053879e+28 0.0 0.0 0.0 2.661002e+40 2.076659e-11 6.807540e-16 1.817249e-16

Compare the largest energy error of the three integration methods on the same system with the same time step.

import pandas as pd
import pyorbiter as orb

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

rows = []
for method in ["verlet", "euler_cromer", "euler"]:
    sim = system.simulate(time_step=orb.seconds_per_day,
                          duration=orb.seconds_per_year, method=method)
    error = orb.conserved_quantities(sim)["energy_error"].abs().max()
    rows.append({"method": method, "max_energy_error": error})
pd.DataFrame(rows)
method max_energy_error
0 verlet 0.000005
1 euler_cromer 0.000483
2 euler 0.152042