Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2025 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 nodal variable names
171 : */
172 : const std::vector<std::string> & get_nodal_var_names();
173 :
174 : /**
175 : * If we read in a nodal solution while reading in a mesh, we can attempt
176 : * to copy that nodal solution into an EquationSystems object.
177 : */
178 : void copy_nodal_solution(System & system,
179 : std::string system_var_name,
180 : std::string exodus_var_name,
181 : unsigned int timestep=1);
182 :
183 : /**
184 : * If we read in a elemental solution while reading in a mesh, we can attempt
185 : * to copy that elemental solution into an EquationSystems object.
186 : */
187 : void copy_elemental_solution(System & system,
188 : std::string system_var_name,
189 : std::string exodus_var_name,
190 : unsigned int timestep=1);
191 :
192 : /**
193 : * Copy global variables into scalar variables of a System object.
194 : */
195 : void copy_scalar_solution(System & system,
196 : std::vector<std::string> system_var_names,
197 : std::vector<std::string> exodus_var_names,
198 : unsigned int timestep=1);
199 :
200 : /**
201 : * Given a vector of global variables and a time step, returns the values
202 : * of the global variable at the corresponding time step index.
203 : * \param global_var_names Vector of names of global variables
204 : * \param timestep The corresponding time step index
205 : * \param global_values The vector to be filled
206 : */
207 : void read_global_variable(std::vector<std::string> global_var_names,
208 : unsigned int timestep,
209 : std::vector<Real> & global_values);
210 :
211 : /**
212 : * Set to true (the default) to write files in an HDF5-based file
213 : * format (when HDF5 is available), or to false to write files in
214 : * the old NetCDF3-based format. If HDF5 is unavailable, this
215 : * setting does nothing.
216 : */
217 : void set_hdf5_writing(bool write_hdf5);
218 :
219 : private:
220 :
221 : /*
222 : * A helper function for use in debug and devel modes, for asserting
223 : * that we get symmetric communication maps from a file we read
224 : * and/or that we're writing them out in a symmetric fashion
225 : * ourselves.
226 : */
227 : void assert_symmetric_cmaps();
228 :
229 : #if defined(LIBMESH_HAVE_EXODUS_API) && defined(LIBMESH_HAVE_NEMESIS_API)
230 : std::unique_ptr<Nemesis_IO_Helper> nemhelper;
231 :
232 : /**
233 : * Keeps track of the current timestep index being written. Used
234 : * when calling write_nodal_data() and other functions.
235 : */
236 : int _timestep;
237 : #endif
238 :
239 : /**
240 : * Controls whether extra debugging information is printed to the screen or not.
241 : */
242 : bool _verbose;
243 :
244 : /**
245 : * Default false. If true, files will be opened with EX_WRITE
246 : * rather than created from scratch when writing.
247 : */
248 : bool _append;
249 :
250 : /**
251 : * Helper function containing code shared between the two different
252 : * versions of write_nodal_data which take std::vector and
253 : * NumericVector, respectively.
254 : */
255 : void prepare_to_write_nodal_data (const std::string & fname,
256 : const std::vector<std::string> & names);
257 :
258 : /**
259 : * The names of the variables to be output.
260 : * If this is empty then all variables are output.
261 : */
262 : std::vector<std::string> _output_variables;
263 :
264 : /**
265 : * If true, _output_variables is allowed to remain empty.
266 : * If false, if _output_variables is empty it will be populated with a complete list of all variables
267 : * By default, calling set_output_variables() sets this flag to true, but it provides an override.
268 : */
269 : bool _allow_empty_variables;
270 : };
271 :
272 :
273 : } // namespace libMesh
274 :
275 :
276 : #endif // LIBMESH_NEMESIS_IO_H
|