| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022 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 "exodusII.h" // for ex_set, ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
* writes the set parameters and optionally set data for 1 or more sets |
| 14 |
* \param exoid exodus file id |
| 15 |
* \param set_count number of sets to write |
| 16 |
* \param *sets array of ex_set structures |
| 17 |
*/ |
| 18 |
|
| 19 |
int ex_put_sets(int exoid, size_t set_count, const struct ex_set *sets) |
| 20 |
{ |
| 21 |
size_t i; |
| 22 |
int needs_define = 0; |
| 23 |
int set_stat; |
| 24 |
int dimid, varid, status, dims[1]; |
| 25 |
int set_id_ndx; |
| 26 |
size_t start[1]; |
| 27 |
int cur_num_sets; |
| 28 |
char errmsg[MAX_ERR_LENGTH]; |
| 29 |
int *sets_to_define = NULL; |
| 30 |
char *numentryptr = NULL; |
| 31 |
char *entryptr = NULL; |
| 32 |
char *extraptr = NULL; |
| 33 |
char *idsptr = NULL; |
| 34 |
char *statptr = NULL; |
| 35 |
char *numdfptr = NULL; |
| 36 |
char *factptr = NULL; |
| 37 |
|
| 38 |
int int_type; |
| 39 |
|
| 40 |
EX_FUNC_ENTER(); |
| 41 |
|
| 42 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 43 |
EX_FUNC_LEAVE(EX_FATAL); |
| 44 |
} |
| 45 |
|
| 46 |
if (!(sets_to_define = malloc(set_count * sizeof(int)))) { |
| 47 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 48 |
"ERROR: failed to allocate memory for internal sets_to_define " |
| 49 |
"array in file id %d", |
| 50 |
exoid); |
| 51 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 52 |
EX_FUNC_LEAVE(EX_FATAL); |
| 53 |
} |
| 54 |
|
| 55 |
/* Note that this routine can be called: |
| 56 |
1) just define the sets |
| 57 |
2) just output the set data (after a previous call to define) |
| 58 |
3) define and output the set data in one call. |
| 59 |
*/ |
| 60 |
for (i = 0; i < set_count; i++) { |
| 61 |
/* first check if any sets are specified */ |
| 62 |
if ((status = nc_inq_dimid(exoid, exi_dim_num_objects(sets[i].type), &dimid)) != NC_NOERR) { |
| 63 |
if (status == NC_EBADDIM) { |
| 64 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss defined for file id %d", |
| 65 |
ex_name_of_object(sets[i].type), exoid); |
| 66 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 67 |
} |
| 68 |
else { |
| 69 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %ss defined in file id %d", |
| 70 |
ex_name_of_object(sets[i].type), exoid); |
| 71 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 72 |
} |
| 73 |
free(sets_to_define); |
| 74 |
EX_FUNC_LEAVE(EX_FATAL); |
| 75 |
} |
| 76 |
|
| 77 |
if (sets[i].id < 0) { |
| 78 |
/* We are adding a set with id = -sets[i].id. We want to define |
| 79 |
* everything, but we don't |
| 80 |
* want to increment the number of sets... Major kluge / proof of concept |
| 81 |
*/ |
| 82 |
needs_define++; |
| 83 |
sets_to_define[i] = -1; |
| 84 |
} |
| 85 |
else { |
| 86 |
status = exi_id_lkup(exoid, sets[i].type, sets[i].id); |
| 87 |
if (status != -EX_LOOKUPFAIL) { /* found the side set id, so set is already defined... */ |
| 88 |
sets_to_define[i] = 0; |
| 89 |
} |
| 90 |
else { |
| 91 |
needs_define++; |
| 92 |
sets_to_define[i] = 1; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (needs_define > 0) { |
| 98 |
/* put netcdf file into define mode */ |
| 99 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 100 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 101 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 102 |
free(sets_to_define); |
| 103 |
EX_FUNC_LEAVE(EX_FATAL); |
| 104 |
} |
| 105 |
|
| 106 |
for (i = 0; i < set_count; i++) { |
| 107 |
if (sets_to_define[i] == 0) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if (sets_to_define[i] > 0) { |
| 112 |
/* NOTE: exi_inc_file_item finds the current number of sets defined |
| 113 |
for a specific file and returns that value incremented. */ |
| 114 |
cur_num_sets = exi_inc_file_item(exoid, exi_get_counter_list(sets[i].type)); |
| 115 |
set_id_ndx = cur_num_sets + 1; |
| 116 |
sets_to_define[i] = set_id_ndx; |
| 117 |
} |
| 118 |
else { |
| 119 |
cur_num_sets = exi_get_file_item(exoid, exi_get_counter_list(sets[i].type)); |
| 120 |
set_id_ndx = cur_num_sets - set_count + i + 1; |
| 121 |
sets_to_define[i] = set_id_ndx; |
| 122 |
} |
| 123 |
|
| 124 |
if (sets[i].num_entry == 0) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
/* setup pointers based on set_type */ |
| 129 |
if (sets[i].type == EX_NODE_SET) { |
| 130 |
numentryptr = DIM_NUM_NOD_NS(set_id_ndx); |
| 131 |
entryptr = VAR_NODE_NS(set_id_ndx); |
| 132 |
extraptr = NULL; |
| 133 |
/* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */ |
| 134 |
numdfptr = DIM_NUM_NOD_NS(set_id_ndx); |
| 135 |
factptr = VAR_FACT_NS(set_id_ndx); |
| 136 |
} |
| 137 |
else if (sets[i].type == EX_EDGE_SET) { |
| 138 |
numentryptr = DIM_NUM_EDGE_ES(set_id_ndx); |
| 139 |
entryptr = VAR_EDGE_ES(set_id_ndx); |
| 140 |
extraptr = VAR_ORNT_ES(set_id_ndx); |
| 141 |
numdfptr = DIM_NUM_DF_ES(set_id_ndx); |
| 142 |
factptr = VAR_FACT_ES(set_id_ndx); |
| 143 |
} |
| 144 |
else if (sets[i].type == EX_FACE_SET) { |
| 145 |
numentryptr = DIM_NUM_FACE_FS(set_id_ndx); |
| 146 |
entryptr = VAR_FACE_FS(set_id_ndx); |
| 147 |
extraptr = VAR_ORNT_FS(set_id_ndx); |
| 148 |
numdfptr = DIM_NUM_DF_FS(set_id_ndx); |
| 149 |
factptr = VAR_FACT_FS(set_id_ndx); |
| 150 |
} |
| 151 |
else if (sets[i].type == EX_SIDE_SET) { |
| 152 |
numentryptr = DIM_NUM_SIDE_SS(set_id_ndx); |
| 153 |
entryptr = VAR_ELEM_SS(set_id_ndx); |
| 154 |
extraptr = VAR_SIDE_SS(set_id_ndx); |
| 155 |
numdfptr = DIM_NUM_DF_SS(set_id_ndx); |
| 156 |
factptr = VAR_FACT_SS(set_id_ndx); |
| 157 |
} |
| 158 |
else if (sets[i].type == EX_ELEM_SET) { |
| 159 |
numentryptr = DIM_NUM_ELE_ELS(set_id_ndx); |
| 160 |
entryptr = VAR_ELEM_ELS(set_id_ndx); |
| 161 |
extraptr = NULL; |
| 162 |
numdfptr = DIM_NUM_DF_ELS(set_id_ndx); |
| 163 |
factptr = VAR_FACT_ELS(set_id_ndx); |
| 164 |
} |
| 165 |
|
| 166 |
/* define dimensions and variables */ |
| 167 |
if ((status = nc_def_dim(exoid, numentryptr, sets[i].num_entry, &dimid)) != NC_NOERR) { |
| 168 |
if (status == NC_ENAMEINUSE) { |
| 169 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 170 |
"ERROR: %s %" PRId64 " -- size already defined in file id %d", |
| 171 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 172 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 173 |
} |
| 174 |
else { |
| 175 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 176 |
"ERROR: failed to define number of entries in %s %" PRId64 " in file id %d", |
| 177 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 178 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 179 |
} |
| 180 |
goto error_ret; |
| 181 |
} |
| 182 |
|
| 183 |
int_type = NC_INT; |
| 184 |
if (ex_int64_status(exoid) & EX_BULK_INT64_DB) { |
| 185 |
int_type = NC_INT64; |
| 186 |
} |
| 187 |
|
| 188 |
/* create variable array in which to store the entry lists */ |
| 189 |
dims[0] = dimid; |
| 190 |
if ((status = nc_def_var(exoid, entryptr, int_type, 1, dims, &varid)) != NC_NOERR) { |
| 191 |
if (status == NC_ENAMEINUSE) { |
| 192 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 193 |
"ERROR: entry list already exists for %s %" PRId64 " in file id %d", |
| 194 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 195 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 196 |
} |
| 197 |
else { |
| 198 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 199 |
"ERROR: failed to create entry list for %s %" PRId64 " in file id %d", |
| 200 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 201 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 202 |
} |
| 203 |
goto error_ret; /* exit define mode and return */ |
| 204 |
} |
| 205 |
exi_compress_variable(exoid, varid, 1); |
| 206 |
|
| 207 |
if (extraptr) { |
| 208 |
if ((status = nc_def_var(exoid, extraptr, int_type, 1, dims, &varid)) != NC_NOERR) { |
| 209 |
if (status == NC_ENAMEINUSE) { |
| 210 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 211 |
"ERROR: extra list already exists for %s %" PRId64 " in file id %d", |
| 212 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 213 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 214 |
} |
| 215 |
else { |
| 216 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 217 |
"ERROR: failed to create extra list for %s %" PRId64 " in file id %d", |
| 218 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 219 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 220 |
} |
| 221 |
goto error_ret; /* exit define mode and return */ |
| 222 |
} |
| 223 |
exi_compress_variable(exoid, varid, 1); |
| 224 |
} |
| 225 |
|
| 226 |
/* Create distribution factors variable if required */ |
| 227 |
if (sets[i].num_distribution_factor > 0) { |
| 228 |
if (sets[i].type != EX_SIDE_SET) { |
| 229 |
/* but sets[i].num_distribution_factor must equal number of nodes */ |
| 230 |
if (sets[i].num_distribution_factor != sets[i].num_entry) { |
| 231 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 232 |
"ERROR: # dist fact (%" PRId64 ") not equal to # nodes (%" PRId64 |
| 233 |
") in node set %" PRId64 " file id %d", |
| 234 |
sets[i].num_distribution_factor, sets[i].num_entry, sets[i].id, exoid); |
| 235 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 236 |
goto error_ret; /* exit define mode and return */ |
| 237 |
} |
| 238 |
} |
| 239 |
else { |
| 240 |
/* reuse dimid from entry lists */ |
| 241 |
if ((status = nc_def_dim(exoid, numdfptr, sets[i].num_distribution_factor, &dimid)) != |
| 242 |
NC_NOERR) { |
| 243 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 244 |
"ERROR: failed to define number of dist factors in %s %" PRId64 |
| 245 |
" in file id %d", |
| 246 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 247 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 248 |
goto error_ret; /* exit define mode and return */ |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/* create variable array in which to store the set distribution factors |
| 253 |
*/ |
| 254 |
dims[0] = dimid; |
| 255 |
if ((status = nc_def_var(exoid, factptr, nc_flt_code(exoid), 1, dims, &varid)) != |
| 256 |
NC_NOERR) { |
| 257 |
if (status == NC_ENAMEINUSE) { |
| 258 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 259 |
"ERROR: dist factors list already exists for %s %" PRId64 " in file id %d", |
| 260 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 261 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 262 |
} |
| 263 |
else { |
| 264 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 265 |
"ERROR: failed to create dist factors list for %s %" PRId64 " in file id %d", |
| 266 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 267 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 268 |
} |
| 269 |
goto error_ret; /* exit define mode and return */ |
| 270 |
} |
| 271 |
exi_compress_variable(exoid, varid, 2); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/* leave define mode */ |
| 276 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 277 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 278 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 279 |
free(sets_to_define); |
| 280 |
EX_FUNC_LEAVE(EX_FATAL); |
| 281 |
} |
| 282 |
|
| 283 |
/* Output the set ids and status... */ |
| 284 |
for (i = 0; i < set_count; i++) { |
| 285 |
/* setup pointers based on sets[i].type */ |
| 286 |
if (sets[i].type == EX_NODE_SET) { |
| 287 |
idsptr = VAR_NS_IDS; |
| 288 |
statptr = VAR_NS_STAT; |
| 289 |
} |
| 290 |
else if (sets[i].type == EX_EDGE_SET) { |
| 291 |
idsptr = VAR_ES_IDS; |
| 292 |
statptr = VAR_ES_STAT; |
| 293 |
} |
| 294 |
else if (sets[i].type == EX_FACE_SET) { |
| 295 |
idsptr = VAR_FS_IDS; |
| 296 |
statptr = VAR_FS_STAT; |
| 297 |
} |
| 298 |
else if (sets[i].type == EX_SIDE_SET) { |
| 299 |
idsptr = VAR_SS_IDS; |
| 300 |
statptr = VAR_SS_STAT; |
| 301 |
} |
| 302 |
else if (sets[i].type == EX_ELEM_SET) { |
| 303 |
idsptr = VAR_ELS_IDS; |
| 304 |
statptr = VAR_ELS_STAT; |
| 305 |
} |
| 306 |
|
| 307 |
/* first: get id of set id variable */ |
| 308 |
if ((status = nc_inq_varid(exoid, idsptr, &varid)) != NC_NOERR) { |
| 309 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s %" PRId64 " in file id %d", |
| 310 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 311 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 312 |
free(sets_to_define); |
| 313 |
EX_FUNC_LEAVE(EX_FATAL); |
| 314 |
} |
| 315 |
|
| 316 |
/* write out set id */ |
| 317 |
start[0] = sets_to_define[i] - 1; |
| 318 |
long long id = sets[i].id; |
| 319 |
if (id < 0) { |
| 320 |
id = -id; |
| 321 |
} |
| 322 |
status = nc_put_var1_longlong(exoid, varid, start, &id); |
| 323 |
|
| 324 |
if (status != NC_NOERR) { |
| 325 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s id %" PRId64 " in file id %d", |
| 326 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 327 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 328 |
free(sets_to_define); |
| 329 |
EX_FUNC_LEAVE(EX_FATAL); |
| 330 |
} |
| 331 |
|
| 332 |
set_stat = (sets[i].num_entry == 0) ? 0 : 1; |
| 333 |
|
| 334 |
if ((status = nc_inq_varid(exoid, statptr, &varid)) != NC_NOERR) { |
| 335 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s status in file id %d", |
| 336 |
ex_name_of_object(sets[i].type), exoid); |
| 337 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 338 |
free(sets_to_define); |
| 339 |
EX_FUNC_LEAVE(EX_FATAL); |
| 340 |
} |
| 341 |
|
| 342 |
if ((status = nc_put_var1_int(exoid, varid, start, &set_stat)) != NC_NOERR) { |
| 343 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 344 |
"ERROR: failed to store %s %" PRId64 " status to file id %d", |
| 345 |
ex_name_of_object(sets[i].type), sets[i].id, exoid); |
| 346 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 347 |
free(sets_to_define); |
| 348 |
EX_FUNC_LEAVE(EX_FATAL); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
free(sets_to_define); |
| 354 |
|
| 355 |
/* Sets are now all defined; see if any set data needs to be output... */ |
| 356 |
status = EX_NOERR; |
| 357 |
for (i = 0; i < set_count; i++) { |
| 358 |
int stat; |
| 359 |
long long id = sets[i].id; |
| 360 |
if (id < 0) { |
| 361 |
id = -id; |
| 362 |
} |
| 363 |
if (sets[i].entry_list != NULL || sets[i].extra_list != NULL) { |
| 364 |
/* NOTE: ex_put_set will write the warning/error message... */ |
| 365 |
stat = ex_put_set(exoid, sets[i].type, id, sets[i].entry_list, sets[i].extra_list); |
| 366 |
if (stat != EX_NOERR) { |
| 367 |
status = EX_FATAL; |
| 368 |
} |
| 369 |
} |
| 370 |
if (sets[i].num_distribution_factor > 0 && sets[i].distribution_factor_list != NULL) { |
| 371 |
/* NOTE: ex_put_set_dist_fact will write the warning/error message... */ |
| 372 |
stat = ex_put_set_dist_fact(exoid, sets[i].type, id, sets[i].distribution_factor_list); |
| 373 |
if (stat != EX_NOERR) { |
| 374 |
status = EX_FATAL; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
EX_FUNC_LEAVE(status); |
| 379 |
|
| 380 |
/* Fatal error: exit definition mode and return */ |
| 381 |
error_ret: |
| 382 |
free(sets_to_define); |
| 383 |
|
| 384 |
exi_leavedef(exoid, __func__); |
| 385 |
EX_FUNC_LEAVE(EX_FATAL); |
| 386 |
} |