Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2022, 2024, 2025 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 * exgini - ex_get_init
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 *
16 * exit conditions -
17 * ex_init_params* info parameter information
18 *
19 * revision history -
20 *
21 *
22 *****************************************************************************/
23
24 #include "exodusII.h" // for ex_init_params, etc
25 #include "exodusII_int.h" // for EX_FATAL, EX_NOERR, etc
26
27 static void exi_get_entity_count(int exoid, ex_init_params *info)
28 {
29 int ndims = 0;
30 #if NC_HAS_HDF5
31 int include_parent_group = 0; // Only want dims in current group
32 nc_inq_dimids(exoid, &ndims, NULL, include_parent_group);
33 int *dimids = calloc(ndims, sizeof(int));
34 nc_inq_dimids(exoid, &ndims, dimids, include_parent_group);
35 #else
36 nc_inq(exoid, &ndims, NULL, NULL, NULL);
37 #endif
38 for (int dimid = 0; dimid < ndims; dimid++) {
39 char dim_nm[NC_MAX_NAME + 1] = {'\0'};
40 size_t dim_sz;
41 #if NC_HAS_HDF5
42 nc_inq_dim(exoid, dimids[dimid], dim_nm, &dim_sz);
43 #else
44 nc_inq_dim(exoid, dimid, dim_nm, &dim_sz);
45 #endif
46 /* For assemblies, we check for a dim starting with "num_entity_assembly" */
47 if (strncmp(dim_nm, "num_entity_assembly", 19) == 0) {
48 info->num_assembly++;
49 }
50 else if (strncmp(dim_nm, "num_values_blob", 15) == 0) {
51 info->num_blob++;
52 }
53 }
54 #if NC_HAS_HDF5
55 free(dimids);
56 #endif
57 }
58
59 /* Used to reduce repeated code below */
60 static int ex_get_dim_value(int exoid, const char *name, const char *dimension_name, int dimension,
61 int64_t *value)
62 {
63 if (nc_inq_dimid(exoid, dimension_name, &dimension) != NC_NOERR) {
64 /* optional and default to zero. */
65 *value = 0;
66 }
67 else {
68 size_t tmp;
69 int status;
70 if ((status = nc_inq_dimlen(exoid, dimension, &tmp)) != NC_NOERR) {
71 char errmsg[MAX_ERR_LENGTH];
72 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of %s in file id %d", name,
73 exoid);
74 ex_err_fn(exoid, __func__, errmsg, status);
75 return EX_FATAL;
76 }
77 *value = tmp;
78 }
79 return EX_NOERR;
80 }
81
82 /*!
83 * \ingroup ModelDescription
84 * reads the initialization parameters from an opened EXODUS file
85 * \param exoid exodus file id
86 * \param[out] info #ex_init_params structure containing metadata for mesh.
87 * \sa ex_get_init()
88 */
89
90 int ex_get_init_ext(int exoid, ex_init_params *info)
91 {
92 EX_FUNC_ENTER();
93 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
94 EX_FUNC_LEAVE(EX_FATAL);
95 }
96
97 info->num_dim = 0;
98 info->num_nodes = 0;
99 info->num_edge = 0;
100 info->num_edge_blk = 0;
101 info->num_face = 0;
102 info->num_face_blk = 0;
103 info->num_elem = 0;
104 info->num_elem_blk = 0;
105 info->num_node_sets = 0;
106 info->num_edge_sets = 0;
107 info->num_face_sets = 0;
108 info->num_side_sets = 0;
109 info->num_elem_sets = 0;
110 info->num_node_maps = 0;
111 info->num_edge_maps = 0;
112 info->num_face_maps = 0;
113 info->num_elem_maps = 0;
114 info->num_assembly = 0;
115 info->num_blob = 0;
116
117 int dimid = 0;
118 if (ex_get_dim_value(exoid, "dimension count", DIM_NUM_DIM, dimid, &info->num_dim) != EX_NOERR) {
119 EX_FUNC_LEAVE(EX_FATAL);
120 }
121 if (ex_get_dim_value(exoid, "nodes", DIM_NUM_NODES, dimid, &info->num_nodes) != EX_NOERR) {
122 EX_FUNC_LEAVE(EX_FATAL);
123 }
124
125 /* Counts for assemblies and blobs */
126 exi_get_entity_count(exoid, info);
127
128 if (ex_get_dim_value(exoid, "edges", DIM_NUM_EDGE, dimid, &info->num_edge) != EX_NOERR) {
129 EX_FUNC_LEAVE(EX_FATAL);
130 }
131 if (ex_get_dim_value(exoid, "faces", DIM_NUM_FACE, dimid, &info->num_face) != EX_NOERR) {
132 EX_FUNC_LEAVE(EX_FATAL);
133 }
134 if (ex_get_dim_value(exoid, "elements", DIM_NUM_ELEM, dimid, &info->num_elem) != EX_NOERR) {
135 EX_FUNC_LEAVE(EX_FATAL);
136 }
137
138 if (ex_get_dim_value(exoid, "element blocks", DIM_NUM_EL_BLK, dimid, &info->num_elem_blk) !=
139 EX_NOERR) {
140 EX_FUNC_LEAVE(EX_FATAL);
141 }
142 if (info->num_elem_blk == 0 && info->num_elem > 0) {
143 char errmsg[MAX_ERR_LENGTH];
144 snprintf(errmsg, MAX_ERR_LENGTH,
145 "ERROR: failed to locate number of element blocks in file id %d", exoid);
146 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
147 EX_FUNC_LEAVE(EX_FATAL);
148 }
149
150 if (ex_get_dim_value(exoid, "node sets", DIM_NUM_NS, dimid, &info->num_node_sets) != EX_NOERR) {
151 EX_FUNC_LEAVE(EX_FATAL);
152 }
153 if (ex_get_dim_value(exoid, "edge sets", DIM_NUM_ES, dimid, &info->num_edge_sets) != EX_NOERR) {
154 EX_FUNC_LEAVE(EX_FATAL);
155 }
156 if (ex_get_dim_value(exoid, "face sets", DIM_NUM_FS, dimid, &info->num_face_sets) != EX_NOERR) {
157 EX_FUNC_LEAVE(EX_FATAL);
158 }
159 if (ex_get_dim_value(exoid, "side sets", DIM_NUM_SS, dimid, &info->num_side_sets) != EX_NOERR) {
160 EX_FUNC_LEAVE(EX_FATAL);
161 }
162 if (ex_get_dim_value(exoid, "elem sets", DIM_NUM_ELS, dimid, &info->num_elem_sets) != EX_NOERR) {
163 EX_FUNC_LEAVE(EX_FATAL);
164 }
165
166 if (ex_get_dim_value(exoid, "node maps", DIM_NUM_NM, dimid, &info->num_node_maps) != EX_NOERR) {
167 EX_FUNC_LEAVE(EX_FATAL);
168 }
169 if (ex_get_dim_value(exoid, "edge maps", DIM_NUM_EDM, dimid, &info->num_edge_maps) != EX_NOERR) {
170 EX_FUNC_LEAVE(EX_FATAL);
171 }
172 if (ex_get_dim_value(exoid, "face maps", DIM_NUM_FAM, dimid, &info->num_face_maps) != EX_NOERR) {
173 EX_FUNC_LEAVE(EX_FATAL);
174 }
175 if (ex_get_dim_value(exoid, "elem maps", DIM_NUM_EM, dimid, &info->num_elem_maps) != EX_NOERR) {
176 EX_FUNC_LEAVE(EX_FATAL);
177 }
178
179 /* Edge and face blocks are also optional (for backwards compatibility) */
180 if (ex_get_dim_value(exoid, "edge blocks", DIM_NUM_ED_BLK, dimid, &info->num_edge_blk) !=
181 EX_NOERR) {
182 EX_FUNC_LEAVE(EX_FATAL);
183 }
184 if (ex_get_dim_value(exoid, "face blocks", DIM_NUM_FA_BLK, dimid, &info->num_face_blk) !=
185 EX_NOERR) {
186 EX_FUNC_LEAVE(EX_FATAL);
187 }
188
189 int rootid = exoid & EX_FILE_ID_MASK;
190 int status;
191 size_t title_len = 0;
192 nc_type title_type = 0;
193 if ((status = nc_inq_att(rootid, NC_GLOBAL, ATT_TITLE, &title_type, &title_len)) == NC_NOERR) {
194 /* Check title length to avoid overrunning clients memory space; include
195 * trailing null */
196 if (title_len > 0) {
197 if (title_len > MAX_LINE_LENGTH) {
198 char *title = malloc(title_len + 1);
199 if ((status = nc_get_att_text(rootid, NC_GLOBAL, ATT_TITLE, title)) == NC_NOERR) {
200 ex_copy_string(info->title, title, MAX_LINE_LENGTH + 1);
201 info->title[MAX_LINE_LENGTH] = '\0';
202 }
203 free(title);
204 }
205 else {
206 status = nc_get_att_text(rootid, NC_GLOBAL, ATT_TITLE, info->title);
207 info->title[title_len] = '\0';
208 }
209 if (status != NC_NOERR) {
210 char errmsg[MAX_ERR_LENGTH];
211 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get title in file id %d", rootid);
212 ex_err_fn(exoid, __func__, errmsg, status);
213 EX_FUNC_LEAVE(EX_FATAL);
214 }
215 }
216 }
217 else {
218 info->title[0] = '\0';
219 }
220
221 /* Update settings in exi_file_item struct */
222 {
223 struct exi_file_item *file = exi_find_file_item(exoid);
224 if (file) {
225 file->has_nodes = info->num_nodes > 0;
226 file->has_edges = info->num_edge > 0;
227 file->has_faces = info->num_face > 0;
228 file->has_elems = info->num_elem > 0;
229 file->assembly_count = info->num_assembly;
230 file->blob_count = info->num_blob;
231 }
232 }
233 EX_FUNC_LEAVE(EX_NOERR);
234 }