| 1 |
// Copyright(C) 1999-2021, 2023 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "Iofaodel_PropertySerialization.h" |
| 8 |
#include "Iofaodel_Utils.h" |
| 9 |
|
| 10 |
#include "Ioss_CommSet.h" |
| 11 |
#include "Ioss_DatabaseIO.h" |
| 12 |
#include "Ioss_DatabaseIO.h" // for DatabaseIO |
| 13 |
#include "Ioss_EdgeBlock.h" |
| 14 |
#include "Ioss_EdgeSet.h" |
| 15 |
#include "Ioss_ElementBlock.h" |
| 16 |
#include "Ioss_ElementSet.h" |
| 17 |
#include "Ioss_FaceBlock.h" |
| 18 |
#include "Ioss_FaceSet.h" |
| 19 |
#include "Ioss_Field.h" // for Region |
| 20 |
#include "Ioss_GroupingEntity.h" |
| 21 |
#include "Ioss_IOFactory.h" |
| 22 |
#include "Ioss_NodeBlock.h" |
| 23 |
#include "Ioss_NodeSet.h" |
| 24 |
#include "Ioss_Property.h" // for Region |
| 25 |
#include "Ioss_Region.h" |
| 26 |
#include "Ioss_Region.h" // for Region |
| 27 |
#include "Ioss_SideBlock.h" |
| 28 |
#include "Ioss_SideSet.h" |
| 29 |
#include "Ioss_State.h" // for State |
| 30 |
#include "Ioss_StructuredBlock.h" |
| 31 |
|
| 32 |
#ifdef NDEBUG |
| 33 |
#undef NDEBUG |
| 34 |
#endif |
| 35 |
#include <cassert> |
| 36 |
|
| 37 |
namespace Iofaodel { |
| 38 |
|
| 39 |
size_t data_size(const Ioss::Property &p) |
| 40 |
{ |
| 41 |
auto type = p.get_type(); |
| 42 |
if (type == Ioss::Property::BasicType::REAL) |
| 43 |
return sizeof(double); |
| 44 |
else if (type == Ioss::Property::BasicType::INTEGER) |
| 45 |
return sizeof(int64_t); |
| 46 |
else if (type == Ioss::Property::BasicType::POINTER) |
| 47 |
return sizeof(int64_t); |
| 48 |
else if (type == Ioss::Property::BasicType::STRING) |
| 49 |
return p.get_string().size(); |
| 50 |
else |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
void map_properties(const Ioss::Region ®ion, const Ioss::GroupingEntity &entity, |
| 55 |
PropertyFunction op) |
| 56 |
{ |
| 57 |
std::vector<std::string> description; |
| 58 |
entity.property_describe(&description); |
| 59 |
for (auto name : description) |
| 60 |
op(region, entity, entity.get_property(name)); |
| 61 |
} |
| 62 |
|
| 63 |
void map_properties(const Ioss::Region ®ion, PropertyFunction op) |
| 64 |
{ |
| 65 |
map_properties(region, region, op); |
| 66 |
|
| 67 |
for (auto entity : region.get_edge_blocks()) |
| 68 |
map_properties(region, *entity, op); |
| 69 |
|
| 70 |
for (auto entity : region.get_element_blocks()) |
| 71 |
map_properties(region, *entity, op); |
| 72 |
|
| 73 |
for (auto entity : region.get_face_blocks()) |
| 74 |
map_properties(region, *entity, op); |
| 75 |
|
| 76 |
for (auto entity : region.get_node_blocks()) |
| 77 |
map_properties(region, *entity, op); |
| 78 |
|
| 79 |
for (auto entity : region.get_structured_blocks()) |
| 80 |
map_properties(region, *entity, op); |
| 81 |
|
| 82 |
for (auto entity : region.get_edgesets()) |
| 83 |
map_properties(region, *entity, op); |
| 84 |
|
| 85 |
for (auto entity : region.get_elementsets()) |
| 86 |
map_properties(region, *entity, op); |
| 87 |
|
| 88 |
for (auto entity : region.get_facesets()) |
| 89 |
map_properties(region, *entity, op); |
| 90 |
|
| 91 |
for (auto entity : region.get_nodesets()) |
| 92 |
map_properties(region, *entity, op); |
| 93 |
|
| 94 |
for (auto entity : region.get_commsets()) |
| 95 |
map_properties(region, *entity, op); |
| 96 |
|
| 97 |
for (auto sideset : region.get_sidesets()) { |
| 98 |
map_properties(region, *sideset, op); |
| 99 |
for (auto sideblock : sideset->get_side_blocks()) { |
| 100 |
map_properties(region, *sideblock, op); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Put this in the meta data section of the LDO |
| 106 |
property_entry_t::property_entry_t(const Ioss::Property &property, const size_t start) |
| 107 |
: basic_type(property.get_type()), |
| 108 |
|
| 109 |
is_implicit(property.is_implicit()), is_valid(property.is_valid()), |
| 110 |
|
| 111 |
name{start, property.get_name().size()}, |
| 112 |
value{name.offset + name.size, Iofaodel::data_size(property)}, |
| 113 |
data_size(name.size + value.size) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
lunasa::DataObject pack_property(const Ioss::Region ®ion, const Ioss::GroupingEntity &entity, |
| 118 |
const Ioss::Property &property) |
| 119 |
{ |
| 120 |
property_entry_t property_entry(property); |
| 121 |
|
| 122 |
meta_entry_t meta_entry{meta_entry_t::IossType::IossProperty, 0, property_entry.data_size}; |
| 123 |
|
| 124 |
auto ldo = lunasa::DataObject(sizeof(meta_entry_t), |
| 125 |
sizeof(property_entry_t) + property_entry.data_size, |
| 126 |
lunasa::DataObject::AllocatorType::eager); |
| 127 |
|
| 128 |
// copy meta_entry_t to meta section |
| 129 |
std::memcpy(static_cast<char *>(ldo.GetMetaPtr()), &meta_entry, sizeof(meta_entry_t)); |
| 130 |
|
| 131 |
// copy property_entry_t to meta section |
| 132 |
std::memcpy(static_cast<char *>(ldo.GetDataPtr()), &property_entry, sizeof(property_entry_t)); |
| 133 |
|
| 134 |
auto entry = static_cast<property_entry_t *>(ldo.GetDataPtr()); |
| 135 |
auto name_ptr = static_cast<char *>(entry->data) + entry->name.offset; |
| 136 |
auto value_ptr = static_cast<void *>(static_cast<char *>(entry->data) + entry->value.offset); |
| 137 |
|
| 138 |
// copy name to data section |
| 139 |
std::memcpy(name_ptr, property.get_name().data(), entry->name.size); |
| 140 |
|
| 141 |
// copy value to data section |
| 142 |
if (property.get_type() == Ioss::Property::BasicType::INTEGER) { |
| 143 |
auto value = static_cast<int64_t *>(value_ptr); |
| 144 |
*value = property.get_int(); |
| 145 |
} |
| 146 |
else if (property.get_type() == Ioss::Property::BasicType::REAL) { |
| 147 |
auto value = static_cast<double *>(value_ptr); |
| 148 |
*value = property.get_int(); |
| 149 |
} |
| 150 |
else if (property.get_type() == Ioss::Property::BasicType::STRING) { |
| 151 |
std::memcpy(static_cast<char *>(value_ptr), property.get_string().data(), |
| 152 |
property.get_string().size()); |
| 153 |
} |
| 154 |
|
| 155 |
if (entry->value.size != Iofaodel::data_size(property)) |
| 156 |
std::cerr << "value.size mismatch: " << entry->value.size |
| 157 |
<< " ?= " << Iofaodel::data_size(property); |
| 158 |
return ldo; |
| 159 |
} |
| 160 |
|
| 161 |
int64_t property_get_int(lunasa::DataObject ldo) |
| 162 |
{ |
| 163 |
auto prop(static_cast<Iofaodel::property_entry_t *>(ldo.GetDataPtr())); |
| 164 |
auto value_ptr = static_cast<void *>(prop->data + prop->value.offset); |
| 165 |
return *(static_cast<int64_t *>(value_ptr)); |
| 166 |
} |
| 167 |
|
| 168 |
std::string property_get_string(lunasa::DataObject ldo) |
| 169 |
{ |
| 170 |
auto prop(static_cast<Iofaodel::property_entry_t *>(ldo.GetDataPtr())); |
| 171 |
return std::string(prop->data + prop->value.offset, prop->value.size); |
| 172 |
} |
| 173 |
} // namespace Iofaodel |