Dune Core Modules (2.5.0)

starcdreader.hh
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
9#include <iostream>
10#include <fstream>
11
12namespace Dune {
13
47 template <class GridType>
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)
65 "Reading Star-CD format is not implemented for dimension " << dim);
66
67 // set up the grid 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
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::GeometryType(Dune::GeometryType::simplex,dim), 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;
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::GeometryType(Dune::GeometryType::pyramid,dim), 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]);
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
vector space out of a tensor product of fields.
Definition: fvector.hh:93
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:268
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:274
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:273
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:275
@ prism
Prism element in three dimensions.
Definition: type.hh:276
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:263
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: gridfactory.hh:290
virtual GridType * createGrid()
Finalize grid creation and hand over the grid.
Definition: gridfactory.hh:316
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: gridfactory.hh:279
Default exception class for I/O errors.
Definition: exceptions.hh:229
Default exception for dummy implementations.
Definition: exceptions.hh:261
File reader for the Star-CD format.
Definition: starcdreader.hh:48
static GridType * read(const std::string &fileName, bool verbose=true)
Read grid from a Star-CD file.
Definition: starcdreader.hh:57
Provide a generic factory class for unstructured grids.
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignment.hh:11
A unique label for each type of element that can occur in a grid.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)