GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/discretization/staggered/elementfluxvariablescache.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 19 27 70.4%
Functions: 48 205 23.4%
Branches: 78 158 49.4%

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 StaggeredDiscretization
10 * \copydoc Dumux::StaggeredElementFluxVariablesCache
11 */
12 #ifndef DUMUX_DISCRETIZATION_STAGGERED_ELEMENT_FLUXVARSCACHE_HH
13 #define DUMUX_DISCRETIZATION_STAGGERED_ELEMENT_FLUXVARSCACHE_HH
14
15 #include <algorithm>
16 #include <cassert>
17 #include <iterator>
18 #include <vector>
19 #include <utility>
20
21 #include <dune/common/exceptions.hh>
22
23 namespace Dumux {
24
25 /*!
26 * \ingroup StaggeredDiscretization
27 * \brief Base class for the stencil local flux variables cache for the staggered model
28 */
29 template<class GridFluxVariablesCache, bool cachingEnabled>
30 class StaggeredElementFluxVariablesCache;
31
32 /*!
33 * \ingroup StaggeredDiscretization
34 * \brief Class for the stencil local flux variables cache for the staggered model.
35 Specialization for the case of storing the fluxvars cache globally.
36 */
37 template<class GFVC>
38 class StaggeredElementFluxVariablesCache<GFVC, true>
39 {
40 //! the type of the flux variables cache filler
41 using FluxVariablesCacheFiller = typename GFVC::Traits::FluxVariablesCacheFiller;
42
43 public:
44 //! export the type of the grid flux variables cache
45 using GridFluxVariablesCache = GFVC;
46
47 //! export the type of the flux variables cache
48 using FluxVariablesCache = typename GFVC::FluxVariablesCache;
49
50
51 StaggeredElementFluxVariablesCache(const GridFluxVariablesCache& global)
52 : gridFluxVarsCachePtr_(&global) {}
53
54 /*!
55 * \brief bind the local view (r-value overload)
56 * This overload is called when an instance of this class is a temporary in the usage context
57 * This allows a usage like this: `const auto view = localView(...).bind(element);`
58 */
59 template<class FVElementGeometry, class ElementVolumeVariables>
60 StaggeredElementFluxVariablesCache bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
61 const FVElementGeometry& fvGeometry,
62 const ElementVolumeVariables& elemVolVars) &&
63 {
64 this->bindElement(element, fvGeometry, elemVolVars);
65 return std::move(*this);
66 }
67
68 //! Specialization for the global caching being enabled - do nothing here
69 template<class FVElementGeometry, class ElementVolumeVariables>
70 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
71 const FVElementGeometry& fvGeometry,
72 const ElementVolumeVariables& elemVolVars) & {}
73
74 /*!
75 * \brief bind the local view (r-value overload)
76 * This overload is called when an instance of this class is a temporary in the usage context
77 * This allows a usage like this: `const auto view = localView(...).bind(element);`
78 */
79 template<class FVElementGeometry, class ElementVolumeVariables>
80 StaggeredElementFluxVariablesCache bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
81 const FVElementGeometry& fvGeometry,
82 const ElementVolumeVariables& elemVolVars) &&
83 {
84 470936 this->bind(element, fvGeometry, elemVolVars);
85 470936 return std::move(*this);
86 }
87
88 //! Specialization for the global caching being enabled - do nothing here
89 template<class FVElementGeometry, class ElementVolumeVariables>
90 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
91 const FVElementGeometry& fvGeometry,
92 const ElementVolumeVariables& elemVolVars) & {}
93
94 /*!
95 * \brief bind the local view (r-value overload)
96 * This overload is called when an instance of this class is a temporary in the usage context
97 * This allows a usage like this: `const auto view = localView(...).bind(element);`
98 */
99 template<class FVElementGeometry, class ElementVolumeVariables>
100 StaggeredElementFluxVariablesCache bindScvf(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
101 const FVElementGeometry& fvGeometry,
102 const ElementVolumeVariables& elemVolVars,
103 const typename FVElementGeometry::SubControlVolumeFace& scvf) &&
104 {
105 this->bindScvf(element, fvGeometry, elemVolVars, scvf);
106 return std::move(*this);
107 }
108
109 //! Specialization for the global caching being enabled - do nothing here
110 template<class FVElementGeometry, class ElementVolumeVariables>
111 void bindScvf(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
112 const FVElementGeometry& fvGeometry,
113 const ElementVolumeVariables& elemVolVars,
114 const typename FVElementGeometry::SubControlVolumeFace& scvf) & {}
115
116 //! Specialization for the global caching being enabled - do nothing here
117 template<class FVElementGeometry, class ElementVolumeVariables>
118 void update(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
119 const FVElementGeometry& fvGeometry,
120 const ElementVolumeVariables& elemVolVars)
121 {
122 DUNE_THROW(Dune::InvalidStateException, "In case of enabled caching, the grid flux variables cache must not to be updated");
123 }
124
125 //! operators in the case of caching
126 template<class SubControlVolumeFace>
127 const FluxVariablesCache& operator [](const SubControlVolumeFace& scvf) const
128
2/10
✓ Branch 1 taken 138882 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 138882 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
207590668 { return (*gridFluxVarsCachePtr_)[scvf.index()]; }
129
130 //! The global object we are a restriction of
131 const GridFluxVariablesCache& gridFluxVarsCache() const
132 { return *gridFluxVarsCachePtr_; }
133
134 private:
135 const GridFluxVariablesCache* gridFluxVarsCachePtr_;
136 };
137
138 /*!
139 * \ingroup StaggeredDiscretization
140 * \brief Class for the stencil local flux variables cache for the staggered model.
141 Specialization for the case of not storing the fluxvars cache globally.
142 */
143 template<class GFVC>
144 1800 class StaggeredElementFluxVariablesCache<GFVC, false>
145 {
146 //! the type of the flux variables cache filler
147 using FluxVariablesCacheFiller = typename GFVC::Traits::FluxVariablesCacheFiller;
148
149 public:
150 //! export the type of the grid flux variables cache
151 using GridFluxVariablesCache = GFVC;
152
153 //! export the type of the flux variables cache
154 using FluxVariablesCache = typename GFVC::FluxVariablesCache;
155
156 StaggeredElementFluxVariablesCache(const GridFluxVariablesCache& global)
157
72/144
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 25 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 25 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 25 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 25 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 25 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 25 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 25 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 25 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 25 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 25 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 25 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 25 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 25 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 25 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 25 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 25 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 25 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 25 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 25 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 25 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 25 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 25 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 25 times.
✗ Branch 74 not taken.
✓ Branch 76 taken 25 times.
✗ Branch 77 not taken.
✓ Branch 79 taken 25 times.
✗ Branch 80 not taken.
✓ Branch 82 taken 25 times.
✗ Branch 83 not taken.
✓ Branch 85 taken 25 times.
✗ Branch 86 not taken.
✓ Branch 88 taken 25 times.
✗ Branch 89 not taken.
✓ Branch 91 taken 25 times.
✗ Branch 92 not taken.
✓ Branch 94 taken 25 times.
✗ Branch 95 not taken.
✓ Branch 97 taken 25 times.
✗ Branch 98 not taken.
✓ Branch 100 taken 25 times.
✗ Branch 101 not taken.
✓ Branch 103 taken 25 times.
✗ Branch 104 not taken.
✓ Branch 106 taken 25 times.
✗ Branch 107 not taken.
✓ Branch 109 taken 25 times.
✗ Branch 110 not taken.
✓ Branch 112 taken 25 times.
✗ Branch 113 not taken.
✓ Branch 115 taken 25 times.
✗ Branch 116 not taken.
✓ Branch 118 taken 25 times.
✗ Branch 119 not taken.
✓ Branch 121 taken 25 times.
✗ Branch 122 not taken.
✓ Branch 124 taken 25 times.
✗ Branch 125 not taken.
✓ Branch 127 taken 25 times.
✗ Branch 128 not taken.
✓ Branch 130 taken 25 times.
✗ Branch 131 not taken.
✓ Branch 133 taken 25 times.
✗ Branch 134 not taken.
✓ Branch 136 taken 25 times.
✗ Branch 137 not taken.
✓ Branch 139 taken 25 times.
✗ Branch 140 not taken.
✓ Branch 142 taken 25 times.
✗ Branch 143 not taken.
✓ Branch 145 taken 25 times.
✗ Branch 146 not taken.
✓ Branch 148 taken 25 times.
✗ Branch 149 not taken.
✓ Branch 151 taken 25 times.
✗ Branch 152 not taken.
✓ Branch 154 taken 25 times.
✗ Branch 155 not taken.
✓ Branch 157 taken 25 times.
✗ Branch 158 not taken.
✓ Branch 160 taken 25 times.
✗ Branch 161 not taken.
✓ Branch 163 taken 25 times.
✗ Branch 164 not taken.
✓ Branch 166 taken 25 times.
✗ Branch 167 not taken.
✓ Branch 169 taken 25 times.
✗ Branch 170 not taken.
✓ Branch 172 taken 25 times.
✗ Branch 173 not taken.
✓ Branch 175 taken 25 times.
✗ Branch 176 not taken.
✓ Branch 178 taken 25 times.
✗ Branch 179 not taken.
✓ Branch 181 taken 25 times.
✗ Branch 182 not taken.
✓ Branch 184 taken 25 times.
✗ Branch 185 not taken.
✓ Branch 187 taken 25 times.
✗ Branch 188 not taken.
✓ Branch 190 taken 25 times.
✗ Branch 191 not taken.
✓ Branch 193 taken 25 times.
✗ Branch 194 not taken.
✓ Branch 196 taken 25 times.
✗ Branch 197 not taken.
✓ Branch 199 taken 25 times.
✗ Branch 200 not taken.
✓ Branch 202 taken 25 times.
✗ Branch 203 not taken.
✓ Branch 205 taken 25 times.
✗ Branch 206 not taken.
✓ Branch 208 taken 25 times.
✗ Branch 209 not taken.
✓ Branch 211 taken 25 times.
✗ Branch 212 not taken.
✓ Branch 214 taken 25 times.
✗ Branch 215 not taken.
1800 : gridFluxVarsCachePtr_(&global) {}
158
159 /*!
160 * \brief bind the local view (r-value overload)
161 * This overload is called when an instance of this class is a temporary in the usage context
162 * This allows a usage like this: `const auto view = localView(...).bind(element);`
163 */
164 template<class FVElementGeometry, class ElementVolumeVariables>
165 StaggeredElementFluxVariablesCache bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
166 const FVElementGeometry& fvGeometry,
167 const ElementVolumeVariables& elemVolVars) &&
168 {
169 this->bindElement_(element, fvGeometry, elemVolVars);
170 return std::move(*this);
171 }
172
173 //! Specialization for the global caching being enabled - do nothing here
174 template<class FVElementGeometry, class ElementVolumeVariables>
175 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
176 const FVElementGeometry& fvGeometry,
177 const ElementVolumeVariables& elemVolVars) &
178 { this->bindElement_(element, fvGeometry, elemVolVars); }
179
180 /*!
181 * \brief bind the local view (r-value overload)
182 * This overload is called when an instance of this class is a temporary in the usage context
183 * This allows a usage like this: `const auto view = localView(...).bind(element);`
184 */
185 template<class FVElementGeometry, class ElementVolumeVariables>
186 1200 StaggeredElementFluxVariablesCache bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
187 const FVElementGeometry& fvGeometry,
188 const ElementVolumeVariables& elemVolVars) &&
189 {
190 1200 this->bind_(element, fvGeometry, elemVolVars);
191 2400 return std::move(*this);
192 }
193
194 //! Specialization for the global caching being enabled - do nothing here
195 template<class FVElementGeometry, class ElementVolumeVariables>
196 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
197 const FVElementGeometry& fvGeometry,
198 const ElementVolumeVariables& elemVolVars) &
199 { this->bind_(element, fvGeometry, elemVolVars); }
200
201 /*!
202 * \brief bind the local view (r-value overload)
203 * This overload is called when an instance of this class is a temporary in the usage context
204 * This allows a usage like this: `const auto view = localView(...).bind(element);`
205 */
206 template<class FVElementGeometry, class ElementVolumeVariables>
207 StaggeredElementFluxVariablesCache bindScvf(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
208 const FVElementGeometry& fvGeometry,
209 const ElementVolumeVariables& elemVolVars,
210 const typename FVElementGeometry::SubControlVolumeFace& scvf) &&
211 {
212 this->bindScvf_(element, fvGeometry, elemVolVars, scvf);
213 return std::move(*this);
214 }
215
216 //! Specialization for the global caching being enabled - do nothing here
217 template<class FVElementGeometry, class ElementVolumeVariables>
218 void bindScvf(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
219 const FVElementGeometry& fvGeometry,
220 const ElementVolumeVariables& elemVolVars,
221 const typename FVElementGeometry::SubControlVolumeFace& scvf) &
222 { this->bindScvf_(element, fvGeometry, elemVolVars, scvf); }
223
224 /*!
225 * \brief Update the transmissibilities if the volume variables have changed
226 * \note Results in undefined behaviour if called before bind() or with a different element
227 */
228 template<class FVElementGeometry, class ElementVolumeVariables>
229 void update(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
230 const FVElementGeometry& fvGeometry,
231 const ElementVolumeVariables& elemVolVars)
232 {
233 // if (FluxVariablesCacheFiller::isSolDependent) TODO
234 // {
235 // const auto& problem = gridFluxVarsCache().problem();
236 // const auto globalI = fvGeometry.gridGeometry().elementMapper().index(element);
237 //
238 // // instantiate filler class
239 // FluxVariablesCacheFiller filler(problem);
240 //
241 // // let the filler class update the caches
242 // for (unsigned int localScvfIdx = 0; localScvfIdx < fluxVarsCache_.size(); ++localScvfIdx)
243 // {
244 // const auto& scvf = fvGeometry.scvf(globalScvfIndices_[localScvfIdx]);
245 //
246 // const auto scvfInsideScvIdx = scvf.insideScvIdx();
247 // const auto& insideElement = scvfInsideScvIdx == globalI ?
248 // element :
249 // fvGeometry.gridGeometry().element(scvfInsideScvIdx);
250 //
251 // filler.fill(*this, fluxVarsCache_[localScvfIdx], insideElement, fvGeometry, elemVolVars, scvf);
252 // }
253 // }
254 }
255
256 //! access operators in the case of no caching
257 template<class SubControlVolumeFace>
258 const FluxVariablesCache& operator [](const SubControlVolumeFace& scvf) const
259 { return fluxVarsCache_[getLocalScvfIdx_(scvf.index())]; }
260
261 template<class SubControlVolumeFace>
262 FluxVariablesCache& operator [](const SubControlVolumeFace& scvf)
263 { return fluxVarsCache_[getLocalScvfIdx_(scvf.index())]; }
264
265 //! The global object we are a restriction of
266 const GridFluxVariablesCache& gridFluxVarsCache() const
267 { return *gridFluxVarsCachePtr_; }
268
269 private:
270
271 /*!
272 * \brief Prepares the transmissibilities of the scv faces in an element
273 * \note the fvGeometry is assumed to be bound to the same element
274 * \note this function has to be called prior to flux calculations on the element.
275 */
276 template<class FVElementGeometry, class ElementVolumeVariables>
277 void bindElement_(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
278 const FVElementGeometry& fvGeometry,
279 const ElementVolumeVariables& elemVolVars)
280 {
281 // resizing of the cache
282 const auto numScvf = fvGeometry.numScvf();
283 fluxVarsCache_.resize(numScvf);
284 globalScvfIndices_.resize(numScvf);
285
286 // instantiate helper class to fill the caches
287 FluxVariablesCacheFiller filler(gridFluxVarsCache().problem());
288
289 std::size_t localScvfIdx = 0;
290 // fill the containers
291 for (auto&& scvf : scvfs(fvGeometry))
292 {
293 filler.fill(*this, fluxVarsCache_[localScvfIdx], element, fvGeometry, elemVolVars, scvf, true);
294 globalScvfIndices_[localScvfIdx] = scvf.index();
295 localScvfIdx++;
296 }
297 }
298
299 /*!
300 * \brief Prepares the transmissibilities of the scv faces in the stencil of an element
301 * \note the fvGeometry is assumed to be bound to the same element
302 * \note this function has to be called prior to flux calculations on the element.
303 */
304 template<class FVElementGeometry, class ElementVolumeVariables>
305 1200 void bind_(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
306 const FVElementGeometry& fvGeometry,
307 const ElementVolumeVariables& elemVolVars)
308 {
309 // instantiate helper class to fill the caches
310 1200 FluxVariablesCacheFiller filler(gridFluxVarsCache().problem());
311
312 // find the number of scv faces that need to be prepared
313 1200 const auto numScvf = fvGeometry.numScvf();
314
315 // fill the containers with the data on the scv faces inside the actual element
316 1200 fluxVarsCache_.resize(numScvf);
317 1200 globalScvfIndices_.resize(numScvf);
318 1200 unsigned int localScvfIdx = 0;
319
4/4
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 600 times.
✓ Branch 2 taken 2400 times.
✓ Branch 3 taken 600 times.
12000 for (auto&& scvf : scvfs(fvGeometry))
320 {
321 4800 filler.fill(*this, fluxVarsCache_[localScvfIdx], element, fvGeometry, elemVolVars, scvf, true);
322 4800 globalScvfIndices_[localScvfIdx] = scvf.index();
323 4800 localScvfIdx++;
324 }
325 1200 }
326
327 /*!
328 * \brief Prepares the transmissibilities of a single scv face
329 * \note the fvGeometry is assumed to be bound to the same element
330 * \note this function has to be called prior to flux calculations on the element.
331 */
332 template<class FVElementGeometry, class ElementVolumeVariables>
333 void bindScvf_ (const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
334 const FVElementGeometry& fvGeometry,
335 const ElementVolumeVariables& elemVolVars,
336 const typename FVElementGeometry::SubControlVolumeFace& scvf)
337 {
338 fluxVarsCache_.resize(1);
339 globalScvfIndices_.resize(1);
340
341 // instantiate helper class to fill the caches
342 // FluxVariablesCacheFiller filler(gridFluxVarsCache().problem());
343 FluxVariablesCacheFiller filler; // TODO: use proper ctor
344
345 filler.fill(*this, fluxVarsCache_[0], element, fvGeometry, elemVolVars, scvf, true);
346 globalScvfIndices_[0] = scvf.index();
347 }
348
349 const GridFluxVariablesCache* gridFluxVarsCachePtr_;
350
351 // get index of scvf in the local container
352 int getLocalScvfIdx_(const int scvfIdx) const
353 {
354 auto it = std::find(globalScvfIndices_.begin(), globalScvfIndices_.end(), scvfIdx);
355 assert(it != globalScvfIndices_.end() && "Could not find the flux vars cache for scvfIdx");
356 return std::distance(globalScvfIndices_.begin(), it);
357 }
358
359 std::vector<FluxVariablesCache> fluxVarsCache_;
360 std::vector<std::size_t> globalScvfIndices_;
361 };
362
363 } // end namespace Dumux
364
365 #endif
366