- 3.0.2 core module.
ADCodegenLinearizer.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 #pragma once
6 
7 #include <ct/core/templateDir.h>
8 
9 #ifdef CPPADCG
10 
11 namespace ct {
12 namespace core {
13 
15 
49 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
50 class ADCodegenLinearizer : public LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
51 {
52 public:
53  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 
56 
57  typedef CppAD::AD<CppAD::cg::CG<SCALAR>> ADCGScalar;
58 
59  typedef ControlledSystem<STATE_DIM, CONTROL_DIM, ADCGScalar> system_t;
60  typedef DynamicsLinearizerADCG<STATE_DIM, CONTROL_DIM, ADCGScalar, ADCGScalar>
61  linearizer_t;
62 
63  typedef typename Base::state_vector_t state_vector_t;
64  typedef typename Base::control_vector_t control_vector_t;
65  typedef typename Base::state_matrix_t state_matrix_t;
66  typedef typename Base::state_control_matrix_t state_control_matrix_t;
67 
68 
70 
75  ADCodegenLinearizer(std::shared_ptr<system_t> nonlinearSystem, bool cacheJac = true)
76  : Base(nonlinearSystem->getType()),
77  dFdx_(state_matrix_t::Zero()),
78  dFdu_(state_control_matrix_t::Zero()),
79  cacheJac_(cacheJac),
80  nonlinearSystem_(nonlinearSystem),
81  linearizer_(std::bind(&system_t::computeControlledDynamics,
82  nonlinearSystem_.get(),
83  std::placeholders::_1,
84  std::placeholders::_2,
85  std::placeholders::_3,
86  std::placeholders::_4))
87  {
88  }
89 
91  ADCodegenLinearizer(const ADCodegenLinearizer<STATE_DIM, CONTROL_DIM>& arg)
92  : Base(arg.nonlinearSystem_->getType()),
93  dFdx_(arg.dFdx_),
94  dFdu_(arg.dFdu_),
95  cacheJac_(arg.cacheJac_),
96  nonlinearSystem_(arg.nonlinearSystem_->clone()),
97  linearizer_(arg.linearizer_)
98  {
99  }
100 
102  ADCodegenLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>* clone() const override
103  {
104  return new ADCodegenLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>(*this);
105  }
106 
108 
123  const state_matrix_t& getDerivativeState(const state_vector_t& x,
124  const control_vector_t& u,
125  const SCALAR t = SCALAR(0.0)) override
126  {
127  dFdx_ = linearizer_.getDerivativeState(x, u, t);
128  return dFdx_;
129  }
130 
132 
147  const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
148  const control_vector_t& u,
149  const SCALAR t = SCALAR(0.0)) override
150  {
151  dFdu_ = linearizer_.getDerivativeControl(x, u, t);
152  return dFdu_;
153  }
154 
156 
162  void compileJIT(const std::string& libName = "ADCodegenLinearizer") { linearizer_.compileJIT(libName); }
164 
177  void generateCode(const std::string& systemName,
178  const std::string& outputDir = ct::core::CODEGEN_OUTPUT_DIR,
179  const std::string& templateDir = ct::core::CODEGEN_TEMPLATE_DIR,
180  const std::string& ns1 = "core",
181  const std::string& ns2 = "generated",
182  bool useReverse = false,
183  bool ignoreZero = true)
184  {
185  std::string codeJacA, codeJacB;
186  linearizer_.generateCode(codeJacA, codeJacB, useReverse, ignoreZero);
187 
188  writeCodeFile(
189  templateDir, outputDir, systemName, ns1, ns2, codeJacA, codeJacB, "AUTOGENERATED_CODE_PLACEHOLDER");
190  }
191 
193  const linearizer_t& getLinearizer() const { return linearizer_; }
194 private:
196 
207  void writeCodeFile(const std::string& templateDir,
208  const std::string& outputDir,
209  const std::string& systemName,
210  const std::string& ns1,
211  const std::string& ns2,
212  const std::string& codeJacA,
213  const std::string& codeJacB,
214  const std::string& codePlaceholder)
215  {
216  std::cout << "Generating linear system..." << std::endl;
217 
218  size_t maxTempVarCountState, maxTempVarCountControl;
219  linearizer_.getMaxTempVarCount(maxTempVarCountState, maxTempVarCountControl);
220 
221  std::string header = internal::CGHelpers::parseFile(templateDir + "/LinearSystem.tpl.h");
222  std::string source = internal::CGHelpers::parseFile(templateDir + "/LinearSystem.tpl.cpp");
223 
224  const std::string scalarName(linearizer_.getOutScalarType());
225 
226  replaceSizesAndNames(header, systemName, scalarName, ns1, ns2);
227  replaceSizesAndNames(source, systemName, scalarName, ns1, ns2);
228 
229  internal::CGHelpers::replaceOnce(header, "MAX_COUNT_STATE", std::to_string(maxTempVarCountState));
230  internal::CGHelpers::replaceOnce(header, "MAX_COUNT_CONTROL", std::to_string(maxTempVarCountControl));
231 
232  internal::CGHelpers::replaceOnce(source, codePlaceholder + "_JAC_A", codeJacA);
233  internal::CGHelpers::replaceOnce(source, codePlaceholder + "_JAC_B", codeJacB);
234 
235  internal::CGHelpers::writeFile(outputDir + "/" + systemName + ".h", header);
236  internal::CGHelpers::writeFile(outputDir + "/" + systemName + ".cpp", source);
237 
238 
239  std::cout << "... Done! Successfully generated linear system" << std::endl;
240  }
241 
243 
250  void replaceSizesAndNames(std::string& file,
251  const std::string& systemName,
252  const std::string& scalarName,
253  const std::string& ns1,
254  const std::string& ns2)
255  {
256  internal::CGHelpers::replaceAll(file, "LINEAR_SYSTEM_NAME", systemName);
257  internal::CGHelpers::replaceAll(file, "NS1", ns1);
258  internal::CGHelpers::replaceAll(file, "NS2", ns2);
259  internal::CGHelpers::replaceAll(file, "STATE_DIM", std::to_string(STATE_DIM));
260  internal::CGHelpers::replaceAll(file, "CONTROL_DIM", std::to_string(CONTROL_DIM));
261  internal::CGHelpers::replaceAll(file, "SCALAR", scalarName);
262  }
263 
264  state_matrix_t dFdx_;
265  state_control_matrix_t dFdu_;
266 
267  bool cacheJac_;
268 
269  std::shared_ptr<system_t> nonlinearSystem_;
270 
271  linearizer_t linearizer_;
272 };
273 
274 } // namespace core
275 } // namespace ct
276 
277 #endif
interface class for a general linear system or linearized system
Definition: LinearSystem.h:23
Eigen::Matrix< double, nControls, 1 > control_vector_t
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t