Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_prop.c

raw
1 /*
2 * Copyright(C) 1999-2020 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, exi_id_lkup, etc
11 #include <stdbool.h>
12
13 /*!
14
15 The function ex_put_prop() stores an integer object property value to
16 a single element block, node set, or side set. Although it is not
17 necessary to invoke ex_put_prop_names(), since ex_put_prop() will
18 allocate space within the data file if it hasn't been previously
19 allocated, it is more efficient to use ex_put_prop_names() if there is
20 more than one property to store.
21
22 It should be noted that the interpretation of the values of the
23 integers stored as properties is left to the application code. In
24 general, a zero (0) means the object does not have the specified
25 property (or is not in the specified group); a nonzero value means the
26 object does have the specified property. When space is allocated for
27 the properties using ex_put_prop_names() or ex_put_prop(), the
28 properties are initialized to zero (0).
29
30 Because the ID of an element block, node set, or side set is just
31 another property (named \b ID), this routine can be used to change
32 the value of an ID. This feature must be used with caution, though,
33 because changing the ID of an object to the ID of another object of
34 the same type (element block, node set, or side set) would cause two
35 objects to have the same ID, and thus only the first would be
36 accessible. Therefore, ex_put_prop() issues a warning if a user
37 attempts to give two objects the same ID.
38
39 \return In case of an error, ex_put_prop() returns a negative number;
40 a warning will return a positive number. Possible causes of errors
41 include:
42 - data file not properly opened with call to ex_create() or ex_open()
43 - data file opened for read only.
44 - data file not initialized properly with call to ex_put_init().
45 - invalid object type specified.
46 - a warning is issued if a user attempts to change the ID of an
47 object to the ID of an existing object of the same type.
48
49 \param[in] exoid exodus file ID returned from a previous call to
50 ex_create() or ex_open().
51 \param[in] obj_type Type of object; use one of the options in the table below.
52 \param[in] obj_id The element block, node set, or side set ID.
53 \param[in] prop_name The name of the property for which the value will be
54 stored.
55 Maximum length of this string is \p MAX_STR_LENGTH .
56 \param[in] value The value of the property.
57
58 | #ex_entity_type | description |
59 | -------------- | ------------------------- |
60 | #EX_NODE_SET | Node Set entity type |
61 | #EX_EDGE_BLOCK | Edge Block entity type |
62 | #EX_EDGE_SET | Edge Set entity type |
63 | #EX_FACE_BLOCK | Face Block entity type |
64 | #EX_FACE_SET | Face Set entity type |
65 | #EX_ELEM_BLOCK | Element Block entity type|
66 | #EX_ELEM_SET | Element Set entity type |
67 | #EX_SIDE_SET | Side Set entity type |
68 | #EX_ELEM_MAP | Element Map entity type |
69 | #EX_NODE_MAP | Node Map entity type |
70 | #EX_EDGE_MAP | Edge Map entity type |
71 | #EX_FACE_MAP | Face Map entity type |
72
73 For an example of code to write out an object property, refer to the
74 description for ex_put_prop_names().
75 */
76
77 int ex_put_prop(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, const char *prop_name,
78 ex_entity_id value)
79 {
80 int status;
81 int oldfill = 0;
82 int temp;
83 bool found = false;
84 int num_props, i, dimid, propid, dims[1];
85 int int_type;
86 size_t start[1];
87 size_t prop_name_len, name_length;
88 char *name;
89 char tmpstr[MAX_STR_LENGTH + 1];
90 char *dim_name;
91 long long vals[1];
92
93 char errmsg[MAX_ERR_LENGTH];
94
95 EX_FUNC_ENTER();
96 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
97 EX_FUNC_LEAVE(EX_FATAL);
98 }
99
100 /* check if property has already been created */
101
102 num_props = ex_get_num_props(exoid, obj_type);
103
104 if (num_props > 1) { /* any properties other than the default 1? */
105
106 for (i = 1; i <= num_props; i++) {
107 switch (obj_type) {
108 case EX_ELEM_BLOCK: name = VAR_EB_PROP(i); break;
109 case EX_EDGE_BLOCK: name = VAR_ED_PROP(i); break;
110 case EX_FACE_BLOCK: name = VAR_FA_PROP(i); break;
111 case EX_NODE_SET: name = VAR_NS_PROP(i); break;
112 case EX_EDGE_SET: name = VAR_ES_PROP(i); break;
113 case EX_FACE_SET: name = VAR_FS_PROP(i); break;
114 case EX_ELEM_SET: name = VAR_ELS_PROP(i); break;
115 case EX_SIDE_SET: name = VAR_SS_PROP(i); break;
116 case EX_ELEM_MAP: name = VAR_EM_PROP(i); break;
117 case EX_FACE_MAP: name = VAR_FAM_PROP(i); break;
118 case EX_EDGE_MAP: name = VAR_EDM_PROP(i); break;
119 case EX_NODE_MAP: name = VAR_NM_PROP(i); break;
120 default:
121 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d",
122 obj_type, exoid);
123 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
124 EX_FUNC_LEAVE(EX_FATAL);
125 }
126
127 if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) {
128 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property array id in file id %d",
129 exoid);
130 ex_err_fn(exoid, __func__, errmsg, status);
131 EX_FUNC_LEAVE(EX_FATAL);
132 }
133
134 /* compare stored attribute name with passed property name */
135 memset(tmpstr, 0, MAX_STR_LENGTH + 1);
136 if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) {
137 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property name in file id %d", exoid);
138 ex_err_fn(exoid, __func__, errmsg, status);
139 EX_FUNC_LEAVE(EX_FATAL);
140 }
141
142 if (strcmp(tmpstr, prop_name) == 0) {
143 found = true;
144 break;
145 }
146 }
147 }
148
149 /* if property array has not been created, create it */
150 if (!found) {
151
152 name_length = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH) + 1;
153
154 /* put netcdf file into define mode */
155 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
156 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid);
157 ex_err_fn(exoid, __func__, errmsg, status);
158 EX_FUNC_LEAVE(EX_FATAL);
159 }
160
161 /* create a variable with a name xx_prop#, where # is the new number */
162 /* of the property */
163
164 switch (obj_type) {
165 case EX_ELEM_BLOCK:
166 name = VAR_EB_PROP(num_props + 1);
167 dim_name = DIM_NUM_EL_BLK;
168 break;
169 case EX_FACE_BLOCK:
170 name = VAR_FA_PROP(num_props + 1);
171 dim_name = DIM_NUM_FA_BLK;
172 break;
173 case EX_EDGE_BLOCK:
174 name = VAR_ED_PROP(num_props + 1);
175 dim_name = DIM_NUM_ED_BLK;
176 break;
177 case EX_NODE_SET:
178 name = VAR_NS_PROP(num_props + 1);
179 dim_name = DIM_NUM_NS;
180 break;
181 case EX_EDGE_SET:
182 name = VAR_ES_PROP(num_props + 1);
183 dim_name = DIM_NUM_ES;
184 break;
185 case EX_FACE_SET:
186 name = VAR_FS_PROP(num_props + 1);
187 dim_name = DIM_NUM_FS;
188 break;
189 case EX_ELEM_SET:
190 name = VAR_ELS_PROP(num_props + 1);
191 dim_name = DIM_NUM_ELS;
192 break;
193 case EX_SIDE_SET:
194 name = VAR_SS_PROP(num_props + 1);
195 dim_name = DIM_NUM_SS;
196 break;
197 case EX_ELEM_MAP:
198 name = VAR_EM_PROP(num_props + 1);
199 dim_name = DIM_NUM_EM;
200 break;
201 case EX_FACE_MAP:
202 name = VAR_FAM_PROP(num_props + 1);
203 dim_name = DIM_NUM_FAM;
204 break;
205 case EX_EDGE_MAP:
206 name = VAR_EDM_PROP(num_props + 1);
207 dim_name = DIM_NUM_EDM;
208 break;
209 case EX_NODE_MAP:
210 name = VAR_NM_PROP(num_props + 1);
211 dim_name = DIM_NUM_NM;
212 break;
213 default:
214 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type,
215 exoid);
216 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
217 goto error_ret; /* Exit define mode and return */
218 }
219
220 /* inquire id of previously defined dimension (number of objects) */
221 if ((status = nc_inq_dimid(exoid, dim_name, &dimid)) != NC_NOERR) {
222 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate number of objects in file id %d",
223 exoid);
224 ex_err_fn(exoid, __func__, errmsg, status);
225 goto error_ret; /* Exit define mode and return */
226 }
227
228 dims[0] = dimid;
229 nc_set_fill(exoid, NC_FILL, &oldfill); /* fill with zeros per routine spec */
230
231 int_type = NC_INT;
232 if (ex_int64_status(exoid) & EX_IDS_INT64_DB) {
233 int_type = NC_INT64;
234 }
235
236 if ((status = nc_def_var(exoid, name, int_type, 1, dims, &propid)) != NC_NOERR) {
237 snprintf(errmsg, MAX_ERR_LENGTH,
238 "ERROR: failed to create property array variable in file id %d", exoid);
239 ex_err_fn(exoid, __func__, errmsg, status);
240 goto error_ret; /* Exit define mode and return */
241 }
242
243 vals[0] = 0; /* fill value */
244 /* create attribute to cause variable to fill with zeros per routine spec
245 */
246 if ((status = nc_put_att_longlong(exoid, propid, NC_FillValue, int_type, 1, vals)) !=
247 NC_NOERR) {
248 snprintf(errmsg, MAX_ERR_LENGTH,
249 "ERROR: failed to create property name fill attribute in file id %d", exoid);
250 ex_err_fn(exoid, __func__, errmsg, status);
251 goto error_ret; /* Exit define mode and return */
252 }
253
254 /* Check that the property name length is less than MAX_NAME_LENGTH */
255 prop_name_len = strlen(prop_name) + 1;
256 if (prop_name_len > name_length) {
257 fprintf(stderr,
258 "Warning: The property name '%s' is too long.\n\tIt will "
259 "be truncated from %d to %d characters\n",
260 prop_name, (int)prop_name_len - 1, (int)name_length - 1);
261 prop_name_len = name_length;
262 }
263
264 /* store property name as attribute of property array variable */
265 if ((status = nc_put_att_text(exoid, propid, ATT_PROP_NAME, prop_name_len,
266 (void *)prop_name)) != NC_NOERR) {
267 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store property name %s in file id %d",
268 prop_name, exoid);
269 ex_err_fn(exoid, __func__, errmsg, status);
270 goto error_ret; /* Exit define mode and return */
271 }
272
273 exi_update_max_name_length(exoid, prop_name_len - 1);
274
275 /* leave define mode */
276 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
277 EX_FUNC_LEAVE(EX_FATAL);
278 }
279
280 nc_set_fill(exoid, oldfill, &temp); /* default: nofill */
281 }
282
283 /* find index into property array using obj_id; put value in property */
284 /* array at proper index; exi_id_lkup returns an index that is 1-based,*/
285 /* but netcdf expects 0-based arrays so subtract 1 */
286
287 /* special case: property name ID - check for duplicate ID assignment */
288 if (strcmp("ID", prop_name) == 0) {
289 int indx = exi_id_lkup(exoid, obj_type, value);
290 if (indx != -EX_LOOKUPFAIL) { /* found the id */
291 snprintf(errmsg, MAX_ERR_LENGTH,
292 "Warning: attempt to assign duplicate %s ID %" PRId64 " in file id %d",
293 ex_name_of_object(obj_type), value, exoid);
294 ex_err_fn(exoid, __func__, errmsg, EX_DUPLICATEID);
295 EX_FUNC_LEAVE(EX_WARN);
296 }
297 }
298
299 status = exi_id_lkup(exoid, obj_type, obj_id);
300 if (status > 0) {
301 start[0] = status - 1;
302 }
303 else {
304 ex_get_err(NULL, NULL, &status);
305
306 if (status != 0) {
307 if (status == EX_NULLENTITY) {
308 snprintf(errmsg, MAX_ERR_LENGTH,
309 "Warning: no properties allowed for NULL %s id %" PRId64 " in file id %d",
310 ex_name_of_object(obj_type), obj_id, exoid);
311 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
312 EX_FUNC_LEAVE(EX_WARN);
313 }
314 snprintf(errmsg, MAX_ERR_LENGTH,
315 "ERROR: failed to find value %" PRId64 " in %s property array in file id %d", obj_id,
316 ex_name_of_object(obj_type), exoid);
317 ex_err_fn(exoid, __func__, errmsg, status);
318 EX_FUNC_LEAVE(EX_FATAL);
319 }
320 }
321
322 /* value is of type 'ex_entity_id' which is a typedef to int64_t or long long
323 */
324 status = nc_put_var1_longlong(exoid, propid, start, (long long *)&value);
325
326 if (status != NC_NOERR) {
327 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store property value in file id %d", exoid);
328 ex_err_fn(exoid, __func__, errmsg, status);
329 EX_FUNC_LEAVE(EX_FATAL);
330 }
331
332 EX_FUNC_LEAVE(EX_NOERR);
333
334 /* Fatal error: exit definition mode and return */
335 error_ret:
336 nc_set_fill(exoid, oldfill, &temp); /* default: nofill */
337
338 exi_leavedef(exoid, __func__);
339 EX_FUNC_LEAVE(EX_FATAL);
340 }