GridFormat 0.5.0
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
22#include <gridformat/grid/cell_type.hpp>
23#include <gridformat/grid/traits.hpp>
24
25namespace GridFormat::MFEM {
26
27#ifndef DOXYGEN
28namespace Detail {
29 CellType cell_type(mfem::Element::Type ct) {
30 switch (ct) {
31 case mfem::Element::Type::POINT: return GridFormat::CellType::vertex;
32 case mfem::Element::Type::SEGMENT: return GridFormat::CellType::segment;
33 case mfem::Element::Type::TRIANGLE: return GridFormat::CellType::triangle;
34 case mfem::Element::Type::QUADRILATERAL: return GridFormat::CellType::quadrilateral;
35 case mfem::Element::Type::TETRAHEDRON: return GridFormat::CellType::tetrahedron;
36 case mfem::Element::Type::WEDGE: break;
37 case mfem::Element::Type::PYRAMID: break;
38 case mfem::Element::Type::HEXAHEDRON: return GridFormat::CellType::hexahedron;
39 }
40 throw NotImplemented("Support for given mfem cell type");
41 }
42} // namespace Detail
43#endif // DOXYGEN
44
45using Point = int;
46using Cell = int;
47
48} // namespace GridFormat::MFEM
49
50// Specializations of the traits required for the `UnstructuredGrid` concept for mfem::Mesh
51namespace GridFormat::Traits {
52
53template<>
54struct Points<mfem::Mesh> {
55 static std::ranges::range auto get(const mfem::Mesh& mesh) {
56 return std::views::iota(0, mesh.GetNV());
57 }
58};
59
60template<>
61struct Cells<mfem::Mesh> {
62 static std::ranges::range auto get(const mfem::Mesh& mesh) {
63 return std::views::iota(0, mesh.GetNE());
64 }
65};
66
67template<>
68struct CellType<mfem::Mesh, GridFormat::MFEM::Cell> {
69 static GridFormat::CellType get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
70 return GridFormat::MFEM::Detail::cell_type(mesh.GetElement(cell)->GetType());
71 }
72};
73
74template<>
75struct CellPoints<mfem::Mesh, GridFormat::MFEM::Cell> {
76 static std::ranges::range decltype(auto) get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
77 const auto element = mesh.GetElement(cell);
78 auto begin = element->GetVertices();
79 return std::ranges::subrange(begin, begin + element->GetNVertices());
80 }
81};
82
83template<>
84struct PointId<mfem::Mesh, GridFormat::MFEM::Point> {
85 static auto get(const mfem::Mesh&, const GridFormat::MFEM::Point& point) {
86 return point;
87 }
88};
89
90template<>
91struct PointCoordinates<mfem::Mesh, GridFormat::MFEM::Point> {
92 static std::ranges::range decltype(auto) get(const mfem::Mesh& mesh, const GridFormat::MFEM::Point& point) {
93 std::array<double, 3> coords = {};
94 auto begin = mesh.GetVertex(point);
95 std::copy(begin, begin + mesh.SpaceDimension(), coords.begin());
96 return coords;
97 }
98};
99
100template<>
101struct NumberOfPoints<mfem::Mesh> {
102 static std::integral auto get(const mfem::Mesh& mesh) {
103 return mesh.GetNV();
104 }
105};
106
107template<>
108struct NumberOfCells<mfem::Mesh> {
109 static std::integral auto get(const mfem::Mesh& mesh) {
110 return mesh.GetNE();
111 }
112};
113
114template<>
115struct NumberOfCellPoints<mfem::Mesh, GridFormat::MFEM::Cell> {
116 static std::integral auto get(const mfem::Mesh& mesh, const GridFormat::MFEM::Cell& cell) {
117 return mesh.GetElement(cell)->GetNVertices();
118 }
119};
120
121} // namespace GridFormat::Traits
122
123#endif // GRIDFORMAT_TRAITS_MFEM_HPP_