| 1 |
/* |
| 2 |
* Copyright(C) 1999-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 |
* expem - ex_put_num_map |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* int map_type type of map (node,edge,face,elem) |
| 16 |
* int map_id id to associate with new map |
| 17 |
* int *map_data map set value array |
| 18 |
* |
| 19 |
* exit conditions - |
| 20 |
* |
| 21 |
* revision history - |
| 22 |
* |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
#include "exodusII.h" // for ex_err, etc |
| 26 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 27 |
|
| 28 |
/*! |
| 29 |
* writes a map; this is a vector of integers of the same length as the |
| 30 |
* number of entries in the source object (nodes, edges, faces, or elements |
| 31 |
* in the file). |
| 32 |
* \param exoid exodus file id |
| 33 |
* \param map_type type of map (node,edge,face,elem) |
| 34 |
* \param map_id id to associate with new map |
| 35 |
* \param map map set value array |
| 36 |
*/ |
| 37 |
|
| 38 |
int ex_put_num_map(int exoid, ex_entity_type map_type, ex_entity_id map_id, const void_int *map) |
| 39 |
{ |
| 40 |
size_t start[1]; |
| 41 |
char errmsg[MAX_ERR_LENGTH]; |
| 42 |
int status; |
| 43 |
|
| 44 |
EX_FUNC_ENTER(); |
| 45 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 46 |
EX_FUNC_LEAVE(EX_FATAL); |
| 47 |
} |
| 48 |
|
| 49 |
const char *dnumentries; |
| 50 |
const char *dnummaps; |
| 51 |
const char *vmapids; |
| 52 |
|
| 53 |
switch (map_type) { |
| 54 |
case EX_NODE_MAP: |
| 55 |
dnumentries = DIM_NUM_NODES; |
| 56 |
dnummaps = DIM_NUM_NM; |
| 57 |
vmapids = VAR_NM_PROP(1); |
| 58 |
break; |
| 59 |
case EX_EDGE_MAP: |
| 60 |
dnumentries = DIM_NUM_EDGE; |
| 61 |
dnummaps = DIM_NUM_EDM; |
| 62 |
vmapids = VAR_EDM_PROP(1); |
| 63 |
break; |
| 64 |
case EX_FACE_MAP: |
| 65 |
dnumentries = DIM_NUM_FACE; |
| 66 |
dnummaps = DIM_NUM_FAM; |
| 67 |
vmapids = VAR_FAM_PROP(1); |
| 68 |
break; |
| 69 |
case EX_ELEM_MAP: |
| 70 |
dnumentries = DIM_NUM_ELEM; |
| 71 |
dnummaps = DIM_NUM_EM; |
| 72 |
vmapids = VAR_EM_PROP(1); |
| 73 |
break; |
| 74 |
default: |
| 75 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Bad map type (%d) specified for file id %d", map_type, |
| 76 |
exoid); |
| 77 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 78 |
EX_FUNC_LEAVE(EX_FATAL); |
| 79 |
} |
| 80 |
|
| 81 |
/* Make sure the file contains entries */ |
| 82 |
int dimid; |
| 83 |
if (nc_inq_dimid(exoid, dnumentries, &dimid) != NC_NOERR) { |
| 84 |
EX_FUNC_LEAVE(EX_NOERR); |
| 85 |
} |
| 86 |
|
| 87 |
/* first check if any maps are specified */ |
| 88 |
if ((status = nc_inq_dimid(exoid, dnummaps, &dimid)) != NC_NOERR) { |
| 89 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss specified in file id %d", |
| 90 |
ex_name_of_object(map_type), exoid); |
| 91 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 92 |
EX_FUNC_LEAVE(EX_FATAL); |
| 93 |
} |
| 94 |
|
| 95 |
/* If the `map_id` is negative, then that specifies a specific location for that map */ |
| 96 |
bool overwrite_map = false; |
| 97 |
bool id_is_index = map_id < 0; |
| 98 |
if (id_is_index) { |
| 99 |
map_id = -map_id; |
| 100 |
} |
| 101 |
|
| 102 |
/* Check for duplicate map id entry */ |
| 103 |
status = exi_id_lkup(exoid, map_type, map_id); |
| 104 |
if (status != -EX_LOOKUPFAIL) { /* found the map id */ |
| 105 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: %s %" PRId64 " already defined in file id %d", |
| 106 |
ex_name_of_object(map_type), map_id, exoid); |
| 107 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 108 |
id_is_index = true; |
| 109 |
overwrite_map = true; |
| 110 |
} |
| 111 |
|
| 112 |
/* Get number of maps initialized for this file */ |
| 113 |
size_t num_entries; |
| 114 |
if ((status = nc_inq_dimlen(exoid, dimid, &num_entries)) != NC_NOERR) { |
| 115 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of %ss in file id %d", |
| 116 |
ex_name_of_object(map_type), exoid); |
| 117 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 118 |
EX_FUNC_LEAVE(EX_FATAL); |
| 119 |
} |
| 120 |
|
| 121 |
/* Keep track of the total number of maps defined using a counter stored |
| 122 |
in a linked list keyed by exoid. |
| 123 |
NOTE: exi_get_file_item is used to find the number of maps |
| 124 |
for a specific file and returns that value. |
| 125 |
*/ |
| 126 |
int cur_num_maps = exi_get_file_item(exoid, exi_get_counter_list(map_type)); |
| 127 |
if (!overwrite_map) { |
| 128 |
int num_maps = num_entries; |
| 129 |
if (cur_num_maps >= num_maps) { |
| 130 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: exceeded number of %ss (%d) specified in file id %d", |
| 131 |
ex_name_of_object(map_type), num_maps, exoid); |
| 132 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 133 |
EX_FUNC_LEAVE(EX_FATAL); |
| 134 |
} |
| 135 |
|
| 136 |
/* NOTE: exi_inc_file_item is used to find the number of maps |
| 137 |
for a specific file and returns that value incremented. */ |
| 138 |
cur_num_maps = exi_inc_file_item(exoid, exi_get_counter_list(map_type)); |
| 139 |
} |
| 140 |
|
| 141 |
if (id_is_index) { |
| 142 |
cur_num_maps = map_id - 1; |
| 143 |
} |
| 144 |
|
| 145 |
/* write out information to previously defined variable */ |
| 146 |
|
| 147 |
/* first get id of variable */ |
| 148 |
int varid; |
| 149 |
if ((status = nc_inq_varid(exoid, vmapids, &varid)) == -1) { |
| 150 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids in file id %d", |
| 151 |
ex_name_of_object(map_type), exoid); |
| 152 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 153 |
EX_FUNC_LEAVE(EX_FATAL); |
| 154 |
} |
| 155 |
|
| 156 |
/* then, write out map id */ |
| 157 |
start[0] = cur_num_maps; |
| 158 |
|
| 159 |
int ldum = (int)map_id; |
| 160 |
if ((status = nc_put_var1_int(exoid, varid, start, &ldum)) != NC_NOERR) { |
| 161 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s id %" PRId64 " in file id %d", |
| 162 |
ex_name_of_object(map_type), map_id, exoid); |
| 163 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 164 |
EX_FUNC_LEAVE(EX_FATAL); |
| 165 |
} |
| 166 |
|
| 167 |
const char *vmap; |
| 168 |
switch (map_type) { |
| 169 |
case EX_NODE_MAP: vmap = VAR_NODE_MAP(cur_num_maps + 1); break; |
| 170 |
case EX_EDGE_MAP: vmap = VAR_EDGE_MAP(cur_num_maps + 1); break; |
| 171 |
case EX_FACE_MAP: vmap = VAR_FACE_MAP(cur_num_maps + 1); break; |
| 172 |
case EX_ELEM_MAP: vmap = VAR_ELEM_MAP(cur_num_maps + 1); break; |
| 173 |
default: |
| 174 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 175 |
"Internal ERROR: unrecognized map type in switch: %d in file id %d", map_type, exoid); |
| 176 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 177 |
EX_FUNC_LEAVE(EX_FATAL); |
| 178 |
} |
| 179 |
|
| 180 |
/* locate variable array in which to store the map */ |
| 181 |
if (nc_inq_varid(exoid, vmap, &varid) != NC_NOERR) { |
| 182 |
int dims[2]; |
| 183 |
|
| 184 |
/* determine number of entries */ |
| 185 |
if ((status = nc_inq_dimid(exoid, dnumentries, &dimid)) == -1) { |
| 186 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 187 |
"ERROR: couldn't determine number of %s entries in file id %d", |
| 188 |
ex_name_of_object(map_type), exoid); |
| 189 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 190 |
EX_FUNC_LEAVE(EX_FATAL); |
| 191 |
} |
| 192 |
|
| 193 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 194 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); |
| 195 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 196 |
EX_FUNC_LEAVE(EX_FATAL); |
| 197 |
} |
| 198 |
|
| 199 |
/* Check type to be used for maps... */ |
| 200 |
int map_int_type = NC_INT; |
| 201 |
if (ex_int64_status(exoid) & EX_MAPS_INT64_DB) { |
| 202 |
map_int_type = NC_INT64; |
| 203 |
} |
| 204 |
|
| 205 |
dims[0] = dimid; |
| 206 |
if ((status = nc_def_var(exoid, vmap, map_int_type, 1, dims, &varid)) == -1) { |
| 207 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define map %s in file id %d", vmap, exoid); |
| 208 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 209 |
} |
| 210 |
exi_compress_variable(exoid, varid, 1); |
| 211 |
|
| 212 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { /* exit define mode */ |
| 213 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 214 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 215 |
varid = -1; /* force early exit */ |
| 216 |
} |
| 217 |
|
| 218 |
if (varid == -1) { /* we couldn't define variable and have prepared error message. */ |
| 219 |
EX_FUNC_LEAVE(EX_FATAL); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/* write out the map */ |
| 224 |
|
| 225 |
if (ex_int64_status(exoid) & EX_MAPS_INT64_API) { |
| 226 |
status = nc_put_var_longlong(exoid, varid, map); |
| 227 |
} |
| 228 |
else { |
| 229 |
status = nc_put_var_int(exoid, varid, map); |
| 230 |
} |
| 231 |
if (status != NC_NOERR) { |
| 232 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s in file id %d", |
| 233 |
ex_name_of_object(map_type), exoid); |
| 234 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 235 |
EX_FUNC_LEAVE(EX_FATAL); |
| 236 |
} |
| 237 |
|
| 238 |
EX_FUNC_LEAVE(EX_NOERR); |
| 239 |
} |