Skip to content

Commit e42fe5f

Browse files
author
Dmitri Naumov
committed
Merge branch 'ImprovementsInTimeLoop' into 'master'
Small improvements in TimeStepAlgorithm See merge request ogs/ogs!5391
2 parents e951979 + c8da434 commit e42fe5f

12 files changed

+105
-113
lines changed

NumLib/TimeStepping/Algorithms/EvolutionaryPIDcontroller.cpp

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
namespace NumLib
2222
{
23-
std::tuple<bool, double> EvolutionaryPIDcontroller::next(
24-
double const solution_error, int const /*number_iterations*/,
25-
NumLib::TimeStep& /*timestep_previous*/, NumLib::TimeStep& timestep_current)
23+
double EvolutionaryPIDcontroller::next(double const solution_error,
24+
int const /*number_iterations*/,
25+
NumLib::TimeStep& /*timestep_previous*/,
26+
NumLib::TimeStep& timestep_current)
2627
{
2728
const bool is_previous_step_accepted = timestep_current.isAccepted();
2829

@@ -50,7 +51,7 @@ std::tuple<bool, double> EvolutionaryPIDcontroller::next(
5051
"\t or the simulation will be halted.",
5152
_tol, h_new);
5253

53-
return std::make_tuple(timestep_current.isAccepted(), h_new);
54+
return h_new;
5455
}
5556

5657
// step accepted.
@@ -60,46 +61,42 @@ std::tuple<bool, double> EvolutionaryPIDcontroller::next(
6061
{
6162
_e_n_minus1 = e_n;
6263

63-
return std::make_tuple(timestep_current.isAccepted(), _h0);
64+
return _h0;
6465
}
65-
else
66-
{
67-
const double h_n = timestep_current.dt();
68-
double h_new = h_n;
66+
const double h_n = timestep_current.dt();
67+
double h_new = h_n;
6968

70-
if (e_n > zero_threshold)
69+
if (e_n > zero_threshold)
70+
{
71+
if (_e_n_minus1 > zero_threshold)
7172
{
72-
if (_e_n_minus1 > zero_threshold)
73+
if (_e_n_minus2 > zero_threshold)
7374
{
74-
if (_e_n_minus2 > zero_threshold)
75-
{
76-
h_new = std::pow(_e_n_minus1 / e_n, _kP) *
77-
std::pow(_tol / e_n, _kI) *
78-
std::pow(
79-
_e_n_minus1 * _e_n_minus1 / (e_n * _e_n_minus2),
80-
_kD) *
81-
h_n;
82-
}
83-
else
84-
{
85-
h_new = std::pow(_e_n_minus1 / e_n, _kP) *
86-
std::pow(_tol / e_n, _kI) * h_n;
87-
}
75+
h_new =
76+
std::pow(_e_n_minus1 / e_n, _kP) *
77+
std::pow(_tol / e_n, _kI) *
78+
std::pow(_e_n_minus1 * _e_n_minus1 / (e_n * _e_n_minus2),
79+
_kD) *
80+
h_n;
8881
}
8982
else
9083
{
91-
h_new = std::pow(_tol / e_n, _kI) * h_n;
84+
h_new = std::pow(_e_n_minus1 / e_n, _kP) *
85+
std::pow(_tol / e_n, _kI) * h_n;
9286
}
9387
}
88+
else
89+
{
90+
h_new = std::pow(_tol / e_n, _kI) * h_n;
91+
}
92+
}
9493

95-
h_new =
96-
limitStepSize(h_new, is_previous_step_accepted, timestep_current);
94+
h_new = limitStepSize(h_new, is_previous_step_accepted, timestep_current);
9795

98-
_e_n_minus2 = _e_n_minus1;
99-
_e_n_minus1 = e_n;
96+
_e_n_minus2 = _e_n_minus1;
97+
_e_n_minus1 = e_n;
10098

101-
return std::make_tuple(timestep_current.isAccepted(), h_new);
102-
}
99+
return h_new;
103100
}
104101

105102
double EvolutionaryPIDcontroller::limitStepSize(

NumLib/TimeStepping/Algorithms/EvolutionaryPIDcontroller.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ class EvolutionaryPIDcontroller final : public TimeStepAlgorithm
6464
{
6565
}
6666

67-
std::tuple<bool, double> next(double solution_error,
68-
int number_iterations,
69-
NumLib::TimeStep& timestep_previous,
70-
NumLib::TimeStep& timestep_current) override;
67+
double next(double solution_error,
68+
int number_iterations,
69+
NumLib::TimeStep& timestep_previous,
70+
NumLib::TimeStep& timestep_current) override;
7171

7272
bool isSolutionErrorComputationNeeded() const override { return true; }
7373

NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,16 @@ FixedTimeStepping::FixedTimeStepping(double t0, double t_end, double dt)
213213
}
214214
}
215215

