| 1 |
// Copyright(C) 1999-2024 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 "Ioss_CodeTypes.h" |
| 8 |
#include "Ioss_ParallelUtils.h" |
| 9 |
#include "Ioss_SmartAssert.h" |
| 10 |
#include "Ioss_Utils.h" |
| 11 |
#include "exonull/Ioexnl_DatabaseIO.h" |
| 12 |
#include "exonull/Ioexnl_Internals.h" |
| 13 |
#include "exonull/Ioexnl_Utils.h" |
| 14 |
#include <array> |
| 15 |
#include <cassert> |
| 16 |
#include <ctime> |
| 17 |
#include <exodusII.h> |
| 18 |
#include <fmt/ostream.h> |
| 19 |
#include <map> |
| 20 |
#include <sstream> |
| 21 |
#include <stdexcept> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "Ioexnl_BaseDatabaseIO.h" |
| 26 |
#include "Ioss_Assembly.h" |
| 27 |
#include "Ioss_Blob.h" |
| 28 |
#include "Ioss_CommSet.h" |
| 29 |
#include "Ioss_DBUsage.h" |
| 30 |
#include "Ioss_EdgeBlock.h" |
| 31 |
#include "Ioss_EdgeSet.h" |
| 32 |
#include "Ioss_ElementBlock.h" |
| 33 |
#include "Ioss_ElementSet.h" |
| 34 |
#include "Ioss_ElementTopology.h" |
| 35 |
#include "Ioss_EntitySet.h" |
| 36 |
#include "Ioss_EntityType.h" |
| 37 |
#include "Ioss_FaceBlock.h" |
| 38 |
#include "Ioss_FaceSet.h" |
| 39 |
#include "Ioss_Field.h" |
| 40 |
#include "Ioss_GroupingEntity.h" |
| 41 |
#include "Ioss_Map.h" |
| 42 |
#include "Ioss_NodeBlock.h" |
| 43 |
#include "Ioss_NodeSet.h" |
| 44 |
#include "Ioss_Property.h" |
| 45 |
#include "Ioss_PropertyManager.h" |
| 46 |
#include "Ioss_Region.h" |
| 47 |
#include "Ioss_SideBlock.h" |
| 48 |
#include "Ioss_SideSet.h" |
| 49 |
#include "Ioss_State.h" |
| 50 |
#include "Ioss_VariableType.h" |
| 51 |
|
| 52 |
// ======================================================================== |
| 53 |
// Static internal helper functions |
| 54 |
// ======================================================================== |
| 55 |
namespace { |
| 56 |
const size_t max_line_length = MAX_LINE_LENGTH; |
| 57 |
|
| 58 |
template <typename T> |
| 59 |
void compute_internal_border_maps(T *entities, T *internal, size_t count, size_t entity_count) |
| 60 |
{ |
| 61 |
// Construct the node/element map (internal vs. border). |
| 62 |
// Border nodes/elements are those in the communication map (use entities array) |
| 63 |
// Internal nodes/elements are the rest. Allocate array to hold all nodes/elements, |
| 64 |
// initialize all to '1', then zero out the nodes/elements in 'entities'. |
| 65 |
// Iterate through array again and consolidate all '1's |
| 66 |
for (size_t ij = 0; ij < count; ij++) { |
| 67 |
internal[ij] = 1; |
| 68 |
} |
| 69 |
for (size_t J = 0; J < entity_count; J++) { |
| 70 |
internal[entities[J] - 1] = 0; |
| 71 |
} |
| 72 |
|
| 73 |
size_t b = 0; |
| 74 |
for (size_t ij = 0; ij < count; ij++) { |
| 75 |
if (internal[ij] == 0) { |
| 76 |
entities[b++] = ij + 1; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
size_t k = 0; |
| 81 |
for (size_t ij = 0; ij < count; ij++) { |
| 82 |
if (internal[ij] == 1) { |
| 83 |
internal[k++] = ij + 1; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
template <typename T> |
| 89 |
void extract_data(std::vector<double> &local_data, T *data, size_t num_entity, size_t comp_count, |
| 90 |
size_t offset) |
| 91 |
{ |
| 92 |
local_data.resize(num_entity); |
| 93 |
if (comp_count == 1 && offset == 0) { |
| 94 |
for (size_t j = 0; j < num_entity; j++) { |
| 95 |
local_data[j] = data[j]; |
| 96 |
} |
| 97 |
} |
| 98 |
else { |
| 99 |
for (size_t j = 0; j < num_entity; j++) { |
| 100 |
local_data[j] = data[offset]; |
| 101 |
offset += comp_count; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
} // namespace |
| 107 |
|
| 108 |
namespace Ioexnl { |
| 109 |
DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename, |
| 110 |
Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator, |
| 111 |
const Ioss::PropertyManager &props) |
| 112 |
: Ioexnl::BaseDatabaseIO(region, filename, db_usage, communicator, props) |
| 113 |
{ |
| 114 |
} |
| 115 |
|
| 116 |
bool DatabaseIO::check_valid_file_ptr(bool, std::string *, int *, bool) const { return true; } |
| 117 |
|
| 118 |
bool DatabaseIO::handle_output_file(bool, std::string *, int *, bool, bool) const { return true; } |
| 119 |
|
| 120 |
int DatabaseIO::get_file_pointer() const { return 0; } |
| 121 |
|
| 122 |
void DatabaseIO::read_meta_data_nl() {} |
| 123 |
|
| 124 |
void DatabaseIO::read_region() {} |
| 125 |
|
| 126 |
void DatabaseIO::get_step_times_nl() {} |
| 127 |
|
| 128 |
int64_t DatabaseIO::write_attribute_field(const Ioss::Field &field, |
| 129 |
const Ioss::GroupingEntity *ge, void *data) const |
| 130 |
{ |
| 131 |
int64_t num_entity = ge->entity_count(); |
| 132 |
int64_t fld_offset = field.get_index(); |
| 133 |
|
| 134 |
int attribute_count = ge->get_property("attribute_count").get_int(); |
| 135 |
assert(fld_offset > 0); |
| 136 |
assert(fld_offset - 1 + field.get_component_count(Ioss::Field::InOut::OUTPUT) <= |
| 137 |
attribute_count); |
| 138 |
|
| 139 |
Ioss::Field::BasicType ioss_type = field.get_type(); |
| 140 |
assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER || |
| 141 |
ioss_type == Ioss::Field::INT64); |
| 142 |
|
| 143 |
if (ioss_type == Ioss::Field::INT64) { |
| 144 |
Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)data, num_entity); |
| 145 |
} |
| 146 |
|
| 147 |
if (fld_offset == 1 && |
| 148 |
field.get_component_count(Ioss::Field::InOut::OUTPUT) == attribute_count) { |
| 149 |
// Write all attributes in one big chunk... |
| 150 |
std::vector<double> temp; |
| 151 |
if (ioss_type == Ioss::Field::INTEGER) { |
| 152 |
int *idata = static_cast<int *>(data); |
| 153 |
extract_data(temp, idata, attribute_count * num_entity, 1, 0); |
| 154 |
} |
| 155 |
else if (ioss_type == Ioss::Field::INT64) { |
| 156 |
auto *idata = static_cast<int64_t *>(data); |
| 157 |
extract_data(temp, idata, attribute_count * num_entity, 1, 0); |
| 158 |
} |
| 159 |
} |
| 160 |
else { |
| 161 |
// Write a subset of the attributes. If scalar, write one; |
| 162 |
// if higher-order (vector3d, ..) write each component. |
| 163 |
if (field.get_component_count(Ioss::Field::InOut::OUTPUT) == 1) { |
| 164 |
std::vector<double> temp; |
| 165 |
if (ioss_type == Ioss::Field::INTEGER) { |
| 166 |
int *idata = static_cast<int *>(data); |
| 167 |
extract_data(temp, idata, num_entity, 1, 0); |
| 168 |
} |
| 169 |
else if (ioss_type == Ioss::Field::INT64) { |
| 170 |
auto *idata = static_cast<int64_t *>(data); |
| 171 |
extract_data(temp, idata, num_entity, 1, 0); |
| 172 |
} |
| 173 |
} |
| 174 |
else { |
| 175 |
// Multi-component... Need a local memory space to push |
| 176 |
// data into and then write that out to the file... |
| 177 |
std::vector<double> local_data(num_entity); |
| 178 |
int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT); |
| 179 |
for (int i = 0; i < comp_count; i++) { |
| 180 |
size_t offset = i; |
| 181 |
if (ioss_type == Ioss::Field::REAL) { |
| 182 |
auto *rdata = static_cast<double *>(data); |
| 183 |
extract_data(local_data, rdata, num_entity, comp_count, offset); |
| 184 |
} |
| 185 |
else if (ioss_type == Ioss::Field::INTEGER) { |
| 186 |
int *idata = static_cast<int *>(data); |
| 187 |
extract_data(local_data, idata, num_entity, comp_count, offset); |
| 188 |
} |
| 189 |
else if (ioss_type == Ioss::Field::INT64) { |
| 190 |
auto *idata = static_cast<int64_t *>(data); |
| 191 |
extract_data(local_data, idata, num_entity, comp_count, offset); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
return num_entity; |
| 197 |
} |
| 198 |
|
| 199 |
int64_t DatabaseIO::put_field_internal(const Ioss::Region *reg, const Ioss::Field &field, |
| 200 |
void *data, size_t data_size) const |
| 201 |
{ |
| 202 |
return Ioexnl::BaseDatabaseIO::put_field_internal(reg, field, data, data_size); |
| 203 |
} |
| 204 |
|
| 205 |
int64_t DatabaseIO::put_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field, |
| 206 |
void *data, size_t data_size) const |
| 207 |
{ |
| 208 |
{ |
| 209 |
size_t num_to_get = field.verify(data_size); |
| 210 |
if (num_to_get > 0) { |
| 211 |
|
| 212 |
Ioss::Field::RoleType role = field.get_role(); |
| 213 |
|
| 214 |
if (role == Ioss::Field::MESH) { |
| 215 |
if (field.get_name() == "mesh_model_coordinates_x") {} |
| 216 |
|
| 217 |
else if (field.get_name() == "mesh_model_coordinates_y") { |
| 218 |
} |
| 219 |
|
| 220 |
else if (field.get_name() == "mesh_model_coordinates_z") { |
| 221 |
} |
| 222 |
|
| 223 |
else if (field.get_name() == "mesh_model_coordinates") { |
| 224 |
// Data required by upper classes store x0, y0, z0, ... xn, yn, zn |
| 225 |
// Data stored in exodus file is x0, ..., xn, y0, ..., yn, z0, ..., zn |
| 226 |
// so we have to allocate some scratch memory to read in the data |
| 227 |
// and then map into supplied 'data' |
| 228 |
std::vector<double> x; |
| 229 |
x.reserve(num_to_get); |
| 230 |
std::vector<double> y; |
| 231 |
if (spatialDimension > 1) { |
| 232 |
y.reserve(num_to_get); |
| 233 |
} |
| 234 |
std::vector<double> z; |
| 235 |
if (spatialDimension == 3) { |
| 236 |
z.reserve(num_to_get); |
| 237 |
} |
| 238 |
|
| 239 |
// Cast 'data' to correct size -- double |
| 240 |
auto *rdata = static_cast<double *>(data); |
| 241 |
|
| 242 |
size_t index = 0; |
| 243 |
for (size_t i = 0; i < num_to_get; i++) { |
| 244 |
x.push_back(rdata[index++]); |
| 245 |
if (spatialDimension > 1) { |
| 246 |
y.push_back(rdata[index++]); |
| 247 |
} |
| 248 |
if (spatialDimension == 3) { |
| 249 |
z.push_back(rdata[index++]); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
else if (field.get_name() == "ids") { |
| 254 |
// The ids coming in are the global ids; their position is the |
| 255 |
// local id -1 (That is, data[0] contains the global id of local |
| 256 |
// node 1) |
| 257 |
handle_node_ids(data, num_to_get); |
| 258 |
} |
| 259 |
else if (field.get_name() == "connectivity") { |
| 260 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 261 |
} |
| 262 |
else if (field.get_name() == "connectivity_raw") { |
| 263 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 264 |
} |
| 265 |
else if (field.get_name() == "node_connectivity_status") { |
| 266 |
// Do nothing, input only field. |
| 267 |
} |
| 268 |
else if (field.get_name() == "implicit_ids") { |
| 269 |
// Do nothing, input only field. |
| 270 |
} |
| 271 |
else { |
| 272 |
return Ioss::Utils::field_warning(nb, field, "mesh output"); |
| 273 |
} |
| 274 |
} |
| 275 |
else if (role == Ioss::Field::TRANSIENT) { |
| 276 |
// Check if the specified field exists on this node block. |
| 277 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 278 |
// exist on the database as scalars with the appropriate |
| 279 |
// extensions. |
| 280 |
|
| 281 |
// Transfer each component of the variable into 'data' and then |
| 282 |
// output. Need temporary storage area of size 'number of |
| 283 |
// nodes in this block. |
| 284 |
write_nodal_transient_field(field, nb, num_to_get, data); |
| 285 |
} |
| 286 |
else if (role == Ioss::Field::REDUCTION) { |
| 287 |
store_reduction_field(field, nb, data); |
| 288 |
} |
| 289 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 290 |
num_to_get = write_attribute_field(field, nb, data); |
| 291 |
} |
| 292 |
} |
| 293 |
return num_to_get; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
int64_t DatabaseIO::put_field_internal(const Ioss::Blob *blob, const Ioss::Field &field, |
| 298 |
void *data, size_t data_size) const |
| 299 |
{ |
| 300 |
{ |
| 301 |
size_t num_to_get = field.verify(data_size); |
| 302 |
if (num_to_get > 0) { |
| 303 |
|
| 304 |
Ioss::Field::RoleType role = field.get_role(); |
| 305 |
|
| 306 |
if (role == Ioss::Field::MESH) { |
| 307 |
if (field.get_name() == "ids") { |
| 308 |
// The ids coming in are the global ids; their position is the |
| 309 |
// local id -1 (That is, data[0] contains the global id of local |
| 310 |
// node 1) |
| 311 |
// handle_node_ids(data, num_to_get); |
| 312 |
} |
| 313 |
else if (field.get_name() == "connectivity") { |
| 314 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 315 |
} |
| 316 |
else if (field.get_name() == "connectivity_raw") { |
| 317 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 318 |
} |
| 319 |
else if (field.get_name() == "node_connectivity_status") { |
| 320 |
// Do nothing, input only field. |
| 321 |
} |
| 322 |
else if (field.get_name() == "implicit_ids") { |
| 323 |
// Do nothing, input only field. |
| 324 |
} |
| 325 |
else { |
| 326 |
return Ioss::Utils::field_warning(blob, field, "mesh output"); |
| 327 |
} |
| 328 |
} |
| 329 |
else if (role == Ioss::Field::TRANSIENT) { |
| 330 |
// Check if the specified field exists on this node block. |
| 331 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 332 |
// exist on the database as scalars with the appropriate |
| 333 |
// extensions. |
| 334 |
|
| 335 |
// Transfer each component of the variable into 'data' and then |
| 336 |
// output. Need temporary storage area of size 'number of |
| 337 |
// nodes in this block. |
| 338 |
write_entity_transient_field(field, blob, num_to_get, data); |
| 339 |
} |
| 340 |
else if (role == Ioss::Field::REDUCTION) { |
| 341 |
store_reduction_field(field, blob, data); |
| 342 |
} |
| 343 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 344 |
num_to_get = write_attribute_field(field, blob, data); |
| 345 |
} |
| 346 |
} |
| 347 |
return num_to_get; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
int64_t DatabaseIO::put_field_internal(const Ioss::Assembly *assembly, const Ioss::Field &field, |
| 352 |
void *data, size_t data_size) const |
| 353 |
{ |
| 354 |
{ |
| 355 |
size_t num_to_get = field.verify(data_size); |
| 356 |
if (num_to_get > 0) { |
| 357 |
|
| 358 |
Ioss::Field::RoleType role = field.get_role(); |
| 359 |
|
| 360 |
if (role == Ioss::Field::MESH) { |
| 361 |
if (field.get_name() == "ids") { |
| 362 |
// The ids coming in are the global ids; their position is the |
| 363 |
// local id -1 (That is, data[0] contains the global id of local |
| 364 |
// node 1) |
| 365 |
// handle_node_ids(data, num_to_get); |
| 366 |
} |
| 367 |
else if (field.get_name() == "connectivity") { |
| 368 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 369 |
} |
| 370 |
else if (field.get_name() == "connectivity_raw") { |
| 371 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 372 |
} |
| 373 |
else if (field.get_name() == "node_connectivity_status") { |
| 374 |
// Do nothing, input only field. |
| 375 |
} |
| 376 |
else if (field.get_name() == "implicit_ids") { |
| 377 |
// Do nothing, input only field. |
| 378 |
} |
| 379 |
else { |
| 380 |
return Ioss::Utils::field_warning(assembly, field, "mesh output"); |
| 381 |
} |
| 382 |
} |
| 383 |
else if (role == Ioss::Field::TRANSIENT) { |
| 384 |
// Check if the specified field exists on this node block. |
| 385 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 386 |
// exist on the database as scalars with the appropriate |
| 387 |
// extensions. |
| 388 |
|
| 389 |
// Transfer each component of the variable into 'data' and then |
| 390 |
// output. Need temporary storage area of size 'number of |
| 391 |
// nodes in this block. |
| 392 |
write_entity_transient_field(field, assembly, num_to_get, data); |
| 393 |
} |
| 394 |
else if (role == Ioss::Field::REDUCTION) { |
| 395 |
store_reduction_field(field, assembly, data); |
| 396 |
} |
| 397 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 398 |
num_to_get = write_attribute_field(field, assembly, data); |
| 399 |
} |
| 400 |
} |
| 401 |
return num_to_get; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
int64_t DatabaseIO::put_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field, |
| 406 |
void *data, size_t data_size) const |
| 407 |
{ |
| 408 |
{ |
| 409 |
size_t num_to_get = field.verify(data_size); |
| 410 |
|
| 411 |
if (num_to_get > 0) { |
| 412 |
// Get the element block id and element count |
| 413 |
size_t my_element_count = eb->entity_count(); |
| 414 |
Ioss::Field::RoleType role = field.get_role(); |
| 415 |
|
| 416 |
if (role == Ioss::Field::MESH) { |
| 417 |
// Handle the MESH fields required for an Exodus file model. |
| 418 |
// (The 'genesis' portion) |
| 419 |
if (field.get_name() == "connectivity") { |
| 420 |
if (my_element_count > 0) { |
| 421 |
// Map element connectivity from global node id to local node id. |
| 422 |
int element_nodes = eb->topology()->number_nodes(); |
| 423 |
nodeMap.reverse_map_data(data, field, num_to_get * element_nodes); |
| 424 |
} |
| 425 |
} |
| 426 |
else if (field.get_name() == "connectivity_edge") { |
| 427 |
if (my_element_count > 0) { |
| 428 |
// Map element connectivity from global edge id to local edge id. |
| 429 |
int element_edges = field.transformed_storage()->component_count(); |
| 430 |
edgeMap.reverse_map_data(data, field, num_to_get * element_edges); |
| 431 |
} |
| 432 |
} |
| 433 |
else if (field.get_name() == "connectivity_face") { |
| 434 |
if (my_element_count > 0) { |
| 435 |
// Map element connectivity from global face id to local face id. |
| 436 |
int element_faces = field.transformed_storage()->component_count(); |
| 437 |
faceMap.reverse_map_data(data, field, num_to_get * element_faces); |
| 438 |
} |
| 439 |
} |
| 440 |
else if (field.get_name() == "connectivity_raw") { |
| 441 |
if (my_element_count > 0) { |
| 442 |
// Element connectivity is already in local node id. |
| 443 |
} |
| 444 |
} |
| 445 |
else if (field.get_name() == "ids") { |
| 446 |
handle_element_ids(eb, data, num_to_get); |
| 447 |
} |
| 448 |
else if (field.get_name() == "implicit_ids") { |
| 449 |
// Do nothing, input only field. |
| 450 |
} |
| 451 |
} |
| 452 |
else if (role == Ioss::Field::MAP) { |
| 453 |
int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT); |
| 454 |
for (int comp = 0; comp < comp_count; comp++) { |
| 455 |
std::vector<char> component(my_element_count * int_byte_size_api()); |
| 456 |
|
| 457 |
if (int_byte_size_api() == 4) { |
| 458 |
int *data32 = reinterpret_cast<int *>(data); |
| 459 |
int *comp32 = reinterpret_cast<int *>(Data(component)); |
| 460 |
|
| 461 |
int index = comp; |
| 462 |
for (size_t i = 0; i < my_element_count; i++) { |
| 463 |
comp32[i] = data32[index]; |
| 464 |
index += comp_count; |
| 465 |
} |
| 466 |
} |
| 467 |
else { |
| 468 |
auto *data64 = reinterpret_cast<int64_t *>(data); |
| 469 |
auto *comp64 = reinterpret_cast<int64_t *>(Data(component)); |
| 470 |
|
| 471 |
int index = comp; |
| 472 |
for (size_t i = 0; i < my_element_count; i++) { |
| 473 |
comp64[i] = data64[index]; |
| 474 |
index += comp_count; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 480 |
num_to_get = write_attribute_field(field, eb, data); |
| 481 |
} |
| 482 |
else if (role == Ioss::Field::TRANSIENT) { |
| 483 |
// Check if the specified field exists on this element block. |
| 484 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 485 |
// exist on the database as scalars with the appropriate |
| 486 |
// extensions. |
| 487 |
|
| 488 |
// Transfer each component of the variable into 'data' and then |
| 489 |
// output. Need temporary storage area of size 'number of |
| 490 |
// elements in this block. |
| 491 |
write_entity_transient_field(field, eb, my_element_count, data); |
| 492 |
} |
| 493 |
else if (role == Ioss::Field::REDUCTION) { |
| 494 |
store_reduction_field(field, eb, data); |
| 495 |
} |
| 496 |
} |
| 497 |
return num_to_get; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
int64_t DatabaseIO::put_field_internal(const Ioss::FaceBlock *eb, const Ioss::Field &field, |
| 502 |
void *data, size_t data_size) const |
| 503 |
{ |
| 504 |
{ |
| 505 |
size_t num_to_get = field.verify(data_size); |
| 506 |
|
| 507 |
if (num_to_get > 0) { |
| 508 |
// Get the face block id and face count |
| 509 |
int64_t my_face_count = eb->entity_count(); |
| 510 |
Ioss::Field::RoleType role = field.get_role(); |
| 511 |
|
| 512 |
if (role == Ioss::Field::MESH) { |
| 513 |
// Handle the MESH fields required for an Exodus file model. |
| 514 |
// (The 'genesis' portion) |
| 515 |
if (field.get_name() == "connectivity") { |
| 516 |
if (my_face_count > 0) { |
| 517 |
// Map face connectivity from global node id to local node id. |
| 518 |
// Do it in 'data' ... |
| 519 |
int face_nodes = eb->topology()->number_nodes(); |
| 520 |
nodeMap.reverse_map_data(data, field, num_to_get * face_nodes); |
| 521 |
} |
| 522 |
} |
| 523 |
else if (field.get_name() == "connectivity_edge") { |
| 524 |
if (my_face_count > 0) { |
| 525 |
// Map face connectivity from global edge id to local edge id. |
| 526 |
int face_edges = field.transformed_storage()->component_count(); |
| 527 |
edgeMap.reverse_map_data(data, field, num_to_get * face_edges); |
| 528 |
} |
| 529 |
} |
| 530 |
else if (field.get_name() == "connectivity_raw") { |
| 531 |
// Do nothing, input only field. |
| 532 |
} |
| 533 |
else if (field.get_name() == "ids") { |
| 534 |
handle_face_ids(eb, data, num_to_get); |
| 535 |
} |
| 536 |
else { |
| 537 |
num_to_get = Ioss::Utils::field_warning(eb, field, "mesh output"); |
| 538 |
} |
| 539 |
} |
| 540 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 541 |
num_to_get = write_attribute_field(field, eb, data); |
| 542 |
} |
| 543 |
else if (role == Ioss::Field::TRANSIENT) { |
| 544 |
// Check if the specified field exists on this face block. |
| 545 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 546 |
// exist on the database as scalars with the appropriate |
| 547 |
// extensions. |
| 548 |
|
| 549 |
// Transfer each component of the variable into 'data' and then |
| 550 |
// output. Need temporary storage area of size 'number of |
| 551 |
// faces in this block. |
| 552 |
write_entity_transient_field(field, eb, my_face_count, data); |
| 553 |
} |
| 554 |
else if (role == Ioss::Field::REDUCTION) { |
| 555 |
store_reduction_field(field, eb, data); |
| 556 |
} |
| 557 |
} |
| 558 |
return num_to_get; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
int64_t DatabaseIO::put_field_internal(const Ioss::EdgeBlock *eb, const Ioss::Field &field, |
| 563 |
void *data, size_t data_size) const |
| 564 |
{ |
| 565 |
{ |
| 566 |
size_t num_to_get = field.verify(data_size); |
| 567 |
|
| 568 |
if (num_to_get > 0) { |
| 569 |
// Get the edge block id and edge count |
| 570 |
int64_t my_edge_count = eb->entity_count(); |
| 571 |
Ioss::Field::RoleType role = field.get_role(); |
| 572 |
|
| 573 |
if (role == Ioss::Field::MESH) { |
| 574 |
// Handle the MESH fields required for an Exodus file model. (The 'genesis' portion) |
| 575 |
if (field.get_name() == "connectivity") { |
| 576 |
if (my_edge_count > 0) { |
| 577 |
// Map edge connectivity from global node id to local node id. |
| 578 |
// Do it in 'data' ... |
| 579 |
int edge_nodes = eb->topology()->number_nodes(); |
| 580 |
nodeMap.reverse_map_data(data, field, num_to_get * edge_nodes); |
| 581 |
} |
| 582 |
} |
| 583 |
else if (field.get_name() == "connectivity_raw") { |
| 584 |
// Do nothing, input only field. |
| 585 |
} |
| 586 |
else if (field.get_name() == "ids") { |
| 587 |
handle_edge_ids(eb, data, num_to_get); |
| 588 |
} |
| 589 |
else { |
| 590 |
num_to_get = Ioss::Utils::field_warning(eb, field, "mesh output"); |
| 591 |
} |
| 592 |
} |
| 593 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 594 |
num_to_get = write_attribute_field(field, eb, data); |
| 595 |
} |
| 596 |
else if (role == Ioss::Field::TRANSIENT) { |
| 597 |
// Check if the specified field exists on this edge block. |
| 598 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 599 |
// exist on the database as scalars with the appropriate |
| 600 |
// extensions. |
| 601 |
|
| 602 |
// Transfer each component of the variable into 'data' and then |
| 603 |
// output. Need temporary storage area of size 'number of |
| 604 |
// edges in this block. |
| 605 |
write_entity_transient_field(field, eb, my_edge_count, data); |
| 606 |
} |
| 607 |
else if (role == Ioss::Field::REDUCTION) { |
| 608 |
store_reduction_field(field, eb, data); |
| 609 |
} |
| 610 |
} |
| 611 |
return num_to_get; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
int64_t DatabaseIO::handle_node_ids(void *ids, int64_t num_to_get) const |
| 616 |
{ |
| 617 |
/*! |
| 618 |
* There are two modes we need to support in this routine: |
| 619 |
* 1. Initial definition of node map (local->global) and |
| 620 |
* nodeMap.reverse (global->local). |
| 621 |
* 2. Redefinition of node map via 'reordering' of the original |
| 622 |
* map when the nodes on this processor are the same, but their |
| 623 |
* order is changed (or count because of ghosting) |
| 624 |
* |
| 625 |
* So, there will be two maps the 'nodeMap.map' map is a 'direct lookup' |
| 626 |
* map which maps current local position to global id and the |
| 627 |
* 'nodeMap.reverse' is an associative lookup which maps the |
| 628 |
* global id to 'original local'. There is also a |
| 629 |
* 'nodeMap.reorder' which is direct lookup and maps current local |
| 630 |
* position to original local. |
| 631 |
|
| 632 |
* The ids coming in are the global ids; their position is the |
| 633 |
* "local id-1" (That is, data[0] contains the global id of local |
| 634 |
* node 1 in this node block). |
| 635 |
* |
| 636 |
* int local_position = nodeMap.reverse[NodeMap[i+1]] |
| 637 |
* (the nodeMap.map and nodeMap.reverse are 1-based) |
| 638 |
* |
| 639 |
* To determine which map to update on a call to this function, we |
| 640 |
* use the following heuristics: |
| 641 |
* -- If the database state is 'STATE_MODEL:', then update the |
| 642 |
* 'nodeMap.reverse' and 'nodeMap.map' |
| 643 |
* |
| 644 |
* -- If the database state is not STATE_MODEL, then leave the |
| 645 |
* 'nodeMap.reverse' and 'nodeMap.map' alone since they correspond to the |
| 646 |
* information already written to the database. [May want to add a |
| 647 |
* STATE_REDEFINE_MODEL] |
| 648 |
* |
| 649 |
* -- In both cases, update the nodeMap.reorder |
| 650 |
* |
| 651 |
* NOTE: The mapping is done on TRANSIENT fields only; MODEL fields |
| 652 |
* should be in the original order... |
| 653 |
*/ |
| 654 |
SMART_ASSERT(num_to_get == nodeCount)(num_to_get)(nodeCount); |
| 655 |
|
| 656 |
nodeMap.set_size(nodeCount); |
| 657 |
|
| 658 |
bool in_define = (dbState == Ioss::STATE_MODEL) || (dbState == Ioss::STATE_DEFINE_MODEL); |
| 659 |
if (int_byte_size_api() == 4) { |
| 660 |
nodeMap.set_map(static_cast<int *>(ids), num_to_get, 0, in_define); |
| 661 |
} |
| 662 |
else { |
| 663 |
nodeMap.set_map(static_cast<int64_t *>(ids), num_to_get, 0, in_define); |
| 664 |
} |
| 665 |
|
| 666 |
if (in_define) { |
| 667 |
// Only a single nodeblock and all set |
| 668 |
assert(get_region()->get_property("node_block_count").get_int() == 1); |
| 669 |
|
| 670 |
// Write to the database... |
| 671 |
} |
| 672 |
return num_to_get; |
| 673 |
} |
| 674 |
|
| 675 |
int64_t DatabaseIO::handle_element_ids(const Ioss::ElementBlock *eb, void *ids, |
| 676 |
size_t num_to_get) const |
| 677 |
{ |
| 678 |
elemMap.set_size(elementCount); |
| 679 |
size_t offset = eb->get_offset(); |
| 680 |
return handle_block_ids(eb, EX_ELEM_MAP, elemMap, ids, num_to_get, offset); |
| 681 |
} |
| 682 |
|
| 683 |
int64_t DatabaseIO::handle_face_ids(const Ioss::FaceBlock *eb, void *ids, size_t num_to_get) const |
| 684 |
{ |
| 685 |
faceMap.set_size(faceCount); |
| 686 |
size_t offset = eb->get_offset(); |
| 687 |
return handle_block_ids(eb, EX_FACE_MAP, faceMap, ids, num_to_get, offset); |
| 688 |
} |
| 689 |
|
| 690 |
int64_t DatabaseIO::handle_edge_ids(const Ioss::EdgeBlock *eb, void *ids, size_t num_to_get) const |
| 691 |
{ |
| 692 |
edgeMap.set_size(edgeCount); |
| 693 |
size_t offset = eb->get_offset(); |
| 694 |
return handle_block_ids(eb, EX_EDGE_MAP, edgeMap, ids, num_to_get, offset); |
| 695 |
} |
| 696 |
|
| 697 |
void DatabaseIO::write_nodal_transient_field(const Ioss::Field &field, |
| 698 |
const Ioss::NodeBlock * /* ge */, int64_t count, |
| 699 |
void *variables) const |
| 700 |
{ |
| 701 |
Ioss::Field::BasicType ioss_type = field.get_type(); |
| 702 |
assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER || |
| 703 |
ioss_type == Ioss::Field::INT64 || ioss_type == Ioss::Field::COMPLEX); |
| 704 |
|
| 705 |
if (ioss_type == Ioss::Field::INT64) { |
| 706 |
Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)variables, count); |
| 707 |
} |
| 708 |
|
| 709 |
// Note that if the field's basic type is COMPLEX, then each component of |
| 710 |
// the VariableType is a complex variable consisting of a real and |
| 711 |
// imaginary part. Since exodus cannot handle complex variables, |
| 712 |
// we have to output a (real and imaginary) X (number of |
| 713 |
// components) fields. For example, if V is a 3d vector of complex |
| 714 |
// data, the data in the 'variables' array are v_x, v.im_x, v_y, |
| 715 |
// v.im_y, v_z, v.im_z which need to be output in six separate |
| 716 |
// exodus fields. These fields were already defined in |
| 717 |
// "write_results_metadata". |
| 718 |
|
| 719 |
std::vector<double> temp(count); |
| 720 |
|
| 721 |
int step = get_current_state(); |
| 722 |
step = get_database_step(step); |
| 723 |
|
| 724 |
// get number of components, cycle through each component |
| 725 |
// and add suffix to base 'field_name'. Look up index |
| 726 |
// of this name in 'm_variables[EX_NODE_BLOCK]' map |
| 727 |
int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT); |
| 728 |
int re_im = 1; |
| 729 |
if (ioss_type == Ioss::Field::COMPLEX) { |
| 730 |
re_im = 2; |
| 731 |
} |
| 732 |
for (int complex_comp = 0; complex_comp < re_im; complex_comp++) { |
| 733 |
for (int i = 0; i < comp_count; i++) { |
| 734 |
std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1); |
| 735 |
|
| 736 |
auto var_iter = m_variables[EX_NODE_BLOCK].find(var_name); |
| 737 |
if (var_iter == m_variables[EX_NODE_BLOCK].end()) { |
| 738 |
std::ostringstream errmsg; |
| 739 |
fmt::print(errmsg, "ERROR: Could not find nodal variable '{}'\n", var_name); |
| 740 |
IOSS_ERROR(errmsg); |
| 741 |
} |
| 742 |
int var_index = var_iter->second; |
| 743 |
|
| 744 |
size_t begin_offset = (re_im * i) + complex_comp; |
| 745 |
size_t stride = re_im * comp_count; |
| 746 |
int64_t num_out = 0; |
| 747 |
|
| 748 |
if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) { |
| 749 |
num_out = nodeMap.map_field_to_db_scalar_order(static_cast<double *>(variables), temp, |
| 750 |
begin_offset, count, stride, 0); |
| 751 |
} |
| 752 |
else if (ioss_type == Ioss::Field::INTEGER) { |
| 753 |
num_out = nodeMap.map_field_to_db_scalar_order(static_cast<int *>(variables), temp, |
| 754 |
begin_offset, count, stride, 0); |
| 755 |
} |
| 756 |
else if (ioss_type == Ioss::Field::INT64) { |
| 757 |
num_out = nodeMap.map_field_to_db_scalar_order(static_cast<int64_t *>(variables), temp, |
| 758 |
begin_offset, count, stride, 0); |
| 759 |
} |
| 760 |
|
| 761 |
if (num_out != nodeCount) { |
| 762 |
std::ostringstream errmsg; |
| 763 |
fmt::print(errmsg, |
| 764 |
"ERROR: Problem outputting nodal variable '{}' with index = {} to file '{}'\n" |
| 765 |
"Should have output {} values, but instead only output {} values.\n", |
| 766 |
var_name, var_index, decoded_filename(), nodeCount, num_out); |
| 767 |
IOSS_ERROR(errmsg); |
| 768 |
} |
| 769 |
|
| 770 |
// Write the variable... |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
void DatabaseIO::write_entity_transient_field(const Ioss::Field &field, |
| 776 |
const Ioss::GroupingEntity *ge, int64_t count, |
| 777 |
void *variables) const |
| 778 |
{ |
| 779 |
static Ioss::Map non_element_map; // Used as an empty map for ge->type() != element block. |
| 780 |
std::vector<double> temp(count); |
| 781 |
|
| 782 |
int step = get_current_state(); |
| 783 |
step = get_database_step(step); |
| 784 |
|
| 785 |
Ioss::Map *map = nullptr; |
| 786 |
int64_t eb_offset = 0; |
| 787 |
if (ge->type() == Ioss::ELEMENTBLOCK) { |
| 788 |
const auto *elb = dynamic_cast<const Ioss::ElementBlock *>(ge); |
| 789 |
Ioss::Utils::check_dynamic_cast(elb); |
| 790 |
eb_offset = elb->get_offset(); |
| 791 |
map = &elemMap; |
| 792 |
} |
| 793 |
else { |
| 794 |
map = &non_element_map; |
| 795 |
} |
| 796 |
|
| 797 |
Ioss::Field::BasicType ioss_type = field.get_type(); |
| 798 |
assert(ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::INTEGER || |
| 799 |
ioss_type == Ioss::Field::INT64 || ioss_type == Ioss::Field::COMPLEX); |
| 800 |
|
| 801 |
if (ioss_type == Ioss::Field::INT64) { |
| 802 |
Ioss::Utils::check_int_to_real_overflow(field, (int64_t *)variables, count); |
| 803 |
} |
| 804 |
|
| 805 |
// Note that if the field's basic type is COMPLEX, then each component of |
| 806 |
// the VariableType is a complex variable consisting of a real and |
| 807 |
// imaginary part. Since exodus cannot handle complex variables, |
| 808 |
// we have to output a (real and imaginary) X (number of |
| 809 |
// components) fields. For example, if V is a 3d vector of complex |
| 810 |
// data, the data in the 'variables' array are v_x, v.im_x, v_y, |
| 811 |
// v.im_y, v_z, v.im_z which need to be output in six separate |
| 812 |
// exodus fields. These fields were already defined in |
| 813 |
// "write_results_metadata". |
| 814 |
|
| 815 |
// get number of components, cycle through each component |
| 816 |
// and add suffix to base 'field_name'. Look up index |
| 817 |
// of this name in 'm_variables[type]' map |
| 818 |
int comp_count = field.get_component_count(Ioss::Field::InOut::OUTPUT); |
| 819 |
|
| 820 |
// Handle quick easy, hopefully common case first... |
| 821 |
ex_entity_type type = Ioexnl::map_exodus_type(ge->type()); |
| 822 |
if (comp_count == 1 && ioss_type == Ioss::Field::REAL && type != EX_SIDE_SET && |
| 823 |
!map->reorders()) { |
| 824 |
// Simply output the variable... |
| 825 |
std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, 1); |
| 826 |
auto var_iter = m_variables[type].find(var_name); |
| 827 |
if (var_iter == m_variables[type].end()) { |
| 828 |
std::ostringstream errmsg; |
| 829 |
fmt::print(errmsg, "ERROR: Could not find field '{}'\n", var_name); |
| 830 |
IOSS_ERROR(errmsg); |
| 831 |
} |
| 832 |
return; |
| 833 |
} |
| 834 |
int re_im = 1; |
| 835 |
if (ioss_type == Ioss::Field::COMPLEX) { |
| 836 |
re_im = 2; |
| 837 |
} |
| 838 |
for (int complex_comp = 0; complex_comp < re_im; complex_comp++) { |
| 839 |
for (int i = 0; i < comp_count; i++) { |
| 840 |
std::string var_name = get_component_name(field, Ioss::Field::InOut::OUTPUT, i + 1); |
| 841 |
|
| 842 |
auto var_iter = m_variables[type].find(var_name); |
| 843 |
if (var_iter == m_variables[type].end()) { |
| 844 |
std::ostringstream errmsg; |
| 845 |
fmt::print(errmsg, "ERROR: Could not find field '{}'\n", var_name); |
| 846 |
IOSS_ERROR(errmsg); |
| 847 |
} |
| 848 |
// var is a [count,comp,re_im] array; re_im = 1(real) or 2(complex) |
| 849 |
// beg_offset = (re_im*i)+complex_comp |
| 850 |
// number_values = count |
| 851 |
// stride = re_im*comp_count |
| 852 |
int64_t begin_offset = (re_im * i) + complex_comp; |
| 853 |
int64_t stride = re_im * comp_count; |
| 854 |
|
| 855 |
if (ioss_type == Ioss::Field::REAL || ioss_type == Ioss::Field::COMPLEX) { |
| 856 |
map->map_field_to_db_scalar_order(static_cast<double *>(variables), temp, begin_offset, |
| 857 |
count, stride, eb_offset); |
| 858 |
} |
| 859 |
else if (ioss_type == Ioss::Field::INTEGER) { |
| 860 |
map->map_field_to_db_scalar_order(static_cast<int *>(variables), temp, begin_offset, |
| 861 |
count, stride, eb_offset); |
| 862 |
} |
| 863 |
else if (ioss_type == Ioss::Field::INT64) { |
| 864 |
map->map_field_to_db_scalar_order(static_cast<int64_t *>(variables), temp, begin_offset, |
| 865 |
count, stride, eb_offset); |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
int64_t DatabaseIO::put_Xset_field_internal(const Ioss::EntitySet *ns, const Ioss::Field &field, |
| 872 |
void *data, size_t data_size) const |
| 873 |
{ |
| 874 |
{ |
| 875 |
size_t entity_count = ns->entity_count(); |
| 876 |
size_t num_to_get = field.verify(data_size); |
| 877 |
if (num_to_get > 0) { |
| 878 |
|
| 879 |
Ioss::Field::RoleType role = field.get_role(); |
| 880 |
|
| 881 |
if (role == Ioss::Field::MESH) { |
| 882 |
|
| 883 |
if (field.get_name() == "ids" || field.get_name() == "ids_raw") { |
| 884 |
// Map node id from global node id to local node id. |
| 885 |
// Do it in 'data' ... |
| 886 |
|
| 887 |
if (field.get_name() == "ids") { |
| 888 |
nodeMap.reverse_map_data(data, field, num_to_get); |
| 889 |
} |
| 890 |
} |
| 891 |
else if (field.get_name() == "orientation") { |
| 892 |
} |
| 893 |
else if (field.get_name() == "distribution_factors") { |
| 894 |
} |
| 895 |
else { |
| 896 |
num_to_get = Ioss::Utils::field_warning(ns, field, "output"); |
| 897 |
} |
| 898 |
} |
| 899 |
else if (role == Ioss::Field::TRANSIENT) { |
| 900 |
// Check if the specified field exists on this element block. |
| 901 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 902 |
// exist on the database as scalars with the appropriate |
| 903 |
// extensions. |
| 904 |
|
| 905 |
// Transfer each component of the variable into 'data' and then |
| 906 |
// output. Need temporary storage area of size 'number of |
| 907 |
// elements in this block. |
| 908 |
write_entity_transient_field(field, ns, entity_count, data); |
| 909 |
} |
| 910 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 911 |
num_to_get = write_attribute_field(field, ns, data); |
| 912 |
} |
| 913 |
else if (role == Ioss::Field::REDUCTION) { |
| 914 |
store_reduction_field(field, ns, data); |
| 915 |
} |
| 916 |
} |
| 917 |
return num_to_get; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
int64_t DatabaseIO::put_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field, |
| 922 |
void *data, size_t data_size) const |
| 923 |
{ |
| 924 |
return put_Xset_field_internal(ns, field, data, data_size); |
| 925 |
} |
| 926 |
|
| 927 |
int64_t DatabaseIO::put_field_internal(const Ioss::EdgeSet *ns, const Ioss::Field &field, |
| 928 |
void *data, size_t data_size) const |
| 929 |
{ |
| 930 |
return put_Xset_field_internal(ns, field, data, data_size); |
| 931 |
} |
| 932 |
|
| 933 |
int64_t DatabaseIO::put_field_internal(const Ioss::FaceSet *ns, const Ioss::Field &field, |
| 934 |
void *data, size_t data_size) const |
| 935 |
{ |
| 936 |
return put_Xset_field_internal(ns, field, data, data_size); |
| 937 |
} |
| 938 |
|
| 939 |
int64_t DatabaseIO::put_field_internal(const Ioss::ElementSet *ns, const Ioss::Field &field, |
| 940 |
void *data, size_t data_size) const |
| 941 |
{ |
| 942 |
return put_Xset_field_internal(ns, field, data, data_size); |
| 943 |
} |
| 944 |
|
| 945 |
int64_t DatabaseIO::put_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field, |
| 946 |
void *data, size_t data_size) const |
| 947 |
{ |
| 948 |
size_t num_to_get = field.verify(data_size); |
| 949 |
size_t entity_count = cs->entity_count(); |
| 950 |
|
| 951 |
assert(num_to_get == entity_count); |
| 952 |
if (num_to_get == 0) { |
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
// Return the <entity (node or side), processor> pair |
| 957 |
if (field.get_name() == "entity_processor") { |
| 958 |
|
| 959 |
// Check type -- node or side |
| 960 |
std::string type = cs->get_property("entity_type").get_string(); |
| 961 |
|
| 962 |
// Allocate temporary storage space |
| 963 |
std::vector<char> entities(entity_count * int_byte_size_api()); |
| 964 |
std::vector<char> procs(entity_count * int_byte_size_api()); |
| 965 |
|
| 966 |
if (type == "node") { |
| 967 |
// Convert global node id to local node id and store in 'entities' |
| 968 |
if (int_byte_size_api() == 4) { |
| 969 |
int *entity_proc = static_cast<int *>(data); |
| 970 |
int *ent = reinterpret_cast<int *>(Data(entities)); |
| 971 |
int *pro = reinterpret_cast<int *>(Data(procs)); |
| 972 |
int j = 0; |
| 973 |
for (size_t i = 0; i < entity_count; i++) { |
| 974 |
int global_id = entity_proc[j++]; |
| 975 |
ent[i] = nodeMap.global_to_local(global_id, true); |
| 976 |
pro[i] = entity_proc[j++]; |
| 977 |
} |
| 978 |
} |
| 979 |
else { |
| 980 |
auto *entity_proc = static_cast<int64_t *>(data); |
| 981 |
auto *ent = reinterpret_cast<int64_t *>(Data(entities)); |
| 982 |
auto *pro = reinterpret_cast<int64_t *>(Data(procs)); |
| 983 |
int64_t j = 0; |
| 984 |
for (size_t i = 0; i < entity_count; i++) { |
| 985 |
int64_t global_id = entity_proc[j++]; |
| 986 |
ent[i] = nodeMap.global_to_local(global_id, true); |
| 987 |
pro[i] = entity_proc[j++]; |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
if (commsetNodeCount > 0) {} |
| 992 |
|
| 993 |
if (commsetNodeCount == 1) { |
| 994 |
// NOTE: The internal and border node maps must be output in one call. |
| 995 |
// In this routine, we only have one commset at a time and can't |
| 996 |
// construct the entire map at one time. This is not really needed, |
| 997 |
// so for now we just skip if there is more than one commset. If |
| 998 |
// this information is really needed, need to cache the information |
| 999 |
// until all commsets have been processed. Also need to change |
| 1000 |
// write_communication_metada() [Maybe, unless client sets correct |
| 1001 |
// properties.] |
| 1002 |
|
| 1003 |
// Construct the node map (internal vs. border). |
| 1004 |
// Border nodes are those in the communication map (use entities array) |
| 1005 |
// Internal nodes are the rest. Allocate array to hold all nodes, |
| 1006 |
// initialize all to '1', then zero out the nodes in 'entities'. |
| 1007 |
// Iterate through array again and consolidate all '1's |
| 1008 |
|
| 1009 |
std::vector<char> internal(nodeCount * int_byte_size_api()); |
| 1010 |
if (int_byte_size_api() == 4) { |
| 1011 |
compute_internal_border_maps(reinterpret_cast<int *>(Data(entities)), |
| 1012 |
reinterpret_cast<int *>(Data(internal)), nodeCount, |
| 1013 |
entity_count); |
| 1014 |
} |
| 1015 |
else { |
| 1016 |
compute_internal_border_maps(reinterpret_cast<int64_t *>(Data(entities)), |
| 1017 |
reinterpret_cast<int64_t *>(Data(internal)), nodeCount, |
| 1018 |
entity_count); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
} |
| 1022 |
else if (type == "side") { |
| 1023 |
std::vector<char> sides(entity_count * int_byte_size_api()); |
| 1024 |
if (int_byte_size_api() == 4) { |
| 1025 |
int *entity_proc = static_cast<int *>(data); |
| 1026 |
int *ent = reinterpret_cast<int *>(Data(entities)); |
| 1027 |
int *sid = reinterpret_cast<int *>(Data(sides)); |
| 1028 |
int *pro = reinterpret_cast<int *>(Data(procs)); |
| 1029 |
int j = 0; |
| 1030 |
for (size_t i = 0; i < entity_count; i++) { |
| 1031 |
ent[i] = elemMap.global_to_local(entity_proc[j++]); |
| 1032 |
sid[i] = entity_proc[j++]; |
| 1033 |
pro[i] = entity_proc[j++]; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
else { |
| 1037 |
auto *entity_proc = static_cast<int64_t *>(data); |
| 1038 |
auto *ent = reinterpret_cast<int64_t *>(Data(entities)); |
| 1039 |
auto *sid = reinterpret_cast<int64_t *>(Data(sides)); |
| 1040 |
auto *pro = reinterpret_cast<int64_t *>(Data(procs)); |
| 1041 |
int64_t j = 0; |
| 1042 |
for (size_t i = 0; i < entity_count; i++) { |
| 1043 |
ent[i] = elemMap.global_to_local(entity_proc[j++]); |
| 1044 |
sid[i] = entity_proc[j++]; |
| 1045 |
pro[i] = entity_proc[j++]; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
// Construct the element map (internal vs. border). |
| 1050 |
// Border elements are those in the communication map (use entities array) |
| 1051 |
// Internal elements are the rest. Allocate array to hold all elements, |
| 1052 |
// initialize all to '1', then zero out the elements in 'entities'. |
| 1053 |
// Iterate through array again and consolidate all '1's |
| 1054 |
std::vector<char> internal(elementCount * int_byte_size_api()); |
| 1055 |
if (int_byte_size_api() == 4) { |
| 1056 |
compute_internal_border_maps(reinterpret_cast<int *>(Data(entities)), |
| 1057 |
reinterpret_cast<int *>(Data(internal)), elementCount, |
| 1058 |
entity_count); |
| 1059 |
} |
| 1060 |
else { |
| 1061 |
compute_internal_border_maps(reinterpret_cast<int64_t *>(Data(entities)), |
| 1062 |
reinterpret_cast<int64_t *>(Data(internal)), elementCount, |
| 1063 |
entity_count); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
else { |
| 1067 |
std::ostringstream errmsg; |
| 1068 |
fmt::print(errmsg, "ERROR: Invalid commset type {}", type); |
| 1069 |
IOSS_ERROR(errmsg); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else if (field.get_name() == "ids") { |
| 1073 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 1074 |
} |
| 1075 |
else { |
| 1076 |
num_to_get = Ioss::Utils::field_warning(cs, field, "output"); |
| 1077 |
} |
| 1078 |
return num_to_get; |
| 1079 |
} |
| 1080 |
|
| 1081 |
int64_t DatabaseIO::put_field_internal(const Ioss::SideSet *fs, const Ioss::Field &field, |
| 1082 |
void * /* data */, size_t data_size) const |
| 1083 |
{ |
| 1084 |
size_t num_to_get = field.verify(data_size); |
| 1085 |
if (field.get_name() == "ids") { |
| 1086 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 1087 |
} |
| 1088 |
else { |
| 1089 |
num_to_get = Ioss::Utils::field_warning(fs, field, "output"); |
| 1090 |
} |
| 1091 |
return num_to_get; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int64_t DatabaseIO::put_field_internal(const Ioss::SideBlock *fb, const Ioss::Field &field, |
| 1095 |
void *data, size_t data_size) const |
| 1096 |
{ |
| 1097 |
size_t num_to_get = field.verify(data_size); |
| 1098 |
if (num_to_get > 0) { |
| 1099 |
|
| 1100 |
size_t entity_count = fb->entity_count(); |
| 1101 |
Ioss::Field::RoleType role = field.get_role(); |
| 1102 |
|
| 1103 |
if (role == Ioss::Field::MESH) { |
| 1104 |
if (field.get_name() == "side_ids" && fb->name() == "universal_sideset") {} |
| 1105 |
|
| 1106 |
else if (field.get_name() == "side_ids") { |
| 1107 |
} |
| 1108 |
|
| 1109 |
else if (field.get_name() == "ids") { |
| 1110 |
// ============================================================= |
| 1111 |
// NOTE: Code is currently commented out since we have |
| 1112 |
// redundant ways of getting the data (element/side) out to |
| 1113 |
// the database. The 'ids' field method relies on a numbering |
| 1114 |
// kluge, so for now trying the 'element_side' field... |
| 1115 |
// ============================================================= |
| 1116 |
} |
| 1117 |
|
| 1118 |
else if (field.get_name() == "distribution_factors") { |
| 1119 |
} |
| 1120 |
else if (field.get_name() == "element_side") { |
| 1121 |
// In exodus, the 'side block' is stored as a sideset. A |
| 1122 |
// sideset has a list of elements and a corresponding local |
| 1123 |
// element side (1-based) |
| 1124 |
|
| 1125 |
// The 'data' passed into the function is stored as a |
| 1126 |
// 2D vector e0,f0,e1,f1,... (e=element, f=side) |
| 1127 |
|
| 1128 |
// To avoid overwriting the passed in data, we allocate |
| 1129 |
// two arrays to store the data for this sideset. |
| 1130 |
|
| 1131 |
// The element_id passed in is the global id; we need to |
| 1132 |
// output the local id. |
| 1133 |
|
| 1134 |
// Allocate space for local side number and element numbers |
| 1135 |
// numbers. |
| 1136 |
// See if edges or faces... |
| 1137 |
size_t side_offset = Ioss::Utils::get_side_offset(fb); |
| 1138 |
|
| 1139 |
size_t index = 0; |
| 1140 |
|
| 1141 |
if (field.get_type() == Ioss::Field::INTEGER) { |
| 1142 |
Ioss::IntVector element(num_to_get); |
| 1143 |
Ioss::IntVector side(num_to_get); |
| 1144 |
int *el_side = reinterpret_cast<int *>(data); |
| 1145 |
|
| 1146 |
try { |
| 1147 |
for (size_t i = 0; i < num_to_get; i++) { |
| 1148 |
element[i] = elemMap.global_to_local(el_side[index++]); |
| 1149 |
side[i] = el_side[index++] + side_offset; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
catch (const std::runtime_error &x) { |
| 1153 |
std::ostringstream errmsg; |
| 1154 |
fmt::print(errmsg, "{}On SideBlock `{}` while outputting field `elem_side`\n", |
| 1155 |
x.what(), fb->name()); |
| 1156 |
IOSS_ERROR(errmsg); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
else { |
| 1160 |
Ioss::Int64Vector element(num_to_get); |
| 1161 |
Ioss::Int64Vector side(num_to_get); |
| 1162 |
auto *el_side = reinterpret_cast<int64_t *>(data); |
| 1163 |
|
| 1164 |
try { |
| 1165 |
for (size_t i = 0; i < num_to_get; i++) { |
| 1166 |
element[i] = elemMap.global_to_local(el_side[index++]); |
| 1167 |
side[i] = el_side[index++] + side_offset; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
catch (const std::runtime_error &x) { |
| 1171 |
std::ostringstream errmsg; |
| 1172 |
fmt::print(errmsg, "{}On SideBlock `{}` while outputting field `elem_side`\n", |
| 1173 |
x.what(), fb->name()); |
| 1174 |
IOSS_ERROR(errmsg); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
} |
| 1178 |
else if (field.get_name() == "element_side_raw") { |
| 1179 |
// In exodus, the 'side block' is stored as a sideset. A |
| 1180 |
// sideset has a list of elements and a corresponding local |
| 1181 |
// element side (1-based) |
| 1182 |
|
| 1183 |
// The 'data' passed into the function is stored as a |
| 1184 |
// 2D vector e0,f0,e1,f1,... (e=element, f=side) |
| 1185 |
|
| 1186 |
// To avoid overwriting the passed in data, we allocate |
| 1187 |
// two arrays to store the data for this sideset. |
| 1188 |
|
| 1189 |
// The element_id passed in is the local id. |
| 1190 |
|
| 1191 |
// See if edges or faces... |
| 1192 |
size_t side_offset = Ioss::Utils::get_side_offset(fb); |
| 1193 |
|
| 1194 |
size_t index = 0; |
| 1195 |
if (field.get_type() == Ioss::Field::INTEGER) { |
| 1196 |
Ioss::IntVector element(num_to_get); |
| 1197 |
Ioss::IntVector side(num_to_get); |
| 1198 |
int *el_side = reinterpret_cast<int *>(data); |
| 1199 |
|
| 1200 |
for (size_t i = 0; i < num_to_get; i++) { |
| 1201 |
element[i] = el_side[index++]; |
| 1202 |
side[i] = el_side[index++] + side_offset; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
else { |
| 1206 |
Ioss::Int64Vector element(num_to_get); |
| 1207 |
Ioss::Int64Vector side(num_to_get); |
| 1208 |
auto *el_side = reinterpret_cast<int64_t *>(data); |
| 1209 |
|
| 1210 |
for (size_t i = 0; i < num_to_get; i++) { |
| 1211 |
element[i] = el_side[index++]; |
| 1212 |
side[i] = el_side[index++] + side_offset; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
else if (field.get_name() == "connectivity") { |
| 1217 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 1218 |
} |
| 1219 |
else if (field.get_name() == "connectivity_raw") { |
| 1220 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 1221 |
} |
| 1222 |
else { |
| 1223 |
num_to_get = Ioss::Utils::field_warning(fb, field, "output"); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
else if (role == Ioss::Field::TRANSIENT) { |
| 1227 |
// Check if the specified field exists on this block. |
| 1228 |
// Note that 'higher-order' storage types (e.g. SYM_TENSOR) |
| 1229 |
// exist on the database as scalars with the appropriate |
| 1230 |
// extensions. |
| 1231 |
|
| 1232 |
// Transfer each component of the variable into 'data' and then |
| 1233 |
// output. Need temporary storage area of size 'number of |
| 1234 |
// entities in this block. |
| 1235 |
write_entity_transient_field(field, fb, entity_count, data); |
| 1236 |
} |
| 1237 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 1238 |
num_to_get = write_attribute_field(field, fb, data); |
| 1239 |
} |
| 1240 |
else if (role == Ioss::Field::REDUCTION) { |
| 1241 |
store_reduction_field(field, fb, data); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
return num_to_get; |
| 1245 |
} |
| 1246 |
|
| 1247 |
void DatabaseIO::write_meta_data(Ioss::IfDatabaseExistsBehavior behavior) |
| 1248 |
{ |
| 1249 |
Ioss::Region *region = get_region(); |
| 1250 |
common_write_metadata(behavior); |
| 1251 |
|
| 1252 |
char the_title[max_line_length + 1]; |
| 1253 |
|
| 1254 |
// Title... |
| 1255 |
if (region->property_exists("title")) { |
| 1256 |
std::string title_str = region->get_property("title").get_string(); |
| 1257 |
Ioss::Utils::copy_string(the_title, title_str); |
| 1258 |
} |
| 1259 |
else { |
| 1260 |
Ioss::Utils::copy_string(the_title, "IOSS Default Output Title"); |
| 1261 |
} |
| 1262 |
|
| 1263 |
bool file_per_processor = true; |
| 1264 |
Ioexnl::Mesh mesh(spatialDimension, the_title, util(), file_per_processor); |
| 1265 |
{ |
| 1266 |
bool omit_maps = false; |
| 1267 |
Ioss::Utils::check_set_bool_property(properties, "OMIT_EXODUS_NUM_MAPS", omit_maps); |
| 1268 |
if (omit_maps) { |
| 1269 |
// Used for special cases only -- typically very large meshes with *known* 1..count maps |
| 1270 |
// and workarounds that avoid calling the "ids" put_field calls. |
| 1271 |
mesh.use_node_map = false; |
| 1272 |
mesh.use_elem_map = false; |
| 1273 |
mesh.use_face_map = false; |
| 1274 |
mesh.use_edge_map = false; |
| 1275 |
} |
| 1276 |
|
| 1277 |
bool minimal_nemesis = false; |
| 1278 |
Ioss::Utils::check_set_bool_property(properties, "MINIMAL_NEMESIS_DATA", minimal_nemesis); |
| 1279 |
if (minimal_nemesis) { |
| 1280 |
// Only output the node communication map data... This is all that stk/sierra needs |
| 1281 |
mesh.full_nemesis_data = false; |
| 1282 |
} |
| 1283 |
|
| 1284 |
mesh.populate(region); |
| 1285 |
gather_communication_metadata(&mesh.comm); |
| 1286 |
|
| 1287 |
if (behavior != Ioss::DB_APPEND && behavior != Ioss::DB_MODIFY) { |
| 1288 |
bool omit_qa = false; |
| 1289 |
Ioss::Utils::check_set_bool_property(properties, "OMIT_QA_RECORDS", omit_qa); |
| 1290 |
if (!omit_qa) { |
| 1291 |
put_qa(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
bool omit_info = false; |
| 1295 |
Ioss::Utils::check_set_bool_property(properties, "OMIT_INFO_RECORDS", omit_info); |
| 1296 |
if (!omit_info) { |
| 1297 |
put_info(); |
| 1298 |
} |
| 1299 |
|
| 1300 |
output_other_metadata(); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
void DatabaseIO::gather_communication_metadata(Ioexnl::CommunicationMetaData *meta) |
| 1306 |
{ |
| 1307 |
// It's possible that we are a serial program outputting information |
| 1308 |
// for later use by a parallel program. |
| 1309 |
|
| 1310 |
meta->processorCount = 0; |
| 1311 |
meta->processorId = 0; |
| 1312 |
meta->outputNemesis = false; |
| 1313 |
|
| 1314 |
if (isParallel) { |
| 1315 |
meta->processorCount = util().parallel_size(); |
| 1316 |
meta->processorId = myProcessor; |
| 1317 |
meta->outputNemesis = true; |
| 1318 |
} |
| 1319 |
else { |
| 1320 |
if (properties.exists("processor_count")) { |
| 1321 |
meta->processorCount = properties.get("processor_count").get_int(); |
| 1322 |
} |
| 1323 |
else if (get_region()->property_exists("processor_count")) { |
| 1324 |
meta->processorCount = get_region()->get_property("processor_count").get_int(); |
| 1325 |
} |
| 1326 |
|
| 1327 |
if (properties.exists("my_processor")) { |
| 1328 |
meta->processorId = properties.get("my_processor").get_int(); |
| 1329 |
} |
| 1330 |
else if (get_region()->property_exists("my_processor")) { |
| 1331 |
meta->processorId = get_region()->get_property("my_processor").get_int(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
if (!get_region()->get_commsets().empty()) { |
| 1335 |
isSerialParallel = true; |
| 1336 |
meta->outputNemesis = true; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
if (isSerialParallel || meta->processorCount > 0) { |
| 1341 |
meta->globalNodes = get_region()->get_optional_property("global_node_count", 1); |
| 1342 |
meta->globalElements = get_region()->get_optional_property("global_element_count", 1); |
| 1343 |
meta->globalElementBlocks = get_region()->get_optional_property( |
| 1344 |
"global_element_block_count", get_region()->get_element_blocks().size()); |
| 1345 |
meta->globalNodeSets = get_region()->get_optional_property( |
| 1346 |
"global_node_set_count", get_region()->get_nodesets().size()); |
| 1347 |
meta->globalSideSets = get_region()->get_optional_property( |
| 1348 |
"global_side_set_count", get_region()->get_sidesets().size()); |
| 1349 |
|
| 1350 |
// ======================================================================== |
| 1351 |
// Load balance parameters (NEMESIS, p15) |
| 1352 |
meta->nodesInternal = get_region()->get_optional_property("internal_node_count", nodeCount); |
| 1353 |
meta->nodesBorder = get_region()->get_optional_property("border_node_count", 0); |
| 1354 |
meta->nodesExternal = 0; // Shadow nodes == 0 for now |
| 1355 |
meta->elementsInternal = |
| 1356 |
get_region()->get_optional_property("internal_element_count", elementCount); |
| 1357 |
meta->elementsBorder = get_region()->get_optional_property("border_element_count", 0); |
| 1358 |
|
| 1359 |
const Ioss::CommSetContainer &comm_sets = get_region()->get_commsets(); |
| 1360 |
for (auto &cs : comm_sets) { |
| 1361 |
std::string type = cs->get_property("entity_type").get_string(); |
| 1362 |
size_t count = cs->entity_count(); |
| 1363 |
int64_t id = Ioexnl::get_id(cs, &ids_); |
| 1364 |
|
| 1365 |
if (type == "node") { |
| 1366 |
meta->nodeMap.emplace_back(id, count, 'n'); |
| 1367 |
} |
| 1368 |
else if (type == "side") { |
| 1369 |
meta->elementMap.emplace_back(id, count, 'e'); |
| 1370 |
} |
| 1371 |
else { |
| 1372 |
std::ostringstream errmsg; |
| 1373 |
fmt::print(errmsg, "Internal Program Error..."); |
| 1374 |
IOSS_ERROR(errmsg); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
} |
| 1378 |
commsetNodeCount = meta->nodeMap.size(); |
| 1379 |
commsetElemCount = meta->elementMap.size(); |
| 1380 |
} |
| 1381 |
} // namespace Ioexnl |