KASKADE 7 development version
gridCover.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2018-2018 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef GRIDCTOOLS_HH
14#define GRIDCTOOLS_HH
15
16#include <algorithm>
17#include <utility>
18
19#include "fem/fixdune.hh"
21
22namespace Kaskade
23{
32 template <class GridView>
33 std::pair<Dune::FieldVector<typename GridView::ctype,GridView::dimensionworld>,
35 boundingBox(GridView const& gv)
36 {
37 using ctype = typename GridView::ctype;
38 int const dim = GridView::dimensionworld;
39
41 high(std::numeric_limits<ctype>::lowest());
42
43 for (auto const& cell: Dune::elements(gv))
44 {
45 auto const& geo = cell.geometry();
46 for (int i=0; i<geo.corners(); ++i)
47 {
48 auto x = geo.corner(i);
49 low = Dune::min(low,x);
50 high = Dune::max(high,x);
51 }
52 }
53
54 return std::make_pair(low,high);
55 }
56
57 // ----------------------------------------------------------------------------------------------
58
69 template <class GridView>
70 std::unique_ptr<typename GridView::Grid> createCoverGrid(GridView const& gv, double volumeRatio=10)
71 {
72 using Grid = typename GridView::Grid;
73
74 // Create a uniform cartesian structured coarse grid that covers the bounding box of the given grid.
75 // The edge length of the cubes is chosen maximal, i.e. the smallest width of the bounding box.
76 auto box = boundingBox(gv);
77// std::cout << "Grid bounding box: " << box.first << " --- " << box.second << "\n";
78 auto d = box.second - box.first;
79 auto h = d;
80 while (true)
81 {
82 auto& maxh = *std::max_element(h.begin(),h.end());
83 auto& minh = *std::min_element(h.begin(),h.end());
84 if (maxh > 2*minh)
85 maxh /= 2;
86 else
87 break;
88 }
89// std::cout << "aiming at bounding box: " << box.first << " --- " << box.first+d << "\n";
90
91 std::unique_ptr<Grid> grid = createCuboid<Grid>(box.first,d,h);
92 auto coverBox = boundingBox(grid->leafGridView());
93// std::cout << "Grid bounding box: " << coverBox.first << " --- " << coverBox.second << "\n";
94
95 // Refine the cover grid until its cells have a volume larger than the given volumeRatio of
96 // any source grid cells they cover.
97 while (true)
98 {
99 bool refine = false;
100 for (auto const& cell: Dune::elements(gv))
101 {
102 auto x = cell.geometry().center();
103 auto const covercell = findCell(grid->leafGridView(),x);
104 if (covercell.geometry().volume() > volumeRatio*cell.geometry().volume())
105 {
106 grid->mark(1,covercell);
107 refine = true;
108 }
109 }
110
111 // stop refining if there is no cell to be refined
112 if (!refine)
113 break;
114
115 grid->preAdapt();
116 grid->adapt();
117 grid->postAdapt();
118 }
119
120 return grid;
121 }
122
123} /* end of namespace Kaskade */
124#endif
This file contains various utility functions that augment the basic functionality of Dune.
std::unique_ptr< typename GridView::Grid > createCoverGrid(GridView const &gv, double volumeRatio=10)
Creates a cartesian structured grid occupying the bounding box of the given grid view.
Definition: gridCover.hh:70
Cell< GridView > findCell(GridView const &gv, GlobalPosition< GridView > global)
Returns a cell of the given grid containing the specified global coordinate.
std::pair< Dune::FieldVector< typename GridView::ctype, GridView::dimensionworld >, Dune::FieldVector< typename GridView::ctype, GridView::dimensionworld > > boundingBox(GridView const &gv)
Computes a the bounding box of the given grid view.
Definition: gridCover.hh:35
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition: fixdune.hh:110
Dune::FieldVector< T, n > min(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise minimum.
Definition: fixdune.hh:122