| 1 |
/* |
| 2 |
* Copyright(C) 1999-2022, 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 |
/*****************************************************************************/ |
| 10 |
/*****************************************************************************/ |
| 11 |
/*****************************************************************************/ |
| 12 |
/* Function(s) contained in this file: |
| 13 |
* |
| 14 |
* exi_leavedef() |
| 15 |
* nei_id_lkup() |
| 16 |
* exi_get_file_type() |
| 17 |
* exi_put_nemesis_version() |
| 18 |
* nei_check_file_version() |
| 19 |
* ex_get_idx() |
| 20 |
* |
| 21 |
***************************************************************************** |
| 22 |
* Much of this code is a modified version of what is found in NemesisI. |
| 23 |
*/ |
| 24 |
/*****************************************************************************/ |
| 25 |
/*****************************************************************************/ |
| 26 |
/*****************************************************************************/ |
| 27 |
|
| 28 |
#include <exodusII.h> // for ex_err, etc |
| 29 |
#include <exodusII_int.h> // for EX_FATAL, EX_NOERR, etc |
| 30 |
|
| 31 |
/* Global variables */ |
| 32 |
char *ne_ret_string; |
| 33 |
|
| 34 |
/*****************************************************************************/ |
| 35 |
/*****************************************************************************/ |
| 36 |
/*****************************************************************************/ |
| 37 |
/* Note: This function assumes a 1-d vector of data for "ne_var_name". |
| 38 |
*/ |
| 39 |
/*****************************************************************************/ |
| 40 |
int nei_id_lkup(int exoid, const char *ne_var_name, int64_t *idx, ex_entity_id ne_var_id) |
| 41 |
{ |
| 42 |
int status; |
| 43 |
int varid; |
| 44 |
if ((status = nc_inq_varid(exoid, ne_var_name, &varid)) != NC_NOERR) { |
| 45 |
char errmsg[MAX_ERR_LENGTH]; |
| 46 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable ID for \"%s\" in file ID %d", |
| 47 |
ne_var_name, exoid); |
| 48 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 49 |
return EX_FATAL; |
| 50 |
} |
| 51 |
|
| 52 |
/* check if I need the length for this variable */ |
| 53 |
if (idx[1] == -1) { |
| 54 |
/* Get the dimension IDs for this variable */ |
| 55 |
int ndims; |
| 56 |
int dimid[1]; |
| 57 |
nc_type var_type; |
| 58 |
if ((status = nc_inq_var(exoid, varid, NULL, &var_type, &ndims, dimid, NULL)) != NC_NOERR) { |
| 59 |
char errmsg[MAX_ERR_LENGTH]; |
| 60 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 61 |
"ERROR: failed to find dimension ID for variable \"%s\" " |
| 62 |
"in file ID %d", |
| 63 |
ne_var_name, exoid); |
| 64 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 65 |
return EX_FATAL; |
| 66 |
} |
| 67 |
|
| 68 |
/* Get the length of this variable */ |
| 69 |
size_t length; |
| 70 |
if ((status = nc_inq_dimlen(exoid, dimid[0], &length)) != NC_NOERR) { |
| 71 |
char errmsg[MAX_ERR_LENGTH]; |
| 72 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 73 |
"ERROR: failed to find dimension for variable \"%s\" in file ID %d", ne_var_name, |
| 74 |
exoid); |
| 75 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 76 |
return EX_FATAL; |
| 77 |
} |
| 78 |
|
| 79 |
idx[1] = length; |
| 80 |
} /* End "if (idx[1] == -1)" */ |
| 81 |
|
| 82 |
int64_t begin = idx[0]; |
| 83 |
int64_t end = idx[1]; |
| 84 |
|
| 85 |
/* Find the index by looping over each entry */ |
| 86 |
int ret = -1; |
| 87 |
for (int64_t my_index = begin; my_index < end; my_index++) { |
| 88 |
size_t start[] = {my_index}; |
| 89 |
long long id_val; |
| 90 |
status = nc_get_var1_longlong(exoid, varid, start, &id_val); |
| 91 |
|
| 92 |
if (status != NC_NOERR) { |
| 93 |
char errmsg[MAX_ERR_LENGTH]; |
| 94 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable \"%s\" in file ID %d", |
| 95 |
ne_var_name, exoid); |
| 96 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 97 |
return EX_FATAL; |
| 98 |
} |
| 99 |
|
| 100 |
if (id_val == ne_var_id) { |
| 101 |
ret = (int)my_index; |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
return ret; |
| 106 |
} |
| 107 |
|
| 108 |
/*****************************************************************************/ |
| 109 |
/*****************************************************************************/ |
| 110 |
/*****************************************************************************/ |
| 111 |
/* This function retrieves the file type from a Nemesis file. |
| 112 |
*/ |
| 113 |
/*****************************************************************************/ |
| 114 |
int exi_get_file_type(int exoid, char *ftype) |
| 115 |
{ |
| 116 |
EX_FUNC_ENTER(); |
| 117 |
|
| 118 |
int varid; |
| 119 |
if (nc_inq_varid(exoid, VAR_FILE_TYPE, &varid) != NC_NOERR) { |
| 120 |
|
| 121 |
/* If no file type is found, assume parallel */ |
| 122 |
ftype[0] = 'p'; |
| 123 |
ftype[1] = '\0'; |
| 124 |
|
| 125 |
EX_FUNC_LEAVE(EX_NOERR); |
| 126 |
} |
| 127 |
|
| 128 |
int status; |
| 129 |
int lftype; |
| 130 |
if ((status = nc_get_var1_int(exoid, varid, NULL, &lftype)) != NC_NOERR) { |
| 131 |
char errmsg[MAX_ERR_LENGTH]; |
| 132 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get variable \"%s\" from file ID %d", |
| 133 |
VAR_FILE_TYPE, exoid); |
| 134 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 135 |
EX_FUNC_LEAVE(EX_FATAL); |
| 136 |
} |
| 137 |
|
| 138 |
/* Set the appropriate character */ |
| 139 |
if (lftype == 0) { |
| 140 |
ex_copy_string(ftype, "p", 2); |
| 141 |
} |
| 142 |
else if (lftype == 1) { |
| 143 |
ex_copy_string(ftype, "s", 2); |
| 144 |
} |
| 145 |
|
| 146 |
EX_FUNC_LEAVE(EX_NOERR); |
| 147 |
} |
| 148 |
|
| 149 |
/*****************************************************************************/ |
| 150 |
/*****************************************************************************/ |
| 151 |
/*****************************************************************************/ |
| 152 |
/* This function outputs the Nemesis version information to the file. |
| 153 |
*/ |
| 154 |
/*****************************************************************************/ |
| 155 |
int exi_put_nemesis_version(int exoid) |
| 156 |
{ |
| 157 |
EX_FUNC_ENTER(); |
| 158 |
|
| 159 |
float file_ver = NEMESIS_FILE_VERSION; |
| 160 |
|
| 161 |
/* Check to see if the nemesis file version is already in the file */ |
| 162 |
if (nc_get_att_float(exoid, NC_GLOBAL, "nemesis_file_version", &file_ver) != NC_NOERR) { |
| 163 |
|
| 164 |
/* Output the Nemesis file version */ |
| 165 |
int status; |
| 166 |
if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_NEM_FILE_VERSION, NC_FLOAT, 1, |
| 167 |
&file_ver)) != NC_NOERR) { |
| 168 |
char errmsg[MAX_ERR_LENGTH]; |
| 169 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output nemesis file version in file ID %d", |
| 170 |
exoid); |
| 171 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 172 |
EX_FUNC_LEAVE(EX_FATAL); |
| 173 |
} |
| 174 |
|
| 175 |
/* Output the Nemesis API version */ |
| 176 |
float api_ver = NEMESIS_API_VERSION; |
| 177 |
if ((status = nc_put_att_float(exoid, NC_GLOBAL, ATT_NEM_API_VERSION, NC_FLOAT, 1, &api_ver)) != |
| 178 |
NC_NOERR) { |
| 179 |
char errmsg[MAX_ERR_LENGTH]; |
| 180 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output nemesis api version in file ID %d", |
| 181 |
exoid); |
| 182 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 183 |
EX_FUNC_LEAVE(EX_FATAL); |
| 184 |
} |
| 185 |
} |
| 186 |
EX_FUNC_LEAVE(EX_NOERR); |
| 187 |
} |
| 188 |
|
| 189 |
/*****************************************************************************/ |
| 190 |
/*****************************************************************************/ |
| 191 |
/*****************************************************************************/ |
| 192 |
/* This function checks that the version info is correct. |
| 193 |
*/ |
| 194 |
/*****************************************************************************/ |
| 195 |
int nei_check_file_version(int exoid) |
| 196 |
{ |
| 197 |
#if 0 |
| 198 |
float file_ver; |
| 199 |
|
| 200 |
int status; |
| 201 |
char errmsg[MAX_ERR_LENGTH]; |
| 202 |
|
| 203 |
EX_FUNC_ENTER(); |
| 204 |
|
| 205 |
/* Get the file version */ |
| 206 |
if ((status = nc_get_att_float(exoid, NC_GLOBAL, "nemesis_file_version", &file_ver)) != NC_NOERR) { |
| 207 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 208 |
"ERROR: failed to get the nemesis file version from file ID %d", |
| 209 |
exoid); |
| 210 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 211 |
EX_FUNC_LEAVE(EX_FATAL); |
| 212 |
} |
| 213 |
|
| 214 |
if (fabs(NEMESIS_FILE_VERSION-file_ver) > 0.001) { |
| 215 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 216 |
"ERROR: Nemesis version mismatch in file ID %d!\n", exoid); |
| 217 |
ex_err_fn(exoid, __func__, errmsg, EX_MSG); |
| 218 |
EX_FUNC_LEAVE(EX_FATAL); |
| 219 |
} |
| 220 |
EX_FUNC_LEAVE(EX_NOERR); |
| 221 |
#else |
| 222 |
EX_UNUSED(exoid); |
| 223 |
return EX_NOERR; |
| 224 |
#endif |
| 225 |
} |
| 226 |
|
| 227 |
/*****************************************************************************/ |
| 228 |
/*****************************************************************************/ |
| 229 |
/*****************************************************************************/ |
| 230 |
/* This function gets the index for the given variable at the |
| 231 |
* position given. |
| 232 |
*/ |
| 233 |
/*****************************************************************************/ |
| 234 |
int ex_get_idx(int exoid, const char *ne_var_name, int64_t *my_index, int pos) |
| 235 |
{ |
| 236 |
int varid; |
| 237 |
size_t start[1], count[1]; |
| 238 |
#if NC_HAS_HDF5 |
| 239 |
long long varidx[2]; |
| 240 |
#else |
| 241 |
int varidx[2]; |
| 242 |
#endif |
| 243 |
char errmsg[MAX_ERR_LENGTH]; |
| 244 |
/*-----------------------------Execution begins-----------------------------*/ |
| 245 |
EX_FUNC_ENTER(); |
| 246 |
|
| 247 |
/* set default values for idx */ |
| 248 |
my_index[0] = 0; |
| 249 |
my_index[1] = -1; |
| 250 |
|
| 251 |
/* |
| 252 |
* assume that if there is an error returned, that this |
| 253 |
* means that this is a parallel file, and the index does |
| 254 |
* not exists. This is not an error |
| 255 |
*/ |
| 256 |
if (nc_inq_varid(exoid, ne_var_name, &varid) == NC_NOERR) { |
| 257 |
/* check if we are at the beginning of the index vector */ |
| 258 |
if (pos == 0) { |
| 259 |
start[0] = pos; |
| 260 |
count[0] = 1; |
| 261 |
} |
| 262 |
else { |
| 263 |
start[0] = pos - 1; |
| 264 |
count[0] = 2; |
| 265 |
} |
| 266 |
|
| 267 |
#if NC_HAS_HDF5 |
| 268 |
int status = nc_get_vara_longlong(exoid, varid, start, count, varidx); |
| 269 |
#else |
| 270 |
int status = nc_get_vara_int(exoid, varid, start, count, varidx); |
| 271 |
#endif |
| 272 |
if (status != NC_NOERR) { |
| 273 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable \"%s\" in file ID %d", |
| 274 |
ne_var_name, exoid); |
| 275 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 276 |
EX_FUNC_LEAVE(-1); |
| 277 |
} |
| 278 |
|
| 279 |
if (pos == 0) { |
| 280 |
my_index[0] = 0; |
| 281 |
my_index[1] = varidx[0]; |
| 282 |
} |
| 283 |
else { |
| 284 |
my_index[0] = varidx[0]; |
| 285 |
my_index[1] = varidx[1]; |
| 286 |
} |
| 287 |
} |
| 288 |
EX_FUNC_LEAVE(1); |
| 289 |
} |