Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 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 * expcor - ex_put_partial_coord
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int start_node_num starting index (1-based) of coordinates
16 *to be written.
17 * int num_nodes number of nodes to write coordinates
18 *for.
19 * float* x_coord X coord array
20 * float* y_coord y coord array
21 * float* z_coord z coord array
22 *
23 * exit conditions -
24 *
25 * revision history -
26 *
27 *
28 *****************************************************************************/
29
30 #include "exodusII.h" // for ex_err, etc
31 #include "exodusII_int.h" // for EX_FATAL, exi_comp_ws, etc
32
33 /*!
34 * writes the coordinates of some of the nodes in the model
35 * Only writes the 'non-null' arrays.
36 * \param exoid exodus file id
37 * \param start_node_num the starting index (1-based) of the coordinates to
38 * be written
39 * \param num_nodes the number of nodes to write coordinates for.
40 * \param x_coor x coord array
41 * \param y_coor y coord array
42 * \param z_coor z coord array
43 */
44
45 int ex_put_partial_coord(int exoid, int64_t start_node_num, int64_t num_nodes, const void *x_coor,
46 const void *y_coor, const void *z_coor)
47 {
48 int status;
49 int coordid = -1;
50 int coordidx, coordidy, coordidz;
51
52 int numnoddim, ndimdim;
53 int64_t num_nod;
54 size_t i, num_dim, start[2], count[2];
55 char errmsg[MAX_ERR_LENGTH];
56
57 EX_FUNC_ENTER();
58 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
59 EX_FUNC_LEAVE(EX_FATAL);
60 }
61
62 /* inquire id's of previously defined dimensions */
63
64 if (nc_inq_dimid(exoid, DIM_NUM_NODES, &numnoddim) != NC_NOERR) {
65 /* If not found, then this file is storing 0 nodes.
66 Return immediately */
67 EX_FUNC_LEAVE(EX_NOERR);
68 }
69
70 {
71 size_t tmp;
72 if ((status = nc_inq_dimlen(exoid, numnoddim, &tmp)) != NC_NOERR) {
73 snprintf(errmsg, MAX_ERR_LENGTH,
74 "ERROR: inquire failed to return number of nodes in file id %d", exoid);
75 ex_err_fn(exoid, __func__, errmsg, status);
76 EX_FUNC_LEAVE(EX_FATAL);
77 }
78 num_nod = tmp;
79 }
80
81 if ((status = nc_inq_dimid(exoid, DIM_NUM_DIM, &ndimdim)) != NC_NOERR) {
82 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate number of dimensions in file id %d",
83 exoid);
84 ex_err_fn(exoid, __func__, errmsg, status);
85 EX_FUNC_LEAVE(EX_FATAL);
86 }
87
88 if ((status = nc_inq_dimlen(exoid, ndimdim, &num_dim)) != NC_NOERR) {
89 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of dimensions in file id %d",
90 exoid);
91 ex_err_fn(exoid, __func__, errmsg, status);
92 EX_FUNC_LEAVE(EX_FATAL);
93 }
94
95 --start_node_num;
96 if (start_node_num + num_nodes > num_nod) {
97 snprintf(errmsg, MAX_ERR_LENGTH,
98 "ERROR: start index (%" PRId64 ") + node count (%" PRId64
99 ") is larger than total number of nodes (%" PRId64 ") in file id %d",
100 start_node_num, num_nodes, num_nod, exoid);
101 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
102 EX_FUNC_LEAVE(EX_FATAL);
103 }
104
105 /* write out the coordinates */
106 if (num_dim > 0) {
107 if ((status = nc_inq_varid(exoid, VAR_COORD_X, &coordidx)) != NC_NOERR) {
108 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate x 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 else {
115 coordidx = -1;
116 }
117 if (num_dim > 1) {
118 if ((status = nc_inq_varid(exoid, VAR_COORD_Y, &coordidy)) != NC_NOERR) {
119 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate y nodal coordinates in file id %d",
120 exoid);
121 ex_err_fn(exoid, __func__, errmsg, status);
122 EX_FUNC_LEAVE(EX_FATAL);
123 }
124 }
125 else {
126 coordidy = -1;
127 }
128 if (num_dim > 2) {
129 if ((status = nc_inq_varid(exoid, VAR_COORD_Z, &coordidz)) != NC_NOERR) {
130 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate z nodal coordinates in file id %d",
131 exoid);
132 ex_err_fn(exoid, __func__, errmsg, status);
133 EX_FUNC_LEAVE(EX_FATAL);
134 }
135 }
136 else {
137 coordidz = -1;
138 }
139
140 /* write out the coordinates */
141 for (i = 0; i < num_dim; i++) {
142 const void *coor = NULL;
143 char *which = NULL;
144
145 start[0] = start_node_num;
146 count[0] = num_nodes;
147 if (count[0] == 0) {
148 start[0] = 0;
149 }
150
151 if (i == 0) {
152 coor = x_coor;
153 which = "X";
154 coordid = coordidx;
155 }
156 else if (i == 1) {
157 coor = y_coor;
158 which = "Y";
159 coordid = coordidy;
160 }
161 else if (i == 2) {
162 coor = z_coor;
163 which = "Z";
164 coordid = coordidz;
165 }
166
167 if (coor != NULL && coordid != -1) {
168 if (exi_comp_ws(exoid) == 4) {
169 status = nc_put_vara_float(exoid, coordid, start, count, coor);
170 }
171 else {
172 status = nc_put_vara_double(exoid, coordid, start, count, coor);
173 }
174
175 if (status != NC_NOERR) {
176 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put %s coord array in file id %d", which,
177 exoid);
178 ex_err_fn(exoid, __func__, errmsg, status);
179 EX_FUNC_LEAVE(EX_FATAL);
180 }
181 }
182 }
183 EX_FUNC_LEAVE(EX_NOERR);
184 }