| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021 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 |
* exgotv - ex_get_object_truth_vector |
| 11 |
* |
| 12 |
* revision history - |
| 13 |
* |
| 14 |
* |
| 15 |
*****************************************************************************/ |
| 16 |
|
| 17 |
#include "exodusII.h" // for ex_err, etc |
| 18 |
#include "exodusII_int.h" // for exi_get_dimension, EX_FATAL, etc |
| 19 |
|
| 20 |
/*! |
| 21 |
* reads the EXODUS specified variable truth vector from the database |
| 22 |
*/ |
| 23 |
|
| 24 |
int ex_get_object_truth_vector(int exoid, ex_entity_type obj_type, ex_entity_id entity_id, |
| 25 |
int num_var, int *var_vec) |
| 26 |
{ |
| 27 |
int statust, status; |
| 28 |
int varid, tabid; |
| 29 |
size_t num_var_db = 0; |
| 30 |
char errmsg[MAX_ERR_LENGTH]; |
| 31 |
|
| 32 |
/* |
| 33 |
* The ent_type and the var_name are used to build the netcdf |
| 34 |
* variables name. Normally this is done via a macro defined in |
| 35 |
* exodusII_int.h |
| 36 |
*/ |
| 37 |
const char *ent_type = NULL; |
| 38 |
const char *var_name = NULL; |
| 39 |
|
| 40 |
EX_FUNC_ENTER(); |
| 41 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 42 |
EX_FUNC_LEAVE(EX_FATAL); |
| 43 |
} |
| 44 |
|
| 45 |
switch (obj_type) { |
| 46 |
case EX_EDGE_BLOCK: |
| 47 |
status = |
| 48 |
exi_get_dimension(exoid, DIM_NUM_EDG_VAR, "edge variables", &num_var_db, &varid, __func__); |
| 49 |
statust = nc_inq_varid(exoid, VAR_EBLK_TAB, &tabid); |
| 50 |
var_name = "vals_edge_var"; |
| 51 |
ent_type = "eb"; |
| 52 |
break; |
| 53 |
case EX_FACE_BLOCK: |
| 54 |
status = |
| 55 |
exi_get_dimension(exoid, DIM_NUM_FAC_VAR, "face variables", &num_var_db, &varid, __func__); |
| 56 |
statust = nc_inq_varid(exoid, VAR_FBLK_TAB, &tabid); |
| 57 |
var_name = "vals_face_var"; |
| 58 |
ent_type = "fb"; |
| 59 |
break; |
| 60 |
case EX_ELEM_BLOCK: |
| 61 |
status = exi_get_dimension(exoid, DIM_NUM_ELE_VAR, "element variables", &num_var_db, &varid, |
| 62 |
__func__); |
| 63 |
statust = nc_inq_varid(exoid, VAR_ELEM_TAB, &tabid); |
| 64 |
var_name = "vals_elem_var"; |
| 65 |
ent_type = "eb"; |
| 66 |
break; |
| 67 |
case EX_NODE_SET: |
| 68 |
status = exi_get_dimension(exoid, DIM_NUM_NSET_VAR, "nodeset variables", &num_var_db, &varid, |
| 69 |
__func__); |
| 70 |
statust = nc_inq_varid(exoid, VAR_NSET_TAB, &tabid); |
| 71 |
var_name = "vals_nset_var"; |
| 72 |
ent_type = "ns"; |
| 73 |
break; |
| 74 |
case EX_EDGE_SET: |
| 75 |
status = exi_get_dimension(exoid, DIM_NUM_ESET_VAR, "edgeset variables", &num_var_db, &varid, |
| 76 |
__func__); |
| 77 |
statust = nc_inq_varid(exoid, VAR_ESET_TAB, &tabid); |
| 78 |
var_name = "vals_eset_var"; |
| 79 |
ent_type = "es"; |
| 80 |
break; |
| 81 |
case EX_FACE_SET: |
| 82 |
status = exi_get_dimension(exoid, DIM_NUM_FSET_VAR, "faceset variables", &num_var_db, &varid, |
| 83 |
__func__); |
| 84 |
statust = nc_inq_varid(exoid, VAR_FSET_TAB, &tabid); |
| 85 |
var_name = "vals_fset_var"; |
| 86 |
ent_type = "fs"; |
| 87 |
break; |
| 88 |
case EX_SIDE_SET: |
| 89 |
status = exi_get_dimension(exoid, DIM_NUM_SSET_VAR, "sideset variables", &num_var_db, &varid, |
| 90 |
__func__); |
| 91 |
statust = nc_inq_varid(exoid, VAR_SSET_TAB, &tabid); |
| 92 |
var_name = "vals_sset_var"; |
| 93 |
ent_type = "ss"; |
| 94 |
break; |
| 95 |
case EX_ELEM_SET: |
| 96 |
status = exi_get_dimension(exoid, DIM_NUM_ELSET_VAR, "elemset variables", &num_var_db, &varid, |
| 97 |
__func__); |
| 98 |
statust = nc_inq_varid(exoid, VAR_ELSET_TAB, &tabid); |
| 99 |
var_name = "vals_elset_var"; |
| 100 |
ent_type = "es"; |
| 101 |
break; |
| 102 |
case EX_BLOB: |
| 103 |
status = |
| 104 |
exi_get_dimension(exoid, DIM_NUM_BLOB_VAR, "blob variables", &num_var_db, &varid, __func__); |
| 105 |
statust = nc_inq_varid(exoid, VAR_BLOB_TAB, &tabid); |
| 106 |
var_name = "vals_blob_var"; |
| 107 |
ent_type = "blob"; |
| 108 |
break; |
| 109 |
default: |
| 110 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type %d specified in file id %d", |
| 111 |
obj_type, exoid); |
| 112 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 113 |
EX_FUNC_LEAVE(EX_WARN); |
| 114 |
} |
| 115 |
|
| 116 |
if (status != NC_NOERR) { |
| 117 |
EX_FUNC_LEAVE(EX_WARN); |
| 118 |
} |
| 119 |
|
| 120 |
/* Determine index of entity_id in id array */ |
| 121 |
int ent_ndx = exi_id_lkup(exoid, obj_type, entity_id); |
| 122 |
if (ent_ndx <= 0) { |
| 123 |
ex_get_err(NULL, NULL, &status); |
| 124 |
if (status != 0) { |
| 125 |
if (status != EX_NULLENTITY) { |
| 126 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 127 |
"ERROR: failed to locate %s id %" PRId64 " in id variable in file id %d", |
| 128 |
ex_name_of_object(obj_type), entity_id, exoid); |
| 129 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 130 |
EX_FUNC_LEAVE(EX_FATAL); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
/* If this is a null entity, then 'ent_ndx' will be negative. |
| 135 |
* We don't care in this __func__, so make it positive and continue... |
| 136 |
*/ |
| 137 |
if (ent_ndx < 0) { |
| 138 |
ent_ndx = -ent_ndx; |
| 139 |
} |
| 140 |
|
| 141 |
if ((int)num_var_db != num_var) { |
| 142 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 143 |
"ERROR: # of variables doesn't match those defined in file id %d", exoid); |
| 144 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 145 |
EX_FUNC_LEAVE(EX_FATAL); |
| 146 |
} |
| 147 |
|
| 148 |
if (statust != NC_NOERR) { |
| 149 |
/* since truth vector isn't stored in the data file, derive it dynamically |
| 150 |
*/ |
| 151 |
for (int i = 0; i < num_var; i++) { |
| 152 |
/* NOTE: names are 1-based */ |
| 153 |
if (nc_inq_varid(exoid, exi_catstr2(var_name, i + 1, ent_type, ent_ndx), &tabid) != |
| 154 |
NC_NOERR) { |
| 155 |
/* variable doesn't exist; put a 0 in the truth vector */ |
| 156 |
var_vec[i] = 0; |
| 157 |
} |
| 158 |
else { |
| 159 |
/* variable exists; put a 1 in the truth vector */ |
| 160 |
var_vec[i] = 1; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
else { |
| 165 |
|
| 166 |
/* read in the truth vector */ |
| 167 |
|
| 168 |
size_t start[2], count[2]; |
| 169 |
start[0] = ent_ndx - 1; |
| 170 |
start[1] = 0; |
| 171 |
|
| 172 |
count[0] = 1; |
| 173 |
count[1] = num_var; |
| 174 |
|
| 175 |
status = nc_get_vara_int(exoid, tabid, start, count, var_vec); |
| 176 |
|
| 177 |
if (status != NC_NOERR) { |
| 178 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get truth vector from file id %d", exoid); |
| 179 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 180 |
EX_FUNC_LEAVE(EX_FATAL); |
| 181 |
} |
| 182 |
} |
| 183 |
EX_FUNC_LEAVE(EX_NOERR); |
| 184 |
} |