OISEAU
A modern DGTD framework
Loading...
Searching...
No Matches
gmsh_file.hpp
1// Copyright (C) 2025 Tiago V. L. Amorim (@tiagovla)
2//
3// This file is part of oiseau (https://github.com/tiagovla/oiseau)
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <istream>
12#include <string>
13#include <utility>
14#include <vector>
15
16namespace oiseau::io {
17
18struct MeshFormatSection {
19 double version{};
20 int is_binary{};
21 std::size_t data_size{};
22
23 MeshFormatSection() = default;
24 MeshFormatSection(double version, int is_binary, std::size_t data_size)
25 : version(version), is_binary(is_binary), data_size(data_size) {}
26};
27
28struct EntityEntry {
29 std::size_t tag;
30 std::vector<double> boundary_coords;
31 std::vector<int> physical_tags;
32 std::vector<int> bounding_tags;
33
34 EntityEntry(std::size_t tag, std::vector<double>&& boundary_coords,
35 std::vector<int>&& physical_tags, std::vector<int>&& bounding_tags)
36 : tag(tag),
37 boundary_coords(std::move(boundary_coords)),
38 physical_tags(std::move(physical_tags)),
39 bounding_tags(std::move(bounding_tags)) {}
40 EntityEntry(std::size_t tag, std::vector<double>&& boundary_coords,
41 std::vector<int>&& physical_tags)
42 : tag(tag),
43 boundary_coords(std::move(boundary_coords)),
44 physical_tags(std::move(physical_tags)) {}
45};
46
47struct EntitiesSection {
48 std::array<std::vector<EntityEntry>, 4> blocks{};
49
50 EntitiesSection() = default;
51 explicit EntitiesSection(std::array<std::vector<EntityEntry>, 4>&& blocks) : blocks(blocks) {}
52};
53
54struct PhysicalNamesSection {
55 int num_physical_names{};
56 std::vector<int> dimensions{};
57 std::vector<int> physical_tags{};
58 std::vector<std::string> names{};
59
60 PhysicalNamesSection() = default;
61 PhysicalNamesSection(int num_physical_names, std::vector<int>&& dimensions,
62 std::vector<int>&& physical_tags, std::vector<std::string>&& names)
63 : num_physical_names(num_physical_names),
64 dimensions(std::move(dimensions)),
65 physical_tags(std::move(physical_tags)),
66 names(std::move(names)) {};
67};
68
69struct NodesBlock {
70 int entity_dim;
71 int entity_tag;
72 int parametric;
73 std::size_t num_nodes_in_block;
74 std::vector<std::size_t> node_tags;
75 std::vector<double> node_coords;
76
77 NodesBlock(int entity_dim, int entity_tag, int parametric, std::size_t num_nodes_in_block,
78 std::vector<std::size_t>&& node_tags, std::vector<double>&& node_coords)
79 : entity_dim(entity_dim),
80 entity_tag(entity_tag),
81 parametric(parametric),
82 num_nodes_in_block(num_nodes_in_block),
83 node_tags(std::move(node_tags)),
84 node_coords(std::move(node_coords)) {}
85};
86
87struct NodesSection {
88 std::size_t num_entity_blocks{};
89 std::size_t num_nodes{};
90 std::size_t min_node_tag{};
91 std::size_t max_node_tag{};
92 std::vector<NodesBlock> blocks{};
93
94 NodesSection() = default;
95 NodesSection(std::size_t num_entity_blocks, std::size_t num_nodes, std::size_t min_node_tag,
96 std::size_t max_node_tag, std::vector<NodesBlock>&& blocks)
97 : num_entity_blocks(num_entity_blocks),
98 num_nodes(num_nodes),
99 min_node_tag(min_node_tag),
100 max_node_tag(max_node_tag),
101 blocks(std::move(blocks)) {}
102};
103
104struct ElementBlock {
105 int entity_dim;
106 int entity_tag;
107 int element_type;
108 std::size_t num_elements_in_block;
109 std::vector<std::size_t> data;
110
111 ElementBlock(int entity_dim, int entity_tag, int element_type, std::size_t num_elements_in_block,
112 std::vector<std::size_t>&& data)
113 : entity_dim(entity_dim),
114 entity_tag(entity_tag),
115 element_type(element_type),
116 num_elements_in_block(num_elements_in_block),
117 data(std::move(data)) {}
118};
119
120struct ElementSection {
121 std::size_t num_element_blocks{};
122 std::size_t num_elements{};
123 std::size_t min_element_tag{};
124 std::size_t max_element_tag{};
125 std::vector<ElementBlock> blocks{};
126
127 ElementSection() = default;
128 ElementSection(std::size_t num_element_blocks, std::size_t num_elements,
129 std::size_t min_element_tag, std::size_t max_element_tag,
130 std::vector<ElementBlock>&& blocks)
131 : num_element_blocks(num_element_blocks),
132 num_elements(num_elements),
133 min_element_tag(min_element_tag),
134 max_element_tag(max_element_tag),
135 blocks(std::move(blocks)) {}
136};
137
138class GMSHFile {
139 public:
140 GMSHFile(MeshFormatSection&& mesh_format_section, PhysicalNamesSection&& physical_names_section,
141 EntitiesSection&& entities_section, NodesSection&& nodes_section,
142 ElementSection&& elements_section)
143 : mesh_format_section(std::move(mesh_format_section)),
144 physical_names_section(std::move(physical_names_section)),
145 entities_section(std::move(entities_section)),
146 nodes_section(std::move(nodes_section)),
147 elements_section(std::move(elements_section)) {}
148
149 explicit GMSHFile(std::istream& f_handler) { read(f_handler); }
150
151 MeshFormatSection mesh_format_section;
152 PhysicalNamesSection physical_names_section;
153 EntitiesSection entities_section;
154 NodesSection nodes_section;
155 ElementSection elements_section;
156
157 private:
158 void read(std::istream& f_handler);
159};
160
161namespace detail {
162MeshFormatSection mesh_format_handler(std::istream& f_handler);
163PhysicalNamesSection physical_names_handler(std::istream& f_handler);
164EntitiesSection entities_handler(std::istream& f_handler, bool is_binary);
165NodesSection nodes_handler(std::istream& f_handler, bool is_binary);
166ElementSection elements_handler(std::istream& f_handler, bool is_binary);
167
168} // namespace detail
169} // namespace oiseau::io
Definition gmsh_file.hpp:120
Definition gmsh_file.hpp:47
Definition gmsh_file.hpp:18
Definition gmsh_file.hpp:87
Definition gmsh_file.hpp:54