Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2025 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 * exutils - utility routines
11 *
12 *****************************************************************************/
13
14 #define _GNU_SOURCE
15 #include <stdlib.h>
16
17 #include <errno.h>
18 #include <stdbool.h>
19
20 #include "exodusII.h"
21 #include "exodusII_int.h"
22
23 struct exi_obj_stats *exoII_eb = NULL;
24 struct exi_obj_stats *exoII_ed = NULL;
25 struct exi_obj_stats *exoII_fa = NULL;
26 struct exi_obj_stats *exoII_ns = NULL;
27 struct exi_obj_stats *exoII_es = NULL;
28 struct exi_obj_stats *exoII_fs = NULL;
29 struct exi_obj_stats *exoII_ss = NULL;
30 struct exi_obj_stats *exoII_els = NULL;
31 struct exi_obj_stats *exoII_em = NULL;
32 struct exi_obj_stats *exoII_edm = NULL;
33 struct exi_obj_stats *exoII_fam = NULL;
34 struct exi_obj_stats *exoII_nm = NULL;
35 struct exi_obj_stats *exoII_ass = NULL;
36
37 /*****************************************************************************
38 *
39 * utility routines for string conversions
40 * exi_catstr - concatenate string/number (where number is converted to ASCII)
41 * exi_catstr2 - concatenate string1/number1/string2/number2 "
42 *
43 * NOTE: these routines reuse the same storage over and over to build
44 * concatenated strings, because the strings are just passed to netCDF
45 * routines as names used to look up variables. if the strings returned
46 * by these routines are needed for any other purpose, they should
47 * immediately be copied into other storage.
48 *****************************************************************************/
49
50 static char ret_string[10 * (MAX_VAR_NAME_LENGTH + 1)];
51 static char *cur_string = &ret_string[0];
52
53 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
54 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
55 #define WINDOWS__ 1
56 #endif
57
58 #ifndef WINDOWS__
59 #if NC_HAS_HDF5
60 extern int NC4_hdf5get_libversion(unsigned *, unsigned *, unsigned *);
61 #endif
62 #endif
63
64 #if 0
65 #if NC_HAS_PNETCDF
66 extern char *ncmpi_inq_libvers();
67 #endif
68 #endif
69
70 /*!
71 \ingroup Utilities
72 \undoc
73 */
74 const char *ex_config(void)
75 {
76 static char buffer[2048];
77 int buffer_size = sizeof(buffer) / sizeof(buffer[0]);
78
79 int j = snprintf(buffer, buffer_size, "\tExodus Version %s, Released %s\n", EXODUS_VERSION,
80 EXODUS_RELEASE_DATE);
81 #if defined(PARALLEL_AWARE_EXODUS)
82 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel enabled\n");
83 #else
84 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel NOT enabled\n");
85 #endif
86 #if defined(EXODUS_THREADSAFE)
87 j += snprintf(buffer + j, buffer_size - j, "\t\tThread Safe enabled\n");
88 #else
89 j += snprintf(buffer + j, buffer_size - j, "\t\tThread Safe NOT enabled\n");
90 #endif
91 #if defined(SEACAS_HIDE_DEPRECATED_CODE)
92 j += snprintf(buffer + j, buffer_size - j, "\t\tDeprecated Functions NOT built\n\n");
93 #else
94 j += snprintf(buffer + j, buffer_size - j, "\t\tDeprecated Functions available\n\n");
95 #endif
96 #if defined(NC_VERSION)
97 j += snprintf(buffer + j, buffer_size - j, "\tNetCDF Version %s\n", NC_VERSION);
98 #else
99 j += snprintf(buffer + j, buffer_size - j, "\tNetCDF Version < 4.3.3\n");
100 #endif
101 #if NC_HAS_CDF5
102 j += snprintf(buffer + j, buffer_size - j, "\t\tCDF5 enabled\n");
103 #endif
104 #ifndef WINDOWS__
105 #if NC_HAS_HDF5
106 {
107 unsigned major, minor, release;
108 NC4_hdf5get_libversion(&major, &minor, &release);
109 j += snprintf(buffer + j, buffer_size - j, "\t\tHDF5 enabled (%u.%u.%u)\n", major, minor,
110 release);
111 }
112 j += snprintf(buffer + j, buffer_size - j, "\t\tZlib Compression (read/write) enabled\n");
113 #if NC_HAS_SZIP_WRITE == 1
114 j += snprintf(buffer + j, buffer_size - j, "\t\tSZip Compression (read/write) enabled\n");
115 #else
116 j += snprintf(buffer + j, buffer_size - j, "\t\tSZip Compression (read/write) NOT enabled\n");
117 #endif
118 #if NC_HAS_ZSTD == 1
119 j += snprintf(buffer + j, buffer_size - j, "\t\tZstd Compression enabled\n");
120 #else
121 j += snprintf(buffer + j, buffer_size - j, "\t\tZstd Compression NOT enabled\n");
122 #endif
123 #if NC_HAS_QUANTIZE == 1
124 j += snprintf(buffer + j, buffer_size - j, "\t\tQuantization support enabled\n");
125 #else
126 j += snprintf(buffer + j, buffer_size - j, "\t\tQuantization support NOT enabled\n");
127 #endif
128 #endif
129 #endif
130 #if defined(PARALLEL_AWARE_EXODUS)
131 #if NC_HAS_PARALLEL
132 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO enabled via HDF5 and/or PnetCDF\n");
133 #else
134 j +=
135 snprintf(buffer + j, buffer_size - j,
136 "\t\tParallel IO *NOT* enabled via HDF5 and/or PnetCDF (PROBABLY A BUILD ERROR!)\n");
137 #endif
138 #if NC_HAS_PARALLEL4
139 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO enabled via HDF5\n");
140 #else
141 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO *NOT* enabled via HDF5\n");
142 #endif
143 #if NC_HAS_PAR_FILTERS
144 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO supports filters\n");
145 #endif
146 #if NC_HAS_PNETCDF
147 {
148 #if 0
149 char *libver = ncmpi_inq_libvers();
150 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO enabled via PnetCDF (%s)\n", libver);
151 #else
152 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO enabled via PnetCDF.\n");
153 #endif
154 }
155 #else
156 j += snprintf(buffer + j, buffer_size - j, "\t\tParallel IO *NOT* enabled via PnetCDF\n");
157 #endif
158 #endif /* PARALLEL_AWARE_EXODUS */
159
160 #if NC_HAS_ERANGE_FILL
161 j += snprintf(buffer + j, buffer_size - j, "\t\tERANGE_FILL support\n");
162 #endif
163 #if NC_RELAX_COORD_BOUND
164 j += snprintf(buffer + j, buffer_size - j, "\t\tRELAX_COORD_BOUND defined\n");
165 #endif
166 #if defined(NC_COMPACT)
167 j += snprintf(buffer + j, buffer_size - j, "\t\tNC_COMPACT defined\n");
168 #endif
169 #if defined(NC_HAVE_META_H)
170 j += snprintf(buffer + j, buffer_size - j, "\t\tNC_HAVE_META_H defined\n");
171 #endif
172 #if NC_HAS_NC2
173 j += snprintf(buffer + j, buffer_size - j, "\t\tAPI Version 2 support enabled\n");
174 #else
175 j += snprintf(buffer + j, buffer_size - j, "\t\tAPI Version 2 support NOT enabled\n");
176 #endif
177 j += snprintf(buffer + j, buffer_size - j, "\n");
178
179 assert(j < 2048);
180 return buffer;
181 }
182 void ex_print_config(void) { fprintf(stderr, "%s\n", ex_config()); }
183
184 /*!
185 \ingroup Utilities
186 \undoc
187 */
188 int exi_check_file_type(const char *path, int *type)
189 {
190 /* Based on (stolen from?) NC_check_file_type from netcdf sources.
191
192 Type is set to:
193 1 if this is a netcdf classic file,
194 2 if this is a netcdf 64-bit offset file,
195 4 pnetcdf cdf5 file.
196 5 if this is an hdf5 file
197 */
198
199 #define MAGIC_NUMBER_LEN 4
200
201 char magic[MAGIC_NUMBER_LEN + 1];
202 EX_FUNC_ENTER();
203
204 *type = 0;
205
206 /* Get the 4-byte magic from the beginning of the file. */
207 {
208 FILE *fp;
209 if (!(fp = fopen(path, "r"))) {
210 char errmsg[MAX_ERR_LENGTH];
211 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Could not open file '%s',\n\t\terror = %s.", path,
212 strerror(errno));
213 ex_err(__func__, errmsg, EX_WRONGFILETYPE);
214 EX_FUNC_LEAVE(EX_FATAL);
215 }
216 int i = fread(magic, 1, MAGIC_NUMBER_LEN, fp);
217 magic[MAGIC_NUMBER_LEN] = '\0';
218 fclose(fp);
219 if (i != MAGIC_NUMBER_LEN) {
220 char errmsg[MAX_ERR_LENGTH];
221 snprintf(errmsg, MAX_ERR_LENGTH,
222 "ERROR: Could not read magic data from file '%s',\n\t\terror = %s.", path,
223 strerror(errno));
224 ex_err(__func__, errmsg, EX_WRONGFILETYPE);
225 EX_FUNC_LEAVE(EX_FATAL);
226 }
227 }
228
229 /* Ignore the first byte for HDF */
230 if (magic[1] == 'H' && magic[2] == 'D' && magic[3] == 'F') {
231 *type = 5;
232 }
233 else if (magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F') {
234 if (magic[3] == '\001') {
235 *type = 1;
236 }
237 else if (magic[3] == '\002') {
238 *type = 2;
239 }
240 else if (magic[3] == '\005') {
241 *type = 4; /* cdf5 (including pnetcdf) file */
242 }
243 }
244 if (*type == 0) {
245 char errmsg[MAX_ERR_LENGTH];
246 snprintf(
247 errmsg, MAX_ERR_LENGTH,
248 "ERROR: Could not recognize %s as a valid Exodus/NetCDF file variant. Magic value is '%s'",
249 path, magic);
250 ex_err(__func__, errmsg, EX_WRONGFILETYPE);
251 EX_FUNC_LEAVE(EX_FATAL);
252 }
253 EX_FUNC_LEAVE(EX_NOERR);
254 }
255
256 /*!
257 \ingroup Utilities
258 \undoc
259 */
260 int ex_set_max_name_length(int exoid, int length)
261 {
262 char errmsg[MAX_ERR_LENGTH];
263
264 EX_FUNC_ENTER();
265 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
266 EX_FUNC_LEAVE(EX_FATAL);
267 }
268 if (length <= 0) {
269 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Max name length must be positive.");
270 ex_err_fn(exoid, __func__, errmsg, NC_EMAXNAME);
271 EX_FUNC_LEAVE(EX_FATAL);
272 }
273 if (length > NC_MAX_NAME) {
274 snprintf(errmsg, MAX_ERR_LENGTH,
275 "ERROR: Max name length (%d) exceeds netcdf max name size (%d).", length, NC_MAX_NAME);
276 ex_err_fn(exoid, __func__, errmsg, NC_EMAXNAME);
277 EX_FUNC_LEAVE(EX_FATAL);
278 }
279
280 ex_set_option(exoid, EX_OPT_MAX_NAME_LENGTH, length);
281
282 EX_FUNC_LEAVE(EX_NOERR);
283 }
284
285 /*!
286 \ingroup Utilities
287 \undoc
288 */
289 void exi_update_max_name_length(int exoid, int length)
290 {
291 EX_FUNC_ENTER();
292 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
293 EX_FUNC_VOID();
294 }
295
296 /* Get current value of the maximum_name_length attribute... */
297 int status;
298 int db_length = 0;
299 int rootid = exoid & EX_FILE_ID_MASK;
300 if ((status = nc_get_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &db_length)) != NC_NOERR) {
301 char errmsg[MAX_ERR_LENGTH];
302 snprintf(errmsg, MAX_ERR_LENGTH,
303 "ERROR: failed to update 'max_name_length' attribute in file id %d", exoid);
304 ex_err_fn(exoid, __func__, errmsg, status);
305 }
306
307 if (length > db_length) {
308 /* Update with new value... */
309 ex_set_max_name_length(exoid, length);
310 if ((status = nc_put_att_int(rootid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &length)) !=
311 NC_NOERR) {
312 char errmsg[MAX_ERR_LENGTH];
313 snprintf(errmsg, MAX_ERR_LENGTH,
314 "ERROR: failed to update 'max_name_length' attribute with new value in file id %d",
315 exoid);
316 ex_err_fn(exoid, __func__, errmsg, status);
317 }
318 nc_sync(rootid);
319 }
320 EX_FUNC_VOID();
321 }
322
323 /*!
324 \internal
325 \undoc
326 */
327 int exi_put_names(int exoid, int varid, size_t num_names, char *const *names,
328 ex_entity_type obj_type, const char *subtype, const char *routine)
329 {
330 EX_FUNC_ENTER();
331 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
332 EX_FUNC_LEAVE(EX_FATAL);
333 }
334 /* inquire previously defined dimensions */
335 size_t name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH) + 1;
336
337 char *int_names = NULL;
338 if (!(int_names = calloc(num_names * name_length, 1))) {
339 char errmsg[MAX_ERR_LENGTH];
340 snprintf(errmsg, MAX_ERR_LENGTH,
341 "ERROR: failed to allocate memory for internal int_names "
342 "array in file id %d",
343 exoid);
344 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
345 EX_FUNC_LEAVE(EX_FATAL);
346 }
347
348 size_t idx = 0;
349 int max_name_len = 0;
350 int found_name = 0;
351 for (size_t i = 0; i < num_names; i++) {
352 if (names != NULL && *names != NULL && *names[i] != '\0') {
353 found_name = 1;
354 ex_copy_string(&int_names[idx], names[i], name_length);
355 size_t length = strlen(names[i]) + 1;
356 if (length > (size_t)name_length) {
357 fprintf(stderr,
358 "Warning: The %s %s name '%s' is too long.\n\tIt will "
359 "be truncated from %d to %d characters. [Called from %s]\n",
360 ex_name_of_object(obj_type), subtype, names[i], (int)length - 1,
361 (int)name_length - 1, routine);
362 length = name_length;
363 }
364
365 if (length > (size_t)max_name_len) {
366 max_name_len = length;
367 }
368 }
369 idx += name_length;
370 }
371
372 int status;
373 if ((status = nc_put_var_text(exoid, varid, int_names)) != NC_NOERR) {
374 free(int_names);
375 char errmsg[MAX_ERR_LENGTH];
376 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s names in file id %d",
377 ex_name_of_object(obj_type), exoid);
378 ex_err_fn(exoid, __func__, errmsg, status);
379 EX_FUNC_LEAVE(EX_FATAL);
380 }
381
382 if (found_name) {
383
384 /* Update the maximum_name_length attribute on the file. */
385 exi_update_max_name_length(exoid, max_name_len - 1);
386 }
387 free(int_names);
388
389 EX_FUNC_LEAVE(EX_NOERR);
390 }
391
392 /*!
393 \internal
394 \undoc
395 */
396 int exi_put_name(int exoid, int varid, size_t index, const char *name, ex_entity_type obj_type,
397 const char *subtype, const char *routine)
398 {
399 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
400 EX_FUNC_LEAVE(EX_FATAL);
401 }
402
403 /* inquire previously defined dimensions */
404 size_t name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH) + 1;
405
406 if (name != NULL && *name != '\0') {
407 size_t start[2], count[2];
408 int too_long = 0;
409 start[0] = index;
410 start[1] = 0;
411
412 count[0] = 1;
413 count[1] = strlen(name) + 1;
414
415 if (count[1] > name_length) {
416 fprintf(stderr,
417 "Warning: The %s %s name '%s' is too long.\n\tIt will be "
418 "truncated from %d to %d characters. [Called from %s]\n",
419 ex_name_of_object(obj_type), subtype, name, (int)strlen(name), (int)name_length - 1,
420 routine);
421 count[1] = name_length;
422 too_long = 1;
423 }
424
425 int status;
426 if ((status = nc_put_vara_text(exoid, varid, start, count, name)) != NC_NOERR) {
427 char errmsg[MAX_ERR_LENGTH];
428 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s name in file id %d",
429 ex_name_of_object(obj_type), exoid);
430 ex_err_fn(exoid, __func__, errmsg, status);
431 return EX_FATAL;
432 }
433
434 /* Add the trailing null if the variable name was too long */
435 if (too_long) {
436 start[1] = name_length - 1;
437 nc_put_var1_text(exoid, varid, start, "\0");
438 }
439
440 /* Update the maximum_name_length attribute on the file. */
441 exi_update_max_name_length(exoid, count[1] - 1);
442 }
443 return EX_NOERR;
444 }
445
446 /*!
447 \internal
448 \undoc
449 */
450 int exi_get_names(int exoid, int varid, size_t num_names, char **names, ex_entity_type obj_type,
451 const char *routine)
452 {
453 /* Query size of names on file
454 * Use the smaller of the size on file or user-specified length
455 */
456 int db_name_size = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH);
457 int api_name_size = ex_inquire_int(exoid, EX_INQ_MAX_READ_NAME_LENGTH);
458 int name_size = db_name_size < api_name_size ? db_name_size : api_name_size;
459
460 for (size_t i = 0; i < num_names; i++) {
461 int status = exi_get_name(exoid, varid, i, names[i], name_size, obj_type, routine);
462 if (status != NC_NOERR) {
463 return status;
464 }
465 }
466 return EX_NOERR;
467 }
468
469 /*!
470 \internal
471 \undoc
472 */
473 int exi_get_name(int exoid, int varid, size_t index, char *name, int name_size,
474 ex_entity_type obj_type, const char *routine)
475 {
476
477 /* read the name */
478 size_t start[2], count[2];
479 start[0] = index;
480 count[0] = 1;
481 start[1] = 0;
482 count[1] = name_size + 1;
483
484 int status = nc_get_vara_text(exoid, varid, start, count, name);
485 if (status != NC_NOERR) {
486 char errmsg[MAX_ERR_LENGTH];
487 snprintf(errmsg, MAX_ERR_LENGTH,
488 "ERROR: failed to get %s name at index %d from file id %d [Called from %s]",
489 ex_name_of_object(obj_type), (int)index, exoid, routine);
490 ex_err_fn(exoid, __func__, errmsg, status);
491 return EX_FATAL;
492 }
493
494 int api_name_size = ex_inquire_int(exoid, EX_INQ_MAX_READ_NAME_LENGTH);
495 name[api_name_size] = '\0';
496
497 exi_trim(name);
498 return EX_NOERR;
499 }
500
501 /*!
502 \internal
503 \undoc
504 */
505 void exi_trim(char *name)
506 {
507 /* Thread-safe, reentrant */
508 /* Trim trailing spaces... */
509 if (name == NULL) {
510 return;
511 }
512
513 size_t size = strlen(name);
514 if (size == 0) {
515 return;
516 }
517
518 char *end = name + size - 1;
519 while (end >= name && isspace(*end)) {
520 end--;
521 }
522
523 *(end + 1) = '\0';
524 }
525
526 /** exi_catstr - concatenate string/number (where number is converted to ASCII)
527 */
528 /*!
529 \internal
530 \undoc
531 */
532 char *exi_catstr(const char *string, int num)
533 {
534 /* Only called from an already locked function */
535 char *tmp_string = cur_string;
536 cur_string += snprintf(cur_string, MAX_VAR_NAME_LENGTH + 1, "%s%d", string, num) + 1;
537 if (cur_string - ret_string > 9 * (MAX_VAR_NAME_LENGTH + 1)) {
538 cur_string = ret_string;
539 }
540 return tmp_string;
541 }
542
543 /** exi_catstr2 - concatenate string1num1string2num2 */
544 /*!
545 \internal
546 \undoc
547 */
548 char *exi_catstr2(const char *string1, int num1, const char *string2, int num2)
549 {
550 /* Only called from an already locked function */
551 char *tmp_string = cur_string;
552 cur_string +=
553 snprintf(cur_string, MAX_VAR_NAME_LENGTH + 1, "%s%d%s%d", string1, num1, string2, num2) + 1;
554 if (cur_string - ret_string > 9 * (MAX_VAR_NAME_LENGTH + 1)) {
555 cur_string = ret_string;
556 }
557 return tmp_string;
558 }
559
560 /*!
561 \internal
562 \undoc
563 */
564 char *ex_name_of_object(ex_entity_type obj_type)
565 {
566 /* Thread-safe and reentrant */
567 switch (obj_type) {
568 case EX_ASSEMBLY: return "assembly";
569 case EX_BLOB: return "blob";
570 case EX_COORDINATE: /* kluge so some wrapper functions work */ return "coordinate";
571 case EX_NODAL: return "nodal";
572 case EX_EDGE_BLOCK: return "edge block";
573 case EX_FACE_BLOCK: return "face block";
574 case EX_ELEM_BLOCK: return "element block";
575 case EX_NODE_SET: return "node set";
576 case EX_EDGE_SET: return "edge set";
577 case EX_FACE_SET: return "face set";
578 case EX_SIDE_SET: return "side set";
579 case EX_ELEM_SET: return "element set";
580 case EX_ELEM_MAP: return "element map";
581 case EX_NODE_MAP: return "node map";
582 case EX_EDGE_MAP: return "edge map";
583 case EX_FACE_MAP: return "face map";
584 case EX_GLOBAL: return "global";
585 default: return "invalid type";
586 }
587 }
588
589 /*!
590 \internal
591 \undoc
592 */
593 ex_entity_type ex_var_type_to_ex_entity_type(char var_type)
594 {
595 /* Thread-safe and reentrant */
596 char var_lower = tolower(var_type);
597 if (var_lower == 'n') {
598 return EX_NODAL;
599 }
600 if (var_lower == 'l') {
601 return EX_EDGE_BLOCK;
602 }
603 if (var_lower == 'f') {
604 return EX_FACE_BLOCK;
605 }
606 if (var_lower == 'e') {
607 return EX_ELEM_BLOCK;
608 }
609 if (var_lower == 'm') {
610 return EX_NODE_SET;
611 }
612 else if (var_lower == 'd') {
613 return EX_EDGE_SET;
614 }
615 else if (var_lower == 'a') {
616 return EX_FACE_SET;
617 }
618 else if (var_lower == 's') {
619 return EX_SIDE_SET;
620 }
621 else if (var_lower == 't') {
622 return EX_ELEM_SET;
623 }
624 else if (var_lower == 'g') {
625 return EX_GLOBAL;
626 }
627 else {
628 return EX_INVALID;
629 }
630 }
631
632 /*!
633 \internal
634 \undoc
635 */
636 char *exi_dim_num_objects(ex_entity_type obj_type)
637 {
638 switch (obj_type) {
639 case EX_NODAL: return DIM_NUM_NODES;
640 case EX_ASSEMBLY: return DIM_NUM_ASSEMBLY;
641 case EX_BLOB: return DIM_NUM_BLOB;
642 case EX_ELEM_BLOCK: return DIM_NUM_EL_BLK;
643 case EX_EDGE_BLOCK: return DIM_NUM_ED_BLK;
644 case EX_FACE_BLOCK: return DIM_NUM_FA_BLK;
645 case EX_NODE_SET: return DIM_NUM_NS;
646 case EX_EDGE_SET: return DIM_NUM_ES;
647 case EX_FACE_SET: return DIM_NUM_FS;
648 case EX_ELEM_SET: return DIM_NUM_ELS;
649 case EX_SIDE_SET: return DIM_NUM_SS;
650 case EX_ELEM_MAP: return DIM_NUM_EM;
651 case EX_FACE_MAP: return DIM_NUM_FAM;
652 case EX_EDGE_MAP: return DIM_NUM_EDM;
653 case EX_NODE_MAP: return DIM_NUM_NM;
654 default: {
655 char errmsg[MAX_ERR_LENGTH];
656 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s", obj_type,
657 __func__);
658 ex_err(__func__, errmsg, EX_BADPARAM);
659 return NULL;
660 }
661 }
662 }
663
664 /*!
665 \internal
666 \undoc
667 */
668 char *exi_dim_num_entries_in_object(ex_entity_type obj_type, int idx)
669 {
670 switch (obj_type) {
671 case EX_NODAL: return DIM_NUM_NODES;
672 case EX_ASSEMBLY: return DIM_NUM_ENTITY_ASSEMBLY(idx);
673 case EX_BLOB: return DIM_NUM_VALUES_BLOB(idx);
674 case EX_EDGE_BLOCK: return DIM_NUM_ED_IN_EBLK(idx);
675 case EX_FACE_BLOCK: return DIM_NUM_FA_IN_FBLK(idx);
676 case EX_ELEM_BLOCK: return DIM_NUM_EL_IN_BLK(idx);
677 case EX_NODE_SET: return DIM_NUM_NOD_NS(idx);
678 case EX_EDGE_SET: return DIM_NUM_EDGE_ES(idx);
679 case EX_FACE_SET: return DIM_NUM_FACE_FS(idx);
680 case EX_SIDE_SET: return DIM_NUM_SIDE_SS(idx);
681 case EX_ELEM_SET: return DIM_NUM_ELE_ELS(idx);
682 default: {
683 char errmsg[MAX_ERR_LENGTH];
684 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s", obj_type,
685 __func__);
686 ex_err(__func__, errmsg, EX_BADPARAM);
687 return NULL;
688 }
689 }
690 }
691
692 /*!
693 \internal
694 \undoc
695 */
696 char *exi_name_var_of_object(ex_entity_type obj_type, int i, int j)
697 {
698 switch (obj_type) {
699 case EX_ASSEMBLY: return VAR_ASSEMBLY_VAR(i, j);
700 case EX_BLOB: return VAR_BLOB_VAR(i, j);
701 case EX_EDGE_BLOCK: return VAR_EDGE_VAR(i, j);
702 case EX_FACE_BLOCK: return VAR_FACE_VAR(i, j);
703 case EX_ELEM_BLOCK: return VAR_ELEM_VAR(i, j);
704 case EX_NODE_SET: return VAR_NS_VAR(i, j);
705 case EX_EDGE_SET: return VAR_ES_VAR(i, j);
706 case EX_FACE_SET: return VAR_FS_VAR(i, j);
707 case EX_SIDE_SET: return VAR_SS_VAR(i, j);
708 case EX_ELEM_SET: return VAR_ELS_VAR(i, j);
709 default: {
710 char errmsg[MAX_ERR_LENGTH];
711 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s", obj_type,
712 __func__);
713 ex_err(__func__, errmsg, EX_BADPARAM);
714 return NULL;
715 }
716 }
717 }
718
719 /*!
720 \internal
721 \undoc
722 */
723 char *exi_name_red_var_of_object(ex_entity_type obj_type, int id)
724 {
725 switch (obj_type) {
726 case EX_ASSEMBLY: return VAR_ASSEMBLY_RED_VAR(id);
727 case EX_BLOB: return VAR_BLOB_RED_VAR(id);
728 case EX_EDGE_BLOCK: return VAR_EDGE_RED_VAR(id);
729 case EX_FACE_BLOCK: return VAR_FACE_RED_VAR(id);
730 case EX_ELEM_BLOCK: return VAR_ELEM_RED_VAR(id);
731 case EX_NODE_SET: return VAR_NS_RED_VAR(id);
732 case EX_EDGE_SET: return VAR_ES_RED_VAR(id);
733 case EX_FACE_SET: return VAR_FS_RED_VAR(id);
734 case EX_SIDE_SET: return VAR_SS_RED_VAR(id);
735 case EX_ELEM_SET: return VAR_ELS_RED_VAR(id);
736 default: {
737 char errmsg[MAX_ERR_LENGTH];
738 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s", obj_type,
739 __func__);
740 ex_err(__func__, errmsg, EX_BADPARAM);
741 return NULL;
742 }
743 }
744 }
745
746 /*!
747 \internal
748 \undoc
749 */
750 char *exi_name_of_map(ex_entity_type map_type, int map_index)
751 {
752 switch (map_type) {
753 case EX_NODE_MAP: return VAR_NODE_MAP(map_index);
754 case EX_EDGE_MAP: return VAR_EDGE_MAP(map_index);
755 case EX_FACE_MAP: return VAR_FACE_MAP(map_index);
756 case EX_ELEM_MAP: return VAR_ELEM_MAP(map_index);
757 default: return NULL;
758 }
759 }
760
761 /*****************************************************************************
762 *
763 * exi_id_lkup - look up id
764 *
765 * entry conditions -
766 * input parameters:
767 * int exoid exodus file id
768 * ex_entity_type id_type id type name:
769 * elem_ss
770 * node_ns
771 2* side_ss
772 * int num id value
773 *
774 * exit conditions -
775 * int return index into table (1-based)
776 *
777 *****************************************************************************/
778
779 /*!
780 \internal
781 \undoc
782 */
783 int exi_id_lkup(int exoid, ex_entity_type id_type, ex_entity_id num)
784 {
785 char *id_table = NULL;
786 char *id_dim = NULL;
787 char *stat_table = NULL;
788 int varid, dimid;
789 size_t dim_len;
790 int64_t i;
791 int64_t *id_vals = NULL;
792 int *stat_vals = NULL;
793
794 static bool sequential = false;
795 struct exi_obj_stats *tmp_stats;
796 int status;
797 char errmsg[MAX_ERR_LENGTH];
798
799 switch (id_type) {
800 case EX_NODAL: return 0;
801 case EX_GLOBAL: return 0;
802 case EX_ASSEMBLY: return num;
803 case EX_BLOB: return num;
804 case EX_ELEM_BLOCK:
805 id_table = VAR_ID_EL_BLK; /* id array name */
806 id_dim = DIM_NUM_EL_BLK; /* id array dimension name*/
807 stat_table = VAR_STAT_EL_BLK; /* id status array name */
808 tmp_stats = exi_get_stat_ptr(exoid, &exoII_eb);
809 break;
810 case EX_NODE_SET:
811 id_table = VAR_NS_IDS;
812 id_dim = DIM_NUM_NS;
813 stat_table = VAR_NS_STAT;
814 tmp_stats = exi_get_stat_ptr(exoid, &exoII_ns);
815 break;
816 case EX_SIDE_SET:
817 id_table = VAR_SS_IDS;
818 id_dim = DIM_NUM_SS;
819 stat_table = VAR_SS_STAT;
820 tmp_stats = exi_get_stat_ptr(exoid, &exoII_ss);
821 break;
822 case EX_EDGE_BLOCK:
823 id_table = VAR_ID_ED_BLK;
824 id_dim = DIM_NUM_ED_BLK;
825 stat_table = VAR_STAT_ED_BLK;
826 tmp_stats = exi_get_stat_ptr(exoid, &exoII_ed);
827 break;
828 case EX_FACE_BLOCK:
829 id_table = VAR_ID_FA_BLK;
830 id_dim = DIM_NUM_FA_BLK;
831 stat_table = VAR_STAT_FA_BLK;
832 tmp_stats = exi_get_stat_ptr(exoid, &exoII_fa);
833 break;
834 case EX_EDGE_SET:
835 id_table = VAR_ES_IDS;
836 id_dim = DIM_NUM_ES;
837 stat_table = VAR_ES_STAT;
838 tmp_stats = exi_get_stat_ptr(exoid, &exoII_es);
839 break;
840 case EX_FACE_SET:
841 id_table = VAR_FS_IDS;
842 id_dim = DIM_NUM_FS;
843 stat_table = VAR_FS_STAT;
844 tmp_stats = exi_get_stat_ptr(exoid, &exoII_fs);
845 break;
846 case EX_ELEM_SET:
847 id_table = VAR_ELS_IDS;
848 id_dim = DIM_NUM_ELS;
849 stat_table = VAR_ELS_STAT;
850 tmp_stats = exi_get_stat_ptr(exoid, &exoII_els);
851 break;
852 case EX_NODE_MAP:
853 id_table = VAR_NM_PROP(1);
854 id_dim = DIM_NUM_NM;
855 stat_table = "";
856 tmp_stats = exi_get_stat_ptr(exoid, &exoII_nm);
857 break;
858 case EX_EDGE_MAP:
859 id_table = VAR_EDM_PROP(1);
860 id_dim = DIM_NUM_EDM;
861 stat_table = "";
862 tmp_stats = exi_get_stat_ptr(exoid, &exoII_edm);
863 break;
864 case EX_FACE_MAP:
865 id_table = VAR_FAM_PROP(1);
866 id_dim = DIM_NUM_FAM;
867 stat_table = "";
868 tmp_stats = exi_get_stat_ptr(exoid, &exoII_fam);
869 break;
870 case EX_ELEM_MAP:
871 id_table = VAR_EM_PROP(1);
872 id_dim = DIM_NUM_EM;
873 stat_table = "";
874 tmp_stats = exi_get_stat_ptr(exoid, &exoII_em);
875 break;
876 default:
877 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unsupported id array type %d for file id %d", id_type,
878 exoid);
879 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
880 return EX_FATAL;
881 }
882
883 if ((tmp_stats->id_vals == NULL) || (!(tmp_stats->valid_ids))) {
884
885 /* first time through or id arrays haven't been completely filled yet */
886
887 /* get size of id array */
888
889 /* First get dimension id of id array */
890 if ((status = nc_inq_dimid(exoid, id_dim, &dimid)) != NC_NOERR) {
891 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate id array dimension in file id %d",
892 exoid);
893 ex_err_fn(exoid, __func__, errmsg, status);
894 return EX_FATAL;
895 }
896
897 /* Next get value of dimension */
898 if ((status = nc_inq_dimlen(exoid, dimid, &dim_len)) != NC_NOERR) {
899 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s array length in file id %d",
900 id_table, exoid);
901 ex_err_fn(exoid, __func__, errmsg, status);
902 return EX_FATAL;
903 }
904
905 /* get variable id of id array */
906 if ((status = nc_inq_varid(exoid, id_table, &varid)) != NC_NOERR) {
907 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s array in file id %d", id_table,
908 exoid);
909 ex_err_fn(exoid, __func__, errmsg, status);
910 return EX_FATAL;
911 }
912
913 /* allocate space for id array and initialize to zero to ensure
914 that the higher bits don't contain garbage while copy from ints */
915 if (!(id_vals = calloc(dim_len, sizeof(int64_t)))) {
916 snprintf(errmsg, MAX_ERR_LENGTH,
917 "ERROR: failed to allocate memory for %s array for file id %d", id_table, exoid);
918 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
919 return EX_FATAL;
920 }
921
922 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
923 status = nc_get_var_longlong(exoid, varid, (long long *)id_vals);
924 }
925 else {
926 int *id_vals_int;
927 if (!(id_vals_int = malloc(dim_len * sizeof(int)))) {
928 snprintf(errmsg, MAX_ERR_LENGTH,
929 "ERROR: failed to allocate memory for temporary array "
930 "id_vals_int for file id %d",
931 exoid);
932 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
933 free(id_vals);
934 return EX_FATAL;
935 }
936 status = nc_get_var_int(exoid, varid, id_vals_int);
937 if (status == NC_NOERR) {
938 for (size_t iii = 0; iii < dim_len; iii++) {
939 id_vals[iii] = (int64_t)id_vals_int[iii];
940 }
941 }
942 free(id_vals_int);
943 }
944
945 if (status != NC_NOERR) {
946 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s array from file id %d", id_table,
947 exoid);
948 ex_err_fn(exoid, __func__, errmsg, status);
949 free(id_vals);
950 return EX_FATAL;
951 }
952
953 /* check if values in stored arrays are filled with non-zeroes */
954 bool filled = true;
955 sequential = true;
956 for (size_t iii = 0; iii < dim_len; iii++) {
957 if (id_vals[iii] != (int64_t)iii + 1) {
958 sequential = false;
959 }
960 if (id_vals[iii] == EX_INVALID_ID || id_vals[iii] == NC_FILL_INT) {
961 filled = false;
962 sequential = false;
963 break; /* id array hasn't been completely filled with valid ids yet */
964 }
965 }
966
967 if (filled) {
968 tmp_stats->valid_ids = true;
969 tmp_stats->sequential = sequential;
970 tmp_stats->num = dim_len;
971 tmp_stats->id_vals = id_vals;
972 }
973 }
974 else {
975 id_vals = tmp_stats->id_vals;
976 dim_len = tmp_stats->num;
977 sequential = tmp_stats->sequential;
978 }
979
980 if (sequential && (size_t)num < dim_len) {
981 i = num - 1;
982 }
983 else {
984 /* Do a linear search through the id array to find the array value
985 corresponding to the passed index number */
986 for (i = 0; i < (int64_t)dim_len; i++) {
987 if (id_vals[i] == num) {
988 break; /* found the id requested */
989 }
990 }
991 }
992 if (i >= (int64_t)dim_len) /* failed to find id number */
993 {
994 if (!(tmp_stats->valid_ids)) {
995 free(id_vals);
996 }
997 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate id %" PRId64 " for file id %d", num,
998 exoid);
999 ex_set_err(__func__, errmsg, EX_LOOKUPFAIL);
1000 return -EX_LOOKUPFAIL; /*if we got here, the id array value doesn't exist */
1001 }
1002
1003 /* Now check status array to see if object is null */
1004 if ((tmp_stats->stat_vals == NULL) || (!(tmp_stats->valid_stat))) {
1005
1006 /* allocate space for new status array */
1007 if (!(stat_vals = malloc(dim_len * sizeof(int)))) {
1008 free(id_vals);
1009 snprintf(errmsg, MAX_ERR_LENGTH,
1010 "ERROR: failed to allocate memory for %s array for file id %d", id_table, exoid);
1011 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
1012 return EX_FATAL;
1013 }
1014
1015 /* first time through or status arrays haven't been filled yet */
1016 if (nc_inq_varid(exoid, stat_table, &varid) == NC_NOERR) {
1017 /* get variable id of status array */
1018 /* if status array exists, use it, otherwise assume object exists
1019 to be backward compatible */
1020
1021 if ((status = nc_get_var_int(exoid, varid, stat_vals)) != NC_NOERR) {
1022 free(id_vals);
1023 free(stat_vals);
1024 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s array from file id %d",
1025 stat_table, exoid);
1026 ex_err_fn(exoid, __func__, errmsg, status);
1027 return EX_FATAL;
1028 }
1029 }
1030 else {
1031 for (size_t j = 0; j < dim_len; j++) {
1032 stat_vals[j] = 1;
1033 }
1034 }
1035
1036 if (tmp_stats->valid_ids) {
1037 /* status array is valid only if ids are valid */
1038 tmp_stats->valid_stat = true;
1039 tmp_stats->stat_vals = stat_vals;
1040 }
1041 }
1042 else {
1043 stat_vals = tmp_stats->stat_vals;
1044 }
1045
1046 if (!(tmp_stats->valid_ids)) {
1047 free(id_vals);
1048 }
1049
1050 if (stat_vals[i] == 0) /* is this object null? */ {
1051 ex_err_fn(exoid, __func__, "", EX_NULLENTITY);
1052 if (!(tmp_stats->valid_stat)) {
1053 free(stat_vals);
1054 }
1055 return -((int)i + 1); /* return index into id array (1-based) */
1056 }
1057 if (!(tmp_stats->valid_stat)) {
1058 free(stat_vals);
1059 }
1060 return i + 1; /* return index into id array (1-based) */
1061 }
1062
1063 /******************************************************************************
1064 *
1065 * exi_get_stat_ptr - returns a pointer to a structure of object ids
1066 *
1067 *****************************************************************************/
1068
1069 /*! this routine returns a pointer to a structure containing the ids of
1070 * element blocks, node sets, or side sets according to exoid; if there
1071 * is not a structure that matches the exoid, one is created.
1072 *
1073 * NOTE: If this file contains `groups` or `change sets`, then each
1074 * group will have its own stat_ptr set of lists...
1075 * \internal
1076 */
1077
1078 struct exi_obj_stats *exi_get_stat_ptr(int exoid, struct exi_obj_stats **obj_ptr)
1079 {
1080 struct exi_obj_stats *tmp_ptr = *obj_ptr;
1081
1082 while (tmp_ptr) {
1083 if ((tmp_ptr)->exoid == exoid) {
1084 break;
1085 }
1086 tmp_ptr = (tmp_ptr)->next;
1087 }
1088
1089 if (!tmp_ptr) { /* exoid not found */
1090 tmp_ptr = (struct exi_obj_stats *)calloc(1, sizeof(struct exi_obj_stats));
1091 tmp_ptr->exoid = exoid;
1092 tmp_ptr->next = *obj_ptr;
1093 tmp_ptr->id_vals = 0;
1094 tmp_ptr->stat_vals = 0;
1095 tmp_ptr->num = 0;
1096 tmp_ptr->valid_ids = 0;
1097 tmp_ptr->valid_stat = 0;
1098 *obj_ptr = tmp_ptr;
1099 }
1100 return tmp_ptr;
1101 }
1102
1103 /******************************************************************************
1104 *
1105 * exi_rm_stat_ptr - removes a pointer to a structure of object ids
1106 *
1107 *****************************************************************************/
1108
1109 /*! this routine removes a pointer to a structure containing the ids of
1110 * element blocks, node sets, or side sets according to exoid; this
1111 * is necessary to clean up because netCDF reuses file ids; should be
1112 * called from ex_close
1113 *
1114 * NOTE: If this file contains `groups` or `change sets`, then each
1115 * group will have its own stat_ptr set of lists. However,
1116 * this routine is called from ex_close which only closes the
1117 * root id, so we need to iterate the entire list to see if there
1118 * are any subgroups of the root group.
1119 * \internal
1120 */
1121
1122 void exi_rm_stat_ptr(int exoid, struct exi_obj_stats **obj_ptr)
1123 {
1124 struct exi_obj_stats *tmp_ptr = *obj_ptr;
1125 struct exi_obj_stats *last_head_list_ptr = *obj_ptr; /* save last head pointer */
1126
1127 int root_id = exoid & EX_FILE_ID_MASK;
1128 while (tmp_ptr) { /* Walk linked list of file ids/vals */
1129 if (root_id == (tmp_ptr->exoid & EX_FILE_ID_MASK)) { /* linear search for exodus file id */
1130 if (tmp_ptr == *obj_ptr) { /* Are we at the head of the list? */
1131 *obj_ptr = (*obj_ptr)->next; /* yes, reset ptr to head of list */
1132 }
1133 else { /* no, remove this record from chain*/
1134 last_head_list_ptr->next = tmp_ptr->next;
1135 }
1136 struct exi_obj_stats *tmp = tmp_ptr;
1137 tmp_ptr = tmp_ptr->next;
1138 free(tmp->id_vals); /* free up memory */
1139 free(tmp->stat_vals);
1140 free(tmp);
1141 }
1142 else {
1143 last_head_list_ptr = tmp_ptr; /* save last head pointer */
1144 tmp_ptr = tmp_ptr->next; /* Loop back if not */
1145 }
1146 }
1147 }
1148
1149 /* structures to hold number of blocks of that type for each file id */
1150 static struct exi_list_item *ed_ctr_list = NULL; /* edge blocks */
1151 static struct exi_list_item *fa_ctr_list = NULL; /* face blocks */
1152 static struct exi_list_item *eb_ctr_list = NULL; /* element blocks */
1153
1154 /* structures to hold number of sets of that type for each file id */
1155 static struct exi_list_item *ns_ctr_list = NULL; /* node sets */
1156 static struct exi_list_item *es_ctr_list = NULL; /* edge sets */
1157 static struct exi_list_item *fs_ctr_list = NULL; /* face sets */
1158 static struct exi_list_item *ss_ctr_list = NULL; /* side sets */
1159 static struct exi_list_item *els_ctr_list = NULL; /* element sets */
1160
1161 /* structures to hold number of blobs/assemblies for each file id */
1162 static struct exi_list_item *assm_ctr_list = NULL; /* assemblies */
1163 static struct exi_list_item *blob_ctr_list = NULL; /* blobs */
1164
1165 /* structures to hold number of maps of that type for each file id */
1166 static struct exi_list_item *nm_ctr_list = NULL; /* node maps */
1167 static struct exi_list_item *edm_ctr_list = NULL; /* edge maps */
1168 static struct exi_list_item *fam_ctr_list = NULL; /* face maps */
1169 static struct exi_list_item *em_ctr_list = NULL; /* element maps */
1170
1171 /*!
1172 \internal
1173 \undoc
1174 */
1175 struct exi_list_item **exi_get_counter_list(ex_entity_type obj_type)
1176 {
1177 /* Thread-safe, but is dealing with globals */
1178 /* Only called from a routine which will be using locks */
1179 switch (obj_type) {
1180 case EX_ASSEMBLY: return &assm_ctr_list;
1181 case EX_BLOB: return &blob_ctr_list;
1182 case EX_ELEM_BLOCK: return &eb_ctr_list;
1183 case EX_NODE_SET: return &ns_ctr_list;
1184 case EX_SIDE_SET: return &ss_ctr_list;
1185 case EX_ELEM_MAP: return &em_ctr_list;
1186 case EX_NODE_MAP: return &nm_ctr_list;
1187 case EX_EDGE_BLOCK: return &ed_ctr_list;
1188 case EX_FACE_BLOCK: return &fa_ctr_list;
1189 case EX_EDGE_SET: return &es_ctr_list;
1190 case EX_FACE_SET: return &fs_ctr_list;
1191 case EX_ELEM_SET: return &els_ctr_list;
1192 case EX_EDGE_MAP: return &edm_ctr_list;
1193 case EX_FACE_MAP: return &fam_ctr_list;
1194 default: return NULL;
1195 }
1196 }
1197
1198 /******************************************************************************
1199 *
1200 * exi_inc_file_item - increment file item
1201 *
1202 *****************************************************************************/
1203
1204 /*! this routine sets up a structure to track and increment a counter for
1205 * each open exodus file. it is designed to be used by the routines
1206 * ex_put_elem_block() and ex_put_set_param(),
1207 * to keep track of the number of element blocks, and each type of set,
1208 * respectively, for each open exodus II file.
1209 *
1210 * The list structure is used as follows:
1211 *
1212 * ptr -----------> list item structure
1213 * -------------------
1214 * exodus file id
1215 * item value (int)
1216 * ptr to next (NULL if last)
1217 *
1218 *
1219 * NOTE: since netCDF reuses its file ids, and a user may open and close any
1220 * number of files in one application, items must be taken out of the
1221 * linked lists in each of the above routines. these should be called
1222 * after ncclose().
1223 * \internal
1224 */
1225
1226 /* NOTE: If this is done for a file which contains `groups` or `change sets`, then
1227 * the exoid refers to the specific group id and not the `root_id`...
1228 */
1229 int exi_inc_file_item(int exoid, /* file id */
1230 struct exi_list_item **list_ptr) /* ptr to ptr to list_item */
1231 {
1232 struct exi_list_item *tlist_ptr = *list_ptr; /* use temp list ptr to walk linked list */
1233 while (tlist_ptr) { /* Walk linked list of file ids/vals */
1234 if (exoid == tlist_ptr->exo_id) { /* linear search for exodus file id */
1235 break; /* Quit if found */
1236 }
1237 tlist_ptr = tlist_ptr->next; /* Loop back if not */
1238 }
1239
1240 if (!tlist_ptr) { /* ptr NULL? */
1241 /* allocate space for new structure record */
1242 tlist_ptr = (struct exi_list_item *)calloc(1, sizeof(struct exi_list_item));
1243 tlist_ptr->exo_id = exoid; /* insert file id */
1244 tlist_ptr->next = *list_ptr; /* insert into head of list */
1245 *list_ptr = tlist_ptr; /* fix up new head of list */
1246 }
1247 return tlist_ptr->value++;
1248 }
1249
1250 /*****************************************************************************
1251 *
1252 * exi_get_file_item - return count
1253 *
1254 *****************************************************************************/
1255
1256 /*! this routine accesses a structure to track and increment a counter for
1257 * each open exodus file. it is designed to be used by the routines
1258 * ex_put_elem_block(), and ex_put_set_param(),
1259 * to get the number of element blocks, or a type of set,
1260 * respectively, for an open exodus II file.
1261 *
1262 * The list structure is used as follows:
1263 *
1264 * ptr -----------> list item structure
1265 * -------------------
1266 * exodus file id
1267 * item value (int)
1268 * ptr to next (NULL if last)
1269 *
1270 *
1271 * NOTE: since netCDF reuses its file ids, and a user may open and close any
1272 * number of files in one application, items must be taken out of the
1273 * linked lists in each of the above routines. these should be called
1274 * after nc_close().
1275 * \internal
1276 */
1277
1278 int exi_get_file_item(int exoid, /* file id */
1279 struct exi_list_item **list_ptr) /* ptr to ptr to list_item */
1280 {
1281 /* Not thread-safe: list_ptr passed in is a global
1282 * Would probably work ok with multiple threads since read-only,
1283 * but possible that list_ptr will be modified while being used
1284 */
1285 struct exi_list_item *tlist_ptr = *list_ptr; /* use temp list ptr to walk linked list */
1286 while (tlist_ptr) { /* Walk linked list of file ids/vals */
1287 if (exoid == tlist_ptr->exo_id) { /* linear search for exodus file id */
1288 break; /* Quit if found */
1289 }
1290 tlist_ptr = tlist_ptr->next; /* Loop back if not */
1291 }
1292
1293 if (!tlist_ptr) { /* ptr NULL? */
1294 return -1;
1295 }
1296
1297 return tlist_ptr->value;
1298 }
1299
1300 /*****************************************************************************
1301 *
1302 * exi_rm_file_item - remove file item
1303 *
1304 *****************************************************************************/
1305
1306 /*! this routine removes a structure to track and increment a counter for
1307 * each open exodus file.
1308 *
1309 * The list structure is used as follows:
1310 *
1311 * ptr -----------> list item structure
1312 * -------------------
1313 * exodus file id
1314 * item value (int)
1315 * ptr to next (NULL if last)
1316 *
1317 *
1318 * NOTE: since netCDF reuses its file ids, and a user may open and close any
1319 * number of files in one application, items must be taken out of the
1320 * linked lists in each of the above routines. these should be called
1321 * after ncclose().
1322 *
1323 * NOTE: this is called from `ex_close` which only closes the root file and
1324 * not each group or change set within that `root` file. If the file
1325 * contains groups, then we need to check the entire list...
1326 * \internal
1327 */
1328
1329 void exi_rm_file_item(int exoid, /* file id */
1330 struct exi_list_item **list_ptr) /* ptr to ptr to list_item */
1331
1332 {
1333 struct exi_list_item *last_head_list_ptr = *list_ptr; /* save last head pointer */
1334
1335 int root_id = exoid & EX_FILE_ID_MASK;
1336
1337 struct exi_list_item *tlist_ptr = *list_ptr;
1338 while (tlist_ptr) { /* Walk linked list of file ids/vals */
1339 if (root_id == (tlist_ptr->exo_id & EX_FILE_ID_MASK)) { /* linear search for exodus file id */
1340 if (tlist_ptr == *list_ptr) { /* Are we at the head of the list? */
1341 *list_ptr = (*list_ptr)->next; /* yes, reset ptr to head of list */
1342 }
1343 else { /* no, remove this record from chain*/
1344 last_head_list_ptr->next = tlist_ptr->next;
1345 }
1346 struct exi_list_item *temp = tlist_ptr;
1347 tlist_ptr = tlist_ptr->next;
1348 free(temp); /* free up memory */
1349 }
1350 else {
1351 last_head_list_ptr = tlist_ptr; /* save last head pointer */
1352 tlist_ptr = tlist_ptr->next; /* Loop back if not */
1353 }
1354 }
1355 }
1356
1357 /*!
1358 \ingroup Utilities
1359 \undoc
1360 */
1361 int ex_get_num_props(int exoid, ex_entity_type obj_type)
1362 {
1363 int cntr, varid;
1364 char *var_name;
1365 char errmsg[MAX_ERR_LENGTH];
1366
1367 EX_FUNC_ENTER();
1368 cntr = 0;
1369
1370 /* loop until there is not a property variable defined; the name of */
1371 /* the variables begin with an increment of 1 ("xx_prop1") so use cntr+1 */
1372 while (true) {
1373 switch (obj_type) {
1374 case EX_ELEM_BLOCK: var_name = VAR_EB_PROP(cntr + 1); break;
1375 case EX_EDGE_BLOCK: var_name = VAR_ED_PROP(cntr + 1); break;
1376 case EX_FACE_BLOCK: var_name = VAR_FA_PROP(cntr + 1); break;
1377 case EX_NODE_SET: var_name = VAR_NS_PROP(cntr + 1); break;
1378 case EX_EDGE_SET: var_name = VAR_ES_PROP(cntr + 1); break;
1379 case EX_FACE_SET: var_name = VAR_FS_PROP(cntr + 1); break;
1380 case EX_SIDE_SET: var_name = VAR_SS_PROP(cntr + 1); break;
1381 case EX_ELEM_SET: var_name = VAR_ELS_PROP(cntr + 1); break;
1382 case EX_ASSEMBLY: var_name = VAR_ASSEMBLY_PROP(cntr + 1); break;
1383 case EX_BLOB: var_name = VAR_BLOB_PROP(cntr + 1); break;
1384 case EX_ELEM_MAP: var_name = VAR_EM_PROP(cntr + 1); break;
1385 case EX_FACE_MAP: var_name = VAR_FAM_PROP(cntr + 1); break;
1386 case EX_EDGE_MAP: var_name = VAR_EDM_PROP(cntr + 1); break;
1387 case EX_NODE_MAP: var_name = VAR_NM_PROP(cntr + 1); break;
1388 default:
1389 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type,
1390 exoid);
1391 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
1392 EX_FUNC_LEAVE(EX_FATAL);
1393 }
1394
1395 if (nc_inq_varid(exoid, var_name, &varid) != NC_NOERR) {
1396 /* no variable with this name; return cntr which is now the number of */
1397 /* properties for this type of entity */
1398 EX_FUNC_LEAVE(cntr);
1399 }
1400 cntr++;
1401 }
1402 EX_FUNC_LEAVE(EX_FATAL);
1403 }
1404
1405 /*!
1406 \ingroup Utilities
1407 \undoc
1408 */
1409 int exi_get_cpu_ws(void) { return sizeof(float); }
1410
1411 /* swap - interchange v[i] and v[j] */
1412 /*!
1413 \internal
1414 \undoc
1415 */
1416 static void ex_swap(int v[], int64_t i, int64_t j)
1417 {
1418 /* Thread-safe, reentrant */
1419 int temp = v[i];
1420 v[i] = v[j];
1421 v[j] = temp;
1422 }
1423
1424 /*!
1425 \internal
1426 \undoc
1427 */
1428 static void ex_swap64(int64_t v[], int64_t i, int64_t j)
1429 {
1430 /* Thread-safe, reentrant */
1431 int64_t temp = v[i];
1432 v[i] = v[j];
1433 v[j] = temp;
1434 }
1435
1436 #define EX_QSORT_CUTOFF 12
1437
1438 /*!
1439 \internal
1440 \undoc
1441 */
1442 static int ex_int_median3(int v[], int iv[], int64_t left, int64_t right)
1443 {
1444 /* Thread-safe, reentrant */
1445 int64_t center = (left + right) / 2;
1446
1447 if (v[iv[left]] > v[iv[center]]) {
1448 ex_swap(iv, left, center);
1449 }
1450 if (v[iv[left]] > v[iv[right]]) {
1451 ex_swap(iv, left, right);
1452 }
1453 if (v[iv[center]] > v[iv[right]]) {
1454 ex_swap(iv, center, right);
1455 }
1456
1457 ex_swap(iv, center, right - 1);
1458 return iv[right - 1];
1459 }
1460
1461 /*!
1462 \internal
1463 \undoc
1464 */
1465 static int64_t ex_int_median3_64(int64_t v[], int64_t iv[], int64_t left, int64_t right)
1466 {
1467 /* Thread-safe, reentrant */
1468 int64_t center = (left + right) / 2;
1469
1470 if (v[iv[left]] > v[iv[center]]) {
1471 ex_swap64(iv, left, center);
1472 }
1473 if (v[iv[left]] > v[iv[right]]) {
1474 ex_swap64(iv, left, right);
1475 }
1476 if (v[iv[center]] > v[iv[right]]) {
1477 ex_swap64(iv, center, right);
1478 }
1479
1480 ex_swap64(iv, center, right - 1);
1481 return iv[right - 1];
1482 }
1483
1484 /*!
1485 \internal
1486 \undoc
1487 */
1488 static void ex_int_iqsort(int v[], int iv[], int left, int right)
1489 {
1490 /* Thread-safe, reentrant */
1491 if (left + EX_QSORT_CUTOFF <= right) {
1492 int pivot = ex_int_median3(v, iv, left, right);
1493 int i = left;
1494 int j = right - 1;
1495
1496 for (;;) {
1497 while (v[iv[++i]] < v[pivot]) {
1498 ;
1499 }
1500 while (v[iv[--j]] > v[pivot]) {
1501 ;
1502 }
1503 if (i < j) {
1504 ex_swap(iv, i, j);
1505 }
1506 else {
1507 break;
1508 }
1509 }
1510
1511 ex_swap(iv, i, right - 1);
1512 ex_int_iqsort(v, iv, left, i - 1);
1513 ex_int_iqsort(v, iv, i + 1, right);
1514 }
1515 }
1516
1517 /*!
1518 \internal
1519 \undoc
1520 */
1521 static void ex_int_iqsort64(int64_t v[], int64_t iv[], int64_t left, int64_t right)
1522 {
1523 /* Thread-safe, reentrant */
1524 if (left + EX_QSORT_CUTOFF <= right) {
1525 int64_t pivot = ex_int_median3_64(v, iv, left, right);
1526 int64_t i = left;
1527 int64_t j = right - 1;
1528
1529 for (;;) {
1530 while (v[iv[++i]] < v[pivot]) {
1531 ;
1532 }
1533 while (v[iv[--j]] > v[pivot]) {
1534 ;
1535 }
1536 if (i < j) {
1537 ex_swap64(iv, i, j);
1538 }
1539 else {
1540 break;
1541 }
1542 }
1543
1544 ex_swap64(iv, i, right - 1);
1545 ex_int_iqsort64(v, iv, left, i - 1);
1546 ex_int_iqsort64(v, iv, i + 1, right);
1547 }
1548 }
1549
1550 /*!
1551 \internal
1552 \undoc
1553 */
1554 static void ex_int_iisort(int v[], int iv[], int N)
1555 {
1556 /* Thread-safe, reentrant */
1557 int ndx = 0;
1558 int small = v[iv[0]];
1559 for (int i = 1; i < N; i++) {
1560 if (v[iv[i]] < small) {
1561 small = v[iv[i]];
1562 ndx = i;
1563 }
1564 }
1565 /* Put smallest value in slot 0 */
1566 ex_swap(iv, 0, ndx);
1567
1568 for (int i = 1; i < N; i++) {
1569 int tmp = iv[i];
1570 int j;
1571 for (j = i; v[tmp] < v[iv[j - 1]]; j--) {
1572 iv[j] = iv[j - 1];
1573 }
1574 iv[j] = tmp;
1575 }
1576 }
1577
1578 /*!
1579 \internal
1580 \undoc
1581 */
1582 static void ex_int_iisort64(int64_t v[], int64_t iv[], int64_t N)
1583 {
1584 /* Thread-safe, reentrant */
1585 int64_t ndx = 0;
1586 int64_t small = v[iv[0]];
1587 for (int64_t i = 1; i < N; i++) {
1588 if (v[iv[i]] < small) {
1589 small = v[iv[i]];
1590 ndx = i;
1591 }
1592 }
1593 /* Put smallest value in slot 0 */
1594 ex_swap64(iv, 0, ndx);
1595
1596 for (int64_t i = 1; i < N; i++) {
1597 int64_t tmp = iv[i];
1598 int64_t j;
1599 for (j = i; v[tmp] < v[iv[j - 1]]; j--) {
1600 iv[j] = iv[j - 1];
1601 }
1602 iv[j] = tmp;
1603 }
1604 }
1605
1606 /*!
1607 * \ingroup Utilities
1608 * \internal
1609 * The following 'indexed qsort' routine is modified from Sedgewicks
1610 * algorithm It selects the pivot based on the median of the left,
1611 * right, and center values to try to avoid degenerate cases occurring
1612 * when a single value is chosen. It performs a quicksort on
1613 * intervals down to the #EX_QSORT_CUTOFF size and then performs a final
1614 * insertion sort on the almost sorted final array. Based on data in
1615 * Sedgewick, the #EX_QSORT_CUTOFF value should be between 5 and 20.
1616 *
1617 * See Sedgewick for further details
1618 * Define `DEBUG_QSORT` at the top of this file and recompile to compile
1619 * in code that verifies that the array is sorted.
1620 *
1621 * NOTE: The 'int' implementation below assumes that *both* the items
1622 * being sorted and the *number* of items being sorted are both
1623 * representable as 'int'.
1624 * \internal
1625 */
1626 void exi_iqsort(int v[], int iv[], size_t N)
1627 {
1628 /* Thread-safe, reentrant */
1629 ex_int_iqsort(v, iv, 0, N - 1);
1630 ex_int_iisort(v, iv, N);
1631
1632 #if defined(DEBUG_QSORT)
1633 fprintf(stderr, "Checking sort of %zu values\n", N + 1);
1634 int i;
1635 for (i = 1; i < N; i++) {
1636 assert(v[iv[i - 1]] <= v[iv[i]]);
1637 }
1638 #endif
1639 }
1640
1641 /*! \sa exi_iqsort() */
1642 void exi_iqsort64(int64_t v[], int64_t iv[], int64_t N)
1643 {
1644 /* Thread-safe, reentrant */
1645 ex_int_iqsort64(v, iv, 0, N - 1);
1646 ex_int_iisort64(v, iv, N);
1647
1648 #if defined(DEBUG_QSORT)
1649 fprintf(stderr, "Checking sort of %" PRId64 " values\n", N + 1);
1650 int i;
1651 for (i = 1; i < N; i++) {
1652 assert(v[iv[i - 1]] <= v[iv[i]]);
1653 }
1654 #endif
1655 }
1656
1657 /*!
1658 * Determine whether the new large model storage is being used in this
1659 * file, or old method. Basically, the difference is whether the
1660 * coordinates and nodal variables are stored in a blob (xyz
1661 * components together) or as a variable per component per
1662 * nodal_variable.
1663 *
1664 * \ingroup Utilities
1665 */
1666 int ex_large_model(int exoid)
1667 {
1668 if (exoid < 0) {
1669 return EXODUS_DEFAULT_SIZE; /* Specified in exodusII_int.h */
1670 }
1671
1672 /* See if the ATT_FILESIZE attribute is defined in the file */
1673 EX_FUNC_ENTER();
1674 int file_size = 0;
1675 int rootid = exoid & EX_FILE_ID_MASK;
1676 if (nc_get_att_int(rootid, NC_GLOBAL, ATT_FILESIZE, &file_size) != NC_NOERR) {
1677 /* Variable not found; default is 0 */
1678 file_size = 0;
1679 }
1680 EX_FUNC_LEAVE(file_size);
1681 }
1682
1683 /*!
1684 \internal
1685 \undoc
1686 */
1687 int exi_get_dimension(int exoid, const char *DIMENSION, const char *label, size_t *count,
1688 int *dimid, const char *routine)
1689 {
1690 int status;
1691
1692 *count = 0;
1693 *dimid = -1;
1694
1695 if ((status = nc_inq_dimid(exoid, DIMENSION, dimid)) != NC_NOERR) {
1696 if (routine != NULL) {
1697 if (status == NC_EBADDIM) {
1698 char errmsg[MAX_ERR_LENGTH];
1699 snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no dimension defining '%s' found in file id %d",
1700 label, exoid);
1701 ex_err_fn(exoid, __func__, errmsg, status);
1702 }
1703 else {
1704 char errmsg[MAX_ERR_LENGTH];
1705 snprintf(errmsg, MAX_ERR_LENGTH,
1706 "ERROR: failed to locate dimension defining number of '%s' in file id %d", label,
1707 exoid);
1708 ex_err_fn(exoid, __func__, errmsg, status);
1709 }
1710 }
1711 return status;
1712 }
1713
1714 if ((status = nc_inq_dimlen(exoid, *dimid, count)) != NC_NOERR) {
1715 if (routine != NULL) {
1716 char errmsg[MAX_ERR_LENGTH];
1717 snprintf(errmsg, MAX_ERR_LENGTH,
1718 "ERROR: failed to get length of dimension defining number of '%s' in file id %d",
1719 label, exoid);
1720 ex_err_fn(exoid, __func__, errmsg, status);
1721 }
1722 return status;
1723 }
1724 return status;
1725 }
1726
1727 /*!
1728 \deprecated
1729 */
1730 size_t ex_header_size(int exoid)
1731 {
1732 EX_UNUSED(exoid);
1733 return 0;
1734 }
1735
1736 void exi_set_compact_storage(int exoid, int varid)
1737 {
1738 /* Capability was released in version 4.7.4
1739 Only applicable to netcdf-4 files, but will
1740 succeed on other files; just won't do anything
1741 */
1742 #if defined(NC_COMPACT)
1743 nc_def_var_chunking(exoid, varid, NC_COMPACT, NULL);
1744 #else
1745 EX_UNUSED(exoid);
1746 EX_UNUSED(varid);
1747 #endif
1748 }
1749
1750 /*
1751 * type = 1 for integer, 2 for real, 3 for character
1752 * If type < 0, then don't compress, but do set collective on parallel
1753 */
1754
1755 /*!
1756 \internal
1757 \undoc
1758 */
1759
1760 void exi_compress_variable(int exoid, int varid, int type)
1761 {
1762 #if NC_HAS_HDF5
1763 struct exi_file_item *file = exi_find_file_item(exoid);
1764
1765 if (!file) {
1766 char errmsg[MAX_ERR_LENGTH];
1767 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for exi_compress_variable().",
1768 exoid);
1769 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
1770 }
1771 else {
1772 /* Compression only supported on HDF5 (NetCDF-4) files; Do not try to compress character data */
1773 int status = NC_NOERR;
1774 if ((type == 1 || type == 2) && file->is_hdf5) {
1775 if (file->compression_algorithm == EX_COMPRESS_GZIP) {
1776 int deflate_level = file->compression_level;
1777 if (deflate_level > 0) {
1778 int compress = 1;
1779 int shuffle = file->shuffle;
1780 status = nc_def_var_deflate(exoid, varid, shuffle, compress, deflate_level);
1781 }
1782 }
1783 else if (file->compression_algorithm == EX_COMPRESS_SZIP) {
1784 #if NC_HAS_SZIP_WRITE == 1
1785 /* See: https://support.hdfgroup.org/doc_resource/SZIP/ and
1786 https://support.hdfgroup.org/HDF5/doc/RM/RM_H5P.html#Property-SetSzip
1787 for details on SZIP library and parameters.
1788 */
1789
1790 #if !defined(NC_SZIP_EC)
1791 const int NC_SZIP_EC = 4; /* Selects entropy coding method for szip. */
1792 #endif
1793 #if !defined(NC_SZIP_NN)
1794 const int NC_SZIP_NN = 32; /* Selects nearest neighbor coding method for szip. */
1795 #endif
1796 /* Even and between 4 and 32; typical values are 8, 10, 16, 32 */
1797 const int SZIP_PIXELS_PER_BLOCK =
1798 file->compression_level == 0 ? 32 : file->compression_level;
1799 status = nc_def_var_szip(exoid, varid, NC_SZIP_NN, SZIP_PIXELS_PER_BLOCK);
1800 #else
1801 char errmsg[MAX_ERR_LENGTH];
1802 snprintf(errmsg, MAX_ERR_LENGTH,
1803 "ERROR: Compression algorithm SZIP is not supported yet (EXPERIMENTAL).");
1804 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
1805 #endif
1806 }
1807 else if (file->compression_algorithm == EX_COMPRESS_ZSTD) {
1808 #if NC_HAS_ZSTD == 1
1809 status = nc_def_var_zstandard(exoid, varid, file->compression_level);
1810 #else
1811 char errmsg[MAX_ERR_LENGTH];
1812 snprintf(errmsg, MAX_ERR_LENGTH,
1813 "ERROR: Compression algorithm ZSTANDARD is not supported in this version of the "
1814 "netCDF library.");
1815 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
1816 #endif
1817 }
1818 else if (file->compression_algorithm == EX_COMPRESS_BZ2) {
1819 #if NC_HAS_BZ2 == 1
1820 status = nc_def_var_bzip2(exoid, varid, file->compression_level);
1821 #else
1822 char errmsg[MAX_ERR_LENGTH];
1823 snprintf(errmsg, MAX_ERR_LENGTH,
1824 "ERROR: Compression algorithm BZIP2 / BZ2 is not supported in this version of the "
1825 "netCDF library.");
1826 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
1827 #endif
1828 }
1829 if (status != NC_NOERR) {
1830 char errmsg[MAX_ERR_LENGTH];
1831 snprintf(errmsg, MAX_ERR_LENGTH,
1832 "ERROR: failed to set compression attribute on variable in file id %d", exoid);
1833 ex_err_fn(exoid, __func__, errmsg, status);
1834 }
1835
1836 if (type == 2 && file->quantize_nsd > 0) {
1837 #if NC_HAS_QUANTIZE == 1
1838 // Lossy compression using netCDF quantize methods.
1839 if ((status = nc_def_var_quantize(exoid, varid, NC_QUANTIZE_GRANULARBR,
1840 file->quantize_nsd)) != NC_NOERR) {
1841 char errmsg[MAX_ERR_LENGTH];
1842 snprintf(errmsg, MAX_ERR_LENGTH,
1843 "ERROR: failed to set quanitzation method on variable in file id %d", exoid);
1844 ex_err_fn(exoid, __func__, errmsg, status);
1845 }
1846 #else
1847 char errmsg[MAX_ERR_LENGTH];
1848 snprintf(errmsg, MAX_ERR_LENGTH,
1849 "ERROR: Quanitzation is not supported in this version of netCDF library.");
1850 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
1851 #endif
1852 }
1853 }
1854 #if defined(PARALLEL_AWARE_EXODUS)
1855 if (file->is_parallel) {
1856 nc_var_par_access(exoid, varid, NC_COLLECTIVE);
1857 }
1858 #endif
1859 }
1860 #endif
1861 }
1862
1863 /*!
1864 \internal
1865 \undoc
1866 */
1867 int exi_leavedef(int exoid, const char *call_func)
1868 {
1869 int status;
1870
1871 struct exi_file_item *file = exi_find_file_item(exoid);
1872 if (!file) {
1873 char errmsg[MAX_ERR_LENGTH];
1874 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for %s called from %s.", exoid,
1875 __func__, call_func);
1876 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
1877 return EX_FATAL;
1878 }
1879
1880 if (!file->persist_define_mode && file->in_define_mode) {
1881 if ((status = nc_enddef(exoid)) != NC_NOERR) {
1882 char errmsg[MAX_ERR_LENGTH];
1883 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d",
1884 exoid);
1885 ex_err_fn(exoid, call_func, errmsg, status);
1886
1887 return EX_FATAL;
1888 }
1889 file->in_define_mode = 0;
1890 }
1891 return EX_NOERR;
1892 }
1893
1894 int exi_redef(int exoid, const char *call_func)
1895 {
1896 int status;
1897
1898 struct exi_file_item *file = exi_find_file_item(exoid);
1899
1900 if (!file) {
1901 char errmsg[MAX_ERR_LENGTH];
1902 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for %s called from %s.", exoid,
1903 __func__, call_func);
1904 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
1905 return EX_FATAL;
1906 }
1907
1908 if (!file->in_define_mode) {
1909 if ((status = nc_redef(exoid)) != NC_NOERR) {
1910 char errmsg[MAX_ERR_LENGTH];
1911 snprintf(errmsg, MAX_ERR_LENGTH,
1912 "ERROR: failed to put file %d into definition mode in exi_redef called from %s",
1913 exoid, call_func);
1914 ex_err_fn(exoid, __func__, errmsg, status);
1915 return EX_FATAL;
1916 }
1917 file->in_define_mode = 1;
1918 }
1919 return EX_NOERR;
1920 }
1921
1922 int exi_persist_redef(int exoid, const char *call_func)
1923 {
1924 int status;
1925
1926 struct exi_file_item *file = exi_find_file_item(exoid);
1927
1928 if (!file) {
1929 char errmsg[MAX_ERR_LENGTH];
1930 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for %s called from %s.", exoid,
1931 __func__, call_func);
1932 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
1933 return EX_FATAL;
1934 }
1935
1936 if ((++file->persist_define_mode == 1) && !file->in_define_mode) {
1937 if ((status = nc_redef(exoid)) != NC_NOERR) {
1938 char errmsg[MAX_ERR_LENGTH];
1939 snprintf(
1940 errmsg, MAX_ERR_LENGTH,
1941 "ERROR: failed to put file %d into definition mode in exi_persist_redef called from %s",
1942 exoid, call_func);
1943 ex_err_fn(exoid, __func__, errmsg, status);
1944 return EX_FATAL;
1945 }
1946 file->in_define_mode = 1;
1947 }
1948 return EX_NOERR;
1949 }
1950
1951 int exi_persist_leavedef(int exoid, const char *call_func)
1952 {
1953 int status;
1954
1955 struct exi_file_item *file = exi_find_file_item(exoid);
1956 if (!file) {
1957 char errmsg[MAX_ERR_LENGTH];
1958 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for %s called from %s.", exoid,
1959 __func__, call_func);
1960 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
1961 return EX_FATAL;
1962 }
1963
1964 if ((file->persist_define_mode-- == 1) && file->in_define_mode) {
1965 if ((status = nc_enddef(exoid)) != NC_NOERR) {
1966 char errmsg[MAX_ERR_LENGTH];
1967 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d",
1968 exoid);
1969 ex_err_fn(exoid, call_func, errmsg, status);
1970
1971 return EX_FATAL;
1972 }
1973 file->in_define_mode = 0;
1974 file->persist_define_mode = 0;
1975 }
1976 return EX_NOERR;
1977 }
1978
1979 static int warning_output = 0;
1980
1981 int exi_check_version(int run_version)
1982 {
1983 if (run_version != EX_API_VERS_NODOT && warning_output == 0) {
1984 int run_version_major = run_version / 100;
1985 int run_version_minor = run_version % 100;
1986 int lib_version_major = EXODUS_VERSION_MAJOR;
1987 int lib_version_minor = EXODUS_VERSION_MINOR;
1988 fprintf(stderr,
1989 "EXODUS: Warning: This code was compiled with exodus "
1990 "version %d.%02d,\n but was linked with exodus "
1991 "library version %d.%02d\n This is probably an "
1992 "error in the build process of this code.\n",
1993 run_version_major, run_version_minor, lib_version_major, lib_version_minor);
1994 warning_output = 1;
1995 }
1996 return warning_output;
1997 }
1998
1999 /*!
2000 \internal
2001 \undoc
2002 */
2003 int exi_handle_mode(unsigned int my_mode, int is_parallel, int run_version)
2004 {
2005 char errmsg[MAX_ERR_LENGTH];
2006 int nc_mode = 0;
2007 static int netcdf4_mode = -1;
2008 #if NC_HAS_CDF5
2009 static int netcdf5_mode = -1;
2010 #endif
2011
2012 int filesiz = 1;
2013 int int64_status;
2014 int pariomode = 0;
2015
2016 /* Contains a 1 in all bits corresponding to file modes */
2017 /* Do not include EX_64BIT_DATA in this list */
2018 static unsigned int all_modes = EX_NORMAL_MODEL | EX_64BIT_OFFSET | EX_NETCDF4 | EX_PNETCDF;
2019
2020 exi_check_version(run_version);
2021
2022 /*
2023 * See if specified mode is supported in the version of netcdf we
2024 * are using
2025 */
2026 #if !NC_HAS_HDF5
2027 if (my_mode & EX_NETCDF4) {
2028 snprintf(errmsg, MAX_ERR_LENGTH,
2029 "EXODUS: ERROR: File format specified as netcdf-4, but the "
2030 "NetCDF library being used was not configured to enable "
2031 "this format\n");
2032 ex_err(__func__, errmsg, EX_BADPARAM);
2033 EX_FUNC_LEAVE(EX_FATAL);
2034 }
2035 #endif
2036
2037 #if !NC_HAS_CDF5
2038 if (my_mode & EX_64BIT_DATA) {
2039 snprintf(errmsg, MAX_ERR_LENGTH,
2040 "EXODUS: ERROR: File format specified as 64bit_data, but "
2041 "the NetCDF library being used does not support this "
2042 "format\n");
2043 ex_err(__func__, errmsg, EX_BADPARAM);
2044 EX_FUNC_LEAVE(EX_FATAL);
2045 }
2046 #endif
2047
2048 /* EX_64_BIT_DATA is 64-bit integer version of EX_PNETCDF. If
2049 EX_64_BIT_DATA and EX_PNETCDF is not set, then set EX_PNETCDF... */
2050 if (my_mode & EX_64BIT_DATA) {
2051 my_mode |= EX_PNETCDF;
2052 }
2053
2054 /* Check that one and only one format mode is specified... */
2055 {
2056 unsigned int set_modes = all_modes & my_mode;
2057
2058 if (set_modes == 0) {
2059 my_mode |= EX_64BIT_OFFSET; /* Default if nothing specified */
2060 }
2061 else {
2062 /* Checks that only a single bit is set */
2063 set_modes = !(set_modes & (set_modes - 1));
2064 if (!set_modes) {
2065 snprintf(errmsg, MAX_ERR_LENGTH,
2066 "EXODUS: ERROR: More than 1 file format "
2067 "(EX_NORMAL_MODEL, EX_LARGE_MODEL, EX_64BIT_OFFSET, "
2068 "or EX_NETCDF4)\nwas specified in the "
2069 "mode argument of the ex_create call. Only a single "
2070 "format can be specified.\n");
2071 ex_err(__func__, errmsg, EX_BADPARAM);
2072 EX_FUNC_LEAVE(EX_FATAL);
2073 }
2074 }
2075 }
2076
2077 /*
2078 * See if any integer data is to be stored as int64 (long long). If
2079 * so, then need to set NC_NETCDF4 and unset NC_CLASSIC_MODEL (or
2080 * set EX_NOCLASSIC. Output meaningful error message if the library
2081 * is not NetCDF-4 enabled...
2082 *
2083 * As of netcdf-4.4.0, can also use NC_64BIT_DATA (CDF5) mode for this...
2084 */
2085 int64_status = my_mode & (EX_ALL_INT64_DB | EX_ALL_INT64_API);
2086
2087 if ((int64_status & EX_ALL_INT64_DB) != 0) {
2088 #if NC_HAS_HDF5 || NC_HAS_CDF5
2089 /* Library DOES support netcdf4 and/or cdf5 ... See if user
2090 * specified either of these and use that one; if not, pick
2091 * netcdf4, non-classic as default.
2092 */
2093 if (my_mode & EX_NETCDF4) {
2094 my_mode |= EX_NOCLASSIC;
2095 }
2096 #if NC_HAS_CDF5
2097 else if (my_mode & EX_64BIT_DATA) {
2098 ; /* Do nothing, already set */
2099 }
2100 else if (my_mode & EX_PNETCDF) {
2101 my_mode |= EX_64BIT_DATA;
2102 }
2103 #endif
2104 else {
2105 /* Unset the current mode so we don't have multiples specified */
2106 /* ~all_modes sets to 1 all bits not associated with file format */
2107 my_mode &= ~all_modes;
2108 #if NC_HAS_HDF5
2109 /* Pick netcdf4 as default mode for 64-bit integers */
2110 my_mode |= EX_NOCLASSIC;
2111 my_mode |= EX_NETCDF4;
2112 #else
2113 /* Pick 64bit_data as default mode for 64-bit integers */
2114 my_mode |= EX_64BIT_DATA;
2115 #endif
2116 }
2117 #else
2118 /* Library does NOT support netcdf4 or cdf5 */
2119 snprintf(errmsg, MAX_ERR_LENGTH,
2120 "EXODUS: ERROR: 64-bit integer storage requested, but the "
2121 "netcdf library does not support the required netcdf-4 or "
2122 "64BIT_DATA extensions.\n");
2123 ex_err(__func__, errmsg, EX_BADPARAM);
2124 EX_FUNC_LEAVE(EX_FATAL);
2125 #endif
2126 }
2127
2128 #if defined(PARALLEL_AWARE_EXODUS)
2129 /* Check parallel io mode. Valid is NC_MPIPOSIX or NC_MPIIO or NC_PNETCDF
2130 * Exodus uses different flag values; map to netcdf values
2131 *
2132 * NOTE: In current versions of NetCDF, MPIPOSIX and MPIIO are ignored and the
2133 * underlying format is either NC_PNETCDF or NC_NETCDF4 (hdf5-based)
2134 * They map NC_MPIIO to NC_PNETCDF, but in the past, exodus mapped EX_MPIIO
2135 * to EX_NETCDF4.
2136 */
2137 if (is_parallel) {
2138 int tmp_mode = 0;
2139 if (my_mode & EX_MPIPOSIX) {
2140 pariomode = NC_MPIIO;
2141 tmp_mode = EX_NETCDF4;
2142 #if !NC_HAS_HDF5
2143 snprintf(errmsg, MAX_ERR_LENGTH,
2144 "EXODUS: ERROR: EX_MPIPOSIX parallel output requested "
2145 "which requires NetCDF-4 support, but the library does "
2146 "not have that option enabled.\n");
2147 ex_err(__func__, errmsg, EX_BADPARAM);
2148 EX_FUNC_LEAVE(EX_FATAL);
2149 #endif
2150 }
2151 else if (my_mode & EX_MPIIO) {
2152 pariomode = NC_MPIIO;
2153 tmp_mode = EX_NETCDF4;
2154 #if !NC_HAS_HDF5
2155 snprintf(errmsg, MAX_ERR_LENGTH,
2156 "EXODUS: ERROR: EX_MPIIO parallel output requested which "
2157 "requires NetCDF-4 support, but the library does not "
2158 "have that option enabled.\n");
2159 ex_err(__func__, errmsg, EX_BADPARAM);
2160 EX_FUNC_LEAVE(EX_FATAL);
2161 #endif
2162 }
2163 else if (my_mode & EX_NETCDF4) {
2164 pariomode = NC_MPIIO;
2165 tmp_mode = EX_NETCDF4;
2166 #if !NC_HAS_HDF5
2167 snprintf(errmsg, MAX_ERR_LENGTH,
2168 "EXODUS: ERROR: EX_NETCDF4 parallel output requested which "
2169 "requires NetCDF-4 support, but the library does not "
2170 "have that option enabled.\n");
2171 ex_err(__func__, errmsg, EX_BADPARAM);
2172 EX_FUNC_LEAVE(EX_FATAL);
2173 #endif
2174 }
2175 else if (my_mode & EX_PNETCDF) {
2176 pariomode = NC_PNETCDF;
2177 /* See if client specified 64-bit or not... */
2178 if ((my_mode & EX_64BIT_DATA) || (int64_status & EX_ALL_INT64_DB)) {
2179 tmp_mode = EX_64BIT_DATA;
2180 }
2181 else {
2182 if (my_mode & EX_64BIT_DATA) {
2183 tmp_mode = EX_64BIT_DATA;
2184 }
2185 else {
2186 tmp_mode = EX_64BIT_OFFSET;
2187 }
2188 }
2189 #if !NC_HAS_PNETCDF
2190 snprintf(errmsg, MAX_ERR_LENGTH,
2191 "EXODUS: ERROR: EX_PNETCDF parallel output requested "
2192 "which requires PNetCDF support, but the library does "
2193 "not have that option enabled.\n");
2194 ex_err(__func__, errmsg, EX_BADPARAM);
2195 EX_FUNC_LEAVE(EX_FATAL);
2196 #endif
2197 }
2198
2199 /* If tmp_mode was set here, then need to clear any other mode that
2200 was potentially already set in my_mode... */
2201 my_mode &= ~all_modes;
2202 my_mode |= tmp_mode;
2203 }
2204 #else
2205 EX_UNUSED(is_parallel);
2206 #endif /* PARALLEL_AWARE_EXODUS */
2207
2208 if (my_mode & EX_NETCDF4) {
2209 nc_mode |= NC_NETCDF4;
2210 }
2211 else {
2212 if (netcdf4_mode == -1) {
2213 char *option = getenv("EXODUS_NETCDF4");
2214 if (option != NULL) {
2215 netcdf4_mode = NC_NETCDF4;
2216 if (option[0] != 'q') {
2217 fprintf(stderr, "EXODUS: Using netcdf version 4 selected via "
2218 "EXODUS_NETCDF4 environment variable\n");
2219 }
2220 }
2221 else {
2222 netcdf4_mode = 0;
2223 }
2224 }
2225 nc_mode |= netcdf4_mode;
2226 }
2227
2228 if (!(my_mode & EX_NOCLASSIC)) {
2229 nc_mode |= NC_CLASSIC_MODEL;
2230 }
2231
2232 #if NC_HAS_CDF5
2233 if (my_mode & EX_64BIT_DATA) {
2234 nc_mode |= (NC_64BIT_DATA);
2235 }
2236 else {
2237 if (netcdf5_mode == -1) {
2238 char *option = getenv("EXODUS_NETCDF5");
2239 if (option != NULL) {
2240 netcdf5_mode = NC_64BIT_DATA;
2241 if (option[0] != 'q') {
2242 fprintf(stderr, "EXODUS: Using netcdf version 5 (CDF5) selected via "
2243 "EXODUS_NETCDF5 environment variable\n");
2244 }
2245 }
2246 else {
2247 netcdf5_mode = 0;
2248 }
2249 }
2250 nc_mode |= netcdf5_mode;
2251 }
2252 #endif
2253
2254 /*
2255 * Hardwire filesiz to 1 for all created files. Reduce complexity in nodal output routines.
2256 * has been default for a decade or so, but still support it on read...
2257 */
2258 if (
2259 #if NC_HAS_HDF5
2260 !(nc_mode & NC_NETCDF4) &&
2261 #endif
2262 #if NC_HAS_CDF5
2263 !(nc_mode & NC_64BIT_DATA) &&
2264 #endif
2265 filesiz == 1) {
2266 nc_mode |= NC_64BIT_OFFSET;
2267 }
2268
2269 if (my_mode & EX_SHARE) {
2270 nc_mode |= NC_SHARE;
2271 }
2272
2273 /*
2274 * set error handling mode to no messages, non-fatal errors
2275 * unless specified differently via environment.
2276 */
2277 {
2278 if (exoptval != EX_VERBOSE) {
2279 /* Avoid getenv call if already in verbose mode */
2280 char *option = getenv("EXODUS_VERBOSE");
2281 if (option != NULL) {
2282 exoptval = EX_VERBOSE;
2283 if (option[0] != 'q') {
2284 fprintf(stderr, "EXODUS: Setting EX_VERBOSE mode since EXODUS_VERBOSE environment "
2285 "variable is set.\n");
2286 }
2287 }
2288 }
2289 ex_opts(exoptval); /* call required to set ncopts first time through */
2290 }
2291
2292 if (my_mode & EX_CLOBBER) {
2293 nc_mode |= NC_CLOBBER;
2294 }
2295 else {
2296 nc_mode |= NC_NOCLOBBER;
2297 }
2298
2299 #if NC_HAS_DISKLESS
2300 /* Use of diskless (in-memory) and parallel is not tested... */
2301 if (my_mode & EX_DISKLESS) {
2302 nc_mode |= NC_DISKLESS;
2303 nc_mode |= NC_WRITE;
2304 #if defined NC_PERSIST
2305 nc_mode |= NC_PERSIST;
2306 #endif
2307 }
2308 #endif
2309 return nc_mode | pariomode;
2310 }
2311
2312 /*!
2313 \internal
2314 \undoc
2315 */
2316 int exi_populate_header(int exoid, const char *path, int my_mode, int is_parallel, int *comp_ws,
2317 int *io_ws)
2318 {
2319 int status;
2320 int old_fill;
2321 int lio_ws;
2322 int filesiz = 1;
2323 bool is_hdf5 = false;
2324 bool is_pnetcdf = false;
2325
2326 float version;
2327 char errmsg[MAX_ERR_LENGTH];
2328 int int64_status = my_mode & (EX_ALL_INT64_DB | EX_ALL_INT64_API);
2329
2330 int format = 0;
2331 int mode;
2332
2333 /* turn off automatic filling of netCDF variables */
2334 if ((status = nc_set_fill(exoid, NC_NOFILL, &old_fill)) != NC_NOERR) {
2335 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to set nofill mode in file id %d", exoid);
2336 ex_err_fn(exoid, __func__, errmsg, status);
2337 return EX_FATAL;
2338 }
2339
2340 /* Verify that there is not an existing file_item struct for this
2341 exoid This could happen (and has) when application calls
2342 ex_open(), but then closes file using nc_close() and then reopens
2343 file. NetCDF will possibly reuse the exoid which results in
2344 internal corruption in exodus data structures since exodus does
2345 not know that file was closed and possibly new file opened for
2346 this exoid
2347 */
2348 if (exi_find_file_item(exoid) != NULL) {
2349 snprintf(errmsg, MAX_ERR_LENGTH,
2350 "ERROR: There is an existing file already using the file "
2351 "id %d which was also assigned to file %s.\n\tWas "
2352 "nc_close() called instead of ex_close() on an open Exodus "
2353 "file?\n",
2354 exoid, path);
2355 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
2356 nc_close(exoid);
2357 return EX_FATAL;
2358 }
2359
2360 /* initialize floating point size conversion. since creating new file,
2361 * i/o wordsize attribute from file is zero.
2362 */
2363 /* Determine format being used for underlying NetCDF file */
2364 nc_inq_format_extended(exoid, &format, &mode);
2365
2366 if (format & NC_FORMAT_PNETCDF) {
2367 is_pnetcdf = true;
2368 ;
2369 }
2370
2371 if (format & NC_FORMAT_NC_HDF5) {
2372 is_hdf5 = true;
2373 }
2374
2375 if (exi_conv_init(exoid, comp_ws, io_ws, 0, int64_status, is_parallel, is_hdf5, is_pnetcdf,
2376 my_mode & EX_WRITE) != EX_NOERR) {
2377 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to init conversion routines in file id %d",
2378 exoid);
2379 ex_err_fn(exoid, __func__, errmsg, EX_LASTERR);
2380 return EX_FATAL;
2381 }
2382
2383 /* put the EXODUS version number, and i/o floating point word size as
2384 * netcdf global attributes
2385 */
2386
2387 /* store Exodus API version # as an attribute */
2388 {
2389 float version_major = EXODUS_VERSION_MAJOR;
2390 float version_minor = EXODUS_VERSION_MINOR;
2391 version = version_major + version_minor / 100.0;
2392 if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_API_VERSION, NC_FLOAT, 1, &version)) !=
2393 NC_NOERR) {
2394 snprintf(errmsg, MAX_ERR_LENGTH,
2395 "ERROR: failed to store Exodus II API version attribute in file id %d", exoid);
2396 ex_err_fn(exoid, __func__, errmsg, status);
2397 return EX_FATAL;
2398 }
2399 }
2400
2401 /* store Exodus file version # as an attribute */
2402 {
2403 float version_major = EXODUS_VERSION_MAJOR;
2404 float version_minor = EXODUS_VERSION_MINOR;
2405 version = version_major + version_minor / 100.0;
2406 if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_VERSION, NC_FLOAT, 1, &version)) !=
2407 NC_NOERR) {
2408 snprintf(errmsg, MAX_ERR_LENGTH,
2409 "ERROR: failed to store Exodus II file version attribute in file id %d", exoid);
2410 ex_err_fn(exoid, __func__, errmsg, status);
2411 return EX_FATAL;
2412 }
2413 }
2414
2415 /* store Exodus file float word size as an attribute */
2416 lio_ws = (*io_ws);
2417 if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, NC_INT, 1, &lio_ws)) !=
2418 NC_NOERR) {
2419 snprintf(errmsg, MAX_ERR_LENGTH,
2420 "ERROR: failed to store Exodus II file float word size "
2421 "attribute in file id %d",
2422 exoid);
2423 ex_err_fn(exoid, __func__, errmsg, status);
2424 return EX_FATAL;
2425 }
2426
2427 /* store Exodus file size (1=large, 0=normal) as an attribute */
2428 if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_FILESIZE, NC_INT, 1, &filesiz)) != NC_NOERR) {
2429 snprintf(errmsg, MAX_ERR_LENGTH,
2430 "ERROR: failed to store Exodus II file size attribute in file id %d", exoid);
2431 ex_err_fn(exoid, __func__, errmsg, status);
2432 return EX_FATAL;
2433 }
2434
2435 {
2436 int max_so_far = 32;
2437 if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far)) !=
2438 NC_NOERR) {
2439 snprintf(errmsg, MAX_ERR_LENGTH,
2440 "ERROR: failed to add maximum_name_length attribute in file id %d", exoid);
2441 ex_err_fn(exoid, __func__, errmsg, status);
2442 return EX_FATAL;
2443 }
2444 }
2445
2446 {
2447 int int64_db_status = int64_status & EX_ALL_INT64_DB;
2448 if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_INT64_STATUS, NC_INT, 1,
2449 &int64_db_status)) != NC_NOERR) {
2450 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add int64_status attribute in file id %d",
2451 exoid);
2452 ex_err_fn(exoid, __func__, errmsg, status);
2453 return EX_FATAL;
2454 }
2455 }
2456
2457 #if 0
2458 /* Testing to see if can eliminate some nc_enddef movement of vars/recs */
2459 if ((status = nc__enddef(exoid, 10000, 4, 10000, 4)) != NC_NOERR) {
2460 #else
2461 if ((status = nc_enddef(exoid)) != NC_NOERR) {
2462 #endif
2463 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to complete definition for file id %d", exoid);
2464 ex_err_fn(exoid, __func__, errmsg, status);
2465 return EX_FATAL;
2466 }
2467 return EX_NOERR;
2468 }
2469
2470 /*!
2471 \internal
2472 \undoc
2473 Safer than strncpy -- guarantees null termination
2474 */
2475 char *ex_copy_string(char *dest, char const *source, size_t elements)
2476 {
2477 char *d;
2478 for (d = dest; d + 1 < dest + elements && *source; d++, source++) {
2479 *d = *source;
2480 }
2481 *d = '\0';
2482 return d;
2483 }
2484
2485 /* Used by the entity attribute code `ex_get_attribute(s)` and `ex_put_attribute(s)` */
2486 int exi_get_varid(int exoid, ex_entity_type obj_type, ex_entity_id id)
2487 {
2488 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
2489 EX_FUNC_LEAVE(EX_FATAL);
2490 }
2491
2492 if (obj_type == EX_GLOBAL) {
2493 return NC_GLOBAL;
2494 }
2495
2496 int status = 0;
2497 char errmsg[MAX_ERR_LENGTH];
2498
2499 if (obj_type == EX_NODAL) {
2500 /* For the nodal entity attributes, we store it on the
2501 "coor_names" variable. Not exactly logical, but it exists in any
2502 model that has nodes, so it at least gives us a place to put the
2503 attribute. Another possibility would be the nodal x-coordinate
2504 variable...
2505 */
2506 int varid = 0;
2507 if ((status = nc_inq_varid(exoid, VAR_NAME_COOR, &varid)) != NC_NOERR) {
2508 snprintf(errmsg, MAX_ERR_LENGTH,
2509 "ERROR: failed to locate node block variable id in file id %d", exoid);
2510 ex_err_fn(exoid, __func__, errmsg, status);
2511 return EX_FATAL;
2512 }
2513 return varid;
2514 }
2515
2516 /* Everything else ... */
2517 /* First, locate index of this objects id `obj_type` id array */
2518 /* First, locate index of this objects id `obj_type` id array */
2519 int id_ndx = exi_id_lkup(exoid, obj_type, id);
2520 if (id_ndx <= 0) {
2521 ex_get_err(NULL, NULL, &status);
2522 if (status != 0) {
2523 if (status == EX_NULLENTITY) { /* NULL object? */
2524 return EX_NOERR;
2525 }
2526 snprintf(errmsg, MAX_ERR_LENGTH,
2527 "ERROR: failed to locate %s id %" PRId64 " in id array in file id %d",
2528 ex_name_of_object(obj_type), id, exoid);
2529 ex_err_fn(exoid, __func__, errmsg, status);
2530 return EX_FATAL;
2531 }
2532 }
2533
2534 const char *entryptr = NULL;
2535 switch (obj_type) {
2536 case EX_ASSEMBLY: entryptr = VAR_ENTITY_ASSEMBLY(id_ndx); break;
2537 case EX_BLOB: entryptr = VAR_ENTITY_BLOB(id_ndx); break;
2538 case EX_NODE_SET: entryptr = VAR_NODE_NS(id_ndx); break;
2539 case EX_EDGE_SET: entryptr = VAR_EDGE_ES(id_ndx); break;
2540 case EX_FACE_SET: entryptr = VAR_FACE_FS(id_ndx); break;
2541 case EX_SIDE_SET: entryptr = VAR_ELEM_SS(id_ndx); break;
2542 case EX_ELEM_SET: entryptr = VAR_ELEM_ELS(id_ndx); break;
2543 case EX_EDGE_BLOCK: entryptr = VAR_EBCONN(id_ndx); break;
2544 case EX_FACE_BLOCK: entryptr = VAR_FBCONN(id_ndx); break;
2545 case EX_ELEM_BLOCK: entryptr = VAR_CONN(id_ndx); break;
2546 default:
2547 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported in call to %s", obj_type,
2548 __func__);
2549 ex_err(__func__, errmsg, EX_BADPARAM);
2550 return EX_FATAL;
2551 }
2552
2553 int varid = 0;
2554 if ((status = nc_inq_varid(exoid, entryptr, &varid)) != NC_NOERR) {
2555 snprintf(errmsg, MAX_ERR_LENGTH,
2556 "ERROR: failed to locate entity list array for %s %" PRId64 " in file id %d",
2557 ex_name_of_object(obj_type), id, exoid);
2558 ex_err_fn(exoid, __func__, errmsg, status);
2559 return EX_FATAL;
2560 }
2561 return varid;
2562 }
2563
2564 /*
2565 * Code from:
2566 * https://stackoverflow.com/questions/11034002/how-to-get-absolute-path-of-file-or-directory-that-does-not-exist
2567 *
2568 * Return the input path in a canonical form. This is achieved by
2569 * expanding all symbolic links, resolving references to "." and "..",
2570 * and removing duplicate "/" characters.
2571 *
2572 * If the file exists, its path is canonicalized and returned. If the file,
2573 * or parts of the containing directory, do not exist, path components are
2574 * removed from the end until an existing path is found. The remainder of the
2575 * path is then appended to the canonical form of the existing path,
2576 * and returned. Consequently, the returned path may not exist. The portion
2577 * of the path which exists, however, is represented in canonical form.
2578 *
2579 * If successful, this function returns a C-string, which needs to be freed by
2580 * the caller using free().
2581 *
2582 * ARGUMENTS:
2583 * file_path
2584 * File path, whose canonical form to return.
2585 *
2586 * RETURNS:
2587 * On success, returns the canonical path to the file, which needs to be freed
2588 * by the caller.
2589 *
2590 * On failure, returns NULL.
2591 */
2592 char *exi_canonicalize_filename(char const *file_path)
2593 {
2594 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
2595 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
2596 return _fullpath(NULL, file_path, _MAX_PATH);
2597 #else
2598 char *canonical_file_path = NULL;
2599 unsigned int file_path_len = strlen(file_path);
2600
2601 if (file_path_len > 0) {
2602 canonical_file_path = realpath(file_path, NULL);
2603 if (canonical_file_path == NULL && errno == ENOENT) {
2604 // The file was not found. Back up to a segment which exists,
2605 // and append the remainder of the path to it.
2606 char *file_path_copy = NULL;
2607 if (file_path[0] == '/' || (strncmp(file_path, "./", 2) == 0) ||
2608 (strncmp(file_path, "../", 3) == 0)) {
2609 // Absolute path, or path starts with "./" or "../"
2610 file_path_copy = strdup(file_path);
2611 }
2612 else {
2613 // Relative path
2614 file_path_copy = (char *)malloc(strlen(file_path) + 3);
2615 strcpy(file_path_copy, "./");
2616 strcat(file_path_copy, file_path);
2617 }
2618
2619 // Remove path components from the end, until an existing path is found
2620 for (int char_idx = strlen(file_path_copy) - 1; char_idx >= 0 && canonical_file_path == NULL;
2621 --char_idx) {
2622 if (file_path_copy[char_idx] == '/') {
2623 // Remove the slash character
2624 file_path_copy[char_idx] = '\0';
2625
2626 canonical_file_path = realpath(file_path_copy, NULL);
2627 if (canonical_file_path != NULL) {
2628 // An existing path was found. Append the remainder of the path
2629 // to a canonical form of the existing path.
2630 char *combined_file_path = (char *)malloc(strlen(canonical_file_path) +
2631 strlen(file_path_copy + char_idx + 1) + 2);
2632 strcpy(combined_file_path, canonical_file_path);
2633 strcat(combined_file_path, "/");
2634 strcat(combined_file_path, file_path_copy + char_idx + 1);
2635 free(canonical_file_path);
2636 canonical_file_path = combined_file_path;
2637 }
2638 else {
2639 // The path segment does not exist. Replace the slash character
2640 // and keep trying by removing the previous path component.
2641 file_path_copy[char_idx] = '/';
2642 }
2643 }
2644 }
2645
2646 free(file_path_copy);
2647 }
2648 }
2649 return canonical_file_path;
2650 #endif
2651 }