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.addRangeCheckedParam<Real>(
53  "wall_time_interval",
55  "wall_time_interval > 0",
56  "The target wall time interval (in seconds) at which to output");
57  params.setDocUnit("wall_time_interval", "seconds");
58  params.addParam<std::vector<Real>>(
59  "sync_times", {}, "Times at which the output and solution is forced to occur");
60  params.addParam<TimesName>(
61  "sync_times_object",
62  "Times object providing the times at which the output and solution is forced to occur");
63  params.addParam<bool>("sync_only", false, "Only export results at sync times");
64  params.addParam<Real>("start_time", "Time at which this output object begins to operate");
65  params.addParam<Real>("end_time", "Time at which this output object stop operating");
66  params.addParam<int>("start_step", "Time step at which this output object begins to operate");
67  params.addParam<int>("end_step", "Time step at which this output object stop operating");
68  params.addParam<Real>(
69  "time_tolerance", 1e-14, "Time tolerance utilized checking start and end times");
70  params.addDeprecatedParam<FunctionName>(
71  "output_limiting_function",
72  "Piecewise base function that sets sync_times",
73  "Replaced by using the Times system with the sync_times_objects parameter");
74 
75  // Update the 'execute_on' input parameter for output
76  ExecFlagEnum & exec_enum = params.set<ExecFlagEnum>("execute_on", true);
77  exec_enum = Output::getDefaultExecFlagEnum();
78  exec_enum = {EXEC_INITIAL, EXEC_TIMESTEP_END};
79  params.setDocString("execute_on", exec_enum.getDocString());
80 
81  // Add ability to append to the 'execute_on' list
82  params.addParam<ExecFlagEnum>("additional_execute_on", exec_enum, exec_enum.getDocString());
83  params.set<ExecFlagEnum>("additional_execute_on").clearSetValues();
84  params.addParamNamesToGroup("execute_on additional_execute_on", "Execution scheduling");
85 
86  // 'Timing' group
87  params.addParamNamesToGroup("time_tolerance time_step_interval sync_times sync_times_object "
88  "sync_only start_time end_time "
89  "start_step end_step min_simulation_time_interval "
90  "wall_time_interval",
91  "Timing and frequency of output");
92 
93  // Add a private parameter for indicating if it was created with short-cut syntax
94  params.addPrivateParam<bool>("_built_by_moose", false);
95 
96  // Register this class as base class
97  params.declareControllable("enable");
98  params.registerBase("Output");
99 
100  return params;
101 }
102 
105 {
107  exec_enum.addAvailableFlags(EXEC_FAILED);
108  return exec_enum;
109 }
110 
111 Output::Output(const InputParameters & parameters)
112  : MooseObject(parameters),
113  Restartable(this, "Output"),
114  MeshChangedInterface(parameters),
115  SetupInterface(this),
116  FunctionInterface(this),
119  ReporterInterface(this),
120  PerfGraphInterface(this),
121  _problem_ptr(getParam<FEProblemBase *>("_fe_problem_base")),
122  _transient(_problem_ptr->isTransient()),
123  _use_displaced(getParam<bool>("use_displaced")),
124  _es_ptr(nullptr),
125  _mesh_ptr(nullptr),
126  _execute_on(getParam<ExecFlagEnum>("execute_on")),
127  _current_execute_flag(EXEC_NONE),
128  _time(_problem_ptr->time()),
129  _time_old(_problem_ptr->timeOld()),
130  _t_step(_problem_ptr->timeStep()),
131  _dt(_problem_ptr->dt()),
132  _dt_old(_problem_ptr->dtOld()),
133  _num(0),
134  _time_step_interval_set_by_addparam(parameters.isParamSetByAddParam("time_step_interval")),
135  // If wall_time_interval is user-specified and time_step_interval is not,
136  // override default value of time_step_interval so output does not occur
137  // after every time step.
138  _time_step_interval(
139  (parameters.isParamSetByUser("wall_time_interval") && _time_step_interval_set_by_addparam)
140  ? std::numeric_limits<unsigned int>::max()
141  : getParam<unsigned int>("time_step_interval")),
142  _min_simulation_time_interval(getParam<Real>("min_simulation_time_interval")),
143  _wall_time_interval(getParam<Real>("wall_time_interval")),
144  _sync_times(std::set<Real>(getParam<std::vector<Real>>("sync_times").begin(),
145  getParam<std::vector<Real>>("sync_times").end())),
146  _sync_times_object(isParamValid("sync_times_object")
147  ? static_cast<Times *>(&_problem_ptr->getUserObject<Times>(
148  getParam<TimesName>("sync_times_object")))
149  : nullptr),
150  _start_time(isParamValid("start_time") ? getParam<Real>("start_time")
151  : std::numeric_limits<Real>::lowest()),
152  _end_time(isParamValid("end_time") ? getParam<Real>("end_time")
153  : std::numeric_limits<Real>::max()),
154  _start_step(isParamValid("start_step") ? getParam<int>("start_step")
155  : std::numeric_limits<int>::lowest()),
156  _end_step(isParamValid("end_step") ? getParam<int>("end_step")
157  : std::numeric_limits<int>::max()),
158  _t_tol(getParam<Real>("time_tolerance")),
159  _sync_only(getParam<bool>("sync_only")),
160  _allow_output(true),
161  _is_advanced(false),
162  _advanced_execute_on(_execute_on, parameters),
163  _last_output_simulation_time(declareRestartableData<Real>("last_output_simulation_time",
164  std::numeric_limits<Real>::lowest())),
165  _last_output_wall_time(std::chrono::steady_clock::now())
166 {
167  if (_use_displaced)
168  {
169  std::shared_ptr<DisplacedProblem> dp = _problem_ptr->getDisplacedProblem();
170  if (dp != nullptr)
171  {
172  _es_ptr = &dp->es();
173  _mesh_ptr = &dp->mesh();
174  }
175  else
176  {
177  mooseWarning(
178  name(),
179  ": Parameter 'use_displaced' ignored, there is no displaced problem in your simulation.");
180  _es_ptr = &_problem_ptr->es();
182  }
183  }
184  else
185  {
186  _es_ptr = &_problem_ptr->es();
188  }
189 
190  // Apply the additional output flags
191  if (isParamValid("additional_execute_on"))
192  {
193  const ExecFlagEnum & add = getParam<ExecFlagEnum>("additional_execute_on");
194  for (auto & me : add)
196  }
197 
198  if (isParamValid("output_limiting_function"))
199  {
200  const Function & olf = getFunction("output_limiting_function");
201  const PiecewiseBase * pwb_olf = dynamic_cast<const PiecewiseBase *>(&olf);
202  if (pwb_olf == nullptr)
203  mooseError("Function muse have a piecewise base!");
204 
205  for (auto i = 0; i < pwb_olf->functionSize(); i++)
206  _sync_times.insert(pwb_olf->domain(i));
207  }
208 
209  // Get sync times from Times object if using
210  if (_sync_times_object)
211  {
212  if (isParamValid("output_limiting_function") || isParamSetByUser("sync_times"))
213  paramError("sync_times_object",
214  "Only one method of specifying sync times is supported at a time");
215  else
216  // Sync times for the time steppers are taken from the output warehouse. The output warehouse
217  // takes sync times from the output objects immediately after the object is constructed. Hence
218  // we must ensure that we set the `_sync_times` in the constructor
220  }
221 }
222 
223 void
225 {
226 }
227 
228 void
230 {
231  // Output is not allowed
232  if (!_allow_output && type != EXEC_FORCED)
233  return;
234 
235  // If recovering disable output of initial condition, it was already output
236  if (type == EXEC_INITIAL && _app.isRecovering())
237  return;
238 
239  // Return if the current output is not on the desired interval and there is
240  // no signal to process
241  const bool on_interval_or_exec_final = (onInterval() || (type == EXEC_FINAL));
242  // Reduce a copy so MPI cannot overwrite a signal received during the collective.
243  int signal_number = Moose::interrupt_signal_number;
244  comm().max(signal_number);
245  // Do not write back zero because the handler may have set the latch after the copy.
246  if (signal_number)
247  Moose::interrupt_signal_number = signal_number;
248  const bool signal_received = signal_number;
249  if (!(on_interval_or_exec_final || signal_received))
250  return;
251 
252  // set current type
254 
255  // Check whether we should output, then do it.
256  if (shouldOutput())
257  {
258  // store current simulation time
260 
261  // store current wall time of output
262  _last_output_wall_time = std::chrono::steady_clock::now();
263 
264  TIME_SECTION("outputStep", 2, "Outputting Step");
265  output();
266  }
267 
269 }
270 
271 bool
273 {
275  return true;
276  return false;
277 }
278 
279 bool
281 {
282  // The output flag to return
283  bool output = false;
284 
285  // Return true if the current step on the current output interval and within the output time range
286  // and within the output step range
287  if (_time >= _start_time && _time <= _end_time && _t_step >= _start_step &&
289  output = true;
290 
291  // Return false if 'sync_only' is set to true
292  if (_sync_only)
293  output = false;
294 
295  if (_sync_times_object)
296  {
297  const auto & sync_times = _sync_times_object->getUniqueTimes();
298  if (sync_times != _sync_times)
299  mooseError("The provided sync times object has changing time values. Only static time "
300  "values are supported since time steppers take sync times from the output "
301  "warehouse which determines its sync times at output construction time.");
302  }
303 
304  // We make sync times have precendence over the other criteria by convention, since they already
305  // take precedence over start/end step, start/end time, step frequency etc.
306  //
307  // Check if enough simulation time has passed between outputs
310  output = false;
311 
312  // If sync times are not skipped, return true if the current time is a sync_time
313  for (const auto _sync_time : _sync_times)
314  {
315  if (std::abs(_sync_time - _time) < _t_tol)
316  output = true;
317  }
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:217
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:50
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:50
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:29
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:203
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
void setDocUnit(const std::string &name, const std::string &doc_unit)
Set the unit string of a parameter.
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:457
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:406
const ExecFlagType EXEC_FORCED
Definition: Moose.C:49
OutputOnWarehouse _advanced_execute_on
Storage for the individual component execute flags.
Definition: Output.h:277
virtual bool onInterval()
Returns true if the output interval is satisfied.
Definition: Output.C:280
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:369
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:250
static ExecFlagEnum getDefaultExecFlagEnum()
Return an ExecFlagEnum object with the available execution flags for Output objects.
Definition: Output.C:104
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:286
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:224
Output(const InputParameters &parameters)
Class constructor.
Definition: Output.C:111
ExecFlagEnum getDefaultExecFlagEnum()
Definition: MooseUtils.C:972
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
Definition: Output.C:272
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:256
void mooseWarning(Args &&... args) const
Real & _last_output_simulation_time
last simulation time an output has occured
Definition: Output.h:280
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:360
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
unsigned int count
Definition: MortarUtils.C:53
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:229
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:259
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:268
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:247
bool _sync_only
Flag for only executing at sync times.
Definition: Output.h:265
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:375
Real _wall_time_interval
Target wall time between outputs in seconds.
Definition: Output.h:241
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:351
bool _is_advanced
Flag for advanced output testing.
Definition: Output.h:271
std::set< Real > _sync_times
Sync times for this outputter.
Definition: Output.h:244
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
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:283
Real _t_tol
Time checking tolerance.
Definition: Output.h:262
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
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: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:1669
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:48
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...
volatile std::sig_atomic_t 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:927
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30