Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 2023, 2024 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "exodusII.h" // for ex_err, etc
10 #include "exodusII_int.h" // for exi_compress_variable, etc
11
12 /*! \cond INTERNAL */
13 static int exi_prepare_result_var(int exoid, int num_vars, char *type_name, char *dim_name,
14 char *variable_name)
15 {
16 int status;
17 int dimid;
18 int varid;
19 int dims[2];
20 int dim_str_name;
21
22 char errmsg[MAX_ERR_LENGTH];
23
24 if ((status = nc_def_dim(exoid, dim_name, num_vars, &dimid)) != NC_NOERR) {
25 if (status == NC_ENAMEINUSE) {
26 snprintf(errmsg, MAX_ERR_LENGTH,
27 "ERROR: %s variable name parameters are already defined "
28 "in file id %d",
29 type_name, exoid);
30 ex_err_fn(exoid, __func__, errmsg, status);
31 }
32 else {
33 snprintf(errmsg, MAX_ERR_LENGTH,
34 "ERROR: failed to define number of %s variables in file id %d", type_name, exoid);
35 ex_err_fn(exoid, __func__, errmsg, status);
36 }
37 return EX_FATAL; /* exit define mode and return */
38 }
39
40 /* Now define type_name variable name variable */
41 if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name)) != NC_NOERR) {
42 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid);
43 ex_err_fn(exoid, __func__, errmsg, status);
44 return EX_FATAL;
45 }
46
47 dims[0] = dimid;
48 dims[1] = dim_str_name;
49 if ((status = nc_def_var(exoid, variable_name, NC_CHAR, 2, dims, &varid)) != NC_NOERR) {
50 if (status == NC_ENAMEINUSE) {
51 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s variable names are already defined in file id %d",
52 type_name, exoid);
53 ex_err_fn(exoid, __func__, errmsg, status);
54 }
55 else {
56 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s variable names in file id %d",
57 type_name, exoid);
58 ex_err_fn(exoid, __func__, errmsg, status);
59 }
60 return EX_FATAL; /* exit define mode and return */
61 }
62 #if defined(EX_CAN_USE_NC_DEF_VAR_FILL)
63 int fill = NC_FILL_CHAR;
64 nc_def_var_fill(exoid, varid, 0, &fill);
65 #endif
66 return EX_NOERR;
67 }
68 /*! \endcond */
69
70 /*!
71 \ingroup ResultsData
72
73 The function ex_put_variable_param() writes the number of global,
74 nodal, nodeset, sideset, edge, face, or element variables that will be
75 written to the database.
76
77 \return In case of an error, ex_put_variable_param() returns a negative
78 number; a warning will return a positive number. Possible causes of
79 errors include:
80 - data file not properly opened with call to ex_create() or ex_open()
81 - data file opened for read only.
82 - invalid variable type specified.
83 - data file not initialized properly with call to ex_put_init().
84 - this routine has already been called with the same variable
85 type; redefining the number of variables is not allowed.
86 - a warning value is returned if the number of variables
87 is specified as zero.
88
89 \param[in] exoid exodus file ID returned from a previous call to ex_create()
90 or ex_open().
91 \param[in] obj_type Variable indicating the type of variable which is
92 described. Use one
93 of the #ex_entity_type types specified in the table below.
94 \param[in] num_vars The number of var_type variables that will be written to the database.
95
96 | ex_entity_type| description |
97 |---------------|---------------------------|
98 | #EX_GLOBAL | Global entity type |
99 | #EX_NODE_SET | Node Set entity type |
100 | #EX_EDGE_BLOCK | Edge Block entity type |
101 | #EX_EDGE_SET | Edge Set entity type |
102 | #EX_FACE_BLOCK | Face Block entity type |
103 | #EX_FACE_SET | Face Set entity type |
104 | #EX_ELEM_BLOCK | Element Block entity type|
105 | #EX_ELEM_SET | Element Set entity type |
106 | #EX_SIDE_SET | Side Set entity type |
107
108 For example, the following code segment initializes the data file to
109 store global variables:
110
111 ~~~{.c}
112 int num_glo_vars, error, exoid;
113
114 \comment{write results variables parameters}
115 num_glo_vars = 3;
116
117 error = ex_put_variable_param (exoid, EX_GLOBAL, num_glo_vars);
118 ~~~
119
120 */
121
122 int ex_put_reduction_variable_param(int exoid, ex_entity_type obj_type, int num_vars)
123 {
124 int time_dim, dimid, dim_str_name, varid;
125 int dims[3];
126 char errmsg[MAX_ERR_LENGTH];
127 int status;
128
129 EX_FUNC_ENTER();
130 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
131 EX_FUNC_LEAVE(EX_FATAL);
132 }
133
134 /* if no variables are to be stored, return with warning */
135 if (num_vars == 0) {
136 snprintf(errmsg, MAX_ERR_LENGTH, "Warning: zero %s variables specified for file id %d",
137 ex_name_of_object(obj_type), exoid);
138 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
139
140 EX_FUNC_LEAVE(EX_WARN);
141 }
142
143 if (obj_type != EX_NODE_SET && obj_type != EX_EDGE_BLOCK && obj_type != EX_EDGE_SET &&
144 obj_type != EX_FACE_BLOCK && obj_type != EX_FACE_SET && obj_type != EX_ELEM_BLOCK &&
145 obj_type != EX_ELEM_SET && obj_type != EX_SIDE_SET && obj_type != EX_GLOBAL &&
146 obj_type != EX_ASSEMBLY && obj_type != EX_BLOB) {
147 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type %d specified in file id %d",
148 obj_type, exoid);
149 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
150 EX_FUNC_LEAVE(EX_WARN);
151 }
152
153 /* inquire previously defined dimensions */
154 if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) {
155 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", exoid);
156 ex_err_fn(exoid, __func__, errmsg, status);
157 EX_FUNC_LEAVE(EX_FATAL);
158 }
159
160 if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name)) < 0) {
161 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get name string length in file id %d",
162 exoid);
163 ex_err_fn(exoid, __func__, errmsg, status);
164 EX_FUNC_LEAVE(EX_FATAL);
165 }
166
167 /* put file into define mode */
168 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
169 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
170 ex_err_fn(exoid, __func__, errmsg, status);
171 EX_FUNC_LEAVE(EX_FATAL);
172 }
173
174 /* define dimensions and variables */
175 if (obj_type == EX_GLOBAL) {
176 if (exi_prepare_result_var(exoid, num_vars, "global", DIM_NUM_GLO_VAR, VAR_NAME_GLO_VAR) !=
177 EX_NOERR) {
178 goto error_ret;
179 }
180
181 if ((status = nc_inq_dimid(exoid, DIM_NUM_GLO_VAR, &dimid)) != NC_NOERR) {
182 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get global variable count in file id %d",
183 exoid);
184 ex_err_fn(exoid, __func__, errmsg, status);
185 EX_FUNC_LEAVE(EX_FATAL);
186 }
187 dims[0] = time_dim;
188 dims[1] = dimid;
189 if ((status = nc_def_var(exoid, VAR_GLO_VAR, nc_flt_code(exoid), 2, dims, &varid)) !=
190 NC_NOERR) {
191 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define global variables in file id %d",
192 exoid);
193 ex_err_fn(exoid, __func__, errmsg, status);
194 goto error_ret; /* exit define mode and return */
195 }
196 exi_compress_variable(exoid, varid, 2);
197 }
198
199 /* netCDF variables in which to store the EXODUS obj_type variable values will
200 * be defined in ex_put_*_var_tab or ex_put_*_var; at this point, we
201 * don't know what obj_type variables are valid for which obj_type blocks
202 * (the info that is stored in the obj_type variable truth table)
203 */
204 else if (obj_type == EX_ELEM_BLOCK) {
205 if (exi_prepare_result_var(exoid, num_vars, "element", DIM_NUM_ELE_RED_VAR,
206 VAR_NAME_ELE_RED_VAR) != EX_NOERR) {
207 goto error_ret;
208 }
209 }
210 else if (obj_type == EX_NODE_SET) {
211 if (exi_prepare_result_var(exoid, num_vars, "nodeset", DIM_NUM_NSET_RED_VAR,
212 VAR_NAME_NSET_RED_VAR) != EX_NOERR) {
213 goto error_ret;
214 }
215 }
216 else if (obj_type == EX_SIDE_SET) {
217 if (exi_prepare_result_var(exoid, num_vars, "sideset", DIM_NUM_SSET_RED_VAR,
218 VAR_NAME_SSET_RED_VAR) != EX_NOERR) {
219 goto error_ret;
220 }
221 }
222 else if (obj_type == EX_ASSEMBLY) {
223 if (exi_prepare_result_var(exoid, num_vars, "assembly", DIM_NUM_ASSEMBLY_RED_VAR,
224 VAR_NAME_ASSEMBLY_RED_VAR) != EX_NOERR) {
225 goto error_ret;
226 }
227 }
228 else if (obj_type == EX_BLOB) {
229 if (exi_prepare_result_var(exoid, num_vars, "blob", DIM_NUM_BLOB_RED_VAR,
230 VAR_NAME_BLOB_RED_VAR) != EX_NOERR) {
231 goto error_ret;
232 }
233 }
234 else if (obj_type == EX_EDGE_BLOCK) {
235 if (exi_prepare_result_var(exoid, num_vars, "edge", DIM_NUM_EDG_RED_VAR,
236 VAR_NAME_EDG_RED_VAR) != EX_NOERR) {
237 goto error_ret;
238 }
239 }
240 else if (obj_type == EX_FACE_BLOCK) {
241 if (exi_prepare_result_var(exoid, num_vars, "face", DIM_NUM_FAC_RED_VAR,
242 VAR_NAME_FAC_RED_VAR) != EX_NOERR) {
243 goto error_ret;
244 }
245 }
246 else if (obj_type == EX_EDGE_SET) {
247 if (exi_prepare_result_var(exoid, num_vars, "edgeset", DIM_NUM_ESET_RED_VAR,
248 VAR_NAME_ESET_RED_VAR) != EX_NOERR) {
249 goto error_ret;
250 }
251 }
252 else if (obj_type == EX_FACE_SET) {
253 if (exi_prepare_result_var(exoid, num_vars, "faceset", DIM_NUM_FSET_RED_VAR,
254 VAR_NAME_FSET_RED_VAR) != EX_NOERR) {
255 goto error_ret;
256 }
257 }
258 else if (obj_type == EX_ELEM_SET) {
259 if (exi_prepare_result_var(exoid, num_vars, "elementset", DIM_NUM_ELSET_RED_VAR,
260 VAR_NAME_ELSET_RED_VAR) != EX_NOERR) {
261 goto error_ret;
262 }
263 }
264
265 /* leave define mode */
266 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
267 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode");
268 ex_err_fn(exoid, __func__, errmsg, status);
269 EX_FUNC_LEAVE(EX_FATAL);
270 }
271
272 EX_FUNC_LEAVE(EX_NOERR);
273
274 /* Fatal error: exit definition mode and return */
275 error_ret:
276 exi_leavedef(exoid, __func__);
277 EX_FUNC_LEAVE(EX_FATAL);
278 }