version 3.11-dev
hybrid/elementvariables.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_ELEMENT_VARIABLES_HH
13#define DUMUX_DISCRETIZATION_HYBRID_CVFE_ELEMENT_VARIABLES_HH
14
15#include <array>
16#include <optional>
17#include <ranges>
18#include <type_traits>
19#include <utility>
20#include <vector>
21#include <memory>
22
23#include <dumux/common/concepts/ipdata_.hh>
24#include <dumux/common/concepts/localdofs_.hh>
27
29
31
39template<class GVC, bool cachingEnabled>
41
47template<class GVC>
48class HybridCVFEElementVariables<GVC, /*cachingEnabled*/true>
49{
50 class MutableVariablesView
51 {
52 public:
53 MutableVariablesView(GVC& gridCache)
54 : gridCache_(gridCache) {}
55
56 using Variables = typename GVC::Variables;
57
58 template<Dumux::Concept::LocalDof LocalDof>
59 Variables& operator [](const LocalDof& localDof) const
60 { return gridCache_.variables(localDof); }
61 private:
62 GVC& gridCache_;
63 };
64
65 class MutableVariablesViewWithIpCacheAccess
66 {
67 public:
68 MutableVariablesViewWithIpCacheAccess(GVC& gridCache)
69 : gridCache_(gridCache)
70 {}
71
72 using Variables = typename GVC::Variables;
73
74 template<Dumux::Concept::LocalDof LocalDof>
75 Variables& operator [](const LocalDof& localDof) const
76 { return gridCache_.variables(localDof); }
77
78 auto& cache(std::size_t eIdx) const
79 { return gridCache_.cache(eIdx); }
80
81 private:
82 GVC& gridCache_;
83 };
84
85public:
87 using GridVariablesCache = GVC;
88
90 using InterpolationPointData = typename GridVariablesCache::InterpolationPointData;
91
93 using MutableView = std::conditional_t<
94 InterpolationPointData::isSolDependent,
95 MutableVariablesViewWithIpCacheAccess,
96 MutableVariablesView
97 >;
98
100 using Variables = typename GridVariablesCache::Variables;
101
103 template<class FVElementGeometry>
104 using DeflectionPolicy = std::conditional_t<
105 InterpolationPointData::isSolDependent,
108 >;
109
112 : gridVariablesCachePtr_(&gridVariablesCache) {}
113
114 const Variables& operator [](std::size_t localDofIdx) const
115 { return gridVariablesCache().variables(eIdx_, localDofIdx); }
116
117 template<Dumux::Concept::LocalDof LocalDof>
118 const Variables& operator [](const LocalDof& localDof) const
119 { return gridVariablesCache().variables(localDof); }
120
121 template<class IpData>
122 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
123 const Variables& operator [](const IpData& ipData) const
124 { return gridVariablesCache().variables(eIdx_, ipData.localDofIndex()); }
125
126 template<Concept::ScvfQpIpData IpData>
128 const IpData& ipData)
129 { return elemVars.gridVariablesCache().scvfCache(elemVars.eIdx_, ipData.scvfIndex(), ipData.qpIndex()); }
130
131 template<Concept::IntersectionQpIpData IpData>
133 const IpData& ipData)
134 { return elemVars.gridVariablesCache().boundaryIntersectionCache(elemVars.eIdx_, ipData.intersectionIndex(), ipData.qpIndex()); }
135
136 template<Concept::QIpData IpData>
138 const IpData& ipData)
139 { return elemVars.gridVariablesCache().elementCache(elemVars.eIdx_, ipData.qpIndex()); }
140
146 template<class FVElementGeometry, class SolutionVector>
147 HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
148 const FVElementGeometry& fvGeometry,
149 const SolutionVector& sol) &&
150 {
151 this->bindElement(element, fvGeometry, sol);
152 return std::move(*this);
153 }
154
155 // For compatibility reasons with the case of not storing the variables.
156 // function to be called before assembling an element, preparing the variables within the stencil
157 template<class FVElementGeometry, class SolutionVector>
158 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
159 const FVElementGeometry& fvGeometry,
160 const SolutionVector& sol) &
161 {
162 bindElement(element, fvGeometry, sol);
163 }
164
170 template<class FVElementGeometry, class SolutionVector>
171 HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
172 const FVElementGeometry& fvGeometry,
173 const SolutionVector& sol) &&
174 {
175 this->bindElement(element, fvGeometry, sol);
176 return std::move(*this);
177 }
178
179 // function to prepare the variables within the element
180 template<class FVElementGeometry, class SolutionVector>
181 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
182 const FVElementGeometry& fvGeometry,
183 const SolutionVector& sol) &
184 {
185 eIdx_ = fvGeometry.gridGeometry().elementMapper().index(element);
186 }
187
190 { return *gridVariablesCachePtr_; }
191
197 { return { gridVars }; }
198
199private:
200 const GridVariablesCache* gridVariablesCachePtr_;
201 std::size_t eIdx_;
202};
203
208template<class GVC>
209class HybridCVFEElementVariables<GVC, /*cachingEnabled*/false>
210{
211 using ThisType = HybridCVFEElementVariables<GVC, /*cachingEnabled*/false>;
212 using GridGeometry = std::decay_t<decltype(std::declval<GVC>().problem().gridGeometry())>;
213 using GridView = typename GridGeometry::GridView;
214
216 static constexpr std::size_t maxNumIntersections = GridView::dimension << 1;
217
218 class MutableVariablesView
219 {
220 public:
221 MutableVariablesView(ThisType& view)
222 : view_(view) {}
223
224 using Variables = typename GVC::Variables;
225
226 template<Dumux::Concept::LocalDof LocalDof>
227 Variables& operator [](const LocalDof& localDof) const
228 { return view_[localDof]; }
229 private:
230 ThisType& view_;
231 };
232
233 class MutableVariablesViewWithIpCacheAccess
234 {
235 public:
236 MutableVariablesViewWithIpCacheAccess(ThisType& view)
237 : view_(view)
238 {}
239
240 using Variables = typename GVC::Variables;
241
242 template<Dumux::Concept::LocalDof LocalDof>
243 Variables& operator [](const LocalDof& localDof) const
244 { return view_[localDof]; }
245
246 auto& cache(std::size_t) const
247 { return *view_.ipDataCache_; }
248
249 private:
250 ThisType& view_;
251 };
252
253public:
256
258 using InterpolationPointData = typename GridVariablesCache::InterpolationPointData;
259
261 using MutableView = std::conditional_t<
262 InterpolationPointData::isSolDependent,
263 MutableVariablesViewWithIpCacheAccess,
264 MutableVariablesView
265 >;
266
268 using Variables = typename GridVariablesCache::Variables;
269
271 template<class FVElementGeometry>
272 using DeflectionPolicy = std::conditional_t<
273 InterpolationPointData::isSolDependent,
276 >;
277
280 : gridVariablesCachePtr_(&gridVarsCache)
281 , ipDataCache_(std::make_shared<InterpolationPointDataCache>())
282 {}
283
289 template<class FVElementGeometry, class SolutionVector>
290 HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
291 const FVElementGeometry& fvGeometry,
292 const SolutionVector& sol) &&
293 {
294 this->bindElement(element, fvGeometry, sol);
295 return std::move(*this);
296 }
297
298 // specialization for control-volume finite element, simply forwards to the bindElement method
299 template<class FVElementGeometry, class SolutionVector>
300 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
301 const FVElementGeometry& fvGeometry,
302 const SolutionVector& sol) &
303 {
304 bindElement(element, fvGeometry, sol);
305 }
306
312 template<class FVElementGeometry, class SolutionVector>
313 HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
314 const FVElementGeometry& fvGeometry,
315 const SolutionVector& sol) &&
316 {
317 this->bindElement(element, fvGeometry, sol);
318 return std::move(*this);
319 }
320
321 // specialization for control-volume finite element
322 template<class FVElementGeometry, class SolutionVector>
323 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
324 const FVElementGeometry& fvGeometry,
325 const SolutionVector& sol) &
326 {
327 // get the solution at the dofs of the element
328 auto elemSol = elementSolution(element, sol, fvGeometry.gridGeometry());
329
330 // resize variables to the required size
331 variables_.resize(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry));
332
333 // update variables related to localDofs
334 for (const auto& localDof : localDofs(fvGeometry))
335 variables_[localDof.index()].update(elemSol, gridVariablesCache().problem(), fvGeometry, ipData(fvGeometry, localDof));
336
337 if constexpr (InterpolationPointData::isSolDependent)
338 {
339 auto newIpDataCache = std::make_shared<InterpolationPointDataCache>(*ipDataCache_);
340 newIpDataCache->update(gridVariablesCache().problem(), element, fvGeometry, variables_);
341 ipDataCache_ = std::move(newIpDataCache);
342 }
343 else
344 ipDataCache_->update(gridVariablesCache().problem(), element, fvGeometry, variables_);
345 }
346
347 const Variables& operator [](std::size_t localIdx) const
348 { return variables_[localIdx]; }
349
350 Variables& operator [](std::size_t localIdx)
351 { return variables_[localIdx]; }
352
353 template<Dumux::Concept::LocalDof LocalDof>
354 const Variables& operator [](const LocalDof& localDof) const
355 { return variables_[localDof.index()]; }
356
357 template<Dumux::Concept::LocalDof LocalDof>
358 Variables& operator [](const LocalDof& localDof)
359 { return variables_[localDof.index()]; }
360
361 template<class IpData>
362 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
363 const Variables& operator [](const IpData& ipData) const
364 { return variables_[ipData.localDofIndex()]; }
365
366 template<class IpData>
367 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
368 Variables& operator [](const IpData& ipData)
369 { return variables_[ipData.localDofIndex()]; }
370
371 template<Concept::ScvfQpIpData IpData>
373 const IpData& ipData)
374 { return elemVars.ipDataCache_->scvfCache(ipData.scvfIndex(), ipData.qpIndex()); }
375
376 template<Concept::IntersectionQpIpData IpData>
378 const IpData& ipData)
379 { return elemVars.ipDataCache_->boundaryIntersectionCache(ipData.intersectionIndex(), ipData.qpIndex()); }
380
381 template<Concept::QIpData IpData>
383 const IpData& ipData)
384 { return elemVars.ipDataCache_->elementCache(ipData.qpIndex()); }
385
388 { return *gridVariablesCachePtr_; }
389
395 { return { *this }; }
396
397private:
398 class InterpolationPointDataCache
399 {
400 public:
401 InterpolationPointDataCache()
402 {}
403
404 template<class Problem, class FVElementGeometry, class ElementVariables>
405 void update(const Problem& problem,
406 const typename FVElementGeometry::Element& element,
407 const FVElementGeometry& fvGeometry,
408 const ElementVariables& elemVars)
409 {
410 updateElementCache_(problem, element, fvGeometry, elemVars);
411 }
412
413 // access operator
414 const InterpolationPointData& scvfCache(std::size_t scvfIdx, std::size_t qpIdx) const
415 { return scvfCache_[scvfIdx][qpIdx]; }
416
417 // access operator
418 InterpolationPointData& scvfCache(std::size_t scvfIdx, std::size_t qpIdx)
419 { return scvfCache_[scvfIdx][qpIdx]; }
420
421 // access operator
422 const InterpolationPointData& elementCache(std::size_t qpIdx) const
423 { return elementCache_[qpIdx]; }
424
425 // access operator
426 InterpolationPointData& elementCache(std::size_t qpIdx)
427 { return elementCache_[qpIdx]; }
428
429 // access operator
430 const InterpolationPointData& boundaryIntersectionCache(std::size_t intersectionIdx, std::size_t qpIdx) const
431 { return (*boundaryIntersectionCache_[intersectionIdx])[qpIdx]; }
432
433 // access operator
434 InterpolationPointData& boundaryIntersectionCache(std::size_t intersectionIdx, std::size_t qpIdx)
435 { return (*boundaryIntersectionCache_[intersectionIdx])[qpIdx]; }
436
437 private:
438 template<class Problem, class FVElementGeometry, class ElementVariables>
439 void updateElementCache_(const Problem& problem,
440 const typename FVElementGeometry::Element& element,
441 const FVElementGeometry& fvGeometry,
442 const ElementVariables& elemVars)
443 {
444 scvfCache_.resize(fvGeometry.numScvf());
445 for (const auto& scvf : scvfs(fvGeometry))
446 {
447 const auto quadRule = Dumux::CVFE::quadratureRule(fvGeometry, scvf);
448 scvfCache_[scvf.index()].resize(std::ranges::size(quadRule));
449 for (const auto& qpData : quadRule)
450 scvfCache_[scvf.index()][qpData.ipData().qpIndex()].update(
451 problem, element, fvGeometry, elemVars, qpData.ipData()
452 );
453 }
454
455 const auto elemQuadRule = Dumux::CVFE::quadratureRule(fvGeometry, element);
456 elementCache_.resize(std::ranges::size(elemQuadRule));
457 for (const auto& qpData : elemQuadRule)
458 elementCache_[qpData.ipData().qpIndex()].update(problem, element, fvGeometry, elemVars, qpData.ipData());
459
460 for (const auto& intersection : intersections(fvGeometry.gridGeometry().gridView(), element))
461 {
462 const auto intersectionIndex = intersection.indexInInside();
463 if (intersection.boundary())
464 {
465 auto& boundaryCache = boundaryIntersectionCache_[intersectionIndex];
466 boundaryCache.emplace();
467 const auto quadRule = Dumux::CVFE::quadratureRule(fvGeometry, intersection);
468 boundaryCache->resize(std::ranges::size(quadRule));
469 for (const auto& qpData : quadRule)
470 (*boundaryCache)[qpData.ipData().qpIndex()].update(problem,
471 element,
472 fvGeometry,
473 elemVars,
474 qpData.ipData());
475 }
476 else
477 boundaryIntersectionCache_[intersectionIndex].reset();
478 }
479 }
480
481 std::vector<std::vector<InterpolationPointData>> scvfCache_;
482 std::vector<InterpolationPointData> elementCache_;
483 std::array<std::optional<std::vector<InterpolationPointData>>, maxNumIntersections> boundaryIntersectionCache_;
484 };
485
486 const GridVariablesCache* gridVariablesCachePtr_;
487 std::vector<Variables> variables_;
488 std::shared_ptr<InterpolationPointDataCache> ipDataCache_;
489};
490
491} // end namespace Dumux::Experimental::CVFE
492
493#endif
Definition: variablesdeflectionpolicy.hh:29
Definition: variablesdeflectionpolicy.hh:95
The local (stencil) element variables class for control-volume finite element without caching.
Definition: hybrid/elementvariables.hh:210
MutableView asMutableView(GridVariablesCache &)
return a local view on variables that is always mutable, regardless of the caching policy
Definition: hybrid/elementvariables.hh:394
HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: hybrid/elementvariables.hh:290
HybridCVFEElementVariables(const GridVariablesCache &gridVarsCache)
Constructor.
Definition: hybrid/elementvariables.hh:279
void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition: hybrid/elementvariables.hh:300
std::conditional_t< InterpolationPointData::isSolDependent, MutableVariablesViewWithIpCacheAccess, MutableVariablesView > MutableView
export type of the mutable version of the view
Definition: hybrid/elementvariables.hh:265
friend const InterpolationPointData & cache(const HybridCVFEElementVariables &elemVars, const IpData &ipData)
Definition: hybrid/elementvariables.hh:372
std::conditional_t< InterpolationPointData::isSolDependent, Dumux::Detail::CVFE::VariablesDeflectionPolicyWithIpCacheUpdate< MutableView, FVElementGeometry >, Dumux::Detail::CVFE::VariablesDeflectionPolicy< MutableView, FVElementGeometry > > DeflectionPolicy
export type of deflection policy
Definition: hybrid/elementvariables.hh:276
GVC GridVariablesCache
export type of the grid variables cache
Definition: hybrid/elementvariables.hh:255
typename GridVariablesCache::InterpolationPointData InterpolationPointData
export interpolation point data
Definition: hybrid/elementvariables.hh:258
const GridVariablesCache & gridVariablesCache() const
The grid variables cache object we are a restriction of.
Definition: hybrid/elementvariables.hh:387
HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: hybrid/elementvariables.hh:313
void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition: hybrid/elementvariables.hh:323
typename GridVariablesCache::Variables Variables
export type of the variables
Definition: hybrid/elementvariables.hh:268
const GridVariablesCache & gridVariablesCache() const
The grid variables cache object we are a restriction of.
Definition: hybrid/elementvariables.hh:189
MutableView asMutableView(GridVariablesCache &gridVars)
return a local view on variables that is always mutable, regardless of the caching policy
Definition: hybrid/elementvariables.hh:196
GVC GridVariablesCache
export type of the grid variables cache
Definition: hybrid/elementvariables.hh:87
typename GridVariablesCache::InterpolationPointData InterpolationPointData
export interpolation point data
Definition: hybrid/elementvariables.hh:90
std::conditional_t< InterpolationPointData::isSolDependent, Dumux::Detail::CVFE::VariablesDeflectionPolicyWithIpCacheUpdate< MutableView, FVElementGeometry >, Dumux::Detail::CVFE::VariablesDeflectionPolicy< MutableView, FVElementGeometry > > DeflectionPolicy
export type of deflection policy
Definition: hybrid/elementvariables.hh:108
friend const InterpolationPointData & cache(const HybridCVFEElementVariables &elemVars, const IpData &ipData)
Definition: hybrid/elementvariables.hh:127
void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition: hybrid/elementvariables.hh:181
HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: hybrid/elementvariables.hh:147
std::conditional_t< InterpolationPointData::isSolDependent, MutableVariablesViewWithIpCacheAccess, MutableVariablesView > MutableView
export type of the mutable version of the view
Definition: hybrid/elementvariables.hh:97
HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: hybrid/elementvariables.hh:171
void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition: hybrid/elementvariables.hh:158
typename GridVariablesCache::Variables Variables
export type of the variables
Definition: hybrid/elementvariables.hh:100
HybridCVFEElementVariables(const GridVariablesCache &gridVariablesCache)
Constructor.
Definition: hybrid/elementvariables.hh:111
The (stencil) element variables class for hybrid control-volume finite element.
Definition: hybrid/elementvariables.hh:40
Element solution classes and factory functions.
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
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
Quadrature rules over sub-control volumes and sub-control volume faces.
Variables deflection policy.