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 
27 {
28  // Base class parameters
30  params += AdvancedOutput::enableOutputTypes("postprocessor scalar vector_postprocessor reporter");
31 
32  // Option for writing vector_postprocessor time file
33  params.addParam<bool>("time_data",
34  false,
35  "When true and VectorPostprocessor data exists, write "
36  "a csv file containing the timestep and time "
37  "information.");
38 
39  // Add option for appending file on restart
40  params.addParam<bool>("append_restart", false, "Append existing file on restart");
41 
42  params.addParam<bool>(
43  "time_column",
44  true,
45  "Whether or not the 'time' column should be written for Postprocessor CSV files");
46 
47  params.addParam<Real>("new_row_tolerance",
49  "The independent variable tolerance for determining when a new row should "
50  "be added to the table (Note: This value must be set independently for "
51  "Postprocessor output to type=Console and type=CSV file separately.");
52  params.addParamNamesToGroup("new_row_tolerance time_data time_column", "Table formatting");
53 
54  return params;
55 }
56 
58  : AdvancedOutput(parameters),
59  _tables_restartable(getParam<bool>("append_restart")),
60  _postprocessor_table(_tables_restartable
61  ? declareRestartableData<FormattedTable>("postprocessor_table")
62  : declareRecoverableData<FormattedTable>("postprocessor_table")),
63  _vector_postprocessor_time_tables(
64  _tables_restartable ? declareRestartableData<std::map<std::string, FormattedTable>>(
65  "vector_postprocessor_time_table")
66  : declareRecoverableData<std::map<std::string, FormattedTable>>(
67  "vector_postprocessor_time_table")),
68  _scalar_table(_tables_restartable ? declareRestartableData<FormattedTable>("scalar_table")
69  : declareRecoverableData<FormattedTable>("scalar_table")),
70  _reporter_table(_tables_restartable ? declareRestartableData<FormattedTable>("reporter_table")
71  : declareRecoverableData<FormattedTable>("reporter_table")),
72  _all_data_table(_tables_restartable ? declareRestartableData<FormattedTable>("all_data_table")
73  : declareRecoverableData<FormattedTable>("all_data_table")),
74  _new_row_tol(getParam<Real>("new_row_tolerance")),
75  _time_data(getParam<bool>("time_data")),
76  _time_column(getParam<bool>("time_column"))
77 
78 {
79  // 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
93 
94  if (_all_data_table.empty() ||
97 
98  // List of names of the postprocessors to output
99  const std::set<std::string> & out = getPostprocessorOutput();
100 
101  // Loop through the postprocessor names and extract the values from the PostprocessorData storage
102  for (const auto & out_name : out)
103  {
106  _all_data_table.addData(out_name, value);
107  }
108 }
109 
110 void
112 {
113  // List of VPP objects with output
114  const std::set<std::string> & vpps = getVectorPostprocessorOutput();
115 
116  for (const auto & combined_name : getReporterOutput())
117  {
118  ReporterName r_name(combined_name);
119 
120  outputReporter<bool>(r_name);
121  outputReporter<unsigned short int>(r_name);
122  outputReporter<unsigned int>(r_name);
123  outputReporter<unsigned long int>(r_name);
124  outputReporter<unsigned long long int>(r_name);
125  outputReporter<short int>(r_name);
126  outputReporter<int>(r_name);
127  outputReporter<long int>(r_name);
128  outputReporter<long long int>(r_name);
129  outputReporter<float>(r_name);
130  outputReporter<long double>(r_name);
131  outputReporter<char>(r_name);
132  outputReporter<std::string>(r_name);
133 
134  // Need to check for reals because PPs and VPPs might have already been outputted
135  if (!hasPostprocessorByName(r_name.getObjectName()) &&
136  vpps.find(r_name.getObjectName()) == vpps.end())
137  outputReporter<Real>(r_name);
138  }
139 }
140 
141 void
143 {
144  // List of VPP objects with output
145  const std::set<std::string> & out = getVectorPostprocessorOutput();
146 
147  for (const auto & r_name : _reporter_data.getReporterNames())
148  {
149  const std::string & vpp_name = r_name.getObjectName();
150  const std::string & vec_name = r_name.getValueName();
151  const bool vpp_out = out.find(vpp_name) != out.end();
152  if (vpp_out && (_reporter_data.hasReporterValue<VectorPostprocessorValue>(r_name)))
153  {
154  auto insert_pair =
156 
157  FormattedTable & table = insert_pair.first->second;
158  table.outputTimeColumn(false);
159 
160  const auto & vector = _reporter_data.getReporterValue<VectorPostprocessorValue>(r_name);
161  table.addData(vec_name, vector);
162 
163  if (_time_data)
164  {
166  t_table.addData("timestep", _t_step, _time);
167  }
168  }
169  }
170 }
171 
172 void
174 {
175  // List of scalar variables
176  const std::set<std::string> & out = getScalarOutput();
177 
178  // Loop through each variable
179  for (const auto & out_name : out)
180  {
181  // Get reference to the variable (0 is for TID)
182  MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
183 
184  // Make sure the value of the variable is in sync with the solution vector
185  scalar_var.reinit();
186 
187  // Next we need to make sure all processors agree on the value of
188  // the variable - not all processors may be able to see all
189  // scalars!
190 
191  // Make a copy rather than taking a reference to the MooseArray,
192  // because if a processor can't see that scalar variable's values
193  // then we'll need to do our own communication of them.
194  VariableValue value(scalar_var.sln());
195  auto value_size = value.size();
196 
197  // libMesh *does* currently guarantee that all processors can
198  // calculate all scalar DoF indices, so this is a const reference
199  const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
200  auto dof_size = dof_indices.size();
201  bool need_release = false;
202 
203  // In dbg mode, if we don't see a scalar we might not even have
204  // its array allocated to full length yet.
205  if (dof_size > value_size)
206  {
207  value.resize(dof_size);
208  need_release = true;
209  }
210 
211  // Finally, let's just let the owners broadcast their values.
212  // There's probably lots of room to optimize this communication
213  // via merging broadcasts and making them asynchronous, but this
214  // code path shouldn't be hit often enough for that to matter.
215 
216  const DofMap & dof_map = scalar_var.sys().dofMap();
217  for (decltype(dof_size) i = 0; i < dof_size; ++i)
218  {
219  const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
220  this->comm().broadcast(value[i], pid);
221  }
222 
223  // If the variable has a single component, simply output the value with the name
224  if (dof_size == 1)
225  {
226  _scalar_table.addData(out_name, value[0], getOutputTime());
227  _all_data_table.addData(out_name, value[0], getOutputTime());
228  }
229 
230  // Multi-component variables are appended with the component index
231  else
232  for (decltype(dof_size) i = 0; i < dof_size; ++i)
233  {
234  std::ostringstream os;
235  os << out_name << "_" << i;
238  }
239 
240  // If we ended up reallocating, we'll need to release memory or leak it
241  if (need_release)
242  value.release();
243  }
244 }
245 
246 void
248 {
251  for (auto & pair : _vector_postprocessor_tables)
252  pair.second.clear();
253  for (auto & pair : _vector_postprocessor_time_tables)
254  pair.second.clear();
257 }
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:346
void reinit(bool reinit_for_derivative_reordering=false)
Fill out the VariableValue arrays from the system solution vector.
static constexpr Real TOLERANCE
void addData(const std::string &name, const T &value)
Method for adding data to the output table.
FormattedTable & _postprocessor_table
Table containing postprocessor data.
Definition: TableOutput.h:70
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
int & _t_step
The current time step.
Definition: Output.h:214
bool empty() const
Returns a boolean value based on whether the FormattedTable contains data or not. ...
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
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:94
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:142
const std::set< std::string > & getReporterOutput()
The list of Reporter names that are set for output.
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.
const T & getReporterValue(const ReporterName &reporter_name, const MooseObject &consumer, const ReporterMode &mode, const std::size_t time_index=0) const
Method for returning read only references to Reporter values.
Definition: ReporterData.h:379
TableOutput(const InputParameters &parameters)
Class constructor.
Definition: TableOutput.C:57
bool hasPostprocessorByName(const PostprocessorName &name) const
Determine if the Postprocessor data exists.
FormattedTable & _scalar_table
Table containing scalar aux variables.
Definition: TableOutput.h:79
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:191
const bool _time_column
Enable/disable output of time column for Postprocessors.
Definition: TableOutput.h:94
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.
void clear()
Definition: TableOutput.C:247
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
virtual void outputReporters() override
Populates the tables with Reporter values.
Definition: TableOutput.C:111
const std::string & getObjectName() const
Return the object name that produces the Reporter value.
Definition: ReporterName.C:34
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 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.
const bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:91
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
std::set< ReporterName > getReporterNames() const
Return a list of all reporter names.
Definition: ReporterData.C:37
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:86
const ReporterData & _reporter_data
Storage for Reporter values.
FormattedTable & _all_data_table
Table containing postprocessor values, scalar aux variables, and Real Reporters.
Definition: TableOutput.h:85
Based class for output objects.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:302
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:192
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
OStreamProxy out
Class for scalar variables (they are different).
FormattedTable & _reporter_table
Table containing Real Reporter values.
Definition: TableOutput.h:82
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
Definition: ReporterData.h:436
const Real _new_row_tol
Tolerance used when deciding whether or not to add a new row to the table.
Definition: TableOutput.h:88
void addRow(Real time)
Force a new row in the table with the passed in time.
static InputParameters validParams()
SystemBase & sys()
Get the system this variable is part of.
const VariableValue & sln() const
Real & _time
The current time for output purposes.
Definition: Output.h:208
virtual Real getOutputTime()
Get the time that will be used for stream/file outputting.
Definition: PetscOutput.C:265
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:73
Real getLastTime()
Retrieve the last time (or independent variable) value.
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:173
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:76
static InputParameters validParams()
Definition: TableOutput.C:26