GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/common/variablesbackend.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 18 27 66.7%
Functions: 244 361 67.6%
Branches: 53 109 48.6%

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/common/typetraits.hh>
26 #include <dune/istl/bvector.hh>
27
28 // forward declaration
29 namespace Dune {
30
31 template<class... Args>
32 class MultiTypeBlockVector;
33
34 } // end namespace Dune
35
36 namespace Dumux {
37
38 /*!
39 * \ingroup Core
40 * \brief Class providing operations with primary variable vectors
41 */
42 template<class DofVector, bool isScalar = Dune::IsNumber<DofVector>::value>
43 class DofBackend;
44
45 /*!
46 * \ingroup Core
47 * \brief Specialization providing operations for scalar/number types
48 */
49 template<class Scalar>
50 class DofBackend<Scalar, true>
51 {
52 public:
53 using DofVector = Scalar; //!< the type of the dofs parametrizing the variables object
54 using SizeType = std::size_t;
55
56 //! Return the number of entries in the dof vector
57 static SizeType size(const DofVector& d)
58 { return 1; }
59
60 //! Make a zero-initialized dof vector instance
61 static DofVector zeros(SizeType size)
62 { return 0.0; }
63
64 //! Perform axpy operation (y += a * x)
65 template<class OtherDofVector>
66 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
67
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 20 times.
105 { y += a*x; }
68 };
69
70 /*!
71 * \ingroup Core
72 * \brief Specialization providing operations for block vectors
73 * \tparam Vector a type that is
74 * - default-constructible
75 * - has size() member
76 * - has resize(0) member
77 * - has axpy(a, x) member
78 */
79 template<class Vector>
80 class DofBackend<Vector, false>
81 {
82 public:
83 using DofVector = Vector; //!< the type of the dofs parametrizing the variables object
84 using SizeType = std::size_t;
85
86 //! Return the number of entries in the dof vector
87 static SizeType size(const DofVector& d)
88
4/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 12888 times.
✗ Branch 7 not taken.
✓ Branch 11 taken 10 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 10 times.
✗ Branch 15 not taken.
17261 { return d.size(); }
89
90 //! Make a zero-initialized dof vector instance
91 15970 static DofVector zeros(SizeType size)
92
2/4
✓ Branch 1 taken 15970 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15970 times.
✗ Branch 5 not taken.
31940 { DofVector d; d.resize(size); return d; }
93
94 //! Perform axpy operation (y += a * x)
95 template<class OtherDofVector>
96 static void axpy(typename DofVector::field_type a, const OtherDofVector& x, DofVector& y)
97 {
98
12/12
✓ Branch 0 taken 19163460 times.
✓ Branch 1 taken 17836 times.
✓ Branch 2 taken 46562144 times.
✓ Branch 3 taken 45123 times.
✓ Branch 4 taken 36239320 times.
✓ Branch 5 taken 56317 times.
✓ Branch 6 taken 10210582 times.
✓ Branch 7 taken 22788 times.
✓ Branch 8 taken 376839 times.
✓ Branch 9 taken 51 times.
✓ Branch 10 taken 147780 times.
✓ Branch 11 taken 20 times.
112842260 for (typename DofVector::size_type i = 0; i < y.size(); ++i)
99 {
100 if constexpr (Dune::IsNumber<std::decay_t<decltype(y[0])>>::value)
101 y[i] += a*x[i];
102 else
103 450800500 y[i].axpy(a, x[i]);
104 }
105 }
106 };
107
108 /*!
109 * \ingroup Core
110 * \brief Specialization providing operations for multitype block vectors
111 */
112 template<class... Blocks>
113 class DofBackend<Dune::MultiTypeBlockVector<Blocks...>, false>
114 {
115 using DV = Dune::MultiTypeBlockVector<Blocks...>;
116 static constexpr auto numBlocks = DV::size();
117
118 using VectorSizeInfo = std::array<std::size_t, numBlocks>;
119
120 public:
121 using DofVector = DV; //!< the type of the dofs parametrizing the variables object
122 using SizeType = VectorSizeInfo;
123
124 //! Return the number of entries in the sub-dof-vectors
125 static SizeType size(const DofVector& d)
126 {
127 VectorSizeInfo result;
128 using namespace Dune::Hybrid;
129
6/13
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2057 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 2057 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
4180 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
130
12/32
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1840 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2077 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2090 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2090 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 2070 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 2070 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 250 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 250 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 237 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 237 times.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
13251 result[i] = d[Dune::index_constant<i>{}].size();
131 });
132 return result;
133 }
134
135 //! Make a zero-initialized dof vector instance
136 2090 static DofVector zeros(const SizeType& size)
137 {
138
1/2
✓ Branch 1 taken 2090 times.
✗ Branch 2 not taken.
2090 DofVector result;
139 using namespace Dune::Hybrid;
140
1/2
✓ Branch 1 taken 2090 times.
✗ Branch 2 not taken.
2090 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
141 result[Dune::index_constant<i>{}].resize(size[i]);
142 });
143 2090 return result;
144 }
145
146 //! Perform axpy operation (y += a * x)
147 template<class Scalar, class OtherDofVector, std::enable_if_t< Dune::IsNumber<Scalar>::value, int> = 0>
148 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
149 {
150 using namespace Dune::Hybrid;
151
3/8
✓ Branch 2 taken 638 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 6046 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 38 times.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
81848 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
152 56488 DofBackend<std::decay_t<decltype(y[Dune::index_constant<i>{}])>>::axpy(
153 84732 a, x[Dune::index_constant<i>{}], y[Dune::index_constant<i>{}]
154 );
155 });
156 }
157 };
158
159 namespace Detail {
160
161 template<class Vars>
162 using SolutionVectorType = typename Vars::SolutionVector;
163
164 template<class Vars, bool varsExportSolution>
165 class VariablesBackend;
166
167 /*!
168 * \ingroup Core
169 * \brief Class providing operations for primary variable vector/scalar types
170 * \note We assume the variables being simply a dof vector if we
171 * do not find the variables class to export `SolutionVector`.
172 */
173 template<class Vars>
174 class VariablesBackend<Vars, false>
175 : public DofBackend<Vars>
176 {
177 using ParentType = DofBackend<Vars>;
178
179 public:
180 using Variables = Vars;
181 using typename ParentType::DofVector;
182
183 //! update to new solution vector
184 static void update(Variables& v, const DofVector& dofs)
185
2/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2985 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
70572 { v = dofs; }
186
187 //! return const reference to dof vector
188 static const DofVector& dofs(const Variables& v)
189 { return v; }
190
191 //! return reference to dof vector
192 static DofVector& dofs(Variables& v)
193 { return v; }
194 };
195
196 /*!
197 * \ingroup Core
198 * \brief Class providing operations for generic variable classes,
199 * containing primary and possibly also secondary variables.
200 */
201 template<class Vars>
202 class VariablesBackend<Vars, true>
203 : public DofBackend<typename Vars::SolutionVector>
204 {
205 public:
206 using DofVector = typename Vars::SolutionVector;
207 using Variables = Vars; //!< the type of the variables object
208
209 //! update to new solution vector
210 static void update(Variables& v, const DofVector& dofs)
211 73 { v.update(dofs); }
212
213 //! return const reference to dof vector
214 static const DofVector& dofs(const Variables& v)
215 { return v.dofs(); }
216
217 //! return reference to dof vector
218 static DofVector& dofs(Variables& v)
219
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(); }
220 };
221 } // end namespace Detail
222
223 /*!
224 * \ingroup Core
225 * \brief Class providing operations for generic variable classes
226 * that represent the state of a numerical solution, possibly
227 * consisting of primary/secondary variables and information on
228 * the time level.
229 */
230 template<class Vars>
231 using VariablesBackend = Detail::VariablesBackend<Vars, Dune::Std::is_detected_v<Detail::SolutionVectorType, Vars>>;
232
233 } // end namespace Dumux
234
235 #endif
236