| 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_ElementTopology.h" |
| 8 |
#include "Ioss_Region.h" |
| 9 |
#include "Ioss_SmartAssert.h" |
| 10 |
#include "Ioss_Utils.h" |
| 11 |
#include "Ioss_VariableType.h" |
| 12 |
#include "exonull/Ioexnl_Utils.h" |
| 13 |
#include <cstring> |
| 14 |
#include <exodusII.h> |
| 15 |
#include <exodusII_int.h> |
| 16 |
#include <fmt/core.h> |
| 17 |
#include <fmt/format.h> |
| 18 |
#include <fmt/ostream.h> |
| 19 |
#include <iosfwd> |
| 20 |
#include <netcdf.h> |
| 21 |
#include <tokenize.h> |
| 22 |
|
| 23 |
#include "Ioss_CoordinateFrame.h" |
| 24 |
#include "Ioss_DatabaseIO.h" |
| 25 |
#include "Ioss_ElementBlock.h" |
| 26 |
#include "Ioss_Field.h" |
| 27 |
#include "Ioss_GroupingEntity.h" |
| 28 |
#include "Ioss_ParallelUtils.h" |
| 29 |
#include "Ioss_Property.h" |
| 30 |
|
| 31 |
namespace { |
| 32 |
size_t match(const std::string &name1, const std::string &name2) |
| 33 |
{ |
| 34 |
size_t l1 = name1.size(); |
| 35 |
size_t l2 = name2.size(); |
| 36 |
size_t len = std::min(l1, l2); |
| 37 |
for (size_t i = 0; i < len; i++) { |
| 38 |
if (name1[i] != name2[i]) { |
| 39 |
while (i > 0 && (isdigit(name1[i - 1]) != 0) && (isdigit(name2[i - 1]) != 0)) { |
| 40 |
i--; |
| 41 |
// Back up to first non-digit so to handle "evar0000, evar0001, ..., evar 1123" |
| 42 |
} |
| 43 |
return i; |
| 44 |
} |
| 45 |
} |
| 46 |
return len; |
| 47 |
} |
| 48 |
|
| 49 |
template <typename INT> |
| 50 |
void internal_write_coordinate_frames(int exoid, const Ioss::CoordinateFrameContainer &frames, |
| 51 |
INT /*dummy*/) |
| 52 |
{ |
| 53 |
// Query number of coordinate frames... |
| 54 |
int nframes = static_cast<int>(frames.size()); |
| 55 |
if (nframes > 0) { |
| 56 |
std::vector<char> tags(nframes); |
| 57 |
std::vector<double> coordinates(nframes * 9); |
| 58 |
std::vector<INT> ids(nframes); |
| 59 |
|
| 60 |
for (size_t i = 0; i < frames.size(); i++) { |
| 61 |
ids[i] = frames[i].id(); |
| 62 |
tags[i] = frames[i].tag(); |
| 63 |
const double *coord = frames[i].coordinates(); |
| 64 |
for (size_t j = 0; j < 9; j++) { |
| 65 |
coordinates[9 * i + j] = coord[j]; |
| 66 |
} |
| 67 |
} |
| 68 |
int ierr = ex_put_coordinate_frames(exoid, nframes, Data(ids), Data(coordinates), Data(tags)); |
| 69 |
if (ierr < 0) { |
| 70 |
Ioexnl::exodus_error(exoid, __LINE__, __func__, __FILE__); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace |
| 76 |
|
| 77 |
namespace Ioexnl { |
| 78 |
void update_last_time_attribute(int exodusFilePtr, double value) |
| 79 |
{ |
| 80 |
double tmp = 0.0; |
| 81 |
int rootid = static_cast<unsigned>(exodusFilePtr) & EX_FILE_ID_MASK; |
| 82 |
int status = nc_get_att_double(rootid, NC_GLOBAL, "last_written_time", &tmp); |
| 83 |
|
| 84 |
if (status == NC_NOERR && value > tmp) { |
| 85 |
status = nc_put_att_double(rootid, NC_GLOBAL, "last_written_time", NC_DOUBLE, 1, &value); |
| 86 |
if (status != NC_NOERR) { |
| 87 |
ex_opts(EX_VERBOSE); |
| 88 |
auto errmsg = fmt::format( |
| 89 |
"Error: failed to define 'last_written_time' attribute to file id {}", exodusFilePtr); |
| 90 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
Ioss::EntityType map_exodus_type(ex_entity_type type) |
| 96 |
{ |
| 97 |
switch (type) { |
| 98 |
case EX_ASSEMBLY: return Ioss::ASSEMBLY; |
| 99 |
case EX_BLOB: return Ioss::BLOB; |
| 100 |
case EX_EDGE_BLOCK: return Ioss::EDGEBLOCK; |
| 101 |
case EX_EDGE_SET: return Ioss::EDGESET; |
| 102 |
case EX_ELEM_BLOCK: return Ioss::ELEMENTBLOCK; |
| 103 |
case EX_ELEM_SET: return Ioss::ELEMENTSET; |
| 104 |
case EX_FACE_BLOCK: return Ioss::FACEBLOCK; |
| 105 |
case EX_FACE_SET: return Ioss::FACESET; |
| 106 |
case EX_NODAL: return Ioss::NODEBLOCK; |
| 107 |
case EX_NODE_SET: return Ioss::NODESET; |
| 108 |
case EX_SIDE_SET: return Ioss::SIDESET; |
| 109 |
case EX_GLOBAL: return Ioss::REGION; |
| 110 |
default: return Ioss::INVALID_TYPE; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
ex_entity_type map_exodus_type(Ioss::EntityType type) |
| 115 |
{ |
| 116 |
switch (type) { |
| 117 |
case Ioss::REGION: return EX_GLOBAL; |
| 118 |
case Ioss::ASSEMBLY: return EX_ASSEMBLY; |
| 119 |
case Ioss::BLOB: return EX_BLOB; |
| 120 |
case Ioss::EDGEBLOCK: return EX_EDGE_BLOCK; |
| 121 |
case Ioss::EDGESET: return EX_EDGE_SET; |
| 122 |
case Ioss::ELEMENTBLOCK: return EX_ELEM_BLOCK; |
| 123 |
case Ioss::ELEMENTSET: return EX_ELEM_SET; |
| 124 |
case Ioss::FACEBLOCK: return EX_FACE_BLOCK; |
| 125 |
case Ioss::FACESET: return EX_FACE_SET; |
| 126 |
case Ioss::NODEBLOCK: return EX_NODAL; |
| 127 |
case Ioss::NODESET: return EX_NODE_SET; |
| 128 |
case Ioss::SIDESET: return EX_SIDE_SET; |
| 129 |
case Ioss::SIDEBLOCK: return EX_SIDE_SET; |
| 130 |
case Ioss::COMMSET: return static_cast<ex_entity_type>(0); |
| 131 |
default: return EX_INVALID; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
bool read_last_time_attribute(int exodusFilePtr, double *value) |
| 136 |
{ |
| 137 |
// Check whether the "last_written_time" attribute exists. If it does, |
| 138 |
// return the value of the attribute in 'value' and return 'true'. |
| 139 |
// If not, don't change 'value' and return 'false'. |
| 140 |
bool found = false; |
| 141 |
|
| 142 |
int rootid = static_cast<unsigned>(exodusFilePtr) & EX_FILE_ID_MASK; |
| 143 |
nc_type att_type = NC_NAT; |
| 144 |
size_t att_len = 0; |
| 145 |
int status = nc_inq_att(rootid, NC_GLOBAL, "last_written_time", &att_type, &att_len); |
| 146 |
if (status == NC_NOERR && att_type == NC_DOUBLE) { |
| 147 |
// Attribute exists on this database, read it... |
| 148 |
double tmp = 0.0; |
| 149 |
status = nc_get_att_double(rootid, NC_GLOBAL, "last_written_time", &tmp); |
| 150 |
if (status == NC_NOERR) { |
| 151 |
*value = tmp; |
| 152 |
found = true; |
| 153 |
} |
| 154 |
else { |
| 155 |
ex_opts(EX_VERBOSE); |
| 156 |
auto errmsg = fmt::format( |
| 157 |
"Error: failed to read last_written_time attribute from file id {}", exodusFilePtr); |
| 158 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 159 |
found = false; |
| 160 |
} |
| 161 |
} |
| 162 |
return found; |
| 163 |
} |
| 164 |
|
| 165 |
bool check_processor_info(const std::string &filename, int exodusFilePtr, int processor_count, |
| 166 |
int processor_id) |
| 167 |
{ |
| 168 |
// A restart file may contain an attribute which contains |
| 169 |
// information about the processor count and current processor id |
| 170 |
// when the file was written. This code checks whether that |
| 171 |
// information matches the current processor count and id. If it |
| 172 |
// exists, but doesn't match, a warning message is printed. |
| 173 |
// Eventually, this will be used to determine whether certain |
| 174 |
// decomposition-related data in the file is valid or has been |
| 175 |
// invalidated by a join/re-spread to a different number of |
| 176 |
// processors. |
| 177 |
bool matches = true; |
| 178 |
|
| 179 |
nc_type att_type = NC_NAT; |
| 180 |
size_t att_len = 0; |
| 181 |
int status = nc_inq_att(exodusFilePtr, NC_GLOBAL, "processor_info", &att_type, &att_len); |
| 182 |
if (status == NC_NOERR && att_type == NC_INT) { |
| 183 |
// Attribute exists on this database, read it and check that the information |
| 184 |
// matches the current processor count and processor id. |
| 185 |
int proc_info[2]; |
| 186 |
status = nc_get_att_int(exodusFilePtr, NC_GLOBAL, "processor_info", proc_info); |
| 187 |
if (status == NC_NOERR) { |
| 188 |
if (proc_info[0] != processor_count && proc_info[0] > 1) { |
| 189 |
fmt::print(Ioss::WarnOut(), |
| 190 |
"Processor decomposition count in file ({}) does not match current " |
| 191 |
"processor count ({}) in file named '{}'.\n", |
| 192 |
proc_info[0], processor_count, filename); |
| 193 |
matches = false; |
| 194 |
} |
| 195 |
if (proc_info[1] != processor_id) { |
| 196 |
fmt::print( |
| 197 |
Ioss::WarnOut(), |
| 198 |
"The file '{}' was originally written on processor {}, but is now being read on " |
| 199 |
"processor {}.\n" |
| 200 |
"This may cause problems if there is any processor-dependent data on the file.\n", |
| 201 |
filename, proc_info[1], processor_id); |
| 202 |
matches = false; |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
ex_opts(EX_VERBOSE); |
| 207 |
auto errmsg = |
| 208 |
fmt::format("Error: failed to read processor info attribute from file {}", filename); |
| 209 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 210 |
return EX_FATAL != 0; |
| 211 |
} |
| 212 |
} |
| 213 |
return matches; |
| 214 |
} |
| 215 |
|
| 216 |
bool type_match(const std::string &type, const char *substring) |
| 217 |
{ |
| 218 |
// Returns true if 'substring' is a sub-string of 'type'. |
| 219 |
// The comparisons are case-insensitive |
| 220 |
// 'substring' is required to be in all lowercase. |
| 221 |
const char *s = substring; |
| 222 |
const char *t = type.c_str(); |
| 223 |
|
| 224 |
SMART_ASSERT(s != nullptr); |
| 225 |
while (*s != '\0' && *t != '\0') { |
| 226 |
if (*s++ != tolower(*t++)) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
void decode_surface_name(Ioexnl::SideSetMap &fs_map, Ioexnl::SideSetSet &fs_set, |
| 234 |
const std::string &name) |
| 235 |
{ |
| 236 |
auto tokens = Ioss::tokenize(name, "_"); |
| 237 |
if (tokens.size() >= 4) { |
| 238 |
// Name of form: "name_eltopo_sidetopo_id" or |
| 239 |
// "name_block_id_sidetopo_id" "name" is typically "surface". |
| 240 |
// The sideset containing this should then be called "name_id" |
| 241 |
|
| 242 |
// Check whether the second-last token is a side topology and |
| 243 |
// the third-last token is an element topology. |
| 244 |
const Ioss::ElementTopology *side_topo = |
| 245 |
Ioss::ElementTopology::factory(tokens[tokens.size() - 2], true); |
| 246 |
if (side_topo != nullptr) { |
| 247 |
const Ioss::ElementTopology *element_topo = |
| 248 |
Ioss::ElementTopology::factory(tokens[tokens.size() - 3], true); |
| 249 |
if (element_topo != nullptr || tokens[tokens.size() - 4] == "block") { |
| 250 |
// The remainder of the tokens will be used to create |
| 251 |
// a side set name and then this sideset will be |
| 252 |
// a side block in that set. |
| 253 |
std::string fs_name; |
| 254 |
size_t last_token = tokens.size() - 3; |
| 255 |
if (element_topo == nullptr) { |
| 256 |
last_token--; |
| 257 |
} |
| 258 |
for (size_t tok = 0; tok < last_token; tok++) { |
| 259 |
fs_name += tokens[tok]; |
| 260 |
} |
| 261 |
fs_name += "_"; |
| 262 |
fs_name += tokens[tokens.size() - 1]; // Add on the id. |
| 263 |
|
| 264 |
fs_set.insert(fs_name); |
| 265 |
fs_map.insert(Ioexnl::SideSetMap::value_type(name, fs_name)); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
bool set_id(const Ioss::GroupingEntity *entity, Ioexnl::EntityIdSet *idset) |
| 272 |
{ |
| 273 |
// See description of 'get_id' function. This function just primes |
| 274 |
// the idset with existing ids so that when we start generating ids, |
| 275 |
// we don't overwrite an existing one. |
| 276 |
|
| 277 |
// Avoid a few string constructors/destructors |
| 278 |
static std::string id_prop("id"); |
| 279 |
|
| 280 |
bool succeed = false; |
| 281 |
if (entity->property_exists(id_prop)) { |
| 282 |
int64_t id = entity->get_property(id_prop).get_int(); |
| 283 |
|
| 284 |
// See whether it already exists... |
| 285 |
auto type = map_exodus_type(entity->type()); |
| 286 |
succeed = idset->insert(std::make_pair(static_cast<int>(type), id)).second; |
| 287 |
if (!succeed) { |
| 288 |
// Need to remove the property so it doesn't cause problems |
| 289 |
// later... |
| 290 |
auto *new_entity = const_cast<Ioss::GroupingEntity *>(entity); |
| 291 |
new_entity->property_erase(id_prop); |
| 292 |
SMART_ASSERT(!entity->property_exists(id_prop))(id_prop); |
| 293 |
} |
| 294 |
} |
| 295 |
return succeed; |
| 296 |
} |
| 297 |
|
| 298 |
// Potentially extract the id from a name possibly of the form name_id. |
| 299 |
// If not of this form, return 0; |
| 300 |
int64_t extract_id(const std::string &name_id) |
| 301 |
{ |
| 302 |
auto tokens = Ioss::tokenize(name_id, "_"); |
| 303 |
|
| 304 |
if (tokens.size() == 1) { |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
// Check whether last token is an integer... |
| 309 |
std::string str_id = tokens.back(); |
| 310 |
std::size_t found = str_id.find_first_not_of("0123456789"); |
| 311 |
if (found == std::string::npos) { |
| 312 |
// All digits... |
| 313 |
return std::stoll(str_id); |
| 314 |
} |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
int64_t get_id(const Ioss::GroupingEntity *entity, Ioexnl::EntityIdSet *idset) |
| 320 |
{ |
| 321 |
// Sierra uses names to refer to grouping entities; however, |
| 322 |
// exodusII requires integer ids. When reading an exodusII file, |
| 323 |
// the DatabaseIO creates a name by concatenating the entity |
| 324 |
// type (e.g., 'block') and the id separated by an underscore. For |
| 325 |
// example, an exodusII element block with an id of 100 would be |
| 326 |
// encoded into "block_100" |
| 327 |
|
| 328 |
// This routine tries to determine the id of the entity using 3 |
| 329 |
// approaches: |
| 330 |
// |
| 331 |
// 1. If the entity contains a property named 'id', this is used. |
| 332 |
// The DatabaseIO actually stores the id in the "id" property; |
| 333 |
// however, other grouping entity creators are not required to do |
| 334 |
// this so the property is not guaranteed to exist. |
| 335 |
// |
| 336 |
// 2.If property does not exist, it tries to decode the entity name |
| 337 |
// based on the above encoding. Again, it is not required that the |
| 338 |
// name follow this convention so success is not guaranteed. |
| 339 |
// |
| 340 |
// 3. If all other schemes fail, the routine picks an id for the entity |
| 341 |
// and returns it. It also stores this id in the "id" property so an |
| 342 |
// entity will always return the same id for multiple calls. |
| 343 |
// Note that this violates the 'const'ness of the entity so we use |
| 344 |
// a const-cast. |
| 345 |
|
| 346 |
// Avoid a few string constructors/destructors |
| 347 |
static std::string prop_name("name"); |
| 348 |
static std::string id_prop("id"); |
| 349 |
|
| 350 |
int64_t id = 1; |
| 351 |
|
| 352 |
if (entity->property_exists(id_prop)) { |
| 353 |
id = entity->get_property(id_prop).get_int(); |
| 354 |
return id; |
| 355 |
} |
| 356 |
|
| 357 |
// Try to decode an id from the name. |
| 358 |
std::string name_string = entity->get_property(prop_name).get_string(); |
| 359 |
std::string type_name = entity->short_type_string(); |
| 360 |
if (std::strncmp(type_name.c_str(), name_string.c_str(), type_name.size()) == 0) { |
| 361 |
id = extract_id(name_string); |
| 362 |
if (id <= 0) { |
| 363 |
id = 1; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// At this point, we either have an id equal to '1' or we have an id |
| 368 |
// extracted from the entities name. Increment it until it is |
| 369 |
// unique... |
| 370 |
ex_entity_type type = map_exodus_type(entity->type()); |
| 371 |
while (idset->find(std::make_pair(static_cast<int>(type), id)) != idset->end()) { |
| 372 |
++id; |
| 373 |
} |
| 374 |
|
| 375 |
// 'id' is a unique id for this entity type... |
| 376 |
idset->insert(std::make_pair(static_cast<int>(type), id)); |
| 377 |
auto *new_entity = const_cast<Ioss::GroupingEntity *>(entity); |
| 378 |
new_entity->property_add(Ioss::Property(id_prop, id)); |
| 379 |
new_entity->property_update("guid", entity->get_database()->util().generate_guid(id)); |
| 380 |
return id; |
| 381 |
} |
| 382 |
|
| 383 |
bool find_displacement_field(Ioss::NameList &fields, const Ioss::GroupingEntity *block, int ndim, |
| 384 |
std::string *disp_name) |
| 385 |
{ |
| 386 |
// This is a kluge to work with many of the SEACAS codes. The |
| 387 |
// convention used (in Blot and others) is that the first 'ndim' |
| 388 |
// nodal variables are assumed to be displacements *if* the first |
| 389 |
// character of the names is 'D' and the last characters match the |
| 390 |
// coordinate labels (typically 'X', 'Y', and 'Z'). This routine |
| 391 |
// looks for the field that has the longest match with the string |
| 392 |
// "displacement" and is of the correct storage type (VECTOR_2D or |
| 393 |
// VECTOR_3D). If found, it returns the name. |
| 394 |
// |
| 395 |
|
| 396 |
static const std::string displace = "displacement"; |
| 397 |
|
| 398 |
size_t max_span = 0; |
| 399 |
for (const auto &name : fields) { |
| 400 |
std::string lc_name(name); |
| 401 |
|
| 402 |
Ioss::Utils::fixup_name(lc_name); |
| 403 |
size_t span = match(lc_name, displace); |
| 404 |
if (span > max_span) { |
| 405 |
const Ioss::VariableType *var_type = block->get_field(name).transformed_storage(); |
| 406 |
int comp_count = var_type->component_count(); |
| 407 |
if (comp_count == ndim) { |
| 408 |
max_span = span; |
| 409 |
*disp_name = name; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
return max_span > 0; |
| 414 |
} |
| 415 |
|
| 416 |
void fix_bad_name(char *name) |
| 417 |
{ |
| 418 |
SMART_ASSERT(name != nullptr); |
| 419 |
|
| 420 |
size_t len = std::strlen(name); |
| 421 |
for (size_t i = 0; i < len; i++) { |
| 422 |
if (name[i] < 32 || name[i] > 126) { |
| 423 |
// Zero out entire name if a bad character found anywhere in the name. |
| 424 |
for (size_t j = 0; j < len; j++) { |
| 425 |
name[j] = '\0'; |
| 426 |
} |
| 427 |
return; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
void exodus_error(int exoid, int lineno, const char *function, const char *filename) |
| 433 |
{ |
| 434 |
std::string empty{}; |
| 435 |
exodus_error(exoid, lineno, function, filename, empty); |
| 436 |
} |
| 437 |
|
| 438 |
void exodus_error(int exoid, int lineno, const char *function, const char *filename, |
| 439 |
const std::string &extra) |
| 440 |
{ |
| 441 |
std::ostringstream errmsg; |
| 442 |
// Create errmsg here so that the exerrval doesn't get cleared by |
| 443 |
// the ex_close call. |
| 444 |
int status; |
| 445 |
ex_get_err(nullptr, nullptr, &status); |
| 446 |
fmt::print(errmsg, "Exodus error ({}) {} at line {} of file '{}' in function '{}'.", status, |
| 447 |
ex_strerror(status), lineno, filename, function); |
| 448 |
|
| 449 |
if (!extra.empty()) { |
| 450 |
fmt::print(errmsg, " {}", extra); |
| 451 |
} |
| 452 |
fmt::print(errmsg, " Please report to [email protected] if you need help."); |
| 453 |
|
| 454 |
ex_err_fn(exoid, nullptr, nullptr, EX_PRTLASTMSG); |
| 455 |
IOSS_ERROR(errmsg); |
| 456 |
} |
| 457 |
|
| 458 |
void write_coordinate_frames(int exoid, const Ioss::CoordinateFrameContainer &frames) |
| 459 |
{ |
| 460 |
if ((ex_int64_status(exoid) & EX_BULK_INT64_API) != 0) { |
| 461 |
internal_write_coordinate_frames(exoid, frames, static_cast<int64_t>(0)); |
| 462 |
} |
| 463 |
else { |
| 464 |
internal_write_coordinate_frames(exoid, frames, 0); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
bool filter_node_list(Ioss::Int64Vector &nodes, |
| 469 |
const std::vector<unsigned char> &node_connectivity_status) |
| 470 |
{ |
| 471 |
// Iterate through 'nodes' and determine which of the nodes are |
| 472 |
// not connected to any non-omitted blocks. The index of these |
| 473 |
// nodes is then put in the 'nodes' list. |
| 474 |
// Assumes that there is at least one omitted element block. The |
| 475 |
// 'nodes' list on entry contains 1-based local node ids, not global. |
| 476 |
// On return, the nodes list contains indices. To filter a nodeset list: |
| 477 |
// for (size_t i = 0; i < nodes.size(); i++) { |
| 478 |
// active_values[i] = some_nset_values[nodes[i]]; |
| 479 |
// } |
| 480 |
|
| 481 |
size_t orig_size = nodes.size(); |
| 482 |
size_t active = 0; |
| 483 |
for (size_t i = 0; i < orig_size; i++) { |
| 484 |
if (node_connectivity_status[nodes[i] - 1] >= 2) { |
| 485 |
// Node is connected to at least 1 active element... |
| 486 |
nodes[active++] = i; |
| 487 |
} |
| 488 |
} |
| 489 |
nodes.resize(active); |
| 490 |
nodes.shrink_to_fit(); // shrink to fit |
| 491 |
return active != orig_size; |
| 492 |
} |
| 493 |
|
| 494 |
void filter_element_list(Ioss::Region *region, Ioss::Int64Vector &elements, |
| 495 |
Ioss::Int64Vector &sides, bool remove_omitted_elements) |
| 496 |
{ |
| 497 |
// Iterate through 'elements' and remove the elements which are in an omitted block. |
| 498 |
// Precondition is that there is at least one omitted element block. |
| 499 |
// The 'elements' list contains local element ids, not global. |
| 500 |
// Since there are typically a small number of omitted blocks, do |
| 501 |
// the following: |
| 502 |
// For each omitted block, determine the min and max element id in |
| 503 |
// that block. Iterate 'elements' vector and set the id to zero if |
| 504 |
// min <= id <= max. Once all omitted blocks have been processed, |
| 505 |
// then iterate the vector and compress out all zeros. Keep 'sides' |
| 506 |
// array consistent. |
| 507 |
|
| 508 |
// Get all element blocks in region... |
| 509 |
bool omitted = false; |
| 510 |
const Ioss::ElementBlockContainer &element_blocks = region->get_element_blocks(); |
| 511 |
for (const auto &block : element_blocks) { |
| 512 |
|
| 513 |
if (Ioss::Utils::block_is_omitted(block)) { |
| 514 |
int64_t min_id = block->get_offset() + 1; |
| 515 |
int64_t max_id = min_id + block->entity_count() - 1; |
| 516 |
for (size_t i = 0; i < elements.size(); i++) { |
| 517 |
if (min_id <= elements[i] && elements[i] <= max_id) { |
| 518 |
omitted = true; |
| 519 |
elements[i] = 0; |
| 520 |
sides[i] = 0; |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
if (remove_omitted_elements && omitted) { |
| 526 |
elements.erase(std::remove(elements.begin(), elements.end(), 0), elements.end()); |
| 527 |
sides.erase(std::remove(sides.begin(), sides.end(), 0), sides.end()); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
void separate_surface_element_sides(Ioss::Int64Vector &element, Ioss::Int64Vector &sides, |
| 532 |
Ioss::Region *region, Ioexnl::TopologyMap &topo_map, |
| 533 |
Ioexnl::TopologyMap &side_map, |
| 534 |
Ioss::SurfaceSplitType split_type, |
| 535 |
const std::string &surface_name) |
| 536 |
{ |
| 537 |
if (!element.empty()) { |
| 538 |
Ioss::ElementBlock *block = nullptr; |
| 539 |
// Topology of sides in current element block |
| 540 |
const Ioss::ElementTopology *common_ftopo = nullptr; |
| 541 |
const Ioss::ElementTopology *topo = nullptr; // Topology of current side |
| 542 |
int64_t current_side = -1; |
| 543 |
|
| 544 |
for (size_t iel = 0; iel < element.size(); iel++) { |
| 545 |
int64_t elem_id = element[iel]; |
| 546 |
if (elem_id <= 0) { |
| 547 |
std::ostringstream errmsg; |
| 548 |
fmt::print(errmsg, |
| 549 |
"ERROR: In sideset/surface '{}' an element with id {} is specified. Element " |
| 550 |
"ids must be greater than zero. ({})", |
| 551 |
surface_name, elem_id, __func__); |
| 552 |
IOSS_ERROR(errmsg); |
| 553 |
} |
| 554 |
if (block == nullptr || !block->contains(elem_id)) { |
| 555 |
block = region->get_element_block(elem_id); |
| 556 |
SMART_ASSERT(block != nullptr); |
| 557 |
SMART_ASSERT(!Ioss::Utils::block_is_omitted(block)); // Filtered out above. |
| 558 |
|
| 559 |
// nullptr if hetero sides on element |
| 560 |
common_ftopo = block->topology()->boundary_type(0); |
| 561 |
if (common_ftopo != nullptr) { |
| 562 |
topo = common_ftopo; |
| 563 |
} |
| 564 |
current_side = -1; |
| 565 |
} |
| 566 |
|
| 567 |
if (common_ftopo == nullptr && sides[iel] != current_side) { |
| 568 |
current_side = sides[iel]; |
| 569 |
if (current_side <= 0 || current_side > block->topology()->number_boundaries()) { |
| 570 |
std::ostringstream errmsg; |
| 571 |
fmt::print( |
| 572 |
errmsg, |
| 573 |
"ERROR: In sideset/surface '{}' for the element with id {} of topology '{}';\n\t" |
| 574 |
"an invalid face index '{}' is specified.\n\tFace indices " |
| 575 |
"must be between 1 and {}. ({})", |
| 576 |
surface_name, fmt::group_digits(elem_id), block->topology()->name(), current_side, |
| 577 |
block->topology()->number_boundaries(), __func__); |
| 578 |
IOSS_ERROR(errmsg); |
| 579 |
} |
| 580 |
topo = block->topology()->boundary_type(sides[iel]); |
| 581 |
SMART_ASSERT(topo != nullptr); |
| 582 |
} |
| 583 |
std::pair<std::string, const Ioss::ElementTopology *> name_topo; |
| 584 |
if (split_type == Ioss::SPLIT_BY_TOPOLOGIES) { |
| 585 |
name_topo = std::make_pair(block->topology()->name(), topo); |
| 586 |
} |
| 587 |
else if (split_type == Ioss::SPLIT_BY_ELEMENT_BLOCK) { |
| 588 |
name_topo = std::make_pair(block->name(), topo); |
| 589 |
} |
| 590 |
topo_map[name_topo]++; |
| 591 |
if (side_map[name_topo] == 0) { |
| 592 |
side_map[name_topo] = sides[iel]; |
| 593 |
} |
| 594 |
else if (side_map[name_topo] != sides[iel]) { |
| 595 |
// Not a consistent side for all sides in this |
| 596 |
// sideset. Set to large number. Note that maximum |
| 597 |
// sides/element is 6, so don't have to worry about |
| 598 |
// a valid element having 999 sides (unless go to |
| 599 |
// arbitrary polyhedra some time...) Using a large |
| 600 |
// number instead of -1 makes it easier to check the |
| 601 |
// parallel consistency... |
| 602 |
side_map[name_topo] = 999; |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
void write_reduction_attributes(int exoid, const Ioss::GroupingEntity *ge) |
| 609 |
{ |
| 610 |
Ioss::NameList properties = ge->property_describe(Ioss::Property::Origin::ATTRIBUTE); |
| 611 |
|
| 612 |
auto type = Ioexnl::map_exodus_type(ge->type()); |
| 613 |
auto id = ge->get_optional_property("id", 0); |
| 614 |
|
| 615 |
double rval = 0.0; |
| 616 |
int64_t ival = 0; |
| 617 |
for (const auto &property_name : properties) { |
| 618 |
auto prop = ge->get_property(property_name); |
| 619 |
|
| 620 |
switch (prop.get_type()) { |
| 621 |
case Ioss::Property::BasicType::REAL: |
| 622 |
rval = prop.get_real(); |
| 623 |
ex_put_double_attribute(exoid, type, id, property_name.c_str(), 1, &rval); |
| 624 |
break; |
| 625 |
case Ioss::Property::BasicType::INTEGER: |
| 626 |
ival = prop.get_int(); |
| 627 |
ex_put_integer_attribute(exoid, type, id, property_name.c_str(), 1, &ival); |
| 628 |
break; |
| 629 |
case Ioss::Property::BasicType::STRING: |
| 630 |
ex_put_text_attribute(exoid, type, id, property_name.c_str(), prop.get_string().c_str()); |
| 631 |
break; |
| 632 |
case Ioss::Property::BasicType::VEC_INTEGER: |
| 633 |
ex_put_integer_attribute(exoid, type, id, property_name.c_str(), prop.get_vec_int().size(), |
| 634 |
Data(prop.get_vec_int())); |
| 635 |
break; |
| 636 |
case Ioss::Property::BasicType::VEC_DOUBLE: |
| 637 |
ex_put_double_attribute(exoid, type, id, property_name.c_str(), |
| 638 |
prop.get_vec_double().size(), Data(prop.get_vec_double())); |
| 639 |
break; |
| 640 |
default:; // Do nothing |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
} // namespace Ioexnl |