| 1 |
// Copyright(C) 1999-2022 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_Utils.h" |
| 8 |
|
| 9 |
#include "Ioss_CommSet.h" |
| 10 |
#include "Ioss_DatabaseIO.h" |
| 11 |
#include "Ioss_DatabaseIO.h" // for DatabaseIO |
| 12 |
#include "Ioss_EdgeBlock.h" |
| 13 |
#include "Ioss_EdgeSet.h" |
| 14 |
#include "Ioss_ElementBlock.h" |
| 15 |
#include "Ioss_ElementSet.h" |
| 16 |
#include "Ioss_FaceBlock.h" |
| 17 |
#include "Ioss_FaceSet.h" |
| 18 |
#include "Ioss_Field.h" // for Region |
| 19 |
#include "Ioss_GroupingEntity.h" |
| 20 |
#include "Ioss_IOFactory.h" |
| 21 |
#include "Ioss_NodeBlock.h" |
| 22 |
#include "Ioss_NodeSet.h" |
| 23 |
#include "Ioss_Property.h" // for Region |
| 24 |
#include "Ioss_Region.h" |
| 25 |
#include "Ioss_Region.h" // for Region |
| 26 |
#include "Ioss_SideBlock.h" |
| 27 |
#include "Ioss_SideSet.h" |
| 28 |
#include "Ioss_State.h" // for State |
| 29 |
#include "Ioss_StructuredBlock.h" |
| 30 |
|
| 31 |
#include "cereal/archives/portable_binary.hpp" |
| 32 |
#include "cereal/types/array.hpp" |
| 33 |
#include "cereal/types/string.hpp" |
| 34 |
#include "cereal/types/utility.hpp" |
| 35 |
#include "cereal/types/vector.hpp" |
| 36 |
#include <sstream> |
| 37 |
|
| 38 |
namespace Iofaodel { |
| 39 |
|
| 40 |
state_entry_t::state_entry_t(const Ioss::Region &r) |
| 41 |
: count(r.get_property("state_count").get_int()), |
| 42 |
value{0, count * sizeof(state_entry_t::basic_type)} |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
sideblock_entry_t::sideblock_entry_t(const Ioss::SideBlock &sb) : entity_count(sb.entity_count()) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
/* The constructor for SideBlocks requires attributes that are neither properties nor fields. |
| 51 |
This function collects and serializes these attributes so that the SideBlock can be |
| 52 |
reconstructed from the data stored in Faodel. */ |
| 53 |
lunasa::DataObject pack_sideblock(const Ioss::SideBlock &sb) |
| 54 |
{ |
| 55 |
// meta, entry, data, data_size |
| 56 |
auto ldo = lunasa::DataObject(sizeof(meta_entry_t), sizeof(sideblock_entry_t), |
| 57 |
lunasa::DataObject::AllocatorType::eager); |
| 58 |
|
| 59 |
auto meta = static_cast<meta_entry_t *>(ldo.GetMetaPtr()); |
| 60 |
meta->ioss_type = meta_entry_t::IossType::IofaodelSideBlock; |
| 61 |
meta->value.offset = 0; |
| 62 |
meta->value.size = sizeof(sideblock_entry_t); |
| 63 |
|
| 64 |
auto entry = static_cast<sideblock_entry_t *>( |
| 65 |
static_cast<void *>(static_cast<char *>(ldo.GetDataPtr()) + meta->value.offset)); |
| 66 |
|
| 67 |
entry->entity_count = sb.entity_count(); |
| 68 |
|
| 69 |
return ldo; |
| 70 |
} |
| 71 |
|
| 72 |
int64_t unpack_sideblocks(lunasa::DataObject ldo) |
| 73 |
{ |
| 74 |
auto meta = static_cast<meta_entry_t *>(ldo.GetMetaPtr()); |
| 75 |
assert(meta->ioss_type == meta_entry_t::IossType::IofaodelSideBlock); |
| 76 |
|
| 77 |
auto entry = static_cast<sideblock_entry_t *>( |
| 78 |
static_cast<void *>(static_cast<char *>(ldo.GetDataPtr()) + meta->value.offset)); |
| 79 |
|
| 80 |
return entry->entity_count; |
| 81 |
} |
| 82 |
|
| 83 |
/* The constructor for StructuredBlocks requires attributes that are neither properties nor |
| 84 |
fields. This function collects and serializes these attributes so that the StructuredBlock can |
| 85 |
be reconstructed from the data stored in Faodel. */ |
| 86 |
lunasa::DataObject pack_structuredblock(const Ioss::StructuredBlock &sb) |
| 87 |
{ |
| 88 |
std::ostringstream outstream; |
| 89 |
cereal::PortableBinaryOutputArchive oarchive(outstream); |
| 90 |
oarchive(sb); |
| 91 |
std::streamoff length = outstream.tellp(); |
| 92 |
|
| 93 |
// meta, entry, data, data_size |
| 94 |
auto ldo = lunasa::DataObject(length); |
| 95 |
std::string outstring = outstream.str(); |
| 96 |
outstring.copy(static_cast<char *>(ldo.GetDataPtr()), length); |
| 97 |
char *p = (char *)ldo.GetDataPtr(); |
| 98 |
|
| 99 |
return ldo; |
| 100 |
} |
| 101 |
|
| 102 |
void unpack_structuredblock(lunasa::DataObject &ldo, Ioss::StructuredBlock &sb) |
| 103 |
{ |
| 104 |
std::stringstream instream; |
| 105 |
char *p = (char *)ldo.GetDataPtr(); |
| 106 |
for (int i = 0; i < ldo.GetDataSize(); i++) { |
| 107 |
instream << p[i]; |
| 108 |
} |
| 109 |
instream.seekg(0, instream.end); |
| 110 |
std::streamoff length = instream.tellg(); |
| 111 |
instream.seekg(0, instream.beg); |
| 112 |
cereal::PortableBinaryInputArchive iarchive(instream); |
| 113 |
iarchive(sb); |
| 114 |
} |
| 115 |
|
| 116 |
lunasa::DataObject pack_states(const Ioss::Region &r) |
| 117 |
{ |
| 118 |
// meta, entry, data, data_size |
| 119 |
auto state_count = r.get_property("state_count").get_int(); |
| 120 |
|
| 121 |
// std::cerr << "state count: " << state_count << std::endl; |
| 122 |
auto data_size = state_count * sizeof(state_entry_t::basic_type); |
| 123 |
|
| 124 |
auto ldo = lunasa::DataObject(sizeof(meta_entry_t), sizeof(state_entry_t) + data_size, |
| 125 |
lunasa::DataObject::AllocatorType::eager); |
| 126 |
|
| 127 |
auto meta = static_cast<meta_entry_t *>(ldo.GetMetaPtr()); |
| 128 |
meta->ioss_type = meta_entry_t::IossType::IofaodelStates; |
| 129 |
meta->value.offset = 0; |
| 130 |
meta->value.size = sizeof(state_entry_t) + data_size; |
| 131 |
|
| 132 |
auto entry = static_cast<state_entry_t *>( |
| 133 |
static_cast<void *>(static_cast<char *>(ldo.GetDataPtr()) + meta->value.offset)); |
| 134 |
entry->count = state_count; |
| 135 |
entry->value.offset = 0; |
| 136 |
entry->value.size = data_size; |
| 137 |
|
| 138 |
auto data = static_cast<Iofaodel::state_entry_t::basic_type *>( |
| 139 |
static_cast<void *>(entry->data + entry->value.offset)); |
| 140 |
|
| 141 |
for (auto state(1); state <= entry->count; state++) |
| 142 |
data[state - 1] = r.get_state_time(state); |
| 143 |
|
| 144 |
return ldo; |
| 145 |
}; |
| 146 |
|
| 147 |
kelpie::Key make_states_search_key(int rank, const Ioss::Region ®ion) |
| 148 |
{ |
| 149 |
auto region_name = region.name(); |
| 150 |
if (region_name.empty()) { |
| 151 |
region_name = "UNNAMED"; |
| 152 |
} |
| 153 |
return kelpie::Key(std::to_string(rank), "/TimeSteps*"); |
| 154 |
} |
| 155 |
|
| 156 |
kelpie::Key make_states_key(int rank, const Ioss::Region ®ion) |
| 157 |
{ |
| 158 |
auto region_name = region.name(); |
| 159 |
if (region_name.empty()) { |
| 160 |
region_name = "UNNAMED"; |
| 161 |
} |
| 162 |
return kelpie::Key(std::to_string(rank), "/TimeSteps"); |
| 163 |
} |
| 164 |
|
| 165 |
kelpie::Key sideblocks_search_key(int rank, const Ioss::Region ®ion, |
| 166 |
const Ioss::SideSet &sideset) |
| 167 |
{ |
| 168 |
auto region_name = region.name(); |
| 169 |
if (region_name.empty()) { |
| 170 |
region_name = "UNNAMED"; |
| 171 |
} |
| 172 |
|
| 173 |
return kelpie::Key(std::to_string(rank), "/SideSet/" + sideset.name() + "/SideBlock/*"); |
| 174 |
} |
| 175 |
|
| 176 |
kelpie::Key make_sideblock_key(int rank, const Ioss::Region ®ion, const Ioss::SideSet &sideset, |
| 177 |
const Ioss::SideBlock &sideblock) |
| 178 |
{ |
| 179 |
auto region_name = region.name(); |
| 180 |
if (region_name.empty()) { |
| 181 |
region_name = "UNNAMED"; |
| 182 |
} |
| 183 |
|
| 184 |
return kelpie::Key(std::to_string(rank), |
| 185 |
"/SideSet/" + sideset.name() + "/SideBlock/" + sideblock.name()); |
| 186 |
} |
| 187 |
|
| 188 |
kelpie::Key structuredblock_search_key(int rank, const Ioss::Region ®ion, |
| 189 |
const Ioss::StructuredBlock &structuredblock) |
| 190 |
{ |
| 191 |
auto region_name = region.name(); |
| 192 |
if (region_name.empty()) { |
| 193 |
region_name = "UNNAMED"; |
| 194 |
} |
| 195 |
return kelpie::Key(std::to_string(rank), |
| 196 |
"/StructuredBlock/" + structuredblock.name() + "/Attributes*"); |
| 197 |
} |
| 198 |
|
| 199 |
kelpie::Key make_structuredblock_key(int rank, const Ioss::Region ®ion, |
| 200 |
const Ioss::StructuredBlock &structuredblock) |
| 201 |
{ |
| 202 |
auto region_name = region.name(); |
| 203 |
if (region_name.empty()) { |
| 204 |
region_name = "UNNAMED"; |
| 205 |
} |
| 206 |
return kelpie::Key(std::to_string(rank), |
| 207 |
"/StructuredBlock/" + structuredblock.name() + "/Attributes"); |
| 208 |
} |
| 209 |
|
| 210 |
kelpie::Key make_key(int rank, const Ioss::Region ®ion, |
| 211 |
const Ioss::GroupingEntity &grouping_entity, const Ioss::Field &field) |
| 212 |
{ |
| 213 |
std::string grouping_entity_name; |
| 214 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 215 |
grouping_entity_name = ""; |
| 216 |
} |
| 217 |
else { |
| 218 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 219 |
} |
| 220 |
|
| 221 |
return kelpie::Key(std::to_string(rank), |
| 222 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 223 |
grouping_entity.type_string() + grouping_entity_name + |
| 224 |
"/Field/RoleType/" + to_string(field.get_role()) + "/BasicType/" + |
| 225 |
to_string(field.get_type()) + "/Name/" + field.get_name()); |
| 226 |
} |
| 227 |
|
| 228 |
kelpie::Key make_key(int rank, const Ioss::Region ®ion, |
| 229 |
const Ioss::GroupingEntity &grouping_entity, const Ioss::Property &property) |
| 230 |
{ |
| 231 |
std::string grouping_entity_name; |
| 232 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 233 |
grouping_entity_name = ""; |
| 234 |
} |
| 235 |
else { |
| 236 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 237 |
} |
| 238 |
|
| 239 |
return kelpie::Key(std::to_string(rank), |
| 240 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 241 |
grouping_entity.type_string() + grouping_entity_name + |
| 242 |
"/Property/BasicType/" + to_string(property.get_type()) + "/Name/" + |
| 243 |
property.get_name()); |
| 244 |
} |
| 245 |
|
| 246 |
kelpie::Key make_key(int rank, const Ioss::Region ®ion, |
| 247 |
const Ioss::GroupingEntity &grouping_entity) |
| 248 |
{ |
| 249 |
std::string grouping_entity_name; |
| 250 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 251 |
grouping_entity_name = ""; |
| 252 |
} |
| 253 |
else { |
| 254 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 255 |
} |
| 256 |
|
| 257 |
return kelpie::Key(std::to_string(rank), |
| 258 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 259 |
grouping_entity.type_string() + grouping_entity_name); |
| 260 |
} |
| 261 |
|
| 262 |
kelpie::Key entity_search_key(int rank, const Ioss::Region ®ion, const std::string &entity) |
| 263 |
{ |
| 264 |
auto region_name = region.name(); |
| 265 |
if (region_name.empty()) { |
| 266 |
region_name = "UNNAMED"; |
| 267 |
} |
| 268 |
return kelpie::Key(std::to_string(rank), "/State/" + |
| 269 |
std::to_string(region.get_current_state()) + |
| 270 |
"/Entity/" + entity + "/*"); |
| 271 |
} |
| 272 |
|
| 273 |
kelpie::Key entity_search_key(int rank, const Ioss::Region ®ion, |
| 274 |
const Ioss::GroupingEntity &grouping_entity) |
| 275 |
{ |
| 276 |
std::string grouping_entity_name; |
| 277 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 278 |
grouping_entity_name = ""; |
| 279 |
} |
| 280 |
else { |
| 281 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 282 |
} |
| 283 |
|
| 284 |
return kelpie::Key(std::to_string(rank), |
| 285 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 286 |
grouping_entity.type_string() + grouping_entity_name + "*"); |
| 287 |
} |
| 288 |
|
| 289 |
kelpie::Key property_search_key(int rank, const Ioss::Region ®ion, |
| 290 |
const Ioss::GroupingEntity &grouping_entity) |
| 291 |
{ |
| 292 |
std::string grouping_entity_name; |
| 293 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 294 |
grouping_entity_name = ""; |
| 295 |
} |
| 296 |
else { |
| 297 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 298 |
} |
| 299 |
|
| 300 |
return kelpie::Key(std::to_string(rank), |
| 301 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 302 |
grouping_entity.type_string() + grouping_entity_name + "/Property/*"); |
| 303 |
} |
| 304 |
|
| 305 |
kelpie::Key make_property_key(int rank, const Ioss::Region ®ion, |
| 306 |
const std::string &entity_type, const std::string &entity_name, |
| 307 |
const std::string &property_type, const std::string &property_name) |
| 308 |
{ |
| 309 |
std::string grouping_entity_name; |
| 310 |
if (entity_type == "Region") { |
| 311 |
grouping_entity_name = ""; |
| 312 |
} |
| 313 |
else { |
| 314 |
grouping_entity_name = "/Name/" + entity_name; |
| 315 |
} |
| 316 |
|
| 317 |
return kelpie::Key(std::to_string(rank), |
| 318 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 319 |
entity_type + grouping_entity_name + "/Property/BasicType/" + |
| 320 |
property_type + "/Name/" + property_name); |
| 321 |
} |
| 322 |
|
| 323 |
kelpie::Key field_search_key(int rank, const Ioss::Region ®ion, |
| 324 |
const Ioss::GroupingEntity &grouping_entity) |
| 325 |
{ |
| 326 |
std::string grouping_entity_name; |
| 327 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 328 |
grouping_entity_name = ""; |
| 329 |
} |
| 330 |
else { |
| 331 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 332 |
} |
| 333 |
|
| 334 |
return kelpie::Key(std::to_string(rank), |
| 335 |
"/State/" + std::to_string(region.get_current_state()) + "/Entity/" + |
| 336 |
grouping_entity.type_string() + grouping_entity_name + "/Field/*"); |
| 337 |
} |
| 338 |
|
| 339 |
kelpie::Key field_search_key(int rank, int state, const Ioss::Region ®ion, |
| 340 |
const Ioss::GroupingEntity &grouping_entity) |
| 341 |
{ |
| 342 |
// The default Region state is -1. When the mesh data is being read from Faodel, the state |
| 343 |
// has not yet been updated. This version of the function allows the caller to specify which |
| 344 |
// state it should be reading keys for. This is required to allow Iofaodel::DatabaseIO to |
| 345 |
// identify the TRANSIENT fields in the Faodel key structure. |
| 346 |
|
| 347 |
std::string grouping_entity_name; |
| 348 |
if (grouping_entity.type() == Ioss::EntityType::REGION) { |
| 349 |
grouping_entity_name = ""; |
| 350 |
} |
| 351 |
else { |
| 352 |
grouping_entity_name = "/Name/" + grouping_entity.name(); |
| 353 |
} |
| 354 |
|
| 355 |
return kelpie::Key(std::to_string(rank), "/State/" + std::to_string(state) + "/Entity/" + |
| 356 |
grouping_entity.type_string() + |
| 357 |
grouping_entity_name + "/Field/*"); |
| 358 |
} |
| 359 |
|
| 360 |
std::string to_string(const Ioss::Property::BasicType &t) |
| 361 |
{ |
| 362 |
switch (t) { |
| 363 |
case Ioss::Property::BasicType::INVALID: return std::string("INVALID"); |
| 364 |
case Ioss::Property::BasicType::REAL: return std::string("REAL"); |
| 365 |
case Ioss::Property::BasicType::INTEGER: return std::string("INTEGER"); |
| 366 |
case Ioss::Property::BasicType::POINTER: return std::string("POINTER"); |
| 367 |
case Ioss::Property::BasicType::STRING: return std::string("STRING"); |
| 368 |
default: return std::string("INVALID"); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
std::string to_string(const Ioss::Field::BasicType &t) |
| 373 |
{ |
| 374 |
// INTEGER == INT32 |
| 375 |
// DOUBLE == REAL |
| 376 |
switch (t) { |
| 377 |
case Ioss::Field::BasicType::INVALID: return std::string("INVALID"); |
| 378 |
case Ioss::Field::BasicType::REAL: return std::string("REAL"); |
| 379 |
case Ioss::Field::BasicType::INTEGER: return std::string("INTEGER"); |
| 380 |
case Ioss::Field::BasicType::INT64: return std::string("INT64"); |
| 381 |
case Ioss::Field::BasicType::COMPLEX: return std::string("COMPLEX,"); |
| 382 |
case Ioss::Field::BasicType::STRING: return std::string("STRING,"); |
| 383 |
case Ioss::Field::BasicType::CHARACTER: return std::string("CHARACTER"); |
| 384 |
default: return std::string("INVALID"); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
std::string to_string(const Ioss::Field::RoleType &t) |
| 389 |
{ |
| 390 |
// INTEGER == INT32 |
| 391 |
// DOUBLE == REAL |
| 392 |
switch (t) { |
| 393 |
case Ioss::Field::RoleType::INTERNAL: return std::string("INTERNAL"); |
| 394 |
case Ioss::Field::RoleType::MESH: return std::string("MESH"); |
| 395 |
case Ioss::Field::RoleType::ATTRIBUTE: return std::string("ATTRIBUTE"); |
| 396 |
case Ioss::Field::RoleType::COMMUNICATION: return std::string("COMMUNICATION"); |
| 397 |
case Ioss::Field::RoleType::INFORMATION: return std::string("INFORMATION"); |
| 398 |
case Ioss::Field::RoleType::REDUCTION: return std::string("REDUCTION"); |
| 399 |
case Ioss::Field::RoleType::TRANSIENT: return std::string("TRANSIENT"); |
| 400 |
default: return std::string("INVALID"); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
std::string to_string(const Ioss::EntityType &t) { return Ioss::Utils::entity_type_to_string(t); } |
| 405 |
|
| 406 |
std::string get_entity_name(const kelpie::Key &k, const std::string &target) |
| 407 |
{ |
| 408 |
std::string name; |
| 409 |
|
| 410 |
std::string front(target + "/"); |
| 411 |
auto begin = k.K2().find(front); |
| 412 |
if (begin != std::string::npos) { |
| 413 |
name = k.K2().substr(begin + front.size()); |
| 414 |
} |
| 415 |
|
| 416 |
return name; |
| 417 |
} |
| 418 |
|
| 419 |
std::set<std::string> get_entity_names(const std::vector<kelpie::Key> &keys, |
| 420 |
const std::string &target) |
| 421 |
{ |
| 422 |
std::set<std::string> names; |
| 423 |
for (auto k : keys) { |
| 424 |
std::string front("Entity/" + target + "/Name/"); |
| 425 |
auto begin = k.K2().find(front); |
| 426 |
if (begin != std::string::npos) { |
| 427 |
|
| 428 |
{ |
| 429 |
std::string back = std::string("/Property/BasicType"); |
| 430 |
auto end = k.K2().find(back); |
| 431 |
if (end != std::string::npos) { |
| 432 |
auto name = k.K2().substr(begin + front.size(), end - begin - front.size()); |
| 433 |
names.insert(name); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
{ |
| 438 |
std::string back = std::string("/Field/RoleType"); |
| 439 |
auto end = k.K2().find(back); |
| 440 |
if (end != std::string::npos) { |
| 441 |
auto name = k.K2().substr(begin + front.size(), end - begin - front.size()); |
| 442 |
names.insert(name); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
return names; |
| 449 |
} |
| 450 |
|
| 451 |
} // namespace Iofaodel |