version 0.4.4
nonlinearsolverfactory.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers ikarus@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
11#include <type_traits>
12#include <utility>
13
14#include <dune/common/float_cmp.hh>
15
20
21namespace Ikarus {
22
23namespace Impl {
24 struct IDBCForceFunction
25 {
26 template <typename A>
27 auto operator()(const A& assembler) {
28 return [&]<typename D>(const D&) { return utils::obtainForcesDueToIDBC(assembler); };
29 }
30 };
31
32 template <typename CT, typename A, typename S>
33 auto updateFunctor(const A& assembler, const S& setting) {
34 return [&]<typename D, typename C>(D& x, const C& b) {
35 if constexpr (not std::is_same_v<C, utils::SyncFERequirements>) {
36 // the right-hand side is reduced
37 if (assembler->dBCOption() == DBCOption::Reduced and assembler->reducedSize() == b.size()) {
38 setting.updateFunction(x, assembler->createFullVector(b));
39 } else if (assembler->reducedSize() == x.size() and
40 assembler->size() == b.size()) // the right-hand side is full but x is reduced
41 {
42 setting.updateFunction(x, assembler->createReducedVector(b));
43 } else
44 setting.updateFunction(x, b);
45 } else { // updates due to inhomogeneous bcs
46 if constexpr (requires { x.parameter(); }) {
47 auto& dv = assembler->dirichletValues();
48 CT newInc = CT::Zero(dv.size());
49 dv.evaluateInhomogeneousBoundaryCondition(newInc, x.parameter());
50 for (const auto i : Dune::range(newInc.size()))
51 if (Dune::FloatCmp::ne(newInc[i], 0.0))
52 x.globalSolution()[i] = newInc[i];
53 }
54 }
55 };
56 }
57} // namespace Impl
58
67template <typename NLSSetting>
69{
76 : settings(s) {}
77
90 template <typename Assembler>
91 auto withIDBCForceFunction(Assembler&& assembler) const {
92 auto idbcForceF = Impl::IDBCForceFunction{}.template operator()(assembler);
93 auto newSettings = settings.rebindIDBCForceFunction(std::move(idbcForceF));
94 return NonlinearSolverFactory<std::decay_t<decltype(newSettings)>>{std::move(newSettings)};
95 }
96
110 template <typename Assembler>
112 auto create(Assembler&& assembler) const {
113 auto f = DifferentiableFunctionFactory::op(assembler);
114 using fTraits = typename decltype(f)::Traits;
115
116 using CorrectionType = std::remove_cvref_t<typename fTraits::template Range<1>>;
117 using Domain = typename fTraits::Domain;
118
119 auto updateF = Impl::updateFunctor<CorrectionType>(assembler, settings);
120 auto settingsNew = settings.rebindUpdateFunction(std::move(updateF));
121 return createNonlinearSolver(std::move(settingsNew), std::move(f));
122 }
123
124private:
125 NLSSetting settings;
126};
127} // namespace Ikarus
Helper for dune-functions.
Contains the generic DifferentiableFunctionFactory class.
Collection of fallback default functions.
Definition: assemblermanipulatorbuildingblocks.hh:22
::value auto createNonlinearSolver(NRConfig &&config, F &&f)
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:106
auto obtainForcesDueToIDBC(const A &assembler)
A helper function to compute the forces due to inhomogeneous Dirichlet BCs.
Definition: functionhelper.hh:134
A factory class for creating nonlinear solvers.
Definition: nonlinearsolverfactory.hh:69
NonlinearSolverFactory(NLSSetting s)
Constructs a NonlinearSolverFactory with the given settings.
Definition: nonlinearsolverfactory.hh:75
auto create(Assembler &&assembler) const
Creates a nonlinear solver using the provided assembler.
Definition: nonlinearsolverfactory.hh:112
auto withIDBCForceFunction(Assembler &&assembler) const
A helper function to create another NonlinearSolverFactory object after binding IDBCForceFunction to ...
Definition: nonlinearsolverfactory.hh:91
static auto op(Assembler &&as, AffordanceCollection< Affordances... > affordances, DBCOption dbcOption=DBCOption::Full)
Definition: differentiablefunctionfactory.hh:25
Concept representing the requirements for a FlatAssembler.A type T satisfies FlatAssembler if it prov...
Definition: utils/concepts.hh:517