Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 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 *
10 * exgcor - ex_get_partial_coord
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int start_node_num starting index of coordinates to be
16 *returned.
17 * int num_nodes number of nodes to read coordinates for.
18 *
19 * exit conditions -
20 * float* x_coord X coord array
21 * float* y_coord y coord array
22 * float* z_coord z coord array
23 *
24 * revision history -
25 *
26 *
27 *****************************************************************************/
28
29 #include "exodusII.h" // for ex_err, etc
30 #include "exodusII_int.h" // for EX_FATAL, exi_comp_ws, etc
31
32 /*!
33 * reads the coordinates of the nodes.
34 * Memory must be allocated for the coordinate arrays (x_coor, y_coor,
35 * and z_coor) before this call is made. The length of each of these
36 * arrays is the number of nodes in the mesh. Because the coordinates
37 * are floating point values, the application code must declare the
38 * arrays passed to be the appropriate type "float" or "double"
39 * to match the compute word size passed in ex_create() or ex_open()
40 * \param exoid exodus file id
41 * \param start_node_num the starting index of the coordinates to be
42 * returned.
43 * \param num_nodes the number of nodes to read coordinates for.
44 * \param[out] x_coor Returned X coordinates of the nodes. These are
45 * returned only if x_coor is non-NULL.
46 * \param[out] y_coor Returned Y coordinates of the nodes. These are
47 * returned only if y_coor is non-NULL.
48 * \param[out] z_coor Returned Z coordinates of the nodes. These are
49 * returned only if z_coor is non-NULL.
50 */
51
52 int ex_get_partial_coord(int exoid, int64_t start_node_num, int64_t num_nodes, void *x_coor,
53 void *y_coor, void *z_coor)
54 {
55 int status;
56 int coordid = -1;
57 int coordidx, coordidy, coordidz;
58
59 int numnoddim, ndimdim;
60 size_t num_nod;
61 size_t num_dim, start[2], count[2], i;
62 char errmsg[MAX_ERR_LENGTH];
63
64 EX_FUNC_ENTER();
65 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
66 EX_FUNC_LEAVE(EX_FATAL);
67 }
68
69 /* inquire id's of previously defined dimensions */
70
71 if (nc_inq_dimid(exoid, DIM_NUM_NODES, &numnoddim) != NC_NOERR) {
72 /* If not found, then this file is storing 0 nodes.
73 Return immediately */
74 EX_FUNC_LEAVE(EX_NOERR);
75 }
76
77 if ((status = nc_inq_dimlen(exoid, numnoddim, &num_nod)) != NC_NOERR) {
78 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of nodes in file id %d", exoid);
79 ex_err_fn(exoid, __func__, errmsg, status);
80 EX_FUNC_LEAVE(EX_FATAL);
81 }
82
83 --start_node_num;
84 if (start_node_num + num_nodes > (int64_t)num_nod) {
85 snprintf(errmsg, MAX_ERR_LENGTH,
86 "ERROR: start index (%" PRId64 ") + node count (%" PRId64
87 ") is larger than total number of nodes (%zu) in file id %d",
88 start_node_num, num_nodes, num_nod, exoid);
89 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
90 EX_FUNC_LEAVE(EX_FATAL);
91 }
92
93 if (exi_get_dimension(exoid, DIM_NUM_DIM, "dimension count", &num_dim, &ndimdim, __func__) !=
94 NC_NOERR) {
95 EX_FUNC_LEAVE(EX_FATAL);
96 }
97
98 /* read in the coordinates */
99 if (ex_large_model(exoid) == 0) {
100 if ((status = nc_inq_varid(exoid, VAR_COORD, &coordid)) != NC_NOERR) {
101 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate nodal coordinates in file id %d",
102 exoid);
103 ex_err_fn(exoid, __func__, errmsg, status);
104 EX_FUNC_LEAVE(EX_FATAL);
105 }
106
107 for (i = 0; i < num_dim; i++) {
108 char *which;
109 start[0] = i;
110 start[1] = start_node_num;
111
112 count[0] = 1;
113 count[1] = num_nodes;
114 if (count[1] == 0) {
115 start[1] = 0;
116 }
117
118 if (i == 0 && x_coor != NULL) {
119 which = "X";
120 if (exi_comp_ws(exoid) == 4) {
121 status = nc_get_vara_float(exoid, coordid, start, count, x_coor);
122 }
123 else {
124 status = nc_get_vara_double(exoid, coordid, start, count, x_coor);
125 }
126 }
127 else if (i == 1 && y_coor != NULL) {
128 which = "Y";
129 if (exi_comp_ws(exoid) == 4) {
130 status = nc_get_vara_float(exoid, coordid, start, count, y_coor);
131 }
132 else {
133 status = nc_get_vara_double(exoid, coordid, start, count, y_coor);
134 }
135 }
136 else if (i == 2 && z_coor != NULL) {
137 which = "Z";
138 if (exi_comp_ws(exoid) == 4) {
139 status = nc_get_vara_float(exoid, coordid, start, count, z_coor);
140 }
141 else {
142 status = nc_get_vara_double(exoid, coordid, start, count, z_coor);
143 }
144 }
145
146 if (status != NC_NOERR) {
147 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s coord array in file id %d", which,
148 exoid);
149 ex_err_fn(exoid, __func__, errmsg, status);
150 EX_FUNC_LEAVE(EX_FATAL);
151 }
152 }
153 }
154 else {
155 if ((status = nc_inq_varid(exoid, VAR_COORD_X, &coordidx)) != NC_NOERR) {
156 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate x nodal coordinates in file id %d",
157 exoid);
158 ex_err_fn(exoid, __func__, errmsg, status);
159 EX_FUNC_LEAVE(EX_FATAL);
160 }
161
162 if (num_dim > 1) {
163 if ((status = nc_inq_varid(exoid, VAR_COORD_Y, &coordidy)) != NC_NOERR) {
164 snprintf(errmsg, MAX_ERR_LENGTH,
165 "ERROR: failed to locate y nodal coordinates in file id %d", exoid);
166 ex_err_fn(exoid, __func__, errmsg, status);
167 EX_FUNC_LEAVE(EX_FATAL);
168 }
169 }
170 else {
171 coordidy = -1;
172 }
173
174 if (num_dim > 2) {
175 if ((status = nc_inq_varid(exoid, VAR_COORD_Z, &coordidz)) != NC_NOERR) {
176 snprintf(errmsg, MAX_ERR_LENGTH,
177 "ERROR: failed to locate z nodal coordinates in file id %d", exoid);
178 ex_err_fn(exoid, __func__, errmsg, status);
179 EX_FUNC_LEAVE(EX_FATAL);
180 }
181 }
182 else {
183 coordidz = -1;
184 }
185
186 /* write out the coordinates */
187 for (i = 0; i < num_dim; i++) {
188 void *coor = NULL;
189 char *which = NULL;
190
191 start[0] = start_node_num;
192 count[0] = num_nodes;
193 if (count[0] == 0) {
194 start[0] = 0;
195 }
196
197 if (i == 0) {
198 coor = x_coor;
199 which = "X";
200 coordid = coordidx;
201 }
202 else if (i == 1) {
203 coor = y_coor;
204 which = "Y";
205 coordid = coordidy;
206 }
207 else if (i == 2) {
208 coor = z_coor;
209 which = "Z";
210 coordid = coordidz;
211 }
212
213 if (coor != NULL && coordid != -1) {
214 if (exi_comp_ws(exoid) == 4) {
215 status = nc_get_vara_float(exoid, coordid, start, count, coor);
216 }
217 else {
218 status = nc_get_vara_double(exoid, coordid, start, count, coor);
219 }
220
221 if (status != NC_NOERR) {
222 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s coord array in file id %d",
223 which, exoid);
224 ex_err_fn(exoid, __func__, errmsg, status);
225 EX_FUNC_LEAVE(EX_FATAL);
226 }
227 }
228 }
229 }
230 EX_FUNC_LEAVE(EX_NOERR);
231 }