dune-mmesh (unstable)

implicitgridfactory.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_MMESH_GRID_IMPLICITGRIDFACTORY_HH
5#define DUNE_MMESH_GRID_IMPLICITGRIDFACTORY_HH
6
7// System includes
8#include <algorithm>
9#include <array>
10#include <limits>
11#include <map>
12#include <memory>
13#include <unordered_map>
14#include <unordered_set>
15
16// Dune includes
17#include <dune/geometry/referenceelements.hh>
18#include <dune/grid/common/boundarysegment.hh>
19#include <dune/grid/common/gridfactory.hh>
20
21// MMesh includes
25
26namespace Dune {
27
34template <class Grid>
35class MMeshImplicitGridFactory : public GridFactoryInterface<Grid> {
37
38 public:
40 typedef typename Grid::ctype ctype;
42 typedef typename Grid::HostGridType HostGrid;
43
45 static const int dimension = Grid::dimension;
47 static const int dimensionworld = Grid::dimensionworld;
48
50 typedef FieldVector<ctype, dimensionworld> WorldVector;
52 typedef FieldMatrix<ctype, dimensionworld, dimensionworld> WorldMatrix;
53
55 typedef Dune::BoundarySegment<dimension, dimensionworld> BoundarySegment;
57 typedef typename Grid::IdType IdType;
58
60 typedef std::unordered_map<IdType, std::size_t> BoundarySegments;
61 typedef std::unordered_map<std::size_t, std::size_t> BoundaryIds;
62
64 typedef std::unordered_map<IdType, std::size_t> InterfaceSegments;
65
66 template <int codim>
67 struct Codim {
68 typedef typename Grid::template Codim<codim>::Entity Entity;
69 };
70
71 private:
72 typedef typename HostGrid::Point Point;
73 typedef typename HostGrid::Vertex_handle Vertex_handle;
74 typedef typename Grid::template HostGridEntity<0> Element_handle;
75 typedef typename Grid::template HostGridEntity<1> Face_handle;
76
77 public:
79 static const bool supportsBoundaryIds = true;
81 static const bool supportPeriodicity = false;
82
85
95 void insertElement(const GeometryType& type,
96 const std::vector<unsigned int>& v) {
97 Element_handle fh;
98 if (isElement(v, fh)) fh->info().insertionIndex = countElements;
99
100 // Increase element count in each case
101 countElements++;
102 };
103
104 void insertElement(const GeometryType& type,
105 const std::vector<unsigned int>& v,
106 const size_t domainMarker) {
107 insertElement(type, v);
108 }
109
115 template <int d = dimension>
116 std::enable_if_t<d == 2, bool> isElement(const std::vector<unsigned int>& v,
117 Element_handle& fh) const {
118 assert(v.size() == dimension + 1);
119 return tr_.is_face(vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], fh);
120 }
121
127 template <int d = dimension>
128 std::enable_if_t<d == 3, bool> isElement(const std::vector<unsigned int>& v,
129 Element_handle& fh) const {
130 assert(v.size() == dimension + 1);
131 return tr_.is_cell(vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], vhs_[v[3]], fh);
132 }
133
138 void insertBoundarySegment(const std::vector<unsigned int>& vertices) {
139 assert(vertices.size() == dimension);
140
141 std::vector<std::size_t> sorted_vertices;
142 for (const auto& v : vertices)
143 sorted_vertices.push_back(vhs_[v]->info().id);
144 std::sort(sorted_vertices.begin(), sorted_vertices.end());
145
146 if (boundarySegments_.find(sorted_vertices) != boundarySegments_.end())
147 DUNE_THROW(GridError, "A boundary segment was inserted twice.");
148
149 boundarySegments_.insert(
150 std::make_pair(sorted_vertices, countBoundarySegments++));
151 }
152
154 const std::vector<unsigned int>& vertices,
155 const std::shared_ptr<Dune::BoundarySegment<dimension, dimension>>&
156 boundarySegment) {
157 DUNE_THROW(NotImplemented,
158 "insertBoundarySegments with Dune::BoundarySegment");
159 }
160
161 void insertInterfaceBoundarySegment(
162 const std::vector<unsigned int>& vertices) {
163 assert(vertices.size() == dimension - 1);
164
165 std::vector<std::size_t> sorted_vertices;
166 for (const auto& v : vertices)
167 sorted_vertices.push_back(vhs_[v]->info().id);
168 std::sort(sorted_vertices.begin(), sorted_vertices.end());
169
170 if (boundarySegments_.find(sorted_vertices) != boundarySegments_.end())
171 DUNE_THROW(GridError, "A boundary segment was inserted twice.");
172
173 interfaceBoundarySegments_.insert(
174 std::make_pair(sorted_vertices, countInterfaceBoundarySegments++));
175 }
176
183 void insertVertex(const WorldVector& pos) {
184 // Insert vertex
185 Vertex_handle vh = tr_.insert(makePoint(pos));
186
187 // Store insertion index
188 vh->info().id = countVertices;
189 vh->info().idWasSet = true;
190
191 // Increase vertex counter
192 countVertices++;
193
194 // Store vertex handle for later use
195 vhs_.push_back(vh);
196 }
197
203 void insertInterface(const std::vector<unsigned int>& vertices,
204 const std::size_t marker = 1) {
205 assert(vertices.size() == dimension);
206
207 std::vector<std::size_t> sorted_vertices;
208 for (const auto& v : vertices)
209 sorted_vertices.push_back(vhs_[v]->info().id);
210 std::sort(sorted_vertices.begin(), sorted_vertices.end());
211 interfaceSegments_.insert(std::make_pair(sorted_vertices, marker));
212 }
213
218 unsigned int insertionIndex(const typename Codim<0>::Entity& entity) const {
219 return entity.impl().hostEntity()->info().insertionIndex;
220 }
221
226 unsigned int insertionIndex(
227 const typename Codim<dimension>::Entity& entity) const {
228 return entity.impl().hostEntity()->info().id;
229 }
230
235 unsigned int insertionIndex(
236 const typename Grid::LeafIntersection& intersection) const {
237 return intersection.impl().boundaryId();
238 }
239
241 const BoundarySegments& boundarySegments() const { return boundarySegments_; }
242
244 const BoundaryIds& boundaryIds() const { return boundaryIds_; }
245
247 void addBoundaryId(std::size_t boundarySegmentIndex, std::size_t boundaryId) {
248 boundaryIds_.insert(std::make_pair(boundarySegmentIndex, boundaryId));
249 }
250
258 std::unique_ptr<Grid> createGrid() {
259 return std::make_unique<Grid>(std::move(tr_), std::move(boundarySegments_),
260 std::move(interfaceBoundarySegments_),
261 std::move(boundaryIds_),
262 std::move(interfaceSegments_));
263 }
264
265 private:
269 template <int dim = dimension>
270 std::enable_if_t<dim == 2, Point> makePoint(const WorldVector& v) const {
271 return Point(v[0], v[1]);
272 }
273
277 template <int dim = dimension>
278 std::enable_if_t<dim == 3, Point> makePoint(const WorldVector& v) const {
279 return Point(v[0], v[1], v[2]);
280 }
281
283 HostGrid tr_;
284 std::vector<Vertex_handle> vhs_;
285 BoundarySegments boundarySegments_, interfaceBoundarySegments_;
286 BoundaryIds boundaryIds_;
287 InterfaceSegments interfaceSegments_;
288 std::size_t countVertices = 0, countElements = 0;
289 std::size_t countBoundarySegments = 0, countInterfaceBoundarySegments = 0;
290};
291
292} // end namespace Dune
293
294#endif
specialization of the implicit GridFactory for MMesh
Definition: implicitgridfactory.hh:35
static const bool supportsBoundaryIds
are boundary ids supported by this factory?
Definition: implicitgridfactory.hh:79
Grid::IdType IdType
type of an id
Definition: implicitgridfactory.hh:57
std::unordered_map< IdType, std::size_t > InterfaceSegments
type of the interface segment set
Definition: implicitgridfactory.hh:64
std::unique_ptr< Grid > createGrid()
finalize grid creation and hand over the grid
Definition: implicitgridfactory.hh:258
void insertVertex(const WorldVector &pos)
Insert a vertex into the macro grid.
Definition: implicitgridfactory.hh:183
void insertInterface(const std::vector< unsigned int > &vertices, const std::size_t marker=1)
insert an interface into the macro grid
Definition: implicitgridfactory.hh:203
void insertElement(const GeometryType &type, const std::vector< unsigned int > &v)
insert an element into the macro grid
Definition: implicitgridfactory.hh:95
MMeshImplicitGridFactory()
Definition: implicitgridfactory.hh:84
std::unordered_map< IdType, std::size_t > BoundarySegments
type of the boundary segment id map
Definition: implicitgridfactory.hh:60
unsigned int insertionIndex(const typename Grid::LeafIntersection &intersection) const
return insertion index of boundary intersection
Definition: implicitgridfactory.hh:235
const BoundaryIds & boundaryIds() const
returns the boundary segment index to boundary id map
Definition: implicitgridfactory.hh:244
std::enable_if_t< d==2, bool > isElement(const std::vector< unsigned int > &v, Element_handle &fh) const
Returns if there is a face with the given vertices in the triangulation2.
Definition: implicitgridfactory.hh:116
Dune::BoundarySegment< dimension, dimensionworld > BoundarySegment
type of a Dune boundary segment
Definition: implicitgridfactory.hh:55
unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
return insertion index of entity
Definition: implicitgridfactory.hh:226
static const int dimension
dimension of the grid
Definition: implicitgridfactory.hh:45
void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert boundary segment
Definition: implicitgridfactory.hh:138
const BoundarySegments & boundarySegments() const
returns the boundary segment to index map
Definition: implicitgridfactory.hh:241
Grid::ctype ctype
type of (scalar) coordinates
Definition: implicitgridfactory.hh:40
FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: implicitgridfactory.hh:52
unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
return index of inserted vertex within the macro grid
Definition: implicitgridfactory.hh:218
Grid::HostGridType HostGrid
type of the hostgrid
Definition: implicitgridfactory.hh:42
static const bool supportPeriodicity
the factory is not able to create periodic meshes
Definition: implicitgridfactory.hh:81
std::enable_if_t< d==3, bool > isElement(const std::vector< unsigned int > &v, Element_handle &fh) const
Returns if there is a cell with the given vertices in the triangulation3.
Definition: implicitgridfactory.hh:128
static const int dimensionworld
dimension of the world
Definition: implicitgridfactory.hh:47
FieldVector< ctype, dimensionworld > WorldVector
type of vector for world coordinates
Definition: implicitgridfactory.hh:50
void addBoundaryId(std::size_t boundarySegmentIndex, std::size_t boundaryId)
add a boundary id
Definition: implicitgridfactory.hh:247
Some common helper methods.
The MMesh class.
Helpers for conversion from CGAL::Point_x to DUNE::FieldVector.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 5, 22:35, 2025)