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-FileCopyrightText: 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 CVFEDiscretization | ||
10 | * \brief The element variables class | ||
11 | */ | ||
12 | #ifndef DUMUX_DISCRETIZATION_CVFE_ELEMENT_VARIABLES_HH | ||
13 | #define DUMUX_DISCRETIZATION_CVFE_ELEMENT_VARIABLES_HH | ||
14 | |||
15 | #include <type_traits> | ||
16 | #include <utility> | ||
17 | #include <vector> | ||
18 | |||
19 | #include <dumux/common/typetraits/localdofs_.hh> | ||
20 | #include <dumux/discretization/elementsolution.hh> | ||
21 | |||
22 | #include "variablesdeflectionpolicy.hh" | ||
23 | |||
24 | namespace Dumux::Detail::CVFE { | ||
25 | |||
26 | /*! | ||
27 | * \ingroup CVFEDiscretization | ||
28 | * \brief The (stencil) element variables class for control-volume finite element | ||
29 | * \note The class is specialized for versions with and without caching | ||
30 | * \tparam GVC the grid variables cache type | ||
31 | * \tparam cachingEnabled if the cache is enabled | ||
32 | */ | ||
33 | template<class GVC, bool cachingEnabled> | ||
34 | class CVFEElementVariables; | ||
35 | |||
36 | /*! | ||
37 | * \ingroup CVFEDiscretization | ||
38 | * \brief The (stencil) element variables class for control-volume finite element with caching | ||
39 | * \note the variables are stored for the whole grid view in the corresponding GridVariables class | ||
40 | */ | ||
41 | template<class GVC> | ||
42 | class CVFEElementVariables<GVC, /*cachingEnabled*/true> | ||
43 | { | ||
44 | class MutableVariablesView | ||
45 | { | ||
46 | public: | ||
47 | 285056 | MutableVariablesView(GVC& gridCache) | |
48 | : gridCache_(gridCache) {} | ||
49 | |||
50 | using Variables = typename GVC::VolumeVariables; | ||
51 | |||
52 | template<class LocalDof> | ||
53 | 6198080 | Variables& operator [](const LocalDof& localDof) const | |
54 | 6198080 | { return gridCache_.volVars(localDof.elementIndex(), localDof.index()); } | |
55 | private: | ||
56 | GVC& gridCache_; | ||
57 | }; | ||
58 | |||
59 | public: | ||
60 | //! export type of the grid variables | ||
61 | using GridVariablesCache = GVC; | ||
62 | //! TODO: Delete after new variables concept has been implemented | ||
63 | //! this is currently needed to support old interface | ||
64 | using GridVolumeVariables = GridVariablesCache; | ||
65 | |||
66 | //! export type of the mutable version of the view | ||
67 | using MutableView = MutableVariablesView; | ||
68 | |||
69 | //! export type of the variables | ||
70 | using Variables = typename GridVariablesCache::VolumeVariables; | ||
71 | //! TODO: Delete after new variables concept has been implemented | ||
72 | //! this is currently needed to support old interface | ||
73 | using VolumeVariables = Variables; | ||
74 | |||
75 | //! export type of deflection policy | ||
76 | template<class FVElementGeometry> | ||
77 | using DeflectionPolicy = VariablesDeflectionPolicy<MutableView, FVElementGeometry>; | ||
78 | |||
79 | //! Constructor | ||
80 |
2/4✓ Branch 2 taken 12001 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
125967 | CVFEElementVariables(const GridVariablesCache& gridVariablesCache) |
81 | : gridVariablesCachePtr_(&gridVariablesCache) {} | ||
82 | |||
83 | 90902256 | const Variables& operator [](std::size_t localDofIdx) const | |
84 | 90902256 | { return gridVolVars().volVars(eIdx_, localDofIdx); } | |
85 | |||
86 | template<class ScvOrLocalDof, typename std::enable_if_t<!std::is_integral<ScvOrLocalDof>::value, int> = 0> | ||
87 | 279199126 | const Variables& operator [](const ScvOrLocalDof& scvOrLocalDof) const | |
88 | { | ||
89 | if constexpr (Dumux::Detail::LocalDofs::isLocalDofType<ScvOrLocalDof>()) | ||
90 | return gridVolVars().volVars(eIdx_, scvOrLocalDof.index()); | ||
91 | else | ||
92 | 273087890 | return gridVolVars().volVars(eIdx_, scvOrLocalDof.indexInElement()); | |
93 | } | ||
94 | |||
95 | /*! | ||
96 | * \brief bind the local view (r-value overload) | ||
97 | * This overload is called when an instance of this class is a temporary in the usage context | ||
98 | * This allows a usage like this: `const auto view = localView(...).bind(element);` | ||
99 | */ | ||
100 | template<class FVElementGeometry, class SolutionVector> | ||
101 | 101960 | CVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | |
102 | const FVElementGeometry& fvGeometry, | ||
103 | const SolutionVector& sol) && | ||
104 | { | ||
105 | 101960 | this->bindElement(element, fvGeometry, sol); | |
106 | return std::move(*this); | ||
107 | } | ||
108 | |||
109 | // For compatibility reasons with the case of not storing the variables. | ||
110 | // function to be called before assembling an element, preparing the variables within the stencil | ||
111 | template<class FVElementGeometry, class SolutionVector> | ||
112 | 560764 | void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | |
113 | const FVElementGeometry& fvGeometry, | ||
114 | const SolutionVector& sol) & | ||
115 | { | ||
116 | 560764 | bindElement(element, fvGeometry, sol); | |
117 | } | ||
118 | |||
119 | /*! | ||
120 | * \brief bind the local view (r-value overload) | ||
121 | * This overload is called when an instance of this class is a temporary in the usage context | ||
122 | * This allows a usage like this: `const auto view = localView(...).bind(element);` | ||
123 | */ | ||
124 | template<class FVElementGeometry, class SolutionVector> | ||
125 | CVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | ||
126 | const FVElementGeometry& fvGeometry, | ||
127 | const SolutionVector& sol) && | ||
128 | { | ||
129 | this->bindElement(element, fvGeometry, sol); | ||
130 | return std::move(*this); | ||
131 | } | ||
132 | |||
133 | // function to prepare the variables within the element | ||
134 | template<class FVElementGeometry, class SolutionVector> | ||
135 |
3/5✓ Branch 3 taken 12000 times.
✓ Branch 4 taken 5052 times.
✓ Branch 6 taken 9600 times.
✗ Branch 7 not taken.
✗ Branch 5 not taken.
|
662724 | void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, |
136 | const FVElementGeometry& fvGeometry, | ||
137 | const SolutionVector& sol) & | ||
138 | { | ||
139 |
6/10✓ Branch 4 taken 12000 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12000 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 9600 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 9600 times.
✗ Branch 14 not taken.
✓ Branch 6 taken 5052 times.
✓ Branch 9 taken 5052 times.
|
662724 | eIdx_ = fvGeometry.gridGeometry().elementMapper().index(element); |
140 | ✗ | } | |
141 | |||
142 | //! The grid variables cache object we are a restriction of | ||
143 | //! TODO: Rename after new variables concept has been implemented | ||
144 | //! this is currently needed to support old interface | ||
145 | 370063672 | const GridVariablesCache& gridVolVars() const | |
146 |
2/2✓ Branch 0 taken 11427850 times.
✓ Branch 1 taken 13527990 times.
|
370063672 | { return *gridVariablesCachePtr_; } |
147 | |||
148 | /*! | ||
149 | * \brief return a local view on variables that is always mutable, regardless of the caching policy | ||
150 | * \pre bind has to be called before, otherwise using the view will result in undefined behavior | ||
151 | */ | ||
152 | 285056 | MutableView asMutableView(GridVariablesCache& gridVars) | |
153 | 285056 | { return { gridVars }; } | |
154 | |||
155 | private: | ||
156 | const GridVariablesCache* gridVariablesCachePtr_; | ||
157 | std::size_t eIdx_; | ||
158 | }; | ||
159 | |||
160 | /*! | ||
161 | * \ingroup CVFEDiscretization | ||
162 | * \brief The local (stencil) element variables class for control-volume finite element without caching | ||
163 | */ | ||
164 | template<class GVC> | ||
165 |
2/14✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 6 taken 42900 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
|
85820 | class CVFEElementVariables<GVC, /*cachingEnabled*/false> |
166 | { | ||
167 | using ThisType = CVFEElementVariables<GVC, /*cachingEnabled*/false>; | ||
168 | |||
169 | class MutableVariablesView | ||
170 | { | ||
171 | public: | ||
172 | 42300 | MutableVariablesView(ThisType& view) | |
173 | : view_(view) {} | ||
174 | |||
175 | using Variables = typename GVC::VolumeVariables; | ||
176 | |||
177 | template<class LocalDof> | ||
178 | 1053900 | Variables& operator [](const LocalDof& localDof) const | |
179 | 1053900 | { return view_[localDof]; } | |
180 | private: | ||
181 | ThisType& view_; | ||
182 | }; | ||
183 | |||
184 | public: | ||
185 | //! export type of the grid variables | ||
186 | using GridVariablesCache = GVC; | ||
187 | //! TODO: Delete after new variables concept has been implemented | ||
188 | //! this is currently needed to support old interface | ||
189 | using GridVolumeVariables = GridVariablesCache; | ||
190 | |||
191 | //! export type of the mutable version of the view | ||
192 | using MutableView = MutableVariablesView; | ||
193 | |||
194 | //! export type of the variables | ||
195 | using Variables = typename GridVariablesCache::VolumeVariables; | ||
196 | //! TODO: Delete after new variables concept has been implemented | ||
197 | //! this is currently needed to support old interface | ||
198 | using VolumeVariables = Variables; | ||
199 | |||
200 | //! export type of deflection policy | ||
201 | template<class FVElementGeometry> | ||
202 | using DeflectionPolicy = VariablesDeflectionPolicy<MutableView, FVElementGeometry>; | ||
203 | |||
204 | //! Constructor | ||
205 | 42910 | CVFEElementVariables(const GridVariablesCache& gridVarsCache) | |
206 |
2/6✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42900 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
42910 | : gridVariablesCachePtr_(&gridVarsCache) {} |
207 | |||
208 | /*! | ||
209 | * \brief bind the local view (r-value overload) | ||
210 | * This overload is called when an instance of this class is a temporary in the usage context | ||
211 | * This allows a usage like this: `const auto view = localView(...).bind(element);` | ||
212 | */ | ||
213 | template<class FVElementGeometry, class SolutionVector> | ||
214 | CVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | ||
215 | const FVElementGeometry& fvGeometry, | ||
216 | const SolutionVector& sol) && | ||
217 | { | ||
218 | this->bindElement(element, fvGeometry, sol); | ||
219 | return std::move(*this); | ||
220 | } | ||
221 | |||
222 | // specialization for control-volume finite element, simply forwards to the bindElement method | ||
223 | template<class FVElementGeometry, class SolutionVector> | ||
224 | 42900 | void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | |
225 | const FVElementGeometry& fvGeometry, | ||
226 | const SolutionVector& sol) & | ||
227 | { | ||
228 |
0/4✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
42900 | bindElement(element, fvGeometry, sol); |
229 | ✗ | } | |
230 | |||
231 | /*! | ||
232 | * \brief bind the local view (r-value overload) | ||
233 | * This overload is called when an instance of this class is a temporary in the usage context | ||
234 | * This allows a usage like this: `const auto view = localView(...).bind(element);` | ||
235 | */ | ||
236 | template<class FVElementGeometry, class SolutionVector> | ||
237 | CVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | ||
238 | const FVElementGeometry& fvGeometry, | ||
239 | const SolutionVector& sol) && | ||
240 | { | ||
241 | this->bindElement(element, fvGeometry, sol); | ||
242 | return std::move(*this); | ||
243 | } | ||
244 | |||
245 | // specialization for control-volume finite element | ||
246 | template<class FVElementGeometry, class SolutionVector> | ||
247 | 85500 | void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element, | |
248 | const FVElementGeometry& fvGeometry, | ||
249 | const SolutionVector& sol) & | ||
250 | { | ||
251 | // get the solution at the dofs of the element | ||
252 | 85500 | auto elemSol = elementSolution(element, sol, fvGeometry.gridGeometry()); | |
253 | |||
254 | // resize variables to the required size | ||
255 | 85500 | variables_.resize(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry)); | |
256 | |||
257 | // update cv related dofs where there exists a localDof | ||
258 |
2/2✓ Branch 0 taken 426500 times.
✓ Branch 1 taken 85500 times.
|
512000 | for (const auto& localDof : localDofs(fvGeometry)) |
259 | 426500 | variables_[localDof.index()].update(elemSol, gridVolVars().problem(), fvGeometry, localDof); | |
260 | 85500 | } | |
261 | |||
262 | 7406800 | const Variables& operator [](std::size_t localIdx) const | |
263 |
2/6✓ Branch 0 taken 9035 times.
✓ Branch 1 taken 5765 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
7406800 | { return variables_[localIdx]; } |
264 | |||
265 | Variables& operator [](std::size_t localIdx) | ||
266 | { return variables_[localIdx]; } | ||
267 | |||
268 | template<class ScvOrLocalDof, typename std::enable_if_t<!std::is_integral<ScvOrLocalDof>::value, int> = 0> | ||
269 | 23224288 | const Variables& operator [](const ScvOrLocalDof& scvOrLocalDof) const | |
270 | { | ||
271 | if constexpr (Dumux::Detail::LocalDofs::isLocalDofType<ScvOrLocalDof>()) | ||
272 | return variables_[scvOrLocalDof.index()]; | ||
273 | else | ||
274 |
3/6✗ Branch 1 not taken.
✓ Branch 2 taken 9600 times.
✓ Branch 3 taken 14800 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 65600 times.
✗ Branch 6 not taken.
|
20914288 | return variables_[scvOrLocalDof.indexInElement()]; |
275 | } | ||
276 | |||
277 | template<class ScvOrLocalDof, typename std::enable_if_t<!std::is_integral<ScvOrLocalDof>::value, int> = 0> | ||
278 | 1275642 | Variables& operator [](const ScvOrLocalDof& scvOrLocalDof) | |
279 | { | ||
280 | if constexpr (Dumux::Detail::LocalDofs::isLocalDofType<ScvOrLocalDof>()) | ||
281 | 1053900 | return variables_[scvOrLocalDof.index()]; | |
282 | else | ||
283 | 221742 | return variables_[scvOrLocalDof.indexInElement()]; | |
284 | } | ||
285 | |||
286 | //! The grid variables cache object we are a restriction of | ||
287 | //! TODO: Rename after new variables concept has been implemented | ||
288 | //! this is currently needed to support old interface | ||
289 | 6502 | const GridVariablesCache& gridVolVars() const | |
290 |
1/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
426502 | { return *gridVariablesCachePtr_; } |
291 | |||
292 | /*! | ||
293 | * \brief return a local view on variables that is always mutable, regardless of the caching policy | ||
294 | * \pre bind has to be called before, otherwise using the view will result in undefined behavior | ||
295 | */ | ||
296 | 42300 | MutableView asMutableView(GridVariablesCache&) | |
297 | 42300 | { return { *this }; } | |
298 | |||
299 | private: | ||
300 | const GridVariablesCache* gridVariablesCachePtr_; | ||
301 | std::vector<Variables> variables_; | ||
302 | }; | ||
303 | |||
304 | } // end namespace Dumux | ||
305 | |||
306 | #endif | ||
307 |