Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-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 /*****************************************************************************/
10 /*****************************************************************************/
11 /* Function(s) contained in this file:
12 * ex_put_cmap_params()
13 *****************************************************************************
14 * This function outputs the communication map parameters.
15 *****************************************************************************
16 * Variable Index:
17 * exoid - The NetCDF ID of an already open NemesisI file.
18 * node_cmap_ids - Pointer to vector of nodal communication
19 * set IDs.
20 * node_cmap_node_cnts - Pointer to a vector which contains a count of
21 * the number of FEM nodes for each nodal
22 * communication map.
23 * elem_cmap_ids - Pointer to vector for retrieval of elemental
24 * communication set IDs.
25 * elem_cmap_elem_cnts - Pointer to a vector which contains a count of
26 * the number of FEM elements for each elemental
27 * communication map.
28 * processor - The processor the file being read was written
29 * for.
30 */
31 /*****************************************************************************/
32 /*****************************************************************************/
33 /*****************************************************************************/
34
35 #include <exodusII.h> // for ex_err, etc
36 #include <exodusII_int.h> // for EX_FATAL, exi_leavedef, etc
37
38 int ex_put_cmap_params(int exoid, const void_int *node_cmap_ids,
39 const void_int *node_cmap_node_cnts, const void_int *elem_cmap_ids,
40 const void_int *elem_cmap_elem_cnts, int64_t processor)
41 {
42 int dimid[1];
43 size_t start[1];
44 char ftype[2];
45
46 char errmsg[MAX_ERR_LENGTH];
47
48 EX_UNUSED(processor);
49
50 EX_FUNC_ENTER();
51 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
52 EX_FUNC_LEAVE(EX_FATAL);
53 }
54
55 int format;
56 nc_inq_format(exoid, &format);
57
58 int index_type = NC_INT;
59 if ((ex_int64_status(exoid) & EX_BULK_INT64_DB) || (format == NC_FORMAT_NETCDF4)) {
60 index_type = NC_INT64;
61 }
62
63 int id_type = NC_INT;
64 if (ex_int64_status(exoid) & EX_IDS_INT64_DB) {
65 id_type = NC_INT64;
66 }
67 /*-----------------------------Execution begins-----------------------------*/
68
69 /*
70 ** with the new database format, this function should only
71 ** be used for writing a parallel file
72 */
73 /* Get the file type */
74 if (exi_get_file_type(exoid, ftype) != EX_NOERR) {
75 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get file type from file ID %d\n", exoid);
76 ex_err_fn(exoid, __func__, errmsg, EX_MSG);
77 EX_FUNC_LEAVE(EX_FATAL);
78 }
79
80 /* make sure that this is a parallel file */
81 if (ftype[0] != 'p') {
82 snprintf(errmsg, MAX_ERR_LENGTH,
83 "ERROR: function for use with parallel files only, file ID %d\n", exoid);
84 ex_err_fn(exoid, __func__, errmsg, EX_MSG);
85 EX_FUNC_LEAVE(EX_FATAL);
86 }
87
88 /* Put NetCDF file into define mode */
89 int status;
90 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
91 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to file ID %d into define mode", exoid);
92 ex_err_fn(exoid, __func__, errmsg, status);
93 EX_FUNC_LEAVE(EX_FATAL);
94 }
95
96 /* Check to see if there are nodal communications maps in the file */
97 size_t num_n_comm_maps;
98 if (nc_inq_dimid(exoid, DIM_NUM_N_CMAPS, &dimid[0]) != NC_NOERR) {
99 num_n_comm_maps = 0;
100 }
101 else {
102 if ((status = nc_inq_dimlen(exoid, dimid[0], &num_n_comm_maps)) != NC_NOERR) {
103 snprintf(errmsg, MAX_ERR_LENGTH,
104 "ERROR: failed to find length of dimension \"%s\" in file ID %d", DIM_NUM_N_CMAPS,
105 exoid);
106 ex_err_fn(exoid, __func__, errmsg, status);
107 EX_FUNC_LEAVE(EX_FATAL);
108 }
109 }
110
111 /*
112 * Add dimensions for the size of the number of nodal
113 * communication maps.
114 */
115 int n_varid_idx;
116 if (num_n_comm_maps > 0) {
117
118 /* add the communications data index variable */
119 if ((status = nc_def_var(exoid, VAR_N_COMM_DATA_IDX, index_type, 1, dimid, &n_varid_idx)) !=
120 NC_NOERR) {
121 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
122 VAR_N_COMM_DATA_IDX, exoid);
123 ex_err_fn(exoid, __func__, errmsg, status);
124 /* Leave define mode before returning */
125 exi_leavedef(exoid, __func__);
126
127 EX_FUNC_LEAVE(EX_FATAL);
128 }
129
130 /* Add dimensions for all of the nodal communication maps */
131 int ncnt_cmap = 0;
132 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
133 for (size_t icm = 0; icm < num_n_comm_maps; icm++) {
134 ncnt_cmap += ((int64_t *)node_cmap_node_cnts)[icm];
135 }
136 }
137 else {
138 for (size_t icm = 0; icm < num_n_comm_maps; icm++) {
139 ncnt_cmap += ((int *)node_cmap_node_cnts)[icm];
140 }
141 }
142
143 if ((status = nc_def_dim(exoid, DIM_NCNT_CMAP, ncnt_cmap, &dimid[0])) != NC_NOERR) {
144 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add dimension for \"%s\" in file ID %d",
145 DIM_NCNT_CMAP, exoid);
146 ex_err_fn(exoid, __func__, errmsg, status);
147 /* Leave define mode before returning */
148 exi_leavedef(exoid, __func__);
149
150 EX_FUNC_LEAVE(EX_FATAL);
151 }
152
153 /* Define variables for the nodal IDS and processor vectors */
154 int varid;
155 if ((status = nc_def_var(exoid, VAR_N_COMM_NIDS, id_type, 1, dimid, &varid)) != NC_NOERR) {
156 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
157 VAR_N_COMM_NIDS, exoid);
158 ex_err_fn(exoid, __func__, errmsg, status);
159 /* Leave define mode before returning */
160 exi_leavedef(exoid, __func__);
161
162 EX_FUNC_LEAVE(EX_FATAL);
163 }
164 exi_compress_variable(exoid, varid, 1);
165
166 if ((status = nc_def_var(exoid, VAR_N_COMM_PROC, NC_INT, 1, dimid, &varid)) != NC_NOERR) {
167 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
168 VAR_N_COMM_PROC, exoid);
169 ex_err_fn(exoid, __func__, errmsg, status);
170 /* Leave define mode before returning */
171 exi_leavedef(exoid, __func__);
172
173 EX_FUNC_LEAVE(EX_FATAL);
174 }
175 exi_compress_variable(exoid, varid, 1);
176
177 } /* End "if (num_n_comm_maps > 0)" */
178
179 /* Check to see if there are elemental communications maps in the file */
180 size_t num_e_comm_maps;
181 if (nc_inq_dimid(exoid, DIM_NUM_E_CMAPS, &dimid[0]) != NC_NOERR) {
182 num_e_comm_maps = 0;
183 }
184 else {
185 if ((status = nc_inq_dimlen(exoid, dimid[0], &num_e_comm_maps)) != NC_NOERR) {
186 snprintf(errmsg, MAX_ERR_LENGTH,
187 "ERROR: failed to find length of dimension \"%s\" in file ID %d", DIM_NUM_E_CMAPS,
188 exoid);
189 ex_err_fn(exoid, __func__, errmsg, status);
190 EX_FUNC_LEAVE(EX_FATAL);
191 }
192 }
193
194 /*
195 * Add dimensions for the size of the number of elemental
196 * communication maps.
197 */
198 int e_varid_idx;
199 if (num_e_comm_maps > 0) {
200
201 /* add the communications data index variable */
202 if ((status = nc_def_var(exoid, VAR_E_COMM_DATA_IDX, index_type, 1, dimid, &e_varid_idx)) !=
203 NC_NOERR) {
204 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
205 VAR_E_COMM_DATA_IDX, exoid);
206 ex_err_fn(exoid, __func__, errmsg, status);
207 /* Leave define mode before returning */
208 exi_leavedef(exoid, __func__);
209
210 EX_FUNC_LEAVE(EX_FATAL);
211 }
212
213 /* Add dimensions for each of the nodal communication maps */
214 int ecnt_cmap = 0;
215 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
216 for (size_t icm = 0; icm < num_e_comm_maps; icm++) {
217 ecnt_cmap += ((int64_t *)elem_cmap_elem_cnts)[icm];
218 }
219 }
220 else {
221 for (size_t icm = 0; icm < num_e_comm_maps; icm++) {
222 ecnt_cmap += ((int *)elem_cmap_elem_cnts)[icm];
223 }
224 }
225 if ((status = nc_def_dim(exoid, DIM_ECNT_CMAP, ecnt_cmap, &dimid[0])) != NC_NOERR) {
226 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add dimension for \"%s\" in file ID %d",
227 DIM_ECNT_CMAP, exoid);
228 ex_err_fn(exoid, __func__, errmsg, status);
229 /* Leave define mode before returning */
230 exi_leavedef(exoid, __func__);
231
232 EX_FUNC_LEAVE(EX_FATAL);
233 }
234
235 /* Define variables for the element IDS and processor vectors */
236 int varid;
237 if ((status = nc_def_var(exoid, VAR_E_COMM_EIDS, id_type, 1, dimid, &varid)) != NC_NOERR) {
238 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
239 VAR_E_COMM_EIDS, exoid);
240 ex_err_fn(exoid, __func__, errmsg, status);
241 /* Leave define mode before returning */
242 exi_leavedef(exoid, __func__);
243
244 EX_FUNC_LEAVE(EX_FATAL);
245 }
246 exi_compress_variable(exoid, varid, 1);
247
248 if ((status = nc_def_var(exoid, VAR_E_COMM_PROC, NC_INT, 1, dimid, &varid)) != NC_NOERR) {
249 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
250 VAR_E_COMM_PROC, exoid);
251 ex_err_fn(exoid, __func__, errmsg, status);
252 /* Leave define mode before returning */
253 exi_leavedef(exoid, __func__);
254
255 EX_FUNC_LEAVE(EX_FATAL);
256 }
257 exi_compress_variable(exoid, varid, 1);
258
259 if ((status = nc_def_var(exoid, VAR_E_COMM_SIDS, NC_INT, 1, dimid, &varid)) != NC_NOERR) {
260 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to add variable \"%s\" in file ID %d",
261 VAR_E_COMM_SIDS, exoid);
262 ex_err_fn(exoid, __func__, errmsg, status);
263 /* Leave define mode before returning */
264 exi_leavedef(exoid, __func__);
265
266 EX_FUNC_LEAVE(EX_FATAL);
267 }
268 exi_compress_variable(exoid, varid, 1);
269
270 } /* End "if (num_e_comm_maps > 0)" */
271
272 /* Exit define mode */
273 exi_leavedef(exoid, __func__);
274
275 /* Set the status of the nodal communication maps */
276 if (num_n_comm_maps > 0) {
277
278 int n_varid;
279 if ((status = nc_inq_varid(exoid, VAR_N_COMM_STAT, &n_varid)) != NC_NOERR) {
280 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable ID for \"%s\" in file ID %d",
281 VAR_N_COMM_STAT, exoid);
282 ex_err_fn(exoid, __func__, errmsg, status);
283 EX_FUNC_LEAVE(EX_FATAL);
284 }
285
286 int64_t nl_ncnt_cmap = 0; /* reset this for index */
287 for (size_t icm = 0; icm < num_n_comm_maps; icm++) {
288
289 size_t ncnc;
290 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
291 ncnc = ((int64_t *)node_cmap_node_cnts)[icm];
292 }
293 else {
294 ncnc = ((int *)node_cmap_node_cnts)[icm];
295 }
296
297 int nmstat = ncnc > 0 ? 1 : 0;
298 start[0] = icm;
299
300 if ((status = nc_put_var1_int(exoid, n_varid, start, &nmstat)) != NC_NOERR) {
301 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unable to output variable in file ID %d", exoid);
302 ex_err_fn(exoid, __func__, errmsg, status);
303 EX_FUNC_LEAVE(EX_FATAL);
304 }
305
306 /* increment to the next starting position */
307 nl_ncnt_cmap += ncnc;
308
309 /* fill the cmap data index */
310 if ((status = nc_put_var1_longlong(exoid, n_varid_idx, start, (long long *)&nl_ncnt_cmap)) !=
311 NC_NOERR) {
312 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output int elem map index in file ID %d",
313 exoid);
314 ex_err_fn(exoid, __func__, errmsg, status);
315 EX_FUNC_LEAVE(EX_FATAL);
316 }
317 } /* End "for(icm=0; icm < num_n_comm_maps; icm++)" */
318
319 /* Get the variable ID for the comm map IDs vector */
320 if ((status = nc_inq_varid(exoid, VAR_N_COMM_IDS, &n_varid)) != NC_NOERR) {
321 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable ID for \"%s\" in file ID %d",
322 VAR_N_COMM_IDS, exoid);
323 ex_err_fn(exoid, __func__, errmsg, status);
324 EX_FUNC_LEAVE(EX_FATAL);
325 }
326
327 /* Output the nodal comm map IDs */
328 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
329 status = nc_put_var_longlong(exoid, n_varid, node_cmap_ids);
330 }
331 else {
332 status = nc_put_var_int(exoid, n_varid, node_cmap_ids);
333 }
334 if (status != NC_NOERR) {
335 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output variable in file ID %d", exoid);
336 ex_err_fn(exoid, __func__, errmsg, status);
337 EX_FUNC_LEAVE(EX_FATAL);
338 }
339
340 } /* End "if (num_n_comm_maps > 0)" */
341
342 /* Set the status of the elemental communication maps */
343 if (num_e_comm_maps > 0) {
344
345 /* Get variable ID for elemental status vector */
346 int e_varid;
347 if ((status = nc_inq_varid(exoid, VAR_E_COMM_STAT, &e_varid)) != NC_NOERR) {
348 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable ID for \"%s\" in file ID %d",
349 VAR_E_COMM_STAT, exoid);
350 ex_err_fn(exoid, __func__, errmsg, status);
351 EX_FUNC_LEAVE(EX_FATAL);
352 }
353
354 int64_t nl_ecnt_cmap = 0; /* reset this for index */
355 for (size_t icm = 0; icm < num_e_comm_maps; icm++) {
356 size_t ecec;
357 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
358 ecec = ((int64_t *)elem_cmap_elem_cnts)[icm];
359 }
360 else {
361 ecec = ((int *)elem_cmap_elem_cnts)[icm];
362 }
363 start[0] = icm;
364 int nmstat = ecec > 0 ? 1 : 0;
365
366 if ((status = nc_put_var1_int(exoid, e_varid, start, &nmstat)) != NC_NOERR) {
367 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unable to output variable in file ID %d", exoid);
368 ex_err_fn(exoid, __func__, errmsg, status);
369 EX_FUNC_LEAVE(EX_FATAL);
370 }
371
372 /* increment to the next starting position */
373 nl_ecnt_cmap += ecec;
374
375 /* fill the cmap data index */
376 if ((status = nc_put_var1_longlong(exoid, e_varid_idx, start, (long long *)&nl_ecnt_cmap)) !=
377 NC_NOERR) {
378 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output int elem map index in file ID %d",
379 exoid);
380 ex_err_fn(exoid, __func__, errmsg, status);
381 EX_FUNC_LEAVE(EX_FATAL);
382 }
383 } /* End "for(icm=0; icm < num_e_comm_maps; icm++)" */
384
385 /* Get the variable ID for the elemental comm map IDs vector */
386 if ((status = nc_inq_varid(exoid, VAR_E_COMM_IDS, &e_varid)) != NC_NOERR) {
387 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find variable ID for \"%s\" in file ID %d",
388 VAR_E_COMM_IDS, exoid);
389 ex_err_fn(exoid, __func__, errmsg, status);
390 EX_FUNC_LEAVE(EX_FATAL);
391 }
392
393 /* Output the elemental comm map IDs */
394 if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
395 status = nc_put_var_longlong(exoid, e_varid, elem_cmap_ids);
396 }
397 else {
398 status = nc_put_var_int(exoid, e_varid, elem_cmap_ids);
399 }
400 if (status != NC_NOERR) {
401 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to output variable in file ID %d", exoid);
402 ex_err_fn(exoid, __func__, errmsg, status);
403
404 EX_FUNC_LEAVE(EX_FATAL);
405 }
406
407 } /* End "if (num_e_comm_maps > 0)" */
408
409 EX_FUNC_LEAVE(EX_NOERR);
410 }