| 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_FATAL, exi_comp_ws, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
\ingroup ResultsData |
| 14 |
The function ex_get_time() reads the time value for a specified time |
| 15 |
step. |
| 16 |
|
| 17 |
Because time values are floating point values, the application code |
| 18 |
must declare the array passed to be the appropriate type (float or |
| 19 |
double) to match the compute word size passed in ex_create() or |
| 20 |
ex_open(). |
| 21 |
|
| 22 |
\return In case of an error, ex_get_time() returns a negative number; a |
| 23 |
warning will return a positive number. Possible causes of errors |
| 24 |
include: |
| 25 |
- data file not properly opened with call to ex_create() or ex_open() |
| 26 |
- no time steps have been stored in the file. |
| 27 |
|
| 28 |
\param[in] exoid exodus file ID returned from a previous call to |
| 29 |
ex_create() or ex_open(). |
| 30 |
\param[in] time_step The time step number. This is essentially an index (in |
| 31 |
the time |
| 32 |
dimension) into the global, nodal, and element variables |
| 33 |
arrays stored |
| 34 |
in the database. The first time step is 1. |
| 35 |
\param[out] time_value Returned time at the specified time step. |
| 36 |
|
| 37 |
As an example, the following coding will read the time value stored in |
| 38 |
the data file for time step n: |
| 39 |
|
| 40 |
~~~{.c} |
| 41 |
int n, error, exoid; |
| 42 |
float time_value; |
| 43 |
|
| 44 |
\comment{read time value at time step 3} |
| 45 |
n = 3; |
| 46 |
error = ex_get_time (exoid, n, &time_value); |
| 47 |
~~~ |
| 48 |
|
| 49 |
*/ |
| 50 |
|
| 51 |
int ex_get_time(int exoid, int time_step, void *time_value) |
| 52 |
{ |
| 53 |
int status; |
| 54 |
int varid; |
| 55 |
size_t start[1]; |
| 56 |
char errmsg[MAX_ERR_LENGTH]; |
| 57 |
struct exi_file_item *file = NULL; |
| 58 |
|
| 59 |
EX_FUNC_ENTER(); |
| 60 |
|
| 61 |
file = exi_find_file_item(exoid); |
| 62 |
if (!file) { |
| 63 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d.", exoid); |
| 64 |
ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID); |
| 65 |
EX_FUNC_LEAVE(EX_FATAL); |
| 66 |
} |
| 67 |
|
| 68 |
int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME); |
| 69 |
|
| 70 |
if (num_time_steps == 0) { |
| 71 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: there are no time_steps on the file id %d", exoid); |
| 72 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 73 |
EX_FUNC_LEAVE(EX_FATAL); |
| 74 |
} |
| 75 |
|
| 76 |
if (time_step <= 0) { |
| 77 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 78 |
"ERROR: time_step must be greater than 0. Entered value is %d in file id %d", |
| 79 |
time_step, exoid); |
| 80 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 81 |
EX_FUNC_LEAVE(EX_FATAL); |
| 82 |
} |
| 83 |
|
| 84 |
if (time_step > num_time_steps) { |
| 85 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 86 |
"ERROR: beginning time_step is out-of-range. Value = %d, " |
| 87 |
"valid range is 1 to %d in file id %d", |
| 88 |
time_step, num_time_steps, exoid); |
| 89 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 90 |
EX_FUNC_LEAVE(EX_FATAL); |
| 91 |
} |
| 92 |
|
| 93 |
varid = file->time_varid; |
| 94 |
if (varid < 0) { |
| 95 |
/* inquire previously defined variable */ |
| 96 |
if ((status = nc_inq_varid(exoid, VAR_WHOLE_TIME, &varid)) != NC_NOERR) { |
| 97 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time variable in file id %d", |
| 98 |
exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 100 |
EX_FUNC_LEAVE(EX_FATAL); |
| 101 |
} |
| 102 |
file->time_varid = varid; |
| 103 |
} |
| 104 |
|
| 105 |
/* read time value */ |
| 106 |
start[0] = --time_step; |
| 107 |
|
| 108 |
if (exi_comp_ws(exoid) == 4) { |
| 109 |
status = nc_get_var1_float(exoid, varid, start, time_value); |
| 110 |
} |
| 111 |
else { |
| 112 |
status = nc_get_var1_double(exoid, varid, start, time_value); |
| 113 |
} |
| 114 |
|
| 115 |
if (status != NC_NOERR) { |
| 116 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get time value in file id %d", exoid); |
| 117 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 118 |
EX_FUNC_LEAVE(EX_FATAL); |
| 119 |
} |
| 120 |
EX_FUNC_LEAVE(EX_NOERR); |
| 121 |
} |