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