Chi-Tech
TimeStepper.cc
Go to the documentation of this file.
1#include "TimeStepper.h"
2
3#include <cmath>
4
5namespace chi_physics
6{
7
9{
11
12 params.AddOptionalParameter("dt", 0.01, "Initial timestep to use");
13 params.AddOptionalParameter("time", 0.0, "Initial time");
15 "time_index", 0, "Time index. Useful for output control.");
16 params.AddOptionalParameter("start_time", 0.0, "Start time");
17 params.AddOptionalParameter("end_time", 1.0, "End time");
19 "max_time_steps",
20 -1,
21 "Maximum number of timesteps to take. A negative number disables this.");
22 params.AddOptionalParameter("dt_min", 1.0e-5, "Minimum allowable timestep.");
23
24 params.AddOptionalParameter("eps",
25 1.0e-8,
26 "General time tolerance. This is used in "
27 "fuzzy equals.");
28
29 return params;
30}
31
33 : ChiObject(params),
34 dt_(params.GetParamValue<double>("dt")),
35 time_(params.GetParamValue<double>("time")),
36 t_index_(params.GetParamValue<size_t>("time_index")),
37
38 start_time_(params.GetParamValue<double>("start_time")),
39 end_time_(params.GetParamValue<double>("end_time")),
40 max_time_steps_(params.GetParamValue<int>("max_time_steps")),
41 dt_min_(params.GetParamValue<double>("dt_min")),
42
43 general_tolerance_(params.GetParamValue<double>("eps")),
44 last_dt_(dt_)
45{
46}
47
48/**Overridable method to get the timestep size.*/
49double TimeStepper::TimeStepSize() const { return dt_; }
50
51/**Returns the current controller time.*/
52double TimeStepper::Time() const { return time_; }
53
54/**Returns the current time index.*/
55size_t TimeStepper::TimeStepIndex() const { return t_index_; }
56
57/**Returns the current controller start_time.*/
58double TimeStepper::StartTime() const { return start_time_; }
59
60/**Returns the current controller end_time.*/
61double TimeStepper::EndTime() const { return end_time_; }
62
63/**Returns the current controller max time steps.*/
65
66/**If start_time <= time <= end_time, this will return true.*/
68{
69 if (max_time_steps_ >= 0 and (t_index_ >= max_time_steps_)) return false;
70
71 bool active = (time_ >= (start_time_ - general_tolerance_) and
73
74 if (std::fabs(end_time_ - time_) < general_tolerance_) active = false;
75
76 return active;
77}
78
79/**Manually set the time step size.*/
81{
82 dt_ = dt;
83
84 dt_ = std::min(end_time_ - time_, dt_);
85}
86
87/**Manually set the current time.*/
88void TimeStepper::SetTime(double time) { time_ = time; }
89
90/**Manually set the start_time.*/
91void TimeStepper::SetStartTime(double time) { start_time_ = time; }
92
93/**Manually set the end_time.*/
94void TimeStepper::SetEndTime(double time)
95{
96 end_time_ = time;
97 dt_ = last_dt_;
98}
99
100/**Manually set the maximum number of time steps. A negative number disables
101 * this check.*/
103
104/**Manually sets the minimum time step size.*/
105void TimeStepper::SetMinimumTimeStepSize(double dt_min) { dt_min_ = dt_min; }
106
107/**Advances the controller's state. The most basic action here is to
108 * advance time and the time index. If the solver is at or beyond its end time
109 * then it will return false. Otherwise it will advance and return true.*/
111{
112 last_dt_ = dt_;
113 time_ += dt_;
114 t_index_ += 1;
115
116 // Still trying to figure this part out
117 // Limit dt to not exceed end_time_
118 // dt_ = std::min(end_time_ - time_, dt_);
119 dt_ = std::max(dt_, dt_min_);
120}
121
122std::string TimeStepper::StringTimeInfo(bool old_time /*=false*/) const
123{
124 const double time = old_time ? time_ : time_ + dt_;
125 std::stringstream outstr;
126 outstr << "Time step " << t_index_;
127 {
128 char buffer[100];
129 snprintf(buffer, 100, ", time = %g", time);
130 outstr << buffer;
131 }
132 {
133 char buffer[100];
134 snprintf(buffer, 100, ", dt = %g", dt_);
135 outstr << buffer;
136 }
137
138 return outstr.str();
139}
140
141} // namespace chi_physics
static chi::InputParameters GetInputParameters()
Definition: ChiObject.cc:4
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
double MaxTimeSteps() const
Definition: TimeStepper.cc:64
static chi::InputParameters GetInputParameters()
Definition: TimeStepper.cc:8
void SetEndTime(double time)
Definition: TimeStepper.cc:94
virtual void Advance()
Definition: TimeStepper.cc:110
void SetTime(double time)
Definition: TimeStepper.cc:88
void SetTimeStepSize(double dt)
Definition: TimeStepper.cc:80
const double general_tolerance_
Definition: TimeStepper.h:85
double TimeStepSize() const
Definition: TimeStepper.cc:49
double EndTime() const
Definition: TimeStepper.cc:61
void SetMinimumTimeStepSize(double dt_min)
Definition: TimeStepper.cc:105
TimeStepper(const chi::InputParameters &params)
Definition: TimeStepper.cc:32
void SetStartTime(double time)
Definition: TimeStepper.cc:91
size_t TimeStepIndex() const
Definition: TimeStepper.cc:55
std::string StringTimeInfo(bool old_time=false) const
Definition: TimeStepper.cc:122
double StartTime() const
Definition: TimeStepper.cc:58