| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 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 |
#include "exodusII.h" |
| 10 |
#include "exodusII_int.h" |
| 11 |
#include <stdbool.h> |
| 12 |
/*! |
| 13 |
* writes the assembly parameters and optionally assembly data for 1 or more assemblies |
| 14 |
* \param exoid exodus file id |
| 15 |
* \param count size of `assemblies` array |
| 16 |
* \param *assemblies array of ex_assembly structures |
| 17 |
*/ |
| 18 |
|
| 19 |
int ex_put_assemblies(int exoid, size_t count, const struct ex_assembly *assemblies) |
| 20 |
{ |
| 21 |
int dimid, status, dims[1]; |
| 22 |
char errmsg[MAX_ERR_LENGTH]; |
| 23 |
|
| 24 |
int int_type; |
| 25 |
|
| 26 |
EX_FUNC_ENTER(); |
| 27 |
|
| 28 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 29 |
EX_FUNC_LEAVE(EX_FATAL); |
| 30 |
} |
| 31 |
|
| 32 |
/* Note that this routine can be called: |
| 33 |
1) just define the assemblies |
| 34 |
2) just output the assembly data (after a previous call to define) |
| 35 |
3) define and output the assembly data in one call. |
| 36 |
*/ |
| 37 |
|
| 38 |
int *entlst_id = (int *)calloc(count, sizeof(int)); |
| 39 |
|
| 40 |
size_t max_name_len = 0; |
| 41 |
bool in_define = false; |
| 42 |
for (size_t i = 0; i < count; i++) { |
| 43 |
/* See if an assembly with this id has already been defined or exists on file... */ |
| 44 |
if (nc_inq_varid(exoid, VAR_ENTITY_ASSEMBLY(assemblies[i].id), &entlst_id[i]) != NC_NOERR) { |
| 45 |
/* Assembly has not already been defined */ |
| 46 |
/* put netcdf file into define mode */ |
| 47 |
if (!in_define) { |
| 48 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 49 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", |
| 50 |
exoid); |
| 51 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 52 |
EX_FUNC_LEAVE(EX_FATAL); |
| 53 |
} |
| 54 |
in_define = true; |
| 55 |
} |
| 56 |
|
| 57 |
if (assemblies[i].entity_count <= 0) { |
| 58 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 59 |
"ERROR: assembly %" PRId64 |
| 60 |
" -- has non-positive entity_count size %d which is not allowed in file id %d", |
| 61 |
assemblies[i].id, assemblies[i].entity_count, exoid); |
| 62 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 63 |
} |
| 64 |
|
| 65 |
char *numentryptr = DIM_NUM_ENTITY_ASSEMBLY(assemblies[i].id); |
| 66 |
|
| 67 |
/* define dimensions and variables */ |
| 68 |
if ((status = nc_def_dim(exoid, numentryptr, assemblies[i].entity_count, &dimid)) != |
| 69 |
NC_NOERR) { |
| 70 |
if (status == NC_ENAMEINUSE) { |
| 71 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 72 |
"ERROR: assembly %" PRId64 " -- size already defined in file id %d", |
| 73 |
assemblies[i].id, exoid); |
| 74 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 75 |
} |
| 76 |
else { |
| 77 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 78 |
"ERROR: failed to define number of entries in assembly %" PRId64 |
| 79 |
" in file id %d", |
| 80 |
assemblies[i].id, exoid); |
| 81 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 82 |
} |
| 83 |
goto error_ret; |
| 84 |
} |
| 85 |
|
| 86 |
int_type = NC_INT; |
| 87 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 88 |
int_type = NC_INT64; |
| 89 |
} |
| 90 |
|
| 91 |
/* create variable array in which to store the entry lists */ |
| 92 |
dims[0] = dimid; |
| 93 |
if ((status = nc_def_var(exoid, VAR_ENTITY_ASSEMBLY(assemblies[i].id), int_type, 1, dims, |
| 94 |
&entlst_id[i])) != NC_NOERR) { |
| 95 |
if (status == NC_ENAMEINUSE) { |
| 96 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 97 |
"ERROR: entry list already exists for assembly %" PRId64 " in file id %d", |
| 98 |
assemblies[i].id, exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 100 |
} |
| 101 |
else { |
| 102 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 103 |
"ERROR: failed to create entry list for assembly %" PRId64 " in file id %d", |
| 104 |
assemblies[i].id, exoid); |
| 105 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 106 |
} |
| 107 |
goto error_ret; /* exit define mode and return */ |
| 108 |
} |
| 109 |
exi_compress_variable(exoid, entlst_id[i], 1); |
| 110 |
|
| 111 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 112 |
long long tmp = assemblies[i].id; |
| 113 |
status = nc_put_att_longlong(exoid, entlst_id[i], EX_ATTRIBUTE_ID, NC_INT64, 1, &tmp); |
| 114 |
} |
| 115 |
else { |
| 116 |
int id = assemblies[i].id; |
| 117 |
status = nc_put_att_int(exoid, entlst_id[i], EX_ATTRIBUTE_ID, NC_INT, 1, &id); |
| 118 |
} |
| 119 |
if (status != NC_NOERR) { |
| 120 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 121 |
"ERROR: failed to store assembly id %" PRId64 " in file id %d", assemblies[i].id, |
| 122 |
exoid); |
| 123 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 124 |
goto error_ret; /* exit define mode and return */ |
| 125 |
} |
| 126 |
|
| 127 |
int type = assemblies[i].type; |
| 128 |
if ((status = nc_put_att_int(exoid, entlst_id[i], EX_ATTRIBUTE_TYPE, NC_INT, 1, &type)) != |
| 129 |
NC_NOERR) { |
| 130 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store assembly type %d in file id %d", |
| 131 |
assemblies[i].type, exoid); |
| 132 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 133 |
goto error_ret; /* exit define mode and return */ |
| 134 |
} |
| 135 |
|
| 136 |
size_t length = strlen(assemblies[i].name) + 1; |
| 137 |
if ((status = nc_put_att_text(exoid, entlst_id[i], EX_ATTRIBUTE_NAME, length, |
| 138 |
assemblies[i].name)) != NC_NOERR) { |
| 139 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store assembly name %s in file id %d", |
| 140 |
assemblies[i].name, exoid); |
| 141 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 142 |
goto error_ret; /* exit define mode and return */ |
| 143 |
} |
| 144 |
if (length > max_name_len) { |
| 145 |
max_name_len = length; |
| 146 |
} |
| 147 |
|
| 148 |
{ |
| 149 |
char *contains = ex_name_of_object(assemblies[i].type); |
| 150 |
if ((status = nc_put_att_text(exoid, entlst_id[i], EX_ATTRIBUTE_TYPENAME, |
| 151 |
strlen(contains) + 1, contains)) != NC_NOERR) { |
| 152 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 153 |
"ERROR: failed to store assembly type name %s in file id %d", assemblies[i].name, |
| 154 |
exoid); |
| 155 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 156 |
goto error_ret; /* exit define mode and return */ |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/* Increment assembly count */ |
| 161 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 162 |
if (file) { |
| 163 |
file->assembly_count++; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
/* leave define mode */ |
| 168 |
if (in_define) { |
| 169 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 170 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid); |
| 171 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 172 |
free(entlst_id); |
| 173 |
EX_FUNC_LEAVE(EX_FATAL); |
| 174 |
} |
| 175 |
in_define = false; |
| 176 |
} |
| 177 |
|
| 178 |
/* Update the maximum_name_length attribute on the file. */ |
| 179 |
if (max_name_len > 0) { |
| 180 |
exi_update_max_name_length(exoid, max_name_len - 1); |
| 181 |
} |
| 182 |
|
| 183 |
/* Assembly are now all defined; see if any set data needs to be output... */ |
| 184 |
for (size_t i = 0; i < count; i++) { |
| 185 |
if (assemblies[i].entity_list != NULL) { |
| 186 |
if ((status = nc_put_var_longlong(exoid, entlst_id[i], |
| 187 |
(long long *)assemblies[i].entity_list)) != EX_NOERR) { |
| 188 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 189 |
"ERROR: failed to output entity list for assembly %" PRId64 " in file id %d", |
| 190 |
assemblies[i].id, exoid); |
| 191 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 192 |
free(entlst_id); |
| 193 |
EX_FUNC_LEAVE(EX_FATAL); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
free(entlst_id); |
| 198 |
EX_FUNC_LEAVE(EX_NOERR); |
| 199 |
|
| 200 |
/* Fatal error: exit definition mode and return */ |
| 201 |
error_ret: |
| 202 |
if (in_define) { |
| 203 |
exi_leavedef(exoid, __func__); |
| 204 |
} |
| 205 |
free(entlst_id); |
| 206 |
EX_FUNC_LEAVE(EX_FATAL); |
| 207 |
} |