Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020 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, ex_name_of_object, etc
10 #include "exodusII_int.h" // for exi_check_valid_file_id, etc
11
12 /*!
13 \ingroup ResultsData
14
15 The function ex_get_reduction_vars() reads the values of the selected
16 entity reduction variables for a single time step. Memory must be
17 allocated for the variables values array before this function is
18 invoked.
19
20 Because variables are floating point values, the application
21 code must declare the array passed to be the appropriate type
22 (float or double) to match the compute word size passed in
23 ex_create() or ex_open().
24
25 In case of an error, ex_get_reduction_vars() returns a negative
26 number; a warning will return a positive number. Possible causes of
27 errors include:
28
29 - data file not properly opened with call to ex_create() or ex_open()
30 - variable does not exist for the desired block or set.
31 - invalid block or set.
32 - no variables of the selected type stored in the file.
33 - a warning value is returned if no variables of the selected entity type are stored in the file.
34
35 \param[in] exoid exodus file ID returned from a previous call to
36 ex_create() or ex_open().
37
38 \param[in] time_step The time step, as described under ex_put_time(), at
39 which the variable values are desired. This is
40 essentially an index (in the time dimension) into the entity
41 variable values array stored in the database. The first time step is 1.
42
43 \param[in] var_type block/variable type node, edge/face/element block, or
44 node/edge/face/side/element set of type ex_entity_type.
45 \param[in] obj_id object id, see ex_get_ids()
46 \param[in] num_variables The number of reduction variables in this object stored in the database.
47 \param[out] var_vals Returned array of num_variables variable values
48 for the time_step'th time step.
49
50 The following is an example code segment that reads the element reduction variables for element
51 block with id 100 at time step 5. There are 'num_variables' reduction variables for element block
52 100.
53
54 ~~~{.c}
55 int num_variables, error, time_step;
56 int blk_id = 100;
57 float *var_values;
58
59 var_values = (float *) callo(num_variables, sizeof(float));
60 error = ex_get_reduction_vars (idexo, time_step, EX_ELEM_BLOCK, blk_id,
61 num_variables, var_values);
62 ~~~
63
64 */
65
66 int ex_get_reduction_vars(int exoid, int time_step, ex_entity_type var_type, ex_entity_id obj_id,
67 int64_t num_variables, void *var_vals)
68 {
69 int status;
70 int varid, obj_id_ndx;
71 size_t start[2], count[2];
72 char errmsg[MAX_ERR_LENGTH];
73
74 EX_FUNC_ENTER();
75 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
76 EX_FUNC_LEAVE(EX_FATAL);
77 }
78
79 if (var_type == EX_GLOBAL) {
80 /* FIXME: Special case: all vars stored in 2-D single array. */
81 status = exi_get_glob_vars(exoid, time_step, num_variables, var_vals);
82 EX_FUNC_LEAVE(status);
83 }
84
85 /* Determine index of obj_id in VAR_ID_XXX array */
86 obj_id_ndx = exi_id_lkup(exoid, var_type, obj_id);
87 if (obj_id_ndx <= 0) {
88 ex_get_err(NULL, NULL, &status);
89
90 if (status != 0) {
91 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: could not find %s %" PRId64 " in file id %d",
92 ex_name_of_object(var_type), obj_id, exoid);
93 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
94 EX_FUNC_LEAVE(EX_FATAL);
95 }
96 }
97
98 /* inquire previously defined variable */
99 if ((status = nc_inq_varid(exoid, exi_name_red_var_of_object(var_type, obj_id_ndx), &varid)) !=
100 NC_NOERR) {
101 snprintf(errmsg, MAX_ERR_LENGTH,
102 "Warning: no reduction variables for %s %" PRId64 " in file id %d",
103 ex_name_of_object(var_type), obj_id, exoid);
104 ex_err_fn(exoid, __func__, errmsg, status);
105 EX_FUNC_LEAVE(EX_WARN);
106 }
107
108 /* read values of reduction variables */
109 start[0] = --time_step;
110 start[1] = 0;
111
112 count[0] = 1;
113 count[1] = num_variables;
114
115 if (exi_comp_ws(exoid) == 4) {
116 status = nc_get_vara_float(exoid, varid, start, count, var_vals);
117 }
118 else {
119 status = nc_get_vara_double(exoid, varid, start, count, var_vals);
120 }
121
122 if (status != NC_NOERR) {
123 snprintf(errmsg, MAX_ERR_LENGTH,
124 "ERROR: failed to get reduction variable values for %s %" PRId64 " in file id %d",
125 ex_name_of_object(var_type), obj_id, exoid);
126 ex_err_fn(exoid, __func__, errmsg, status);
127 EX_FUNC_LEAVE(EX_FATAL);
128 }
129 EX_FUNC_LEAVE(EX_NOERR);
130 }