version 3.11-dev
Loading...
Searching...
No Matches
cvfe/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>
29
31
33
41template<class GVC, bool cachingEnabled>
43
49template<class GVC>
50class HybridCVFEElementVariables<GVC, /*cachingEnabled*/true>
51{
52 class MutableVariablesView
53 {
54 public:
55 MutableVariablesView(GVC& gridCache)
56 : gridCache_(gridCache) {}
57
58 using Variables = typename GVC::Variables;
59
60 template<Dumux::Concept::LocalDof LocalDof>
61 Variables& operator [](const LocalDof& localDof) const
62 { return gridCache_.variables(localDof); }
63 private:
64 GVC& gridCache_;
65 };
66
67 class MutableVariablesViewWithIpCacheAccess
68 {
69 public:
70 MutableVariablesViewWithIpCacheAccess(GVC& gridCache)
71 : gridCache_(gridCache)
72 {}
73
74 using Variables = typename GVC::Variables;
75
76 template<Dumux::Concept::LocalDof LocalDof>
77 Variables& operator [](const LocalDof& localDof) const
78 { return gridCache_.variables(localDof); }
79
80 auto& cache(std::size_t eIdx) const
81 { return gridCache_.cache(eIdx); }
82
83 private:
84 GVC& gridCache_;
85 };
86
87public:
89 using GridVariablesCache = GVC;
90
92 using InterpolationPointData = typename GridVariablesCache::InterpolationPointData;
93
95 using MutableView = std::conditional_t<
96 InterpolationPointData::isSolDependent,
97 MutableVariablesViewWithIpCacheAccess,
98 MutableVariablesView
99 >;
100
102 using Variables = typename GridVariablesCache::Variables;
103
105 template<class FVElementGeometry>
106 using DeflectionPolicy = std::conditional_t<
107 InterpolationPointData::isSolDependent,
110 >;
111
115
116 const Variables& operator [](std::size_t localDofIdx) const
117 { return gridVariablesCache().variables(eIdx_, localDofIdx); }
118
119 template<Dumux::Concept::LocalDof LocalDof>
120 const Variables& operator [](const LocalDof& localDof) const
121 { return gridVariablesCache().variables(localDof); }
122
123 template<class IpData>
124 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
125 const Variables& operator [](const IpData& ipData) const
126 { return gridVariablesCache().variables(eIdx_, ipData.localDofIndex()); }
127
128 template<Concept::ScvfQpIpData IpData>
130 const IpData& ipData)
131 { return elemVars.gridVariablesCache().scvfCache(elemVars.eIdx_, ipData.scvfIndex(), ipData.qpIndex()); }
132
133 template<Concept::BoundaryFaceQpIpData IpData>
135 const IpData& ipData)
136 { return elemVars.gridVariablesCache().boundaryFaceCache(elemVars.eIdx_, ipData.boundaryFaceIndex(), ipData.qpIndex()); }
137
138 template<Concept::QIpData IpData>
140 const IpData& ipData)
141 { return elemVars.gridVariablesCache().elementCache(elemVars.eIdx_, ipData.qpIndex()); }
142
148 template<class FVElementGeometry, class SolutionVector>
149 HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
150 const FVElementGeometry& fvGeometry,
151 const SolutionVector& sol) &&
152 {
153 this->bindElement(element, fvGeometry, sol);
154 return std::move(*this);
155 }
156
157 // For compatibility reasons with the case of not storing the variables.
158 // function to be called before assembling an element, preparing the variables within the stencil
159 template<class FVElementGeometry, class SolutionVector>
160 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
161 const FVElementGeometry& fvGeometry,
162 const SolutionVector& sol) &
163 {
164 bindElement(element, fvGeometry, sol);
165 }
166
172 template<class FVElementGeometry, class SolutionVector>
173 HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
174 const FVElementGeometry& fvGeometry,
175 const SolutionVector& sol) &&
176 {
177 this->bindElement(element, fvGeometry, sol);
178 return std::move(*this);
179 }
180
181 // function to prepare the variables within the element
182 template<class FVElementGeometry, class SolutionVector>
183 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
184 const FVElementGeometry& fvGeometry,
185 const SolutionVector& sol) &
186 {
187 const auto& gridDiscretization = Deprecated::gridGeometry(fvGeometry);
188 eIdx_ = gridDiscretization.elementMapper().index(element);
189 }
190
193 { return *gridVariablesCachePtr_; }
194
200 { return { gridVars }; }
201
202private:
203 const GridVariablesCache* gridVariablesCachePtr_;
204 std::size_t eIdx_;
205};
206
211template<class GVC>
212class HybridCVFEElementVariables<GVC, /*cachingEnabled*/false>
213{
214 using ThisType = HybridCVFEElementVariables<GVC, /*cachingEnabled*/false>;
215 using Problem = std::decay_t<decltype(std::declval<GVC>().problem())>;
216 using GridGeometry = typename ProblemTraits<Problem>::GridGeometry;
217 using GridView = typename GridGeometry::GridView;
218
220 static constexpr std::size_t maxNumBoundaryFaces = GridView::dimension << 1;
221
222 class MutableVariablesView
223 {
224 public:
225 MutableVariablesView(ThisType& view)
226 : view_(view) {}
227
228 using Variables = typename GVC::Variables;
229
230 template<Dumux::Concept::LocalDof LocalDof>
231 Variables& operator [](const LocalDof& localDof) const
232 { return view_[localDof]; }
233 private:
234 ThisType& view_;
235 };
236
237 class MutableVariablesViewWithIpCacheAccess
238 {
239 public:
240 MutableVariablesViewWithIpCacheAccess(ThisType& view)
241 : view_(view)
242 {}
243
244 using Variables = typename GVC::Variables;
245
246 template<Dumux::Concept::LocalDof LocalDof>
247 Variables& operator [](const LocalDof& localDof) const
248 { return view_[localDof]; }
249
250 auto& cache(std::size_t) const
251 { return *view_.ipDataCache_; }
252
253 private:
254 ThisType& view_;
255 };
256
257public:
260
262 using InterpolationPointData = typename GridVariablesCache::InterpolationPointData;
263
265 using MutableView = std::conditional_t<
266 InterpolationPointData::isSolDependent,
267 MutableVariablesViewWithIpCacheAccess,
268 MutableVariablesView
269 >;
270
272 using Variables = typename GridVariablesCache::Variables;
273
275 template<class FVElementGeometry>
276 using DeflectionPolicy = std::conditional_t<
277 InterpolationPointData::isSolDependent,
280 >;
281
284 : gridVariablesCachePtr_(&gridVarsCache)
285 , ipDataCache_(std::make_shared<InterpolationPointDataCache>())
286 {}
287
293 template<class FVElementGeometry, class SolutionVector>
294 HybridCVFEElementVariables bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
295 const FVElementGeometry& fvGeometry,
296 const SolutionVector& sol) &&
297 {
298 this->bindElement(element, fvGeometry, sol);
299 return std::move(*this);
300 }
301
302 // specialization for control-volume finite element, simply forwards to the bindElement method
303 template<class FVElementGeometry, class SolutionVector>
304 void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
305 const FVElementGeometry& fvGeometry,
306 const SolutionVector& sol) &
307 {
308 bindElement(element, fvGeometry, sol);
309 }
310
316 template<class FVElementGeometry, class SolutionVector>
317 HybridCVFEElementVariables bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
318 const FVElementGeometry& fvGeometry,
319 const SolutionVector& sol) &&
320 {
321 this->bindElement(element, fvGeometry, sol);
322 return std::move(*this);
323 }
324
325 // specialization for control-volume finite element
326 template<class FVElementGeometry, class SolutionVector>
327 void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim<0>::Entity& element,
328 const FVElementGeometry& fvGeometry,
329 const SolutionVector& sol) &
330 {
331 // get the solution at the dofs of the element
332 const auto& gridDiscretization = Deprecated::gridGeometry(fvGeometry);
333 auto elemSol = elementSolution(element, sol, gridDiscretization);
334
335 // resize variables to the required size
336 variables_.resize(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry));
337
338 // update variables related to localDofs
339 for (const auto& localDof : localDofs(fvGeometry))
340 variables_[localDof.index()].update(elemSol, gridVariablesCache().problem(), fvGeometry, ipData(fvGeometry, localDof));
341
342 if constexpr (InterpolationPointData::isSolDependent)
343 {
344 auto newIpDataCache = std::make_shared<InterpolationPointDataCache>(*ipDataCache_);
345 newIpDataCache->update(gridVariablesCache().problem(), element, fvGeometry, variables_);
346 ipDataCache_ = std::move(newIpDataCache);
347 }
348 else
349 ipDataCache_->update(gridVariablesCache().problem(), element, fvGeometry, variables_);
350 }
351
352 const Variables& operator [](std::size_t localIdx) const
353 { return variables_[localIdx]; }
354
355 Variables& operator [](std::size_t localIdx)
356 { return variables_[localIdx]; }
357
358 template<Dumux::Concept::LocalDof LocalDof>
359 const Variables& operator [](const LocalDof& localDof) const
360 { return variables_[localDof.index()]; }
361
362 template<Dumux::Concept::LocalDof LocalDof>
363 Variables& operator [](const LocalDof& localDof)
364 { return variables_[localDof.index()]; }
365
366 template<class IpData>
367 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
368 const Variables& operator [](const IpData& ipData) const
369 { return variables_[ipData.localDofIndex()]; }
370
371 template<class IpData>
372 requires requires (const IpData& ipData) { ipData.localDofIndex(); }
373 Variables& operator [](const IpData& ipData)
374 { return variables_[ipData.localDofIndex()]; }
375
376 template<Concept::ScvfQpIpData IpData>
378 const IpData& ipData)
379 { return elemVars.ipDataCache_->scvfCache(ipData.scvfIndex(), ipData.qpIndex()); }
380
381 template<Concept::BoundaryFaceQpIpData IpData>
383 const IpData& ipData)
384 { return elemVars.ipDataCache_->boundaryFaceCache(ipData.boundaryFaceIndex(), ipData.qpIndex()); }
385
386 template<Concept::QIpData IpData>
388 const IpData& ipData)
389 { return elemVars.ipDataCache_->elementCache(ipData.qpIndex()); }
390
393 { return *gridVariablesCachePtr_; }
394
400 { return { *this }; }
401
402private:
403 class InterpolationPointDataCache
404 {
405 public:
406 InterpolationPointDataCache()
407 {}
408
409 template<class Problem, class FVElementGeometry, class ElementVariables>
410 void update(const Problem& problem,
411 const typename FVElementGeometry::Element& element,
412 const FVElementGeometry& fvGeometry,
413 const ElementVariables& elemVars)
414 {
415 updateElementCache_(problem, element, fvGeometry, elemVars);
416 }
417
418 // access operator
419 const InterpolationPointData& scvfCache(std::size_t scvfIdx, std::size_t qpIdx) const
420 { return scvfCache_[scvfIdx][qpIdx]; }
421
422 // access operator
423 InterpolationPointData& scvfCache(std::size_t scvfIdx, std::size_t qpIdx)
424 { return scvfCache_[scvfIdx][qpIdx]; }
425
426 // access operator
427 const InterpolationPointData& elementCache(std::size_t qpIdx) const
428 { return elementCache_[qpIdx]; }
429
430 // access operator
431 InterpolationPointData& elementCache(std::size_t qpIdx)
432 { return elementCache_[qpIdx]; }
433
434 // access operator
435 const InterpolationPointData& boundaryFaceCache(std::size_t bfIdx, std::size_t qpIdx) const
436 { return boundaryFaceCache_[bfIdx][qpIdx]; }
437
438 // access operator
439 InterpolationPointData& boundaryFaceCache(std::size_t bfIdx, std::size_t qpIdx)
440 { return boundaryFaceCache_[bfIdx][qpIdx]; }
441
442 private:
443 template<class Problem, class FVElementGeometry, class ElementVariables>
444 void updateElementCache_(const Problem& problem,
445 const typename FVElementGeometry::Element& element,
446 const FVElementGeometry& fvGeometry,
447 const ElementVariables& elemVars)
448 {
449 scvfCache_.resize(fvGeometry.numScvf());
450 for (const auto& scvf : scvfs(fvGeometry))
451 {
452 const auto quadRule = Dumux::CVFE::quadratureRule(fvGeometry, scvf);
453 scvfCache_[scvf.index()].resize(std::ranges::size(quadRule));
454 for (const auto& qpData : quadRule)
455 scvfCache_[scvf.index()][qpData.ipData().qpIndex()].update(
456 problem, element, fvGeometry, elemVars, qpData.ipData()
457 );
458 }
459
460 const auto elemQuadRule = Dumux::CVFE::quadratureRule(fvGeometry, element);
461 elementCache_.resize(std::ranges::size(elemQuadRule));
462 for (const auto& qpData : elemQuadRule)
463 elementCache_[qpData.ipData().qpIndex()].update(problem, element, fvGeometry, elemVars, qpData.ipData());
464
465 for (const auto& boundaryFace : boundaryFaces(fvGeometry))
466 {
467 auto& bfCache = boundaryFaceCache_[boundaryFace.index()];
468 const auto quadRule = Dumux::CVFE::quadratureRule(fvGeometry, boundaryFace);
469 bfCache.resize(std::ranges::size(quadRule));
470 for (const auto& qpData : quadRule)
471 bfCache[qpData.ipData().qpIndex()].update(problem,
472 element,
473 fvGeometry,
474 elemVars,
475 qpData.ipData());
476 }
477 }
478
479 std::vector<std::vector<InterpolationPointData>> scvfCache_;
480 std::vector<InterpolationPointData> elementCache_;
481 std::array<std::vector<InterpolationPointData>, maxNumBoundaryFaces> boundaryFaceCache_;
482 };
483
484 const GridVariablesCache* gridVariablesCachePtr_;
485 std::vector<Variables> variables_;
486 std::shared_ptr<InterpolationPointDataCache> ipDataCache_;
487};
488
489} // end namespace Dumux::Experimental::CVFE
490
491#endif
Definition variablesdeflectionpolicy.hh:29
Definition variablesdeflectionpolicy.hh:95
std::conditional_t< InterpolationPointData::isSolDependent, MutableVariablesViewWithIpCacheAccess, MutableVariablesView > MutableView
export type of the mutable version of the view
Definition cvfe/hybrid/elementvariables.hh:265
MutableView asMutableView(GridVariablesCache &)
return a local view on variables that is always mutable, regardless of the caching policy
Definition cvfe/hybrid/elementvariables.hh:399
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 cvfe/hybrid/elementvariables.hh:294
HybridCVFEElementVariables(const GridVariablesCache &gridVarsCache)
Constructor.
Definition cvfe/hybrid/elementvariables.hh:283
void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition cvfe/hybrid/elementvariables.hh:304
friend const InterpolationPointData & cache(const HybridCVFEElementVariables &elemVars, const IpData &ipData)
Definition cvfe/hybrid/elementvariables.hh:377
GVC GridVariablesCache
export type of the grid variables cache
Definition cvfe/hybrid/elementvariables.hh:259
typename GridVariablesCache::InterpolationPointData InterpolationPointData
export interpolation point data
Definition cvfe/hybrid/elementvariables.hh:262
const GridVariablesCache & gridVariablesCache() const
The grid variables cache object we are a restriction of.
Definition cvfe/hybrid/elementvariables.hh:392
std::conditional_t< InterpolationPointData::isSolDependent, Dumux::Detail::CVFE::VariablesDeflectionPolicyWithIpCacheUpdate< MutableView, FVElementGeometry >, Dumux::Detail::CVFE::VariablesDeflectionPolicy< MutableView, FVElementGeometry > > DeflectionPolicy
export type of deflection policy
Definition cvfe/hybrid/elementvariables.hh:276
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 cvfe/hybrid/elementvariables.hh:317
void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition cvfe/hybrid/elementvariables.hh:327
typename GridVariablesCache::Variables Variables
export type of the variables
Definition cvfe/hybrid/elementvariables.hh:272
const GridVariablesCache & gridVariablesCache() const
The grid variables cache object we are a restriction of.
Definition cvfe/hybrid/elementvariables.hh:192
MutableView asMutableView(GridVariablesCache &gridVars)
return a local view on variables that is always mutable, regardless of the caching policy
Definition cvfe/hybrid/elementvariables.hh:199
GVC GridVariablesCache
export type of the grid variables cache
Definition cvfe/hybrid/elementvariables.hh:89
std::conditional_t< InterpolationPointData::isSolDependent, MutableVariablesViewWithIpCacheAccess, MutableVariablesView > MutableView
export type of the mutable version of the view
Definition cvfe/hybrid/elementvariables.hh:95
typename GridVariablesCache::InterpolationPointData InterpolationPointData
export interpolation point data
Definition cvfe/hybrid/elementvariables.hh:92
friend const InterpolationPointData & cache(const HybridCVFEElementVariables &elemVars, const IpData &ipData)
Definition cvfe/hybrid/elementvariables.hh:129
void bindElement(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition cvfe/hybrid/elementvariables.hh:183
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 cvfe/hybrid/elementvariables.hh:149
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 cvfe/hybrid/elementvariables.hh:173
void bind(const typename FVElementGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const FVElementGeometry &fvGeometry, const SolutionVector &sol) &
Definition cvfe/hybrid/elementvariables.hh:160
typename GridVariablesCache::Variables Variables
export type of the variables
Definition cvfe/hybrid/elementvariables.hh:102
std::conditional_t< InterpolationPointData::isSolDependent, Dumux::Detail::CVFE::VariablesDeflectionPolicyWithIpCacheUpdate< MutableView, FVElementGeometry >, Dumux::Detail::CVFE::VariablesDeflectionPolicy< MutableView, FVElementGeometry > > DeflectionPolicy
export type of deflection policy
Definition cvfe/hybrid/elementvariables.hh:106
HybridCVFEElementVariables(const GridVariablesCache &gridVariablesCache)
Constructor.
Definition cvfe/hybrid/elementvariables.hh:113
The (stencil) element variables class for hybrid control-volume finite element.
Definition cvfe/hybrid/elementvariables.hh:42
Base class for all standard finite volume or finite element problems.
Definition common/problem.hh:39
Type traits for problem classes.
Helpers for deprecation.
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:159
Definition cvfe/hybrid/elementvariables.hh:32
auto localDofs(const FVElementGeometry &fvGeometry)
range over local dofs
Definition localdof.hh:50
Quadrature rules over sub-control volumes and sub-control volume faces.
Detail::ProblemGridGeometry< Problem > GridGeometry
Definition common/typetraits/problem.hh:50
Variables deflection policy.