Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt
- name: Run tests
run: |
pytest -vv

16 changes: 8 additions & 8 deletions nbp/bodies/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def from_tuple_parameters(cls, name: str, mass: float, radius: float, position:

>>> planet1 = Body.from_tuple_parameters("Planet1", 100.0, 20.0, (1.0, 2.0, 3.0), (4.0, 5.0, 6.0))
>>> planet1.position
array([[ 1.],
[ 2.],
[ 3.]])
array([[1.],
[2.],
[3.]])
>>> planet1.velocity
array([[ 4.],
[ 5.],
[ 6.]])
array([[4.],
[5.],
[6.]])
>>> planet1.name
'Planet1'
>>> planet1.mass
Expand All @@ -42,11 +42,11 @@ def from_tuple_parameters(cls, name: str, mass: float, radius: float, position:
>>> planet1.position == planet2.position
array([[ True],
[ True],
[ True]], dtype=bool)
[ True]])
>>> planet1.velocity == planet2.velocity
array([[ True],
[ True],
[ True]], dtype=bool)
[ True]])
"""
return Body(
name,
Expand Down
30 changes: 15 additions & 15 deletions nbp/helpers/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
def tuple_to_numpy(t: tuple) -> numpy.ndarray:
"""
>>> tuple_to_numpy((1, 2, 3))
array([[ 1.],
[ 2.],
[ 3.]])
array([[1.],
[2.],
[3.]])
>>> tuple_to_numpy((123.123, 234.234, 345.345))
array([[ 123.123],
[ 234.234],
[ 345.345]])
array([[123.123],
[234.234],
[345.345]])
"""
return numpy.array([[t[0]],
[t[1]],
Expand Down Expand Up @@ -43,19 +43,19 @@ def dict_to_numpy(dictionary: dict) -> numpy.ndarray:
"""
>>> a = dict_to_numpy({'x': 1.0, 'y': 2.0, 'z': 3.0})
>>> a
array([[ 1.],
[ 2.],
[ 3.]])
array([[1.],
[2.],
[3.]])
>>> numpy_to_dict(a) == {'x': 1.0, 'y': 2.0, 'z': 3.0}
True
>>> dict_to_numpy({'x': 1.5, 'y': 2.5, 'z': 3.5})
array([[ 1.5],
[ 2.5],
[ 3.5]])
array([[1.5],
[2.5],
[3.5]])
>>> dict_to_numpy({'x': -100, 'y': 212, 'z': 31234})
array([[ -100.],
[ 212.],
[ 31234.]])
array([[ -100.],
[ 212.],
[31234.]])
"""
return tuple_to_numpy((
dictionary['x'],
Expand Down
64 changes: 32 additions & 32 deletions nbp/helpers/physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ def distance_to(one_body: Body, other_body: Body) -> numpy.ndarray:
>>> earth = Body.from_tuple_parameters("Earth", 5.972*(10**24), 100, (1.506*(10**11), 0, 100), (0, 29290, 0))
>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100, (1.496*(10**11), 384.4*(10**6), -500), (1050, 29290, 0))
>>> distance_to(moon, earth)
array([[ 1.00000000e+09],
[ -3.84400000e+08],
[ 6.00000000e+02]])
array([[ 1.000e+09],
[-3.844e+08],
[ 6.000e+02]])

Distance to itself is always 0 in all directions.

>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100, (1.496*(10**11), 384.4*(10**6), -500), (1050, 29290, 0))
>>> distance_to(moon, moon)
array([[ 0.],
[ 0.],
[ 0.]])
array([[0.],
[0.],
[0.]])

>>> from nbp.helpers.numpy import tuple_to_numpy
>>> import numpy as np
>>> velocity = tuple_to_numpy((0, 0, 0))
>>> one = Body('saturn', 100, 100, np.array([[0.], [0.], [0.]]), velocity)
>>> two = Body('neptune', 100, 100, tuple_to_numpy((0, 146.2, 0)), velocity)
>>> distance_to(one, two)
array([[ 0. ],
[ 146.2],
[ 0. ]])
array([[ 0. ],
[146.2],
[ 0. ]])
"""
return other_body.position - one_body.position

Expand All @@ -49,12 +49,12 @@ def absolute_distance_to_one(one_body: Body, other_body: Body) -> float:
>>> earth = Body.from_tuple_parameters("Earth", 5.972*(10**24), 100.0, (1.496*(10**11), 0, 0), (0, 29290, 0))
>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100.0, (1.496*(10**11), 384.4*(10**6), 0), (1050, 29290, 0))
>>> absolute_distance_to_one(moon, earth)
384400000.0
np.float64(384400000.0)

>>> earth = Body.from_tuple_parameters("Earth", 5.972*(10**24), 100.0, (1.496*(10**11), 0, 0), (0, 29290, 0))
>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100.0, (1.496*(10**11), -384.4*(10**6), -69834), (1050, 29290, 0))
>>> absolute_distance_to_one(moon, earth)
384400006.34337604
np.float64(384400006.34337604)

>>> from nbp.helpers.numpy import tuple_to_numpy
>>> import numpy as np
Expand Down Expand Up @@ -107,9 +107,9 @@ def acceleration_to_all(one_body: Body, bodies: [Body]) -> numpy.ndarray:
>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100.0, (0, 384.4*(10**6), -1000), (0, 0, 0))

>>> acceleration_to_all(moon, [kg, earth1, earth2, moon])
array([[ -4.46878801e-05],
[ -5.48536334e-03],
[ 6.07257588e-08]])
array([[-4.46878801e-05],
[-5.48536334e-03],
[ 6.07257588e-08]])

