Select Git revision
ASTNodeDataVariant.hpp
CheckpointUtils.cpp 15.22 KiB
#include <utils/checkpointing/CheckpointUtils.hpp>
#include <analysis/IQuadratureDescriptor.hpp>
#include <language/modules/MeshModuleTypes.hpp>
#include <language/modules/SchemeModuleTypes.hpp>
#include <language/utils/ASTNodeDataTypeTraits.hpp>
#include <language/utils/DataHandler.hpp>
#include <mesh/ItemType.hpp>
#include <mesh/Mesh.hpp>
#include <mesh/MeshVariant.hpp>
#include <mesh/NamedBoundaryDescriptor.hpp>
#include <mesh/NamedInterfaceDescriptor.hpp>
#include <mesh/NamedZoneDescriptor.hpp>
#include <mesh/NumberedBoundaryDescriptor.hpp>
#include <mesh/NumberedInterfaceDescriptor.hpp>
#include <mesh/NumberedZoneDescriptor.hpp>
#include <scheme/DiscreteFunctionP0.hpp>
#include <scheme/DiscreteFunctionP0Vector.hpp>
#include <utils/checkpointing/DiscreteFunctionTypeHFType.hpp>
#include <utils/checkpointing/IBoundaryDescriptorHFType.hpp>
#include <utils/checkpointing/IInterfaceDescriptorHFType.hpp>
#include <utils/checkpointing/IZoneDescriptorHFType.hpp>
#include <utils/checkpointing/ItemTypeHFType.hpp>
#include <utils/checkpointing/QuadratureTypeHFType.hpp>
#include <utils/checkpointing/RefItemListHFType.hpp>
template <ItemType item_type, size_t Dimension>
void
writeRefItemList(const Connectivity<Dimension>& connectivity, HighFive::Group& connectivity_group)
{
for (size_t i_item_list = 0; i_item_list < connectivity.template numberOfRefItemList<item_type>(); ++i_item_list) {
auto ref_item_list = connectivity.template refItemList<item_type>(i_item_list);
std::ostringstream ref_item_list_group_name;
ref_item_list_group_name << "item_ref_list/" << itemName(item_type) << '/' << ref_item_list.refId().tagName();
HighFive::Group ref_item_list_group = connectivity_group.createGroup(ref_item_list_group_name.str());
ref_item_list_group.createAttribute("tag_name", ref_item_list.refId().tagName());
ref_item_list_group.createAttribute("tag_number", ref_item_list.refId().tagNumber());
ref_item_list_group.createAttribute("type", ref_item_list.type());
write(ref_item_list_group, "list", ref_item_list.list());
}
}
template <size_t Dimension>
void
writeConnectivity(const Connectivity<Dimension>& connectivity, HighFive::File& file, HighFive::Group& checkpoint_group)
{
std::string connectivity_group_name = "connectivity/" + std::to_string(connectivity.id());
if (not checkpoint_group.exist(connectivity_group_name)) {
bool linked = false;
for (auto group_name : file.listObjectNames()) {
if (file.exist(group_name + "/" + connectivity_group_name)) {
checkpoint_group.createHardLink(connectivity_group_name,
file.getGroup(group_name + "/" + connectivity_group_name));
linked = true;
break;
}
}
if (not linked) {
HighFive::Group connectivity_group = checkpoint_group.createGroup(connectivity_group_name);
connectivity_group.createAttribute("dimension", connectivity.dimension());
connectivity_group.createAttribute("id", connectivity.id());
connectivity_group.createAttribute("type", std::string{"unstructured"});
write(connectivity_group, "cell_to_node_matrix_values",
connectivity.getMatrix(ItemType::cell, ItemType::node).values());
write(connectivity_group, "cell_to_node_matrix_rowsMap",
connectivity.getMatrix(ItemType::cell, ItemType::node).rowsMap());
if constexpr (Dimension > 1) {
write(connectivity_group, "cell_to_face_matrix_values",
connectivity.getMatrix(ItemType::cell, ItemType::face).values());
write(connectivity_group, "cell_to_face_matrix_rowsMap",
connectivity.getMatrix(ItemType::cell, ItemType::face).rowsMap());
write(connectivity_group, "face_to_node_matrix_values",
connectivity.getMatrix(ItemType::face, ItemType::node).values());
write(connectivity_group, "face_to_node_matrix_rowsMap",
connectivity.getMatrix(ItemType::face, ItemType::node).rowsMap());
write(connectivity_group, "node_to_face_matrix_values",
connectivity.getMatrix(ItemType::node, ItemType::face).values());
write(connectivity_group, "node_to_face_matrix_rowsMap",
connectivity.getMatrix(ItemType::node, ItemType::face).rowsMap());
write(connectivity_group, "cell_face_is_reversed", connectivity.cellFaceIsReversed().arrayView());
}
if constexpr (Dimension > 2) {
write(connectivity_group, "cell_to_edge_matrix_values",
connectivity.getMatrix(ItemType::cell, ItemType::edge).values());
write(connectivity_group, "cell_to_edge_matrix_rowsMap",
connectivity.getMatrix(ItemType::cell, ItemType::edge).rowsMap());
write(connectivity_group, "face_to_edge_matrix_values",
connectivity.getMatrix(ItemType::face, ItemType::edge).values());
write(connectivity_group, "face_to_edge_matrix_rowsMap",
connectivity.getMatrix(ItemType::face, ItemType::edge).rowsMap());
write(connectivity_group, "edge_to_node_matrix_values",
connectivity.getMatrix(ItemType::edge, ItemType::node).values());
write(connectivity_group, "edge_to_node_matrix_rowsMap",
connectivity.getMatrix(ItemType::edge, ItemType::node).rowsMap());
write(connectivity_group, "node_to_edge_matrix_values",
connectivity.getMatrix(ItemType::node, ItemType::edge).values());
write(connectivity_group, "node_to_edge_matrix_rowsMap",
connectivity.getMatrix(ItemType::node, ItemType::edge).rowsMap());
write(connectivity_group, "face_edge_is_reversed", connectivity.faceEdgeIsReversed().arrayView());
}
write(connectivity_group, "cell_type", connectivity.cellType());
write(connectivity_group, "cell_numbers", connectivity.cellNumber());
write(connectivity_group, "node_numbers", connectivity.nodeNumber());
write(connectivity_group, "cell_owner", connectivity.cellOwner());
write(connectivity_group, "node_owner", connectivity.nodeOwner());
if constexpr (Dimension > 1) {
write(connectivity_group, "face_numbers", connectivity.faceNumber());
write(connectivity_group, "face_owner", connectivity.faceOwner());
}
if constexpr (Dimension > 2) {
write(connectivity_group, "edge_numbers", connectivity.edgeNumber());
write(connectivity_group, "edge_owner", connectivity.edgeOwner());
}
writeRefItemList<ItemType::cell>(connectivity, connectivity_group);
writeRefItemList<ItemType::face>(connectivity, connectivity_group);
writeRefItemList<ItemType::edge>(connectivity, connectivity_group);
writeRefItemList<ItemType::node>(connectivity, connectivity_group);
}
}
}
void
writeIBoundaryDescriptor(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const IBoundaryDescriptor> iboundary_descriptor_p =
dynamic_cast<const DataHandler<const IBoundaryDescriptor>&>(embedded_data.get()).data_ptr();
const IBoundaryDescriptor& iboundary_descriptor = *iboundary_descriptor_p;
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(iboundary_descriptor_p)>));
variable_group.createAttribute("iboundary_descriptor_type", iboundary_descriptor.type());
switch (iboundary_descriptor.type()) {
case IBoundaryDescriptor::Type::named: {
const NamedBoundaryDescriptor& named_boundary_descriptor =
dynamic_cast<const NamedBoundaryDescriptor&>(iboundary_descriptor);
variable_group.createAttribute("name", named_boundary_descriptor.name());
break;
}
case IBoundaryDescriptor::Type::numbered: {
const NumberedBoundaryDescriptor& numbered_boundary_descriptor =
dynamic_cast<const NumberedBoundaryDescriptor&>(iboundary_descriptor);
variable_group.createAttribute("number", numbered_boundary_descriptor.number());
break;
}
}
}
void
writeIDiscreteFunctionDescriptor(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const IDiscreteFunctionDescriptor> idiscrete_function_desriptor_p =
dynamic_cast<const DataHandler<const IDiscreteFunctionDescriptor>&>(embedded_data.get()).data_ptr();
const IDiscreteFunctionDescriptor& idiscrete_function_descriptor = *idiscrete_function_desriptor_p;
variable_group.createAttribute("type",
dataTypeName(ast_node_data_type_from<decltype(idiscrete_function_desriptor_p)>));
variable_group.createAttribute("discrete_function_type", idiscrete_function_descriptor.type());
}
void
writeIInterfaceDescriptor(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const IInterfaceDescriptor> iinterface_descriptor_p =
dynamic_cast<const DataHandler<const IInterfaceDescriptor>&>(embedded_data.get()).data_ptr();
const IInterfaceDescriptor& iinterface_descriptor = *iinterface_descriptor_p;
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(iinterface_descriptor_p)>));
variable_group.createAttribute("iinterface_descriptor_type", iinterface_descriptor.type());
switch (iinterface_descriptor.type()) {
case IInterfaceDescriptor::Type::named: {
const NamedInterfaceDescriptor& named_interface_descriptor =
dynamic_cast<const NamedInterfaceDescriptor&>(iinterface_descriptor);
variable_group.createAttribute("name", named_interface_descriptor.name());
break;
}
case IInterfaceDescriptor::Type::numbered: {
const NumberedInterfaceDescriptor& numbered_boundary_descriptor =
dynamic_cast<const NumberedInterfaceDescriptor&>(iinterface_descriptor);
variable_group.createAttribute("number", numbered_boundary_descriptor.number());
break;
}
}
}
void
writeIQuadratureDescriptor(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const IQuadratureDescriptor> iquadrature_descriptor_p =
dynamic_cast<const DataHandler<const IQuadratureDescriptor>&>(embedded_data.get()).data_ptr();
const IQuadratureDescriptor& iquadrature_descriptor = *iquadrature_descriptor_p;
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(iquadrature_descriptor_p)>));
variable_group.createAttribute("quadrature_type", iquadrature_descriptor.type());
variable_group.createAttribute("quadrature_degree", iquadrature_descriptor.degree());
}
void
writeItemType(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const ItemType> item_type_p =
dynamic_cast<const DataHandler<const ItemType>&>(embedded_data.get()).data_ptr();
const ItemType& item_type = *item_type_p;
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(item_type_p)>));
variable_group.createAttribute("item_type", item_type);
}
void
writeIZoneDescriptor(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File&,
HighFive::Group&,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const IZoneDescriptor> izone_descriptor_p =
dynamic_cast<const DataHandler<const IZoneDescriptor>&>(embedded_data.get()).data_ptr();
const IZoneDescriptor& izone_descriptor = *izone_descriptor_p;
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(izone_descriptor_p)>));
variable_group.createAttribute("izone_descriptor_type", izone_descriptor.type());
switch (izone_descriptor.type()) {
case IZoneDescriptor::Type::named: {
const NamedZoneDescriptor& named_zone_descriptor = dynamic_cast<const NamedZoneDescriptor&>(izone_descriptor);
variable_group.createAttribute("name", named_zone_descriptor.name());
break;
}
case IZoneDescriptor::Type::numbered: {
const NumberedZoneDescriptor& numbered_boundary_descriptor =
dynamic_cast<const NumberedZoneDescriptor&>(izone_descriptor);
variable_group.createAttribute("number", numbered_boundary_descriptor.number());
break;
}
}
}
void
writeMesh(const std::string& symbol_name,
const EmbeddedData& embedded_data,
HighFive::File& file,
HighFive::Group& checkpoint_group,
HighFive::Group& symbol_table_group)
{
HighFive::Group variable_group = symbol_table_group.createGroup("embedded/" + symbol_name);
std::shared_ptr<const MeshVariant> mesh_v =
dynamic_cast<const DataHandler<const MeshVariant>&>(embedded_data.get()).data_ptr();
variable_group.createAttribute("type", dataTypeName(ast_node_data_type_from<decltype(mesh_v)>));
variable_group.createAttribute("id", mesh_v->id());
std::string mesh_group_name = "mesh/" + std::to_string(mesh_v->id());
if (not checkpoint_group.exist(mesh_group_name)) {
bool linked = false;
for (auto group_name : file.listObjectNames()) {
if (file.exist(group_name + "/" + mesh_group_name)) {
checkpoint_group.createHardLink(mesh_group_name, file.getGroup(group_name + "/" + mesh_group_name));
linked = true;
break;
}
}
if (not linked) {
HighFive::Group mesh_group = checkpoint_group.createGroup(mesh_group_name);
mesh_group.createAttribute("connectivity", mesh_v->connectivity().id());
std::visit(
[&](auto&& mesh) {
using MeshType = mesh_type_t<decltype(mesh)>;
if constexpr (is_polygonal_mesh_v<MeshType>) {
mesh_group.createAttribute("id", mesh->id());
mesh_group.createAttribute("type", std::string{"polygonal"});
mesh_group.createAttribute("dimension", mesh->dimension());
write(mesh_group, "xr", mesh->xr());
} else {
throw UnexpectedError("unexpected mesh type");
}
},
mesh_v->variant());
}
}
std::visit(
[&](auto&& mesh) {
using MeshType = mesh_type_t<decltype(mesh)>;
if constexpr (is_polygonal_mesh_v<MeshType>) {
writeConnectivity(mesh->connectivity(), file, checkpoint_group);
}
},
mesh_v->variant());
}