version 3.11-dev
Loading...
Searching...
No Matches
fem/fegriddiscretization.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_FE_GRID_DISCRETIZATION_HH
13#define DUMUX_DISCRETIZATION_FE_GRID_DISCRETIZATION_HH
14
15#include <span>
16#include <utility>
17#include <vector>
18#include <unordered_map>
19
20#include <dune/common/reservedvector.hh>
21
23
28
30
31namespace Dumux::Experimental {
32
39template<class GV, class Traits>
41: public BaseGridGeometry<GV, Traits>
42{
43 using ThisType = FEGridDiscretization<GV, Traits>;
44 using ParentType = BaseGridGeometry<GV, Traits>;
45 using GridIndexType = typename IndexTraits<GV>::GridIndex;
46 using LocalIndexType = typename IndexTraits<GV>::LocalIndex;
47 static const int dim = GV::dimension;
48
49public:
51 using DiscretizationMethod = typename Traits::DiscretizationMethod;
52 static constexpr DiscretizationMethod discMethod{};
53
54 static constexpr std::size_t maxNumElementDofs = Traits::maxNumElementDofs;
55
58
62 using LocalView = typename Traits::template LocalView<ThisType, true>;
66 using DofMapper = typename Traits::DofMapper;
68 using FeCache = typename Traits::FeCache;
70 using GridView = GV;
74 using ElementQuadratureRule = typename Traits::ElementQuadratureRule;
76 using IntersectionQuadratureRule = typename Traits::IntersectionQuadratureRule;
78 using BoundaryFaceQuadratureRule = typename Traits::BoundaryFaceQuadratureRule;
79
81 FEGridDiscretization(std::shared_ptr<BasicGridGeometry> gg)
82 : ParentType(std::move(gg))
83 , dofMapper_(this->gridView(), Traits::layout())
84 , cache_(*this)
85 , periodicGridTraits_(this->gridView().grid())
86 {
87 update_();
88 }
89
94
96 const DofMapper& dofMapper() const
97 { return dofMapper_; }
98
100 std::size_t numDofs() const
101 { return this->dofMapper().size(); }
102
105 {
107 update_();
108 }
109
112 {
113 ParentType::update(std::move(gridView));
114 update_();
115 }
116
118 const FeCache& feCache() const
119 { return feCache_; }
120
122 bool dofOnBoundary(GridIndexType dofIdx) const
123 { return boundaryDofIndices_[dofIdx]; }
124
126 bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
127 { return periodicDofMap_.count(dofIdx); }
128
130 GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
131 { return periodicDofMap_.at(dofIdx); }
132
134 const std::unordered_map<GridIndexType, GridIndexType>& periodicDofMap() const
135 { return periodicDofMap_; }
136
138 friend inline LocalView localView(const FEGridDiscretization& gg)
139 { return { gg.cache_ }; }
140
141private:
142 class FEGridDiscretizationCache
143 {
144 friend class FEGridDiscretization;
145 public:
147 using DofHelper = typename Traits::DofHelper;
148
149 explicit FEGridDiscretizationCache(const FEGridDiscretization& gg)
150 : gridDiscretization_(&gg)
151 {}
152
153 const FEGridDiscretization& gridDiscretization() const
154 { return *gridDiscretization_; }
155
157 auto boundaryFaces(GridIndexType eIdx) const -> std::span<const BoundaryFace>
158 {
159 if (auto it = boundaryFaces_.find(eIdx); it != boundaryFaces_.end())
160 return {it->second};
161 return {};
162 }
163
164 private:
165 void clear_()
166 { boundaryFaces_.clear(); }
167
168 std::unordered_map<GridIndexType, Dune::ReservedVector<BoundaryFace, 2*dim>> boundaryFaces_;
169
170 const FEGridDiscretization* gridDiscretization_;
171 };
172
173public:
176 using Cache = FEGridDiscretizationCache;
177
178private:
180 void update_()
181 {
182 using DofHelper = typename Cache::DofHelper;
183
184 dofMapper_.update(this->gridView());
185 cache_.clear_();
186 boundaryDofIndices_.assign(numDofs(), false);
187
188 for (const auto& element : elements(this->gridView()))
189 {
190 auto elementGeometry = element.geometry();
191 const auto& localCoefficients = this->feCache().get(element.type()).localCoefficients();
192 const auto eIdx = this->elementMapper().index(element);
193
194 LocalIndexType numBoundaryFaces = 0;
195 for (const auto& intersection : intersections(this->gridView(), element))
196 {
197 if (intersection.boundary() && !periodicGridTraits_.isPeriodic(intersection))
198 {
199 // add one boundary face per boundary intersection
200 const auto isGeometry = intersection.geometry();
201 cache_.boundaryFaces_[eIdx].push_back(BoundaryFace{
202 isGeometry.center(),
203 isGeometry.volume(),
204 intersection.centerUnitOuterNormal(),
205 numBoundaryFaces++,
206 static_cast<LocalIndexType>(intersection.indexInInside()),
207 typename BoundaryFace::Traits::BoundaryFlag{intersection}
208 });
209
210 // mark all dofs on this intersection as boundary dofs
211 for (LocalIndexType keyIdx = 0; keyIdx < localCoefficients.size(); ++keyIdx)
212 {
213 if (DofHelper::localDofOnIntersection(elementGeometry.type(),
214 intersection.indexInInside(),
215 localCoefficients.localKey(keyIdx)))
216 {
217 const auto dofIdxGlobal = DofHelper::dofIndex(
218 this->dofMapper(), element, localCoefficients.localKey(keyIdx));
219 boundaryDofIndices_[dofIdxGlobal] = true;
220 }
221 }
222 }
223
224 // inform the grid discretization if we have periodic boundaries
225 else if (periodicGridTraits_.isPeriodic(intersection))
226 {
227 this->setPeriodic();
228
229 const auto eps = 1e-7*(elementGeometry.corner(1) - elementGeometry.corner(0)).two_norm();
230 for (int localDofIdx = 0; localDofIdx < localCoefficients.size(); ++localDofIdx)
231 {
232 if (!DofHelper::localDofOnIntersection(elementGeometry.type(),
233 intersection.indexInInside(),
234 localCoefficients.localKey(localDofIdx)))
235 continue;
236
237 const auto dofIdxGlobal = DofHelper::dofIndex(
238 this->dofMapper(), element, localCoefficients.localKey(localDofIdx));
239 const auto dofPos = DofHelper::dofPosition(elementGeometry, localCoefficients.localKey(localDofIdx));
240
241 const auto& outside = intersection.outside();
242 const auto outsideGeometry = outside.geometry();
243 const auto& localCoefficientsOut = this->feCache().get(outsideGeometry.type()).localCoefficients();
244 for (const auto& isOutside : intersections(this->gridView(), outside))
245 {
246 if (periodicGridTraits_.isPeriodic(isOutside))
247 {
248 for (int localDofIdxOut = 0; localDofIdxOut < localCoefficientsOut.size(); ++localDofIdxOut)
249 {
250 const auto& localKeyOut = localCoefficientsOut.localKey(localDofIdxOut);
251 if (!DofHelper::localDofOnIntersection(outsideGeometry.type(),
252 isOutside.indexInInside(),
253 localKeyOut))
254 continue;
255
256 const auto dofIdxGlobalOut = DofHelper::dofIndex(this->dofMapper(), outside, localKeyOut);
257 const auto dofPosOutside = DofHelper::dofPosition(outsideGeometry, localKeyOut);
258 const auto shift = std::abs((this->bBoxMax()-this->bBoxMin())*intersection.centerUnitOuterNormal());
259 if (std::abs((dofPosOutside - dofPos).two_norm() - shift) < eps)
260 periodicDofMap_[dofIdxGlobal] = dofIdxGlobalOut;
261 }
262 }
263 }
264 }
265 }
266 }
267 }
268
269 // error check: periodic boundaries currently don't work in parallel
270 if (this->isPeriodic() && this->gridView().comm().size() > 1)
271 DUNE_THROW(Dune::NotImplemented, "Periodic boundaries for FE method for parallel simulations!");
272 }
273
274 DofMapper dofMapper_;
275 const FeCache feCache_;
276 // dofs on the boundary
277 std::vector<bool> boundaryDofIndices_;
278 // a map for periodic boundary dofs
279 std::unordered_map<GridIndexType, GridIndexType> periodicDofMap_;
280 Cache cache_;
282};
283
284} // end namespace Dumux::Experimental
285
286#endif
Base class for grid geometries.
Implementation of a boundary face related to primary grid elements (dune intersections).
const ElementMapper & elementMapper() const
Returns the mapper for elements to indices for constant grids.
Definition basegridgeometry.hh:112
void setPeriodic(bool value=true)
Set the periodicity of the grid geometry.
Definition basegridgeometry.hh:169
const GlobalCoordinate & bBoxMax() const
The coordinate of the corner of the GridView's bounding box with the largest values.
Definition basegridgeometry.hh:156
Element element(GridIndexType eIdx) const
Get an element from a global element index.
Definition basegridgeometry.hh:142
const GridView & gridView() const
Return the gridView this grid geometry object lives on.
Definition basegridgeometry.hh:100
void update(const GridView &gridView)
Update all fvElementGeometries (call this after grid adaption).
Definition basegridgeometry.hh:88
const GlobalCoordinate & bBoxMin() const
The coordinate of the corner of the GridView's bounding box with the smallest values.
Definition basegridgeometry.hh:149
bool isPeriodic() const
Returns if the grid geometry is periodic (at all).
Definition basegridgeometry.hh:162
Class for a boundary face related to primary grid elements (dune intersections).
Definition boundaryface.hh:69
Default class for finite element grid discretizations.
Definition fem/fegriddiscretization.hh:42
typename Traits::BoundaryFaceQuadratureRule BoundaryFaceQuadratureRule
the quadrature rule type for boundary faces
Definition fem/fegriddiscretization.hh:78
Experimental::BoundaryFace< GV > BoundaryFace
export the boundary face type
Definition fem/fegriddiscretization.hh:57
typename Traits::ElementQuadratureRule ElementQuadratureRule
the quadrature rule type for elements
Definition fem/fegriddiscretization.hh:74
BasicGridGeometry_t< GV, Traits > BasicGridGeometry
export basic grid geometry type for the alternative constructor
Definition fem/fegriddiscretization.hh:60
typename Traits::template LocalView< ThisType, true > LocalView
export the type of the element discretization (the local view type)
Definition fem/fegriddiscretization.hh:62
GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
The index of the d.o.f. on the other side of the periodic boundary.
Definition fem/fegriddiscretization.hh:130
void update(const GridView &gridView)
update all geometries (call this after grid adaption)
Definition fem/fegriddiscretization.hh:104
GV GridView
export the grid view type
Definition fem/fegriddiscretization.hh:70
friend LocalView localView(const FEGridDiscretization &gg)
local view of this object (constructed with the internal cache)
Definition fem/fegriddiscretization.hh:138
typename PeriodicGridTraits< typename GV::Grid >::SupportsPeriodicity SupportsPeriodicity
export whether the grid supports periodicity
Definition fem/fegriddiscretization.hh:72
static constexpr DiscretizationMethod discMethod
Definition fem/fegriddiscretization.hh:52
bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
If a d.o.f. is on a periodic boundary.
Definition fem/fegriddiscretization.hh:126
typename Traits::IntersectionQuadratureRule IntersectionQuadratureRule
the quadrature rule type for intersections
Definition fem/fegriddiscretization.hh:76
bool dofOnBoundary(GridIndexType dofIdx) const
If a d.o.f. is on the boundary.
Definition fem/fegriddiscretization.hh:122
FEGridDiscretization(const GridView &gridView)
Constructor.
Definition fem/fegriddiscretization.hh:91
const DofMapper & dofMapper() const
The dof mapper.
Definition fem/fegriddiscretization.hh:96
std::size_t numDofs() const
The total number of degrees of freedom.
Definition fem/fegriddiscretization.hh:100
void update(GridView &&gridView)
update all geometries (call this after grid adaption)
Definition fem/fegriddiscretization.hh:111
FEGridDiscretizationCache Cache
Definition fem/fegriddiscretization.hh:176
const FeCache & feCache() const
The finite element cache for creating local FE bases.
Definition fem/fegriddiscretization.hh:118
const std::unordered_map< GridIndexType, GridIndexType > & periodicDofMap() const
Returns the map between dofs across periodic boundaries.
Definition fem/fegriddiscretization.hh:134
typename Traits::FeCache FeCache
export the finite element cache type
Definition fem/fegriddiscretization.hh:68
FEGridDiscretization(std::shared_ptr< BasicGridGeometry > gg)
Constructor.
Definition fem/fegriddiscretization.hh:81
typename Traits::DiscretizationMethod DiscretizationMethod
export the discretization method this geometry belongs to
Definition fem/fegriddiscretization.hh:51
Extrusion_t< Traits > Extrusion
export the type of extrusion
Definition fem/fegriddiscretization.hh:64
static constexpr std::size_t maxNumElementDofs
Definition fem/fegriddiscretization.hh:54
typename Traits::DofMapper DofMapper
export dof mapper type
Definition fem/fegriddiscretization.hh:66
Helper classes to compute the integration elements.
Dune::Std::detected_or_t< Dumux::BasicGridGeometry< GV, typename T::ElementMapper, typename T::VertexMapper >, Detail::SpecifiesBaseGridGeometry, T > BasicGridGeometry_t
Type of the basic grid geometry implementation used as backend.
Definition basegridgeometry.hh:38
BaseGridGeometry(std::shared_ptr< BaseImplementation > impl)
Constructor from a BaseImplementation.
Definition basegridgeometry.hh:72
Defines the index types used for grid and local indices.
Definition assembly/assembler.hh:44
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition extrusion.hh:236
Grid properties related to periodicity.
Quadrature rules over sub-control volumes and sub-control volume faces.
typename GridView::IndexSet::IndexType GridIndex
Definition indextraits.hh:27
unsigned int LocalIndex
Definition indextraits.hh:28
Definition periodicgridtraits.hh:24
Definition periodicgridtraits.hh:23