-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create law and test * Fix documentation
- Loading branch information
Showing
4 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Gravity | ||
======= | ||
*Gravity* is a fundamental interaction which causes mutual attraction between all things that | ||
have mass. | ||
""" |
54 changes: 54 additions & 0 deletions
54
symplyphysics/laws/gravity/keplers_constant_via_attracting_body_mass.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Kepler's constant via attracting body mass | ||
========================================== | ||
Kepler's constant is a physical quantity that only depends on the gravitational constant and the mass of | ||
the orbited body, such as the Sun. It is constant in the sense that all planets that orbit the same body | ||
approximately have the same value of the Kepler's constant. | ||
""" | ||
|
||
from sympy import Eq, pi | ||
from sympy.physics.units import gravitational_constant | ||
from symplyphysics import ( | ||
units, | ||
Quantity, | ||
Symbol, | ||
validate_input, | ||
validate_output, | ||
symbols, | ||
clone_symbol, | ||
) | ||
|
||
keplers_constant = Symbol("keplers_constant", units.length**3 / units.time**2) | ||
r""" | ||
Symbol: | ||
K | ||
Latex: | ||
:math:`\mathfrak{K}` | ||
""" | ||
|
||
attracting_body_mass = clone_symbol(symbols.basic.mass, "attracting_body_mass") | ||
""" | ||
The :attr:`~symplyphysics.symbols.basic.mass` of the attracting body. | ||
Symbol: | ||
M | ||
""" | ||
|
||
law = Eq(keplers_constant, gravitational_constant * attracting_body_mass / (4 * pi**2)) | ||
r""" | ||
K = G * M / (4 * pi^2) | ||
Latex: | ||
:math:`\mathfrak{K} = \frac{G M}{4 \pi^2}` | ||
""" | ||
|
||
|
||
@validate_input(attracting_body_mass_=attracting_body_mass) | ||
@validate_output(keplers_constant) | ||
def calculate_keplers_constant(attracting_body_mass_: Quantity) -> Quantity: | ||
result = law.rhs.subs({ | ||
attracting_body_mass: attracting_body_mass_, | ||
}) | ||
return Quantity(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/gravity/keplers_constant_via_attracting_body_mass_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from collections import namedtuple | ||
from pytest import fixture, raises | ||
from symplyphysics import ( | ||
assert_equal, | ||
Quantity, | ||
errors, | ||
units, | ||
) | ||
from symplyphysics.laws.gravity import keplers_constant_via_attracting_body_mass as law | ||
from symplyphysics.quantities import solar_mass | ||
|
||
Args = namedtuple("Args", "m") | ||
|
||
|
||
@fixture(name="test_args") | ||
def test_args_fixture() -> Args: | ||
return Args(m=solar_mass) | ||
|
||
|
||
def test_law(test_args: Args) -> None: | ||
result = law.calculate_keplers_constant(test_args.m) | ||
assert_equal(result, 7.5e-6 * units.astronomical_unit**3 / units.day**2) | ||
|
||
|
||
def test_bad_mass() -> None: | ||
mb = Quantity(units.coulomb) | ||
with raises(errors.UnitsError): | ||
law.calculate_keplers_constant(mb) | ||
with raises(TypeError): | ||
law.calculate_keplers_constant(100) |