version 3.10-dev
variablesbackend.hh
Go to the documentation of this file.
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//
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
29
30// forward declaration
31namespace Dune {
32
33template<class... Args>
35
36} // end namespace Dune
37
39
41{
42 template<class V>
43 auto operator()(const V& v) -> decltype(std::declval<V>().resize(0))
44 {}
45};
46
47template<class Vector>
48static constexpr auto hasResize()
49{ return decltype( isValid(HasResize())(std::declval<Vector>()) )::value; }
50
51} // end namespace Dumux::Detail::VariablesBackend
52
53namespace Dumux {
54
59template<class DofVector, bool isScalar = Dune::IsNumber<DofVector>::value>
61
66template<class Scalar>
67class DofBackend<Scalar, true>
68{
69public:
70 using DofVector = Scalar;
71 using SizeType = std::size_t;
72
74 static SizeType size(const DofVector& d)
75 { return 1; }
76
79 { return 0.0; }
80
82 template<class OtherDofVector>
83 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
84 { y += a*x; }
85};
86
96template<class Vector>
97class DofBackend<Vector, false>
98{
99public:
100 using DofVector = Vector;
101 using SizeType = std::size_t;
102
104 static SizeType size(const DofVector& d)
105 { return d.size(); }
106
109 {
110 DofVector d;
111 if constexpr (Detail::DofBackend::hasResize<Vector>())
112 d.resize(size);
113 return d;
114 }
115
117 template<class OtherDofVector>
118 static void axpy(typename DofVector::field_type a, const OtherDofVector& x, DofVector& y)
119 {
120 for (typename DofVector::size_type i = 0; i < y.size(); ++i)
121 {
122 if constexpr (Dune::IsNumber<std::decay_t<decltype(y[0])>>::value)
123 y[i] += a*x[i];
124 else
125 y[i].axpy(a, x[i]);
126 }
127 }
128};
129
134template<class... Blocks>
135class DofBackend<Dune::MultiTypeBlockVector<Blocks...>, false>
136{
137 using DV = Dune::MultiTypeBlockVector<Blocks...>;
138 static constexpr auto numBlocks = DV::size();
139
140 using VectorSizeInfo = std::array<std::size_t, numBlocks>;
141
142public:
143 using DofVector = DV;
144 using SizeType = VectorSizeInfo;
145
147 static SizeType size(const DofVector& d)
148 {
149 VectorSizeInfo result;
150 using namespace Dune::Hybrid;
151 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
152 result[i] = d[Dune::index_constant<i>{}].size();
153 });
154 return result;
155 }
156
158 static DofVector zeros(const SizeType& size)
159 {
160 DofVector result;
161 using namespace Dune::Hybrid;
162 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
163 result[Dune::index_constant<i>{}].resize(size[i]);
164 });
165 return result;
166 }
167
169 template<class Scalar, class OtherDofVector, std::enable_if_t< Dune::IsNumber<Scalar>::value, int> = 0>
170 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
171 {
172 using namespace Dune::Hybrid;
173 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
174 DofBackend<std::decay_t<decltype(y[Dune::index_constant<i>{}])>>::axpy(
175 a, x[Dune::index_constant<i>{}], y[Dune::index_constant<i>{}]
176 );
177 });
178 }
179};
180
181namespace Detail {
182
183template<class Vars>
184using SolutionVectorType = typename Vars::SolutionVector;
185
186template<class Vars, bool varsExportSolution>
188
195template<class Vars>
196class VariablesBackend<Vars, false>
197: public Dumux::DofBackend<Vars>
198{
200
201public:
202 using Variables = Vars;
203 using typename ParentType::DofVector;
204
206 static void update(Variables& v, const DofVector& dofs)
207 { v = dofs; }
208
210 static const DofVector& dofs(const Variables& v)
211 { return v; }
212
214 static DofVector& dofs(Variables& v)
215 { return v; }
216};
217
223template<class Vars>
224class VariablesBackend<Vars, true>
225: public Dumux::DofBackend<typename Vars::SolutionVector>
226{
227public:
228 using DofVector = typename Vars::SolutionVector;
229 using Variables = Vars;
230
232 static void update(Variables& v, const DofVector& dofs)
233 { v.update(dofs); }
234
236 static const DofVector& dofs(const Variables& v)
237 { return v.dofs(); }
238
241 { return v.dofs(); }
242};
243} // end namespace Detail
244
252template<class Vars>
254
255} // end namespace Dumux
256
257#endif
static void update(Variables &v, const DofVector &dofs)
update to new solution vector
Definition: variablesbackend.hh:206
Vars Variables
Definition: variablesbackend.hh:202
static const DofVector & dofs(const Variables &v)
return const reference to dof vector
Definition: variablesbackend.hh:210
static DofVector & dofs(Variables &v)
return reference to dof vector
Definition: variablesbackend.hh:214
static void update(Variables &v, const DofVector &dofs)
update to new solution vector
Definition: variablesbackend.hh:232
Vars Variables
the type of the variables object
Definition: variablesbackend.hh:229
static DofVector & dofs(Variables &v)
return reference to dof vector
Definition: variablesbackend.hh:240
static const DofVector & dofs(const Variables &v)
return const reference to dof vector
Definition: variablesbackend.hh:236
typename Vars::SolutionVector DofVector
Definition: variablesbackend.hh:228
Definition: variablesbackend.hh:187
static SizeType size(const DofVector &d)
Return the number of entries in the sub-dof-vectors.
Definition: variablesbackend.hh:147
static void axpy(Scalar a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:170
VectorSizeInfo SizeType
Definition: variablesbackend.hh:144
static DofVector zeros(const SizeType &size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:158
static SizeType size(const DofVector &d)
Return the number of entries in the dof vector.
Definition: variablesbackend.hh:74
static DofVector zeros(SizeType size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:78
std::size_t SizeType
Definition: variablesbackend.hh:71
Scalar DofVector
the type of the dofs parametrizing the variables object
Definition: variablesbackend.hh:70
static void axpy(Scalar a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:83
static SizeType size(const DofVector &d)
Return the number of entries in the dof vector.
Definition: variablesbackend.hh:104
static void axpy(typename DofVector::field_type a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:118
std::size_t SizeType
Definition: variablesbackend.hh:101
Vector DofVector
the type of the dofs parametrizing the variables object
Definition: variablesbackend.hh:100
static DofVector zeros(SizeType size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:108
Class providing operations with primary variable vectors.
Definition: variablesbackend.hh:60
Definition: variablesbackend.hh:34
constexpr auto isValid(const Expression &t)
A function that creates a test functor to do class member introspection at compile time.
Definition: isvalid.hh:81
A helper function for class member function introspection.
Definition: variablesbackend.hh:38
static constexpr auto hasResize()
Definition: variablesbackend.hh:48
typename Vars::SolutionVector SolutionVectorType
Definition: variablesbackend.hh:184
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24
Definition: variablesbackend.hh:41
auto operator()(const V &v) -> decltype(std::declval< V >().resize(0))
Definition: variablesbackend.hh:43