Skip to content
This repository was archived by the owner on Oct 14, 2023. It is now read-only.

Commit 4871992

Browse files
committed
Added propagation code, removed twobody package
Changed the way quantities are handled in assertions, see astropy/astropy#1930
1 parent 874d35e commit 4871992

4 files changed

Lines changed: 79 additions & 16 deletions

File tree

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from astropy import time
88

99
from poliastro.bodies import Sun, Earth
10-
from poliastro.twobody.core import State
10+
from poliastro.twobody import State
1111

1212

1313
def test_state_raises_valueerror_if_meaningless_state():
@@ -45,7 +45,7 @@ def test_state_has_elements_given_in_constructor():
4545
argp = 286.537 * u.deg
4646
nu = 23.33 * u.deg
4747
ss = State.from_elements(Sun, (a, ecc, inc, raan, argp, nu))
48-
assert ss.elements() == (a, ecc, inc, raan, argp, nu)
48+
assert ss.elements == (a, ecc, inc, raan, argp, nu)
4949

5050

5151
def test_state_raises_unitserror_if_elements_units_are_wrong():
@@ -85,23 +85,37 @@ def test_convert_from_rv_to_coe():
8585
raan = 227.89 * u.deg
8686
argp = 53.38 * u.deg
8787
nu = 92.335 * u.deg
88-
expected_r = [6525.344, 6861.535, 6449.125] * u.km
89-
expected_v = [4.902276, 5.533124, -1.975709] * u.km / u.s
88+
expected_r = [6525.344, 6861.535, 6449.125] # km
89+
expected_v = [4.902276, 5.533124, -1.975709] # km / s
9090
r, v = State.from_elements(Earth, (a, ecc, inc, raan, argp, nu)).rv()
91-
assert_array_almost_equal(r, expected_r, decimal=1)
92-
assert_array_almost_equal(v, expected_v, decimal=3)
91+
assert_array_almost_equal(r.value, expected_r, decimal=1)
92+
assert_array_almost_equal(v.value, expected_v, decimal=3)
9393

9494

9595
def test_convert_from_coe_to_rv():
9696
# Data from Vallado, example 2.5
9797
attractor = Earth
9898
r = [6524.384, 6862.875, 6448.296] * u.km
9999
v = [4.901327, 5.533756, -1.976341] * u.km / u.s
100-
a, ecc, inc, raan, argp, nu = State.from_vectors(Earth, r, v).elements()
100+
a, ecc, inc, raan, argp, nu = State.from_vectors(Earth, r, v).elements
101101
p = a * (1 - ecc ** 2)
102-
assert_almost_equal(p, 11067.79 * u.km, decimal=-1)
103-
assert_almost_equal(ecc, 0.832853, decimal=3)
104-
assert_almost_equal(inc, 87.870 * u.deg, decimal=2)
105-
assert_almost_equal(raan, 227.89 * u.deg, decimal=2)
106-
assert_almost_equal(argp, 53.38 * u.deg, decimal=2)
107-
assert_almost_equal(nu, 92.335 * u.deg, decimal=1)
102+
assert_almost_equal(p.value, 11067.79, decimal=-1)
103+
assert_almost_equal(ecc.value, 0.832853, decimal=3)
104+
assert_almost_equal(inc.value, 87.870, decimal=2)
105+
assert_almost_equal(raan.value, 227.89, decimal=1)
106+
assert_almost_equal(argp.value, 53.38, decimal=1)
107+
assert_almost_equal(nu.value, 92.335, decimal=1)
108+
109+
110+
def test_propagate():
111+
# Data from Vallado, example 2.4
112+
r0 = [1131.340, -2282.343, 6672.423] * u.km
113+
v0 = [-5.64305, 4.30333, 2.42879] * u.km / u.s
114+
ss0 = State.from_vectors(Earth, r0, v0)
115+
tof = 40 * u.min
116+
ss1 = ss0.propagate(tof)
117+
r, v = ss1.r, ss1.v
118+
assert_array_almost_equal(r.value, [-4219.7527, 4363.0292, -3958.7666],
119+
decimal=-1)
120+
assert_array_almost_equal(v.value, [3.689866, -1.916735, -6.112511],
121+
decimal=2)
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
from poliastro.util import transform, check_units
1414

