| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022, 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 |
#include "exodusII.h" // for ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, DIM_NUM_INFO, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
\ingroup Utilities |
| 14 |
|
| 15 |
The function ex_put_info() writes information records to the |
| 16 |
database. The records are #MAX_LINE_LENGTH character strings. |
| 17 |
|
| 18 |
In case of an error, ex_put_info() returns a negative number; |
| 19 |
a warning will return a positive number. Possible causes of errors |
| 20 |
include: |
| 21 |
- data file not properly opened with call to ex_create() or ex_open() |
| 22 |
- data file opened for read only. |
| 23 |
- information records already exist in file. |
| 24 |
|
| 25 |
\param[in] exoid exodus file ID returned from a previous call to |
| 26 |
ex_create() or ex_open(). |
| 27 |
\param[in] num_info The number of information records. |
| 28 |
\param[in] info Array containing the information records. To only |
| 29 |
define the number of info records instead of |
| 30 |
defining and outputting, pass NULL for |
| 31 |
info argument. |
| 32 |
|
| 33 |
The following code will write out three information records |
| 34 |
to an open exodus file - |
| 35 |
|
| 36 |
~~~{.c} |
| 37 |
int error, exoid, num_info; |
| 38 |
char *info[3]; |
| 39 |
|
| 40 |
\comment{write information records} |
| 41 |
num_info = 3; |
| 42 |
|
| 43 |
info[0] = "This is the first information record."; |
| 44 |
info[1] = "This is the second information record."; |
| 45 |
info[2] = "This is the third information record."; |
| 46 |
|
| 47 |
error = ex_put_info(exoid, num_info, info); |
| 48 |
~~~ |
| 49 |
|
| 50 |
The following code will first tell the database that there are three |
| 51 |
information records, and then later actually output those records. |
| 52 |
|
| 53 |
~~~{.c} |
| 54 |
int error, exoid, num_info; |
| 55 |
char *info[3]; |
| 56 |
|
| 57 |
\comment{Define the number of information records that will be written |
| 58 |
later.} |
| 59 |
num_info = 3; |
| 60 |
|
| 61 |
error = ex_put_info(exoid, num_info, NULL); |
| 62 |
|
| 63 |
\comment{Now, actually write the information records} |
| 64 |
info[0] = "This is the first information record."; |
| 65 |
info[1] = "This is the second information record."; |
| 66 |
info[2] = "This is the third information record."; |
| 67 |
error = ex_put_info(exoid, num_info, info); |
| 68 |
|
| 69 |
~~~ |
| 70 |
|
| 71 |
*/ |
| 72 |
|
| 73 |
int ex_put_info(int exoid, int num_info, char *const info[]) |
| 74 |
{ |
| 75 |
int status; |
| 76 |
int i, lindim, num_info_dim, dims[2], varid; |
| 77 |
size_t start[2], count[2]; |
| 78 |
char errmsg[MAX_ERR_LENGTH]; |
| 79 |
|
| 80 |
int rootid = exoid & EX_FILE_ID_MASK; |
| 81 |
|
| 82 |
EX_FUNC_ENTER(); |
| 83 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 84 |
EX_FUNC_LEAVE(EX_FATAL); |
| 85 |
} |
| 86 |
|
| 87 |
/* only do this if there are records */ |
| 88 |
if (num_info > 0) { |
| 89 |
|
| 90 |
/* See if the number of info records has already been defined. |
| 91 |
Assume that if the DIM_NUM_INFO dimension exists, then the |
| 92 |
VAR_INFO variable also exists... |
| 93 |
*/ |
| 94 |
status = nc_inq_dimid(rootid, DIM_NUM_INFO, &num_info_dim); |
| 95 |
if (status != NC_NOERR) { |
| 96 |
|
| 97 |
/* put file into define mode */ |
| 98 |
if ((status = exi_redef(rootid, __func__)) != NC_NOERR) { |
| 99 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed put file id %d into define mode", rootid); |
| 100 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 101 |
EX_FUNC_LEAVE(EX_FATAL); |
| 102 |
} |
| 103 |
|
| 104 |
/* define dimensions */ |
| 105 |
if ((status = nc_def_dim(rootid, DIM_NUM_INFO, num_info, &num_info_dim)) != NC_NOERR) { |
| 106 |
if (status == NC_ENAMEINUSE) { /* duplicate entry? */ |
| 107 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: info records already exist in file id %d", |
| 108 |
rootid); |
| 109 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 110 |
} |
| 111 |
else { |
| 112 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 113 |
"ERROR: failed to define number of info records in file id %d", rootid); |
| 114 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 115 |
} |
| 116 |
|
| 117 |
goto error_ret; /* exit define mode and return */ |
| 118 |
} |
| 119 |
|
| 120 |
/* create line length dimension */ |
| 121 |
if ((status = nc_def_dim(rootid, DIM_LIN, (MAX_LINE_LENGTH + 1), &lindim)) != NC_NOERR) { |
| 122 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define line length in file id %d", |
| 123 |
rootid); |
| 124 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 125 |
goto error_ret; /* exit define mode and return */ |
| 126 |
} |
| 127 |
|
| 128 |
/* define variable */ |
| 129 |
dims[0] = num_info_dim; |
| 130 |
dims[1] = lindim; |
| 131 |
|
| 132 |
if ((status = nc_def_var(rootid, VAR_INFO, NC_CHAR, 2, dims, &varid)) != NC_NOERR) { |
| 133 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define info record in file id %d", |
| 134 |
rootid); |
| 135 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 136 |
goto error_ret; /* exit define mode and return */ |
| 137 |
} |
| 138 |
/* In parallel, only rank=0 will write the info records. |
| 139 |
* Should be able to take advantage of HDF5 handling identical data on all ranks |
| 140 |
* or use the compact storage, but we had issues on some NFS filesystems and some |
| 141 |
* compilers/mpi so are doing it this way... |
| 142 |
*/ |
| 143 |
#if defined(PARALLEL_AWARE_EXODUS) |
| 144 |
if (exi_is_parallel(rootid)) { |
| 145 |
nc_var_par_access(rootid, varid, NC_INDEPENDENT); |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
/* leave define mode */ |
| 150 |
if ((status = exi_leavedef(rootid, __func__)) != NC_NOERR) { |
| 151 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 152 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 153 |
EX_FUNC_LEAVE(EX_FATAL); |
| 154 |
} |
| 155 |
} |
| 156 |
else { |
| 157 |
if ((status = nc_inq_varid(rootid, VAR_INFO, &varid)) != NC_NOERR) { |
| 158 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find info record variable in file id %d", |
| 159 |
rootid); |
| 160 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 161 |
EX_FUNC_LEAVE(EX_FATAL); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (info != NULL) { |
| 166 |
/* write out information records */ |
| 167 |
for (i = 0; i < num_info; i++) { |
| 168 |
int length = strlen(info[i]) + 1; |
| 169 |
start[0] = i; |
| 170 |
start[1] = 0; |
| 171 |
|
| 172 |
count[0] = 1; |
| 173 |
count[1] = length < MAX_LINE_LENGTH ? length : MAX_LINE_LENGTH; |
| 174 |
|
| 175 |
if ((status = nc_put_vara_text(rootid, varid, start, count, info[i])) != NC_NOERR) { |
| 176 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store info record in file id %d", |
| 177 |
rootid); |
| 178 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 179 |
EX_FUNC_LEAVE(EX_FATAL); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
/* PnetCDF applies setting to entire file, so put back to collective... */ |
| 184 |
#if defined(PARALLEL_AWARE_EXODUS) |
| 185 |
if (exi_is_parallel(rootid)) { |
| 186 |
nc_var_par_access(rootid, varid, NC_COLLECTIVE); |
| 187 |
} |
| 188 |
#endif |
| 189 |
} |
| 190 |
EX_FUNC_LEAVE(EX_NOERR); |
| 191 |
|
| 192 |
/* Fatal error: exit definition mode and return */ |
| 193 |
error_ret: |
| 194 |
exi_leavedef(rootid, __func__); |
| 195 |
EX_FUNC_LEAVE(EX_FATAL); |
| 196 |
} |