Skip to content

Commit

Permalink
Law: semimajor axis of a planet (#866)
Browse files Browse the repository at this point in the history
* Move law of semiminor axis to radial motion folder

* Create law and test for semimajor axis

* Fix description
  • Loading branch information
alesanter authored Jul 23, 2024
1 parent 5fbd61f commit 9408ecb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
r"""
Semimajor axis via Kepler's constant and total energy
=====================================================
The semi-major axis of an orbiting planet depends on the Kepler's constant of
the star---planet system and the total energy of the planet per unit of its mass.
**Notes:**
#. Works for both elliptical (:math:`\varepsilon < 0`) and hyperbolical
(:math:`\varepsilon > 0`) orbits.
"""

from sympy import Eq, pi
from symplyphysics import (
units,
Quantity,
Symbol,
validate_input,
validate_output,
)

semimajor_axis = Symbol("semimajor_axis", units.length)
"""
The semi-major axis of the planet's orbit.
Symbol:
a
"""

kepler_constant = Symbol("kepler_constant", units.length**3 / units.time**2)
r"""
The Kepler's constant, whose value is determined by the mass of the orbited star.
Symbol:
K
Latex:
:math:`\mathfrak{K}`
"""

total_energy_per_unit_mass = Symbol("total_energy_per_unit_mass", units.energy / units.mass)
r"""
The total energy of the planet per unit of its :attr:`~symplyphysics.symbols.basic.mass`.
Can be negative or positive.
Symbol:
epsilon
Latex:
:math:`\varepsilon`
"""

law = Eq(semimajor_axis, 2 * pi**2 * kepler_constant / abs(total_energy_per_unit_mass))
r"""
a = 2 * pi^2 * K / abs(epsilon)
Latex:
.. math::
a = \frac{2 \pi^2 \mathfrak{K}}{|\varepsilon|}
"""


@validate_input(
kepler_constant_=kepler_constant,
total_energy_per_unit_mass_=total_energy_per_unit_mass,
)
@validate_output(semimajor_axis)
def calculate_semimajor_axis(
kepler_constant_: Quantity,
total_energy_per_unit_mass_: Quantity,
) -> Quantity:
result = law.rhs.subs({
kepler_constant: kepler_constant_,
total_energy_per_unit_mass: total_energy_per_unit_mass_,
})
return Quantity(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import namedtuple
from pytest import fixture, raises
from symplyphysics import (
assert_equal,
errors,
units,
Quantity,
)
from symplyphysics.laws.gravity.radial_motion import semimajor_axis_via_kepler_constant_and_total_energy as law

Args = namedtuple("Args", "k e")


@fixture(name="test_args")
def test_args_fixture() -> Args:
k = Quantity(7.5e-6 * units.astronomical_unit**3 / units.day**2)
e = Quantity(-4.4e8 * units.joule / units.kilogram)
return Args(k=k, e=e)


def test_law(test_args: Args) -> None:
result = law.calculate_semimajor_axis(test_args.k, test_args.e)
assert_equal(result, 1.0 * units.astronomical_unit, tolerance=9e-3)


def test_bad_kepler_constant(test_args: Args) -> None:
kb = Quantity(units.coulomb)
with raises(errors.UnitsError):
law.calculate_semimajor_axis(kb, test_args.e)
with raises(TypeError):
law.calculate_semimajor_axis(100, test_args.e)


def test_bad_energy_per_mass(test_args: Args) -> None:
eb = Quantity(units.coulomb)
with raises(errors.UnitsError):
law.calculate_semimajor_axis(test_args.k, eb)
with raises(TypeError):
law.calculate_semimajor_axis(test_args.k, 100)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
units,
Quantity,
)
from symplyphysics.laws.gravity import semiminor_axis_of_elliptical_orbit_via_orbit_parameters as law
from symplyphysics.laws.gravity.radial_motion import semiminor_axis_of_elliptical_orbit_via_orbit_parameters as law

Args = namedtuple("Args", "s a m")

Expand Down

0 comments on commit 9408ecb

Please sign in to comment.