Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_map.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 * expmap - ex_put_map
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int* elem_map element order map array
16 *
17 * exit conditions -
18 *
19 * revision history -
20 *
21 *
22 *****************************************************************************/
23
24 #include "exodusII.h" // for ex_err, etc
25 #include "exodusII_int.h" // for EX_FATAL, EX_NOERR, etc
26
27 /*!
28 The function ex_put_map() writes out the optional element order map to
29 the database. The function ex_put_init() must be invoked before this call
30 is made.
31
32 In case of an error, ex_put_map() returns a negative number; a warning
33 will return a positive number. Possible causes of errors include:
34 - data file not properly opened with call to ex_create() or ex_open()
35 - data file opened for read only.
36 - data file not initialized properly with call to ex_put_init().
37 - an element map already exists in the file.
38
39 \param[in] exoid exodus file ID returned from a previous call to
40 ex_create() or ex_open().
41 \param[in] elem_map The element order map.
42
43 The following code generates a default element order map and outputs
44 it to an open exodus file. This is a trivial case and included just
45 for illustration. Since this map is optional, it should be written out
46 only if it contains something other than the default map.
47
48 ~~~{.c}
49 int error, exoid;
50 int *elem_map = (int *)calloc(num_elem, sizeof(int));
51 for (i=0; i < num_elem; i++) {
52 elem_map[i] = i+1;
53 }
54 error = ex_put_map(exoid, elem_map);
55 ~~~
56
57 */
58
59 int ex_put_map(int exoid, const void_int *elem_map)
60 {
61 int numelemdim, dims[1], mapid, status;
62 int map_int_type;
63 char errmsg[MAX_ERR_LENGTH];
64
65 EX_FUNC_ENTER();
66 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
67 EX_FUNC_LEAVE(EX_FATAL);
68 }
69
70 /* inquire id's of previously defined dimensions */
71
72 /* determine number of elements. Return if zero... */
73 if (nc_inq_dimid(exoid, DIM_NUM_ELEM, &numelemdim) != NC_NOERR) {
74 EX_FUNC_LEAVE(EX_NOERR);
75 }
76
77 /* put netcdf file into define mode */
78 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
79 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
80 ex_err_fn(exoid, __func__, errmsg, status);
81 EX_FUNC_LEAVE(EX_FATAL);
82 }
83
84 /* create a variable array in which to store the element map */
85 dims[0] = numelemdim;
86
87 map_int_type = NC_INT;
88 if (ex_int64_status(exoid) & EX_MAPS_INT64_DB) {
89 map_int_type = NC_INT64;
90 }
91
92 if ((status = nc_def_var(exoid, VAR_MAP, map_int_type, 1, dims, &mapid)) != NC_NOERR) {
93 if (status == NC_ENAMEINUSE) {
94 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: element map already exists in file id %d", exoid);
95 ex_err_fn(exoid, __func__, errmsg, status);
96 }
97 else {
98 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to create element map array in file id %d",
99 exoid);
100 ex_err_fn(exoid, __func__, errmsg, status);
101 }
102 goto error_ret; /* exit define mode and return */
103 }
104 exi_compress_variable(exoid, mapid, 1);
105
106 /* leave define mode */
107 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
108 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode");
109 ex_err_fn(exoid, __func__, errmsg, status);
110 EX_FUNC_LEAVE(EX_FATAL);
111 }
112
113 /* write out the element order map */
114 if (ex_int64_status(exoid) & EX_MAPS_INT64_API) {
115 status = nc_put_var_longlong(exoid, mapid, elem_map);
116 }
117 else {
118 status = nc_put_var_int(exoid, mapid, elem_map);
119 }
120
121 if (status != NC_NOERR) {
122 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store element map in file id %d", exoid);
123 ex_err_fn(exoid, __func__, errmsg, status);
124 EX_FUNC_LEAVE(EX_FATAL);
125 }
126
127 EX_FUNC_LEAVE(EX_NOERR);
128
129 /* Fatal error: exit definition mode and return */
130 error_ret:
131 exi_leavedef(exoid, __func__);
132 EX_FUNC_LEAVE(EX_FATAL);
133 }