| 1 |
/* |
| 2 |
* Copyright(C) 1999-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" // for ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 11 |
#include <assert.h> |
| 12 |
#include <stdbool.h> |
| 13 |
|
| 14 |
static const char *exi_get_metadata_attribute(const char *name, const char *prefix, int pre_len) |
| 15 |
{ |
| 16 |
/* |
| 17 |
* Each field or basis attribute metadata attribute consists of 2 or more attributes. |
| 18 |
* Return the string corresponding to {type} in an attribute of the form "Field@{name}@{type}" |
| 19 |
* or "Basis@{name}@{type}" or "Quad@{name}@{type}". |
| 20 |
*/ |
| 21 |
|
| 22 |
if (strncmp(name, prefix, pre_len) == 0) { |
| 23 |
/* Return the suffix (if any) following the last "@" */ |
| 24 |
char *suffix = strrchr(name, '@'); |
| 25 |
if (suffix != NULL) { |
| 26 |
suffix++; |
| 27 |
return suffix; |
| 28 |
} |
| 29 |
} |
| 30 |
return NULL; |
| 31 |
} |
| 32 |
|
| 33 |
static const char *exi_get_attribute_metadata_name(const char *attrib, int offset) |
| 34 |
{ |
| 35 |
/* |
| 36 |
* PRECONDITION: `attrib` is a basis or field metadata attribute of the form |
| 37 |
* "Basis@{name}@{type}" or "Field@{name}@{type}" `offset` is the length |
| 38 |
* of `Basis@` or `Field@` or `Quad@` |
| 39 |
* |
| 40 |
* Returns the `{name}` portion in `name` |
| 41 |
*/ |
| 42 |
static char name[EX_MAX_NAME + 1]; |
| 43 |
memset(name, '\0', EX_MAX_NAME + 1); |
| 44 |
for (int i = 0; attrib[i + offset] != '@'; i++) { |
| 45 |
name[i] = attrib[i + offset]; |
| 46 |
} |
| 47 |
return name; |
| 48 |
} |
| 49 |
|
| 50 |
static int exi_get_attribute_count(int exoid, ex_entity_type obj_type, ex_entity_id id, int *varid) |
| 51 |
{ |
| 52 |
int att_count = 0; |
| 53 |
int status; |
| 54 |
|
| 55 |
if (obj_type == EX_GLOBAL) { |
| 56 |
*varid = NC_GLOBAL; |
| 57 |
|
| 58 |
if ((status = nc_inq(exoid, NULL, NULL, &att_count, NULL)) != NC_NOERR) { |
| 59 |
char errmsg[MAX_ERR_LENGTH]; |
| 60 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get GLOBAL attribute count"); |
| 61 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 62 |
return EX_FATAL; |
| 63 |
} |
| 64 |
} |
| 65 |
else { |
| 66 |
*varid = exi_get_varid(exoid, obj_type, id); |
| 67 |
if (*varid <= 0) { |
| 68 |
/* Error message handled in exi_get_varid */ |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
if ((status = nc_inq_var(exoid, *varid, NULL, NULL, NULL, NULL, &att_count)) != NC_NOERR) { |
| 73 |
char errmsg[MAX_ERR_LENGTH]; |
| 74 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 75 |
"ERROR: failed to get attribute count on %s with id %" PRId64, |
| 76 |
ex_name_of_object(obj_type), id); |
| 77 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 78 |
return EX_FATAL; |
| 79 |
} |
| 80 |
} |
| 81 |
return att_count; |
| 82 |
} |
| 83 |
|
| 84 |
int ex_get_field_metadata_count(int exoid, ex_entity_type obj_type, ex_entity_id id) |
| 85 |
{ |
| 86 |
EX_FUNC_ENTER(); |
| 87 |
|
| 88 |
int varid; |
| 89 |
int att_count = exi_get_attribute_count(exoid, obj_type, id, &varid); |
| 90 |
if (att_count < 0) { |
| 91 |
char errmsg[MAX_ERR_LENGTH]; |
| 92 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Negative attribute count (%d) on %s with id %" PRId64, |
| 93 |
att_count, ex_name_of_object(obj_type), id); |
| 94 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 95 |
EX_FUNC_LEAVE(EX_FATAL); |
| 96 |
} |
| 97 |
|
| 98 |
/* Get names of each attribute and see if it is a 'Field metadata' name */ |
| 99 |
int count = 0; |
| 100 |
for (int i = 0; i < att_count; i++) { |
| 101 |
char name[EX_MAX_NAME + 1]; |
| 102 |
int status; |
| 103 |
if ((status = nc_inq_attname(exoid, varid, i, name)) != NC_NOERR) { |
| 104 |
char errmsg[MAX_ERR_LENGTH]; |
| 105 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 106 |
"ERROR: failed to get attribute named %s on %s with id %" PRId64, name, |
| 107 |
ex_name_of_object(obj_type), id); |
| 108 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 109 |
EX_FUNC_LEAVE(EX_FATAL); |
| 110 |
} |
| 111 |
const char *type = exi_get_metadata_attribute(name, "Field@", 6); |
| 112 |
if (type != NULL && strcmp("type", type) == 0) { |
| 113 |
count++; |
| 114 |
} |
| 115 |
} |
| 116 |
EX_FUNC_LEAVE(count); |
| 117 |
} |
| 118 |
|
| 119 |
/*! Get the values for the specified attribute. */ |
| 120 |
int ex_get_field_metadata(int exoid, ex_field *field) |
| 121 |
{ |
| 122 |
EX_FUNC_ENTER(); |
| 123 |
|
| 124 |
int varid; |
| 125 |
int att_count = exi_get_attribute_count(exoid, field[0].entity_type, field[0].entity_id, &varid); |
| 126 |
if (att_count < 0) { |
| 127 |
char errmsg[MAX_ERR_LENGTH]; |
| 128 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Negative attribute count (%d) on %s with id %" PRId64, |
| 129 |
att_count, ex_name_of_object(field[0].entity_type), field[0].entity_id); |
| 130 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 131 |
EX_FUNC_LEAVE(EX_FATAL); |
| 132 |
} |
| 133 |
|
| 134 |
/* Iterate through each Field metadata field and populate `field` */ |
| 135 |
int count = 0; |
| 136 |
for (int i = 0; i < att_count; i++) { |
| 137 |
char attr_name[EX_MAX_NAME + 1]; |
| 138 |
int status; |
| 139 |
if ((status = nc_inq_attname(exoid, varid, i, attr_name)) != NC_NOERR) { |
| 140 |
char errmsg[MAX_ERR_LENGTH]; |
| 141 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 142 |
"ERROR: failed to get attribute named %s on %s with id %" PRId64, attr_name, |
| 143 |
ex_name_of_object(field[0].entity_type), field[0].entity_id); |
| 144 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 145 |
EX_FUNC_LEAVE(EX_FATAL); |
| 146 |
} |
| 147 |
|
| 148 |
const char *fld_type = exi_get_metadata_attribute(attr_name, "Field@", 6); |
| 149 |
if (fld_type != NULL) { |
| 150 |
/* Get the field name. We know that the `name` is of the form "Field@{name}@{item}" */ |
| 151 |
const char *fld_name = exi_get_attribute_metadata_name(attr_name, 6); |
| 152 |
|
| 153 |
/* If this is the first time we have seen this `fld_name`, then increment count and |
| 154 |
* store the name */ |
| 155 |
int found = -1; |
| 156 |
int which = 0; |
| 157 |
for (int ii = 0; ii < count; ii++) { |
| 158 |
if (strcmp(field[ii].name, fld_name) == 0) { |
| 159 |
found = ii; |
| 160 |
which = ii; |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (found == -1) { |
| 166 |
which = count; |
| 167 |
strcpy(field[count].name, fld_name); |
| 168 |
/* Set default separator type... */ |
| 169 |
field[count].component_separator[0] = '_'; |
| 170 |
field[count].component_separator[1] = '\0'; |
| 171 |
|
| 172 |
count++; |
| 173 |
} |
| 174 |
|
| 175 |
nc_type type; /* integer, double, character, ... */ |
| 176 |
size_t val_count; /* how many `type` values */ |
| 177 |
if ((status = nc_inq_att(exoid, varid, attr_name, &type, &val_count)) != NC_NOERR) { |
| 178 |
char errmsg[MAX_ERR_LENGTH]; |
| 179 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 180 |
"ERROR: failed to get parameters for attribute named %s on %s with id %" PRId64, |
| 181 |
attr_name, ex_name_of_object(field[0].entity_type), field[0].entity_id); |
| 182 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 183 |
EX_FUNC_LEAVE(EX_FATAL); |
| 184 |
} |
| 185 |
|
| 186 |
if (strcmp(fld_type, "type") == 0) { |
| 187 |
status = nc_get_att_int(exoid, varid, attr_name, (int *)field[which].type); |
| 188 |
if (field[which].nesting == 0) { |
| 189 |
field[which].nesting = val_count; |
| 190 |
} |
| 191 |
} |
| 192 |
else if (strcmp(fld_type, "separator") == 0) { |
| 193 |
status = nc_get_att_text(exoid, varid, attr_name, field[which].component_separator); |
| 194 |
} |
| 195 |
else if (strcmp(fld_type, "cardinality") == 0) { |
| 196 |
status = nc_get_att_int(exoid, varid, attr_name, field[which].cardinality); |
| 197 |
if (field[which].nesting == 0) { |
| 198 |
field[which].nesting = val_count; |
| 199 |
} |
| 200 |
} |
| 201 |
else if (strcmp(fld_type, "type_name") == 0) { |
| 202 |
status = nc_get_att_text(exoid, varid, attr_name, field[which].type_name); |
| 203 |
} |
| 204 |
else if (strcmp(fld_type, "suffices") == 0) { |
| 205 |
status = nc_get_att_text(exoid, varid, attr_name, field[which].suffices); |
| 206 |
} |
| 207 |
else { |
| 208 |
char errmsg[MAX_ERR_LENGTH]; |
| 209 |
snprintf( |
| 210 |
errmsg, MAX_ERR_LENGTH, |
| 211 |
"ERROR: Invalid field metadata attribute type %s on field %s on %s with id %" PRId64, |
| 212 |
fld_type, fld_name, ex_name_of_object(field[0].entity_type), field[0].entity_id); |
| 213 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 214 |
EX_FUNC_LEAVE(EX_FATAL); |
| 215 |
} |
| 216 |
if (status != NC_NOERR) { |
| 217 |
char errmsg[MAX_ERR_LENGTH]; |
| 218 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 219 |
"ERROR: failed to read field metadata attribute type %s on field %s on %s with id " |
| 220 |
"%" PRId64, |
| 221 |
fld_type, fld_name, ex_name_of_object(field[0].entity_type), field[0].entity_id); |
| 222 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 223 |
EX_FUNC_LEAVE(EX_FATAL); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
EX_FUNC_LEAVE(EX_NOERR); |
| 228 |
} |
| 229 |
|
| 230 |
int exi_get_metadata_count(int exoid, const char *which) |
| 231 |
{ |
| 232 |
EX_FUNC_ENTER(); |
| 233 |
|
| 234 |
int varid; |
| 235 |
int att_count = exi_get_attribute_count(exoid, EX_GLOBAL, 0, &varid); |
| 236 |
if (att_count < 0) { |
| 237 |
char errmsg[MAX_ERR_LENGTH]; |
| 238 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Negative attribute count (%d)", att_count); |
| 239 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 240 |
EX_FUNC_LEAVE(EX_FATAL); |
| 241 |
} |
| 242 |
|
| 243 |
/* Get names of each attribute and see if it is a `which` metadata name */ |
| 244 |
size_t att_len = strlen(which); |
| 245 |
int count = 0; |
| 246 |
for (int i = 0; i < att_count; i++) { |
| 247 |
char name[EX_MAX_NAME + 1]; |
| 248 |
int status; |
| 249 |
if ((status = nc_inq_attname(exoid, varid, i, name)) != NC_NOERR) { |
| 250 |
char errmsg[MAX_ERR_LENGTH]; |
| 251 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get attribute named %s", name); |
| 252 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 253 |
EX_FUNC_LEAVE(EX_FATAL); |
| 254 |
} |
| 255 |
const char *type = exi_get_metadata_attribute(name, which, att_len); |
| 256 |
if (type != NULL && strcmp("cardinality", type) == 0) { |
| 257 |
count++; |
| 258 |
} |
| 259 |
} |
| 260 |
EX_FUNC_LEAVE(count); |
| 261 |
} |
| 262 |
|
| 263 |
int ex_get_basis_count(int exoid) { return exi_get_metadata_count(exoid, "Basis@"); } |
| 264 |
|
| 265 |
int ex_get_quadrature_count(int exoid) { return exi_get_metadata_count(exoid, "Quad@"); } |
| 266 |
|
| 267 |
int ex_get_basis(int exoid, ex_basis **pbasis, int *num_basis) |
| 268 |
{ |
| 269 |
/* |
| 270 |
* -- If this function is called and there is no basis metadata on the |
| 271 |
* entity, it will return EX_NOTFOUND; |
| 272 |
* |
| 273 |
* -- If there are basis defined on the database, it will: |
| 274 |
* - determine number of basis defined on database and return the count in `num_basis` |
| 275 |
* - allocate `num_basis` copies of `ex_basis` |
| 276 |
* - determine the cardinality of each basis and allocate the array members of each basis |
| 277 |
* struct. |
| 278 |
* - populate the array members for each basis. |
| 279 |
* |
| 280 |
* Upon return, the `pbasis` will contain `num_basis` structs fully populated. |
| 281 |
*/ |
| 282 |
|
| 283 |
// TODO: Should all array members of the struct be allocated and initialized to zero, or |
| 284 |
// only the members that are actually defined on the database... |
| 285 |
EX_FUNC_ENTER(); |
| 286 |
|
| 287 |
*num_basis = ex_get_basis_count(exoid); |
| 288 |
if (*num_basis == 0) { |
| 289 |
EX_FUNC_LEAVE(EX_NOTFOUND); |
| 290 |
} |
| 291 |
|
| 292 |
int varid; |
| 293 |
int att_count = exi_get_attribute_count(exoid, EX_GLOBAL, 0, &varid); |
| 294 |
if (att_count < 0) { |
| 295 |
char errmsg[MAX_ERR_LENGTH]; |
| 296 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Negative attribute count (%d)", att_count); |
| 297 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 298 |
EX_FUNC_LEAVE(EX_FATAL); |
| 299 |
} |
| 300 |
|
| 301 |
if (*pbasis == NULL) { |
| 302 |
struct ex_basis *basis = (ex_basis *)calloc(*num_basis, sizeof(struct ex_basis)); |
| 303 |
*pbasis = basis; |
| 304 |
} |
| 305 |
struct ex_basis *basis = *pbasis; |
| 306 |
|
| 307 |
// First, iterate through each attribute and get the basis name and cardinality |
| 308 |
int count = 0; |
| 309 |
for (int att = 0; att < att_count; att++) { |
| 310 |
char attr_name[EX_MAX_NAME + 1]; |
| 311 |
int status; |
| 312 |
if ((status = nc_inq_attname(exoid, varid, att, attr_name)) != NC_NOERR) { |
| 313 |
char errmsg[MAX_ERR_LENGTH]; |
| 314 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get attribute named %s", attr_name); |
| 315 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 316 |
EX_FUNC_LEAVE(EX_FATAL); |
| 317 |
} |
| 318 |
|
| 319 |
if (strncmp("Basis@", attr_name, 6) != 0) { |
| 320 |
continue; |
| 321 |
} |
| 322 |
|
| 323 |
const char *basis_type = exi_get_metadata_attribute(attr_name, "Basis@", 6); |
| 324 |
if (basis_type != NULL && strcmp(basis_type, "cardinality") == 0) { |
| 325 |
/* Get the basis name. We know that the `name` is of the form "Basis@{name}@{item}" */ |
| 326 |
const char *basis_name = exi_get_attribute_metadata_name(attr_name, 6); |
| 327 |
strcpy(basis[count].name, basis_name); |
| 328 |
|
| 329 |
int cardinality = 0; |
| 330 |
status = nc_get_att_int(exoid, varid, attr_name, &cardinality); |
| 331 |
basis[count].cardinality = cardinality; |
| 332 |
count++; |
| 333 |
if (count == *num_basis) { |
| 334 |
break; |
| 335 |
} |
| 336 |
continue; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (count != *num_basis) { |
| 341 |
char errmsg[MAX_ERR_LENGTH]; |
| 342 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 343 |
"ERROR: Internal error populating basis name and cardinality. Did not find correct " |
| 344 |
"number of basis attributes."); |
| 345 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 346 |
EX_FUNC_LEAVE(EX_FATAL); |
| 347 |
} |
| 348 |
|
| 349 |
ex_initialize_basis_struct(basis, *num_basis, 1); |
| 350 |
|
| 351 |
// Now iterate the attributes again and fully populate the basis struct(s) |
| 352 |
for (int att = 0; att < att_count; att++) { |
| 353 |
char attr_name[EX_MAX_NAME + 1]; |
| 354 |
int status; |
| 355 |
if ((status = nc_inq_attname(exoid, varid, att, attr_name)) != NC_NOERR) { |
| 356 |
char errmsg[MAX_ERR_LENGTH]; |
| 357 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get attribute named %s", attr_name); |
| 358 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 359 |
EX_FUNC_LEAVE(EX_FATAL); |
| 360 |
} |
| 361 |
|
| 362 |
if (strncmp("Basis@", attr_name, 6) != 0) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
const char *basis_type = exi_get_metadata_attribute(attr_name, "Basis@", 6); |
| 367 |
if (basis_type != NULL) { |
| 368 |
/* Get the basis name. We know that the `name` is of the form "Basis@{name}@{item}" */ |
| 369 |
const char *basis_name = exi_get_attribute_metadata_name(attr_name, 6); |
| 370 |
|
| 371 |
// There is no guarantee that we will be getting names in the same order as above since |
| 372 |
// attributes can be in any order. But, the name and cardinality for each basis will already |
| 373 |
// be set, so we just need to find the correct one... |
| 374 |
int which = -1; |
| 375 |
for (int i = 0; i < *num_basis; i++) { |
| 376 |
if (strcmp(basis[i].name, basis_name) == 0) { |
| 377 |
which = i; |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (which == -1) { |
| 383 |
// Internal error... |
| 384 |
} |
| 385 |
|
| 386 |
if (strcmp(basis_type, "cardinality") == 0) { |
| 387 |
// Cardinality already set; skip |
| 388 |
} |
| 389 |
else if (strcmp(basis_type, "subc_dim") == 0) { |
| 390 |
status = nc_get_att_int(exoid, varid, attr_name, basis[which].subc_dim); |
| 391 |
} |
| 392 |
else if (status == NC_NOERR && strcmp(basis_type, "subc_ordinal") == 0) { |
| 393 |
status = nc_get_att_int(exoid, varid, attr_name, basis[which].subc_ordinal); |
| 394 |
} |
| 395 |
else if (status == NC_NOERR && strcmp(basis_type, "subc_dof_ordinal") == 0) { |
| 396 |
status = nc_get_att_int(exoid, varid, attr_name, basis[which].subc_dof_ordinal); |
| 397 |
} |
| 398 |
else if (status == NC_NOERR && strcmp(basis_type, "subc_num_dof") == 0) { |
| 399 |
status = nc_get_att_int(exoid, varid, attr_name, basis[which].subc_num_dof); |
| 400 |
} |
| 401 |
else if (status == NC_NOERR && strcmp(basis_type, "xi") == 0) { |
| 402 |
status = nc_get_att_double(exoid, varid, attr_name, basis[which].xi); |
| 403 |
} |
| 404 |
else if (status == NC_NOERR && strcmp(basis_type, "eta") == 0) { |
| 405 |
status = nc_get_att_double(exoid, varid, attr_name, basis[which].eta); |
| 406 |
} |
| 407 |
else if (status == NC_NOERR && strcmp(basis_type, "zeta") == 0) { |
| 408 |
status = nc_get_att_double(exoid, varid, attr_name, basis[which].zeta); |
| 409 |
} |
| 410 |
|
| 411 |
if (status != NC_NOERR) { |
| 412 |
char errmsg[MAX_ERR_LENGTH]; |
| 413 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to read Basis %s metadata", |
| 414 |
basis[which].name); |
| 415 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 416 |
EX_FUNC_LEAVE(EX_FATAL); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
EX_FUNC_LEAVE(EX_NOERR); |
| 421 |
} |
| 422 |
|
| 423 |
int ex_get_quadrature(int exoid, ex_quadrature **pquad, int *num_quad) |
| 424 |
{ |
| 425 |
/* |
| 426 |
* -- If this function is called and there is no quadrature metadata on the |
| 427 |
* entity, it will return EX_NOTFOUND; |
| 428 |
* |
| 429 |
* -- If there are quadrature defined on the database, it will: |
| 430 |
* - determine number of quadrature defined on database and return the count in `num_quad` |
| 431 |
* - allocate `num_quad` copies of `ex_quad` |
| 432 |
* - determine the cardinality of each quadrature and allocate the array members of each |
| 433 |
* quadrature struct. |
| 434 |
* - populate the array members for each quadrature. |
| 435 |
* |
| 436 |
* Upon return, the `pquad` will contain `num_basis` structs fully populated. |
| 437 |
*/ |
| 438 |
|
| 439 |
// TODO: Should all array members of the struct(xi, eta, zeta, |
| 440 |
// weight) be allocated and initialized to zero, or only the |
| 441 |
// members that are actually defined on the database... |
| 442 |
EX_FUNC_ENTER(); |
| 443 |
|
| 444 |
*num_quad = ex_get_quadrature_count(exoid); |
| 445 |
if (*num_quad == 0) { |
| 446 |
EX_FUNC_LEAVE(EX_NOTFOUND); |
| 447 |
} |
| 448 |
|
| 449 |
int varid; |
| 450 |
int att_count = exi_get_attribute_count(exoid, EX_GLOBAL, 0, &varid); |
| 451 |
if (att_count < 0) { |
| 452 |
char errmsg[MAX_ERR_LENGTH]; |
| 453 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Negative attribute count (%d)", att_count); |
| 454 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 455 |
EX_FUNC_LEAVE(EX_FATAL); |
| 456 |
} |
| 457 |
|
| 458 |
if (*pquad == NULL) { |
| 459 |
struct ex_quadrature *quad = (ex_quadrature *)calloc(*num_quad, sizeof(struct ex_quadrature)); |
| 460 |
*pquad = quad; |
| 461 |
} |
| 462 |
struct ex_quadrature *quad = *pquad; |
| 463 |
|
| 464 |
// First, iterate through each attribute and get the quadrature name and cardinality |
| 465 |
int count = 0; |
| 466 |
for (int att = 0; att < att_count; att++) { |
| 467 |
char attr_name[EX_MAX_NAME + 1]; |
| 468 |
int status; |
| 469 |
if ((status = nc_inq_attname(exoid, varid, att, attr_name)) != NC_NOERR) { |
| 470 |
char errmsg[MAX_ERR_LENGTH]; |
| 471 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get attribute named %s", attr_name); |
| 472 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 473 |
EX_FUNC_LEAVE(EX_FATAL); |
| 474 |
} |
| 475 |
|
| 476 |
if (strncmp("Quad@", attr_name, 5) != 0) { |
| 477 |
continue; |
| 478 |
} |
| 479 |
|
| 480 |
const char *quadrature_type = exi_get_metadata_attribute(attr_name, "Quad@", 5); |
| 481 |
if (quadrature_type != NULL && strcmp(quadrature_type, "cardinality") == 0) { |
| 482 |
/* Get the quadrature name. We know that the `name` is of the form "Quad@{name}@{item}" */ |
| 483 |
const char *quadrature_name = exi_get_attribute_metadata_name(attr_name, 5); |
| 484 |
strcpy(quad[count].name, quadrature_name); |
| 485 |
|
| 486 |
int cardinality = 0; |
| 487 |
status = nc_get_att_int(exoid, varid, attr_name, &cardinality); |
| 488 |
quad[count].cardinality = cardinality; |
| 489 |
count++; |
| 490 |
if (count == *num_quad) { |
| 491 |
break; |
| 492 |
} |
| 493 |
continue; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if (count != *num_quad) { |
| 498 |
char errmsg[MAX_ERR_LENGTH]; |
| 499 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 500 |
"ERROR: Internal error populating quadrature name and cardinality. Did not find " |
| 501 |
"correct number of quadrature attributes."); |
| 502 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 503 |
EX_FUNC_LEAVE(EX_FATAL); |
| 504 |
} |
| 505 |
|
| 506 |
ex_initialize_quadrature_struct(quad, *num_quad, 1); |
| 507 |
|
| 508 |
// Now iterate the attributes again and fully populate the quadrature struct(s) |
| 509 |
for (int att = 0; att < att_count; att++) { |
| 510 |
char attr_name[EX_MAX_NAME + 1]; |
| 511 |
int status; |
| 512 |
if ((status = nc_inq_attname(exoid, varid, att, attr_name)) != NC_NOERR) { |
| 513 |
char errmsg[MAX_ERR_LENGTH]; |
| 514 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get attribute named %s", attr_name); |
| 515 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 516 |
EX_FUNC_LEAVE(EX_FATAL); |
| 517 |
} |
| 518 |
|
| 519 |
if (strncmp("Quad@", attr_name, 5) != 0) { |
| 520 |
continue; |
| 521 |
} |
| 522 |
|
| 523 |
const char *quadrature_type = exi_get_metadata_attribute(attr_name, "Quad@", 5); |
| 524 |
if (quadrature_type != NULL) { |
| 525 |
|
| 526 |
/* Get the quadrature name. We know that the `name` is of the form "Quad@{name}@{item}" */ |
| 527 |
const char *quadrature_name = exi_get_attribute_metadata_name(attr_name, 5); |
| 528 |
|
| 529 |
// There is no guarantee that we will be getting names in the same order as above since |
| 530 |
// attributes can be in any order. But, the name and cardinality for each quadrature will |
| 531 |
// already be set, so we just need to find the correct one... |
| 532 |
int which = -1; |
| 533 |
for (int i = 0; i < *num_quad; i++) { |
| 534 |
if (strcmp(quad[i].name, quadrature_name) == 0) { |
| 535 |
which = i; |
| 536 |
break; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
if (which == -1) { |
| 541 |
// Internal error... |
| 542 |
} |
| 543 |
|
| 544 |
if (strcmp(quadrature_type, "cardinality") == 0) { |
| 545 |
// Cardinality already set; skip |
| 546 |
} |
| 547 |
else if (strcmp(quadrature_type, "xi") == 0) { |
| 548 |
status = nc_get_att_double(exoid, varid, attr_name, quad[which].xi); |
| 549 |
} |
| 550 |
else if (status == NC_NOERR && strcmp(quadrature_type, "eta") == 0) { |
| 551 |
status = nc_get_att_double(exoid, varid, attr_name, quad[which].eta); |
| 552 |
} |
| 553 |
else if (status == NC_NOERR && strcmp(quadrature_type, "zeta") == 0) { |
| 554 |
status = nc_get_att_double(exoid, varid, attr_name, quad[which].zeta); |
| 555 |
} |
| 556 |
else if (status == NC_NOERR && strcmp(quadrature_type, "weight") == 0) { |
| 557 |
status = nc_get_att_double(exoid, varid, attr_name, quad[which].weight); |
| 558 |
} |
| 559 |
// NOTE: Do not put an else since will fall through if the |
| 560 |
// arrays are NULL even though quadrature_type is valid. |
| 561 |
|
| 562 |
if (status != NC_NOERR) { |
| 563 |
char errmsg[MAX_ERR_LENGTH]; |
| 564 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to read Quadrature %s metadata", |
| 565 |
quad[which].name); |
| 566 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 567 |
EX_FUNC_LEAVE(EX_FATAL); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
EX_FUNC_LEAVE(EX_NOERR); |
| 572 |
} |