dune-mmesh (1.4)

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/gridfactory.hh>
19#include <dune/grid/common/boundarysegment.hh>
20
21// MMesh includes
25
26namespace Dune
27{
28
35 template< class Grid >
37 : public GridFactoryInterface< Grid >
38 {
40
41 public:
43 typedef typename Grid::ctype ctype;
45 typedef typename Grid::HostGridType HostGrid;
46
48 static const int dimension = Grid::dimension;
50 static const int dimensionworld = Grid::dimensionworld;
51
53 typedef FieldVector< ctype, dimensionworld > WorldVector;
55 typedef FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix;
56
58 typedef Dune::BoundarySegment< dimension, dimensionworld > BoundarySegment;
60 typedef typename Grid::IdType IdType;
61
63 typedef std::unordered_map< IdType, std::size_t > BoundarySegments;
64 typedef std::unordered_map< std::size_t, std::size_t > BoundaryIds;
65
67 typedef std::unordered_map< IdType, std::size_t > InterfaceSegments;
68
69 template< int codim >
70 struct Codim
71 {
72 typedef typename Grid::template Codim< codim >::Entity Entity;
73 };
74
75 private:
76 typedef typename HostGrid::Point Point;
77 typedef typename HostGrid::Vertex_handle Vertex_handle;
78 typedef typename Grid::template HostGridEntity<0> Element_handle;
79 typedef typename Grid::template HostGridEntity<1> Face_handle;
80
81 public:
83 static const bool supportsBoundaryIds = true;
85 static const bool supportPeriodicity = false;
86
89 {}
90
99 void insertElement ( const GeometryType &type,
100 const std::vector< unsigned int > &v )
101 {
102 Element_handle fh;
103 if( isElement( v, fh ) )
104 fh->info().insertionIndex = countElements;
105
106 // Increase element count in each case
107 countElements++;
108 };
109
110 void insertElement ( const GeometryType &type,
111 const std::vector< unsigned int > &v,
112 const size_t domainMarker )
113 {
114 insertElement( type, v );
115 }
116
122 template< int d = dimension >
123 std::enable_if_t< d == 2, bool >
124 isElement( const std::vector< unsigned int >& v, Element_handle& fh ) const
125 {
126 assert( v.size() == dimension+1 );
127 return tr_.is_face( vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], fh );
128 }
129
135 template< int d = dimension >
136 std::enable_if_t< d == 3, bool >
137 isElement( const std::vector< unsigned int >& v, Element_handle& fh ) const
138 {
139 assert( v.size() == dimension+1 );
140 return tr_.is_cell( vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], vhs_[v[3]], fh );
141 }
142
147 void insertBoundarySegment(const std::vector<unsigned int>& vertices)
148 {
149 assert( vertices.size() == dimension );
150
151 std::vector< std::size_t > sorted_vertices;
152 for ( const auto& v : vertices )
153 sorted_vertices.push_back( vhs_[v]->info().id );
154 std::sort(sorted_vertices.begin(), sorted_vertices.end());
155
156 if( boundarySegments_.find( sorted_vertices ) != boundarySegments_.end() )
157 DUNE_THROW( GridError, "A boundary segment was inserted twice." );
158
159 boundarySegments_.insert( std::make_pair( sorted_vertices, countBoundarySegments++ ) );
160 }
161
162 void insertBoundarySegment(const std::vector<unsigned int>& vertices,
163 const std::shared_ptr<Dune::BoundarySegment<dimension,dimension>>& boundarySegment)
164 {
165 DUNE_THROW( NotImplemented, "insertBoundarySegments with Dune::BoundarySegment" );
166 }
167
168 void insertInterfaceBoundarySegment ( const std::vector< unsigned int >& vertices )
169 {
170 assert( vertices.size() == dimension-1 );
171
172 std::vector< std::size_t > sorted_vertices;
173 for ( const auto& v : vertices )
174 sorted_vertices.push_back( vhs_[v]->info().id );
175 std::sort(sorted_vertices.begin(), sorted_vertices.end());
176
177 if( boundarySegments_.find( sorted_vertices ) != boundarySegments_.end() )
178 DUNE_THROW( GridError, "A boundary segment was inserted twice." );
179
180 interfaceBoundarySegments_.insert( std::make_pair( sorted_vertices, countInterfaceBoundarySegments++ ) );
181 }
182
189 void insertVertex ( const WorldVector &pos )
190 {
191 // Insert vertex
192 Vertex_handle vh = tr_.insert( makePoint( pos ) );
193
194 // Store insertion index
195 vh->info().id = countVertices;
196 vh->info().idWasSet = true;
197
198 // Increase vertex counter
199 countVertices++;
200
201 // Store vertex handle for later use
202 vhs_.push_back( vh );
203 }
204
210 void insertInterface ( const std::vector< unsigned int > &vertices, const std::size_t marker = 1 )
211 {
212 assert( vertices.size() == dimension );
213
214 std::vector< std::size_t > sorted_vertices;
215 for( const auto& v : vertices )
216 sorted_vertices.push_back( vhs_[v]->info().id );
217 std::sort(sorted_vertices.begin(), sorted_vertices.end());
218 interfaceSegments_.insert( std::make_pair( sorted_vertices, marker ) );
219 }
220
225 unsigned int insertionIndex ( const typename Codim<0>::Entity &entity ) const
226 {
227 return entity.impl().hostEntity()->info().insertionIndex;
228 }
229
234 unsigned int insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
235 {
236 return entity.impl().hostEntity()->info().id;
237 }
238
243 unsigned int insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
244 {
245 return intersection.impl().boundaryId();
246 }
247
250 {
251 return boundarySegments_;
252 }
253
255 const BoundaryIds& boundaryIds() const
256 {
257 return boundaryIds_;
258 }
259
261 void addBoundaryId( std::size_t boundarySegmentIndex, std::size_t boundaryId )
262 {
263 boundaryIds_.insert( std::make_pair( boundarySegmentIndex, boundaryId ) );
264 }
265
273 std::unique_ptr<Grid> createGrid ()
274 {
275 return std::make_unique<Grid> (
276 std::move(tr_),
277 std::move(boundarySegments_),
278 std::move(interfaceBoundarySegments_),
279 std::move(boundaryIds_),
280 std::move(interfaceSegments_)
281 );
282 }
283
284 private:
288 template< int dim = dimension >
289 std::enable_if_t< dim == 2, Point >
290 makePoint( const WorldVector& v ) const
291 {
292 return Point ( v[ 0 ], v[ 1 ] );
293 }
294
298 template< int dim = dimension >
299 std::enable_if_t< dim == 3, Point >
300 makePoint( const WorldVector& v ) const
301 {
302 return Point ( v[ 0 ], v[ 1 ], v[ 2 ] );
303 }
304
306 HostGrid tr_;
307 std::vector< Vertex_handle > vhs_;
308 BoundarySegments boundarySegments_, interfaceBoundarySegments_;
309 BoundaryIds boundaryIds_;
310 InterfaceSegments interfaceSegments_;
311 std::size_t countVertices = 0, countElements = 0;
312 std::size_t countBoundarySegments = 0, countInterfaceBoundarySegments = 0;
313 };
314
315} // end namespace Dune
316
317#endif
specialization of the implicit GridFactory for MMesh
Definition: implicitgridfactory.hh:38
static const bool supportsBoundaryIds
are boundary ids supported by this factory?
Definition: implicitgridfactory.hh:83
Grid::IdType IdType
type of an id
Definition: implicitgridfactory.hh:60
std::unique_ptr< Grid > createGrid()
finalize grid creation and hand over the grid
Definition: implicitgridfactory.hh:273
void insertVertex(const WorldVector &pos)
Insert a vertex into the macro grid.
Definition: implicitgridfactory.hh:189
void insertInterface(const std::vector< unsigned int > &vertices, const std::size_t marker=1)
insert an interface into the macro grid
Definition: implicitgridfactory.hh:210
void insertElement(const GeometryType &type, const std::vector< unsigned int > &v)
insert an element into the macro grid
Definition: implicitgridfactory.hh:99
MMeshImplicitGridFactory()
Definition: implicitgridfactory.hh:88
std::unordered_map< IdType, std::size_t > BoundarySegments
type of the boundary segment id map
Definition: implicitgridfactory.hh:63
unsigned int insertionIndex(const typename Grid::LeafIntersection &intersection) const
return insertion index of boundary intersection
Definition: implicitgridfactory.hh:243
const BoundaryIds & boundaryIds() const
returns the boundary segment index to boundary id map
Definition: implicitgridfactory.hh:255
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 triangulation 2.
Definition: implicitgridfactory.hh:124
unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
return insertion index of entity
Definition: implicitgridfactory.hh:234
static const int dimension
dimension of the grid
Definition: implicitgridfactory.hh:48
std::unordered_map< IdType, std::size_t > InterfaceSegments
type of the interface segment set
Definition: implicitgridfactory.hh:67
void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert boundary segment
Definition: implicitgridfactory.hh:147
const BoundarySegments & boundarySegments() const
returns the boundary segment to index map
Definition: implicitgridfactory.hh:249
Grid::ctype ctype
type of (scalar) coordinates
Definition: implicitgridfactory.hh:43
FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: implicitgridfactory.hh:55
unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
return index of inserted vertex within the macro grid
Definition: implicitgridfactory.hh:225
Grid::HostGridType HostGrid
type of the hostgrid
Definition: implicitgridfactory.hh:45
static const bool supportPeriodicity
the factory is not able to create periodic meshes
Definition: implicitgridfactory.hh:85
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 triangulation 3.
Definition: implicitgridfactory.hh:137
static const int dimensionworld
dimension of the world
Definition: implicitgridfactory.hh:50
Dune::BoundarySegment< dimension, dimensionworld > BoundarySegment
type of a Dune boundary segment
Definition: implicitgridfactory.hh:58
FieldVector< ctype, dimensionworld > WorldVector
type of vector for world coordinates
Definition: implicitgridfactory.hh:53
void addBoundaryId(std::size_t boundarySegmentIndex, std::size_t boundaryId)
add a boundary id
Definition: implicitgridfactory.hh:261
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 (Apr 6, 22:49, 2025)