Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021, 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_err, etc
10 #include "exodusII_int.h" // for EX_FATAL, exi_id_lkup, etc
11
12 /*! write out the connectivity array */
13 int ex_int_write_conn(int exoid, ex_entity_id blk_id, const char *type, int var_id,
14 const void_int *var_conn)
15 {
16 int status = 0;
17 if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
18 status = nc_put_var_longlong(exoid, var_id, var_conn);
19 }
20 else {
21 status = nc_put_var_int(exoid, var_id, var_conn);
22 }
23 if (status != NC_NOERR) {
24 char errmsg[MAX_ERR_LENGTH];
25 snprintf(errmsg, MAX_ERR_LENGTH,
26 "ERROR: failed to write connectivity array for %s block %" PRId64 " in file id %d",
27 type, blk_id, exoid);
28 ex_err_fn(exoid, __func__, errmsg, status);
29 return status;
30 }
31 return status;
32 }
33
34 /*!
35 * writes the connectivity array for a block
36 * \param exoid exodus file id
37 * \param blk_type type of block
38 * \param blk_id id of block
39 * \param node_conn node-element connectivity
40 * \param elem_edge_conn element-edge connectivity (NULL if none)
41 * \param elem_face_conn element-face connectivity (NULL if none)
42 */
43
44 int ex_put_conn(int exoid, ex_entity_type blk_type, ex_entity_id blk_id, const void_int *node_conn,
45 const void_int *elem_edge_conn, const void_int *elem_face_conn)
46 {
47 int connid = -1;
48 int status;
49 char errmsg[MAX_ERR_LENGTH];
50
51 EX_FUNC_ENTER();
52 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
53 EX_FUNC_LEAVE(EX_FATAL);
54 }
55
56 int blk_id_ndx = exi_id_lkup(exoid, blk_type, blk_id);
57 if (blk_id_ndx <= 0) {
58 ex_get_err(NULL, NULL, &status);
59
60 if (status != 0) {
61 if (status == EX_NULLENTITY) {
62 snprintf(errmsg, MAX_ERR_LENGTH,
63 "Warning: connectivity array not allowed for NULL %s %" PRId64 " in file id %d",
64 ex_name_of_object(blk_type), blk_id, exoid);
65 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
66 EX_FUNC_LEAVE(EX_WARN);
67 }
68 snprintf(errmsg, MAX_ERR_LENGTH,
69 "ERROR: failed to locate %s id %" PRId64 " in id array in file id %d",
70 ex_name_of_object(blk_type), blk_id, exoid);
71 ex_err_fn(exoid, __func__, errmsg, status);
72 EX_FUNC_LEAVE(EX_FATAL);
73 }
74 }
75
76 /* inquire id's of previously defined dimensions */
77 if (node_conn) {
78 switch (blk_type) {
79 case EX_ELEM_BLOCK: status = nc_inq_varid(exoid, VAR_CONN(blk_id_ndx), &connid); break;
80 case EX_FACE_BLOCK: status = nc_inq_varid(exoid, VAR_FBCONN(blk_id_ndx), &connid); break;
81 case EX_EDGE_BLOCK: status = nc_inq_varid(exoid, VAR_EBCONN(blk_id_ndx), &connid); break;
82 default:
83 snprintf(errmsg, MAX_ERR_LENGTH,
84 "Internal ERROR: unrecognized block type in switch: %d in file id %d", blk_type,
85 exoid);
86 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
87 EX_FUNC_LEAVE(EX_FATAL);
88 }
89 if (status != NC_NOERR) {
90 snprintf(errmsg, MAX_ERR_LENGTH,
91 "ERROR: failed to locate connectivity array for %s %" PRId64 " in file id %d",
92 ex_name_of_object(blk_type), blk_id, exoid);
93 ex_err_fn(exoid, __func__, errmsg, status);
94 EX_FUNC_LEAVE(EX_FATAL);
95 }
96
97 status = ex_int_write_conn(exoid, blk_id, ex_name_of_object(blk_type), connid, node_conn);
98 if (status != NC_NOERR) {
99 EX_FUNC_LEAVE(EX_FATAL);
100 }
101 }
102
103 /* If there are edge and face connectivity arrays that belong with the element
104 * block, write them now. Warn if they are required but not specified or
105 * specified but not required.
106 */
107 if (blk_type == EX_ELEM_BLOCK) {
108 int nedpereldim, nfapereldim;
109 size_t num_ed_per_elem, num_fa_per_elem;
110
111 if (elem_edge_conn != NULL) {
112 status = nc_inq_dimid(exoid, DIM_NUM_EDG_PER_EL(blk_id_ndx), &nedpereldim);
113 if (status != NC_NOERR) {
114 snprintf(errmsg, MAX_ERR_LENGTH,
115 "ERROR: edge connectivity specified but failed to "
116 "locate number of edges/element in block %" PRId64 " in file id %d",
117 blk_id, exoid);
118 ex_err_fn(exoid, __func__, errmsg, status);
119 EX_FUNC_LEAVE(EX_FATAL);
120 }
121 }
122
123 if (elem_face_conn != NULL) {
124 status = nc_inq_dimid(exoid, DIM_NUM_FAC_PER_EL(blk_id_ndx), &nfapereldim);
125 if (status != NC_NOERR) {
126 snprintf(errmsg, MAX_ERR_LENGTH,
127 "ERROR: face connectivity specified but failed to "
128 "locate number of faces/element in block %" PRId64 " in file id %d",
129 blk_id, exoid);
130 ex_err_fn(exoid, __func__, errmsg, status);
131 EX_FUNC_LEAVE(EX_FATAL);
132 }
133 }
134
135 num_ed_per_elem = 0;
136 if ((elem_edge_conn != NULL) &&
137 ((status = nc_inq_dimlen(exoid, nedpereldim, &num_ed_per_elem)) != NC_NOERR)) {
138 snprintf(errmsg, MAX_ERR_LENGTH,
139 "ERROR: failed to get number of edges/elem in block %" PRId64 " in file id %d",
140 blk_id, exoid);
141 ex_err_fn(exoid, __func__, errmsg, status);
142 EX_FUNC_LEAVE(EX_FATAL);
143 }
144
145 num_fa_per_elem = 0;
146 if ((elem_face_conn != NULL) &&
147 ((status = nc_inq_dimlen(exoid, nfapereldim, &num_fa_per_elem)) != NC_NOERR)) {
148 snprintf(errmsg, MAX_ERR_LENGTH,
149 "ERROR: failed to get number of faces/elem in block %" PRId64 " in file id %d",
150 blk_id, exoid);
151 ex_err_fn(exoid, __func__, errmsg, status);
152 EX_FUNC_LEAVE(EX_FATAL);
153 }
154
155 if ((num_ed_per_elem == 0 && elem_edge_conn != NULL) ||
156 (num_ed_per_elem != 0 && elem_edge_conn == NULL)) {
157 snprintf(errmsg, MAX_ERR_LENGTH,
158 "ERROR: number of edges per element (%ld) doesn't "
159 "agree with elem_edge_conn (0x%p)",
160 (long)num_ed_per_elem, (void *)elem_edge_conn);
161 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
162 EX_FUNC_LEAVE(EX_FATAL);
163 }
164
165 if ((num_fa_per_elem == 0 && elem_face_conn != NULL) ||
166 (num_fa_per_elem != 0 && elem_face_conn == NULL)) {
167 snprintf(errmsg, MAX_ERR_LENGTH,
168 "ERROR: number of faces per element (%ld) doesn't "
169 "agree with elem_face_conn (0x%p)",
170 (long)num_fa_per_elem, (void *)elem_face_conn);
171 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
172 EX_FUNC_LEAVE(EX_FATAL);
173 }
174
175 if (num_ed_per_elem != 0) {
176 status = nc_inq_varid(exoid, VAR_ECONN(blk_id_ndx), &connid);
177 if (status != NC_NOERR) {
178 snprintf(errmsg, MAX_ERR_LENGTH,
179 "ERROR: failed to locate connectivity array for "
180 "element edge block %" PRId64 " in file id %d",
181 blk_id, exoid);
182 ex_err_fn(exoid, __func__, errmsg, status);
183 EX_FUNC_LEAVE(EX_FATAL);
184 }
185 status = ex_int_write_conn(exoid, blk_id, "element edge", connid, elem_edge_conn);
186 if (status != NC_NOERR) {
187 EX_FUNC_LEAVE(EX_FATAL);
188 }
189 }
190
191 if (num_fa_per_elem != 0) {
192 status = nc_inq_varid(exoid, VAR_FCONN(blk_id_ndx), &connid);
193 if (status != NC_NOERR) {
194 snprintf(errmsg, MAX_ERR_LENGTH,
195 "ERROR: failed to locate connectivity array for "
196 "element face block %" PRId64 " in file id %d",
197 blk_id, exoid);
198 ex_err_fn(exoid, __func__, errmsg, status);
199 EX_FUNC_LEAVE(EX_FATAL);
200 }
201 status = ex_int_write_conn(exoid, blk_id, "element face", connid, elem_face_conn);
202 if (status != NC_NOERR) {
203 EX_FUNC_LEAVE(EX_FATAL);
204 }
205 }
206 }
207
208 EX_FUNC_LEAVE(EX_NOERR);
209 }