Skip to content

Commit

Permalink
Merge branch 'ImproveErrorMessageInTimeLoopParsing' into 'master'
Browse files Browse the repository at this point in the history
[TimeLoop] Improve error message if end time is smaller than initial time

See merge request ogs/ogs!5140
  • Loading branch information
endJunction committed Oct 23, 2024
2 parents f5f4d06 + 27e0524 commit ebd40a7
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 35 deletions.
39 changes: 28 additions & 11 deletions NumLib/TimeStepping/Algorithms/CreateEvolutionaryPIDcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
namespace NumLib
{
class TimeStepAlgorithm;
std::unique_ptr<TimeStepAlgorithm> createEvolutionaryPIDcontroller(
BaseLib::ConfigTree const& config,
std::vector<double> const& fixed_times_for_output)

EvolutionaryPIDcontrollerParameters parseEvolutionaryPIDcontroller(
BaseLib::ConfigTree const& config)
{
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__type}
config.checkConfigParameter("type", "EvolutionaryPIDcontroller");
Expand All @@ -30,6 +30,7 @@ std::unique_ptr<TimeStepAlgorithm> createEvolutionaryPIDcontroller(
auto const t0 = config.getConfigParameter<double>("t_initial");
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__EvolutionaryPIDcontroller__t_end}
auto const t_end = config.getConfigParameter<double>("t_end");

//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__EvolutionaryPIDcontroller__dt_guess}
auto const h0 = config.getConfigParameter<double>("dt_guess");

Expand All @@ -45,14 +46,30 @@ std::unique_ptr<TimeStepAlgorithm> createEvolutionaryPIDcontroller(
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__EvolutionaryPIDcontroller__tol}
auto const tol = config.getConfigParameter<double>("tol");

return std::make_unique<EvolutionaryPIDcontroller>(t0,
t_end,
h0,
h_min,
h_max,
rel_h_min,
rel_h_max,
tol,
return {t0, t_end, h0, h_min, h_max, rel_h_min, rel_h_max, tol};
}

std::unique_ptr<TimeStepAlgorithm> createEvolutionaryPIDcontroller(
EvolutionaryPIDcontrollerParameters const& config,
std::vector<double> const& fixed_times_for_output)
{
if (config.t_end < config.t0)
{
OGS_FATAL(
"Evolutionary PID controller timestepping: end time ({}) is "
"smaller than initial time ({})",
config.t_end,
config.t0);
}

return std::make_unique<EvolutionaryPIDcontroller>(config.t0,
config.t_end,
config.h0,
config.h_min,
config.h_max,
config.rel_h_min,
config.rel_h_max,
config.tol,
fixed_times_for_output);
}
} // end of namespace NumLib
19 changes: 18 additions & 1 deletion NumLib/TimeStepping/Algorithms/CreateEvolutionaryPIDcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ namespace NumLib
{
class TimeStepAlgorithm;

struct EvolutionaryPIDcontrollerParameters final
{
double t0;
double t_end;
double h0;
double h_min;
double h_max;
double rel_h_min;
double rel_h_max;
double tol;
};

/// Parse an EvolutionaryPIDcontroller time stepper from the given
/// configuration
EvolutionaryPIDcontrollerParameters parseEvolutionaryPIDcontroller(
BaseLib::ConfigTree const& config);

/// Create an EvolutionaryPIDcontroller time stepper from the given
/// configuration
std::unique_ptr<TimeStepAlgorithm> createEvolutionaryPIDcontroller(
BaseLib::ConfigTree const& config,
EvolutionaryPIDcontrollerParameters const& config,
std::vector<double> const& fixed_times_for_output);
} // end of namespace NumLib
30 changes: 24 additions & 6 deletions NumLib/TimeStepping/Algorithms/CreateFixedTimeStepping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

namespace NumLib
{
std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
BaseLib::ConfigTree const& config,
std::vector<double> const& fixed_times_for_output)
FixedTimeSteppingParameters parseFixedTimeStepping(
BaseLib::ConfigTree const& config)
{
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__type}
config.checkConfigParameter("type", "FixedTimeStepping");
Expand Down Expand Up @@ -49,14 +48,33 @@ std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__FixedTimeStepping__timesteps__pair__delta_t}
pair.getConfigParameter<double>("delta_t"));
}
if (!FixedTimeStepping::areRepeatDtPairsValid(repeat_dt_pairs))

return {t_initial, t_end, repeat_dt_pairs};
}

