1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// Copyright (C) 2012-2023 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
#include "xdmf_utils.h"
#include <array>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <map>
#include <pugixml.hpp>
#include <vector>
using namespace dolfinx;
using namespace dolfinx::io;
namespace
{
template <typename T, std::size_t ndim>
using mdspan_t = md::mdspan<T, md::dextents<std::size_t, ndim>>;
} // namespace
//----------------------------------------------------------------------------
std::pair<std::string, int>
xdmf_utils::get_cell_type(const pugi::xml_node& topology_node)
{
assert(topology_node);
pugi::xml_attribute type_attr = topology_node.attribute("TopologyType");
assert(type_attr);
const static std::map<std::string, std::pair<std::string, int>> xdmf_to_dolfin
= {{"polyvertex", {"point", 1}},
{"polyline", {"interval", 1}},
{"edge_3", {"interval", 2}},
{"triangle", {"triangle", 1}},
{"triangle_6", {"triangle", 2}},
{"tetrahedron", {"tetrahedron", 1}},
{"tetrahedron_10", {"tetrahedron", 2}},
{"quadrilateral", {"quadrilateral", 1}},
{"quadrilateral_8", {"quadrilateral", 2}},
{"quadrilateral_9", {"quadrilateral", 2}},
{"quadrilateral_16", {"quadrilateral", 3}},
{"hexahedron", {"hexahedron", 1}},
{"hexahedron_20", {"hexahedron", 2}},
{"wedge", {"prism", 1}},
{"hexahedron_27", {"hexahedron", 2}}};
// Convert XDMF cell type string to DOLFINx cell type string
std::string cell_type = type_attr.as_string();
std::ranges::transform(cell_type, cell_type.begin(),
[](auto c) { return std::tolower(c); });
auto it = xdmf_to_dolfin.find(cell_type);
if (it == xdmf_to_dolfin.end())
{
throw std::runtime_error("Cannot recognise cell type. Unknown value: "
+ cell_type);
}
return it->second;
}
//----------------------------------------------------------------------------
std::array<std::string, 2>
xdmf_utils::get_hdf5_paths(const pugi::xml_node& dataitem_node)
{
// Check that node is a DataItem node
assert(dataitem_node);
const std::string dataitem_str = "DataItem";
if (dataitem_node.name() != dataitem_str)
{
throw std::runtime_error("Node name is \""
+ std::string(dataitem_node.name())
+ R"(", expecting "DataItem")");
}
// Check that format is HDF
pugi::xml_attribute format_attr = dataitem_node.attribute("Format");
assert(format_attr);
const std::string format = format_attr.as_string();
if (format.compare("HDF") != 0)
{
throw std::runtime_error("DataItem format \"" + format
+ R"(" is not "HDF")");
}
// Get path data
pugi::xml_node path_node = dataitem_node.first_child();
assert(path_node);
// Create string from path and trim leading and trailing whitespace
std::string path = path_node.text().get();
boost::algorithm::trim(path);
// Split string into file path and HD5 internal path
std::vector<std::string> paths;
boost::split(paths, path, boost::is_any_of(":"));
assert(paths.size() == 2);
return {{paths[0], paths[1]}};
}
//-----------------------------------------------------------------------------
std::filesystem::path
xdmf_utils::get_hdf5_filename(const std::filesystem::path& xdmf_filename)
{
std::filesystem::path p = xdmf_filename;
p.replace_extension("h5");
if (p.string() == xdmf_filename)
{
throw std::runtime_error("Cannot deduce name of HDF5 file from XDMF "
"filename. Filename clash. Check XDMF filename");
}
return p;
}
//-----------------------------------------------------------------------------
std::vector<std::int64_t>
xdmf_utils::get_dataset_shape(const pugi::xml_node& dataset_node)
{
// Get Dimensions attribute string
assert(dataset_node);
pugi::xml_attribute dimensions_attr = dataset_node.attribute("Dimensions");
// Gets dimensions, if attribute is present
std::vector<std::int64_t> dims;
if (dimensions_attr)
{
// Split dimensions string
const std::string dims_str = dimensions_attr.as_string();
std::vector<std::string> dims_list;
boost::split(dims_list, dims_str, boost::is_any_of(" "));
// Cast dims to integers
for (const auto& d : dims_list)
dims.push_back(boost::lexical_cast<std::int64_t>(d));
}
return dims;
}
//----------------------------------------------------------------------------
std::int64_t xdmf_utils::get_num_cells(const pugi::xml_node& topology_node)
{
assert(topology_node);
// Get number of cells from topology
std::int64_t num_cells_topology = -1;
pugi::xml_attribute num_cells_attr
= topology_node.attribute("NumberOfElements");
if (num_cells_attr)
num_cells_topology = num_cells_attr.as_llong();
// Get number of cells from topology dataset
pugi::xml_node topology_dataset_node = topology_node.child("DataItem");
assert(topology_dataset_node);
const std::vector tdims = get_dataset_shape(topology_dataset_node);
// Check that number of cells can be determined
if (tdims.size() != 2 and num_cells_topology == -1)
throw std::runtime_error("Cannot determine number of cells in XDMF mesh");
// Check for consistency if number of cells appears in both the
// topology and DataItem nodes
if (num_cells_topology != -1 and tdims.size() == 2)
{
if (num_cells_topology != tdims[0])
throw std::runtime_error("Cannot determine number of cells in XDMF mesh");
}
return std::max(num_cells_topology, tdims[0]);
}
//----------------------------------------------------------------------------
std::string xdmf_utils::vtk_cell_type_str(mesh::CellType cell_type,
int num_nodes)
{
static const std::map<mesh::CellType, std::map<int, std::string>> vtk_map
= {{mesh::CellType::point, {{1, "PolyVertex"}}},
{mesh::CellType::interval, {{2, "PolyLine"}, {3, "Edge_3"}}},
{mesh::CellType::triangle,
{{3, "Triangle"}, {6, "Triangle_6"}, {10, "Triangle_10"}}},
{mesh::CellType::quadrilateral,
{{4, "Quadrilateral"},
{8, "Quadrilateral_8"},
{9, "Quadrilateral_9"},
{16, "Quadrilateral_16"}}},
{mesh::CellType::prism, {{6, "Wedge"}}},
{mesh::CellType::tetrahedron,
{{4, "Tetrahedron"}, {10, "Tetrahedron_10"}, {20, "Tetrahedron_20"}}},
{mesh::CellType::hexahedron,
{{8, "Hexahedron"}, {27, "Hexahedron_27"}, {20, "Hexahedron_20"}}}};
// Get cell family
auto cell = vtk_map.find(cell_type);
if (cell == vtk_map.end())
throw std::runtime_error("Could not find cell type.");
// Get cell string
auto cell_str = cell->second.find(num_nodes);
if (cell_str == cell->second.end())
throw std::runtime_error("Could not find VTK string for cell order.");
return cell_str->second;
}
//-----------------------------------------------------------------------------
|