28 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR,
typename TIME>
29 class DynamicsLinearizerADCG :
public internal::DynamicsLinearizerADBase<STATE_DIM, CONTROL_DIM, SCALAR, TIME>
32 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 typedef internal::DynamicsLinearizerADBase<STATE_DIM, CONTROL_DIM, SCALAR, TIME> Base;
36 typedef typename Base::OUT_SCALAR OUT_SCALAR;
42 typedef typename Base::state_control_matrix_t state_control_matrix_t;
44 typedef typename Base::dynamics_fct_t dynamics_fct_t;
52 DynamicsLinearizerADCG(dynamics_fct_t dyn,
bool cacheJac =
true)
55 dFdx_(state_matrix_t::Zero()),
56 dFdu_(state_control_matrix_t::Zero()),
57 x_at_cache_(state_vector_t::Random()),
58 u_at_cache_(control_vector_t::Random()),
62 maxTempVarCountState_(0),
63 maxTempVarCountControl_(0)
68 DynamicsLinearizerADCG(
const DynamicsLinearizerADCG& rhs)
69 : Base(rhs.dynamics_fct_),
70 dynamics_fct_(rhs.dynamics_fct_),
73 x_at_cache_(rhs.x_at_cache_),
74 u_at_cache_(rhs.u_at_cache_),
75 jitLibName_(rhs.jitLibName_),
76 compiled_(rhs.compiled_),
77 cacheJac_(rhs.cacheJac_),
78 maxTempVarCountState_(rhs.maxTempVarCountState_),
79 maxTempVarCountControl_(rhs.maxTempVarCountControl_)
83 dynamicLib_ = internal::CGHelpers::loadDynamicLibCppad<OUT_SCALAR>(jitLibName_);
84 model_ = std::shared_ptr<CppAD::cg::GenericModel<OUT_SCALAR>>(
85 dynamicLib_->model(
"DynamicsLinearizerADCG" + jitLibName_));
98 const state_matrix_t& getDerivativeState(
const state_vector_t& x,
99 const control_vector_t& u,
100 const OUT_SCALAR t = 0.0)
103 throw std::runtime_error(
104 "Called getDerivativeState on ADCodegenLinearizer before compiling. Call 'compile()' before");
107 if (!cacheJac_ || (x != x_at_cache_ || u != u_at_cache_))
108 computeJacobian(x, u);
122 const state_control_matrix_t& getDerivativeControl(
const state_vector_t& x,
123 const control_vector_t& u,
124 const OUT_SCALAR t = 0.0)
127 throw std::runtime_error(
128 "Called getDerivativeState on ADCodegenLinearizer before compiling. Call 'compile()' before");
131 if (!cacheJac_ || (x != x_at_cache_ || u != u_at_cache_))
132 computeJacobian(x, u);
144 void compileJIT(
const std::string& libName =
"DynamicsLinearizerADCG",
bool verbose =
false)
150 clock_gettime(CLOCK_MONOTONIC, &ts);
153 std::string uniqueID =
154 std::to_string(std::hash<std::thread::id>()(std::this_thread::get_id())) +
"_" + std::to_string(ts.tv_nsec);
156 jitLibName_ = libName + uniqueID;
158 CppAD::cg::ModelCSourceGen<OUT_SCALAR> cgen(this->f_,
"DynamicsLinearizerADCG" + jitLibName_);
159 cgen.setCreateJacobian(
true);
160 CppAD::cg::ModelLibraryCSourceGen<OUT_SCALAR> libcgen(cgen);
161 std::string tempDir =
"cppad_temp" + uniqueID;
164 std::cout <<
"Starting to compile " << jitLibName_ <<
" library ..." << std::endl;
165 std::cout <<
"In temporary directory " << tempDir << std::endl;
169 CppAD::cg::DynamicModelLibraryProcessor<OUT_SCALAR> p(libcgen, jitLibName_);
170 compiler_.setTemporaryFolder(tempDir);
171 dynamicLib_ = std::shared_ptr<CppAD::cg::DynamicLib<OUT_SCALAR>>(p.createDynamicLibrary(compiler_));
173 model_ = std::shared_ptr<CppAD::cg::GenericModel<OUT_SCALAR>>(
174 dynamicLib_->model(
"DynamicsLinearizerADCG" + jitLibName_));
179 std::cout <<
"Successfully compiled " << jitLibName_ << std::endl;
196 void generateCode(std::string& codeJacA, std::string& codeJacB,
bool useReverse =
false,
bool ignoreZero =
true)
198 this->sparsityA_.clearWork();
199 size_t jacDimension = STATE_DIM * STATE_DIM;
200 codeJacA = internal::CGHelpers::generateJacobianSource<typename SCALAR::value_type, OUT_SCALAR>(this->f_,
201 this->sparsityA_, jacDimension, maxTempVarCountState_, useReverse, ignoreZero,
"jac",
"x_in",
"vX_",
202 Base::getOutScalarType());
204 this->sparsityB_.clearWork();
205 jacDimension = STATE_DIM * CONTROL_DIM;
206 codeJacB = internal::CGHelpers::generateJacobianSource<typename SCALAR::value_type, OUT_SCALAR>(this->f_,
207 this->sparsityB_, jacDimension, maxTempVarCountControl_, useReverse, ignoreZero,
"jac",
"x_in",
"vU_",
208 Base::getOutScalarType());
212 void getMaxTempVarCount(
size_t& maxTempVarCountState,
size_t& maxTempVarCountControl)
const 214 maxTempVarCountState = maxTempVarCountState_;
215 maxTempVarCountControl = maxTempVarCountControl_;
219 const std::shared_ptr<CppAD::cg::DynamicLib<OUT_SCALAR>> getDynamicLib()
const {
return dynamicLib_; }
227 void computeJacobian(
const state_vector_t& x,
const control_vector_t& u)
230 Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> input(STATE_DIM + CONTROL_DIM);
233 Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> jac(Base::FullJac_entries);
235 jac = model_->Jacobian(input);
237 Eigen::Map<Eigen::Matrix<OUT_SCALAR, STATE_DIM + CONTROL_DIM, STATE_DIM>> out(jac.data());
239 dFdx_ = out.template topRows<STATE_DIM>().transpose();
240 dFdu_ = out.template bottomRows<CONTROL_DIM>().transpose();
247 dynamics_fct_t dynamics_fct_;
249 state_matrix_t dFdx_;
250 state_control_matrix_t dFdu_;
252 state_vector_t x_at_cache_;
253 control_vector_t u_at_cache_;
255 std::string jitLibName_;
258 CppAD::cg::GccCompiler<OUT_SCALAR> compiler_;
260 std::shared_ptr<CppAD::cg::DynamicLib<OUT_SCALAR>> dynamicLib_;
261 std::shared_ptr<CppAD::cg::GenericModel<OUT_SCALAR>> model_;
263 size_t maxTempVarCountState_;
264 size_t maxTempVarCountControl_;
Eigen::Matrix< double, nControls, 1 > control_vector_t
const bool verbose
Definition: JacobianCGTest.h:19
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t