https://mooseframework.inl.gov
Output.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 // Standard includes
11 #include <cmath>
12 #include <limits>
13 
14 // MOOSE includes
15 #include "Output.h"
16 #include "FEProblem.h"
17 #include "DisplacedProblem.h"
18 #include "MooseApp.h"
19 #include "Postprocessor.h"
20 #include "Restartable.h"
21 #include "FileMesh.h"
22 #include "MooseUtils.h"
23 #include "MooseApp.h"
24 #include "Console.h"
25 #include "Function.h"
26 #include "PiecewiseLinear.h"
27 #include "Times.h"
28 
29 #include "libmesh/equation_systems.h"
30 
33 {
34  // Get the parameters from the parent object
36  params += SetupInterface::validParams();
37 
38  // Displaced Mesh options
39  params.addParam<bool>(
40  "use_displaced", false, "Enable/disable the use of the displaced mesh for outputting");
41 
42  // Output intervals and timing
43  params.addRangeCheckedParam<unsigned int>(
44  "time_step_interval",
45  1,
46  "time_step_interval > 0",
47  "The interval (number of time steps) at which output occurs. "
48  "Unless explicitly set, the default value of this parameter is set "
49  "to infinity if the wall_time_interval is explicitly set.");
50  params.addParam<Real>(
51  "min_simulation_time_interval", 0.0, "The minimum simulation time between output steps");
52  params.addParam<Real>("simulation_time_interval",
54  "The target simulation time interval (in seconds) at which to output");
55  params.addRangeCheckedParam<Real>(
56  "wall_time_interval",
58  "wall_time_interval > 0",
59  "The target wall time interval (in seconds) at which to output");
60  params.addParam<std::vector<Real>>(
61  "sync_times", {}, "Times at which the output and solution is forced to occur");
62  params.addParam<TimesName>(
63  "sync_times_object",
64  "Times object providing the times at which the output and solution is forced to occur");
65  params.addParam<bool>("sync_only", false, "Only export results at sync times");
66  params.addParam<Real>("start_time", "Time at which this output object begins to operate");
67  params.addParam<Real>("end_time", "Time at which this output object stop operating");
68  params.addParam<int>("start_step", "Time step at which this output object begins to operate");
69  params.addParam<int>("end_step", "Time step at which this output object stop operating");
70  params.addParam<Real>(
71  "time_tolerance", 1e-14, "Time tolerance utilized checking start and end times");
72  params.addDeprecatedParam<FunctionName>(
73  "output_limiting_function",
74  "Piecewise base function that sets sync_times",
75  "Replaced by using the Times system with the sync_times_objects parameter");
76 
77  // Update the 'execute_on' input parameter for output
78  ExecFlagEnum & exec_enum = params.set<ExecFlagEnum>("execute_on", true);
79  exec_enum = Output::getDefaultExecFlagEnum();
80  exec_enum = {EXEC_INITIAL, EXEC_TIMESTEP_END};
81  params.setDocString("execute_on", exec_enum.getDocString());
82 
83  // Add ability to append to the 'execute_on' list
84  params.addParam<ExecFlagEnum>("additional_execute_on", exec_enum, exec_enum.getDocString());
85  params.set<ExecFlagEnum>("additional_execute_on").clearSetValues();
86  params.addParamNamesToGroup("execute_on additional_execute_on", "Execution scheduling");
87 
88  // 'Timing' group
89  params.addParamNamesToGroup("time_tolerance time_step_interval sync_times sync_times_object "
90  "sync_only start_time end_time "
91  "start_step end_step min_simulation_time_interval "
92  "simulation_time_interval wall_time_interval",
93  "Timing and frequency of output");
94 
95  // Add a private parameter for indicating if it was created with short-cut syntax
96  params.addPrivateParam<bool>("_built_by_moose", false);
97 
98  // Register this class as base class
99  params.declareControllable("enable");
100  params.registerBase("Output");
101 
102  return params;
103 }
104 
107 {
109  exec_enum.addAvailableFlags(EXEC_FAILED);
110  return exec_enum;
111 }
112 
113 Output::Output(const InputParameters & parameters)
114  : MooseObject(parameters),
115  Restartable(this, "Output"),
116  MeshChangedInterface(parameters),
117  SetupInterface(this),
118  FunctionInterface(this),
121  ReporterInterface(this),
122  PerfGraphInterface(this),
123  _problem_ptr(getParam<FEProblemBase *>("_fe_problem_base")),
124  _transient(_problem_ptr->isTransient()),
125  _use_displaced(getParam<bool>("use_displaced")),
126  _es_ptr(nullptr),
127  _mesh_ptr(nullptr),
128  _execute_on(getParam<ExecFlagEnum>("execute_on")),
129  _current_execute_flag(EXEC_NONE),
130  _time(_problem_ptr->time()),
131  _time_old(_problem_ptr->timeOld()),
132  _t_step(_problem_ptr->timeStep()),
133  _dt(_problem_ptr->dt()),
134  _dt_old(_problem_ptr->dtOld()),
135  _num(0),
136  _time_step_interval_set_by_addparam(parameters.isParamSetByAddParam("time_step_interval")),
137  // If wall_time_interval is user-specified and time_step_interval is not,
138  // override default value of time_step_interval so output does not occur
139  // after every time step.
140  _time_step_interval(
141  (parameters.isParamSetByUser("wall_time_interval") && _time_step_interval_set_by_addparam)
142  ? std::numeric_limits<unsigned int>::max()
143  : getParam<unsigned int>("time_step_interval")),
144  _min_simulation_time_interval(getParam<Real>("min_simulation_time_interval")),
145  _simulation_time_interval(getParam<Real>("simulation_time_interval")),
146  _wall_time_interval(getParam<Real>("wall_time_interval")),
147  _sync_times(std::set<Real>(getParam<std::vector<Real>>("sync_times").begin(),
148  getParam<std::vector<Real>>("sync_times").end())),
149  _sync_times_object(isParamValid("sync_times_object")
150  ? static_cast<Times *>(&_problem_ptr->getUserObject<Times>(
151  getParam<TimesName>("sync_times_object")))
152  : nullptr),
153  _start_time(isParamValid("start_time") ? getParam<Real>("start_time")
154  : std::numeric_limits<Real>::lowest()),
155  _end_time(isParamValid("end_time") ? getParam<Real>("end_time")
156  : std::numeric_limits<Real>::max()),
157  _start_step(isParamValid("start_step") ? getParam<int>("start_step")
158  : std::numeric_limits<int>::lowest()),
159  _end_step(isParamValid("end_step") ? getParam<int>("end_step")
160  : std::numeric_limits<int>::max()),
161  _t_tol(getParam<Real>("time_tolerance")),
162  _sync_only(getParam<bool>("sync_only")),
163  _allow_output(true),
164  _is_advanced(false),
165  _advanced_execute_on(_execute_on, parameters),
166  _last_output_simulation_time(declareRestartableData<Real>("last_output_simulation_time",
167  std::numeric_limits<Real>::lowest())),
168  _last_output_wall_time(std::chrono::steady_clock::now())
169 {
170  if (_use_displaced)
171  {
172  std::shared_ptr<DisplacedProblem> dp = _problem_ptr->getDisplacedProblem();
173  if (dp != nullptr)
174  {
175  _es_ptr = &dp->es();
176  _mesh_ptr = &dp->mesh();
177  }
178  else
179  {
180  mooseWarning(
181  name(),
182  ": Parameter 'use_displaced' ignored, there is no displaced problem in your simulation.");
183  _es_ptr = &_problem_ptr->es();
185  }
186  }
187  else
188  {
189  _es_ptr = &_problem_ptr->es();
191  }
192 
193  // Apply the additional output flags
194  if (isParamValid("additional_execute_on"))
195  {
196  const ExecFlagEnum & add = getParam<ExecFlagEnum>("additional_execute_on");
197  for (auto & me : add)
199  }
200 
201  if (isParamValid("output_limiting_function"))
202  {
203  const Function & olf = getFunction("output_limiting_function");
204  const PiecewiseBase * pwb_olf = dynamic_cast<const PiecewiseBase *>(&olf);
205  if (pwb_olf == nullptr)
206  mooseError("Function muse have a piecewise base!");
207 
208  for (auto i = 0; i < pwb_olf->functionSize(); i++)
209  _sync_times.insert(pwb_olf->domain(i));
210  }
211 
212  // Get sync times from Times object if using
213  if (_sync_times_object)
214  {
215  if (isParamValid("output_limiting_function") || isParamSetByUser("sync_times"))
216  paramError("sync_times_object",
217  "Only one method of specifying sync times is supported at a time");
218  else
219  // Sync times for the time steppers are taken from the output warehouse. The output warehouse
220  // takes sync times from the output objects immediately after the object is constructed. Hence
221  // we must ensure that we set the `_sync_times` in the constructor
223  }
224 }
225 
226 void
228 {
229 }
230 
231 void
233 {
234  // Output is not allowed
235  if (!_allow_output && type != EXEC_FORCED)
236  return;
237 
238  // If recovering disable output of initial condition, it was already output
239  if (type == EXEC_INITIAL && _app.isRecovering())
240  return;
241 
242  // Return if the current output is not on the desired interval and there is
243  // no signal to process
244  const bool on_interval_or_exec_final = (onInterval() || (type == EXEC_FINAL));
245  // Sync across processes and only output one time per signal received.
247  const bool signal_received = Moose::interrupt_signal_number;
248  if (!(on_interval_or_exec_final || signal_received))
249  return;
250 
251  // set current type
253 
254  // Check whether we should output, then do it.
255  if (shouldOutput())
256  {
257  // store current simulation time
259 
260  // store current wall time of output
261  _last_output_wall_time = std::chrono::steady_clock::now();
262 
263  TIME_SECTION("outputStep", 2, "Outputting Step");
264  output();
265  }
266 
268 }
269 
270 bool
272 {
274  return true;
275  return false;
276 }
277 
278 bool
280 {
281  // The output flag to return
282  bool output = false;
283 
284  // Return true if the current step on the current output interval and within the output time range
285  // and within the output step range
286  if (_time >= _start_time && _time <= _end_time && _t_step >= _start_step &&
288  output = true;
289 
290  // Return false if 'sync_only' is set to true
291  if (_sync_only)
292  output = false;
293 
294  if (_sync_times_object)
295  {
296  const auto & sync_times = _sync_times_object->getUniqueTimes();
297  if (sync_times != _sync_times)
298  mooseError("The provided sync times object has changing time values. Only static time "
299  "values are supported since time steppers take sync times from the output "
300  "warehouse which determines its sync times at output construction time.");
301  }
302 
303  // If sync times are not skipped, return true if the current time is a sync_time
304  for (const auto _sync_time : _sync_times)
305  {
306  if (std::abs(_sync_time - _time) < _t_tol)
307  output = true;
308  }
309 
310  // check if enough simulation time has passed between outputs
313  output = false;
314 
315  // check if enough wall time has passed between outputs
316  const auto now = std::chrono::steady_clock::now();
317  // count below returns an interger type, so lets express on a millisecond
318  // scale and convert to seconds for finer resolution
320  std::chrono::duration_cast<std::chrono::milliseconds>(now - _last_output_wall_time).count() /
321  1000.0;
322  // Take the maximum wall time since last output accross all processors
325  output = true;
326 
327  // Return the output status
328  return output;
329 }
330 
331 void
333 {
334  if (_app.isParamValid("output_wall_time_interval"))
335  {
336  _wall_time_interval = _app.getParam<Real>("output_wall_time_interval");
337 
338  // If default value of _wall_time_interval was just overriden and user did not
339  // explicitly specify _time_step_interval, override default value of
340  // _time_step_interval so output does not occur after every time step
343  }
344 }
345 
346 Real
348 {
349  if (_transient)
350  return _time;
351  else
352  return _t_step;
353 }
354 
355 Real
357 {
358  if (_transient)
359  return _time_old;
360  else
361  return _t_step - 1;
362 }
363 
364 Real
366 {
367  if (_transient)
368  return _dt;
369  else
370  return 1;
371 }
372 
373 Real
375 {
376  if (_transient)
377  return _dt_old;
378  else
379  return 1;
380 }
381 
382 int
384 {
385  return _t_step;
386 }
387 
388 const MultiMooseEnum &
390 {
391  return _execute_on;
392 }
393 
394 bool
396 {
397  return _is_advanced;
398 }
399 
400 const OutputOnWarehouse &
402 {
403  mooseError("The output object ", name(), " is not an AdvancedOutput, use isAdvanced() to check.");
404  return _advanced_execute_on;
405 }
Real & _time_old
The old time.
Definition: Output.h:217
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
const MultiMooseEnum & executeOn() const
Get the current &#39;execute_on&#39; selections for display.
Definition: Output.C:389
const ExecFlagType EXEC_FAILED
Definition: Moose.C:48
Function base which provides a piecewise approximation to a specified (x,y) point data set...
Definition: PiecewiseBase.h:20
Base class for function objects.
Definition: Function.h:36
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
virtual Real dtOld()
Get old time step size.
Definition: Output.C:374
A class for creating restricted objects.
Definition: Restartable.h:28
ExecFlagEnum _execute_on
The common Execution types; this is used as the default execution type for everything except system i...
Definition: Output.h:203
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
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:439
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:388
const ExecFlagType EXEC_FORCED
Definition: Moose.C:47
OutputOnWarehouse _advanced_execute_on
Storage for the individual component execute flags.
Definition: Output.h:280
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:279
void setDocString(const std::string &name, const std::string &doc)
Set the doc string of a parameter.
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
void setAdditionalValue(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
virtual Real dt()
Get the current time step size.
Definition: Output.C:365
Real & _dt_old
Old time step delta.
Definition: Output.h:226
const Function & getFunction(const std::string &name) const
Get a function with a given name.
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.
Real _start_time
Start outputting time.
Definition: Output.h:253
static ExecFlagEnum getDefaultExecFlagEnum()
Return an ExecFlagEnum object with the available execution flags for Output objects.
Definition: Output.C:106
Times objects are under the hood Reporters, but limited to a vector of Real.
Definition: Times.h:18
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
const std::set< Real > getUniqueTimes() const
Getter for a set of the full times.
Definition: Times.h:29
void addAvailableFlags(const ExecFlagType &flag, Args... flags)
Add additional execute_on flags to the list of possible flags.
Definition: ExecFlagEnum.h:82
int & _t_step
The current time step.
Definition: Output.h:220
bool _transient
Transient flag (true = transient)
Definition: Output.h:188
const Parallel::Communicator & _communicator
virtual Real domain(const int i) const
Definition: PiecewiseBase.C:28
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:36
Real _wall_time_since_last_output
time in seconds since last output
Definition: Output.h:289
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
auto max(const L &left, const R &right)
virtual void solveSetup()
A method called just prior to the solve, this is used by PetscOutput to perform the necessary setup a...
Definition: Output.C:227
Output(const InputParameters &parameters)
Class constructor.
Definition: Output.C:113
ExecFlagEnum getDefaultExecFlagEnum()
Return the default ExecFlagEnum for MOOSE.
Definition: MooseUtils.C:972
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
Definition: Output.C:271
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System...
int _start_step
Start outputting at this time step.
Definition: Output.h:259
void mooseWarning(Args &&... args) const
Real & _last_output_simulation_time
last simulation time an output has occured
Definition: Output.h:283
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
virtual Real timeOld()
Get the old output time.
Definition: Output.C:356
Interface for notifications that the mesh has changed.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const bool _time_step_interval_set_by_addparam
Whether time step interval is set by AddParam.
Definition: Output.h:232
ExecFlagType _current_execute_flag
Current execute on flag.
Definition: Output.h:211
virtual void outputStep(const ExecFlagType &type)
A single call to this function should output all the necessary data for a single timestep.
Definition: Output.C:232
virtual Real functionSize() const
Definition: PiecewiseBase.C:22
Interface to allow object to consume Reporter values.
int _end_step
End outputting at this time step.
Definition: Output.h:262
virtual libMesh::EquationSystems & es() override
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:185
bool _allow_output
Flag for disabling output.
Definition: Output.h:271
virtual void output()=0
Overload this function with the desired output activities.
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
A helper warehouse class for storing the "execute_on" settings for the various output types...
std::string getDocString() const
Generate a documentation string for the "execute_on" parameter.
Definition: ExecFlagEnum.C:40
const Times *const _sync_times_object
Sync times object for this outputter.
Definition: Output.h:250
bool _sync_only
Flag for only executing at sync times.
Definition: Output.h:268
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:357
Real _wall_time_interval
Target wall time between outputs in seconds.
Definition: Output.h:244
bool isValueSet(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
static InputParameters validParams()
Interface for objects interacting with the PerfGraph.
unsigned int _time_step_interval
The output time step interval.
Definition: Output.h:235
bool _use_displaced
Flag for using displaced mesh.
Definition: Output.h:191
virtual Real time()
Get the output time.
Definition: Output.C:347
bool _is_advanced
Flag for advanced output testing.
Definition: Output.h:274
std::set< Real > _sync_times
Sync times for this outputter.
Definition: Output.h:247
void setWallTimeIntervalFromCommandLineParam()
Function to set the wall time interval based on value of command line parameter (used for testing onl...
Definition: Output.C:332
bool isAdvanced()
Returns true if this object is an AdvancedOutput object.
Definition: Output.C:395
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual std::shared_ptr< const DisplacedProblem > getDisplacedProblem() const
std::chrono::time_point< std::chrono::steady_clock > _last_output_wall_time
last wall time an output has occured
Definition: Output.h:286
Real _t_tol
Time checking tolerance.
Definition: Output.h:265
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
void max(const T &r, T &o, Request &req) const
libMesh::EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:194
virtual MooseMesh & mesh() override
MooseMesh * _mesh_ptr
A convenience pointer to the current mesh (reference or displaced depending on "use_displaced") ...
Definition: Output.h:197
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:271
int interrupt_signal_number
Used by the signal handler to determine if we should write a checkpoint file out at any point during ...
Definition: Moose.C:822
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 addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:199
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
static InputParameters validParams()
Definition: MooseObject.C:25
virtual const OutputOnWarehouse & advancedExecuteOn() const
Returns the advanced &#39;execute_on&#39; settings.
Definition: Output.C:401
virtual int timeStep()
Get the current time step.
Definition: Output.C:383
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:1874
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition: MooseBase.h:205
const ExecFlagType EXEC_FINAL
Definition: Moose.C:46
static InputParameters validParams()
Definition: Output.C:32
Interface for objects that need to use functions.
void declareControllable(const std::string &name, std::set< ExecFlagType > execute_flags={})
Declare the given parameters as controllable.
Real & _time
The current time for output purposes.
Definition: Output.h:214
void ErrorVector unsigned int
Real & _dt
Time step delta.
Definition: Output.h:223
const Real _min_simulation_time_interval
Minimum simulation time between outputs.
Definition: Output.h:238
Interface class for classes which interact with Postprocessors.
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...
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30