4#ifndef DUNE_MMESH_GRID_IMPLICITGRIDFACTORY_HH
5#define DUNE_MMESH_GRID_IMPLICITGRIDFACTORY_HH
13#include <unordered_map>
14#include <unordered_set>
17#include <dune/geometry/referenceelements.hh>
18#include <dune/grid/common/boundarysegment.hh>
19#include <dune/grid/common/gridfactory.hh>
40 typedef typename Grid::ctype
ctype;
52 typedef FieldMatrix<ctype, dimensionworld, dimensionworld>
WorldMatrix;
57 typedef typename Grid::IdType
IdType;
61 typedef std::unordered_map<std::size_t, std::size_t> BoundaryIds;
68 typedef typename Grid::template Codim<codim>::Entity Entity;
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;
96 const std::vector<unsigned int>& v) {
98 if (
isElement(v, fh)) fh->info().insertionIndex = countElements;
105 const std::vector<unsigned int>& v,
106 const size_t domainMarker) {
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 {
119 return tr_.is_face(vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], fh);
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 {
131 return tr_.is_cell(vhs_[v[0]], vhs_[v[1]], vhs_[v[2]], vhs_[v[3]], fh);
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());
146 if (boundarySegments_.find(sorted_vertices) != boundarySegments_.end())
147 DUNE_THROW(GridError,
"A boundary segment was inserted twice.");
149 boundarySegments_.insert(
150 std::make_pair(sorted_vertices, countBoundarySegments++));
154 const std::vector<unsigned int>& vertices,
155 const std::shared_ptr<Dune::BoundarySegment<dimension, dimension>>&
157 DUNE_THROW(NotImplemented,
158 "insertBoundarySegments with Dune::BoundarySegment");
161 void insertInterfaceBoundarySegment(
162 const std::vector<unsigned int>& vertices) {
163 assert(vertices.size() ==
dimension - 1);
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());
170 if (boundarySegments_.find(sorted_vertices) != boundarySegments_.end())
171 DUNE_THROW(GridError,
"A boundary segment was inserted twice.");
173 interfaceBoundarySegments_.insert(
174 std::make_pair(sorted_vertices, countInterfaceBoundarySegments++));
185 Vertex_handle vh = tr_.insert(makePoint(pos));
188 vh->info().id = countVertices;
189 vh->info().idWasSet =
true;
204 const std::size_t marker = 1) {
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));
219 return entity.impl().hostEntity()->info().insertionIndex;
227 const typename Codim<dimension>::Entity& entity)
const {
228 return entity.impl().hostEntity()->info().id;
236 const typename Grid::LeafIntersection& intersection)
const {
237 return intersection.impl().boundaryId();
247 void addBoundaryId(std::size_t boundarySegmentIndex, std::size_t boundaryId) {
248 boundaryIds_.insert(std::make_pair(boundarySegmentIndex, boundaryId));
259 return std::make_unique<Grid>(std::move(tr_), std::move(boundarySegments_),
260 std::move(interfaceBoundarySegments_),
261 std::move(boundaryIds_),
262 std::move(interfaceSegments_));
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]);
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]);
284 std::vector<Vertex_handle> vhs_;
286 BoundaryIds boundaryIds_;
288 std::size_t countVertices = 0, countElements = 0;
289 std::size_t countBoundarySegments = 0, countInterfaceBoundarySegments = 0;
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.
Helpers for conversion from CGAL::Point_x to DUNE::FieldVector.