version 3.11-dev
hybrid/gridvariablescache.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_VARIABLES_CACHE_HH
13#define DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_VARIABLES_CACHE_HH
14
15#include <unordered_map>
16#include <utility>
17#include <vector>
18#include <ranges>
19#include <memory>
20
22
23#include <dumux/common/concepts/localdofs_.hh>
24
25// make the local view function available whenever we use this class
29#include "elementvariables.hh"
30
32
33template<class P, class V, class IPD>
35{
36 using Problem = P;
37 using Variables = V;
39
40 template<class GridVariablesCache, bool cachingEnabled>
42};
43
48template<class Traits, bool enableCaching>
50
51// specialization in case of storing the local variables
52template<class Traits>
53class HybridCVFEGridVariablesCache<Traits, /*cachingEnabled*/true>
54{
56public:
58 using Problem = typename Traits::Problem;
59
61 using Variables = typename Traits::Variables;
62
64 using InterpolationPointData = typename Traits::InterpolationPointData;
65
67 static constexpr bool cachingEnabled = true;
68
70 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
71
73 using MutableLocalView = typename LocalView::MutableView;
74
75 HybridCVFEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
76
77 template<class GridGeometry, class SolutionVector>
78 void init(const GridGeometry& gridGeometry, const SolutionVector& sol)
79 {
80 variables_.resize(gridGeometry.gridView().size(0));
81 ipDataCache_ = std::make_shared<InterpolationPointDataCache>();
82 ipDataCache_->resize(gridGeometry.gridView().size(0));
83
84 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
85 {
86 const auto element = gridGeometry.element(eIdx);
87 const auto fvGeometry = localView(gridGeometry).bindElement(element);
88
89 // get the element solution
90 auto elemSol = elementSolution(element, sol, gridGeometry);
91
92 variables_[eIdx].resize(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry));
93 for (const auto& localDof : localDofs(fvGeometry))
94 variables_[eIdx][localDof.index()].update(elemSol, problem, fvGeometry, ipData(fvGeometry, localDof));
95
96 ipDataCache_->update(problem, element, fvGeometry, variables_[eIdx]);
97 });
98 }
99
100 template<class GridGeometry, class SolutionVector>
101 void update(const GridGeometry& gridGeometry, const SolutionVector& sol)
102 {
103 if constexpr (InterpolationPointData::isSolDependent)
104 {
105 auto newIpDataCache = std::make_shared<InterpolationPointDataCache>();
106 newIpDataCache->resize(gridGeometry.gridView().size(0));
107
108 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem(), newIpDataCache](const std::size_t eIdx)
109 {
110 const auto element = gridGeometry.element(eIdx);
111 const auto fvGeometry = localView(gridGeometry).bindElement(element);
112
113 // get the element solution
114 auto elemSol = elementSolution(element, sol, gridGeometry);
115
116 for (const auto& localDof : localDofs(fvGeometry))
117 variables_[eIdx][localDof.index()].update(elemSol, problem, fvGeometry, ipData(fvGeometry, localDof));
118
119 newIpDataCache->update(problem, element, fvGeometry, variables_[eIdx]);
120 });
121
122 ipDataCache_ = std::move(newIpDataCache);
123 }
124 else
125 {
126 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
127 {
128 const auto element = gridGeometry.element(eIdx);
129 const auto fvGeometry = localView(gridGeometry).bindElement(element);
130
131 // get the element solution
132 auto elemSol = elementSolution(element, sol, gridGeometry);
133
134 for (const auto& localDof : localDofs(fvGeometry))
135 variables_[eIdx][localDof.index()].update(elemSol, problem, fvGeometry, ipData(fvGeometry, localDof));
136 });
137 }
138 }
139
140 template<class ScvOrLocalDof>
141 const Variables& variables(const ScvOrLocalDof& scvOrLocalDof) const
142 {
143 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
144 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
145 else
146 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
147 }
148
149 template<class ScvOrLocalDof>
150 Variables& variables(const ScvOrLocalDof& scvOrLocalDof)
151 {
152 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
153 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
154 else
155 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
156 }
157
158 const Variables& variables(const std::size_t eIdx, const std::size_t localIdx) const
159 { return variables_[eIdx][localIdx]; }
160
161 Variables& variables(const std::size_t eIdx, const std::size_t localIdx)
162 { return variables_[eIdx][localIdx]; }
163
164 const InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
165 { return ipDataCache_->scvfCache(eIdx, scvfIdx, qpIdx); }
166
167 InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
168 { return ipDataCache_->scvfCache(eIdx, scvfIdx, qpIdx); }
169
170 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
171 { return ipDataCache_->elementCache(eIdx, qpIdx); }
172
173 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
174 { return ipDataCache_->elementCache(eIdx, qpIdx); }
175
176 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
177 { return ipDataCache_->boundaryFaceCache(eIdx, bfIdx, qpIdx); }
178
179 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
180 { return ipDataCache_->boundaryFaceCache(eIdx, bfIdx, qpIdx); }
181
182 const Problem& problem() const
183 { return *problemPtr_; }
184
185private:
186 class InterpolationPointDataCache
187 {
188 struct ElementCache
189 {
190 std::vector<InterpolationPointData> scvfCache;
191 std::vector<std::size_t> qpsOffset;
192 std::vector<InterpolationPointData> elementCache;
193 std::unordered_map<int, std::vector<InterpolationPointData>> boundaryFaceCache;
194
195 template<class Problem, class FVElementGeometry, class ElementVariables>
196 void update(const Problem& problem,
197 const typename FVElementGeometry::Element& element,
198 const FVElementGeometry& fvGeometry,
199 const ElementVariables& elemVars)
200 {
201 qpsOffset.resize(fvGeometry.numScvf() + 1, 0);
202 for (const auto& scvf : scvfs(fvGeometry))
203 {
204 const auto numQps = std::ranges::size(Dumux::CVFE::quadratureRule(fvGeometry, scvf));
205 qpsOffset[scvf.index() + 1] = numQps;
206 }
207 for (std::size_t i = 2; i < qpsOffset.size(); ++i)
208 qpsOffset[i] += qpsOffset[i-1];
209
210 scvfCache.resize(qpsOffset.back());
211 for (const auto& scvf : scvfs(fvGeometry))
212 {
213 for (const auto& qpData : Dumux::CVFE::quadratureRule(fvGeometry, scvf))
214 {
215 const auto scvfIdx = qpData.ipData().scvfIndex();
216 const auto qpIdx = qpData.ipData().qpIndex();
217 scvfCache[qpsOffset[scvfIdx] + qpIdx].update(problem,
218 element,
219 fvGeometry,
220 elemVars,
221 qpData.ipData());
222 }
223 }
224
225 const auto elemQuadRule = Dumux::CVFE::quadratureRule(fvGeometry, element);
226 elementCache.resize(std::ranges::size(elemQuadRule));
227 for (const auto& qpData : elemQuadRule)
228 elementCache[qpData.ipData().qpIndex()].update(problem, element, fvGeometry, elemVars, qpData.ipData());
229
230 boundaryFaceCache.clear();
231 for (const auto& boundaryFace : boundaryFaces(fvGeometry))
232 {
233 auto& bfCache = boundaryFaceCache[boundaryFace.index()];
234 const auto quadRule = Dumux::CVFE::quadratureRule(fvGeometry, boundaryFace);
235 bfCache.resize(std::ranges::size(quadRule));
236 for (const auto& qpData : quadRule)
237 bfCache[qpData.ipData().qpIndex()].update(problem,
238 element,
239 fvGeometry,
240 elemVars,
241 qpData.ipData());
242 }
243 }
244 };
245
246 public:
247
248 InterpolationPointDataCache()
249 {}
250
251 void resize(const std::size_t numElements)
252 {
253 elementCaches_.resize(numElements);
254 }
255
256 template<class Problem, class FVElementGeometry, class ElementVariables>
257 void update(const Problem& problem,
258 const typename FVElementGeometry::Element& element,
259 const FVElementGeometry& fvGeometry,
260 const ElementVariables& elemVars)
261 {
262 const auto eIdx = fvGeometry.gridGeometry().elementMapper().index(element);
263 elementCaches_[eIdx].update(problem, element, fvGeometry, elemVars);
264 }
265
266 // access operator
267 const InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
268 {
269 const auto& elementCache = elementCaches_[eIdx];
270 return elementCache.scvfCache[elementCache.qpsOffset[scvfIdx] + qpIdx];
271 }
272
273 // access operator
274 InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
275 {
276 auto& elementCache = elementCaches_[eIdx];
277 return elementCache.scvfCache[elementCache.qpsOffset[scvfIdx] + qpIdx];
278 }
279
280 // access operator
281 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
282 { return elementCaches_[eIdx].elementCache[qpIdx]; }
283
284 // access operator
285 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
286 { return elementCaches_[eIdx].elementCache[qpIdx]; }
287
288 // access operator
289 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
290 { return elementCaches_[eIdx].boundaryFaceCache.at(bfIdx)[qpIdx]; }
291
292 // access operator
293 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
294 { return elementCaches_[eIdx].boundaryFaceCache[bfIdx][qpIdx]; }
295
296 const ElementCache& cache(std::size_t eIdx) const
297 { return elementCaches_[eIdx]; }
298
299 ElementCache& cache(std::size_t eIdx)
300 { return elementCaches_[eIdx]; }
301
302 private:
303 std::vector<ElementCache> elementCaches_;
304 };
305
306public:
307 const auto& cache(std::size_t eIdx) const
308 { return ipDataCache_->cache(eIdx); }
309
310 auto& cache(std::size_t eIdx)
311 { return ipDataCache_->cache(eIdx); }
312
313private:
314 const Problem* problemPtr_;
315 std::vector<std::vector<Variables>> variables_;
316 std::shared_ptr<InterpolationPointDataCache> ipDataCache_;
317};
318
319// Specialization when the current local variables are not stored
320template<class Traits>
321class HybridCVFEGridVariablesCache<Traits, /*cachingEnabled*/false>
322{
324
325public:
327 using Problem = typename Traits::Problem;
328
330 using Variables = typename Traits::Variables;
331
333 static constexpr bool cachingEnabled = false;
334
336 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
337
339 using MutableLocalView = typename LocalView::MutableView;
340
342 using InterpolationPointData = typename Traits::InterpolationPointData;
343
344 HybridCVFEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
345
346 template<class GridGeometry, class SolutionVector>
347 void update(const GridGeometry& gridGeometry, const SolutionVector& sol) {}
348
349 const Problem& problem() const
350 { return *problemPtr_;}
351
352private:
353 const Problem* problemPtr_;
354};
355
356} // end namespace Dumux::Experimental::CVFE
357
358#endif
The (stencil) element variables class for hybrid control-volume finite element.
Definition: hybrid/elementvariables.hh:40
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition: hybrid/gridvariablescache.hh:342
HybridCVFEGridVariablesCache(const Problem &problem)
Definition: hybrid/gridvariablescache.hh:344
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: hybrid/gridvariablescache.hh:336
const Problem & problem() const
Definition: hybrid/gridvariablescache.hh:349
typename Traits::Problem Problem
export the problem type
Definition: hybrid/gridvariablescache.hh:327
typename Traits::Variables Variables
export the variables type
Definition: hybrid/gridvariablescache.hh:330
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition: hybrid/gridvariablescache.hh:339
void update(const GridGeometry &gridGeometry, const SolutionVector &sol)
Definition: hybrid/gridvariablescache.hh:347
void update(const GridGeometry &gridGeometry, const SolutionVector &sol)
Definition: hybrid/gridvariablescache.hh:101
Variables & variables(const std::size_t eIdx, const std::size_t localIdx)
Definition: hybrid/gridvariablescache.hh:161
void init(const GridGeometry &gridGeometry, const SolutionVector &sol)
Definition: hybrid/gridvariablescache.hh:78
Variables & variables(const ScvOrLocalDof &scvOrLocalDof)
Definition: hybrid/gridvariablescache.hh:150
auto & cache(std::size_t eIdx)
Definition: hybrid/gridvariablescache.hh:310
typename Traits::Variables Variables
export the variables type
Definition: hybrid/gridvariablescache.hh:61
const InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
Definition: hybrid/gridvariablescache.hh:176
InterpolationPointData & scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
Definition: hybrid/gridvariablescache.hh:167
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition: hybrid/gridvariablescache.hh:73
const InterpolationPointData & scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
Definition: hybrid/gridvariablescache.hh:164
HybridCVFEGridVariablesCache(const Problem &problem)
Definition: hybrid/gridvariablescache.hh:75
const auto & cache(std::size_t eIdx) const
Definition: hybrid/gridvariablescache.hh:307
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition: hybrid/gridvariablescache.hh:64
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: hybrid/gridvariablescache.hh:70
const Problem & problem() const
Definition: hybrid/gridvariablescache.hh:182
typename Traits::Problem Problem
export the problem type
Definition: hybrid/gridvariablescache.hh:58
InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
Definition: hybrid/gridvariablescache.hh:179
const Variables & variables(const ScvOrLocalDof &scvOrLocalDof) const
Definition: hybrid/gridvariablescache.hh:141
InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx)
Definition: hybrid/gridvariablescache.hh:173
const Variables & variables(const std::size_t eIdx, const std::size_t localIdx) const
Definition: hybrid/gridvariablescache.hh:158
const InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx) const
Definition: hybrid/gridvariablescache.hh:170
The grid variables cache class for hybrid control-volume finite element methods.
Definition: hybrid/gridvariablescache.hh:49
The local element solution class for control-volume finite element methods.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:26
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethods::cctpfa||GridGeometry::discMethod==DiscretizationMethods::ccmpfa, CCElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for cell-centered schemes.
Definition: cellcentered/elementsolution.hh:101
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading)
Definition: parallel_for.hh:160
The element variables class for hybrid control-volume finite element methods.
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: hybrid/elementvariables.hh:30
auto localDofs(const FVElementGeometry &fvGeometry)
range over local dofs
Definition: localdof.hh:50
Parallel for loop (multithreading)
Quadrature rules over sub-control volumes and sub-control volume faces.
IPD InterpolationPointData
Definition: hybrid/gridvariablescache.hh:38
P Problem
Definition: hybrid/gridvariablescache.hh:36
V Variables
Definition: hybrid/gridvariablescache.hh:37