12#ifndef DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_VARIABLES_CACHE_HH
13#define DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_VARIABLES_CACHE_HH
15#include <unordered_map>
23#include <dumux/common/concepts/localdofs_.hh>
33template<
class P,
class V,
class IPD>
40 template<
class Gr
idVariablesCache,
bool cachingEnabled>
48template<
class Traits,
bool enableCaching>
67 static constexpr bool cachingEnabled =
true;
77 template<
class Gr
idGeometry,
class SolutionVector>
78 void init(
const GridGeometry& gridGeometry,
const SolutionVector& sol)
80 variables_.resize(gridGeometry.gridView().size(0));
81 ipDataCache_ = std::make_shared<InterpolationPointDataCache>();
82 ipDataCache_->resize(gridGeometry.gridView().size(0));
84 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](
const std::size_t eIdx)
86 const auto element = gridGeometry.element(eIdx);
87 const auto fvGeometry =
localView(gridGeometry).bindElement(element);
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));
96 ipDataCache_->update(problem, element, fvGeometry, variables_[eIdx]);
100 template<
class Gr
idGeometry,
class SolutionVector>
101 void update(
const GridGeometry& gridGeometry,
const SolutionVector& sol)
103 if constexpr (InterpolationPointData::isSolDependent)
105 auto newIpDataCache = std::make_shared<InterpolationPointDataCache>();
106 newIpDataCache->resize(gridGeometry.gridView().size(0));
108 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem(), newIpDataCache](
const std::size_t eIdx)
110 const auto element = gridGeometry.element(eIdx);
111 const auto fvGeometry =
localView(gridGeometry).bindElement(element);
116 for (
const auto& localDof :
localDofs(fvGeometry))
117 variables_[eIdx][localDof.index()].update(elemSol, problem, fvGeometry, ipData(fvGeometry, localDof));
119 newIpDataCache->update(problem, element, fvGeometry, variables_[eIdx]);
122 ipDataCache_ = std::move(newIpDataCache);
126 Dumux::parallelFor(gridGeometry.gridView().size(0), [&, &problem = problem()](
const std::size_t eIdx)
128 const auto element = gridGeometry.element(eIdx);
129 const auto fvGeometry =
localView(gridGeometry).bindElement(element);
134 for (
const auto& localDof :
localDofs(fvGeometry))
135 variables_[eIdx][localDof.index()].update(elemSol, problem, fvGeometry, ipData(fvGeometry, localDof));
140 template<
class ScvOrLocalDof>
143 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
144 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
146 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
149 template<
class ScvOrLocalDof>
152 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
153 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
155 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
159 {
return variables_[eIdx][localIdx]; }
162 {
return variables_[eIdx][localIdx]; }
165 {
return ipDataCache_->scvfCache(eIdx, scvfIdx, qpIdx); }
168 {
return ipDataCache_->scvfCache(eIdx, scvfIdx, qpIdx); }
171 {
return ipDataCache_->elementCache(eIdx, qpIdx); }
174 {
return ipDataCache_->elementCache(eIdx, qpIdx); }
177 {
return ipDataCache_->boundaryIntersectionCache(eIdx, intersectionIdx, qpIdx); }
180 {
return ipDataCache_->boundaryIntersectionCache(eIdx, intersectionIdx, qpIdx); }
183 {
return *problemPtr_; }
186 class InterpolationPointDataCache
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>> boundaryIntersectionCache;
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)
201 qpsOffset.resize(fvGeometry.numScvf() + 1, 0);
202 for (
const auto& scvf : scvfs(fvGeometry))
205 qpsOffset[scvf.index() + 1] = numQps;
207 for (std::size_t i = 2; i < qpsOffset.size(); ++i)
208 qpsOffset[i] += qpsOffset[i-1];
210 scvfCache.resize(qpsOffset.back());
211 for (
const auto& scvf : scvfs(fvGeometry))
215 const auto scvfIdx = qpData.ipData().scvfIndex();
216 const auto qpIdx = qpData.ipData().qpIndex();
217 scvfCache[qpsOffset[scvfIdx] + qpIdx].update(problem,
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());
230 boundaryIntersectionCache.clear();
231 for (
const auto& intersection : intersections(fvGeometry.gridGeometry().gridView(), element))
233 if (intersection.boundary())
235 const auto intersectionIndex = intersection.indexInInside();
236 auto& boundaryCache = boundaryIntersectionCache[intersectionIndex];
238 boundaryCache.resize(std::ranges::size(quadRule));
239 for (
const auto& qpData : quadRule)
240 boundaryCache[qpData.ipData().qpIndex()].update(problem,
252 InterpolationPointDataCache()
255 void resize(
const std::size_t numElements)
257 elementCaches_.resize(numElements);
260 template<
class Problem,
class FVElementGeometry,
class ElementVariables>
261 void update(
const Problem& problem,
262 const typename FVElementGeometry::Element& element,
263 const FVElementGeometry& fvGeometry,
264 const ElementVariables& elemVars)
266 const auto eIdx = fvGeometry.gridGeometry().elementMapper().index(element);
267 elementCaches_[eIdx].update(problem, element, fvGeometry, elemVars);
271 const InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
const
273 const auto& elementCache = elementCaches_[eIdx];
274 return elementCache.scvfCache[elementCache.qpsOffset[scvfIdx] + qpIdx];
278 InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
280 auto& elementCache = elementCaches_[eIdx];
281 return elementCache.scvfCache[elementCache.qpsOffset[scvfIdx] + qpIdx];
285 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
const
286 {
return elementCaches_[eIdx].elementCache[qpIdx]; }
289 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
290 {
return elementCaches_[eIdx].elementCache[qpIdx]; }
293 const InterpolationPointData& boundaryIntersectionCache(std::size_t eIdx,
int intersectionIdx, std::size_t qpIdx)
const
294 {
return elementCaches_[eIdx].boundaryIntersectionCache.at(intersectionIdx)[qpIdx]; }
297 InterpolationPointData& boundaryIntersectionCache(std::size_t eIdx,
int intersectionIdx, std::size_t qpIdx)
298 {
return elementCaches_[eIdx].boundaryIntersectionCache[intersectionIdx][qpIdx]; }
300 const ElementCache& cache(std::size_t eIdx)
const
301 {
return elementCaches_[eIdx]; }
303 ElementCache& cache(std::size_t eIdx)
304 {
return elementCaches_[eIdx]; }
307 std::vector<ElementCache> elementCaches_;
311 const auto&
cache(std::size_t eIdx)
const
312 {
return ipDataCache_->cache(eIdx); }
315 {
return ipDataCache_->cache(eIdx); }
318 const Problem* problemPtr_;
319 std::vector<std::vector<Variables>> variables_;
320 std::shared_ptr<InterpolationPointDataCache> ipDataCache_;
324template<
class Traits>
337 static constexpr bool cachingEnabled =
false;
350 template<
class Gr
idGeometry,
class SolutionVector>
351 void update(
const GridGeometry& gridGeometry,
const SolutionVector& sol) {}
354 {
return *problemPtr_;}
357 const Problem* problemPtr_;
The (stencil) element variables class for hybrid control-volume finite element.
Definition: hybrid/elementvariables.hh:40
Definition: hybrid/gridvariablescache.hh:326
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition: hybrid/gridvariablescache.hh:346
HybridCVFEGridVariablesCache(const Problem &problem)
Definition: hybrid/gridvariablescache.hh:348
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition: hybrid/gridvariablescache.hh:340
const Problem & problem() const
Definition: hybrid/gridvariablescache.hh:353
typename Traits::Problem Problem
export the problem type
Definition: hybrid/gridvariablescache.hh:331
typename Traits::Variables Variables
export the variables type
Definition: hybrid/gridvariablescache.hh:334
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition: hybrid/gridvariablescache.hh:343
void update(const GridGeometry &gridGeometry, const SolutionVector &sol)
Definition: hybrid/gridvariablescache.hh:351
Definition: hybrid/gridvariablescache.hh:54
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
InterpolationPointData & boundaryIntersectionCache(std::size_t eIdx, int intersectionIdx, std::size_t qpIdx)
Definition: hybrid/gridvariablescache.hh:179
Variables & variables(const ScvOrLocalDof &scvOrLocalDof)
Definition: hybrid/gridvariablescache.hh:150
auto & cache(std::size_t eIdx)
Definition: hybrid/gridvariablescache.hh:314
typename Traits::Variables Variables
export the variables type
Definition: hybrid/gridvariablescache.hh:61
InterpolationPointData & scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
Definition: hybrid/gridvariablescache.hh:167
const InterpolationPointData & boundaryIntersectionCache(std::size_t eIdx, int intersectionIdx, std::size_t qpIdx) const
Definition: hybrid/gridvariablescache.hh:176
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:311
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
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:148
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.
Definition: hybrid/gridvariablescache.hh:35
IPD InterpolationPointData
Definition: hybrid/gridvariablescache.hh:38
P Problem
Definition: hybrid/gridvariablescache.hh:36
V Variables
Definition: hybrid/gridvariablescache.hh:37