From 29ae002a874edf036eeb3ffba4a35909bb00db78 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Wed, 9 Jun 2021 09:23:49 +0200 Subject: [PATCH] changed: avoid templating in setupPropertyTree simply pass the required params to function instead. --- CMakeLists_files.cmake | 1 - opm/simulators/linalg/ISTLSolverEbos.hpp | 5 +- .../linalg/ISTLSolverEbosFlexible.hpp | 5 +- opm/simulators/linalg/setupPropertyTree.cpp | 67 +++++++++++++ opm/simulators/linalg/setupPropertyTree.hpp | 8 +- .../linalg/setupPropertyTree_impl.hpp | 96 ------------------- 6 files changed, 79 insertions(+), 103 deletions(-) delete mode 100644 opm/simulators/linalg/setupPropertyTree_impl.hpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index b54f4087c..4e27779d2 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -266,7 +266,6 @@ list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/findOverlapRowsAndColumns.hpp opm/simulators/linalg/getQuasiImpesWeights.hpp opm/simulators/linalg/setupPropertyTree.hpp - opm/simulators/linalg/setupPropertyTree_impl.hpp opm/simulators/timestepping/AdaptiveSimulatorTimer.hpp opm/simulators/timestepping/AdaptiveTimeSteppingEbos.hpp opm/simulators/timestepping/ConvergenceReport.hpp diff --git a/opm/simulators/linalg/ISTLSolverEbos.hpp b/opm/simulators/linalg/ISTLSolverEbos.hpp index b8d707324..f176832ad 100644 --- a/opm/simulators/linalg/ISTLSolverEbos.hpp +++ b/opm/simulators/linalg/ISTLSolverEbos.hpp @@ -125,7 +125,10 @@ namespace Opm comm_.reset( new CommunicationType( simulator_.vanguard().grid().comm() ) ); #endif parameters_.template init(); - prm_ = setupPropertyTree(parameters_); + prm_ = setupPropertyTree(parameters_, + EWOMS_PARAM_IS_SET(TypeTag, int, LinearSolverMaxIter), + EWOMS_PARAM_IS_SET(TypeTag, int, CprMaxEllIter)); + #if HAVE_CUDA || HAVE_OPENCL || HAVE_FPGA { std::string accelerator_mode = EWOMS_GET_PARAM(TypeTag, std::string, AcceleratorMode); diff --git a/opm/simulators/linalg/ISTLSolverEbosFlexible.hpp b/opm/simulators/linalg/ISTLSolverEbosFlexible.hpp index d6994b781..4370fac14 100644 --- a/opm/simulators/linalg/ISTLSolverEbosFlexible.hpp +++ b/opm/simulators/linalg/ISTLSolverEbosFlexible.hpp @@ -99,7 +99,10 @@ public: , interiorCellNum_(detail::numMatrixRowsToUseInSolver(simulator_.vanguard().grid(), ownersFirst_)) { parameters_.template init(); - prm_ = setupPropertyTree(parameters_); + prm_ = setupPropertyTree(parameters_, + EWOMS_PARAM_IS_SET(TypeTag, int, LinearSolverMaxIter), + EWOMS_PARAM_IS_SET(TypeTag, int, CprMaxEllIter)); + extractParallelGridInformationToISTL(simulator_.vanguard().grid(), parallelInformation_); // For some reason simulator_.model().elementMapper() is not initialized at this stage // Hence const auto& elemMapper = simulator_.model().elementMapper(); does not work. diff --git a/opm/simulators/linalg/setupPropertyTree.cpp b/opm/simulators/linalg/setupPropertyTree.cpp index 209324319..67ea84c67 100644 --- a/opm/simulators/linalg/setupPropertyTree.cpp +++ b/opm/simulators/linalg/setupPropertyTree.cpp @@ -21,12 +21,79 @@ #include +#include + #include #include namespace Opm { +/// Set up a property tree intended for FlexibleSolver by either reading +/// the tree from a JSON file or creating a tree giving the default solver +/// and preconditioner. If the latter, the parameters --linear-solver-reduction, +/// --linear-solver-maxiter and --linear-solver-verbosity are used, but if reading +/// from file the data in the JSON file will override any other options. +boost::property_tree::ptree +setupPropertyTree(FlowLinearSolverParameters p, // Note: copying the parameters to potentially override. + bool LinearSolverMaxIterSet, + bool CprMaxEllIterSet) +{ + std::string conf = p.linsolver_; + + // Get configuration from file. + if (conf.size() > 5 && conf.substr(conf.size() - 5, 5) == ".json") { // the ends_with() method is not available until C++20 +#if BOOST_VERSION / 100 % 1000 > 48 + if ( !filesystem::exists(conf) ) { + OPM_THROW(std::invalid_argument, "JSON file " << conf << " does not exist."); + } + try { + boost::property_tree::ptree prm; + boost::property_tree::read_json(conf, prm); + return prm; + } + catch (...) { + OPM_THROW(std::invalid_argument, "Failed reading linear solver configuration from JSON file " << conf); + } +#else + OPM_THROW(std::invalid_argument, + "--linear-solver-configuration=file.json not supported with " + << "boost version. Needs version > 1.48."); +#endif + } + + // Use CPR configuration. + if ((conf == "cpr") || (conf == "cpr_trueimpes") || (conf == "cpr_quasiimpes")) { + if (conf == "cpr") { + // Treat "cpr" as short cut for the true IMPES variant. + conf = "cpr_trueimpes"; + } + if (!LinearSolverMaxIterSet) { + // Use our own default unless it was explicitly overridden by user. + p.linear_solver_maxiter_ = 20; + } + if (!CprMaxEllIterSet) { + // Use our own default unless it was explicitly overridden by user. + p.cpr_max_ell_iter_ = 1; + } + return setupCPR(conf, p); + } + + if (conf == "amg") { + return setupAMG(conf, p); + } + + // Use ILU0 configuration. + if (conf == "ilu0") { + return setupILU(conf, p); + } + + // No valid configuration option found. + OPM_THROW(std::invalid_argument, + conf << " is not a valid setting for --linear-solver-configuration." + << " Please use ilu0, cpr, cpr_trueimpes, or cpr_quasiimpes"); +} + boost::property_tree::ptree setupCPR(const std::string& conf, const FlowLinearSolverParameters& p) { diff --git a/opm/simulators/linalg/setupPropertyTree.hpp b/opm/simulators/linalg/setupPropertyTree.hpp index 688ac1e90..fed1ebf66 100644 --- a/opm/simulators/linalg/setupPropertyTree.hpp +++ b/opm/simulators/linalg/setupPropertyTree.hpp @@ -23,12 +23,14 @@ #include #include +#include namespace Opm { -template -boost::property_tree::ptree setupPropertyTree(FlowLinearSolverParameters p); +boost::property_tree::ptree setupPropertyTree(FlowLinearSolverParameters p, + bool LinearSolverMaxIterSet, + bool CprMaxEllIterSet); boost::property_tree::ptree setupCPR(const std::string& conf, const FlowLinearSolverParameters& p); boost::property_tree::ptree setupAMG(const std::string& conf, const FlowLinearSolverParameters& p); @@ -36,6 +38,4 @@ boost::property_tree::ptree setupILU(const std::string& conf, const FlowLinearSo } // namespace Opm -#include "setupPropertyTree_impl.hpp" - #endif // OPM_SETUPPROPERTYTREE_HEADER_INCLUDED diff --git a/opm/simulators/linalg/setupPropertyTree_impl.hpp b/opm/simulators/linalg/setupPropertyTree_impl.hpp deleted file mode 100644 index 558ff9ca8..000000000 --- a/opm/simulators/linalg/setupPropertyTree_impl.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - Copyright 2019 SINTEF Digital, Mathematics and Cybernetics. - - This file is part of the Open Porous Media project (OPM). - - OPM is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OPM is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with OPM. If not, see . -*/ - -#include - -#include -#include - -#include - -namespace Opm -{ - -/// Set up a property tree intended for FlexibleSolver by either reading -/// the tree from a JSON file or creating a tree giving the default solver -/// and preconditioner. If the latter, the parameters --linear-solver-reduction, -/// --linear-solver-maxiter and --linear-solver-verbosity are used, but if reading -/// from file the data in the JSON file will override any other options. -template -boost::property_tree::ptree -setupPropertyTree(FlowLinearSolverParameters p) // Note: copying the parameters to potentially override. -{ - std::string conf = p.linsolver_; - - // Get configuration from file. - if (conf.size() > 5 && conf.substr(conf.size() - 5, 5) == ".json") { // the ends_with() method is not available until C++20 -#if BOOST_VERSION / 100 % 1000 > 48 - if ( !filesystem::exists(conf) ) { - OPM_THROW(std::invalid_argument, "JSON file " << conf << " does not exist."); - } - try { - boost::property_tree::ptree prm; - boost::property_tree::read_json(conf, prm); - return prm; - } - catch (...) { - OPM_THROW(std::invalid_argument, "Failed reading linear solver configuration from JSON file " << conf); - } -#else - OPM_THROW(std::invalid_argument, - "--linear-solver-configuration=file.json not supported with " - << "boost version. Needs version > 1.48."); -#endif - } - - // Use CPR configuration. - if ((conf == "cpr") || (conf == "cpr_trueimpes") || (conf == "cpr_quasiimpes")) { - if (conf == "cpr") { - // Treat "cpr" as short cut for the true IMPES variant. - conf = "cpr_trueimpes"; - } - if (!EWOMS_PARAM_IS_SET(TypeTag, int, LinearSolverMaxIter)) { - // Use our own default unless it was explicitly overridden by user. - p.linear_solver_maxiter_ = 20; - } - if (!EWOMS_PARAM_IS_SET(TypeTag, int, CprMaxEllIter)) { - // Use our own default unless it was explicitly overridden by user. - p.cpr_max_ell_iter_ = 1; - } - return setupCPR(conf, p); - } - - if (conf == "amg") { - return setupAMG(conf, p); - } - - // Use ILU0 configuration. - if (conf == "ilu0") { - return setupILU(conf, p); - } - - // No valid configuration option found. - OPM_THROW(std::invalid_argument, - conf << " is not a valid setting for --linear-solver-configuration." - << " Please use ilu0, cpr, cpr_trueimpes, or cpr_quasiimpes"); -} - - - -} // namespace Opm