version 3.11-dev
discretization/cvfe/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_CVFE_GRID_FLUXVARSCACHE_HH
13#define DUMUX_DISCRETIZATION_CVFE_GRID_FLUXVARSCACHE_HH
14
16
17// make the local view function available whenever we use this class
21
22namespace Dumux {
23
28template<class P, class FVC>
30{
31 using Problem = P;
32 using FluxVariablesCache = FVC;
33
34 template<class GridFluxVariablesCache, bool cachingEnabled>
36};
37
43template<class Problem,
44 class FluxVariablesCache,
45 bool cachingEnabled,
46 class Traits,
47 class ScvfQuadratureRule>
49
54template<class Problem,
55 class FluxVariablesCache,
56 bool cachingEnabled = false,
58using CVFEGridFluxVariablesCache = CVFEGridFluxVariablesCacheImpl<Problem, FluxVariablesCache, cachingEnabled,
60
67template<class P, class FVC, class Traits>
68class CVFEGridFluxVariablesCacheImpl<P, FVC, true, Traits, QuadratureRules::MidpointQuadrature>
69{
70 using Problem = typename Traits::Problem;
72
73public:
75 using FluxVariablesCache = typename Traits::FluxVariablesCache;
76
78 static constexpr bool cachingEnabled = true;
79
81 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
82
83 CVFEGridFluxVariablesCacheImpl(const Problem& problem) : problemPtr_(&problem) {}
84
85 template<class GridGeometry, class GridVolumeVariables, class SolutionVector>
86 void update(const GridGeometry& gridGeometry,
87 const GridVolumeVariables& gridVolVars,
88 const SolutionVector& sol,
89 bool forceUpdate = false)
90 {
91 // Here, we do not do anything unless it is a forced update
92 if (forceUpdate)
93 {
94 fluxVarsCache_.resize(gridGeometry.gridView().size(0));
95 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
96 {
97 // Prepare the geometries within the elements of the stencil
98 const auto element = gridGeometry.element(eIdx);
99 const auto fvGeometry = localView(gridGeometry).bind(element);
100 const auto elemVolVars = localView(gridVolVars).bind(element, fvGeometry, sol);
101
102 // only update shape functions for fluxes if update is forced
103 fluxVarsCache_[eIdx].resize(fvGeometry.numScvf());
104 for (const auto& scvf : scvfs(fvGeometry))
105 cache(eIdx, scvf.index()).update(problem, element, fvGeometry, elemVolVars, scvf);
106 });
107 }
108 }
109
110 template<class FVElementGeometry, class ElementVolumeVariables>
111 void updateElement(const typename FVElementGeometry::Element& element,
112 const FVElementGeometry& fvGeometry,
113 const ElementVolumeVariables& elemVolVars)
114 {
115 if constexpr (FluxVariablesCache::isSolDependent)
116 {
117 const auto eIdx = fvGeometry.gridGeometry().elementMapper().index(element);
118 fluxVarsCache_[eIdx].resize(fvGeometry.numScvf());
119 for (const auto& scvf : scvfs(fvGeometry))
120 cache(eIdx, scvf.index()).update(problem(), element, fvGeometry, elemVolVars, scvf);
121 }
122 }
123
124 const Problem& problem() const
125 { return *problemPtr_; }
126
127 // access operator
128 const FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx) const
129 { return fluxVarsCache_[eIdx][scvfIdx]; }
130
131 // access operator
132 FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx)
133 { return fluxVarsCache_[eIdx][scvfIdx]; }
134
135private:
136 // currently bound element
137 const Problem* problemPtr_;
138 std::vector<std::vector<FluxVariablesCache>> fluxVarsCache_;
139};
140
147template<class P, class FVC, class Traits, class ScvfQR>
148class CVFEGridFluxVariablesCacheImpl<P, FVC, true, Traits, ScvfQR>
149{
150 using Problem = typename Traits::Problem;
152
153public:
155 using FluxVariablesCache = typename Traits::FluxVariablesCache;
156
158 static constexpr bool cachingEnabled = true;
159
161 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
162
163 CVFEGridFluxVariablesCacheImpl(const Problem& problem) : problemPtr_(&problem) {}
164
165 template<class GridGeometry, class GridVolumeVariables, class SolutionVector>
166 void update(const GridGeometry& gridGeometry,
167 const GridVolumeVariables& gridVolVars,
168 const SolutionVector& sol,
169 bool forceUpdate = false)
170 {
171 // Here, we do not do anything unless it is a forced update
172 if (forceUpdate)
173 {
174 fluxVarsCache_.resize(gridGeometry.gridView().size(0));
175 qpsOffset_.resize(gridGeometry.gridView().size(0));
176 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
177 {
178 // Prepare the geometries within the elements of the stencil
179 const auto element = gridGeometry.element(eIdx);
180 const auto fvGeometry = localView(gridGeometry).bind(element);
181 const auto elemVolVars = localView(gridVolVars).bind(element, fvGeometry, sol);
182
183 // Compute total number of cache entries and offsets for this element
184 qpsOffset_[eIdx].resize(fvGeometry.numScvf() + 1, 0);
185 for (const auto& scvf : scvfs(fvGeometry))
186 {
187 const auto numQps = std::ranges::size(CVFE::quadratureRule(fvGeometry, scvf));
188 qpsOffset_[eIdx][scvf.index() + 1] = numQps;
189 }
190 for (std::size_t i = 2; i < qpsOffset_[eIdx].size(); ++i)
191 qpsOffset_[eIdx][i] += qpsOffset_[eIdx][i-1];
192
193 // Resize flat cache and update entries
194 fluxVarsCache_[eIdx].resize(qpsOffset_[eIdx].back());
195 for (const auto& scvf : scvfs(fvGeometry))
196 {
197 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, scvf))
198 cache(eIdx, qpData.ipData().scvfIndex(), qpData.ipData().qpIndex()).update(problem,
199 element,
200 fvGeometry,
201 elemVolVars,
202 qpData.ipData());
203 }
204 });
205 }
206 }
207
208 template<class FVElementGeometry, class ElementVolumeVariables>
209 void updateElement(const typename FVElementGeometry::Element& element,
210 const FVElementGeometry& fvGeometry,
211 const ElementVolumeVariables& elemVolVars)
212 {
213 if constexpr (FluxVariablesCache::isSolDependent)
214 {
215 const auto eIdx = fvGeometry.gridGeometry().elementMapper().index(element);
216
217 // Compute offsets for this element
218 qpsOffset_[eIdx].resize(fvGeometry.numScvf() + 1, 0);
219 for (const auto& scvf : scvfs(fvGeometry))
220 {
221 const auto numQps = std::ranges::size(CVFE::quadratureRule(fvGeometry, scvf));
222 qpsOffset_[eIdx][scvf.index() + 1] = numQps;
223 }
224 for (std::size_t i = 2; i < qpsOffset_[eIdx].size(); ++i)
225 qpsOffset_[eIdx][i] += qpsOffset_[eIdx][i-1];
226
227 // Resize and update flat cache
228 fluxVarsCache_[eIdx].resize(qpsOffset_[eIdx].back());
229 for (const auto& scvf : scvfs(fvGeometry))
230 {
231 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, scvf))
232 cache(eIdx, qpData.ipData().scvfIndex(), qpData.ipData().qpIndex()).update(problem(),
233 element,
234 fvGeometry,
235 elemVolVars,
236 qpData.ipData());
237 }
238 }
239 }
240
241 const Problem& problem() const
242 { return *problemPtr_; }
243
244 // access operator
245 const FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
246 { return fluxVarsCache_[eIdx][qpsOffset_[eIdx][scvfIdx] + qpIdx]; }
247
248 // access operator
249 FluxVariablesCache& cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
250 { return fluxVarsCache_[eIdx][qpsOffset_[eIdx][scvfIdx] + qpIdx]; }
251
252private:
253 // currently bound element
254 const Problem* problemPtr_;
255 std::vector<std::vector<FluxVariablesCache>> fluxVarsCache_;
256 std::vector<std::vector<std::size_t>> qpsOffset_;
257};
258
263template<class P, class FVC, class Traits, class ScvfQR>
264class CVFEGridFluxVariablesCacheImpl<P, FVC, false, Traits, ScvfQR>
265{
266 using Problem = typename Traits::Problem;
268
269public:
271 using FluxVariablesCache = typename Traits::FluxVariablesCache;
272
274 static constexpr bool cachingEnabled = false;
275
277 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
278
279 CVFEGridFluxVariablesCacheImpl(const Problem& problem) : problemPtr_(&problem) {}
280
281 template<class GridGeometry, class GridVolumeVariables, class SolutionVector>
282 void update(const GridGeometry& gridGeometry,
283 const GridVolumeVariables& gridVolVars,
284 const SolutionVector& sol,
285 bool forceUpdate = false) {}
286
287 const Problem& problem() const
288 { return *problemPtr_; }
289
290private:
291 const Problem* problemPtr_;
292};
293
294} // end namespace Dumux
295
296#endif
The flux variables caches for an element.
Definition: discretization/cvfe/elementfluxvariablescache.hh:48
Flux variable caches on a gridview with grid caching disabled.
Definition: discretization/cvfe/gridfluxvariablescache.hh:265
CVFEGridFluxVariablesCacheImpl(const Problem &problem)
Definition: discretization/cvfe/gridfluxvariablescache.hh:279
typename Traits::FluxVariablesCache FluxVariablesCache
export the flux variable cache type
Definition: discretization/cvfe/gridfluxvariablescache.hh:271
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: discretization/cvfe/gridfluxvariablescache.hh:277
void update(const GridGeometry &gridGeometry, const GridVolumeVariables &gridVolVars, const SolutionVector &sol, bool forceUpdate=false)
Definition: discretization/cvfe/gridfluxvariablescache.hh:282
const Problem & problem() const
Definition: discretization/cvfe/gridfluxvariablescache.hh:287
Flux variable caches on a gridview with grid caching enabled (general quadrature specialization)
Definition: discretization/cvfe/gridfluxvariablescache.hh:149
CVFEGridFluxVariablesCacheImpl(const Problem &problem)
Definition: discretization/cvfe/gridfluxvariablescache.hh:163
typename Traits::FluxVariablesCache FluxVariablesCache
export the flux variable cache type
Definition: discretization/cvfe/gridfluxvariablescache.hh:155
FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
Definition: discretization/cvfe/gridfluxvariablescache.hh:249
const Problem & problem() const
Definition: discretization/cvfe/gridfluxvariablescache.hh:241
void updateElement(const typename FVElementGeometry::Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars)
Definition: discretization/cvfe/gridfluxvariablescache.hh:209
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: discretization/cvfe/gridfluxvariablescache.hh:161
const FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
Definition: discretization/cvfe/gridfluxvariablescache.hh:245
void update(const GridGeometry &gridGeometry, const GridVolumeVariables &gridVolVars, const SolutionVector &sol, bool forceUpdate=false)
Definition: discretization/cvfe/gridfluxvariablescache.hh:166
Flux variable caches on a gridview with grid caching enabled (MidpointQuadrature specialization)
Definition: discretization/cvfe/gridfluxvariablescache.hh:69
const Problem & problem() const
Definition: discretization/cvfe/gridfluxvariablescache.hh:124
void updateElement(const typename FVElementGeometry::Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars)
Definition: discretization/cvfe/gridfluxvariablescache.hh:111
const FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx) const
Definition: discretization/cvfe/gridfluxvariablescache.hh:128
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: discretization/cvfe/gridfluxvariablescache.hh:81
void update(const GridGeometry &gridGeometry, const GridVolumeVariables &gridVolVars, const SolutionVector &sol, bool forceUpdate=false)
Definition: discretization/cvfe/gridfluxvariablescache.hh:86
typename Traits::FluxVariablesCache FluxVariablesCache
export the flux variable cache type
Definition: discretization/cvfe/gridfluxvariablescache.hh:75
CVFEGridFluxVariablesCacheImpl(const Problem &problem)
Definition: discretization/cvfe/gridfluxvariablescache.hh:83
FluxVariablesCache & cache(std::size_t eIdx, std::size_t scvfIdx)
Definition: discretization/cvfe/gridfluxvariablescache.hh:132
Flux variable caches implementation on a gridview.
Definition: discretization/cvfe/gridfluxvariablescache.hh:48
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:148
Dune::Std::detected_or_t< QuadratureRules::MidpointQuadrature, DefinesScvfQuadratureRule, FluxVariablesCache > ScvfQuadratureRuleOrDefault_t
Definition: discretization/cvfe/elementfluxvariablescache.hh:34
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/gridfluxvariablescache.hh:30
P Problem
Definition: discretization/cvfe/gridfluxvariablescache.hh:31
FVC FluxVariablesCache
Definition: discretization/cvfe/gridfluxvariablescache.hh:32