www.mooseframework.org
Output.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 // 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.addDeprecatedParam<unsigned int>(
51  "interval",
52  "The interval (number of time steps) at which output occurs",
53  "Deprecated, use time_step_interval");
54  params.deprecateParam("interval", "time_step_interval", "02/01/2025");
55  params.addParam<Real>(
56  "min_simulation_time_interval", 0.0, "The minimum simulation time between output steps");
57  params.addDeprecatedParam<Real>("minimum_time_interval",
58  "The minimum simulation time between output steps",
59  "Deprecated, use min_simulation_time_interval");
60  params.deprecateParam("minimum_time_interval", "min_simulation_time_interval", "02/01/2025");
61  params.addParam<Real>("simulation_time_interval",
63  "The target simulation time interval (in seconds) at which to output");
64  params.addParam<Real>("wall_time_interval",
66  "The target wall time interval (in seconds) at which to output");
67  params.addParam<std::vector<Real>>(
68  "sync_times", {}, "Times at which the output and solution is forced to occur");
69  params.addParam<TimesName>(
70  "sync_times_object",
71  "Times object providing the times at which the output and solution is forced to occur");
72  params.addParam<bool>("sync_only", false, "Only export results at sync times");
73  params.addParam<Real>("start_time", "Time at which this output object begins to operate");
74  params.addParam<Real>("end_time", "Time at which this output object stop operating");
75  params.addParam<int>("start_step", "Time step at which this output object begins to operate");
76  params.addParam<int>("end_step", "Time step at which this output object stop operating");
77  params.addParam<Real>(
78  "time_tolerance", 1e-14, "Time tolerance utilized checking start and end times");
79  params.addDeprecatedParam<FunctionName>(
80  "output_limiting_function",
81  "Piecewise base function that sets sync_times",
82  "Replaced by using the Times system with the sync_times_objects parameter");
83 
84  // Update the 'execute_on' input parameter for output
85  ExecFlagEnum & exec_enum = params.set<ExecFlagEnum>("execute_on", true);
86  exec_enum = Output::getDefaultExecFlagEnum();
87  exec_enum = {EXEC_INITIAL, EXEC_TIMESTEP_END};
88  params.setDocString("execute_on", exec_enum.getDocString());
89 
90  // Add ability to append to the 'execute_on' list
91  params.addParam<ExecFlagEnum>("additional_execute_on", exec_enum, exec_enum.getDocString());
92  params.set<ExecFlagEnum>("additional_execute_on").clear();
93  params.addParamNamesToGroup("execute_on additional_execute_on", "Execution scheduling");
94 
95  // 'Timing' group
96  params.addParamNamesToGroup("time_tolerance time_step_interval sync_times sync_times_object "
97  "sync_only start_time end_time "
98  "start_step end_step min_simulation_time_interval "
99  "simulation_time_interval wall_time_interval",
100  "Timing and frequency of output");
101 
102  // Add a private parameter for indicating if it was created with short-cut syntax
103  params.addPrivateParam<bool>("_built_by_moose", false);
104 
105  // Register this class as base class
106  params.declareControllable("enable");
107  params.registerBase("Output");
108 
109  return params;
110 }
111 
114 {
116  exec_enum.addAvailableFlags(EXEC_FAILED);
117  return exec_enum;
118 }
119 
120 Output::Output(const InputParameters & parameters)
121  : MooseObject(parameters),
122  Restartable(this, "Output"),
123  MeshChangedInterface(parameters),
124  SetupInterface(this),
125  FunctionInterface(this),
128  ReporterInterface(this),
129  PerfGraphInterface(this),
130  _problem_ptr(getParam<FEProblemBase *>("_fe_problem_base")),
131  _transient(_problem_ptr->isTransient()),
132  _use_displaced(getParam<bool>("use_displaced")),
133  _es_ptr(nullptr),
134  _mesh_ptr(nullptr),
135  _execute_on(getParam<ExecFlagEnum>("execute_on")),
136  _current_execute_flag(EXEC_NONE),
137  _time(_problem_ptr->time()),
138  _time_old(_problem_ptr->timeOld()),
139  _t_step(_problem_ptr->timeStep()),
140  _dt(_problem_ptr->dt()),
141  _dt_old(_problem_ptr->dtOld()),
142  _num(0),
143  _time_step_interval_set_by_addparam(parameters.isParamSetByAddParam("time_step_interval")),
144  // If wall_time_interval is user-specified and time_step_interval is not,
145  // override default value of time_step_interval so output does not occur
146  // after every time step.
147  _time_step_interval(
148  (parameters.isParamSetByUser("wall_time_interval") && _time_step_interval_set_by_addparam)
149  ? std::numeric_limits<unsigned int>::max()
150  : getParam<unsigned int>("time_step_interval")),
151  _min_simulation_time_interval(getParam<Real>("min_simulation_time_interval")),
152  _simulation_time_interval(getParam<Real>("simulation_time_interval")),
153  _wall_time_interval(getParam<Real>("wall_time_interval")),
154  _sync_times(std::set<Real>(getParam<std::vector<Real>>("sync_times").begin(),
155  getParam<std::vector<Real>>("sync_times").end())),
156  _sync_times_object(isParamValid("sync_times_object")
157  ? static_cast<Times *>(&_problem_ptr->getUserObject<Times>(
158  getParam<TimesName>("sync_times_object")))
159  : nullptr),
160  _start_time(isParamValid("start_time") ? getParam<Real>("start_time")
161  : std::numeric_limits<Real>::lowest()),
162  _end_time(isParamValid("end_time") ? getParam<Real>("end_time")
163  : std::numeric_limits<Real>::max()),
164  _start_step(isParamValid("start_step") ? getParam<int>("start_step")
165  : std::numeric_limits<int>::lowest()),
166  _end_step(isParamValid("end_step") ? getParam<int>("end_step")
167  : std::numeric_limits<int>::max()),
168  _t_tol(getParam<Real>("time_tolerance")),
169  _sync_only(getParam<bool>("sync_only")),
170  _allow_output(true),
171  _is_advanced(false),
172  _advanced_execute_on(_execute_on, parameters),
173  _last_output_simulation_time(declareRestartableData<Real>("last_output_simulation_time",
174  std::numeric_limits<Real>::lowest())),
175  _last_output_wall_time(std::chrono::steady_clock::now())
176 {
177  if (_use_displaced)
178  {
179  std::shared_ptr<DisplacedProblem> dp = _problem_ptr->getDisplacedProblem();
180  if (dp != nullptr)
181  {
182  _es_ptr = &dp->es();
183  _mesh_ptr = &dp->mesh();
184  }
185  else
186  {
187  mooseWarning(
188  name(),
189  ": Parameter 'use_displaced' ignored, there is no displaced problem in your simulation.");
190  _es_ptr = &_problem_ptr->es();
192  }
193  }
194  else
195  {
196  _es_ptr = &_problem_ptr->es();
198  }
199 
200  // Apply the additional output flags
201  if (isParamValid("additional_execute_on"))
202  {
203  const ExecFlagEnum & add = getParam<ExecFlagEnum>("additional_execute_on");
204  for (auto & me : add)
206  }
207 
208  if (isParamValid("output_limiting_function"))
209  {
210  const Function & olf = getFunction("output_limiting_function");
211  const PiecewiseBase * pwb_olf = dynamic_cast<const PiecewiseBase *>(&olf);
212  if (pwb_olf == nullptr)
213  mooseError("Function muse have a piecewise base!");
214 
215  for (auto i = 0; i < pwb_olf->functionSize(); i++)
216  _sync_times.insert(pwb_olf->domain(i));
217  }
218 
219  // Get sync times from Times object if using
220  if (_sync_times_object)
221  {
222  if (isParamValid("output_limiting_function") || isParamSetByUser("sync_times"))
223  paramError("sync_times_object",
224  "Only one method of specifying sync times is supported at a time");
225  else
226  // Sync times for the time steppers are taken from the output warehouse. The output warehouse
227  // takes sync times from the output objects immediately after the object is constructed. Hence
228  // we must ensure that we set the `_sync_times` in the constructor
230  }
231 }
232 
233 void
235 {
236 }
237 
238 void
240 {
241  // Output is not allowed
242  if (!_allow_output && type != EXEC_FORCED)
243  return;
244 
245  // If recovering disable output of initial condition, it was already output
246  if (type == EXEC_INITIAL && _app.isRecovering())
247  return;
248 
249  // Return if the current output is not on the desired interval and there is
250  // no signal to process
251  const bool on_interval_or_exec_final = (onInterval() || (type == EXEC_FINAL));
252  // Sync across processes and only output one time per signal received.
254  const bool signal_received = Moose::interrupt_signal_number;
255  if (!(on_interval_or_exec_final || signal_received))
256  return;
257 
258  // set current type
260 
261  // Check whether we should output, then do it.
262  if (shouldOutput())
263  {
264  // store current simulation time
266 
267  // store current wall time of output
268  _last_output_wall_time = std::chrono::steady_clock::now();
269 
270  TIME_SECTION("outputStep", 2, "Outputting Step");
271  output();
272  }
273 
275 }
276 
277 bool
279 {
281  return true;
282  return false;
283 }
284 
285 bool
287 {
288  // The output flag to return
289  bool output = false;
290 
291  // Return true if the current step on the current output interval and within the output time range
292  // and within the output step range
293  if (_time >= _start_time && _time <= _end_time && _t_step >= _start_step &&
295  output = true;
296 
297  // Return false if 'sync_only' is set to true
298  if (_sync_only)
299  output = false;
300 
301  if (_sync_times_object)
302  {
303  const auto & sync_times = _sync_times_object->getUniqueTimes();
304  if (sync_times != _sync_times)
305  mooseError("The provided sync times object has changing time values. Only static time "
306  "values are supported since time steppers take sync times from the output "
307  "warehouse which determines its sync times at output construction time.");
308  }
309 
310  // If sync times are not skipped, return true if the current time is a sync_time
311  if (_sync_times.find(_time) != _sync_times.end())
312  output = true;
313 
314  // check if enough simulation time has passed between outputs
317  output = false;
318 
319  // check if enough wall time has passed between outputs
320  const auto now = std::chrono::steady_clock::now();
321  // count below returns an interger type, so lets express on a millisecond
322  // scale and convert to seconds for finer resolution
324  std::chrono::duration_cast<std::chrono::milliseconds>(now - _last_output_wall_time).count() /
325  1000.0;
326  // Take the maximum wall time since last output accross all processors
329  output = true;
330 
331  // Return the output status
332  return output;
333 }
334 
335 void
337 {
338  if (_app.isParamValid("output_wall_time_interval"))
339  {
340  _wall_time_interval = _app.getParam<Real>("output_wall_time_interval");
341 
342  // If default value of _wall_time_interval was just overriden and user did not
343  // explicitly specify _time_step_interval, override default value of
344  // _time_step_interval so output does not occur after every time step
347  }
348 }
349 
350 Real
352 {
353  if (_transient)
354  return _time;
355  else
356  return _t_step;
357 }
358 
359 Real
361 {
362  if (_transient)
363  return _time_old;
364  else
365  return _t_step - 1;
366 }
367 
368 Real
370 {
371  if (_transient)
372  return _dt;
373  else
374  return 1;
375 }
376 
377 Real
379 {
380  if (_transient)
381  return _dt_old;
382  else
383  return 1;
384 }
385 
386 int
388 {
389  return _t_step;
390 }
391 
392 const MultiMooseEnum &
394 {
395  return _execute_on;
396 }
397 
398 bool
400 {
401  return _is_advanced;
402 }
403 
404 const OutputOnWarehouse &
406 {
407  mooseError("The output object ", name(), " is not an AdvancedOutput, use isAdvanced() to check.");
408  return _advanced_execute_on;
409 }
Real & _time_old
The old time.
Definition: Output.h:211
const MultiMooseEnum & executeOn() const
Get the current &#39;execute_on&#39; selections for display.
Definition: Output.C:393
const ExecFlagType EXEC_FAILED
Definition: Moose.C:40
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:37
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
virtual Real dtOld()
Get old time step size.
Definition: Output.C:378
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:197
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
const ExecFlagType EXEC_FORCED
Definition: Moose.C:39
OutputOnWarehouse _advanced_execute_on
Storage for the individual component execute flags.
Definition: Output.h:274
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:286
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...
virtual Real dt()
Get the current time step size.
Definition: Output.C:369
Real & _dt_old
Old time step delta.
Definition: Output.h:220
const Function & getFunction(const std::string &name) const
Get a function with a given name.
const ExecFlagType EXEC_NONE
Definition: Moose.C:27
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:247
static ExecFlagEnum getDefaultExecFlagEnum()
Return an ExecFlagEnum object with the available execution flags for Output objects.
Definition: Output.C:113
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
bool isParamValid(const std::string &name) const
Definition: MooseApp.h:174
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:214
bool _transient
Transient flag (true = transient)
Definition: Output.h:182
const Parallel::Communicator & _communicator
virtual Real domain(const int i) const
Definition: PiecewiseBase.C:28
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:32
Real _wall_time_since_last_output
time in seconds since last output
Definition: Output.h:283
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
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:234
Output(const InputParameters &parameters)
Class constructor.
Definition: Output.C:120
ExecFlagEnum getDefaultExecFlagEnum()
Return the default ExecFlagEnum for MOOSE.
Definition: MooseUtils.C:1053
virtual EquationSystems & es() override
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
Definition: Output.C:278
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:253
Real & _last_output_simulation_time
last simulation time an output has occured
Definition: Output.h:277
bool contains(const std::string &value) const
Contains methods for seeing if a value is in the MultiMooseEnum.
virtual Real timeOld()
Get the old output time.
Definition: Output.C:360
Interface for notifications that the mesh has changed.
EquationSystems * _es_ptr
Reference the the libMesh::EquationSystems object that contains the data.
Definition: Output.h:188
void deprecateParam(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
const bool _time_step_interval_set_by_addparam
Whether time step interval is set by AddParam.
Definition: Output.h:226
ExecFlagType _current_execute_flag
Current execute on flag.
Definition: Output.h:205
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:239
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:256
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:179
bool _allow_output
Flag for disabling output.
Definition: Output.h:265
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:50
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:39
const Times *const _sync_times_object
Sync times object for this outputter.
Definition: Output.h:244
bool _sync_only
Flag for only executing at sync times.
Definition: Output.h:262
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
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 ...
Real _wall_time_interval
Target wall time between outputs in seconds.
Definition: Output.h:238
static InputParameters validParams()
Interface for objects interacting with the PerfGraph.
unsigned int _time_step_interval
The output time step interval.
Definition: Output.h:229
bool _use_displaced
Flag for using displaced mesh.
Definition: Output.h:185
virtual Real time()
Get the output time.
Definition: Output.C:351
bool _is_advanced
Flag for advanced output testing.
Definition: Output.h:268
std::set< Real > _sync_times
Sync times for this outputter.
Definition: Output.h:241
void setWallTimeIntervalFromCommandLineParam()
Function to set the wall time interval based on value of command line parameter (used for testing onl...
Definition: Output.C:336
bool isAdvanced()
Returns true if this object is an AdvancedOutput object.
Definition: Output.C:399
const T & getParam(const std::string &name)
Retrieve a parameter for the object.
Definition: MooseApp.h:1474
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
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:280
Real _t_tol
Time checking tolerance.
Definition: Output.h:259
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
void max(const T &r, T &o, Request &req) const
void push_back(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
virtual MooseMesh & mesh() override
MooseMesh * _mesh_ptr
A convenience pointer to the current mesh (reference or displaced depending on "use_displaced") ...
Definition: Output.h:191
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:641
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
static InputParameters validParams()
Definition: MooseObject.C:24
virtual const OutputOnWarehouse & advancedExecuteOn() const
Returns the advanced &#39;execute_on&#39; settings.
Definition: Output.C:405
virtual int timeStep()
Get the current time step.
Definition: Output.C:387
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:1167
const ExecFlagType EXEC_FINAL
Definition: Moose.C:38
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:208
void ErrorVector unsigned int
Real & _dt
Time step delta.
Definition: Output.h:217
const Real _min_simulation_time_interval
Minimum simulation time between outputs.
Definition: Output.h:232
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:28