| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 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 |
* exgssc - ex_get_side_set_node_count |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* int side_set_id side set id |
| 16 |
* |
| 17 |
* exit conditions - |
| 18 |
* int *side_set_node_cnt_list returned array of number of nodes for |
| 19 |
* side or face |
| 20 |
* revision history - |
| 21 |
* |
| 22 |
*****************************************************************************/ |
| 23 |
|
| 24 |
#include "exodusII.h" // for ex_err, ex_block, etc |
| 25 |
#include "exodusII_int.h" // for elem_blk_parm, EX_FATAL, etc |
| 26 |
|
| 27 |
int ex_get_side_set_node_count(int exoid, ex_entity_id side_set_id, int *side_set_node_cnt_list) |
| 28 |
{ |
| 29 |
int status; |
| 30 |
int num_side_sets, num_elem_blks, ndim; |
| 31 |
size_t tot_num_ss_elem = 0; |
| 32 |
int64_t side, elem; |
| 33 |
void_int *elem_blk_ids = NULL; |
| 34 |
void_int *ss_elem_ndx = NULL; |
| 35 |
void_int *side_set_elem_list = NULL; |
| 36 |
void_int *side_set_side_list = NULL; |
| 37 |
size_t elem_ctr; |
| 38 |
|
| 39 |
struct exi_elem_blk_parm *elem_blk_parms = NULL; |
| 40 |
|
| 41 |
char errmsg[MAX_ERR_LENGTH]; |
| 42 |
int err_stat = EX_NOERR; |
| 43 |
|
| 44 |
EX_FUNC_ENTER(); |
| 45 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 46 |
EX_FUNC_LEAVE(EX_FATAL); |
| 47 |
} |
| 48 |
|
| 49 |
/* first check if any side sets are specified */ |
| 50 |
/* inquire how many side sets have been stored */ |
| 51 |
num_side_sets = ex_inquire_int(exoid, EX_INQ_SIDE_SETS); |
| 52 |
if (num_side_sets < 0) { |
| 53 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of side sets in file id %d", |
| 54 |
exoid); |
| 55 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 56 |
EX_FUNC_LEAVE(EX_FATAL); |
| 57 |
} |
| 58 |
|
| 59 |
if (num_side_sets == 0) { |
| 60 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no side sets defined in file id %d", exoid); |
| 61 |
ex_err_fn(exoid, __func__, errmsg, EX_NOENTITY); |
| 62 |
EX_FUNC_LEAVE(EX_WARN); |
| 63 |
} |
| 64 |
|
| 65 |
/* Lookup index of side set id in VAR_SS_IDS array */ |
| 66 |
if (exi_id_lkup(exoid, EX_SIDE_SET, side_set_id) <= 0) { |
| 67 |
; |
| 68 |
} |
| 69 |
ex_get_err(NULL, NULL, &status); |
| 70 |
|
| 71 |
if (status != 0) { |
| 72 |
if (status == EX_NULLENTITY) { |
| 73 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: side set %" PRId64 " is NULL in file id %d", |
| 74 |
side_set_id, exoid); |
| 75 |
ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY); |
| 76 |
EX_FUNC_LEAVE(EX_WARN); |
| 77 |
} |
| 78 |
|
| 79 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 80 |
"ERROR: failed to locate side set %" PRId64 " in VAR_SS_IDS array in file id %d", |
| 81 |
side_set_id, exoid); |
| 82 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 83 |
EX_FUNC_LEAVE(EX_FATAL); |
| 84 |
} |
| 85 |
|
| 86 |
num_elem_blks = ex_inquire_int(exoid, EX_INQ_ELEM_BLK); |
| 87 |
if (num_elem_blks < 0) { |
| 88 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of element blocks in file id %d", |
| 89 |
exoid); |
| 90 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 91 |
EX_FUNC_LEAVE(EX_FATAL); |
| 92 |
} |
| 93 |
|
| 94 |
/* get the dimensionality of the coordinates; this is necessary to |
| 95 |
distinguish between 2d TRIs and 3d TRIs */ |
| 96 |
ndim = ex_inquire_int(exoid, EX_INQ_DIM); |
| 97 |
if (ndim < 0) { |
| 98 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get dimensionality in file id %d", exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 100 |
EX_FUNC_LEAVE(EX_FATAL); |
| 101 |
} |
| 102 |
|
| 103 |
bool ints_64 = ex_int64_status(exoid) & EX_BULK_INT64_API; |
| 104 |
int int_size = ints_64 ? sizeof(int64_t) : sizeof(int); |
| 105 |
|
| 106 |
/* First determine the # of elements in the side set*/ |
| 107 |
int err; |
| 108 |
if (int_size == sizeof(int64_t)) { |
| 109 |
int64_t ss_elem = 0; |
| 110 |
int64_t ss_df = 0; |
| 111 |
err = ex_get_set_param(exoid, EX_SIDE_SET, side_set_id, &ss_elem, &ss_df); |
| 112 |
tot_num_ss_elem = ss_elem; |
| 113 |
} |
| 114 |
else { |
| 115 |
int ss_elem = 0; |
| 116 |
int ss_df = 0; |
| 117 |
err = ex_get_set_param(exoid, EX_SIDE_SET, side_set_id, &ss_elem, &ss_df); |
| 118 |
tot_num_ss_elem = ss_elem; |
| 119 |
} |
| 120 |
|
| 121 |
if (err == -1) { |
| 122 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 123 |
"ERROR: failed to get number of elements in side set %" PRId64 " in file id %d", |
| 124 |
side_set_id, exoid); |
| 125 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 126 |
EX_FUNC_LEAVE(EX_FATAL); |
| 127 |
} |
| 128 |
|
| 129 |
/* Allocate space for the side set element list */ |
| 130 |
{ |
| 131 |
if (!(side_set_elem_list = malloc(tot_num_ss_elem * int_size))) { |
| 132 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 133 |
"ERROR: failed to allocate space for side set element " |
| 134 |
"list for file id %d", |
| 135 |
exoid); |
| 136 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 137 |
EX_FUNC_LEAVE(EX_FATAL); |
| 138 |
} |
| 139 |
|
| 140 |
/* Allocate space for the side set side list */ |
| 141 |
if (!(side_set_side_list = malloc(tot_num_ss_elem * int_size))) { |
| 142 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 143 |
"ERROR: failed to allocate space for side set side list " |
| 144 |
"for file id %d", |
| 145 |
exoid); |
| 146 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 147 |
err_stat = EX_FATAL; |
| 148 |
goto cleanup; |
| 149 |
} |
| 150 |
|
| 151 |
if (ex_get_set(exoid, EX_SIDE_SET, side_set_id, side_set_elem_list, side_set_side_list) == -1) { |
| 152 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get side set %" PRId64 " in file id %d", |
| 153 |
side_set_id, exoid); |
| 154 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 155 |
err_stat = EX_FATAL; |
| 156 |
goto cleanup; |
| 157 |
} |
| 158 |
|
| 159 |
/* Allocate space for the ss element index array */ |
| 160 |
if (!(ss_elem_ndx = malloc(tot_num_ss_elem * int_size))) { |
| 161 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 162 |
"ERROR: failed to allocate space for side set elem sort " |
| 163 |
"array for file id %d", |
| 164 |
exoid); |
| 165 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 166 |
err_stat = EX_FATAL; |
| 167 |
goto cleanup; |
| 168 |
} |
| 169 |
|
| 170 |
if (int_size == sizeof(int64_t)) { |
| 171 |
/* Sort side set element list into index array - non-destructive */ |
| 172 |
int64_t *elems = (int64_t *)ss_elem_ndx; |
| 173 |
for (size_t i = 0; i < tot_num_ss_elem; i++) { |
| 174 |
elems[i] = i; /* init index array to current position */ |
| 175 |
} |
| 176 |
exi_iqsort64(side_set_elem_list, elems, tot_num_ss_elem); |
| 177 |
} |
| 178 |
else { |
| 179 |
/* Sort side set element list into index array - non-destructive */ |
| 180 |
int *elems = (int *)ss_elem_ndx; |
| 181 |
for (size_t i = 0; i < tot_num_ss_elem; i++) { |
| 182 |
elems[i] = i; /* init index array to current position */ |
| 183 |
} |
| 184 |
exi_iqsort(side_set_elem_list, elems, tot_num_ss_elem); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/* Allocate space for the element block ids */ |
| 189 |
{ |
| 190 |
int_size = sizeof(int); |
| 191 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 192 |
int_size = sizeof(int64_t); |
| 193 |
} |
| 194 |
|
| 195 |
if (!(elem_blk_ids = malloc(num_elem_blks * int_size))) { |
| 196 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 197 |
"ERROR: failed to allocate space for element block ids " |
| 198 |
"for file id %d", |
| 199 |
exoid); |
| 200 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 201 |
err_stat = EX_FATAL; |
| 202 |
goto cleanup; |
| 203 |
} |
| 204 |
|
| 205 |
if (ex_get_ids(exoid, EX_ELEM_BLOCK, elem_blk_ids) == -1) { |
| 206 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get element block ids in file id %d", |
| 207 |
exoid); |
| 208 |
ex_err_fn(exoid, __func__, errmsg, EX_MSG); |
| 209 |
err_stat = EX_FATAL; |
| 210 |
goto cleanup; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* Allocate space for the element block params */ |
| 215 |
if (!(elem_blk_parms = malloc(num_elem_blks * sizeof(struct exi_elem_blk_parm)))) { |
| 216 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 217 |
"ERROR: failed to allocate space for element block params " |
| 218 |
"for file id %d", |
| 219 |
exoid); |
| 220 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 221 |
err_stat = EX_FATAL; |
| 222 |
goto cleanup; |
| 223 |
} |
| 224 |
|
| 225 |
elem_ctr = 0; |
| 226 |
for (int ib = 0; ib < num_elem_blks; ib++) { |
| 227 |
ex_entity_id id; |
| 228 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 229 |
id = ((int64_t *)elem_blk_ids)[ib]; |
| 230 |
} |
| 231 |
else { |
| 232 |
id = ((int *)elem_blk_ids)[ib]; |
| 233 |
} |
| 234 |
|
| 235 |
err_stat = exi_get_block_param(exoid, id, ndim, &elem_blk_parms[ib]); |
| 236 |
if (err_stat != EX_NOERR) { |
| 237 |
goto cleanup; |
| 238 |
} |
| 239 |
|
| 240 |
elem_ctr += elem_blk_parms[ib].num_elem_in_blk; |
| 241 |
elem_blk_parms[ib].elem_ctr = elem_ctr; /* save elem number max */ |
| 242 |
} |
| 243 |
|
| 244 |
/* Finally... Create the list of node counts for each face in the |
| 245 |
* side set. |
| 246 |
*/ |
| 247 |
|
| 248 |
/* There is not partial read for this function, but all ranks must call because exodus runs |
| 249 |
* NC_COLLECTIVE for all variables. Typically, either all ranks call and get same data, or one |
| 250 |
* rank reads. To do the one rank read, we only store the data if `side_set_node_cnt_list != |
| 251 |
* NULL` |
| 252 |
*/ |
| 253 |
if (side_set_node_cnt_list != NULL) { |
| 254 |
int jb = 0; /* The current element block... */ |
| 255 |
for (size_t ii = 0; ii < tot_num_ss_elem; ii++) { |
| 256 |
|
| 257 |
size_t i = 0; |
| 258 |
if (ints_64) { |
| 259 |
i = ((int64_t *)ss_elem_ndx)[ii]; |
| 260 |
elem = ((int64_t *)side_set_elem_list)[i]; |
| 261 |
side = ((int64_t *)side_set_side_list)[i] - 1; /* Convert to 0-based sides */ |
| 262 |
} |
| 263 |
else { |
| 264 |
i = ((int *)ss_elem_ndx)[ii]; |
| 265 |
elem = ((int *)side_set_elem_list)[i]; |
| 266 |
side = ((int *)side_set_side_list)[i] - 1; /* Convert to 0-based sides */ |
| 267 |
} |
| 268 |
|
| 269 |
/* |
| 270 |
* Since the elements are being accessed in sorted, order, the |
| 271 |
* block that contains the elements must progress sequentially |
| 272 |
* from block 0 to block[num_elem_blks-1]. Once we find an element |
| 273 |
* not in this block, find a following block that contains it... |
| 274 |
*/ |
| 275 |
for (; jb < num_elem_blks; jb++) { |
| 276 |
if (elem <= elem_blk_parms[jb].elem_ctr) { |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if (jb < num_elem_blks) { |
| 282 |
assert(side < elem_blk_parms[jb].num_sides); |
| 283 |
side_set_node_cnt_list[i] = elem_blk_parms[jb].num_nodes_per_side[side]; |
| 284 |
} |
| 285 |
else { |
| 286 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 287 |
"ERROR: Invalid element number %" PRId64 " found in side set %" PRId64 |
| 288 |
" in file %d", |
| 289 |
elem, side_set_id, exoid); |
| 290 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 291 |
err_stat = EX_FATAL; |
| 292 |
goto cleanup; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* All done: release connectivity array space, element block ids |
| 298 |
* array, element block parameters array, and side set element index |
| 299 |
* array |
| 300 |
*/ |
| 301 |
cleanup: |
| 302 |
free(elem_blk_ids); |
| 303 |
free(elem_blk_parms); |
| 304 |
free(ss_elem_ndx); |
| 305 |
free(side_set_side_list); |
| 306 |
free(side_set_elem_list); |
| 307 |
|
| 308 |
EX_FUNC_LEAVE(err_stat); |
| 309 |
} |