| 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_block, ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 11 |
#include <stdbool.h> |
| 12 |
|
| 13 |
/*! |
| 14 |
* writes the parameters used to describe an element/face/edge block |
| 15 |
* \param exoid exodus file id |
| 16 |
* \param block_count number of blocks being defined |
| 17 |
* \param blocks array of ex_block structures describing |
| 18 |
* block counts |
| 19 |
*/ |
| 20 |
|
| 21 |
int ex_put_block_params(int exoid, size_t block_count, const struct ex_block *blocks) |
| 22 |
{ |
| 23 |
size_t i; |
| 24 |
int conn_int_type; |
| 25 |
int status; |
| 26 |
int arbitrary_polyhedra = 0; /* 1 if block is arbitrary 2d polyhedra type; 2 if 3d polyhedra */ |
| 27 |
int att_name_varid = -1; |
| 28 |
int varid, dimid, dims[2], blk_id_ndx, blk_stat, strdim; |
| 29 |
size_t start[2]; |
| 30 |
size_t num_blk; |
| 31 |
int cur_num_blk, numblkdim, numattrdim; |
| 32 |
int nnodperentdim = -1; |
| 33 |
int nedgperentdim = -1; |
| 34 |
int nfacperentdim = -1; |
| 35 |
int connid = 0; |
| 36 |
int npeid; |
| 37 |
char errmsg[MAX_ERR_LENGTH]; |
| 38 |
char *entity_type1 = NULL; |
| 39 |
char *entity_type2 = NULL; |
| 40 |
int *blocks_to_define = NULL; |
| 41 |
const char *dnumblk = NULL; |
| 42 |
const char *vblkids = NULL; |
| 43 |
const char *vblksta = NULL; |
| 44 |
const char *vnodcon = NULL; |
| 45 |
const char *vnpecnt = NULL; |
| 46 |
const char *vedgcon = NULL; |
| 47 |
const char *vfaccon = NULL; |
| 48 |
const char *vconn = NULL; |
| 49 |
const char *vattnam = NULL; |
| 50 |
const char *vblkatt = NULL; |
| 51 |
const char *dneblk = NULL; |
| 52 |
const char *dnape = NULL; |
| 53 |
const char *dnnpe = NULL; |
| 54 |
const char *dnepe = NULL; |
| 55 |
const char *dnfpe = NULL; |
| 56 |
|
| 57 |
if (block_count == 0) { |
| 58 |
return EX_NOERR; |
| 59 |
} |
| 60 |
|
| 61 |
EX_FUNC_ENTER(); |
| 62 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 63 |
EX_FUNC_LEAVE(EX_FATAL); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* ======================================================================== |
| 68 |
* Check whether `blocks` is homogeneous (all same type) and if so, does it |
| 69 |
* contain entries for all blocks of that type that will be defined. If so, |
| 70 |
* can consolidate some operations... |
| 71 |
*/ |
| 72 |
bool all_same = true; |
| 73 |
int last_type = blocks[0].type; |
| 74 |
for (i = 0; i < block_count; i++) { |
| 75 |
if (blocks[i].type != last_type) { |
| 76 |
all_same = false; |
| 77 |
break; |
| 78 |
} |
| 79 |
/* See if storing an 'nsided' element block (arbitrary 2d polyhedra or super |
| 80 |
* element) */ |
| 81 |
if (strlen(blocks[i].topology) >= 3) { |
| 82 |
if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && |
| 83 |
(blocks[i].topology[1] == 's' || blocks[i].topology[1] == 'S') && |
| 84 |
(blocks[i].topology[2] == 'i' || blocks[i].topology[2] == 'I')) { |
| 85 |
all_same = false; |
| 86 |
break; |
| 87 |
} |
| 88 |
else if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && |
| 89 |
(blocks[i].topology[1] == 'f' || blocks[i].topology[1] == 'F') && |
| 90 |
(blocks[i].topology[2] == 'a' || blocks[i].topology[2] == 'A')) { |
| 91 |
/* If a FACE_BLOCK, then we are dealing with the faces of the nfaced |
| 92 |
* blocks[i]. */ |
| 93 |
all_same = false; |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (all_same) { |
| 100 |
/* |
| 101 |
* Check number of blocks of this type on the database and |
| 102 |
* see if that is the size of `blocks` array (i.e., are all |
| 103 |
* blocks of that type being defined in this call. |
| 104 |
*/ |
| 105 |
switch (last_type) { |
| 106 |
case EX_EDGE_BLOCK: dnumblk = DIM_NUM_ED_BLK; break; |
| 107 |
case EX_FACE_BLOCK: dnumblk = DIM_NUM_FA_BLK; break; |
| 108 |
case EX_ELEM_BLOCK: dnumblk = DIM_NUM_EL_BLK; break; |
| 109 |
default: |
| 110 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 111 |
"ERROR: Bad block type (%d) specified for all blocks file id %d", last_type, exoid); |
| 112 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 113 |
EX_FUNC_LEAVE(EX_FATAL); |
| 114 |
} |
| 115 |
if ((status = exi_get_dimension(exoid, dnumblk, ex_name_of_object(last_type), &num_blk, &dimid, |
| 116 |
__func__)) != NC_NOERR) { |
| 117 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: No %ss defined in file id %d", |
| 118 |
ex_name_of_object(last_type), exoid); |
| 119 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 120 |
EX_FUNC_LEAVE(EX_FATAL); |
| 121 |
} |
| 122 |
|
| 123 |
if (block_count == num_blk) { |
| 124 |
status = exi_put_homogenous_block_params(exoid, block_count, blocks); |
| 125 |
EX_FUNC_LEAVE(status); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (!(blocks_to_define = malloc(block_count * sizeof(int)))) { |
| 130 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 131 |
"ERROR: failed to allocate memory for internal blocks_to_define " |
| 132 |
"array in file id %d", |
| 133 |
exoid); |
| 134 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 135 |
EX_FUNC_LEAVE(EX_FATAL); |
| 136 |
} |
| 137 |
|
| 138 |
for (i = 0; i < block_count; i++) { |
| 139 |
switch (blocks[i].type) { |
| 140 |
case EX_EDGE_BLOCK: |
| 141 |
dnumblk = DIM_NUM_ED_BLK; |
| 142 |
vblkids = VAR_ID_ED_BLK; |
| 143 |
vblksta = VAR_STAT_ED_BLK; |
| 144 |
break; |
| 145 |
case EX_FACE_BLOCK: |
| 146 |
dnumblk = DIM_NUM_FA_BLK; |
| 147 |
vblkids = VAR_ID_FA_BLK; |
| 148 |
vblksta = VAR_STAT_FA_BLK; |
| 149 |
break; |
| 150 |
case EX_ELEM_BLOCK: |
| 151 |
dnumblk = DIM_NUM_EL_BLK; |
| 152 |
vblkids = VAR_ID_EL_BLK; |
| 153 |
vblksta = VAR_STAT_EL_BLK; |
| 154 |
break; |
| 155 |
default: |
| 156 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 157 |
"ERROR: Bad block type (%d) specified for entry %d file id %d", blocks[i].type, |
| 158 |
(int)i, exoid); |
| 159 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 160 |
free(blocks_to_define); |
| 161 |
EX_FUNC_LEAVE(EX_FATAL); |
| 162 |
} |
| 163 |
|
| 164 |
/* first check if any blocks of that type are specified */ |
| 165 |
if ((status = exi_get_dimension(exoid, dnumblk, ex_name_of_object(blocks[i].type), &num_blk, |
| 166 |
&dimid, __func__)) != NC_NOERR) { |
| 167 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: No %ss defined in file id %d", |
| 168 |
ex_name_of_object(blocks[i].type), exoid); |
| 169 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 170 |
free(blocks_to_define); |
| 171 |
EX_FUNC_LEAVE(EX_FATAL); |
| 172 |
} |
| 173 |
|
| 174 |
/* Next: Make sure that there are not any duplicate block ids by |
| 175 |
searching the vblkids array. |
| 176 |
WARNING: This must be done outside of define mode because id_lkup |
| 177 |
accesses |
| 178 |
the database to determine the position |
| 179 |
*/ |
| 180 |
|
| 181 |
if ((status = nc_inq_varid(exoid, vblkids, &varid)) != NC_NOERR) { |
| 182 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids in file id %d", |
| 183 |
ex_name_of_object(blocks[i].type), exoid); |
| 184 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 185 |
free(blocks_to_define); |
| 186 |
EX_FUNC_LEAVE(EX_FATAL); |
| 187 |
} |
| 188 |
|
| 189 |
status = exi_id_lkup(exoid, blocks[i].type, blocks[i].id); |
| 190 |
if (-status != EX_LOOKUPFAIL) { /* found the element block id */ |
| 191 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s id %" PRId64 " already exists in file id %d", |
| 192 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 193 |
ex_err_fn(exoid, __func__, errmsg, EX_DUPLICATEID); |
| 194 |
free(blocks_to_define); |
| 195 |
EX_FUNC_LEAVE(EX_FATAL); |
| 196 |
} |
| 197 |
|
| 198 |
/* Keep track of the total number of element blocks defined using a counter |
| 199 |
stored in a linked list keyed by exoid. |
| 200 |
NOTE: exi_get_file_item is a function that finds the number of element |
| 201 |
blocks for a specific file and returns that value. |
| 202 |
*/ |
| 203 |
cur_num_blk = exi_get_file_item(exoid, exi_get_counter_list(blocks[i].type)); |
| 204 |
if (cur_num_blk >= (int)num_blk) { |
| 205 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: exceeded number of %ss (%d) defined in file id %d", |
| 206 |
ex_name_of_object(blocks[i].type), (int)num_blk, exoid); |
| 207 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 208 |
free(blocks_to_define); |
| 209 |
EX_FUNC_LEAVE(EX_FATAL); |
| 210 |
} |
| 211 |
|
| 212 |
/* NOTE: exi_inc_file_item is a function that finds the number of element |
| 213 |
blocks for a specific file and returns that value incremented. */ |
| 214 |
cur_num_blk = exi_inc_file_item(exoid, exi_get_counter_list(blocks[i].type)); |
| 215 |
start[0] = cur_num_blk; |
| 216 |
|
| 217 |
/* write out block id to previously defined id array variable*/ |
| 218 |
status = nc_put_var1_longlong(exoid, varid, start, (long long *)&blocks[i].id); |
| 219 |
|
| 220 |
if (status != NC_NOERR) { |
| 221 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s id to file id %d", |
| 222 |
ex_name_of_object(blocks[i].type), exoid); |
| 223 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 224 |
free(blocks_to_define); |
| 225 |
EX_FUNC_LEAVE(EX_FATAL); |
| 226 |
} |
| 227 |
|
| 228 |
blocks_to_define[i] = start[0] + 1; /* element id index into vblkids array*/ |
| 229 |
|
| 230 |
if (blocks[i].num_entry == 0) { /* Is this a NULL element block? */ |
| 231 |
blk_stat = 0; /* change element block status to NULL */ |
| 232 |
} |
| 233 |
else { |
| 234 |
blk_stat = 1; /* change element block status to TRUE */ |
| 235 |
} |
| 236 |
|
| 237 |
if ((status = nc_inq_varid(exoid, vblksta, &varid)) != NC_NOERR) { |
| 238 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s status in file id %d", |
| 239 |
ex_name_of_object(blocks[i].type), exoid); |
| 240 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 241 |
free(blocks_to_define); |
| 242 |
EX_FUNC_LEAVE(EX_FATAL); |
| 243 |
} |
| 244 |
|
| 245 |
if ((status = nc_put_var1_int(exoid, varid, start, &blk_stat)) != NC_NOERR) { |
| 246 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 247 |
"ERROR: failed to store %s id %" PRId64 " status to file id %d", |
| 248 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 249 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 250 |
free(blocks_to_define); |
| 251 |
EX_FUNC_LEAVE(EX_FATAL); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/* put netcdf file into define mode */ |
| 256 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 257 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); |
| 258 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 259 |
free(blocks_to_define); |
| 260 |
EX_FUNC_LEAVE(EX_FATAL); |
| 261 |
} |
| 262 |
|
| 263 |
for (i = 0; i < block_count; i++) { |
| 264 |
if (blocks[i].num_entry == 0) { /* Is this a NULL element block? */ |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
blk_id_ndx = blocks_to_define[i]; |
| 269 |
|
| 270 |
switch (blocks[i].type) { |
| 271 |
case EX_EDGE_BLOCK: |
| 272 |
dneblk = DIM_NUM_ED_IN_EBLK(blk_id_ndx); |
| 273 |
dnnpe = DIM_NUM_NOD_PER_ED(blk_id_ndx); |
| 274 |
dnepe = NULL; |
| 275 |
dnfpe = NULL; |
| 276 |
dnape = DIM_NUM_ATT_IN_EBLK(blk_id_ndx); |
| 277 |
vblkatt = VAR_EATTRIB(blk_id_ndx); |
| 278 |
vattnam = VAR_NAME_EATTRIB(blk_id_ndx); |
| 279 |
vnodcon = VAR_EBCONN(blk_id_ndx); |
| 280 |
vedgcon = NULL; |
| 281 |
vfaccon = NULL; |
| 282 |
break; |
| 283 |
case EX_FACE_BLOCK: |
| 284 |
dneblk = DIM_NUM_FA_IN_FBLK(blk_id_ndx); |
| 285 |
dnnpe = DIM_NUM_NOD_PER_FA(blk_id_ndx); |
| 286 |
dnepe = NULL; |
| 287 |
dnfpe = NULL; |
| 288 |
dnape = DIM_NUM_ATT_IN_FBLK(blk_id_ndx); |
| 289 |
vblkatt = VAR_FATTRIB(blk_id_ndx); |
| 290 |
vattnam = VAR_NAME_FATTRIB(blk_id_ndx); |
| 291 |
vnodcon = VAR_FBCONN(blk_id_ndx); |
| 292 |
vnpecnt = VAR_FBEPEC(blk_id_ndx); |
| 293 |
vedgcon = NULL; |
| 294 |
vfaccon = NULL; |
| 295 |
break; |
| 296 |
case EX_ELEM_BLOCK: |
| 297 |
dneblk = DIM_NUM_EL_IN_BLK(blk_id_ndx); |
| 298 |
dnnpe = DIM_NUM_NOD_PER_EL(blk_id_ndx); |
| 299 |
dnepe = DIM_NUM_EDG_PER_EL(blk_id_ndx); |
| 300 |
dnfpe = DIM_NUM_FAC_PER_EL(blk_id_ndx); |
| 301 |
dnape = DIM_NUM_ATT_IN_BLK(blk_id_ndx); |
| 302 |
vblkatt = VAR_ATTRIB(blk_id_ndx); |
| 303 |
vattnam = VAR_NAME_ATTRIB(blk_id_ndx); |
| 304 |
vnodcon = VAR_CONN(blk_id_ndx); |
| 305 |
vnpecnt = VAR_EBEPEC(blk_id_ndx); |
| 306 |
vedgcon = VAR_ECONN(blk_id_ndx); |
| 307 |
vfaccon = VAR_FCONN(blk_id_ndx); |
| 308 |
break; |
| 309 |
default: goto error_ret; |
| 310 |
} |
| 311 |
|
| 312 |
/* define some dimensions and variables*/ |
| 313 |
if ((status = nc_def_dim(exoid, dneblk, blocks[i].num_entry, &numblkdim)) != NC_NOERR) { |
| 314 |
if (status == NC_ENAMEINUSE) { /* duplicate entry */ |
| 315 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s %" PRId64 " already defined in file id %d", |
| 316 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 317 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 318 |
} |
| 319 |
else { |
| 320 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 321 |
"ERROR: failed to define number of entities/block for %s %" PRId64 " file id %d", |
| 322 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 323 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 324 |
} |
| 325 |
goto error_ret; /* exit define mode and return */ |
| 326 |
} |
| 327 |
|
| 328 |
if (dnnpe && blocks[i].num_nodes_per_entry > 0) { |
| 329 |
/* A nfaced block would not have any nodes defined... */ |
| 330 |
if ((status = nc_def_dim(exoid, dnnpe, blocks[i].num_nodes_per_entry, &nnodperentdim)) != |
| 331 |
NC_NOERR) { |
| 332 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 333 |
"ERROR: failed to define number of nodes/entity for %s %" PRId64 " in file id %d", |
| 334 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 335 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 336 |
goto error_ret; /* exit define mode and return */ |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (dnepe && blocks[i].num_edges_per_entry > 0) { |
| 341 |
if ((status = nc_def_dim(exoid, dnepe, blocks[i].num_edges_per_entry, &nedgperentdim)) != |
| 342 |
NC_NOERR) { |
| 343 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 344 |
"ERROR: failed to define number of edges/entity for %s %" PRId64 " in file id %d", |
| 345 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 346 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 347 |
goto error_ret; /* exit define mode and return */ |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if (dnfpe && blocks[i].num_faces_per_entry > 0) { |
| 352 |
if ((status = nc_def_dim(exoid, dnfpe, blocks[i].num_faces_per_entry, &nfacperentdim)) != |
| 353 |
NC_NOERR) { |
| 354 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 355 |
"ERROR: failed to define number of faces/entity for %s %" PRId64 " in file id %d", |
| 356 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 357 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 358 |
goto error_ret; /* exit define mode and return */ |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/* element attribute array */ |
| 363 |
if (blocks[i].num_attribute > 0) { |
| 364 |
|
| 365 |
if ((status = nc_def_dim(exoid, dnape, blocks[i].num_attribute, &numattrdim)) != NC_NOERR) { |
| 366 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 367 |
"ERROR: failed to define number of attributes in %s %" PRId64 " in file id %d", |
| 368 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 369 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 370 |
goto error_ret; /* exit define mode and return */ |
| 371 |
} |
| 372 |
|
| 373 |
dims[0] = numblkdim; |
| 374 |
dims[1] = numattrdim; |
| 375 |
|
| 376 |
if ((status = nc_def_var(exoid, vblkatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) { |
| 377 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 378 |
"ERROR: failed to define attributes for %s %" PRId64 " in file id %d", |
| 379 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 380 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 381 |
goto error_ret; /* exit define mode and return */ |
| 382 |
} |
| 383 |
exi_compress_variable(exoid, varid, 2); |
| 384 |
|
| 385 |
/* inquire previously defined dimensions */ |
| 386 |
if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) { |
| 387 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid); |
| 388 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 389 |
goto error_ret; |
| 390 |
} |
| 391 |
|
| 392 |
/* Attribute names... */ |
| 393 |
dims[0] = numattrdim; |
| 394 |
dims[1] = strdim; |
| 395 |
|
| 396 |
if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) { |
| 397 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 398 |
"ERROR: failed to define %s attribute name array in file id %d", |
| 399 |
ex_name_of_object(blocks[i].type), exoid); |
| 400 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 401 |
goto error_ret; /* exit define mode and return */ |
| 402 |
} |
| 403 |
#if defined(EX_CAN_USE_NC_DEF_VAR_FILL) |
| 404 |
int fill = NC_FILL_CHAR; |
| 405 |
nc_def_var_fill(exoid, att_name_varid, 0, &fill); |
| 406 |
#endif |
| 407 |
} |
| 408 |
|
| 409 |
conn_int_type = NC_INT; |
| 410 |
if (ex_int64_status(exoid) & EX_BULK_INT64_DB) { |
| 411 |
conn_int_type = NC_INT64; |
| 412 |
} |
| 413 |
|
| 414 |
/* See if storing an 'nsided' element block (arbitrary 2d polyhedra or super |
| 415 |
* element) */ |
| 416 |
if (strlen(blocks[i].topology) >= 3) { |
| 417 |
if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && |
| 418 |
(blocks[i].topology[1] == 's' || blocks[i].topology[1] == 'S') && |
| 419 |
(blocks[i].topology[2] == 'i' || blocks[i].topology[2] == 'I')) { |
| 420 |
arbitrary_polyhedra = 1; |
| 421 |
} |
| 422 |
else if ((blocks[i].topology[0] == 'n' || blocks[i].topology[0] == 'N') && |
| 423 |
(blocks[i].topology[1] == 'f' || blocks[i].topology[1] == 'F') && |
| 424 |
(blocks[i].topology[2] == 'a' || blocks[i].topology[2] == 'A')) { |
| 425 |
/* If a FACE_BLOCK, then we are dealing with the faces of the nfaced |
| 426 |
* blocks[i]. */ |
| 427 |
arbitrary_polyhedra = blocks[i].type == EX_FACE_BLOCK ? 1 : 2; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
/* element connectivity array */ |
| 432 |
if (arbitrary_polyhedra > 0) { |
| 433 |
if (blocks[i].type != EX_FACE_BLOCK && blocks[i].type != EX_ELEM_BLOCK) { |
| 434 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 435 |
"ERROR: Bad block type (%d) for nsided/nfaced block in file id %d", blocks[i].type, |
| 436 |
exoid); |
| 437 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 438 |
goto error_ret; |
| 439 |
} |
| 440 |
|
| 441 |
if (arbitrary_polyhedra == 1) { |
| 442 |
dims[0] = nnodperentdim; |
| 443 |
vconn = vnodcon; |
| 444 |
|
| 445 |
/* store entity types as attribute of npeid variable -- node/elem, |
| 446 |
* node/face, face/elem*/ |
| 447 |
entity_type1 = "NODE"; |
| 448 |
if (blocks[i].type == EX_ELEM_BLOCK) { |
| 449 |
entity_type2 = "ELEM"; |
| 450 |
} |
| 451 |
else { |
| 452 |
entity_type2 = "FACE"; |
| 453 |
} |
| 454 |
} |
| 455 |
else if (arbitrary_polyhedra == 2) { |
| 456 |
dims[0] = nfacperentdim; |
| 457 |
vconn = vfaccon; |
| 458 |
|
| 459 |
/* store entity types as attribute of npeid variable -- node/elem, |
| 460 |
* node/face, face/elem*/ |
| 461 |
entity_type1 = "FACE"; |
| 462 |
entity_type2 = "ELEM"; |
| 463 |
} |
| 464 |
|
| 465 |
if ((status = nc_def_var(exoid, vconn, conn_int_type, 1, dims, &connid)) != NC_NOERR) { |
| 466 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 467 |
"ERROR: failed to create connectivity array for %s %" PRId64 " in file id %d", |
| 468 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 469 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 470 |
goto error_ret; /* exit define mode and return */ |
| 471 |
} |
| 472 |
|
| 473 |
/* element face-per-element or node-per-element count array */ |
| 474 |
dims[0] = numblkdim; |
| 475 |
|
| 476 |
if ((status = nc_def_var(exoid, vnpecnt, conn_int_type, 1, dims, &npeid)) != NC_NOERR) { |
| 477 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 478 |
"ERROR: failed to create face- or node- per-entity " |
| 479 |
"count array for %s %" PRId64 " in file id %d", |
| 480 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 481 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 482 |
goto error_ret; /* exit define mode and return */ |
| 483 |
} |
| 484 |
|
| 485 |
if ((status = nc_put_att_text(exoid, npeid, "entity_type1", strlen(entity_type1) + 1, |
| 486 |
entity_type1)) != NC_NOERR) { |
| 487 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 488 |
"ERROR: failed to store entity type attribute text for %s %" PRId64 |
| 489 |
" in file id %d", |
| 490 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 491 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 492 |
goto error_ret; /* exit define mode and return */ |
| 493 |
} |
| 494 |
if ((status = nc_put_att_text(exoid, npeid, "entity_type2", strlen(entity_type2) + 1, |
| 495 |
entity_type2)) != NC_NOERR) { |
| 496 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 497 |
"ERROR: failed to store entity type attribute text for %s %" PRId64 |
| 498 |
" in file id %d", |
| 499 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 500 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 501 |
goto error_ret; /* exit define mode and return */ |
| 502 |
} |
| 503 |
} |
| 504 |
else { |
| 505 |
if (blocks[i].num_nodes_per_entry > 0) { |
| 506 |
/* "Normal" (non-polyhedra) element block type */ |
| 507 |
dims[0] = numblkdim; |
| 508 |
dims[1] = nnodperentdim; |
| 509 |
|
| 510 |
if ((status = nc_def_var(exoid, vnodcon, conn_int_type, 2, dims, &connid)) != NC_NOERR) { |
| 511 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 512 |
"ERROR: failed to create connectivity array for %s %" PRId64 " in file id %d", |
| 513 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 514 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 515 |
goto error_ret; /* exit define mode and return */ |
| 516 |
} |
| 517 |
exi_compress_variable(exoid, connid, 1); |
| 518 |
} |
| 519 |
} |
| 520 |
/* store element type as attribute of connectivity variable */ |
| 521 |
if (connid > 0) { |
| 522 |
if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(blocks[i].topology) + 1, |
| 523 |
blocks[i].topology)) != NC_NOERR) { |
| 524 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s type name %s in file id %d", |
| 525 |
ex_name_of_object(blocks[i].type), blocks[i].topology, exoid); |
| 526 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 527 |
goto error_ret; /* exit define mode and return */ |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
if (arbitrary_polyhedra == 0) { |
| 532 |
if (vedgcon && blocks[i].num_edges_per_entry) { |
| 533 |
dims[0] = numblkdim; |
| 534 |
dims[1] = nedgperentdim; |
| 535 |
|
| 536 |
if ((status = nc_def_var(exoid, vedgcon, conn_int_type, 2, dims, &varid)) != NC_NOERR) { |
| 537 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 538 |
"ERROR: failed to create edge connectivity array for %s %" PRId64 |
| 539 |
" in file id %d", |
| 540 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 541 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 542 |
goto error_ret; /* exit define mode and return */ |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
if (vfaccon && blocks[i].num_faces_per_entry) { |
| 547 |
dims[0] = numblkdim; |
| 548 |
dims[1] = nfacperentdim; |
| 549 |
|
| 550 |
if ((status = nc_def_var(exoid, vfaccon, conn_int_type, 2, dims, &varid)) != NC_NOERR) { |
| 551 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 552 |
"ERROR: failed to create face connectivity array for %s %" PRId64 |
| 553 |
" in file id %d", |
| 554 |
ex_name_of_object(blocks[i].type), blocks[i].id, exoid); |
| 555 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 556 |
goto error_ret; /* exit define mode and return */ |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
free(blocks_to_define); |
| 563 |
|
| 564 |
/* leave define mode */ |
| 565 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 566 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 567 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 568 |
EX_FUNC_LEAVE(EX_FATAL); |
| 569 |
} |
| 570 |
|
| 571 |
for (i = 0; i < block_count; i++) { |
| 572 |
switch (blocks[i].type) { |
| 573 |
case EX_EDGE_BLOCK: vblkids = VAR_ID_ED_BLK; break; |
| 574 |
case EX_FACE_BLOCK: vblkids = VAR_ID_FA_BLK; break; |
| 575 |
case EX_ELEM_BLOCK: vblkids = VAR_ID_EL_BLK; break; |
| 576 |
default: EX_FUNC_LEAVE(EX_FATAL); /* should have been handled earlier; quiet compiler here */ |
| 577 |
} |
| 578 |
|
| 579 |
nc_inq_varid(exoid, vblkids, &att_name_varid); |
| 580 |
|
| 581 |
if (blocks[i].num_attribute > 0 && att_name_varid >= 0) { |
| 582 |
/* Output a dummy empty attribute name in case client code doesn't |
| 583 |
write anything; avoids corruption in some cases. |
| 584 |
*/ |
| 585 |
size_t count[2]; |
| 586 |
char *text = ""; |
| 587 |
|
| 588 |
count[0] = 1; |
| 589 |
start[1] = 0; |
| 590 |
count[1] = strlen(text) + 1; |
| 591 |
|
| 592 |
for (int64_t j = 0; j < blocks[i].num_attribute; j++) { |
| 593 |
start[0] = j; |
| 594 |
nc_put_vara_text(exoid, att_name_varid, start, count, text); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
EX_FUNC_LEAVE(EX_NOERR); |
| 600 |
|
| 601 |
/* Fatal error: exit definition mode and return */ |
| 602 |
error_ret: |
| 603 |
free(blocks_to_define); |
| 604 |
|
| 605 |
exi_leavedef(exoid, __func__); |
| 606 |
EX_FUNC_LEAVE(EX_FATAL); |
| 607 |
} |