Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_get_var_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 *
10 * exgvart - ex_get_var_time
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int var_type variable type global, nodal,
16 * edge/face/elem block,
17 * node/edge/face/side/elem set
18 * int var_index element variable index
19 * int id entry number
20 * int beg_time_step time step number
21 * int end_time_step time step number
22 *
23 * exit conditions -
24 * float* var_vals array of element variable values
25 *
26 * revision history -
27 * 20061002 - David Thompson - Adapted from ex_get_var_time
28 *
29 *
30 *****************************************************************************/
31
32 #include "exodusII.h"
33 #include "exodusII_int.h"
34
35 /*!
36 \ingroup ResultsData
37
38 * reads the values of a variable for a single entry of an object (block or set)
39 * through a
40 * specified number of time steps in the database; assume the first
41 * variable index, entry number, and time step are 1
42 */
43
44 int ex_get_var_time(int exoid, ex_entity_type var_type, int var_index, int64_t id,
45 int beg_time_step, int end_time_step, void *var_vals)
46 {
47 int dimid, varid;
48 int status;
49 int *stat_vals = NULL;
50 int64_t numel = 0;
51 size_t num_obj, i;
52 size_t num_entries_this_obj = 0;
53 size_t start[2], count[2];
54 char errmsg[MAX_ERR_LENGTH];
55 const char *varobjids;
56 const char *varobstat;
57
58 EX_FUNC_ENTER();
59 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
60 EX_FUNC_LEAVE(EX_FATAL);
61 }
62
63 switch (var_type) {
64 case EX_GLOBAL:
65 status = exi_get_glob_var_time(exoid, var_index, beg_time_step, end_time_step, var_vals);
66 EX_FUNC_LEAVE(status);
67 case EX_NODAL:
68 status = exi_get_nodal_var_time(exoid, var_index, id, beg_time_step, end_time_step, var_vals);
69 EX_FUNC_LEAVE(status);
70 case EX_EDGE_BLOCK:
71 varobjids = VAR_ID_ED_BLK;
72 varobstat = VAR_STAT_ED_BLK;
73 break;
74 case EX_FACE_BLOCK:
75 varobjids = VAR_ID_FA_BLK;
76 varobstat = VAR_STAT_FA_BLK;
77 break;
78 case EX_ELEM_BLOCK:
79 varobjids = VAR_ID_EL_BLK;
80 varobstat = VAR_STAT_EL_BLK;
81 break;
82 case EX_NODE_SET:
83 varobjids = VAR_NS_IDS;
84 varobstat = VAR_NS_STAT;
85 break;
86 case EX_EDGE_SET:
87 varobjids = VAR_ES_IDS;
88 varobstat = VAR_ES_STAT;
89 break;
90 case EX_FACE_SET:
91 varobjids = VAR_FS_IDS;
92 varobstat = VAR_FS_STAT;
93 break;
94 case EX_SIDE_SET:
95 varobjids = VAR_SS_IDS;
96 varobstat = VAR_SS_STAT;
97 break;
98 case EX_ELEM_SET:
99 varobjids = VAR_ELS_IDS;
100 varobstat = VAR_ELS_STAT;
101 break;
102 default:
103 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type (%d) specified for file id %d",
104 var_type, exoid);
105 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
106 EX_FUNC_LEAVE(EX_FATAL);
107 }
108
109 /* assume entry number is 1-based (the first entry of an object is 1, not 0);
110 * adjust so it is 0-based
111 */
112 id--;
113
114 /* find what object the entry is in */
115
116 /* first, find out how many objects there are */
117 status = exi_get_dimension(exoid, exi_dim_num_objects(var_type), ex_name_of_object(var_type),
118 &num_obj, &dimid, __func__);
119 if (status != NC_NOERR) {
120 EX_FUNC_LEAVE(status);
121 }
122
123 /* get the array of object ids */
124 /* don't think we need this anymore since the netcdf variable names
125 associated with objects don't contain the object ids */
126
127 if ((status = nc_inq_varid(exoid, varobjids, &varid)) != NC_NOERR) {
128 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids in file id %d",
129 ex_name_of_object(var_type), exoid);
130 ex_err_fn(exoid, __func__, errmsg, status);
131 EX_FUNC_LEAVE(EX_FATAL);
132 }
133
134 /* allocate space for stat array */
135 if (!(stat_vals = calloc(num_obj, sizeof(int)))) {
136 snprintf(errmsg, MAX_ERR_LENGTH,
137 "ERROR: failed to allocate memory for %s status array for file id %d",
138 ex_name_of_object(var_type), exoid);
139 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
140 EX_FUNC_LEAVE(EX_FATAL);
141 }
142
143 /* get variable id of status array */
144 if (nc_inq_varid(exoid, varobstat, &varid) == NC_NOERR) {
145 /* if status array exists, use it, otherwise assume, object exists
146 to be backward compatible */
147
148 if ((status = nc_get_var_int(exoid, varid, stat_vals)) != NC_NOERR) {
149 free(stat_vals);
150 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s status array from file id %d",
151 ex_name_of_object(var_type), exoid);
152 ex_err_fn(exoid, __func__, errmsg, status);
153 EX_FUNC_LEAVE(EX_FATAL);
154 }
155 }
156 else { /* default: status is true */
157 for (i = 0; i < num_obj; i++) {
158 stat_vals[i] = 1;
159 }
160 }
161
162 /* loop through each object until id is found; since entry
163 * numbers are sequential (beginning with 1) id is in obj_i
164 * when id_first_i <= id <= id_last_i, where
165 * id_first_i is the entry number of the first entry in
166 * obj_i and id_last_i is the entry number of the last
167 * entry in obj_i
168 */
169
170 i = 0;
171 if (stat_vals[i] != 0) {
172 if ((status = nc_inq_dimid(exoid, exi_dim_num_entries_in_object(var_type, i + 1), &dimid)) !=
173 NC_NOERR) {
174 snprintf(errmsg, MAX_ERR_LENGTH,
175 "ERROR: failed to locate number of entries in %zuth %s in file id %d", i,
176 ex_name_of_object(var_type), exoid);
177 ex_err_fn(exoid, __func__, errmsg, status);
178 free(stat_vals);
179 EX_FUNC_LEAVE(EX_FATAL);
180 }
181
182 if ((status = nc_inq_dimlen(exoid, dimid, &num_entries_this_obj)) != NC_NOERR) {
183 snprintf(errmsg, MAX_ERR_LENGTH,
184 "ERROR: failed to get number of entries in %zuth %s in file id %d", i,
185 ex_name_of_object(var_type), exoid);
186 ex_err_fn(exoid, __func__, errmsg, status);
187 free(stat_vals);
188 EX_FUNC_LEAVE(EX_FATAL);
189 }
190 } /* End NULL object check */
191
192 numel = num_entries_this_obj;
193
194 while (numel <= id) {
195 if (stat_vals[++i] != 0) {
196 if ((status = nc_inq_dimid(exoid, exi_dim_num_entries_in_object(var_type, i + 1), &dimid)) !=
197 NC_NOERR) {
198 snprintf(errmsg, MAX_ERR_LENGTH,
199 "ERROR: failed to locate number of entries in %zuth %s in file id %d", i,
200 ex_name_of_object(var_type), exoid);
201 ex_err_fn(exoid, __func__, errmsg, status);
202 free(stat_vals);
203 EX_FUNC_LEAVE(EX_FATAL);
204 }
205
206 if ((status = nc_inq_dimlen(exoid, dimid, &num_entries_this_obj)) != NC_NOERR) {
207 snprintf(errmsg, MAX_ERR_LENGTH,
208 "ERROR: failed to get number of entries in %zuth %s in file id %d", i,
209 ex_name_of_object(var_type), exoid);
210 ex_err_fn(exoid, __func__, errmsg, status);
211 free(stat_vals);
212 EX_FUNC_LEAVE(EX_FATAL);
213 }
214 numel += num_entries_this_obj;
215 }
216 }
217 size_t offset = id - (numel - num_entries_this_obj);
218
219 /* inquire previously defined variable */
220 if ((status = nc_inq_varid(exoid, exi_name_var_of_object(var_type, var_index, i + 1), &varid)) !=
221 NC_NOERR) {
222 snprintf(errmsg, MAX_ERR_LENGTH,
223 "ERROR: failed to locate variable %zu for %dth %s in file id %d", i, var_index,
224 ex_name_of_object(var_type), exoid);
225 ex_err_fn(exoid, __func__, errmsg, status);
226 free(stat_vals);
227 EX_FUNC_LEAVE(EX_FATAL);
228 }
229
230 free(stat_vals);
231
232 /* Check that times are in range */
233 {
234 int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME);
235
236 if (num_time_steps == 0) {
237 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: there are no time_steps on the file id %d", exoid);
238 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
239 EX_FUNC_LEAVE(EX_FATAL);
240 }
241
242 if (beg_time_step <= 0 || beg_time_step > num_time_steps) {
243 snprintf(errmsg, MAX_ERR_LENGTH,
244 "ERROR: beginning time_step is out-of-range. Value = %d, "
245 "valid range is 1 to %d in file id %d",
246 beg_time_step, num_time_steps, exoid);
247 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
248 EX_FUNC_LEAVE(EX_FATAL);
249 }
250
251 if (end_time_step < 0) {
252 /* user is requesting the maximum time step; we find this out using the
253 * database inquire function to get the number of time steps; the ending
254 * time step number is 1 less due to 0 based array indexing in C
255 */
256 end_time_step = ex_inquire_int(exoid, EX_INQ_TIME);
257 }
258 else if (end_time_step < beg_time_step || end_time_step > num_time_steps) {
259 snprintf(errmsg, MAX_ERR_LENGTH,
260 "ERROR: end time_step is out-of-range. Value = %d, valid "
261 "range is %d to %d in file id %d",
262 beg_time_step, end_time_step, num_time_steps, exoid);
263 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
264 EX_FUNC_LEAVE(EX_FATAL);
265 }
266 }
267
268 /* read values of object variable */
269 start[0] = --beg_time_step;
270 start[1] = offset;
271
272 end_time_step--;
273
274 count[0] = end_time_step - beg_time_step + 1;
275 count[1] = 1;
276
277 if (exi_comp_ws(exoid) == 4) {
278 status = nc_get_vara_float(exoid, varid, start, count, var_vals);
279 }
280 else {
281 status = nc_get_vara_double(exoid, varid, start, count, var_vals);
282 }
283
284 if (status != NC_NOERR) {
285 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s variable values in file id %d",
286 ex_name_of_object(var_type), exoid);
287 ex_err_fn(exoid, __func__, errmsg, status);
288 EX_FUNC_LEAVE(EX_FATAL);
289 }
290 EX_FUNC_LEAVE(EX_NOERR);
291 }