www.mooseframework.org
TableOutput.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "TableOutput.h"
16 
17 // MOOSE includes
18 #include "Conversion.h"
19 #include "FEProblem.h"
20 #include "Executioner.h"
21 #include "MooseApp.h"
22 #include "MooseVariableScalar.h"
23 #include "PetscSupport.h"
24 #include "Postprocessor.h"
25 #include "SystemBase.h"
26 
27 #include "libmesh/dof_map.h"
28 #include "libmesh/string_to_enum.h"
29 
30 template <>
33 {
34  // Fit mode selection Enum
36 
37  // Base class parameters
39  params += AdvancedOutput::enableOutputTypes("postprocessor scalar vector_postprocessor");
40 
41  // Option for writing vector_postprocessor time file
42  params.addParam<bool>("time_data",
43  false,
44  "When true and VecptorPostprocessor data exists, write "
45  "a csv file containing the timestep and time "
46  "information.");
47 
48  // Add option for appending file on restart
49  params.addParam<bool>("append_restart", false, "Append existing file on restart");
50 
51  params.addParam<bool>(
52  "time_column",
53  true,
54  "Whether or not the 'time' column should be written for Postprocessor CSV files");
55 
56  return params;
57 }
58 
60  : AdvancedOutput(parameters),
61  _tables_restartable(getParam<bool>("append_restart")),
62  _postprocessor_table(_tables_restartable
63  ? declareRestartableData<FormattedTable>("postprocessor_table")
64  : declareRecoverableData<FormattedTable>("postprocessor_table")),
65  _vector_postprocessor_time_tables(
66  _tables_restartable ? declareRestartableData<std::map<std::string, FormattedTable>>(
67  "vector_postprocessor_time_table")
68  : declareRecoverableData<std::map<std::string, FormattedTable>>(
69  "vector_postprocessor_time_table")),
70  _scalar_table(_tables_restartable ? declareRestartableData<FormattedTable>("scalar_table")
71  : declareRecoverableData<FormattedTable>("scalar_table")),
72  _all_data_table(_tables_restartable ? declareRestartableData<FormattedTable>("all_data_table")
73  : declareRecoverableData<FormattedTable>("all_data_table")),
74  _time_data(getParam<bool>("time_data")),
75  _time_column(getParam<bool>("time_column"))
76 
77 {
78 }
79 
80 void
82 {
83  // List of names of the postprocessors to output
84  const std::set<std::string> & out = getPostprocessorOutput();
85 
86  // Loop through the postprocessor names and extract the values from the PostprocessorData storage
87  for (const auto & out_name : out)
88  {
90 
92  _postprocessor_table.addData(out_name, value, time());
93 
95  _all_data_table.addData(out_name, value, time());
96  }
97 }
98 
99 void
101 {
102  // List of names of the postprocessors to output
103  const std::set<std::string> & out = getVectorPostprocessorOutput();
104 
105  // Loop through the postprocessor names and extract the values from the VectorPostprocessorData
106  // storage
107  for (const auto & vpp_name : out)
108  {
110  {
111  const auto & vectors = _problem_ptr->getVectorPostprocessorVectors(vpp_name);
112 
113  auto insert_pair =
115 
116  FormattedTable & table = insert_pair.first->second;
117 
118  table.clear();
119  table.outputTimeColumn(false);
120 
121  for (const auto & vec_it : vectors)
122  {
123  const auto & vector = *vec_it.second.current;
124  table.addData(vec_it.first, vector);
125  }
126 
127  if (_time_data)
128  {
130  t_table.addData("timestep", _t_step, _time);
131  }
132  }
133  }
134 }
135 
136 void
138 {
139  // List of scalar variables
140  const std::set<std::string> & out = getScalarOutput();
141 
142  // Loop through each variable
143  for (const auto & out_name : out)
144  {
145  // Get reference to the variable (0 is for TID)
146  MooseVariableScalar & scalar_var = _problem_ptr->getScalarVariable(0, out_name);
147 
148  // Make sure the value of the variable is in sync with the solution vector
149  scalar_var.reinit();
150 
151  // Next we need to make sure all processors agree on the value of
152  // the variable - not all processors may be able to see all
153  // scalars!
154 
155  // Make a copy rather than taking a reference to the MooseArray,
156  // because if a processor can't see that scalar variable's values
157  // then we'll need to do our own communication of them.
158  VariableValue value = scalar_var.sln();
159  auto value_size = value.size();
160 
161  // libMesh *does* currently guarantee that all processors can
162  // calculate all scalar DoF indices, so this is a const reference
163  const std::vector<dof_id_type> & dof_indices = scalar_var.dofIndices();
164  auto dof_size = dof_indices.size();
165  bool need_release = false;
166 
167  // In dbg mode, if we don't see a scalar we might not even have
168  // its array allocated to full length yet.
169  if (dof_size > value_size)
170  {
171  value.resize(dof_size);
172  need_release = true;
173  }
174 
175  // Finally, let's just let the owners broadcast their values.
176  // There's probably lots of room to optimize this communication
177  // via merging broadcasts and making them asynchronous, but this
178  // code path shouldn't be hit often enough for that to matter.
179 
180  const DofMap & dof_map = scalar_var.sys().dofMap();
181  for (decltype(dof_size) i = 0; i < dof_size; ++i)
182  {
183  const processor_id_type pid = dof_map.dof_owner(dof_indices[i]);
184  this->comm().broadcast(value[i], pid);
185  }
186 
187  // If the variable has a single component, simply output the value with the name
188  if (dof_size == 1)
189  {
190  _scalar_table.addData(out_name, value[0], time());
191  _all_data_table.addData(out_name, value[0], time());
192  }
193 
194  // Multi-component variables are appended with the component index
195  else
196  for (decltype(dof_size) i = 0; i < dof_size; ++i)
197  {
198  std::ostringstream os;
199  os << out_name << "_" << i;
200  _scalar_table.addData(os.str(), value[i], time());
201  _all_data_table.addData(os.str(), value[i], time());
202  }
203 
204  // If we ended up reallocating, we'll need to release memory or leak it
205  if (need_release)
206  value.release();
207  }
208 }
const std::set< std::string > & getPostprocessorOutput()
The list of postprocessor names that are set for output.
VariableValue & sln()
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:66
void resize(const unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:205
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:258
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...
void addData(const std::string &name, Real value, Real time)
Method for adding data to the output table.
int & _t_step
The current time step.
Definition: Output.h:195
const std::set< std::string > & getVectorPostprocessorOutput()
The list of VectorPostprocessor names that are set for output.
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.
InputParameters validParams< AdvancedOutput >()
virtual void outputVectorPostprocessors() override
Populates the tables with VectorPostprocessor values.
Definition: TableOutput.C:100
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 the dof map.
Definition: SystemBase.C:610
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:59
FormattedTable & _scalar_table
Table containing scalar aux variables.
Definition: TableOutput.h:79
Real PostprocessorValue
MOOSE typedefs.
Definition: MooseTypes.h:73
DofMap & dof_map
bool _time_column
Enable/disable output of time column for Postprocessors.
Definition: TableOutput.h:88
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:171
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
static InputParameters enableOutputTypes(const std::string &names=std::string())
A method for enabling individual output type control.
bool _time_data
Enable/disable VecptorPostprocessor time data file.
Definition: TableOutput.h:85
This class is used for building, formatting, and outputting tables of numbers.
virtual void outputPostprocessors() override
Populates the tables with postprocessor values.
Definition: TableOutput.C:81
FormattedTable & _all_data_table
Table containing postprocessor values and scalar aux variables.
Definition: TableOutput.h:82
Based class for output objects.
std::vector< dof_id_type > & dofIndices()
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:250
void release()
Manually deallocates the data pointer.
Definition: MooseArray.h:56
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.
SystemBase & sys()
Get the system this variable is part of.
Real & _time
The current time for output purposes.
Definition: Output.h:189
std::map< std::string, FormattedTable > _vector_postprocessor_tables
Formatted tables for outputting vector postprocessor data. One per VectorPostprocessor.
Definition: TableOutput.h:73
virtual void outputScalarVariables() override
Populates the tables with scalar aux variables.
Definition: TableOutput.C:137
InputParameters validParams< TableOutput >()
Definition: TableOutput.C:32
std::map< std::string, FormattedTable > & _vector_postprocessor_time_tables
Table for vector postprocessor time data.
Definition: TableOutput.h:76