| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022, 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 |
/***************************************************************************** |
| 9 |
* |
| 10 |
* expmp - ex_put_map_param |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* int num_node_maps number of node maps |
| 16 |
* int num_elem_maps number of element maps |
| 17 |
* |
| 18 |
* exit conditions - |
| 19 |
* |
| 20 |
* |
| 21 |
*****************************************************************************/ |
| 22 |
|
| 23 |
#include "exodusII.h" // for ex_err, etc |
| 24 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 25 |
|
| 26 |
/*! |
| 27 |
* defines the number of node and element maps. It is more efficient |
| 28 |
* to define both of these at the same time; however, they can be |
| 29 |
* defined in separate calls by setting only one of the counts to a |
| 30 |
* non-zero value. It is an error to redefine the number of node or |
| 31 |
* element maps. |
| 32 |
* \param exoid exodus file id |
| 33 |
* \param num_node_maps number of node maps |
| 34 |
* \param num_elem_maps number of element maps |
| 35 |
*/ |
| 36 |
|
| 37 |
int ex_put_map_param(int exoid, int num_node_maps, int num_elem_maps) |
| 38 |
{ |
| 39 |
int dim[2], dimid, strdim, varid, status; |
| 40 |
int var_nm_id, var_em_id; |
| 41 |
int i; |
| 42 |
char errmsg[MAX_ERR_LENGTH]; |
| 43 |
int id_type = NC_INT; |
| 44 |
int int_type = NC_INT; |
| 45 |
|
| 46 |
EX_FUNC_ENTER(); |
| 47 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 48 |
EX_FUNC_LEAVE(EX_FATAL); |
| 49 |
} |
| 50 |
|
| 51 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 52 |
id_type = NC_INT64; |
| 53 |
} |
| 54 |
if (ex_int64_status(exoid) & EX_BULK_INT64_DB) { |
| 55 |
int_type = NC_INT64; |
| 56 |
} |
| 57 |
|
| 58 |
/* return if these have been defined before */ |
| 59 |
if ((num_node_maps > 0 && ((nc_inq_dimid(exoid, DIM_NUM_NM, &dimid)) == NC_NOERR)) || |
| 60 |
(num_elem_maps > 0 && ((nc_inq_dimid(exoid, DIM_NUM_EM, &dimid)) == NC_NOERR))) { |
| 61 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: number of maps already defined for file id %d", exoid); |
| 62 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 63 |
EX_FUNC_LEAVE(EX_FATAL); |
| 64 |
} |
| 65 |
|
| 66 |
if ((num_node_maps > 0) || (num_elem_maps > 0)) { |
| 67 |
|
| 68 |
/* inquire previously defined dimensions */ |
| 69 |
if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) { |
| 70 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid); |
| 71 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 72 |
EX_FUNC_LEAVE(EX_FATAL); |
| 73 |
} |
| 74 |
|
| 75 |
/* put file into define mode */ |
| 76 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 77 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 78 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 79 |
EX_FUNC_LEAVE(EX_FATAL); |
| 80 |
} |
| 81 |
|
| 82 |
/* node maps: */ |
| 83 |
if (num_node_maps > 0) { |
| 84 |
|
| 85 |
if ((status = nc_def_dim(exoid, DIM_NUM_NM, num_node_maps, &dimid)) != NC_NOERR) { |
| 86 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 87 |
"ERROR: failed to define number of node maps in file id %d", exoid); |
| 88 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 89 |
goto error_ret; /* exit define mode and return */ |
| 90 |
} |
| 91 |
|
| 92 |
/* node maps id array: */ |
| 93 |
dim[0] = dimid; |
| 94 |
if ((status = nc_def_var(exoid, VAR_NM_PROP(1), id_type, 1, dim, &var_nm_id)) != NC_NOERR) { |
| 95 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 96 |
"ERROR: failed to create node maps property array in file id %d", exoid); |
| 97 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 98 |
goto error_ret; /* exit define mode and return */ |
| 99 |
} |
| 100 |
|
| 101 |
/* store property name as attribute of property array variable */ |
| 102 |
if ((status = nc_put_att_text(exoid, var_nm_id, ATT_PROP_NAME, 3, "ID")) != NC_NOERR) { |
| 103 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 104 |
"ERROR: failed to store node map property name %s in file id %d", "ID", exoid); |
| 105 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 106 |
goto error_ret; /* exit define mode and return */ |
| 107 |
} |
| 108 |
|
| 109 |
/* Node map names... */ |
| 110 |
dim[0] = dimid; |
| 111 |
dim[1] = strdim; |
| 112 |
|
| 113 |
if (nc_def_var(exoid, VAR_NAME_NM, NC_CHAR, 2, dim, &varid) != NC_NOERR) { |
| 114 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 115 |
"ERROR: failed to define node map name array in file id %d", exoid); |
| 116 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 117 |
goto error_ret; /* exit define mode and return */ |
| 118 |
} |
| 119 |
#if defined(EX_CAN_USE_NC_DEF_VAR_FILL) |
| 120 |
int fill = NC_FILL_CHAR; |
| 121 |
nc_def_var_fill(exoid, varid, 0, &fill); |
| 122 |
#endif |
| 123 |
|
| 124 |
/* determine number of nodes */ |
| 125 |
if ((status = nc_inq_dimid(exoid, DIM_NUM_NODES, &dimid)) != NC_NOERR) { |
| 126 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: couldn't determine number of nodes in file id %d", |
| 127 |
exoid); |
| 128 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 129 |
goto error_ret; /* exit define mode and return */ |
| 130 |
} |
| 131 |
|
| 132 |
dim[0] = dimid; |
| 133 |
|
| 134 |
/* create variable array in which to store the node maps */ |
| 135 |
for (i = 0; i < num_node_maps; i++) { |
| 136 |
if ((status = nc_def_var(exoid, VAR_NODE_MAP(i + 1), int_type, 1, dim, &varid)) != |
| 137 |
NC_NOERR) { |
| 138 |
if (status == NC_ENAMEINUSE) { |
| 139 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: node map %d already defined in file id %d", i, |
| 140 |
exoid); |
| 141 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 142 |
} |
| 143 |
else { |
| 144 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to create node map %d in file id %d", i, |
| 145 |
exoid); |
| 146 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 147 |
} |
| 148 |
goto error_ret; /* exit define mode and return */ |
| 149 |
} |
| 150 |
exi_compress_variable(exoid, varid, 1); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/* element maps: */ |
| 155 |
if (num_elem_maps > 0) { |
| 156 |
if ((status = nc_def_dim(exoid, DIM_NUM_EM, num_elem_maps, &dimid)) != NC_NOERR) { |
| 157 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 158 |
"ERROR: failed to define number of element maps in file id %d", exoid); |
| 159 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 160 |
goto error_ret; /* exit define mode and return */ |
| 161 |
} |
| 162 |
|
| 163 |
/* element maps id array: */ |
| 164 |
dim[0] = dimid; |
| 165 |
if ((status = nc_def_var(exoid, VAR_EM_PROP(1), id_type, 1, dim, &var_em_id)) != NC_NOERR) { |
| 166 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 167 |
"ERROR: failed to create element maps property array in file id %d", exoid); |
| 168 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 169 |
goto error_ret; /* exit define mode and return */ |
| 170 |
} |
| 171 |
|
| 172 |
/* store property name as attribute of property array variable */ |
| 173 |
if ((status = nc_put_att_text(exoid, var_em_id, ATT_PROP_NAME, 3, "ID")) != NC_NOERR) { |
| 174 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 175 |
"ERROR: failed to store element map property name %s in file id %d", "ID", exoid); |
| 176 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 177 |
goto error_ret; /* exit define mode and return */ |
| 178 |
} |
| 179 |
|
| 180 |
/* Element map names... */ |
| 181 |
dim[0] = dimid; |
| 182 |
dim[1] = strdim; |
| 183 |
|
| 184 |
if ((status = nc_def_var(exoid, VAR_NAME_EM, NC_CHAR, 2, dim, &varid)) != NC_NOERR) { |
| 185 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 186 |
"ERROR: failed to define element map name array in file id %d", exoid); |
| 187 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 188 |
goto error_ret; /* exit define mode and return */ |
| 189 |
} |
| 190 |
#if defined(EX_CAN_USE_NC_DEF_VAR_FILL) |
| 191 |
int fill = NC_FILL_CHAR; |
| 192 |
nc_def_var_fill(exoid, varid, 0, &fill); |
| 193 |
#endif |
| 194 |
/* determine number of elements */ |
| 195 |
if ((status = nc_inq_dimid(exoid, DIM_NUM_ELEM, &dimid)) != NC_NOERR) { |
| 196 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 197 |
"ERROR: couldn't determine number of elements in file id %d", exoid); |
| 198 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 199 |
goto error_ret; /* exit define mode and return */ |
| 200 |
} |
| 201 |
|
| 202 |
/* create variable array in which to store the element maps */ |
| 203 |
dim[0] = dimid; |
| 204 |
for (i = 0; i < num_elem_maps; i++) { |
| 205 |
if ((status = nc_def_var(exoid, VAR_ELEM_MAP(i + 1), int_type, 1, dim, &varid)) != |
| 206 |
NC_NOERR) { |
| 207 |
if (status == NC_ENAMEINUSE) { |
| 208 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: element map %d already defined in file id %d", |
| 209 |
i, exoid); |
| 210 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 211 |
} |
| 212 |
else { |
| 213 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to create element map %d in file id %d", |
| 214 |
i, exoid); |
| 215 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 216 |
} |
| 217 |
goto error_ret; /* exit define mode and return */ |
| 218 |
} |
| 219 |
exi_compress_variable(exoid, varid, 1); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/* leave define mode */ |
| 224 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 225 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 226 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 227 |
EX_FUNC_LEAVE(EX_FATAL); |
| 228 |
} |
| 229 |
|
| 230 |
/* Fill the id arrays with EX_INVALID_ID */ |
| 231 |
{ |
| 232 |
int maxset = num_node_maps > num_elem_maps ? num_node_maps : num_elem_maps; |
| 233 |
int *invalid_ids = malloc(maxset * sizeof(int)); |
| 234 |
if (invalid_ids == NULL) { |
| 235 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 236 |
"ERROR: failed to allocate memory for invalid id " |
| 237 |
"storage in file id %d", |
| 238 |
exoid); |
| 239 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 240 |
EX_FUNC_LEAVE(EX_FATAL); |
| 241 |
} |
| 242 |
for (i = 0; i < maxset; i++) { |
| 243 |
invalid_ids[i] = EX_INVALID_ID; |
| 244 |
} |
| 245 |
if (num_node_maps > 0) { |
| 246 |
if ((status = nc_put_var_int(exoid, var_nm_id, invalid_ids)) != NC_NOERR) { |
| 247 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output node map ids in file id %d", |
| 248 |
exoid); |
| 249 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 250 |
free(invalid_ids); |
| 251 |
EX_FUNC_LEAVE(EX_FATAL); |
| 252 |
} |
| 253 |
} |
| 254 |
if (num_elem_maps > 0) { |
| 255 |
if ((status = nc_put_var_int(exoid, var_em_id, invalid_ids)) != NC_NOERR) { |
| 256 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output element map ids in file id %d", |
| 257 |
exoid); |
| 258 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 259 |
free(invalid_ids); |
| 260 |
EX_FUNC_LEAVE(EX_FATAL); |
| 261 |
} |
| 262 |
} |
| 263 |
free(invalid_ids); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
EX_FUNC_LEAVE(EX_NOERR); |
| 268 |
|
| 269 |
/* Fatal error: exit definition mode and return */ |
| 270 |
error_ret: |
| 271 |
exi_leavedef(exoid, __func__); |
| 272 |
EX_FUNC_LEAVE(EX_FATAL); |
| 273 |
} |