dune-grid-glue  2.9
gridglueamirawriter.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-dune-grid-glue-exception
8 #ifndef DUNE_GRIDGLUE_ADAPTER_GRIDGLUEAMIRAWRITER_HH
9 #define DUNE_GRIDGLUE_ADAPTER_GRIDGLUEAMIRAWRITER_HH
10 
11 #include <fstream>
12 #include <sstream>
13 #include <type_traits>
14 
15 namespace Dune {
16 namespace GridGlue {
17 
21 {
22 
26  template <class Glue, int side>
27  static void writeIntersections(const Glue& glue, const std::string& filename)
28  {
29  static_assert((side==0 || side==1), "'side' can only be 0 or 1");
30 
31  std::ofstream fgrid;
32 
33  fgrid.open(filename.c_str());
34 
35  using GridView = typename Glue::template GridView<side>;
36  const int dim = GridView::dimension;
37  const int domdimw = GridView::dimensionworld;
38 
39  // coordinates have to be in R^3 in the VTK format
40  std::string coordinatePadding;
41  for (int i=domdimw; i<3; i++)
42  coordinatePadding += " 0";
43 
44  int overlaps = glue.size();
45 
46  if (dim==3) {
47 
48  fgrid << "# HyperSurface 0.1 ASCII \n" << std::endl;
49  fgrid<<"\n";
50  fgrid<<"Parameters {\n";
51  fgrid<<" Materials {\n";
52  fgrid<<" outside {\n";
53  fgrid<<" Id 0\n";
54  fgrid<<" }\n";
55  fgrid<<" inside {\n";
56  fgrid<<" Id 1\n";
57  fgrid<<" }\n";
58  fgrid<<" }\n";
59  fgrid<<"\n";
60  fgrid<<"}\n";
61 
62  // ////////////////////////////////////////////
63  // Write vertices
64  // ////////////////////////////////////////////
65 
66  //use dim and not dim+1
67  fgrid<<"\nVertices "<< overlaps*(dim)<<"\n";
68  auto isEnd = glue.template iend<side>();
69  for (auto isIt = glue.template ibegin<side>(); isIt != isEnd; ++isIt)
70  {
71  const auto& geometry = isIt->geometry();
72  for (int i = 0; i < geometry.corners(); ++i)
73  fgrid << geometry.corner(i) << coordinatePadding << std::endl;
74  }
75 
76  // ////////////////////////////////////////////
77  // Write triangles
78  // ////////////////////////////////////////////
79 
80  fgrid<<"NBranchingPoints 0\n";
81  fgrid<<"NVerticesOnCurves 0\n";
82  fgrid<<"BoundaryCurves 0\n";
83  fgrid<<"Patches 1\n";
84  fgrid<<"{\n";
85  fgrid<<"InnerRegion inside\n";
86  fgrid<<"OuterRegion outside\n";
87  fgrid<<"BoundaryID 0\n";
88  fgrid<<"BranchingPoints 0";
89  fgrid<<"\n";
90 
91  fgrid<<"Triangles "<<overlaps<<std::endl;
92 
93  for (int i=0;i<overlaps; i++)
94  fgrid<<i*dim+1<<" "<<i*dim+2<<" "<<i*dim+3<<std::endl;
95  fgrid<<"}\n";
96 
97  } else if (dim==2) {
98 
99  fgrid << "# AmiraMesh 3D ASCII 2.0 \n";
100  fgrid<<"\n";
101  fgrid<<"define Lines "<<3*overlaps<<"\n";
102  fgrid<<"nVertices "<<2*overlaps<<"\n";
103  fgrid<<"\n";
104  fgrid<<"Parameters {\n";
105  fgrid<<" ContentType \"HxLineSet\" \n";
106  fgrid<<"}\n";
107  fgrid<<"\n";
108  fgrid<<"Lines { int LineIdx } @1\n";
109  fgrid<<"Vertices { float[3] Coordinates } @2\n";
110  fgrid<<"\n";
111  fgrid<<"# Data section follows\n";
112  fgrid<<"@1 \n";
113  for (int i=0; i<overlaps;i++)
114  fgrid<<2*i<<"\n"<<2*i+1<<"\n"<<-1<<"\n";
115  fgrid<<"\n";
116  fgrid<<"@2 \n";
117 
118  auto isEnd = glue.template iend<side>();
119  for (auto isIt = glue.template ibegin<side>(); isIt != isEnd; ++isIt) {
120  const auto& geometry = isIt->geometry();
121  for (int i = 0; i <2; ++i)
122  fgrid << geometry.corner(i) <<" "<<0<<"\n";
123  }
124  }
125 
126  fgrid.close();
127  }
128 
129 public:
130  template<typename Glue>
131  static void write(const Glue& glue, const std::string& path, int appendix=1)
132  {
133  std::ostringstream name0;
134  name0 << path;
135  name0 << "/domain.surf" << std::setw(3) << std::setfill('0') << appendix;
136 
137  // Write extracted grid and remote intersection on the grid1-side
138  writeIntersections<Glue,0>(glue,name0.str());
139 
140  std::ostringstream name1;
141  name1 << path;
142  name1 << "/target.surf" << std::setw(3) << std::setfill('0') << appendix;
143 
144  writeIntersections<Glue,1>(glue, name1.str());
145  }
146 
147 };
148 
149 } // namespace GridGlue
150 } // namespace Dune
151 
152 #endif // DUNE_GRIDGLUE_ADAPTER_GRIDGLUEAMIRAWRITER_HH
Definition: gridglue.hh:37
Write remote intersections to a AmiraMesh file for debugging purposes.
Definition: gridglueamirawriter.hh:21
static void write(const Glue &glue, const std::string &path, int appendix=1)
Definition: gridglueamirawriter.hh:131