dune-grid  2.6-git
starcdreader.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 #ifndef DUNE_STARCD_READER_HH
4 #define DUNE_STARCD_READER_HH
5 
6 #include <dune/common/exceptions.hh>
7 #include <dune/geometry/type.hh>
9 #include <iostream>
10 #include <fstream>
11 
12 namespace Dune {
13 
47  template <class GridType>
48  class StarCDReader {
49 
50  public:
51 
57  static GridType* read(const std::string& fileName, bool verbose = true)
58  {
59  // extract the grid dimension
60  const int dim = GridType::dimension;
61 
62  // currently only dim = 3 is implemented
63  if (dim != 3)
64  DUNE_THROW(Dune::NotImplemented,
65  "Reading Star-CD format is not implemented for dimension " << dim);
66 
67  // set up the grid factory
68  GridFactory<GridType> factory;
69 
70  // set the name of the vertex file
71  std::string vertexFileName = fileName + ".vrt";
72 
73  // set the vertex input stream
74  std::ifstream vertexFile(vertexFileName.c_str());
75  if (!vertexFile)
76  DUNE_THROW(Dune::IOError, "Could not open " << vertexFileName);
77 
78  // read the vertices
79  int dummyIdx;
80  int numberOfVertices = 0;
81  while (vertexFile >> dummyIdx) {
82  numberOfVertices++;
83 
84  Dune::FieldVector<double,dim> position;
85 
86  for (int k = 0; k < dim; k++)
87  vertexFile >> position[k];
88 
89  factory.insertVertex(position);
90  }
91  if (verbose)
92  std::cout << numberOfVertices << " vertices read." << std::endl;
93 
94  // set the name of the element file
95  std::string elementFileName = fileName + ".cel";
96 
97  // set the element input stream
98  std::ifstream elementFile(elementFileName.c_str());
99  if (!elementFile)
100  DUNE_THROW(Dune::IOError, "Could not open " << elementFileName);
101 
102  // read the elements
103  int numberOfElements = 0;
104  int numberOfSimplices = 0;
105  int numberOfPyramids = 0;
106  int numberOfPrisms = 0;
107  int numberOfCubes = 0;;
108  int maxNumberOfVertices = (int)pow(2, dim);
109  int isVolume = 1;
110  while (elementFile >> dummyIdx) {
111  std::vector<unsigned int> vertices(maxNumberOfVertices);
112  for (int k = 0; k < maxNumberOfVertices; k++)
113  elementFile >> vertices[k];
114 
115  int boundaryId;
116  elementFile >> boundaryId;
117 
118  int volumeOrSurface[2];
119  elementFile >> volumeOrSurface[0] >> volumeOrSurface[1];
120 
121  if (volumeOrSurface[0] == isVolume) {
122  numberOfElements++;
123 
124  if (vertices[2] == vertices[3]) { // simplex or prism
125  if (vertices[4] == vertices[5]) { // simplex
126  numberOfSimplices++;
127  std::vector<unsigned int> simplexVertices(4);
128  for (int k = 0; k < 3; k++)
129  simplexVertices[k] = vertices[k] - 1;
130  simplexVertices[3] = vertices[4] - 1;
131  factory.insertElement(Dune::GeometryTypes::tetrahedron, simplexVertices);
132  }
133  else { // prism
134  numberOfPrisms++;
135  std::vector<unsigned int> prismVertices(6);
136  for (int k = 0; k < 3; k++)
137  prismVertices[k] = vertices[k] - 1;
138  for (int k = 3; k < 6; k++)
139  prismVertices[k] = vertices[k+1] - 1;
140  factory.insertElement(Dune::GeometryTypes::prism, prismVertices);
141  }
142  }
143  else { // cube or pyramid
144  if (vertices[4] == vertices[5]) { // pyramid
145  numberOfPyramids++;
146  std::vector<unsigned int> pyramidVertices(5);
147  for (int k = 0; k < 5; k++)
148  pyramidVertices[k] = vertices[k] - 1;
149  factory.insertElement(Dune::GeometryTypes::pyramid, pyramidVertices);
150  }
151  else { // cube
152  numberOfCubes++;
153  std::vector<unsigned int> cubeVertices(8);
154  for (int k = 0; k < 8; k++)
155  cubeVertices[k] = vertices[k] - 1;
156  std::swap(cubeVertices[2], cubeVertices[3]);
157  std::swap(cubeVertices[6], cubeVertices[7]);
158  factory.insertElement(Dune::GeometryTypes::hexahedron, cubeVertices);
159  }
160  }
161  }
162  }
163  if (verbose)
164  std::cout << numberOfElements << " elements read: "
165  << numberOfSimplices << " simplices, " << numberOfPyramids << " pyramids, "
166  << numberOfPrisms << " prisms, " << numberOfCubes << " cubes." << std::endl;
167 
168  // finish off the construction of the grid object
169  if (verbose)
170  std::cout << "Starting createGrid() ... " << std::flush;
171 
172  return factory.createGrid();
173 
174  }
175 
176  };
177 
178 }
179 
180 #endif
File reader for the Star-CD format.
Definition: starcdreader.hh:48
Definition: common.hh:187
Definition: common.hh:184
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:263
Provide a generic factory class for unstructured grids.
virtual GridType * createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:316
Definition: common.hh:186
static GridType * read(const std::string &fileName, bool verbose=true)
Read grid from a Star-CD file.
Definition: starcdreader.hh:57
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: common/gridfactory.hh:290
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: common/gridfactory.hh:279
Definition: common.hh:185
Include standard header files.
Definition: agrid.hh:58