std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
FixedTimeSteppingParameters const& parameters,
std::vector<double> const& fixed_times_for_output)
{
if (parameters.t_end < parameters.t_initial)
{
OGS_FATAL(
"fixed timestepping: end time ({}) is smaller than initial time "
"({})",
parameters.t_end,
parameters.t_initial);
}

if (!FixedTimeStepping::areRepeatDtPairsValid(parameters.repeat_dt_pairs))
{
OGS_FATAL(
"CreateFixedTimeStepping: invalid specification of (repeat, "
"delta_t) pairs");
}

return std::make_unique<FixedTimeStepping>(
t_initial, t_end, repeat_dt_pairs, fixed_times_for_output);
return std::make_unique<FixedTimeStepping>(parameters.t_initial,
parameters.t_end,
parameters.repeat_dt_pairs,
fixed_times_for_output);
}
} // end of namespace NumLib
16 changes: 15 additions & 1 deletion NumLib/TimeStepping/Algorithms/CreateFixedTimeStepping.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <memory>
#include <vector>

#include "FixedTimeStepping.h"

namespace BaseLib
{
class ConfigTree;
Expand All @@ -22,9 +24,21 @@ class ConfigTree;
namespace NumLib
{
class TimeStepAlgorithm;

struct FixedTimeSteppingParameters final
{
double t_initial;
double t_end;
std::vector<RepeatDtPair> repeat_dt_pairs;
};

/// Create a FixedTimeStepping time stepper from the given
/// configuration
FixedTimeSteppingParameters parseFixedTimeStepping(
BaseLib::ConfigTree const& config);

std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
BaseLib::ConfigTree const& config,
FixedTimeSteppingParameters const& parameters,
std::vector<double> const& fixed_times_for_output);

} // end of namespace NumLib
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

namespace NumLib
{
class TimeStepAlgorithm;
std::unique_ptr<TimeStepAlgorithm> createIterationNumberBasedTimeStepping(
BaseLib::ConfigTree const& config,
std::vector<double> const& fixed_times_for_output)

IterationNumberBasedTimeSteppingParameters
parseIterationNumberBasedTimeStepping(BaseLib::ConfigTree const& config)
{
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__type}
config.checkConfigParameter("type", "IterationNumberBasedTimeStepping");
Expand All @@ -30,6 +29,15 @@ std::unique_ptr<TimeStepAlgorithm> createIterationNumberBasedTimeStepping(
auto const t_initial = config.getConfigParameter<double>("t_initial");
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__IterationNumberBasedTimeStepping__t_end}
auto const t_end = config.getConfigParameter<double>("t_end");
if (t_end < t_initial)
{
OGS_FATAL(
"iteration number based timestepping: t_end({}) is smaller than "
"t_initial({})",
t_end,
t_initial);
}

//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__IterationNumberBasedTimeStepping__initial_dt}
auto const initial_dt = config.getConfigParameter<double>("initial_dt");
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__IterationNumberBasedTimeStepping__minimum_dt}
Expand All @@ -44,9 +52,35 @@ std::unique_ptr<TimeStepAlgorithm> createIterationNumberBasedTimeStepping(
//! \ogs_file_param{prj__time_loop__processes__process__time_stepping__IterationNumberBasedTimeStepping__multiplier}
config.getConfigParameter<std::vector<double>>("multiplier");

return {t_initial,
t_end,
minimum_dt,
maximum_dt,
initial_dt,
std::move(number_iterations),
std::move(multiplier)};
}

/// Create a IterationNumberBasedTimeStepping time stepper from the given
/// configuration.
std::unique_ptr<TimeStepAlgorithm> createIterationNumberBasedTimeStepping(
IterationNumberBasedTimeSteppingParameters&& parameters,
std::vector<double> const& fixed_times_for_output)
{
if (parameters.t_end < parameters.t_initial)
{
OGS_FATAL(
"iteration number based timestepping: end time ({}) is smaller "
"than initial time ({})",
parameters.t_end,
parameters.t_initial);
}

return std::make_unique<IterationNumberBasedTimeStepping>(
t_initial, t_end, minimum_dt, maximum_dt, initial_dt,
std::move(number_iterations), std::move(multiplier),
fixed_times_for_output);
parameters.t_initial, parameters.t_end, parameters.minimum_dt,
parameters.maximum_dt, parameters.initial_dt,
std::move(parameters.number_iterations),
std::move(parameters.multiplier), fixed_times_for_output);
}

} // namespace NumLib
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ class ConfigTree;
namespace NumLib
{
class TimeStepAlgorithm;
}

namespace NumLib
struct IterationNumberBasedTimeSteppingParameters final
{
double t_initial;
double t_end;
double minimum_dt;
double maximum_dt;
double initial_dt;
std::vector<int> number_iterations;
std::vector<double> multiplier;
};

IterationNumberBasedTimeSteppingParameters
parseIterationNumberBasedTimeStepping(BaseLib::ConfigTree const& config);

