dune-grid-glue  2.9
gridgluevtkwriter.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-dune-grid-glue-exception
5 /*
6  * Filename: GridGlueVtkWriter.hh
7  * Version: 1.0
8  * Created on: Mar 5, 2009
9  * Author: Gerrit Buse
10  * ---------------------------------
11  * Project: dune-grid-glue
12  * Description: Class thought to make graphical debugging of couplings easier.
13  *
14  */
20 #ifndef DUNE_GRIDGLUE_ADAPTER_GRIDGLUEVTKWRITER_HH
21 #define DUNE_GRIDGLUE_ADAPTER_GRIDGLUEVTKWRITER_HH
22 
23 
24 #include <fstream>
25 #include <iomanip>
26 #include <type_traits>
27 #include <vector>
28 
29 #include <dune/common/classname.hh>
30 #include <dune/geometry/type.hh>
31 #include <dune/geometry/referenceelements.hh>
32 
34 
35 namespace Dune {
36 namespace GridGlue {
37 
41 {
42 
46  template <class Glue, int side>
47  static void writeExtractedPart(const Glue& glue, const std::string& filename)
48  {
49  static_assert((side==0 || side==1), "'side' can only be 0 or 1");
50 
51  std::ofstream fgrid;
52 
53  fgrid.open(filename.c_str());
54 
55  using GridView = typename Glue::template GridView<side>;
56  using Extractor = typename Glue::template GridPatch<side>;
57 
58  typedef typename GridView::ctype ctype;
59 
60  const int domdimw = GridView::dimensionworld;
61  const int patchDim = Extractor::dim - Extractor::codim;
62 
63  // coordinates have to be in R^3 in the VTK format
64  std::string coordinatePadding;
65  for (int i=domdimw; i<3; i++)
66  coordinatePadding += " 0";
67 
68  fgrid << "# vtk DataFile Version 2.0\nFilename: " << filename << "\nASCII" << std::endl;
69 
70  // WRITE POINTS
71  // ----------------
72  std::vector<typename Extractor::Coords> coords;
73  glue.template patch<side>().getCoords(coords);
74 
75  fgrid << ((patchDim==3) ? "DATASET UNSTRUCTURED_GRID" : "DATASET POLYDATA") << std::endl;
76  fgrid << "POINTS " << coords.size() << " " << Dune::className<ctype>() << std::endl;
77 
78  for (size_t i=0; i<coords.size(); i++)
79  fgrid << coords[i] << coordinatePadding << std::endl;
80 
81  fgrid << std::endl;
82 
83  // WRITE POLYGONS
84  // ----------------
85 
86  std::vector<typename Extractor::VertexVector> faces;
87  std::vector<Dune::GeometryType> geometryTypes;
88  glue.template patch<side>().getFaces(faces);
89  glue.template patch<side>().getGeometryTypes(geometryTypes);
90 
91  unsigned int faceCornerCount = 0;
92  for (size_t i=0; i<faces.size(); i++)
93  faceCornerCount += faces[i].size();
94 
95  fgrid << ((patchDim==3) ? "CELLS " : "POLYGONS ")
96  << geometryTypes.size() << " " << geometryTypes.size() + faceCornerCount << std::endl;
97 
98  for (size_t i=0; i<faces.size(); i++) {
99 
100  fgrid << faces[i].size();
101 
102  // vtk expects the vertices to by cyclically ordered
103  // therefore unfortunately we have to deal with several element types on a case-by-case basis
104  if (geometryTypes[i].isSimplex()) {
105  for (int j=0; j<patchDim+1; j++)
106  fgrid << " " << faces[i][j];
107 
108  } else if (geometryTypes[i].isQuadrilateral()) {
109  fgrid << " " << faces[i][0] << " " << faces[i][1]
110  << " " << faces[i][3] << " " << faces[i][2];
111 
112  } else if (geometryTypes[i].isPyramid()) {
113  fgrid << " " << faces[i][0] << " " << faces[i][1]
114  << " " << faces[i][3] << " " << faces[i][2] << " " << faces[i][4];
115 
116  } else if (geometryTypes[i].isPrism()) {
117  fgrid << " " << faces[i][0] << " " << faces[i][2] << " " << faces[i][1]
118  << " " << faces[i][3] << " " << faces[i][5] << " " << faces[i][4];
119 
120  } else if (geometryTypes[i].isHexahedron()) {
121  fgrid << " " << faces[i][0] << " " << faces[i][1]
122  << " " << faces[i][3] << " " << faces[i][2]
123  << " " << faces[i][4] << " " << faces[i][5]
124  << " " << faces[i][7] << " " << faces[i][6];
125 
126  } else {
127  DUNE_THROW(Dune::NotImplemented, "Geometry type " << geometryTypes[i] << " not supported yet");
128  }
129 
130  fgrid << std::endl;
131  }
132 
133  fgrid << std::endl;
134 
135  // 3d VTK files need an extra section specifying the CELL_TYPES aka GeometryTypes
136  if (patchDim==3) {
137 
138  fgrid << "CELL_TYPES " << geometryTypes.size() << std::endl;
139 
140  for (size_t i=0; i<geometryTypes.size(); i++) {
141  if (geometryTypes[i].isSimplex())
142  fgrid << "10" << std::endl;
143  else if (geometryTypes[i].isHexahedron())
144  fgrid << "12" << std::endl;
145  else if (geometryTypes[i].isPrism())
146  fgrid << "13" << std::endl;
147  else if (geometryTypes[i].isPyramid())
148  fgrid << "14" << std::endl;
149  else
150  DUNE_THROW(Dune::NotImplemented, "Geometry type " << geometryTypes[i] << " not supported yet");
151 
152  }
153 
154  }
155 
156 #if 0
157  // WRITE CELL DATA
158  // ---------------
159  ctype accum = 0.0, delta = 1.0 / (ctype) (gridSubEntityData.size()-1);
160 
161  fgrid << "CELL_DATA " << gridSubEntityData.size() << std::endl;
162  fgrid << "SCALARS property_coding " << Dune::className<ctype>() << " 1" << std::endl;
163  fgrid << "LOOKUP_TABLE default" << std::endl;
164 
165  for (typename GridSubEntityData::const_iterator sEIt = gridSubEntityData.begin();
166  sEIt != gridSubEntityData.end();
167  ++sEIt, accum += delta)
168  {
169  // "encode" the parent with one color...
170  fgrid << accum << std::endl;
171  }
172 #endif
173  fgrid.close();
174  }
175 
176 
180  template <class Glue, int side>
181  static void writeIntersections(const Glue& glue, const std::string& filename)
182  {
183  static_assert((side==0 || side==1), "'side' can only be 0 or 1");
184 
185  std::ofstream fmerged;
186 
187  fmerged.open(filename.c_str());
188 
189  using GridView = typename Glue::template GridView<side>;
190  typedef typename GridView::ctype ctype;
191 
192  const int domdimw = GridView::dimensionworld;
193  const int intersectionDim = Glue::Intersection::mydim;
194 
195  // coordinates have to be in R^3 in the VTK format
196  std::string coordinatePadding;
197  for (int i=domdimw; i<3; i++)
198  coordinatePadding += " 0";
199 
200  int overlaps = glue.size();
201 
202  // WRITE POINTS
203  // ----------------
204  using Extractor = typename Glue::template GridPatch<0>;
205  std::vector<typename Extractor::Coords> coords;
206  glue.template patch<side>().getCoords(coords);
207 
208  // the merged grid (i.e. the set of remote intersections
209  fmerged << "# vtk DataFile Version 2.0\nFilename: " << filename << "\nASCII" << std::endl;
210  fmerged << ((intersectionDim==3) ? "DATASET UNSTRUCTURED_GRID" : "DATASET POLYDATA") << std::endl;
211  fmerged << "POINTS " << overlaps*(intersectionDim+1) << " " << Dune::className<ctype>() << std::endl;
212 
213  for (const auto& intersection : intersections(glue, Reverse<side == 1>{}))
214  {
215  const auto& geometry = intersection.geometry();
216  for (int i = 0; i < geometry.corners(); ++i)
217  fmerged << geometry.corner(i) << coordinatePadding << std::endl;
218  }
219 
220  // WRITE POLYGONS
221  // ----------------
222 
223  std::vector<typename Extractor::VertexVector> faces;
224  std::vector<Dune::GeometryType> geometryTypes;
225  glue.template patch<side>().getFaces(faces);
226  glue.template patch<side>().getGeometryTypes(geometryTypes);
227 
228  unsigned int faceCornerCount = 0;
229  for (size_t i=0; i<faces.size(); i++)
230  faceCornerCount += faces[i].size();
231 
232  int grid0SimplexCorners = intersectionDim+1;
233  fmerged << ((intersectionDim==3) ? "CELLS " : "POLYGONS ")
234  << overlaps << " " << (grid0SimplexCorners+1)*overlaps << std::endl;
235 
236  for (int i = 0; i < overlaps; ++i) {
237  fmerged << grid0SimplexCorners;
238  for (int j=0; j<grid0SimplexCorners; j++)
239  fmerged << " " << grid0SimplexCorners*i+j;
240  fmerged << std::endl;
241  }
242 
243  // 3d VTK files need an extra section specifying the CELL_TYPES aka GeometryTypes
244  if (intersectionDim==3) {
245 
246  fmerged << "CELL_TYPES " << overlaps << std::endl;
247 
248  for (int i = 0; i < overlaps; i++)
249  fmerged << "10" << std::endl;
250 
251  }
252 
253 #if 0
254  // WRITE CELL DATA
255  // ---------------
256  ctype accum = 0.0, delta = 1.0 / (ctype) (gridSubEntityData.size()-1);
257 
258  fmerged << "CELL_DATA " << overlaps << std::endl;
259  fmerged << "SCALARS property_coding " << Dune::className<ctype>() << " 1" << std::endl;
260  fmerged << "LOOKUP_TABLE default" << std::endl;
261 
262  for (typename GridSubEntityData::const_iterator sEIt = gridSubEntityData.begin();
263  sEIt != gridSubEntityData.end();
264  ++sEIt, accum += delta)
265  {
266  // ...and mark all of its merged grid parts with the same color
267  for (int j = 0; j < sEIt->first.second; ++j)
268  fmerged << accum << std::endl;
269  }
270 #endif
271  fmerged.close();
272  }
273 
274 public:
275  template<typename Glue>
276  static void write(const Glue& glue, const std::string& filenameTrunk)
277  {
278 
279  // Write extracted grid and remote intersection on the grid0-side
280  writeExtractedPart<Glue,0>(glue,
281  filenameTrunk + "-grid0.vtk");
282 
283  writeIntersections<Glue,0>(glue,
284  filenameTrunk + "-intersections-grid0.vtk");
285 
286  // Write extracted grid and remote intersection on the grid1-side
287  writeExtractedPart<Glue,1>(glue,
288  filenameTrunk + "-grid1.vtk");
289 
290  writeIntersections<Glue,1>(glue,
291  filenameTrunk + "-intersections-grid1.vtk");
292 
293  }
294 
295 };
296 
297 } /* namespace GridGlue */
298 } /* namespace Dune */
299 
300 #endif // DUNE_GRIDGLUE_ADAPTER_GRIDGLUEVTKWRITER_HH
Central component of the module implementing the coupling of two grids.
Definition: gridglue.hh:37
IteratorRange<... > intersections(const GridGlue<... > &glue, const Reverse<... > &reverse=!reversed)
Iterate over all intersections of a GridGlue.
Write remote intersections to a vtk file for debugging purposes.
Definition: gridgluevtkwriter.hh:41
static void write(const Glue &glue, const std::string &filenameTrunk)
Definition: gridgluevtkwriter.hh:276
Definition: rangegenerators.hh:17
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:46
static constexpr auto codim
Definition: extractor.hh:52
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:275
static constexpr auto dim
Definition: extractor.hh:51