https://mooseframework.inl.gov
CommonOutputAction.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 // MOOSE includes
11 #include "CommonOutputAction.h"
12 #include "MooseApp.h"
13 #include "FEProblem.h"
14 #include "MooseObjectAction.h"
15 #include "ActionFactory.h"
16 #include "Output.h"
17 #include "OutputWarehouse.h"
18 #include "MooseUtils.h"
19 #include "PerfGraphReporter.h"
20 
21 // Extrnal includes
22 #include "tinydir.h"
23 #include "pcrecpp.h"
24 #include <libgen.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 registerMooseAction("MooseApp", CommonOutputAction, "common_output");
30 registerMooseAction("MooseApp", CommonOutputAction, "add_output");
31 registerMooseAction("MooseApp", CommonOutputAction, "add_reporter");
32 
35 {
37  params.addClassDescription("Adds short-cut syntax and common parameters to the Outputs block.");
38 
39  // Short-cut methods for typical output objects
40  params.addParam<bool>(
41  "exodus", false, "Output the results using the default settings for Exodus output.");
42  params.addParam<bool>(
43  "nemesis", false, "Output the results using the default settings for Nemesis output");
44  params.addParam<bool>(
45  "console", true, "Output the results using the default settings for Console output");
46  params.addParam<bool>("csv",
47  false,
48  "Output the scalar variable and postprocessors to a *.csv "
49  "file using the default CSV output.");
50  params.addParam<bool>("xml",
51  false,
52  "Output the vector postprocessors to a *.xml "
53  "file using the default XML output.");
54  params.addParam<bool>("json",
55  false,
56  "Output Reporter values to a *.json "
57  "file using the default JSON output.");
58  params.addParam<bool>(
59  "vtk", false, "Output the results using the default settings for VTKOutput output");
60  params.addParam<bool>(
61  "xda", false, "Output the results using the default settings for XDA/XDR output (ascii)");
62  params.addParam<bool>(
63  "xdr", false, "Output the results using the default settings for XDA/XDR output (binary)");
64  params.addParam<bool>(
65  "gmv", false, "Output the results using the default settings for GMV output");
66  params.addParam<bool>(
67  "tecplot", false, "Output the results using the default settings for Tecplot output");
68  params.addParam<bool>(
69  "gnuplot",
70  false,
71  "Output the scalar and postprocessor results using the default settings for GNUPlot output");
72  params.addParam<bool>(
73  "solution_history", false, "Print a solution history file (.slh) using the default settings");
74  params.addParam<bool>("progress", false, "Print a progress bar");
75  params.addParam<bool>("dofmap", false, "Create the dof map .json output file");
76  params.addParam<bool>("controls", false, "Enable the screen output of Control systems.");
77 
78  // Common parameters
79 
80  // Note: Be sure that objects that share these parameters utilize the same defaults
81  params.addParam<bool>("color", true, "Set to false to turn off all coloring in all outputs");
82  params.addParam<std::string>("file_base",
83  "Common file base name to be utilized with all output objects");
84  params.addParam<std::vector<std::string>>("output_if_base_contains",
85  "If this is supplied then output will only be done in "
86  "the case that the output base contains one of these "
87  "strings. This is helpful in outputting only a subset "
88  "of outputs when using MultiApps.");
89  params.addParam<unsigned int>(
90  "time_step_interval", 1, "The interval (number of time steps) at which output occurs");
91  params.addParam<std::vector<Real>>("sync_times",
92  std::vector<Real>(),
93  "Times at which the output and solution is forced to occur");
94  params.addParam<Real>(
95  "min_simulation_time_interval", 0.0, "The minimum simulation time between output steps");
96  params.addParam<bool>(
97  "append_date", false, "When true the date and time are appended to the output filename.");
98  params.addParam<std::string>("append_date_format",
99  "The format of the date/time to append (see "
100  "http://www.cplusplus.com/reference/ctime/"
101  "strftime).");
102 
103  params.addParam<std::vector<VariableName>>(
104  "hide",
105  {},
106  "A list of the variables and postprocessors that should NOT be output to the Exodus "
107  "file (may include Variables, ScalarVariables, and Postprocessor names).");
108  params.addParam<std::vector<VariableName>>(
109  "show",
110  {},
111  "A list of the variables and postprocessors that should be output to the Exodus file "
112  "(may include Variables, ScalarVariables, and Postprocessor names).");
113 
114  // Add the 'execute_on' input parameter
116  exec_enum = {EXEC_INITIAL, EXEC_TIMESTEP_END};
117  params.addParam<ExecFlagEnum>("execute_on", exec_enum, exec_enum.getDocString());
118 
119  // Add special Console flags
120  params.addDeprecatedParam<bool>(
121  "print_perf_log", false, "Use perf_graph instead!", "Use perf_graph instead!");
122 
123  params.addParam<bool>(
124  "perf_graph", false, "Enable printing of the performance graph to the screen (Console)");
125 
126  params.addParam<bool>("perf_graph_live", true, "Enables printing of live progress messages");
127  params.addParam<Real>(
128  "perf_graph_live_time_limit", 5.0, "Time (in seconds) to wait before printing a message.");
129  params.addParam<unsigned int>(
130  "perf_graph_live_mem_limit", 100, "Memory (in MB) to cause a message to be printed.");
131  params.addParam<bool>("perf_graph_json", false, "Output the perf graph in JSON");
132  params.addParam<FileName>("perf_graph_json_file", "Path to a .json file to store the perf graph");
133 
134  params.addParam<bool>("print_mesh_changed_info",
135  false,
136  "When true, each time the mesh is changed the mesh information is printed");
137  params.addParam<bool>("print_linear_residuals",
138  true,
139  "Enable printing of linear residuals to the screen (Console)");
140  params.addParam<bool>("print_nonlinear_residuals",
141  true,
142  "Enable printing of nonlinear residuals to the screen (Console)");
143  params.addParam<bool>("print_nonlinear_converged_reason",
144  true,
145  "Enable/disable printing of the nonlinear solver convergence reason to the "
146  "screen. This parameter only affects the output of the third-party solver "
147  "(e.g. PETSc), not MOOSE itself.");
148  params.addParam<bool>("print_linear_converged_reason",
149  true,
150  "Enable/disable printing of the linear solver convergence reason to the "
151  "screen. This parameter only affects the output of the third-party solver "
152  "(e.g. PETSc), not MOOSE itself.");
153 
154  params.addParam<bool>(
155  "solution_invalidity_history",
156  true,
157  "Enable printing of the time history of the solution invalidity occurrences "
158  "to the screen (console)");
159 
160  // Return object
161  return params;
162 }
163 
165  : Action(params), _action_params(_action_factory.getValidParams("AddOutputAction"))
166 {
167 }
168 
169 void
171 {
172  // Name of the PerfGraphReporter that could be created for the perf graph output
173  static const std::string perf_graph_reporter_name = "perf_graph_json";
174  // Name of the JSON output that could be created for the perf graph output
175  static const OutputName perf_graph_json_output_name = "auto_perf_graph_json";
176 
177  if (_current_task == "common_output")
178  {
179  // Store the common output parameters in the OutputWarehouse
181 
182 // Create the actions for the short-cut methods
183 #ifdef LIBMESH_HAVE_EXODUS_API
184  if (getParam<bool>("exodus"))
185  create("Exodus", "exodus");
186 #else
187  if (getParam<bool>("exodus"))
188  mooseWarning("Exodus output requested but not enabled through libMesh");
189 #endif
190 
191 #ifdef LIBMESH_HAVE_NEMESIS_API
192  if (getParam<bool>("nemesis"))
193  create("Nemesis", "nemesis");
194 #else
195  if (getParam<bool>("nemesis"))
196  mooseWarning("Nemesis output requested but not enabled through libMesh");
197 #endif
198 
199  // Only create a Console if screen output was not created
200  if (getParam<bool>("console") && !hasConsole())
201  create("Console", "console");
202 
203  if (getParam<bool>("csv"))
204  create("CSV", "csv");
205 
206  if (getParam<bool>("xml"))
207  create("XMLOutput", "xml");
208 
209  if (getParam<bool>("json"))
210  create("JSON", "json");
211 
212 #ifdef LIBMESH_HAVE_VTK
213  if (getParam<bool>("vtk"))
214  create("VTK", "vtk");
215 #else
216  if (getParam<bool>("vtk"))
217  mooseWarning("VTK output requested but not enabled through libMesh");
218 #endif
219 
220  if (getParam<bool>("xda"))
221  create("XDA", "xda");
222 
223  if (getParam<bool>("xdr"))
224  create("XDR", "xdr");
225 
226  if (getParam<bool>("gmv"))
227  create("GMV", "gmv");
228 
229  if (getParam<bool>("tecplot"))
230  create("Tecplot", "tecplot");
231 
232  if (getParam<bool>("gnuplot"))
233  create("Gnuplot", "gnuplot");
234 
235  if (getParam<bool>("solution_history"))
236  create("SolutionHistory", "solution_history");
237 
238  if (getParam<bool>("progress"))
239  create("Progress", "progress");
240 
241  if (getParam<bool>("dofmap"))
242  create("DOFMap", "dofmap");
243 
244  // Helper for looking through a pair of [parameters, param name]
245  // to find a true boolean parameter, returning the pair that was found
246  const auto find_param =
247  [](const std::vector<std::pair<const InputParameters *, std::string>> & options)
248  {
249  std::optional<std::string> from_param_name;
250  const InputParameters * from_params = nullptr;
251  for (const auto & [params, param_name] : options)
252  if (params->template get<bool>(param_name))
253  {
254  from_param_name = param_name;
255  from_params = params;
256  break;
257  }
258  return std::make_pair(from_param_name, from_params);
259  };
260 
261  {
262  const auto [from_param_name, from_params] =
263  find_param({{&parameters(), "controls"}, {&_app.parameters(), "show_controls"}});
264  if (from_param_name)
265  create("ControlOutput", *from_param_name, from_params);
266  }
267 
268  if (!_app.getParam<bool>("no_timing"))
269  {
270  auto [from_param_name, from_params] = find_param({{&parameters(), "perf_graph"},
271  {&parameters(), "print_perf_log"},
272  {&_app.parameters(), "timing"}});
273  if (from_param_name)
274  create("PerfGraphOutput", *from_param_name, from_params);
275 
276  const auto add_perf_graph_json = [this](const std::string & from_param_name,
277  const std::string & set_param_name,
278  const std::string & set_param_value)
279  {
280  const auto & value_names = PerfGraphReporter::value_names;
281  std::vector<ReporterName> reporters;
282  reporters.reserve(value_names.size());
283  for (const auto & value_name : value_names)
284  {
285  const ReporterName name(perf_graph_reporter_name, value_name);
286 
287  // To avoid this reporter value appearing in all other JSON output
288  _common_reporter_names.push_back(name);
289 
290  reporters.push_back(name);
291  }
292 
293  auto params = _factory.getValidParams("JSON");
294  params.set<std::vector<ReporterName>>("reporters") = reporters;
295  params.set<ExecFlagEnum>("execute_on") = {EXEC_FINAL};
296  params.set<ExecFlagEnum>("execute_system_information_on") = {EXEC_NONE};
297  params.set<std::string>(set_param_name) = set_param_value;
298  params.set<bool>("distributed") = false;
299  if (set_param_name == "file_base")
300  params.set<bool>("append_date") = false;
301  create("JSON", from_param_name, &parameters(), &params, perf_graph_json_output_name);
302  };
303 
304  if (getParam<bool>("perf_graph_json"))
305  add_perf_graph_json("perf_graph_json", "file_base_suffix", "perf_graph");
306  if (isParamValid("perf_graph_json_file"))
307  {
308  const auto & file = getParam<FileName>("perf_graph_json_file");
309  if (MooseUtils::getExtension(file, true) != "json")
310  paramError("perf_graph_json_file", "File must end with .json");
311  const auto file_stripped = MooseUtils::stripExtension(file, true);
312  add_perf_graph_json("perf_graph_json_file", "file_base", file_stripped);
313  }
314 
315  if (!getParam<bool>("perf_graph_live"))
316  {
317  if (!from_param_name)
318  perfGraph().setActive(false);
320  }
321  }
322  else
323  {
324  perfGraph().setActive(false);
326  }
327 
328  perfGraph().setLiveTimeLimit(getParam<Real>("perf_graph_live_time_limit"));
329  perfGraph().setLiveMemoryLimit(getParam<unsigned int>("perf_graph_live_mem_limit"));
330 
331  if (getParam<bool>("solution_invalidity_history"))
332  {
333  create("SolutionInvalidityOutput", "solution_invalidity_history");
334  }
335 
336  if (!getParam<bool>("color"))
337  Moose::setColorConsole(false);
338  }
339  else if (_current_task == "add_output")
340  {
341  // More like remove output. This can't be done during the "common_output" task because the
342  // finite element problem is not yet constructed
343 
344  if (!getParam<bool>("console") || (isParamValid("print_nonlinear_converged_reason") &&
345  !getParam<bool>("print_nonlinear_converged_reason")))
347 
348  if (!getParam<bool>("console") || (isParamValid("print_linear_converged_reason") &&
349  !getParam<bool>("print_linear_converged_reason")))
351  }
352  else if (_current_task == "add_reporter")
353  {
354  if (!_app.getParam<bool>("no_timing") &&
355  (getParam<bool>("perf_graph_json") || isParamValid("perf_graph_json_file")))
356  {
357  auto params = _factory.getValidParams("PerfGraphReporter");
358  params.set<ExecFlagEnum>("execute_on") = EXEC_FINAL;
359  params.set<std::vector<OutputName>>("outputs") =
360  std::vector<OutputName>{perf_graph_json_output_name};
361  _problem->addReporter("PerfGraphReporter", perf_graph_reporter_name, params);
362  }
363  }
364  else
365  mooseError("unrecognized task ", _current_task, " in CommonOutputAction.");
366 }
367 
368 void
369 CommonOutputAction::create(const std::string & object_type,
370  const std::optional<std::string> & param_name,
371  const InputParameters * const from_params /* = nullptr */,
372  const InputParameters * const apply_params /* = nullptr */,
373  const std::optional<std::string> & object_name /* = {} */)
374 {
375  // Create our copy of the parameters
376  auto params = _action_params;
377 
378  // Set the 'type =' parameters for the desired object
379  params.set<std::string>("type") = object_type;
380 
381  // Create the complete object name (uses lower case of type)
382  std::string name;
383  if (object_name)
384  name = *object_name;
385  else
386  {
387  name = object_type;
388  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
389  }
390 
391  // Create the action
392  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
393  _action_factory.create("AddOutputAction", name, params));
394  auto & object_params = action->getObjectParams();
395 
396  // Set flag indicating that the object to be created was created with short-cut syntax
397  object_params.set<bool>("_built_by_moose") = true;
398 
399  // Apply any additional parameters
400  if (apply_params)
401  object_params.applyParameters(*apply_params);
402 
403  // Associate all action output object errors with param_name
404  // If from_params is specified, it means to associate it with parameters other than parameters()
405  if (param_name)
406  {
407  const InputParameters & associated_params = from_params ? *from_params : parameters();
408  associateWithParameter(associated_params, *param_name, object_params);
409  }
410 
411  // Add the action to the warehouse
412  _awh.addActionBlock(action);
413 }
414 
415 bool
417 {
418 
419  // Loop through all of the actions for adding output objects
420  for (ActionIterator it = _awh.actionBlocksWithActionBegin("add_output");
421  it != _awh.actionBlocksWithActionEnd("add_output");
422  it++)
423  {
424  MooseObjectAction * moa = dynamic_cast<MooseObjectAction *>(*it);
425  if (!moa)
426  continue;
427 
428  const std::string & type = moa->getMooseObjectType();
429  InputParameters & params = moa->getObjectParams();
430  if (type.compare("Console") == 0 && params.get<bool>("output_screen"))
431  return true;
432  }
433 
434  return false;
435 }
static const std::vector< ReporterValueName > value_names
The reporter values that this object declares; used within the CommonOutputAction to single out value...
InputParameters _action_params
Parameters from the action being created (AddOutputAction)
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:394
void create(const std::string &object_type, const std::optional< std::string > &param_name, const InputParameters *const from_params=nullptr, const InputParameters *const apply_params=nullptr, const std::optional< std::string > &object_name={})
Helper method for creating the short-cut actions.
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
std::list< Action * >::iterator ActionIterator
alias to hide implementation details
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:169
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:467
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
bool hasConsole()
Check if a Console object that outputs to the screen has been defined.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
Factory & _factory
The Factory associated with the MooseApp.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
ActionFactory & _action_factory
Builds Actions.
const ExecFlagType EXEC_NONE
Definition: Moose.C:29
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
static ExecFlagEnum getDefaultExecFlagEnum()
Return an ExecFlagEnum object with the available execution flags for Output objects.
Definition: Output.C:104
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
CommonOutputAction(const InputParameters &params)
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:36
Base class for actions.
Definition: Action.h:34
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters &parameters)
Definition: ActionFactory.C:40
static InputParameters validParams()
void mooseWarning(Args &&... args) const
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
static InputParameters validParams()
Definition: Action.C:26
InputParameters & getObjectParams()
Retrieve the parameters of the object to be created by this action.
std::vector< ReporterName > _common_reporter_names
The reporter names that we do not want to include in general JSON output.
std::string stripExtension(const std::string &s, const bool rfind)
Definition: MooseUtils.C:427
void setActive(bool active)
Turn on or off timing.
Definition: PerfGraph.h:129
void setLiveMemoryLimit(unsigned int mem_limit)
Sert the memory limit before a message prints.
Definition: PerfGraph.h:157
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
void disableLivePrint()
Completely disables Live Print (cannot be restarted)
Definition: PerfGraph.C:68
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
const std::string & _current_task
The current action (even though we have separate instances for each action)
Definition: Action.h:172
std::string getDocString() const
Generate a documentation string for the "execute_on" parameter.
Definition: ExecFlagEnum.C:40
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:385
const std::string & getMooseObjectType() const
Return the object type to be created.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void dontAddNonlinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -snes_converged_reason is not added to the PetscOptions storage object to be ...
registerMooseAction("MooseApp", CommonOutputAction, "common_output")
bool setColorConsole(bool use_color, bool force=false)
Turns color escape sequences on/off for info written to stdout.
Definition: Moose.C:800
PerfGraph & perfGraph()
Get the PerfGraph.
void associateWithParameter(const std::string &param_name, InputParameters &params) const
Associates the object&#39;s parameters params with the input location from this Action&#39;s parameter with t...
Definition: Action.C:153
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
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...
void setCommonParameters(const InputParameters *params_ptr)
Stores the common InputParameters object.
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:178
ActionIterator actionBlocksWithActionEnd(const std::string &task)
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...
std::string getExtension(const std::string &filename, const bool rfind)
Definition: MooseUtils.C:411
void dontAddLinearConvergedReason(FEProblemBase &fe_problem)
Function to ensure that -ksp_converged_reason is not added to the PetscOptions storage object to be l...
void setLiveTimeLimit(Real time_limit)
Set the time limit before a message prints.
Definition: PerfGraph.h:149
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:209
ActionIterator actionBlocksWithActionBegin(const std::string &task)
Iterators to the Actions in the warehouse.
const ExecFlagType EXEC_FINAL
Definition: Moose.C:46
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
Meta-action for creating common output object parameters This action serves two purpose, first it adds common output object parameters.
OutputWarehouse & getOutputWarehouse()
Get the OutputWarehouse objects.
Definition: MooseApp.C:2136
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30