GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/common/variablesbackend.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 18 27 66.7%
Functions: 240 357 67.2%
Branches: 60 109 55.0%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup Core
10 * \brief Backends for operations on different solution vector types
11 * or more generic variable classes to be used in places where
12 * several different types/layouts should be supported.
13 */
14 #ifndef DUMUX_COMMON_VARIABLES_BACKEND_HH
15 #define DUMUX_COMMON_VARIABLES_BACKEND_HH
16
17 #include <array>
18 #include <utility>
19 #include <type_traits>
20
21 #include <dune/common/indices.hh>
22 #include <dune/common/typetraits.hh>
23 #include <dune/common/hybridutilities.hh>
24 #include <dune/common/std/type_traits.hh>
25 #include <dune/istl/bvector.hh>
26
27 // forward declaration
28 namespace Dune {
29
30 template<class... Args>
31 class MultiTypeBlockVector;
32
33 } // end namespace Dune
34
35 namespace Dumux {
36
37 /*!
38 * \ingroup Core
39 * \brief Class providing operations with primary variable vectors
40 */
41 template<class DofVector, bool isScalar = Dune::IsNumber<DofVector>::value>
42 class DofBackend;
43
44 /*!
45 * \ingroup Core
46 * \brief Specialization providing operations for scalar/number types
47 */
48 template<class Scalar>
49 class DofBackend<Scalar, true>
50 {
51 public:
52 using DofVector = Scalar; //!< the type of the dofs parametrizing the variables object
53 using SizeType = std::size_t;
54
55 //! Return the number of entries in the dof vector
56 static SizeType size(const DofVector& d)
57 { return 1; }
58
59 //! Make a zero-initialized dof vector instance
60 static DofVector zeros(SizeType size)
61 { return 0.0; }
62
63 //! Perform axpy operation (y += a * x)
64 template<class OtherDofVector>
65 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
66
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 20 times.
108 { y += a*x; }
67 };
68
69 /*!
70 * \ingroup Core
71 * \brief Specialization providing operations for block vectors
72 * \tparam Vector a type that is
73 * - default-constructible
74 * - has size() member
75 * - has resize(0) member
76 * - has axpy(a, x) member
77 */
78 template<class Vector>
79 class DofBackend<Vector, false>
80 {
81 public:
82 using DofVector = Vector; //!< the type of the dofs parametrizing the variables object
83 using SizeType = std::size_t;
84
85 //! Return the number of entries in the dof vector
86 static SizeType size(const DofVector& d)
87
4/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 52 times.
✓ Branch 6 taken 12929 times.
✗ Branch 7 not taken.
✓ Branch 11 taken 10 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 10 times.
✗ Branch 15 not taken.
17308 { return d.size(); }
88
89 //! Make a zero-initialized dof vector instance
90 16015 static DofVector zeros(SizeType size)
91
2/4
✓ Branch 1 taken 16015 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 16015 times.
✗ Branch 5 not taken.
32030 { DofVector d; d.resize(size); return d; }
92
93 //! Perform axpy operation (y += a * x)
94 template<class OtherDofVector>
95 static void axpy(typename DofVector::field_type a, const OtherDofVector& x, DofVector& y)
96 {
97
12/12
✓ Branch 0 taken 20622658 times.
✓ Branch 1 taken 19090 times.
✓ Branch 2 taken 55641357 times.
✓ Branch 3 taken 46472 times.
✓ Branch 4 taken 36254733 times.
✓ Branch 5 taken 56510 times.
✓ Branch 6 taken 10212262 times.
✓ Branch 7 taken 22798 times.
✓ Branch 8 taken 376839 times.
✓ Branch 9 taken 51 times.
✓ Branch 10 taken 147780 times.
✓ Branch 11 taken 20 times.
123400570 for (typename DofVector::size_type i = 0; i < y.size(); ++i)
98 493022516 y[i].axpy(a, x[i]);
99 }
100 };
101
102 /*!
103 * \ingroup Core
104 * \brief Specialization providing operations for multitype block vectors
105 */
106 template<class... Blocks>
107 class DofBackend<Dune::MultiTypeBlockVector<Blocks...>, false>
108 {
109 using DV = Dune::MultiTypeBlockVector<Blocks...>;
110 static constexpr auto numBlocks = DV::size();
111
112 using VectorSizeInfo = std::array<std::size_t, numBlocks>;
113
114 public:
115 using DofVector = DV; //!< the type of the dofs parametrizing the variables object
116 using SizeType = VectorSizeInfo;
117
118 //! Return the number of entries in the sub-dof-vectors
119 static SizeType size(const DofVector& d)
120 {
121 VectorSizeInfo result;
122 using namespace Dune::Hybrid;
123
8/13
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2296 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2296 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
4644 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
124
16/32
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2079 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2316 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2320 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2320 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 2300 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 2300 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 241 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 241 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 239 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 239 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 2 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 2 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 2 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 2 times.
✗ Branch 47 not taken.
14643 result[i] = d[Dune::index_constant<i>{}].size();
125 });
126 return result;
127 }
128
129 //! Make a zero-initialized dof vector instance
130 2322 static DofVector zeros(const SizeType& size)
131 {
132
1/2
✓ Branch 1 taken 2322 times.
✗ Branch 2 not taken.
2322 DofVector result;
133 using namespace Dune::Hybrid;
134
1/2
✓ Branch 1 taken 2322 times.
✗ Branch 2 not taken.
2322 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
135 result[Dune::index_constant<i>{}].resize(size[i]);
136 });
137 2322 return result;
138 }
139
140 //! Perform axpy operation (y += a * x)
141 template<class Scalar, class OtherDofVector, std::enable_if_t< Dune::IsNumber<Scalar>::value, int> = 0>
142 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
143 {
144 using namespace Dune::Hybrid;
145
4/8
✓ Branch 2 taken 1240 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 6093 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 16 times.
✗ Branch 15 not taken.
✓ Branch 18 taken 2 times.
✗ Branch 19 not taken.
89108 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
146 61328 DofBackend<std::decay_t<decltype(y[Dune::index_constant<i>{}])>>::axpy(
147 91992 a, x[Dune::index_constant<i>{}], y[Dune::index_constant<i>{}]
148 );
149 });
150 }
151 };
152
153 namespace Detail {
154
155 template<class Vars>
156 using SolutionVectorType = typename Vars::SolutionVector;
157
158 template<class Vars, bool varsExportSolution>
159 class VariablesBackend;
160
161 /*!
162 * \ingroup Core
163 * \brief Class providing operations for primary variable vector/scalar types
164 * \note We assume the variables being simply a dof vector if we
165 * do not find the variables class to export `SolutionVector`.
166 */
167 template<class Vars>
168 class VariablesBackend<Vars, false>
169 : public DofBackend<Vars>
170 {
171 using ParentType = DofBackend<Vars>;
172
173 public:
174 using Variables = Vars;
175 using typename ParentType::DofVector;
176
177 //! update to new solution vector
178 static void update(Variables& v, const DofVector& dofs)
179
2/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2985 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
72023 { v = dofs; }
180
181 //! return const reference to dof vector
182 static const DofVector& dofs(const Variables& v)
183 { return v; }
184
185 //! return reference to dof vector
186 static DofVector& dofs(Variables& v)
187 { return v; }
188 };
189
190 /*!
191 * \ingroup Core
192 * \brief Class providing operations for generic variable classes,
193 * containing primary and possibly also secondary variables.
194 */
195 template<class Vars>
196 class VariablesBackend<Vars, true>
197 : public DofBackend<typename Vars::SolutionVector>
198 {
199 public:
200 using DofVector = typename Vars::SolutionVector;
201 using Variables = Vars; //!< the type of the variables object
202
203 //! update to new solution vector
204 static void update(Variables& v, const DofVector& dofs)
205 73 { v.update(dofs); }
206
207 //! return const reference to dof vector
208 static const DofVector& dofs(const Variables& v)
209 { return v.dofs(); }
210
211 //! return reference to dof vector
212 static DofVector& dofs(Variables& v)
213
8/19
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 15 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 15 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 15 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 38 times.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
178 { return v.dofs(); }
214 };
215 } // end namespace Detail
216
217 /*!
218 * \ingroup Core
219 * \brief Class providing operations for generic variable classes
220 * that represent the state of a numerical solution, possibly
221 * consisting of primary/secondary variables and information on
222 * the time level.
223 */
224 template<class Vars>
225 using VariablesBackend = Detail::VariablesBackend<Vars, Dune::Std::is_detected_v<Detail::SolutionVectorType, Vars>>;
226
227 } // end namespace Dumux
228
229 #endif
230