version 3.11-dev
discretization/cvfe/hybrid/gridfluxvariablescache.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-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_FLUXVARSCACHE_HH
13#define DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_FLUXVARSCACHE_HH
14
15#include <memory>
16#include <ranges>
17#include <unordered_map>
18
20
21// make the local view function available whenever we use this class
25
26namespace Dumux {
27
32template<class P, class FVC>
34{
35 using Problem = P;
36 using FluxVariablesCache = FVC;
37
38 template<class GridFluxVariablesCache, bool cachingEnabled>
40};
41
47template<class Problem,
48 class FluxVariablesCache,
49 bool cachingEnabled = false,
52
59template<class P, class FVC, class Traits>
60class HybridCVFEGridFluxVariablesCache<P, FVC, true, Traits>
61{
62 using Problem = typename Traits::Problem;
64
65public:
67 using FluxVariablesCache = typename Traits::FluxVariablesCache;
68
70 static constexpr bool cachingEnabled = true;
71
73 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
74
75 HybridCVFEGridFluxVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
76
77 template<class GridGeometry, class GridVolumeVariables, class SolutionVector>
78 void update(const GridGeometry& gridGeometry,
79 const GridVolumeVariables& gridVolVars,
80 const SolutionVector& sol,
81 bool forceUpdate = false)
82 {
83 // Here, we do not do anything unless it is a forced update
84 if (forceUpdate)
85 {
86 fluxVarsCache_.resize(gridGeometry.gridView().size(0));
87 scvfOffset_.resize(gridGeometry.gridView().size(0));
88 elementCache_.resize(gridGeometry.gridView().size(0));
89 boundaryFaceCache_.resize(gridGeometry.gridView().size(0));
90 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
91 {
92 // Prepare the geometries within the elements of the stencil
93 const auto element = gridGeometry.element(eIdx);
94 const auto fvGeometry = localView(gridGeometry).bind(element);
95 const auto elemVolVars = localView(gridVolVars).bind(element, fvGeometry, sol);
96
97 // Build offset vector and resize flat cache
98 scvfOffset_[eIdx].resize(fvGeometry.numScvf() + 1, 0);
99 for (const auto& scvf : scvfs(fvGeometry))
100 {
101 const auto numQps = std::ranges::size(CVFE::quadratureRule(fvGeometry, scvf));
102 scvfOffset_[eIdx][scvf.index() + 1] = numQps;
103 }
104 for (std::size_t i = 2; i < scvfOffset_[eIdx].size(); ++i)
105 scvfOffset_[eIdx][i] += scvfOffset_[eIdx][i-1];
106
107 fluxVarsCache_[eIdx].resize(scvfOffset_[eIdx].back());
108 for (const auto& scvf : scvfs(fvGeometry))
109 {
110 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, scvf))
111 cache(eIdx, qpData.ipData().scvfIndex(), qpData.ipData().qpIndex()).update(problem,
112 element,
113 fvGeometry,
114 elemVolVars,
115 qpData.ipData());
116 }
117
118 // Resize element cache based on element quadrature rule
119 const auto elemQuadRule = CVFE::quadratureRule(fvGeometry, element);
120 elementCache_[eIdx].resize(std::ranges::size(elemQuadRule));
121 for (const auto& qpData : elemQuadRule)
122 elementCache_[eIdx][qpData.ipData().qpIndex()].update(problem, element, fvGeometry, elemVolVars, qpData.ipData());
123
124 // Rebuild boundary face cache for this element
125 if (!boundaryFaceCache_[eIdx])
126 boundaryFaceCache_[eIdx] = std::make_unique<std::unordered_map<int, std::vector<FluxVariablesCache>>>();
127 else
128 boundaryFaceCache_[eIdx]->clear();
129
130 for (const auto& boundaryFace : boundaryFaces(fvGeometry))
131 {
132 const auto quadRule = CVFE::quadratureRule(fvGeometry, boundaryFace);
133 const auto bfIdx = boundaryFace.index();
134 (*boundaryFaceCache_[eIdx])[bfIdx].resize(std::ranges::size(quadRule));
135
136 for (const auto& qpData : quadRule)
137 (*boundaryFaceCache_[eIdx])[bfIdx][qpData.ipData().qpIndex()].update(problem,
138 element,
139 fvGeometry,
140 elemVolVars,
141 qpData.ipData());
142 }
143 });
144 }
145 }
146
147 template<class FVElementGeometry, class ElementVolumeVariables>
148 void updateElement(const typename FVElementGeometry::Element& element,
149 const FVElementGeometry& fvGeometry,
150 const ElementVolumeVariables& elemVolVars)
151 {
152 if constexpr (FluxVariablesCache::isSolDependent)
153 {
154 const auto eIdx = fvGeometry.gridGeometry().elementMapper().index(element);
155
156 // Build offset vector and resize flat cache
157 scvfOffset_[eIdx].resize(fvGeometry.numScvf() + 1, 0);
158 for (const auto& scvf : scvfs(fvGeometry))
159 {
160 const auto numQps = std::ranges::size(CVFE::quadratureRule(fvGeometry, scvf));
161 scvfOffset_[eIdx][scvf.index() + 1] = numQps;
162 }
163 for (std::size_t i = 2; i < scvfOffset_[eIdx].size(); ++i)
164 scvfOffset_[eIdx][i] += scvfOffset_[eIdx][i-1];
165
166 fluxVarsCache_[eIdx].resize(scvfOffset_[eIdx].back());
167 for (const auto& scvf : scvfs(fvGeometry))
168 {
169 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, scvf))
170 cache(eIdx, qpData.ipData().scvfIndex(), qpData.ipData().qpIndex()).update(problem(),
171 element,
172 fvGeometry,
173 elemVolVars,
174 qpData.ipData());
175 }
176
177 // Resize element cache based on element quadrature rule
178 const auto elemQuadRule = CVFE::quadratureRule(fvGeometry, element);
179 elementCache_[eIdx].resize(std::ranges::size(elemQuadRule));
180 for (const auto& qpData : elemQuadRule)
181 elementCache_[eIdx][qpData.ipData().qpIndex()].update(problem(), element, fvGeometry, elemVolVars, qpData.ipData());
182
183 // Rebuild boundary face cache for this element
184 if (!boundaryFaceCache_[eIdx])
185 boundaryFaceCache_[eIdx] = std::make_unique<std::unordered_map<int, std::vector<FluxVariablesCache>>>();
186 else
187 boundaryFaceCache_[eIdx]->clear();
188
189 for (const auto& boundaryFace : boundaryFaces(fvGeometry))
190 {
191 const auto quadRule = CVFE::quadratureRule(fvGeometry, boundaryFace);
192 const auto bfIdx = boundaryFace.index();
193 (*boundaryFaceCache_[eIdx])[bfIdx].resize(std::ranges::size(quadRule));
194
195 for (const auto& qpData : quadRule)
196 (*boundaryFaceCache_[eIdx])[bfIdx][qpData.ipData().qpIndex()].update(problem(),
197 element,
198 fvGeometry,
199 elemVolVars,
200 qpData.ipData());
201 }
202 }
203 }
204
205 const Problem& problem() const
206 { return *problemPtr_; }
207
208 // access operator
209 const FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
210 { return fluxVarsCache_[eIdx][scvfOffset_[eIdx][scvfIdx] + qpIdx]; }
211
212 // access operator
213 FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
214 { return fluxVarsCache_[eIdx][scvfOffset_[eIdx][scvfIdx] + qpIdx]; }
215
216 // access operator
217 const FluxVariablesCache& elementCache(std::size_t eIdx, std::size_t qpIdx) const { return elementCache_[eIdx][qpIdx]; }
218 FluxVariablesCache& elementCache(std::size_t eIdx, std::size_t qpIdx) { return elementCache_[eIdx][qpIdx]; }
219
220 // access operator for boundary face cache
221 FluxVariablesCache& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) { return (*boundaryFaceCache_[eIdx])[bfIdx][qpIdx]; }
222 const FluxVariablesCache& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const { return (*boundaryFaceCache_[eIdx]).at(bfIdx)[qpIdx]; }
223
224private:
225 // currently bound element
226 const Problem* problemPtr_;
227 std::vector<std::vector<FluxVariablesCache>> fluxVarsCache_;
228 std::vector<std::vector<std::size_t>> scvfOffset_;
229 std::vector<std::vector<FluxVariablesCache>> elementCache_;
230 std::vector<std::unique_ptr<std::unordered_map<int, std::vector<FluxVariablesCache>>>> boundaryFaceCache_;
231};
232
237template<class P, class FVC, class Traits>
238class HybridCVFEGridFluxVariablesCache<P, FVC, false, Traits>
239{
240 using Problem = typename Traits::Problem;
242
243public:
245 using FluxVariablesCache = typename Traits::FluxVariablesCache;
246
248 static constexpr bool cachingEnabled = false;
249
251 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
252
253 HybridCVFEGridFluxVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
254
255 template<class GridGeometry, class GridVolumeVariables, class SolutionVector>
256 void update(const GridGeometry& gridGeometry,
257 const GridVolumeVariables& gridVolVars,
258 const SolutionVector& sol,
259 bool forceUpdate = false) {}
260
261 const Problem& problem() const
262 { return *problemPtr_; }
263
264private:
265 const Problem* problemPtr_;
266};
267
268} // end namespace Dumux
269
270#endif
The flux variables caches for an element when using hybrid CVFE discretizations.
Definition: discretization/cvfe/hybrid/elementfluxvariablescache.hh:38
Flux variable caches on a gridview with grid caching disabled.
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:239
const Problem & problem() const
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:261
void update(const GridGeometry &gridGeometry, const GridVolumeVariables &gridVolVars, const SolutionVector &sol, bool forceUpdate=false)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:256
typename Traits::FluxVariablesCache FluxVariablesCache
export the flux variable cache type
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:245
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:251
HybridCVFEGridFluxVariablesCache(const Problem &problem)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:253
Flux variable caches on a gridview with grid caching enabled (general quadrature specialization)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:61
const FluxVariablesCache & elementCache(std::size_t eIdx, std::size_t qpIdx) const
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:217
const Problem & problem() const
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:205
FluxVariablesCache & elementCache(std::size_t eIdx, std::size_t qpIdx)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:218
HybridCVFEGridFluxVariablesCache(const Problem &problem)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:75
FluxVariablesCache & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:221
FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:213
const FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:209
const FluxVariablesCache & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:222
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:73
void updateElement(const typename FVElementGeometry::Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:148
typename Traits::FluxVariablesCache FluxVariablesCache
export the flux variable cache type
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:67
void update(const GridGeometry &gridGeometry, const GridVolumeVariables &gridVolVars, const SolutionVector &sol, bool forceUpdate=false)
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:78
Flux variable caches implementation on a gridview.
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:51
Global flux variable cache.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:26
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading)
Definition: parallel_for.hh:160
Free function to get the local view of a grid cache object.
auto quadratureRule(const FVElementGeometry &fvGeometry, const typename FVElementGeometry::SubControlVolume &scv, QuadratureRules::MidpointQuadrature)
Midpoint quadrature for scv.
Definition: quadraturerules.hh:159
Definition: adapt.hh:17
Parallel for loop (multithreading)
Quadrature rules over sub-control volumes and sub-control volume faces.
Flux variable caches traits.
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:34
FVC FluxVariablesCache
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:36
P Problem
Definition: discretization/cvfe/hybrid/gridfluxvariablescache.hh:35