version 0.4.2
newtonraphson.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
18
19namespace Ikarus {
20
21template <typename NLO, typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault>
22class NewtonRaphson;
23
25{
26 double tol{1e-8};
27 int maxIter{20};
28};
29
34template <typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault>
36{
37 using LinearSolver = LS;
38 using UpdateFunction = UF;
42
43 template <typename UF2>
46 .parameters = parameters, .linearSolver = linearSolver, .updateFunction = std::forward<UF2>(updateFunction)};
47 return settings;
48 }
49
50 template <typename NLO>
52};
53
62template <typename NLO, typename NRConfig>
63requires traits::isSpecialization<NewtonRaphsonConfig, std::remove_cvref_t<NRConfig>>::value
64auto createNonlinearSolver(NRConfig&& config, NLO&& nonLinearOperator) {
66 using UF = std::remove_cvref_t<NRConfig>::UpdateFunction;
67 auto solverFactory = []<class NLO2, class LS2, class UF2>(NLO2&& nlo2, LS2&& ls, UF2&& uf) {
68 return std::make_shared<
69 NewtonRaphson<std::remove_cvref_t<NLO2>, std::remove_cvref_t<LS2>, std::remove_cvref_t<UF2>>>(
70 nlo2, std::forward<LS2>(ls), std::forward<UF2>(uf));
71 };
72
73 if constexpr (std::remove_cvref_t<NLO>::numberOfFunctions == 3) {
74 auto solver =
75 solverFactory(nonLinearOperator.template subOperator<1, 2>(), std::forward<NRConfig>(config).linearSolver,
76 std::forward<NRConfig>(config).updateFunction);
77 solver->setup(config.parameters);
78 return solver;
79 } else {
80 static_assert(std::remove_cvref_t<NLO>::numberOfFunctions > 1,
81 "The number of derivatives in the nonlinear operator have to be more than 1");
82 auto solver = solverFactory(nonLinearOperator, std::forward<NRConfig>(config).linearSolver,
83 std::forward<NRConfig>(config).updateFunction);
84 ;
85
86 solver->setup(std::forward<NRConfig>(config).parameters);
87 return solver;
88 }
89}
90
100template <typename NLO, typename LS, typename UF>
101class NewtonRaphson : public IObservable<NonLinearSolverMessages>
102{
103public:
106 static constexpr bool isLinearSolver =
108
110 using ValueType = typename NLO::template ParameterValue<0>;
111
112 using UpdateFunction = UF;
113 using NonLinearOperator = NLO;
114
121 template <typename LS2 = LS, typename UF2 = UF>
122 explicit NewtonRaphson(const NonLinearOperator& nonLinearOperator, LS2&& linearSolver = {}, UF2&& updateFunction = {})
123 : nonLinearOperator_{nonLinearOperator},
124 linearSolver_{std::forward<LS2>(linearSolver)},
125 updateFunction_{std::forward<UF2>(updateFunction)} {
126 if constexpr (std::is_same_v<typename NonLinearOperator::ValueType, Eigen::VectorXd>)
127 correction_.setZero(this->nonLinearOperator().value().size());
128 }
129
134 void setup(const Settings& settings) { settings_ = settings; }
135
136#ifndef DOXYGEN
137 struct NoPredictor
138 {
139 };
140#endif
146 template <typename SolutionType = NoPredictor>
147 requires std::is_same_v<SolutionType, NoPredictor> ||
148 std::is_convertible_v<SolutionType, std::remove_cvref_t<typename NonLinearOperator::ValueType>>
149 [[nodiscard(
150 "The solve method returns information of the solution process. You should store this information and check if "
151 "it was successful")]] Ikarus::NonLinearSolverInformation
152 solve(const SolutionType& dxPredictor = NoPredictor{}) {
154 Ikarus::NonLinearSolverInformation solverInformation;
155 solverInformation.success = true;
156 auto& x = nonLinearOperator().firstParameter();
157 if constexpr (not std::is_same_v<SolutionType, NoPredictor>)
158 updateFunction_(x, dxPredictor);
159 nonLinearOperator().updateAll();
160 const auto& rx = nonLinearOperator().value();
161 const auto& Ax = nonLinearOperator().derivative();
162 auto rNorm = norm(rx);
163 decltype(rNorm) dNorm;
164 int iter{0};
165 if constexpr (isLinearSolver)
166 linearSolver_.analyzePattern(Ax);
167 while (rNorm > settings_.tol && iter < settings_.maxIter) {
169 if constexpr (isLinearSolver) {
170 linearSolver_.factorize(Ax);
171 linearSolver_.solve(correction_, -rx);
172 dNorm = correction_.norm();
173 updateFunction_(x, correction_);
174 } else {
175 correction_ = -linearSolver_(rx, Ax);
176 dNorm = norm(correction_);
177 updateFunction_(x, correction_);
178 }
179 this->notify(NonLinearSolverMessages::CORRECTIONNORM_UPDATED, static_cast<double>(dNorm));
181 nonLinearOperator().updateAll();
182 rNorm = norm(rx);
183 this->notify(NonLinearSolverMessages::RESIDUALNORM_UPDATED, static_cast<double>(rNorm));
185 ++iter;
186 }
187 if (iter == settings_.maxIter)
188 solverInformation.success = false;
189 solverInformation.iterations = iter;
190 solverInformation.residualNorm = static_cast<double>(rNorm);
191 solverInformation.correctionNorm = static_cast<double>(dNorm);
192 if (solverInformation.success)
194 return solverInformation;
195 }
196
201 auto& nonLinearOperator() { return nonLinearOperator_; }
202
203private:
204 NonLinearOperator nonLinearOperator_;
205 typename NonLinearOperator::ValueType correction_;
206 LS linearSolver_;
207 UpdateFunction updateFunction_;
208 Settings settings_;
209};
210
221template <typename NLO, typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault>
222auto makeNewtonRaphson(const NLO& nonLinearOperator, LS&& linearSolver = {}, UF&& updateFunction = {}) {
223 return std::make_shared<NewtonRaphson<NLO, LS, UF>>(nonLinearOperator, std::forward<LS>(linearSolver),
224 std::move(updateFunction));
225}
226
227template <typename NLO, typename LS = utils::SolverDefault, typename UF = utils::UpdateDefault>
228NewtonRaphson(const NLO& nonLinearOperator, LS&& linearSolver = {},
229 UF&& updateFunction = {}) -> NewtonRaphson<NLO, std::remove_cvref_t<LS>, std::remove_cvref_t<UF>>;
230
231} // namespace Ikarus
Helper for the autodiff library.
Collection of fallback default functions.
Several concepts.
Implementation of the Newton-Raphson method for solving nonlinear equations.
Type-erased linear solver with templated scalar type.
Enums for observer messages.
Implementation of the observer design pattern.
auto norm(const Eigen::MatrixBase< Derived > &v)
Adding free norm function to Eigen types.
Definition: linearalgebrahelper.hh:259
Definition: dirichletbcenforcement.hh:6
LinearSolverTemplate< double > LinearSolver
Definition: linearsolver.hh:235
::value auto createNonlinearSolver(NRConfig &&config, NLO &&nonLinearOperator)
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:64
NewtonRaphson(const NLO &nonLinearOperator, LS &&linearSolver={}, UF &&updateFunction={}) -> NewtonRaphson< NLO, std::remove_cvref_t< LS >, std::remove_cvref_t< UF > >
auto makeNewtonRaphson(const NLO &nonLinearOperator, LS &&linearSolver={}, UF &&updateFunction={})
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:222
Implementation of the Newton-Raphson method for solving nonlinear equations.
Definition: newtonraphson.hh:102
Ikarus::NonLinearSolverInformation solve(const SolutionType &dxPredictor=NoPredictor{})
Solve the nonlinear system.
Definition: newtonraphson.hh:152
NRSettings Settings
Compile-time boolean indicating if the linear solver satisfies the non-linear solver concept.
Definition: newtonraphson.hh:105
UF UpdateFunction
Type representing the update function.
Definition: newtonraphson.hh:112
NLO NonLinearOperator
Type of the non-linear operator.
Definition: newtonraphson.hh:113
NewtonRaphson(const NonLinearOperator &nonLinearOperator, LS2 &&linearSolver={}, UF2 &&updateFunction={})
Constructor for NewtonRaphson.
Definition: newtonraphson.hh:122
void setup(const Settings &settings)
Set up the solver with the given settings.
Definition: newtonraphson.hh:134
auto & nonLinearOperator()
Access the nonlinear operator.
Definition: newtonraphson.hh:201
static constexpr bool isLinearSolver
Type representing the parameter vector of the nonlinear operator.
Definition: newtonraphson.hh:106
typename NLO::template ParameterValue< 0 > ValueType
Definition: newtonraphson.hh:110
Definition: newtonraphson.hh:25
int maxIter
Definition: newtonraphson.hh:27
double tol
Definition: newtonraphson.hh:26
Config for the Newton-Raphson solver.
Definition: newtonraphson.hh:36
NRSettings parameters
Definition: newtonraphson.hh:39
UF updateFunction
Definition: newtonraphson.hh:41
LS LinearSolver
Definition: newtonraphson.hh:37
LS linearSolver
Definition: newtonraphson.hh:40
UF UpdateFunction
Definition: newtonraphson.hh:38
auto rebindUpdateFunction(UF2 &&updateFunction) const
Definition: newtonraphson.hh:44
Information about the result of a non-linear solver.
Definition: solverinfos.hh:19
double correctionNorm
Definition: solverinfos.hh:28
int iterations
Definition: solverinfos.hh:29
double residualNorm
Definition: solverinfos.hh:27
bool success
Definition: solverinfos.hh:26
Represents a NonLinearOperator class for handling nonlinear operators.
Definition: nonlinearoperator.hh:156
Generic observable interface for the Observer design pattern. See for a description of the design pa...
Definition: observer.hh:129
void notify(NonLinearSolverMessages message)
Notify observers about a specific message type.
Concept to check if a linear solver implements all the needed functions for given vector and matrix t...
Definition: concepts.hh:212