| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 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 |
#include "exodusII.h" |
| 10 |
#include "exodusII_int.h" |
| 11 |
|
| 12 |
static int exi_look_up_var(int exoid, ex_entity_type var_type, ex_entity_id obj_id, |
| 13 |
const char *var_obj_id, const char *dim_num_obj_var, int *varid) |
| 14 |
{ |
| 15 |
int status; |
| 16 |
int obj_id_ndx; |
| 17 |
char errmsg[MAX_ERR_LENGTH]; |
| 18 |
|
| 19 |
if (var_type == EX_ASSEMBLY) { |
| 20 |
status = nc_inq_varid(exoid, VAR_ENTITY_ASSEMBLY(obj_id), varid); |
| 21 |
if (status != 0) { |
| 22 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 23 |
"ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d", |
| 24 |
ex_name_of_object(var_type), obj_id, var_obj_id, exoid); |
| 25 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 26 |
return EX_FATAL; |
| 27 |
} |
| 28 |
obj_id_ndx = obj_id; |
| 29 |
} |
| 30 |
else if (var_type == EX_BLOB) { |
| 31 |
status = nc_inq_varid(exoid, VAR_ENTITY_BLOB(obj_id), varid); |
| 32 |
if (status != 0) { |
| 33 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 34 |
"ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d", |
| 35 |
ex_name_of_object(var_type), obj_id, var_obj_id, exoid); |
| 36 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 37 |
return EX_FATAL; |
| 38 |
} |
| 39 |
obj_id_ndx = obj_id; |
| 40 |
} |
| 41 |
else { |
| 42 |
/* Determine index of obj_id in var_obj_id array */ |
| 43 |
obj_id_ndx = exi_id_lkup(exoid, var_type, obj_id); |
| 44 |
if (obj_id_ndx <= 0) { |
| 45 |
ex_get_err(NULL, NULL, &status); |
| 46 |
|
| 47 |
if (status != 0) { |
| 48 |
if (status == EX_NULLENTITY) { |
| 49 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 50 |
"Warning: no variables allowed for NULL block %" PRId64 " in file id %d", obj_id, |
| 51 |
exoid); |
| 52 |
ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY); |
| 53 |
return EX_WARN; |
| 54 |
} |
| 55 |
|
| 56 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 57 |
"ERROR: failed to locate %s id %" PRId64 " in %s array in file id %d", |
| 58 |
ex_name_of_object(var_type), obj_id, var_obj_id, exoid); |
| 59 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 60 |
return EX_FATAL; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
if ((status = nc_inq_varid(exoid, exi_name_red_var_of_object(var_type, obj_id_ndx), varid)) != |
| 65 |
NC_NOERR) { |
| 66 |
if (status == NC_ENOTVAR) { /* variable doesn't exist, create it! */ |
| 67 |
int time_dim; |
| 68 |
if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) { |
| 69 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", |
| 70 |
exoid); |
| 71 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 72 |
return EX_FATAL; |
| 73 |
} |
| 74 |
|
| 75 |
size_t num_obj_var; |
| 76 |
int numvardim; |
| 77 |
if ((status = exi_get_dimension(exoid, dim_num_obj_var, ex_name_of_object(var_type), |
| 78 |
&num_obj_var, &numvardim, __func__)) != EX_NOERR) { |
| 79 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 80 |
"ERROR: failed to inquire number of %s reduction variables in file id %d", |
| 81 |
ex_name_of_object(var_type), exoid); |
| 82 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 83 |
return EX_FATAL; |
| 84 |
} |
| 85 |
|
| 86 |
/* variable doesn't exist so put file into define mode */ |
| 87 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 88 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 89 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 90 |
return EX_FATAL; |
| 91 |
} |
| 92 |
|
| 93 |
/* define NetCDF variable to store reduction variable values */ |
| 94 |
int dims[] = {time_dim, numvardim}; |
| 95 |
if ((status = nc_def_var(exoid, exi_name_red_var_of_object(var_type, obj_id_ndx), |
| 96 |
nc_flt_code(exoid), 2, dims, varid)) != NC_NOERR) { |
| 97 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s in file id %d", |
| 98 |
ex_name_of_object(var_type), exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 100 |
exi_leavedef(exoid, __func__); |
| 101 |
return EX_FATAL; |
| 102 |
} |
| 103 |
exi_compress_variable(exoid, *varid, 2); |
| 104 |
|
| 105 |
/* leave define mode */ |
| 106 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 107 |
return EX_FATAL; |
| 108 |
} |
| 109 |
} |
| 110 |
else { |
| 111 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s %s in file id %d", |
| 112 |
ex_name_of_object(var_type), exi_name_red_var_of_object(var_type, obj_id_ndx), |
| 113 |
exoid); |
| 114 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 115 |
return EX_FATAL; |
| 116 |
} |
| 117 |
} |
| 118 |
return EX_NOERR; |
| 119 |
} |
| 120 |
|
| 121 |
/*! |
| 122 |
\ingroup ResultsData |
| 123 |
writes the values of a single variable of the specified type for a |
| 124 |
single time step. The function ex_put_reduction_variable_param() must be invoked |
| 125 |
before this call is made. |
| 126 |
|
| 127 |
Because variables are floating point values, the application |
| 128 |
code must declare the array passed to be the appropriate type |
| 129 |
(float or double) to match the compute word size passed in |
| 130 |
ex_create() or ex_open(). |
| 131 |
|
| 132 |
\return In case of an error, ex_put_reduction_vars() returns a negative number; a |
| 133 |
warning will return a positive number. Possible causes of errors |
| 134 |
include: |
| 135 |
- data file not properly opened with call to ex_create() or ex_open() |
| 136 |
- data file opened for read only. |
| 137 |
- data file not initialized properly with call to ex_put_init(). |
| 138 |
- ex_put_variable_param() not called previously specifying the number of |
| 139 |
variables. |
| 140 |
|
| 141 |
\param[in] exoid |
| 142 |
exodus file ID returned from a previous call to |
| 143 |
ex_create() or ex_open(). |
| 144 |
|
| 145 |
\param[in] time_step |
| 146 |
The time step number, as described under |
| 147 |
ex_put_time(). This is essentially a counter that is incremented when |
| 148 |
results variables are output. The first time step is 1. |
| 149 |
|
| 150 |
\param[in] var_type |
| 151 |
type (edge block, face block, edge set, ... ) |
| 152 |
|
| 153 |
\param[in] obj_id |
| 154 |
entity block/set id (ignored for global and nodal variables) |
| 155 |
|
| 156 |
\param[in] num_variables |
| 157 |
The number of reduction variables in this block/set |
| 158 |
|
| 159 |
\param[in] var_vals |
| 160 |
Array of `num_variables` values for all reduction variables for the `time_step`-th time |
| 161 |
step. |
| 162 |
|
| 163 |
As an example, the following code segment writes all the assembly |
| 164 |
variables for assembly 100 for a single time step: |
| 165 |
|
| 166 |
~~~{.c} |
| 167 |
int num_assem_vars, num_assembly, error, exoid, time_step; |
| 168 |
float *assembly_var_vals; |
| 169 |
|
| 170 |
\comment{write assembly variables} |
| 171 |
assembly_var_vals = (float *) calloc(num_assem_vars, sizeof(float)); |
| 172 |
for (int k=1; k <= num_assem_vars; k++) { |
| 173 |
\comment{application code fills in this array} |
| 174 |
assembly_var_vals[j] = 10.0; |
| 175 |
} |
| 176 |
error = ex_put_reduction_vars(exoid, time_step, EX_ASSEMBLY, 100, num_assem_vars, |
| 177 |
assembly_var_vals); |
| 178 |
} |
| 179 |
~~~ |
| 180 |
*/ |
| 181 |
|
| 182 |
int ex_put_reduction_vars(int exoid, int time_step, ex_entity_type var_type, ex_entity_id obj_id, |
| 183 |
int64_t num_variables, const void *var_vals) |
| 184 |
{ |
| 185 |
int varid; |
| 186 |
size_t start[2], count[2]; |
| 187 |
int status; |
| 188 |
char errmsg[MAX_ERR_LENGTH]; |
| 189 |
|
| 190 |
EX_FUNC_ENTER(); |
| 191 |
|
| 192 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 193 |
EX_FUNC_LEAVE(EX_FATAL); |
| 194 |
} |
| 195 |
|
| 196 |
switch (var_type) { |
| 197 |
/* NOTE: Global variables are always reduction variables, so use the ex_put_var function. */ |
| 198 |
case EX_GLOBAL: |
| 199 |
EX_FUNC_UNLOCK(); |
| 200 |
return ex_put_var(exoid, time_step, var_type, 1, 1, num_variables, var_vals); |
| 201 |
break; |
| 202 |
case EX_ASSEMBLY: |
| 203 |
status = exi_look_up_var(exoid, var_type, obj_id, "assembly", DIM_NUM_ASSEMBLY_RED_VAR, &varid); |
| 204 |
break; |
| 205 |
case EX_BLOB: |
| 206 |
status = exi_look_up_var(exoid, var_type, obj_id, "blob", DIM_NUM_BLOB_RED_VAR, &varid); |
| 207 |
break; |
| 208 |
case EX_EDGE_BLOCK: |
| 209 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_ID_ED_BLK, DIM_NUM_EDG_RED_VAR, &varid); |
| 210 |
break; |
| 211 |
case EX_FACE_BLOCK: |
| 212 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_ID_FA_BLK, DIM_NUM_FAC_RED_VAR, &varid); |
| 213 |
break; |
| 214 |
case EX_ELEM_BLOCK: |
| 215 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_ID_EL_BLK, DIM_NUM_ELE_RED_VAR, &varid); |
| 216 |
break; |
| 217 |
case EX_NODE_SET: |
| 218 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_NS_IDS, DIM_NUM_NSET_RED_VAR, &varid); |
| 219 |
break; |
| 220 |
case EX_EDGE_SET: |
| 221 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_ES_IDS, DIM_NUM_ESET_RED_VAR, &varid); |
| 222 |
break; |
| 223 |
case EX_FACE_SET: |
| 224 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_FS_IDS, DIM_NUM_FSET_RED_VAR, &varid); |
| 225 |
break; |
| 226 |
case EX_SIDE_SET: |
| 227 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_SS_IDS, DIM_NUM_SSET_RED_VAR, &varid); |
| 228 |
break; |
| 229 |
case EX_ELEM_SET: |
| 230 |
status = exi_look_up_var(exoid, var_type, obj_id, VAR_ELS_IDS, DIM_NUM_ELSET_RED_VAR, &varid); |
| 231 |
break; |
| 232 |
default: |
| 233 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: invalid variable type (%d) specified for file id %d", |
| 234 |
var_type, exoid); |
| 235 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 236 |
EX_FUNC_LEAVE(EX_FATAL); |
| 237 |
} |
| 238 |
|
| 239 |
if (status != EX_NOERR) { |
| 240 |
EX_FUNC_LEAVE(status); |
| 241 |
} |
| 242 |
|
| 243 |
/* store element variable values */ |
| 244 |
start[0] = time_step - 1; |
| 245 |
count[0] = 1; |
| 246 |
|
| 247 |
start[1] = 0; |
| 248 |
count[1] = num_variables; |
| 249 |
|
| 250 |
if (exi_comp_ws(exoid) == 4) { |
| 251 |
status = nc_put_vara_float(exoid, varid, start, count, var_vals); |
| 252 |
} |
| 253 |
else { |
| 254 |
status = nc_put_vara_double(exoid, varid, start, count, var_vals); |
| 255 |
} |
| 256 |
|
| 257 |
if (status != NC_NOERR) { |
| 258 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 259 |
"ERROR: failed to store %s %" PRId64 " at step %d in file id %d", |
| 260 |
ex_name_of_object(var_type), obj_id, time_step, exoid); |
| 261 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 262 |
EX_FUNC_LEAVE(EX_FATAL); |
| 263 |
} |
| 264 |
|
| 265 |
EX_FUNC_LEAVE(EX_NOERR); |
| 266 |
} |