GCC Code Coverage Report


Directory: ./
File: include/types.hpp
Date: 2024-04-18 12:22:13
Exec Total Coverage
Lines: 27 28 96.4%
Functions: 29 34 85.3%
Branches: 24 37 64.9%

Line Branch Exec Source
1 #pragma once
2
3 #ifndef REAL_TYPE
4 #define REAL_TYPE double
5 #endif
6
7 #ifndef DIM
8 #define DIM 3
9 #endif
10
11 #include <eigen3/Eigen/Dense>
12 #include <yaml-cpp/yaml.h>
13
14 using Real = REAL_TYPE;
15 using Vec = Eigen::Matrix<Real, DIM, 1>;
16 using VecVec = Eigen::Matrix<Real, DIM, Eigen::Dynamic>;
17 using ArrayI = Eigen::Array<int, DIM, 1>;
18
19 /**
20 * Get the minimum image vector from position r1 to r2 with respect to a box of size L.
21 */
22 695206245 inline Vec r_ij(const Vec &L, const Vec &r1, const Vec &r2) {
23 695206245 Vec result;
24 695206245 result = r2 - r1;
25 695206245 result.array() -= (result.array() / L.array()).round() * L.array();
26 695206245 return result;
27 }
28
29 namespace YAML {
30
31 // The following enables yaml-cpp to natively encode and decode Eigen matrices and arrays.
32 // For example, one can now use node[key].as<Vec>()
33 template <template <typename, int...> class EigenClass, typename Scalar, int d>
34 struct convert<EigenClass<Scalar, d, 1>> {
35 20 static Node encode(const EigenClass<Scalar, d, 1> &rhs) {
36 20 Node node;
37
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (int i = 0; i < d; ++i) {
38
1/2
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 node.push_back(rhs[i]);
39 }
40 20 return node;
41 }
42
43 110 static bool decode(const Node &node, EigenClass<Scalar, d, 1> &rhs) {
44
2/4
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 106 times.
110 if (!node.IsSequence() || node.size() != d) {
45 return false;
46 }
47
48
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 106 times.
440 for (int i = 0; i < d; ++i) {
49
1/2
✓ Branch 2 taken 318 times.
✗ Branch 3 not taken.
330 rhs[i] = node[i].as<Scalar>();
50 }
51 110 return true;
52 }
53 };
54
55 // This helper class is needed because C++ forbids partial specialization of functions.
56 // In our case, we want to specialize the parse function to generic Eigen types.
57 template <typename ReturnT, typename DefaultT = ReturnT> struct ParseHelper {
58
1/2
✓ Branch 2 taken 473 times.
✗ Branch 3 not taken.
946 ReturnT operator()(const Node &node, const std::string &key) { return node[key].as<ReturnT>(); }
59 1468 ReturnT operator()(const Node &node, const std::string &key, DefaultT defaultValue) {
60
4/6
✓ Branch 1 taken 405 times.
✓ Branch 2 taken 329 times.
✓ Branch 4 taken 405 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 58 times.
✗ Branch 8 not taken.
1468 return node[key] ? (*this)(node, key) : defaultValue;
61 }
62 };
63
64 template <template <typename, int...> class EigenClass, typename Scalar1, int d, typename Scalar2>
65 struct ParseHelper<EigenClass<Scalar1, d, 1>, Scalar2> {
66 274 EigenClass<Scalar1, d, 1> operator()(const Node &node, const std::string &key) {
67
4/6
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 106 times.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 31 times.
✗ Branch 8 not taken.
822 return node[key].IsScalar() ? EigenClass<Scalar1, d, 1>::Constant(node[key].as<Scalar1>())
68
4/7
✓ Branch 3 taken 106 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 106 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 31 times.
✓ Branch 10 taken 106 times.
✗ Branch 13 not taken.
548 : node[key].as<EigenClass<Scalar1, d, 1>>();
69 }
70 84 EigenClass<Scalar1, d, 1> operator()(const Node &node, const std::string &key,
71 Scalar2 defaultValue) {
72
3/4
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 29 times.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
113 return node[key] ? (*this)(node, key) : EigenClass<Scalar1, d, 1>::Constant(defaultValue);
73 }
74 };
75
76 // Two helper methods are provided for easy and consistent parsing of yaml nodes (with and without
77 // setting defaults)
78 300 template <typename ReturnT> auto parse(const Node &node, const std::string &key) {
79 300 return ParseHelper<ReturnT>{}(node, key);
80 }
81
82 template <typename ReturnT, typename DefaultT>
83 818 auto parse(const Node &node, const std::string &key, DefaultT defaultValue) {
84 818 return ParseHelper<ReturnT, DefaultT>{}(node, key, defaultValue);
85 }
86
87 } // namespace YAML
88