| 1 |
/* |
| 2 |
* Copyright(C) 1999-, 20242024 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <CJ_Internals.h> |
| 10 |
|
| 11 |
#define NO_NETCDF_2 |
| 12 |
#include <exodusII.h> |
| 13 |
#include <exodusII_int.h> |
| 14 |
#include <fmt/format.h> |
| 15 |
#include <netcdf.h> |
| 16 |
#include <vector_data.h> |
| 17 |
|
| 18 |
#include <CJ_ExodusEntity.h> |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <copy_string_cpp.h> |
| 22 |
#include <cstring> |
| 23 |
#include <smart_assert.h> |
| 24 |
|
| 25 |
using entity_id = int64_t; |
| 26 |
|
| 27 |
namespace { |
| 28 |
nc_type get_type(int exoid, unsigned int type) |
| 29 |
{ |
| 30 |
if ((ex_int64_status(exoid) & type) != 0U) { |
| 31 |
return NC_INT64; |
| 32 |
} |
| 33 |
return NC_INT; |
| 34 |
} |
| 35 |
int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num, |
| 36 |
const char *stat_var, const char *id_var, const char *name_var); |
| 37 |
|
| 38 |
int put_int_array(int exoid, const char *var_type, const std::vector<int> &array); |
| 39 |
int put_id_array(int exoid, const char *var_type, const std::vector<entity_id> &ids); |
| 40 |
|
| 41 |
int define_coordinate_vars(int exodusFilePtr, size_t nodes, int node_dim, int dimension, |
| 42 |
int dim_dim, int str_dim); |
| 43 |
} // namespace |
| 44 |
|
| 45 |
Excn::Redefine::Redefine(int exoid) : exodusFilePtr(exoid) |
| 46 |
{ |
| 47 |
// Enter define mode... |
| 48 |
int status = nc_redef(exodusFilePtr); |
| 49 |
if (status != NC_NOERR) { |
| 50 |
ex_opts(EX_VERBOSE); |
| 51 |
std::string errmsg; |
| 52 |
errmsg = fmt::format("Error: failed to put file id {} into define mode", exodusFilePtr); |
| 53 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 54 |
exit(EXIT_FAILURE); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
Excn::Redefine::~Redefine() |
| 59 |
{ |
| 60 |
try { |
| 61 |
int status = nc_enddef(exodusFilePtr); |
| 62 |
if (status != NC_NOERR) { |
| 63 |
ex_opts(EX_VERBOSE); |
| 64 |
std::string errmsg; |
| 65 |
errmsg = fmt::format("Error: failed to complete variable definitions in file id {}", |
| 66 |
exodusFilePtr); |
| 67 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 68 |
exit(EXIT_FAILURE); |
| 69 |
} |
| 70 |
} |
| 71 |
catch (...) { |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
Excn::Internals::Internals(int exoid, int maximum_name_length) |
| 76 |
: exodusFilePtr(exoid), maximumNameLength(maximum_name_length) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
template <typename INT> |
| 81 |
int Excn::Internals::write_meta_data(const Mesh<INT> &mesh, const std::vector<Block> &blocks, |
| 82 |
const std::vector<NodeSet<INT>> &nodesets, |
| 83 |
const std::vector<SideSet<INT>> &sidesets, |
| 84 |
const CommunicationMetaData &comm) |
| 85 |
{ |
| 86 |
SMART_ASSERT(blocks.size() == mesh.blockCount); |
| 87 |
SMART_ASSERT(nodesets.size() == mesh.nodesetCount); |
| 88 |
SMART_ASSERT(sidesets.size() == mesh.sidesetCount); |
| 89 |
|
| 90 |
// May need to reorder the element blocks based on the 'offset_' |
| 91 |
// member. An element block contains the elements from 'offset_+1' |
| 92 |
// to 'offset_+elementCount'. Typically, this is the order that |
| 93 |
// they appear in the 'blocks' array, but not always... |
| 94 |
// |
| 95 |
// This is a very hard error to track down, so we just copy the |
| 96 |
// 'blocks' array to a new array and sort it on the 'offset_' |
| 97 |
// member... |
| 98 |
// |
| 99 |
// All block-related calls after these meta-data calls are based on |
| 100 |
// the block id and not the block order, so we don't need to fix |
| 101 |
// them. |
| 102 |
for (size_t i = 0; i < blocks.size(); i++) { |
| 103 |
blocks[i].position_ = i; |
| 104 |
} |
| 105 |
|
| 106 |
// Check whether current order is consistent. The problem with just |
| 107 |
// sorting the data is that if a subset of the entire model is being |
| 108 |
// joined, there may be zero-length element blocks. These blocks |
| 109 |
// have an offset of zero since there is no "lowest id" to get the |
| 110 |
// offset from. |
| 111 |
// |
| 112 |
// If all that is wanted is a single subset of the entire model, it |
| 113 |
// doesn't matter if the blocks are reordered due to the zero |
| 114 |
// offsets; however, if the user then later joins multiple subsets |
| 115 |
// together, there will be issues of block ordering mismatches. |
| 116 |
// |
| 117 |
// So, to avoid sorting the data into incorrect order, we check |
| 118 |
// whether the data are consistent before sorting and if so, don't |
| 119 |
// do the sort. Concsistent means that all blocks that have a |
| 120 |
// nonzero element count are in the correct order. |
| 121 |
size_t last_offset = 0; |
| 122 |
bool order_ok = true; |
| 123 |
for (const auto &block : blocks) { |
| 124 |
if (block.elementCount > 0) { |
| 125 |
if (block.offset_ < last_offset) { |
| 126 |
order_ok = false; |
| 127 |
break; |
| 128 |
} |
| 129 |
last_offset = block.offset_; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
std::vector<Block> sorted_blocks(blocks); |
| 134 |
if (!order_ok) { |
| 135 |
std::sort(sorted_blocks.begin(), sorted_blocks.end(), |
| 136 |
[](const Block &b1, const Block &b2) { return b1.offset_ < b2.offset_; }); |
| 137 |
|
| 138 |
// Now, update the position_ field based on the sorted order. |
| 139 |
for (size_t i = 0; i < blocks.size(); i++) { |
| 140 |
size_t orig_position = sorted_blocks[i].position_; |
| 141 |
blocks[orig_position].position_ = i; |
| 142 |
SMART_ASSERT(blocks[orig_position].id == sorted_blocks[i].id); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
int ierr; |
| 147 |
{ |
| 148 |
Excn::Redefine the_database(exodusFilePtr); |
| 149 |
|
| 150 |
ierr = put_metadata(mesh, comm); |
| 151 |
if (ierr != EX_NOERR) { |
| 152 |
return ierr; |
| 153 |
} |
| 154 |
|
| 155 |
ierr = put_metadata(sorted_blocks); |
| 156 |
if (ierr != EX_NOERR) { |
| 157 |
return ierr; |
| 158 |
} |
| 159 |
|
| 160 |
ierr = put_metadata(nodesets); |
| 161 |
if (ierr != EX_NOERR) { |
| 162 |
return ierr; |
| 163 |
} |
| 164 |
|
| 165 |
ierr = put_metadata(sidesets); |
| 166 |
if (ierr != EX_NOERR) { |
| 167 |
return ierr; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// NON-Define mode output... |
| 172 |
ierr = put_non_define_data(mesh, comm); |
| 173 |
if (ierr != EX_NOERR) { |
| 174 |
return ierr; |
| 175 |
} |
| 176 |
|
| 177 |
ierr = put_non_define_data(sorted_blocks); |
| 178 |
if (ierr != EX_NOERR) { |
| 179 |
return ierr; |
| 180 |
} |
| 181 |
|
| 182 |
ierr = put_non_define_data(nodesets); |
| 183 |
if (ierr != EX_NOERR) { |
| 184 |
return ierr; |
| 185 |
} |
| 186 |
|
| 187 |
ierr = put_non_define_data(sidesets); |
| 188 |
if (ierr != EX_NOERR) { |
| 189 |
return ierr; |
| 190 |
} |
| 191 |
|
| 192 |
// For now, put entity names using the ExodusII api... |
| 193 |
{ |
| 194 |
size_t max_entity = std::max({mesh.blockCount, mesh.nodesetCount, mesh.sidesetCount}); |
| 195 |
for (size_t i = 0; i < mesh.blockCount; i++) { |
| 196 |
max_entity = std::max(max_entity, blocks[i].attributeCount); |
| 197 |
} |
| 198 |
|
| 199 |
size_t name_size = ex_inquire_int(exodusFilePtr, EX_INQ_MAX_READ_NAME_LENGTH); |
| 200 |
auto *names = new char *[max_entity]; |
| 201 |
for (size_t i = 0; i < max_entity; i++) { |
| 202 |
names[i] = new char[name_size + 1]; |
| 203 |
} |
| 204 |
|
| 205 |
if (mesh.blockCount > 0) { |
| 206 |
for (size_t i = 0; i < mesh.blockCount; i++) { |
| 207 |
copy_string(names[i], sorted_blocks[i].name_, name_size + 1); |
| 208 |
} |
| 209 |
ex_put_names(exodusFilePtr, EX_ELEM_BLOCK, names); |
| 210 |
|
| 211 |
for (size_t i = 0; i < mesh.blockCount; i++) { |
| 212 |
if (blocks[i].attributeCount > 0) { |
| 213 |
SMART_ASSERT(blocks[i].attributeCount == blocks[i].attributeNames.size()); |
| 214 |
for (size_t j = 0; j < blocks[i].attributeCount; j++) { |
| 215 |
std::memset(names[j], '\0', name_size + 1); |
| 216 |
if (!blocks[i].attributeNames[j].empty()) { |
| 217 |
copy_string(names[j], blocks[i].attributeNames[j], name_size + 1); |
| 218 |
} |
| 219 |
} |
| 220 |
ierr = ex_put_attr_names(exodusFilePtr, EX_ELEM_BLOCK, blocks[i].id, names); |
| 221 |
SMART_ASSERT(ierr == 0); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (mesh.nodesetCount > 0) { |
| 227 |
for (size_t i = 0; i < mesh.nodesetCount; i++) { |
| 228 |
copy_string(names[i], nodesets[i].name_, name_size + 1); |
| 229 |
} |
| 230 |
ex_put_names(exodusFilePtr, EX_NODE_SET, names); |
| 231 |
} |
| 232 |
|
| 233 |
if (mesh.sidesetCount > 0) { |
| 234 |
for (size_t i = 0; i < mesh.sidesetCount; i++) { |
| 235 |
copy_string(names[i], sidesets[i].name_, name_size + 1); |
| 236 |
} |
| 237 |
ex_put_names(exodusFilePtr, EX_SIDE_SET, names); |
| 238 |
} |
| 239 |
|
| 240 |
for (size_t i = 0; i < max_entity; i++) { |
| 241 |
delete[] names[i]; |
| 242 |
} |
| 243 |
delete[] names; |
| 244 |
} |
| 245 |
|
| 246 |
ex_update(exodusFilePtr); |
| 247 |
return EX_NOERR; |
| 248 |
} |
| 249 |
|
| 250 |
template <typename INT> |
| 251 |
int Excn::Internals::put_metadata(const Mesh<INT> &mesh, const CommunicationMetaData & /*unused*/) |
| 252 |
{ |
| 253 |
// define some attributes... |
| 254 |
int status = nc_put_att_text(exodusFilePtr, NC_GLOBAL, ATT_TITLE, mesh.title.length() + 1, |
| 255 |
mesh.title.c_str()); |
| 256 |
if (status != NC_NOERR) { |
| 257 |
std::string errmsg = |
| 258 |
fmt::format("Error: failed to define title attribute to file id {}", exodusFilePtr); |
| 259 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 260 |
return EX_FATAL; |
| 261 |
} |
| 262 |
|
| 263 |
// For use later to help readers know how much memory to allocate |
| 264 |
// for name storage, we define an attribute containing the maximum |
| 265 |
// size of any name. |
| 266 |
{ |
| 267 |
int current_len = 0; |
| 268 |
status = nc_put_att_int(exodusFilePtr, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, ¤t_len); |
| 269 |
if (status != NC_NOERR) { |
| 270 |
ex_opts(EX_VERBOSE); |
| 271 |
std::string errmsg = fmt::format( |
| 272 |
"Error: failed to define ATT_MAX_NAME_LENGTH attribute to file id {}", exodusFilePtr); |
| 273 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 274 |
return EX_FATAL; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
// create name string length dimension |
| 279 |
if (maximumNameLength < 32) { |
| 280 |
maximumNameLength = 32; |
| 281 |
} |
| 282 |
int namestrdim = 0; |
| 283 |
status = nc_def_dim(exodusFilePtr, DIM_STR_NAME, maximumNameLength + 1, &namestrdim); |
| 284 |
if (status != NC_NOERR) { |
| 285 |
ex_opts(EX_VERBOSE); |
| 286 |
std::string errmsg = |
| 287 |
fmt::format("Error: failed to define name string length in file id {}", exodusFilePtr); |
| 288 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 289 |
return EX_FATAL; |
| 290 |
} |
| 291 |
|
| 292 |
// ...and some dimensions.. |
| 293 |
int numdimdim = 0; |
| 294 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_DIM, mesh.dimensionality, &numdimdim); |
| 295 |
if (status != NC_NOERR) { |
| 296 |
ex_opts(EX_VERBOSE); |
| 297 |
std::string errmsg = |
| 298 |
fmt::format("Error: failed to define number of dimensions in file id {}", exodusFilePtr); |
| 299 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 300 |
return EX_FATAL; |
| 301 |
} |
| 302 |
|
| 303 |
int timedim = 0; |
| 304 |
if ((status = nc_def_dim(exodusFilePtr, DIM_TIME, NC_UNLIMITED, &timedim)) != NC_NOERR) { |
| 305 |
std::string errmsg = |
| 306 |
fmt::format("Error: failed to define time dimension in file id {}", exodusFilePtr); |
| 307 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 308 |
return EX_FATAL; |
| 309 |
} |
| 310 |
|
| 311 |
int varid = 0; |
| 312 |
{ |
| 313 |
std::array dim{timedim}; |
| 314 |
if ((status = nc_def_var(exodusFilePtr, VAR_WHOLE_TIME, nc_flt_code(exodusFilePtr), 1, |
| 315 |
Data(dim), &varid)) != NC_NOERR) { |
| 316 |
std::string errmsg = fmt::format( |
| 317 |
"Error: failed to define whole time step variable in file id {}", exodusFilePtr); |
| 318 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 319 |
return EX_FATAL; |
| 320 |
} |
| 321 |
|
| 322 |
struct exi_file_item *file = exi_find_file_item(exodusFilePtr); |
| 323 |
if (file) { |
| 324 |
file->time_varid = varid; |
| 325 |
} |
| 326 |
|
| 327 |
exi_compress_variable(exodusFilePtr, varid, -2); /* don't compress, but do set collective io */ |
| 328 |
} |
| 329 |
|
| 330 |
int map_type = get_type(exodusFilePtr, EX_MAPS_INT64_DB); |
| 331 |
int numnoddim = 0; |
| 332 |
if (mesh.nodeCount > 0) { |
| 333 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_NODES, mesh.nodeCount, &numnoddim); |
| 334 |
if (status != NC_NOERR) { |
| 335 |
ex_opts(EX_VERBOSE); |
| 336 |
std::string errmsg = |
| 337 |
fmt::format("Error: failed to define number of nodes in file id {}", exodusFilePtr); |
| 338 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 339 |
return EX_FATAL; |
| 340 |
} |
| 341 |
|
| 342 |
// Define the node map here to avoid a later redefine call |
| 343 |
std::array dims{numnoddim}; |
| 344 |
status = nc_def_var(exodusFilePtr, VAR_NODE_NUM_MAP, map_type, 1, Data(dims), &varid); |
| 345 |
if (status != NC_NOERR) { |
| 346 |
ex_opts(EX_VERBOSE); |
| 347 |
if (status == NC_ENAMEINUSE) { |
| 348 |
std::string errmsg = |
| 349 |
fmt::format("Error: node numbering map already exists in file id {}", exodusFilePtr); |
| 350 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 351 |
} |
| 352 |
else { |
| 353 |
std::string errmsg = fmt::format( |
| 354 |
"Error: failed to create node numbering map array in file id {}", exodusFilePtr); |
| 355 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 356 |
} |
| 357 |
return EX_FATAL; |
| 358 |
} |
| 359 |
exi_compress_variable(exodusFilePtr, varid, 1); |
| 360 |
} |
| 361 |
|
| 362 |
if (mesh.elementCount > 0) { |
| 363 |
int numelemdim = 0; |
| 364 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_ELEM, mesh.elementCount, &numelemdim); |
| 365 |
if (status != NC_NOERR) { |
| 366 |
ex_opts(EX_VERBOSE); |
| 367 |
std::string errmsg = |
| 368 |
fmt::format("Error: failed to define number of elements in file id {}", exodusFilePtr); |
| 369 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 370 |
return EX_FATAL; |
| 371 |
} |
| 372 |
|
| 373 |
// Define the element map here to avoid a later redefine call |
| 374 |
std::array dims{numelemdim}; |
| 375 |
varid = 0; |
| 376 |
status = nc_def_var(exodusFilePtr, VAR_ELEM_NUM_MAP, map_type, 1, Data(dims), &varid); |
| 377 |
if (status != NC_NOERR) { |
| 378 |
ex_opts(EX_VERBOSE); |
| 379 |
if (status == NC_ENAMEINUSE) { |
| 380 |
std::string errmsg = |
| 381 |
fmt::format("Error: element numbering map already exists in file id {}", exodusFilePtr); |
| 382 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 383 |
} |
| 384 |
else { |
| 385 |
std::string errmsg = fmt::format( |
| 386 |
"Error: failed to create element numbering map in file id {}", exodusFilePtr); |
| 387 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 388 |
} |
| 389 |
return EX_FATAL; |
| 390 |
} |
| 391 |
exi_compress_variable(exodusFilePtr, varid, 1); |
| 392 |
} |
| 393 |
|
| 394 |
if (mesh.blockCount > 0) { |
| 395 |
status = define_netcdf_vars(exodusFilePtr, "element block", mesh.blockCount, DIM_NUM_EL_BLK, |
| 396 |
VAR_STAT_EL_BLK, VAR_ID_EL_BLK, VAR_NAME_EL_BLK); |
| 397 |
if (status != EX_NOERR) { |
| 398 |
return EX_FATAL; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
// node set id array: |
| 403 |
if (mesh.nodesetCount > 0) { |
| 404 |
status = define_netcdf_vars(exodusFilePtr, "node set", mesh.nodesetCount, DIM_NUM_NS, |
| 405 |
VAR_NS_STAT, VAR_NS_IDS, VAR_NAME_NS); |
| 406 |
if (status != EX_NOERR) { |
| 407 |
return EX_FATAL; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// side set id array: |
| 412 |
if (mesh.sidesetCount > 0) { |
| 413 |
status = define_netcdf_vars(exodusFilePtr, "side set", mesh.sidesetCount, DIM_NUM_SS, |
| 414 |
VAR_SS_STAT, VAR_SS_IDS, VAR_NAME_SS); |
| 415 |
if (status != EX_NOERR) { |
| 416 |
return EX_FATAL; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
status = define_coordinate_vars(exodusFilePtr, mesh.nodeCount, numnoddim, mesh.dimensionality, |
| 421 |
numdimdim, namestrdim); |
| 422 |
if (status != EX_NOERR) { |
| 423 |
return EX_FATAL; |
| 424 |
} |
| 425 |
|
| 426 |
return EX_NOERR; |
| 427 |
} |
| 428 |
|
| 429 |
int Excn::Internals::put_metadata(const std::vector<Block> &blocks) |
| 430 |
{ |
| 431 |
std::string errmsg; |
| 432 |
int status = 0; // clear error code |
| 433 |
|
| 434 |
if (blocks.empty()) { |
| 435 |
return EX_NOERR; |
| 436 |
} |
| 437 |
|
| 438 |
// Get number of element blocks defined for this file |
| 439 |
int dimid; |
| 440 |
size_t num_elem_blk = 0; |
| 441 |
status = nc_inq_dimid(exodusFilePtr, DIM_NUM_EL_BLK, &dimid); |
| 442 |
if (status != NC_NOERR) { |
| 443 |
ex_opts(EX_VERBOSE); |
| 444 |
errmsg = fmt::format("Error: no element blocks defined in file id {}", exodusFilePtr); |
| 445 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 446 |
return EX_FATAL; |
| 447 |
} |
| 448 |
|
| 449 |
int namestrdim; |
| 450 |
status = nc_inq_dimid(exodusFilePtr, DIM_STR_NAME, &namestrdim); |
| 451 |
if (status != NC_NOERR) { |
| 452 |
ex_opts(EX_VERBOSE); |
| 453 |
errmsg = fmt::format("Error: failed to get name string length in file id {}", exodusFilePtr); |
| 454 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 455 |
return EX_FATAL; |
| 456 |
} |
| 457 |
|
| 458 |
status = nc_inq_dimlen(exodusFilePtr, dimid, &num_elem_blk); |
| 459 |
if (status != NC_NOERR) { |
| 460 |
ex_opts(EX_VERBOSE); |
| 461 |
errmsg = |
| 462 |
fmt::format("Error: failed to get number of element blocks in file id {}", exodusFilePtr); |
| 463 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 464 |
return EX_FATAL; |
| 465 |
} |
| 466 |
|
| 467 |
SMART_ASSERT(blocks.size() == num_elem_blk); |
| 468 |
|
| 469 |
// Iterate over element blocks ... |
| 470 |
for (size_t iblk = 0; iblk < num_elem_blk; iblk++) { |
| 471 |
|
| 472 |
exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_ELEM_BLOCK)); |
| 473 |
|
| 474 |
if (blocks[iblk].elementCount == 0) { |
| 475 |
continue; |
| 476 |
} |
| 477 |
|
| 478 |
// define some dimensions and variables |
| 479 |
int numelbdim; |
| 480 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_EL_IN_BLK(iblk + 1), blocks[iblk].elementCount, |
| 481 |
&numelbdim); |
| 482 |
if (status != NC_NOERR) { |
| 483 |
if (status == NC_ENAMEINUSE) { // duplicate entry |
| 484 |
ex_opts(EX_VERBOSE); |
| 485 |
errmsg = fmt::format("Error: element block {} already defined in file id {}", |
| 486 |
blocks[iblk].id, exodusFilePtr); |
| 487 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 488 |
} |
| 489 |
else { |
| 490 |
ex_opts(EX_VERBOSE); |
| 491 |
errmsg = |
| 492 |
fmt::format("Error: failed to define number of elements/block for block {} file id {}", |
| 493 |
blocks[iblk].id, exodusFilePtr); |
| 494 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 495 |
} |
| 496 |
return EX_FATAL; |
| 497 |
} |
| 498 |
|
| 499 |
int nelnoddim; |
| 500 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_PER_EL(iblk + 1), blocks[iblk].nodesPerElement, |
| 501 |
&nelnoddim); |
| 502 |
if (status != NC_NOERR) { |
| 503 |
ex_opts(EX_VERBOSE); |
| 504 |
errmsg = |
| 505 |
fmt::format("Error: failed to define number of nodes/element for block {} in file id {}", |
| 506 |
blocks[iblk].id, exodusFilePtr); |
| 507 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 508 |
return EX_FATAL; |
| 509 |
} |
| 510 |
|
| 511 |
// element attribute array |
| 512 |
if (blocks[iblk].attributeCount > 0) { |
| 513 |
int numattrdim; |
| 514 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_ATT_IN_BLK(iblk + 1), blocks[iblk].attributeCount, |
| 515 |
&numattrdim); |
| 516 |
if (status != NC_NOERR) { |
| 517 |
ex_opts(EX_VERBOSE); |
| 518 |
errmsg = |
| 519 |
fmt::format("Error: failed to define number of attributes in block {} in file id {}", |
| 520 |
blocks[iblk].id, exodusFilePtr); |
| 521 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 522 |
return EX_FATAL; |
| 523 |
} |
| 524 |
|
| 525 |
std::array dims{numelbdim, numattrdim}; |
| 526 |
int varid = 0; |
| 527 |
status = nc_def_var(exodusFilePtr, VAR_ATTRIB(iblk + 1), nc_flt_code(exodusFilePtr), 2, |
| 528 |
Data(dims), &varid); |
| 529 |
if (status != NC_NOERR) { |
| 530 |
ex_opts(EX_VERBOSE); |
| 531 |
errmsg = |
| 532 |
fmt::format("Error: failed to define attributes for element block {} in file id {}", |
| 533 |
blocks[iblk].id, exodusFilePtr); |
| 534 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 535 |
return EX_FATAL; |
| 536 |
} |
| 537 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 538 |
|
| 539 |
// Attribute name array... |
| 540 |
std::array adims{numattrdim, namestrdim}; |
| 541 |
|
| 542 |
status = |
| 543 |
nc_def_var(exodusFilePtr, VAR_NAME_ATTRIB(iblk + 1), NC_CHAR, 2, Data(adims), &varid); |
| 544 |
if (status != NC_NOERR) { |
| 545 |
ex_opts(EX_VERBOSE); |
| 546 |
errmsg = fmt::format("Error: failed to define attribute name array for element block {}" |
| 547 |
" in file id {}", |
| 548 |
blocks[iblk].id, exodusFilePtr); |
| 549 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 550 |
return EX_FATAL; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
// element connectivity array |
| 555 |
std::array dims{numelbdim, nelnoddim}; |
| 556 |
|
| 557 |
int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB); |
| 558 |
int connid; |
| 559 |
status = nc_def_var(exodusFilePtr, VAR_CONN(iblk + 1), bulk_type, 2, Data(dims), &connid); |
| 560 |
if (status != NC_NOERR) { |
| 561 |
ex_opts(EX_VERBOSE); |
| 562 |
errmsg = fmt::format("Error: failed to create connectivity array for block {} in file id {}", |
| 563 |
blocks[iblk].id, exodusFilePtr); |
| 564 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 565 |
return EX_FATAL; |
| 566 |
} |
| 567 |
exi_compress_variable(exodusFilePtr, connid, 1); |
| 568 |
|
| 569 |
// store element type as attribute of connectivity variable |
| 570 |
status = nc_put_att_text(exodusFilePtr, connid, ATT_NAME_ELB, |
| 571 |
static_cast<int>(blocks[iblk].elType.size()) + 1, |
| 572 |
blocks[iblk].elType.c_str()); |
| 573 |
if (status != NC_NOERR) { |
| 574 |
ex_opts(EX_VERBOSE); |
| 575 |
errmsg = fmt::format("Error: failed to store element type name {} in file id {}", |
| 576 |
blocks[iblk].elType, exodusFilePtr); |
| 577 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 578 |
return EX_FATAL; |
| 579 |
} |
| 580 |
} |
| 581 |
return EX_NOERR; |
| 582 |
} |
| 583 |
|
| 584 |
template <typename INT> |
| 585 |
int Excn::Internals::put_non_define_data(const Mesh<INT> & /*unused*/, |
| 586 |
const CommunicationMetaData & /*unused*/) |
| 587 |
{ |
| 588 |
return EX_NOERR; |
| 589 |
} |
| 590 |
|
| 591 |
int Excn::Internals::put_non_define_data(const std::vector<Block> &blocks) |
| 592 |
{ |
| 593 |
int num_elem_blk = blocks.size(); // Verified via SMART_ASSERT earlier... |
| 594 |
|
| 595 |
if (num_elem_blk > 0) { |
| 596 |
// first get id of element block ids array variable |
| 597 |
std::vector<entity_id> elem_blk_id(num_elem_blk); |
| 598 |
for (int iblk = 0; iblk < num_elem_blk; iblk++) { |
| 599 |
elem_blk_id[iblk] = blocks[iblk].id; |
| 600 |
} |
| 601 |
|
| 602 |
if (put_id_array(exodusFilePtr, VAR_ID_EL_BLK, elem_blk_id) != NC_NOERR) { |
| 603 |
return EX_FATAL; |
| 604 |
} |
| 605 |
|
| 606 |
// Now, write the element block status array |
| 607 |
std::vector<int> elem_blk_status(num_elem_blk); |
| 608 |
for (int iblk = 0; iblk < num_elem_blk; iblk++) { |
| 609 |
elem_blk_status[iblk] = blocks[iblk].elementCount > 0 ? 1 : 0; |
| 610 |
} |
| 611 |
|
| 612 |
if (put_int_array(exodusFilePtr, VAR_STAT_EL_BLK, elem_blk_status) != NC_NOERR) { |
| 613 |
return EX_FATAL; |
| 614 |
} |
| 615 |
} |
| 616 |
return EX_NOERR; |
| 617 |
} |
| 618 |
|
| 619 |
// ======================================================================== |
| 620 |
template <typename INT> int Excn::Internals::put_metadata(const std::vector<NodeSet<INT>> &nodesets) |
| 621 |
{ |
| 622 |
if (nodesets.empty()) { |
| 623 |
return EX_NOERR; |
| 624 |
} |
| 625 |
|
| 626 |
std::string errmsg; |
| 627 |
int status = 0; // clear error code |
| 628 |
|
| 629 |
// Get number of node sets defined for this file |
| 630 |
int dimid; |
| 631 |
status = nc_inq_dimid(exodusFilePtr, DIM_NUM_NS, &dimid); |
| 632 |
if (status != NC_NOERR) { |
| 633 |
ex_opts(EX_VERBOSE); |
| 634 |
if (status == NC_EBADDIM) { |
| 635 |
errmsg = fmt::format("Error: no node sets defined for file id {}", exodusFilePtr); |
| 636 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 637 |
} |
| 638 |
else { |
| 639 |
errmsg = |
| 640 |
fmt::format("Error: failed to locate node sets defined in file id {}", exodusFilePtr); |
| 641 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 642 |
} |
| 643 |
return EX_FATAL; |
| 644 |
} |
| 645 |
|
| 646 |
// inquire how many node sets are to be stored |
| 647 |
int num_node_sets = ex_inquire_int(exodusFilePtr, EX_INQ_NODE_SETS); |
| 648 |
SMART_ASSERT(static_cast<int>(nodesets.size()) == num_node_sets); |
| 649 |
|
| 650 |
for (int i = 0; i < num_node_sets; i++) { |
| 651 |
|
| 652 |
// NOTE: exi_inc_file_item is used to find the number of node sets |
| 653 |
// for a specific file and returns that value incremented. |
| 654 |
int cur_num_node_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_NODE_SET)); |
| 655 |
|
| 656 |
if (nodesets[i].nodeCount == 0) { |
| 657 |
continue; |
| 658 |
} |
| 659 |
|
| 660 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_NOD_NS(cur_num_node_sets + 1), nodesets[i].nodeCount, |
| 661 |
&dimid); |
| 662 |
if (status != NC_NOERR) { |
| 663 |
ex_opts(EX_VERBOSE); |
| 664 |
if (status == NC_ENAMEINUSE) { |
| 665 |
errmsg = fmt::format("Error: node set {} already defined in file id {}", nodesets[i].id, |
| 666 |
exodusFilePtr); |
| 667 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 668 |
} |
| 669 |
else { |
| 670 |
errmsg = fmt::format("Error: failed to define number of nodes for set {} in file id {}", |
| 671 |
nodesets[i].id, exodusFilePtr); |
| 672 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 673 |
} |
| 674 |
return EX_FATAL; |
| 675 |
} |
| 676 |
|
| 677 |
// define variable to store node set node list here instead of in expns |
| 678 |
int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB); |
| 679 |
std::array dims{dimid}; |
| 680 |
int varid; |
| 681 |
status = nc_def_var(exodusFilePtr, VAR_NODE_NS(cur_num_node_sets + 1), bulk_type, 1, Data(dims), |
| 682 |
&varid); |
| 683 |
if (status != NC_NOERR) { |
| 684 |
ex_opts(EX_VERBOSE); |
| 685 |
if (status == NC_ENAMEINUSE) { |
| 686 |
errmsg = fmt::format("Error: node set {} node list already defined in file id {}", |
| 687 |
nodesets[i].id, exodusFilePtr); |
| 688 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 689 |
} |
| 690 |
else { |
| 691 |
errmsg = fmt::format("Error: failed to create node set {} node list in file id {}", |
| 692 |
nodesets[i].id, exodusFilePtr); |
| 693 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 694 |
} |
| 695 |
return EX_FATAL; |
| 696 |
} |
| 697 |
exi_compress_variable(exodusFilePtr, varid, 1); |
| 698 |
|
| 699 |
// Create variable for distribution factors if required |
| 700 |
if (nodesets[i].dfCount > 0) { |
| 701 |
// num_dist_per_set should equal num_nodes_per_set |
| 702 |
if (nodesets[i].dfCount != nodesets[i].nodeCount) { |
| 703 |
status = EX_FATAL; |
| 704 |
ex_opts(EX_VERBOSE); |
| 705 |
errmsg = fmt::format("Error: # dist fact ({}) not equal to # nodes ({}) " |
| 706 |
"in node set {} file id {}", |
| 707 |
(int64_t)nodesets[i].dfCount, (int64_t)nodesets[i].nodeCount, |
| 708 |
nodesets[i].id, exodusFilePtr); |
| 709 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 710 |
return EX_FATAL; |
| 711 |
} |
| 712 |
|
| 713 |
// create variable for distribution factors |
| 714 |
status = nc_def_var(exodusFilePtr, VAR_FACT_NS(cur_num_node_sets + 1), |
| 715 |
nc_flt_code(exodusFilePtr), 1, Data(dims), &varid); |
| 716 |
if (status != NC_NOERR) { |
| 717 |
ex_opts(EX_VERBOSE); |
| 718 |
if (status == NC_ENAMEINUSE) { |
| 719 |
errmsg = fmt::format("Error: node set {} dist factors already exist in file id {}", |
| 720 |
nodesets[i].id, exodusFilePtr); |
| 721 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 722 |
} |
| 723 |
else { |
| 724 |
errmsg = fmt::format("Error: failed to create node set {} dist factors in file id {}", |
| 725 |
nodesets[i].id, exodusFilePtr); |
| 726 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 727 |
} |
| 728 |
return EX_FATAL; |
| 729 |
} |
| 730 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 731 |
} |
| 732 |
} |
| 733 |
return EX_NOERR; |
| 734 |
} |
| 735 |
|
| 736 |
template <typename INT> |
| 737 |
int Excn::Internals::put_non_define_data(const std::vector<NodeSet<INT>> &nodesets) |
| 738 |
{ |
| 739 |
if (nodesets.empty()) { |
| 740 |
return EX_NOERR; |
| 741 |
} |
| 742 |
|
| 743 |
// Output nodeset ids... |
| 744 |
int num_nodesets = nodesets.size(); |
| 745 |
std::vector<entity_id> nodeset_id(num_nodesets); |
| 746 |
for (int i = 0; i < num_nodesets; i++) { |
| 747 |
nodeset_id[i] = nodesets[i].id; |
| 748 |
} |
| 749 |
|
| 750 |
if (put_id_array(exodusFilePtr, VAR_NS_IDS, nodeset_id) != NC_NOERR) { |
| 751 |
return EX_FATAL; |
| 752 |
} |
| 753 |
|
| 754 |
// Now, write the status array |
| 755 |
std::vector<int> status(num_nodesets); |
| 756 |
for (int i = 0; i < num_nodesets; i++) { |
| 757 |
status[i] = nodesets[i].nodeCount > 0 ? 1 : 0; |
| 758 |
} |
| 759 |
|
| 760 |
if (put_int_array(exodusFilePtr, VAR_NS_STAT, status) != NC_NOERR) { |
| 761 |
return EX_FATAL; |
| 762 |
} |
| 763 |
|
| 764 |
return EX_NOERR; |
| 765 |
} |
| 766 |
|
| 767 |
// ======================================================================== |
| 768 |
template <typename INT> int Excn::Internals::put_metadata(const std::vector<SideSet<INT>> &sidesets) |
| 769 |
{ |
| 770 |
if (sidesets.empty()) { |
| 771 |
return EX_NOERR; |
| 772 |
} |
| 773 |
|
| 774 |
std::string errmsg; |
| 775 |
int status = 0; // clear error code |
| 776 |
|
| 777 |
// Get number of side sets defined for this file |
| 778 |
int dimid; |
| 779 |
status = nc_inq_dimid(exodusFilePtr, DIM_NUM_SS, &dimid); |
| 780 |
if (status != NC_NOERR) { |
| 781 |
ex_opts(EX_VERBOSE); |
| 782 |
if (status == NC_EBADDIM) { |
| 783 |
errmsg = fmt::format("Error: no side sets defined for file id {}", exodusFilePtr); |
| 784 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 785 |
} |
| 786 |
else { |
| 787 |
errmsg = |
| 788 |
fmt::format("Error: failed to locate side sets defined in file id {}", exodusFilePtr); |
| 789 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 790 |
} |
| 791 |
return EX_FATAL; |
| 792 |
} |
| 793 |
|
| 794 |
// inquire how many side sets are to be stored |
| 795 |
int num_side_sets = ex_inquire_int(exodusFilePtr, EX_INQ_SIDE_SETS); |
| 796 |
SMART_ASSERT(static_cast<int>(sidesets.size()) == num_side_sets); |
| 797 |
|
| 798 |
for (int i = 0; i < num_side_sets; i++) { |
| 799 |
|
| 800 |
// NOTE: exi_inc_file_item is used to find the number of side sets |
| 801 |
// for a specific file and returns that value incremented. |
| 802 |
int cur_num_side_sets = exi_inc_file_item(exodusFilePtr, exi_get_counter_list(EX_SIDE_SET)); |
| 803 |
|
| 804 |
if (sidesets[i].sideCount == 0) { |
| 805 |
continue; |
| 806 |
} |
| 807 |
|
| 808 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_SIDE_SS(cur_num_side_sets + 1), |
| 809 |
sidesets[i].sideCount, &dimid); |
| 810 |
if (status != NC_NOERR) { |
| 811 |
ex_opts(EX_VERBOSE); |
| 812 |
if (status == NC_ENAMEINUSE) { |
| 813 |
errmsg = fmt::format("Error: side set {} already defined in file id {}", sidesets[i].id, |
| 814 |
exodusFilePtr); |
| 815 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 816 |
} |
| 817 |
else { |
| 818 |
errmsg = fmt::format("Error: failed to define number of sides for set {} in file id {}", |
| 819 |
sidesets[i].id, exodusFilePtr); |
| 820 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 821 |
} |
| 822 |
return EX_FATAL; |
| 823 |
} |
| 824 |
|
| 825 |
int bulk_type = get_type(exodusFilePtr, EX_BULK_INT64_DB); |
| 826 |
std::array dims{dimid}; |
| 827 |
int varid = 0; |
| 828 |
status = nc_def_var(exodusFilePtr, VAR_ELEM_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims), |
| 829 |
&varid); |
| 830 |
if (status != NC_NOERR) { |
| 831 |
ex_opts(EX_VERBOSE); |
| 832 |
if (status == NC_ENAMEINUSE) { |
| 833 |
errmsg = fmt::format("Error: side set {} element list already defined in file id {}", |
| 834 |
sidesets[i].id, exodusFilePtr); |
| 835 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 836 |
} |
| 837 |
else { |
| 838 |
errmsg = fmt::format("Error: failed to create side set {} element list in file id {}", |
| 839 |
sidesets[i].id, exodusFilePtr); |
| 840 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 841 |
} |
| 842 |
return EX_FATAL; |
| 843 |
} |
| 844 |
exi_compress_variable(exodusFilePtr, varid, 1); |
| 845 |
|
| 846 |
// create side list variable for side set |
| 847 |
status = nc_def_var(exodusFilePtr, VAR_SIDE_SS(cur_num_side_sets + 1), bulk_type, 1, Data(dims), |
| 848 |
&varid); |
| 849 |
if (status != NC_NOERR) { |
| 850 |
ex_opts(EX_VERBOSE); |
| 851 |
if (status == NC_ENAMEINUSE) { |
| 852 |
errmsg = fmt::format("Error: side list already exists for side set {} in file id {}", |
| 853 |
sidesets[i].id, exodusFilePtr); |
| 854 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 855 |
} |
| 856 |
else { |
| 857 |
errmsg = fmt::format("Error: failed to create side list for side set {} in file id {}", |
| 858 |
sidesets[i].id, exodusFilePtr); |
| 859 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 860 |
} |
| 861 |
return EX_FATAL; |
| 862 |
} |
| 863 |
exi_compress_variable(exodusFilePtr, varid, 1); |
| 864 |
|
| 865 |
// Create variable for distribution factors if required |
| 866 |
if (sidesets[i].dfCount > 0) { |
| 867 |
status = nc_def_dim(exodusFilePtr, DIM_NUM_DF_SS(cur_num_side_sets + 1), sidesets[i].dfCount, |
| 868 |
&dimid); |
| 869 |
if (status != NC_NOERR) { |
| 870 |
ex_opts(EX_VERBOSE); |
| 871 |
if (status == NC_ENAMEINUSE) { |
| 872 |
errmsg = fmt::format("Error: side set df count {} already defined in file id {}", |
| 873 |
sidesets[i].id, exodusFilePtr); |
| 874 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 875 |
} |
| 876 |
else { |
| 877 |
errmsg = fmt::format("Error: failed to define side set df count for set {} in file id {}", |
| 878 |
sidesets[i].id, exodusFilePtr); |
| 879 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 880 |
} |
| 881 |
return EX_FATAL; |
| 882 |
} |
| 883 |
|
| 884 |
// create distribution factor list variable for side set |
| 885 |
dims[0] = dimid; |
| 886 |
status = nc_def_var(exodusFilePtr, VAR_FACT_SS(cur_num_side_sets + 1), |
| 887 |
nc_flt_code(exodusFilePtr), 1, Data(dims), &varid); |
| 888 |
if (status != NC_NOERR) { |
| 889 |
ex_opts(EX_VERBOSE); |
| 890 |
if (status == NC_ENAMEINUSE) { |
| 891 |
errmsg = |
| 892 |
fmt::format("Error: dist factor list already exists for side set {} in file id {}", |
| 893 |
sidesets[i].id, exodusFilePtr); |
| 894 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 895 |
} |
| 896 |
else { |
| 897 |
errmsg = |
| 898 |
fmt::format("Error: failed to create dist factor list for side set {} in file id {}", |
| 899 |
sidesets[i].id, exodusFilePtr); |
| 900 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 901 |
} |
| 902 |
return EX_FATAL; |
| 903 |
} |
| 904 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 905 |
} |
| 906 |
} |
| 907 |
return EX_NOERR; |
| 908 |
} |
| 909 |
|
| 910 |
template <typename INT> |
| 911 |
int Excn::Internals::put_non_define_data(const std::vector<SideSet<INT>> &sidesets) |
| 912 |
{ |
| 913 |
if (sidesets.empty()) { |
| 914 |
return EX_NOERR; |
| 915 |
} |
| 916 |
|
| 917 |
// Output sideset ids... |
| 918 |
int num_sidesets = sidesets.size(); |
| 919 |
std::vector<entity_id> sideset_id(num_sidesets); |
| 920 |
for (int i = 0; i < num_sidesets; i++) { |
| 921 |
sideset_id[i] = sidesets[i].id; |
| 922 |
} |
| 923 |
|
| 924 |
if (put_id_array(exodusFilePtr, VAR_SS_IDS, sideset_id) != NC_NOERR) { |
| 925 |
return EX_FATAL; |
| 926 |
} |
| 927 |
|
| 928 |
// Now, write the status array |
| 929 |
std::vector<int> status(num_sidesets); |
| 930 |
for (int i = 0; i < num_sidesets; i++) { |
| 931 |
status[i] = sidesets[i].sideCount > 0 ? 1 : 0; |
| 932 |
} |
| 933 |
|
| 934 |
if (put_int_array(exodusFilePtr, VAR_SS_STAT, status) != NC_NOERR) { |
| 935 |
return EX_FATAL; |
| 936 |
} |
| 937 |
|
| 938 |
return EX_NOERR; |
| 939 |
} |
| 940 |
|
| 941 |
namespace { |
| 942 |
int put_int_array(int exoid, const char *var_type, const std::vector<int> &array) |
| 943 |
{ |
| 944 |
int var_id; |
| 945 |
int status = nc_inq_varid(exoid, var_type, &var_id); |
| 946 |
if (status != NC_NOERR) { |
| 947 |
ex_opts(EX_VERBOSE); |
| 948 |
std::string errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid); |
| 949 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 950 |
return EX_FATAL; |
| 951 |
} |
| 952 |
|
| 953 |
status = nc_put_var_int(exoid, var_id, Data(array)); |
| 954 |
if (status != NC_NOERR) { |
| 955 |
ex_opts(EX_VERBOSE); |
| 956 |
std::string errmsg = |
| 957 |
fmt::format("Error: failed to write {} array in file id {}", var_type, exoid); |
| 958 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 959 |
return EX_FATAL; |
| 960 |
} |
| 961 |
return EX_NOERR; |
| 962 |
} |
| 963 |
|
| 964 |
int put_id_array(int exoid, const char *var_type, const std::vector<entity_id> &ids) |
| 965 |
{ |
| 966 |
std::string errmsg; |
| 967 |
int var_id; |
| 968 |
|
| 969 |
int status = nc_inq_varid(exoid, var_type, &var_id); |
| 970 |
if (status != NC_NOERR) { |
| 971 |
ex_opts(EX_VERBOSE); |
| 972 |
errmsg = fmt::format("Error: failed to locate {} in file id {}", var_type, exoid); |
| 973 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 974 |
return EX_FATAL; |
| 975 |
} |
| 976 |
|
| 977 |
int id_type = get_type(exoid, EX_IDS_INT64_API); |
| 978 |
|
| 979 |
if (id_type == NC_INT64) { |
| 980 |
status = nc_put_var_longlong(exoid, var_id, (long long int *)Data(ids)); |
| 981 |
} |
| 982 |
else { |
| 983 |
// Have entity_id (long long), need ints... |
| 984 |
std::vector<int> int_ids(ids.size()); |
| 985 |
#ifdef _MSC_VER |
| 986 |
#pragma warning(push) |
| 987 |
#pragma warning(disable : 4244) |
| 988 |
#endif |
| 989 |
int_ids.assign(ids.begin(), ids.end()); |
| 990 |
#ifdef _MSC_VER |
| 991 |
#pragma warning(pop) |
| 992 |
#endif |
| 993 |
status = nc_put_var_int(exoid, var_id, Data(int_ids)); |
| 994 |
} |
| 995 |
|
| 996 |
if (status != NC_NOERR) { |
| 997 |
ex_opts(EX_VERBOSE); |
| 998 |
errmsg = fmt::format("Error: failed to write {} array in file id {}", var_type, exoid); |
| 999 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1000 |
return EX_FATAL; |
| 1001 |
} |
| 1002 |
return EX_NOERR; |
| 1003 |
} |
| 1004 |
|
| 1005 |
int define_coordinate_vars(int exodusFilePtr, size_t nodes, int node_dim, int dimension, |
| 1006 |
int dim_dim, int str_dim) |
| 1007 |
{ |
| 1008 |
std::string errmsg; |
| 1009 |
int status; |
| 1010 |
int varid; |
| 1011 |
|
| 1012 |
if (nodes > 0) { |
| 1013 |
// node coordinate arrays -- separate storage... |
| 1014 |
std::array dim{node_dim}; |
| 1015 |
if (dimension > 0) { |
| 1016 |
status = nc_def_var(exodusFilePtr, VAR_COORD_X, nc_flt_code(exodusFilePtr), 1, Data(dim), |
| 1017 |
&varid); |
| 1018 |
if (status != NC_NOERR) { |
| 1019 |
ex_opts(EX_VERBOSE); |
| 1020 |
errmsg = fmt::format("Error: failed to define node x coordinate array in file id {}", |
| 1021 |
exodusFilePtr); |
| 1022 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 1023 |
return EX_FATAL; |
| 1024 |
} |
| 1025 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 1026 |
} |
| 1027 |
|
| 1028 |
if (dimension > 1) { |
| 1029 |
status = nc_def_var(exodusFilePtr, VAR_COORD_Y, nc_flt_code(exodusFilePtr), 1, Data(dim), |
| 1030 |
&varid); |
| 1031 |
if (status != NC_NOERR) { |
| 1032 |
ex_opts(EX_VERBOSE); |
| 1033 |
errmsg = fmt::format("Error: failed to define node y coordinate array in file id {}", |
| 1034 |
exodusFilePtr); |
| 1035 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 1036 |
return EX_FATAL; |
| 1037 |
} |
| 1038 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 1039 |
} |
| 1040 |
|
| 1041 |
if (dimension > 2) { |
| 1042 |
status = nc_def_var(exodusFilePtr, VAR_COORD_Z, nc_flt_code(exodusFilePtr), 1, Data(dim), |
| 1043 |
&varid); |
| 1044 |
if (status != NC_NOERR) { |
| 1045 |
ex_opts(EX_VERBOSE); |
| 1046 |
errmsg = fmt::format("Error: failed to define node z coordinate array in file id {}", |
| 1047 |
exodusFilePtr); |
| 1048 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 1049 |
return EX_FATAL; |
| 1050 |
} |
| 1051 |
exi_compress_variable(exodusFilePtr, varid, 2); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
// coordinate names array |
| 1056 |
std::array dim{dim_dim, str_dim}; |
| 1057 |
status = nc_def_var(exodusFilePtr, VAR_NAME_COOR, NC_CHAR, 2, Data(dim), &varid); |
| 1058 |
if (status != NC_NOERR) { |
| 1059 |
ex_opts(EX_VERBOSE); |
| 1060 |
errmsg = |
| 1061 |
fmt::format("Error: failed to define coordinate name array in file id {}", exodusFilePtr); |
| 1062 |
ex_err_fn(exodusFilePtr, __func__, errmsg.c_str(), status); |
| 1063 |
return EX_FATAL; |
| 1064 |
} |
| 1065 |
return EX_NOERR; |
| 1066 |
} |
| 1067 |
|
| 1068 |
int define_netcdf_vars(int exoid, const char *type, size_t count, const char *dim_num, |
| 1069 |
const char *stat_var, const char *id_var, const char *name_var) |
| 1070 |
{ |
| 1071 |
std::string errmsg; |
| 1072 |
|
| 1073 |
int namestrdim = 0; |
| 1074 |
int status = nc_inq_dimid(exoid, DIM_STR_NAME, &namestrdim); |
| 1075 |
if (status != NC_NOERR) { |
| 1076 |
ex_opts(EX_VERBOSE); |
| 1077 |
errmsg = fmt::format("Error: failed to get string name dimension in file id {}", exoid); |
| 1078 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1079 |
return EX_FATAL; |
| 1080 |
} |
| 1081 |
|
| 1082 |
int dimid = 0; |
| 1083 |
status = nc_def_dim(exoid, dim_num, count, &dimid); |
| 1084 |
if (status != NC_NOERR) { |
| 1085 |
ex_opts(EX_VERBOSE); |
| 1086 |
errmsg = fmt::format("Error: failed to define number of {}s in file id {}", type, exoid); |
| 1087 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1088 |
return EX_FATAL; |
| 1089 |
} |
| 1090 |
|
| 1091 |
// id status array: |
| 1092 |
std::array dim{dimid}; |
| 1093 |
int varid = 0; |
| 1094 |
status = nc_def_var(exoid, stat_var, NC_INT, 1, Data(dim), &varid); |
| 1095 |
if (status != NC_NOERR) { |
| 1096 |
ex_opts(EX_VERBOSE); |
| 1097 |
errmsg = fmt::format("Error: failed to define side {} status in file id {}", type, exoid); |
| 1098 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1099 |
return EX_FATAL; |
| 1100 |
} |
| 1101 |
|
| 1102 |
// id array: |
| 1103 |
int ids_type = get_type(exoid, EX_IDS_INT64_DB); |
| 1104 |
status = nc_def_var(exoid, id_var, ids_type, 1, Data(dim), &varid); |
| 1105 |
if (status != NC_NOERR) { |
| 1106 |
ex_opts(EX_VERBOSE); |
| 1107 |
errmsg = fmt::format("Error: failed to define {} property in file id {}", type, exoid); |
| 1108 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1109 |
return EX_FATAL; |
| 1110 |
} |
| 1111 |
|
| 1112 |
// store property name as attribute of property array variable |
| 1113 |
status = nc_put_att_text(exoid, varid, ATT_PROP_NAME, 3, "ID"); |
| 1114 |
if (status != NC_NOERR) { |
| 1115 |
ex_opts(EX_VERBOSE); |
| 1116 |
errmsg = fmt::format("Error: failed to store {} property name {} in file id {}", type, "ID", |
| 1117 |
exoid); |
| 1118 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1119 |
return EX_FATAL; |
| 1120 |
} |
| 1121 |
|
| 1122 |
std::array dims{dimid, namestrdim}; |
| 1123 |
status = nc_def_var(exoid, name_var, NC_CHAR, 2, Data(dims), &varid); |
| 1124 |
if (status != NC_NOERR) { |
| 1125 |
ex_opts(EX_VERBOSE); |
| 1126 |
errmsg = fmt::format("Error: failed to define {} name array in file id {}", type, exoid); |
| 1127 |
ex_err_fn(exoid, __func__, errmsg.c_str(), status); |
| 1128 |
return EX_FATAL; |
| 1129 |
} |
| 1130 |
return EX_NOERR; |
| 1131 |
} |
| 1132 |
} // namespace |
| 1133 |
|
| 1134 |
template int Excn::Internals::write_meta_data(const Excn::Mesh<int> &mesh, |
| 1135 |
const std::vector<Excn::Block> &blocks, |
| 1136 |
const std::vector<Excn::NodeSet<int>> &nodesets, |
| 1137 |
const std::vector<Excn::SideSet<int>> &sidesets, |
| 1138 |
const Excn::CommunicationMetaData &comm); |
| 1139 |
template int Excn::Internals::write_meta_data(const Excn::Mesh<int64_t> &mesh, |
| 1140 |
const std::vector<Excn::Block> &blocks, |
| 1141 |
const std::vector<Excn::NodeSet<int64_t>> &nodesets, |
| 1142 |
const std::vector<Excn::SideSet<int64_t>> &sidesets, |
| 1143 |
const Excn::CommunicationMetaData &comm); |