GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/numdiff/activation.hpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 16 16 100.0%
Branches: 17 30 56.7%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, LAAS-CNRS, New York University,
5 // Max Planck Gesellschaft, University of Edinburgh
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef CROCODDYL_CORE_NUMDIFF_ACTIVATION_HPP_
11 #define CROCODDYL_CORE_NUMDIFF_ACTIVATION_HPP_
12
13 #include <iostream>
14 #include <vector>
15
16 #include "crocoddyl/core/activation-base.hpp"
17 #include "crocoddyl/core/fwd.hpp"
18
19 namespace crocoddyl {
20
21 template <typename _Scalar>
22 class ActivationModelNumDiffTpl : public ActivationModelAbstractTpl<_Scalar> {
23 public:
24 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25
26 typedef _Scalar Scalar;
27 typedef MathBaseTpl<Scalar> MathBase;
28 typedef ActivationModelAbstractTpl<Scalar> Base;
29 typedef ActivationDataNumDiffTpl<Scalar> Data;
30 typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract;
31 typedef typename MathBase::VectorXs VectorXs;
32 typedef typename MathBase::MatrixXs MatrixXs;
33
34 /**
35 * @brief Construct a new ActivationModelNumDiff object
36 *
37 * @param model
38 */
39 explicit ActivationModelNumDiffTpl(boost::shared_ptr<Base> model);
40
41 /**
42 * @brief Destroy the ActivationModelNumDiff object
43 */
44 virtual ~ActivationModelNumDiffTpl();
45
46 /**
47 * @brief @copydoc Base::calc()
48 */
49 virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data,
50 const Eigen::Ref<const VectorXs>& r);
51
52 /**
53 * @brief @copydoc Base::calcDiff()
54 */
55 virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data,
56 const Eigen::Ref<const VectorXs>& r);
57
58 /**
59 * @brief Create a Data object from the given model.
60 *
61 * @return boost::shared_ptr<ActivationDataAbstract>
62 */
63 virtual boost::shared_ptr<ActivationDataAbstract> createData();
64
65 /**
66 * @brief Get the model_ object
67 *
68 * @return Base&
69 */
70 const boost::shared_ptr<Base>& get_model() const;
71
72 /**
73 * @brief Return the disturbance constant used in the numerical
74 * differentiation routine
75 */
76 const Scalar get_disturbance() const;
77
78 /**
79 * @brief Modify the disturbance constant used in the numerical
80 * differentiation routine
81 */
82 void set_disturbance(const Scalar disturbance);
83
84 private:
85 boost::shared_ptr<Base>
86 model_; //!< model to compute the finite differentiation from
87 Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian
88 //!< calculation
89
90 protected:
91 using Base::nr_;
92 };
93
94 template <typename _Scalar>
95 struct ActivationDataNumDiffTpl : public ActivationDataAbstractTpl<_Scalar> {
96 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
97
98 typedef _Scalar Scalar;
99 typedef MathBaseTpl<Scalar> MathBase;
100 typedef typename MathBase::VectorXs VectorXs;
101 typedef ActivationDataAbstractTpl<Scalar> Base;
102 typedef typename MathBase::MatrixXs MatrixXs;
103
104 /**
105 * @brief Construct a new ActivationDataNumDiff object
106 *
107 * @tparam Model is the type of the ActivationModel.
108 * @param model is the object to compute the numerical differentiation from.
109 */
110 template <template <typename Scalar> class Model>
111 11 explicit ActivationDataNumDiffTpl(Model<Scalar>* const model)
112 : Base(model),
113
1/2
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
11 dr(model->get_model()->get_nr()),
114
1/2
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 rp(model->get_model()->get_nr()),
115
3/6
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 11 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 11 times.
✗ Branch 13 not taken.
22 Arr_(Arr.rows(), Arr.cols()) {
116
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 dr.setZero();
117
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 rp.setZero();
118
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 Arr_.setZero();
119
1/2
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
11 data_0 = model->get_model()->createData();
120 11 const std::size_t nr = model->get_model()->get_nr();
121 11 data_rp.clear();
122
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 11 times.
60 for (std::size_t i = 0; i < nr; ++i) {
123
2/4
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 49 times.
✗ Branch 7 not taken.
49 data_rp.push_back(model->get_model()->createData());
124 }
125
126 11 data_r2p.clear();
127
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
55 for (std::size_t i = 0; i < 4; ++i) {
128
2/4
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 44 times.
✗ Branch 7 not taken.
44 data_r2p.push_back(model->get_model()->createData());
129 }
130 11 }
131
132 VectorXs dr; //!< disturbance: \f$ [\hdot \;\; disturbance \;\; \hdot] \f$
133 VectorXs rp; //!< The input + the disturbance on one DoF "\f$ r^+ = rp = \int
134 //!< r + dr \f$"
135 boost::shared_ptr<Base> data_0; //!< The data that contains the final results
136 std::vector<boost::shared_ptr<Base> >
137 data_rp; //!< The temporary data associated with the input variation
138 std::vector<boost::shared_ptr<Base> >
139 data_r2p; //!< The temporary data associated with the input variation
140
141 MatrixXs Arr_;
142 using Base::a_value;
143 using Base::Ar;
144 using Base::Arr;
145 };
146
147 } // namespace crocoddyl
148
149 /* --- Details -------------------------------------------------------------- */
150 /* --- Details -------------------------------------------------------------- */
151 /* --- Details -------------------------------------------------------------- */
152 #include "crocoddyl/core/numdiff/activation.hxx"
153
154 #endif // CROCODDYL_CORE_NUMDIFF_ACTIVATION_HPP_
155