Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of termination condition #262

Merged
merged 3 commits into from
Nov 15, 2024
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
10 changes: 5 additions & 5 deletions include/tudat/astro/propagators/integrateEquations.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ void propagateToExactTerminationCondition(
dependentVariableHistory[ endTime ] = dependentVariableFunction( );

// Check stopping conditions to be able to save details
propagationTerminationCondition->checkStopCondition( endTime, currentCpuTime );
propagationTerminationCondition->checkStopCondition( endTime, currentCpuTime, endState.template cast< double >( ) );
}
}
else
{
// Check stopping conditions to be able to save details
integrator->getStateDerivativeFunction( )( endTime, endState );
propagationTerminationCondition->checkStopCondition( endTime, currentCpuTime );
propagationTerminationCondition->checkStopCondition( endTime, currentCpuTime, endState.template cast< double >( ) );
}

// Turn step size control back on
Expand Down Expand Up @@ -618,7 +618,7 @@ void integrateEquationsFromIntegrator(
std::chrono::steady_clock::now( ) - initialClockTime ).count( ) * 1.0e-9;
cumulativeComputationTimeHistory[ currentTime ] = currentCPUTime;

if( propagationTerminationCondition->checkStopCondition( static_cast< double >( currentTime ), currentCPUTime ) )
if( propagationTerminationCondition->checkStopCondition( static_cast< double >( currentTime ), currentCPUTime, newState.template cast< double >( ) ) )
{
// Propagate to the exact termination conditions
if( propagationTerminationCondition->iterateToExactTermination( ) )
Expand Down Expand Up @@ -720,8 +720,8 @@ void integrateEquationsFromIntegrator(
const std::function< void( StateType& ) > statePostProcessingFunction = std::function< void( StateType& ) >( ),
const std::shared_ptr< SingleArcPropagatorProcessingSettings > processingSettings = std::make_shared< SingleArcPropagatorProcessingSettings >( ) )
{
std::function< bool( const double, const double ) > stopPropagationFunction =
std::bind( &PropagationTerminationCondition::checkStopCondition, propagationTerminationCondition, std::placeholders::_1, std::placeholders::_2 );
std::function< bool( const double, const double, const Eigen::MatrixXd& ) > stopPropagationFunction =
std::bind( &PropagationTerminationCondition::checkStopCondition, propagationTerminationCondition, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 );

// Create numerical integrator.
std::shared_ptr< numerical_integrators::NumericalIntegrator< TimeType, StateType, StateType, typename scalar_type< TimeType >::value_type > > integrator =
Expand Down
6 changes: 3 additions & 3 deletions include/tudat/math/integrators/numericalIntegrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NumericalIntegrator
*/
NumericalIntegrator( const StateDerivativeFunction& stateDerivativeFunction ) :
stateDerivativeFunction_( stateDerivativeFunction ),
propagationTerminationFunction_( [ = ]( const double, const double ){ return false; } )
propagationTerminationFunction_( [ = ]( const double, const double, const Eigen::MatrixXd& ){ return false; } )
{ }

//! Default virtual destructor.
Expand Down Expand Up @@ -185,7 +185,7 @@ class NumericalIntegrator
* \param terminationFunction Function that returns true if termination condition is reached, false if it has not,
* as a function of current time.
*/
void setPropagationTerminationFunction( std::function< bool( const double, const double ) > terminationFunction )
void setPropagationTerminationFunction( std::function< bool( const double, const double, const Eigen::MatrixXd& ) > terminationFunction )
{
propagationTerminationFunction_ = terminationFunction;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ class NumericalIntegrator
* By default, this function evaluates always to false, so the propagation termination conditions will not be
* checked during the integration subteps.
*/
std::function< bool( const double, const double ) > propagationTerminationFunction_;
std::function< bool( const double, const double, const Eigen::MatrixXd& ) > propagationTerminationFunction_;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class RungeKuttaFixedStepSizeIntegrator
// Check if propagation should terminate because the propagation termination condition has been reached
// while computing the intermediate state.
// If so, return immediately the current state (not recomputed yet), which will be discarded.
if ( this->propagationTerminationFunction_( static_cast< double >( time ), TUDAT_NAN ) )
if ( this->propagationTerminationFunction_( static_cast< double >( time ), TUDAT_NAN, intermediateState.template cast< double >( ) ) )
{
this->propagationTerminationConditionReachedDuringStep_ = true;
return this->currentState_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ ::performIntegrationStep( const TimeStepType stepSize )
// Check if propagation should terminate because the propagation termination condition has been reached
// while computing the intermediate state.
// If so, return immediately the current state (not recomputed yet), which will be discarded.
if ( this->propagationTerminationFunction_( static_cast< double >( time ), TUDAT_NAN ) )
if ( this->propagationTerminationFunction_( static_cast< double >( time ), TUDAT_NAN, intermediateState.template cast< double >( ) ) )
{
this->propagationTerminationConditionReachedDuringStep_ = true;
return this->currentState_;
Expand Down
20 changes: 10 additions & 10 deletions include/tudat/simulation/propagation_setup/propagationTermination.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class PropagationTerminationCondition
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
virtual bool checkStopCondition( const double time, const double cpuTime ) = 0;
virtual bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) ) = 0;

virtual bool iterateToExactTermination( )
{
Expand Down Expand Up @@ -167,7 +167,7 @@ class FixedTimePropagationTerminationCondition: public PropagationTerminationCon
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime );
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) );

//! Function to retrieve time at which the propagation is to stop.
/*!
Expand Down Expand Up @@ -213,7 +213,7 @@ class FixedCPUTimePropagationTerminationCondition: public PropagationTermination
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime );
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) );

private:

Expand Down Expand Up @@ -275,7 +275,7 @@ class SingleVariableLimitPropagationTerminationCondition: public PropagationTerm
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime );
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) );

//! Function to return current difference between termination variable, and the value at which the propagation must terminate
/*!
Expand Down Expand Up @@ -329,7 +329,7 @@ class CustomTerminationCondition: public PropagationTerminationCondition
* condition, or whether it is to terminate on the first step where it is violated.
*/
CustomTerminationCondition(
std::function< bool( const double ) >& checkStopCondition,
std::function< bool( const double, const Eigen::MatrixXd& ) >& checkStopCondition,
const bool checkTerminationToExactCondition = false ):
PropagationTerminationCondition( custom_stopping_condition, checkTerminationToExactCondition ),
checkStopCondition_( checkStopCondition )
Expand All @@ -342,16 +342,16 @@ class CustomTerminationCondition: public PropagationTerminationCondition
* \param cpuTime Current CPU time in propagation.
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime )
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState )
{
TUDAT_UNUSED_PARAMETER( cpuTime );
return checkStopCondition_( time );
return checkStopCondition_( time, currentState );
}