216-
std::tuple<bool, double> FixedTimeStepping::next(
217-
double const /*solution_error*/, int const /*number_iterations*/,
218-
NumLib::TimeStep& /*ts_previous*/, NumLib::TimeStep& ts_current)
216+
double FixedTimeStepping::next(double const /*solution_error*/,
217+
int const /*number_iterations*/,
218+
NumLib::TimeStep& /*ts_previous*/,
219+
NumLib::TimeStep& ts_current)
219220
{
220221
// check if last time step
221222
if (ts_current.timeStepNumber() == _dt_vector.size() ||
222223
ts_current.current() >= end())
223224
{
224-
return std::make_tuple(true, 0.0);
225+
return 0.0;
225226
}
226227

227228
double dt = _dt_vector[ts_current.timeStepNumber()];
@@ -230,7 +231,7 @@ std::tuple<bool, double> FixedTimeStepping::next(
230231
dt = end()() - ts_current.current()();
231232
}
232233

233-
return std::make_tuple(true, dt);
234+
return dt;
234235
}
235236

236237
bool FixedTimeStepping::areRepeatDtPairsValid(

NumLib/TimeStepping/Algorithms/FixedTimeStepping.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class FixedTimeStepping final : public TimeStepAlgorithm
5252
std::vector<RepeatDtPair> const& repeat_dt_pairs,
5353
std::vector<double> const& fixed_times_for_output);
5454

55-
std::tuple<bool, double> next(double solution_error, int number_iterations,
56-
NumLib::TimeStep& ts_previous,
57-
NumLib::TimeStep& ts_current) override;
55+
double next(double solution_error, int number_iterations,
56+
NumLib::TimeStep& ts_previous,
57+
NumLib::TimeStep& ts_current) override;
5858

5959
static bool areRepeatDtPairsValid(
6060
std::vector<RepeatDtPair> const& repeat_dt_pairs);

NumLib/TimeStepping/Algorithms/IterationNumberBasedTimeStepping.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,45 @@ IterationNumberBasedTimeStepping::IterationNumberBasedTimeStepping(
5858
}
5959
}
6060

