Dune Core Modules (unstable)

continuousgridcreator.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_GRID_IO_FILE_GMSH_GRIDCREATORS_CONTINUOUSGRIDCREATOR_HH
5#define DUNE_GRID_IO_FILE_GMSH_GRIDCREATORS_CONTINUOUSGRIDCREATOR_HH
6
7#include <cassert>
8#include <cstdint>
9#include <limits>
10#include <vector>
11
13#include <dune/common/hybridutilities.hh>
15
16#include <dune/grid/io/file/gmsh/types.hh>
17#include <dune/grid/io/file/gmsh/gridcreatorinterface.hh>
19
20namespace Dune::Impl::Gmsh
21{
22 // Create a grid where the input points and connectivity is already
23 // connected correctly.
24 template <class Grid>
25 struct ContinuousGridCreator
26 : public GridCreatorInterface<Grid, ContinuousGridCreator<Grid> >
27 {
28 using Super = GridCreatorInterface<Grid, ContinuousGridCreator<Grid> >;
29 using GlobalCoordinate = typename Super::GlobalCoordinate;
30 using Nodes = std::vector<GlobalCoordinate>;
31
32
33 public:
34 using Super::Super;
35 using Super::factory;
36
37 template <class NodeAttributes>
38 void insertVerticesImpl (std::size_t numNodes,
39 std::pair<std::size_t,std::size_t> nodeTagRange,
40 std::vector<NodeAttributes> const& entityBlocks)
41 {
42 vertexMap_.resize(nodeTagRange.second - nodeTagRange.first + 1);
43 vertexShift_ = nodeTagRange.first;
44 nodes_.resize(numNodes);
45 GlobalCoordinate p;
46 size_t vertexIndex = 0;
47
48 for (auto const& entityBlock : entityBlocks) {
49 for (auto const& node : entityBlock.nodes) {
50 for (std::size_t j = 0; j < p.size(); ++j)
51 p[j] = node.xyz[j];
52 nodes_[vertexIndex] = p;
53 vertexMap_[node.tag - vertexShift_] = vertexIndex++;
54 }
55 }
56 }
57
58 template <class ElementAttributes, class BoundaryEntities>
59 void insertElementsImpl (std::size_t /*numElements*/,
60 std::pair<std::size_t,std::size_t> /*elementTagRange*/,
61 std::vector<ElementAttributes> const& entityBlocks,
62 BoundaryEntities const& /*boundaryEntities*/)
63 {
64 std::vector<unsigned int> connectivity;
65 std::size_t cornerIndex = 0;
66 std::vector<std::int64_t> cornerVertices(nodes_.size(), -1);
67
68 for (auto const& entityBlock : entityBlocks) {
69 if (entityBlock.entityDim < Grid::dimension-1)
70 continue;
71
72 auto type = gmshNumberToGeometryType(entityBlock.elementType);
73 CellType cell{type};
74
75 if (entityBlock.entityDim == Grid::dimension) { //element
76 auto refElem = referenceElement<double,Grid::dimension>(cell.type());
77 connectivity.resize(refElem.size(Grid::dimension));
78
79 for (auto const& element : entityBlock.elements) {
80 GMSH4_ASSERT(element.nodes.size() >= connectivity.size());
81 for (std::size_t j = 0; j < connectivity.size(); ++j) {
82 auto index = vertexMap_[element.nodes[j] - vertexShift_];
83 auto& vertex = cornerVertices.at(index);
84 if (vertex < 0) {
85 factory().insertVertex(nodes_.at(index));
86 vertex = cornerIndex++;
87 }
88 connectivity[cell.gmshVertexToDuneVertex(j)] = vertex;
89 }
90
91 factory().insertElement(cell.type(), connectivity);
92 }
93 }
94 }
95 nodes_.clear();
96 }
97
98 private:
100 Nodes nodes_;
101
102 std::vector<std::size_t> vertexMap_;
103 std::size_t vertexShift_ = 0;
104 };
105
106 // deduction guides
107 template <class Grid>
108 ContinuousGridCreator(GridFactory<Grid>&)
109 -> ContinuousGridCreator<Grid>;
110
111} // end namespace Dune::Impl::Gmsh
112
113#endif
static constexpr int dimension
The dimension of the grid.
Definition: grid.hh:387
Provide a generic factory class for unstructured grids.
A few common exception classes.
Macro for wrapping error checks and throwing exceptions.
#define GMSH4_ASSERT(cond)
check if condition cond holds; otherwise, throw a Gmsh4Error.
Definition: errors.hh:34
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Nov 3, 23:36, 2025)