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