Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 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 EX_FATAL, etc
11
12 static int exi_look_up_var(int exoid, ex_entity_type var_type, int var_index, ex_entity_id obj_id,
13 const char *VOBJID, const char *VOBJTAB, const char *DNUMOBJ,
14 const char *DNUMOBJVAR, int *varid)
15 {
16 int status;
17 int dimid, time_dim, numobjdim, dims[2];
18
19 int obj_id_ndx = 0;
20
21 int *obj_var_truth_tab;
22 char errmsg[MAX_ERR_LENGTH];
23
24 if (var_type == EX_ASSEMBLY) {
25 status = nc_inq_varid(exoid, VAR_ENTITY_ASSEMBLY(obj_id), varid);
26 if (status != 0) {
27 snprintf(errmsg, MAX_ERR_LENGTH,
28 "ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d",
29 ex_name_of_object(var_type), obj_id, VOBJID, exoid);
30 ex_err_fn(exoid, __func__, errmsg, status);
31 return EX_FATAL;
32 }
33 obj_id_ndx = obj_id;
34 }
35 else if (var_type == EX_BLOB) {
36 status = nc_inq_varid(exoid, VAR_ENTITY_BLOB(obj_id), varid);
37 if (status != 0) {
38 snprintf(errmsg, MAX_ERR_LENGTH,
39 "ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d",
40 ex_name_of_object(var_type), obj_id, VOBJID, exoid);
41 ex_err_fn(exoid, __func__, errmsg, status);
42 return EX_FATAL;
43 }
44 obj_id_ndx = obj_id;
45 }
46 else {
47 /* Determine index of obj_id in VOBJID array */
48 obj_id_ndx = exi_id_lkup(exoid, var_type, obj_id);
49 if (obj_id_ndx <= 0) {
50 ex_get_err(NULL, NULL, &status);
51
52 if (status != 0) {
53 if (status == EX_NULLENTITY) {
54 snprintf(errmsg, MAX_ERR_LENGTH,
55 "Warning: no variables allowed for NULL block %" PRId64 " in file id %d", obj_id,
56 exoid);
57 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
58 return EX_WARN;
59 }
60
61 snprintf(errmsg, MAX_ERR_LENGTH,
62 "ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d",
63 ex_name_of_object(var_type), obj_id, VOBJID, exoid);
64 ex_err_fn(exoid, __func__, errmsg, status);
65 return EX_FATAL;
66 }
67 }
68 }
69
70 if ((status = nc_inq_varid(exoid, exi_name_var_of_object(var_type, var_index, obj_id_ndx),
71 varid)) != NC_NOERR) {
72 if (status == NC_ENOTVAR) { /* variable doesn't exist, create it! */
73 /* check for the existence of an TNAME variable truth table */
74 if (nc_inq_varid(exoid, VOBJTAB, varid) == NC_NOERR) {
75 /* find out number of TNAMEs and TNAME variables */
76 size_t num_obj = 0;
77 size_t num_obj_var = 0;
78
79 status = exi_get_dimension(exoid, DNUMOBJ, ex_name_of_object(var_type), &num_obj, &dimid,
80 __func__);
81 if (status != NC_NOERR) {
82 return status;
83 }
84
85 status = exi_get_dimension(exoid, DNUMOBJVAR, ex_name_of_object(var_type), &num_obj_var,
86 &dimid, __func__);
87 if (status != NC_NOERR) {
88 return status;
89 }
90
91 if (!(obj_var_truth_tab = malloc(num_obj * num_obj_var * sizeof(int)))) {
92 snprintf(errmsg, MAX_ERR_LENGTH,
93 "ERROR: failed to allocate memory for %s variable "
94 "truth table in file id %d",
95 ex_name_of_object(var_type), exoid);
96 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
97 return EX_FATAL;
98 }
99
100 /* read in the TNAME variable truth table */
101 if ((status = nc_get_var_int(exoid, *varid, obj_var_truth_tab)) != NC_NOERR) {
102 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get truth table from file id %d",
103 exoid);
104 ex_err_fn(exoid, __func__, errmsg, status);
105 return EX_FATAL;
106 }
107
108 if (obj_var_truth_tab[num_obj_var * (obj_id_ndx - 1) + var_index - 1] == 0L) {
109 free(obj_var_truth_tab);
110 snprintf(
111 errmsg, MAX_ERR_LENGTH, "ERROR: Invalid %s variable %d, %s %" PRId64 " in file id %d",
112 ex_name_of_object(var_type), var_index, ex_name_of_object(var_type), obj_id, exoid);
113 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
114 return EX_FATAL;
115 }
116 free(obj_var_truth_tab);
117 }
118
119 if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) {
120 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d",
121 exoid);
122 ex_err_fn(exoid, __func__, errmsg, status);
123 goto error_ret; /* exit define mode and return */
124 }
125
126 size_t num_entity = 0;
127 exi_get_dimension(exoid, exi_dim_num_entries_in_object(var_type, obj_id_ndx),
128 ex_name_of_object(var_type), &num_entity, &numobjdim, __func__);
129
130 /* variable doesn't exist so put file into define mode */
131 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
132 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
133 ex_err_fn(exoid, __func__, errmsg, status);
134 return EX_FATAL;
135 }
136
137 /* define netCDF variable to store TNAME variable values */
138 dims[0] = time_dim;
139 dims[1] = numobjdim;
140 if ((status = nc_def_var(exoid, exi_name_var_of_object(var_type, var_index, obj_id_ndx),
141 nc_flt_code(exoid), 2, dims, varid)) != NC_NOERR) {
142 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s variable %d in file id %d",
143 ex_name_of_object(var_type), var_index, exoid);
144 ex_err_fn(exoid, __func__, errmsg, status);
145 goto error_ret;
146 }
147 exi_compress_variable(exoid, *varid, 2);
148
149 /* leave define mode */
150 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
151 return EX_FATAL;
152 }
153 }
154 else {
155 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s variable %s in file id %d",
156 ex_name_of_object(var_type), exi_name_var_of_object(var_type, var_index, obj_id_ndx),
157 exoid);
158 ex_err_fn(exoid, __func__, errmsg, status);
159 return EX_FATAL;
160 }
161 }
162 return EX_NOERR;
163
164 /* Fatal error: exit definition mode and return */
165 error_ret:
166 exi_leavedef(exoid, __func__);
167 return EX_FATAL;
168 }
169
170 /*!
171 \ingroup ResultsData
172 writes the values of a single variable of the specified type for a
173 single time step. The function ex_put_variable_param() must be invoked
174 before this call is made.
175
176 Because variables are floating point values, the application
177 code must declare the array passed to be the appropriate type
178 (float or double) to match the compute word size passed in
179 ex_create() or ex_open().
180
181 \return In case of an error, ex_put_var() returns a negative number; a
182 warning will return a positive number. Possible causes of errors
183 include:
184 - data file not properly opened with call to ex_create() or ex_open()
185 - data file opened for read only.
186 - data file not initialized properly with call to ex_put_init().
187 - ex_put_variable_param() not called previously specifying the number of
188 variables.
189
190 \param[in] exoid
191 exodus file ID returned from a previous call to
192 ex_create() or ex_open().
193
194 \param[in] var_type
195 type (edge block, face block, edge set, ... )
196
197 \param[in] var_index
198 The index of the variable. The first variable has an index of 1.
199
200 \param[in] obj_id
201 entity block/set id (ignored for global and nodal variables)
202
203 \param[in] num_entries_this_obj
204 The number of items in this block/set
205
206 \param[in] beg_time_step
207 The beginning time step number, as described under ex_put_time(). This
208 is essentially a counter that is incremented when results variables
209 are output. The first time step is 1.
210
211 \param[in] end_time_step
212 The last step number to put values. The first time step is 1.
213
214 \param[in] var_vals
215 Array of `num_entries_this_obj` values of the `var_index`-th variable for the requested time
216 step(s).
217
218 As an example, the following code segment writes all the nodal
219 variables for a single time step:
220
221 ~~~{.c}
222 int num_nod_vars, num_nodes, error, exoid, beg_time_step, end_time_step;
223 float *nodal_var_vals;
224
225 \comment{write nodal variables}
226 nodal_var_vals = (float *) calloc(num_nodes*num_steps, sizeof(float));
227 for (k=1; k <= num_nod_vars; k++) {
228 for (t=1, t < num_steps; t++) {
229 for (j=0; j < num_nodes; j++) {
230 \comment{application code fills in this array}
231 nodal_var_vals[j] = 10.0;
232 }
233 }
234 error = ex_put_var(exoid, EX_NODAL, 0, k, num_nodes,
235 1, 1+num_steps, nodal_var_vals);
236 }
237 ~~~
238 */
239
240 int ex_put_var_multi_time(int exoid, ex_entity_type var_type, int var_index, ex_entity_id obj_id,
241 int64_t num_entries_this_obj, int beg_time_step, int end_time_step,
242 const void *var_vals)
243 {
244 int varid;
245 size_t start[2], count[2];
246 int status;
247 char errmsg[MAX_ERR_LENGTH];
248
249 EX_FUNC_ENTER();
250
251 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
252 EX_FUNC_LEAVE(EX_FATAL);
253 }
254
255 switch (var_type) {
256 case EX_GLOBAL:
257 if (num_entries_this_obj <= 0) {
258 snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no global variables specified for file id %d",
259 exoid);
260 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
261
262 EX_FUNC_LEAVE(EX_WARN);
263 }
264
265 /* inquire previously defined variable */
266 if ((status = nc_inq_varid(exoid, VAR_GLO_VAR, &varid)) != NC_NOERR) {
267 if (status == NC_ENOTVAR) {
268 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no global variables defined in file id %d", exoid);
269 ex_err_fn(exoid, __func__, errmsg, status);
270 }
271 else {
272 snprintf(errmsg, MAX_ERR_LENGTH,
273 "ERROR: failed to get global variables parameters in file id %d", exoid);
274 ex_err_fn(exoid, __func__, errmsg, status);
275 }
276 EX_FUNC_LEAVE(EX_FATAL);
277 }
278 break;
279 case EX_NODAL:
280 status = exi_put_nodal_var_time(exoid, var_index, num_entries_this_obj, beg_time_step,
281 end_time_step, var_vals);
282 EX_FUNC_LEAVE(status);
283 case EX_ASSEMBLY:
284 status = exi_look_up_var(exoid, var_type, var_index, obj_id, "", VAR_ASSEMBLY_TAB,
285 DIM_NUM_ASSEMBLY, DIM_NUM_ASSEMBLY_VAR, &varid);
286 break;
287 case EX_BLOB:
288 status = exi_look_up_var(exoid, var_type, var_index, obj_id, "", VAR_BLOB_TAB, DIM_NUM_BLOB,
289 DIM_NUM_BLOB_VAR, &varid);
290 break;
291 case EX_EDGE_BLOCK:
292 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_ID_ED_BLK, VAR_EBLK_TAB,
293 DIM_NUM_ED_BLK, DIM_NUM_EDG_VAR, &varid);
294 break;
295 case EX_FACE_BLOCK:
296 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_ID_FA_BLK, VAR_FBLK_TAB,
297 DIM_NUM_FA_BLK, DIM_NUM_FAC_VAR, &varid);
298 break;
299 case EX_ELEM_BLOCK:
300 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_ID_EL_BLK, VAR_ELEM_TAB,
301 DIM_NUM_EL_BLK, DIM_NUM_ELE_VAR, &varid);
302 break;
303 case EX_NODE_SET:
304 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_NS_IDS, VAR_NSET_TAB,
305 DIM_NUM_NS, DIM_NUM_NSET_VAR, &varid);
306 break;
307 case EX_EDGE_SET:
308 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_ES_IDS, VAR_ESET_TAB,
309 DIM_NUM_ES, DIM_NUM_ESET_VAR, &varid);
310 break;
311 case EX_FACE_SET:
312 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_FS_IDS, VAR_FSET_TAB,
313 DIM_NUM_FS, DIM_NUM_FSET_VAR, &varid);
314 break;
315 case EX_SIDE_SET:
316 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_SS_IDS, VAR_SSET_TAB,
317 DIM_NUM_SS, DIM_NUM_SSET_VAR, &varid);
318 break;
319 case EX_ELEM_SET:
320 status = exi_look_up_var(exoid, var_type, var_index, obj_id, VAR_ELS_IDS, VAR_ELSET_TAB,
321 DIM_NUM_ELS, DIM_NUM_ELSET_VAR, &varid);
322 break;
323 default:
324 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: invalid variable type (%d) specified for file id %d",
325 var_type, exoid);
326 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
327 EX_FUNC_LEAVE(EX_FATAL);
328 }
329
330 if (status != EX_NOERR) {
331 EX_FUNC_LEAVE(status);
332 }
333
334 /* store element variable values */
335 start[0] = beg_time_step - 1;
336 start[1] = 0;
337
338 if (var_type == EX_GLOBAL) {
339 /* global variables may be written
340 * - all at once (by setting var_index to 1 and num_entries_this_obj to
341 * num_glob, or
342 * - one at a time (by setting var_index to the desired index and
343 * num_entries_this_obj to 1.
344 */
345 start[1] = var_index - 1;
346 }
347 count[0] = end_time_step - beg_time_step + 1;
348 count[1] = num_entries_this_obj;
349
350 if (exi_comp_ws(exoid) == 4) {
351 status = nc_put_vara_float(exoid, varid, start, count, var_vals);
352 }
353 else {
354 status = nc_put_vara_double(exoid, varid, start, count, var_vals);
355 }
356
357 if (status != NC_NOERR) {
358 snprintf(errmsg, MAX_ERR_LENGTH,
359 "ERROR: failed to store %s %" PRId64 " variable %d at steps %d to %d in file id %d",
360 ex_name_of_object(var_type), obj_id, var_index, beg_time_step, end_time_step, exoid);
361 ex_err_fn(exoid, __func__, errmsg, status);
362 EX_FUNC_LEAVE(EX_FATAL);
363 }
364
365 EX_FUNC_LEAVE(EX_NOERR);
366 }