DOLFIN-X
DOLFIN-X C++ interface
xdmf_meshtags.h
1 // Copyright (C) 2020 Michal Habera
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 "pugixml.hpp"
10 #include "xdmf_mesh.h"
11 #include "xdmf_utils.h"
12 #include <dolfinx/common/MPI.h>
13 #include <dolfinx/mesh/MeshTags.h>
14 #include <hdf5.h>
15 #include <string>
16 #include <vector>
17 
18 namespace dolfinx
19 {
20 namespace io
21 {
22 namespace xdmf_meshtags
23 {
24 
26 template <typename T>
27 void add_meshtags(MPI_Comm comm, const mesh::MeshTags<T>& meshtags,
28  pugi::xml_node& xml_node, const hid_t h5_id,
29  const std::string name)
30 {
31  // Get mesh
32  assert(meshtags.mesh());
33  std::shared_ptr<const mesh::Mesh> mesh = meshtags.mesh();
34  const int dim = meshtags.dim();
35  const std::vector<std::int32_t>& active_entities = meshtags.indices();
36  const std::string path_prefix = "/MeshTags/" + name;
37  xdmf_mesh::add_topology_data(comm, xml_node, h5_id, path_prefix,
38  mesh->topology(), mesh->geometry(), dim,
39  active_entities);
40 
41  // Add attribute node with values
42  pugi::xml_node attribute_node = xml_node.append_child("Attribute");
43  assert(attribute_node);
44  attribute_node.append_attribute("Name") = name.c_str();
45  attribute_node.append_attribute("AttributeType") = "Scalar";
46  attribute_node.append_attribute("Center") = "Cell";
47 
48  std::int64_t global_num_values = 0;
49  const std::int64_t local_num_values = active_entities.size();
50  MPI_Allreduce(&local_num_values, &global_num_values, 1, MPI_INT64_T, MPI_SUM,
51  comm);
52  const std::int64_t offset
53  = dolfinx::MPI::global_offset(comm, active_entities.size(), true);
54  const bool use_mpi_io = (dolfinx::MPI::size(comm) > 1);
55  xdmf_utils::add_data_item(attribute_node, h5_id, path_prefix + "/Values",
56  meshtags.values(), offset, {global_num_values, 1},
57  "", use_mpi_io);
58 }
59 
60 } // namespace xdmf_meshtags
61 } // namespace io
62 } // namespace dolfinx
static int size(MPI_Comm comm)
Return size of the group (number of processes) associated with the communicator.
Definition: MPI.cpp:87
static std::size_t global_offset(MPI_Comm comm, std::size_t range, bool exclusive)
Find global offset (index) (wrapper for MPI_(Ex)Scan with MPI_SUM as reduction op)
Definition: MPI.cpp:94
void add_topology_data(MPI_Comm comm, pugi::xml_node &xml_node, const hid_t h5_id, const std::string path_prefix, const mesh::Topology &topology, const mesh::Geometry &geometry, const int cell_dim, const std::vector< std::int32_t > &active_entities)
Add Topology xml node.
Definition: xdmf_mesh.cpp:18