Debian Codegraph @

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

raw
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 *
10 * expcss - ex_put_concat_sets
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int set_type type of set
16 * struct ex_set_specs* set_specs set specs structure
17 *
18 * exit conditions -
19 *
20 * revision history -
21 *
22 *
23 *****************************************************************************/
24
25 #include "exodusII.h" // for ex_err, etc
26 #include "exodusII_int.h" // for EX_FATAL, exi_comp_ws, etc
27
28 /*!
29 * writes the set ID's, set entry count array, set entry pointers array,
30 * set entry list, set extra list, and distribution factors list for
31 * all the sets of the specified type.
32 * \param exoid exodus file id
33 * \param set_type type of set
34 * \param set_specs set specs structure
35 */
36
37 int ex_put_concat_sets(int exoid, ex_entity_type set_type, const struct ex_set_specs *set_specs)
38 {
39 int status;
40 int temp;
41 const void_int *num_entries_per_set = set_specs->num_entries_per_set;
42 const void_int *num_dist_per_set = set_specs->num_dist_per_set;
43 const void_int *sets_entry_index = set_specs->sets_entry_index;
44 const void_int *sets_dist_index = set_specs->sets_dist_index;
45 const void *sets_dist_fact = set_specs->sets_dist_fact;
46 size_t num_df, num_entry;
47 int i, cur_num_sets, num_sets;
48 int dimid, varid, set_id_ndx, dims[1];
49 int *set_stat = NULL;
50 int set_int_type, int_size;
51
52 const float *flt_dist_fact = NULL;
53 const double *dbl_dist_fact = NULL;
54 char errmsg[MAX_ERR_LENGTH];
55 char *idsptr = NULL;
56 char *statptr = NULL;
57 char *numdfptr = NULL;
58 char *factptr = NULL;
59 char *elemptr = NULL;
60 char *extraptr = NULL;
61 ex_inquiry ex_inq_val;
62
63 EX_FUNC_ENTER();
64 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
65 EX_FUNC_LEAVE(EX_FATAL);
66 }
67
68 int_size = sizeof(int);
69 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
70 int_size = sizeof(int64_t);
71 }
72
73 /* setup pointers based on set_type
74 NOTE: there is another block that sets more stuff later ... */
75
76 if (set_type == EX_NODE_SET) {
77 ex_inq_val = EX_INQ_NODE_SETS;
78 idsptr = VAR_NS_IDS;
79 statptr = VAR_NS_STAT;
80 }
81 else if (set_type == EX_EDGE_SET) {
82 ex_inq_val = EX_INQ_EDGE_SETS;
83 idsptr = VAR_ES_IDS;
84 statptr = VAR_ES_STAT;
85 }
86 else if (set_type == EX_FACE_SET) {
87 ex_inq_val = EX_INQ_FACE_SETS;
88 idsptr = VAR_FS_IDS;
89 statptr = VAR_FS_STAT;
90 }
91 else if (set_type == EX_SIDE_SET) {
92 ex_inq_val = EX_INQ_SIDE_SETS;
93 idsptr = VAR_SS_IDS;
94 statptr = VAR_SS_STAT;
95 }
96 else if (set_type == EX_ELEM_SET) {
97 ex_inq_val = EX_INQ_ELEM_SETS;
98 idsptr = VAR_ELS_IDS;
99 statptr = VAR_ELS_STAT;
100 }
101 else {
102 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: invalid set type (%d)", set_type);
103 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
104 EX_FUNC_LEAVE(EX_FATAL);
105 }
106
107 /* first check if any sets are specified */
108 if ((status = nc_inq_dimid(exoid, exi_dim_num_objects(set_type), &temp)) != NC_NOERR) {
109 if (status == NC_EBADDIM) {
110 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss defined for file id %d",
111 ex_name_of_object(set_type), exoid);
112 ex_err_fn(exoid, __func__, errmsg, status);
113 }
114 else {
115 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %ss defined in file id %d",
116 ex_name_of_object(set_type), exoid);
117 ex_err_fn(exoid, __func__, errmsg, status);
118 }
119 EX_FUNC_LEAVE(EX_FATAL);
120 }
121
122 /* inquire how many sets are to be stored */
123 num_sets = ex_inquire_int(exoid, ex_inq_val);
124 if (num_sets < 0) {
125 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of %ss defined for file id %d",
126 ex_name_of_object(set_type), exoid);
127 /* use error val from inquire */
128 ex_err_fn(exoid, __func__, errmsg, EX_LASTERR);
129 EX_FUNC_LEAVE(EX_FATAL);
130 }
131
132 /* Fill out set status array */
133
134 /* First, allocate space for the status list */
135 if (!(set_stat = malloc(num_sets * sizeof(int)))) {
136 snprintf(errmsg, MAX_ERR_LENGTH,
137 "ERROR: failed to allocate space for %s status array in file id %d",
138 ex_name_of_object(set_type), exoid);
139 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
140 EX_FUNC_LEAVE(EX_FATAL);
141 }
142
143 if (int_size == sizeof(int64_t)) {
144 for (i = 0; i < num_sets; i++) {
145 set_stat[i] = (((int64_t *)num_entries_per_set)[i] == 0) ? 0 : 1;
146 }
147 }
148 else {
149 for (i = 0; i < num_sets; i++) {
150 set_stat[i] = (((int *)num_entries_per_set)[i] == 0) ? 0 : 1;
151 }
152 }
153
154 /* Next, get variable id of status array */
155 if ((status = nc_inq_varid(exoid, statptr, &varid)) != NC_NOERR) {
156 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s status in file id %d",
157 ex_name_of_object(set_type), exoid);
158 ex_err_fn(exoid, __func__, errmsg, status);
159 free(set_stat);
160 EX_FUNC_LEAVE(EX_FATAL);
161 }
162
163 status = nc_put_var_int(exoid, varid, set_stat);
164
165 if (status != NC_NOERR) {
166 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s status array to file id %d",
167 ex_name_of_object(set_type), exoid);
168 ex_err_fn(exoid, __func__, errmsg, status);
169 free(set_stat);
170 EX_FUNC_LEAVE(EX_FATAL);
171 }
172
173 /* put netcdf file into define mode */
174 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
175 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
176 ex_err_fn(exoid, __func__, errmsg, status);
177 free(set_stat);
178 EX_FUNC_LEAVE(EX_FATAL);
179 }
180
181 /* create set definitions */
182 for (i = 0; i < num_sets; i++) {
183 int64_t set_id;
184 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
185 set_id = ((int64_t *)set_specs->sets_ids)[i];
186 }
187 else {
188 set_id = ((int *)set_specs->sets_ids)[i];
189 }
190
191 /* Keep track of the total number of sets defined using a counter stored
192 in a linked list keyed by exoid.
193 NOTE: exi_get_file_item is used to find the number of sets of type
194 for a specific file and returns that value.
195 */
196 cur_num_sets = exi_get_file_item(exoid, exi_get_counter_list(set_type));
197 if (cur_num_sets >= num_sets) {
198 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: exceeded number of %ss (%d) defined in file id %d",
199 ex_name_of_object(set_type), num_sets, exoid);
200 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
201 goto error_ret;
202 }
203
204 /* NOTE: exi_inc_file_item is used to find the number of sets
205 for a specific file and returns that value incremented. */
206
207 cur_num_sets = exi_inc_file_item(exoid, exi_get_counter_list(set_type));
208 set_id_ndx = cur_num_sets + 1;
209
210 /* setup more pointers based on set_type */
211 if (set_type == EX_NODE_SET) {
212 elemptr = VAR_NODE_NS(set_id_ndx);
213 extraptr = NULL;
214 /* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */
215 numdfptr = DIM_NUM_NOD_NS(set_id_ndx);
216 factptr = VAR_FACT_NS(set_id_ndx);
217 }
218 else if (set_type == EX_EDGE_SET) {
219 elemptr = VAR_EDGE_ES(set_id_ndx);
220 extraptr = VAR_ORNT_ES(set_id_ndx);
221 numdfptr = DIM_NUM_DF_ES(set_id_ndx);
222 factptr = VAR_FACT_ES(set_id_ndx);
223 }
224 else if (set_type == EX_FACE_SET) {
225 elemptr = VAR_FACE_FS(set_id_ndx);
226 extraptr = VAR_ORNT_FS(set_id_ndx);
227 numdfptr = DIM_NUM_DF_FS(set_id_ndx);
228 factptr = VAR_FACT_FS(set_id_ndx);
229 }
230 else if (set_type == EX_SIDE_SET) {
231 elemptr = VAR_ELEM_SS(set_id_ndx);
232 extraptr = VAR_SIDE_SS(set_id_ndx);
233 numdfptr = DIM_NUM_DF_SS(set_id_ndx);
234 factptr = VAR_FACT_SS(set_id_ndx);
235 }
236 if (set_type == EX_ELEM_SET) {
237 elemptr = VAR_ELEM_ELS(set_id_ndx);
238 extraptr = NULL;
239 numdfptr = DIM_NUM_DF_ELS(set_id_ndx);
240 factptr = VAR_FACT_ELS(set_id_ndx);
241 }
242
243 /* define dimension for number of entries per set */
244 if (set_stat[i] == 0) { /* Is this a NULL set? */
245 continue; /* Do not create anything for NULL sets! */
246 }
247
248 if (int_size == sizeof(int)) {
249 status = nc_def_dim(exoid, exi_dim_num_entries_in_object(set_type, set_id_ndx),
250 ((int *)num_entries_per_set)[i], &dimid);
251 }
252 else {
253 status = nc_def_dim(exoid, exi_dim_num_entries_in_object(set_type, set_id_ndx),
254 ((int64_t *)num_entries_per_set)[i], &dimid);
255 }
256
257 if (status != NC_NOERR) {
258 if (status == NC_ENAMEINUSE) {
259 snprintf(errmsg, MAX_ERR_LENGTH,
260 "ERROR: %s entry count %" PRId64 " already defined in file id %d",
261 ex_name_of_object(set_type), set_id, exoid);
262 ex_err_fn(exoid, __func__, errmsg, status);
263 }
264 else {
265 snprintf(errmsg, MAX_ERR_LENGTH,
266 "ERROR: failed to define number of entries for %s %" PRId64 " in file id %d",
267 ex_name_of_object(set_type), set_id, exoid);
268 ex_err_fn(exoid, __func__, errmsg, status);
269 }
270 goto error_ret;
271 }
272
273 /* create element list variable for set */
274 set_int_type = NC_INT;
275 if (ex_int64_status(exoid) & EX_BULK_INT64_DB) {
276 set_int_type = NC_INT64;
277 }
278
279 dims[0] = dimid;
280 if ((status = nc_def_var(exoid, elemptr, set_int_type, 1, dims, &temp)) != NC_NOERR) {
281 if (status == NC_ENAMEINUSE) {
282 snprintf(errmsg, MAX_ERR_LENGTH,
283 "ERROR: element list already exists for %s %" PRId64 " in file id %d",
284 ex_name_of_object(set_type), set_id, exoid);
285 ex_err_fn(exoid, __func__, errmsg, status);
286 }
287 else {
288 snprintf(errmsg, MAX_ERR_LENGTH,
289 "ERROR: failed to create element list for %s %" PRId64 " in file id %d",
290 ex_name_of_object(set_type), set_id, exoid);
291 ex_err_fn(exoid, __func__, errmsg, status);
292 }
293 goto error_ret; /* exit define mode and return */
294 }
295 exi_compress_variable(exoid, temp, 1);
296
297 /* create extra list variable for set (only for edge, face and side sets)
298 */
299 if (extraptr) {
300 if ((status = nc_def_var(exoid, extraptr, set_int_type, 1, dims, &temp)) != NC_NOERR) {
301 if (status == NC_ENAMEINUSE) {
302 snprintf(errmsg, MAX_ERR_LENGTH,
303 "ERROR: extra list already exists for %s %" PRId64 " in file id %d",
304 ex_name_of_object(set_type), set_id, exoid);
305 ex_err_fn(exoid, __func__, errmsg, status);
306 }
307 else {
308 snprintf(errmsg, MAX_ERR_LENGTH,
309 "ERROR: failed to create extra list for %s %" PRId64 " in file id %d",
310 ex_name_of_object(set_type), set_id, exoid);
311 ex_err_fn(exoid, __func__, errmsg, status);
312 }
313 goto error_ret; /* exit define mode and return */
314 }
315 exi_compress_variable(exoid, temp, 1);
316 }
317
318 /* define dimension for number of dist factors per set */
319 /* NOTE: only define df count if the dist factors exist! */
320 if (int_size == sizeof(int64_t)) {
321 num_df = ((int64_t *)num_dist_per_set)[i];
322 num_entry = ((int64_t *)num_entries_per_set)[i];
323 }
324 else {
325 num_df = ((int *)num_dist_per_set)[i];
326 num_entry = ((int *)num_entries_per_set)[i];
327 }
328
329 if (num_df > 0) {
330
331 if (set_type == EX_NODE_SET) {
332 if (num_df != num_entry) {
333 snprintf(errmsg, MAX_ERR_LENGTH,
334 "ERROR: # dist fact (%zu) not equal to # nodes (%zu) in node set %" PRId64
335 " file id %d",
336 num_df, num_entry, set_id, exoid);
337 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
338 goto error_ret; /* exit define mode and return */
339 }
340
341 /* reuse dimid from entry lists */
342 }
343 else {
344 if ((status = nc_def_dim(exoid, numdfptr, num_df, &dimid)) != NC_NOERR) {
345 if (status == NC_ENAMEINUSE) {
346 snprintf(errmsg, MAX_ERR_LENGTH,
347 "ERROR: %s df count %" PRId64 " already defined in file id %d",
348 ex_name_of_object(set_type), set_id, exoid);
349 ex_err_fn(exoid, __func__, errmsg, status);
350 }
351 else {
352 snprintf(errmsg, MAX_ERR_LENGTH,
353 "ERROR: failed to define %s df count for set %" PRId64 " in file id %d",
354 ex_name_of_object(set_type), set_id, exoid);
355 ex_err_fn(exoid, __func__, errmsg, status);
356 }
357 goto error_ret;
358 }
359 }
360
361 /* create distribution factor list variable for set */
362 dims[0] = dimid;
363 if ((status = nc_def_var(exoid, factptr, nc_flt_code(exoid), 1, dims, &temp)) != NC_NOERR) {
364 if (status == NC_ENAMEINUSE) {
365 snprintf(errmsg, MAX_ERR_LENGTH,
366 "ERROR: dist factor list already exists for %s %" PRId64 " in file id %d",
367 ex_name_of_object(set_type), set_id, exoid);
368 ex_err_fn(exoid, __func__, errmsg, status);
369 }
370 else {
371 snprintf(errmsg, MAX_ERR_LENGTH,
372 "ERROR: failed to create dist factor list for %s %" PRId64 " in file id %d",
373 ex_name_of_object(set_type), set_id, exoid);
374 ex_err_fn(exoid, __func__, errmsg, status);
375 }
376 goto error_ret; /* exit define mode and return */
377 }
378 exi_compress_variable(exoid, temp, 2);
379 } /* end define dist factors */
380 }
381
382 /* leave define mode */
383 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
384 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode");
385 ex_err_fn(exoid, __func__, errmsg, status);
386 free(set_stat);
387 EX_FUNC_LEAVE(EX_FATAL);
388 }
389
390 /* Next, fill out set ids array */
391
392 /* first get id of set ids array variable */
393 if ((status = nc_inq_varid(exoid, idsptr, &varid)) != NC_NOERR) {
394 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids array in file id %d",
395 ex_name_of_object(set_type), exoid);
396 ex_err_fn(exoid, __func__, errmsg, status);
397 free(set_stat);
398 EX_FUNC_LEAVE(EX_FATAL);
399 }
400
401 /* then, write out set id list */
402 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
403 status = nc_put_var_longlong(exoid, varid, set_specs->sets_ids);
404 }
405 else {
406 status = nc_put_var_int(exoid, varid, set_specs->sets_ids);
407 }
408
409 if (status != NC_NOERR) {
410 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s id array in file id %d",
411 ex_name_of_object(set_type), exoid);
412 ex_err_fn(exoid, __func__, errmsg, status);
413 free(set_stat);
414 EX_FUNC_LEAVE(EX_FATAL);
415 }
416
417 /* If the sets_entry_index is passed in as a NULL pointer, then
418 * the user only wants us to define the sets and not populate
419 * the data structures.
420 */
421 if (sets_entry_index == 0) {
422 free(set_stat);
423 EX_FUNC_LEAVE(EX_NOERR);
424 }
425
426 /* Now, use ExodusII call to store sets */
427 for (i = 0; i < num_sets; i++) {
428 int64_t set_id;
429 size_t df_ndx;
430
431 if (set_stat[i] == 0) { /* Is this a NULL set? */
432 continue; /* Do not create anything for NULL sets! */
433 }
434
435 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
436 set_id = ((int64_t *)set_specs->sets_ids)[i];
437 }
438 else {
439 set_id = ((int *)set_specs->sets_ids)[i];
440 }
441
442 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
443 int64_t *extra_list = NULL;
444 /* set extra list */
445 if (set_type == EX_EDGE_SET || set_type == EX_FACE_SET || set_type == EX_SIDE_SET) {
446 extra_list = &(((int64_t *)set_specs->sets_extra_list)[((int64_t *)sets_entry_index)[i]]);
447 }
448
449 status = ex_put_set(
450 exoid, set_type, set_id,
451 &(((int64_t *)set_specs->sets_entry_list)[((int64_t *)sets_entry_index)[i]]), extra_list);
452 }
453 else {
454 int *extra_list = NULL;
455 /* set extra list */
456 if (set_type == EX_EDGE_SET || set_type == EX_FACE_SET || set_type == EX_SIDE_SET) {
457 extra_list = &(((int *)set_specs->sets_extra_list)[((int *)sets_entry_index)[i]]);
458 }
459
460 status = ex_put_set(exoid, set_type, set_id,
461 &(((int *)set_specs->sets_entry_list)[((int *)sets_entry_index)[i]]),
462 extra_list);
463 }
464 if (status != NC_NOERR) {
465 free(set_stat);
466 EX_FUNC_LEAVE(EX_FATAL); /* error will be reported by subroutine */
467 }
468
469 if (int_size == sizeof(int)) {
470 num_df = ((int *)num_dist_per_set)[i];
471 df_ndx = ((int *)sets_dist_index)[i];
472 }
473 else {
474 num_df = ((int64_t *)num_dist_per_set)[i];
475 df_ndx = ((int64_t *)sets_dist_index)[i];
476 }
477
478 if (exi_comp_ws(exoid) == sizeof(float)) {
479 flt_dist_fact = sets_dist_fact;
480 if (num_df > 0) { /* store dist factors if required */
481 if (ex_put_set_dist_fact(exoid, set_type, set_id, &(flt_dist_fact[df_ndx])) == -1) {
482 snprintf(errmsg, MAX_ERR_LENGTH,
483 "ERROR: failed to store %s %" PRId64 " dist factors for file id %d",
484 ex_name_of_object(set_type), set_id, exoid);
485 /* use error val from exodusII routine */
486 ex_err_fn(exoid, __func__, errmsg, EX_LASTERR);
487 free(set_stat);
488 EX_FUNC_LEAVE(EX_FATAL);
489 }
490 }
491 }
492 else if (exi_comp_ws(exoid) == sizeof(double)) {
493 dbl_dist_fact = sets_dist_fact;
494 if (num_df) { /* only store if they exist */
495 if (ex_put_set_dist_fact(exoid, set_type, set_id, &(dbl_dist_fact[df_ndx])) == -1) {
496 snprintf(errmsg, MAX_ERR_LENGTH,
497 "ERROR: failed to store %s %" PRId64 " dist factors for file id %d",
498 ex_name_of_object(set_type), set_id, exoid);
499 /* use error val from exodusII routine */
500 ex_err_fn(exoid, __func__, errmsg, EX_LASTERR);
501 free(set_stat);
502 EX_FUNC_LEAVE(EX_FATAL);
503 }
504 }
505 }
506 else {
507 /* unknown floating point word size */
508 snprintf(errmsg, MAX_ERR_LENGTH,
509 "ERROR: unsupported floating point word size %d for file id %d", exi_comp_ws(exoid),
510 exoid);
511 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
512 free(set_stat);
513 EX_FUNC_LEAVE(EX_FATAL);
514 }
515 }
516 free(set_stat);
517 EX_FUNC_LEAVE(EX_NOERR);
518
519 /* Fatal error: exit definition mode and return */
520 error_ret:
521 free(set_stat);
522
523 exi_leavedef(exoid, __func__);
524 EX_FUNC_LEAVE(EX_FATAL);
525 }