| 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, etc |
| 10 |
#include "exodusII_int.h" // for EX_WARN, exi_comp_ws, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
The function ex_get_nodal_var_time() reads the values of a nodal |
| 14 |
variable for a single node through a specified number of time |
| 15 |
steps. Memory must be allocated for the nodal variable values array |
| 16 |
before this function is invoked. |
| 17 |
|
| 18 |
Because nodal variables are floating point values, the application |
| 19 |
code must declare the array passed to be the appropriate type |
| 20 |
(float or ``double'') to match the compute word size passed in |
| 21 |
ex_create() or ex_open(). |
| 22 |
|
| 23 |
\return In case of an error, ex_get_nodal_var_time() returns a |
| 24 |
negative number; a warning will return a positive number. Possible |
| 25 |
causes of errors include: |
| 26 |
- specified nodal variable does not exist. |
| 27 |
- a warning value is returned if no nodal variables are stored in the file. |
| 28 |
|
| 29 |
\param[in] exoid exodus file ID returned from a previous call to |
| 30 |
ex_create() or |
| 31 |
ex_open(). |
| 32 |
|
| 33 |
\param[in] nodal_var_index The index of the desired nodal variable. The first |
| 34 |
variable has an |
| 35 |
index of 1. |
| 36 |
|
| 37 |
\param[in] node_number The internal ID (see Section LocalNodeIds) of the |
| 38 |
desired |
| 39 |
node. The first node is 1. |
| 40 |
|
| 41 |
\param[in] beg_time_step The beginning time step for which a nodal variable |
| 42 |
value |
| 43 |
is desired. This is not a time value but rather a |
| 44 |
time step number, |
| 45 |
as described under ex_put_time(). The first time |
| 46 |
step is 1. |
| 47 |
|
| 48 |
\param[in] end_time_step The last time step for which a nodal variable value |
| 49 |
is desired. If |
| 50 |
negative, the last time step in the database will |
| 51 |
be used. The first |
| 52 |
time step is 1. |
| 53 |
|
| 54 |
\param[out] nodal_var_vals Returned array of(end_time_step {-} |
| 55 |
beg_time_step +1) values |
| 56 |
of the node_number-th node for the |
| 57 |
nodal_var_index-th nodal |
| 58 |
variable. |
| 59 |
|
| 60 |
For example, the following code segment will read the values of the |
| 61 |
first nodal variable for node number one for all time steps stored in |
| 62 |
the data file: |
| 63 |
|
| 64 |
~~~{.c} |
| 65 |
int num_time_steps, var_index, node_num, beg_time, end_time, error, exoid; |
| 66 |
float *var_values; |
| 67 |
|
| 68 |
\comment{determine how many time steps are stored} |
| 69 |
num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME); |
| 70 |
|
| 71 |
\comment{read a nodal variable through time} |
| 72 |
var_values = (float *) calloc (num_time_steps, sizeof(float)); |
| 73 |
|
| 74 |
var_index = 1; node_num = 1; beg_time = 1; end_time = -1; |
| 75 |
error = ex_get_var_time(exoid, EX_NODAL, var_index, node_num, beg_time, |
| 76 |
end_time, var_values); |
| 77 |
|
| 78 |
~~~ |
| 79 |
*/ |
| 80 |
|
| 81 |
int exi_get_nodal_var_time(int exoid, int nodal_var_index, int64_t node_number, int beg_time_step, |
| 82 |
int end_time_step, void *nodal_var_vals) |
| 83 |
{ |
| 84 |
int status; |
| 85 |
int varid; |
| 86 |
size_t start[3], count[3]; |
| 87 |
char errmsg[MAX_ERR_LENGTH]; |
| 88 |
|
| 89 |
EX_FUNC_ENTER(); |
| 90 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 91 |
EX_FUNC_LEAVE(EX_FATAL); |
| 92 |
} |
| 93 |
/* Check that times are in range */ |
| 94 |
{ |
| 95 |
int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME); |
| 96 |
|
| 97 |
if (num_time_steps == 0) { |
| 98 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: there are no time_steps on the file id %d", exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 100 |
EX_FUNC_LEAVE(EX_FATAL); |
| 101 |
} |
| 102 |
|
| 103 |
if (beg_time_step <= 0 || beg_time_step > num_time_steps) { |
| 104 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 105 |
"ERROR: beginning time_step is out-of-range. Value = %d, " |
| 106 |
"valid range is 1 to %d in file id %d", |
| 107 |
beg_time_step, num_time_steps, exoid); |
| 108 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 109 |
EX_FUNC_LEAVE(EX_FATAL); |
| 110 |
} |
| 111 |
|
| 112 |
if (end_time_step < 0) { |
| 113 |
/* user is requesting the maximum time step; we find this out using the |
| 114 |
* database inquire function to get the number of time steps; the ending |
| 115 |
* time step number is 1 less due to 0 based array indexing in C |
| 116 |
*/ |
| 117 |
end_time_step = num_time_steps; |
| 118 |
} |
| 119 |
else if (end_time_step < beg_time_step || end_time_step > num_time_steps) { |
| 120 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 121 |
"ERROR: end time_step is out-of-range. Value = %d, valid " |
| 122 |
"range is %d to %d in file id %d", |
| 123 |
beg_time_step, end_time_step, num_time_steps, exoid); |
| 124 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 125 |
EX_FUNC_LEAVE(EX_FATAL); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
beg_time_step--; |
| 130 |
end_time_step--; |
| 131 |
node_number--; |
| 132 |
|
| 133 |
if (ex_large_model(exoid) == 0) { |
| 134 |
/* read values of the nodal variable; |
| 135 |
* assume node number is 1-based (first node is numbered 1); adjust |
| 136 |
* so it is 0-based |
| 137 |
*/ |
| 138 |
if ((status = nc_inq_varid(exoid, VAR_NOD_VAR, &varid)) != NC_NOERR) { |
| 139 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: could not find nodal variable %d in file id %d", |
| 140 |
nodal_var_index, exoid); |
| 141 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 142 |
EX_FUNC_LEAVE(EX_WARN); |
| 143 |
} |
| 144 |
|
| 145 |
start[0] = beg_time_step; |
| 146 |
start[1] = --nodal_var_index; |
| 147 |
start[2] = node_number; |
| 148 |
|
| 149 |
count[0] = end_time_step - beg_time_step + 1; |
| 150 |
count[1] = 1; |
| 151 |
count[2] = 1; |
| 152 |
} |
| 153 |
else { |
| 154 |
if ((status = nc_inq_varid(exoid, VAR_NOD_VAR_NEW(nodal_var_index), &varid)) != NC_NOERR) { |
| 155 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: could not find nodal variable %d in file id %d", |
| 156 |
nodal_var_index, exoid); |
| 157 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 158 |
EX_FUNC_LEAVE(EX_WARN); |
| 159 |
} |
| 160 |
|
| 161 |
/* read values of the nodal variable; |
| 162 |
* assume node number is 1-based (first node is numbered 1); adjust |
| 163 |
* so it is 0-based |
| 164 |
*/ |
| 165 |
|
| 166 |
start[0] = beg_time_step; |
| 167 |
start[1] = node_number; |
| 168 |
|
| 169 |
count[0] = end_time_step - beg_time_step + 1; |
| 170 |
count[1] = 1; |
| 171 |
} |
| 172 |
|
| 173 |
if (exi_comp_ws(exoid) == 4) { |
| 174 |
status = nc_get_vara_float(exoid, varid, start, count, nodal_var_vals); |
| 175 |
} |
| 176 |
else { |
| 177 |
status = nc_get_vara_double(exoid, varid, start, count, nodal_var_vals); |
| 178 |
} |
| 179 |
|
| 180 |
if (status != NC_NOERR) { |
| 181 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get nodal variables in file id %d", exoid); |
| 182 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 183 |
EX_FUNC_LEAVE(EX_FATAL); |
| 184 |
} |
| 185 |
EX_FUNC_LEAVE(EX_NOERR); |
| 186 |
} |