version 3.8
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/istl/bvector.hh>
26
27// forward declaration
28namespace Dune {
29
30template<class... Args>
32
33} // end namespace Dune
34
35namespace Dumux {
36
41template<class DofVector, bool isScalar = Dune::IsNumber<DofVector>::value>
43
48template<class Scalar>
49class DofBackend<Scalar, true>
50{
51public:
52 using DofVector = Scalar;
53 using SizeType = std::size_t;
54
56 static SizeType size(const DofVector& d)
57 { return 1; }
58
61 { return 0.0; }
62
64 template<class OtherDofVector>
65 static void axpy(Scalar a, const OtherDofVector& x, DofVector& y)
66 { y += a*x; }
67};
68
78template<class Vector>
79class DofBackend<Vector, false>
80{
81public:
82 using DofVector = Vector;
83 using SizeType = std::size_t;
84
86 static SizeType size(const DofVector& d)
87 { return d.size(); }
88
91 { DofVector d; d.resize(size); return d; }
92
94 template<class OtherDofVector>
95 static void axpy(typename DofVector::field_type a, const OtherDofVector& x, DofVector& y)
96 {
97 for (typename DofVector::size_type i = 0; i < y.size(); ++i)
98 y[i].axpy(a, x[i]);
99 }
100};
101
106template<class... Blocks>
107class 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
114public:
115 using DofVector = DV;
116 using SizeType = VectorSizeInfo;
117
119 static SizeType size(const DofVector& d)
120 {
121 VectorSizeInfo result;
122 using namespace Dune::Hybrid;
123 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
124 result[i] = d[Dune::index_constant<i>{}].size();
125 });
126 return result;
127 }
128
130 static DofVector zeros(const SizeType& size)
131 {
132 DofVector result;
133 using namespace Dune::Hybrid;
134 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
135 result[Dune::index_constant<i>{}].resize(size[i]);
136 });
137 return result;
138 }
139
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 forEach(std::make_index_sequence<numBlocks>{}, [&](auto i) {
146 DofBackend<std::decay_t<decltype(y[Dune::index_constant<i>{}])>>::axpy(
147 a, x[Dune::index_constant<i>{}], y[Dune::index_constant<i>{}]
148 );
149 });
150 }
151};
152
153namespace Detail {
154
155template<class Vars>
156using SolutionVectorType = typename Vars::SolutionVector;
157
158template<class Vars, bool varsExportSolution>
160
167template<class Vars>
168class VariablesBackend<Vars, false>
169: public DofBackend<Vars>
170{
172
173public:
174 using Variables = Vars;
175 using typename ParentType::DofVector;
176
178 static void update(Variables& v, const DofVector& dofs)
179 { v = dofs; }
180
182 static const DofVector& dofs(const Variables& v)
183 { return v; }
184
186 static DofVector& dofs(Variables& v)
187 { return v; }
188};
189
195template<class Vars>
196class VariablesBackend<Vars, true>
197: public DofBackend<typename Vars::SolutionVector>
198{
199public:
200 using DofVector = typename Vars::SolutionVector;
201 using Variables = Vars;
202
204 static void update(Variables& v, const DofVector& dofs)
205 { v.update(dofs); }
206
208 static const DofVector& dofs(const Variables& v)
209 { return v.dofs(); }
210
213 { return v.dofs(); }
214};
215} // end namespace Detail
216
224template<class Vars>
226
227} // end namespace Dumux
228
229#endif
static void update(Variables &v, const DofVector &dofs)
update to new solution vector
Definition: variablesbackend.hh:178
Vars Variables
Definition: variablesbackend.hh:174
static const DofVector & dofs(const Variables &v)
return const reference to dof vector
Definition: variablesbackend.hh:182
static DofVector & dofs(Variables &v)
return reference to dof vector
Definition: variablesbackend.hh:186
static void update(Variables &v, const DofVector &dofs)
update to new solution vector
Definition: variablesbackend.hh:204
Vars Variables
the type of the variables object
Definition: variablesbackend.hh:201
static DofVector & dofs(Variables &v)
return reference to dof vector
Definition: variablesbackend.hh:212
static const DofVector & dofs(const Variables &v)
return const reference to dof vector
Definition: variablesbackend.hh:208
typename Vars::SolutionVector DofVector
Definition: variablesbackend.hh:200
Definition: variablesbackend.hh:159
static SizeType size(const DofVector &d)
Return the number of entries in the sub-dof-vectors.
Definition: variablesbackend.hh:119
static void axpy(Scalar a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:142
VectorSizeInfo SizeType
Definition: variablesbackend.hh:116
static DofVector zeros(const SizeType &size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:130
static SizeType size(const DofVector &d)
Return the number of entries in the dof vector.
Definition: variablesbackend.hh:56
static DofVector zeros(SizeType size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:60
std::size_t SizeType
Definition: variablesbackend.hh:53
Scalar DofVector
the type of the dofs parametrizing the variables object
Definition: variablesbackend.hh:52
static void axpy(Scalar a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:65
static SizeType size(const DofVector &d)
Return the number of entries in the dof vector.
Definition: variablesbackend.hh:86
static void axpy(typename DofVector::field_type a, const OtherDofVector &x, DofVector &y)
Perform axpy operation (y += a * x)
Definition: variablesbackend.hh:95
std::size_t SizeType
Definition: variablesbackend.hh:83
Vector DofVector
the type of the dofs parametrizing the variables object
Definition: variablesbackend.hh:82
static DofVector zeros(SizeType size)
Make a zero-initialized dof vector instance.
Definition: variablesbackend.hh:90
Class providing operations with primary variable vectors.
Definition: variablesbackend.hh:42
Definition: variablesbackend.hh:31
typename Vars::SolutionVector SolutionVectorType
Definition: variablesbackend.hh:156
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24