From cc6204360d6b59ee217b2815eb6ef7976155fceb Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Fri, 24 Jan 2020 15:21:56 +0100 Subject: [PATCH] [test][params] Add unit test for translating solver parameters to dune-istl format --- test/common/parameters/CMakeLists.txt | 6 +- test/common/parameters/params_solver.input | 13 +++ test/common/parameters/test_paramtranslate.cc | 105 ++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 test/common/parameters/params_solver.input create mode 100644 test/common/parameters/test_paramtranslate.cc diff --git a/test/common/parameters/CMakeLists.txt b/test/common/parameters/CMakeLists.txt index a69c776a62..00e3bd0075 100644 --- a/test/common/parameters/CMakeLists.txt +++ b/test/common/parameters/CMakeLists.txt @@ -1,3 +1,5 @@ dumux_add_test(SOURCES test_loggingparametertree.cc - LABELS unit) -dune_symlink_to_source_files(FILES "params.input") + LABELS unit) +dumux_add_test(SOURCES test_paramtranslate.cc + LABELS unit) +dune_symlink_to_source_files(FILES "params.input" "params_solver.input") diff --git a/test/common/parameters/params_solver.input b/test/common/parameters/params_solver.input new file mode 100644 index 0000000000..17f154e374 --- /dev/null +++ b/test/common/parameters/params_solver.input @@ -0,0 +1,13 @@ +[LinearSolver] +Type = cgsolver +ResidualReduction = 1e-8 + +[LinearSolver.Preconditioner] +Type = amg + +[SubProblem0.LinearSolver] +ResidualReduction = 1e-6 +Preconditioner.Type = ssor + +[SubProblem1.LinearSolver] +MaxIterations = 2000 diff --git a/test/common/parameters/test_paramtranslate.cc b/test/common/parameters/test_paramtranslate.cc new file mode 100644 index 0000000000..6864f3b1fa --- /dev/null +++ b/test/common/parameters/test_paramtranslate.cc @@ -0,0 +1,105 @@ +#include + +#include +#include + +#include +#include + +#include + +namespace Dumux { + +Dune::ParameterTree +extractAndTranslateParams(const std::string& paramGroup, + const std::vector>& translatedKeyList, + const std::string& keyPrefix = "", + const std::string& translatedKeyPrefix = "") +{ + Dune::ParameterTree translatedParams; + + const std::string emptyString{""}; + for (const auto& [key, translatedKey] : translatedKeyList) + { + // if key doesn't exist, empty string is returned + const auto prefixedKey = keyPrefix == "" ? key : keyPrefix + "." + key; + auto value = getParamFromGroup(paramGroup, prefixedKey, emptyString); + + // if value is not empty string -> translate and insert + if (value != emptyString) + { + const auto prefixedTranslatedKey = translatedKeyPrefix == "" ? translatedKey : translatedKeyPrefix + "." + translatedKey; + translatedParams[prefixedTranslatedKey] = std::move(value); + } + } + + return translatedParams; +} + +void checkParameter(const Dune::ParameterTree& params, const std::string& key, const std::string& value) +{ + const auto v = params.get(key); + if (v != value) + DUNE_THROW(Dune::Exception, "Key: " << key << " returns " << v << " but should return " << value); +} + +} // end namespace Dumux + +int main (int argc, char *argv[]) try +{ + using namespace Dumux; + + // maybe initialize mpi + Dune::MPIHelper::instance(argc, argv); + + // initialize parameter tree + Parameters::init(argc, argv, "params_solver.input"); + + // the parameter group of our solvers + std::string paramGroup0{"SubProblem0"}; + std::string paramGroup1{"SubProblem1"}; + + // a list of parameters to check and translate + std::vector> solverParamsDumuxToIstl { + {"Type", "type"}, + {"ResidualReduction", "reduction"}, + {"MaxIterations", "maxIter"}, + {"Preconditioner.Type", "preconditioner.type"} + }; + + // group prefix for linear solvers + const std::string solverPrefix{"LinearSolver"}; + + // create a configuration parameter tree for paramGroup0 + auto configParams0 = extractAndTranslateParams(paramGroup0, solverParamsDumuxToIstl, solverPrefix); + std::cout << "\ndune-istl solver config for SubProblem0:" + << "\n-----------------------------------------\n"; + configParams0.report(); + + // create a configuration parameter tree for paramGroup1 + auto configParams1 = extractAndTranslateParams(paramGroup1, solverParamsDumuxToIstl, solverPrefix); + std::cout << "\ndune-istl solver config for SubProblem1:" + << "\n-----------------------------------------\n"; + configParams1.report(); + + // test output + checkParameter(configParams0, "reduction", "1e-6"); + checkParameter(configParams0, "type", "cgsolver"); + checkParameter(configParams0, "preconditioner.type", "ssor"); + checkParameter(configParams1, "reduction", "1e-8"); + checkParameter(configParams1, "type", "cgsolver"); + checkParameter(configParams1, "preconditioner.type", "amg"); + checkParameter(configParams1, "maxIter", "2000"); + + std::cout << "\nConfig correct!" << std::endl; + + return 0; +} +// ////////////////////////////////// +// Error handler +// ///////////////////////////////// +catch (const Dune::Exception& e) +{ + std::cout << e << std::endl; + return 1; +} -- GitLab