www.mooseframework.org
TableOutput.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 "TableOutput.h"
11 
12 // MOOSE includes
13 #include "Conversion.h"
14 #include "FEProblem.h"
15 #include "Executioner.h"
16 #include "MooseApp.h"
17 #include "MooseVariableScalar.h"
18 #include "PetscSupport.h"
19 #include "Postprocessor.h"
20 #include "SystemBase.h"
21 
22 #include "libmesh/dof_map.h"
23 #include "libmesh/string_to_enum.h"
24 
25 template <>
28 {
29  // Fit mode selection Enum
31 
32  // Base class parameters
34  params += AdvancedOutput::enableOutputTypes("postprocessor scalar vector_postprocessor");
35 
36  // Option for writing vector_postprocessor time file
37  params.addParam<bool>("time_data",
38  false,
39  "When true and VecptorPostprocessor data exists, write "
40  "a csv file containing the timestep and time "
41  "information.");
42 
43  // Add option for appending file on restart
44  params.addParam<bool>("append_restart", false, "Append existing file on restart");
45 
46  params.addParam<bool>(
47  "time_column",
48  true,
49  "Whether or not the 'time' column should be written for Postprocessor CSV files");
50 
51  params.addParam<Real>("new_row_tolerance",
52  libMesh::TOLERANCE * libMesh::TOLERANCE,
53  "The independent variable tolerance for determining when a new row should "
54  "be added to the table (Note: This value must be set independently for "
55  "Postprocessor output to type=Console and type=CSV file separately.");
56 
57  return params;
58 }
59 
61  : AdvancedOutput(parameters),
62  _tables_restartable(getParam<bool>("append_restart")),
63  _postprocessor_table(_tables_restartable
64  ? declareRestartableData<FormattedTable>("postprocessor_table")
65  : declareRecoverableData<FormattedTable>("postprocessor_table")),
66  _vector_postprocessor_time_tables(
67  _tables_restartable ? declareRestartableData<std::map<std::string, FormattedTable>>(
68  "vector_postprocessor_time_table")
69  : declareRecoverableData<std::map<std::string, FormattedTable>>(
70  "vector_postprocessor_time_table")),
71  _scalar_table(_tables_restartable ? declareRestartableData<FormattedTable>("scalar_table")
72  : declareRecoverableData<FormattedTable>("scalar_table")),
73  _all_data_table(_tables_restartable ? declareRestartableData<FormattedTable>("all_data_table")
74  : declareRecoverableData<FormattedTable>("all_data_table")),
75  _new_row_tol(getParam<Real>("new_row_tolerance")),
76  _time_data(getParam<bool>("time_data")),
77  _time_column(getParam<bool>("time_column"))
78 
79 {
80  // Set a Boolean indicating whether or not we will output the time column
83 }
84 
85 void
87 {
88  // Add new row to the tables
91  {
94  }
95 
96  // List of names of the postprocessors to output
97  const std::set<std::string> & out = getPostprocessorOutput();
98 
99  // Loop through the postprocessor names and extract the values from the PostprocessorData storage
100  for (const auto & out_name : out)
101  {
103 
104  _postprocessor_table.addData(out_name, value);
105  _all_data_table.addData(out_name, value);
106  }
107 }
108 
109 void
111 {
112  // List of names of the postprocessors to output
113  const std::set<std::string> & out = getVectorPostprocessorOutput();
114 
115  // Loop through the postprocessor names and extract the values from the VectorPostprocessorData
116  // storage
117  for (const auto & vpp_name : out)
118  {
120  {
121  const auto & vectors = _problem_ptr->getVectorPostprocessorVectors(vpp_name);
122 
123  auto insert_pair =
125 
126  FormattedTable & table = insert_pair.first->second;
127 
128  table.clear();
129  table.outputTimeColumn(false);
130 
131  for (const auto & vec_it : vectors)
132  {
133  const auto & vector = *vec_it.second.current;
134  table.addData(vec_it.first, vector);
135  }
136 
137  if (_time_data)
138  {
140  t_table.addData("timestep", _t_step, _time);
141  }
142  }
143  }
144 }
145 
146 void
148 {
149  // List of scalar variables
150  const std::set<std::string> & out = getScalarOutput();
151 
152  // Loop through each variable
153  for (const auto & out_name : out)
154  {
155  // Get reference to the variable (0 is for TID)
156  MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
157 
158  // Make sure the value of the variable is in sync with the solution vector
159  scalar_var.reinit();
160 
161  // Next we need to make sure all processors agree on the value of
162  // the variable - not all processors may be able to see all
163  // scalars!
164 
165  // Make a copy rather than taking a reference to the MooseArray,
166  // because if a processor can't see that scalar variable's values
167  // then we'll need to do our own communication of them.
168  VariableValue value(scalar_var.sln());
169  auto value_size = value.size();
170 
171  // libMesh *does* currently guarantee that all processors can
172  // calculate all scalar DoF indices, so this is a const reference
173  const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
174  auto dof_size = dof_indices.size();
175  bool need_release = false;
176 
177  // In dbg mode, if we don't see a scalar we might not even have
178  // its array allocated to full length yet.
179  if (dof_size > value_size)
180  {
181  value.resize(dof_size);
182  need_release = true;
183  }
184 
185  // Finally, let's just let the owners broadcast their values.
186  // There's probably lots of room to optimize this communication
187  // via merging broadcasts and making them asynchronous, but this
188  // code path shouldn't be hit often enough for that to matter.
189 
190  const DofMap & dof_map = scalar_var.sys().dofMap();
191  for (decltype(dof_size) i = 0; i < dof_size; ++i)
192  {
193  const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
194  this->comm().broadcast(value[i], pid);
195  }
196 
197  // If the variable has a single component, simply output the value with the name
198  if (dof_size == 1)
199  {
200  _scalar_table.addData(out_name, value[0], time());
201  _all_data_table.addData(out_name, value[0], time());
202  }
203 
204  // Multi-component variables are appended with the component index
205  else
206  for (decltype(dof_size) i = 0; i < dof_size; ++i)
207  {
208  std::ostringstream os;
209  os << out_name << "_" << i;
210  _scalar_table.addData(os.str(), value[i], time());
211  _all_data_table.addData(os.str(), value[i], time());
212  }
213 
214  // If we ended up reallocating, we'll need to release memory or leak it
215  if (need_release)
216  value.release();
217  }
218 }
219 
220 void
222 {
224  for (auto & pair : _vector_postprocessor_tables)
225  pair.second.clear();
226  for (auto & pair : _vector_postprocessor_time_tables)
227  pair.second.clear();
230 }
const std::set< std::string > & getPostprocessorOutput()
The list of postprocessor names that are set for output.
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:231
VariableValue & sln()
InputParameters validParams< AdvancedOutput >()
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:253
FormattedTable & _postprocessor_table
Table containing postprocessor data.
Definition: TableOutput.h:66
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
int & _t_step
The current time step.
Definition: Output.h:194
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
const std::set< std::string > & getVectorPostprocessorOutput()
The list of VectorPostprocessor names that are set for output.
std::pair< typename M::iterator, bool > moose_try_emplace(M &m, const typename M::key_type &k, Args &&... args)
Function to mirror the behavior of the C++17 std::map::try_emplace() method (no hint).
Definition: Moose.h:75
bool vectorPostprocessorHasVectors(const std::string &vpp_name)
Whether or not the specified VectorPostprocessor has declared any vectors.
void outputTimeColumn(bool output_time)
Set whether or not to output time column.
virtual void outputVectorPostprocessors() override
Populates the tables with VectorPostprocessor values.
Definition: TableOutput.C:110
static MooseEnum getWidthModes()
const std::vector< std::pair< std::string, VectorPostprocessorData::VectorPostprocessorState > > & getVectorPostprocessorVectors(const std::string &vpp_name)
Get the vectors for a specific VectorPostprocessor.
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.
TableOutput(const InputParameters &parameters)
Class constructor.
Definition: TableOutput.C:60
FormattedTable & _scalar_table
Table containing scalar aux variables.
Definition: TableOutput.h:75
Real PostprocessorValue
MOOSE typedefs.
Definition: MooseTypes.h:154
DofMap & dof_map
const bool _time_column
Enable/disable output of time column for Postprocessors.
Definition: TableOutput.h:87
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
void clear()
Definition: TableOutput.C:221
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
static InputParameters enableOutputTypes(const std::string &names=std::string())
A method for enabling individual output type control.
This class is used for building, formatting, and outputting tables of numbers.
const bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:84
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:86
FormattedTable & _all_data_table
Table containing postprocessor values and scalar aux variables.
Definition: TableOutput.h:78
Based class for output objects.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:197
void addData(const std::string &name, Real value)
Method for adding data to the output table.
Class for scalar variables (they are different).
MPI_Comm comm
PostprocessorValue & getPostprocessorValue(const PostprocessorName &name)
Get a reference to the value associated with the postprocessor.
const Real _new_row_tol
Tolerance used when deciding whether or not to add a new row to the table.
Definition: TableOutput.h:81
void addRow(Real time)
Force a new row in the table with the passed in time.
SystemBase & sys()
Get the system this variable is part of.
Real & _time
The current time for output purposes.
Definition: Output.h:188
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:69
Real getLastTime()
Retrieve the last time (or independent variable) value.
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:147
InputParameters validParams< TableOutput >()
Definition: TableOutput.C:27
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:72