Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021, 2023 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 \ingroup Utilities
11
12 \note The ex_create_int() is an internal function called by
13 ex_create(). The user should call ex_create() and not ex_create_int().
14
15 The function ex_create() creates a new exodus file and returns an ID
16 that can subsequently be used to refer to the file.
17
18 All floating point values in an exodus file are stored as either
19 4-byte (float) or 8-byte (double) numbers; no mixing of 4- and
20 8-byte numbers in a single file is allowed. An application code can
21 compute either 4- or 8-byte values and can designate that the values
22 be stored in the exodus file as either 4- or 8-byte numbers;
23 conversion between the 4- and 8-byte values is performed automatically
24 by the API routines. Thus, there are four possible combinations of
25 compute word size and storage (or I/O) word size.
26
27 \return In case of an error, ex_create() returns a negative number. Possible
28 causes of errors include:
29 - Passing a file name that includes a directory that does not
30 exist.
31 - Specifying a file name of a file that exists and also
32 specifying a no clobber option.
33 - Attempting to create a file in a directory without permission
34 to create files there.
35 - Passing an invalid file clobber mode.
36
37 \param path The file name of the new exodus file. This can be given as either an
38 absolute path name (from the root of the file system) or a relative
39 path name (from the current directory).
40
41 \param cmode Mode. Use one of the following predefined constants:
42 \arg #EX_NOCLOBBER To create the new file only if the given file name does
43 not refer to a
44 file that already exists.
45
46 \arg #EX_CLOBBER To create the new file, regardless of whether a file with
47 the same
48 name already exists. If a file with the same name does
49 exist, its
50 contents will be erased.
51
52 \arg #EX_64BIT_OFFSET To create a model that can store individual datasets
53 larger than
54 2 gigabytes. This modifies the internal storage used by
55 exodusII and
56 also puts the underlying NetCDF file into the \e 64-bit
57 offset'
58 mode. See largemodel for more details on this
59 mode. A large model file will also be created if the
60 environment variable EXODUS_LARGE_MODEL is defined
61 in the users environment. A message will be printed to
62 standard output
63 if this environment variable is found. #EX_LARGE_MODEL is
64 alias.
65
66 \arg #EX_NORMAL_MODEL Create a standard model.
67
68 \arg #EX_64BIT_DATA To create a model using the CDF5 format which uses the
69 classic model but has 64-bit dimensions and sizes.
70 This type will also be created if the
71 environment variable EXODUS_NETCDF5 is defined in the
72 users environment. A message will be printed to standard
73 output if
74 this environment variable is found.
75
76 \arg #EX_NETCDF4 To create a model using the HDF5-based NetCDF-4
77 output. An HDF5-based NetCDF-4 file will also be created
78 if the
79 environment variable EXODUS_NETCDF4 is defined in the
80 users environment. A message will be printed to standard
81 output if
82 this environment variable is found.
83
84 \arg #EX_NOSHARE Do not open the underlying NetCDF file in \e share
85 mode. See the
86 NetCDF documentation for more details.
87
88 \arg #EX_SHARE Do open the underlying NetCDF file in \e share mode. See
89 the NetCDF
90 documentation for more details.
91
92 \param[in,out] comp_ws The word size in bytes (0, 4 or 8) of the floating point
93 variables
94 used in the application program. If 0 (zero) is passed,
95 the default
96 sizeof(float) will be used and returned in this
97 variable. WARNING: all
98 exodus functions requiring floats must be passed floats
99 declared with
100 this passed in or returned compute word size (4 or 8).}
101
102 \param io_ws The word size in bytes (4 or 8) of the floating point
103 data as they are to be stored in the exodus file.
104
105 \param run_version (internally generated) used to verify compatibility of library
106 and include files.
107
108 The following code segment creates an exodus file called \file{test.exo}:
109
110 ~~~{.c}
111 #include "exodusII.h"
112 int CPU_word_size, IO_word_size, exoid;
113 CPU_word_size = sizeof(float); \comment{use float or double}
114 IO_word_size = 8; \comment{store variables as doubles}
115
116 \comment{create exodus file}
117 exoid = ex_create ("test.exo" \comment{filename path}
118 EX_CLOBBER, \comment{create mode}
119 &CPU_word_size, \comment{CPU float word size in bytes}
120 &IO_word_size); \comment{I/O float word size in bytes}
121 ~~~
122
123 */
124 #include "exodusII.h"
125 #include "exodusII_int.h"
126 #include <stdlib.h>
127
128 /* NOTE: Do *not* call `ex_create_int()` directly. The public API
129 * function name is `ex_create()` which is a wrapper that calls
130 * `ex_create_int` with an additional argument to make sure
131 * library and include file are consistent
132 */
133 int ex_create_int(const char *path, int cmode, int *comp_ws, int *io_ws, int run_version)
134 {
135 int exoid = 0;
136 int status = 0;
137 char errmsg[MAX_ERR_LENGTH];
138 int nc_mode = 0;
139
140 unsigned int my_mode = cmode;
141 int is_parallel = 0;
142
143 EX_FUNC_ENTER();
144
145 nc_mode = exi_handle_mode(my_mode, is_parallel, run_version);
146
147 if (!path || strlen(path) == 0) {
148 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Filename is not specified.");
149 ex_err(__func__, errmsg, EX_BADFILEMODE);
150 EX_FUNC_LEAVE(EX_FATAL);
151 }
152
153 char *canon_path = exi_canonicalize_filename(path);
154
155 /* Verify that this file is not already open for read or write...
156 In theory, should be ok for the file to be open multiple times
157 for read, but bad things can happen if being read and written
158 at the same time...
159 */
160 if (exi_check_multiple_open(canon_path, EX_WRITE, __func__)) {
161 free(canon_path);
162 EX_FUNC_LEAVE(EX_FATAL);
163 }
164
165 #if defined NC_NOATTCREORD
166 /* Disable attribute creation order tracking if available... */
167 nc_mode |= NC_NOATTCREORD;
168 #endif
169
170 #if defined NC_NODIMSCALE_ATTACH
171 /* Disable attaching dimscales to variables (netcdf-c issue #2128) if available */
172 nc_mode |= NC_NODIMSCALE_ATTACH;
173 #endif
174
175 if ((status = nc_create(path, nc_mode, &exoid)) != NC_NOERR) {
176 #if NC_HAS_HDF5
177 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s", canon_path);
178 #else
179 if (my_mode & EX_NETCDF4) {
180 snprintf(errmsg, MAX_ERR_LENGTH,
181 "ERROR: file create failed for %s in NETCDF4 "
182 "mode.\n\tThis library does not support netcdf-4 files.",
183 canon_path);
184 }
185 else {
186 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s", canon_path);
187 }
188 #endif
189 ex_err(__func__, errmsg, status);
190 free(canon_path);
191 EX_FUNC_LEAVE(EX_FATAL);
192 }
193
194 status = exi_populate_header(exoid, canon_path, my_mode, is_parallel, comp_ws, io_ws);
195 if (status != EX_NOERR) {
196 free(canon_path);
197 EX_FUNC_LEAVE(status);
198 }
199
200 free(canon_path);
201 EX_FUNC_LEAVE(exoid);
202 }