GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/numdiff/activation.hxx Lines: 31 33 93.9 %
Date: 2024-02-13 11:12:33 Branches: 21 72 29.2 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh,
5
//                          New York University, Max Planck Gesellschaft
6
// Copyright note valid unless otherwise stated in individual files.
7
// All rights reserved.
8
///////////////////////////////////////////////////////////////////////////////
9
10
#include "crocoddyl/core/utils/exception.hpp"
11
12
namespace crocoddyl {
13
14
template <typename Scalar>
15
10
ActivationModelNumDiffTpl<Scalar>::ActivationModelNumDiffTpl(
16
    boost::shared_ptr<Base> model)
17
    : Base(model->get_nr()),
18
      model_(model),
19
10
      e_jac_(std::sqrt(2.0 * std::numeric_limits<Scalar>::epsilon())) {}
20
21
template <typename Scalar>
22
24
ActivationModelNumDiffTpl<Scalar>::~ActivationModelNumDiffTpl() {}
23
24
template <typename Scalar>
25
9
void ActivationModelNumDiffTpl<Scalar>::calc(
26
    const boost::shared_ptr<ActivationDataAbstract>& data,
27
    const Eigen::Ref<const VectorXs>& r) {
28

9
  if (static_cast<std::size_t>(r.size()) != model_->get_nr()) {
29
    throw_pretty("Invalid argument: "
30
                 << "r has wrong dimension (it should be " +
31
                        std::to_string(model_->get_nr()) + ")");
32
  }
33
9
  boost::shared_ptr<Data> data_nd = boost::static_pointer_cast<Data>(data);
34
9
  model_->calc(data_nd->data_0, r);
35
9
  data->a_value = data_nd->data_0->a_value;
36
9
}
37
38
template <typename Scalar>
39
9
void ActivationModelNumDiffTpl<Scalar>::calcDiff(
40
    const boost::shared_ptr<ActivationDataAbstract>& data,
41
    const Eigen::Ref<const VectorXs>& r) {
42

9
  if (static_cast<std::size_t>(r.size()) != model_->get_nr()) {
43
    throw_pretty("Invalid argument: "
44
                 << "r has wrong dimension (it should be " +
45
                        std::to_string(model_->get_nr()) + ")");
46
  }
47
9
  boost::shared_ptr<Data> data_nd = boost::static_pointer_cast<Data>(data);
48
49
9
  const Scalar a_value0 = data_nd->data_0->a_value;
50
9
  data->a_value = data_nd->data_0->a_value;
51
9
  const std::size_t nr = model_->get_nr();
52
53
  // Computing the d activation(r) / dr
54
9
  const Scalar rh_jac = e_jac_ * std::max(1., r.norm());
55
9
  data_nd->rp = r;
56
54
  for (unsigned int i_r = 0; i_r < nr; ++i_r) {
57
45
    data_nd->rp(i_r) += rh_jac;
58

45
    model_->calc(data_nd->data_rp[i_r], data_nd->rp);
59
45
    data_nd->rp(i_r) -= rh_jac;
60
45
    data->Ar(i_r) = (data_nd->data_rp[i_r]->a_value - a_value0) / rh_jac;
61
  }
62
63
  // Computing the d^2 action(r) / dr^2
64


9
  data_nd->Arr_.noalias() = data->Ar * data->Ar.transpose();
65

9
  data->Arr.diagonal() = data_nd->Arr_.diagonal();
66
9
}
67
68
template <typename Scalar>
69
boost::shared_ptr<ActivationDataAbstractTpl<Scalar> >
70
11
ActivationModelNumDiffTpl<Scalar>::createData() {
71
11
  return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
72
}
73
74
template <typename Scalar>
75
const boost::shared_ptr<ActivationModelAbstractTpl<Scalar> >&
76
137
ActivationModelNumDiffTpl<Scalar>::get_model() const {
77
137
  return model_;
78
}
79
80
template <typename Scalar>
81
9
const Scalar ActivationModelNumDiffTpl<Scalar>::get_disturbance() const {
82
9
  return e_jac_;
83
}
84
85
template <typename Scalar>
86
void ActivationModelNumDiffTpl<Scalar>::set_disturbance(
87
    const Scalar disturbance) {
88
  if (disturbance < 0.) {
89
    throw_pretty("Invalid argument: "
90
                 << "Disturbance constant is positive");
91
  }
92
  e_jac_ = disturbance;
93
}
94
95
}  // namespace crocoddyl