Skip to content

Commit

Permalink
feat: return conditional solution from segments/sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwa2710 committed Oct 6, 2023
1 parent 4465595 commit a8933c9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Segment(pybind11::module&
.def_readonly("name", &Segment::Solution::name)
.def_readonly("dynamics", &Segment::Solution::dynamics)
.def_readonly("states", &Segment::Solution::states)
.def_readonly("condition_is_satisfied", &Segment::Solution::conditionIsSatisfied)

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Sequence(pybind11::module
class_<Sequence::Solution>(aModule, "SequenceSolution")

.def_readonly("segment_solutions", &Sequence::Solution::segmentSolutions)
.def_readonly("sequence_is_complete", &Sequence::Solution::sequenceIsComplete)

.def("get_states", &Sequence::Solution::getStates)

;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,17 @@ class Propagator

/// @brief Calculate the state subject to an Event Condition, given initial state and maximum end time
/// @code
/// State state = propagator.calculateStateAt(aState, anInstant, anEventCondition);
/// NumericalSolver::ConditionSolution state = propagator.calculateStateAt(aState, anInstant,
/// anEventCondition);
/// @endcode
/// @param [in] aState An initial state
/// @param [in] anInstant An instant
/// @param [in] anEventCondition An event condition
/// @return State
/// @return NumericalSolver::ConditionSolution

State calculateStateAt(const State& aState, const Instant& anInstant, const EventCondition& anEventCondition) const;
NumericalSolver::ConditionSolution calculateStateAt(
const State& aState, const Instant& anInstant, const EventCondition& anEventCondition
) const;

/// @brief Calculate the states at an array of instants, given an initial state
/// @brief Can only be used with sorted instants array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ class Segment
struct Solution
{
public:
Solution(const String& aName, const Array<Shared<Dynamics>>& aDynamicsArray, const Array<State>& aStates);
Solution(
const String& aName,
const Array<Shared<Dynamics>>& aDynamicsArray,
const Array<State>& aStates,
const bool& aConditionIsSatisfied
);

String name; /// Name of the segment.
Array<Shared<Dynamics>> dynamics; /// List of dynamics used.
Array<State> states; /// Array of states for the segment.
bool conditionIsSatisfied; /// True if the event condition is satisfied.
};

/// @brief Output stream operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Sequence
struct Solution
{
Array<Segment::Solution> segmentSolutions;
bool sequenceIsComplete;

Array<State> getStates() const;
};
Expand Down
9 changes: 2 additions & 7 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/Propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ State Propagator::calculateStateAt(const State& aState, const Instant& anInstant
);
}

State Propagator::calculateStateAt(
NumericalSolver::ConditionSolution Propagator::calculateStateAt(
const State& aState, const Instant& anInstant, const EventCondition& anEventCondition
) const
{
Expand All @@ -182,12 +182,7 @@ State Propagator::calculateStateAt(
anEventCondition
);

if (!conditionSolution.conditionIsSatisfied)
{
throw ostk::core::error::RuntimeError("Condition not satisfied.");
}

return conditionSolution.state;
return conditionSolution;
}

Array<State> Propagator::calculateStatesAt(const State& aState, const Array<Instant>& anInstantArray) const
Expand Down
12 changes: 8 additions & 4 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/Segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ using ostk::physics::time::Duration;
using ostk::astro::trajectory::Propagator;

Segment::Solution::Solution(
const String& aName, const Array<Shared<Dynamics>>& aDynamicsArray, const Array<State>& aStates
const String& aName,
const Array<Shared<Dynamics>>& aDynamicsArray,
const Array<State>& aStates,
const bool& aConditionIsSatisfied
)
: name(aName),
dynamics(aDynamicsArray),
states(aStates)
states(aStates),
conditionIsSatisfied(aConditionIsSatisfied)
{
}

Expand Down Expand Up @@ -108,14 +112,14 @@ Segment::Solution Segment::solve(const State& aState, const Duration& maximumPro

const Instant startInstant = aState.getInstant();

// TBI: Handle the case where the condition is not met
const State finalState =
const NumericalSolver::ConditionSolution conditionSolution =
propagator.calculateStateAt(aState, startInstant + maximumPropagationDuration, *eventCondition_);

return {
name_,
dynamics_,
propagator.accessNumericalSolver().accessObservedStates(),
conditionSolution.conditionIsSatisfied,
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/OpenSpaceToolkit/Astrodynamics/Trajectory/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,16 @@ Sequence::Solution Sequence::solve(const State& aState) const

segmentSolutions.add(segmentSolution);

if (!segmentSolution.conditionIsSatisfied)
{
return {segmentSolutions, false};
}

initialState = segmentSolution.states.accessLast();
}
}

return {segmentSolutions};
return {segmentSolutions, true};
}

void Sequence::print(std::ostream& anOutputStream, bool displayDecorator) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,12 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Propagator, Calcul

const Propagator propagator = {defaultRKD5_, defaultDynamics_};

const State endState = propagator.calculateStateAt(state, endInstant, condition);
const NumericalSolver::ConditionSolution conditionSolution =
propagator.calculateStateAt(state, endInstant, condition);

const State endState = conditionSolution.state;

EXPECT_TRUE(conditionSolution.conditionIsSatisfied);
EXPECT_TRUE(endState.accessInstant() < endInstant);
EXPECT_LT((endState.accessInstant() - condition.getInstant()).inSeconds(), 1e-7);

Expand All @@ -432,7 +436,7 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Trajectory_Orbit_Models_Propagator, Calcul
state.accessInstant() + Duration::Seconds(7000.0),
};

EXPECT_ANY_THROW(propagator.calculateStateAt(state, endInstant, failureCondition));
EXPECT_FALSE(propagator.calculateStateAt(state, endInstant, failureCondition).conditionIsSatisfied);
}
}

Expand Down

0 comments on commit a8933c9

Please sign in to comment.