- 3.0.1 core module.
ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME > Class Template Reference

Computes the linearization of a system dynamics function through autodiff with code generation. More...

#include <DynamicsLinearizerADCG.h>

Inheritance diagram for ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >:
ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >

Public Types

typedef Base::OUT_SCALAR OUT_SCALAR
 scalar type of resulting linear system More...
 
typedef Base::state_vector_t state_vector_t
 state vector type More...
 
typedef Base::control_vector_t control_vector_t
 control vector type More...
 
typedef Base::state_matrix_t state_matrix_t
 state Jacobian type (A) More...
 
typedef Base::state_control_matrix_t state_control_matrix_t
 control Jacobian type (B) More...
 
typedef Base::dynamics_fct_t dynamics_fct_t
 dynamics function signature More...
 
- Public Types inherited from ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >
typedef std::conditional<(std::is_same< SCALAR, CppAD::AD< double >>::value)||(std::is_same< SCALAR, CppAD::AD< CppAD::cg::CG< double >>>::value), double, float >::type OUT_SCALAR
 scalar type of resulting linear system More...
 
typedef StateVector< STATE_DIM, OUT_SCALARstate_vector_t
 
typedef ControlVector< CONTROL_DIM, OUT_SCALARcontrol_vector_t
 
typedef StateVector< STATE_DIM, SCALARstate_vector_ad_t
 
typedef ControlVector< CONTROL_DIM, SCALARcontrol_vector_ad_t
 
typedef StateMatrix< STATE_DIM, OUT_SCALARstate_matrix_t
 
typedef StateControlMatrix< STATE_DIM, CONTROL_DIM, OUT_SCALARstate_control_matrix_t
 
typedef std::function< void(const state_vector_ad_t &, const TIME &, const control_vector_ad_t &, state_vector_ad_t &)> dynamics_fct_t
 dynamics function signature More...
 

Public Member Functions

 DynamicsLinearizerADCG (dynamics_fct_t dyn, bool cacheJac=true)
 default constructor More...
 
 DynamicsLinearizerADCG (const DynamicsLinearizerADCG &rhs)
 copy constructor More...
 
const state_matrix_tgetDerivativeState (const state_vector_t &x, const control_vector_t &u, const OUT_SCALAR t=0.0)
 compute and return derivative w.r.t. state More...
 
const state_control_matrix_tgetDerivativeControl (const state_vector_t &x, const control_vector_t &u, const OUT_SCALAR t=0.0)
 compute and return derivative w.r.t. control More...
 
void compileJIT (const std::string &libName="DynamicsLinearizerADCG", bool verbose=false)
 compile just-in-time More...
 
void generateCode (std::string &codeJacA, std::string &codeJacB, bool useReverse=false, bool ignoreZero=true)
 generates source code More...
 
void getMaxTempVarCount (size_t &maxTempVarCountState, size_t &maxTempVarCountControl) const
 accessor to maxTempVarCount variables More...
 
const std::shared_ptr< CppAD::cg::DynamicLib< OUT_SCALAR > > getDynamicLib () const
 retrieve the dynamic library, e.g. for testing purposes More...
 
- Public Member Functions inherited from ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >
 DynamicsLinearizerADBase (dynamics_fct_t dyn)
 default constructor More...
 
 DynamicsLinearizerADBase (const DynamicsLinearizerADBase &arg)
 copy constructor More...
 
template<typename T = std::string>
std::enable_if< std::is_same< OUT_SCALAR, double >::value, T >::type getOutScalarType () const
 
template<typename T = std::string>
std::enable_if< std::is_same< OUT_SCALAR, float >::value, T >::type getOutScalarType () const
 
virtual ~DynamicsLinearizerADBase ()
 destructor More...
 

Public Attributes

EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME > Base
 
- Public Attributes inherited from ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >
 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
 

Protected Member Functions

