version 3.11-dev
Loading...
Searching...
No Matches
multidomain/newtonsolver.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_MULTIDOMAIN_NEWTON_SOLVER_HH
14#define DUMUX_MULTIDOMAIN_NEWTON_SOLVER_HH
15
16#include <memory>
18
19namespace Dumux {
20namespace Detail {
21
22template<class Assembler, class Index>
23using DetectPVSwitchMultiDomain = typename Assembler::template GridVariables<Index::value>::VolumeVariables::PrimaryVariableSwitch;
24
25template<class Assembler, class Index>
26using DetectLocalDofVariablesPVSwitchMultiDomain = typename Assembler::template GridVariables<Index::value>::Variables::PrimaryVariableSwitch;
27
28} // end namespace Detail
29
35template <class Assembler, class LinearSolver, class CouplingManager,
36 class Reassembler = DefaultPartialReassembler,
37 class Comm = Dune::Communication<Dune::MPIHelper::MPICommunicator> >
38class MultiDomainNewtonSolver: public NewtonSolver<Assembler, LinearSolver, Reassembler, Comm>
39{
41 using typename ParentType::Backend;
42 using typename ParentType::SolutionVector;
43
44 static constexpr bool assemblerExportsVariables = Detail::PDESolver::assemblerExportsVariables<Assembler>;
45
46 template<std::size_t i>
47 using PrimaryVariableSwitch =
48 Dune::Std::detected_or_t<int, Detail::DetectPVSwitchMultiDomain, Assembler, Dune::index_constant<i>>;
49
50 template<std::size_t i>
51 using HasPriVarsSwitch =
52 Dune::Std::is_detected<Detail::DetectPVSwitchMultiDomain, Assembler, Dune::index_constant<i>>; // std::true_type or std::false_type
53
54 template<std::size_t i>
55 using PrivarSwitchPtr = std::unique_ptr<PrimaryVariableSwitch<i>>;
56 using PriVarSwitchPtrTuple = typename Assembler::Traits::template Tuple<PrivarSwitchPtr>;
57
58public:
59 using typename ParentType::Variables;
60
64 MultiDomainNewtonSolver(std::shared_ptr<Assembler> assembler,
65 std::shared_ptr<LinearSolver> linearSolver,
66 std::shared_ptr<CouplingManager> couplingManager,
67 const Comm& comm = Dune::MPIHelper::getCommunication(),
68 const std::string& paramGroup = "")
69 : ParentType(assembler, linearSolver, comm, paramGroup)
70 , couplingManager_(couplingManager)
71 {
72 using namespace Dune::Hybrid;
73 forEach(std::make_index_sequence<Assembler::Traits::numSubDomains>{}, [&](auto&& id)
74 {
75 static_assert(!Dune::Std::is_detected_v<Detail::DetectLocalDofVariablesPVSwitchMultiDomain, Assembler, std::decay_t<decltype(id)>>,
76 "The primary variable switch is not implemented for variables per local dof");
77
78 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
79 using PVSwitch = PrimaryVariableSwitch<std::decay_t<decltype(id)>::value>;
80 elementAt(priVarSwitches_, id) = std::make_unique<PVSwitch>(priVarSwitchVerbosity);
81 });
82
83 priVarsSwitchedInLastIteration_.fill(false);
84 }
85
89 void newtonBeginStep(const Variables& varsCurrentIter) override
90 {
91 ParentType::newtonBeginStep(varsCurrentIter);
92 couplingManager_->updateSolution(Backend::dofs(varsCurrentIter));
93 }
94
102 void newtonBegin(Variables& vars) override
103 {
105
106 using namespace Dune::Hybrid;
107 forEach(std::make_index_sequence<Assembler::Traits::numSubDomains>{}, [&](auto&& id)
108 {
109 this->initPriVarSwitch_(vars, id, HasPriVarsSwitch<std::decay_t<decltype(id)>::value>{});
110 });
111 }
112
117 bool newtonConverged() const override
118 {
119 if (Dune::any_true(priVarsSwitchedInLastIteration_))
120 return false;
121
123 }
124
125
132 void newtonEndStep(Variables& varsCurrentIter, const SolutionVector& uLastIter) override
133 {
134 using namespace Dune::Hybrid;
135 forEach(std::make_index_sequence<Assembler::Traits::numSubDomains>{}, [&](auto&& id)
136 {
137 auto& uCurrentIter = Backend::dofs(varsCurrentIter)[id];
138 if constexpr (!assemblerExportsVariables)
139 this->invokePriVarSwitch_(this->assembler().gridVariables(id),
140 uCurrentIter, id, HasPriVarsSwitch<std::decay_t<decltype(id)>::value>{});
141 else
142 this->invokePriVarSwitch_(varsCurrentIter[id], uCurrentIter, id, HasPriVarsSwitch<std::decay_t<decltype(id)>::value>{});
143 });
144
145 ParentType::newtonEndStep(varsCurrentIter, uLastIter);
146 couplingManager_->updateSolution(Backend::dofs(varsCurrentIter));
147 }
148
149protected:
153 void solutionChanged_(Variables& vars, const SolutionVector& uCurrentIter) override
154 {
155 couplingManager_->updateSolution(uCurrentIter);
156 ParentType::solutionChanged_(vars, uCurrentIter);
157 }
158
159private:
160
164 template<std::size_t i>
165 void initPriVarSwitch_(Variables&, Dune::index_constant<i> id, std::false_type) {}
166
170 template<std::size_t i>
171 void initPriVarSwitch_(Variables& vars, Dune::index_constant<i> id, std::true_type)
172 {
173 using namespace Dune::Hybrid;
174 auto& priVarSwitch = *elementAt(priVarSwitches_, id);
175 auto& sol = Backend::dofs(vars)[id];
176
177 priVarSwitch.reset(sol.size());
178 priVarsSwitchedInLastIteration_[i] = false;
179
180 const auto& problem = this->assembler().problem(id);
181 const auto& gridGeometry = this->assembler().gridGeometry(id);
182 if constexpr (!assemblerExportsVariables)
183 priVarSwitch.updateDirichletConstraints(problem, gridGeometry, this->assembler().gridVariables(id), sol);
184 else // This expects usage of MultiDomainGridVariables
185 priVarSwitch.updateDirichletConstraints(problem, gridGeometry, vars[id], sol[id]);
186 }
187
191 template<class SubVars, class SubSol, std::size_t i>
192 void invokePriVarSwitch_(SubVars&, SubSol&, Dune::index_constant<i> id, std::false_type) {}
193
197 template<class SubVars, class SubSol, std::size_t i>
198 void invokePriVarSwitch_(SubVars& subVars, SubSol& uCurrentIter, Dune::index_constant<i> id, std::true_type)
199 {
200 // update the variable switch (returns true if the pri vars at at least one dof were switched)
201 // for disabled grid variable caching
202 const auto& gridGeometry = this->assembler().gridGeometry(id);
203 const auto& problem = this->assembler().problem(id);
204
205 using namespace Dune::Hybrid;
206 auto& priVarSwitch = *elementAt(priVarSwitches_, id);
207
208 // invoke the primary variable switch
209 priVarsSwitchedInLastIteration_[i] = priVarSwitch.update(uCurrentIter, subVars, problem, gridGeometry);
210
211 if (priVarsSwitchedInLastIteration_[i])
212 {
213 for (const auto& element : elements(gridGeometry.gridView()))
214 {
215 // if the volume variables are cached globally, we need to update those where the primary variables have been switched
216 priVarSwitch.updateSwitchedVolVars(problem, element, gridGeometry, subVars, uCurrentIter);
217
218 // if the flux variables are cached globally, we need to update those where the primary variables have been switched
219 priVarSwitch.updateSwitchedFluxVarsCache(problem, element, gridGeometry, subVars, uCurrentIter);
220 }
221 }
222 }
223
225 std::shared_ptr<CouplingManager> couplingManager_;
226
228 PriVarSwitchPtrTuple priVarSwitches_;
230 std::array<bool, Assembler::Traits::numSubDomains> priVarsSwitchedInLastIteration_;
231};
232
233} // end namespace Dumux
234
235#endif
The interface of the coupling manager for multi domain problems.
Definition multidomain/couplingmanager.hh:37
Definition partialreassembler.hh:32
Base class for linear solvers.
Definition solver.hh:27
bool newtonConverged() const override
Returns true if the error of the solution is below the tolerance.
Definition multidomain/newtonsolver.hh:117
MultiDomainNewtonSolver(std::shared_ptr< Assembler > assembler, std::shared_ptr< LinearSolver > linearSolver, std::shared_ptr< CouplingManager > couplingManager, const Comm &comm=Dune::MPIHelper::getCommunication(), const std::string &paramGroup="")
The constructor.
Definition multidomain/newtonsolver.hh:64
void newtonEndStep(Variables &varsCurrentIter, const SolutionVector &uLastIter) override
Indicates that one Newton iteration was finished.
Definition multidomain/newtonsolver.hh:132
void newtonBeginStep(const Variables &varsCurrentIter) override
Indicates the beginning of a Newton iteration.
Definition multidomain/newtonsolver.hh:89
void newtonBegin(Variables &vars) override
Called before the Newton method is applied to an non-linear system of equations.
Definition multidomain/newtonsolver.hh:102
void solutionChanged_(Variables &vars, const SolutionVector &uCurrentIter) override
Update solution-depended quantities like grid variables after the solution has changed.
Definition multidomain/newtonsolver.hh:153
const std::string & paramGroup() const
Returns the parameter group.
Definition nonlinear/newtonsolver.hh:826
virtual void newtonBegin(Variables &initVars)
Definition nonlinear/newtonsolver.hh:389
virtual void newtonEndStep(Variables &vars, const SolutionVector &uLastIter)
Definition nonlinear/newtonsolver.hh:611
NewtonSolver(std::shared_ptr< Assembler > assembler, std::shared_ptr< LinearSolver > linearSolver, const Communication &comm=Dune::MPIHelper::getCommunication(), const std::string &paramGroup="", const std::string &paramGroupName="Newton", int verbosity=2)
Definition nonlinear/newtonsolver.hh:209
virtual void solutionChanged_(Variables &vars, const SolutionVector &uCurrentIter)
Definition nonlinear/newtonsolver.hh:860
virtual void newtonBeginStep(const Variables &vars)
Definition nonlinear/newtonsolver.hh:450
typename Backend::DofVector SolutionVector
Definition nonlinear/newtonsolver.hh:188
const Communication & comm() const
the communicator for parallel runs
Definition nonlinear/newtonsolver.hh:239
VariablesBackend< typename ParentType::Variables > Backend
Definition nonlinear/newtonsolver.hh:187
virtual bool newtonConverged() const
Definition nonlinear/newtonsolver.hh:656
Assembler Assembler
Definition common/pdesolver.hh:67
const LinearSolver & linearSolver() const
Definition common/pdesolver.hh:133
const Assembler & assembler() const
Definition common/pdesolver.hh:121
Detail::PDESolver::AssemblerVariables< Assembler > Variables
Definition common/pdesolver.hh:71
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:168
constexpr bool assemblerExportsVariables
Definition common/pdesolver.hh:35
Definition cvfelocalresidual.hh:25
typename Assembler::template GridVariables< Index::value >::Variables::PrimaryVariableSwitch DetectLocalDofVariablesPVSwitchMultiDomain
Definition multidomain/newtonsolver.hh:26
typename Assembler::template GridVariables< Index::value >::VolumeVariables::PrimaryVariableSwitch DetectPVSwitchMultiDomain
Definition multidomain/newtonsolver.hh:23
Definition adapt.hh:17
Reference implementation of a Newton solver.