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