OGS
CreateNonlinearSolver.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <spdlog/fmt/ranges.h>
7
8#include <boost/algorithm/string.hpp>
9#include <memory>
10
11#include "BaseLib/ConfigTree.h"
12#include "BaseLib/Error.h"
16
17namespace NumLib
18{
19std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
21 BaseLib::ConfigTree const& config)
22{
24 auto const type = config.getConfigParameter<std::string>("type");
25
27 auto const max_iter = config.getConfigParameter<int>("max_iter");
28
29 if (type == "Picard")
30 {
31 auto const tag = NonlinearSolverTag::Picard;
32 using ConcreteNLS = NonlinearSolver<tag>;
33 return std::make_pair(
34 std::make_unique<ConcreteNLS>(linear_solver, max_iter), tag);
35 }
36 if (type == "Newton")
37 {
38 auto const recompute_jacobian =
40 config.getConfigParameter<int>("recompute_jacobian", 1);
41
43 auto const damping = config.getConfigParameter<double>("damping", 1.0);
44 if (damping <= 0)
45 {
47 "The damping factor for the Newton method must be positive, "
48 "got "
49 "{:g}.",
50 damping);
51 }
52 auto const damping_reduction =
54 config.getConfigParameterOptional<double>("damping_reduction");
55 std::unique_ptr<NewtonStepStrategy> standard_newton;
56 if (damping_reduction)
57 {
58 standard_newton = std::make_unique<DampingReductionStrategy>(
59 damping, *damping_reduction);
60 }
61 else
62 {
63 standard_newton = std::make_unique<FixedDampingStrategy>(damping);
64 }
65 auto const tag = NonlinearSolverTag::Newton;
66 using ConcreteNLS = NonlinearSolver<tag>;
67 return std::make_pair(
68 std::make_unique<ConcreteNLS>(linear_solver, max_iter,
69 std::move(standard_newton),
70 recompute_jacobian),
71 tag);
72 }
73#ifdef USE_PETSC
74 if (boost::iequals(type, "PETScSNES"))
75 {
76 auto prefix =
78 config.getConfigParameter<std::string>("prefix", "");
79 auto const tag = NonlinearSolverTag::Newton;
80 using ConcreteNLS = PETScNonlinearSolver;
81 return std::make_pair(std::make_unique<ConcreteNLS>(
82 linear_solver, max_iter, std::move(prefix)),
83 tag);
84 }
85 static constexpr std::array valid_types = {"PETScSNES", "Newton", "Picard"};
86#else
87 static constexpr std::array valid_types = {"Newton", "Picard"};
88#endif
89
90 OGS_FATAL("Invalid non-linear solver type '{}'. Supported values: {}.",
91 type, fmt::join(valid_types, ", "));
92}
93
94} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:19
MathLib::EigenLisLinearSolver GlobalLinearSolver
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
NonlinearSolverTag
Tag used to specify which nonlinear solver will be used.
Definition Types.h:13
std::pair< std::unique_ptr< NonlinearSolverBase >, NonlinearSolverTag > createNonlinearSolver(GlobalLinearSolver &linear_solver, BaseLib::ConfigTree const &config)