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