Debian Codegraph @

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

raw
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
12 /*!
13 * Internal function used to put a homogeneous `blocks` array that
14 * contains all blocks of that specified type that will be defined.
15 * Permits some optimizations and safer for N->1 parallel.
16 * Arbitrary polyhedra are handled in more general routine; not here.
17 */
18 int exi_put_homogenous_block_params(int exoid, size_t block_count, const struct ex_block *blocks)
19 {
20
21 int status;
22 int varid, dims[2];
23 char errmsg[MAX_ERR_LENGTH];
24
25 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
26 EX_FUNC_LEAVE(EX_FATAL);
27 }
28
29 const char *vblkids = NULL;
30 const char *vblksta = NULL;
31
32 switch (blocks[0].type) {
33 case EX_EDGE_BLOCK:
34 vblkids = VAR_ID_ED_BLK;
35 vblksta = VAR_STAT_ED_BLK;
36 break;
37 case EX_FACE_BLOCK:
38 vblkids = VAR_ID_FA_BLK;
39 vblksta = VAR_STAT_FA_BLK;
40 break;
41 case EX_ELEM_BLOCK:
42 vblkids = VAR_ID_EL_BLK;
43 vblksta = VAR_STAT_EL_BLK;
44 break;
45 default:
46 snprintf(errmsg, MAX_ERR_LENGTH,
47 "ERROR: Bad block type (%d) specified for all blocks file id %d", blocks[0].type,
48 exoid);
49 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
50 return EX_FATAL;
51 }
52
53 { /* Output ids for this block */
54 long long *ids = NULL;
55 if (!(ids = malloc(block_count * sizeof(long long)))) {
56 snprintf(errmsg, MAX_ERR_LENGTH,
57 "ERROR: failed to allocate memory for block ids "
58 "array in file id %d",
59 exoid);
60 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
61 return EX_FATAL;
62 }
63
64 for (size_t i = 0; i < block_count; i++) {
65 ids[i] = (long long)blocks[i].id;
66 }
67 /* write out block id to previously defined id array variable*/
68 if ((status = nc_inq_varid(exoid, vblkids, &varid)) != NC_NOERR) {
69 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s ids in file id %d",
70 ex_name_of_object(blocks[0].type), exoid);
71 ex_err_fn(exoid, __func__, errmsg, status);
72 free(ids);
73 return EX_FATAL;
74 }
75
76 if ((status = nc_put_var_longlong(exoid, varid, ids)) != NC_NOERR) {
77 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s ids to file id %d",
78 ex_name_of_object(blocks[0].type), exoid);
79 ex_err_fn(exoid, __func__, errmsg, status);
80 free(ids);
81 return EX_FATAL;
82 }
83 free(ids);
84 }
85
86 { /* Output the block status array */
87 int *stat = NULL;
88 if (!(stat = malloc(block_count * sizeof(int)))) {
89 snprintf(errmsg, MAX_ERR_LENGTH,
90 "ERROR: failed to allocate memory for status array "
91 "array in file id %d",
92 exoid);
93 ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL);
94 return EX_FATAL;
95 }
96
97 for (size_t i = 0; i < block_count; i++) {
98 stat[i] = (blocks[i].num_entry == 0) ? 0 : 1;
99 }
100
101 if ((status = nc_inq_varid(exoid, vblksta, &varid)) != NC_NOERR) {
102 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s status in file id %d",
103 ex_name_of_object(blocks[0].type), exoid);
104 ex_err_fn(exoid, __func__, errmsg, status);
105 free(stat);
106 return EX_FATAL;
107 }
108
109 if ((status = nc_put_var_int(exoid, varid, stat)) != NC_NOERR) {
110 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s status to file id %d",
111 ex_name_of_object(blocks[0].type), exoid);
112 ex_err_fn(exoid, __func__, errmsg, status);
113 free(stat);
114 return EX_FATAL;
115 }
116 free(stat);
117 }
118
119 /* ======================================================================== */
120 /* put netcdf file into define mode */
121 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
122 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid);
123 ex_err_fn(exoid, __func__, errmsg, status);
124 return EX_FATAL;
125 }
126
127 /* inquire previously defined dimensions */
128 int strdim = 0;
129 if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
130 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid);
131 ex_err_fn(exoid, __func__, errmsg, status);
132 goto error_ret;
133 }
134
135 for (size_t i = 0; i < block_count; i++) {
136 int blk_id_ndx = 1 + exi_inc_file_item(exoid, exi_get_counter_list(blocks[i].type));
137
138 if (blocks[i].num_entry == 0) { /* Is this a NULL element block? */
139 continue;
140 }
141
142 const char *vnodcon = NULL;
143 const char *vedgcon = NULL;
144 const char *vfaccon = NULL;
145 const char *vattnam = NULL;
146 const char *vblkatt = NULL;
147 const char *dneblk = NULL;
148 const char *dnape = NULL;
149 const char *dnnpe = NULL;
150 const char *dnepe = NULL;
151 const char *dnfpe = NULL;
152
153 switch (blocks[i].type) {
154 case EX_EDGE_BLOCK:
155 dneblk = DIM_NUM_ED_IN_EBLK(blk_id_ndx);
156 dnnpe = DIM_NUM_NOD_PER_ED(blk_id_ndx);
157 dnepe = NULL;
158 dnfpe = NULL;
159 dnape = DIM_NUM_ATT_IN_EBLK(blk_id_ndx);
160 vblkatt = VAR_EATTRIB(blk_id_ndx);
161 vattnam = VAR_NAME_EATTRIB(blk_id_ndx);
162 vnodcon = VAR_EBCONN(blk_id_ndx);
163 vedgcon = NULL;
164 vfaccon = NULL;
165 break;
166 case EX_FACE_BLOCK:
167 dneblk = DIM_NUM_FA_IN_FBLK(blk_id_ndx);
168 dnnpe = DIM_NUM_NOD_PER_FA(blk_id_ndx);
169 dnepe = NULL;
170 dnfpe = NULL;
171 dnape = DIM_NUM_ATT_IN_FBLK(blk_id_ndx);
172 vblkatt = VAR_FATTRIB(blk_id_ndx);
173 vattnam = VAR_NAME_FATTRIB(blk_id_ndx);
174 vnodcon = VAR_FBCONN(blk_id_ndx);
175 vedgcon = NULL;
176 vfaccon = NULL;
177 break;
178 case EX_ELEM_BLOCK:
179 dneblk = DIM_NUM_EL_IN_BLK(blk_id_ndx);
180 dnnpe = DIM_NUM_NOD_PER_EL(blk_id_ndx);
181 dnepe = DIM_NUM_EDG_PER_EL(blk_id_ndx);
182 dnfpe = DIM_NUM_FAC_PER_EL(blk_id_ndx);
183 dnape = DIM_NUM_ATT_IN_BLK(blk_id_ndx);
184 vblkatt = VAR_ATTRIB(blk_id_ndx);
185 vattnam = VAR_NAME_ATTRIB(blk_id_ndx);
186 vnodcon = VAR_CONN(blk_id_ndx);
187 vedgcon = VAR_ECONN(blk_id_ndx);
188 vfaccon = VAR_FCONN(blk_id_ndx);
189 break;
190 default: goto error_ret;
191 }
192
193 /* define some dimensions and variables*/
194 int numblkdim = 0;
195 if ((status = nc_def_dim(exoid, dneblk, blocks[i].num_entry, &numblkdim)) != NC_NOERR) {
196 if (status == NC_ENAMEINUSE) { /* duplicate entry */
197 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s %" PRId64 " already defined in file id %d",
198 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
199 ex_err_fn(exoid, __func__, errmsg, status);
200 }
201 else {
202 snprintf(errmsg, MAX_ERR_LENGTH,
203 "ERROR: failed to define number of entities/block for %s %" PRId64 " file id %d",
204 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
205 ex_err_fn(exoid, __func__, errmsg, status);
206 }
207 goto error_ret; /* exit define mode and return */
208 }
209
210 int nnodperentdim = -1;
211 if (dnnpe && blocks[i].num_nodes_per_entry > 0) {
212 /* A nfaced block would not have any nodes defined... */
213 if ((status = nc_def_dim(exoid, dnnpe, blocks[i].num_nodes_per_entry, &nnodperentdim)) !=
214 NC_NOERR) {
215 snprintf(errmsg, MAX_ERR_LENGTH,
216 "ERROR: failed to define number of nodes/entity for %s %" PRId64 " in file id %d",
217 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
218 ex_err_fn(exoid, __func__, errmsg, status);
219 goto error_ret; /* exit define mode and return */
220 }
221 }
222
223 int nedgperentdim = -1;
224 if (dnepe && blocks[i].num_edges_per_entry > 0) {
225 if ((status = nc_def_dim(exoid, dnepe, blocks[i].num_edges_per_entry, &nedgperentdim)) !=
226 NC_NOERR) {
227 snprintf(errmsg, MAX_ERR_LENGTH,
228 "ERROR: failed to define number of edges/entity for %s %" PRId64 " in file id %d",
229 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
230 ex_err_fn(exoid, __func__, errmsg, status);
231 goto error_ret; /* exit define mode and return */
232 }
233 }
234
235 int nfacperentdim = -1;
236 if (dnfpe && blocks[i].num_faces_per_entry > 0) {
237 if ((status = nc_def_dim(exoid, dnfpe, blocks[i].num_faces_per_entry, &nfacperentdim)) !=
238 NC_NOERR) {
239 snprintf(errmsg, MAX_ERR_LENGTH,
240 "ERROR: failed to define number of faces/entity for %s %" PRId64 " in file id %d",
241 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
242 ex_err_fn(exoid, __func__, errmsg, status);
243 goto error_ret; /* exit define mode and return */
244 }
245 }
246
247 /* element attribute array */
248 if (blocks[i].num_attribute > 0) {
249
250 int numattrdim = 0;
251 if ((status = nc_def_dim(exoid, dnape, blocks[i].num_attribute, &numattrdim)) != NC_NOERR) {
252 snprintf(errmsg, MAX_ERR_LENGTH,
253 "ERROR: failed to define number of attributes in %s %" PRId64 " in file id %d",
254 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
255 ex_err_fn(exoid, __func__, errmsg, status);
256 goto error_ret; /* exit define mode and return */
257 }
258
259 dims[0] = numblkdim;
260 dims[1] = numattrdim;
261
262 if ((status = nc_def_var(exoid, vblkatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
263 snprintf(errmsg, MAX_ERR_LENGTH,
264 "ERROR: failed to define attributes for %s %" PRId64 " in file id %d",
265 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
266 ex_err_fn(exoid, __func__, errmsg, status);
267 goto error_ret; /* exit define mode and return */
268 }
269 exi_compress_variable(exoid, varid, 2);
270
271 /* Attribute names... */
272 dims[0] = numattrdim;
273 dims[1] = strdim;
274
275 int att_name_varid = -1;
276 if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &att_name_varid)) != NC_NOERR) {
277 snprintf(errmsg, MAX_ERR_LENGTH,
278 "ERROR: failed to define %s attribute name array in file id %d",
279 ex_name_of_object(blocks[i].type), exoid);
280 ex_err_fn(exoid, __func__, errmsg, status);
281 goto error_ret; /* exit define mode and return */
282 }
283 #if defined(EX_CAN_USE_NC_DEF_VAR_FILL)
284 int fill = NC_FILL_CHAR;
285 nc_def_var_fill(exoid, att_name_varid, 0, &fill);
286 #endif
287 }
288
289 int conn_int_type = NC_INT;
290 if (ex_int64_status(exoid) & EX_BULK_INT64_DB) {
291 conn_int_type = NC_INT64;
292 }
293
294 /* element connectivity array */
295 if (blocks[i].num_nodes_per_entry > 0) {
296 /* "Normal" (non-polyhedra) element block type */
297 dims[0] = numblkdim;
298 dims[1] = nnodperentdim;
299
300 int connid = 0;
301 if ((status = nc_def_var(exoid, vnodcon, conn_int_type, 2, dims, &connid)) != NC_NOERR) {
302 snprintf(errmsg, MAX_ERR_LENGTH,
303 "ERROR: failed to create connectivity array for %s %" PRId64 " in file id %d",
304 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
305 ex_err_fn(exoid, __func__, errmsg, status);
306 goto error_ret; /* exit define mode and return */
307 }
308 exi_compress_variable(exoid, connid, 1);
309
310 /* store element type as attribute of connectivity variable */
311 if ((status = nc_put_att_text(exoid, connid, ATT_NAME_ELB, strlen(blocks[i].topology) + 1,
312 blocks[i].topology)) != NC_NOERR) {
313 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s type name %s in file id %d",
314 ex_name_of_object(blocks[i].type), blocks[i].topology, exoid);
315 ex_err_fn(exoid, __func__, errmsg, status);
316 goto error_ret; /* exit define mode and return */
317 }
318 }
319
320 if (vedgcon && blocks[i].num_edges_per_entry) {
321 dims[0] = numblkdim;
322 dims[1] = nedgperentdim;
323
324 if ((status = nc_def_var(exoid, vedgcon, conn_int_type, 2, dims, &varid)) != NC_NOERR) {
325 snprintf(errmsg, MAX_ERR_LENGTH,
326 "ERROR: failed to create edge connectivity array for %s %" PRId64 " in file id %d",
327 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
328 ex_err_fn(exoid, __func__, errmsg, status);
329 goto error_ret; /* exit define mode and return */
330 }
331 }
332
333 if (vfaccon && blocks[i].num_faces_per_entry) {
334 dims[0] = numblkdim;
335 dims[1] = nfacperentdim;
336
337 if ((status = nc_def_var(exoid, vfaccon, conn_int_type, 2, dims, &varid)) != NC_NOERR) {
338 snprintf(errmsg, MAX_ERR_LENGTH,
339 "ERROR: failed to create face connectivity array for %s %" PRId64 " in file id %d",
340 ex_name_of_object(blocks[i].type), blocks[i].id, exoid);
341 ex_err_fn(exoid, __func__, errmsg, status);
342 goto error_ret; /* exit define mode and return */
343 }
344 }
345 }
346
347 /* leave define mode */
348 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
349 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid);
350 ex_err_fn(exoid, __func__, errmsg, status);
351 return EX_FATAL;
352 }
353
354 /* ======================================================================== */
355 for (size_t i = 0; i < block_count; i++) {
356 switch (blocks[i].type) {
357 case EX_EDGE_BLOCK: vblkids = VAR_ID_ED_BLK; break;
358 case EX_FACE_BLOCK: vblkids = VAR_ID_FA_BLK; break;
359 case EX_ELEM_BLOCK: vblkids = VAR_ID_EL_BLK; break;
360 default: return EX_FATAL; /* should have been handled earlier; quiet compiler here */
361 }
362
363 int att_name_varid = -1;
364 nc_inq_varid(exoid, vblkids, &att_name_varid);
365
366 if (blocks[i].num_attribute > 0 && att_name_varid >= 0) {
367 /* Output a dummy empty attribute name in case client code doesn't
368 write anything; avoids corruption in some cases.
369 */
370 size_t count[2];
371 size_t start[2];
372 char *text = "";
373
374 count[0] = 1;
375 start[1] = 0;
376 count[1] = strlen(text) + 1;
377
378 for (int64_t j = 0; j < blocks[i].num_attribute; j++) {
379 start[0] = j;
380 nc_put_vara_text(exoid, att_name_varid, start, count, text);
381 }
382 }
383 }
384
385 return EX_NOERR;
386
387 /* Fatal error: exit definition mode and return */
388 error_ret:
389 exi_leavedef(exoid, __func__);
390 return EX_FATAL;
391 }