15+
from . import _ast2body
16+
1517
J2000 = time.Time("J2000", scale='utc')
1618

1719

1820
class State(object):
19-
"""Class to represent a position of a body wrt to an attractor.
21+
"""Class to represent the position of a body wrt to an attractor.
2022
2123
"""
2224
def __init__(self, attractor, r, v, epoch):
@@ -80,6 +82,7 @@ def from_elements(cls, attractor, elements, epoch=J2000):
8082
ss._elements = elements
8183
return ss
8284

85+
@property
8386
def elements(self):
8487
"""Classical orbital elements.
8588
@@ -96,11 +99,23 @@ def rv(self):
9699
"""
97100
return self.r, self.v
98101

102+
def propagate(self, time_of_flight):
103+
"""Propagate this `State` some `time` and return the result.
104+
105+
"""
106+
r, v = kepler(self.attractor.k.to(u.km ** 3 / u.s ** 2).value,
107+
self.r.to(u.km).value, self.v.to(u.km / u.s).value,
108+
time_of_flight.to(u.s).value)
109+
return self.from_vectors(self.attractor, r * u.km, v * u.km / u.s,
110+
self.epoch + time_of_flight)
111+
99112

100113
def coe2rv(attractor, elements):
101114
"""Converts from orbital elements to vectors.
102115
103116
"""
117+
# TODO: Invert this logic, the low-level function should not
118+
# depend on `State` but the contrary
104119
ss = State.from_elements(attractor, elements)
105120
return ss.r, ss.v
106121

@@ -117,7 +132,7 @@ def rv2coe(attractor, r, v):
117132
n = np.cross([0, 0, 1], h) / norm(h)
118133
e = ((((v.dot(v) - k / (norm(r) * r.unit)) * r - r.dot(v) * v) / k)
119134
.decompose().value)
120-
ecc = norm(e)
135+
ecc = norm(e) * u.one
121136
p = h.dot(h) / k
122137
# TODO: Cannot define a parabola with its semi-major axis
123138
a = p / (1 - ecc ** 2)
@@ -131,3 +146,37 @@ def rv2coe(attractor, r, v):
131146
% (2 * np.pi) * u.rad).to(u.deg)
132147

133148
return a, ecc, inc, raan, argp, nu
149+
150+
151+
def kepler(k, r0, v0, tof):
152+
"""Propagates orbit.
153+
154+
This is a wrapper around kepler from ast2body.for.
155+
156+
Parameters
157+
----------
158+
k : float
159+
Gravitational constant of main attractor (km^3 / s^2).
160+
r0 : array
161+
Initial position (km).
162+
v0 : array
163+
Initial velocity (km).
164+
tof : float
165+
Time of flight (s).
166+
167+
Raises
168+
------
169+
RuntimeError
170+
If the status of the subroutine is not 'ok'.
171+
172+
"""
173+
r0 = np.asarray(r0).astype(np.float)
174+
v0 = np.asarray(v0).astype(np.float)
175+
tof = float(tof)
176+
assert r0.shape == (3,)
177+
assert v0.shape == (3,)
178+
r, v, error = _ast2body.kepler(r0, v0, tof, k)
179+
error = error.strip().decode('ascii')
180+
if error != 'ok':
181+
raise RuntimeError("There was an error: {}".format(error))
182+
return r, v

‎poliastro/twobody/__init__.py‎

Whitespace-only changes.

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def configuration(parent_package='', top_path=None):
3838
"astrodynamics", "orbits", "kepler", "orbital mechanics"
3939
],
4040
requires=["numpy", "astropy", "pytest"],
41-
packages=['poliastro', 'poliastro.twobody'],
41+
packages=['poliastro'],
4242
classifiers=[
4343
"Development Status :: 2 - Pre-Alpha",
4444
"Intended Audience :: Education",

0 commit comments

Comments
 (0)