Skip to main content

get_orbital_elements

get_orbital_elements(sim_data, body, parent, mu=None, G=None)

Osculating orbital elements of one body about another at every time step.

Converts the position and velocity of body relative to parent into the six classical Keplerian elements, once per time step. These are the osculating elements: the two-body orbit the body would follow from that instant if every other body vanished. In a pure two-body run they stay nearly constant, apart from the true anomaly. In a many-body run they drift, and the drift shows how the other bodies perturb the orbit.

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. It does not need to be shifted first: the relative position and velocity are computed here. required
body str The id of the orbiting body. required
parent str The id of the body it orbits. required
mu float The gravitational parameter of the orbit in m3/s2. The default None uses G * M_parent, with the parent’s mass taken from its first row. Pass G * (M_parent + m_body) for the exact two-body value when the body’s mass is not negligible. None
G float The gravitational constant in m^3 kg^-1 s^-2, used only to build the default mu. The default None uses the value recorded in the run (see run_settings()), or gravitational_constant 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 at which both bodies are present, sorted by time, and these columns:
• time: time in seconds.
• a: semi-major axis in meters, from the vis-viva equation. Negative for a hyperbolic orbit.
• e: eccentricity, dimensionless. 0 is a circle, between 0 and 1 an ellipse, 1 or more an unbound orbit.
• i: inclination to the x-y plane in degrees, from 0 to 180. Values above 90 are retrograde orbits.
• lan: longitude of the ascending node in degrees, measured in the x-y plane from the +x axis.
• arg_pe: argument of periapsis in degrees, measured in the orbit plane from the ascending node to periapsis, in the direction of motion.
• nu: true anomaly in degrees, measured in the orbit plane from periapsis to the body, in the direction of motion.
• period: orbital period in seconds, \(2\pi\sqrt{a^3/\mu}\). NaN for an unbound orbit. lan, arg_pe and nu are wrapped into the range 0 to 360. An angle a rounding error below zero wraps to just under 360, and can print as 360. The result does not carry the run settings of sim_data.

Raises

Name Type Description
TypeError If sim_data is not a pandas or polars DataFrame with the columns simulate_system() produces.
ValueError If body or parent is not an id in sim_data, or if they are the same body.

See Also

Notes

Some elements are undefined for special orbits. These conventions keep every angle defined:

  • Equatorial orbit (the orbit lies in the x-y plane, so there is no ascending node): the node is taken to be the +x axis. lan is 0 and arg_pe is the angle from +x to periapsis.
  • Circular orbit (e below 1e-12, so there is no periapsis): arg_pe is 0 and nu is measured from the ascending node. It is then the argument of latitude.
  • Circular and equatorial: lan and arg_pe are both 0 and nu is the angle from the +x axis.

An orbit that is close to one of these cases, but not exactly in it, has a poorly determined lan or arg_pe. Small perturbations can then swing those angles by large amounts while the orbit itself barely changes.

The elements come from the specific angular momentum \(\mathbf{h} = \mathbf{r} \times \mathbf{v}\), the eccentricity vector \(\mathbf{e} = (\mathbf{v} \times \mathbf{h})/\mu - \mathbf{r}/r\), and the vis-viva equation \(1/a = 2/r - v^2/\mu\), where \(\mathbf{r}\) and \(\mathbf{v}\) are the position and velocity of body relative to parent.

Examples

Earth’s elements about the Sun over one year. With only two bodies they hold nearly steady while the true anomaly nu advances. This orbit lies in the x-y plane, so i and lan are 0 by the equatorial convention in the Notes.

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_orbital_elements(sim, "Earth", "Sun").head()
time a e i lan arg_pe nu period
0 0.0 1.496000e+11 0.0167 0.0 0.0 102.950000 360.000000 3.155419e+07
1 86400.0 1.496000e+11 0.0167 0.0 0.0 102.945353 1.024056 3.155419e+07
2 172800.0 1.496000e+11 0.0167 0.0 0.0 102.940707 2.048100 3.155419e+07
3 259200.0 1.496000e+11 0.0167 0.0 0.0 102.936065 3.072118 3.155419e+07
4 345600.0 1.496000e+11 0.0167 0.0 0.0 102.931429 4.096100 3.155419e+07

Place a probe from its elements, simulate, and recover those elements from the positions and velocities. They match to within the integration error of a one-day time step.

import pyorbiter as orb

sim = (
    orb.create_system()
    .add_sun()
    .add_body_keplerian("Probe", mass=1000, a=2e11, e=0.3, i=20, lan=40,
                        arg_pe=60, nu=0, parent="Sun")
    .simulate(time_step=orb.seconds_per_day, duration=100 * orb.seconds_per_day)
)
orb.get_orbital_elements(sim, "Probe", "Sun").iloc[::25].round(4)
time a e i lan arg_pe nu period
0 0.0 2.000000e+11 0.3000 20.0 40.0 60.0000 0.0000 4.877582e+07
25 2160000.0 2.000028e+11 0.3000 20.0 40.0 59.9897 30.3823 4.877686e+07
50 4320000.0 2.000077e+11 0.3001 20.0 40.0 59.9860 57.4178 4.877864e+07
75 6480000.0 2.000109e+11 0.3001 20.0 40.0 59.9867 79.9266 4.877981e+07
100 8640000.0 2.000124e+11 0.3001 20.0 40.0 59.9881 98.4077 4.878036e+07