https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.addParam<bool>("dynamic_time_sequence",
36 true,
37 "Whether the time sequence is dynamic and thus needs to be updated");
38
39 params.registerBase("Times");
40 return params;
41}
42
43Times::Times(const InputParameters & parameters)
44 : GeneralReporter(parameters),
45 // Times will be replicated on every rank
46 _times(declareValueByName<std::vector<Real>, ReporterVectorContext<Real>>(
47 "times", REPORTER_MODE_REPLICATED)),
48 _need_broadcast(getParam<bool>("auto_broadcast")),
49 _need_sort(getParam<bool>("auto_sort")),
50 _need_unique(getParam<bool>("unique_times")),
51 _unique_tol(getParam<Real>("unique_tolerance")),
52 _dynamic_time_sequence(getParam<bool>("dynamic_time_sequence"))
53{
54}
55
56Real
57Times::getTimeAtIndex(unsigned int index) const
58{
59 if (_times.size() < index)
60 mooseError("Times retrieved with an out-of-bound index");
61
62 if (_times.size())
63 return _times[index];
64 else
65 mooseError("Times vector has not been initialized.");
66}
67
68Real
69Times::getPreviousTime(const Real current_time) const
70{
71 if (!_times.size())
72 mooseError("Times vector has not been initialized.");
73
74 Real previous_time = _times[0];
75 for (const auto i : make_range(std::size_t(1), _times.size()))
76 {
77 const auto & time = _times[i];
78 if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
79 return previous_time;
80 previous_time = time;
81 }
82 // entire Times vector is prior to current_time
83 return previous_time;
84}
85
86Real
87Times::getNextTime(const Real current_time, const bool error_if_no_next) const
88{
89 for (const auto i : index_range(_times))
90 {
91 const auto & time = _times[i];
92 if (MooseUtils::absoluteFuzzyGreaterThan(time, current_time))
93 return time;
94 }
95 if (_times.size() && error_if_no_next)
96 mooseError("No next time in Times vector for time ",
97 current_time,
98 ". Maximum time in vector is ",
99 *_times.rbegin());
100 else if (!error_if_no_next)
101 return std::numeric_limits<Real>::max();
102 else
103 mooseError("Times vector has not been initialized.");
104}
105
106const std::vector<Real> &
108{
109 if (_times.size())
110 return _times;
111 else
112 {
113 mooseError("Times vector has not been initialized.");
114 }
115}
116
117void
119{
120 _times.clear();
121}
122
123void
125{
126 if (_need_broadcast)
127 // The consumer/producer reporter interface can keep track of whether a reduction is needed
128 // (for example if a consumer needs replicated data, but the producer is distributed) however,
129 // we have currently made the decision that times should ALWAYS be replicated
130 _communicator.allgather(_times, /* identical buffer lengths = */ false);
131
132 if (_need_sort)
133 std::sort(_times.begin(), _times.end());
134 if (_need_unique)
135 _times.erase(unique(_times.begin(),
136 _times.end(),
137 [this](Real l, Real r) { return std::abs(l - r) < _unique_tol; }),
138 _times.end());
139}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ReporterMode REPORTER_MODE_REPLICATED
Reporter object that has a single execution of the "execute" method for each execute flag.
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn't required or valid in the derived class...
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...
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 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...
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
This context is specific for vector types of reporters, mainly for declaring a vector of the type fro...
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
void clearTimes()
Clear the times vector.
Definition Times.C:118
static InputParameters validParams()
Definition Times.C:14
const bool _need_broadcast
Whether generation of times is distributed or not (and therefore needs a broadcast)
Definition Times.h:77
const std::vector< Real > & getTimes() const
Getter for the full times vector.
Definition Times.C:107
virtual void finalize() override
In charge of reduction across all ranks.
Definition Times.C:124
Times(const InputParameters &parameters)
Definition Times.C:43
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:87
const bool _need_sort
Whether times should be sorted, because they come from different sources for example.
Definition Times.h:79
std::vector< Real > & _times
The vector holding the times.
Definition Times.h:74
Real getTimeAtIndex(unsigned int index) const
Getter for a single time at a known index.
Definition Times.C:57
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:69
const bool _need_unique
Whether duplicate times should be removed.
Definition Times.h:81
const Parallel::Communicator & _communicator