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 : // Local includes
20 : #include "libmesh/exodusII_io.h"
21 :
22 : #include "libmesh/boundary_info.h"
23 : #include "libmesh/cell_c0polyhedron.h"
24 : #include "libmesh/dof_map.h"
25 : #include "libmesh/dyna_io.h" // ElementDefinition for BEX
26 : #include "libmesh/enum_elem_type.h"
27 : #include "libmesh/elem.h"
28 : #include "libmesh/enum_to_string.h"
29 : #include "libmesh/equation_systems.h"
30 : #include "libmesh/exodusII_io_helper.h"
31 : #include "libmesh/face_c0polygon.h"
32 : #include "libmesh/fpe_disabler.h"
33 : #include "libmesh/int_range.h"
34 : #include "libmesh/libmesh_logging.h"
35 : #include "libmesh/mesh_base.h"
36 : #include "libmesh/mesh_communication.h"
37 : #include "libmesh/numeric_vector.h"
38 : #include "libmesh/parallel_mesh.h"
39 : #include "libmesh/parallel.h"
40 : #include "libmesh/system.h"
41 : #include "libmesh/utility.h"
42 :
43 : // TIMPI includes
44 : #include "timpi/parallel_sync.h"
45 :
46 : // C++ includes
47 : #include <cmath> // llround
48 : #include <cstring>
49 : #include <fstream>
50 : #include <map>
51 : #include <memory>
52 : #include <sstream>
53 :
54 : #ifdef LIBMESH_HAVE_EXODUS_API
55 : namespace
56 : {
57 : using namespace libMesh;
58 :
59 34608 : const std::vector<Real> & bex_constraint_vec(std::size_t i,
60 : const ExodusII_IO_Helper & helper)
61 : {
62 0 : std::size_t vec_offset = 0;
63 34608 : for (auto block_num : index_range(helper.bex_dense_constraint_vecs))
64 : {
65 0 : const auto & vecblock = helper.bex_dense_constraint_vecs[block_num];
66 0 : libmesh_assert_greater_equal(i, vec_offset);
67 34608 : if (i - vec_offset < vecblock.size())
68 34608 : return vecblock[i - vec_offset];
69 :
70 0 : vec_offset += vecblock.size();
71 : }
72 :
73 0 : libmesh_error_msg("Requested BEX coefficient vector " << i << " not found");
74 : }
75 :
76 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
77 : enum class SolutionOrdering
78 : {
79 : variable_major,
80 : geometry_major
81 : };
82 :
83 : std::size_t ordered_solution_index (const std::size_t var,
84 : const std::size_t geometry_index,
85 : const std::size_t num_vars,
86 : const std::size_t num_geometric_entries,
87 : const SolutionOrdering ordering)
88 : {
89 126 : if (ordering == SolutionOrdering::variable_major)
90 252 : return var * num_geometric_entries + geometry_index;
91 : else
92 0 : return geometry_index * num_vars + var;
93 : }
94 :
95 : std::vector<Real>
96 3 : complex_soln_components (const std::vector<Number> & soln,
97 : const std::size_t num_vars,
98 : const bool write_complex_abs,
99 : const SolutionOrdering source_ordering,
100 : const SolutionOrdering destination_ordering)
101 : {
102 : const std::size_t num_values = soln.size();
103 : libmesh_assert_greater(num_vars, 0);
104 : libmesh_assert_equal_to(num_values % num_vars, 0);
105 3 : const std::size_t num_geometric_entries = num_values / num_vars;
106 :
107 : // This will contain the real and imaginary parts and the magnitude
108 : // of the values in soln
109 3 : const std::size_t nco = write_complex_abs ? 3 : 2;
110 3 : const std::size_t num_complex_vars = nco * num_vars;
111 3 : std::vector<Real> complex_soln(nco * num_values);
112 :
113 10 : for (const auto var : make_range(num_vars))
114 70 : for (const auto geometry_index : make_range(num_geometric_entries))
115 : {
116 : const Number value =
117 : soln[ordered_solution_index
118 63 : (var, geometry_index, num_vars, num_geometric_entries, source_ordering)];
119 :
120 63 : const std::size_t complex_var = nco * var;
121 : complex_soln[ordered_solution_index
122 : (complex_var, geometry_index, num_complex_vars,
123 63 : num_geometric_entries, destination_ordering)] = value.real();
124 : complex_soln[ordered_solution_index
125 : (complex_var + 1, geometry_index, num_complex_vars,
126 63 : num_geometric_entries, destination_ordering)] = value.imag();
127 :
128 63 : if (write_complex_abs)
129 : complex_soln[ordered_solution_index
130 : (complex_var + 2, geometry_index, num_complex_vars,
131 126 : num_geometric_entries, destination_ordering)] = std::abs(value);
132 : }
133 :
134 3 : return complex_soln;
135 : }
136 : #endif // LIBMESH_USE_COMPLEX_NUMBERS
137 :
138 : }
139 : #endif
140 :
141 : namespace libMesh
142 : {
143 :
144 : // ------------------------------------------------------------
145 : // ExodusII_IO class members
146 31002 : ExodusII_IO::ExodusII_IO (MeshBase & mesh,
147 31002 : bool single_precision) :
148 : MeshInput<MeshBase> (mesh),
149 : MeshOutput<MeshBase> (mesh,
150 : /* is_parallel_format = */ false,
151 : /* serial_only_needed_on_proc_0 = */ true),
152 : ParallelObject(mesh),
153 : #ifdef LIBMESH_HAVE_EXODUS_API
154 0 : exio_helper(std::make_unique<ExodusII_IO_Helper>(*this, false, true, single_precision)),
155 29158 : _timestep(1),
156 29158 : _verbose(false),
157 29158 : _append(false),
158 : #endif
159 29158 : _allow_empty_variables(false),
160 29158 : _write_complex_abs(true),
161 29158 : _set_unique_ids_from_maps(false),
162 29158 : _disc_bex(false),
163 31002 : _allow_hi_order_elemental(false)
164 : {
165 : // if !LIBMESH_HAVE_EXODUS_API, we didn't use this
166 922 : libmesh_ignore(single_precision);
167 31002 : }
168 :
169 :
170 :
171 79 : ExodusII_IO::ExodusII_IO (const MeshBase & mesh,
172 79 : bool single_precision) :
173 : MeshInput<MeshBase> (),
174 : MeshOutput<MeshBase> (mesh,
175 : /* is_parallel_format = */ false,
176 : /* serial_only_needed_on_proc_0 = */ true),
177 : ParallelObject(mesh),
178 : #ifdef LIBMESH_HAVE_EXODUS_API
179 0 : exio_helper(std::make_unique<ExodusII_IO_Helper>(*this, false, true, single_precision)),
180 67 : _timestep(1),
181 67 : _verbose(false),
182 67 : _append(false),
183 : #endif
184 67 : _allow_empty_variables(false),
185 67 : _write_complex_abs(true),
186 67 : _set_unique_ids_from_maps(false),
187 67 : _disc_bex(false),
188 79 : _allow_hi_order_elemental(false)
189 : {
190 : // if !LIBMESH_HAVE_EXODUS_API, we didn't use this
191 6 : libmesh_ignore(single_precision);
192 79 : }
193 :
194 :
195 :
196 639 : int ExodusII_IO::get_exodus_version()
197 : {
198 : #ifdef LIBMESH_HAVE_EXODUS_API
199 639 : return ExodusII_IO_Helper::get_exodus_version();
200 : #else
201 : return 0;
202 : #endif
203 : }
204 :
205 :
206 :
207 142 : void ExodusII_IO::set_extra_integer_vars(const std::vector<std::string> & extra_integer_vars)
208 : {
209 142 : _extra_integer_vars = extra_integer_vars;
210 142 : }
211 :
212 70 : void ExodusII_IO::set_output_variables(const std::vector<std::string> & output_variables,
213 : bool allow_empty)
214 : {
215 70 : _output_variables = output_variables;
216 70 : _allow_empty_variables = allow_empty;
217 70 : }
218 :
219 :
220 :
221 284 : void ExodusII_IO::write_discontinuous_exodusII(const std::string & name,
222 : const EquationSystems & es,
223 : const std::set<std::string> * system_names)
224 : {
225 24 : std::vector<std::string> solution_names;
226 16 : std::vector<Number> v;
227 :
228 284 : es.build_variable_names (solution_names, nullptr, system_names);
229 284 : es.build_discontinuous_solution_vector (v, system_names,
230 : nullptr, false, /* defaults */
231 284 : this->get_add_sides());
232 284 : this->write_nodal_data_discontinuous(name, v, solution_names);
233 284 : }
234 :
235 :
236 : #ifdef LIBMESH_HAVE_EXODUS_API
237 0 : void ExodusII_IO::write_timestep_discontinuous (const std::string &fname,
238 : const EquationSystems &es,
239 : const int timestep,
240 : const Real time,
241 : const std::set<std::string> * system_names)
242 : {
243 0 : _timestep = timestep;
244 0 : write_discontinuous_equation_systems (fname,es,system_names);
245 :
246 0 : if (MeshOutput<MeshBase>::mesh().processor_id())
247 0 : return;
248 :
249 0 : exio_helper->write_timestep(timestep, time);
250 : }
251 :
252 : #else
253 : void ExodusII_IO::write_timestep_discontinuous (const std::string & /* fname */,
254 : const EquationSystems & /* es */,
255 : const int /* timestep */,
256 : const Real /* time */,
257 : const std::set<std::string> * /*system_names*/)
258 : { libmesh_error(); }
259 : #endif
260 :
261 :
262 : // ------------------------------------------------------------
263 : // When the Exodus API is present...
264 : #ifdef LIBMESH_HAVE_EXODUS_API
265 :
266 31361 : ExodusII_IO::~ExodusII_IO ()
267 : {
268 31081 : exio_helper->close();
269 31361 : }
270 :
271 :
272 2422 : void ExodusII_IO::read (const std::string & fname)
273 : {
274 441 : LOG_SCOPE("read()", "ExodusII_IO");
275 :
276 : // Get a reference to the mesh we are reading
277 2422 : MeshBase & mesh = MeshInput<MeshBase>::mesh();
278 :
279 : // Add extra integers into the mesh
280 294 : std::vector<unsigned int> extra_ids;
281 2564 : for (auto & name : _extra_integer_vars)
282 278 : extra_ids.push_back(mesh.add_elem_integer(name));
283 :
284 : // Clear any existing mesh data
285 2422 : mesh.clear();
286 :
287 : // Keep track of what kinds of elements this file contains
288 2422 : elems_of_dimension.clear();
289 2422 : elems_of_dimension.resize(4, false);
290 :
291 : // Open the exodus file in EX_READ mode
292 2422 : exio_helper->open(fname.c_str(), /*read_only=*/true);
293 :
294 : // Get header information from exodus file
295 2422 : exio_helper->read_and_store_header_info();
296 :
297 : // Read the QA records
298 2422 : exio_helper->read_qa_records();
299 :
300 : // Print header information
301 2422 : exio_helper->print_header();
302 :
303 : // Read nodes from the exodus file
304 2422 : exio_helper->read_nodes();
305 :
306 : // Reserve space for the nodes.
307 2569 : mesh.reserve_nodes(exio_helper->num_nodes);
308 :
309 : // Read the node number map from the Exodus file. This is
310 : // required if we want to preserve the numbering of nodes as it
311 : // exists in the Exodus file. If the Exodus file does not contain
312 : // a node_num_map, the identity map is returned by this call.
313 2422 : exio_helper->read_node_num_map();
314 :
315 : // Read the element number map from the Exodus file. This is
316 : // required if we want to preserve the numbering of elements as it
317 : // exists in the Exodus file. If the Exodus file does not contain
318 : // an elem_num_map, the identity map is returned by this call.
319 : //
320 : // We now do this before creating nodes, so if we have any spline
321 : // nodes that need a NodeElem attached we can give them unused elem
322 : // ids.
323 2422 : exio_helper->read_elem_num_map();
324 :
325 : // Read any Bezier Extraction coefficient vectors from the file,
326 : // such as might occur in an IsoGeometric Analysis (IGA) mesh.
327 2422 : exio_helper->read_bex_cv_blocks();
328 :
329 : // If we have Rational Bezier weights, we'll need to
330 : // store them.
331 147 : unsigned char weight_index = 0;
332 147 : const bool weights_exist = !exio_helper->w.empty();
333 :
334 : // If we have Bezier extraction coefficients, we'll need to put
335 : // NODEELEM elements on spline nodes, since our Rational Bezier
336 : // elements will be connected to nodes derived from those; nothing
337 : // else will be directly connected to the spline nodes.
338 147 : const bool bex_cv_exist = !exio_helper->bex_dense_constraint_vecs.empty();
339 :
340 : // If the user requests to set Node/Elem unique_ids based on the
341 : // node/elem_num_maps, then it is very likely that those unique_ids
342 : // will not be "unique" across the entire set of DofObjects (Nodes
343 : // and Elems), although they should of course still be unique within
344 : // the set of Nodes and the set of Elems on an individual basis. We
345 : // therefore need to relax some of the uniqueness checking that is
346 : // done in debug mode by setting the following flag on the Mesh.
347 2422 : if (_set_unique_ids_from_maps)
348 4 : mesh.allow_node_and_elem_unique_id_overlap(true);
349 :
350 : // Even if weights don't exist, we still use RATIONAL_BERNSTEIN for
351 : // Bezier-Bernstein BEX elements, we just use 1.0 as the weight on
352 : // every node.
353 2422 : if (bex_cv_exist)
354 : {
355 9 : const Real default_weight = 1.0;
356 : weight_index = cast_int<unsigned char>
357 85 : (mesh.add_node_datum<Real>("rational_weight", true,
358 : &default_weight));
359 0 : mesh.set_default_mapping_type(RATIONAL_BERNSTEIN_MAP);
360 0 : mesh.set_default_mapping_data(weight_index);
361 : }
362 :
363 294 : std::unordered_map<const Node *, Elem *> spline_nodeelem_ptrs;
364 :
365 : // Loop over the nodes, create Nodes with local processor_id 0.
366 327841 : for (auto i : make_range(exio_helper->num_nodes))
367 : {
368 : // Determine the libmesh node id implied by "i". The
369 : // get_libmesh_node_id() helper function expects a 1-based
370 : // Exodus node id, so we construct the "implied" Exodus node id
371 : // from "i" by adding 1.
372 325419 : auto libmesh_node_id = exio_helper->get_libmesh_node_id(/*exodus_node_id=*/i+1);
373 :
374 : // Catch the node that was added to the mesh
375 451699 : Node * added_node = mesh.add_point (Point(exio_helper->x[i], exio_helper->y[i], exio_helper->z[i]), libmesh_node_id);
376 :
377 : // Sanity check: throw an error if the Mesh assigned an ID to
378 : // the Node which does not match the libmesh_id we just determined.
379 325419 : libmesh_error_msg_if(added_node->id() != static_cast<unsigned>(libmesh_node_id),
380 : "Error! Mesh assigned node ID "
381 : << added_node->id()
382 : << " which is different from the (zero-based) Exodus ID "
383 : << libmesh_node_id
384 : << "!");
385 :
386 : // If the _set_unique_ids_from_maps flag is true, set the
387 : // unique_id for "node", otherwise do nothing.
388 325419 : exio_helper->conditionally_set_node_unique_id(mesh, added_node, i);
389 :
390 : // If we have a set of spline weights, these nodes are going to
391 : // be used as control points for Bezier elements, and we need
392 : // to attach a NodeElem to each to make sure it doesn't get
393 : // flagged as an unused node.
394 325419 : if (weights_exist)
395 : {
396 162 : const auto w = exio_helper->w[i];
397 162 : Point & p = *added_node;
398 0 : p /= w; // Exodus Bezier Extraction stores spline nodes in projective space
399 :
400 162 : added_node->set_extra_datum<Real>(weight_index, exio_helper->w[i]);
401 : }
402 :
403 325419 : if (bex_cv_exist)
404 : {
405 357 : std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
406 :
407 : // Give the NodeElem ids at the end, so we can match any
408 : // existing ids in the file for other elements
409 : //
410 : // We don't set the unique_id for this NodeElem here even if
411 : // the user has set the _set_unique_ids_from_maps flag
412 : // because these NodeElems don't have entries in the
413 : // elem_num_map. Therefore, we just let the Mesh assign
414 : // whatever unique_id is "next" as the Elem is added to the
415 : // Mesh.
416 357 : elem->set_id() = exio_helper->end_elem_id() + i;
417 :
418 357 : elem->set_node(0, added_node);
419 357 : Elem * added_elem = mesh.add_elem(std::move(elem));
420 357 : spline_nodeelem_ptrs[added_node] = added_elem;
421 357 : }
422 : }
423 :
424 : // This assert is no longer valid if the nodes are not numbered
425 : // sequentially starting from 1 in the Exodus file.
426 : // libmesh_assert_equal_to (static_cast<unsigned int>(exio_helper->num_nodes), mesh.n_nodes());
427 :
428 : // Get information about all the element and edge blocks
429 2422 : exio_helper->read_block_info();
430 :
431 : // Reserve space for the elements. Account for any NodeElem that
432 : // have already been attached to spline control nodes.
433 2716 : mesh.reserve_elem(exio_helper->w.size() + exio_helper->num_elem);
434 :
435 : // Read variables for extra integer IDs
436 2930 : std::vector<std::map<dof_id_type, Real>> elem_ids(extra_ids.size());
437 : // We use the last time step to load the IDs
438 2422 : exio_helper->read_num_time_steps();
439 2422 : unsigned int last_step = exio_helper->num_time_steps;
440 2564 : for (auto i : index_range(extra_ids))
441 286 : exio_helper->read_elemental_var_values(_extra_integer_vars[i], last_step, elem_ids[i]);
442 :
443 : // Read in the element connectivity for each block.
444 147 : int nelem_last_block = 0;
445 :
446 : // If we're building Bezier elements from spline nodes, we need to
447 : // calculate those elements' local nodes on the fly, and we'll be
448 : // calculating them from constraint matrix columns, and we'll need
449 : // to make sure that the same node is found each time it's
450 : // calculated from multiple neighboring elements.
451 294 : std::map<std::vector<std::pair<dof_id_type, Real>>, Node *> local_nodes;
452 :
453 : // We'll set any spline NodeElem subdomain_id() values to exceed the
454 : // maximum of subdomain_id() values set via Exodus block ids.
455 2422 : subdomain_id_type max_subdomain_id = std::numeric_limits<subdomain_id_type>::min();
456 :
457 : // We've already added all the nodes explicitly specified in the
458 : // file, but if we have spline nodes we may need to add assembly
459 : // element nodes based on them. Add them contiguously so we're
460 : // compatible with any subsequent code paths (including our
461 : // ExodusII_IO::write()!) that don't support sparse ids.
462 2422 : dof_id_type n_nodes = mesh.n_nodes();
463 :
464 : // Loop over all the element blocks
465 9564 : for (int i=0; i<exio_helper->num_elem_blk; i++)
466 : {
467 : // Read the information for block i
468 7213 : exio_helper->read_elem_in_block (i);
469 : const subdomain_id_type subdomain_id =
470 7213 : restrict_int<subdomain_id_type>(exio_helper->get_block_id(i));
471 7213 : max_subdomain_id = std::max(max_subdomain_id, subdomain_id);
472 :
473 : // populate the map of names
474 7759 : std::string subdomain_name = exio_helper->get_block_name(i);
475 7213 : if (!subdomain_name.empty())
476 5428 : mesh.set_subdomain_name(subdomain_id, subdomain_name);
477 :
478 : // Set any relevant node/edge maps for this element
479 7282 : const std::string type_str (exio_helper->get_elem_type());
480 7282 : const auto & conv = exio_helper->get_conversion(type_str);
481 7213 : const bool is_c0polygon = (conv.libmesh_elem_type() == C0POLYGON);
482 7213 : const bool is_c0polyhedron = (conv.libmesh_elem_type() == C0POLYHEDRON);
483 546 : std::size_t c0polygon_connect_offset = 0;
484 546 : std::size_t c0polyhedron_connect_offset = 0;
485 :
486 : // Loop over all the faces in this block
487 7213 : int jmax = nelem_last_block+exio_helper->num_elem_this_blk;
488 311754 : for (int j=nelem_last_block; j<jmax; j++)
489 : {
490 304612 : const int elem_num = j - nelem_last_block;
491 279734 : std::unique_ptr<Elem> uelem;
492 :
493 304612 : if (is_c0polygon)
494 : {
495 12 : const int n_elem_nodes = exio_helper->elem_node_counts[elem_num];
496 12 : libmesh_error_msg_if(n_elem_nodes < 3,
497 : "Error: Exodus NSIDED block element "
498 : << elem_num
499 : << " has only " << n_elem_nodes << " nodes.");
500 :
501 13 : uelem = std::make_unique<C0Polygon>(cast_int<unsigned int>(n_elem_nodes));
502 : }
503 304600 : else if (is_c0polyhedron)
504 : {
505 12 : const int n_faces = exio_helper->elem_face_counts[elem_num];
506 12 : libmesh_error_msg_if(n_faces < 4,
507 : "Error: Exodus NFACED block element "
508 : << elem_num
509 : << " has only " << n_faces << " faces.");
510 :
511 : std::vector<std::shared_ptr<Polygon>>
512 14 : sides(cast_int<std::size_t>(n_faces));
513 :
514 108 : for (auto s : index_range(sides))
515 : {
516 8 : libmesh_assert_less(c0polyhedron_connect_offset,
517 : exio_helper->connect.size());
518 : const int exodus_face_id =
519 96 : exio_helper->connect[c0polyhedron_connect_offset++];
520 :
521 96 : libmesh_error_msg_if
522 : (exodus_face_id <= 0 ||
523 : exodus_face_id >
524 : cast_int<int>(exio_helper->c0polyhedron_face_connect.size()),
525 : "Error: Exodus NFACED block element "
526 : << elem_num
527 : << " references face ID " << exodus_face_id
528 : << ", but the file contains "
529 : << exio_helper->c0polyhedron_face_connect.size()
530 : << " faces.");
531 :
532 : const auto & face_nodes =
533 96 : exio_helper->c0polyhedron_face_connect[exodus_face_id - 1];
534 : auto side = std::make_shared<C0Polygon>
535 112 : (cast_int<unsigned int>(face_nodes.size()));
536 :
537 528 : for (auto n : index_range(face_nodes))
538 : {
539 : const auto libmesh_node_id =
540 468 : exio_helper->get_libmesh_node_id(face_nodes[n]);
541 432 : side->set_node(n, mesh.node_ptr(libmesh_node_id));
542 : }
543 :
544 16 : sides[s] = std::move(side);
545 : }
546 :
547 12 : std::unique_ptr<Node> mid_elem_node;
548 13 : uelem = std::make_unique<C0Polyhedron>(sides, mid_elem_node);
549 12 : if (mid_elem_node)
550 : {
551 13 : Node * added_node = mesh.add_node(std::move(mid_elem_node));
552 12 : if (added_node->id() >= n_nodes)
553 12 : n_nodes = added_node->id() + 1;
554 : }
555 10 : }
556 : else
557 584300 : uelem = Elem::build(conv.libmesh_elem_type());
558 :
559 : // Make sure that Exodus's number of nodes per Elem matches
560 : // the number of Nodes for this type of Elem. We only check
561 : // this for the first Elem in each block, since these values
562 : // are the same for every Elem in the block.
563 304612 : if (!is_c0polygon && !is_c0polyhedron && !elem_num)
564 7189 : libmesh_error_msg_if(exio_helper->num_nodes_per_elem != static_cast<int>(uelem->n_nodes()),
565 : "Error: Exodus file says "
566 : << exio_helper->num_nodes_per_elem
567 : << " nodes per Elem, but Elem type "
568 : << Utility::enum_to_string(uelem->type())
569 : << " has " << uelem->n_nodes() << " nodes.");
570 :
571 : // Assign the current subdomain to this Elem
572 304612 : uelem->subdomain_id() = subdomain_id;
573 :
574 : // Determine the libmesh elem id implied by "j". The
575 : // ExodusII_IO_Helper::get_libmesh_elem_id() helper function
576 : // expects a 1-based Exodus elem id, so we construct the
577 : // "implied" Exodus elem id from "j" by adding 1.
578 304612 : auto libmesh_elem_id = exio_helper->get_libmesh_elem_id(/*exodus_elem_id=*/j+1);
579 :
580 24878 : uelem->set_id(libmesh_elem_id);
581 :
582 : // Record that we have seen an element of dimension uelem->dim()
583 304612 : elems_of_dimension[uelem->dim()] = true;
584 :
585 : // Catch the Elem pointer that the Mesh throws back
586 354368 : Elem * elem = mesh.add_elem(std::move(uelem));
587 :
588 : // If the _set_unique_ids_from_maps flag is true, set the
589 : // unique_id for "elem", otherwise do nothing.
590 304612 : exio_helper->conditionally_set_elem_unique_id(mesh, elem, j);
591 :
592 : // If the Mesh assigned an ID different from the one we
593 : // tried to give it, we should probably error.
594 304612 : libmesh_error_msg_if(elem->id() != static_cast<unsigned>(libmesh_elem_id),
595 : "Error! Mesh assigned ID "
596 : << elem->id()
597 : << " which is different from the (zero-based) Exodus ID "
598 : << libmesh_elem_id
599 : << "!");
600 :
601 : // Assign extra integer IDs
602 305393 : for (auto & id : extra_ids)
603 : {
604 878 : const Real v = elem_ids[id][elem->id()];
605 :
606 852 : if (v == Real(-1))
607 : {
608 142 : elem->set_extra_integer(id, DofObject::invalid_id);
609 142 : continue;
610 : }
611 :
612 : // Ignore FE_INVALID here even if we've enabled FPEs; a
613 : // thrown exception is preferred over an FPE signal.
614 40 : FPEDisabler disable_fpes;
615 710 : const long long iv = std::llround(v);
616 :
617 : // Check if the real number is outside of the range we can
618 : // convert exactly
619 :
620 20 : long long max_representation = 1;
621 20 : max_representation = (max_representation << std::min(std::numeric_limits<Real>::digits,
622 20 : std::numeric_limits<double>::digits));
623 710 : libmesh_error_msg_if(iv > max_representation,
624 : "Error! An element integer value higher than "
625 : << max_representation
626 : << " was found! Exodus uses real numbers for storing element "
627 : " integers, which can only represent integers from 0 to "
628 : << max_representation
629 : << ".");
630 :
631 848 : libmesh_error_msg_if(iv < 0,
632 : "Error! An element integer value less than -1"
633 : << " was found! Exodus uses real numbers for storing element "
634 : " integers, which can only represent integers from 0 to "
635 : << max_representation
636 : << ".");
637 :
638 :
639 657 : elem->set_extra_integer(id, cast_int<dof_id_type>(iv));
640 : }
641 :
642 : // Set all the nodes for this element
643 : //
644 : // If we don't have any Bezier extraction operators, this
645 : // is easy: we've already built all our nodes and just need
646 : // to link to them.
647 304541 : if (is_c0polyhedron)
648 : {
649 : // Node pointers were already assigned while constructing
650 : // the polygonal sides above.
651 : }
652 304529 : else if (exio_helper->bex_cv_conn.empty())
653 : {
654 : const int n_nodes_this_elem =
655 329339 : is_c0polygon ?
656 12 : exio_helper->elem_node_counts[elem_num] :
657 24874 : exio_helper->num_nodes_per_elem;
658 :
659 1868840 : for (int k=0; k<n_nodes_this_elem; k++)
660 : {
661 : // Get index into this block's connectivity array
662 : std::size_t gi =
663 1564376 : is_c0polygon ?
664 60 : c0polygon_connect_offset++ :
665 1564316 : elem_num * exio_helper->num_nodes_per_elem + conv.get_node_map(k);
666 126571 : libmesh_assert_less(gi, exio_helper->connect.size());
667 :
668 : // Get the 1-based Exodus node id from the "connect" array
669 1564376 : auto exodus_node_id = exio_helper->connect[gi];
670 :
671 : // Convert this index to a libMesh Node id
672 1564376 : auto libmesh_node_id = exio_helper->get_libmesh_node_id(exodus_node_id);
673 :
674 : // Set the node pointer in the Elem
675 1564376 : elem->set_node(k, mesh.node_ptr(libmesh_node_id));
676 : }
677 : }
678 : else // We have Bezier Extraction data
679 : {
680 0 : auto & constraint_rows = mesh.set_constraint_rows();
681 :
682 : const DynaIO::ElementDefinition & dyna_elem_defn =
683 65 : DynaIO::find_elem_definition(elem->type(),
684 65 : elem->dim(),
685 65 : int(elem->default_order()));
686 :
687 : std::vector<std::vector<Real>>
688 67 : my_constraint_mat(exio_helper->bex_num_elem_cvs);
689 65 : for (auto spline_node_index :
690 1436 : make_range(exio_helper->bex_num_elem_cvs))
691 : {
692 1371 : my_constraint_mat[spline_node_index].resize(elem->n_nodes());
693 :
694 1371 : const auto & my_constraint_rows = exio_helper->bex_cv_conn[elem_num];
695 : const unsigned long elem_coef_vec_index =
696 1371 : my_constraint_rows[spline_node_index] - 1; // Exodus isn't 0-based
697 1371 : const auto & my_vec = bex_constraint_vec(elem_coef_vec_index, *exio_helper);
698 0 : for (auto elem_node_index :
699 34608 : make_range(elem->n_nodes()))
700 : {
701 33237 : my_constraint_mat[spline_node_index][elem_node_index] =
702 33237 : my_vec[elem_node_index];
703 : }
704 :
705 : }
706 :
707 : // The tailing entries in each element's connectivity
708 : // vector are indices to Exodus constraint coefficient
709 : // rows.
710 :
711 : // Concatenating these rows gives a matrix with
712 : // all the constraints for the element nodes: each
713 : // column of that matrix is the constraint coefficients
714 : // for the node associated with that column (via the
715 : // Exodus numbering, not the libMesh numbering).
716 65 : const auto & my_constraint_rows = exio_helper->bex_cv_conn[elem_num];
717 :
718 0 : for (auto elem_node_index :
719 1424 : make_range(elem->n_nodes()))
720 : {
721 : // New finite element node data = dot product of
722 : // constraint matrix columns with spline node data.
723 : // Store each column's non-zero entries, along with
724 : // the global spline node indices, as a key to
725 : // identify shared finite element nodes.
726 0 : std::vector<std::pair<dof_id_type, Real>> key;
727 :
728 1359 : for (auto spline_node_index :
729 34596 : make_range(exio_helper->bex_num_elem_cvs))
730 : {
731 : // Pick out a row of the element constraint matrix
732 : const unsigned long elem_coef_vec_index =
733 33237 : my_constraint_rows[spline_node_index] - 1; // Exodus isn't 0-based
734 :
735 : auto & coef_vec =
736 33237 : bex_constraint_vec(elem_coef_vec_index, *exio_helper);
737 :
738 : // Get coef from this node's column intersect that row
739 : const Real coef =
740 33237 : libmesh_vector_at(coef_vec, elem_node_index);
741 :
742 : // Get the libMesh node corresponding to that row
743 33237 : const int gi = elem_num * exio_helper->bex_num_elem_cvs + spline_node_index;
744 :
745 : // Get the 1-based Exodus node id from the "connect" array
746 33237 : auto exodus_node_id = exio_helper->connect[gi];
747 :
748 : // Convert this index to a libMesh Node id
749 33237 : auto libmesh_node_id = exio_helper->get_libmesh_node_id(exodus_node_id);
750 :
751 33237 : if (coef != 0) // Ignore irrelevant spline nodes
752 3661 : key.emplace_back(libmesh_node_id, coef);
753 : }
754 :
755 : // Have we already created this node? Connect it.
756 1359 : if (const auto local_node_it = local_nodes.find(key);
757 0 : local_node_it != local_nodes.end())
758 493 : elem->set_node(dyna_elem_defn.nodes[elem_node_index], local_node_it->second);
759 : // Have we not yet created this node? Construct it,
760 : // along with its weight and libMesh constraint row,
761 : // then connect it.
762 : else
763 : {
764 0 : Point p(0);
765 0 : Real w = 0;
766 0 : std::vector<std::pair<std::pair<const Elem *, unsigned int>, Real>> constraint_row;
767 :
768 2723 : for (auto [libmesh_spline_node_id, coef] : key)
769 : {
770 1857 : const Node & spline_node = mesh.node_ref(libmesh_spline_node_id);
771 :
772 1857 : p.add_scaled(spline_node, coef);
773 1857 : const Real spline_w = weights_exist ?
774 825 : spline_node.get_extra_datum<Real>(weight_index) : 1;
775 1857 : w += coef * spline_w;
776 :
777 : const Elem * nodeelem =
778 1857 : libmesh_map_find(spline_nodeelem_ptrs, &spline_node);
779 1857 : constraint_row.emplace_back(std::make_pair(nodeelem, 0), coef);
780 : }
781 :
782 866 : Node *n = mesh.add_point(p, n_nodes++);
783 866 : if (weights_exist)
784 402 : n->set_extra_datum<Real>(weight_index, w);
785 :
786 : // If we're building disconnected Bezier
787 : // extraction elements then we don't want to
788 : // find the new nodes to reuse later; each
789 : // finite element node will connect to only one
790 : // element.
791 866 : if (!_disc_bex)
792 551 : local_nodes[key] = n;
793 866 : elem->set_node(dyna_elem_defn.nodes[elem_node_index], n);
794 :
795 866 : constraint_rows[n] = constraint_row;
796 : }
797 : }
798 65 : }
799 254856 : }
800 :
801 544 : libmesh_assert(!is_c0polygon ||
802 : c0polygon_connect_offset == exio_helper->connect.size());
803 544 : libmesh_assert(!is_c0polyhedron ||
804 : c0polyhedron_connect_offset == exio_helper->connect.size());
805 :
806 : // running sum of # of elements per block,
807 : // (should equal total number of elements in the end)
808 7142 : nelem_last_block += exio_helper->num_elem_this_blk;
809 : }
810 :
811 : // Now we know enough to fix any spline NodeElem subdomains
812 2351 : max_subdomain_id++;
813 2708 : for (auto p : spline_nodeelem_ptrs)
814 357 : p.second->subdomain_id() = max_subdomain_id;
815 :
816 : // Read in edge blocks, storing information in the BoundaryInfo object.
817 : // Edge blocks are treated as BCs.
818 2351 : exio_helper->read_edge_blocks(mesh);
819 :
820 : // Set the mesh dimension to the largest encountered for an element
821 11755 : for (unsigned char i=0; i!=4; ++i)
822 9984 : if (elems_of_dimension[i])
823 2783 : mesh.set_mesh_dimension(i);
824 :
825 : // Read in sideset information -- this is useful for applying boundary conditions
826 : {
827 : // Get basic information about all sidesets
828 2351 : exio_helper->read_sideset_info();
829 145 : int offset=0;
830 12337 : for (int i=0; i<exio_helper->num_side_sets; i++)
831 : {
832 : // Compute new offset
833 9986 : offset += (i > 0 ? exio_helper->num_sides_per_set[i-1] : 0);
834 9986 : exio_helper->read_sideset (i, offset);
835 :
836 10592 : std::string sideset_name = exio_helper->get_side_set_name(i);
837 9986 : if (!sideset_name.empty())
838 526 : mesh.get_boundary_info().sideset_name
839 8178 : (cast_int<boundary_id_type>(exio_helper->get_side_set_id(i)))
840 526 : = sideset_name;
841 : }
842 :
843 105131 : for (auto e : index_range(exio_helper->elem_list))
844 : {
845 : // Call helper function to get the libmesh Elem id for the
846 : // e'th entry in the current elem_list.
847 : dof_id_type libmesh_elem_id =
848 109980 : exio_helper->get_libmesh_elem_id(exio_helper->elem_list[e]);
849 :
850 : // Set any relevant node/edge maps for this element
851 102780 : Elem & elem = mesh.elem_ref(libmesh_elem_id);
852 :
853 102780 : const auto & conv = exio_helper->get_conversion(elem.type());
854 :
855 : // Map the zero-based Exodus side numbering to the libmesh side numbering
856 102780 : unsigned int raw_side_index = exio_helper->side_list[e]-1;
857 102780 : std::size_t side_index_offset = conv.get_shellface_index_offset();
858 :
859 102780 : if (raw_side_index < side_index_offset)
860 : {
861 : // We assume this is a "shell face"
862 3328 : int mapped_shellface = raw_side_index;
863 :
864 : // Check for errors
865 39936 : libmesh_error_msg_if(mapped_shellface < 0 || mapped_shellface >= 2,
866 : "Bad 0-based shellface id: "
867 : << mapped_shellface
868 : << " detected in Exodus file "
869 : << exio_helper->current_filename);
870 :
871 : // Add this (elem,shellface,id) triplet to the BoundaryInfo object.
872 39936 : mesh.get_boundary_info().add_shellface (libmesh_elem_id,
873 3328 : cast_int<unsigned short>(mapped_shellface),
874 43264 : cast_int<boundary_id_type>(exio_helper->id_list[e]));
875 : }
876 : else
877 : {
878 62844 : unsigned int side_index = static_cast<unsigned int>(raw_side_index - side_index_offset);
879 62844 : int mapped_side = conv.get_side_map(side_index);
880 :
881 : // Check for errors
882 62844 : libmesh_error_msg_if(mapped_side == ExodusII_IO_Helper::Conversion::invalid_id,
883 : "Invalid 1-based side id: "
884 : << side_index
885 : << " detected for "
886 : << Utility::enum_to_string(elem.type())
887 : << " in Exodus file "
888 : << exio_helper->current_filename);
889 :
890 62844 : libmesh_error_msg_if(mapped_side < 0 ||
891 : cast_int<unsigned int>(mapped_side) >= elem.n_sides(),
892 : "Bad 0-based side id: "
893 : << mapped_side
894 : << " detected for "
895 : << Utility::enum_to_string(elem.type())
896 : << " in Exodus file "
897 : << exio_helper->current_filename);
898 :
899 : // Add this (elem,side,id) triplet to the BoundaryInfo object.
900 62844 : mesh.get_boundary_info().add_side (libmesh_elem_id,
901 3872 : cast_int<unsigned short>(mapped_side),
902 66716 : cast_int<boundary_id_type>(exio_helper->id_list[e]));
903 : }
904 : } // end for (elem_list)
905 : } // end read sideset info
906 :
907 : // Read in elemset information and apply to Mesh elements if present
908 : {
909 2351 : exio_helper->read_elemset_info();
910 :
911 : // Mimic behavior of sideset case where we store all the set
912 : // information in a single array with offsets.
913 145 : int offset=0;
914 2493 : for (int i=0; i<exio_helper->num_elem_sets; i++)
915 : {
916 : // Compute new offset
917 142 : offset += (i > 0 ? exio_helper->num_elems_per_set[i-1] : 0);
918 142 : exio_helper->read_elemset (i, offset);
919 :
920 : // TODO: add support for elemset names
921 : // std::string elemset_name = exio_helper->get_elem_set_name(i);
922 : // if (!elemset_name.empty())
923 : // mesh.get_boundary_info().elemset_name(cast_int<boundary_id_type>(exio_helper->get_elem_set_id(i))) = elemset_name;
924 : }
925 :
926 : // Debugging: print the concatenated list of elemset ids
927 : // libMesh::out << "Concatenated list of elemset Elem ids (Exodus numbering):" << std::endl;
928 : // for (const auto & id : exio_helper->elemset_list)
929 : // libMesh::out << id << " ";
930 : // libMesh::out << std::endl;
931 :
932 : // Next we need to assign the elemset ids to the mesh using the
933 : // Elem's "extra_integers" support, if we have any.
934 2351 : if (exio_helper->num_elem_all_elemsets)
935 : {
936 : // Build map from Elem -> {elemsets}. This is needed only
937 : // temporarily to determine a unique set of elemset codes.
938 4 : std::map<Elem *, MeshBase::elemset_type> elem_to_elemsets;
939 639 : for (auto e : index_range(exio_helper->elemset_list))
940 : {
941 : // Call helper function to get the libmesh Elem id for the
942 : // e'th entry in the current elemset_list.
943 : dof_id_type libmesh_elem_id =
944 584 : exio_helper->get_libmesh_elem_id(exio_helper->elemset_list[e]);
945 :
946 : // Get a pointer to this Elem
947 568 : Elem * elem = mesh.elem_ptr(libmesh_elem_id);
948 :
949 : // Debugging:
950 : // libMesh::out << "Elem " << elem->id() << " is in elemset " << exio_helper->elemset_id_list[e] << std::endl;
951 :
952 : // Store elemset id in the map
953 568 : elem_to_elemsets[elem].insert(exio_helper->elemset_id_list[e]);
954 : }
955 :
956 : // Create a set of unique elemsets
957 4 : std::set<MeshBase::elemset_type> unique_elemsets;
958 497 : for (const auto & pr : elem_to_elemsets)
959 426 : unique_elemsets.insert(pr.second);
960 :
961 : // Debugging: print the unique elemsets
962 : // libMesh::out << "The set of unique elemsets which exist on the Mesh:" << std::endl;
963 : // for (const auto & s : unique_elemsets)
964 : // {
965 : // for (const auto & elemset_id : s)
966 : // libMesh::out << elemset_id << " ";
967 : // libMesh::out << std::endl;
968 : // }
969 :
970 : // Enumerate the unique_elemsets and tell the mesh about them
971 2 : dof_id_type code = 0;
972 284 : for (const auto & s : unique_elemsets)
973 420 : mesh.add_elemset_code(code++, s);
974 :
975 : // Sanity check: make sure that MeshBase::n_elemsets() reports
976 : // the expected value after calling MeshBase::add_elemset_code()
977 : // one or more times.
978 2 : libmesh_assert_msg(exio_helper->num_elem_sets == cast_int<int>(mesh.n_elemsets()),
979 : "Error: mesh.n_elemsets() is " << mesh.n_elemsets()
980 : << ", but mesh should have " << exio_helper->num_elem_sets << " elemsets.");
981 :
982 : // Create storage for the extra integer on all Elems. Elems which
983 : // are not in any set will use the default value of DofObject::invalid_id
984 : unsigned int elemset_index =
985 140 : mesh.add_elem_integer("elemset_code",
986 : /*allocate_data=*/true);
987 :
988 : // Store the appropriate extra_integer value on all Elems that need it.
989 497 : for (const auto & [elem, s] : elem_to_elemsets)
990 426 : elem->set_extra_integer(elemset_index, mesh.get_elemset_code(s));
991 : }
992 : } // done reading elemset info
993 :
994 : // Read nodeset info
995 : {
996 : // This fills in the following fields of the helper for later use:
997 : // nodeset_ids
998 : // num_nodes_per_set
999 : // num_node_df_per_set
1000 : // node_sets_node_index
1001 : // node_sets_dist_index
1002 : // node_sets_node_list
1003 : // node_sets_dist_fact
1004 2351 : exio_helper->read_all_nodesets();
1005 :
1006 10901 : for (int nodeset=0; nodeset<exio_helper->num_node_sets; nodeset++)
1007 : {
1008 : boundary_id_type nodeset_id =
1009 9108 : cast_int<boundary_id_type>(exio_helper->nodeset_ids[nodeset]);
1010 :
1011 9108 : std::string nodeset_name = exio_helper->get_node_set_name(nodeset);
1012 8550 : if (!nodeset_name.empty())
1013 8310 : mesh.get_boundary_info().nodeset_name(nodeset_id) = nodeset_name;
1014 :
1015 : // Get starting index of node ids for current nodeset.
1016 9108 : unsigned int offset = exio_helper->node_sets_node_index[nodeset];
1017 :
1018 110262 : for (int i=0; i<exio_helper->num_nodes_per_set[nodeset]; ++i)
1019 : {
1020 101154 : int exodus_node_id = exio_helper->node_sets_node_list[i + offset];
1021 101154 : auto libmesh_node_id = exio_helper->get_libmesh_node_id(exodus_node_id);
1022 101154 : mesh.get_boundary_info().add_node(libmesh_node_id, nodeset_id);
1023 : }
1024 : }
1025 : }
1026 :
1027 : #if LIBMESH_DIM < 3
1028 : libmesh_error_msg_if(mesh.mesh_dimension() > LIBMESH_DIM,
1029 : "Cannot open dimension "
1030 : << mesh.mesh_dimension()
1031 : << " mesh file when configured without "
1032 : << mesh.mesh_dimension()
1033 : << "D support.");
1034 : #endif
1035 4479 : }
1036 :
1037 :
1038 :
1039 : ExodusHeaderInfo
1040 213 : ExodusII_IO::read_header (const std::string & fname)
1041 : {
1042 : // We will need the Communicator of the Mesh we were created with.
1043 213 : MeshBase & mesh = MeshInput<MeshBase>::mesh();
1044 :
1045 : // Eventual return value
1046 6 : ExodusHeaderInfo header_info;
1047 :
1048 : // File I/O is done on processor 0, then broadcast to other procs
1049 219 : if (mesh.processor_id() == 0)
1050 : {
1051 : // Open the exodus file in EX_READ mode
1052 36 : exio_helper->open(fname.c_str(), /*read_only=*/true);
1053 :
1054 : // Get header information from exodus file without updating the
1055 : // Helper object's internal data structures.
1056 69 : header_info = exio_helper->read_header();
1057 :
1058 : // Close the file, we are now done with it. The goal is to keep the
1059 : // exio_helper object unchanged while calling this function,
1060 : // although it can't quite be marked "const" because we do have to
1061 : // actually open/close the file. This way, it should be possible to
1062 : // use the same ExodusII_IO object to read the headers of multiple
1063 : // different mesh files.
1064 36 : exio_helper->close();
1065 : }
1066 :
1067 : // Broadcast header_info to other procs before returning
1068 213 : header_info.broadcast(mesh.comm());
1069 :
1070 : // Return the information we read back to the user.
1071 213 : return header_info;
1072 : }
1073 :
1074 :
1075 :
1076 0 : void ExodusII_IO::verbose (bool set_verbosity)
1077 : {
1078 0 : _verbose = set_verbosity;
1079 :
1080 : // Set the verbose flag in the helper object as well.
1081 0 : exio_helper->verbose = _verbose;
1082 0 : }
1083 :
1084 :
1085 :
1086 0 : void ExodusII_IO::write_complex_magnitude (bool val)
1087 : {
1088 0 : _write_complex_abs = val;
1089 0 : }
1090 :
1091 284 : void ExodusII_IO::set_unique_ids_from_maps (bool val)
1092 : {
1093 284 : _set_unique_ids_from_maps = val;
1094 :
1095 : // Set this flag on the helper object as well. The helper needs to know about this
1096 : // flag, since it sometimes needs to construct libmesh Node ids from nodal connectivity
1097 : // arrays (see e.g. ExodusII_IO_Helper::read_edge_blocks()).
1098 284 : exio_helper->set_unique_ids_from_maps = val;
1099 284 : }
1100 :
1101 0 : void ExodusII_IO::use_mesh_dimension_instead_of_spatial_dimension(bool val)
1102 : {
1103 0 : exio_helper->use_mesh_dimension_instead_of_spatial_dimension(val);
1104 0 : }
1105 :
1106 :
1107 :
1108 0 : void ExodusII_IO::write_as_dimension(unsigned dim)
1109 : {
1110 0 : exio_helper->write_as_dimension(dim);
1111 0 : }
1112 :
1113 :
1114 :
1115 0 : void ExodusII_IO::set_coordinate_offset(Point p)
1116 : {
1117 : libmesh_warning("This method may be deprecated in the future");
1118 0 : exio_helper->set_coordinate_offset(p);
1119 0 : }
1120 :
1121 :
1122 :
1123 355 : void ExodusII_IO::append(bool val)
1124 : {
1125 355 : _append = val;
1126 355 : }
1127 :
1128 :
1129 :
1130 2414 : void ExodusII_IO::write_added_sides (bool val)
1131 : {
1132 68 : exio_helper->set_add_sides(val);
1133 2414 : }
1134 :
1135 :
1136 :
1137 49551 : bool ExodusII_IO::get_add_sides ()
1138 : {
1139 49551 : return exio_helper->get_add_sides();
1140 : }
1141 :
1142 :
1143 :
1144 :
1145 0 : const std::vector<Real> & ExodusII_IO::get_time_steps()
1146 : {
1147 0 : libmesh_error_msg_if
1148 : (!exio_helper->opened_for_reading,
1149 : "ERROR, ExodusII file must be opened for reading before calling ExodusII_IO::get_time_steps()!");
1150 :
1151 0 : exio_helper->read_time_steps();
1152 0 : return exio_helper->time_steps;
1153 : }
1154 :
1155 :
1156 :
1157 0 : int ExodusII_IO::get_num_time_steps()
1158 : {
1159 0 : libmesh_error_msg_if(!exio_helper->opened_for_reading && !exio_helper->opened_for_writing,
1160 : "ERROR, ExodusII file must be opened for reading or writing before calling ExodusII_IO::get_num_time_steps()!");
1161 :
1162 0 : exio_helper->read_num_time_steps();
1163 0 : return exio_helper->num_time_steps;
1164 : }
1165 :
1166 :
1167 :
1168 2994 : void ExodusII_IO::copy_nodal_solution(System & system,
1169 : std::string system_var_name,
1170 : std::string exodus_var_name,
1171 : unsigned int timestep)
1172 : {
1173 252 : LOG_SCOPE("copy_nodal_solution()", "ExodusII_IO");
1174 :
1175 2994 : const unsigned int var_num = system.variable_number(system_var_name);
1176 :
1177 2994 : const MeshBase & mesh = MeshInput<MeshBase>::mesh();
1178 :
1179 2994 : libmesh_error_msg_if(mesh.allow_renumbering(),
1180 : "ERROR, nodal data cannot be loaded if the mesh may be renumbered!");
1181 :
1182 : // With Exodus files we only open them on processor 0, so that's the
1183 : // where we have to do the data read too.
1184 2994 : if (system.comm().rank() == 0)
1185 : {
1186 516 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
1187 : "ERROR, ExodusII file must be opened for reading before copying a nodal solution!");
1188 :
1189 990 : exio_helper->read_nodal_var_values(exodus_var_name, timestep);
1190 : }
1191 :
1192 2994 : auto & node_var_value_map = exio_helper->nodal_var_values;
1193 :
1194 2994 : const bool serial_on_zero = mesh.is_serial_on_zero();
1195 :
1196 : // If our mesh isn't serial, then non-root processors need to
1197 : // request the data for their parts of the mesh and insert it
1198 : // themselves.
1199 2994 : if (!serial_on_zero)
1200 : {
1201 16 : std::unordered_map<processor_id_type, std::vector<dof_id_type>> node_ids_to_request;
1202 2474 : if (this->processor_id() != 0)
1203 : {
1204 8 : std::vector<dof_id_type> node_ids;
1205 41425 : for (auto & node : mesh.local_node_ptr_range())
1206 39281 : node_ids.push_back(node->id());
1207 2140 : if (!node_ids.empty())
1208 3290 : node_ids_to_request[0] = std::move(node_ids);
1209 : }
1210 :
1211 : auto value_gather_functor =
1212 1639 : [& node_var_value_map]
1213 : (processor_id_type,
1214 : const std::vector<dof_id_type> & ids,
1215 37170 : std::vector<Real> & values)
1216 : {
1217 8 : const std::size_t query_size = ids.size();
1218 1647 : values.resize(query_size);
1219 38796 : for (std::size_t i=0; i != query_size; ++i)
1220 : {
1221 37162 : if (const auto it = node_var_value_map.find(ids[i]);
1222 13 : it != node_var_value_map.end())
1223 : {
1224 37162 : values[i] = it->second;
1225 37136 : node_var_value_map.erase(it);
1226 : }
1227 : else
1228 0 : values[i] = std::numeric_limits<Real>::quiet_NaN();
1229 : }
1230 4105 : };
1231 :
1232 : auto value_action_functor =
1233 1639 : [& node_var_value_map]
1234 : (processor_id_type,
1235 : const std::vector<dof_id_type> & ids,
1236 37144 : const std::vector<Real> & values)
1237 : {
1238 8 : const std::size_t query_size = ids.size();
1239 38796 : for (std::size_t i=0; i != query_size; ++i)
1240 37162 : if (!libmesh_isnan(values[i]))
1241 37162 : node_var_value_map[ids[i]] = values[i];
1242 2474 : };
1243 :
1244 8 : Real * value_ex = nullptr;
1245 : Parallel::pull_parallel_vector_data
1246 2466 : (system.comm(), node_ids_to_request, value_gather_functor,
1247 : value_action_functor, value_ex);
1248 : }
1249 :
1250 : // Everybody inserts the data they've received. If we're
1251 : // serial_on_zero then proc 0 inserts everybody's data and other
1252 : // procs have empty map ranges.
1253 78786 : for (auto p : exio_helper->nodal_var_values)
1254 : {
1255 6307 : dof_id_type i = p.first;
1256 75792 : const Node * node = MeshInput<MeshBase>::mesh().query_node_ptr(i);
1257 :
1258 75792 : if (node &&
1259 201965 : (serial_on_zero || node->processor_id() == system.processor_id()) &&
1260 75792 : node->n_comp(system.number(), var_num) > 0)
1261 : {
1262 75792 : dof_id_type dof_index = node->dof_number(system.number(), var_num, 0);
1263 :
1264 : // If the dof_index is local to this processor, set the value
1265 75792 : system.solution->set (dof_index, p.second);
1266 : }
1267 : }
1268 :
1269 2994 : system.solution->close();
1270 2994 : system.update();
1271 2994 : }
1272 :
1273 :
1274 :
1275 1687 : void ExodusII_IO::copy_elemental_solution(System & system,
1276 : std::string system_var_name,
1277 : std::string exodus_var_name,
1278 : unsigned int timestep)
1279 : {
1280 144 : LOG_SCOPE("copy_elemental_solution()", "ExodusII_IO");
1281 :
1282 1687 : const unsigned int var_num = system.variable_number(system_var_name);
1283 : // Assert that variable is an elemental data variable.
1284 : //
1285 : // NOTE: Currently, this reader is capable of reading only individual components of vector types,
1286 : // and each must be written out to its own scalar elemental data variable.
1287 1687 : const auto & var_type = system.variable_type(var_num);
1288 1687 : libmesh_error_msg_if(!EquationSystems::is_elemental_data_fe_type(var_type),
1289 : "Error! Trying to copy elemental solution into a variable that is not elemental data.");
1290 :
1291 1687 : const MeshBase & mesh = MeshInput<MeshBase>::mesh();
1292 48 : const DofMap & dof_map = system.get_dof_map();
1293 :
1294 1687 : libmesh_error_msg_if(mesh.allow_renumbering(),
1295 : "ERROR, elemental data cannot be loaded if the mesh may be renumbered!");
1296 :
1297 : // Map from element ID to elemental variable value. We need to use
1298 : // a map here rather than a vector (e.g. elem_var_values) since the
1299 : // libmesh element numbering can contain "holes". This is the case
1300 : // if we are reading elemental var values from an adaptively refined
1301 : // mesh that has not been sequentially renumbered.
1302 96 : std::map<dof_id_type, Real> elem_var_value_map;
1303 :
1304 : // With Exodus files we only open them on processor 0, so that's the
1305 : // where we have to do the data read too.
1306 1687 : if (system.comm().rank() == 0)
1307 : {
1308 271 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
1309 : "ERROR, ExodusII file must be opened for reading before copying an elemental solution!");
1310 :
1311 518 : exio_helper->read_elemental_var_values(exodus_var_name, timestep, elem_var_value_map);
1312 : }
1313 :
1314 1687 : const bool serial_on_zero = mesh.is_serial_on_zero();
1315 :
1316 : // If our mesh isn't serial, then non-root processors need to
1317 : // request the data for their parts of the mesh and insert it
1318 : // themselves.
1319 1687 : if (!serial_on_zero)
1320 : {
1321 28 : std::unordered_map<processor_id_type, std::vector<dof_id_type>> elem_ids_to_request;
1322 507 : if (this->processor_id() != 0)
1323 : {
1324 14 : std::vector<dof_id_type> elem_ids;
1325 1274 : for (auto & elem : mesh.active_local_element_ptr_range())
1326 854 : elem_ids.push_back(elem->id());
1327 :
1328 413 : if (!elem_ids.empty())
1329 427 : elem_ids_to_request[0] = std::move(elem_ids);
1330 : }
1331 :
1332 : auto value_gather_functor =
1333 203 : [& elem_var_value_map]
1334 : (processor_id_type,
1335 : const std::vector<dof_id_type> & ids,
1336 497 : std::vector<Real> & values)
1337 : {
1338 14 : const std::size_t query_size = ids.size();
1339 217 : values.resize(query_size);
1340 672 : for (std::size_t i=0; i != query_size; ++i)
1341 : {
1342 483 : if (const auto it = elem_var_value_map.find(ids[i]);
1343 28 : it != elem_var_value_map.end())
1344 : {
1345 483 : values[i] = it->second;
1346 427 : elem_var_value_map.erase(it);
1347 : }
1348 : else
1349 0 : values[i] = std::numeric_limits<Real>::quiet_NaN();
1350 : }
1351 696 : };
1352 :
1353 : auto value_action_functor =
1354 203 : [& elem_var_value_map]
1355 : (processor_id_type,
1356 : const std::vector<dof_id_type> & ids,
1357 441 : const std::vector<Real> & values)
1358 : {
1359 14 : const std::size_t query_size = ids.size();
1360 672 : for (std::size_t i=0; i != query_size; ++i)
1361 483 : if (!libmesh_isnan(values[i]))
1362 483 : elem_var_value_map[ids[i]] = values[i];
1363 507 : };
1364 :
1365 14 : Real * value_ex = nullptr;
1366 : Parallel::pull_parallel_vector_data
1367 493 : (system.comm(), elem_ids_to_request, value_gather_functor,
1368 : value_action_functor, value_ex);
1369 : }
1370 :
1371 : std::map<dof_id_type, Real>::iterator
1372 48 : it = elem_var_value_map.begin(),
1373 48 : end = elem_var_value_map.end();
1374 :
1375 : // Everybody inserts the data they've received. If we're
1376 : // serial_on_zero then proc 0 inserts everybody's data and other
1377 : // procs have empty map ranges.
1378 4027 : for (; it!=end; ++it)
1379 : {
1380 2340 : const Elem * elem = mesh.query_elem_ptr(it->first);
1381 :
1382 2340 : if (elem && elem->n_comp(system.number(), var_num) > 0)
1383 : {
1384 2340 : dof_id_type dof_index = elem->dof_number(system.number(), var_num, 0);
1385 2340 : if (serial_on_zero || dof_map.local_index(dof_index ))
1386 2340 : system.solution->set (dof_index, it->second);
1387 : }
1388 : }
1389 :
1390 1687 : system.solution->close();
1391 1687 : system.update();
1392 1687 : }
1393 :
1394 0 : void ExodusII_IO::copy_scalar_solution(System & system,
1395 : std::vector<std::string> system_var_names,
1396 : std::vector<std::string> exodus_var_names,
1397 : unsigned int timestep)
1398 : {
1399 0 : LOG_SCOPE("copy_scalar_solution()", "ExodusII_IO");
1400 :
1401 0 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
1402 : "ERROR, ExodusII file must be opened for reading before copying a scalar solution!");
1403 :
1404 0 : libmesh_error_msg_if(system_var_names.size() != exodus_var_names.size(),
1405 : "ERROR, the number of system_var_names must match exodus_var_names.");
1406 :
1407 0 : std::vector<Real> values_from_exodus;
1408 0 : read_global_variable(exodus_var_names, timestep, values_from_exodus);
1409 :
1410 : #ifdef LIBMESH_HAVE_MPI
1411 0 : if (this->n_processors() > 1)
1412 : {
1413 0 : const Parallel::MessageTag tag = this->comm().get_unique_tag(1);
1414 0 : if (this->processor_id() == this->n_processors()-1)
1415 0 : this->comm().receive(0, values_from_exodus, tag);
1416 0 : if (this->processor_id() == 0)
1417 0 : this->comm().send(this->n_processors()-1, values_from_exodus, tag);
1418 0 : }
1419 : #endif
1420 :
1421 0 : if (system.processor_id() == (system.n_processors()-1))
1422 : {
1423 0 : const DofMap & dof_map = system.get_dof_map();
1424 :
1425 0 : for (auto i : index_range(system_var_names))
1426 : {
1427 0 : const unsigned int var_num = system.variable_scalar_number(system_var_names[i], 0);
1428 :
1429 0 : std::vector<dof_id_type> SCALAR_dofs;
1430 0 : dof_map.SCALAR_dof_indices(SCALAR_dofs, var_num);
1431 :
1432 0 : system.solution->set (SCALAR_dofs[0], values_from_exodus[i]);
1433 : }
1434 : }
1435 :
1436 0 : system.solution->close();
1437 0 : system.update();
1438 0 : }
1439 :
1440 0 : void ExodusII_IO::read_elemental_variable(std::string elemental_var_name,
1441 : unsigned int timestep,
1442 : std::map<unsigned int, Real> & unique_id_to_value_map)
1443 : {
1444 0 : LOG_SCOPE("read_elemental_variable()", "ExodusII_IO");
1445 :
1446 : // Note that this function MUST be called before renumbering
1447 0 : std::map<dof_id_type, Real> elem_var_value_map;
1448 :
1449 0 : exio_helper->read_elemental_var_values(elemental_var_name, timestep, elem_var_value_map);
1450 0 : for (auto & pr : elem_var_value_map)
1451 : {
1452 0 : const Elem * elem = MeshInput<MeshBase>::mesh().query_elem_ptr(pr.first);
1453 0 : unique_id_to_value_map.emplace(elem->top_parent()->unique_id(), pr.second);
1454 : }
1455 0 : }
1456 :
1457 0 : void ExodusII_IO::read_global_variable(std::vector<std::string> global_var_names,
1458 : unsigned int timestep,
1459 : std::vector<Real> & global_values)
1460 : {
1461 0 : LOG_SCOPE("read_global_variable()", "ExodusII_IO");
1462 :
1463 0 : std::size_t size = global_var_names.size();
1464 0 : libmesh_error_msg_if(size == 0, "ERROR, empty list of global variables to read from the Exodus file.");
1465 :
1466 : // read the values for all global variables
1467 0 : std::vector<Real> values_from_exodus;
1468 0 : exio_helper->read_var_names(ExodusII_IO_Helper::GLOBAL);
1469 0 : exio_helper->read_global_values(values_from_exodus, timestep);
1470 0 : std::vector<std::string> global_var_names_exodus = exio_helper->global_var_names;
1471 :
1472 0 : if (values_from_exodus.size() == 0)
1473 0 : return; // This will happen in parallel on procs that are not 0
1474 :
1475 0 : global_values.clear();
1476 0 : for (std::size_t i = 0; i != size; ++i)
1477 : {
1478 : // for each global variable in global_var_names, look the corresponding one in global_var_names_from_exodus
1479 : // and fill global_values accordingly
1480 0 : auto it = find(global_var_names_exodus.begin(), global_var_names_exodus.end(), global_var_names[i]);
1481 0 : if (it != global_var_names_exodus.end())
1482 0 : global_values.push_back(values_from_exodus[it - global_var_names_exodus.begin()]);
1483 : else
1484 0 : libmesh_error_msg("ERROR, Global variable " << global_var_names[i] << \
1485 : " not found in Exodus file.");
1486 : }
1487 :
1488 0 : }
1489 :
1490 423 : void ExodusII_IO::write_element_data (const EquationSystems & es)
1491 : {
1492 18 : LOG_SCOPE("write_element_data()", "ExodusII_IO");
1493 :
1494 : // Be sure the file has been opened for writing!
1495 435 : libmesh_error_msg_if(MeshOutput<MeshBase>::mesh().processor_id() == 0 && !exio_helper->opened_for_writing,
1496 : "ERROR, ExodusII file must be initialized before outputting element variables.");
1497 :
1498 : // This function currently only works on serialized meshes. We rely
1499 : // on having a reference to a non-const MeshBase object from our
1500 : // MeshInput parent class to construct a MeshSerializer object,
1501 : // similar to what is done in ExodusII_IO::write(). Note that
1502 : // calling ExodusII_IO::write_timestep() followed by
1503 : // ExodusII_IO::write_element_data() when the underlying Mesh is a
1504 : // DistributedMesh will result in an unnecessary additional
1505 : // serialization/re-parallelization step.
1506 : // The "true" specifies that we only need the mesh serialized to processor 0
1507 429 : MeshSerializer serialize(MeshInput<MeshBase>::mesh(), !MeshOutput<MeshBase>::_is_parallel_format, true);
1508 :
1509 : // To be (possibly) filled with a filtered list of variable names to output.
1510 18 : std::vector<std::string> names;
1511 :
1512 : // If _output_variables is populated, build_elemental_solution_vector() will filter this list to
1513 : // the variables that can be written as elemental data.
1514 423 : if (_output_variables.size() > 0)
1515 68 : names.assign(_output_variables.begin(), _output_variables.end());
1516 :
1517 : // If we pass in a list of names to "build_elemental_solution_vector()"
1518 : // it'll filter the variables coming back.
1519 12 : std::vector<Number> soln;
1520 423 : es.build_elemental_solution_vector(soln, names, _allow_hi_order_elemental);
1521 :
1522 : // Also, store the list of subdomains on which each variable is active
1523 18 : std::vector<std::set<subdomain_id_type>> vars_active_subdomains;
1524 423 : es.get_vars_active_subdomains(names, vars_active_subdomains);
1525 :
1526 423 : if (soln.empty()) // If there is nothing to write just return
1527 12 : return;
1528 :
1529 : // The data must ultimately be written block by block. This means that this data
1530 : // must be sorted appropriately.
1531 75 : if (MeshOutput<MeshBase>::mesh().processor_id())
1532 0 : return;
1533 :
1534 6 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
1535 :
1536 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
1537 :
1538 : std::vector<std::string> complex_names =
1539 3 : exio_helper->get_complex_names(names, _write_complex_abs);
1540 :
1541 : std::vector<std::set<subdomain_id_type>>
1542 : complex_vars_active_subdomains =
1543 : exio_helper->get_complex_vars_active_subdomains(vars_active_subdomains,
1544 3 : _write_complex_abs);
1545 3 : exio_helper->initialize_element_variables(complex_names, complex_vars_active_subdomains);
1546 :
1547 : const std::vector<Real> complex_soln =
1548 3 : complex_soln_components(soln, names.size(), _write_complex_abs,
1549 : SolutionOrdering::variable_major,
1550 3 : SolutionOrdering::variable_major);
1551 :
1552 3 : exio_helper->write_element_values(mesh, complex_soln, _timestep, complex_vars_active_subdomains);
1553 :
1554 : #else
1555 66 : exio_helper->initialize_element_variables(names, vars_active_subdomains);
1556 66 : exio_helper->write_element_values(mesh, soln, _timestep, vars_active_subdomains);
1557 : #endif
1558 798 : }
1559 :
1560 :
1561 :
1562 : void
1563 70 : ExodusII_IO::write_element_data_from_discontinuous_nodal_data
1564 : (const EquationSystems & es,
1565 : const std::set<std::string> * system_names,
1566 : const std::string & var_suffix)
1567 : {
1568 6 : LOG_SCOPE("write_element_data_from_discontinuous_nodal_data()", "ExodusII_IO");
1569 :
1570 : // Be sure that some other function has already opened the file and prepared it
1571 : // for writing. This is the same behavior as the write_element_data() function
1572 : // which we are trying to mimic.
1573 72 : libmesh_error_msg_if(MeshOutput<MeshBase>::mesh().processor_id() == 0 && !exio_helper->opened_for_writing,
1574 : "ERROR, ExodusII file must be initialized before outputting element variables.");
1575 :
1576 : // This function currently only works on serialized meshes. The
1577 : // "true" flag specifies that we only need the mesh serialized to
1578 : // processor 0
1579 : MeshSerializer serialize(MeshInput<MeshBase>::mesh(),
1580 70 : !MeshOutput<MeshBase>::_is_parallel_format,
1581 74 : true);
1582 :
1583 : // Note: in general we want to respect the contents of
1584 : // _output_variables, only building a solution vector with values
1585 : // from the requested variables. First build a list of all variable
1586 : // names, then throw out ones that aren't in _output_variables, if
1587 : // any.
1588 6 : std::vector<std::string> var_names;
1589 70 : es.build_variable_names (var_names, /*fetype=*/nullptr, system_names);
1590 :
1591 : // Get a subset of all variable names that can be written directly
1592 : // as elemental data. We treat those slightly differently since they
1593 : // truly only have a single value per Elem.
1594 6 : std::vector<std::string> elemental_var_names;
1595 70 : if (!_output_variables.empty())
1596 0 : elemental_var_names.assign(_output_variables.begin(), _output_variables.end());
1597 70 : es.build_elemental_data_variable_names(elemental_var_names, system_names);
1598 :
1599 : // Remove all names from var_names that are not in _output_variables.
1600 : // Note: This approach avoids errors when the user provides invalid
1601 : // variable names in _output_variables, as the code will not try to
1602 : // write a variable that doesn't exist.
1603 70 : if (!_output_variables.empty())
1604 : {
1605 : var_names.erase
1606 : (std::remove_if
1607 0 : (var_names.begin(),
1608 : var_names.end(),
1609 0 : [this](const std::string & name)
1610 0 : {return !std::count(_output_variables.begin(),
1611 : _output_variables.end(),
1612 0 : name);}),
1613 0 : var_names.end());
1614 : }
1615 :
1616 : // Build a solution vector, limiting the results to the variables in
1617 : // var_names and the Systems in system_names, and only computing values
1618 : // at the vertices.
1619 4 : std::vector<Number> v;
1620 : es.build_discontinuous_solution_vector
1621 70 : (v, system_names, &var_names, /*vertices_only=*/true);
1622 :
1623 : // Get active subdomains for each variable in var_names.
1624 6 : std::vector<std::set<subdomain_id_type>> vars_active_subdomains;
1625 70 : es.get_vars_active_subdomains(var_names, vars_active_subdomains);
1626 :
1627 : // Determine names of variables to write based on the number of
1628 : // nodes/vertices the elements in different subdomains have.
1629 4 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
1630 4 : std::map<subdomain_id_type, unsigned int> subdomain_id_to_vertices_per_elem;
1631 970 : for (const auto & elem : mesh.active_element_ptr_range())
1632 : {
1633 : // Try to insert key/value pair into the map. If this returns
1634 : // false, check the returned iterator's value to make sure it
1635 : // matches. It shouldn't actually be possible for this to fail
1636 : // (since if the Mesh was like this it would have already
1637 : // failed) but it doesn't hurt to be on the safe side.
1638 : auto pr2 = subdomain_id_to_vertices_per_elem.emplace
1639 432 : (elem->subdomain_id(), elem->n_vertices());
1640 432 : libmesh_error_msg_if(!pr2.second && pr2.first->second != elem->n_vertices(),
1641 : "Elem with different number of vertices found.");
1642 66 : }
1643 :
1644 : // Determine "derived" variable names. These names are created by
1645 : // starting with the base variable name and appending the user's
1646 : // variable_suffix (default: "_elem_node_") followed by a node id.
1647 : //
1648 : // Not every derived variable will be active on every subdomain,
1649 : // even if the original variable _is_ active. Subdomains can have
1650 : // different geometric element types (with differing numbers of
1651 : // nodes), so some of the derived variable names will be inactive on
1652 : // those subdomains.
1653 : //
1654 : // Since we would otherwise generate the same name once per
1655 : // subdomain, we keep the list of names unique as we are creating
1656 : // it. We can't use a std::set for this because we don't want the
1657 : // variables names to be in a different order from the order
1658 : // they were written in the call to: build_discontinuous_solution_vector()
1659 : //
1660 : // The list of derived variable names includes one for each vertex,
1661 : // for higher-order elements we currently only write out vertex
1662 : // values, but this could be changed in the future without too much
1663 : // trouble.
1664 6 : std::vector<std::string> derived_var_names;
1665 :
1666 : // Keep track of mapping from derived_name to (orig_name, node_id)
1667 : // pair. We will use this later to determine whether a given
1668 : // variable is active on a given subdomain.
1669 : std::map<std::string, std::pair<std::string, unsigned int>>
1670 4 : derived_name_to_orig_name_and_node_id;
1671 :
1672 124 : for (const auto & pr : subdomain_id_to_vertices_per_elem)
1673 : {
1674 54 : const subdomain_id_type sbd_id = pr.first;
1675 : const unsigned int vertices_per_elem =
1676 54 : subdomain_id_to_vertices_per_elem[sbd_id];
1677 :
1678 58 : std::ostringstream oss;
1679 486 : for (unsigned int n=0; n<vertices_per_elem; ++n)
1680 3024 : for (const auto & orig_var_name : var_names)
1681 : {
1682 2592 : oss.str("");
1683 2592 : oss.clear();
1684 96 : oss << orig_var_name << var_suffix << n;
1685 192 : std::string derived_name = oss.str();
1686 :
1687 : // Only add this var name if it's not already in the list.
1688 2592 : if (!std::count(derived_var_names.begin(), derived_var_names.end(), derived_name))
1689 : {
1690 2592 : derived_var_names.push_back(derived_name);
1691 : // Add entry for derived_name -> (orig_name, node_id) mapping.
1692 2592 : derived_name_to_orig_name_and_node_id[derived_name] =
1693 2688 : std::make_pair(orig_var_name, n);
1694 : }
1695 : }
1696 50 : }
1697 :
1698 : // For each derived variable name, determine whether it is active
1699 : // based on how many nodes/vertices the elements in a given subdomain have,
1700 : // and whether they were active on the subdomain to begin with.
1701 : std::vector<std::set<subdomain_id_type>>
1702 76 : derived_vars_active_subdomains(derived_var_names.size());
1703 :
1704 : // A new data structure for keeping track of a list of variable names
1705 : // that are in the discontinuous solution vector on each subdomain. Used
1706 : // for indexing. Note: if a variable was inactive at the System level,
1707 : // an entry for it will still be in the discontinuous solution vector,
1708 : // but it will just have a value of zero. On the other hand, when we
1709 : // create the derived variable names some of them are "inactive" on
1710 : // different subdomains in the sense that they don't exist at all, i.e.
1711 : // there is no zero padding for them. We need to be able to distinguish
1712 : // between these two types in order to do the indexing into this vector
1713 : // correctly.
1714 : std::map<subdomain_id_type, std::vector<std::string>>
1715 4 : subdomain_to_var_names;
1716 :
1717 2662 : for (auto derived_var_id : index_range(derived_var_names))
1718 : {
1719 192 : const auto & derived_name = derived_var_names[derived_var_id];
1720 96 : const auto & [orig_name, node_id] =
1721 2592 : libmesh_map_find (derived_name_to_orig_name_and_node_id,
1722 : derived_name);
1723 :
1724 : // For each subdomain, determine whether the current variable
1725 : // should be active on that subdomain.
1726 5184 : for (const auto & pr : subdomain_id_to_vertices_per_elem)
1727 : {
1728 : // Convenience variables for the current subdomain and the
1729 : // number of nodes elements in this subdomain have.
1730 2592 : subdomain_id_type sbd_id = pr.first;
1731 : unsigned int vertices_per_elem_this_sbd =
1732 2592 : subdomain_id_to_vertices_per_elem[sbd_id];
1733 :
1734 : // Check whether variable orig_name was active on this
1735 : // subdomain to begin with by looking in the
1736 : // vars_active_subdomains container. We assume that the
1737 : // location of orig_name in the var_names vector matches its
1738 : // index in the vars_active_subdomains container.
1739 2592 : auto var_loc = std::find(var_names.begin(), var_names.end(), orig_name);
1740 2592 : libmesh_error_msg_if(var_loc == var_names.end(),
1741 : "Variable " << orig_name << " somehow not found in var_names array.");
1742 96 : auto var_id = std::distance(var_names.begin(), var_loc);
1743 :
1744 : // The derived_var will only be active if this subdomain has
1745 : // enough vertices for that to be the case.
1746 2592 : if (node_id < vertices_per_elem_this_sbd)
1747 : {
1748 : // Regardless of whether the original variable was not active on this subdomain,
1749 : // the discontinuous solution vector will have zero padding for it, and
1750 : // we will need to account for it. Therefore it should still be added to
1751 : // the subdomain_to_var_names data structure!
1752 2592 : subdomain_to_var_names[sbd_id].push_back(derived_name);
1753 :
1754 : // If the original variable was not active on the
1755 : // current subdomain, it should not be added to the
1756 : // derived_vars_active_subdomains data structure, since
1757 : // it will not be written to the Exodus file.
1758 :
1759 : // Determine if the original variable was active on the
1760 : // current subdomain.
1761 : bool orig_var_active =
1762 2688 : (vars_active_subdomains[var_id].empty() ||
1763 0 : vars_active_subdomains[var_id].count(sbd_id));
1764 :
1765 : // And only if it was, add it to the
1766 : // derived_vars_active_subdomains data structure.
1767 96 : if (orig_var_active)
1768 2592 : derived_vars_active_subdomains[derived_var_id].insert(sbd_id);
1769 : }
1770 : } // end loop over subdomain_id_to_vertices_per_elem
1771 : } // end loop over derived_var_names
1772 :
1773 : // At this point we've built the "true" list of derived names, but
1774 : // if there are any elemental data variables in this list, we now want
1775 : // to remove all but one copy of them from the derived_var_names list,
1776 : // and rename them in (but not remove them from) the subdomain_to_var_names
1777 : // list, and then update the derived_vars_active_subdomains containers
1778 : // before finally calling the Exodus helper functions.
1779 2662 : for (auto & derived_var_name : derived_var_names)
1780 : {
1781 : // Get the original name associated with this derived name.
1782 : const auto & name_and_id =
1783 2592 : libmesh_map_find (derived_name_to_orig_name_and_node_id,
1784 : derived_var_name);
1785 :
1786 : // Convenience variables for the map entry's contents.
1787 2592 : const std::string & orig_name = name_and_id.first;
1788 :
1789 : // Was the original name an elemental data variable?
1790 2592 : if (std::count(elemental_var_names.begin(),
1791 : elemental_var_names.end(),
1792 96 : orig_name))
1793 : {
1794 : // Rename this variable in the subdomain_to_var_names vectors.
1795 4320 : for (auto & pr : subdomain_to_var_names)
1796 : {
1797 : // Reference to ordered list of variable names on this subdomain.
1798 80 : auto & name_vec = pr.second;
1799 :
1800 : auto name_vec_it =
1801 2000 : std::find(name_vec.begin(),
1802 : name_vec.end(),
1803 160 : derived_var_name);
1804 :
1805 2160 : if (name_vec_it != name_vec.end())
1806 : {
1807 : // Actually rename it back to the orig_name, dropping
1808 : // the "_elem_corner_" stuff.
1809 80 : *name_vec_it = orig_name;
1810 : }
1811 : }
1812 :
1813 : // Finally, rename the variable in the derived_var_names vector itself.
1814 80 : derived_var_name = orig_name;
1815 : } // if (elemental data)
1816 : } // end loop over derived names
1817 :
1818 : // Now remove duplicate entries from derived_var_names after the first.
1819 : // Also update the derived_vars_active_subdomains container in a consistent way.
1820 : {
1821 6 : std::vector<std::string> derived_var_names_edited;
1822 6 : std::vector<std::set<subdomain_id_type>> derived_vars_active_subdomains_edited;
1823 74 : std::vector<unsigned int> found_first(elemental_var_names.size());
1824 :
1825 2662 : for (auto i : index_range(derived_var_names))
1826 : {
1827 192 : const auto & derived_var_name = derived_var_names[i];
1828 192 : const auto & active_set = derived_vars_active_subdomains[i];
1829 :
1830 : // Determine whether we will keep this derived variable name in
1831 : // the final container.
1832 96 : bool keep = true;
1833 15552 : for (auto j : index_range(elemental_var_names))
1834 12480 : if (derived_var_name == elemental_var_names[j])
1835 : {
1836 2240 : if (!found_first[j])
1837 270 : found_first[j] = 1;
1838 :
1839 : else
1840 70 : keep = false;
1841 : }
1842 :
1843 : // We also don't keep variables that are not active on any subdomains.
1844 : // Contrary to other uses of the var_active_subdomains container where
1845 : // the empty set means "all" subdomains, here it really means "none".
1846 2592 : if (active_set.empty())
1847 0 : keep = false;
1848 :
1849 2592 : if (keep)
1850 : {
1851 702 : derived_var_names_edited.push_back(derived_var_name);
1852 702 : derived_vars_active_subdomains_edited.push_back(active_set);
1853 : }
1854 : }
1855 :
1856 : // We built the filtered ranges, now swap them with the originals.
1857 2 : derived_var_names.swap(derived_var_names_edited);
1858 2 : derived_vars_active_subdomains.swap(derived_vars_active_subdomains_edited);
1859 66 : }
1860 :
1861 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
1862 : // Build complex variable names "r_foo", "i_foo", "a_foo" and the lists of
1863 : // subdomains on which they are active.
1864 : auto complex_var_names =
1865 : exio_helper->get_complex_names(derived_var_names,
1866 0 : _write_complex_abs);
1867 : auto complex_vars_active_subdomains =
1868 : exio_helper->get_complex_vars_active_subdomains(derived_vars_active_subdomains,
1869 0 : _write_complex_abs);
1870 : auto complex_subdomain_to_var_names =
1871 : exio_helper->get_complex_subdomain_to_var_names(subdomain_to_var_names,
1872 0 : _write_complex_abs);
1873 :
1874 : // Make expanded version of vector "v" in which each entry in the
1875 : // original expands to an ("r_", "i_", "a_") triple.
1876 : // "nco" is the number of complex outputs, which depends on whether
1877 : // or not we are writing out the complex magnitudes.
1878 : std::vector<Real> complex_v;
1879 0 : int nco = _write_complex_abs ? 3 : 2;
1880 0 : complex_v.reserve(nco * v.size());
1881 0 : for (const auto & val : v)
1882 : {
1883 0 : complex_v.push_back(val.real());
1884 0 : complex_v.push_back(val.imag());
1885 0 : if (_write_complex_abs)
1886 0 : complex_v.push_back(std::abs(val));
1887 : }
1888 :
1889 : // Finally, initialize storage for the variables and write them to file.
1890 : exio_helper->initialize_element_variables
1891 0 : (complex_var_names, complex_vars_active_subdomains);
1892 : exio_helper->write_element_values_element_major
1893 0 : (mesh, complex_v, _timestep,
1894 : complex_vars_active_subdomains,
1895 : complex_var_names,
1896 : complex_subdomain_to_var_names);
1897 : #else
1898 :
1899 : // Call function which writes the derived variable names to the
1900 : // Exodus file.
1901 70 : exio_helper->initialize_element_variables(derived_var_names, derived_vars_active_subdomains);
1902 :
1903 : // ES::build_discontinuous_solution_vector() creates a vector with
1904 : // an element-major ordering, so call Helper::write_element_values()
1905 : // passing false for the last argument.
1906 : exio_helper->write_element_values_element_major
1907 70 : (mesh, v, _timestep,
1908 : derived_vars_active_subdomains,
1909 : derived_var_names,
1910 : subdomain_to_var_names);
1911 : #endif
1912 268 : }
1913 :
1914 :
1915 :
1916 48039 : void ExodusII_IO::write_nodal_data (const std::string & fname,
1917 : const std::vector<Number> & soln,
1918 : const std::vector<std::string> & names)
1919 : {
1920 2097 : LOG_SCOPE("write_nodal_data()", "ExodusII_IO");
1921 :
1922 2796 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
1923 :
1924 2796 : int num_vars = cast_int<int>(names.size());
1925 48039 : dof_id_type num_nodes = mesh.n_nodes();
1926 :
1927 : // The names of the variables to be output
1928 2097 : std::vector<std::string> output_names;
1929 :
1930 48039 : if (_allow_empty_variables || !_output_variables.empty())
1931 0 : output_names = _output_variables;
1932 : else
1933 48039 : output_names = names;
1934 :
1935 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
1936 : std::vector<std::string> complex_names =
1937 : exio_helper->get_complex_names(output_names,
1938 249 : _write_complex_abs);
1939 :
1940 : // Call helper function for opening/initializing data, giving it the
1941 : // complex variable names
1942 498 : this->write_nodal_data_common(fname, complex_names, /*continuous=*/true);
1943 : #else
1944 : // Call helper function for opening/initializing data
1945 92784 : this->write_nodal_data_common(fname, output_names, /*continuous=*/true);
1946 : #endif
1947 :
1948 49437 : if (mesh.processor_id())
1949 1398 : return;
1950 :
1951 : // This will count the number of variables actually output
1952 22210 : for (int c=0; c<num_vars; c++)
1953 : {
1954 15702 : std::stringstream name_to_find;
1955 :
1956 : std::vector<std::string>::iterator pos =
1957 15702 : std::find(output_names.begin(), output_names.end(), names[c]);
1958 14409 : if (pos == output_names.end())
1959 0 : continue;
1960 :
1961 : unsigned int variable_name_position =
1962 1293 : cast_int<unsigned int>(pos - output_names.begin());
1963 :
1964 : // Set up temporary vectors to be passed to Exodus to write the
1965 : // nodal values for a single variable at a time.
1966 : #ifdef LIBMESH_USE_REAL_NUMBERS
1967 2586 : std::vector<Number> cur_soln;
1968 :
1969 : // num_nodes is either exactly how much space we will need for
1970 : // each vector, or a safe upper bound for the amount of memory
1971 : // we will require when there are gaps in the numbering.
1972 13664 : cur_soln.reserve(num_nodes);
1973 : #else
1974 : std::vector<Real> real_parts;
1975 : std::vector<Real> imag_parts;
1976 : std::vector<Real> magnitudes;
1977 745 : real_parts.reserve(num_nodes);
1978 745 : imag_parts.reserve(num_nodes);
1979 745 : if (_write_complex_abs)
1980 745 : magnitudes.reserve(num_nodes);
1981 : #endif
1982 :
1983 : // There could be gaps in soln based on node numbering, but in
1984 : // serial the empty numbers are left empty.
1985 : // There could also be offsets in soln based on "fake" nodes
1986 : // inserted on each processor (because NumericVector indices
1987 : // have to be contiguous); the helper keeps track of those.
1988 : // We now copy the proper solution values contiguously into
1989 : // "cur_soln", removing the gaps.
1990 46342591 : for (const auto & node : mesh.node_ptr_range())
1991 : {
1992 : const dof_id_type idx =
1993 25251548 : (exio_helper->node_id_to_vec_id(node->id()))
1994 25251548 : * num_vars + c;
1995 : #ifdef LIBMESH_USE_REAL_NUMBERS
1996 25746737 : cur_soln.push_back(soln[idx]);
1997 : #else
1998 1598826 : real_parts.push_back(soln[idx].real());
1999 1598826 : imag_parts.push_back(soln[idx].imag());
2000 1598826 : if (_write_complex_abs)
2001 1598826 : magnitudes.push_back(std::abs(soln[idx]));
2002 : #endif
2003 11823 : }
2004 :
2005 : // If we're adding extra sides, we need to add their data too.
2006 : //
2007 : // Because soln was created from a parallel NumericVector, its
2008 : // numbering was contiguous on each processor; we need to use
2009 : // the same offsets here, and we need to loop through elements
2010 : // from earlier ranks first.
2011 14409 : if (exio_helper->get_add_sides())
2012 : {
2013 : std::vector<std::vector<const Elem *>>
2014 375 : elems_by_pid(mesh.n_processors());
2015 :
2016 3655 : for (const auto & elem : mesh.active_element_ptr_range())
2017 2070 : elems_by_pid[elem->processor_id()].push_back(elem);
2018 :
2019 2075 : for (auto p : index_range(elems_by_pid))
2020 : {
2021 : dof_id_type global_idx =
2022 1775 : exio_helper->added_node_offset_on(p) * num_vars + c;
2023 3455 : for (const Elem * elem : elems_by_pid[p])
2024 : {
2025 8880 : for (auto s : elem->side_index_range())
2026 : {
2027 7200 : if (EquationSystems::redundant_added_side(*elem,s))
2028 1944 : continue;
2029 :
2030 : const std::vector<unsigned int> side_nodes =
2031 5694 : elem->nodes_on_side(s);
2032 :
2033 38736 : for (auto n : index_range(side_nodes))
2034 : {
2035 2790 : libmesh_ignore(n);
2036 2790 : libmesh_assert_less(global_idx, soln.size());
2037 : #ifdef LIBMESH_USE_REAL_NUMBERS
2038 33480 : cur_soln.push_back(soln[global_idx]);
2039 : #else
2040 2790 : real_parts.push_back(soln[global_idx].real());
2041 2790 : imag_parts.push_back(soln[global_idx].imag());
2042 2790 : if (_write_complex_abs)
2043 2790 : magnitudes.push_back(std::abs(soln[global_idx]));
2044 : #endif
2045 33480 : global_idx += num_vars;
2046 : }
2047 : }
2048 : }
2049 : }
2050 250 : }
2051 :
2052 : // Finally, actually call the Exodus API to write to file.
2053 : #ifdef LIBMESH_USE_REAL_NUMBERS
2054 13664 : exio_helper->write_nodal_values(variable_name_position+1, cur_soln, _timestep);
2055 : #else
2056 745 : int nco = _write_complex_abs ? 3 : 2;
2057 745 : exio_helper->write_nodal_values(nco*variable_name_position+1, real_parts, _timestep);
2058 745 : exio_helper->write_nodal_values(nco*variable_name_position+2, imag_parts, _timestep);
2059 745 : if (_write_complex_abs)
2060 745 : exio_helper->write_nodal_values(3*variable_name_position+3, magnitudes, _timestep);
2061 : #endif
2062 :
2063 11823 : }
2064 45243 : }
2065 :
2066 :
2067 :
2068 :
2069 0 : void ExodusII_IO::write_information_records (const std::vector<std::string> & records)
2070 : {
2071 0 : if (MeshOutput<MeshBase>::mesh().processor_id())
2072 0 : return;
2073 :
2074 0 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2075 : "ERROR, ExodusII file must be initialized before outputting information records.");
2076 :
2077 0 : exio_helper->write_information_records(records);
2078 : }
2079 :
2080 :
2081 :
2082 0 : void ExodusII_IO::write_global_data (const std::vector<Number> & soln,
2083 : const std::vector<std::string> & names)
2084 : {
2085 0 : LOG_SCOPE("write_global_data()", "ExodusII_IO");
2086 :
2087 0 : if (MeshOutput<MeshBase>::mesh().processor_id())
2088 0 : return;
2089 :
2090 0 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2091 : "ERROR, ExodusII file must be initialized before outputting global variables.");
2092 :
2093 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
2094 :
2095 : std::vector<std::string> complex_names =
2096 : exio_helper->get_complex_names(names,
2097 0 : _write_complex_abs);
2098 :
2099 0 : exio_helper->initialize_global_variables(complex_names);
2100 :
2101 : const std::vector<Real> complex_soln =
2102 0 : complex_soln_components(soln, names.size(), _write_complex_abs,
2103 : SolutionOrdering::variable_major,
2104 0 : SolutionOrdering::variable_major);
2105 :
2106 0 : exio_helper->write_global_values(complex_soln, _timestep);
2107 :
2108 : #else
2109 0 : exio_helper->initialize_global_variables(names);
2110 0 : exio_helper->write_global_values(soln, _timestep);
2111 : #endif
2112 0 : }
2113 :
2114 :
2115 :
2116 48039 : void ExodusII_IO::write_timestep (const std::string & fname,
2117 : const EquationSystems & es,
2118 : const int timestep,
2119 : const Real time,
2120 : const std::set<std::string> * system_names)
2121 : {
2122 48039 : _timestep = timestep;
2123 48039 : MeshOutput<MeshBase>::write_equation_systems(fname,es,system_names);
2124 :
2125 49437 : if (MeshOutput<MeshBase>::mesh().processor_id())
2126 699 : return;
2127 :
2128 7801 : exio_helper->write_timestep(timestep, time);
2129 : }
2130 :
2131 :
2132 12959 : void ExodusII_IO::write_equation_systems (const std::string & fname,
2133 : const EquationSystems & es,
2134 : const std::set<std::string> * system_names)
2135 : {
2136 12959 : write_timestep(fname, es, 1, 0, system_names);
2137 12959 : }
2138 :
2139 :
2140 0 : void ExodusII_IO::write_elemsets()
2141 : {
2142 0 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2143 : "ERROR, ExodusII file must be opened for writing "
2144 : "before calling ExodusII_IO::write_elemsets()!");
2145 :
2146 0 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2147 0 : exio_helper->write_elemsets(mesh);
2148 0 : }
2149 :
2150 : void
2151 71 : ExodusII_IO::
2152 : write_sideset_data(int timestep,
2153 : const std::vector<std::string> & var_names,
2154 : const std::vector<std::set<boundary_id_type>> & side_ids,
2155 : const std::vector<std::map<BoundaryInfo::BCTuple, Real>> & bc_vals)
2156 : {
2157 71 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2158 : "ERROR, ExodusII file must be opened for writing "
2159 : "before calling ExodusII_IO::write_sideset_data()!");
2160 :
2161 4 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2162 71 : exio_helper->write_sideset_data(mesh, timestep, var_names, side_ids, bc_vals);
2163 71 : }
2164 :
2165 :
2166 :
2167 : void
2168 71 : ExodusII_IO::
2169 : read_sideset_data(int timestep,
2170 : std::vector<std::string> & var_names,
2171 : std::vector<std::set<boundary_id_type>> & side_ids,
2172 : std::vector<std::map<BoundaryInfo::BCTuple, Real>> & bc_vals)
2173 : {
2174 71 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2175 : "ERROR, ExodusII file must be opened for reading "
2176 : "before calling ExodusII_IO::read_sideset_data()!");
2177 :
2178 4 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2179 71 : exio_helper->read_sideset_data(mesh, timestep, var_names, side_ids, bc_vals);
2180 71 : }
2181 :
2182 :
2183 :
2184 : void
2185 142 : ExodusII_IO::
2186 : get_sideset_data_indices (std::map<BoundaryInfo::BCTuple, unsigned int> & bc_array_indices)
2187 :
2188 : {
2189 142 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2190 : "ERROR, ExodusII file must be opened for reading "
2191 : "before calling ExodusII_IO::get_sideset_data_indices()!");
2192 :
2193 8 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2194 142 : exio_helper->get_sideset_data_indices(mesh, bc_array_indices);
2195 142 : }
2196 :
2197 : void
2198 142 : ExodusII_IO::
2199 : get_nodeset_data_indices (std::map<BoundaryInfo::NodeBCTuple, unsigned int> & bc_array_indices)
2200 : {
2201 142 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2202 : "ERROR, ExodusII file must be opened for reading "
2203 : "before calling ExodusII_IO::get_nodeset_data_indices()!");
2204 :
2205 142 : exio_helper->get_nodeset_data_indices(bc_array_indices);
2206 142 : }
2207 :
2208 : void
2209 71 : ExodusII_IO::
2210 : write_nodeset_data (int timestep,
2211 : const std::vector<std::string> & var_names,
2212 : const std::vector<std::set<boundary_id_type>> & node_boundary_ids,
2213 : const std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> & bc_vals)
2214 : {
2215 71 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2216 : "ERROR, ExodusII file must be opened for writing "
2217 : "before calling ExodusII_IO::write_nodeset_data()!");
2218 :
2219 71 : exio_helper->write_nodeset_data(timestep, var_names, node_boundary_ids, bc_vals);
2220 71 : }
2221 :
2222 :
2223 :
2224 : void
2225 71 : ExodusII_IO::
2226 : read_nodeset_data (int timestep,
2227 : std::vector<std::string> & var_names,
2228 : std::vector<std::set<boundary_id_type>> & node_boundary_ids,
2229 : std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> & bc_vals)
2230 : {
2231 71 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2232 : "ERROR, ExodusII file must be opened for reading "
2233 : "before calling ExodusII_IO::read_nodeset_data()!");
2234 :
2235 71 : exio_helper->read_nodeset_data(timestep, var_names, node_boundary_ids, bc_vals);
2236 71 : }
2237 :
2238 : void
2239 71 : ExodusII_IO::
2240 : write_elemset_data (int timestep,
2241 : const std::vector<std::string> & var_names,
2242 : const std::vector<std::set<elemset_id_type>> & elemset_ids_in,
2243 : const std::vector<std::map<std::pair<dof_id_type, elemset_id_type>, Real>> & elemset_vals)
2244 : {
2245 71 : libmesh_error_msg_if(!exio_helper->opened_for_writing,
2246 : "ERROR, ExodusII file must be opened for writing "
2247 : "before calling ExodusII_IO::write_elemset_data()!");
2248 :
2249 71 : exio_helper->write_elemset_data(timestep, var_names, elemset_ids_in, elemset_vals);
2250 71 : }
2251 :
2252 :
2253 :
2254 : void
2255 71 : ExodusII_IO::
2256 : read_elemset_data (int timestep,
2257 : std::vector<std::string> & var_names,
2258 : std::vector<std::set<elemset_id_type>> & elemset_ids_in,
2259 : std::vector<std::map<std::pair<dof_id_type, elemset_id_type>, Real>> & elemset_vals)
2260 : {
2261 71 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2262 : "ERROR, ExodusII file must be opened for reading "
2263 : "before calling ExodusII_IO::read_elemset_data()!");
2264 :
2265 71 : exio_helper->read_elemset_data(timestep, var_names, elemset_ids_in, elemset_vals);
2266 71 : }
2267 :
2268 : void
2269 71 : ExodusII_IO::get_elemset_data_indices (std::map<std::pair<dof_id_type, elemset_id_type>, unsigned int> & elemset_array_indices)
2270 : {
2271 71 : libmesh_error_msg_if(!exio_helper->opened_for_reading,
2272 : "ERROR, ExodusII file must be opened for reading "
2273 : "before calling ExodusII_IO::get_elemset_data_indices()!");
2274 :
2275 71 : exio_helper->get_elemset_data_indices(elemset_array_indices);
2276 71 : }
2277 :
2278 :
2279 2875 : void ExodusII_IO::write (const std::string & fname)
2280 : {
2281 264 : LOG_SCOPE("write()", "ExodusII_IO");
2282 :
2283 176 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2284 :
2285 : // We may need to gather a DistributedMesh to output it, making that
2286 : // const qualifier in our constructor a dirty lie
2287 : // The "true" specifies that we only need the mesh serialized to processor 0
2288 : MeshSerializer serialize
2289 : (const_cast<MeshBase &>(mesh),
2290 3051 : !MeshOutput<MeshBase>::_is_parallel_format, true);
2291 :
2292 88 : libmesh_assert( !exio_helper->opened_for_writing );
2293 :
2294 : // If the user has set the append flag here, it doesn't really make
2295 : // sense: the intent of this function is to write a Mesh with no
2296 : // data, while "appending" is really intended to add data to an
2297 : // existing file. If we're verbose, print a message to this effect.
2298 88 : if (_append && _verbose)
2299 : libmesh_warning("Warning: Appending in ExodusII_IO::write() does not make sense.\n"
2300 : "Creating a new file instead!");
2301 :
2302 5662 : exio_helper->create(fname);
2303 5662 : exio_helper->initialize(fname,mesh);
2304 2875 : exio_helper->write_nodal_coordinates(mesh);
2305 2875 : exio_helper->write_elements(mesh);
2306 2875 : exio_helper->write_sidesets(mesh);
2307 2875 : exio_helper->write_nodesets(mesh);
2308 2875 : exio_helper->write_elemsets(mesh);
2309 :
2310 2875 : if ((mesh.get_boundary_info().n_edge_conds() > 0) && _verbose)
2311 : libmesh_warning("Warning: Mesh contains edge boundary IDs, but these "
2312 : "are not supported by the ExodusII format.");
2313 2875 : }
2314 :
2315 :
2316 :
2317 1491 : void ExodusII_IO::write_nodal_data_discontinuous (const std::string & fname,
2318 : const std::vector<Number> & soln,
2319 : const std::vector<std::string> & names)
2320 : {
2321 63 : LOG_SCOPE("write_nodal_data_discontinuous()", "ExodusII_IO");
2322 :
2323 84 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2324 :
2325 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
2326 :
2327 : std::vector<std::string> complex_names =
2328 : exio_helper->get_complex_names(names,
2329 21 : _write_complex_abs);
2330 :
2331 : // Call helper function for opening/initializing data, giving it the
2332 : // complex variable names
2333 42 : this->write_nodal_data_common(fname, complex_names, /*continuous=*/false);
2334 : #else
2335 : // Call helper function for opening/initializing data
2336 2856 : this->write_nodal_data_common(fname, names, /*continuous=*/false);
2337 : #endif
2338 :
2339 1533 : if (mesh.processor_id())
2340 42 : return;
2341 :
2342 42 : int num_vars = cast_int<int>(names.size());
2343 21 : libmesh_assert_equal_to(soln.size() % num_vars, 0);
2344 273 : int num_nodes = soln.size() / num_vars;
2345 21 : libmesh_assert_equal_to(exio_helper->num_nodes, num_nodes);
2346 :
2347 : #ifndef NDEBUG
2348 21 : if (!this->get_add_sides())
2349 : {
2350 4 : int num_real_nodes = 0;
2351 4040 : for (const auto & elem : mesh.active_element_ptr_range())
2352 4036 : num_real_nodes += elem->n_nodes();
2353 4 : libmesh_assert_equal_to(num_real_nodes, num_nodes);
2354 : }
2355 : #endif
2356 :
2357 708 : for (int c=0; c<num_vars; c++)
2358 : {
2359 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
2360 38 : std::vector<Real> real_parts(num_nodes);
2361 38 : std::vector<Real> imag_parts(num_nodes);
2362 : std::vector<Real> magnitudes;
2363 38 : if (_write_complex_abs)
2364 38 : magnitudes.resize(num_nodes);
2365 :
2366 : // The number of complex outputs depends on whether or not we are
2367 : // writing out the absolute values.
2368 38 : int nco = _write_complex_abs ? 3 : 2;
2369 :
2370 110896 : for (int i=0; i<num_nodes; ++i)
2371 : {
2372 110858 : real_parts[i] = soln[i*num_vars + c].real();
2373 110858 : imag_parts[i] = soln[i*num_vars + c].imag();
2374 110858 : if (_write_complex_abs)
2375 110858 : magnitudes[i] = std::abs(soln[i*num_vars + c]);
2376 : }
2377 38 : exio_helper->write_nodal_values(nco*c+1, real_parts, _timestep);
2378 38 : exio_helper->write_nodal_values(nco*c+2, imag_parts, _timestep);
2379 38 : if (_write_complex_abs)
2380 38 : exio_helper->write_nodal_values(3*c+3, magnitudes, _timestep);
2381 : #else
2382 : // Copy out this variable's solution
2383 456 : std::vector<Number> cur_soln(num_nodes);
2384 :
2385 677280 : for (int i=0; i<num_nodes; i++)
2386 898578 : cur_soln[i] = soln[i*num_vars + c];
2387 :
2388 418 : exio_helper->write_nodal_values(c+1,cur_soln,_timestep);
2389 : #endif
2390 : }
2391 21 : }
2392 :
2393 :
2394 :
2395 49530 : void ExodusII_IO::write_nodal_data_common(std::string fname,
2396 : const std::vector<std::string> & names,
2397 : bool continuous)
2398 : {
2399 2880 : const MeshBase & mesh = MeshOutput<MeshBase>::mesh();
2400 :
2401 : // This function can be called multiple times, we only want to open
2402 : // the ExodusII file the first time it's called.
2403 49530 : if (!exio_helper->opened_for_writing)
2404 : {
2405 : // If we're appending, open() the file with read_only=false,
2406 : // otherwise create() it and write the contents of the mesh to
2407 : // it.
2408 18911 : if (_append)
2409 : {
2410 : // We do our writing only from proc 0, to avoid race
2411 : // conditions with Exodus 8
2412 365 : if (!MeshOutput<MeshBase>::mesh().processor_id())
2413 : {
2414 60 : exio_helper->open(fname.c_str(), /*read_only=*/false);
2415 : // If we're appending, it's not valid to call exio_helper->initialize()
2416 : // or exio_helper->initialize_nodal_variables(), but we do need to set up
2417 : // certain aspects of the Helper object itself, such as the number of nodes
2418 : // and elements. We do that by reading the header...
2419 60 : exio_helper->read_and_store_header_info();
2420 :
2421 : // ...and reading the block info
2422 60 : exio_helper->read_block_info();
2423 : }
2424 : // Keep other processors aware of what we've done on root
2425 : else
2426 : {
2427 295 : exio_helper->opened_for_writing = true;
2428 295 : exio_helper->current_filename = fname;
2429 : }
2430 : }
2431 : else
2432 : {
2433 36556 : exio_helper->create(fname);
2434 :
2435 : // But some of our write calls are parallel-only, due to
2436 : // calls to parallel-only getter functions.
2437 36556 : exio_helper->initialize(fname, mesh, !continuous);
2438 :
2439 18556 : exio_helper->write_nodal_coordinates(mesh, !continuous);
2440 18556 : exio_helper->write_elements(mesh, !continuous);
2441 :
2442 18556 : exio_helper->write_sidesets(mesh);
2443 18556 : exio_helper->write_nodesets(mesh);
2444 18556 : exio_helper->write_elemsets(mesh);
2445 :
2446 18556 : exio_helper->initialize_nodal_variables(names);
2447 : }
2448 : }
2449 : else
2450 : {
2451 : // We are already open for writing, so check that the filename
2452 : // passed to this function matches the filename currently in use
2453 : // by the helper.
2454 30619 : libmesh_error_msg_if(fname != exio_helper->current_filename,
2455 : "Error! This ExodusII_IO object is already associated with file: "
2456 : << exio_helper->current_filename
2457 : << ", cannot use it with requested file: "
2458 : << fname);
2459 : }
2460 49530 : }
2461 :
2462 142 : const std::vector<std::string> & ExodusII_IO::get_nodal_var_names()
2463 : {
2464 142 : exio_helper->read_var_names(ExodusII_IO_Helper::NODAL);
2465 142 : return exio_helper->nodal_var_names;
2466 : }
2467 :
2468 0 : const std::vector<std::string> & ExodusII_IO::get_elem_var_names()
2469 : {
2470 0 : exio_helper->read_var_names(ExodusII_IO_Helper::ELEMENTAL);
2471 0 : return exio_helper->elem_var_names;
2472 : }
2473 :
2474 0 : const std::vector<std::string> & ExodusII_IO::get_global_var_names()
2475 : {
2476 0 : exio_helper->read_var_names(ExodusII_IO_Helper::GLOBAL);
2477 0 : return exio_helper->global_var_names;
2478 : }
2479 :
2480 71 : const std::vector<int> & ExodusII_IO::get_elem_num_map() const
2481 : {
2482 : // We could make this function non-const and have it call
2483 : // exio_helper->read_elem_num_map() before returning a reference...
2484 : // but the intention is that this will be called some time after a
2485 : // mesh is read in, in which case it would be doing extra work to
2486 : // read in the elem_num_map twice.
2487 71 : return exio_helper->elem_num_map;
2488 : }
2489 :
2490 71 : const std::vector<int> & ExodusII_IO::get_node_num_map() const
2491 : {
2492 71 : return exio_helper->node_num_map;
2493 : }
2494 :
2495 0 : ExodusII_IO_Helper & ExodusII_IO::get_exio_helper()
2496 : {
2497 : // Provide a warning when accessing the helper object
2498 : // since it is a non-public API and is likely to see
2499 : // future API changes
2500 : libmesh_experimental();
2501 :
2502 0 : return *exio_helper;
2503 : }
2504 :
2505 :
2506 0 : void ExodusII_IO::set_hdf5_writing(bool write_hdf5)
2507 : {
2508 0 : exio_helper->set_hdf5_writing(write_hdf5);
2509 0 : }
2510 :
2511 :
2512 1988 : void ExodusII_IO::set_max_name_length(unsigned int max_length)
2513 : {
2514 1988 : exio_helper->set_max_name_length(max_length);
2515 1988 : }
2516 :
2517 :
2518 8 : void ExodusII_IO::set_discontinuous_bex(bool disc_bex)
2519 : {
2520 8 : _disc_bex = disc_bex;
2521 8 : }
2522 :
2523 :
2524 71 : void ExodusII_IO::write_discontinuous_elemental_data(bool allow_hi_order_elemental)
2525 : {
2526 71 : _allow_hi_order_elemental = allow_hi_order_elemental;
2527 71 : }
2528 :
2529 :
2530 :
2531 : // LIBMESH_HAVE_EXODUS_API is not defined, declare error() versions of functions...
2532 : #else
2533 :
2534 :
2535 :
2536 : ExodusII_IO::~ExodusII_IO () = default;
2537 :
2538 :
2539 :
2540 : void ExodusII_IO::read (const std::string &)
2541 : {
2542 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2543 : }
2544 :
2545 :
2546 :
2547 : ExodusHeaderInfo ExodusII_IO::read_header (const std::string &)
2548 : {
2549 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2550 : }
2551 :
2552 :
2553 :
2554 : void ExodusII_IO::verbose (bool)
2555 : {
2556 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2557 : }
2558 :
2559 :
2560 :
2561 : void ExodusII_IO::use_mesh_dimension_instead_of_spatial_dimension(bool)
2562 : {
2563 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2564 : }
2565 :
2566 :
2567 :
2568 : void ExodusII_IO::write_as_dimension(unsigned)
2569 : {
2570 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2571 : }
2572 :
2573 :
2574 :
2575 : void ExodusII_IO::set_coordinate_offset(Point)
2576 : {
2577 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2578 : }
2579 :
2580 :
2581 :
2582 : void ExodusII_IO::append(bool)
2583 : {
2584 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2585 : }
2586 :
2587 :
2588 :
2589 : void ExodusII_IO::write_added_sides (bool)
2590 : {
2591 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2592 : }
2593 :
2594 :
2595 :
2596 : bool ExodusII_IO::get_add_sides ()
2597 : {
2598 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2599 : return false;
2600 : }
2601 :
2602 :
2603 :
2604 : const std::vector<Real> & ExodusII_IO::get_time_steps()
2605 : {
2606 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2607 : }
2608 :
2609 :
2610 :
2611 : int ExodusII_IO::get_num_time_steps()
2612 : {
2613 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2614 : }
2615 :
2616 :
2617 : void ExodusII_IO::copy_nodal_solution(System &,
2618 : std::string,
2619 : std::string,
2620 : unsigned int)
2621 : {
2622 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2623 : }
2624 :
2625 :
2626 :
2627 : void ExodusII_IO::copy_elemental_solution(System &,
2628 : std::string,
2629 : std::string,
2630 : unsigned int)
2631 : {
2632 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2633 : }
2634 :
2635 :
2636 :
2637 : void ExodusII_IO::copy_scalar_solution(System &,
2638 : std::vector<std::string>,
2639 : std::vector<std::string>,
2640 : unsigned int)
2641 : {
2642 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2643 : }
2644 :
2645 :
2646 :
2647 : void ExodusII_IO::write_element_data (const EquationSystems &)
2648 : {
2649 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2650 : }
2651 :
2652 :
2653 :
2654 : void
2655 : ExodusII_IO::write_element_data_from_discontinuous_nodal_data
2656 : (const EquationSystems &,
2657 : const std::set<std::string> *,
2658 : const std::string & )
2659 : {
2660 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2661 : }
2662 :
2663 :
2664 :
2665 : void ExodusII_IO::write_nodal_data (const std::string &,
2666 : const std::vector<Number> &,
2667 : const std::vector<std::string> &)
2668 : {
2669 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2670 : }
2671 :
2672 :
2673 :
2674 : void ExodusII_IO::write_information_records (const std::vector<std::string> &)
2675 : {
2676 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2677 : }
2678 :
2679 :
2680 :
2681 : void ExodusII_IO::write_global_data (const std::vector<Number> &,
2682 : const std::vector<std::string> &)
2683 : {
2684 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2685 : }
2686 :
2687 :
2688 :
2689 : void ExodusII_IO::write_timestep (const std::string &,
2690 : const EquationSystems &,
2691 : const int,
2692 : const Real,
2693 : const std::set<std::string> *)
2694 : {
2695 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2696 : }
2697 :
2698 :
2699 : void ExodusII_IO::write_equation_systems (const std::string &,
2700 : const EquationSystems &,
2701 : const std::set<std::string> *)
2702 : {
2703 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2704 : }
2705 :
2706 :
2707 : void ExodusII_IO::write_elemsets()
2708 : {
2709 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2710 : }
2711 :
2712 : void
2713 : ExodusII_IO::
2714 : write_sideset_data (int,
2715 : const std::vector<std::string> &,
2716 : const std::vector<std::set<boundary_id_type>> &,
2717 : const std::vector<std::map<BoundaryInfo::BCTuple, Real>> &)
2718 : {
2719 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2720 : }
2721 :
2722 :
2723 :
2724 : void
2725 : ExodusII_IO::
2726 : read_sideset_data (int,
2727 : std::vector<std::string> &,
2728 : std::vector<std::set<boundary_id_type>> &,
2729 : std::vector<std::map<BoundaryInfo::BCTuple, Real>> &)
2730 : {
2731 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2732 : }
2733 :
2734 : void
2735 : ExodusII_IO::
2736 : get_sideset_data_indices (std::map<BoundaryInfo::BCTuple, unsigned int> &)
2737 :
2738 : {
2739 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2740 : }
2741 :
2742 : void
2743 : ExodusII_IO::
2744 : write_nodeset_data (int,
2745 : const std::vector<std::string> &,
2746 : const std::vector<std::set<boundary_id_type>> &,
2747 : const std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> &)
2748 : {
2749 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2750 : }
2751 :
2752 : void
2753 : ExodusII_IO::
2754 : read_nodeset_data (int,
2755 : std::vector<std::string> &,
2756 : std::vector<std::set<boundary_id_type>> &,
2757 : std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> &)
2758 : {
2759 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2760 : }
2761 :
2762 : void
2763 : ExodusII_IO::
2764 : get_nodeset_data_indices (std::map<BoundaryInfo::NodeBCTuple, unsigned int> &)
2765 : {
2766 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2767 : }
2768 :
2769 : void
2770 : ExodusII_IO::
2771 : write_elemset_data (int,
2772 : const std::vector<std::string> &,
2773 : const std::vector<std::set<elemset_id_type>> &,
2774 : const std::vector<std::map<std::pair<dof_id_type, elemset_id_type>, Real>> &)
2775 : {
2776 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2777 : }
2778 :
2779 : void
2780 : ExodusII_IO::
2781 : read_elemset_data (int,
2782 : std::vector<std::string> &,
2783 : std::vector<std::set<elemset_id_type>> &,
2784 : std::vector<std::map<std::pair<dof_id_type, elemset_id_type>, Real>> &)
2785 : {
2786 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2787 : }
2788 :
2789 : void
2790 : ExodusII_IO::
2791 : get_elemset_data_indices (std::map<std::pair<dof_id_type, elemset_id_type>, unsigned int> &)
2792 : {
2793 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2794 : }
2795 :
2796 : void ExodusII_IO::write (const std::string &)
2797 : {
2798 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2799 : }
2800 :
2801 :
2802 :
2803 : void ExodusII_IO::write_nodal_data_discontinuous (const std::string &,
2804 : const std::vector<Number> &,
2805 : const std::vector<std::string> &)
2806 : {
2807 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2808 : }
2809 :
2810 :
2811 :
2812 : void ExodusII_IO::write_nodal_data_common(std::string,
2813 : const std::vector<std::string> &,
2814 : bool)
2815 : {
2816 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2817 : }
2818 :
2819 :
2820 : const std::vector<std::string> & ExodusII_IO::get_elem_var_names()
2821 : {
2822 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2823 : }
2824 :
2825 : const std::vector<std::string> & ExodusII_IO::get_nodal_var_names()
2826 : {
2827 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2828 : }
2829 :
2830 : const std::vector<std::string> & ExodusII_IO::get_global_var_names()
2831 : {
2832 : libmesh_error_msg("ERROR, ExodusII API is not defined.");
2833 : }
2834 :
2835 : void ExodusII_IO::set_hdf5_writing(bool) {}
2836 :
2837 : #endif // LIBMESH_HAVE_EXODUS_API
2838 : } // namespace libMesh
|