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_MESH_OUTPUT_H
21 : #define LIBMESH_MESH_OUTPUT_H
22 :
23 :
24 : // Local includes
25 : #include "libmesh/libmesh_common.h"
26 : #include "libmesh/libmesh_logging.h"
27 : #include "libmesh/mesh_base.h"
28 : #include "libmesh/mesh_serializer.h"
29 :
30 : // C++ includes
31 : #include <cstddef>
32 : #include <limits>
33 : #include <string>
34 : #include <vector>
35 :
36 : namespace libMesh
37 : {
38 :
39 : // Forward declares
40 : class EquationSystems;
41 : template <typename T> class NumericVector;
42 :
43 :
44 : /**
45 : * This class defines an abstract interface for \p Mesh output.
46 : * Specific classes derived from this class actually implement
47 : * writing various mesh formats.
48 : *
49 : * \author Benjamin S. Kirk
50 : * \date 2004
51 : */
52 : template <class MT>
53 : class MeshOutput
54 : {
55 : protected:
56 :
57 : /**
58 : * Default constructor. Will set the _obj to nullptr, effectively
59 : * rendering this object useless.
60 : */
61 : explicit
62 : MeshOutput (const bool is_parallel_format = false, const bool serial_only_needed_on_proc_0 = false);
63 :
64 : /**
65 : * Constructor. Takes a reference to a constant object.
66 : * This constructor will only allow us to write the object.
67 : */
68 : explicit
69 : MeshOutput (const MT &, const bool is_parallel_format = false, const bool serial_only_needed_on_proc_0 = false);
70 :
71 :
72 : public:
73 :
74 : /**
75 : * Destructor.
76 : */
77 : virtual ~MeshOutput ();
78 :
79 : /**
80 : * This method implements writing a mesh to a specified file.
81 : *
82 : * Note that writes may be buffered for efficiency, and so may not
83 : * reach disk until after the file has been closed, which happens
84 : * when the \p MeshOutput object is destructed.
85 : */
86 : virtual void write (const std::string &) = 0;
87 :
88 : /**
89 : * This method implements writing a mesh with data to a specified file
90 : * where the data is taken from the \p EquationSystems object.
91 : */
92 : virtual void write_equation_systems (const std::string &,
93 : const EquationSystems &,
94 : const std::set<std::string> * system_names=nullptr);
95 :
96 : /**
97 : * This method implements writing a mesh with discontinuous data to a
98 : * specified file where the data is taken from the \p EquationSystems
99 : * object.
100 : */
101 : virtual void write_discontinuous_equation_systems (const std::string &,
102 : const EquationSystems &,
103 : const std::set<std::string> * system_names=nullptr);
104 :
105 : /**
106 : * This method implements writing a mesh with nodal data to a
107 : * specified file where the nodal data and variable names are provided.
108 : */
109 0 : virtual void write_nodal_data (const std::string &,
110 : const std::vector<Number> &,
111 : const std::vector<std::string> &)
112 0 : { libmesh_not_implemented(); }
113 :
114 : /**
115 : * This method implements writing a mesh with discontinuous data to a
116 : * specified file where the nodal data and variables names are provided.
117 : */
118 0 : virtual void write_nodal_data_discontinuous (const std::string &,
119 : const std::vector<Number> &,
120 : const std::vector<std::string> &)
121 0 : { libmesh_not_implemented(); }
122 :
123 : /**
124 : * This method may be overridden by "parallel" output formats for
125 : * writing nodal data. Instead of getting a localized copy of the
126 : * nodal solution vector, it is passed a NumericVector of
127 : * type=PARALLEL which is in node-major order i.e.
128 : * (u0,v0,w0, u1,v1,w1, u2,v2,w2, u3,v3,w3, ...)
129 : * and contains n_nodes*n_vars total entries. Then, it is up to the
130 : * individual I/O class to extract the required solution values from
131 : * this vector and write them in parallel.
132 : *
133 : * If not implemented, localizes the parallel vector into a std::vector
134 : * and calls the other version of this function.
135 : */
136 : virtual void write_nodal_data (const std::string &,
137 : const NumericVector<Number> &,
138 : const std::vector<std::string> &);
139 :
140 : /**
141 : * This method should be overridden by "parallel" output formats for
142 : * writing nodal data. Instead of getting a localized copy of the
143 : * nodal solution vector, it directly uses EquationSystems
144 : * current_local_solution vectors to look up nodal values.
145 : *
146 : * If not implemented, reorders the solutions into a nodal-only
147 : * NumericVector and calls the above version of this function.
148 : */
149 : virtual void write_nodal_data (const std::string &,
150 : const EquationSystems &,
151 : const std::set<std::string> *);
152 :
153 : /**
154 : * Return/set the precision to use when writing ASCII files.
155 : *
156 : * By default we use numeric_limits<Real>::max_digits10, which
157 : * should be enough to write out to ASCII and get the exact same
158 : * Real back when reading in.
159 : */
160 : unsigned int & ascii_precision ();
161 :
162 : protected:
163 :
164 :
165 : /**
166 : * \returns The object as a read-only reference.
167 : */
168 : const MT & mesh() const;
169 :
170 :
171 : /**
172 : * \returns Whether or not added sides are expected to be output,
173 : * to plot SIDE_DISCONTINUOUS data. Subclasses should override this
174 : * if they are capable of plotting such data.
175 : */
176 31221 : virtual bool get_add_sides() { return false; }
177 :
178 :
179 : /**
180 : * Flag specifying whether this format is parallel-capable.
181 : * If this is false (default) I/O is only permitted when the mesh
182 : * has been serialized.
183 : */
184 : const bool _is_parallel_format;
185 :
186 : /**
187 : * Flag specifying whether this format can be written by only
188 : * serializing the mesh to processor zero
189 : *
190 : * If this is false (default) the mesh will be serialized to
191 : * all processors
192 : */
193 : const bool _serial_only_needed_on_proc_0;
194 :
195 : private:
196 :
197 :
198 : /**
199 : * A pointer to a constant object.
200 : * This allows us to write the object to file.
201 : */
202 : const MT * const _obj;
203 :
204 : /**
205 : * Precision to use when writing ASCII files.
206 : */
207 : unsigned int _ascii_precision;
208 : };
209 :
210 :
211 :
212 :
213 :
214 :
215 : // ------------------------------------------------------------
216 : // MeshOutput inline members
217 : template <class MT>
218 : inline
219 0 : MeshOutput<MT>::MeshOutput (const bool is_parallel_format, const bool serial_only_needed_on_proc_0) :
220 0 : _is_parallel_format(is_parallel_format),
221 0 : _serial_only_needed_on_proc_0(serial_only_needed_on_proc_0),
222 0 : _obj(nullptr),
223 0 : _ascii_precision (std::numeric_limits<Real>::max_digits10)
224 0 : {}
225 :
226 :
227 :
228 : template <class MT>
229 : inline
230 89223 : MeshOutput<MT>::MeshOutput (const MT & obj, const bool is_parallel_format, const bool serial_only_needed_on_proc_0) :
231 82076 : _is_parallel_format(is_parallel_format),
232 82076 : _serial_only_needed_on_proc_0(serial_only_needed_on_proc_0),
233 82076 : _obj (&obj),
234 89223 : _ascii_precision (std::numeric_limits<Real>::max_digits10)
235 : {
236 89223 : if (!_is_parallel_format && !this->mesh().is_serial())
237 : {
238 48762 : if (this->mesh().processor_id() == 0)
239 : {
240 6113 : libmesh_do_once(libMesh::out <<
241 : "Warning: This MeshOutput subclass only supports meshes which have been serialized!"
242 : << std::endl;);
243 : }
244 : }
245 89223 : }
246 :
247 :
248 :
249 : template <class MT>
250 : inline
251 1891 : MeshOutput<MT>::~MeshOutput ()
252 : {
253 52063 : }
254 :
255 :
256 :
257 : template <class MT>
258 : inline
259 12563 : const MT & MeshOutput<MT>::mesh () const
260 : {
261 12563 : libmesh_assert(_obj);
262 291850 : return *_obj;
263 : }
264 :
265 :
266 :
267 : template <class MT>
268 : inline
269 9551 : unsigned int & MeshOutput<MT>::ascii_precision ()
270 : {
271 9551 : return _ascii_precision;
272 : }
273 :
274 :
275 : } // namespace libMesh
276 :
277 :
278 : #endif // LIBMESH_MESH_OUTPUT_H
|