DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
xdmf_mesh.h
1// Copyright (C) 2012-2018 Chris N. Richardson and Garth N. Wells
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "xdmf_utils.h"
10#include <array>
11#include <cstdint>
12#include <dolfinx/mesh/MeshTags.h>
13#include <format>
14#include <hdf5.h>
15#include <mpi.h>
16#include <pugixml.hpp>
17#include <span>
18#include <string>
19#include <string_view>
20#include <utility>
21#include <variant>
22#include <vector>
23
24namespace pugi
25{
26class xml_node;
27} // namespace pugi
28
29namespace dolfinx
30{
31
32namespace mesh
33{
34template <std::floating_point T>
35class Geometry;
36template <std::floating_point T>
37class Mesh;
38class Topology;
39} // namespace mesh
40
43{
48template <std::floating_point U>
49void add_mesh(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
50 const mesh::Mesh<U>& mesh, std::string_view path_prefix);
51
62template <std::floating_point U>
63void add_topology_data(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
64 std::string_view path_prefix,
65 const mesh::Topology& topology,
66 const mesh::Geometry<U>& geometry, int cell_dim,
67 std::span<const std::int32_t> entities);
68
70template <std::floating_point U>
71void add_geometry_data(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
72 std::string_view path_prefix,
74
81std::pair<std::variant<std::vector<float>, std::vector<double>>,
82 std::array<std::size_t, 2>>
83read_geometry_data(MPI_Comm comm, hid_t h5_id, const pugi::xml_node& node);
84
91std::pair<std::vector<std::int64_t>, std::array<std::size_t, 2>>
92read_topology_data(MPI_Comm comm, hid_t h5_id, const pugi::xml_node& node);
93
95template <typename T, std::floating_point U>
96void add_meshtags(MPI_Comm comm, const mesh::MeshTags<T>& meshtags,
97 const mesh::Geometry<U>& geometry, pugi::xml_node& xml_node,
98 hid_t h5_id, std::string_view name)
99{
100 spdlog::info("XDMF: add meshtags ({})", name);
101 // Get mesh
102 const int dim = meshtags.dim();
103 std::shared_ptr<const common::IndexMap> entity_map
104 = meshtags.topology()->index_map(dim);
105 if (!entity_map)
106 {
107 throw std::runtime_error("Missing entities. Did you forget to call "
108 "dolfinx::mesh::Topology::create_entities?");
109 }
110 const std::int32_t num_local_entities = entity_map->size_local();
111
112 // Find number of tagged entities in local range
113 auto it = std::ranges::lower_bound(meshtags.indices(), num_local_entities);
114 const int num_active_entities = std::distance(meshtags.indices().begin(), it);
115
116 const std::string path_prefix = std::format("/MeshTags/{}", name);
118 comm, xml_node, h5_id, path_prefix, *meshtags.topology(), geometry, dim,
119 std::span<const std::int32_t>(meshtags.indices().data(),
120 num_active_entities));
121
122 // Add attribute node with values
123 pugi::xml_node attribute_node = xml_node.append_child("Attribute");
124 assert(attribute_node);
125 attribute_node.append_attribute("Name") = std::string(name).c_str();
126 attribute_node.append_attribute("AttributeType") = "Scalar";
127 attribute_node.append_attribute("Center") = "Cell";
128
129 std::int64_t global_num_values = 0;
130 const std::int64_t local_num_values = num_active_entities;
131 MPI_Allreduce(&local_num_values, &global_num_values, 1, MPI_INT64_T, MPI_SUM,
132 comm);
133 const std::int64_t num_local = num_active_entities;
134 std::int64_t offset = 0;
135 MPI_Exscan(&num_local, &offset, 1, MPI_INT64_T, MPI_SUM, comm);
136 const bool use_mpi_io = (dolfinx::MPI::size(comm) > 1);
137 xdmf_utils::add_data_item(
138 attribute_node, h5_id, std::format("{}/Values", path_prefix),
139 std::span<const T>(meshtags.values().data(), num_active_entities), offset,
140 {global_num_values, 1}, "", use_mpi_io);
141}
142} // namespace io::xdmf_mesh
143} // namespace dolfinx
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:37
MeshTags associate values with mesh topology entities.
Definition MeshTags.h:33
std::span< const std::int32_t > indices() const
Definition MeshTags.h:101
std::span< const T > values() const
Values attached to topology entities.
Definition MeshTags.h:104
int dim() const
Return topological dimension of tagged entities.
Definition MeshTags.h:107
std::shared_ptr< const Topology > topology() const
Return topology.
Definition MeshTags.h:110
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
int size(MPI_Comm comm)
Definition MPI.cpp:72
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
Low-level methods for reading XDMF files.
Definition xdmf_mesh.h:43
std::pair< std::vector< std::int64_t >, std::array< std::size_t, 2 > > read_topology_data(MPI_Comm comm, hid_t h5_id, const pugi::xml_node &node)
Read topology (cell connectivity) data.
Definition xdmf_mesh.cpp:302
std::pair< std::variant< std::vector< float >, std::vector< double > >, std::array< std::size_t, 2 > > read_geometry_data(MPI_Comm comm, hid_t h5_id, const pugi::xml_node &node)
Read geometry (coordinate) data.
Definition xdmf_mesh.cpp:261
void add_geometry_data(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, std::string_view path_prefix, const mesh::Geometry< U > &geometry)
Add Geometry xml node.
Definition xdmf_mesh.cpp:166
void add_topology_data(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, std::string_view path_prefix, const mesh::Topology &topology, const mesh::Geometry< U > &geometry, int cell_dim, std::span< const std::int32_t > entities)
Definition xdmf_mesh.cpp:26
void add_mesh(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, const mesh::Mesh< U > &mesh, std::string_view path_prefix)
Definition xdmf_mesh.cpp:221
void add_meshtags(MPI_Comm comm, const mesh::MeshTags< T > &meshtags, const mesh::Geometry< U > &geometry, pugi::xml_node &xml_node, hid_t h5_id, std::string_view name)
Add mesh tags to XDMF file.
Definition xdmf_mesh.h:96
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
Top-level namespace.
Definition defines.h:12