opm-simulators/opm/simulators/linalg/setupPropertyTree_impl.hpp

97 lines
3.4 KiB
C++
Raw Normal View History

/*
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 <http://www.gnu.org/licenses/>.
*/
#include <opm/simulators/linalg/setupPropertyTree.hpp>
2020-02-17 07:10:01 -06:00
#include <boost/version.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <opm/common/utility/FileSystem.hpp>
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<class TypeTag>
boost::property_tree::ptree
2020-10-11 02:24:05 -05:00
setupPropertyTree(FlowLinearSolverParameters p) // Note: copying the parameters to potentially override.
{
std::string conf = p.linsolver_;
2020-10-11 02:24:05 -05:00
// 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
2020-02-17 07:10:01 -06:00
#if BOOST_VERSION / 100 % 1000 > 48
if ( !filesystem::exists(conf) ) {
OPM_THROW(std::invalid_argument, "JSON file " << conf << " does not exist.");
}
try {
2020-10-11 02:24:05 -05:00
boost::property_tree::ptree prm;
boost::property_tree::read_json(conf, prm);
2020-10-11 02:24:05 -05:00
return prm;
}
catch (...) {
OPM_THROW(std::invalid_argument, "Failed reading linear solver configuration from JSON file " << conf);
}
#else
2020-10-11 02:24:05 -05:00
OPM_THROW(std::invalid_argument,
"--linear-solver-configuration=file.json not supported with "
2020-10-11 02:24:05 -05:00
<< "boost version. Needs version > 1.48.");
2020-02-17 07:10:01 -06:00
#endif
}
2020-10-11 02:24:05 -05:00
// 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;
}
2020-10-11 02:24:05 -05:00
return setupCPR(conf, p);
}
2020-10-11 02:24:05 -05:00
if (conf == "amg") {
return setupAMG(conf, p);
}
2020-10-11 02:24:05 -05:00
// 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");
}
2020-10-11 02:24:05 -05:00
} // namespace Opm