Loading...
Searching...
No Matches
xdmf_mesh.h
// Copyright (C) 2012-2018 Chris N. Richardson and Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "xdmf_utils.h"
#include <array>
#include <cstdint>
#include <dolfinx/mesh/MeshTags.h>
#include <hdf5.h>
#include <mpi.h>
#include <pugixml.hpp>
#include <span>
#include <string>
#include <utility>
#include <variant>
#include <vector>
namespace pugi
{
class xml_node;
} // namespace pugi
namespace dolfinx
{
namespace mesh
{
template <std::floating_point T>
class Geometry;
template <std::floating_point T>
class Mesh;
class Topology;
} // namespace mesh
namespace io::xdmf_mesh
{
template <std::floating_point U>
void add_mesh(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
const mesh::Mesh<U>& mesh, const std::string& path_prefix);
template <std::floating_point U>
void add_topology_data(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
const std::string& path_prefix,
const mesh::Topology& topology,
const mesh::Geometry<U>& geometry, int cell_dim,
std::span<const std::int32_t> entities);
template <std::floating_point U>
void add_geometry_data(MPI_Comm comm, pugi::xml_node& xml_node, hid_t h5_id,
const std::string& path_prefix,
const mesh::Geometry<U>& geometry);
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);
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);
template <typename T, std::floating_point U>
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, const std::string& name)
{
spdlog::info("XDMF: add meshtags ({})", name.c_str());
// Get mesh
const int dim = meshtags.dim();
std::shared_ptr<const common::IndexMap> entity_map
= meshtags.topology()->index_map(dim);
if (!entity_map)
{
throw std::runtime_error("Missing entities. Did you forget to call "
"dolfinx::mesh::Topology::create_entities?");
}
const std::int32_t num_local_entities = entity_map->size_local();
// Find number of tagged entities in local range
auto it = std::ranges::lower_bound(meshtags.indices(), num_local_entities);
const int num_active_entities = std::distance(meshtags.indices().begin(), it);
const std::string path_prefix = "/MeshTags/" + name;
xdmf_mesh::add_topology_data(
comm, xml_node, h5_id, path_prefix, *meshtags.topology(), geometry, dim,
std::span<const std::int32_t>(meshtags.indices().data(),
num_active_entities));
// Add attribute node with values
pugi::xml_node attribute_node = xml_node.append_child("Attribute");
assert(attribute_node);
attribute_node.append_attribute("Name") = name.c_str();
attribute_node.append_attribute("AttributeType") = "Scalar";
attribute_node.append_attribute("Center") = "Cell";
std::int64_t global_num_values = 0;
const std::int64_t local_num_values = num_active_entities;
MPI_Allreduce(&local_num_values, &global_num_values, 1, MPI_INT64_T, MPI_SUM,
comm);
const std::int64_t num_local = num_active_entities;
std::int64_t offset = 0;
MPI_Exscan(&num_local, &offset, 1, MPI_INT64_T, MPI_SUM, comm);
const bool use_mpi_io = (dolfinx::MPI::size(comm) > 1);
xdmf_utils::add_data_item(
attribute_node, h5_id, path_prefix + std::string("/Values"),
std::span<const T>(meshtags.values().data(), num_active_entities), offset,
{global_num_values, 1}, "", use_mpi_io);
}
} // namespace io::xdmf_mesh
} // namespace dolfinx
dolfinx::mesh::Geometry
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
dolfinx::mesh::MeshTags
MeshTags associate values with mesh topology entities.
Definition MeshTags.h:33
dolfinx::mesh::MeshTags::indices
std::span< const std::int32_t > indices() const
Definition MeshTags.h:101
dolfinx::mesh::MeshTags::values
std::span< const T > values() const
Values attached to topology entities.
Definition MeshTags.h:104
dolfinx::mesh::MeshTags::dim
int dim() const
Return topological dimension of tagged entities.
Definition MeshTags.h:107
dolfinx::mesh::MeshTags::topology
std::shared_ptr< const Topology > topology() const
Return topology.
Definition MeshTags.h:110
dolfinx::mesh::Mesh
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
dolfinx::mesh::Topology
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:41
dolfinx::MPI::size
int size(MPI_Comm comm)
Definition MPI.cpp:72
dolfinx::geometry
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:22
dolfinx::io::xdmf_mesh
Low-level methods for reading XDMF files.
Definition xdmf_mesh.h:41
dolfinx::io::xdmf_mesh::read_topology_data
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:294
dolfinx::io::xdmf_mesh::read_geometry_data
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:254
dolfinx::io::xdmf_mesh::add_meshtags
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, const std::string &name)
Add mesh tags to XDMF file.
Definition xdmf_mesh.h:95
dolfinx::io::xdmf_mesh::add_mesh
void add_mesh(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, const mesh::Mesh< U > &mesh, const std::string &path_prefix)
Definition xdmf_mesh.cpp:214
dolfinx::io::xdmf_mesh::add_topology_data
void add_topology_data(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, const std::string &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:23
dolfinx::io::xdmf_mesh::add_geometry_data
void add_geometry_data(MPI_Comm comm, pugi::xml_node &xml_node, hid_t h5_id, const std::string &path_prefix, const mesh::Geometry< U > &geometry)
Add Geometry xml node.
Definition xdmf_mesh.cpp:160
dolfinx::mesh
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
dolfinx
Top-level namespace.
Definition defines.h:12