private:

//! Custom temination function.
std::function< bool( const double ) > checkStopCondition_;
std::function< bool( const double, const Eigen::MatrixXd& ) > checkStopCondition_;

};

Expand Down Expand Up @@ -389,7 +389,7 @@ class HybridPropagationTerminationCondition: public PropagationTerminationCondit
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime );
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) );

bool iterateToExactTermination( );

Expand Down Expand Up @@ -463,7 +463,7 @@ class NonSequentialPropagationTerminationCondition: public PropagationTerminatio
* \param cpuTime Current CPU time in propagation
* \return True if propagation is to be stopped, false otherwise.
*/
bool checkStopCondition( const double time, const double cpuTime );
bool checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& currentState = Eigen::MatrixXd::Zero( 0, 0 ) );

//! Function to retrieve termination condition for forward propagation that is checked when calling checkStopCondition is called.
/*!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,18 @@ class PropagationCustomTerminationSettings: public PropagationTerminationSetting
* stopped.
*/
PropagationCustomTerminationSettings( const std::function< bool( const double ) >& checkStopCondition ):
PropagationTerminationSettings( custom_stopping_condition ),
checkStopCondition_( [=]( const double time, const Eigen::MatrixXd& ){ return checkStopCondition( time ); } ){ }

PropagationCustomTerminationSettings( const std::function< bool( const double, const Eigen::MatrixXd& ) >& checkStopCondition ):
PropagationTerminationSettings( custom_stopping_condition ),
checkStopCondition_( checkStopCondition ){ }

//! Destructor
~PropagationCustomTerminationSettings( ){ }

//! Custom temination function.
std::function< bool( const double ) > checkStopCondition_;
std::function< bool( const double, const Eigen::MatrixXd& ) > checkStopCondition_;

};

Expand Down Expand Up @@ -334,6 +338,13 @@ inline std::shared_ptr< PropagationTerminationSettings > popagationCustomTermina
checkStopCondition );
}

