libMesh
mesh_output.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 // Local includes
20 #include "libmesh/distributed_mesh.h"
21 #include "libmesh/equation_systems.h"
22 #include "libmesh/mesh_output.h"
23 #include "libmesh/parallel.h"
24 #include "libmesh/unstructured_mesh.h"
25 #include "libmesh/numeric_vector.h"
26 
27 namespace libMesh
28 {
29 
30 template <class MT>
31 void MeshOutput<MT>::write_equation_systems (const std::string & fname,
32  const EquationSystems & es,
33  const std::set<std::string> * system_names)
34 {
35  LOG_SCOPE("write_equation_systems()", "MeshOutput");
36 
37  // We may need to gather and/or renumber a DistributedMesh to output
38  // it, making that const qualifier in our constructor a dirty lie
39  MT & my_mesh = const_cast<MT &>(*_obj);
40 
41  // If we're asked to write data that's associated with a different
42  // mesh, output files full of garbage are the result.
43  libmesh_assert_equal_to(&es.get_mesh(), _obj);
44 
45  // A non-renumbered mesh may not have a contiguous numbering, and
46  // that needs to be fixed before we can build a solution vector.
47  if (my_mesh.max_elem_id() != my_mesh.n_elem() ||
48  my_mesh.max_node_id() != my_mesh.n_nodes())
49  {
50  // If we were allowed to renumber then we should have already
51  // been properly renumbered...
52  libmesh_assert(!my_mesh.allow_renumbering());
53 
54  libmesh_do_once(libMesh::out <<
55  "Warning: This MeshOutput subclass only supports meshes which are contiguously renumbered!"
56  << std::endl;);
57 
58  my_mesh.allow_renumbering(true);
59 
60  my_mesh.renumber_nodes_and_elements();
61 
62  // Not sure what good going back to false will do here, the
63  // renumbering horses have already left the barn...
64  my_mesh.allow_renumbering(false);
65  }
66 
67  MeshSerializer serialize(const_cast<MT &>(*_obj), !_is_parallel_format, _serial_only_needed_on_proc_0);
68 
69  // Build the list of variable names that will be written.
70  std::vector<std::string> names;
71  es.build_variable_names (names, libmesh_nullptr, system_names);
72 
73  if (!_is_parallel_format)
74  {
75  // Build the nodal solution values & get the variable
76  // names from the EquationSystems object
77  std::vector<Number> soln;
78  es.build_solution_vector (soln, system_names);
79 
80  this->write_nodal_data (fname, soln, names);
81  }
82  else // _is_parallel_format
83  {
84  UniquePtr<NumericVector<Number>> parallel_soln =
85  es.build_parallel_solution_vector(system_names);
86 
87  this->write_nodal_data (fname, *parallel_soln, names);
88  }
89 }
90 
91 
92 
93 template <class MT>
94 void MeshOutput<MT>::write_nodal_data (const std::string & fname,
95  const NumericVector<Number> & parallel_soln,
96  const std::vector<std::string> & names)
97 {
98  // This is the fallback implementation for parallel I/O formats that
99  // do not yet implement proper writing in parallel, and instead rely
100  // on the full solution vector being available on all processors.
101  std::vector<Number> soln;
102  parallel_soln.localize(soln);
103  this->write_nodal_data(fname, soln, names);
104 }
105 
106 
107 
108 // Instantiate for our Mesh types. If this becomes too cumbersome later,
109 // move any functions in this file to the header file instead.
110 template class MeshOutput<MeshBase>;
111 template class MeshOutput<UnstructuredMesh>;
112 template class MeshOutput<DistributedMesh>;
113 
114 } // namespace libMesh
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &)
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: mesh_output.h:96
This is the EquationSystems class.
const class libmesh_nullptr_t libmesh_nullptr
UniquePtr< NumericVector< Number > > build_parallel_solution_vector(const std::set< std::string > *system_names=libmesh_nullptr) const
A version of build_solution_vector which is appropriate for "parallel" output formats like Nemesis...
The libMesh namespace provides an interface to certain functionality in the library.
void build_solution_vector(std::vector< Number > &soln, const std::string &system_name, const std::string &variable_name="all_vars") const
Fill the input vector soln with the solution values for the system named name.
libmesh_assert(j)
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
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 ...
Definition: mesh_output.C:31
Temporarily serialize a DistributedMesh for output; a distributed mesh is allgathered by the MeshSeri...
OStreamProxy out
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.
const MeshBase & get_mesh() const
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.