| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 2023 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 |
#include "exodusII.h" |
| 9 |
#include <inttypes.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> /* for malloc(), free() */ |
| 12 |
|
| 13 |
#define EX_TEST_FILENAME "edgeFace.exo" |
| 14 |
|
| 15 |
#define EXCHECK(funcall, errmsg) \ |
| 16 |
do { \ |
| 17 |
if ((funcall) < 0) { \ |
| 18 |
fprintf(stderr, errmsg); \ |
| 19 |
return 1; \ |
| 20 |
} \ |
| 21 |
} while (0) |
| 22 |
|
| 23 |
#define EXCHKPI(funcall, errmsg, sucmsg, ival) \ |
| 24 |
do { \ |
| 25 |
if ((funcall) < 0) { \ |
| 26 |
fprintf(stderr, errmsg); \ |
| 27 |
return 1; \ |
| 28 |
} \ |
| 29 |
else { \ |
| 30 |
fprintf(stdout, sucmsg, ival); \ |
| 31 |
} \ |
| 32 |
} while (0) |
| 33 |
|
| 34 |
ex_entity_type obj_types[] = {EX_EDGE_BLOCK, EX_FACE_BLOCK, EX_ELEM_BLOCK, EX_NODE_SET, |
| 35 |
EX_EDGE_SET, EX_FACE_SET, EX_SIDE_SET, EX_ELEM_SET, |
| 36 |
EX_NODE_MAP, EX_EDGE_MAP, EX_FACE_MAP, EX_ELEM_MAP}; |
| 37 |
|
| 38 |
ex_inquiry obj_sizes[] = { |
| 39 |
EX_INQ_EDGE_BLK, EX_INQ_FACE_BLK, EX_INQ_ELEM_BLK, EX_INQ_NODE_SETS, |
| 40 |
EX_INQ_EDGE_SETS, EX_INQ_FACE_SETS, EX_INQ_SIDE_SETS, EX_INQ_ELEM_SETS, |
| 41 |
EX_INQ_NODE_MAP, EX_INQ_EDGE_MAP, EX_INQ_FACE_MAP, EX_INQ_ELEM_MAP, |
| 42 |
}; |
| 43 |
|
| 44 |
const char *obj_typenames[] = {" Edge block", " Face block", "Element block", " Node set", |
| 45 |
" Edge set", " Face set", " Side set", " Element set", |
| 46 |
" Node map", " Edge map", " Face map", " Element map"}; |
| 47 |
|
| 48 |
int obj_sizeinq[] = {EX_INQ_EDGE, |
| 49 |
EX_INQ_FACE, |
| 50 |
EX_INQ_ELEM, |
| 51 |
EX_INQ_NS_NODE_LEN, |
| 52 |
EX_INQ_ES_LEN, |
| 53 |
EX_INQ_FS_LEN, |
| 54 |
EX_INQ_SS_ELEM_LEN, |
| 55 |
EX_INQ_ELS_LEN, |
| 56 |
-1, |
| 57 |
-1, |
| 58 |
-1, |
| 59 |
-1}; |
| 60 |
|
| 61 |
#define OBJECT_IS_BLOCK(i) (((i) < 3)) |
| 62 |
#define OBJECT_IS_SET(i) (((i) > 2) && ((i) < 8)) |
| 63 |
|
| 64 |
int cReadEdgeFace(int argc, char **argv) |
| 65 |
{ |
| 66 |
int nids; |
| 67 |
char **var_names; |
| 68 |
int num_vars; /* number of variables per object */ |
| 69 |
int num_entries; /* number of values per variable per object */ |
| 70 |
|
| 71 |
int appWordSize = 8; |
| 72 |
int diskWordSize = 8; |
| 73 |
float exoVersion; |
| 74 |
int exoid = ex_open(EX_TEST_FILENAME, EX_READ, &appWordSize, &diskWordSize, &exoVersion); |
| 75 |
if (exoid <= 0) { |
| 76 |
fprintf(stderr, "Unable to open \"%s\" for reading.\n", EX_TEST_FILENAME); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
ex_init_params modelParams; |
| 81 |
EXCHECK(ex_get_init_ext(exoid, &modelParams), "Unable to read database parameters.\n"); |
| 82 |
|
| 83 |
fprintf(stdout, |
| 84 |
"Title: <%s>\n" |
| 85 |
"Dimension: %" PRId64 "\n" |
| 86 |
"Nodes: %" PRId64 "\n" |
| 87 |
"Edges: %" PRId64 "\n" |
| 88 |
"Faces: %" PRId64 "\n" |
| 89 |
"Elements: %" PRId64 "\n" |
| 90 |
"Edge Blocks: %" PRId64 "\n" |
| 91 |
"Face Blocks: %" PRId64 "\n" |
| 92 |
"Element Blocks: %" PRId64 "\n" |
| 93 |
"Node Sets: %" PRId64 "\n" |
| 94 |
"Edge Sets: %" PRId64 "\n" |
| 95 |
"Face Sets: %" PRId64 "\n" |
| 96 |
"Side Sets: %" PRId64 "\n" |
| 97 |
"Element Sets: %" PRId64 "\n" |
| 98 |
"Node Maps: %" PRId64 "\n" |
| 99 |
"Edge Maps: %" PRId64 "\n" |
| 100 |
"Face Maps: %" PRId64 "\n" |
| 101 |
"Element Maps: %" PRId64 "\n", |
| 102 |
modelParams.title, modelParams.num_dim, modelParams.num_nodes, modelParams.num_edge, |
| 103 |
modelParams.num_face, modelParams.num_elem, modelParams.num_edge_blk, |
| 104 |
modelParams.num_face_blk, modelParams.num_elem_blk, modelParams.num_node_sets, |
| 105 |
modelParams.num_edge_sets, modelParams.num_face_sets, modelParams.num_side_sets, |
| 106 |
modelParams.num_elem_sets, modelParams.num_node_maps, modelParams.num_edge_maps, |
| 107 |
modelParams.num_face_maps, modelParams.num_elem_maps); |
| 108 |
|
| 109 |
int num_timesteps = ex_inquire_int(exoid, EX_INQ_TIME); |
| 110 |
|
| 111 |
/* *** NEW API *** */ |
| 112 |
for (size_t i = 0; i < sizeof(obj_types) / sizeof(obj_types[0]); ++i) { |
| 113 |
int *truth_tab = NULL; |
| 114 |
int have_var_names = 0; |
| 115 |
|
| 116 |
EXCHECK(ex_inquire(exoid, obj_sizes[i], &nids, 0, 0), |
| 117 |
"Object ID list size could not be determined.\n"); |
| 118 |
|
| 119 |
if (!nids) { |
| 120 |
fprintf(stdout, "=== %ss: none\n\n", obj_typenames[i]); |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
fprintf(stdout, "=== %ss: %d\n", obj_typenames[i], nids); |
| 125 |
|
| 126 |
int *ids = (int *)malloc(nids * sizeof(int)); |
| 127 |
char **obj_names = (char **)malloc(nids * sizeof(char *)); |
| 128 |
for (int obj = 0; obj < nids; ++obj) { |
| 129 |
obj_names[obj] = (char *)malloc((MAX_STR_LENGTH + 1) * sizeof(char)); |
| 130 |
} |
| 131 |
|
| 132 |
EXCHECK(ex_get_ids(exoid, obj_types[i], ids), "Could not read object ids.\n"); |
| 133 |
EXCHECK(ex_get_names(exoid, obj_types[i], obj_names), "Could not read object ids.\n"); |
| 134 |
|
| 135 |
if ((OBJECT_IS_BLOCK(i)) || (OBJECT_IS_SET(i))) { |
| 136 |
EXCHECK(ex_get_variable_param(exoid, obj_types[i], &num_vars), |
| 137 |
"Could not read number of variables.\n"); |
| 138 |
|
| 139 |
if (num_vars && num_timesteps > 0) { |
| 140 |
truth_tab = (int *)malloc(num_vars * nids * sizeof(int)); |
| 141 |
EXCHECK(ex_get_truth_table(exoid, obj_types[i], nids, num_vars, truth_tab), |
| 142 |
"Could not read truth table.\n"); |
| 143 |
int *tp = truth_tab; |
| 144 |
fprintf(stdout, "Truth:"); |
| 145 |
for (int obj = 0; obj < nids; ++obj) { |
| 146 |
for (int j = 0; j < num_vars; ++j, ++tp) { |
| 147 |
fprintf(stdout, " %d", *tp); |
| 148 |
} |
| 149 |
fprintf(stdout, "\n "); |
| 150 |
} |
| 151 |
fprintf(stdout, "\n"); |
| 152 |
|
| 153 |
var_names = (char **)malloc(num_vars * sizeof(char *)); |
| 154 |
for (int j = 0; j < num_vars; ++j) { |
| 155 |
var_names[j] = (char *)malloc((MAX_STR_LENGTH + 1) * sizeof(char)); |
| 156 |
} |
| 157 |
|
| 158 |
EXCHECK(ex_get_variable_names(exoid, obj_types[i], num_vars, var_names), |
| 159 |
"Could not read variable names.\n"); |
| 160 |
have_var_names = 1; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if (!have_var_names) { |
| 165 |
var_names = NULL; |
| 166 |
} |
| 167 |
|
| 168 |
for (int obj = 0; obj < nids; ++obj) { |
| 169 |
if (obj_names[obj]) { |
| 170 |
fprintf(stdout, "%s %3d (%s): ", obj_typenames[i], ids[obj], obj_names[obj]); |
| 171 |
} |
| 172 |
else { |
| 173 |
fprintf(stdout, "%s %3d: ", obj_typenames[i], ids[obj]); |
| 174 |
} |
| 175 |
|
| 176 |
if (OBJECT_IS_BLOCK(i)) { |
| 177 |
int *nconn; |
| 178 |
int *econn; |
| 179 |
int *fconn; |
| 180 |
int ele; |
| 181 |
int ctr; |
| 182 |
int num_attrs; |
| 183 |
int itmp[4]; |
| 184 |
if (obj_types[i] == EX_ELEM_BLOCK) { |
| 185 |
EXCHECK(ex_get_block(exoid, obj_types[i], ids[obj], 0, itmp, itmp + 1, itmp + 2, itmp + 3, |
| 186 |
&num_attrs), |
| 187 |
"Could not read block params.\n"); |
| 188 |
fprintf(stdout, |
| 189 |
"Entries: %3d Nodes/entry: %d Edges/entry: %d Faces/entry: %d Attributes: %d", |
| 190 |
itmp[0], itmp[1], itmp[2], itmp[3], num_attrs); |
| 191 |
} |
| 192 |
else { |
| 193 |
EXCHECK(ex_get_block(exoid, obj_types[i], ids[obj], 0, itmp, itmp + 1, 0, 0, &num_attrs), |
| 194 |
"Could not read block params.\n"); |
| 195 |
fprintf(stdout, "Entries: %3d Nodes/entry: %d Attributes: %d", itmp[0], itmp[1], |
| 196 |
num_attrs); |
| 197 |
itmp[2] = itmp[3] = 0; |
| 198 |
} |
| 199 |
fprintf(stdout, "\n "); |
| 200 |
num_entries = itmp[0]; |
| 201 |
nconn = itmp[1] ? (int *)malloc(itmp[1] * num_entries * sizeof(int)) : NULL; |
| 202 |
econn = itmp[2] ? (int *)malloc(itmp[2] * num_entries * sizeof(int)) : NULL; |
| 203 |
fconn = itmp[3] ? (int *)malloc(itmp[3] * num_entries * sizeof(int)) : NULL; |
| 204 |
EXCHECK(ex_get_conn(exoid, obj_types[i], ids[obj], nconn, econn, fconn), |
| 205 |
"Could not read connectivity.\n"); |
| 206 |
for (ele = 0; ele < num_entries; ++ele) { |
| 207 |
for (ctr = 0; ctr < itmp[1]; ++ctr) { |
| 208 |
fprintf(stdout, " %2d", nconn[ele * itmp[1] + ctr]); |
| 209 |
} |
| 210 |
if (itmp[2]) { |
| 211 |
fprintf(stdout, " ++"); |
| 212 |
for (ctr = 0; ctr < itmp[2]; ++ctr) { |
| 213 |
fprintf(stdout, " %2d", econn[ele * itmp[2] + ctr]); |
| 214 |
} |
| 215 |
} |
| 216 |
if (itmp[3]) { |
| 217 |
fprintf(stdout, " ++"); |
| 218 |
for (ctr = 0; ctr < itmp[3]; ++ctr) { |
| 219 |
fprintf(stdout, " %2d", fconn[ele * itmp[3] + ctr]); |
| 220 |
} |
| 221 |
} |
| 222 |
fprintf(stdout, "\n "); |
| 223 |
} |
| 224 |
free(nconn); |
| 225 |
free(econn); |
| 226 |
free(fconn); |
| 227 |
|
| 228 |
if (num_attrs) { |
| 229 |
char **attr_names; |
| 230 |
double *attr; |
| 231 |
attr = (double *)malloc(num_entries * num_attrs * sizeof(double)); |
| 232 |
attr_names = (char **)malloc(num_attrs * sizeof(char *)); |
| 233 |
for (int j = 0; j < num_attrs; ++j) { |
| 234 |
attr_names[j] = (char *)malloc((MAX_STR_LENGTH + 1) * sizeof(char)); |
| 235 |
} |
| 236 |
|
| 237 |
EXCHECK(ex_get_attr_names(exoid, obj_types[i], ids[obj], attr_names), |
| 238 |
"Could not read attributes names.\n"); |
| 239 |
EXCHECK(ex_get_attr(exoid, obj_types[i], ids[obj], attr), |
| 240 |
"Could not read attribute values.\n"); |
| 241 |
|
| 242 |
fprintf(stdout, "\n Attributes:\n ID "); |
| 243 |
for (int j = 0; j < num_attrs; ++j) { |
| 244 |
fprintf(stdout, " %s", attr_names[j]); |
| 245 |
} |
| 246 |
fprintf(stdout, "\n"); |
| 247 |
for (int j = 0; j < num_entries; ++j) { |
| 248 |
fprintf(stdout, " %2d ", j + 1); |
| 249 |
for (int k = 0; k < num_attrs; ++k) { |
| 250 |
fprintf(stdout, " %4.1f", attr[j * num_attrs + k]); |
| 251 |
} |
| 252 |
fprintf(stdout, "\n"); |
| 253 |
} |
| 254 |
|
| 255 |
for (int j = 0; j < num_attrs; ++j) { |
| 256 |
free(attr_names[j]); |
| 257 |
} |
| 258 |
free(attr_names); |
| 259 |
free(attr); |
| 260 |
} |
| 261 |
} |
| 262 |
else if (OBJECT_IS_SET(i)) { |
| 263 |
int num_df; |
| 264 |
EXCHECK(ex_get_set_param(exoid, obj_types[i], ids[obj], &num_entries, &num_df), |
| 265 |
"Could not read set parameters.\n"); |
| 266 |
|
| 267 |
int *set_entry = (int *)malloc(num_entries * sizeof(int)); |
| 268 |
int *set_extra = (obj_types[i] != EX_NODE_SET && obj_types[i] != EX_ELEM_SET) |
| 269 |
? (int *)malloc(num_entries * sizeof(int)) |
| 270 |
: NULL; |
| 271 |
EXCHECK(ex_get_set(exoid, obj_types[i], ids[obj], set_entry, set_extra), |
| 272 |
"Could not read set.\n"); |
| 273 |
fprintf(stdout, "Entries: %3d Distribution factors: %3d\n", num_entries, num_df); |
| 274 |
if (set_extra) { |
| 275 |
for (int j = 0; j < num_entries; ++j) { |
| 276 |
fprintf(stdout, " %2d %2d\n", set_entry[j], set_extra[j]); |
| 277 |
} |
| 278 |
} |
| 279 |
else { |
| 280 |
for (int j = 0; j < num_entries; ++j) { |
| 281 |
fprintf(stdout, " %2d\n", set_entry[j]); |
| 282 |
} |
| 283 |
} |
| 284 |
free(set_entry); |
| 285 |
free(set_extra); |
| 286 |
|
| 287 |
double *set_df = num_df ? (double *)malloc(num_df * sizeof(double)) : NULL; |
| 288 |
if (set_df) { |
| 289 |
EXCHECK(ex_get_set_dist_fact(exoid, obj_types[i], ids[obj], set_df), |
| 290 |
"Could not read set distribution factors.\n"); |
| 291 |
fprintf(stdout, "\n Distribution factors:\n"); |
| 292 |
for (int j = 0; j < num_df; ++j) { |
| 293 |
fprintf(stdout, " %4.1f\n", set_df[j]); |
| 294 |
} |
| 295 |
free(set_df); |
| 296 |
} |
| 297 |
} |
| 298 |
else { /* object is map */ |
| 299 |
switch (obj_types[i]) { |
| 300 |
case EX_NODE_MAP: num_entries = modelParams.num_nodes; break; |
| 301 |
case EX_EDGE_MAP: num_entries = modelParams.num_edge; break; |
| 302 |
case EX_FACE_MAP: num_entries = modelParams.num_face; break; |
| 303 |
case EX_ELEM_MAP: num_entries = modelParams.num_elem; break; |
| 304 |
default: num_entries = 0; |
| 305 |
} |
| 306 |
if (num_entries) { |
| 307 |
fprintf(stdout, "Entries: %3d\n :", num_entries); |
| 308 |
int *map = (int *)malloc(num_entries * sizeof(int)); |
| 309 |
EXCHECK(ex_get_num_map(exoid, obj_types[i], ids[obj], map), "Could not read map.\n"); |
| 310 |
for (int j = 0; j < num_entries; ++j) { |
| 311 |
fprintf(stdout, " %d", map[j]); |
| 312 |
} |
| 313 |
free(map); |
| 314 |
} |
| 315 |
else { |
| 316 |
fprintf(stdout, "Entries: none"); |
| 317 |
} |
| 318 |
} |
| 319 |
fprintf(stdout, "\n"); |
| 320 |
|
| 321 |
/* Read results variables */ |
| 322 |
if (((OBJECT_IS_BLOCK(i)) || (OBJECT_IS_SET(i))) && num_vars && num_timesteps > 0) { |
| 323 |
/* Print out all the time values to exercise get_var */ |
| 324 |
double *entry_vals = (double *)malloc(num_entries * sizeof(double)); |
| 325 |
for (int j = 0; j < num_vars; ++j) { |
| 326 |
if (!truth_tab[num_vars * obj + j]) { |
| 327 |
continue; |
| 328 |
} |
| 329 |
|
| 330 |
fprintf(stdout, " Variable: %s", var_names[j]); |
| 331 |
for (int ti = 1; ti <= num_timesteps; ++ti) { |
| 332 |
EXCHECK(ex_get_var(exoid, ti, obj_types[i], 1 + j, ids[obj], num_entries, entry_vals), |
| 333 |
"Could not read variable values.\n"); |
| 334 |
|
| 335 |
fprintf(stdout, "\n @t%d ", ti); |
| 336 |
for (int k = 0; k < num_entries; ++k) { |
| 337 |
fprintf(stdout, " %4.1f", entry_vals[k]); |
| 338 |
} |
| 339 |
} |
| 340 |
fprintf(stdout, "\n"); |
| 341 |
} |
| 342 |
fprintf(stdout, "\n"); |
| 343 |
free(entry_vals); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if (((OBJECT_IS_BLOCK(i)) || (OBJECT_IS_SET(i))) && num_vars && num_timesteps > 0) { |
| 348 |
/* Print out one element's time values to exercise get_var_time */ |
| 349 |
double *entry_vals = (double *)malloc(num_timesteps * sizeof(double)); |
| 350 |
int itmp[2]; |
| 351 |
EXCHECK(ex_inquire(exoid, obj_sizeinq[i], itmp, 0, 0), "Inquire failed.\n"); |
| 352 |
itmp[1] = 11; |
| 353 |
while (itmp[1] > itmp[0]) { |
| 354 |
itmp[1] /= 2; |
| 355 |
} |
| 356 |
for (int j = 0; j < num_vars; ++j) { |
| 357 |
/* FIXME: This works for the dataset created by CreateEdgeFace, but not for any dataset in |
| 358 |
* general since |
| 359 |
* NULL truth table entries may mean the referenced elements don't have variable values. |
| 360 |
*/ |
| 361 |
EXCHECK(ex_get_var_time(exoid, obj_types[i], j + 1, itmp[1], 1, num_timesteps, entry_vals), |
| 362 |
"Could not read variable over time.\n"); |
| 363 |
fprintf(stdout, " Variable over time: %s Entry: %3d ", var_names[j], itmp[1]); |
| 364 |
for (int ti = 1; ti <= num_timesteps; ++ti) { |
| 365 |
fprintf(stdout, " @t%d: %4.1f", ti, entry_vals[ti - 1]); |
| 366 |
} |
| 367 |
fprintf(stdout, "\n"); |
| 368 |
} |
| 369 |
free(entry_vals); |
| 370 |
} |
| 371 |
|
| 372 |
if (var_names) { |
| 373 |
for (int j = 0; j < num_vars; ++j) { |
| 374 |
free(var_names[j]); |
| 375 |
} |
| 376 |
free(var_names); |
| 377 |
} |
| 378 |
free(truth_tab); |
| 379 |
free(ids); |
| 380 |
|
| 381 |
for (int obj = 0; obj < nids; ++obj) { |
| 382 |
free(obj_names[obj]); |
| 383 |
} |
| 384 |
free(obj_names); |
| 385 |
|
| 386 |
fprintf(stdout, "\n"); |
| 387 |
} |
| 388 |
|
| 389 |
EXCHECK(ex_close(exoid), "Unable to close database.\n"); |
| 390 |
|
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
#if !defined(USING_CMAKE) |
| 395 |
int main(int argc, char *argv[]) { return cReadEdgeFace(argc, argv); } |
| 396 |
#endif |