| 1 |
/* |
| 2 |
* Copyright(C) 1999-2022, 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 |
* exppem - ex_put_partial_num_map |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* int map_id element map id |
| 16 |
* int ent_start first entry in map |
| 17 |
* int ent_count number of entries in map |
| 18 |
* int *map map |
| 19 |
* |
| 20 |
* exit conditions - |
| 21 |
* |
| 22 |
* revision history - |
| 23 |
* |
| 24 |
* |
| 25 |
*****************************************************************************/ |
| 26 |
|
| 27 |
#include "exodusII.h" // for ex_err, etc |
| 28 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 29 |
|
| 30 |
/*! |
| 31 |
* writes a map; this is a vector of integers of length number of mesh |
| 32 |
* objects of that type (element, node, face, edge) |
| 33 |
*/ |
| 34 |
int ex_put_partial_num_map(int exoid, ex_entity_type map_type, ex_entity_id map_id, |
| 35 |
int64_t ent_start, int64_t ent_count, const void_int *map) |
| 36 |
{ |
| 37 |
int status; |
| 38 |
int dimid, varid, map_exists; |
| 39 |
size_t start[1]; |
| 40 |
size_t num_maps, num_mobj, count[1]; |
| 41 |
int cur_num_maps; |
| 42 |
char errmsg[MAX_ERR_LENGTH]; |
| 43 |
const char *dnumentries; |
| 44 |
const char *dnummaps; |
| 45 |
const char *vmapids; |
| 46 |
const char *vmap; |
| 47 |
|
| 48 |
EX_FUNC_ENTER(); |
| 49 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 50 |
EX_FUNC_LEAVE(EX_FATAL); |
| 51 |
} |
| 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 |
if (nc_inq_dimid(exoid, dnumentries, &dimid) != NC_NOERR) { |
| 83 |
EX_FUNC_LEAVE(EX_NOERR); |
| 84 |
} |
| 85 |
|
| 86 |
/* first check if any maps are specified */ |
| 87 |
if ((status = nc_inq_dimid(exoid, dnummaps, &dimid)) != NC_NOERR) { |
| 88 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss specified in file id %d", |
| 89 |
ex_name_of_object(map_type), exoid); |
| 90 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 91 |
EX_FUNC_LEAVE(EX_FATAL); |
| 92 |
} |
| 93 |
|
| 94 |
/* If the `map_id` is negative, then that specifies a specific location for that map */ |
| 95 |
bool id_is_index = map_id < 0; |
| 96 |
if (id_is_index) { |
| 97 |
map_id = -map_id; |
| 98 |
} |
| 99 |
|
| 100 |
/* Check for duplicate map id entry */ |
| 101 |
status = exi_id_lkup(exoid, map_type, map_id); |
| 102 |
if (status == -EX_LOOKUPFAIL) { /* did not find the map id */ |
| 103 |
map_exists = 0; /* Map is being defined */ |
| 104 |
} |
| 105 |
else { |
| 106 |
map_exists = 1; /* assume a portion of this map has already been written */ |
| 107 |
} |
| 108 |
|
| 109 |
/* Check for duplicate map id entry */ |
| 110 |
if (!map_exists) { |
| 111 |
/* Get number of maps initialized for this file */ |
| 112 |
if ((status = nc_inq_dimlen(exoid, dimid, &num_maps)) != NC_NOERR) { |
| 113 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of %ss in file id %d", |
| 114 |
ex_name_of_object(map_type), exoid); |
| 115 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 116 |
EX_FUNC_LEAVE(EX_FATAL); |
| 117 |
} |
| 118 |
|
| 119 |
/* Keep track of the total number of maps defined using a |
| 120 |
counter stored in a linked list keyed by exoid. NOTE: |
| 121 |
exi_get_file_item is used to find the number of element maps for a |
| 122 |
specific file and returns that value. |
| 123 |
*/ |
| 124 |
cur_num_maps = exi_get_file_item(exoid, exi_get_counter_list(map_type)); |
| 125 |
if (cur_num_maps >= (int)num_maps) { |
| 126 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 127 |
"ERROR: exceeded number of %ss (%zu) specified in file id %d", |
| 128 |
ex_name_of_object(map_type), num_maps, exoid); |
| 129 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 130 |
EX_FUNC_LEAVE(EX_FATAL); |
| 131 |
} |
| 132 |
|
| 133 |
/* NOTE: exi_inc_file_item is used to find the number of element maps |
| 134 |
for a specific file and returns that value incremented. */ |
| 135 |
cur_num_maps = exi_inc_file_item(exoid, exi_get_counter_list(map_type)); |
| 136 |
|
| 137 |
if (id_is_index) { |
| 138 |
cur_num_maps = map_id - 1; |
| 139 |
} |
| 140 |
} |
| 141 |
else { |
| 142 |
int map_ndx = exi_id_lkup(exoid, map_type, map_id); |
| 143 |
cur_num_maps = map_ndx - 1; |
| 144 |
} |
| 145 |
|
| 146 |
/* determine number of elements */ |
| 147 |
if ((status = nc_inq_dimid(exoid, dnumentries, &dimid)) != NC_NOERR) { |
| 148 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 149 |
"ERROR: couldn't determine number of mesh objects in file id %d", exoid); |
| 150 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 151 |
EX_FUNC_LEAVE(EX_FATAL); |
| 152 |
} |
| 153 |
|
| 154 |
if ((status = nc_inq_dimlen(exoid, dimid, &num_mobj)) != NC_NOERR) { |
| 155 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of mesh objects in file id %d", |
| 156 |
exoid); |
| 157 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 158 |
EX_FUNC_LEAVE(EX_FATAL); |
| 159 |
} |
| 160 |
|
| 161 |
/* Check input parameters for a valid range of numbers */ |
| 162 |
if (ent_count == 0) { |
| 163 |
ent_start = 0; |
| 164 |
} |
| 165 |
|
| 166 |
if (ent_start < 0 || ent_start > (int64_t)num_mobj) { |
| 167 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: start count is invalid in file id %d", exoid); |
| 168 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 169 |
EX_FUNC_LEAVE(EX_FATAL); |
| 170 |
} |
| 171 |
if (ent_count < 0) { |
| 172 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid count value in file id %d", exoid); |
| 173 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 174 |
EX_FUNC_LEAVE(EX_FATAL); |
| 175 |
} |
| 176 |
if (ent_count > 0 && (ent_start + ent_count - 1 > (int64_t)num_mobj)) { |
| 177 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 178 |
"ERROR: start+count-1 is larger than mesh object count in file id %d", exoid); |
| 179 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 180 |
EX_FUNC_LEAVE(EX_FATAL); |
| 181 |
} |
| 182 |
|
| 183 |
/* write out information to previously defined variable */ |
| 184 |
|
| 185 |
/* first get id of variable */ |
| 186 |
if ((status = nc_inq_varid(exoid, vmapids, &varid)) == -1) { |
| 187 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids 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 |
/* then, write out map id */ |
| 194 |
if (!map_exists) { |
| 195 |
start[0] = cur_num_maps; |
| 196 |
{ |
| 197 |
if ((status = nc_put_var1_longlong(exoid, varid, start, (long long *)&map_id)) != NC_NOERR) { |
| 198 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s id %" PRId64 " in file id %d", |
| 199 |
ex_name_of_object(map_type), map_id, exoid); |
| 200 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 201 |
EX_FUNC_LEAVE(EX_FATAL); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
switch (map_type) { |
| 207 |
case EX_NODE_MAP: vmap = VAR_NODE_MAP(cur_num_maps + 1); break; |
| 208 |
case EX_EDGE_MAP: vmap = VAR_EDGE_MAP(cur_num_maps + 1); break; |
| 209 |
case EX_FACE_MAP: vmap = VAR_FACE_MAP(cur_num_maps + 1); break; |
| 210 |
case EX_ELEM_MAP: vmap = VAR_ELEM_MAP(cur_num_maps + 1); break; |
| 211 |
default: |
| 212 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 213 |
"Internal ERROR: unrecognized map type in switch: %d in file id %d", map_type, exoid); |
| 214 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 215 |
EX_FUNC_LEAVE(EX_FATAL); |
| 216 |
} |
| 217 |
|
| 218 |
/* locate variable array in which to store the map */ |
| 219 |
if ((status = nc_inq_varid(exoid, vmap, &varid)) != NC_NOERR) { |
| 220 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s %" PRId64 " in file id %d", |
| 221 |
ex_name_of_object(map_type), map_id, exoid); |
| 222 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 223 |
EX_FUNC_LEAVE(EX_FATAL); |
| 224 |
} |
| 225 |
|
| 226 |
/* write out the map */ |
| 227 |
start[0] = ent_start - 1; |
| 228 |
count[0] = ent_count; |
| 229 |
|
| 230 |
if (count[0] == 0) { |
| 231 |
start[0] = 0; |
| 232 |
} |
| 233 |
|
| 234 |
if (ex_int64_status(exoid) & EX_MAPS_INT64_API) { |
| 235 |
status = nc_put_vara_longlong(exoid, varid, start, count, map); |
| 236 |
} |
| 237 |
else { |
| 238 |
status = nc_put_vara_int(exoid, varid, start, count, map); |
| 239 |
} |
| 240 |
|
| 241 |
if (status != NC_NOERR) { |
| 242 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s in file id %d", |
| 243 |
ex_name_of_object(map_type), exoid); |
| 244 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 245 |
EX_FUNC_LEAVE(EX_FATAL); |
| 246 |
} |
| 247 |
|
| 248 |
EX_FUNC_LEAVE(EX_NOERR); |
| 249 |
} |