| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | #include "oiseau/io/gmsh.hpp" | ||
| 8 | |||
| 9 | #include <cstddef> | ||
| 10 | #include <filesystem> | ||
| 11 | #include <fstream> | ||
| 12 | #include <istream> | ||
| 13 | #include <sstream> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | #include <unordered_map> | ||
| 17 | #include <utility> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | #include "oiseau/io/gmsh_file.hpp" | ||
| 21 | #include "oiseau/mesh/cell.hpp" | ||
| 22 | #include "oiseau/mesh/geometry.hpp" | ||
| 23 | #include "oiseau/mesh/mesh.hpp" | ||
| 24 | #include "oiseau/mesh/topology.hpp" | ||
| 25 | |||
| 26 | namespace oiseau::io { | ||
| 27 | |||
| 28 | namespace detail { | ||
| 29 | 11 | oiseau::mesh::CellType gmsh_celltype_to_oiseau_celltype(const std::size_t s) { | |
| 30 | static const std::unordered_map<std::size_t, oiseau::mesh::CellKind> gmsh_to_kind = { | ||
| 31 | {15, oiseau::mesh::CellKind::Point}, {1, oiseau::mesh::CellKind::Interval}, | ||
| 32 | {2, oiseau::mesh::CellKind::Triangle}, {3, oiseau::mesh::CellKind::Quadrilateral}, | ||
| 33 | {4, oiseau::mesh::CellKind::Tetrahedron}, {5, oiseau::mesh::CellKind::Hexahedron}, | ||
| 34 |
4/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
17 | }; |
| 35 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | auto it = gmsh_to_kind.find(s); |
| 36 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 10 times.
|
11 | if (it == gmsh_to_kind.end()) { |
| 37 |
3/6✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
1 | throw std::runtime_error("Unknown Gmsh cell type: " + std::to_string(s)); |
| 38 | } | ||
| 39 | |||
| 40 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
20 | return oiseau::mesh::get_cell_type(it->second); |
| 41 | } | ||
| 42 | } // namespace detail | ||
| 43 | |||
| 44 | 2 | oiseau::mesh::Mesh gmsh_read_from_string(const std::string &content) { | |
| 45 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | std::istringstream stream(content); |
| 46 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | return gmsh_read_from_stream(stream); |
| 47 | 2 | } | |
| 48 | |||
| 49 | ✗ | oiseau::mesh::Mesh gmsh_read_from_path(const std::filesystem::path &path) { | |
| 50 | ✗ | std::ifstream f_handler(path); | |
| 51 | ✗ | return gmsh_read_from_stream(f_handler); | |
| 52 | ✗ | } | |
| 53 | |||
| 54 | 2 | oiseau::mesh::Mesh gmsh_read_from_stream(std::istream &f_handler) { | |
| 55 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | GMSHFile file = GMSHFile(f_handler); |
| 56 | 2 | std::vector<double> x; | |
| 57 | 2 | std::vector<std::vector<std::size_t>> conn; | |
| 58 | 2 | std::vector<oiseau::mesh::CellType> cell_types; | |
| 59 | |||
| 60 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | x.reserve(file.nodes_section.num_nodes * 3); |
| 61 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
|
5 | for (auto &block : file.nodes_section.blocks) { |
| 62 |
1/2✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | x.insert(x.end(), block.node_coords.begin(), block.node_coords.end()); |
| 63 | } | ||
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | cell_types.reserve(file.elements_section.num_elements); |
| 66 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | conn.reserve(file.elements_section.num_elements); |
| 67 | |||
| 68 |
2/2✓ Branch 5 taken 3 times.
✓ Branch 6 taken 2 times.
|
5 | for (const auto &block : file.elements_section.blocks) { |
| 69 | 3 | std::size_t elem_size = block.data.size() / block.num_elements_in_block; | |
| 70 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
|
11 | for (std::size_t i = 0; i < block.num_elements_in_block; ++i) { |
| 71 | 8 | std::vector<std::size_t> tmp; | |
| 72 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | tmp.reserve(elem_size - 1); |
| 73 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 8 times.
|
44 | for (std::size_t j = 1; j < elem_size; ++j) { |
| 74 |
1/2✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
36 | tmp.emplace_back(block.data[i * elem_size + j] - 1); |
| 75 | } | ||
| 76 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | cell_types.emplace_back(detail::gmsh_celltype_to_oiseau_celltype(block.element_type)); |
| 77 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | conn.emplace_back(std::move(tmp)); |
| 78 | 8 | } | |
| 79 | } | ||
| 80 | |||
| 81 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | oiseau::mesh::Geometry geometry = oiseau::mesh::Geometry(std::move(x), 3); |
| 82 |
1/2✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | oiseau::mesh::Topology topology = oiseau::mesh::Topology(std::move(conn), std::move(cell_types)); |
| 83 | 2 | oiseau::mesh::Mesh mesh(std::move(topology), std::move(geometry)); | |
| 84 | 2 | return mesh; | |
| 85 | 2 | }; | |
| 86 | |||
| 87 | ✗ | void gmsh_write(const std::string &filename, const oiseau::mesh::Mesh &mesh) { | |
| 88 | ✗ | throw std::logic_error("gmsh_write not implemented yet."); | |
| 89 | }; | ||
| 90 | |||
| 91 | } // namespace oiseau::io | ||
| 92 |