| 1 |
// Copyright(C) 1999-2025 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" // for Int64Vector, IntVector |
| 8 |
#include "Ioss_SideBlock.h" // for SideBlock |
| 9 |
#include "Ioss_Utils.h" // for Utils, IOSS_ERROR |
| 10 |
#include "generated/Iogn_DatabaseIO.h" |
| 11 |
#include "generated/Iogn_GeneratedMesh.h" // for GeneratedMesh |
| 12 |
#include <cassert> // for assert |
| 13 |
#include <cmath> // for sqrt |
| 14 |
#include <fmt/format.h> |
| 15 |
#include <fmt/ostream.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string> // for string, operator==, etc |
| 18 |
|
| 19 |
#include "Ioss_CommSet.h" // for CommSet |
| 20 |
#include "Ioss_DBUsage.h" // for DatabaseUsage |
| 21 |
#include "Ioss_DatabaseIO.h" // for DatabaseIO |
| 22 |
#include "Ioss_ElementBlock.h" // for ElementBlock |
| 23 |
#include "Ioss_EntityType.h" // for EntityType, etc |
| 24 |
#include "Ioss_Field.h" // for Field, etc |
| 25 |
#include "Ioss_GroupingEntity.h" // for GroupingEntity |
| 26 |
#include "Ioss_IOFactory.h" // for IOFactory |
| 27 |
#include "Ioss_Map.h" // for Map, MapContainer |
| 28 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 29 |
#include "Ioss_NodeSet.h" // for NodeSet |
| 30 |
#include "Ioss_ParallelUtils.h" // for ParallelUtils |
| 31 |
#include "Ioss_Property.h" // for Property |
| 32 |
#include "Ioss_PropertyManager.h" // for PropertyManager |
| 33 |
#include "Ioss_Region.h" // for Region |
| 34 |
#include "Ioss_SideSet.h" // for SideSet |
| 35 |
#include "Ioss_VariableType.h" // for VariableType |
| 36 |
|
| 37 |
namespace { |
| 38 |
template <typename INT> |
| 39 |
void map_global_to_local(const Ioss::Map &map, size_t count, size_t stride, INT *data) |
| 40 |
{ |
| 41 |
for (size_t i = 0; i < count; i += stride) { |
| 42 |
int64_t local = map.global_to_local(data[i], true); |
| 43 |
data[i] = static_cast<INT>(local); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
template <typename INT> |
| 48 |
void fill_transient_data(size_t component_count, double *data, INT *ids, size_t count, |
| 49 |
double offset = 0.0) |
| 50 |
{ |
| 51 |
if (component_count == 1) { |
| 52 |
for (size_t i = 0; i < count; i++) { |
| 53 |
data[i] = std::sqrt((double)ids[i]) + offset; |
| 54 |
} |
| 55 |
} |
| 56 |
else { |
| 57 |
for (size_t i = 0; i < count; i++) { |
| 58 |
for (size_t j = 0; j < component_count; j++) { |
| 59 |
data[i * component_count + j] = j + std::sqrt((double)ids[i]) + offset; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void fill_transient_data(const Ioss::GroupingEntity *entity, const Ioss::Field &field, void *data, |
| 66 |
void *id_data, size_t count, double offset = 0.0) |
| 67 |
{ |
| 68 |
const Ioss::Field &ids = entity->get_fieldref("ids"); |
| 69 |
if (ids.is_type(Ioss::Field::INTEGER)) { |
| 70 |
fill_transient_data(field.raw_storage()->component_count(), reinterpret_cast<double *>(data), |
| 71 |
reinterpret_cast<int *>(id_data), count, offset); |
| 72 |
} |
| 73 |
else { |
| 74 |
fill_transient_data(field.raw_storage()->component_count(), reinterpret_cast<double *>(data), |
| 75 |
reinterpret_cast<int64_t *>(id_data), count, offset); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void fill_constant_data(const Ioss::Field &field, void *data, double value) |
| 80 |
{ |
| 81 |
auto *rdata = reinterpret_cast<double *>(data); |
| 82 |
size_t count = field.raw_count(); |
| 83 |
size_t component_count = field.raw_storage()->component_count(); |
| 84 |
for (size_t i = 0; i < count * component_count; i++) { |
| 85 |
rdata[i] = value; |
| 86 |
} |
| 87 |
} |
| 88 |
} // namespace |
| 89 |
namespace Iogn { |
| 90 |
|
| 91 |
// ======================================================================== |
| 92 |
const IOFactory *IOFactory::factory() |
| 93 |
{ |
| 94 |
static IOFactory registerThis; |
| 95 |
return ®isterThis; |
| 96 |
} |
| 97 |
|
| 98 |
IOFactory::IOFactory() : Ioss::IOFactory("generated") {} |
| 99 |
|
| 100 |
Ioss::DatabaseIO *IOFactory::make_IO(const std::string &filename, Ioss::DatabaseUsage db_usage, |
| 101 |
Ioss_MPI_Comm communicator, |
| 102 |
const Ioss::PropertyManager &props) const |
| 103 |
{ |
| 104 |
return new DatabaseIO(nullptr, filename, db_usage, communicator, props); |
| 105 |
} |
| 106 |
|
| 107 |
// ======================================================================== |
| 108 |
DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename, |
| 109 |
Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator, |
| 110 |
const Ioss::PropertyManager &props) |
| 111 |
: Ioss::DatabaseIO(region, filename, db_usage, communicator, props) |
| 112 |
{ |
| 113 |
if (is_input()) { |
| 114 |
dbState = Ioss::STATE_UNKNOWN; |
| 115 |
} |
| 116 |
else { |
| 117 |
IOSS_ERROR("Generated mesh option is only valid for input mesh."); |
| 118 |
} |
| 119 |
if (props.exists("USE_CONSTANT_DF")) { |
| 120 |
m_useVariableDf = false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
DatabaseIO::~DatabaseIO() { delete m_generatedMesh; } |
| 125 |
|
| 126 |
void DatabaseIO::read_meta_data_nl() |
| 127 |
{ |
| 128 |
if (m_generatedMesh == nullptr) { |
| 129 |
if (get_filename() == "external") { |
| 130 |
IOSS_ERROR("ERROR: (generated mesh) 'external' specified for mesh, but " |
| 131 |
"getGeneratedMesh was not called to set the external mesh.\n"); |
| 132 |
} |
| 133 |
else { |
| 134 |
m_generatedMesh = |
| 135 |
new GeneratedMesh(get_filename(), util().parallel_size(), util().parallel_rank()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
assert(m_generatedMesh != nullptr); |
| 140 |
|
| 141 |
Ioss::Region *this_region = get_region(); |
| 142 |
auto glob_node_count = m_generatedMesh->node_count(); |
| 143 |
auto glob_elem_count = m_generatedMesh->element_count(); |
| 144 |
|
| 145 |
this_region->property_add(Ioss::Property("global_node_count", glob_node_count)); |
| 146 |
this_region->property_add(Ioss::Property("global_element_count", glob_elem_count)); |
| 147 |
|
| 148 |
const int64_t two_billion = 2ll << 30; |
| 149 |
if ((glob_node_count > two_billion || glob_elem_count > two_billion) && |
| 150 |
int_byte_size_api() == 4) { |
| 151 |
IOSS_ERROR(fmt::format( |
| 152 |
"ERROR: The node count is {} and the element count is {}.\n" |
| 153 |
" This exceeds the capacity of the 32-bit integers ({})\n" |
| 154 |
" which are being requested by the client.\n" |
| 155 |
" The mesh requires 64-bit integers which can be requested by setting the " |
| 156 |
"`INTEGER_SIZE_API=8` property.", |
| 157 |
fmt::group_digits(glob_node_count), fmt::group_digits(glob_elem_count), |
| 158 |
fmt::group_digits(two_billion))); |
| 159 |
} |
| 160 |
|
| 161 |
spatialDimension = 3; |
| 162 |
nodeCount = m_generatedMesh->node_count_proc(); |
| 163 |
elementCount = m_generatedMesh->element_count_proc(); |
| 164 |
elementBlockCount = m_generatedMesh->block_count(); |
| 165 |
nodesetCount = m_generatedMesh->nodeset_count(); |
| 166 |
sidesetCount = m_generatedMesh->sideset_count(); |
| 167 |
|
| 168 |
get_step_times_nl(); |
| 169 |
|
| 170 |
add_transient_fields(this_region); |
| 171 |
get_nodeblocks(); |
| 172 |
get_elemblocks(); |
| 173 |
get_nodesets(); |
| 174 |
get_sidesets(); |
| 175 |
get_commsets(); |
| 176 |
|
| 177 |
this_region->property_add( |
| 178 |
Ioss::Property(std::string("title"), std::string("GeneratedMesh: ") += get_filename())); |
| 179 |
} |
| 180 |
|
| 181 |
bool DatabaseIO::begin_nl(Ioss::State /* state */) { return true; } |
| 182 |
|
| 183 |
bool DatabaseIO::end_nl(Ioss::State /* state */) { return true; } |
| 184 |
|
| 185 |
bool DatabaseIO::begin_state_nl(int /* state */, double time) |
| 186 |
{ |
| 187 |
currentTime = time; |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
int64_t DatabaseIO::get_field_internal(const Ioss::NodeBlock *nb, const Ioss::Field &field, |
| 192 |
void *data, size_t data_size) const |
| 193 |
{ |
| 194 |
size_t num_to_get = field.verify(data_size); |
| 195 |
|
| 196 |
Ioss::Field::RoleType role = field.get_role(); |
| 197 |
if (role == Ioss::Field::MESH) { |
| 198 |
if (field.get_name() == "mesh_model_coordinates") { |
| 199 |
// Cast 'data' to correct size -- double |
| 200 |
auto *rdata = static_cast<double *>(data); |
| 201 |
m_generatedMesh->coordinates(rdata); |
| 202 |
} |
| 203 |
else if (field.get_name() == "mesh_model_coordinates_x") { |
| 204 |
// Cast 'data' to correct size -- double |
| 205 |
auto *rdata = static_cast<double *>(data); |
| 206 |
m_generatedMesh->coordinates(1, rdata); |
| 207 |
} |
| 208 |
else if (field.get_name() == "mesh_model_coordinates_y") { |
| 209 |
// Cast 'data' to correct size -- double |
| 210 |
auto *rdata = static_cast<double *>(data); |
| 211 |
m_generatedMesh->coordinates(2, rdata); |
| 212 |
} |
| 213 |
else if (field.get_name() == "mesh_model_coordinates_z") { |
| 214 |
// Cast 'data' to correct size -- double |
| 215 |
auto *rdata = static_cast<double *>(data); |
| 216 |
m_generatedMesh->coordinates(3, rdata); |
| 217 |
} |
| 218 |
|
| 219 |
// NOTE: The implicit_ids field is ONLY provided for backward- |
| 220 |
// compatibility and should not be used unless absolutely |
| 221 |
// required. For generated mesh, the implicit_ids and ids are the same. |
| 222 |
else if (field.get_name() == "ids" || field.get_name() == "implicit_ids") { |
| 223 |
// Map the local ids in this node block |
| 224 |
// (1...node_count) to global node ids. |
| 225 |
get_node_map().map_implicit_data(data, field, num_to_get, 0); |
| 226 |
} |
| 227 |
else if (field.get_name() == "owning_processor") { |
| 228 |
int *owner = static_cast<int *>(data); |
| 229 |
m_generatedMesh->owning_processor(owner, num_to_get); |
| 230 |
} |
| 231 |
else if (field.get_name() == "connectivity") { |
| 232 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 233 |
} |
| 234 |
else if (field.get_name() == "connectivity_raw") { |
| 235 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 236 |
} |
| 237 |
else { |
| 238 |
num_to_get = Ioss::Utils::field_warning(nb, field, "input"); |
| 239 |
} |
| 240 |
return num_to_get; |
| 241 |
} |
| 242 |
|
| 243 |
const Ioss::Field &id_fld = nb->get_fieldref("ids"); |
| 244 |
std::vector<char> ids(id_fld.get_size()); |
| 245 |
get_field_internal(nb, id_fld, Data(ids), id_fld.get_size()); |
| 246 |
fill_transient_data(nb, field, data, Data(ids), num_to_get, currentTime); |
| 247 |
|
| 248 |
return num_to_get; |
| 249 |
} |
| 250 |
|
| 251 |
int64_t DatabaseIO::get_field_internal(const Ioss::Region * /*region*/, const Ioss::Field &field, |
| 252 |
void *data, size_t /*data_size*/) const |
| 253 |
{ |
| 254 |
Ioss::Field::RoleType role = field.get_role(); |
| 255 |
if (role == Ioss::Field::TRANSIENT) { |
| 256 |
// Fill the field with arbitrary data... |
| 257 |
(reinterpret_cast<double *>(data))[0] = static_cast<double>(rand()); |
| 258 |
} |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
int64_t DatabaseIO::get_field_internal(const Ioss::ElementBlock *eb, const Ioss::Field &field, |
| 263 |
void *data, size_t data_size) const |
| 264 |
{ |
| 265 |
size_t num_to_get = field.verify(data_size); |
| 266 |
|
| 267 |
int64_t id = eb->get_property("id").get_int(); |
| 268 |
int64_t element_count = eb->entity_count(); |
| 269 |
Ioss::Field::RoleType role = field.get_role(); |
| 270 |
|
| 271 |
if (role == Ioss::Field::MESH) { |
| 272 |
// Handle the MESH fields required for an ExodusII file model. |
| 273 |
// (The 'genesis' portion) |
| 274 |
|
| 275 |
if (field.get_name() == "connectivity" || field.get_name() == "connectivity_raw") { |
| 276 |
assert(field.raw_storage()->component_count() == m_generatedMesh->topology_type(id).second); |
| 277 |
|
| 278 |
// The generated mesh connectivity is returned in a vector. Ids are global |
| 279 |
if (field.is_type(Ioss::Field::INTEGER)) { |
| 280 |
int *connect = static_cast<int *>(data); |
| 281 |
m_generatedMesh->connectivity(id, connect); |
| 282 |
if (field.get_name() == "connectivity_raw") { |
| 283 |
map_global_to_local(get_node_map(), |
| 284 |
element_count * field.raw_storage()->component_count(), 1, connect); |
| 285 |
} |
| 286 |
} |
| 287 |
else { |
| 288 |
auto *connect = static_cast<int64_t *>(data); |
| 289 |
m_generatedMesh->connectivity(id, connect); |
| 290 |
if (field.get_name() == "connectivity_raw") { |
| 291 |
map_global_to_local(get_node_map(), |
| 292 |
element_count * field.raw_storage()->component_count(), 1, connect); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
else if (field.get_name() == "ids" || field.get_name() == "implicit_ids") { |
| 297 |
// Map the local ids in this element block |
| 298 |
// (eb_offset+1...eb_offset+1+element_count) to global element ids. |
| 299 |
get_element_map().map_implicit_data(data, field, num_to_get, eb->get_offset()); |
| 300 |
} |
| 301 |
else { |
| 302 |
num_to_get = Ioss::Utils::field_warning(eb, field, "input"); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
else if (role == Ioss::Field::ATTRIBUTE) { |
| 307 |
if (element_count > 0) { |
| 308 |
int64_t attribute_count = eb->get_property("attribute_count").get_int(); |
| 309 |
if (attribute_count > 0) { |
| 310 |
auto *attr = static_cast<double *>(data); |
| 311 |
for (size_t i = 0; i < num_to_get; i++) { |
| 312 |
attr[i] = 1.0; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
else if (role == Ioss::Field::TRANSIENT) { |
| 319 |
// Fill the field with arbitrary data... |
| 320 |
const Ioss::Field &id_fld = eb->get_fieldref("ids"); |
| 321 |
std::vector<char> ids(id_fld.get_size()); |
| 322 |
get_field_internal(eb, id_fld, Data(ids), id_fld.get_size()); |
| 323 |
fill_transient_data(eb, field, data, Data(ids), num_to_get, currentTime); |
| 324 |
} |
| 325 |
else if (role == Ioss::Field::REDUCTION) { |
| 326 |
num_to_get = Ioss::Utils::field_warning(eb, field, "input reduction"); |
| 327 |
} |
| 328 |
return num_to_get; |
| 329 |
} |
| 330 |
|
| 331 |
int64_t DatabaseIO::get_field_internal(const Ioss::SideBlock *sd_blk, const Ioss::Field &field, |
| 332 |
void *data, size_t data_size) const |
| 333 |
{ |
| 334 |
size_t num_to_get = field.verify(data_size); |
| 335 |
|
| 336 |
int64_t id = sd_blk->get_property("id").get_int(); |
| 337 |
size_t entity_count = sd_blk->entity_count(); |
| 338 |
if (num_to_get != entity_count) { |
| 339 |
IOSS_ERROR("Partial field input not implemented for side blocks"); |
| 340 |
} |
| 341 |
|
| 342 |
Ioss::Field::RoleType role = field.get_role(); |
| 343 |
if (role == Ioss::Field::MESH) { |
| 344 |
|
| 345 |
if (field.get_name() == "ids") { |
| 346 |
// A sideset' is basically an exodus sideset. A |
| 347 |
// sideset has a list of elements and a corresponding local |
| 348 |
// element side (1-based) The side id is: side_id = |
| 349 |
// 10*element_id + local_side_number This assumes that all |
| 350 |
// sides in a sideset are boundary sides. |
| 351 |
std::vector<int64_t> elem_side; |
| 352 |
m_generatedMesh->sideset_elem_sides(id, elem_side); |
| 353 |
if (field.is_type(Ioss::Field::INTEGER)) { |
| 354 |
int *ids = static_cast<int *>(data); |
| 355 |
for (size_t i = 0; i < num_to_get; i++) { |
| 356 |
ids[i] = static_cast<int>(10 * elem_side[2 * i + 0] + elem_side[2 * i + 1] + 1); |
| 357 |
} |
| 358 |
} |
| 359 |
else { |
| 360 |
auto *ids = static_cast<int64_t *>(data); |
| 361 |
for (size_t i = 0; i < num_to_get; i++) { |
| 362 |
ids[i] = 10 * elem_side[2 * i + 0] + elem_side[2 * i + 1] + 1; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
else if (field.get_name() == "element_side" || field.get_name() == "element_side_raw") { |
| 368 |
// Since we only have a single array, we need to allocate an extra |
| 369 |
// array to store all of the data. Note also that the element_id |
| 370 |
// is the global id but only the local id is stored so we need to |
| 371 |
// map from local_to_global prior to generating the side id... |
| 372 |
|
| 373 |
std::vector<int64_t> elem_side; |
| 374 |
m_generatedMesh->sideset_elem_sides(id, elem_side); |
| 375 |
if (field.get_name() == "element_side_raw") { |
| 376 |
map_global_to_local(get_element_map(), elem_side.size(), 2, Data(elem_side)); |
| 377 |
} |
| 378 |
|
| 379 |
if (field.is_type(Ioss::Field::INTEGER)) { |
| 380 |
int *element_side = static_cast<int *>(data); |
| 381 |
for (size_t i = 0; i < num_to_get; i++) { |
| 382 |
element_side[2 * i + 0] = static_cast<int>(elem_side[2 * i + 0]); |
| 383 |
element_side[2 * i + 1] = static_cast<int>(elem_side[2 * i + 1] + 1); |
| 384 |
} |
| 385 |
} |
| 386 |
else { |
| 387 |
auto *element_side = static_cast<int64_t *>(data); |
| 388 |
for (size_t i = 0; i < num_to_get; i++) { |
| 389 |
element_side[2 * i + 0] = elem_side[2 * i + 0]; |
| 390 |
element_side[2 * i + 1] = elem_side[2 * i + 1] + 1; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
else if (field.get_name() == "distribution_factors") { |
| 396 |
if (m_useVariableDf) { |
| 397 |
const Ioss::Field &id_fld = sd_blk->get_fieldref("ids"); |
| 398 |
std::vector<char> ids(id_fld.get_size()); |
| 399 |
get_field_internal(sd_blk, id_fld, Data(ids), id_fld.get_size()); |
| 400 |
fill_transient_data(sd_blk, field, data, Data(ids), num_to_get); |
| 401 |
} |
| 402 |
else { |
| 403 |
fill_constant_data(field, data, 1.0); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
else { |
| 408 |
num_to_get = Ioss::Utils::field_warning(sd_blk, field, "input"); |
| 409 |
} |
| 410 |
} |
| 411 |
else if (role == Ioss::Field::TRANSIENT) { |
| 412 |
const Ioss::Field &id_fld = sd_blk->get_fieldref("ids"); |
| 413 |
std::vector<char> ids(id_fld.get_size()); |
| 414 |
get_field_internal(sd_blk, id_fld, Data(ids), id_fld.get_size()); |
| 415 |
fill_transient_data(sd_blk, field, data, Data(ids), num_to_get, currentTime); |
| 416 |
} |
| 417 |
return num_to_get; |
| 418 |
} |
| 419 |
|
| 420 |
int64_t DatabaseIO::get_field_internal(const Ioss::NodeSet *ns, const Ioss::Field &field, |
| 421 |
void *data, size_t data_size) const |
| 422 |
{ |
| 423 |
size_t num_to_get = field.verify(data_size); |
| 424 |
|
| 425 |
int64_t id = ns->get_property("id").get_int(); |
| 426 |
Ioss::Field::RoleType role = field.get_role(); |
| 427 |
if (role == Ioss::Field::MESH) { |
| 428 |
|
| 429 |
if (field.get_name() == "ids" || field.get_name() == "ids_raw") { |
| 430 |
std::vector<int64_t> nodes; |
| 431 |
m_generatedMesh->nodeset_nodes(id, nodes); |
| 432 |
if (field.get_name() == "ids_raw") { |
| 433 |
map_global_to_local(get_node_map(), nodes.size(), 1, Data(nodes)); |
| 434 |
} |
| 435 |
|
| 436 |
if (field.is_type(Ioss::Field::INTEGER)) { |
| 437 |
int *ids = static_cast<int *>(data); |
| 438 |
#ifdef _MSC_VER |
| 439 |
#pragma warning(push) |
| 440 |
#pragma warning(disable : 4244) |
| 441 |
#endif |
| 442 |
std::copy(nodes.begin(), nodes.end(), ids); |
| 443 |
#ifdef _MSC_VER |
| 444 |
#pragma warning(pop) |
| 445 |
#endif |
| 446 |
} |
| 447 |
else { |
| 448 |
auto *ids = static_cast<int64_t *>(data); |
| 449 |
std::copy(nodes.begin(), nodes.end(), ids); |
| 450 |
} |
| 451 |
} |
| 452 |
else if (field.get_name() == "distribution_factors") { |
| 453 |
if (m_useVariableDf) { |
| 454 |
const Ioss::Field &id_fld = ns->get_fieldref("ids"); |
| 455 |
std::vector<char> ids(id_fld.get_size()); |
| 456 |
get_field_internal(ns, id_fld, Data(ids), id_fld.get_size()); |
| 457 |
fill_transient_data(ns, field, data, Data(ids), num_to_get); |
| 458 |
} |
| 459 |
else { |
| 460 |
fill_constant_data(field, data, 1.0); |
| 461 |
} |
| 462 |
} |
| 463 |
else { |
| 464 |
num_to_get = Ioss::Utils::field_warning(ns, field, "input"); |
| 465 |
} |
| 466 |
} |
| 467 |
else if (role == Ioss::Field::TRANSIENT) { |
| 468 |
const Ioss::Field &id_fld = ns->get_fieldref("ids"); |
| 469 |
std::vector<char> ids(id_fld.get_size()); |
| 470 |
get_field_internal(ns, id_fld, Data(ids), id_fld.get_size()); |
| 471 |
fill_transient_data(ns, field, data, Data(ids), num_to_get, currentTime); |
| 472 |
} |
| 473 |
return num_to_get; |
| 474 |
} |
| 475 |
|
| 476 |
int64_t DatabaseIO::get_field_internal(const Ioss::CommSet *cs, const Ioss::Field &field, |
| 477 |
void *data, size_t data_size) const |
| 478 |
{ |
| 479 |
size_t num_to_get = field.verify(data_size); |
| 480 |
size_t entity_count = cs->entity_count(); |
| 481 |
assert(num_to_get == entity_count); |
| 482 |
|
| 483 |
// Return the <entity (node or face), processor> pair |
| 484 |
if (field.get_name() == "entity_processor" || field.get_name() == "entity_processor_raw") { |
| 485 |
|
| 486 |
// Check type -- node or face |
| 487 |
std::string type = cs->get_property("entity_type").get_string(); |
| 488 |
|
| 489 |
if (type == "node") { |
| 490 |
// Allocate temporary storage space |
| 491 |
Ioss::Int64Vector entities(num_to_get); |
| 492 |
Ioss::IntVector procs(num_to_get); |
| 493 |
m_generatedMesh->node_communication_map(entities, procs); |
| 494 |
|
| 495 |
// and store in 'data' ... |
| 496 |
if (field.is_type(Ioss::Field::INTEGER)) { |
| 497 |
int *entity_proc = static_cast<int *>(data); |
| 498 |
|
| 499 |
size_t j = 0; |
| 500 |
for (size_t i = 0; i < entity_count; i++) { |
| 501 |
assert(entities[i] > 0); |
| 502 |
entity_proc[j++] = static_cast<int>(entities[i]); |
| 503 |
entity_proc[j++] = procs[i]; |
| 504 |
} |
| 505 |
|
| 506 |
if (field.get_name() == "entity_processor_raw") { |
| 507 |
map_global_to_local(get_node_map(), 2 * entity_count, 2, entity_proc); |
| 508 |
} |
| 509 |
} |
| 510 |
else { |
| 511 |
auto *entity_proc = static_cast<int64_t *>(data); |
| 512 |
|
| 513 |
size_t j = 0; |
| 514 |
for (size_t i = 0; i < entity_count; i++) { |
| 515 |
assert(entities[i] > 0); |
| 516 |
entity_proc[j++] = entities[i]; |
| 517 |
entity_proc[j++] = procs[i]; |
| 518 |
} |
| 519 |
|
| 520 |
if (field.get_name() == "entity_processor_raw") { |
| 521 |
map_global_to_local(get_node_map(), 2 * entity_count, 2, entity_proc); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
else { |
| 526 |
IOSS_ERROR(fmt::format("Invalid commset type {}", type)); |
| 527 |
} |
| 528 |
} |
| 529 |
else if (field.get_name() == "ids") { |
| 530 |
// Do nothing, just handles an idiosyncrasy of the GroupingEntity |
| 531 |
} |
| 532 |
else { |
| 533 |
num_to_get = Ioss::Utils::field_warning(cs, field, "input"); |
| 534 |
} |
| 535 |
return num_to_get; |
| 536 |
} |
| 537 |
|
| 538 |
const Ioss::Map &DatabaseIO::get_node_map() const |
| 539 |
{ |
| 540 |
// Allocate space for node number map and read it in... |
| 541 |
// Can be called multiple times, allocate 1 time only |
| 542 |
if (nodeMap.map().empty()) { |
| 543 |
nodeMap.set_size(nodeCount); |
| 544 |
std::vector<int64_t> map; |
| 545 |
m_generatedMesh->node_map(map); |
| 546 |
nodeMap.set_map(Data(map), map.size(), 0, true); |
| 547 |
} |
| 548 |
return nodeMap; |
| 549 |
} |
| 550 |
|
| 551 |
const Ioss::Map &DatabaseIO::get_element_map() const |
| 552 |
{ |
| 553 |
// Allocate space for element number map and read it in... |
| 554 |
// Can be called multiple times, allocate 1 time only |
| 555 |
if (elemMap.map().empty()) { |
| 556 |
elemMap.set_size(elementCount); |
| 557 |
std::vector<int64_t> map; |
| 558 |
m_generatedMesh->element_map(map); |
| 559 |
elemMap.set_map(Data(map), map.size(), 0, true); |
| 560 |
} |
| 561 |
return elemMap; |
| 562 |
} |
| 563 |
|
| 564 |
void DatabaseIO::get_nodeblocks() |
| 565 |
{ |
| 566 |
std::string block_name = "nodeblock_1"; |
| 567 |
auto block = new Ioss::NodeBlock(this, block_name, m_generatedMesh->node_count_proc(), 3); |
| 568 |
block->property_add(Ioss::Property("id", 1)); |
| 569 |
block->property_add(Ioss::Property("guid", util().generate_guid(1))); |
| 570 |
get_region()->add(block); |
| 571 |
add_transient_fields(block); |
| 572 |
} |
| 573 |
|
| 574 |
void DatabaseIO::get_step_times_nl() |
| 575 |
{ |
| 576 |
auto time_step_count = m_generatedMesh->timestep_count(); |
| 577 |
for (int i = 0; i < time_step_count; i++) { |
| 578 |
double time = m_generatedMesh->timestep_initial() + i * m_generatedMesh->timestep_interval(); |
| 579 |
get_region()->add_state(time); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
std::vector<double> DatabaseIO::get_db_step_times_nl() |
| 584 |
{ |
| 585 |
std::vector<double> timesteps; |
| 586 |
|
| 587 |
int time_step_count = m_generatedMesh->timestep_count(); |
| 588 |
timesteps.reserve(time_step_count); |
| 589 |
for (int i = 0; i < time_step_count; i++) { |
| 590 |
double time = m_generatedMesh->timestep_initial() + i * m_generatedMesh->timestep_interval(); |
| 591 |
timesteps.push_back(time); |
| 592 |
} |
| 593 |
|
| 594 |
return timesteps; |
| 595 |
} |
| 596 |
|
| 597 |
void DatabaseIO::get_elemblocks() |
| 598 |
{ |
| 599 |
// Attributes of an element block are: |
| 600 |
// -- id |
| 601 |
// -- name |
| 602 |
// -- element type |
| 603 |
// -- number of elements |
| 604 |
// -- number of attributes per element |
| 605 |
// -- number of nodes per element (derivable from type) |
| 606 |
// -- number of faces per element (derivable from type) |
| 607 |
// -- number of edges per element (derivable from type) |
| 608 |
|
| 609 |
auto block_count = m_generatedMesh->block_count(); |
| 610 |
for (int i = 0; i < block_count; i++) { |
| 611 |
std::string name = Ioss::Utils::encode_entity_name("block", i + 1); |
| 612 |
std::string type = m_generatedMesh->topology_type(i + 1).first; |
| 613 |
size_t element_count = m_generatedMesh->element_count_proc(i + 1); |
| 614 |
auto block = new Ioss::ElementBlock(this, name, type, element_count); |
| 615 |
|
| 616 |
block->property_add(Ioss::Property("id", i + 1)); |
| 617 |
block->property_add(Ioss::Property("guid", util().generate_guid(i + 1))); |
| 618 |
block->property_add(Ioss::Property("original_block_order", i)); |
| 619 |
|
| 620 |
block->property_add( |
| 621 |
Ioss::Property("global_entity_count", m_generatedMesh->element_count(i + 1))); |
| 622 |
|
| 623 |
if (type == "shell4" || type == "tri3") { |
| 624 |
block->field_add( |
| 625 |
Ioss::Field("thickness", Ioss::Field::REAL, "scalar", Ioss::Field::ATTRIBUTE)); |
| 626 |
} |
| 627 |
|
| 628 |
get_region()->add(block); |
| 629 |
add_transient_fields(block); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
void DatabaseIO::get_nodesets() |
| 634 |
{ |
| 635 |
// Attributes of a nodeset are: |
| 636 |
// -- id |
| 637 |
// -- name |
| 638 |
// -- number of nodes |
| 639 |
// -- number of distribution factors (see next comment) |
| 640 |
// ----the #distribution factors should equal #nodes or 0, any |
| 641 |
// other value does not make sense. If it is 0, then a substitute |
| 642 |
// list will be created returning 1.0 for the factor |
| 643 |
|
| 644 |
// In a parallel execution, it is possible that a nodeset will have |
| 645 |
// no nodes or distribution factors on a particular processor... |
| 646 |
|
| 647 |
// Get nodeset metadata |
| 648 |
for (int ins = 0; ins < nodesetCount; ins++) { |
| 649 |
int64_t number_nodes = m_generatedMesh->nodeset_node_count_proc(ins + 1); |
| 650 |
|
| 651 |
std::string name = Ioss::Utils::encode_entity_name("nodelist", ins + 1); |
| 652 |
auto nodeset = new Ioss::NodeSet(this, name, number_nodes); |
| 653 |
nodeset->property_add(Ioss::Property("id", ins + 1)); |
| 654 |
nodeset->property_add(Ioss::Property("guid", util().generate_guid(ins + 1))); |
| 655 |
get_region()->add(nodeset); |
| 656 |
add_transient_fields(nodeset); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
std::string DatabaseIO::get_sideset_topology() const |
| 661 |
{ |
| 662 |
return m_generatedMesh->get_sideset_topology(); |
| 663 |
} |
| 664 |
|
| 665 |
void DatabaseIO::get_sidesets() |
| 666 |
{ |
| 667 |
const std::string face_topo = get_sideset_topology(); |
| 668 |
|
| 669 |
m_sideset_names.reserve(sidesetCount); |
| 670 |
for (int iss = 0; iss < sidesetCount; iss++) { |
| 671 |
std::string name = Ioss::Utils::encode_entity_name("surface", iss + 1); |
| 672 |
m_sideset_names.push_back(name); |
| 673 |
auto sideset = new Ioss::SideSet(this, name); |
| 674 |
sideset->property_add(Ioss::Property("id", iss + 1)); |
| 675 |
sideset->property_add(Ioss::Property("guid", util().generate_guid(iss + 1))); |
| 676 |
get_region()->add(sideset); |
| 677 |
|
| 678 |
Ioss::NameList touching_blocks = m_generatedMesh->sideset_touching_blocks(iss + 1); |
| 679 |
if (touching_blocks.size() == 1) { |
| 680 |
std::string sd_block_name = name; |
| 681 |
sd_block_name += "_"; |
| 682 |
sd_block_name += face_topo; |
| 683 |
const std::string &side_topo_name = face_topo; |
| 684 |
std::string elem_topo_name = "unknown"; |
| 685 |
int64_t number_faces = m_generatedMesh->sideset_side_count_proc(iss + 1); |
| 686 |
|
| 687 |
auto sd_block = |
| 688 |
new Ioss::SideBlock(this, sd_block_name, side_topo_name, elem_topo_name, number_faces); |
| 689 |
sideset->add(sd_block); |
| 690 |
sd_block->property_add(Ioss::Property("id", iss + 1)); |
| 691 |
sd_block->property_add(Ioss::Property("guid", util().generate_guid(iss + 1))); |
| 692 |
|
| 693 |
std::string storage = "Real["; |
| 694 |
storage += face_topo == "quad4" ? std::to_string(4) : std::to_string(3); |
| 695 |
storage += "]"; |
| 696 |
sd_block->field_add( |
| 697 |
Ioss::Field("distribution_factors", Ioss::Field::REAL, storage, Ioss::Field::MESH)); |
| 698 |
|
| 699 |
Ioss::ElementBlock *el_block = get_region()->get_element_block(touching_blocks[0]); |
| 700 |
sd_block->set_parent_element_block(el_block); |
| 701 |
add_transient_fields(sd_block); |
| 702 |
} |
| 703 |
else { |
| 704 |
for (auto &touching_block : touching_blocks) { |
| 705 |
std::string sd_block_name = |
| 706 |
"surface_" + touching_block + "_edge2_" + std::to_string(iss + 1); |
| 707 |
const std::string &side_topo_name = face_topo; |
| 708 |
std::string elem_topo_name = "unknown"; |
| 709 |
int64_t number_faces = m_generatedMesh->sideset_side_count_proc(iss + 1); |
| 710 |
|
| 711 |
auto sd_block = new Ioss::SideBlock(this, sd_block_name, side_topo_name, elem_topo_name, |
| 712 |
number_faces); |
| 713 |
sideset->add(sd_block); |
| 714 |
sd_block->property_add(Ioss::Property("id", iss + 1)); |
| 715 |
sd_block->property_add(Ioss::Property("guid", util().generate_guid(iss + 1))); |
| 716 |
|
| 717 |
std::string storage = "Real["; |
| 718 |
storage += face_topo == "quad4" ? std::to_string(4) : std::to_string(3); |
| 719 |
storage += "]"; |
| 720 |
sd_block->field_add( |
| 721 |
Ioss::Field("distribution_factors", Ioss::Field::REAL, storage, Ioss::Field::MESH)); |
| 722 |
|
| 723 |
Ioss::ElementBlock *el_block = get_region()->get_element_block(touching_block); |
| 724 |
sd_block->set_parent_element_block(el_block); |
| 725 |
add_transient_fields(sd_block); |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
void DatabaseIO::get_commsets() |
| 732 |
{ |
| 733 |
if (util().parallel_size() > 1) { |
| 734 |
// Get size of communication map... |
| 735 |
size_t my_node_count = m_generatedMesh->communication_node_count_proc(); |
| 736 |
|
| 737 |
// Create a single node commset |
| 738 |
auto *commset = new Ioss::CommSet(this, "commset_node", "node", my_node_count); |
| 739 |
commset->property_add(Ioss::Property("id", 1)); |
| 740 |
commset->property_add(Ioss::Property("guid", util().generate_guid(1))); |
| 741 |
get_region()->add(commset); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
unsigned DatabaseIO::entity_field_support() const |
| 746 |
{ |
| 747 |
return Ioss::NODEBLOCK | Ioss::ELEMENTBLOCK | Ioss::REGION | Ioss::NODESET | Ioss::SIDESET; |
| 748 |
} |
| 749 |
|
| 750 |
void DatabaseIO::add_transient_fields(Ioss::GroupingEntity *entity) |
| 751 |
{ |
| 752 |
Ioss::EntityType type = entity->type(); |
| 753 |
size_t var_count = m_generatedMesh->get_variable_count(type); |
| 754 |
for (size_t i = 0; i < var_count; i++) { |
| 755 |
std::string var_name = entity->type_string() + "_" + std::to_string(i + 1); |
| 756 |
entity->field_add( |
| 757 |
Ioss::Field(std::move(var_name), Ioss::Field::REAL, "scalar", Ioss::Field::TRANSIENT)); |
| 758 |
} |
| 759 |
} |
| 760 |
} // namespace Iogn |