dune-mmesh (unstable)

gridfactory.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_INTERFACE_GRIDFACTORY_HH
5#define DUNE_MMESH_INTERFACE_GRIDFACTORY_HH
6
7// Dune includes
8#include <dune/grid/common/boundarysegment.hh>
9#include <dune/grid/common/gridfactory.hh>
10
11namespace Dune {
12
19template <class MMeshImp>
20class GridFactory<MMeshInterfaceGrid<MMeshImp> >
21 : public GridFactoryInterface<MMeshInterfaceGrid<MMeshImp> > {
22 typedef GridFactory<MMeshInterfaceGrid<MMeshImp> > This;
23
24 public:
27
29 typedef typename Grid::ctype ctype;
30 typedef typename Grid::HostGridType HostGrid;
31 typedef typename HostGrid::Vertex_handle VertexHandle;
32
34 typedef MMeshImp MMesh;
35
37 static const int dimension = Grid::dimension;
39 static const int dimensionworld = Grid::dimensionworld;
40
42 typedef FieldVector<ctype, dimensionworld> WorldVector;
44 typedef FieldMatrix<ctype, dimensionworld, dimensionworld> WorldMatrix;
45
46 typedef Dune::BoundarySegment<dimension, dimensionworld> BoundarySegment;
47 typedef std::unordered_map<std::vector<std::size_t>, std::size_t,
49 BoundarySegments;
50 typedef std::map<std::vector<std::size_t>, unsigned int> InsertionIndexMap;
51
52 typedef std::map<std::size_t, std::size_t> VertexIdMap;
53
54 template <int codim>
55 struct Codim {
56 typedef typename Grid::template Codim<codim>::Entity Entity;
57 };
58
59 public:
61 static const bool supportsBoundaryIds = true;
63 static const bool supportPeriodicity = false;
64
66 GridFactory(const std::shared_ptr<MMesh> mMesh) : mMesh_(mMesh) {}
67
70 DUNE_THROW(NotImplemented, "GridFactory() for MMeshInterfaceGrid");
71 }
72
78 void insertElement(const GeometryType &type,
79 const std::vector<unsigned int> &vertices) {
80 // mark vertices as interface segment in host mmesh
81 std::vector<std::size_t> ids;
82 for (const auto &v : vertices) ids.push_back(vertexIdMap_.at(v));
83
84 std::sort(ids.begin(), ids.end());
85
86 (mMesh_->interfaceSegments()).insert(std::make_pair(ids, 1));
87
88 insertionIndexMap_.insert({ids, countElements++});
89 };
90
98 const std::vector<unsigned int> &vertices) {
99 std::vector<std::size_t> sorted_vertices;
100 for (const auto &v : vertices)
101 sorted_vertices.push_back(vertexIdMap_.at(v));
102 std::sort(sorted_vertices.begin(), sorted_vertices.end());
103
104 if (boundarySegments_.find(sorted_vertices) != boundarySegments_.end())
105 DUNE_THROW(GridError, "A boundary segment was inserted twice.");
106
107 boundarySegments_.insert(
108 std::make_pair(sorted_vertices, countBoundarySegments++));
109 }
110
111 void insertBoundarySegment(
112 const std::vector<unsigned int> &vertices,
113 const std::shared_ptr<BoundarySegment> &boundarySegment) {
114 DUNE_THROW(NotImplemented,
115 "insertBoundarySegments with Dune::BoundarySegment");
116 }
117
124 void insertVertex(const WorldVector &pos) {
125 // get the vertex handle from the host mmesh
126 VertexHandle vh = mMesh_->getHostGrid().insert(makePoint(pos));
127
128 vertexIdMap_.insert({countVertices, vh->info().id});
129 vh->info().isInterface = true;
130
131 countVertices++;
132 }
133
141 void addVertexHandle(const VertexHandle &vh) {
142 vertexIdMap_.insert({countVertices, vh->info().id});
143 vh->info().isInterface = true;
144
145 countVertices++;
146 }
147
152 unsigned int insertionIndex(const typename Codim<0>::Entity &entity) const {
153 std::vector<std::size_t> ids;
154 for (std::size_t i = 0; i < entity.subEntities(dimension); ++i)
155 ids.push_back(entity.template subEntity<dimension>(i)
156 .impl()
157 .hostEntity()
158 ->info()
159 .id);
160 std::sort(ids.begin(), ids.end());
161 auto it = insertionIndexMap_.find(ids);
162 if (it != insertionIndexMap_.end())
163 return it->second;
164 else
165 return 0; // should not happen
166 }
167
172 unsigned int insertionIndex(
173 const typename Codim<dimension>::Entity &entity) const {
174 std::size_t index = mMesh_->interfaceGrid().leafIndexSet().index(entity);
175 assert(index < std::numeric_limits<unsigned int>::max());
176 return index;
177 }
178
188 DUNE_THROW(
189 InvalidStateException,
190 "The interface grid cannot be created, get the pointer by getGrid()!");
191 return nullptr;
192 }
193
194 auto getGrid() {
195 mMesh_->interfaceGridPtr()->setIds();
196 mMesh_->interfaceGridPtr()->setIndices();
197 mMesh_->interfaceGridPtr()->setBoundarySegments(boundarySegments_);
198
199 // Return pointer to grid
200 return mMesh_->interfaceGridPtr();
201 }
202
203 private:
205 std::shared_ptr<MMesh> mMesh_;
206 BoundarySegments boundarySegments_;
207 std::size_t countBoundarySegments = 0;
208 VertexIdMap vertexIdMap_;
209 InsertionIndexMap insertionIndexMap_;
210 unsigned int countElements = 0;
211 std::size_t countVertices = 0;
212};
213
214} // end namespace Dune
215
216#endif
specialization of the GridFactory for MMesh InterfaceGrid
Definition: gridfactory.hh:21
FieldVector< ctype, dimensionworld > WorldVector
type of vector for world coordinates
Definition: gridfactory.hh:42
unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
return insertion index of vertex entity
Definition: gridfactory.hh:172
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert a boundary segment into the macro grid
Definition: gridfactory.hh:97
Grid::GridPtrType createGrid()
finalize grid creation and hand over the grid
Definition: gridfactory.hh:187
void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
insert an element into the macro grid
Definition: gridfactory.hh:78
void insertVertex(const WorldVector &pos)
Insert a vertex into the macro grid.
Definition: gridfactory.hh:124
unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
return insertion index of entity
Definition: gridfactory.hh:152
FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: gridfactory.hh:44
GridFactory(const std::shared_ptr< MMesh > mMesh)
Definition: gridfactory.hh:66
MMeshImp MMesh
type of corresponding mmesh
Definition: gridfactory.hh:34
MMeshInterfaceGrid< MMeshImp > Grid
type of interface grid
Definition: gridfactory.hh:26
Grid::ctype ctype
type of (scalar) coordinates
Definition: gridfactory.hh:29
void addVertexHandle(const VertexHandle &vh)
Add existing vertex handle from the macro grid to the interface grid.
Definition: gridfactory.hh:141
Provides a DUNE grid interface class for the interface of a MMesh interface grid .
Definition: grid.hh:90
typename MMesh::HostGridType HostGridType
the underlying hostgrid
Definition: grid.hh:115
std::unique_ptr< GridImp > GridPtrType
the unique pointer to the grid
Definition: grid.hh:112
FieldType ctype
The type used to store coordinates, inherited from the MMesh.
Definition: grid.hh:124
Hash a UInt vector.
Definition: common.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 4, 22:38, 2025)