| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022, 2023, 2024 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 |
* |
| 10 |
* expclb - ex_put_concat_elem_block: write element block parameters |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int idexo exodus file id |
| 15 |
* char** elem_type element type string |
| 16 |
* int* num_elem_this_blk number of elements in the element blk |
| 17 |
* int* num_nodes_per_elem number of nodes per element block |
| 18 |
* int* num_attr_this_blk number of attributes |
| 19 |
* int define_maps if != 0, write maps, else don't |
| 20 |
* |
| 21 |
*****************************************************************************/ |
| 22 |
|
| 23 |
#include "exodusII.h" // for ex_err, etc |
| 24 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 25 |
|
| 26 |
/*! |
| 27 |
* writes the parameters used to describe an element block |
| 28 |
* \param exoid exodus file id |
| 29 |
* \param elem_blk_id element block id |
| 30 |
* \param elem_type element type string |
| 31 |
* \param num_elem_this_blk number of elements in the element blk |
| 32 |
* \param num_nodes_per_elem number of nodes per element block |
| 33 |
* \param num_attr_this_blk number of attributes |
| 34 |
* \param define_maps if != 0, write maps, else don't |
| 35 |
*/ |
| 36 |
int ex_put_concat_elem_block(int exoid, const void_int *elem_blk_id, char *const elem_type[], |
| 37 |
const void_int *num_elem_this_blk, const void_int *num_nodes_per_elem, |
| 38 |
const void_int *num_attr_this_blk, int define_maps) |
| 39 |
{ |
| 40 |
size_t i; |
| 41 |
int varid, dimid, dims[2], strdim, *eb_array; |
| 42 |
int temp; |
| 43 |
int iblk; |
| 44 |
int status; |
| 45 |
int num_elem_blk; |
| 46 |
int map_int_type, conn_int_type; |
| 47 |
size_t length; |
| 48 |
int cur_num_elem_blk, nelnoddim, numelbdim, numattrdim, connid, numelemdim, numnodedim; |
| 49 |
char errmsg[MAX_ERR_LENGTH]; |
| 50 |
|
| 51 |
EX_FUNC_ENTER(); |
| 52 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 53 |
EX_FUNC_LEAVE(EX_FATAL); |
| 54 |
} |
| 55 |
|
| 56 |
/* first check if any element blocks are specified |
| 57 |
* OK if zero... |
| 58 |
*/ |
| 59 |
if (nc_inq_dimid(exoid, DIM_NUM_EL_BLK, &dimid) != NC_NOERR) { |
| 60 |
EX_FUNC_LEAVE(EX_NOERR); |
| 61 |
} |
| 62 |
|
| 63 |
/* Get number of element blocks defined for this file */ |
| 64 |
if ((status = nc_inq_dimlen(exoid, dimid, &length)) != NC_NOERR) { |
| 65 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of element blocks in file id %d", |
| 66 |
exoid); |
| 67 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 68 |
EX_FUNC_LEAVE(EX_FATAL); |
| 69 |
} |
| 70 |
num_elem_blk = length; |
| 71 |
|
| 72 |
/* Fill out the element block status array */ |
| 73 |
if (!(eb_array = malloc(num_elem_blk * sizeof(int)))) { |
| 74 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 75 |
"ERROR: failed to allocate space for element block status " |
| 76 |
"array in file id %d", |
| 77 |
exoid); |
| 78 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 79 |
EX_FUNC_LEAVE(EX_FATAL); |
| 80 |
} |
| 81 |
|
| 82 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 83 |
for (int ib = 0; ib < num_elem_blk; ib++) { |
| 84 |
eb_array[ib] = (((int64_t *)num_elem_this_blk)[ib] == 0) ? 0 : 1; |
| 85 |
} |
| 86 |
} |
| 87 |
else { |
| 88 |
for (int ib = 0; ib < num_elem_blk; ib++) { |
| 89 |
eb_array[ib] = (((int *)num_elem_this_blk)[ib] == 0) ? 0 : 1; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/* Next, get variable id of status array */ |
| 94 |
if ((status = nc_inq_varid(exoid, VAR_STAT_EL_BLK, &varid)) != NC_NOERR) { |
| 95 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate element block status in file id %d", |
| 96 |
exoid); |
| 97 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 98 |
free(eb_array); |
| 99 |
EX_FUNC_LEAVE(EX_FATAL); |
| 100 |
} |
| 101 |
|
| 102 |
status = nc_put_var_int(exoid, varid, eb_array); |
| 103 |
|
| 104 |
if (status != NC_NOERR) { |
| 105 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 106 |
"ERROR: failed to store element block status array to file id %d", exoid); |
| 107 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 108 |
free(eb_array); |
| 109 |
EX_FUNC_LEAVE(EX_FATAL); |
| 110 |
} |
| 111 |
|
| 112 |
/* Next, fill out ids array */ |
| 113 |
/* first get id of ids array variable */ |
| 114 |
if ((status = nc_inq_varid(exoid, VAR_ID_EL_BLK, &varid)) != NC_NOERR) { |
| 115 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 116 |
"ERROR: failed to locate element block ids array in file id %d", exoid); |
| 117 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 118 |
free(eb_array); |
| 119 |
EX_FUNC_LEAVE(EX_FATAL); |
| 120 |
} |
| 121 |
|
| 122 |
/* then, write out id list */ |
| 123 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 124 |
status = nc_put_var_longlong(exoid, varid, elem_blk_id); |
| 125 |
} |
| 126 |
else { |
| 127 |
status = nc_put_var_int(exoid, varid, elem_blk_id); |
| 128 |
} |
| 129 |
|
| 130 |
if (status != NC_NOERR) { |
| 131 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store element block id array in file id %d", |
| 132 |
exoid); |
| 133 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 134 |
free(eb_array); |
| 135 |
EX_FUNC_LEAVE(EX_FATAL); |
| 136 |
} |
| 137 |
|
| 138 |
/* inquire previously defined dimensions */ |
| 139 |
if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) { |
| 140 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid); |
| 141 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 142 |
free(eb_array); |
| 143 |
EX_FUNC_LEAVE(EX_FATAL); |
| 144 |
} |
| 145 |
|
| 146 |
/* put netcdf file into define mode */ |
| 147 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 148 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); |
| 149 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 150 |
free(eb_array); |
| 151 |
EX_FUNC_LEAVE(EX_FATAL); |
| 152 |
} |
| 153 |
|
| 154 |
conn_int_type = NC_INT; |
| 155 |
if (ex_int64_status(exoid) & EX_BULK_INT64_DB) { |
| 156 |
conn_int_type = NC_INT64; |
| 157 |
} |
| 158 |
|
| 159 |
map_int_type = NC_INT; |
| 160 |
if (ex_int64_status(exoid) & EX_MAPS_INT64_DB) { |
| 161 |
map_int_type = NC_INT64; |
| 162 |
} |
| 163 |
|
| 164 |
/* Iterate over element blocks ... */ |
| 165 |
for (iblk = 0; iblk < num_elem_blk; iblk++) { |
| 166 |
ex_entity_id eb_id; |
| 167 |
size_t num_elem; |
| 168 |
size_t num_npe; |
| 169 |
size_t num_attr; |
| 170 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 171 |
eb_id = ((int64_t *)elem_blk_id)[iblk]; |
| 172 |
num_elem = ((int64_t *)num_elem_this_blk)[iblk]; |
| 173 |
num_npe = ((int64_t *)num_nodes_per_elem)[iblk]; |
| 174 |
num_attr = ((int64_t *)num_attr_this_blk)[iblk]; |
| 175 |
} |
| 176 |
else { |
| 177 |
eb_id = ((int *)elem_blk_id)[iblk]; |
| 178 |
num_elem = ((int *)num_elem_this_blk)[iblk]; |
| 179 |
num_npe = ((int *)num_nodes_per_elem)[iblk]; |
| 180 |
num_attr = ((int *)num_attr_this_blk)[iblk]; |
| 181 |
} |
| 182 |
|
| 183 |
cur_num_elem_blk = exi_get_file_item(exoid, exi_get_counter_list(EX_ELEM_BLOCK)); |
| 184 |
if (cur_num_elem_blk >= num_elem_blk) { |
| 185 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 186 |
"ERROR: exceeded number of element blocks (%d) defined in file id %d", num_elem_blk, |
| 187 |
exoid); |
| 188 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 189 |
goto error_ret; |
| 190 |
} |
| 191 |
|
| 192 |
/* NOTE: exi_inc_file_item is used to find the number of element blocks |
| 193 |
for a specific file and returns that value incremented. */ |
| 194 |
cur_num_elem_blk = exi_inc_file_item(exoid, exi_get_counter_list(EX_ELEM_BLOCK)); |
| 195 |
|
| 196 |
if (eb_array[iblk] == 0) { /* Is this a NULL element block? */ |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
/* define some dimensions and variables*/ |
| 201 |
if ((status = nc_def_dim(exoid, DIM_NUM_EL_IN_BLK(cur_num_elem_blk + 1), num_elem, |
| 202 |
&numelbdim)) != NC_NOERR) { |
| 203 |
if (status == NC_ENAMEINUSE) { /* duplicate entry */ |
| 204 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 205 |
"ERROR: element block %" PRId64 " already defined in file id %d", eb_id, exoid); |
| 206 |
} |
| 207 |
else { |
| 208 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 209 |
"ERROR: failed to define number of elements/block for " |
| 210 |
"block %" PRId64 " file id %d", |
| 211 |
eb_id, exoid); |
| 212 |
} |
| 213 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 214 |
goto error_ret; /* exit define mode and return */ |
| 215 |
} |
| 216 |
|
| 217 |
if ((status = nc_def_dim(exoid, DIM_NUM_NOD_PER_EL(cur_num_elem_blk + 1), num_npe, |
| 218 |
&nelnoddim)) != NC_NOERR) { |
| 219 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 220 |
"ERROR: failed to define number of nodes/element for block %" PRId64 |
| 221 |
" in file id %d", |
| 222 |
eb_id, exoid); |
| 223 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 224 |
goto error_ret; /* exit define mode and return */ |
| 225 |
} |
| 226 |
|
| 227 |
/* element connectivity array */ |
| 228 |
dims[0] = numelbdim; |
| 229 |
dims[1] = nelnoddim; |
| 230 |
|
| 231 |
if ((status = nc_def_var(exoid, VAR_CONN(cur_num_elem_blk + 1), conn_int_type, 2, dims, |
| 232 |
&connid)) != NC_NOERR) { |
| 233 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 234 |
"ERROR: failed to create connectivity array for block %" PRId64 " in file id %d", |
| 235 |
eb_id, exoid); |
| 236 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 237 |
goto error_ret; /* exit define mode and return */ |
| 238 |
} |
| 239 |
exi_compress_variable(exoid, connid, 1); |
| 240 |
|
| 241 |
/* store element type as attribute of connectivity variable */ |
| 242 |
if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(elem_type[iblk]) + 1, |
| 243 |
(void *)elem_type[iblk])) != NC_NOERR) { |
| 244 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store element type name %s in file id %d", |
| 245 |
elem_type[iblk], exoid); |
| 246 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 247 |
goto error_ret; /* exit define mode and return */ |
| 248 |
} |
| 249 |
|
| 250 |
/* element attribute array */ |
| 251 |
if (num_attr > 0) { |
| 252 |
if ((status = nc_def_dim(exoid, DIM_NUM_ATT_IN_BLK(cur_num_elem_blk + 1), num_attr, |
| 253 |
&numattrdim)) != NC_NOERR) { |
| 254 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 255 |
"ERROR: failed to define number of attributes in block %" PRId64 " in file id %d", |
| 256 |
eb_id, exoid); |
| 257 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 258 |
goto error_ret; /* exit define mode and return */ |
| 259 |
} |
| 260 |
|
| 261 |
/* Attribute names... */ |
| 262 |
dims[0] = numattrdim; |
| 263 |
dims[1] = strdim; |
| 264 |
|
| 265 |
if ((status = nc_def_var(exoid, VAR_NAME_ATTRIB(cur_num_elem_blk + 1), NC_CHAR, 2, dims, |
| 266 |
&temp)) != NC_NOERR) { |
| 267 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 268 |
"ERROR: failed to define element attribute name array " |
| 269 |
"in file id %d", |
| 270 |
exoid); |
| 271 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 272 |
goto error_ret; /* exit define mode and return */ |
| 273 |
} |
| 274 |
#if defined(EX_CAN_USE_NC_DEF_VAR_FILL) |
| 275 |
int fill = NC_FILL_CHAR; |
| 276 |
nc_def_var_fill(exoid, temp, 0, &fill); |
| 277 |
#endif |
| 278 |
eb_array[iblk] = temp; |
| 279 |
|
| 280 |
dims[0] = numelbdim; |
| 281 |
dims[1] = numattrdim; |
| 282 |
|
| 283 |
if ((status = nc_def_var(exoid, VAR_ATTRIB(cur_num_elem_blk + 1), nc_flt_code(exoid), 2, dims, |
| 284 |
&temp)) != NC_NOERR) { |
| 285 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 286 |
"ERROR: failed to define attributes for element block %" PRId64 " in file id %d", |
| 287 |
eb_id, exoid); |
| 288 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 289 |
goto error_ret; /* exit define mode and return */ |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* Define the element map here to avoid a later redefine call */ |
| 295 |
if (define_maps != 0) { |
| 296 |
if (nc_inq_varid(exoid, VAR_ELEM_NUM_MAP, &temp) != NC_NOERR) { |
| 297 |
/* Map does not exist */ |
| 298 |
/* Possible to have zero elements but >0 element blocks. |
| 299 |
* Only define map if there are nonzero elements |
| 300 |
*/ |
| 301 |
if (nc_inq_dimid(exoid, DIM_NUM_ELEM, &numelemdim) == NC_NOERR) { |
| 302 |
dims[0] = numelemdim; |
| 303 |
|
| 304 |
if ((status = nc_def_var(exoid, VAR_ELEM_NUM_MAP, map_int_type, 1, dims, &temp)) != |
| 305 |
NC_NOERR) { |
| 306 |
if (status == NC_ENAMEINUSE) { |
| 307 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 308 |
"ERROR: element numbering map already exists in file id %d", exoid); |
| 309 |
} |
| 310 |
else { |
| 311 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 312 |
"ERROR: failed to create element numbering map in file id %d", exoid); |
| 313 |
} |
| 314 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 315 |
goto error_ret; /* exit define mode and return */ |
| 316 |
} |
| 317 |
exi_compress_variable(exoid, temp, 1); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/* Do the same for the node numbering map */ |
| 322 |
if (nc_inq_varid(exoid, VAR_NODE_NUM_MAP, &temp) != NC_NOERR) { |
| 323 |
/* Map does not exist */ |
| 324 |
if ((nc_inq_dimid(exoid, DIM_NUM_NODES, &numnodedim)) == NC_NOERR) { |
| 325 |
dims[0] = numnodedim; |
| 326 |
if ((status = nc_def_var(exoid, VAR_NODE_NUM_MAP, map_int_type, 1, dims, &temp)) != |
| 327 |
NC_NOERR) { |
| 328 |
if (status == NC_ENAMEINUSE) { |
| 329 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 330 |
"ERROR: node numbering map already exists in file id %d", exoid); |
| 331 |
} |
| 332 |
else { |
| 333 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 334 |
"ERROR: failed to create node numbering map array " |
| 335 |
"in file id %d", |
| 336 |
exoid); |
| 337 |
} |
| 338 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 339 |
goto error_ret; /* exit define mode and return */ |
| 340 |
} |
| 341 |
exi_compress_variable(exoid, temp, 1); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/* leave define mode */ |
| 347 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 348 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 349 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 350 |
free(eb_array); |
| 351 |
EX_FUNC_LEAVE(EX_FATAL); |
| 352 |
} |
| 353 |
|
| 354 |
{ |
| 355 |
/* Write dummy attribute name. Without this we get corruption in the |
| 356 |
* attribute name. |
| 357 |
*/ |
| 358 |
size_t start[2], count[2]; |
| 359 |
char *text = ""; |
| 360 |
count[0] = 1; |
| 361 |
start[1] = 0; |
| 362 |
count[1] = strlen(text) + 1; |
| 363 |
|
| 364 |
for (iblk = 0; iblk < num_elem_blk; iblk++) { |
| 365 |
size_t num_attr; |
| 366 |
if (eb_array[iblk] == 0) { /* Is this a NULL element block? */ |
| 367 |
continue; |
| 368 |
} |
| 369 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 370 |
num_attr = ((int64_t *)num_attr_this_blk)[iblk]; |
| 371 |
} |
| 372 |
else { |
| 373 |
num_attr = ((int *)num_attr_this_blk)[iblk]; |
| 374 |
} |
| 375 |
for (i = 0; i < num_attr; i++) { |
| 376 |
start[0] = i; |
| 377 |
nc_put_vara_text(exoid, eb_array[iblk], start, count, text); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
free(eb_array); |
| 382 |
|
| 383 |
EX_FUNC_LEAVE(EX_NOERR); |
| 384 |
|
| 385 |
/* Fatal error: exit definition mode and return */ |
| 386 |
error_ret: |
| 387 |
free(eb_array); |
| 388 |
exi_leavedef(exoid, __func__); |
| 389 |
EX_FUNC_LEAVE(EX_FATAL); |
| 390 |
} |