| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022 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 |
* expvtt - ex_put_truth_table |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* int obj_type object type |
| 16 |
* int num_blk number of blocks |
| 17 |
* int num_var number of variables |
| 18 |
* int* variable_table variable truth table array |
| 19 |
* |
| 20 |
* exit conditions - |
| 21 |
* |
| 22 |
* revision history - |
| 23 |
* |
| 24 |
* |
| 25 |
*****************************************************************************/ |
| 26 |
|
| 27 |
#include "exodusII.h" // for ex_err, etc |
| 28 |
#include "exodusII_int.h" // for exi_get_dimension, EX_FATAL, etc |
| 29 |
|
| 30 |
/*! |
| 31 |
\ingroup ResultsData |
| 32 |
|
| 33 |
* writes the EXODUS variable truth table to the database; also, |
| 34 |
* creates netCDF variables in which to store EXODUS variable |
| 35 |
* values; although this table isn't required (because the netCDF |
| 36 |
* variables can also be created in ex_put_var()), this call will save |
| 37 |
* tremendous time because all of the variables are defined at once |
| 38 |
* while the file is in define mode, rather than going in and out of |
| 39 |
* define mode (causing the entire file to be copied over and over) |
| 40 |
* which is what occurs when the variables are defined in ex_put_var() |
| 41 |
* \param exoid exodus file id |
| 42 |
* \param obj_type object type |
| 43 |
* \param num_blk number of blocks or sets |
| 44 |
* \param num_var number of variables |
| 45 |
* \param *var_tab variable truth table array |
| 46 |
* |
| 47 |
The following coding will create, populate, and write an element |
| 48 |
variable truth table to an opened exodus file (NOTE: all element |
| 49 |
variables are valid for all element blocks in this example.): |
| 50 |
|
| 51 |
~~~{.c} |
| 52 |
int *truth_tab, num_elem_blk, num_ele_vars, error, exoid; |
| 53 |
|
| 54 |
\comment{write element variable truth table} |
| 55 |
truth_tab = (int *)calloc((num_elem_blk*num_ele_vars), sizeof(int)); |
| 56 |
|
| 57 |
for (i=0, k=0; i < num_elem_blk; i++) { |
| 58 |
for (j=0; j < num_ele_vars; j++) { |
| 59 |
truth_tab[k++] = 1; |
| 60 |
} |
| 61 |
} |
| 62 |
error = ex_put_truth_table(exoid, EX_ELEM_BLOCK, num_elem_blk, num_ele_vars, |
| 63 |
truth_tab); |
| 64 |
~~~ |
| 65 |
*/ |
| 66 |
|
| 67 |
int ex_put_truth_table(int exoid, ex_entity_type obj_type, int num_blk, int num_var, |
| 68 |
const int *var_tab) |
| 69 |
{ |
| 70 |
int numelblkdim, numelvardim, timedim, dims[2], varid; |
| 71 |
char *sta_type = NULL; |
| 72 |
char *tab_type = NULL; |
| 73 |
size_t num_entity = 0; |
| 74 |
size_t num_var_db = 0; |
| 75 |
int *stat_vals = NULL; |
| 76 |
int i, j, k; |
| 77 |
int status; |
| 78 |
char errmsg[MAX_ERR_LENGTH]; |
| 79 |
|
| 80 |
/* |
| 81 |
* The ent_type and the var_name are used to build the netcdf |
| 82 |
* variables name. Normally this is done via a macro defined in |
| 83 |
* exodusII_int.h |
| 84 |
*/ |
| 85 |
const char *ent_type = NULL; |
| 86 |
const char *var_name = NULL; |
| 87 |
const char *ent_size = NULL; |
| 88 |
|
| 89 |
EX_FUNC_ENTER(); |
| 90 |
|
| 91 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 92 |
EX_FUNC_LEAVE(EX_FATAL); |
| 93 |
} |
| 94 |
|
| 95 |
exi_get_dimension(exoid, exi_dim_num_objects(obj_type), ex_name_of_object(obj_type), &num_entity, |
| 96 |
&numelblkdim, __func__); |
| 97 |
|
| 98 |
if (obj_type == EX_ELEM_BLOCK) { |
| 99 |
exi_get_dimension(exoid, DIM_NUM_ELE_VAR, "element variables", &num_var_db, &numelvardim, |
| 100 |
__func__); |
| 101 |
var_name = "vals_elem_var"; |
| 102 |
ent_type = "eb"; |
| 103 |
ent_size = "num_el_in_blk"; |
| 104 |
sta_type = VAR_STAT_EL_BLK; |
| 105 |
tab_type = VAR_ELEM_TAB; |
| 106 |
} |
| 107 |
else if (obj_type == EX_EDGE_BLOCK) { |
| 108 |
exi_get_dimension(exoid, DIM_NUM_EDG_VAR, "edge block variables", &num_var_db, &numelvardim, |
| 109 |
__func__); |
| 110 |
var_name = "vals_edge_var"; |
| 111 |
ent_type = "eb"; |
| 112 |
ent_size = "num_ed_in_blk"; |
| 113 |
sta_type = VAR_STAT_ED_BLK; |
| 114 |
tab_type = VAR_EBLK_TAB; |
| 115 |
} |
| 116 |
else if (obj_type == EX_FACE_BLOCK) { |
| 117 |
exi_get_dimension(exoid, DIM_NUM_FAC_VAR, "face block variables", &num_var_db, &numelvardim, |
| 118 |
__func__); |
| 119 |
var_name = "vals_face_var"; |
| 120 |
ent_type = "fb"; |
| 121 |
ent_size = "num_fa_in_blk"; |
| 122 |
sta_type = VAR_STAT_FA_BLK; |
| 123 |
tab_type = VAR_FBLK_TAB; |
| 124 |
} |
| 125 |
else if (obj_type == EX_SIDE_SET) { |
| 126 |
exi_get_dimension(exoid, DIM_NUM_SSET_VAR, "sideset variables", &num_var_db, &numelvardim, |
| 127 |
__func__); |
| 128 |
var_name = "vals_sset_var"; |
| 129 |
ent_type = "ss"; |
| 130 |
ent_size = "num_side_ss"; |
| 131 |
sta_type = VAR_SS_STAT; |
| 132 |
tab_type = VAR_SSET_TAB; |
| 133 |
} |
| 134 |
else if (obj_type == EX_NODE_SET) { |
| 135 |
exi_get_dimension(exoid, DIM_NUM_NSET_VAR, "nodeset variables", &num_var_db, &numelvardim, |
| 136 |
__func__); |
| 137 |
var_name = "vals_nset_var"; |
| 138 |
ent_type = "ns"; |
| 139 |
ent_size = "num_nod_ns"; |
| 140 |
sta_type = VAR_NS_STAT; |
| 141 |
tab_type = VAR_NSET_TAB; |
| 142 |
} |
| 143 |
else if (obj_type == EX_EDGE_SET) { |
| 144 |
exi_get_dimension(exoid, DIM_NUM_ESET_VAR, "edge set variables", &num_var_db, &numelvardim, |
| 145 |
__func__); |
| 146 |
var_name = "vals_eset_var"; |
| 147 |
ent_type = "es"; |
| 148 |
ent_size = "num_edge_es"; |
| 149 |
sta_type = VAR_ES_STAT; |
| 150 |
tab_type = VAR_ESET_TAB; |
| 151 |
} |
| 152 |
else if (obj_type == EX_FACE_SET) { |
| 153 |
exi_get_dimension(exoid, DIM_NUM_FSET_VAR, "face set variables", &num_var_db, &numelvardim, |
| 154 |
__func__); |
| 155 |
var_name = "vals_fset_var"; |
| 156 |
ent_type = "fs"; |
| 157 |
ent_size = "num_face_fs"; |
| 158 |
sta_type = VAR_FS_STAT; |
| 159 |
tab_type = VAR_FSET_TAB; |
| 160 |
} |
| 161 |
else if (obj_type == EX_ELEM_SET) { |
| 162 |
exi_get_dimension(exoid, DIM_NUM_ELSET_VAR, "element set variables", &num_var_db, &numelvardim, |
| 163 |
__func__); |
| 164 |
var_name = "vals_elset_var"; |
| 165 |
ent_type = "es"; |
| 166 |
ent_size = "num_ele_els"; |
| 167 |
sta_type = VAR_ELS_STAT; |
| 168 |
tab_type = VAR_ELSET_TAB; |
| 169 |
} |
| 170 |
else if (obj_type == EX_BLOB) { |
| 171 |
exi_get_dimension(exoid, DIM_NUM_BLOB_VAR, "blob variables", &num_var_db, &numelvardim, |
| 172 |
__func__); |
| 173 |
var_name = "vals_blob_var"; |
| 174 |
ent_type = "blob"; |
| 175 |
ent_size = "num_values_blob"; |
| 176 |
sta_type = ""; |
| 177 |
tab_type = VAR_BLOB_TAB; |
| 178 |
} |
| 179 |
|
| 180 |
else { /* invalid variable type */ |
| 181 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type %d specified in file id %d", |
| 182 |
obj_type, exoid); |
| 183 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 184 |
EX_FUNC_LEAVE(EX_WARN); |
| 185 |
} |
| 186 |
|
| 187 |
if ((int)num_entity != num_blk) { |
| 188 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: # of %s doesn't match those defined in file id %d", |
| 189 |
ex_name_of_object(obj_type), exoid); |
| 190 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 191 |
EX_FUNC_LEAVE(EX_FATAL); |
| 192 |
} |
| 193 |
|
| 194 |
if ((int)num_var_db != num_var) { |
| 195 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 196 |
"ERROR: # of %s variables doesn't match those defined in file id %d", |
| 197 |
ex_name_of_object(obj_type), exoid); |
| 198 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 199 |
EX_FUNC_LEAVE(EX_FATAL); |
| 200 |
} |
| 201 |
|
| 202 |
/* Get status array for later use */ |
| 203 |
if (!(stat_vals = malloc(num_blk * sizeof(int)))) { |
| 204 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 205 |
"ERROR: failed to allocate memory for %s status array for file id %d", |
| 206 |
ex_name_of_object(obj_type), exoid); |
| 207 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 208 |
EX_FUNC_LEAVE(EX_FATAL); |
| 209 |
} |
| 210 |
|
| 211 |
if (strlen(sta_type) > 1) { |
| 212 |
status = nc_inq_varid(exoid, sta_type, &varid); |
| 213 |
} |
| 214 |
else { |
| 215 |
status = EX_FATAL; // Anything except EX_NOERR so hits `else` below |
| 216 |
} |
| 217 |
|
| 218 |
/* get variable id of status array */ |
| 219 |
if (status == NC_NOERR) { |
| 220 |
/* if status array exists (V 2.01+), use it, otherwise assume |
| 221 |
object exists to be backward compatible */ |
| 222 |
|
| 223 |
if ((status = nc_get_var_int(exoid, varid, stat_vals)) != NC_NOERR) { |
| 224 |
free(stat_vals); |
| 225 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s status array from file id %d", |
| 226 |
ex_name_of_object(obj_type), exoid); |
| 227 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 228 |
EX_FUNC_LEAVE(EX_FATAL); |
| 229 |
} |
| 230 |
} |
| 231 |
else { |
| 232 |
/* status array doesn't exist (V2.00), dummy one up for later checking */ |
| 233 |
for (i = 0; i < num_blk; i++) { |
| 234 |
stat_vals[i] = 1; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/* put netcdf file into define mode */ |
| 239 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 240 |
free(stat_vals); |
| 241 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 242 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 243 |
EX_FUNC_LEAVE(EX_FATAL); |
| 244 |
} |
| 245 |
|
| 246 |
/* inquire previously defined dimensions */ |
| 247 |
if ((status = nc_inq_dimid(exoid, DIM_TIME, &timedim)) != NC_NOERR) { |
| 248 |
free(stat_vals); |
| 249 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time variable in file id %d", exoid); |
| 250 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 251 |
goto error_ret; /* exit define mode and return */ |
| 252 |
} |
| 253 |
|
| 254 |
/* define netCDF variables in which to store EXODUS element |
| 255 |
* variable values |
| 256 |
*/ |
| 257 |
|
| 258 |
k = 0; |
| 259 |
for (i = 0; i < num_blk; i++) { |
| 260 |
for (j = 1; j <= num_var; j++) { |
| 261 |
|
| 262 |
/* check if variables are to be put out for this entity */ |
| 263 |
if (var_tab[k] != 0) { |
| 264 |
if (stat_vals[i] != 0) { /* check for NULL entity */ |
| 265 |
/* NOTE: This code used to zero out the var_tab entry |
| 266 |
if the stat_vals[i] value was zero. However, in some |
| 267 |
cases it is good to know that a variable was assigned to |
| 268 |
an entity even if that entity is empty. The code was |
| 269 |
changed to not modify the truth table. |
| 270 |
*/ |
| 271 |
dims[0] = timedim; |
| 272 |
|
| 273 |
/* Determine number of entities in block */ |
| 274 |
if ((status = nc_inq_dimid(exoid, exi_catstr(ent_size, (i + 1)), &dims[1])) != NC_NOERR) { |
| 275 |
free(stat_vals); |
| 276 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 277 |
"ERROR: failed to locate number of entities in " |
| 278 |
"%d'th %s in file id %d", |
| 279 |
i + 1, ex_name_of_object(obj_type), exoid); |
| 280 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 281 |
goto error_ret; /* exit define mode and return */ |
| 282 |
} |
| 283 |
|
| 284 |
/* define netCDF variable to store variable values; the j |
| 285 |
* index cycles from 1 through the number of variables so |
| 286 |
* that the index of the EXODUS variable (which is part |
| 287 |
* of the name of the netCDF variable) will begin at 1 |
| 288 |
* instead of 0 |
| 289 |
*/ |
| 290 |
|
| 291 |
if ((status = nc_def_var(exoid, exi_catstr2(var_name, j, ent_type, i + 1), |
| 292 |
nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) { |
| 293 |
if (status != NC_ENAMEINUSE) { |
| 294 |
free(stat_vals); |
| 295 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 296 |
"ERROR: failed to define variable for %d'th %s in file id %d", i + 1, |
| 297 |
ex_name_of_object(obj_type), exoid); |
| 298 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 299 |
goto error_ret; /* exit define mode and return */ |
| 300 |
} |
| 301 |
exi_compress_variable(exoid, varid, 2); |
| 302 |
} |
| 303 |
} |
| 304 |
} /* if */ |
| 305 |
k++; /* increment element truth table pointer */ |
| 306 |
} /* for j */ |
| 307 |
} /* for i */ |
| 308 |
|
| 309 |
free(stat_vals); |
| 310 |
|
| 311 |
/* create a variable array in which to store the truth table |
| 312 |
*/ |
| 313 |
|
| 314 |
dims[0] = numelblkdim; |
| 315 |
dims[1] = numelvardim; |
| 316 |
status = nc_def_var(exoid, tab_type, NC_INT, 2, dims, &varid); |
| 317 |
|
| 318 |
if (status != NC_NOERR) { |
| 319 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 320 |
"ERROR: failed to define %s variable truth table in file id %d", |
| 321 |
ex_name_of_object(obj_type), exoid); |
| 322 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 323 |
goto error_ret; /* exit define mode and return */ |
| 324 |
} |
| 325 |
|
| 326 |
/* leave define mode */ |
| 327 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 328 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 329 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 330 |
EX_FUNC_LEAVE(EX_FATAL); |
| 331 |
} |
| 332 |
|
| 333 |
/* write out the element variable truth table */ |
| 334 |
status = nc_put_var_int(exoid, varid, var_tab); |
| 335 |
|
| 336 |
if (status != NC_NOERR) { |
| 337 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store variable truth table in file id %d", |
| 338 |
exoid); |
| 339 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 340 |
EX_FUNC_LEAVE(EX_FATAL); |
| 341 |
} |
| 342 |
|
| 343 |
EX_FUNC_LEAVE(EX_NOERR); |
| 344 |
|
| 345 |
/* Fatal error: exit definition mode and return */ |
| 346 |
error_ret: |
| 347 |
exi_leavedef(exoid, __func__); |
| 348 |
EX_FUNC_LEAVE(EX_FATAL); |
| 349 |
} |