inline std::shared_ptr< PropagationTerminationSettings > popagationCustomTerminationSettingsFromFullState(
const std::function< bool( const double, const Eigen::MatrixXd& ) > checkStopCondition )
{
return std::make_shared< PropagationCustomTerminationSettings >(
checkStopCondition );
}

inline std::shared_ptr< PropagationTerminationSettings > nonSequentialPropagationTerminationSettings(
const std::shared_ptr< PropagationTerminationSettings > forwardTerminationSettings,
const std::shared_ptr< PropagationTerminationSettings > backwardTerminationSettings )
Expand Down
19 changes: 10 additions & 9 deletions src/simulation/propagation_setup/propagationTermination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace propagators
{

//! Function to check whether the propagation is to be be stopped
bool FixedTimePropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime )
bool FixedTimePropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& state )
{
bool stopPropagation = false;

Expand All @@ -34,13 +34,13 @@ bool FixedTimePropagationTerminationCondition::checkStopCondition( const double
}

//! Function to check whether the propagation is to be be stopped
bool FixedCPUTimePropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime )
bool FixedCPUTimePropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& state )
{
return cpuTime >= cpuStopTime_;
}

//! Function to check whether the propagation is to be be stopped
bool SingleVariableLimitPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime )
bool SingleVariableLimitPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& state )
{
bool stopPropagation = false;
double currentVariable = variableRetrievalFunction_( );
Expand All @@ -58,7 +58,7 @@ bool SingleVariableLimitPropagationTerminationCondition::checkStopCondition( con
}

//! Function to check whether the propagation is to be be stopped
bool HybridPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime )
bool HybridPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& state )
{
// Check if single condition is fulfilled.
bool stopPropagation = -1;
Expand All @@ -68,7 +68,7 @@ bool HybridPropagationTerminationCondition::checkStopCondition( const double tim
stopPropagation = false;
for( unsigned int i = 0; i < propagationTerminationCondition_.size( ); i++ )
{
if( propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime ) )
if( propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime, state ) )
{
stopIndex = i;
stopPropagation = true;
Expand All @@ -86,7 +86,7 @@ bool HybridPropagationTerminationCondition::checkStopCondition( const double tim
stopPropagation = true;
for( unsigned int i = 0; i < propagationTerminationCondition_.size( ); i++ )
{
if( !propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime ) )
if( !propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime, state ) )
{
stopIndex = i;
stopPropagation = false;
Expand All @@ -105,7 +105,7 @@ bool HybridPropagationTerminationCondition::checkStopCondition( const double tim
{
for( unsigned int i = ( stopIndex + 1 ); i < propagationTerminationCondition_.size( ); i++ )
{
isConditionMetWhenStopping_[ i ] = propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime );
isConditionMetWhenStopping_[ i ] = propagationTerminationCondition_.at( i )->checkStopCondition( time, cpuTime, state );
}
}

Expand All @@ -129,11 +129,12 @@ bool HybridPropagationTerminationCondition::iterateToExactTermination( )
}

//! Function to check whether the propagation is to be be stopped
bool NonSequentialPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime )
bool NonSequentialPropagationTerminationCondition::checkStopCondition( const double time, const double cpuTime, const Eigen::MatrixXd& state )
{
// Check if single condition is fulfilled.
bool stopPropagation = false;
if ( forwardPropagationTerminationCondition_->checkStopCondition( time, cpuTime ) || backwardPropagationTerminationCondition_->checkStopCondition( time, cpuTime ) )
if ( forwardPropagationTerminationCondition_->checkStopCondition( time, cpuTime, state ) ||
backwardPropagationTerminationCondition_->checkStopCondition( time, cpuTime, state ) )
{
stopPropagation = true;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/astro/propagators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ TUDAT_ADD_TEST_CASE(HybridArcDynamics PRIVATE_LINKS ${Tudat_PROPAGATION_LIBRARIE

TUDAT_ADD_TEST_CASE(PropagationTerminationReason PRIVATE_LINKS ${Tudat_ESTIMATION_LIBRARIES})

TUDAT_ADD_TEST_CASE(PropagationTermination PRIVATE_LINKS ${Tudat_ESTIMATION_LIBRARIES})

TUDAT_ADD_TEST_CASE(RotationalDynamicsPropagator PRIVATE_LINKS ${Tudat_PROPAGATION_LIBRARIES})

TUDAT_ADD_TEST_CASE(CR3BPPropagation PRIVATE_LINKS ${Tudat_PROPAGATION_LIBRARIES})
Expand Down
Loading
Loading