Loading [MathJax]/extensions/tex2jax.js

dune-mmesh (unstable)

dgfparser.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_MMESH_GRID_DGFPARSER_HH
4#define DUNE_MMESH_GRID_DGFPARSER_HH
5
6// System includes
7#include <memory>
8#include <utility>
9#include <vector>
10
11// Dune includes
12#include <dune/grid/common/intersection.hh>
13#include <dune/grid/io/file/dgfparser/blocks/projection.hh>
14#include <dune/grid/io/file/dgfparser/dgfparser.hh>
15#include <dune/grid/io/file/dgfparser/parser.hh>
16
17// MMesh includes
18#include <dune/mmesh/grid/explicitgridfactory.hh>
19
20namespace Dune {
21
22// External Forward Declarations
23// -----------------------------
24template <class GridImp, class IntersectionImp>
25class Intersection;
26
27// DGFGridFactory for MMesh
28// ------------------------
29template <class HostGrid, int dim>
30struct DGFGridFactory<MMesh<HostGrid, dim> > {
31 typedef MMesh<HostGrid, dim> Grid;
32 const static int dimension = Grid::dimension;
33 typedef MPIHelper::MPICommunicator MPICommunicatorType;
34 typedef typename Grid::template Codim<0>::Entity Element;
35 typedef typename Grid::template Codim<dimension>::Entity Vertex;
36 typedef Dune::MMeshExplicitGridFactory<Grid> GridFactory;
37
38 explicit DGFGridFactory(
39 std::istream &input,
40 MPICommunicatorType comm = MPIHelper::getCommunicator());
41 explicit DGFGridFactory(
42 const std::string &filename,
43 MPICommunicatorType comm = MPIHelper::getCommunicator());
44
46 Grid *grid() const { return grid_; }
47
49 template <class Intersection>
50 bool wasInserted(const Intersection &intersection) const {
51 return factory_.wasInserted(intersection);
52 }
53
55 template <class Intersection>
56 int boundaryId(const Intersection &intersection) const {
57 return intersection.impl().boundaryId();
58 }
59
61 std::vector<double> &parameter(const Element &element) {
62 if (numParameters<0>() <= 0) {
63 DUNE_THROW(InvalidStateException,
64 "Calling DGFGridFactory::parameter is only allowed if there "
65 "are parameters.");
66 }
67 return dgf_.elParams[factory_.insertionIndex(element)];
68 }
69
71 std::vector<double> &parameter(const Vertex &vertex) {
72 if (numParameters<dimension>() <= 0) {
73 DUNE_THROW(InvalidStateException,
74 "Calling DGFGridFactory::parameter is only allowed if there "
75 "are parameters.");
76 }
77 return dgf_.vtxParams[factory_.insertionIndex(vertex)];
78 }
79
81 bool haveBoundaryParameters() const { return dgf_.haveBndParameters; }
82
83 template <class GG, class II>
84 const DGFBoundaryParameter::type boundaryParameter(
85 const Intersection<GG, II> &intersection) const {
86 return DGFBoundaryParameter::type();
87 }
88
89 template <int codim>
90 int numParameters() const {
91 return 0;
92 }
93
94 private:
95 bool generate(std::istream &input) {
96 dgf_.element = DuneGridFormatParser::Simplex;
97 dgf_.dimgrid = dimension;
98 dgf_.dimw = dimension;
99
100 if (!dgf_.readDuneGrid(input, dimension, dimension)) return false;
101
102 // Insert vertices
103 for (int n = 0; n < dgf_.nofvtx; ++n) {
104 typename GridFactory::WorldVector coord;
105 for (std::size_t i = 0; i < dimension; ++i) coord[i] = dgf_.vtx[n][i];
106 factory_.insertVertex(coord);
107 }
108
109 // Insert elements
110 for (int n = 0; n < dgf_.nofelements; ++n)
111 factory_.insertElement(GeometryTypes::simplex(dimension),
112 dgf_.elements[n]);
113
114 // Insert boundary segments and ids
115 for (const auto &face : dgf_.facemap) {
116 const auto &entityKey = face.first;
117 const std::size_t boundaryId = face.second.first;
118
119 std::vector<unsigned int> vertices;
120 for (int i = 0; i < entityKey.size(); ++i)
121 vertices.push_back(entityKey[i]);
122
123 std::sort(vertices.begin(), vertices.end());
124
125 // insert boundary segment
126 factory_.insertBoundarySegment(vertices);
127
128 // insert boundary id
129 std::size_t index = factory_.boundarySegments().size() - 1;
130 factory_.addBoundaryId(index, boundaryId);
131 }
132
133 grid_ = factory_.createGrid().release();
134 return true;
135 }
136
137 Grid *grid_;
138 GridFactory factory_;
139 DuneGridFormatParser dgf_;
140};
141
142// DGFGridInfo for MMesh
143// ---------------------
144
145template <class HostGrid, int dim>
146struct DGFGridInfo<MMesh<HostGrid, dim> > {
147 static int refineStepsForHalf() { return 2; }
148
149 static double refineWeight() { return -1; }
150};
151
152// Implementation of DGFGridFactory for MMesh
153// ------------------------------------------
154
156template <class HostGrid, int dim>
157inline DGFGridFactory<MMesh<HostGrid, dim> >::DGFGridFactory(
158 std::istream &input, MPICommunicatorType comm)
159 : dgf_(0, 1) {
160 input.clear();
161 input.seekg(0);
162 if (!input) DUNE_THROW(DGFException, "Error resetting input stream.");
163 generate(input);
164}
165
167template <class HostGrid, int dim>
168inline DGFGridFactory<MMesh<HostGrid, dim> >::DGFGridFactory(
169 const std::string &filename, MPICommunicatorType comm)
170 : dgf_(0, 1) {
171 std::ifstream input(filename.c_str());
172 if (!input)
173 DUNE_THROW(DGFException, "Macrofile " << filename << " not found.");
174 if (!generate(input))
175 DUNE_THROW(DGFException,
176 "Could not generate MMesh from macrofile " << filename);
177 input.close();
178}
179
180} // namespace Dune
181
182#endif
specialization of the explicit GridFactory for MMesh
Definition: explicitgridfactory.hh:32
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 17, 23:30, 2025)