www.mooseframework.org
Nemesis.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "Nemesis.h"
11 
12 // MOOSE includes
13 #include "FEProblem.h"
14 #include "MooseApp.h"
15 #include "MooseMesh.h"
16 #include "MooseVariableScalar.h"
17 #include "SystemBase.h"
18 
19 #include "libmesh/dof_map.h"
20 #include "libmesh/nemesis_io.h"
21 
22 registerMooseObject("MooseApp", Nemesis);
23 
24 template <>
27 {
28  // Get the base class parameters
30  params += AdvancedOutput::enableOutputTypes("scalar postprocessor input");
31 
32  // Add description for the Nemesis class
33  params.addClassDescription("Object for output data in the Nemesis format");
34 
35  // Return the InputParameters
36  return params;
37 }
38 
39 Nemesis::Nemesis(const InputParameters & parameters)
40  : AdvancedOutput(parameters),
41  _nemesis_io_ptr(nullptr),
42  _file_num(0),
43  _nemesis_num(0),
44  _nemesis_initialized(false)
45 {
46 }
47 
49 
50 void
52 {
53 
55 
56  // Make certain that a Nemesis_IO object exists
57  meshChanged();
58 }
59 
60 void
62 {
63  // Do not delete the Nemesis_IO object if it has not been used; also there is no need to setup
64  // the object in this case, so just return
65  if (_nemesis_io_ptr != nullptr && !_nemesis_initialized)
66  return;
67 
68  // Increment the file number
69  _file_num++;
70 
71  // Reset the number of outputs for this file
72  _nemesis_num = 1;
73 
74  // Create the new NemesisIO object
75  _nemesis_io_ptr = libmesh_make_unique<Nemesis_IO>(_problem_ptr->mesh().getMesh());
76  _nemesis_initialized = false;
77 }
78 
79 void
81 {
82  // List of desired postprocessor outputs
83  const std::set<std::string> & pps = getPostprocessorOutput();
84 
85  // Append the postprocessor data to the global name value parameters; scalar outputs
86  // also append these member variables
87  for (const auto & name : pps)
88  {
89  _global_names.push_back(name);
91  }
92 }
93 
94 void
96 {
97  // List of desired scalar outputs
98  const std::set<std::string> & out = getScalarOutput();
99 
100  // Append the scalar to the global output lists
101  for (const auto & out_name : out)
102  {
103  // Make sure scalar values are in sync with the solution vector
104  // and are visible on this processor. See TableOutput.C for
105  // TableOutput::outputScalarVariables() explanatory comments
106 
107  MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
108  scalar_var.reinit();
109  VariableValue value(scalar_var.sln());
110 
111  const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
112  const unsigned int n = dof_indices.size();
113  value.resize(n);
114 
115  const DofMap & dof_map = scalar_var.sys().dofMap();
116  for (unsigned int i = 0; i != n; ++i)
117  {
118  const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
119  this->comm().broadcast(value[i], pid);
120  }
121 
122  // If the scalar has a single component, output the name directly
123  if (n == 1)
124  {
125  _global_names.push_back(out_name);
126  _global_values.push_back(value[0]);
127  }
128 
129  // If the scalar as many components add indices to the end of the name
130  else
131  {
132  for (unsigned int i = 0; i < n; ++i)
133  {
134  std::ostringstream os;
135  os << out_name << "_" << i;
136  _global_names.push_back(os.str());
137  _global_values.push_back(value[i]);
138  }
139  }
140  }
141 }
142 
143 void
145 {
146  if (!shouldOutput(type))
147  return;
148 
149  // Clear the global variables (postprocessors and scalars)
150  _global_names.clear();
151  _global_values.clear();
152 
153  // Call the output methods
155 
156  // Set up the whitelist of nodal variable names to write.
157  _nemesis_io_ptr->set_output_variables(
158  std::vector<std::string>(getNodalVariableOutput().begin(), getNodalVariableOutput().end()));
159 
160  // Write nodal data
161  _nemesis_io_ptr->write_timestep(
163  _nemesis_initialized = true;
164 
165  // Write elemental data
166  std::vector<std::string> elemental(getElementalVariableOutput().begin(),
168  _nemesis_io_ptr->set_output_variables(elemental);
169  _nemesis_io_ptr->write_element_data(*_es_ptr);
170 
171  // Increment output call counter for the current file
172  _nemesis_num++;
173 
174  // Write the global variables (populated by the output methods)
175  if (!_global_values.empty())
176  _nemesis_io_ptr->write_global_data(_global_values, _global_names);
177 }
178 
179 std::string
181 {
182  // Append the .e extension on the base file name
183  std::ostringstream output;
184  output << _file_base << ".e";
185 
186  // Add the _000x extension to the file
187  if (_file_num > 1)
188  output << "-s" << std::setw(_padding) << std::setprecision(0) << std::setfill('0') << std::right
189  << _file_num;
190 
191  // Return the filename
192  return output.str();
193 }
const std::set< std::string > & getPostprocessorOutput()
The list of postprocessor names that are set for output.
virtual void initialSetup() override
Sets up the libMesh::NemesisII_IO object used for outputting to the Nemesis format.
Definition: Nemesis.C:51
std::vector< Real > _global_values
Storage for scalar values (postprocessors and scalar AuxVariables)
Definition: Nemesis.h:81
VariableValue & sln()
InputParameters validParams< AdvancedOutput >()
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:253
virtual void outputScalarVariables() override
Writes scalar AuxVariables to global output parameters.
Definition: Nemesis.C:95
std::vector< std::string > _global_names
Storage for names of the above scalar values.
Definition: Nemesis.h:84
unsigned int _nemesis_num
Count of outputs per exodus file.
Definition: Nemesis.h:91
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:81
InputParameters validParams< Nemesis >()
Definition: Nemesis.C:26
const std::string & type() const
Get the type of this object.
Definition: MooseObject.h:53
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:87
virtual DofMap & dofMap()
Gets writeable reference to the dof map.
Definition: SystemBase.C:932
const std::set< std::string > & getScalarOutput()
The list of scalar variables names that are set for output.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:176
virtual void output(const ExecFlagType &type)
A single call to this function should output all the necessary data for a single timestep.
DofMap & dof_map
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
virtual bool shouldOutput(const ExecFlagType &type)
Handles logic for determining if a step should be output.
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
static InputParameters enableOutputTypes(const std::string &names=std::string())
A method for enabling individual output type control.
Based class for output objects.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:197
MatType type
virtual void initialSetup()
Initialization method.
bool _nemesis_initialized
Flag if the output has been initialized.
Definition: Nemesis.h:94
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
PetscInt n
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
Class for scalar variables (they are different).
MPI_Comm comm
virtual MooseMesh & mesh() override
virtual void output(const ExecFlagType &type) override
Overload the Output::output method, this is required for Nemesis output due to the method utilized fo...
Definition: Nemesis.C:144
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
const std::set< std::string > & getElementalVariableOutput()
The list of elemental nonlinear variables names that are set for output.
PostprocessorValue & getPostprocessorValue(const PostprocessorName &name)
Get a reference to the value associated with the postprocessor.
const std::set< std::string > & getNodalVariableOutput()
The list of nodal nonlinear variables names that are set for output.
virtual void outputPostprocessors() override
Writes postprocessor values to global output parameters.
Definition: Nemesis.C:80
unsigned int _file_num
Current output filename; utilized by filename() to create the proper suffix.
Definition: Nemesis.h:87
registerMooseObject("MooseApp", Nemesis)
virtual void meshChanged() override
Creates a new NemesisII_IO output object for outputing a new mesh.
Definition: Nemesis.C:61
virtual std::string filename() override
Returns the current filename, this method handles the -s000 suffix common to NemesisII files...
Definition: Nemesis.C:180
SystemBase & sys()
Get the system this variable is part of.
Nemesis(const InputParameters &parameters)
Class constructor.
Definition: Nemesis.C:39
Real getGlobalTimeOffset() const
Each App has it&#39;s own local time.
Definition: MooseApp.h:229
virtual ~Nemesis()
Class destructor.
Definition: Nemesis.C:48
std::unique_ptr< Nemesis_IO > _nemesis_io_ptr
Pointer to the libMesh::NemesisII_IO object that performs the actual data output. ...
Definition: Nemesis.h:78
Class for output data to the Nemesis format.
Definition: Nemesis.h:30