Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021 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_comp_ws, etc
11
12 /*!
13 The function ex_get_coord() reads the nodal coordinates of the
14 nodes. Memory must be allocated for the coordinate arrays (x_coor,
15 y_coor, and z_coor) before this call is made. The length of each
16 of these arrays is the number of nodes in the mesh.
17
18 Because the coordinates are floating point values, the application
19 code must declare the arrays passed to be the appropriate type
20 (float or double) to match the compute word size passed in
21 ex_create() or ex_open().
22
23 \return
24 In case of an error, ex_get_coord() returns a negative number;
25 a warning will return a positive number. Possible causes of errors
26 include:
27 - data file not properly opened with call to ex_create() or ex_open()
28 - a warning value is returned if nodal coordinates were not stored.
29
30 \param[in] exoid exodus file ID returned from a previous call to
31 ex_create() or ex_open().
32 \param[out] x_coor Returned X coordinates of the nodes. If this is NULL,
33 the
34 X-coordinates will not be read.
35 \param[out] y_coor Returned Y coordinates of the nodes. These are returned
36 only if
37 num_dim > 1; otherwise, pass in NULL. If this
38 is NULL, the Y-coordinates will not be read.
39 \param[out] z_coor Returned Z coordinates of the nodes. These are returned
40 only if
41 num_dim > 2; otherwise, pass in NULL. If this
42 is NULL, the Z-coordinates will not be read.
43
44 The following code segment will read the nodal coordinates
45 from an open exodus file :
46
47 ~~~{.c}
48 int error, exoid;
49
50 double *x, *y, *z;
51
52 \comment{read nodal coordinates values from database}
53 x = (double *)calloc(num_nodes, sizeof(double));
54 y = (double *)calloc(num_nodes, sizeof(double));
55 if (num_dim >= 3)
56 z = (double *)calloc(num_nodes, sizeof(double));
57 else
58 z = 0;
59
60 error = ex_get_coord(exoid, x, y, z);
61
62 \comment{Do the same as the previous call in three separate calls}
63 error = ex_get_coord(exoid, x, NULL, NULL);
64 error = ex_get_coord(exoid, NULL, y, NULL);
65 if (num_dim >= 3)
66 error = ex_get_coord(exoid, NULL, NULL, z);
67 ~~~
68
69 */
70
71 int ex_get_coord(int exoid, void *x_coor, void *y_coor, void *z_coor)
72 {
73 int status;
74 int coordid = -1;
75 int coordidx, coordidy, coordidz;
76
77 int numnoddim, ndimdim;
78 size_t num_nod, num_dim;
79 char errmsg[MAX_ERR_LENGTH];
80
81 EX_FUNC_ENTER();
82 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
83 EX_FUNC_LEAVE(EX_FATAL);
84 }
85
86 /* inquire id's of previously defined dimensions */
87
88 if (exi_get_dimension(exoid, DIM_NUM_DIM, "dimension count", &num_dim, &ndimdim, __func__) !=
89 NC_NOERR) {
90 EX_FUNC_LEAVE(EX_FATAL);
91 }
92
93 if (nc_inq_dimid(exoid, DIM_NUM_NODES, &numnoddim) != NC_NOERR) {
94 /* If not found, then this file is storing 0 nodes.
95 Return immediately */
96 EX_FUNC_LEAVE(EX_NOERR);
97 }
98
99 if ((status = nc_inq_dimlen(exoid, numnoddim, &num_nod)) != NC_NOERR) {
100 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of nodes in file id %d", exoid);
101 ex_err_fn(exoid, __func__, errmsg, status);
102 EX_FUNC_LEAVE(EX_FATAL);
103 }
104
105 /* read in the coordinates */
106 if (ex_large_model(exoid) == 0) {
107 if ((status = nc_inq_varid(exoid, VAR_COORD, &coordid)) != NC_NOERR) {
108 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate nodal coordinates in file id %d",
109 exoid);
110 ex_err_fn(exoid, __func__, errmsg, status);
111 EX_FUNC_LEAVE(EX_FATAL);
112 }
113
114 for (size_t i = 0; i < num_dim; i++) {
115 char *which = NULL;
116 size_t start[2];
117 size_t count[2];
118 start[0] = i;
119 start[1] = 0;
120
121 count[0] = 1;
122 count[1] = num_nod;
123
124 if (i == 0 && x_coor != NULL) {
125 which = "X";
126 if (exi_comp_ws(exoid) == 4) {
127 status = nc_get_vara_float(exoid, coordid, start, count, x_coor);
128 }
129 else {
130 status = nc_get_vara_double(exoid, coordid, start, count, x_coor);
131 }
132 }
133 else if (i == 1 && y_coor != NULL) {
134 which = "Y";
135 if (exi_comp_ws(exoid) == 4) {
136 status = nc_get_vara_float(exoid, coordid, start, count, y_coor);
137 }
138 else {
139 status = nc_get_vara_double(exoid, coordid, start, count, y_coor);
140 }
141 }
142 else if (i == 2 && z_coor != NULL) {
143 which = "Z";
144 if (exi_comp_ws(exoid) == 4) {
145 status = nc_get_vara_float(exoid, coordid, start, count, z_coor);
146 }
147 else {
148 status = nc_get_vara_double(exoid, coordid, start, count, z_coor);
149 }
150 }
151
152 if (status != NC_NOERR) {
153 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s coord array in file id %d", which,
154 exoid);
155 ex_err_fn(exoid, __func__, errmsg, status);
156 EX_FUNC_LEAVE(EX_FATAL);
157 }
158 }
159 }
160 else {
161 if ((status = nc_inq_varid(exoid, VAR_COORD_X, &coordidx)) != NC_NOERR) {
162 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate x nodal coordinates in file id %d",
163 exoid);
164 ex_err_fn(exoid, __func__, errmsg, status);
165 EX_FUNC_LEAVE(EX_FATAL);
166 }
167
168 if (num_dim > 1) {
169 if ((status = nc_inq_varid(exoid, VAR_COORD_Y, &coordidy)) != NC_NOERR) {
170 snprintf(errmsg, MAX_ERR_LENGTH,
171 "ERROR: failed to locate y nodal coordinates in file id %d", exoid);
172 ex_err_fn(exoid, __func__, errmsg, status);
173 EX_FUNC_LEAVE(EX_FATAL);
174 }
175 }
176 else {
177 coordidy = -1;
178 }
179
180 if (num_dim > 2) {
181 if ((status = nc_inq_varid(exoid, VAR_COORD_Z, &coordidz)) != NC_NOERR) {
182 snprintf(errmsg, MAX_ERR_LENGTH,
183 "ERROR: failed to locate z nodal coordinates in file id %d", exoid);
184 ex_err_fn(exoid, __func__, errmsg, status);
185 EX_FUNC_LEAVE(EX_FATAL);
186 }
187 }
188 else {
189 coordidz = -1;
190 }
191
192 /* write out the coordinates */
193 for (size_t i = 0; i < num_dim; i++) {
194 void *coor = NULL;
195 char *which = NULL;
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_var_float(exoid, coordid, coor);
216 }
217 else {
218 status = nc_get_var_double(exoid, coordid, 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 }