Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_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 ex_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 exi_set_compact_storage(exoid, varid);
63 #if defined(EX_CAN_USE_NC_DEF_VAR_FILL)
64 int fill = NC_FILL_CHAR;
65 nc_def_var_fill(exoid, varid, 0, &fill);
66 #endif
67 return EX_NOERR;
68 }
69 /*! \endcond */
70
71 /*!
72 \ingroup ResultsData
73
74 The function ex_put_variable_param() writes the number of global,
75 nodal, nodeset, sideset, edge, face, or element variables that will be
76 written to the database.
77
78 \return In case of an error, ex_put_variable_param() returns a negative
79 number; a warning will return a positive number. Possible causes of
80 errors include:
81 - data file not properly opened with call to ex_create() or ex_open()
82 - data file opened for read only.
83 - invalid variable type specified.
84 - data file not initialized properly with call to ex_put_init().
85 - this routine has already been called with the same variable
86 type; redefining the number of variables is not allowed.
87 - a warning value is returned if the number of variables
88 is specified as zero.
89
90 \param[in] exoid exodus file ID returned from a previous call to ex_create()
91 or ex_open().
92 \param[in] obj_type Variable indicating the type of variable which is
93 described. Use one
94 of the #ex_entity_type types specified in the table below.
95 \param[in] num_vars The number of var_type variables that will be written to the database.
96
97 | ex_entity_type| description |
98 |---------------|---------------------------|
99 | #EX_GLOBAL | Global entity type |
100 | #EX_NODAL | Nodal entity type |
101 | #EX_NODE_SET | Node Set entity type |
102 | #EX_EDGE_BLOCK | Edge Block entity type |
103 | #EX_EDGE_SET | Edge Set entity type |
104 | #EX_FACE_BLOCK | Face Block entity type |
105 | #EX_FACE_SET | Face Set entity type |
106 | #EX_ELEM_BLOCK | Element Block entity type|
107 | #EX_ELEM_SET | Element Set entity type |
108 | #EX_SIDE_SET | Side Set entity type |
109
110 For example, the following code segment initializes the data file to
111 store global variables:
112
113 ~~~{.c}
114 int num_glo_vars, error, exoid;
115
116 \comment{write results variables parameters}
117 num_glo_vars = 3;
118
119 error = ex_put_variable_param (exoid, EX_GLOBAL, num_glo_vars);
120 ~~~
121
122 */
123
124 int ex_put_variable_param(int exoid, ex_entity_type obj_type, int num_vars)
125 {
126 int time_dim, num_nod_dim = 0, dimid, dim_str_name, varid;
127 int dims[3];
128 char errmsg[MAX_ERR_LENGTH];
129 int status;
130
131 EX_FUNC_ENTER();
132 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
133 EX_FUNC_LEAVE(EX_FATAL);
134 }
135
136 /* if no variables are to be stored, return with warning */
137 if (num_vars == 0) {
138 snprintf(errmsg, MAX_ERR_LENGTH, "Warning: zero %s variables specified for file id %d",
139 ex_name_of_object(obj_type), exoid);
140 ex_err_fn(exoid, __func__, errmsg, -EX_BADPARAM);
141
142 EX_FUNC_LEAVE(EX_WARN);
143 }
144
145 if (obj_type != EX_NODAL && obj_type != EX_NODE_SET && obj_type != EX_EDGE_BLOCK &&
146 obj_type != EX_EDGE_SET && obj_type != EX_FACE_BLOCK && obj_type != EX_FACE_SET &&
147 obj_type != EX_ELEM_BLOCK && obj_type != EX_ELEM_SET && obj_type != EX_SIDE_SET &&
148 obj_type != EX_GLOBAL && obj_type != EX_ASSEMBLY && obj_type != EX_BLOB) {
149 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type %d specified in file id %d",
150 obj_type, exoid);
151 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
152 EX_FUNC_LEAVE(EX_FATAL);
153 }
154
155 /* inquire previously defined dimensions */
156 if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) {
157 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", exoid);
158 ex_err_fn(exoid, __func__, errmsg, status);
159 EX_FUNC_LEAVE(EX_FATAL);
160 }
161
162 if (obj_type == EX_NODAL) {
163 if (nc_inq_dimid(exoid, DIM_NUM_NODES, &num_nod_dim) != NC_NOERR) {
164 EX_FUNC_LEAVE(EX_NOERR); /* Probably no nodes on database (e.g., badly
165 load-balanced parallel run) */
166 }
167 }
168
169 if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name)) < 0) {
170 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get name string length in file id %d",
171 exoid);
172 ex_err_fn(exoid, __func__, errmsg, status);
173 EX_FUNC_LEAVE(EX_FATAL);
174 }
175
176 /* put file into define mode */
177 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
178 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
179 ex_err_fn(exoid, __func__, errmsg, status);
180 EX_FUNC_LEAVE(EX_FATAL);
181 }
182
183 /* define dimensions and variables */
184 if (obj_type == EX_GLOBAL) {
185 if (ex_prepare_result_var(exoid, num_vars, "global", DIM_NUM_GLO_VAR, VAR_NAME_GLO_VAR) !=
186 EX_NOERR) {
187 goto error_ret;
188 }
189
190 if ((status = nc_inq_dimid(exoid, DIM_NUM_GLO_VAR, &dimid)) != NC_NOERR) {
191 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get global variable count in file id %d",
192 exoid);
193 ex_err_fn(exoid, __func__, errmsg, status);
194 EX_FUNC_LEAVE(EX_FATAL);
195 }
196 dims[0] = time_dim;
197 dims[1] = dimid;
198 if ((status = nc_def_var(exoid, VAR_GLO_VAR, nc_flt_code(exoid), 2, dims, &varid)) !=
199 NC_NOERR) {
200 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define global variables in file id %d",
201 exoid);
202 ex_err_fn(exoid, __func__, errmsg, status);
203 goto error_ret; /* exit define mode and return */
204 }
205 exi_compress_variable(exoid, varid, 2);
206 if (num_vars * 8 < 64 * 1024) {
207 exi_set_compact_storage(exoid, varid);
208 }
209 }
210 else if (obj_type == EX_NODAL) {
211 if (ex_prepare_result_var(exoid, num_vars, "nodal", DIM_NUM_NOD_VAR, VAR_NAME_NOD_VAR) !=
212 EX_NOERR) {
213 goto error_ret;
214 }
215
216 int i;
217 for (i = 1; i <= num_vars; i++) {
218 dims[0] = time_dim;
219 dims[1] = num_nod_dim;
220 if ((status = nc_def_var(exoid, VAR_NOD_VAR_NEW(i), nc_flt_code(exoid), 2, dims, &varid)) !=
221 NC_NOERR) {
222 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define nodal variable %d in file id %d",
223 i, exoid);
224 ex_err_fn(exoid, __func__, errmsg, status);
225 goto error_ret; /* exit define mode and return */
226 }
227 exi_compress_variable(exoid, varid, 2);
228 }
229 }
230
231 /* netCDF variables in which to store the EXODUS obj_type variable values will
232 * be defined in ex_put_*_var_tab or ex_put_*_var; at this point, we
233 * don't know what obj_type variables are valid for which obj_type blocks
234 * (the info that is stored in the obj_type variable truth table)
235 */
236 else if (obj_type == EX_ELEM_BLOCK) {
237 if (ex_prepare_result_var(exoid, num_vars, "element", DIM_NUM_ELE_VAR, VAR_NAME_ELE_VAR) !=
238 EX_NOERR) {
239 goto error_ret;
240 }
241 }
242 else if (obj_type == EX_NODE_SET) {
243 if (ex_prepare_result_var(exoid, num_vars, "nodeset", DIM_NUM_NSET_VAR, VAR_NAME_NSET_VAR) !=
244 EX_NOERR) {
245 goto error_ret;
246 }
247 }
248 else if (obj_type == EX_SIDE_SET) {
249 if (ex_prepare_result_var(exoid, num_vars, "sideset", DIM_NUM_SSET_VAR, VAR_NAME_SSET_VAR) !=
250 EX_NOERR) {
251 goto error_ret;
252 }
253 }
254 else if (obj_type == EX_ASSEMBLY) {
255 if (ex_prepare_result_var(exoid, num_vars, "assembly", DIM_NUM_ASSEMBLY_VAR,
256 VAR_NAME_ASSEMBLY_VAR) != EX_NOERR) {
257 goto error_ret;
258 }
259 }
260 else if (obj_type == EX_BLOB) {
261 if (ex_prepare_result_var(exoid, num_vars, "blob", DIM_NUM_BLOB_VAR, VAR_NAME_BLOB_VAR) !=
262 EX_NOERR) {
263 goto error_ret;
264 }
265 }
266 else if (obj_type == EX_EDGE_BLOCK) {
267 if (ex_prepare_result_var(exoid, num_vars, "edge", DIM_NUM_EDG_VAR, VAR_NAME_EDG_VAR) !=
268 EX_NOERR) {
269 goto error_ret;
270 }
271 }
272 else if (obj_type == EX_FACE_BLOCK) {
273 if (ex_prepare_result_var(exoid, num_vars, "face", DIM_NUM_FAC_VAR, VAR_NAME_FAC_VAR) !=
274 EX_NOERR) {
275 goto error_ret;
276 }
277 }
278 else if (obj_type == EX_EDGE_SET) {
279 if (ex_prepare_result_var(exoid, num_vars, "edgeset", DIM_NUM_ESET_VAR, VAR_NAME_ESET_VAR) !=
280 EX_NOERR) {
281 goto error_ret;
282 }
283 }
284 else if (obj_type == EX_FACE_SET) {
285 if (ex_prepare_result_var(exoid, num_vars, "faceset", DIM_NUM_FSET_VAR, VAR_NAME_FSET_VAR) !=
286 EX_NOERR) {
287 goto error_ret;
288 }
289 }
290 else if (obj_type == EX_ELEM_SET) {
291 if (ex_prepare_result_var(exoid, num_vars, "elementset", DIM_NUM_ELSET_VAR,
292 VAR_NAME_ELSET_VAR) != EX_NOERR) {
293 goto error_ret;
294 }
295 }
296
297 /* leave define mode */
298 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
299 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode");
300 ex_err_fn(exoid, __func__, errmsg, status);
301 EX_FUNC_LEAVE(EX_FATAL);
302 }
303
304 EX_FUNC_LEAVE(EX_NOERR);
305
306 /* Fatal error: exit definition mode and return */
307 error_ret:
308 exi_leavedef(exoid, __func__);
309 EX_FUNC_LEAVE(EX_FATAL);
310 }