Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_get_concat_side_set_node_count.c

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