Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 :
4 : // This library is free software; you can redistribute it and/or
5 : // modify it under the terms of the GNU Lesser General Public
6 : // License as published by the Free Software Foundation; either
7 : // version 2.1 of the License, or (at your option) any later version.
8 :
9 : // This library is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : // Lesser General Public License for more details.
13 :
14 : // You should have received a copy of the GNU Lesser General Public
15 : // License along with this library; if not, write to the Free Software
16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 :
18 :
19 :
20 : #ifndef LIBMESH_NEMESIS_IO_H
21 : #define LIBMESH_NEMESIS_IO_H
22 :
23 :
24 : // Local includes
25 : #include "libmesh/libmesh_common.h"
26 : #include "libmesh/mesh_input.h"
27 : #include "libmesh/mesh_output.h"
28 : #include "libmesh/parallel_object.h"
29 :
30 : // C++ includes
31 :
32 : namespace libMesh
33 : {
34 :
35 : // Forward declarations
36 : class Nemesis_IO_Helper;
37 : class System;
38 :
39 : /**
40 : * The \p Nemesis_IO class implements reading parallel meshes in the
41 : * \p Nemesis file format from Sandia National Labs. Nemesis files
42 : * are essentially in the Exodus format plus some additional information.
43 : * All the Nemesis files for a single mesh have the same basename, e.g.
44 : * cylinder.e, followed by ".size.rank", where size is the total number
45 : * of files the Mesh is split into and rank is the ID of the processor's
46 : * elements that were written to the file.
47 : *
48 : * \author John Peterson
49 : * \date 2008
50 : */
51 500 : class Nemesis_IO : public MeshInput<MeshBase>,
52 : public MeshOutput<MeshBase>,
53 : public ParallelObject
54 : {
55 :
56 : public:
57 :
58 : /**
59 : * Constructor. Takes a writable reference to a mesh object.
60 : * This is the constructor required to read a mesh.
61 : */
62 : explicit
63 : Nemesis_IO (MeshBase & mesh, bool single_precision=false);
64 :
65 : /**
66 : * Constructor. Takes a const reference to a mesh object.
67 : * This constructor is acceptable to write meshes.
68 : */
69 : explicit
70 : Nemesis_IO (const MeshBase & mesh, bool single_precision=false);
71 :
72 : /**
73 : * Destructor.
74 : */
75 : virtual ~Nemesis_IO ();
76 :
77 : /**
78 : * Implements reading the mesh from several different files.
79 : * You provide the basename, then LibMesh appends the ".size.rank"
80 : * depending on this->n_processors() and this->processor_id().
81 : */
82 : virtual void read (const std::string & base_filename) override;
83 :
84 : /**
85 : * This method implements writing a mesh to a specified file.
86 : */
87 : virtual void write (const std::string & base_filename) override;
88 :
89 : /**
90 : * Write one timestep's worth of the solution.
91 : */
92 : void write_timestep (const std::string & fname,
93 : const EquationSystems & es,
94 : const int timestep,
95 : const Real time);
96 :
97 : /**
98 : * Specify the list of variables which should be included in the
99 : * output (whitelist) If empty, then all variables will be present
100 : * in the output.
101 : *
102 : * This interface is copied from ExodusII_IO since it was found to
103 : * be useful there, but perhaps eventually these implementations
104 : * could somehow be combined.
105 : */
106 : void set_output_variables(const std::vector<std::string> & output_variables,
107 : bool allow_empty = true);
108 :
109 : /**
110 : * Output a nodal solution from data in \p soln
111 : */
112 : virtual void write_nodal_data (const std::string & fname,
113 : const std::vector<Number> & soln,
114 : const std::vector<std::string> & names) override;
115 :
116 : /**
117 : * Output a nodal solution from EquationSystems current_local_solutions
118 : */
119 : virtual void write_nodal_data (const std::string & fname,
120 : const EquationSystems & es,
121 : const std::set<std::string> * system_names) override;
122 :
123 : /**
124 : * Output a nodal solution in parallel, without localizing the soln vector.
125 : */
126 : virtual void write_nodal_data (const std::string & fname,
127 : const NumericVector<Number> & parallel_soln,
128 : const std::vector<std::string> & names) override;
129 :
130 : /**
131 : * Write out element solution in parallel, without localizing the solution vector.
132 : *
133 : * \note Unlike write_nodal_data(), this function is not virtual and
134 : * it does not override anything from the base class. This design is
135 : * similar to the function by the same name in ExodusII_IO.
136 : */
137 : void write_element_data (const EquationSystems & es);
138 :
139 : /**
140 : * Set the flag indicating if we should be verbose.
141 : */
142 : void verbose (bool set_verbosity);
143 :
144 : /**
145 : * Set the flag indicating whether the complex modulus should be
146 : * written when complex numbers are enabled. By default this flag
147 : * is set to true.
148 : */
149 : void write_complex_magnitude (bool val);
150 :
151 : /**
152 : * Write out global variables.
153 : */
154 : void write_global_data (const std::vector<Number> &,
155 : const std::vector<std::string> &);
156 :
157 : /**
158 : * Write out information records.
159 : */
160 : void write_information_records (const std::vector<std::string> &);
161 :
162 : /**
163 : * If true, this flag will cause the Nemesis_IO object to attempt to
164 : * open an existing file for writing, rather than creating a new file.
165 : * Obviously this will only work if the file already exists.
166 : */
167 : void append(bool val);
168 :
169 : /**
170 : * Return list of the elemental variable names
171 : */
172 : const std::vector<std::string> & get_elem_var_names();
173 :
174 : /**
175 : * Return list of the nodal variable names
176 : */
177 : const std::vector<std::string> & get_nodal_var_names();
178 :
179 : /**
180 : * Return list of the global variable names
181 : */
182 : const std::vector<std::string> & get_global_var_names();
183 :
184 : /**
185 : * \returns An array containing the timesteps in the file.
186 : */
187 : const std::vector<Real> & get_time_steps();
188 :
189 : /**
190 : * \returns The number of timesteps currently stored in the Exodus
191 : * file.
192 : *
193 : * Knowing the number of time steps currently stored in the file is
194 : * sometimes necessary when appending, so we can know where to start
195 : * writing new data. Throws an error if the file is not currently
196 : * open for reading or writing.
197 : */
198 : int get_num_time_steps();
199 :
200 : /**
201 : * If we read in a nodal solution while reading in a mesh, we can attempt
202 : * to copy that nodal solution into an EquationSystems object.
203 : */
204 : void copy_nodal_solution(System & system,
205 : std::string system_var_name,
206 : std::string exodus_var_name,
207 : unsigned int timestep=1);
208 :
209 : /**
210 : * If we read in a elemental solution while reading in a mesh, we can attempt
211 : * to copy that elemental solution into an EquationSystems object.
212 : */
213 : void copy_elemental_solution(System & system,
214 : std::string system_var_name,
215 : std::string exodus_var_name,
216 : unsigned int timestep=1);
217 :
218 : /**
219 : * Copy global variables into scalar variables of a System object.
220 : */
221 : void copy_scalar_solution(System & system,
222 : std::vector<std::string> system_var_names,
223 : std::vector<std::string> exodus_var_names,
224 : unsigned int timestep=1);
225 :
226 : /**
227 : * Given a vector of global variables and a time step, returns the values
228 : * of the global variable at the corresponding time step index.
229 : * \param global_var_names Vector of names of global variables
230 : * \param timestep The corresponding time step index
231 : * \param global_values The vector to be filled
232 : */
233 : void read_global_variable(std::vector<std::string> global_var_names,
234 : unsigned int timestep,
235 : std::vector<Real> & global_values);
236 :
237 : /**
238 : * Set to true (the default) to write files in an HDF5-based file
239 : * format (when HDF5 is available), or to false to write files in
240 : * the old NetCDF3-based format. If HDF5 is unavailable, this
241 : * setting does nothing.
242 : */
243 : void set_hdf5_writing(bool write_hdf5);
244 :
245 : #ifdef LIBMESH_HAVE_NEMESIS_API
246 : /**
247 : * Return a reference to the Nemesis_IO_Helper object.
248 : */
249 : Nemesis_IO_Helper & get_nemio_helper();
250 : #endif
251 :
252 : private:
253 :
254 : /*
255 : * A helper function for use in debug and devel modes, for asserting
256 : * that we get symmetric communication maps from a file we read
257 : * and/or that we're writing them out in a symmetric fashion
258 : * ourselves.
259 : */
260 : void assert_symmetric_cmaps();
261 :
262 : #if defined(LIBMESH_HAVE_EXODUS_API) && defined(LIBMESH_HAVE_NEMESIS_API)
263 : std::unique_ptr<Nemesis_IO_Helper> nemhelper;
264 :
265 : /**
266 : * Keeps track of the current timestep index being written. Used
267 : * when calling write_nodal_data() and other functions.
268 : */
269 : int _timestep;
270 : #endif
271 :
272 : /**
273 : * Controls whether extra debugging information is printed to the screen or not.
274 : */
275 : bool _verbose;
276 :
277 : /**
278 : * Default false. If true, files will be opened with EX_WRITE
279 : * rather than created from scratch when writing.
280 : */
281 : bool _append;
282 :
283 : /**
284 : * Helper function containing code shared between the two different
285 : * versions of write_nodal_data which take std::vector and
286 : * NumericVector, respectively.
287 : */
288 : void prepare_to_write_nodal_data (const std::string & fname,
289 : const std::vector<std::string> & names);
290 :
291 : /**
292 : * The names of the variables to be output.
293 : * If this is empty then all variables are output.
294 : */
295 : std::vector<std::string> _output_variables;
296 :
297 : /**
298 : * If true, _output_variables is allowed to remain empty.
299 : * If false, if _output_variables is empty it will be populated with a complete list of all variables
300 : * By default, calling set_output_variables() sets this flag to true, but it provides an override.
301 : */
302 : bool _allow_empty_variables;
303 : };
304 :
305 :
306 : } // namespace libMesh
307 :
308 :
309 : #endif // LIBMESH_NEMESIS_IO_H
|