| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 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 |
#include "exodusII.h" // for ex_err, etc |
| 13 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 14 |
|
| 15 |
/* |
| 16 |
* reads the element block ids from the database |
| 17 |
*/ |
| 18 |
|
| 19 |
struct ncvar |
| 20 |
{ /* variable */ |
| 21 |
char name[MAX_VAR_NAME_LENGTH]; |
| 22 |
nc_type type; |
| 23 |
int ndims; |
| 24 |
int dims[NC_MAX_VAR_DIMS]; |
| 25 |
int natts; |
| 26 |
}; |
| 27 |
|
| 28 |
static int ex_get_nonstandard_ids(int exoid, ex_entity_type obj_type, void_int *ids) |
| 29 |
{ |
| 30 |
|
| 31 |
int64_t count = 0; |
| 32 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 33 |
if (file) { |
| 34 |
if (obj_type == EX_ASSEMBLY) { |
| 35 |
count = file->assembly_count; |
| 36 |
} |
| 37 |
else if (obj_type == EX_BLOB) { |
| 38 |
count = file->blob_count; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if (count > 0) { |
| 43 |
/* For assemblies, we need to get the `assembly_entity` variables and read the ids from them |
| 44 |
* For blobs, we need to get the `blob_entity` variables and read the ids from them |
| 45 |
*/ |
| 46 |
int num_found = 0; |
| 47 |
struct ncvar var; |
| 48 |
int nvars; |
| 49 |
int rootid = exoid & EX_FILE_ID_MASK; |
| 50 |
int root_var_count = 0; |
| 51 |
if (rootid != exoid) { |
| 52 |
nc_inq(rootid, NULL, &root_var_count, NULL, NULL); |
| 53 |
} |
| 54 |
nc_inq(exoid, NULL, &nvars, NULL, NULL); |
| 55 |
char *type = NULL; |
| 56 |
if (obj_type == EX_ASSEMBLY) { |
| 57 |
type = "assembly_entity"; |
| 58 |
} |
| 59 |
else if (obj_type == EX_BLOB) { |
| 60 |
type = "blob_entity"; |
| 61 |
} |
| 62 |
|
| 63 |
for (int varid = root_var_count; varid < nvars + root_var_count; varid++) { |
| 64 |
int status; |
| 65 |
if ((status = nc_inq_var(exoid, varid, var.name, &var.type, &var.ndims, var.dims, |
| 66 |
&var.natts)) != NC_NOERR) { |
| 67 |
char errmsg[MAX_ERR_LENGTH]; |
| 68 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get variable parameters in file id %d", |
| 69 |
exoid); |
| 70 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 71 |
return EX_FATAL; |
| 72 |
} |
| 73 |
if ((strncmp(var.name, type, strlen(type)) == 0)) { |
| 74 |
/* Query the "_id" attribute on this object type. */ |
| 75 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 76 |
long long id = 0; |
| 77 |
status = nc_get_att_longlong(exoid, varid, EX_ATTRIBUTE_ID, &id); |
| 78 |
((int64_t *)ids)[num_found++] = id; |
| 79 |
} |
| 80 |
else { |
| 81 |
int id = 0; |
| 82 |
status = nc_get_att_int(exoid, varid, EX_ATTRIBUTE_ID, &id); |
| 83 |
((int *)ids)[num_found++] = id; |
| 84 |
} |
| 85 |
if (num_found == count) { |
| 86 |
break; |
| 87 |
} |
| 88 |
if (status != NC_NOERR) { |
| 89 |
char errmsg[MAX_ERR_LENGTH]; |
| 90 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s ids in file id %d", |
| 91 |
ex_name_of_object(obj_type), exoid); |
| 92 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 93 |
return EX_FATAL; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
return EX_NOERR; |
| 99 |
} |
| 100 |
|
| 101 |
int ex_get_ids(int exoid, ex_entity_type obj_type, void_int *ids) |
| 102 |
{ |
| 103 |
int varid = 0; |
| 104 |
int status = 0; |
| 105 |
|
| 106 |
const char *varidobj = NULL; |
| 107 |
|
| 108 |
EX_FUNC_ENTER(); |
| 109 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 110 |
EX_FUNC_LEAVE(EX_FATAL); |
| 111 |
} |
| 112 |
|
| 113 |
if (obj_type == EX_ASSEMBLY || obj_type == EX_BLOB) { |
| 114 |
status = ex_get_nonstandard_ids(exoid, obj_type, ids); |
| 115 |
EX_FUNC_LEAVE(status); |
| 116 |
} |
| 117 |
|
| 118 |
/* Now handle the rest of the object types */ |
| 119 |
switch (obj_type) { |
| 120 |
case EX_EDGE_BLOCK: varidobj = VAR_ID_ED_BLK; break; |
| 121 |
case EX_FACE_BLOCK: varidobj = VAR_ID_FA_BLK; break; |
| 122 |
case EX_ELEM_BLOCK: varidobj = VAR_ID_EL_BLK; break; |
| 123 |
case EX_NODE_SET: varidobj = VAR_NS_IDS; break; |
| 124 |
case EX_EDGE_SET: varidobj = VAR_ES_IDS; break; |
| 125 |
case EX_FACE_SET: varidobj = VAR_FS_IDS; break; |
| 126 |
case EX_SIDE_SET: varidobj = VAR_SS_IDS; break; |
| 127 |
case EX_ELEM_SET: varidobj = VAR_ELS_IDS; break; |
| 128 |
case EX_NODE_MAP: varidobj = VAR_NM_PROP(1); break; |
| 129 |
case EX_EDGE_MAP: varidobj = VAR_EDM_PROP(1); break; |
| 130 |
case EX_FACE_MAP: varidobj = VAR_FAM_PROP(1); break; |
| 131 |
case EX_ELEM_MAP: varidobj = VAR_EM_PROP(1); break; |
| 132 |
default: /* invalid variable type */ |
| 133 |
{ |
| 134 |
char errmsg[MAX_ERR_LENGTH]; |
| 135 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid type specified in file id %d", exoid); |
| 136 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 137 |
EX_FUNC_LEAVE(EX_FATAL); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* Determine if there are any 'obj-type' objects */ |
| 142 |
if ((status = nc_inq_dimid(exoid, exi_dim_num_objects(obj_type), &varid)) != NC_NOERR) { |
| 143 |
char errmsg[MAX_ERR_LENGTH]; |
| 144 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no %s defined in file id %d", |
| 145 |
ex_name_of_object(obj_type), exoid); |
| 146 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 147 |
EX_FUNC_LEAVE(EX_WARN); |
| 148 |
} |
| 149 |
|
| 150 |
/* inquire id's of previously defined dimensions and variables */ |
| 151 |
if ((status = nc_inq_varid(exoid, varidobj, &varid)) != NC_NOERR) { |
| 152 |
char errmsg[MAX_ERR_LENGTH]; |
| 153 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids variable in file id %d", |
| 154 |
ex_name_of_object(obj_type), exoid); |
| 155 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 156 |
EX_FUNC_LEAVE(EX_FATAL); |
| 157 |
} |
| 158 |
|
| 159 |
/* read in the element block ids */ |
| 160 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 161 |
status = nc_get_var_longlong(exoid, varid, ids); |
| 162 |
} |
| 163 |
else { |
| 164 |
status = nc_get_var_int(exoid, varid, ids); |
| 165 |
} |
| 166 |
|
| 167 |
if (status != NC_NOERR) { |
| 168 |
char errmsg[MAX_ERR_LENGTH]; |
| 169 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to return %s ids in file id %d", |
| 170 |
ex_name_of_object(obj_type), exoid); |
| 171 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 172 |
EX_FUNC_LEAVE(EX_FATAL); |
| 173 |
} |
| 174 |
EX_FUNC_LEAVE(EX_NOERR); |
| 175 |
} |