/// Create a IterationNumberBasedTimeStepping time stepper from the given
/// configuration.
std::unique_ptr<TimeStepAlgorithm> createIterationNumberBasedTimeStepping(
BaseLib::ConfigTree const& config,
IterationNumberBasedTimeSteppingParameters&& parameters,
std::vector<double> const& fixed_times_for_output);
} // namespace NumLib
10 changes: 6 additions & 4 deletions NumLib/TimeStepping/CreateTimeStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ std::unique_ptr<TimeStepAlgorithm> createTimeStepper(
}
if (type == "FixedTimeStepping")
{
return NumLib::createFixedTimeStepping(config, fixed_times_for_output);
return NumLib::createFixedTimeStepping(parseFixedTimeStepping(config),
fixed_times_for_output);
}
if (type == "EvolutionaryPIDcontroller")
{
return NumLib::createEvolutionaryPIDcontroller(config,
fixed_times_for_output);
return NumLib::createEvolutionaryPIDcontroller(
parseEvolutionaryPIDcontroller(config), fixed_times_for_output);
}
if (type == "IterationNumberBasedTimeStepping")
{
return NumLib::createIterationNumberBasedTimeStepping(
config, fixed_times_for_output);
parseIterationNumberBasedTimeStepping(config),
fixed_times_for_output);
}
OGS_FATAL(
"Unknown time stepping type: '{:s}'. The available types are: "
Expand Down
2 changes: 1 addition & 1 deletion ProcessLib/TimeLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TimeLoop

NumLib::Time endTime() const { return _end_time; }
NumLib::Time currentTime() const { return _current_time; }
bool successful_time_step = false;
bool successful_time_step = true;

private:
bool preTsNonlinearSolvePostTs(NumLib::Time const& t, double const dt,
Expand Down
30 changes: 30 additions & 0 deletions Tests/NumLib/CreateEvolutionaryPIDcontroller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* \file
* \copyright
* Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/

#include "NumLib/TimeStepping/Algorithms/CreateEvolutionaryPIDcontroller.h"

#include <gtest/gtest.h>

#include <numeric>

#include "NumLib/TimeStepping/Algorithms/TimeStepAlgorithm.h"

TEST(NumLibCreateEvolutionaryPIDcontrollerTimeStepping,
InitialTimeGreaterThanEndTime)
{
double const t_initial = 1;
double const t_end = 0;

NumLib::EvolutionaryPIDcontrollerParameters parameters{
t_initial, t_end, 0, 0, 0, 0, 0, 0};
EXPECT_ANY_THROW(
auto fixed_time_step_algorithm =
NumLib::createEvolutionaryPIDcontroller(std::move(parameters), {}));
}
27 changes: 27 additions & 0 deletions Tests/NumLib/CreateFixedTimeStepping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* \file
* \copyright
* Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/

#include "NumLib/TimeStepping/Algorithms/CreateFixedTimeStepping.h"

#include <gtest/gtest.h>

#include <numeric>

TEST(NumLibCreateFixedTimeStepping, InitialTimeGreaterThanEndTime)
{
double const t_initial = 1;
double const t_end = 0;
std::vector<NumLib::RepeatDtPair> const repeat_dt_pair;

NumLib::FixedTimeSteppingParameters const parameters{t_initial, t_end,
repeat_dt_pair};
EXPECT_ANY_THROW(auto fixed_time_step_algorithm =
NumLib::createFixedTimeStepping(parameters, {}));
}
30 changes: 30 additions & 0 deletions Tests/NumLib/CreateIterationNumberBasedTimeStepping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* \file
* \copyright
* Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/

#include "NumLib/TimeStepping/Algorithms/CreateIterationNumberBasedTimeStepping.h"

#include <gtest/gtest.h>

#include <numeric>

#include "NumLib/TimeStepping/Algorithms/TimeStepAlgorithm.h"

TEST(NumLibCreateIterationNumberBasedTimeStepping,
InitialTimeGreaterThanEndTime)
{
double const t_initial = 1;
double const t_end = 0;

NumLib::IterationNumberBasedTimeSteppingParameters parameters{
t_initial, t_end, 0, 0, 0, {}, {}};
EXPECT_ANY_THROW(auto fixed_time_step_algorithm =
NumLib::createIterationNumberBasedTimeStepping(
std::move(parameters), {}));
}
3 changes: 2 additions & 1 deletion Tests/NumLib/TestTimeSteppingEvolutionaryPIDcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ std::unique_ptr<NumLib::TimeStepAlgorithm> createTestTimeStepper(
BaseLib::ConfigTree conf(std::move(ptree), "", BaseLib::ConfigTree::onerror,
BaseLib::ConfigTree::onwarning);
auto const& sub_config = conf.getConfigSubtree("time_stepping");
return NumLib::createEvolutionaryPIDcontroller(sub_config, {});
return NumLib::createEvolutionaryPIDcontroller(
NumLib::parseEvolutionaryPIDcontroller(sub_config), {});
}

TEST(NumLibTimeStepping, testEvolutionaryPIDcontroller)
Expand Down

0 comments on commit ebd40a7

Please sign in to comment.