Skip to content

Commit

Permalink
feat: make the propagator maintain the frame and size of the input St…
Browse files Browse the repository at this point in the history
…ate (#246)

* feat: make the propagator maintain the frame and size of the input State in the output

* chore: uncomment other tests

* Apply suggestions from code review

Co-authored-by: Vishwa Shah <vishwa2710@gmail.com>

* refactor: make propagator StateBuilder a local variable and fix syntax errors from uncommenting code

* fix: remove hard-coded gcrf usage in Propagated model

---------

Co-authored-by: Vishwa Shah <vishwa2710@gmail.com>
  • Loading branch information
kyle-cochran and vishwa2710 authored Oct 17, 2023
1 parent 8dc35fa commit 7c1420a
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit/Model.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/State/NumericalSolver.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/StateBuilder.hpp>

namespace ostk
{
Expand All @@ -55,6 +56,7 @@ using ostk::physics::time::Instant;
using ostk::astro::trajectory::state::NumericalSolver;
using ostk::astro::EventCondition;
using ostk::astro::trajectory::State;
using ostk::astro::trajectory::StateBuilder;
using ostk::astro::Dynamics;
using ostk::astro::flight::system::SatelliteSystem;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ using ostk::physics::units::Time;

static const Derived::Unit GravitationalParameterSIUnit =
Derived::Unit::GravitationalParameter(Length::Unit::Meter, Time::Unit::Second);
static const Shared<const Frame> gcrfSPtr = Frame::GCRF();

Propagated::Propagated(const Propagator& aPropagator, const State& aState)
: Model(),
Expand Down Expand Up @@ -140,6 +139,9 @@ Array<State> Propagated::calculateStatesAt(const Array<Instant>& anInstantArray)
}
}

// Builder for output states based on cached array
StateBuilder outputStateBuilder = {this->cachedStateArray_.accessFirst()};

Array<State> allStates = Array<State>::Empty();

// Maintain counter separately so as to only iterate once through instant array
Expand Down Expand Up @@ -213,12 +215,12 @@ Array<State> Propagated::calculateStatesAt(const Array<Instant>& anInstantArray)
(forwardStates[k].accessCoordinates() * forwardWeight +
backwardStates[k].accessCoordinates() * backwardWeight);

averagedStates.add({
instants[k],
coordinates,
gcrfSPtr,
propagator_.accessCoordinatesBroker(),
});
averagedStates.add(
outputStateBuilder.build(
instants[k],
coordinates
)
);
}

allStates.add(averagedStates);
Expand Down
65 changes: 38 additions & 27 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/Propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,21 @@ State Propagator::calculateStateAt(const State& aState, const Instant& anInstant
throw ostk::core::error::runtime::Undefined("Propagator");
}

const State state = {
aState.accessInstant(),
aState.inFrame(Propagator::IntegrationFrameSPtr).extractCoordinates(this->coordinatesBrokerSPtr_->getSubsets()),
Propagator::IntegrationFrameSPtr,
this->coordinatesBrokerSPtr_,
};
const StateBuilder solverStateBuilder = {Propagator::IntegrationFrameSPtr, coordinatesBrokerSPtr_};

const State solverInputState = solverStateBuilder.reduce(aState.inFrame(Propagator::IntegrationFrameSPtr));

return numericalSolver_.integrateTime(
state,
const State solverOutputState = numericalSolver_.integrateTime(
solverInputState,
anInstant,
Dynamics::GetSystemOfEquations(this->dynamicsContexts_, state.accessInstant(), Propagator::IntegrationFrameSPtr)
Dynamics::GetSystemOfEquations(
this->dynamicsContexts_, solverInputState.accessInstant(), Propagator::IntegrationFrameSPtr
)
);

const StateBuilder outputStateBuilder = {aState};

return outputStateBuilder.expand(solverOutputState.inFrame(aState.accessFrame()), aState);
}

