version 3.8
discretization/pq1bubble/fvgridgeometry.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-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
14#ifndef DUMUX_DISCRETIZATION_PQ1BUBBLE_GRID_GEOMETRY_HH
15#define DUMUX_DISCRETIZATION_PQ1BUBBLE_GRID_GEOMETRY_HH
16
17#include <utility>
18#include <unordered_map>
19
20#include <dune/grid/common/mcmgmapper.hh>
21
23
26
29
37
38namespace Dumux {
39
40namespace Detail {
41template<class GV, class T>
42using PQ1BubbleGeometryHelper_t = Dune::Std::detected_or_t<
45 T
46>;
47} // end namespace Detail
48
49template <class GridView>
51{
52 using DofMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
53
58 static Dune::MCMGLayout layout()
59 {
60 return [](Dune::GeometryType gt, int dimgrid) {
61 return (gt.dim() == dimgrid) || (gt.dim() == 0);
62 };
63 }
64};
65
72template<class GridView, class MapperTraits = PQ1BubbleMapperTraits<GridView>>
74: public MapperTraits
75{
78
79 template<class GridGeometry, bool enableCache>
81};
82
89template<class Scalar,
90 class GV,
91 bool enableCaching = true,
94: public BaseGridGeometry<GV, Traits>
95{
98 using GridIndexType = typename IndexTraits<GV>::GridIndex;
99 using LocalIndexType = typename IndexTraits<GV>::LocalIndex;
100
101 using Element = typename GV::template Codim<0>::Entity;
102 using CoordScalar = typename GV::ctype;
103 static const int dim = GV::dimension;
104 static const int dimWorld = GV::dimensionworld;
105
106 static_assert(dim > 1, "Only implemented for dim > 1");
107
108public:
112
114 using LocalView = typename Traits::template LocalView<ThisType, true>;
116 using SubControlVolume = typename Traits::SubControlVolume;
118 using SubControlVolumeFace = typename Traits::SubControlVolumeFace;
122 using DofMapper = typename Traits::DofMapper;
126 using GridView = GV;
127
131 , dofMapper_(gridView, Traits::layout())
132 , cache_(*this)
133 {
134 update_();
135 }
136
138 const DofMapper& dofMapper() const
139 { return dofMapper_; }
140
142 std::size_t numScv() const
143 { return numScv_; }
144
146 std::size_t numScvf() const
147 { return numScvf_; }
148
150 std::size_t numBoundaryScvf() const
151 { return numBoundaryScvf_; }
152
154 std::size_t numDofs() const
155 { return this->dofMapper().size(); }
156
159 {
161 update_();
162 }
163
166 {
167 ParentType::update(std::move(gridView));
168 update_();
169 }
170
172 const FeCache& feCache() const
173 { return feCache_; }
174
176 bool dofOnBoundary(GridIndexType dofIdx) const
177 { return boundaryDofIndices_[dofIdx]; }
178
180 bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
181 { return periodicVertexMap_.count(dofIdx); }
182
184 GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
185 { return periodicVertexMap_.at(dofIdx); }
186
188 const std::unordered_map<GridIndexType, GridIndexType>& periodicVertexMap() const
189 { return periodicVertexMap_; }
190
193 { return { gg.cache_ }; }
194
195private:
196
197 class PQ1BubbleGridGeometryCache
198 {
199 friend class PQ1BubbleFVGridGeometry;
200 public:
203
204 explicit PQ1BubbleGridGeometryCache(const PQ1BubbleFVGridGeometry& gg)
205 : gridGeometry_(&gg)
206 {}
207
208 const PQ1BubbleFVGridGeometry& gridGeometry() const
209 { return *gridGeometry_; }
210
212 const std::vector<SubControlVolume>& scvs(GridIndexType eIdx) const
213 { return scvs_[eIdx]; }
214
216 const std::vector<SubControlVolumeFace>& scvfs(GridIndexType eIdx) const
217 { return scvfs_[eIdx]; }
218
220 bool hasBoundaryScvf(GridIndexType eIdx) const
221 { return hasBoundaryScvf_[eIdx]; }
222
224 const std::vector<std::array<LocalIndexType, 2>>& scvfBoundaryGeometryKeys(GridIndexType eIdx) const
225 { return scvfBoundaryGeometryKeys_.at(eIdx); }
226
227 private:
228 void clear_()
229 {
230 scvs_.clear();
231 scvfs_.clear();
232 hasBoundaryScvf_.clear();
233 scvfBoundaryGeometryKeys_.clear();
234 }
235
236 std::vector<std::vector<SubControlVolume>> scvs_;
237 std::vector<std::vector<SubControlVolumeFace>> scvfs_;
238 std::vector<bool> hasBoundaryScvf_;
239 std::unordered_map<GridIndexType, std::vector<std::array<LocalIndexType, 2>>> scvfBoundaryGeometryKeys_;
240
241 const PQ1BubbleFVGridGeometry* gridGeometry_;
242 };
243
244public:
247 using Cache = PQ1BubbleGridGeometryCache;
248
249private:
250 using GeometryHelper = typename Cache::GeometryHelper;
251
252 void update_()
253 {
254 cache_.clear_();
255 dofMapper_.update(this->gridView());
256
257 auto numElements = this->gridView().size(0);
258 cache_.scvs_.resize(numElements);
259 cache_.scvfs_.resize(numElements);
260 cache_.hasBoundaryScvf_.resize(numElements, false);
261
262 boundaryDofIndices_.assign(numDofs(), false);
263
264 numScv_ = 0;
265 numScvf_ = 0;
266 numBoundaryScvf_ = 0;
267
268 // Build the scvs and scv faces
269 for (const auto& element : elements(this->gridView()))
270 {
271 auto eIdx = this->elementMapper().index(element);
272
273 // get the element geometry
274 auto elementGeometry = element.geometry();
275 const auto refElement = referenceElement(elementGeometry);
276
277 // instantiate the geometry helper
278 GeometryHelper geometryHelper(elementGeometry);
279
280 numScv_ += geometryHelper.numScv();
281 // construct the sub control volumes
282 cache_.scvs_[eIdx].resize(geometryHelper.numScv());
283 for (LocalIndexType scvLocalIdx = 0; scvLocalIdx < geometryHelper.numScv(); ++scvLocalIdx)
284 {
285 auto corners = geometryHelper.getScvCorners(scvLocalIdx);
286 cache_.scvs_[eIdx][scvLocalIdx] = SubControlVolume(
287 geometryHelper.scvVolume(scvLocalIdx, corners),
288 geometryHelper.dofPosition(scvLocalIdx),
289 Dumux::center(corners),
290 scvLocalIdx,
291 eIdx,
292 geometryHelper.dofIndex(this->dofMapper(), element, scvLocalIdx),
293 geometryHelper.isOverlappingScv(scvLocalIdx)
294 );
295 }
296
297 // construct the sub control volume faces
298 numScvf_ += geometryHelper.numInteriorScvf();
299 cache_.scvfs_[eIdx].resize(geometryHelper.numInteriorScvf());
300 LocalIndexType scvfLocalIdx = 0;
301 for (; scvfLocalIdx < geometryHelper.numInteriorScvf(); ++scvfLocalIdx)
302 {
303 const auto scvPair = geometryHelper.getScvPairForScvf(scvfLocalIdx);
304 const auto corners = geometryHelper.getScvfCorners(scvfLocalIdx);
305 const auto area = Dumux::convexPolytopeVolume<dim-1>(
306 geometryHelper.getInteriorScvfGeometryType(scvfLocalIdx),
307 [&](unsigned int i){ return corners[i]; }
308 );
309
310 cache_.scvfs_[eIdx][scvfLocalIdx] = SubControlVolumeFace(
311 Dumux::center(corners),
312 area,
313 geometryHelper.normal(corners, scvPair),
314 std::move(scvPair),
315 scvfLocalIdx,
316 geometryHelper.isOverlappingScvf(scvfLocalIdx)
317 );
318 }
319
320 // construct the sub control volume faces on the domain boundary
321 for (const auto& intersection : intersections(this->gridView(), element))
322 {
323 if (intersection.boundary() && !intersection.neighbor())
324 {
325 cache_.hasBoundaryScvf_[eIdx] = true;
326
327 const auto localFacetIndex = intersection.indexInInside();
328 const auto numBoundaryScvf = geometryHelper.numBoundaryScvf(localFacetIndex);
329 numScvf_ += numBoundaryScvf;
330 numBoundaryScvf_ += numBoundaryScvf;
331
332 for (unsigned int isScvfLocalIdx = 0; isScvfLocalIdx < numBoundaryScvf; ++isScvfLocalIdx)
333 {
334 // find the scvs this scvf is belonging to
335 const auto scvPair = geometryHelper.getScvPairForBoundaryScvf(localFacetIndex, isScvfLocalIdx);
336 const auto corners = geometryHelper.getBoundaryScvfCorners(localFacetIndex, isScvfLocalIdx);
337 const auto area = Dumux::convexPolytopeVolume<dim-1>(
338 Dune::GeometryTypes::cube(dim-1),
339 [&](unsigned int i){ return corners[i]; }
340 );
341 cache_.scvfs_[eIdx].emplace_back(
342 Dumux::center(corners),
343 area,
344 intersection.centerUnitOuterNormal(),
345 std::move(scvPair),
346 scvfLocalIdx,
347 typename SubControlVolumeFace::Traits::BoundaryFlag{ intersection }
348 );
349
350 // store look-up map to construct boundary scvf geometries
351 cache_.scvfBoundaryGeometryKeys_[eIdx].emplace_back(std::array<LocalIndexType, 2>{{
352 static_cast<LocalIndexType>(localFacetIndex),
353 static_cast<LocalIndexType>(isScvfLocalIdx)
354 }});
355
356 // increment local counter
357 scvfLocalIdx++;
358 }
359
360 // TODO also move this to helper class
361
362 // add all vertices on the intersection to the set of boundary vertices
363 for (int localVIdx = 0; localVIdx < numBoundaryScvf; ++localVIdx)
364 {
365 const auto vIdx = refElement.subEntity(localFacetIndex, 1, localVIdx, dim);
366 const auto vIdxGlobal = this->dofMapper().subIndex(element, vIdx, dim);
367 boundaryDofIndices_[vIdxGlobal] = true;
368 }
369 }
370
371 // inform the grid geometry if we have periodic boundaries
372 else if (intersection.boundary() && intersection.neighbor())
373 {
374 this->setPeriodic();
375
376 // find the mapped periodic vertex of all vertices on periodic boundaries
377 const auto fIdx = intersection.indexInInside();
378 const auto numFaceVerts = refElement.size(fIdx, 1, dim);
379 const auto eps = 1e-7*(elementGeometry.corner(1) - elementGeometry.corner(0)).two_norm();
380 for (int localVIdx = 0; localVIdx < numFaceVerts; ++localVIdx)
381 {
382 const auto vIdx = refElement.subEntity(fIdx, 1, localVIdx, dim);
383 const auto vIdxGlobal = this->dofMapper().subIndex(element, vIdx, dim);
384 const auto vPos = elementGeometry.corner(vIdx);
385
386 const auto& outside = intersection.outside();
387 const auto outsideGeometry = outside.geometry();
388 for (const auto& isOutside : intersections(this->gridView(), outside))
389 {
390 // only check periodic vertices of the periodic neighbor
391 if (isOutside.boundary() && isOutside.neighbor())
392 {
393 const auto fIdxOutside = isOutside.indexInInside();
394 const auto numFaceVertsOutside = refElement.size(fIdxOutside, 1, dim);
395 for (int localVIdxOutside = 0; localVIdxOutside < numFaceVertsOutside; ++localVIdxOutside)
396 {
397 const auto vIdxOutside = refElement.subEntity(fIdxOutside, 1, localVIdxOutside, dim);
398 const auto vPosOutside = outsideGeometry.corner(vIdxOutside);
399 const auto shift = std::abs((this->bBoxMax()-this->bBoxMin())*intersection.centerUnitOuterNormal());
400 if (std::abs((vPosOutside-vPos).two_norm() - shift) < eps)
401 periodicVertexMap_[vIdxGlobal] = this->dofMapper().subIndex(outside, vIdxOutside, dim);
402 }
403 }
404 }
405 }
406 }
407 }
408 }
409
410 // error check: periodic boundaries currently don't work for pq1bubble in parallel
411 if (this->isPeriodic() && this->gridView().comm().size() > 1)
412 DUNE_THROW(Dune::NotImplemented, "Periodic boundaries for pq1bubble method for parallel simulations!");
413 }
414
415 DofMapper dofMapper_;
416
417 const FeCache feCache_;
418
419 std::size_t numScv_;
420 std::size_t numScvf_;
421 std::size_t numBoundaryScvf_;
422
423 // vertices on the boundary
424 std::vector<bool> boundaryDofIndices_;
425
426 // a map for periodic boundary vertices
427 std::unordered_map<GridIndexType, GridIndexType> periodicVertexMap_;
428
429 Cache cache_;
430};
431
432} // end namespace Dumux
433
434#endif
Base class for grid geometries.
Compute the center point of a convex polytope geometry or a random-access container of corner points.
Base class for all grid geometries.
Definition: basegridgeometry.hh:52
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
Definition: pq1bubblefecache.hh:29
Base class for the finite volume geometry vector for pq1bubble models This builds up the sub control ...
Definition: discretization/pq1bubble/fvelementgeometry.hh:40
Base class for the finite volume geometry vector for pq1bubble schemes This builds up the sub control...
Definition: discretization/pq1bubble/fvgridgeometry.hh:95
void update(const GridView &gridView)
update all geometries (call this after grid adaption)
Definition: discretization/pq1bubble/fvgridgeometry.hh:158
friend LocalView localView(const PQ1BubbleFVGridGeometry &gg)
local view of this object (constructed with the internal cache)
Definition: discretization/pq1bubble/fvgridgeometry.hh:192
PQ1BubbleGridGeometryCache Cache
Definition: discretization/pq1bubble/fvgridgeometry.hh:247
static constexpr DiscretizationMethod discMethod
Definition: discretization/pq1bubble/fvgridgeometry.hh:111
std::size_t numBoundaryScvf() const
The total number of boundary sub control volume faces.
Definition: discretization/pq1bubble/fvgridgeometry.hh:150
std::size_t numScv() const
The total number of sub control volumes.
Definition: discretization/pq1bubble/fvgridgeometry.hh:142
std::size_t numScvf() const
The total number of sub control volume faces.
Definition: discretization/pq1bubble/fvgridgeometry.hh:146
const FeCache & feCache() const
The finite element cache for creating local FE bases.
Definition: discretization/pq1bubble/fvgridgeometry.hh:172
GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
The index of the vertex / d.o.f. on the other side of the periodic boundary.
Definition: discretization/pq1bubble/fvgridgeometry.hh:184
Extrusion_t< Traits > Extrusion
export the type of extrusion
Definition: discretization/pq1bubble/fvgridgeometry.hh:120
typename Traits::SubControlVolumeFace SubControlVolumeFace
export the type of sub control volume
Definition: discretization/pq1bubble/fvgridgeometry.hh:118
std::size_t numDofs() const
The total number of degrees of freedom.
Definition: discretization/pq1bubble/fvgridgeometry.hh:154
const std::unordered_map< GridIndexType, GridIndexType > & periodicVertexMap() const
Returns the map between dofs across periodic boundaries.
Definition: discretization/pq1bubble/fvgridgeometry.hh:188
bool dofOnBoundary(GridIndexType dofIdx) const
If a vertex / d.o.f. is on the boundary.
Definition: discretization/pq1bubble/fvgridgeometry.hh:176
typename Traits::DofMapper DofMapper
export dof mapper type
Definition: discretization/pq1bubble/fvgridgeometry.hh:122
const DofMapper & dofMapper() const
The dofMapper.
Definition: discretization/pq1bubble/fvgridgeometry.hh:138
GV GridView
export the grid view type
Definition: discretization/pq1bubble/fvgridgeometry.hh:126
bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
If a vertex / d.o.f. is on a periodic boundary.
Definition: discretization/pq1bubble/fvgridgeometry.hh:180
typename Traits::template LocalView< ThisType, true > LocalView
export the type of the fv element geometry (the local view type)
Definition: discretization/pq1bubble/fvgridgeometry.hh:114
typename Traits::SubControlVolume SubControlVolume
export the type of sub control volume
Definition: discretization/pq1bubble/fvgridgeometry.hh:116
PQ1BubbleFVGridGeometry(const GridView gridView)
Constructor.
Definition: discretization/pq1bubble/fvgridgeometry.hh:129
void update(GridView &&gridView)
update all geometries (call this after grid adaption)
Definition: discretization/pq1bubble/fvgridgeometry.hh:165
Dumux::PQ1BubbleFECache< CoordScalar, Scalar, dim > FeCache
export the finite element cache type
Definition: discretization/pq1bubble/fvgridgeometry.hh:124
A class to create sub control volume and sub control volume face geometries per element.
Definition: discretization/pq1bubble/geometryhelper.hh:166
Class for a sub control volume face in the cvfe method, i.e a part of the boundary of a sub control v...
Definition: discretization/pq1bubble/subcontrolvolumeface.hh:60
the sub control volume for the pq1bubble scheme
Definition: discretization/pq1bubble/subcontrolvolume.hh:56
Defines the default element and vertex mapper types.
Base class for the local finite volume geometry for the pq1bubble method This builds up the sub contr...
Helper class constructing the dual grid finite volume geometries for the cvfe discretizazion method.
the sub control volume for the cvfe scheme
Base class for a sub control volume face.
Helper classes to compute the integration elements.
auto convexPolytopeVolume(Dune::GeometryType type, const CornerF &c)
Compute the volume of several common geometry types.
Definition: volume.hh:41
Corners::value_type center(const Corners &corners)
The center of a given list of corners.
Definition: center.hh:24
Defines the index types used for grid and local indices.
The available discretization methods in Dumux.
Dune::Std::detected_or_t< Dumux::PQ1BubbleGeometryHelper< GV, typename T::SubControlVolume, typename T::SubControlVolumeFace >, SpecifiesGeometryHelper, T > PQ1BubbleGeometryHelper_t
Definition: discretization/pq1bubble/fvgridgeometry.hh:46
typename T::GeometryHelper SpecifiesGeometryHelper
Definition: basegridgeometry.hh:30
CVFE< CVFEMethods::PQ1Bubble > PQ1Bubble
Definition: method.hh:108
Definition: adapt.hh:17
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition: extrusion.hh:166
A finite element cache for P1/Q1 function with bubble.
Definition: defaultmappertraits.hh:23
Definition: method.hh:46
Structure to define the index types used for grid and local indices.
Definition: indextraits.hh:26
The default traits for the pq1bubble finite volume grid geometry Defines the scv and scvf types and t...
Definition: discretization/pq1bubble/fvgridgeometry.hh:75
Definition: discretization/pq1bubble/fvgridgeometry.hh:51
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > DofMapper
Definition: discretization/pq1bubble/fvgridgeometry.hh:52
static Dune::MCMGLayout layout()
layout for elements and vertices
Definition: discretization/pq1bubble/fvgridgeometry.hh:58
Compute the volume of several common geometry types.