void computeJacobian (const state_vector_t &x, const control_vector_t &u)
 computes the Jacobians More...
 
- Protected Member Functions inherited from ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >
void initialize ()
 initialize all utilities More...
 
void recordTerms ()
 record the model More...
 
void setupSparsityA ()
 setup the sparsity of the state Jacobian More...
 
void setupSparsityB ()
 setup the sparsity of the input Jacobian More...
 

Protected Attributes

dynamics_fct_t dynamics_fct_
 function handle to system dynamics More...
 
state_matrix_t dFdx_
 Jacobian wrt state. More...
 
state_control_matrix_t dFdu_
 Jacobian wrt input. More...
 
state_vector_t x_at_cache_
 state at which Jacobian has been cached More...
 
control_vector_t u_at_cache_
 input at which Jacobian has been cached More...
 
std::string jitLibName_
 name of the library compiled with JIT More...
 
bool compiled_
 flag if library from generated code is compiled More...
 
bool cacheJac_
 flag if Jacobian will be cached More...
 
CppAD::cg::GccCompiler< OUT_SCALARcompiler_
 compiler instance for JIT compilation More...
 
std::shared_ptr< CppAD::cg::DynamicLib< OUT_SCALAR > > dynamicLib_
 compiled and dynamically loaded library More...
 
std::shared_ptr< CppAD::cg::GenericModel< OUT_SCALAR > > model_
 Auto-Diff model. More...
 
size_t maxTempVarCountState_
 number of temporary variables in the source code of the state Jacobian More...
 
size_t maxTempVarCountControl_
 number of temporary variables in the source code of the input Jacobian More...
 
- Protected Attributes inherited from ct::core::internal::DynamicsLinearizerADBase< STATE_DIM, CONTROL_DIM, SCALAR, TIME >
const size_t A_entries = STATE_DIM * STATE_DIM
 number of entries in the state Jacobian More...
 
const size_t B_entries = STATE_DIM * CONTROL_DIM
 number of entries in the input Jacobian More...
 
const size_t FullJac_entries
 number of entries in the stacked Jacobian More...
 
dynamics_fct_t dynamics_fct_
 function handle to system dynamics More...
 
CppAD::ADFun< typename SCALAR::value_type > f_
 Auto-Diff function. More...
 
SparsityPattern sparsityA_
 sparsity pattern of the state Jacobian More...
 
SparsityPattern sparsityB_
 sparsity pattern of the input Jacobian More...
 

Detailed Description

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
class ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >

Computes the linearization of a system dynamics function through autodiff with code generation.

This class takes a function handle representing system dynamics of the form $ f(x(t),t,u(t),\dot{x(t)}) $ or $ f(x[n],n,u[n],x[n+1]) $ where the last argument is the result of the evaluation in each case. It then computes the linearization around a given point $ x = x_s $, $ u = u_s $.

Template Parameters
STATE_DIMdimension of state vector
CONTROL_DIMdimension of control vector
SCALARscalar type
TIMEtype of time variable of dynamics

Member Typedef Documentation

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::OUT_SCALAR ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::OUT_SCALAR

scalar type of resulting linear system

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::state_vector_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::state_vector_t

state vector type

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::control_vector_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::control_vector_t

control vector type

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::state_matrix_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::state_matrix_t

state Jacobian type (A)

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::state_control_matrix_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::state_control_matrix_t

control Jacobian type (B)

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
typedef Base::dynamics_fct_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::dynamics_fct_t

dynamics function signature

Constructor & Destructor Documentation

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::DynamicsLinearizerADCG ( dynamics_fct_t  dyn,
bool  cacheJac = true 
)
inline

default constructor

Initializes the DynamicsLinearizerADCG with the system dynamics

Parameters
dynfunction handle to system dynamics
cacheJacif true, caches the Jacobians to prevent recomputation for same state/control
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::DynamicsLinearizerADCG ( const DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME > &  rhs)
inline

copy constructor

