https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Nemesis.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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
23
26{
28 params += AdvancedOutput::enableOutputTypes("scalar postprocessor input");
29 params.addParam<bool>("write_hdf5", false, "Enables HDF5 output format for Nemesis files.");
30 params.addClassDescription("Object for output data in the Nemesis (parallel ExodusII) format.");
31 return params;
32}
33
35 : AdvancedOutput(parameters),
36 _nemesis_io_ptr(nullptr),
37 _file_num(declareRestartableData<unsigned int>("nemesis_file_num", 0)),
38 _nemesis_num(declareRestartableData<unsigned int>("nemesis_num", 0)),
39 _nemesis_initialized(false),
40 _recovering(_app.isRecovering()),
41 _nemesis_mesh_changed(declareRestartableData<bool>("nemesis_mesh_changed", true)),
42 _write_hdf5(getParam<bool>("write_hdf5"))
43{
44}
45
46void
51
52void
54{
55 // Do not delete the Nemesis_IO object if it has not been used; also there is no need to setup
56 // the object in this case, so just return
57 if (_nemesis_io_ptr != nullptr && !_nemesis_initialized)
58 return;
59
60 // Indicate to the Nemesis object that the mesh has changed
62}
63
64void
66{
68 {
69 // Do nothing if the Nemesis_IO objects exists, but has not been initialized
71 return;
72
73 // Do nothing if the mesh has not changed
75 return;
76 }
77
78 // Create the new NemesisIO object
79 _nemesis_io_ptr = std::make_unique<libMesh::Nemesis_IO>(_problem_ptr->mesh().getMesh());
81
82 if (_write_hdf5)
83 {
84#ifndef LIBMESH_HAVE_HDF5
85 mooseError("Moose input requested HDF Nemesis output, but libMesh was built without HDF5.");
86#endif
87
88 // This is redundant unless the libMesh default changes
89 _nemesis_io_ptr->set_hdf5_writing(true);
90 }
91 else
92 {
93 _nemesis_io_ptr->set_hdf5_writing(false);
94 }
95
97 {
98 // Set the recovering flag to false so that this special case is not triggered again
99 _recovering = false;
100
101 // Set the append flag to true b/c on recover the file is being appended
102 _nemesis_io_ptr->append(true);
103 }
104 else
105 {
106 // Increment file counter
108 _file_num++;
109
110 // Disable file appending and reset nemesis file number count
111 _nemesis_io_ptr->append(false);
112 _nemesis_num = 1;
113 }
114}
115
116void
118{
119 // List of desired postprocessor outputs
120 const std::set<std::string> & pps = getPostprocessorOutput();
121
122 // Append the postprocessor data to the global name value parameters; scalar outputs
123 // also append these member variables
124 for (const auto & name : pps)
125 {
126 _global_names.push_back(name);
128 }
129}
130
131void
133{
134 // List of desired scalar outputs
135 const std::set<std::string> & out = getScalarOutput();
136
137 // Append the scalar to the global output lists
138 for (const auto & out_name : out)
139 {
140 // Make sure scalar values are in sync with the solution vector
141 // and are visible on this processor. See TableOutput.C for
142 // TableOutput::outputScalarVariables() explanatory comments
143
144 MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
145 scalar_var.reinit();
146 VariableValue value(scalar_var.sln());
147
148 const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
149 const unsigned int n = dof_indices.size();
150 value.resize(n);
151
152 const DofMap & dof_map = scalar_var.sys().dofMap();
153 for (unsigned int i = 0; i != n; ++i)
154 {
155 const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
156 this->comm().broadcast(value[i], pid);
157 }
158
159 // If the scalar has a single component, output the name directly
160 if (n == 1)
161 {
162 _global_names.push_back(out_name);
163 _global_values.push_back(value[0]);
164 }
165
166 // If the scalar as many components add indices to the end of the name
167 else
168 {
169 for (unsigned int i = 0; i < n; ++i)
170 {
171 std::ostringstream os;
172 os << out_name << "_" << i;
173 _global_names.push_back(os.str());
174 _global_values.push_back(value[i]);
175 }
176 }
177 }
178}
179
180void
182{
183 outputSetup();
184
185 // Clear the global variables (postprocessors and scalars)
186 _global_names.clear();
187 _global_values.clear();
188
189 // Call the output methods
191
192 // Set up the whitelist of nodal variable names to write.
193 _nemesis_io_ptr->set_output_variables(
194 std::vector<std::string>(getNodalVariableOutput().begin(), getNodalVariableOutput().end()));
195
196 // Write nodal data
197 _nemesis_io_ptr->write_timestep(
200
201 // Write elemental data
202 std::vector<std::string> elemental(getElementalVariableOutput().begin(),
204 _nemesis_io_ptr->set_output_variables(elemental);
205 _nemesis_io_ptr->write_element_data(*_es_ptr);
206
207 // Increment output call counter for the current file
208 _nemesis_num++;
209
210 // Write the global variables (populated by the output methods)
211 if (!_global_values.empty())
212 _nemesis_io_ptr->write_global_data(_global_values, _global_names);
213
214 // Reset the mesh changed flag
215 _nemesis_mesh_changed = false;
216}
217
218std::string
220{
221 // Append the .n extension on the base file name
222 std::ostringstream output;
223 output << _file_base << ".n";
224
225 // Add the _000x extension to the file
226 if (_file_num > 1)
227 output << "-s" << std::setw(_padding) << std::setprecision(0) << std::setfill('0') << std::right
228 << _file_num;
229
230 // Return the filename
231 return output.str();
232}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
OutputTools< Real >::VariableValue VariableValue
Definition MooseTypes.h:348
registerMooseObject("MooseApp", Nemesis)
void ErrorVector unsigned int
Based class for output objects.
static InputParameters enableOutputTypes(const std::string &names=std::string())
A method for enabling individual output type control.
virtual void output()
A single call to this function should output all the necessary data for a single timestep.
virtual void initialSetup()
Call init() method on setup.
const std::set< std::string > & getElementalVariableOutput()
The list of elemental nonlinear variables names that are set for output.
const std::set< std::string > & getNodalVariableOutput()
The list of nodal nonlinear variables names that are set for output.
static InputParameters validParams()
const std::set< std::string > & getScalarOutput()
The list of scalar variables names that are set for output.
const std::set< std::string > & getPostprocessorOutput()
The list of postprocessor names that are set for output.
virtual MooseVariableScalar & getScalarVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
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.
virtual MooseMesh & mesh() override
unsigned int _padding
Number of digits to pad the extensions.
Definition FileOutput.h:83
std::string _file_base
The base filename from the input paramaters.
Definition FileOutput.h:89
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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.
Real getGlobalTimeOffset() const
Each App has it's own local time.
Definition MooseApp.h:318
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3557
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
SystemBase & sys()
Get the system this variable is part of.
Class for scalar variables (they are different).
const VariableValue & sln() const
void reinit(bool reinit_for_derivative_reordering=false)
Fill out the VariableValue arrays from the system solution vector.
unsigned int & _file_num
Current output filename; utilized by filename() to create the proper suffix.
Definition Nemesis.h:90
virtual void outputPostprocessors() override
Writes postprocessor values to global output parameters.
Definition Nemesis.C:117
std::vector< Real > _global_values
Storage for scalar values (postprocessors and scalar AuxVariables)
Definition Nemesis.h:84
virtual void outputSetup()
Performs the necessary deletion and re-creating of NemesisII_IO object.
Definition Nemesis.C:65
bool & _nemesis_mesh_changed
A flag indicating to the Nemesis object that the mesh has changed.
Definition Nemesis.h:103
virtual void outputScalarVariables() override
Writes scalar AuxVariables to global output parameters.
Definition Nemesis.C:132
virtual void meshChanged() override
Creates a new NemesisII_IO output object for outputting a new mesh.
Definition Nemesis.C:53
virtual std::string filename() override
Returns the current filename, this method handles the -s000 suffix common to NemesisII files.
Definition Nemesis.C:219
bool _write_hdf5
Flag to output HDF5 format (when available) in Nemesis.
Definition Nemesis.h:109
unsigned int & _nemesis_num
Count of outputs per exodus file.
Definition Nemesis.h:94
Nemesis(const InputParameters &parameters)
Class constructor.
Definition Nemesis.C:34
std::vector< std::string > _global_names
Storage for names of the above scalar values.
Definition Nemesis.h:87
bool _nemesis_initialized
Flag if the output has been initialized.
Definition Nemesis.h:97
std::unique_ptr< libMesh::Nemesis_IO > _nemesis_io_ptr
Pointer to the libMesh::NemesisII_IO object that performs the actual data output.
Definition Nemesis.h:81
virtual void initialSetup() override
Sets up the libMesh::NemesisII_IO object used for outputting to the Nemesis format.
Definition Nemesis.C:47
virtual void output() override
Overload the Output::output method, this is required for Nemesis output due to the method utilized fo...
Definition Nemesis.C:181
static InputParameters validParams()
Definition Nemesis.C:25
bool _recovering
Flag indicating MOOSE is recovering via –recover command-line option.
Definition Nemesis.h:100
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition Output.h:185
libMesh::EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition Output.h:194
virtual Real getOutputTime()
Get the time that will be used for stream/file outputting.
virtual libMesh::DofMap & dofMap()
Gets writeable reference to the dof map.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
const Parallel::Communicator & comm() const