Debian Codegraph @

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

raw
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, ATT_PROP_NAME, etc
11 #include <stdbool.h>
12
13 /*!
14
15 The function ex_put_prop_array() stores an array of ({num_elem_blk},
16 num_node_sets, or num_side_sets) integer property values for all
17 element blocks, node sets, or side sets. The order of the values in
18 the array must correspond to the order in which the element blocks,
19 node sets, or side sets were introduced into the file. For instance,
20 if the parameters for element block with ID 20 were written to a file
21 (via ex_put_elem_block()), and then parameters for element block with
22 ID 10, followed by the parameters for element block with ID 30, the
23 first, second, and third elements in the property array would
24 correspond to element block 20, element block 10, and element block
25 30, respectively.
26
27 One should note that this same functionality (writing properties to
28 multiple objects) can be accomplished with multiple calls to
29 ex_put_prop().
30
31 Although it is not necessary to invoke ex_put_prop_names(), since
32 ex_put_prop_array() will allocate space within the data file if it
33 hasn't been previously allocated, it is more efficient to use
34 ex_put_prop_names() if there is more than one property to store.
35
36 \return In case of an error, ex_put_prop_array() returns a negative
37 number; a warning will return a positive number. Possible causes of
38 errors include:
39 - data file not properly opened with call to ex_create() or ex_open()
40 - data file opened for read only.
41 - data file not initialized properly with call to ex_put_init().
42 - invalid object type specified.
43
44 \param[in] exoid exodus file ID returned from a previous call to
45 ex_create() or ex_open().
46 \param[in] obj_type Type of object; use one of the options in the table below.
47 \param[in] prop_name The name of the property for which the values will be
48 stored. Maximum
49 length of this string is \p MAX_STR_LENGTH .
50 \param[in] values An array of property values.
51
52 | #ex_entity_type | description |
53 | -------------- | ------------------------- |
54 | #EX_NODE_SET | Node Set entity type |
55 | #EX_EDGE_BLOCK | Edge Block entity type |
56 | #EX_EDGE_SET | Edge Set entity type |
57 | #EX_FACE_BLOCK | Face Block entity type |
58 | #EX_FACE_SET | Face Set entity type |
59 | #EX_ELEM_BLOCK | Element Block entity type|
60 | #EX_ELEM_SET | Element Set entity type |
61 | #EX_SIDE_SET | Side Set entity type |
62 | #EX_ELEM_MAP | Element Map entity type |
63 | #EX_NODE_MAP | Node Map entity type |
64 | #EX_EDGE_MAP | Edge Map entity type |
65 | #EX_FACE_MAP | Face Map entity type |
66
67 For an example of code to write an array of object properties, refer
68 to the description for ex_put_prop_names().
69 */
70
71 int ex_put_prop_array(int exoid, ex_entity_type obj_type, const char *prop_name,
72 const void_int *values)
73 {
74 int oldfill = 0;
75 int temp;
76 int num_props, i, propid, dimid, dims[1], status;
77 bool found = false;
78 int int_type;
79 size_t num_obj;
80 char *name;
81 char tmpstr[MAX_STR_LENGTH + 1];
82
83 char errmsg[MAX_ERR_LENGTH];
84
85 EX_FUNC_ENTER();
86 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
87 EX_FUNC_LEAVE(EX_FATAL);
88 }
89
90 /* check if property has already been created */
91
92 num_props = ex_get_num_props(exoid, obj_type);
93
94 /* inquire id of previously defined dimension (number of objects) */
95 status = exi_get_dimension(exoid, exi_dim_num_objects(obj_type), ex_name_of_object(obj_type),
96 &num_obj, &dimid, __func__);
97 if (status != NC_NOERR) {
98 EX_FUNC_LEAVE(status);
99 }
100
101 for (i = 1; i <= num_props; i++) {
102 switch (obj_type) {
103 case EX_ELEM_BLOCK: name = VAR_EB_PROP(i); break;
104 case EX_FACE_BLOCK: name = VAR_FA_PROP(i); break;
105 case EX_EDGE_BLOCK: name = VAR_ED_PROP(i); break;
106 case EX_NODE_SET: name = VAR_NS_PROP(i); break;
107 case EX_EDGE_SET: name = VAR_ES_PROP(i); break;
108 case EX_FACE_SET: name = VAR_FS_PROP(i); break;
109 case EX_ELEM_SET: name = VAR_ELS_PROP(i); break;
110 case EX_SIDE_SET: name = VAR_SS_PROP(i); break;
111 case EX_ELEM_MAP: name = VAR_EM_PROP(i); break;
112 case EX_FACE_MAP: name = VAR_FAM_PROP(i); break;
113 case EX_EDGE_MAP: name = VAR_EDM_PROP(i); break;
114 case EX_NODE_MAP: name = VAR_NM_PROP(i); break;
115 default:
116 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type,
117 exoid);
118 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
119 EX_FUNC_LEAVE(EX_FATAL);
120 }
121
122 if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) {
123 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property array id in file id %d",
124 exoid);
125 ex_err_fn(exoid, __func__, errmsg, status);
126 EX_FUNC_LEAVE(EX_FATAL);
127 }
128
129 /* compare stored attribute name with passed property name */
130 memset(tmpstr, 0, MAX_STR_LENGTH + 1);
131 if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) {
132 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property name in file id %d", exoid);
133 ex_err_fn(exoid, __func__, errmsg, status);
134 EX_FUNC_LEAVE(EX_FATAL);
135 }
136
137 if (strcmp(tmpstr, prop_name) == 0) {
138 found = true;
139 break;
140 }
141 }
142
143 /* if property array has not been created, create it */
144 if (!found) {
145
146 /* put netcdf file into define mode */
147 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
148 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid);
149 ex_err_fn(exoid, __func__, errmsg, status);
150 EX_FUNC_LEAVE(EX_FATAL);
151 }
152
153 /* create a variable with a name xx_prop#, where # is the new number */
154 /* of properties */
155 switch (obj_type) {
156 case EX_ELEM_BLOCK: name = VAR_EB_PROP(num_props + 1); break;
157 case EX_FACE_BLOCK: name = VAR_FA_PROP(num_props + 1); break;
158 case EX_EDGE_BLOCK: name = VAR_ED_PROP(num_props + 1); break;
159 case EX_NODE_SET: name = VAR_NS_PROP(num_props + 1); break;
160 case EX_EDGE_SET: name = VAR_ES_PROP(num_props + 1); break;
161 case EX_FACE_SET: name = VAR_FS_PROP(num_props + 1); break;
162 case EX_ELEM_SET: name = VAR_ELS_PROP(num_props + 1); break;
163 case EX_SIDE_SET: name = VAR_SS_PROP(num_props + 1); break;
164 case EX_ELEM_MAP: name = VAR_EM_PROP(num_props + 1); break;
165 case EX_FACE_MAP: name = VAR_FAM_PROP(num_props + 1); break;
166 case EX_EDGE_MAP: name = VAR_EDM_PROP(num_props + 1); break;
167 case EX_NODE_MAP: name = VAR_NM_PROP(num_props + 1); break;
168 default:
169 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type,
170 exoid);
171 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
172 goto error_ret; /* Exit define mode and return */
173 }
174
175 dims[0] = dimid;
176 nc_set_fill(exoid, NC_FILL, &oldfill); /* fill with zeros per routine spec */
177
178 int_type = NC_INT;
179 if (ex_int64_status(exoid) & EX_IDS_INT64_DB) {
180 int_type = NC_INT64;
181 }
182
183 if ((status = nc_def_var(exoid, name, int_type, 1, dims, &propid)) != NC_NOERR) {
184 snprintf(errmsg, MAX_ERR_LENGTH,
185 "ERROR: failed to create property array variable in file id %d", exoid);
186 ex_err_fn(exoid, __func__, errmsg, status);
187 goto error_ret; /* Exit define mode and return */
188 }
189 nc_set_fill(exoid, oldfill, &temp); /* default: nofill */
190
191 /* store property name as attribute of property array variable */
192 if ((status = nc_put_att_text(exoid, propid, ATT_PROP_NAME, strlen(prop_name) + 1,
193 prop_name)) != NC_NOERR) {
194 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store property name %s in file id %d",
195 prop_name, exoid);
196 ex_err_fn(exoid, __func__, errmsg, status);
197 goto error_ret; /* Exit define mode and return */
198 }
199
200 /* leave define mode */
201
202 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
203 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode");
204 ex_err_fn(exoid, __func__, errmsg, status);
205 EX_FUNC_LEAVE(EX_FATAL);
206 }
207 }
208
209 /* put num_obj values in property array */
210 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
211 status = nc_put_var_longlong(exoid, propid, values);
212 }
213 else {
214 status = nc_put_var_int(exoid, propid, values);
215 }
216
217 if (status != NC_NOERR) {
218 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store property values in file id %d", exoid);
219 ex_err_fn(exoid, __func__, errmsg, status);
220 EX_FUNC_LEAVE(EX_FATAL);
221 }
222
223 EX_FUNC_LEAVE(EX_NOERR);
224
225 /* Fatal error: exit definition mode and return */
226 error_ret:
227 nc_set_fill(exoid, oldfill, &temp); /* default: nofill */
228 exi_leavedef(exoid, __func__);
229 EX_FUNC_LEAVE(EX_FATAL);
230 }