GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
mfem.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
9#ifndef GRIDFORMAT_TRAITS_MFEM_HPP_
10#define GRIDFORMAT_TRAITS_MFEM_HPP_
11
12#include <cassert>
13#include <ranges>
14#include <vector>
15#include <array>
16#include <type_traits>
17
18#include <mfem/mesh/mesh.hpp>
19
20#include <gridformat/common/exceptions.hpp>
21#include <gridformat/common/filtered_range.hpp>
22
23#include <gridformat/grid/cell_type.hpp>
24#include <gridformat/grid/traits.hpp>
25
26namespace GridFormat::MFEM {
27
28#ifndef DOXYGEN
29namespace Detail {
30 CellType cell_type(mfem::Element::Type ct) {
31 switch (ct) {
32 case mfem::Element::Type::POINT: return GridFormat::CellType::vertex;
33 case mfem::Element::Type::SEGMENT: return GridFormat::CellType::segment;
34 case mfem::Element::Type::TRIANGLE: return GridFormat::CellType::triangle;
35 case mfem::Element::Type::QUADRILATERAL: return GridFormat::CellType::quadrilateral;
36 case mfem::Element::Type::TETRAHEDRON: return GridFormat::CellType::tetrahedron;
37 case mfem::Element::Type::WEDGE: break;
38 case mfem::Element::Type::PYRAMID: break;
39 case mfem::Element::Type::HEXAHEDRON: return GridFormat::CellType::hexahedron;
40 }
41 throw NotImplemented("Support for given mfem cell type");
42 }
43} // namespace Detail
44#endif // DOXYGEN
45
46using Point = int;
47using Cell = int;
48
49} // namespace GridFormat::MFEM
50
51// Specializations of the traits required for the `UnstructuredGrid` concept for mfem::Mesh
52namespace GridFormat::Traits {
53
54template<>
55struct Points<mfem::Mesh> {
56 static std::ranges::range auto get(const mfem::Mesh& mesh) {
57 return std::views::iota(0, mesh.GetNV());
58 }
59};
60
61template<>
62struct Cells<mfem::Mesh> {
63 static std::ranges::range auto get(const mfem::Mesh& mesh) {
64 return std::views::iota(0, mesh.GetNE());
65 }
66};
67
68template<>
69struct CellType<mfem::Mesh, GridFormat::MFEM::Cell> {
70 static GridFormat::CellType get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
71 return GridFormat::MFEM::Detail::cell_type(mesh.GetElement(cell)->GetType());
72 }
73};
74
75template<>
76struct CellPoints<mfem::Mesh, GridFormat::MFEM::Cell> {
77 static std::ranges::range decltype(auto) get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
78 const auto element = mesh.GetElement(cell);
79 auto begin = element->GetVertices();
80 return std::ranges::subrange(begin, begin + element->GetNVertices());
81 }
82};
83
84template<>
85struct PointId<mfem::Mesh, GridFormat::MFEM::Point> {
86 static auto get(const mfem::Mesh&, const GridFormat::MFEM::Point& point) {
87 return point;
88 }
89};
90
91template<>
92struct PointCoordinates<mfem::Mesh, GridFormat::MFEM::Point> {
93 static std::ranges::range decltype(auto) get(const mfem::Mesh& mesh, const GridFormat::MFEM::Point& point) {
94 std::array<double, 3> coords = {};
95 auto begin = mesh.GetVertex(point);
96 std::copy(begin, begin + mesh.SpaceDimension(), coords.begin());
97 return coords;
98 }
99};
100
101template<>
102struct NumberOfPoints<mfem::Mesh> {
103 static std::integral auto get(const mfem::Mesh& mesh) {
104 return mesh.GetNV();
105 }
106};
107
108template<>
109struct NumberOfCells<mfem::Mesh> {
110 static std::integral auto get(const mfem::Mesh& mesh) {
111 return mesh.GetNE();
112 }
113};
114
115template<>
116struct NumberOfCellPoints<mfem::Mesh, GridFormat::MFEM::Cell> {
117 static std::integral auto get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
118 return mesh.GetElement(cell)->GetNVertices();
119 }
120};
121
122} // namespace GridFormat::Traits
123
124#endif // GRIDFORMAT_TRAITS_MFEM_HPP_