Member Function Documentation

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
const state_matrix_t& ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::getDerivativeState ( const state_vector_t x,
const control_vector_t u,
const OUT_SCALAR  t = 0.0 
)
inline

compute and return derivative w.r.t. state

Warning
Call compileJIT() before calling this function
Parameters
xstate to linearize at
ucontrol to linearize at
ttime
Returns
Jacobian w.r.t. state

Referenced by ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::getAandB(), ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::getDerivativeState(), and ct::core::ADCodegenLinearizer< state_dim, control_dim >::getDerivativeState().

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
const state_control_matrix_t& ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::getDerivativeControl ( const state_vector_t x,
const control_vector_t u,
const OUT_SCALAR  t = 0.0 
)
inline

compute and return derivative w.r.t. control

Warning
Call compileJIT() before calling this function
Parameters
xstate to linearize at
ucontrol to linearize at
ttime
Returns
Jacobian w.r.t. control

Referenced by ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::getAandB(), ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::getDerivativeControl(), and ct::core::ADCodegenLinearizer< state_dim, control_dim >::getDerivativeControl().

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
void ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::compileJIT ( const std::string &  libName = "DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >",
bool  verbose = false 
)
inline

compile just-in-time

Generates the source code, compiles it and dynamically loads the resulting library.

Note
If this function takes a long time, consider generating the source code using generateCode() and compile it before runtime.

Referenced by ct::core::ADCodegenLinearizer< state_dim, control_dim >::compileJIT(), and ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::compileJIT().

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
void ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::generateCode ( std::string &  codeJacA,
std::string &  codeJacB,
bool  useReverse = false,
bool  ignoreZero = true 
)
inline

generates source code

This generates source code for computing the system linearization. The generated string should be passed to a function which writes the generated code to a file.

This function uses a template file in which it replaces two placeholders, each identified as the string "AUTOGENERATED_CODE_PLACEHOLDER"

Parameters
[out]codeJacAstring with the generated code for the A matrix
[out]codeJacBstring with the generated code for the B matrix
[in]useReverseif true, uses Auto-Diff reverse mode
[in]ignoreZeroif true, zero entries are not assigned zero

Referenced by ct::core::ADCodegenLinearizer< state_dim, control_dim >::generateCode(), and ct::core::DiscreteSystemLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR >::generateCode().

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
void ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::getMaxTempVarCount ( size_t &  maxTempVarCountState,
size_t &  maxTempVarCountControl 
) const
inline
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
const std::shared_ptr<CppAD::cg::DynamicLib<OUT_SCALAR> > ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::getDynamicLib ( ) const
inline

retrieve the dynamic library, e.g. for testing purposes

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
void ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::computeJacobian ( const state_vector_t x,
const control_vector_t u 
)
inlineprotected

computes the Jacobians

Given a state and input this method evaluates both Jacobians and caches them

Parameters
xstate to linearize around
uinput to linearize around

Referenced by ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, ADCGScalar, ADCGScalar >::getDerivativeControl(), and ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, ADCGScalar, ADCGScalar >::getDerivativeState().

Member Data Documentation

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef internal::DynamicsLinearizerADBase<STATE_DIM, CONTROL_DIM, SCALAR, TIME> ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::Base
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
dynamics_fct_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::dynamics_fct_
protected

function handle to system dynamics

template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
state_matrix_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::dFdx_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
std::string ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::jitLibName_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
bool ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::cacheJac_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
CppAD::cg::GccCompiler<OUT_SCALAR> ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::compiler_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
std::shared_ptr<CppAD::cg::DynamicLib<OUT_SCALAR> > ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::dynamicLib_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
size_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::maxTempVarCountState_
protected
template<size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
size_t ct::core::DynamicsLinearizerADCG< STATE_DIM, CONTROL_DIM, SCALAR, TIME >::maxTempVarCountControl_
protected

The documentation for this class was generated from the following file: