libMesh
exodusII_io.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 // C++ includes
20 #include <fstream>
21 #include <cstring>
22 #include <sstream>
23 #include <map>
24 
25 // Local includes
26 #include "libmesh/exodusII_io.h"
27 #include "libmesh/boundary_info.h"
28 #include "libmesh/mesh_base.h"
29 #include "libmesh/enum_elem_type.h"
30 #include "libmesh/elem.h"
31 #include "libmesh/equation_systems.h"
32 #include "libmesh/libmesh_logging.h"
33 #include "libmesh/system.h"
34 #include "libmesh/numeric_vector.h"
35 #include "libmesh/exodusII_io_helper.h"
36 #include "libmesh/string_to_enum.h"
37 #include "libmesh/mesh_communication.h"
38 #include "libmesh/parallel_mesh.h"
39 
40 namespace libMesh
41 {
42 
43 // ------------------------------------------------------------
44 // ExodusII_IO class members
46 #ifdef LIBMESH_HAVE_EXODUS_API
47  bool single_precision
48 #else
49  bool
50 #endif
51  ) :
52  MeshInput<MeshBase> (mesh),
53  MeshOutput<MeshBase> (mesh,
54  /* is_parallel_format = */ false,
55  /* serial_only_needed_on_proc_0 = */ true),
56  ParallelObject(mesh),
57 #ifdef LIBMESH_HAVE_EXODUS_API
58  exio_helper(new ExodusII_IO_Helper(*this, false, true, single_precision)),
59  _timestep(1),
60  _verbose(false),
61  _append(false),
62 #endif
63  _allow_empty_variables(false)
64 {
65 }
66 
67 
68 void ExodusII_IO::set_output_variables(const std::vector<std::string> & output_variables,
69  bool allow_empty)
70 {
71  _output_variables = output_variables;
72  _allow_empty_variables = allow_empty;
73 }
74 
75 
76 
77 #ifdef LIBMESH_ENABLE_DEPRECATED
79  std::string var_name,
80  unsigned int timestep)
81 {
82  libmesh_deprecated();
83  copy_nodal_solution(system, var_name, var_name, timestep);
84 }
85 #endif
86 
87 
88 
90  const EquationSystems & es,
91  const std::set<std::string> * system_names)
92 {
93  std::vector<std::string> solution_names;
94  std::vector<Number> v;
95 
96  es.build_variable_names (solution_names, libmesh_nullptr, system_names);
97  es.build_discontinuous_solution_vector (v, system_names);
98 
99  this->write_nodal_data_discontinuous(name, v, solution_names);
100 }
101 
102 
103 
104 
105 // ------------------------------------------------------------
106 // When the Exodus API is present...
107 #ifdef LIBMESH_HAVE_EXODUS_API
108 
110 {
111  exio_helper->close();
112 }
113 
114 
115 
116 void ExodusII_IO::read (const std::string & fname)
117 {
118  // Get a reference to the mesh we are reading
120 
121  // Clear any existing mesh data
122  mesh.clear();
123 
124  // Keep track of what kinds of elements this file contains
125  elems_of_dimension.clear();
126  elems_of_dimension.resize(4, false);
127 
128 #ifdef DEBUG
129  this->verbose(true);
130 #endif
131 
132  // Instantiate the ElementMaps interface
134 
135  // Open the exodus file in EX_READ mode
136  exio_helper->open(fname.c_str(), /*read_only=*/true);
137 
138  // Get header information from exodus file
139  exio_helper->read_header();
140 
141  // Read the QA records
142  exio_helper->read_qa_records();
143 
144  // Print header information
145  exio_helper->print_header();
146 
147  // Read nodes from the exodus file
148  exio_helper->read_nodes();
149 
150  // Reserve space for the nodes.
151  mesh.reserve_nodes(exio_helper->num_nodes);
152 
153  // Read the node number map from the Exodus file. This is
154  // required if we want to preserve the numbering of nodes as it
155  // exists in the Exodus file. If the Exodus file does not contain
156  // a node_num_map, the identity map is returned by this call.
157  exio_helper->read_node_num_map();
158 
159  // Loop over the nodes, create Nodes with local processor_id 0.
160  for (int i=0; i<exio_helper->num_nodes; i++)
161  {
162  // Use the node_num_map to get the correct ID for Exodus
163  int exodus_id = exio_helper->node_num_map[i];
164 
165  // Catch the node that was added to the mesh
166  Node * added_node = mesh.add_point (Point(exio_helper->x[i], exio_helper->y[i], exio_helper->z[i]), exodus_id-1);
167 
168  // If the Mesh assigned an ID different from what is in the
169  // Exodus file, we should probably error.
170  if (added_node->id() != static_cast<unsigned>(exodus_id-1))
171  libmesh_error_msg("Error! Mesh assigned node ID " \
172  << added_node->id() \
173  << " which is different from the (zero-based) Exodus ID " \
174  << exodus_id-1 \
175  << "!");
176  }
177 
178  // This assert is no longer valid if the nodes are not numbered
179  // sequentially starting from 1 in the Exodus file.
180  // libmesh_assert_equal_to (static_cast<unsigned int>(exio_helper->num_nodes), mesh.n_nodes());
181 
182  // Get information about all the blocks
183  exio_helper->read_block_info();
184 
185  // Reserve space for the elements
186  mesh.reserve_elem(exio_helper->num_elem);
187 
188  // Read the element number map from the Exodus file. This is
189  // required if we want to preserve the numbering of elements as it
190  // exists in the Exodus file. If the Exodus file does not contain
191  // an elem_num_map, the identity map is returned by this call.
192  exio_helper->read_elem_num_map();
193 
194  // Read in the element connectivity for each block.
195  int nelem_last_block = 0;
196 
197  // Loop over all the blocks
198  for (int i=0; i<exio_helper->num_elem_blk; i++)
199  {
200  // Read the information for block i
201  exio_helper->read_elem_in_block (i);
202  int subdomain_id = exio_helper->get_block_id(i);
203 
204  // populate the map of names
205  std::string subdomain_name = exio_helper->get_block_name(i);
206  if (!subdomain_name.empty())
207  mesh.subdomain_name(static_cast<subdomain_id_type>(subdomain_id)) = subdomain_name;
208 
209  // Set any relevant node/edge maps for this element
210  const std::string type_str (exio_helper->get_elem_type());
211  const ExodusII_IO_Helper::Conversion conv = em.assign_conversion(type_str);
212 
213  // Loop over all the faces in this block
214  int jmax = nelem_last_block+exio_helper->num_elem_this_blk;
215  for (int j=nelem_last_block; j<jmax; j++)
216  {
217  Elem * elem = Elem::build (conv.get_canonical_type()).release();
218  libmesh_assert (elem);
219  elem->subdomain_id() = static_cast<subdomain_id_type>(subdomain_id) ;
220 
221  // Use the elem_num_map to obtain the ID of this element in the Exodus file
222  int exodus_id = exio_helper->elem_num_map[j];
223 
224  // Assign this element the same ID it had in the Exodus
225  // file, but make it zero-based by subtracting 1. Note:
226  // some day we could use 1-based numbering in libmesh and
227  // thus match the Exodus numbering exactly, but at the
228  // moment libmesh is zero-based.
229  elem->set_id(exodus_id-1);
230 
231  // Record that we have seen an element of dimension elem->dim()
232  elems_of_dimension[elem->dim()] = true;
233 
234  // Catch the Elem pointer that the Mesh throws back
235  elem = mesh.add_elem (elem);
236 
237  // If the Mesh assigned an ID different from what is in the
238  // Exodus file, we should probably error.
239  if (elem->id() != static_cast<unsigned>(exodus_id-1))
240  libmesh_error_msg("Error! Mesh assigned ID " \
241  << elem->id() \
242  << " which is different from the (zero-based) Exodus ID " \
243  << exodus_id-1 \
244  << "!");
245 
246  // Set all the nodes for this element
247  for (int k=0; k<exio_helper->num_nodes_per_elem; k++)
248  {
249  // global index
250  int gi = (j-nelem_last_block)*exio_helper->num_nodes_per_elem + conv.get_node_map(k);
251 
252  // The entries in 'connect' are actually (1-based)
253  // indices into the node_num_map, so to get the right
254  // node ID we:
255  // 1.) Subtract 1 from connect[gi]
256  // 2.) Pass it through node_num_map to get the corresponding Exodus ID
257  // 3.) Subtract 1 from that, since libmesh node numbering is "zero"-based,
258  // even when the Exodus node numbering doesn't start with 1.
259  int libmesh_node_id = exio_helper->node_num_map[exio_helper->connect[gi] - 1] - 1;
260 
261  // Set the node pointer in the Elem
262  elem->set_node(k) = mesh.node_ptr(libmesh_node_id);
263  }
264  }
265 
266  // running sum of # of elements per block,
267  // (should equal total number of elements in the end)
268  nelem_last_block += exio_helper->num_elem_this_blk;
269  }
270 
271  // This assert isn't valid if the Exodus file's numbering doesn't
272  // start with 1! For example, if Exodus's elem_num_map is 21, 22,
273  // 23, 24, 25, 26, 27, 28, 29, 30, ... 84, then by the time you are
274  // done with the loop above, mesh.n_elem() will report 84 and
275  // nelem_last_block will be 64.
276  // libmesh_assert_equal_to (static_cast<unsigned>(nelem_last_block), mesh.n_elem());
277 
278  // Set the mesh dimension to the largest encountered for an element
279  for (unsigned char i=0; i!=4; ++i)
280  if (elems_of_dimension[i])
281  mesh.set_mesh_dimension(i);
282 
283  // Read in sideset information -- this is useful for applying boundary conditions
284  {
285  // Get basic information about all sidesets
286  exio_helper->read_sideset_info();
287  int offset=0;
288  for (int i=0; i<exio_helper->num_side_sets; i++)
289  {
290  // Compute new offset
291  offset += (i > 0 ? exio_helper->num_sides_per_set[i-1] : 0);
292  exio_helper->read_sideset (i, offset);
293 
294  std::string sideset_name = exio_helper->get_side_set_name(i);
295  if (!sideset_name.empty())
297  (cast_int<boundary_id_type>(exio_helper->get_side_set_id(i)))
298  = sideset_name;
299  }
300 
301  for (std::size_t e=0; e<exio_helper->elem_list.size(); e++)
302  {
303  // The numbers in the Exodus file sidesets should be thought
304  // of as (1-based) indices into the elem_num_map array. So,
305  // to get the right element ID we have to:
306  // 1.) Subtract 1 from elem_list[e] (to get a zero-based index)
307  // 2.) Pass it through elem_num_map (to get the corresponding Exodus ID)
308  // 3.) Subtract 1 from that, since libmesh is "zero"-based,
309  // even when the Exodus numbering doesn't start with 1.
310  dof_id_type libmesh_elem_id =
311  cast_int<dof_id_type>(exio_helper->elem_num_map[exio_helper->elem_list[e] - 1] - 1);
312 
313  // Set any relevant node/edge maps for this element
314  Elem & elem = mesh.elem_ref(libmesh_elem_id);
315 
316  const ExodusII_IO_Helper::Conversion conv = em.assign_conversion(elem.type());
317 
318  // Map the zero-based Exodus side numbering to the libmesh side numbering
319  unsigned int raw_side_index = exio_helper->side_list[e]-1;
320  unsigned int side_index_offset = conv.get_shellface_index_offset();
321 
322  if (raw_side_index < side_index_offset)
323  {
324  // We assume this is a "shell face"
325  int mapped_shellface = raw_side_index;
326 
327  // Check for errors
328  if (mapped_shellface == ExodusII_IO_Helper::Conversion::invalid_id)
329  libmesh_error_msg("Invalid 1-based side id: " \
330  << mapped_shellface \
331  << " detected for " \
332  << Utility::enum_to_string(elem.type()));
333 
334  // Add this (elem,shellface,id) triplet to the BoundaryInfo object.
335  mesh.get_boundary_info().add_shellface (libmesh_elem_id,
336  cast_int<unsigned short>(mapped_shellface),
337  cast_int<boundary_id_type>(exio_helper->id_list[e]));
338  }
339  else
340  {
341  unsigned int side_index = static_cast<unsigned int>(raw_side_index - side_index_offset);
342  int mapped_side = conv.get_side_map(side_index);
343 
344  // Check for errors
346  libmesh_error_msg("Invalid 1-based side id: " \
347  << side_index \
348  << " detected for " \
349  << Utility::enum_to_string(elem.type()));
350 
351  // Add this (elem,side,id) triplet to the BoundaryInfo object.
352  mesh.get_boundary_info().add_side (libmesh_elem_id,
353  cast_int<unsigned short>(mapped_side),
354  cast_int<boundary_id_type>(exio_helper->id_list[e]));
355  }
356  }
357  }
358 
359  // Read nodeset info
360  {
361  exio_helper->read_nodeset_info();
362 
363  for (int nodeset=0; nodeset<exio_helper->num_node_sets; nodeset++)
364  {
365  boundary_id_type nodeset_id =
366  cast_int<boundary_id_type>(exio_helper->nodeset_ids[nodeset]);
367 
368  std::string nodeset_name = exio_helper->get_node_set_name(nodeset);
369  if (!nodeset_name.empty())
370  mesh.get_boundary_info().nodeset_name(nodeset_id) = nodeset_name;
371 
372  exio_helper->read_nodeset(nodeset);
373 
374  for (std::size_t node=0; node<exio_helper->node_list.size(); node++)
375  {
376  // As before, the entries in 'node_list' are 1-based
377  // indices into the node_num_map array, so we have to map
378  // them. See comment above.
379  int libmesh_node_id = exio_helper->node_num_map[exio_helper->node_list[node] - 1] - 1;
380  mesh.get_boundary_info().add_node(cast_int<dof_id_type>(libmesh_node_id),
381  nodeset_id);
382  }
383  }
384  }
385 
386 #if LIBMESH_DIM < 3
387  if (mesh.mesh_dimension() > LIBMESH_DIM)
388  libmesh_error_msg("Cannot open dimension " \
389  << mesh.mesh_dimension() \
390  << " mesh file when configured without " \
391  << mesh.mesh_dimension() \
392  << "D support.");
393 #endif
394 }
395 
396 
397 
398 void ExodusII_IO::verbose (bool set_verbosity)
399 {
400  _verbose = set_verbosity;
401 
402  // Set the verbose flag in the helper object as well.
403  exio_helper->verbose = _verbose;
404 }
405 
406 
407 
409 {
410  exio_helper->use_mesh_dimension_instead_of_spatial_dimension(val);
411 }
412 
413 
414 
416 {
417  exio_helper->write_as_dimension(dim);
418 }
419 
420 
421 
423 {
424  libmesh_warning("This method may be deprecated in the future");
425  exio_helper->set_coordinate_offset(p);
426 }
427 
428 
429 
430 void ExodusII_IO::append(bool val)
431 {
432  _append = val;
433 }
434 
435 
436 
437 const std::vector<Real> & ExodusII_IO::get_time_steps()
438 {
439  if (!exio_helper->opened_for_reading)
440  libmesh_error_msg("ERROR, ExodusII file must be opened for reading before calling ExodusII_IO::get_time_steps()!");
441 
442  exio_helper->read_time_steps();
443  return exio_helper->time_steps;
444 }
445 
446 
447 
449 {
450  if (!exio_helper->opened_for_reading && !exio_helper->opened_for_writing)
451  libmesh_error_msg("ERROR, ExodusII file must be opened for reading or writing before calling ExodusII_IO::get_num_time_steps()!");
452 
453  exio_helper->read_num_time_steps();
454  return exio_helper->num_time_steps;
455 }
456 
457 
458 
460  std::string system_var_name,
461  std::string exodus_var_name,
462  unsigned int timestep)
463 {
464  if (!exio_helper->opened_for_reading)
465  libmesh_error_msg("ERROR, ExodusII file must be opened for reading before copying a nodal solution!");
466 
467  exio_helper->read_nodal_var_values(exodus_var_name, timestep);
468 
469  const unsigned int var_num = system.variable_number(system_var_name);
470 
471  for (std::size_t i=0; i<exio_helper->nodal_var_values.size(); ++i)
472  {
473  const Node * node = MeshInput<MeshBase>::mesh().query_node_ptr(i);
474 
475  if (node && node->n_comp(system.number(), var_num) > 0)
476  {
477  dof_id_type dof_index = node->dof_number(system.number(), var_num, 0);
478 
479  // If the dof_index is local to this processor, set the value
480  if ((dof_index >= system.solution->first_local_index()) && (dof_index < system.solution->last_local_index()))
481  system.solution->set (dof_index, exio_helper->nodal_var_values[i]);
482  }
483  }
484 
485  system.solution->close();
486  system.update();
487 }
488 
489 
490 
492  std::string system_var_name,
493  std::string exodus_var_name,
494  unsigned int timestep)
495 {
496  if (system.comm().rank() == 0)
497  {
498  if (!exio_helper->opened_for_reading)
499  libmesh_error_msg("ERROR, ExodusII file must be opened for reading before copying an elemental solution!");
500 
501  // Map from element ID to elemental variable value. We need to use
502  // a map here rather than a vector (e.g. elem_var_values) since the
503  // libmesh element numbering can contain "holes". This is the case
504  // if we are reading elemental var values from an adaptively refined
505  // mesh that has not been sequentially renumbered.
506  std::map<dof_id_type, Real> elem_var_value_map;
507  exio_helper->read_elemental_var_values(exodus_var_name, timestep, elem_var_value_map);
508 
509  const unsigned int var_num = system.variable_number(system_var_name);
510  if (system.variable_type(var_num) != FEType(CONSTANT, MONOMIAL))
511  libmesh_error_msg("Error! Trying to copy elemental solution into a variable that is not of CONSTANT MONOMIAL type.");
512 
513  std::map<dof_id_type, Real>::iterator
514  it = elem_var_value_map.begin(),
515  end = elem_var_value_map.end();
516 
517  for (; it!=end; ++it)
518  {
519  const Elem * elem = MeshInput<MeshBase>::mesh().query_elem_ptr(it->first);
520 
521  if (elem && elem->n_comp(system.number(), var_num) > 0)
522  {
523  dof_id_type dof_index = elem->dof_number(system.number(), var_num, 0);
524  system.solution->set (dof_index, it->second);
525  }
526  }
527  }
528 
529  system.solution->close();
530  system.update();
531 }
532 
533 
534 
536 {
537  // Be sure the file has been opened for writing!
538  if (MeshOutput<MeshBase>::mesh().processor_id() == 0 && !exio_helper->opened_for_writing)
539  libmesh_error_msg("ERROR, ExodusII file must be initialized before outputting element variables.");
540 
541  // This function currently only works on serialized meshes. We rely
542  // on having a reference to a non-const MeshBase object from our
543  // MeshInput parent class to construct a MeshSerializer object,
544  // similar to what is done in ExodusII_IO::write(). Note that
545  // calling ExodusII_IO::write_timestep() followed by
546  // ExodusII_IO::write_element_data() when the underlying Mesh is a
547  // DistributedMesh will result in an unnecessary additional
548  // serialization/re-parallelization step.
549  // The "true" specifies that we only need the mesh serialized to processor 0
551 
552  // To be (possibly) filled with a filtered list of variable names to output.
553  std::vector<std::string> names;
554 
555  // If _output_variables is populated, only output the monomials which are
556  // also in the _output_variables vector.
557  if (_output_variables.size() > 0)
558  {
559  std::vector<std::string> monomials;
560  const FEType type(CONSTANT, MONOMIAL);
561 
562  // Create a list of monomial variable names
563  es.build_variable_names(monomials, &type);
564 
565  // Filter that list against the _output_variables list. Note: if names is still empty after
566  // all this filtering, all the monomial variables will be gathered
567  std::vector<std::string>::iterator it = monomials.begin();
568  for (; it!=monomials.end(); ++it)
569  if (std::find(_output_variables.begin(), _output_variables.end(), *it) != _output_variables.end())
570  names.push_back(*it);
571  }
572 
573  // If we pass in a list of names to "get_solution" it'll filter the variables coming back
574  std::vector<Number> soln;
575  es.get_solution(soln, names);
576 
577  if (soln.empty()) // If there is nothing to write just return
578  return;
579 
580  // The data must ultimately be written block by block. This means that this data
581  // must be sorted appropriately.
582  if (MeshOutput<MeshBase>::mesh().processor_id())
583  return;
584 
586 
587 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
588 
589  std::vector<std::string> complex_names = exio_helper->get_complex_names(names);
590 
591  exio_helper->initialize_element_variables(complex_names);
592 
593  unsigned int num_values = soln.size();
594  unsigned int num_vars = names.size();
595  unsigned int num_elems = num_values / num_vars;
596 
597  // This will contain the real and imaginary parts and the magnitude
598  // of the values in soln
599  std::vector<Real> complex_soln(3*num_values);
600 
601  for (unsigned i=0; i<num_vars; ++i)
602  {
603 
604  for (unsigned int j=0; j<num_elems; ++j)
605  {
606  Number value = soln[i*num_vars + j];
607  complex_soln[3*i*num_elems + j] = value.real();
608  }
609  for (unsigned int j=0; j<num_elems; ++j)
610  {
611  Number value = soln[i*num_vars + j];
612  complex_soln[3*i*num_elems + num_elems +j] = value.imag();
613  }
614  for (unsigned int j=0; j<num_elems; ++j)
615  {
616  Number value = soln[i*num_vars + j];
617  complex_soln[3*i*num_elems + 2*num_elems + j] = std::abs(value);
618  }
619  }
620 
621  exio_helper->write_element_values(mesh, complex_soln, _timestep);
622 
623 #else
624  exio_helper->initialize_element_variables(names);
625  exio_helper->write_element_values(mesh, soln, _timestep);
626 #endif
627 }
628 
629 
630 
631 void ExodusII_IO::write_nodal_data (const std::string & fname,
632  const std::vector<Number> & soln,
633  const std::vector<std::string> & names)
634 {
635  LOG_SCOPE("write_nodal_data()", "ExodusII_IO");
636 
638 
639  int num_vars = cast_int<int>(names.size());
640  dof_id_type num_nodes = mesh.n_nodes();
641 
642  // The names of the variables to be output
643  std::vector<std::string> output_names;
644 
646  output_names = _output_variables;
647  else
648  output_names = names;
649 
650 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
651 
652  std::vector<std::string> complex_names = exio_helper->get_complex_names(names);
653 
654  // Call helper function for opening/initializing data, giving it the
655  // complex variable names
656  this->write_nodal_data_common(fname, complex_names, /*continuous=*/true);
657 #else
658  // Call helper function for opening/initializing data
659  this->write_nodal_data_common(fname, output_names, /*continuous=*/true);
660 #endif
661 
662  if (mesh.processor_id())
663  return;
664 
665  // This will count the number of variables actually output
666  for (int c=0; c<num_vars; c++)
667  {
668  std::stringstream name_to_find;
669 
670  std::vector<std::string>::iterator pos =
671  std::find(output_names.begin(), output_names.end(), names[c]);
672  if (pos == output_names.end())
673  continue;
674 
675  unsigned int variable_name_position =
676  cast_int<unsigned int>(pos - output_names.begin());
677 
678 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
679  std::vector<Real> real_parts(num_nodes);
680  std::vector<Real> imag_parts(num_nodes);
681  std::vector<Real> magnitudes(num_nodes);
682 
683  for (unsigned int i=0; i<num_nodes; ++i)
684  {
685  real_parts[i] = soln[i*num_vars + c].real();
686  imag_parts[i] = soln[i*num_vars + c].imag();
687  magnitudes[i] = std::abs(soln[i*num_vars + c]);
688  }
689  exio_helper->write_nodal_values(3*variable_name_position+1,real_parts,_timestep);
690  exio_helper->write_nodal_values(3*variable_name_position+2,imag_parts,_timestep);
691  exio_helper->write_nodal_values(3*variable_name_position+3,magnitudes,_timestep);
692 #else
693  std::vector<Number> cur_soln(num_nodes);
694 
695  // Copy out this variable's solution
696  for (dof_id_type i=0; i<num_nodes; i++)
697  cur_soln[i] = soln[i*num_vars + c];
698  exio_helper->write_nodal_values(variable_name_position+1,cur_soln,_timestep);
699 #endif
700 
701  }
702 }
703 
704 
705 
706 
707 void ExodusII_IO::write_information_records (const std::vector<std::string> & records)
708 {
710  return;
711 
712  if (!exio_helper->opened_for_writing)
713  libmesh_error_msg("ERROR, ExodusII file must be initialized before outputting information records.");
714 
715  exio_helper->write_information_records(records);
716 }
717 
718 
719 
720 void ExodusII_IO::write_global_data (const std::vector<Number> & soln,
721  const std::vector<std::string> & names)
722 {
724  return;
725 
726  if (!exio_helper->opened_for_writing)
727  libmesh_error_msg("ERROR, ExodusII file must be initialized before outputting global variables.");
728 
729 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
730 
731  std::vector<std::string> complex_names = exio_helper->get_complex_names(names);
732 
733  exio_helper->initialize_global_variables(complex_names);
734 
735  unsigned int num_values = soln.size();
736  unsigned int num_vars = names.size();
737  unsigned int num_elems = num_values / num_vars;
738 
739  // This will contain the real and imaginary parts and the magnitude
740  // of the values in soln
741  std::vector<Real> complex_soln(3*num_values);
742 
743  for (unsigned i=0; i<num_vars; ++i)
744  {
745 
746  for (unsigned int j=0; j<num_elems; ++j)
747  {
748  Number value = soln[i*num_vars + j];
749  complex_soln[3*i*num_elems + j] = value.real();
750  }
751  for (unsigned int j=0; j<num_elems; ++j)
752  {
753  Number value = soln[i*num_vars + j];
754  complex_soln[3*i*num_elems + num_elems +j] = value.imag();
755  }
756  for (unsigned int j=0; j<num_elems; ++j)
757  {
758  Number value = soln[i*num_vars + j];
759  complex_soln[3*i*num_elems + 2*num_elems + j] = std::abs(value);
760  }
761  }
762 
763  exio_helper->write_global_values(complex_soln, _timestep);
764 
765 #else
766  exio_helper->initialize_global_variables(names);
767  exio_helper->write_global_values(soln, _timestep);
768 #endif
769 }
770 
771 
772 
773 void ExodusII_IO::write_timestep (const std::string & fname,
774  const EquationSystems & es,
775  const int timestep,
776  const Real time)
777 {
778  _timestep = timestep;
779  write_equation_systems(fname,es);
780 
782  return;
783 
784  exio_helper->write_timestep(timestep, time);
785 }
786 
787 
788 
789 void ExodusII_IO::write (const std::string & fname)
790 {
792 
793  // We may need to gather a DistributedMesh to output it, making that
794  // const qualifier in our constructor a dirty lie
795  // The "true" specifies that we only need the mesh serialized to processor 0
797 
798  libmesh_assert( !exio_helper->opened_for_writing );
799 
800  // If the user has set the append flag here, it doesn't really make
801  // sense: the intent of this function is to write a Mesh with no
802  // data, while "appending" is really intended to add data to an
803  // existing file. If we're verbose, print a message to this effect.
804  if (_append && _verbose)
805  libMesh::out << "Warning: Appending in ExodusII_IO::write() does not make sense.\n"
806  << "Creating a new file instead!"
807  << std::endl;
808 
809  exio_helper->create(fname);
810  exio_helper->initialize(fname,mesh);
811  exio_helper->write_nodal_coordinates(mesh);
812  exio_helper->write_elements(mesh);
813  exio_helper->write_sidesets(mesh);
814  exio_helper->write_nodesets(mesh);
815 
816  if ((mesh.get_boundary_info().n_edge_conds() > 0) && _verbose)
817  {
818  libMesh::out << "Warning: Mesh contains edge boundary IDs, but these "
819  << "are not supported by the ExodusII format."
820  << std::endl;
821  }
822 }
823 
824 
825 
826 void ExodusII_IO::write_nodal_data_discontinuous (const std::string & fname,
827  const std::vector<Number> & soln,
828  const std::vector<std::string> & names)
829 {
830  LOG_SCOPE("write_nodal_data_discontinuous()", "ExodusII_IO");
831 
833 
834  int num_vars = cast_int<int>(names.size());
835  int num_nodes = 0;
836  for (const auto & elem : mesh.active_element_ptr_range())
837  num_nodes += elem->n_nodes();
838 
839 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
840 
841  std::vector<std::string> complex_names = exio_helper->get_complex_names(names);
842 
843  // Call helper function for opening/initializing data, giving it the
844  // complex variable names
845  this->write_nodal_data_common(fname, complex_names, /*continuous=*/false);
846 #else
847  // Call helper function for opening/initializing data
848  this->write_nodal_data_common(fname, names, /*continuous=*/false);
849 #endif
850 
851  if (mesh.processor_id())
852  return;
853 
854  for (int c=0; c<num_vars; c++)
855  {
856 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
857  std::vector<Real> real_parts(num_nodes);
858  std::vector<Real> imag_parts(num_nodes);
859  std::vector<Real> magnitudes(num_nodes);
860 
861  for (int i=0; i<num_nodes; ++i)
862  {
863  real_parts[i] = soln[i*num_vars + c].real();
864  imag_parts[i] = soln[i*num_vars + c].imag();
865  magnitudes[i] = std::abs(soln[i*num_vars + c]);
866  }
867  exio_helper->write_nodal_values(3*c+1,real_parts,_timestep);
868  exio_helper->write_nodal_values(3*c+2,imag_parts,_timestep);
869  exio_helper->write_nodal_values(3*c+3,magnitudes,_timestep);
870 #else
871  // Copy out this variable's solution
872  std::vector<Number> cur_soln(num_nodes);
873 
874  for (int i=0; i<num_nodes; i++)
875  cur_soln[i] = soln[i*num_vars + c];
876 
877  exio_helper->write_nodal_values(c+1,cur_soln,_timestep);
878 #endif
879  }
880 }
881 
882 
883 
885  const std::vector<std::string> & names,
886  bool continuous)
887 {
889 
890  // This function can be called multiple times, we only want to open
891  // the ExodusII file the first time it's called.
892  if (!exio_helper->opened_for_writing)
893  {
894  // If we're appending, open() the file with read_only=false,
895  // otherwise create() it and write the contents of the mesh to
896  // it.
897  if (_append)
898  {
899  exio_helper->open(fname.c_str(), /*read_only=*/false);
900  // If we're appending, it's not valid to call exio_helper->initialize()
901  // or exio_helper->initialize_nodal_variables(), but we do need to set up
902  // certain aspects of the Helper object itself, such as the number of nodes
903  // and elements. We do that by reading the header...
904  exio_helper->read_header();
905 
906  // ...and reading the block info
907  exio_helper->read_block_info();
908  }
909  else
910  {
911  exio_helper->create(fname);
912 
913  exio_helper->initialize(fname, mesh, !continuous);
914  exio_helper->write_nodal_coordinates(mesh, !continuous);
915  exio_helper->write_elements(mesh, !continuous);
916 
917  exio_helper->write_sidesets(mesh);
918  exio_helper->write_nodesets(mesh);
919 
920  if ((mesh.get_boundary_info().n_edge_conds() > 0) && _verbose)
921  {
922  libMesh::out << "Warning: Mesh contains edge boundary IDs, but these "
923  << "are not supported by the ExodusII format."
924  << std::endl;
925  }
926 
927  exio_helper->initialize_nodal_variables(names);
928  }
929  }
930  else
931  {
932  // We are already open for writing, so check that the filename
933  // passed to this function matches the filename currently in use
934  // by the helper.
935  if (fname != exio_helper->current_filename)
936  libmesh_error_msg("Error! This ExodusII_IO object is already associated with file: " \
937  << exio_helper->current_filename \
938  << ", cannot use it with requested file: " \
939  << fname);
940  }
941 }
942 
943 const std::vector<std::string> & ExodusII_IO::get_nodal_var_names()
944 {
945  exio_helper->read_var_names(ExodusII_IO_Helper::NODAL);
946  return exio_helper->nodal_var_names;
947 }
948 
949 const std::vector<std::string> & ExodusII_IO::get_elem_var_names()
950 {
952  return exio_helper->elem_var_names;
953 }
954 
955 
956 // LIBMESH_HAVE_EXODUS_API is not defined, declare error() versions of functions...
957 #else
958 
959 
960 
962 {
963 }
964 
965 
966 
967 void ExodusII_IO::read (const std::string &)
968 {
969  libmesh_error_msg("ERROR, ExodusII API is not defined.");
970 }
971 
972 
973 
974 void ExodusII_IO::verbose (bool)
975 {
976  libmesh_error_msg("ERROR, ExodusII API is not defined.");
977 }
978 
979 
980 
982 {
983  libmesh_error_msg("ERROR, ExodusII API is not defined.");
984 }
985 
986 
987 
988 void ExodusII_IO::write_as_dimension(unsigned)
989 {
990  libmesh_error_msg("ERROR, ExodusII API is not defined.");
991 }
992 
993 
994 
996 {
997  libmesh_error_msg("ERROR, ExodusII API is not defined.");
998 }
999 
1000 
1001 
1002 void ExodusII_IO::append(bool)
1003 {
1004  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1005 }
1006 
1007 
1008 
1009 const std::vector<Real> & ExodusII_IO::get_time_steps()
1010 {
1011  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1012 }
1013 
1014 
1015 
1017 {
1018  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1019 }
1020 
1021 
1023  std::string,
1024  std::string,
1025  unsigned int)
1026 {
1027  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1028 }
1029 
1030 
1031 
1033  std::string,
1034  std::string,
1035  unsigned int)
1036 {
1037  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1038 }
1039 
1040 
1041 
1043 {
1044  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1045 }
1046 
1047 
1048 
1049 void ExodusII_IO::write_nodal_data (const std::string &,
1050  const std::vector<Number> &,
1051  const std::vector<std::string> &)
1052 {
1053  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1054 }
1055 
1056 
1057 
1058 void ExodusII_IO::write_information_records (const std::vector<std::string> &)
1059 {
1060  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1061 }
1062 
1063 
1064 
1065 void ExodusII_IO::write_global_data (const std::vector<Number> &,
1066  const std::vector<std::string> &)
1067 {
1068  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1069 }
1070 
1071 
1072 
1073 void ExodusII_IO::write_timestep (const std::string &,
1074  const EquationSystems &,
1075  const int,
1076  const Real)
1077 {
1078  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1079 }
1080 
1081 
1082 
1083 void ExodusII_IO::write (const std::string &)
1084 {
1085  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1086 }
1087 
1088 
1089 
1090 void ExodusII_IO::write_nodal_data_discontinuous (const std::string &,
1091  const std::vector<Number> &,
1092  const std::vector<std::string> &)
1093 {
1094  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1095 }
1096 
1097 
1098 
1099 void ExodusII_IO::write_nodal_data_common(std::string,
1100  const std::vector<std::string> &,
1101  bool)
1102 {
1103  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1104 }
1105 
1106 
1107 const std::vector<std::string> & ExodusII_IO::get_elem_var_names()
1108 {
1109  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1110 }
1111 
1112 const std::vector<std::string> & ExodusII_IO::get_nodal_var_names()
1113 {
1114  libmesh_error_msg("ERROR, ExodusII API is not defined.");
1115 }
1116 
1117 #endif // LIBMESH_HAVE_EXODUS_API
1118 } // namespace libMesh
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:178
void use_mesh_dimension_instead_of_spatial_dimension(bool val)
In the general case, meshes containing 2D elements can be manifolds living in 3D space, thus by default we write all meshes with the Exodus dimension set to LIBMESH_DIM = mesh.spatial_dimension().
Definition: exodusII_io.C:408
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition: mesh_base.h:117
double abs(double a)
This is the EquationSystems class.
virtual void reserve_nodes(const dof_id_type nn)=0
Reserves space for a known number of nodes.
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:1941
A Node is like a Point, but with more information.
Definition: node.h:52
std::string & nodeset_name(boundary_id_type id)
static UniquePtr< Elem > build(const ElemType type, Elem *p=libmesh_nullptr)
Definition: elem.C:238
void write_as_dimension(unsigned dim)
Directly control the num_dim which is written to the Exodus file.
Definition: exodusII_io.C:415
void write_timestep(const std::string &fname, const EquationSystems &es, const int timestep, const Real time)
Writes out the solution at a specific timestep.
Definition: exodusII_io.C:773
unsigned int dim
static const int invalid_id
An invalid_id that can be returned to signal failure in case something goes wrong.
std::vector< bool > elems_of_dimension
A vector of bools describing what dimension elements have been encountered when reading a mesh...
Definition: mesh_input.h:97
std::size_t n_edge_conds() const
This is the base class from which all geometric element types are derived.
Definition: elem.h:89
MeshBase & mesh
ExodusII_IO_Helper::Conversion assign_conversion(std::string type_str)
const class libmesh_nullptr_t libmesh_nullptr
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
Definition: dof_object.h:810
virtual const Node * node_ptr(const dof_id_type i) const =0
void copy_nodal_solution(System &system, std::string var_name, unsigned int timestep=1)
Backward compatibility version of function that takes a single variable name.
Definition: exodusII_io.C:78
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end...
virtual SimpleRange< element_iterator > active_element_ptr_range()=0
const MT & mesh() const
Definition: mesh_output.h:216
This class defines an abstract interface for Mesh output.
Definition: mesh_output.h:53
The libMesh namespace provides an interface to certain functionality in the library.
ExodusII_IO(MeshBase &mesh, bool single_precision=false)
Constructor.
Definition: exodusII_io.C:45
const FEType & variable_type(const unsigned int i) const
Definition: system.h:2164
void append(bool val)
If true, this flag will cause the ExodusII_IO object to attempt to open an existing file for writing...
Definition: exodusII_io.C:430
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
void build_discontinuous_solution_vector(std::vector< Number > &soln, const std::set< std::string > *system_names=libmesh_nullptr) const
Fill the input vector soln with solution values.
This is the MeshBase class.
Definition: mesh_base.h:68
dof_id_type & set_id()
Definition: dof_object.h:641
libmesh_assert(j)
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) libmesh_override
Write out a nodal solution.
Definition: exodusII_io.C:631
unsigned short int variable_number(const std::string &var) const
Definition: system.C:1263
int _timestep
Stores the current value of the timestep when calling ExodusII_IO::write_timestep().
Definition: exodusII_io.h:271
void add_node(const Node *node, const boundary_id_type id)
Add Node node with boundary id id to the boundary information data structures.
int8_t boundary_id_type
Definition: id_types.h:51
This class defines an abstract interface for Mesh input.
Definition: mesh_base.h:50
void set_output_variables(const std::vector< std::string > &output_variables, bool allow_empty=true)
Sets the list of variable names to be included in the output.
Definition: exodusII_io.C:68
bool _verbose
should we be verbose?
Definition: exodusII_io.h:276
bool _append
Default false.
Definition: exodusII_io.h:282
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
void copy_elemental_solution(System &system, std::string system_var_name, std::string exodus_var_name, unsigned int timestep=1)
If we read in a elemental solution while reading in a mesh, we can attempt to copy that elemental sol...
Definition: exodusII_io.C:491
const std::vector< Real > & get_time_steps()
Definition: exodusII_io.C:437
std::vector< std::string > _output_variables
The names of the variables to be output.
Definition: exodusII_io.h:289
This is the ExodusII_IO_Helper class.
void write_information_records(const std::vector< std::string > &)
Write out information records.
Definition: exodusII_io.C:707
std::string & subdomain_name(subdomain_id_type id)
Definition: mesh_base.C:576
UniquePtr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1523
subdomain_id_type subdomain_id() const
Definition: elem.h:1951
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition: mesh_base.h:199
void verbose(bool set_verbosity)
Set the flag indicating if we should be verbose.
Definition: exodusII_io.C:398
void write_nodal_data_common(std::string fname, const std::vector< std::string > &names, bool continuous=true)
This function factors out a bunch of code which is common to the write_nodal_data() and write_nodal_d...
Definition: exodusII_io.C:884
This class forms the base class for all other classes that are expected to be implemented in parallel...
virtual void clear()
Deletes all the data that are currently stored.
Definition: mesh_base.C:285
std::string & sideset_name(boundary_id_type id)
void write_global_data(const std::vector< Number > &, const std::vector< std::string > &)
Write out global variables.
Definition: exodusII_io.C:720
std::string enum_to_string(const T e)
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=libmesh_nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
virtual const Elem & elem_ref(const dof_id_type i) const
Definition: mesh_base.h:490
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition: system.C:425
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void read(const std::string &name) libmesh_override
This method implements reading a mesh from a specified file.
Definition: exodusII_io.C:116
unsigned int number() const
Definition: system.h:2006
virtual void write(const std::string &fname) libmesh_override
This method implements writing a mesh to a specified file.
Definition: exodusII_io.C:789
Temporarily serialize a DistributedMesh for output; a distributed mesh is allgathered by the MeshSeri...
const Parallel::Communicator & comm() const
OStreamProxy out
virtual ~ExodusII_IO()
Destructor.
Definition: exodusII_io.C:109
const std::vector< std::string > & get_elem_var_names()
Return list of the elemental variable names.
Definition: exodusII_io.C:949
void build_variable_names(std::vector< std::string > &var_names, const FEType *type=libmesh_nullptr, const std::set< std::string > *system_names=libmesh_nullptr) const
Fill the input vector var_names with the names of the variables for each system.
unsigned int mesh_dimension() const
Definition: mesh_base.C:148
void add_side(const dof_id_type elem, const unsigned short int side, const boundary_id_type id)
Add side side of element number elem with boundary id id to the boundary information data structure...
void add_shellface(const dof_id_type elem, const unsigned short int shellface, const boundary_id_type id)
Add shell face shellface of element number elem with boundary id id to the boundary information data ...
static const bool value
Definition: xdr_io.C:108
virtual unsigned int dim() const =0
void write_element_data(const EquationSystems &es)
Write out element solution.
Definition: exodusII_io.C:535
bool _allow_empty_variables
If true, _output_variables is allowed to remain empty.
Definition: exodusII_io.h:304
unsigned int rank() const
Definition: parallel.h:724
UniquePtr< ExodusII_IO_Helper > exio_helper
Only attempt to instantiate an ExodusII helper class if the Exodus API is defined.
Definition: exodusII_io.h:265
void write_nodal_data_discontinuous(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
Write out a discontinuous nodal solution.
Definition: exodusII_io.C:826
unsigned int n_comp(const unsigned int s, const unsigned int var) const
Definition: dof_object.h:780
dof_id_type id() const
Definition: dof_object.h:632
virtual dof_id_type n_nodes() const =0
const std::vector< std::string > & get_nodal_var_names()
Return list of the nodal variable names.
Definition: exodusII_io.C:943
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
virtual void reserve_elem(const dof_id_type ne)=0
Reserves space for a known number of elements.
void write_discontinuous_exodusII(const std::string &name, const EquationSystems &es, const std::set< std::string > *system_names=libmesh_nullptr)
Writes a exodusII file with discontinuous data.
Definition: exodusII_io.C:89
void get_solution(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for CONSTANT MONOMIALs.
void set_coordinate_offset(Point p)
Allows you to set a vector that is added to the coordinates of all of the nodes.
Definition: exodusII_io.C:422
processor_id_type processor_id() const
uint8_t dof_id_type
Definition: id_types.h:64