3.1-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
gridmanagertests.hh
Go to the documentation of this file.
1/*****************************************************************************
2 * See the file COPYING for full copying permissions. *
3 * *
4 * This program is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 3 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 *****************************************************************************/
24#ifndef DUMUX_TEST_IO_GRIDMANAGER_TESTS_HH
25#define DUMUX_TEST_IO_GRIDMANAGER_TESTS_HH
26
27#include <dune/common/version.hh>
28#include <dune/geometry/referenceelements.hh>
29#include <dune/grid/common/datahandleif.hh>
30#include <dune/grid/common/mcmgmapper.hh>
31#include <dune/grid/io/file/vtk.hh>
32
35
36namespace Dumux {
37
42template <class GridView>
44 : public Dune::CommDataHandleIF< VertexHandleNonZeroMin<GridView>, int >
45{
46 using Container = std::vector<int>;
47
48public:
49 VertexHandleNonZeroMin(Container &container, const GridView &gridView)
50 : gridView_(gridView)
51 , container_(container)
52 {}
53
54 bool contains(int dim, int codim) const
55 {
56 // only communicate vertices
57 return codim == dim;
58 }
59
60 bool fixedsize(int dim, int codim) const
61 {
62 // for each vertex we communicate a single field vector which
63 // has a fixed size
64 return true;
65 }
66
67 template<class EntityType>
68 size_t size (const EntityType &e) const
69 {
70 // communicate a field type per entity
71 return 1;
72 }
73
74 template<class MessageBufferImp, class EntityType>
75 void gather(MessageBufferImp &buff, const EntityType &e) const
76 {
77 int vIdx = gridView_.indexSet().index(e);
78 buff.write(container_[vIdx]);
79 }
80
81 template<class MessageBufferImp, class EntityType>
82 void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
83 {
84 int vIdx = gridView_.indexSet().index(e);
85 int tmp;
86 buff.read(tmp);
87 using std::min;
88 if (tmp > 0)
89 {
90 if (container_[vIdx] == 0)
91 container_[vIdx] = tmp;
92 else
93 container_[vIdx] = min(container_[vIdx], tmp);
94 }
95 }
96
97private:
98 const GridView gridView_;
99 Container &container_;
100};
101
102template<class Grid>
104{
105 using GridView = typename Grid::LeafGridView;
106 using Scalar = double;
107 static const int dim = Grid::dimension;
108 using GridManager = typename Dumux::GridManager<Grid>;
109 using ReferenceElements = typename Dune::ReferenceElements<Scalar, dim>;
110
111public:
112
113 static void testBoundaryAndElementMarkers(const std::string& type = "gmsh",
114 const std::string& vtkFileName = "test",
115 bool refine = true)
116 {
117 // make the grid manager and initialize the grid
118 GridManager gridManager;
119 gridManager.init();
120 auto gridData = gridManager.getGridData();
121 const auto& leafGridView = gridManager.grid().leafGridView();
122
123 // read the boundary and element markers as well as the rank
124 std::vector<int> boundaryMarker, elementMarker, rank;
125 getBoundaryMarkers_(leafGridView, gridData, boundaryMarker);
126 getElementMarkers_(leafGridView, gridData, elementMarker, type);
127 getRank_(leafGridView, rank);
128
129 // construct a vtk output writer and attach the boundary markers
130 Dune::VTKSequenceWriter<typename Grid::LeafGridView> vtkWriter(leafGridView, vtkFileName, ".", "");
131 vtkWriter.addVertexData(boundaryMarker, "boundaryMarker");
132 vtkWriter.addCellData(elementMarker, "elementMarker");
133 vtkWriter.addCellData(rank, "rank");
134 vtkWriter.write(0);
135
136 if (refine)
137 {
138 // refine grid once and write out the markers again
139 gridManager.grid().globalRefine(1);
140 getBoundaryMarkers_(leafGridView, gridData, boundaryMarker);
141 getElementMarkers_(leafGridView, gridData, elementMarker, type);
142 getRank_(leafGridView, rank);
143 vtkWriter.write(1);
144 }
145 }
146
147 static void testElementMarkers(const std::string& type = "gmsh",
148 const std::string& vtkFileName = "test",
149 bool refine = true)
150 {
151 // make the grid manager and initialize the grid
152 GridManager gridManager;
153 gridManager.init();
154 auto gridData = gridManager.getGridData();
155
156 // read the element markers and the rank
157 std::vector<int> elementMarker, rank;
158 getElementMarkers_(gridManager.grid().leafGridView(), gridData, elementMarker, type);
159 getRank_(gridManager.grid().leafGridView(), rank);
160
161 // construct a vtk output writer and attach the element markers
162 Dune::VTKSequenceWriter<typename Grid::LeafGridView> vtkWriter(gridManager.grid().leafGridView(), vtkFileName, ".", "");
163 vtkWriter.addCellData(elementMarker, "elementMarker");
164 vtkWriter.addCellData(rank, "rank");
165 vtkWriter.write(0);
166
167 if (refine)
168 {
169 // refine grid once and write out the markers again
170 gridManager.grid().globalRefine(1);
171 getElementMarkers_(gridManager.grid().leafGridView(), gridData, elementMarker, type);
172 getRank_(gridManager.grid().leafGridView(), rank);
173 vtkWriter.write(1);
174 }
175 }
176
177 static void testVertexMarkers(const std::string& type = "dgf",
178 const std::string& vtkFileName = "test")
179 {
180 // make the grid manager and initialize the grid
181 GridManager gridManager;
182 gridManager.init();
183 auto gridData = gridManager.getGridData();
184
185 // read the element markers and the rank
186 std::vector<int> vertexMarker;
187 getVertexMarkers_(gridManager.grid().levelGridView(0), gridData, vertexMarker, type);
188
189 // construct a vtk output writer and attach the element markers
190 Dune::VTKSequenceWriter<typename Grid::LevelGridView> vtkWriter(gridManager.grid().levelGridView(0), vtkFileName, ".", "");
191 vtkWriter.addVertexData(vertexMarker, "vertexData");
192 vtkWriter.write(0);
193 }
194
195private:
196
197 static void getRank_(const GridView& gridView, std::vector<int>& rank)
198 {
199 rank.clear();
200 rank.resize(gridView.size(0));
201
202 for(const auto& element : elements(gridView))
203 {
204 auto eIdx = gridView.indexSet().index(element);
205 rank[eIdx] = gridView.comm().rank();
206 }
207 }
208
209 template<class GridData>
210 static void getElementMarkers_(const GridView& gridView,
211 const GridData& gridData,
212 std::vector<int>& elementMarker,
213 const std::string& type)
214 {
215 elementMarker.clear();
216 elementMarker.resize(gridView.size(0));
217
218 for(const auto& element : elements(gridView))
219 {
220 const auto eIdx = gridView.indexSet().index(element);
221 if (type == "gmsh")
222 elementMarker[eIdx] = gridData->getElementDomainMarker(element);
223 else if (type == "dgf")
224 elementMarker[eIdx] = gridData->parameters(element)[0];
225 else
226 DUNE_THROW(Dune::InvalidStateException, "No parameters for type " << type);
227 }
228 }
229
230 template<class LevelGridView, class GridData>
231 static void getVertexMarkers_(const LevelGridView& gridView,
232 const GridData& gridData,
233 std::vector<int>& vertexMarker,
234 const std::string& type)
235 {
236 if (type != "dgf")
237 DUNE_THROW(Dune::InvalidStateException, "Vertex marker only exist for dgf grids.");
238
239 vertexMarker.clear();
240 vertexMarker.resize(gridView.size(Grid::dimension));
241
242 for (const auto& vertex : vertices(gridView.grid().levelGridView(0)))
243 {
244 const auto vIdx = gridView.indexSet().index(vertex);
245 vertexMarker[vIdx] = gridData->parameters(vertex)[0];
246 }
247 }
248
249 template<class GridData>
250 static void getBoundaryMarkers_(const GridView& gridView,
251 const GridData& gridData,
252 std::vector<int>& boundaryMarker)
253 {
254 boundaryMarker.clear();
255 boundaryMarker.resize(gridView.size(dim), 0);
256
257 for(const auto& element : elements(gridView))
258 {
259 for(const auto& intersection : intersections(gridView, element))
260 {
261 if (!intersection.boundary())
262 continue;
263
264 const auto refElement = ReferenceElements::general(element.geometry().type());
265 const auto numVertices = refElement.size(intersection.indexInInside(), 1, dim);
266 // loop over vertices of the intersection facet
267 for(std::size_t vIdx = 0; vIdx < numVertices; vIdx++)
268 {
269 // get local vertex index with respect to the element
270 auto vIdxLocal = refElement.subEntity(intersection.indexInInside(), 1, vIdx, dim);
271 auto vIdxGlobal = gridView.indexSet().subIndex(element, vIdxLocal, dim);
272
273 // make sure we always take the lowest non-zero marker (problem dependent!)
274 if (boundaryMarker[vIdxGlobal] == 0)
275 boundaryMarker[vIdxGlobal] = gridData->getBoundaryDomainMarker(intersection);
276 else
277 {
278 if (boundaryMarker[vIdxGlobal] > gridData->getBoundaryDomainMarker(intersection))
279 boundaryMarker[vIdxGlobal] = gridData->getBoundaryDomainMarker(intersection);
280 }
281 }
282 }
283 }
284
285 // In a parallel setting, it is possible that not all boundary vertices
286 // will be reached on each process by the procedure above that loops
287 // over the boundary intersections and their vertices. This mandates the
288 // following synchronization.
289 if (gridView.comm().size() > 1)
290 {
291 VertexHandleNonZeroMin<GridView> dataHandle(boundaryMarker, gridView);
292 gridView.communicate(dataHandle,
293 Dune::InteriorBorder_All_Interface,
294 Dune::ForwardCommunication);
295 }
296 }
297};
298
299} // end namespace Dumux
300
301#endif
The available discretization methods in Dumux.
Dune::IteratorRange< typename MultiDomainGlue< DomainGridView, TargetGridView, DomainMapper, TargetMapper >::Intersections::const_iterator > intersections(const MultiDomainGlue< DomainGridView, TargetGridView, DomainMapper, TargetMapper > &glue)
Range generator to iterate with range-based for loops over all intersections as follows: for (const a...
Definition: glue.hh:62
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition: gridmanager_base.hh:312
data handle for parallel communication which takes the minimum non-zero values that are attached to v...
Definition: gridmanagertests.hh:45
bool fixedsize(int dim, int codim) const
Definition: gridmanagertests.hh:60
void gather(MessageBufferImp &buff, const EntityType &e) const
Definition: gridmanagertests.hh:75
size_t size(const EntityType &e) const
Definition: gridmanagertests.hh:68
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
Definition: gridmanagertests.hh:82
VertexHandleNonZeroMin(Container &container, const GridView &gridView)
Definition: gridmanagertests.hh:49
bool contains(int dim, int codim) const
Definition: gridmanagertests.hh:54
Definition: gridmanagertests.hh:104
static void testElementMarkers(const std::string &type="gmsh", const std::string &vtkFileName="test", bool refine=true)
Definition: gridmanagertests.hh:147
static void testBoundaryAndElementMarkers(const std::string &type="gmsh", const std::string &vtkFileName="test", bool refine=true)
Definition: gridmanagertests.hh:113
static void testVertexMarkers(const std::string &type="dgf", const std::string &vtkFileName="test")
Definition: gridmanagertests.hh:177
Convience header that includes all grid manager specializations.