23template <
typename F,
typename LS = utils::SolverDefault,
typename UF = utils::UpdateDefault>
37template <
typename LS = utils::SolverDefault,
typename UF = utils::UpdateDefault>
46 template <
typename UF2>
65template <
typename LS,
typename UF>
80template <
typename F,
typename NRConfig>
81requires traits::isSpecialization<NewtonRaphsonConfig, std::remove_cvref_t<NRConfig>>::value
84 using UF = std::remove_cvref_t<NRConfig>::UpdateFunction;
85 auto solverFactory = []<
class F2, class LS2, class UF2>(F2&& f2, LS2&& ls, UF2&& uf) {
86 return std::make_shared<NewtonRaphson<std::remove_cvref_t<F2>, std::remove_cvref_t<LS2>, std::remove_cvref_t<UF2>>>(
87 f2, std::forward<LS2>(ls), std::forward<UF2>(uf));
90 if constexpr (std::remove_cvref_t<F>::nDerivatives == 2) {
91 auto solver = solverFactory(derivative(f), std::forward<NRConfig>(config).linearSolver,
92 std::forward<NRConfig>(config).updateFunction);
93 solver->setup(config.parameters);
96 static_assert(std::remove_cvref_t<F>::nDerivatives >= 1,
97 "The number of derivatives in the differentiable function have to be more than 0");
99 solverFactory(f, std::forward<NRConfig>(config).linearSolver, std::forward<NRConfig>(config).updateFunction);
102 solver->setup(std::forward<NRConfig>(config).parameters);
116template <
typename F,
typename LS,
typename UF>
127 using Domain =
typename SignatureTraits::Domain;
138 template <
typename LS2 = LS,
typename UF2 = UF>
141 jacobianFunction_{derivative(residualFunction_)},
142 linearSolver_{std::forward<LS2>(linearSolver)},
143 updateFunction_{std::forward<UF2>(updateFunction)} {}
151 DUNE_THROW(Dune::InvalidStateException,
152 "Minimum number of iterations cannot be greater than maximum number of iterations");
153 settings_ = settings;
162 "The solve method returns information of the solution process. You should store this information and check if "
170 solverInformation.success =
true;
172 decltype(
auto) rx = residualFunction_(x);
173 decltype(
auto) Ax = jacobianFunction_(x);
174 auto rNorm =
norm(rx);
175 solverInformation.residualNorm =
static_cast<double>(rNorm);
177 decltype(rNorm) dNorm;
180 linearSolver_.analyzePattern(Ax);
182 while ((rNorm > settings_.
tol && iter < settings_.
maxIter) or iter < settings_.
minIter) {
185 linearSolver_.factorize(Ax);
186 linearSolver_.solve(correction_, -rx);
187 dNorm = correction_.norm();
189 correction_ = -linearSolver_(rx, Ax);
190 dNorm =
norm(correction_);
192 solverInformation.correctionNorm =
static_cast<double>(dNorm);
195 updateFunction_(x, correction_);
199 rx = residualFunction_(x);
200 Ax = jacobianFunction_(x);
202 solverInformation.residualNorm =
static_cast<double>(rNorm);
206 solverInformation.iterations = iter;
210 solverInformation.success =
false;
211 solverInformation.iterations = iter;
212 if (solverInformation.success)
214 return solverInformation;
225 typename DifferentiableFunction::Derivative jacobianFunction_;
226 std::remove_cvref_t<CorrectionType> correction_;
242template <
typename F,
typename LS = utils::SolverDefault,
typename UF = utils::UpdateDefault>
244 return std::make_shared<NewtonRaphson<F, LS, UF>>(f, std::forward<LS>(linearSolver), std::move(updateFunction));
247template <
typename F,
typename LS = utils::SolverDefault,
typename UF = utils::UpdateDefault>
249 UF&& updateFunction = {}) ->
NewtonRaphson<F, std::remove_cvref_t<LS>, std::remove_cvref_t<UF>>;
Collection of fallback default functions.
Helper for the autodiff library.
State for all nonlinear solvers.
Implementation of the solver information returned by the nonlinear solvers.
Base for all nonlinear solvers.
Type-erased linear solver with templated scalar type.
Implementation of the observer design pattern with broadcasters.
Enums for observer messages.
auto norm(const Eigen::MatrixBase< Derived > &v)
Adding free norm function to Eigen types.
Definition: linearalgebrahelper.hh:259
Definition: assemblermanipulatorbuildingblocks.hh:22
auto makeNewtonRaphson(const F &f, LS &&linearSolver={}, UF &&updateFunction={})
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:243
LinearSolverTemplate< double > LinearSolver
Definition: linearsolver.hh:235
::value auto createNonlinearSolver(NRConfig &&config, F &&f)
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:82
NonLinearSolverMessages
Enum class defining non-linear solver-related messages.
Definition: broadcastermessages.hh:22
NewtonRaphson(const F &f, LS &&linearSolver={}, UF &&updateFunction={}) -> NewtonRaphson< F, std::remove_cvref_t< LS >, std::remove_cvref_t< UF > >
Implementation of the Newton-Raphson method for solving nonlinear equations.
Definition: newtonraphson.hh:118
static constexpr bool isLinearSolver
Definition: newtonraphson.hh:125
NRSettings Settings
Definition: newtonraphson.hh:120
typename SignatureTraits::Domain Domain
Type representing the parameter vector of the function.
Definition: newtonraphson.hh:127
F DifferentiableFunction
Type of the non-linear operator.
Definition: newtonraphson.hh:130
typename SignatureTraits::template Range< 0 > CorrectionType
Type of the correction of x += deltaX.
Definition: newtonraphson.hh:122
typename F::Traits SignatureTraits
Definition: newtonraphson.hh:121
void setup(const Settings &settings)
Set up the solver with the given settings.
Definition: newtonraphson.hh:149
NonLinearSolverInformation solve(Domain &x)
Solve the nonlinear system.
Definition: newtonraphson.hh:164
typename SignatureTraits::template Range< 1 > JacobianType
Compile-time boolean indicating if the linear solver satisfies the non-linear solver concept.
Definition: newtonraphson.hh:124
NewtonRaphson(const DifferentiableFunction &residual, LS2 &&linearSolver={}, UF2 &&updateFunction={})
Constructor for NewtonRaphson.
Definition: newtonraphson.hh:139
auto & residual()
Access the function.
Definition: newtonraphson.hh:221
UF UpdateFunction
Type representing the update function.
Definition: newtonraphson.hh:129
Definition: newtonraphson.hh:27
int maxIter
Definition: newtonraphson.hh:29
int minIter
Definition: newtonraphson.hh:30
double tol
Definition: newtonraphson.hh:28
Config for the Newton-Raphson solver.
Definition: newtonraphson.hh:39
NRSettings parameters
Definition: newtonraphson.hh:42
UF updateFunction
Definition: newtonraphson.hh:44
LS LinearSolver
Definition: newtonraphson.hh:40
LS linearSolver
Definition: newtonraphson.hh:43
UF UpdateFunction
Definition: newtonraphson.hh:41
auto rebindUpdateFunction(UF2 &&updateFunction) const
Definition: newtonraphson.hh:47
Base for all nonlinear solvers. Defines the message interface that can be broadcasted to listeners.
Definition: nonlinearsolverbase.hh:24
NonlinearSolverStateType< F > State
Definition: nonlinearsolverbase.hh:25
Information about the result of a non-linear solver.
Definition: solverinfos.hh:21
void notify(NonLinearSolverMessages message, const State &data)
This calls all the registered functions.
Definition: broadcaster.hh:61
Default functor for solving operations.
Definition: defaultfunctions.hh:26
Concept to check if a linear solver implements all the needed functions for given vector and matrix t...
Definition: utils/concepts.hh:226