61-
std::tuple<bool, double> IterationNumberBasedTimeStepping::next(
62-
double const /*solution_error*/, int const number_iterations,
63-
NumLib::TimeStep& ts_previous, NumLib::TimeStep& ts_current)
61+
double IterationNumberBasedTimeStepping::next(double const /*solution_error*/,
62+
int const number_iterations,
63+
NumLib::TimeStep& ts_previous,
64+
NumLib::TimeStep& ts_current)
6465
{
6566
_iter_times = number_iterations;
6667

67-
if (_previous_time_step_accepted)
68+
if (ts_previous.isAccepted())
6869
{
6970
ts_previous = ts_current;
7071
}
7172

7273
// confirm current time and move to the next if accepted
7374
if (ts_current.isAccepted())
7475
{
75-
_previous_time_step_accepted = true;
76-
return std::make_tuple(_previous_time_step_accepted,
77-
getNextTimeStepSize(ts_previous, ts_current));
76+
ts_previous.setAccepted(true);
77+
return getNextTimeStepSize(ts_previous, ts_current);
7878
}
79-
else
80-
{
81-
double dt = getNextTimeStepSize(ts_previous, ts_current);
82-
// In case it is the first time be rejected, re-computed dt again with
83-
// current dt
84-
if (std::abs(dt - ts_current.dt()) <
85-
std::numeric_limits<double>::epsilon())
86-
{
87-
// time step was rejected, keep dt for the next dt computation.
88-
ts_previous = // essentially equal to _ts_prev.dt = _ts_current.dt.
89-
TimeStep{ts_previous.previous(), ts_previous.previous() + dt,
90-
ts_previous.timeStepNumber()};
91-
dt = getNextTimeStepSize(ts_previous, ts_current);
92-
}
9379

80+
double dt = getNextTimeStepSize(ts_previous, ts_current);
81+
// In case it is the first time be rejected, re-computed dt again with
82+
// current dt
83+
if (std::abs(dt - ts_current.dt()) < std::numeric_limits<double>::epsilon())
84+
{
9485
// time step was rejected, keep dt for the next dt computation.
95-
ts_previous = // essentially equal to ts_previous.dt = _ts_current.dt.
86+
ts_previous = // essentially equal to _ts_prev.dt = _ts_current.dt.
9687
TimeStep{ts_previous.previous(), ts_previous.previous() + dt,
9788
ts_previous.timeStepNumber()};
98-
ts_current = TimeStep{ts_current.previous(), ts_current.previous() + dt,
99-
ts_current.timeStepNumber()};
100-
101-
_previous_time_step_accepted = false;
102-
return std::make_tuple(_previous_time_step_accepted, dt);
89+
dt = getNextTimeStepSize(ts_previous, ts_current);
10390
}
91+
92+
// time step was rejected, keep dt for the next dt computation.
93+
ts_previous = // essentially equal to ts_previous.dt = _ts_current.dt.
94+
TimeStep{ts_previous.previous(), ts_previous.previous() + dt,
95+
ts_previous.timeStepNumber()};
96+
ts_current = TimeStep{ts_current.previous(), ts_current.previous() + dt,
97+
ts_current.timeStepNumber()};
98+
99+
return dt;
104100
}
105101

106102
double findMultiplier(

NumLib/TimeStepping/Algorithms/IterationNumberBasedTimeStepping.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class IterationNumberBasedTimeStepping final : public TimeStepAlgorithm
9797

9898
~IterationNumberBasedTimeStepping() override = default;
9999

100-
std::tuple<bool, double> next(double solution_error, int number_iterations,
101-
NumLib::TimeStep& ts_previous,
102-
NumLib::TimeStep& ts_current) override;
100+
double next(double solution_error, int number_iterations,
101+
NumLib::TimeStep& ts_previous,
102+
NumLib::TimeStep& ts_current) override;
103103

104104
bool isSolutionErrorComputationNeeded() const override { return true; }
105105

@@ -130,7 +130,6 @@ class IterationNumberBasedTimeStepping final : public TimeStepAlgorithm
130130
/// The number of nonlinear iterations.
131131
int _iter_times = 0;
132132

133-
bool _previous_time_step_accepted = true;
134133
std::vector<double> const _fixed_times_for_output;
135134
};
136135

NumLib/TimeStepping/Algorithms/TimeStepAlgorithm.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class TimeStepAlgorithm
4949
/// the next step
5050
/// \param ts_current the current time step used to compute the size of the
5151
/// next step
52-
/// \return A step acceptance flag and the computed step size.
53-
virtual std::tuple<bool, double> next(const double solution_error,
54-
int number_iterations,
55-
NumLib::TimeStep& ts_previous,
56-
NumLib::TimeStep& ts_current) = 0;
52+
/// \return the computed step size.
53+
virtual double next(const double solution_error,
54+
int number_iterations,
55+
NumLib::TimeStep& ts_previous,
56+
NumLib::TimeStep& ts_current) = 0;
5757

5858
/// Get a flag to indicate whether this algorithm needs to compute
5959
/// solution error. The default return value is false.

ProcessLib/TimeLoop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ std::pair<NumLib::TimeIncrement, bool> TimeLoop::computeTimeStepping(
328328
ppd.timestep_current.setAccepted(
329329
ppd.nonlinear_solver_status.error_norms_met);
330330

331-
auto [previous_step_accepted, timestepper_dt] = timestep_algorithm.next(
331+
auto const timestepper_dt = timestep_algorithm.next(
332332
solution_error, ppd.nonlinear_solver_status.number_iterations,
333333
ppd.timestep_previous, ppd.timestep_current);
334334

335-
if (!previous_step_accepted)
335+
if (!ppd.timestep_current.isAccepted())
336336
{
337337
// Not all processes have accepted steps.
338338
all_process_steps_accepted = false;

Tests/NumLib/TestFixedTimeStepping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ TEST_F(NumLibFixedTimeStepping, next)
104104
NumLib::Time(0), NumLib::Time(t_initial), 0);
105105
for (auto const& expected_time_point : expected_time_points)
106106
{
107-
auto [is_next, step_size] =
107+
auto const step_size =
108108
fixed_time_stepping.next(0.0 /* solution_error */,
109109
0 /* number_of_iterations */,
110110
ts_dummy,
@@ -166,7 +166,7 @@ TEST_F(NumLibFixedTimeStepping, next_StaticTest)
166166
NumLib::TimeStep ts_current(NumLib::Time(0), NumLib::Time(t_initial), 0);
167167
for (auto const& expected_time_point : expected_time_points)
168168
{
169-
auto [is_next, step_size] =
169+
auto const step_size =
170170
fixed_time_stepping.next(0.0 /* solution_error */,
171171
0 /* number_of_iterations */,
172172
ts_dummy,

Tests/NumLib/TestTimeSteppingEvolutionaryPIDcontroller.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
5454
int const number_iterations = 0;
5555
// 1st step
5656

57-
auto [step_accepted, timestepper_dt] = PIDStepper->next(
57+
auto const timestepper_dt = PIDStepper->next(
5858
solution_error, number_iterations, previous_timestep, current_timestep);
5959

60-
ASSERT_TRUE(step_accepted);
60+
ASSERT_TRUE(current_timestep.isAccepted());
6161
NumLib::updateTimeSteps(timestepper_dt, previous_timestep,
6262
current_timestep);
6363

@@ -73,9 +73,9 @@ TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
7373

7474
// e_n_minus1 is filled.
7575
solution_error = 1.0e-4;
76-
auto [step_accepted1, timestepper_dt1] = PIDStepper->next(
77-
solution_error, number_iterations, previous_timestep, current_timestep);
78-
ASSERT_TRUE(step_accepted1);
76+
PIDStepper->next(solution_error, number_iterations, previous_timestep,
77+
current_timestep);
78+
ASSERT_TRUE(current_timestep.isAccepted());
7979
NumLib::updateTimeSteps(timestepper_dt, previous_timestep,
8080
current_timestep);
8181
/// ts = PIDStepper->getTimeStep();
@@ -89,9 +89,9 @@ TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
8989

9090
// e_n_minus2 is filled.
9191
solution_error = 0.5e-3;
92-
auto [step_accepted2, timestepper_dt2] = PIDStepper->next(
92+
auto timestepper_dt2 = PIDStepper->next(
9393
solution_error, number_iterations, previous_timestep, current_timestep);
94-
ASSERT_TRUE(step_accepted2);
94+
ASSERT_TRUE(current_timestep.isAccepted());
9595
NumLib::updateTimeSteps(timestepper_dt2, previous_timestep,
9696
current_timestep);
9797
/// ts = PIDStepper->getTimeStep();
@@ -104,9 +104,9 @@ TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
104104
// If error > solution_error, step is rejected and new step size is
105105
// estimated.
106106
solution_error = 0.01;
107-
auto [step_accepted3, timestepper_dt3] = PIDStepper->next(
108-
solution_error, number_iterations, previous_timestep, current_timestep);
109-
ASSERT_TRUE(!step_accepted3);
107+
PIDStepper->next(solution_error, number_iterations, previous_timestep,
108+
current_timestep);
109+
ASSERT_TRUE(!current_timestep.isAccepted());
110110
/// ts = PIDStepper->getTimeStep();
111111
h_new = current_timestep.dt();
112112
// No change in current_timestep.timeStepNumber
@@ -120,9 +120,9 @@ TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
120120

121121
// With e_n, e_n_minus1, e_n_minus2
122122
solution_error = 0.4e-3;
123-
auto [step_accepted4, timestepper_dt4] = PIDStepper->next(
123+
auto timestepper_dt4 = PIDStepper->next(
124124
solution_error, number_iterations, previous_timestep, current_timestep);
125-
ASSERT_TRUE(step_accepted4);
125+
ASSERT_TRUE(current_timestep.isAccepted());
126126
NumLib::updateTimeSteps(timestepper_dt4, previous_timestep,
127127
current_timestep);
128128
/// ts = PIDStepper->getTimeStep();

0 commit comments

Comments
 (0)