NumericalSolver::ConditionSolution Propagator::calculateStateToCondition(
Expand All @@ -168,20 +171,21 @@ NumericalSolver::ConditionSolution Propagator::calculateStateToCondition(

const Instant startInstant = aState.getInstant();

const State state = {
aState.accessInstant(),
aState.inFrame(Propagator::IntegrationFrameSPtr).extractCoordinates(this->coordinatesBrokerSPtr_->getSubsets()),
Propagator::IntegrationFrameSPtr,
this->coordinatesBrokerSPtr_,
};
const StateBuilder solverStateBuilder = {Propagator::IntegrationFrameSPtr, coordinatesBrokerSPtr_};

const State solverInputState = solverStateBuilder.reduce(aState.inFrame(Propagator::IntegrationFrameSPtr));

const NumericalSolver::ConditionSolution conditionSolution = numericalSolver_.integrateTime(
state,
NumericalSolver::ConditionSolution conditionSolution = numericalSolver_.integrateTime(
solverInputState,
anInstant,
Dynamics::GetSystemOfEquations(this->dynamicsContexts_, startInstant, Propagator::IntegrationFrameSPtr),
anEventCondition
);

const StateBuilder outputStateBuilder = {aState};

conditionSolution.state = outputStateBuilder.expand(conditionSolution.state.inFrame(aState.accessFrame()), aState);

return conditionSolution;
}

Expand Down Expand Up @@ -210,20 +214,19 @@ Array<State> Propagator::calculateStatesAt(const State& aState, const Array<Inst
}
}

const State state = {
aState.accessInstant(),
aState.inFrame(Propagator::IntegrationFrameSPtr).extractCoordinates(this->coordinatesBrokerSPtr_->getSubsets()),
Propagator::IntegrationFrameSPtr,
this->coordinatesBrokerSPtr_,
};
const StateBuilder solverStateBuilder = {Propagator::IntegrationFrameSPtr, coordinatesBrokerSPtr_};

const State solverInputState = solverStateBuilder.reduce(aState.inFrame(Propagator::IntegrationFrameSPtr));

const Instant startInstant = state.accessInstant();
const Instant startInstant = solverInputState.accessInstant();

Array<Instant> forwardInstants;
forwardInstants.reserve(anInstantArray.getSize());
Array<Instant> backwardInstants;
backwardInstants.reserve(anInstantArray.getSize());

const StateBuilder outputStateBuilder(aState);

for (const Instant& anInstant : anInstantArray)
{
if (anInstant <= startInstant)
Expand All @@ -241,7 +244,7 @@ Array<State> Propagator::calculateStatesAt(const State& aState, const Array<Inst
if (!forwardInstants.isEmpty())
{
forwardPropagatedStates = numericalSolver_.integrateTime(
state,
solverInputState,
forwardInstants,
Dynamics::GetSystemOfEquations(this->dynamicsContexts_, startInstant, Propagator::IntegrationFrameSPtr)
);
Expand All @@ -254,15 +257,23 @@ Array<State> Propagator::calculateStatesAt(const State& aState, const Array<Inst
std::reverse(backwardInstants.begin(), backwardInstants.end());

backwardPropagatedStates = numericalSolver_.integrateTime(
state,
solverInputState,
backwardInstants,
Dynamics::GetSystemOfEquations(this->dynamicsContexts_, startInstant, Propagator::IntegrationFrameSPtr)
);

std::reverse(backwardPropagatedStates.begin(), backwardPropagatedStates.end());
}

return backwardPropagatedStates + forwardPropagatedStates;
Array<State> outputStates;
outputStates.reserve(backwardPropagatedStates.getSize() + forwardPropagatedStates.getSize());

for (const State& solverOutputState : backwardPropagatedStates + forwardPropagatedStates)
{
outputStates.add(outputStateBuilder.expand(solverOutputState.inFrame(aState.accessFrame()), aState));
}

return outputStates;
}

void Propagator::print(std::ostream& anOutputStream, bool displayDecorator) const
Expand Down
Loading

0 comments on commit 7c1420a

Please sign in to comment.