"""
total_acceleration = numpy.array([[0],
Expand All @@ -130,9 +130,9 @@ def calculate_position(body: Body, delta_time: float) -> numpy.ndarray:

>>> test_body = Body.from_tuple_parameters("Test_body", 1.0, 1.0, (60, -20, 15), (4, 10.2, -6))
>>> calculate_position(test_body, 3.0)
array([[ 72. ],
[ 10.6],
[ -3. ]])
array([[72. ],
[10.6],
[-3. ]])
"""
return body.position + (delta_time * body.velocity)

Expand All @@ -151,17 +151,17 @@ def calculate_velocity(one_body: Body, delta_time: float, other_bodies: [Body])
>>> e5 = Body.from_tuple_parameters("Earth5", (5.972*(10**24)), 100.0, (6371000, 9000, -532), (0, 0, 0))
>>> e6 = Body.from_tuple_parameters("Earth6", (5.972*(10**24)), 100.0, (-6371000, -9000, 532), (0, 0, 0))
>>> calculate_velocity(kg, 314.0, [kg, e1, e2, e3, e4, e5, e6])
array([[ 0.],
[ 0.],
[ 0.]])
array([[0.],
[0.],
[0.]])

>>> kg = Body.from_tuple_parameters("kg", 1.0, 100.0, (0, 0, 0), (0, 0, 0))
>>> earth1 = Body.from_tuple_parameters("Earth1", (5.972*(10**24)), 100.0, (0, 6371000, 6280), (0, 0, 0))
>>> moon = Body.from_tuple_parameters("Moon", 0.0735*(10**24), 100.0, (0, 384.4*(10**6), -1000), (0, 0, 0))
>>> calculate_velocity(kg, 16.0, [kg, earth1, moon])
array([[ 0.00000000e+00],
[ 1.57114698e+02],
[ 1.54870030e-01]])
array([[0.00000000e+00],
[1.57114698e+02],
[1.54870030e-01]])
"""

return one_body.velocity + (delta_time * acceleration_to_all(one_body, other_bodies))
Expand All @@ -179,14 +179,14 @@ def merge_bodies(one_body: Body, other_body: Body) -> Body:
>>> planet = merge_bodies(earth, moon)

>>> planet.position
array([[ 1.50587842e+11],
[ 4.67395352e+06],
[ 9.27053180e+01]])
array([[1.50587842e+11],
[4.67395352e+06],
[9.27053180e+01]])

>>> planet.velocity
array([[ -8.60185262e+01],
[ 2.85777959e+04],
[ -7.59904061e-01]])
array([[-8.60185262e+01],
[ 2.85777959e+04],
[-7.59904061e-01]])

>>> planet.radius
6413824.949215559
Expand Down Expand Up @@ -235,7 +235,7 @@ def minimal_distance(bodies: [Body]) -> float:
>>> saturn = Body.from_tuple_parameters('saturn', 568000000000000000000000000, 100, (0, 1352550000000, 0), (0, 10180, 0))
>>> neptune = Body.from_tuple_parameters('neptune', 102413000000000000000000000, 100, (0, -4444450000000, 500000), (-5370, 0, 0))
>>> minimal_distance([sun, earth, moon, jupiter, saturn, neptune])
405500037.33168757
np.float64(405500037.33168757)

>>> sun = Body.from_tuple_parameters('Sun', 1989000000000000000000000000000, 100, (0, 0, 0), (0, 0, 0))
>>> earth = Body.from_tuple_parameters('earth', 5972000000000000000000000, 100, (0, 152100000000, 1000), (29290, 0, 32))
Expand All @@ -244,15 +244,15 @@ def minimal_distance(bodies: [Body]) -> float:
>>> saturn = Body.from_tuple_parameters('saturn', 568000000000000000000000000, 100, (0, 1352550000000, 0), (0, 10180, 0))
>>> neptune = Body.from_tuple_parameters('neptune', 102413000000000000000000000, 100, (0, -4444450000000, 500000), (-5370, 0, 0))
>>> minimal_distance([sun, earth, moon, jupiter, saturn, neptune])
405500037.76202041
np.float64(405500037.7620204)

>>> sun = Body.from_tuple_parameters('Sun', 1989000000000000000000000000000, 100, (0, 0, 0), (0, 0, 0))
>>> earth = Body.from_tuple_parameters('earth', 5972000000000000000000000, 100, (107550941418, 107550941418, 100000), (29290, 0, 32))
>>> jupiter = Body.from_tuple_parameters('jupiter', 1900000000000000000000000000, 100, (816620000000, 0, -1000), (40, 12440, 1))
>>> saturn = Body.from_tuple_parameters('saturn', 568000000000000000000000000, 100, (0, 1352550000000, 0), (0, 10180, 0))
>>> neptune = Body.from_tuple_parameters('neptune', 102413000000000000000000000, 100, (0, -4444450000000, 500000), (-5370, 0, 0))
>>> minimal_distance([sun, earth, jupiter, saturn, neptune])
152099999999.3627
np.float64(152099999999.3627)

>>> from nbp.helpers.numpy import tuple_to_numpy
>>> import numpy as np
Expand Down
10 changes: 5 additions & 5 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pylint==1.5.5
pytest==2.9.2
codecov==2.0.5
coverage==4.1
pytest-cov==2.2.1
pylint
pytest
codecov
coverage
pytest-cov
13 changes: 6 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
gevent==1.1.2
bottle==0.12.9
websockets==3.2
bottle==0.12.9
numpy==1.11.0
cerberus==0.9.2
pytest==2.9.2
gevent
bottle
websockets
numpy
cerberus
pytest