Skip to content

Commit

Permalink
Merge branch 'UseTimeInsteadDouble' into 'master'
Browse files Browse the repository at this point in the history
Use data struct Time instead a double for time

See merge request ogs/ogs!5030
  • Loading branch information
endJunction committed Aug 30, 2024
2 parents afc3f62 + bdb633b commit d3713ce
Show file tree
Hide file tree
Showing 101 changed files with 1,095 additions and 1,377 deletions.
4 changes: 2 additions & 2 deletions Applications/ApplicationsLib/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ void Simulation::initializeDataStructures(
double Simulation::currentTime() const
{
auto const& time_loop = project_data->getTimeLoop();
return time_loop.currentTime();
return time_loop.currentTime()();
}

double Simulation::endTime() const
{
auto const& time_loop = project_data->getTimeLoop();
return time_loop.endTime();
return time_loop.endTime()();
}

bool Simulation::executeTimeStep()
Expand Down
13 changes: 7 additions & 6 deletions NumLib/TimeStepping/Algorithms/EvolutionaryPIDcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,26 @@ double EvolutionaryPIDcontroller::limitStepSize(
return limited_h;
}

// find first fixed timestep for output larger than the current time, i.e.,
// find first fixed output time larger than the current time, i.e.,
// current time < fixed output time
auto fixed_output_time_it = std::find_if(
std::begin(_fixed_times_for_output), std::end(_fixed_times_for_output),
[&timestep_current](auto const fixed_output_time)
{ return timestep_current.current() < fixed_output_time; });
{ return timestep_current.current() < Time{fixed_output_time}; });

if (fixed_output_time_it != _fixed_times_for_output.end())
{
// check if the fixed output time is in the interval
// (current time, current time + limited_h)
if (*fixed_output_time_it < timestep_current.current() + limited_h)
if (Time{*fixed_output_time_it} <
timestep_current.current() + limited_h)
{
// check if the potential adjusted time step is larger than zero
if (std::abs(*fixed_output_time_it - timestep_current.current()) >
if (std::abs(*fixed_output_time_it - timestep_current.current()()) >
std::numeric_limits<double>::epsilon() *
timestep_current.current())
timestep_current.current()())
{
return *fixed_output_time_it - timestep_current.current();
return *fixed_output_time_it - timestep_current.current()();
}
}
}
Expand Down
60 changes: 33 additions & 27 deletions NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
#include <limits>
#include <numeric>

#include "NumLib/TimeStepping/Time.h"

