https://mooseframework.inl.gov
Times.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 #include "Times.h"
11 #include "libmesh/parallel_algebra.h"
12 
15 {
17  params.addParam<bool>(
18  "unique_times", true, "Whether duplicate values should be removed from the Times vector");
19  params.addParam<Real>(
20  "unique_tolerance",
21  1e-12,
22  "Absolute tolerance for removing duplicated times when getting a set of unique times");
23  params.addParam<bool>("auto_sort", true, "Whether Times should be sorted");
24  // This parameter should be set by each derived class depending on whether the generation of
25  // times is replicated or distributed. We want times to be replicated across all ranks
26  params.addRequiredParam<bool>("auto_broadcast",
27  "Wether Times should be broadcasted across all ranks");
28  params.addParamNamesToGroup("auto_broadcast auto_sort unique_times unique_tolerance", "Advanced");
29 
30  // Unlikely to ever be used as Times do not loop over the mesh and use material properties,
31  // let alone stateful
32  params.suppressParameter<MaterialPropertyName>("prop_getter_suffix");
33  params.suppressParameter<bool>("use_interpolated_state");
34 
35  params.registerBase("Times");
36  return params;
37 }
38 
39 Times::Times(const InputParameters & parameters)
40  : GeneralReporter(parameters),
41  // Times will be replicated on every rank
42  _times(declareValueByName<std::vector<Real>, ReporterVectorContext<Real>>(
43  "times", REPORTER_MODE_REPLICATED)),
44  _need_broadcast(getParam<bool>("auto_broadcast")),
45  _need_sort(getParam<bool>("auto_sort")),
46  _need_unique(getParam<bool>("unique_times")),
47  _unique_tol(getParam<Real>("unique_tolerance"))
48 {
49 }
50 
51 Real
52 Times::getTimeAtIndex(unsigned int index) const
53 {
54  if (_times.size() < index)
55  mooseError("Times retrieved with an out-of-bound index");
56 
57  if (_times.size())
58  return _times[index];
59  else
60  mooseError("Times vector has not been initialized.");
61 }
62 
63 Real
64 Times::getPreviousTime(const Real current_time) const
65 {
66  if (!_times.size())
67  mooseError("Times vector has not been initialized.");
68 
69  Real previous_time = _times[0];
70  for (const auto i : make_range(std::size_t(1), _times.size()))
71  {
72  const auto & time = _times[i];
73  if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
74  return previous_time;
75  previous_time = time;
76  }
77  // entire Times vector is prior to current_time
78  return previous_time;
79 }
80 
81 Real
82 Times::getNextTime(const Real current_time, const bool error_if_no_next) const
83 {
84  for (const auto i : index_range(_times))
85  {
86  const auto & time = _times[i];
87  if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
88  return time;
89  }
90  if (_times.size() && error_if_no_next)
91  mooseError("No next time in Times vector for time ",
92  current_time,
93  ". Maximum time in vector is ",
94  *_times.rbegin());
95  else if (!error_if_no_next)
97  else
98  mooseError("Times vector has not been initialized.");
99 }
100 
101 const std::vector<Real> &
103 {
104  if (_times.size())
105  return _times;
106  else
107  mooseError("Times vector has not been initialized.");
108 }
109 
110 void
112 {
113  _times.clear();
114 }
115 
116 void
118 {
119  if (_need_broadcast)
120  // The consumer/producer reporter interface can keep track of whether a reduction is needed
121  // (for example if a consumer needs replicated data, but the producer is distributed) however,
122  // we have currently made the decision that times should ALWAYS be replicated
123  _communicator.allgather(_times, /* identical buffer lengths = */ false);
124 
125  if (_need_sort)
126  std::sort(_times.begin(), _times.end());
127  if (_need_unique)
128  _times.erase(unique(_times.begin(),
129  _times.end(),
130  [this](Real l, Real r) { return std::abs(l - r) < _unique_tol; }),
131  _times.end());
132 }
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
const bool _need_unique
Whether duplicate times should be removed.
Definition: Times.h:79
This context is specific for vector types of reporters, mainly for declaring a vector of the type fro...
Real getNextTime(const Real current_time, const bool error_if_no_next) const
Find the next time in the times vector for a given time If current_time is also in the times vector w...
Definition: Times.C:82
Reporter object that has a single execution of the "execute" method for for each execute flag...
const std::vector< Real > & getTimes() const
Getter for the full times vector.
Definition: Times.C:102
Times(const InputParameters &parameters)
Definition: Times.C:39
const bool _need_broadcast
Whether generation of times is distributed or not (and therefore needs a broadcast) ...
Definition: Times.h:75
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
void clearTimes()
Clear the times vector.
Definition: Times.C:111
const Parallel::Communicator & _communicator
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
auto max(const L &left, const R &right)
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System...
static InputParameters validParams()
Definition: Times.C:14
Real getPreviousTime(const Real current_time) const
Find the previous time in the times vector for a given time If current_time is also in the times vect...
Definition: Times.C:64
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Real getTimeAtIndex(unsigned int index) const
Getter for a single time at a known index.
Definition: Times.C:52
const Real _unique_tol
Absolute tolerance for performing duplication checks to make the times vector unique.
Definition: Times.h:81
IntRange< T > make_range(T beg, T end)
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...
const ReporterMode REPORTER_MODE_REPLICATED
virtual void finalize() override
In charge of reduction across all ranks.
Definition: Times.C:117
auto index_range(const T &sizable)
std::vector< Real > & _times
The vector holding the times.
Definition: Times.h:72
bool absoluteFuzzyGreaterThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than another variable within an absolute tolerance...
Definition: MooseUtils.h:428
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 bool _need_sort
Whether times should be sorted, because they come from different sources for example.
Definition: Times.h:77