Skip to main content

shift_reference_frame

shift_reference_frame(sim_data, center_id, keep_center=True)

Re-center a simulation on one body, or on the center of mass.

Recalculates the positions and velocities of every body relative to a chosen body, at every time step. This anchors the camera to that body: it sits at the origin with zero velocity for the whole run, and everything else is described as seen from it. Use it to look at a moon from its planet, or to view a system from its barycenter, where the star’s small wobble becomes visible.

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. Positions are in meters, velocities in meters per second, masses in kilograms, time in seconds. required
center_id str The id of the body to use as the new origin, or "barycenter" for the mass-weighted center of the whole system. If a body is actually named "barycenter", that body is used. required
keep_center bool Whether to keep the central body’s rows, which are zero in all six state columns. The default True keeps them. False drops them. It has no effect when shifting to the computed barycenter, which is not a body and has no rows. True

Returns

Name Type Description
DataFrame The same kind of DataFrame it was given (pandas in, pandas out; polars in, polars out), with the same columns. x, y, z (meters) and vx, vy, vz (meters per second) are replaced by their values relative to the new origin. id, mass and time are unchanged. The recorded run settings are carried over.

Raises

Name Type Description
ValueError If center_id is not "barycenter" and no body with that id is in sim_data.

See Also

Notes

The barycenter at each time step is

\[\mathbf{R} = \frac{\sum_i m_i \mathbf{r}_i}{\sum_i m_i}, \qquad \mathbf{V} = \frac{\sum_i m_i \mathbf{v}_i}{\sum_i m_i}\]

summed over the bodies present at that step. With no outside forces the barycenter moves in a straight line at constant speed, so the barycentric frame is inertial and total momentum is zero in it. A frame centered on a body is not inertial, because the body accelerates. Momentum and angular momentum are not conserved in such a frame.

Examples

Shift a Sun, Earth and Jupiter run to its barycenter. The Sun is no longer at the origin: Jupiter holds it several hundred thousand kilometers away.

import pyorbiter as orb

sim = (
    orb.create_system()
    .add_sun()
    .add_planet("Earth", parent="Sun")
    .add_planet("Jupiter", parent="Sun")
    .simulate(time_step=orb.seconds_per_day, duration=orb.seconds_per_year)
)
orb.shift_reference_frame(sim, "barycenter").head()
id mass x y z vx vy vz time
0 Sun 1.989000e+30 -6.836400e+08 -1.751068e+08 1.597801e+07 3.324093 -12.646382 -0.020019 0.0
1 Earth 5.972000e+24 -3.364923e+10 1.431852e+11 1.597801e+07 -29516.862958 -6800.789728 -0.020019 0.0
2 Jupiter 1.898000e+27 7.165231e+11 1.830518e+11 -1.674413e+10 -3390.593344 13274.114285 20.978391 0.0
3 Sun 1.989000e+30 -6.833520e+08 -1.761992e+08 1.597626e+07 3.343056 -12.639879 -0.020470 86400.0
4 Earth 5.972000e+24 -3.619435e+10 1.425753e+11 1.597626e+07 -29393.473069 -7316.261060 -0.020449 86400.0

Center a Sun, Earth and Moon run on Earth, drop Earth’s rows, and plot the Moon’s path around the origin.

import pyorbiter as orb

sim = (
    orb.create_system()
    .add_sun()
    .add_planet("Earth", parent="Sun")
    .add_planet("Moon", parent="Earth")
    .simulate(time_step=orb.seconds_per_hour, duration=28 * orb.seconds_per_day)
)
earth_view = orb.shift_reference_frame(sim, "Earth", keep_center=False)
orb.plot_orbits(earth_view[earth_view["id"] == "Moon"], three_d=False)