| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022 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_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
|
| 14 |
The function ex_put_prop_names() writes object property names and |
| 15 |
allocates space for object property arrays used to assign integer |
| 16 |
properties to element blocks, node sets, or side sets. The property |
| 17 |
arrays are initialized to zero (0). Although this function is |
| 18 |
optional, since ex_put_prop() will allocate space within the data file |
| 19 |
if it hasn't been previously allocated, it is more efficient to use |
| 20 |
ex_put_prop_names() if there is more than one property to store. |
| 21 |
|
| 22 |
\return In case of an error, ex_put_prop_names() returns a negative number; a |
| 23 |
warning will return a positive number. Possible causes of errors |
| 24 |
include: |
| 25 |
- data file not properly opened with call to ex_create() or ex_open() |
| 26 |
- data file opened for read only. |
| 27 |
- data file not initialized properly with call to ex_put_init(). |
| 28 |
- invalid object type specified. |
| 29 |
- no object of the specified type is stored in the file. |
| 30 |
|
| 31 |
\param[in] exoid exodus file ID returned from a previous call to |
| 32 |
ex_create() or ex_open(). |
| 33 |
\param[in] obj_type Type of object; use one of the options in the table |
| 34 |
below. |
| 35 |
\param[in] num_props The number of integer properties to be assigned to all of |
| 36 |
the objects |
| 37 |
of the type specified (element blocks, node sets, or side |
| 38 |
sets). |
| 39 |
\param[in] prop_names Array containing num_props names (of maximum length |
| 40 |
of \p MAX_STR_LENGTH ) of properties to be stored. |
| 41 |
|
| 42 |
| #ex_entity_type | description | |
| 43 |
| -------------- | ------------------------- | |
| 44 |
| #EX_NODE_SET | Node Set entity type | |
| 45 |
| #EX_EDGE_BLOCK | Edge Block entity type | |
| 46 |
| #EX_EDGE_SET | Edge Set entity type | |
| 47 |
| #EX_FACE_BLOCK | Face Block entity type | |
| 48 |
| #EX_FACE_SET | Face Set entity type | |
| 49 |
| #EX_ELEM_BLOCK | Element Block entity type| |
| 50 |
| #EX_ELEM_SET | Element Set entity type | |
| 51 |
| #EX_SIDE_SET | Side Set entity type | |
| 52 |
| #EX_ELEM_MAP | Element Map entity type | |
| 53 |
| #EX_NODE_MAP | Node Map entity type | |
| 54 |
| #EX_EDGE_MAP | Edge Map entity type | |
| 55 |
| #EX_FACE_MAP | Face Map entity type | |
| 56 |
|
| 57 |
For instance, suppose a user wanted to assign the 1st, 3rd, and 5th |
| 58 |
element blocks (those element blocks stored 1st, 3rd, and 5th, |
| 59 |
regardless of their ID) to a group (property) called \b TOP, and the |
| 60 |
2nd, 3rd, and 4th element blocks to a group called \b LSIDE. This |
| 61 |
could be accomplished with the following code: |
| 62 |
|
| 63 |
~~~{.c} |
| 64 |
|
| 65 |
char* prop_names[2]; |
| 66 |
int top_part[] = {1,0,1,0,1}; |
| 67 |
int lside_part[] = {0,1,1,1,0}; |
| 68 |
|
| 69 |
int id[] = {10, 20, 30, 40, 50}; |
| 70 |
|
| 71 |
prop_names[0] = "TOP"; |
| 72 |
prop_names[1] = "LSIDE"; |
| 73 |
|
| 74 |
\comment{This call to ex_put_prop_names is optional, but more efficient} |
| 75 |
ex_put_prop_names (exoid, EX_ELEM_BLOCK, 2, prop_names); |
| 76 |
|
| 77 |
\comment{The property values can be output individually thus} |
| 78 |
for (i=0; i < 5; i++) { |
| 79 |
ex_put_prop (exoid, EX_ELEM_BLOCK, id[i], prop_names[0], |
| 80 |
top_part[i]); |
| 81 |
|
| 82 |
ex_put_prop (exoid, EX_ELEM_BLOCK, id[i], prop_names[1], |
| 83 |
lside_part[i]); |
| 84 |
} |
| 85 |
|
| 86 |
\comment{Alternatively, the values can be output as an array} |
| 87 |
ex_put_prop_array (exoid, EX_ELEM_BLOCK, prop_names[0], |
| 88 |
top_part); |
| 89 |
|
| 90 |
ex_put_prop_array (exoid, EX_ELEM_BLOCK, prop_names[1], |
| 91 |
lside_part); |
| 92 |
|
| 93 |
~~~ |
| 94 |
|
| 95 |
*/ |
| 96 |
|
| 97 |
int ex_put_prop_names(int exoid, ex_entity_type obj_type, int num_props, char **prop_names) |
| 98 |
{ |
| 99 |
int status; |
| 100 |
int oldfill, temp; |
| 101 |
int i, propid, dimid, dims[1]; |
| 102 |
size_t name_length, prop_name_len; |
| 103 |
char *name; |
| 104 |
long long vals[1]; |
| 105 |
size_t max_name_len = 0; |
| 106 |
int int_type = NC_INT; |
| 107 |
|
| 108 |
char errmsg[MAX_ERR_LENGTH]; |
| 109 |
|
| 110 |
EX_FUNC_ENTER(); |
| 111 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 112 |
EX_FUNC_LEAVE(EX_FATAL); |
| 113 |
} |
| 114 |
|
| 115 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 116 |
int_type = NC_INT64; |
| 117 |
} |
| 118 |
|
| 119 |
/* Get the name string length */ |
| 120 |
name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH) + 1; |
| 121 |
|
| 122 |
/* inquire id of previously defined dimension (number of objects) */ |
| 123 |
if ((status = nc_inq_dimid(exoid, exi_dim_num_objects(obj_type), &dimid)) != NC_NOERR) { |
| 124 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate number of %s in file id %d", |
| 125 |
ex_name_of_object(obj_type), exoid); |
| 126 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 127 |
EX_FUNC_LEAVE(EX_FATAL); |
| 128 |
} |
| 129 |
|
| 130 |
nc_set_fill(exoid, NC_FILL, &oldfill); /* fill with zeros per routine spec */ |
| 131 |
|
| 132 |
/* put netcdf file into define mode */ |
| 133 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 134 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid); |
| 135 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 136 |
EX_FUNC_LEAVE(EX_FATAL); |
| 137 |
} |
| 138 |
|
| 139 |
/* define num_props variables; we postpend the netcdf variable name with */ |
| 140 |
/* a counter starting at 2 because "xx_prop1" is reserved for the id array*/ |
| 141 |
dims[0] = dimid; |
| 142 |
|
| 143 |
for (i = 0; i < num_props; i++) { |
| 144 |
switch (obj_type) { |
| 145 |
case EX_ELEM_BLOCK: name = VAR_EB_PROP(i + 2); break; |
| 146 |
case EX_FACE_BLOCK: name = VAR_FA_PROP(i + 2); break; |
| 147 |
case EX_EDGE_BLOCK: name = VAR_ED_PROP(i + 2); break; |
| 148 |
case EX_NODE_SET: name = VAR_NS_PROP(i + 2); break; |
| 149 |
case EX_SIDE_SET: name = VAR_SS_PROP(i + 2); break; |
| 150 |
case EX_EDGE_SET: name = VAR_ES_PROP(i + 2); break; |
| 151 |
case EX_FACE_SET: name = VAR_FS_PROP(i + 2); break; |
| 152 |
case EX_ELEM_SET: name = VAR_ELS_PROP(i + 2); break; |
| 153 |
case EX_ELEM_MAP: name = VAR_EM_PROP(i + 2); break; |
| 154 |
case EX_FACE_MAP: name = VAR_FAM_PROP(i + 2); break; |
| 155 |
case EX_EDGE_MAP: name = VAR_EDM_PROP(i + 2); break; |
| 156 |
case EX_NODE_MAP: name = VAR_NM_PROP(i + 2); break; |
| 157 |
default: |
| 158 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type, |
| 159 |
exoid); |
| 160 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 161 |
goto error_ret; /* Exit define mode and return */ |
| 162 |
} |
| 163 |
|
| 164 |
if ((status = nc_def_var(exoid, name, int_type, 1, dims, &propid)) != NC_NOERR) { |
| 165 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 166 |
"ERROR: failed to create property array variable in file id %d", exoid); |
| 167 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 168 |
goto error_ret; /* Exit define mode and return */ |
| 169 |
} |
| 170 |
|
| 171 |
vals[0] = 0; /* fill value */ |
| 172 |
|
| 173 |
/* create attribute to cause variable to fill with zeros per routine spec |
| 174 |
*/ |
| 175 |
if ((status = nc_put_att_longlong(exoid, propid, NC_FillValue, int_type, 1, vals)) != |
| 176 |
NC_NOERR) { |
| 177 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 178 |
"ERROR: failed to create property name fill attribute in file id %d", exoid); |
| 179 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 180 |
goto error_ret; /* Exit define mode and return */ |
| 181 |
} |
| 182 |
|
| 183 |
/* Check that the property name length is less than MAX_NAME_LENGTH */ |
| 184 |
prop_name_len = strlen(prop_names[i]) + 1; |
| 185 |
if (prop_name_len > name_length) { |
| 186 |
fprintf(stderr, |
| 187 |
"Warning: The property name '%s' is too long.\n\tIt will " |
| 188 |
"be truncated from %d to %d characters\n", |
| 189 |
prop_names[i], (int)prop_name_len - 1, (int)name_length - 1); |
| 190 |
prop_name_len = name_length; |
| 191 |
} |
| 192 |
|
| 193 |
if (prop_name_len > max_name_len) { |
| 194 |
max_name_len = prop_name_len; |
| 195 |
} |
| 196 |
|
| 197 |
/* store property name as attribute of property array variable */ |
| 198 |
if ((status = nc_put_att_text(exoid, propid, ATT_PROP_NAME, prop_name_len, prop_names[i])) != |
| 199 |
NC_NOERR) { |
| 200 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store property name %s in file id %d", |
| 201 |
prop_names[i], exoid); |
| 202 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 203 |
goto error_ret; /* Exit define mode and return */ |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* leave define mode */ |
| 208 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 209 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 210 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 211 |
EX_FUNC_LEAVE(EX_FATAL); |
| 212 |
} |
| 213 |
|
| 214 |
/* Update the maximum_name_length attribute on the file. */ |
| 215 |
exi_update_max_name_length(exoid, max_name_len - 1); |
| 216 |
|
| 217 |
nc_set_fill(exoid, oldfill, &temp); /* default: turn off fill */ |
| 218 |
EX_FUNC_LEAVE(EX_NOERR); |
| 219 |
|
| 220 |
/* Fatal error: exit definition mode and return */ |
| 221 |
error_ret: |
| 222 |
exi_leavedef(exoid, __func__); |
| 223 |
EX_FUNC_LEAVE(EX_FATAL); |
| 224 |
} |