Skip to content

Commit

Permalink
fix: adapt python bindings for tabulated orbit model (#102)
Browse files Browse the repository at this point in the history
* fix: adapt python bindings for tabulated

* feat: add some more tests

* chore: clean up test

* feat: missed a spot

---------

Co-authored-by: Remy Derollez <remy@loftorbital.com>
Co-authored-by: vishwa shah <vishwa@loftorbital.com>
  • Loading branch information
3 people authored May 15, 2023
1 parent aa6f077 commit cfe9de1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/// Apache License 2.0
/// Apache License 2.0

#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Model.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Kepler.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Propagated.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Tabulated.hpp>

#include <OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit/Messages.cpp>
#include <OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit/Model.cpp>
Expand All @@ -24,6 +25,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Orbit(pybind11::module& a
using ostk::astro::trajectory::State;
using ostk::astro::trajectory::orbit::models::Kepler;
using ostk::astro::trajectory::orbit::models::Propagated;
using ostk::astro::trajectory::orbit::models::Tabulated;
using ostk::astro::trajectory::orbit::models::SGP4;

{
Expand Down Expand Up @@ -77,6 +79,14 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Orbit(pybind11::module& a
},
return_value_policy::reference
) // [TBR]
.def(
"access_tabulated_model",
+[](const Orbit& anOrbit) -> const Tabulated&
{
return anOrbit.accessModel().as<Tabulated>();
},
return_value_policy::reference
) // [TBR]

.def("get_revolution_number_at", &Orbit::getRevolutionNumberAt, arg("instant"))
.def("get_pass_at", &Orbit::getPassAt, arg("instant"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Apache License 2.0
/// Apache License 2.0

#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Tabulated.hpp>

Expand All @@ -12,7 +12,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Orbit_Models_Tabulated(py
using ostk::astro::trajectory::State;
using ostk::astro::trajectory::orbit::models::Tabulated;

class_<Tabulated> tabulated_class(aModule, "Tabulated");
class_<Tabulated, ostk::astro::trajectory::orbit::Model> tabulated_class(aModule, "Tabulated");

enum_<Tabulated::InterpolationType>(tabulated_class, "InterpolationType")

Expand Down Expand Up @@ -41,6 +41,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Orbit_Models_Tabulated(py
.def("get_epoch", &Tabulated::getEpoch)
.def("get_revolution_number_at_epoch", &Tabulated::getRevolutionNumberAtEpoch)
.def("get_interval", &Tabulated::getInterval)
.def("get_interpolation_type", &Tabulated::getInterpolationType)
.def("calculate_state_at", &Tabulated::calculateStateAt, arg("instant"))
.def("calculate_states_at", &Tabulated::calculateStatesAt, arg("instants"))
.def("calculate_revolution_number_at", &Tabulated::calculateRevolutionNumberAt, arg("instant"))
Expand Down
36 changes: 35 additions & 1 deletion bindings/python/test/trajectory/orbit/models/test_tabulated.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Apache License 2.0
# Apache License 2.0

import pytest

Expand All @@ -11,11 +11,18 @@
from ostk.physics.coordinate import Position
from ostk.physics.coordinate import Velocity
from ostk.physics.coordinate import Frame
from ostk.physics import Environment

from ostk.astrodynamics.trajectory import State
from ostk.astrodynamics.trajectory import Orbit
from ostk.astrodynamics.trajectory.orbit.models import Tabulated


@pytest.fixture
def earth():
return Environment.default().access_celestial_object_with_name("Earth")


@pytest.fixture
def reference_states() -> list[State]:
data = [
Expand Down Expand Up @@ -240,6 +247,33 @@ def test_constructor(
is not None
)

def test_constructor_orbit_tabulated_sucess(
self,
test_states: list[State],
earth,
):
tabulated = Tabulated(
states=test_states,
initial_revolution_number=1,
interpolation_type=Tabulated.InterpolationType.CubicSpline,
)

orbit: Orbit = Orbit(tabulated, earth)

assert orbit is not None
assert isinstance(orbit, Orbit)

def test_get_interpolation_type(self, test_states: list[State]):
tabulated = Tabulated(
states=test_states,
initial_revolution_number=1,
interpolation_type=Tabulated.InterpolationType.CubicSpline,
)

assert (
tabulated.get_interpolation_type() == Tabulated.InterpolationType.CubicSpline
)

@pytest.mark.parametrize(
"interpolation_type,error_tolerance",
(
Expand Down
132 changes: 56 additions & 76 deletions bindings/python/test/trajectory/test_orbit.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,79 @@
# Apache License 2.0
# Apache License 2.0

import pytest

import numpy
from ostk.physics import Environment
from ostk.physics.units import Length, Angle
from ostk.physics.time import Scale, Instant, DateTime

import ostk.physics as physics
from ostk.astrodynamics.trajectory import Orbit, State
from ostk.astrodynamics.trajectory.orbit.models import SGP4
from ostk.astrodynamics.trajectory.orbit.models.sgp4 import TLE

import ostk.astrodynamics as astrodynamics

Length = physics.units.Length
Angle = physics.units.Angle
Scale = physics.time.Scale
Instant = physics.time.Instant
Interval = physics.time.Interval
DateTime = physics.time.DateTime
Position = physics.coordinate.Position
Velocity = physics.coordinate.Velocity
Frame = physics.coordinate.Frame
Environment = physics.Environment
@pytest.fixture
def earth():
return Environment.default().access_celestial_object_with_name("Earth")

Trajectory = astrodynamics.Trajectory
Model = astrodynamics.trajectory.Model
Orbit = astrodynamics.trajectory.Orbit
Pass = astrodynamics.trajectory.orbit.Pass
Kepler = astrodynamics.trajectory.orbit.models.Kepler
COE = astrodynamics.trajectory.orbit.models.kepler.COE
SGP4 = astrodynamics.trajectory.orbit.models.SGP4
TLE = astrodynamics.trajectory.orbit.models.sgp4.TLE
State = astrodynamics.trajectory.State
Access = astrodynamics.Access

earth = Environment.default().access_celestial_object_with_name("Earth")
class TestOrbit:
def test_trajectory_orbit_constructors(self, earth):
# Construct Two-Line Element set
tle = TLE(
"1 25544U 98067A 18231.17878740 .00000187 00000-0 10196-4 0 9994",
"2 25544 51.6447 64.7824 0005971 73.1467 36.4366 15.53848234128316",
)

# Construct orbit using SGP4 model
orbit = Orbit(SGP4(tle), earth)

def test_trajectory_orbit_constructors():
# Construct Two-Line Element set
tle = TLE(
"1 25544U 98067A 18231.17878740 .00000187 00000-0 10196-4 0 9994",
"2 25544 51.6447 64.7824 0005971 73.1467 36.4366 15.53848234128316",
)
assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()

# Construct orbit using SGP4 model
orbit = Orbit(SGP4(tle), earth)
# Construct get state at current epoch
state: State = orbit.get_state_at(Instant.now())

assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()
assert state is not None
assert isinstance(state, State)

# Construct get state at current epoch
state: State = orbit.get_state_at(Instant.now())
def test_trajectory_orbit_circular(self, earth):
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)
inclination = Angle.degrees(45.0)

assert state is not None
assert isinstance(state, State)
orbit: Orbit = Orbit.circular(epoch, altitude, inclination, earth)

@pytest.mark.skip
def test_trajectory_orbit_equatorial(self, earth):
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)
eccentricity = 0.1

def test_trajectory_orbit_circular():
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)
inclination = Angle.degrees(45.0)
orbit: Orbit = Orbit.equatorial(epoch, altitude, eccentricity, earth)

orbit: Orbit = Orbit.circular(epoch, altitude, inclination, earth)
assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()

@pytest.mark.skip
def test_trajectory_orbit_circular_equatorial(self, earth):
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)

@pytest.mark.skip
def test_trajectory_orbit_equatorial():
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)
eccentricity = 0.1
orbit: Orbit = Orbit.circular_equatorial(epoch, altitude, earth)

orbit: Orbit = Orbit.equatorial(epoch, altitude, eccentricity, earth)
assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()

assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()
@pytest.mark.skip
def test_trajectory_orbit_sun_synchronous(self, earth):
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)

orbit: Orbit = Orbit.sun_synchronous(epoch, altitude, earth)

@pytest.mark.skip
def test_trajectory_orbit_circular_equatorial():
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)

orbit: Orbit = Orbit.circular_equatorial(epoch, altitude, earth)

assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()


@pytest.mark.skip
def test_trajectory_orbit_sun_synchronous():
epoch = Instant.date_time(DateTime(2018, 1, 1, 0, 0, 0), Scale.UTC)
altitude = Length.kilometers(500.0)

orbit: Orbit = Orbit.sun_synchronous(epoch, altitude, earth)

assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()
assert orbit is not None
assert isinstance(orbit, Orbit)
assert orbit.is_defined()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Apache License 2.0
/// Apache License 2.0

#ifndef __OpenSpaceToolkit_Astrodynamics_Trajectory_Models_Tabulated__
#define __OpenSpaceToolkit_Astrodynamics_Trajectory_Models_Tabulated__
Expand Down Expand Up @@ -50,8 +50,6 @@ using ostk::astro::trajectory::State;

#define DEFAULT_TABULATED_INTERPOLATION_TYPE Tabulated::InterpolationType::Linear

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// @brief Tabulated trajectory model
///
/// Interpolation is performed between states using the specified interpolation scheme.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// @project Open Space Toolkit ▸ Astrodynamics
/// @file OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Tabulated.test.cpp
/// @author Vishwa Shah <vishwa@loftorbital.com>
/// @license Apache License 2.0

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Apache License 2.0

#include <OpenSpaceToolkit/Core/Containers/Array.hpp>
#include <OpenSpaceToolkit/Core/Containers/Table.hpp>

#include <OpenSpaceToolkit/Physics/Coordinate/Frame.hpp>
#include <OpenSpaceToolkit/Physics/Environment.hpp>

#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Models/Tabulated.hpp>

#include <Global.test.hpp>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using ostk::core::types::Integer;
using ostk::core::types::Size;
using ostk::core::types::Index;
Expand All @@ -31,6 +24,7 @@ using ostk::core::fs::File;

using ostk::math::obj::VectorXd;

using ostk::physics::Environment;
using ostk::physics::time::Instant;
using ostk::physics::time::DateTime;
using ostk::physics::time::Duration;
Expand All @@ -40,11 +34,10 @@ using ostk::physics::coord::Velocity;
using ostk::physics::coord::Frame;

using ostk::astro::trajectory::State;
using ostk::astro::trajectory::Orbit;
using ostk::astro::trajectory::orbit::Model;
using ostk::astro::trajectory::orbit::models::Tabulated;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Tabulated : public ::testing::Test
{
protected:
Expand Down Expand Up @@ -94,6 +87,10 @@ class OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Tabulated : public
TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Tabulated, Constructor)
{
const Tabulated tabulated(states_, 0, Tabulated::InterpolationType::Linear);

Environment environment = Environment::Default();

const Orbit orbit = {tabulated, environment.accessCelestialObjectWithName("Earth")};
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Tabulated, CalculateStateAt)
Expand Down Expand Up @@ -199,5 +196,3 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Tabulated, NotEqua

EXPECT_TRUE(tabulated != anotherTabulated);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 comments on commit cfe9de1

Please sign in to comment.