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/common/span.hpp>
14 #include <dolfinx/mesh/MeshTags.h>
15 #include <hdf5.h>
16 #include <string>
17 #include <vector>
18 
19 namespace dolfinx
20 {
21 namespace io
22 {
23 namespace xdmf_meshtags
24 {
25 
27 template <typename T>
28 void add_meshtags(MPI_Comm comm, const mesh::MeshTags<T>& meshtags,
29  pugi::xml_node& xml_node, const hid_t h5_id,
30  const std::string name)
31 {
32  // Get mesh
33  assert(meshtags.mesh());
34  std::shared_ptr<const mesh::Mesh> mesh = meshtags.mesh();
35  const int dim = meshtags.dim();
36 
37  const std::int32_t num_local_entities
38  = mesh->topology().index_map(dim)->size_local();
39 
40  // Find number of tagged entities in local range
41  const int num_active_entities
42  = std::lower_bound(meshtags.indices().begin(), meshtags.indices().end(),
43  num_local_entities)
44  - meshtags.indices().begin();
45 
46  const std::string path_prefix = "/MeshTags/" + name;
48  comm, xml_node, h5_id, path_prefix, mesh->topology(), mesh->geometry(),
49  dim, tcb::span(meshtags.indices().data(), num_active_entities));
50 
51  // Add attribute node with values
52  pugi::xml_node attribute_node = xml_node.append_child("Attribute");
53  assert(attribute_node);
54  attribute_node.append_attribute("Name") = name.c_str();
55  attribute_node.append_attribute("AttributeType") = "Scalar";
56  attribute_node.append_attribute("Center") = "Cell";
57 
58  std::int64_t global_num_values = 0;
59  const std::int64_t local_num_values = num_active_entities;
60  MPI_Allreduce(&local_num_values, &global_num_values, 1, MPI_INT64_T, MPI_SUM,
61  comm);
62  const std::int64_t offset
63  = dolfinx::MPI::global_offset(comm, num_active_entities, true);
64  const bool use_mpi_io = (dolfinx::MPI::size(comm) > 1);
65 
66  xdmf_utils::add_data_item(
67  attribute_node, h5_id, path_prefix + "/Values",
68  tcb::span<const T>(meshtags.values().data(), num_active_entities), offset,
69  {global_num_values, 1}, "", use_mpi_io);
70 }
71 
72 } // namespace xdmf_meshtags
73 } // namespace io
74 } // namespace dolfinx
static int size(MPI_Comm comm)
Return size of the group (number of processes) associated with the communicator.
Definition: MPI.cpp:85
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:92
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 tcb::span< const std::int32_t > &active_entities)
Add Topology xml node.
Definition: xdmf_mesh.cpp:20