namespace
{
/// Returns sum of the newly added time increments.
double addTimeIncrement(std::vector<double>& delta_ts, std::size_t const repeat,
double const delta_t, double const t_curr)
NumLib::Time addTimeIncrement(std::vector<double>& delta_ts,
std::size_t const repeat, double const delta_t,
NumLib::Time const t_curr)
{
auto const new_size = delta_ts.size() + repeat;
try
Expand All @@ -48,9 +51,9 @@ double addTimeIncrement(std::vector<double>& delta_ts, std::size_t const repeat,

namespace NumLib
{
std::size_t findDeltatInterval(double const t_initial,
std::size_t findDeltatInterval(Time const& t_initial,
std::vector<double> const& delta_ts,
double const fixed_output_time)
Time const& fixed_output_time)
{
if (fixed_output_time < t_initial)
{
Expand All @@ -72,35 +75,39 @@ std::size_t findDeltatInterval(double const t_initial,
}

void incorporateFixedTimesForOutput(
double const t_initial, double const t_end, std::vector<double>& delta_ts,
NumLib::Time const t_initial, NumLib::Time const t_end,
std::vector<double>& delta_ts,
std::vector<double> const& fixed_times_for_output)
{
if (fixed_times_for_output.empty())
{
return;
}

if (auto lower_bound =
std::lower_bound(begin(fixed_times_for_output),
end(fixed_times_for_output), t_initial);
if (auto lower_bound = std::lower_bound(
begin(fixed_times_for_output), end(fixed_times_for_output),
t_initial,
[](auto const time, NumLib::Time const& initial_time)
{ return NumLib::Time(time) < initial_time; });
lower_bound != begin(fixed_times_for_output))
{
WARN(
"Request for output at times {}, but the simulation's start time "
"is {}. Output will be skipped.",
fmt::join(begin(fixed_times_for_output), lower_bound, ", "),
t_initial);
t_initial());
}

if (auto upper_bound = std::upper_bound(begin(fixed_times_for_output),
end(fixed_times_for_output), t_end);
if (auto upper_bound =
std::upper_bound(begin(fixed_times_for_output),
end(fixed_times_for_output), t_end());
upper_bound != end(fixed_times_for_output))
{
WARN(
"Request for output at times {}, but simulation's end time is {}. "
"Output will be skipped.",
fmt::join(upper_bound, end(fixed_times_for_output), ", "),
t_end);
t_end());
}

if (delta_ts.empty())
Expand All @@ -112,31 +119,31 @@ void incorporateFixedTimesForOutput(
// incorporate fixed output times into dts vector
for (auto const fixed_time_for_output : fixed_times_for_output)
{
auto const interval_number =
findDeltatInterval(t_initial, delta_ts, fixed_time_for_output);
auto const interval_number = findDeltatInterval(
t_initial, delta_ts, Time(fixed_time_for_output));
if (interval_number == std::numeric_limits<std::size_t>::max())
{
WARN("Did not find interval for fixed output time {}",
fixed_time_for_output);
continue;
}

auto const lower_bound = std::accumulate(
begin(delta_ts), begin(delta_ts) + interval_number, t_initial);
auto const upper_bound = lower_bound + delta_ts[interval_number];
if (fixed_time_for_output - lower_bound <=
TimeStep::minimalTimeStepSize)
// in order to use the comparison from struct Time
if (NumLib::Time(fixed_time_for_output) == lower_bound)
{
continue;
}
if (upper_bound - fixed_time_for_output <=
TimeStep::minimalTimeStepSize)
if (upper_bound == NumLib::Time(fixed_time_for_output))
{
continue;
}
delta_ts[interval_number] = fixed_time_for_output - lower_bound;
delta_ts[interval_number] = fixed_time_for_output - lower_bound();

delta_ts.insert(delta_ts.begin() + interval_number + 1,
upper_bound - fixed_time_for_output);
upper_bound() - fixed_time_for_output);
}
}

Expand All @@ -145,7 +152,7 @@ FixedTimeStepping::FixedTimeStepping(
std::vector<double> const& fixed_times_for_output)
: TimeStepAlgorithm(t0, tn)
{
double t_curr = _t_initial;
Time t_curr = _t_initial;

if (!areRepeatDtPairsValid(repeat_dt_pairs))
{
Expand All @@ -163,8 +170,8 @@ FixedTimeStepping::FixedTimeStepping(
if (t_curr <= _t_end)
{
auto const delta_t = std::get<1>(repeat_dt_pairs.back());
auto const repeat =
static_cast<std::size_t>(std::ceil((_t_end - t_curr) / delta_t));
auto const repeat = static_cast<std::size_t>(
std::ceil((_t_end() - t_curr()) / delta_t));
addTimeIncrement(_dt_vector, repeat, delta_t, t_curr);
}

Expand Down Expand Up @@ -210,16 +217,15 @@ std::tuple<bool, double> FixedTimeStepping::next(
{
// check if last time step
if (ts_current.timeStepNumber() == _dt_vector.size() ||
std::abs(ts_current.current() - end()) <
std::numeric_limits<double>::epsilon())
ts_current.current() >= end())
{
return std::make_tuple(false, 0.0);
return std::make_tuple(true, 0.0);
}

double dt = _dt_vector[ts_current.timeStepNumber()];
if (ts_current.current() + dt > end())
{ // upper bound by t_end
dt = end() - ts_current.current();
dt = end()() - ts_current.current()();
}

return std::make_tuple(true, dt);
Expand Down
4 changes: 2 additions & 2 deletions NumLib/TimeStepping/Algorithms/FixedTimeStepping.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FixedTimeStepping final : public TimeStepAlgorithm
std::vector<double> _dt_vector;
};

std::size_t findDeltatInterval(double const t_initial,
std::size_t findDeltatInterval(Time const& t_initial,
std::vector<double> const& delta_ts,
double const fixed_output_time);
Time const& fixed_output_time);
} // namespace NumLib
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ double IterationNumberBasedTimeStepping::getNextTimeStepSize(
auto fixed_output_time_it = std::find_if(
std::begin(_fixed_times_for_output), std::end(_fixed_times_for_output),
[&ts_current](auto const fixed_output_time)
{ return ts_current.current() < fixed_output_time; });
{ return ts_current.current()() < fixed_output_time; });

if (fixed_output_time_it != _fixed_times_for_output.end())
{
// check if the fixed output time is in the interval
// (current time, current time + dt)
if (*fixed_output_time_it < ts_current.current() + dt)
if (*fixed_output_time_it < ts_current.current()() + dt)
{
// check if the potential adjusted time step is larger than zero
if (std::abs(*fixed_output_time_it - ts_current.current()) >
std::numeric_limits<double>::epsilon() * ts_current.current())
if (std::abs(*fixed_output_time_it - ts_current.current()()) >
std::numeric_limits<double>::epsilon() * ts_current.current()())
{
return *fixed_output_time_it - ts_current.current();
return *fixed_output_time_it - ts_current.current()();
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions NumLib/TimeStepping/Algorithms/TimeStepAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@
#include <algorithm>
#include <limits>

#include "NumLib/TimeStepping/Time.h"

namespace NumLib
{
double possiblyClampDtToNextFixedTime(
double const t, double const dt,
Time const& t, double const dt,
std::vector<double> const& fixed_output_times)
{
auto const specific_time = std::upper_bound(
std::cbegin(fixed_output_times), std::cend(fixed_output_times), t);
std::cbegin(fixed_output_times), std::cend(fixed_output_times), t());

if (specific_time == std::cend(fixed_output_times))
{
return dt;
}

double const t_to_specific_time = *specific_time - t;
if ((t_to_specific_time > std::numeric_limits<double>::epsilon()) &&
(t + dt - *specific_time > 0.0))
if ((t < Time(*specific_time)) && t + dt > Time(*specific_time))
{
double const t_to_specific_time = *specific_time - t();
return t_to_specific_time;
}

Expand Down
12 changes: 7 additions & 5 deletions NumLib/TimeStepping/Algorithms/TimeStepAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace NumLib
{
struct Time;

/**
* \brief Interface of time stepping algorithms
*/
Expand All @@ -35,9 +37,9 @@ class TimeStepAlgorithm
virtual ~TimeStepAlgorithm() = default;

/// return the beginning of time steps
double begin() const { return _t_initial; }
Time begin() const { return _t_initial; }
/// return the end of time steps
double end() const { return _t_end; }
Time end() const { return _t_end; }
/// reset the current step size from the previous time
virtual void resetCurrentTimeStep(const double /*dt*/,
TimeStep& /*ts_previous*/,
Expand Down Expand Up @@ -73,9 +75,9 @@ class TimeStepAlgorithm

protected:
/// initial time
const double _t_initial;
const Time _t_initial;
/// end time
const double _t_end;
const Time _t_end;
};

/// If any of the fixed times will be reached with given time increment, it will
Expand All @@ -85,7 +87,7 @@ class TimeStepAlgorithm
/// \param dt Suggested time increment.
/// \param fixed_output_times Sorted list of times which are to be reached.
double possiblyClampDtToNextFixedTime(
double const t, double const dt,
Time const& t, double const dt,
std::vector<double> const& fixed_output_times);

bool canReduceTimestepSize(TimeStep const& timestep_previous,
Expand Down
Loading

0 comments on commit d3713ce

Please sign in to comment.