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 
26 {
27  // Get the base class parameters
29  params += AdvancedOutput::enableOutputTypes("scalar postprocessor input");
30 
31  // Add description for the Nemesis class
32  params.addClassDescription("Object for output data in the Nemesis (parallel ExodusII) format.");
33 
34  // Return the InputParameters
35  return params;
36 }
37 
38 Nemesis::Nemesis(const InputParameters & parameters)
39  : AdvancedOutput(parameters),
40  _nemesis_io_ptr(nullptr),
41  _file_num(0),
42  _nemesis_num(0),
43  _nemesis_initialized(false)
44 {
45 }
46 
48 
49 void
51 {
52 
54 
55  // Make certain that a Nemesis_IO object exists
56  meshChanged();
57 }
58 
59 void
61 {
62  // Do not delete the Nemesis_IO object if it has not been used; also there is no need to setup
63  // the object in this case, so just return
64  if (_nemesis_io_ptr != nullptr && !_nemesis_initialized)
65  return;
66 
67  // Increment the file number
68  _file_num++;
69 
70  // Reset the number of outputs for this file
71  _nemesis_num = 1;
72 
73  // Create the new NemesisIO object
74  _nemesis_io_ptr = std::make_unique<Nemesis_IO>(_problem_ptr->mesh().getMesh());
75  _nemesis_initialized = false;
76 }
77 
78 void
80 {
81  // List of desired postprocessor outputs
82  const std::set<std::string> & pps = getPostprocessorOutput();
83 
84  // Append the postprocessor data to the global name value parameters; scalar outputs
85  // also append these member variables
86  for (const auto & name : pps)
87  {
88  _global_names.push_back(name);
90  }
91 }
92 
93 void
95 {
96  // List of desired scalar outputs
97  const std::set<std::string> & out = getScalarOutput();
98 
99  // Append the scalar to the global output lists
100  for (const auto & out_name : out)
101  {
102  // Make sure scalar values are in sync with the solution vector
103  // and are visible on this processor. See TableOutput.C for
104  // TableOutput::outputScalarVariables() explanatory comments
105 
106  MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
107  scalar_var.reinit();
108  VariableValue value(scalar_var.sln());
109 
110  const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
111  const unsigned int n = dof_indices.size();
112  value.resize(n);
113 
114  const DofMap & dof_map = scalar_var.sys().dofMap();
115  for (unsigned int i = 0; i != n; ++i)
116  {
117  const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
118  this->comm().broadcast(value[i], pid);
119  }
120 
121  // If the scalar has a single component, output the name directly
122  if (n == 1)
123  {
124  _global_names.push_back(out_name);
125  _global_values.push_back(value[0]);
126  }
127 
128  // If the scalar as many components add indices to the end of the name
129  else
130  {
131  for (unsigned int i = 0; i < n; ++i)
132  {
133  std::ostringstream os;
134  os << out_name << "_" << i;
135  _global_names.push_back(os.str());
136  _global_values.push_back(value[i]);
137  }
138  }
139  }
140 }
141 
142 void
144 {
145  // Clear the global variables (postprocessors and scalars)
146  _global_names.clear();
147  _global_values.clear();
148 
149  // Call the output methods
151 
152  // Set up the whitelist of nodal variable names to write.
153  _nemesis_io_ptr->set_output_variables(
154  std::vector<std::string>(getNodalVariableOutput().begin(), getNodalVariableOutput().end()));
155 
156  // Write nodal data
157  _nemesis_io_ptr->write_timestep(
159  _nemesis_initialized = true;
160 
161  // Write elemental data
162  std::vector<std::string> elemental(getElementalVariableOutput().begin(),
164  _nemesis_io_ptr->set_output_variables(elemental);
165  _nemesis_io_ptr->write_element_data(*_es_ptr);
166 
167  // Increment output call counter for the current file
168  _nemesis_num++;
169 
170  // Write the global variables (populated by the output methods)
171  if (!_global_values.empty())
172  _nemesis_io_ptr->write_global_data(_global_values, _global_names);
173 }
174 
175 std::string
177 {
178  // Append the .e extension on the base file name
179  std::ostringstream output;
180  output << _file_base << ".e";
181 
182  // Add the _000x extension to the file
183  if (_file_num > 1)
184  output << "-s" << std::setw(_padding) << std::setprecision(0) << std::setfill('0') << std::right
185  << _file_num;
186 
187  // Return the filename
188  return output.str();
189 }
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:50
static InputParameters validParams()
Definition: Nemesis.C:25
std::vector< Real > _global_values
Storage for scalar values (postprocessors and scalar AuxVariables)
Definition: Nemesis.h:77
void reinit(bool reinit_for_derivative_reordering=false)
Fill out the VariableValue arrays from the system solution vector.
virtual void outputScalarVariables() override
Writes scalar AuxVariables to global output parameters.
Definition: Nemesis.C:94
std::vector< std::string > _global_names
Storage for names of the above scalar values.
Definition: Nemesis.h:80
unsigned int _nemesis_num
Count of outputs per exodus file.
Definition: Nemesis.h:87
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:89
virtual void output()
A single call to this function should output all the necessary data for a single timestep.
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
unsigned int _padding
Number of digits to pad the extensions.
Definition: FileOutput.h:83
uint8_t processor_id_type
virtual DofMap & dofMap()
Gets writeable reference to the dof map.
Definition: SystemBase.C:1131
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
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:3198
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:188
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:179
virtual MooseVariableScalar & getScalarVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
static InputParameters enableOutputTypes(const std::string &names=std::string())
A method for enabling individual output type control.
const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name, std::size_t t_index=0) const
Get a read-only reference to the value associated with a Postprocessor that exists.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
virtual void output() override
Overload the Output::output method, this is required for Nemesis output due to the method utilized fo...
Definition: Nemesis.C:143
Based class for output objects.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:302
virtual void initialSetup()
Call init() method on setup.
bool _nemesis_initialized
Flag if the output has been initialized.
Definition: Nemesis.h:90
OStreamProxy out
Class for scalar variables (they are different).
virtual MooseMesh & mesh() override
const std::set< std::string > & getElementalVariableOutput()
The list of elemental nonlinear variables names that are set for output.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
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:79
unsigned int _file_num
Current output filename; utilized by filename() to create the proper suffix.
Definition: Nemesis.h:83
registerMooseObject("MooseApp", Nemesis)
static InputParameters validParams()
virtual void meshChanged() override
Creates a new NemesisII_IO output object for outputting a new mesh.
Definition: Nemesis.C:60
virtual std::string filename() override
Returns the current filename, this method handles the -s000 suffix common to NemesisII files...
Definition: Nemesis.C:176
SystemBase & sys()
Get the system this variable is part of.
Nemesis(const InputParameters &parameters)
Class constructor.
Definition: Nemesis.C:38
Real getGlobalTimeOffset() const
Each App has it&#39;s own local time.
Definition: MooseApp.h:307
const VariableValue & sln() const
virtual ~Nemesis()
Class destructor.
Definition: Nemesis.C:47
virtual Real getOutputTime()
Get the time that will be used for stream/file outputting.
Definition: PetscOutput.C:265
std::unique_ptr< Nemesis_IO > _nemesis_io_ptr
Pointer to the libMesh::NemesisII_IO object that performs the actual data output. ...
Definition: Nemesis.h:74
Class for output data to the Nemesis format.
Definition: Nemesis.h:24