3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
multidomain/couplingmanager.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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_MULTIDOMAIN_COUPLING_MANAGER_HH
26#define DUMUX_MULTIDOMAIN_COUPLING_MANAGER_HH
27
28#include <memory>
29#include <tuple>
30#include <vector>
31#include <dune/common/exceptions.hh>
32#include <dune/common/indices.hh>
33#include <dune/common/shared_ptr.hh>
34#include <dune/common/hybridutilities.hh>
35#include <dune/istl/multitypeblockvector.hh>
36
40
41namespace Dumux {
42
43namespace Detail {
44
45// helper to create a multitype vector of references to solution vectors
46template<class... Args, std::size_t ...Is>
47auto toRef(const std::tuple<Args...>& v, std::index_sequence<Is...> indices)
48{
50}
51
52} // end namespace Detail
53
58template<class Traits>
60{
61 template<std::size_t id> using SubDomainTypeTag = typename Traits::template SubDomain<id>::TypeTag;
62 template<std::size_t id> using PrimaryVariables = GetPropType<SubDomainTypeTag<id>, Properties::PrimaryVariables>;
63 template<std::size_t id> using GridView = typename GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>::GridView;
64 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
65 template<std::size_t id> using Problem = GetPropType<SubDomainTypeTag<id>, Properties::Problem>;
66 template<std::size_t id> using ProblemPtr = const Problem<id> *;
67 using ProblemPtrs = typename Traits::template Tuple<ProblemPtr>;
68
69 template<std::size_t id>
70 using SubSolutionVector
71 = std::decay_t<decltype(std::declval<typename Traits::SolutionVector>()[Dune::index_constant<id>()])>;
72
73public:
75 template<std::size_t i, std::size_t j>
76 using CouplingStencilType = std::vector<std::size_t>;
77
79 using SolutionVector = typename Traits::SolutionVector;
80
81protected:
83 using SolutionVectorStorage = typename Traits::template TupleOfSharedPtr<SubSolutionVector>;
84
85public:
95 {
96 using namespace Dune::Hybrid;
97 forEach(problems_, [](auto& problem){
98 problem = nullptr;
99 });
100
101 forEach(curSols_, [](auto& solutionVector){
102 solutionVector = std::make_shared<typename std::decay_t<decltype(solutionVector)>::element_type>();
103 });
104 }
105
109 // \{
110
125 template<std::size_t i, std::size_t j>
126 const CouplingStencilType<i, j>& couplingStencil(Dune::index_constant<i> domainI,
127 const Element<i>& elementI,
128 Dune::index_constant<j> domainJ) const
129 {
130 static_assert(i != j, "Domain i cannot be coupled to itself!");
131 static_assert(AlwaysFalse<Dune::index_constant<i>>::value,
132 "The coupling manager does not implement the couplingStencil() function");
133 }
134
143 template<std::size_t id, class JacobianPattern>
144 void extendJacobianPattern(Dune::index_constant<id> domainI, JacobianPattern& pattern) const
145 {}
146
147 // \}
148
152 // \{
153
168 template<std::size_t i, class Assembler>
169 void bindCouplingContext(Dune::index_constant<i> domainI,
170 const Element<i>& elementI,
171 const Assembler& assembler)
172 {}
173
174
194 template<std::size_t i, std::size_t j, class LocalAssemblerI>
195 void updateCouplingContext(Dune::index_constant<i> domainI,
196 const LocalAssemblerI& localAssemblerI,
197 Dune::index_constant<j> domainJ,
198 std::size_t dofIdxGlobalJ,
199 const PrimaryVariables<j>& priVarsJ,
200 int pvIdxJ)
201 {
202 curSol(domainJ)[dofIdxGlobalJ][pvIdxJ] = priVarsJ[pvIdxJ];
203 }
204
219 template<std::size_t i, class LocalAssemblerI, class UpdatableElementVolVars, class UpdatableFluxVarCache>
220 void updateCoupledVariables(Dune::index_constant<i> domainI,
221 const LocalAssemblerI& localAssemblerI,
222 UpdatableElementVolVars& elemVolVars,
223 UpdatableFluxVarCache& elemFluxVarsCache)
224 {}
225
232 {
233 using namespace Dune::Hybrid;
234 forEach(integralRange(Dune::Hybrid::size(curSols_)), [&](const auto id)
235 {
236 // copy external solution into object stored in this class
237 *std::get<id>(curSols_) = curSol[id];
238 });
239 }
240
241 // \}
242
260 template<std::size_t i, std::size_t j, class LocalAssemblerI>
261 decltype(auto) evalCouplingResidual(Dune::index_constant<i> domainI,
262 const LocalAssemblerI& localAssemblerI,
263 Dune::index_constant<j> domainJ,
264 std::size_t dofIdxGlobalJ) const
265 {
266 return localAssemblerI.evalLocalResidual();
267 }
268
275 template<std::size_t i, class LocalAssemblerI, class JacobianMatrixDiagBlock, class GridVariables>
276 void evalAdditionalDomainDerivatives(Dune::index_constant<i> domainI,
277 const LocalAssemblerI& localAssemblerI,
278 const typename LocalAssemblerI::LocalResidual::ElementResidualVector& origResiduals,
279 JacobianMatrixDiagBlock& A,
280 GridVariables& gridVariables)
281 {}
282
286 template<std::size_t i>
287 decltype(auto) numericEpsilon(Dune::index_constant<i>,
288 const std::string& paramGroup) const
289 {
290 constexpr auto numEq = PrimaryVariables<i>::dimension;
292 }
293
298 template<typename... SubProblems>
299 void setSubProblems(const std::tuple<std::shared_ptr<SubProblems>...>& problems)
300 {
301 using namespace Dune::Hybrid;
302 forEach(integralRange(size(problems_)), [&](const auto i)
303 { setSubProblem(std::get<i>(problems), i); });
304 }
305
311 template<class SubProblem, std::size_t i>
312 void setSubProblem(std::shared_ptr<SubProblem> problem, Dune::index_constant<i> domainIdx)
313 { std::get<i>(problems_) = problem.get(); }
314
320 template<std::size_t i>
321 const Problem<i>& problem(Dune::index_constant<i> domainIdx) const
322 {
323 const Problem<i>* p = std::get<i>(problems_);
324 assert(p && "The problem pointer is invalid. Use setSubProblems() before calling this function");
325 return *p;
326 }
327
328protected:
335 {
336 using namespace Dune::Hybrid;
337 forEach(integralRange(Dune::Hybrid::size(curSols_)), [&](const auto id)
338 {
339 // do not take ownership of the external pointer's object
340 std::get<id>(curSols_) = Dune::stackobject_to_shared_ptr(*std::get<id>(curSol));
341 });
342 }
343
349 template<std::size_t i>
350 SubSolutionVector<i>& curSol(Dune::index_constant<i> domainIdx)
351 { return *std::get<i>(curSols_); }
352
358 template<std::size_t i>
359 const SubSolutionVector<i>& curSol(Dune::index_constant<i> domainIdx) const
360 { return *std::get<i>(curSols_); }
361
368 [[deprecated("This function returns a Dune::MultiTypeBlockVector<SubDomainSolutionVector&, ....> (i.e. storing references). "
369 "Use curSol(domainIdx) to get a reference to the corresponding subdomain solution vector. Will be removed after 3.5")]]
370 decltype(auto) curSol()
371 {
372 return Detail::toRef(curSols_, std::make_index_sequence<Traits::numSubDomains>());
373 }
374
381 [[deprecated("This function returns a Dune::MultiTypeBlockVector<SubDomainSolutionVector&, ....> (i.e. storing references). "
382 "Use curSol(domainIdx) to get a reference to the corresponding subdomain solution vector. Will be removed after 3.5")]]
383 decltype(auto) curSol() const
384 {
385 return Detail::toRef(curSols_, std::make_index_sequence<Traits::numSubDomains>());
386 }
387
388private:
393 SolutionVectorStorage curSols_;
394
399 ProblemPtrs problems_;
400};
401
402} // end namespace Dumux
403
404#endif
An adapter class for local assemblers using numeric differentiation.
Type traits.
void updateCouplingContext(Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, Dune::index_constant< j > domainJ, std::size_t dofIdxGlobalJ, const PrimaryVariables< j > &priVarsJ, int pvIdxJ)
updates all data and variables that are necessary to evaluate the residual of the element of domain i...
Definition: multidomain/couplingmanager.hh:195
decltype(auto) evalCouplingResidual(Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, Dune::index_constant< j > domainJ, std::size_t dofIdxGlobalJ) const
evaluates the element residual of a coupled element of domain i which depends on the variables at the...
Definition: multidomain/couplingmanager.hh:261
Definition: adapt.hh:29
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:150
auto toRef(const std::tuple< Args... > &v, std::index_sequence< Is... > indices)
Definition: multidomain/couplingmanager.hh:47
A helper class for local assemblers using numeric differentiation to determine the epsilon.
Definition: numericepsilon.hh:41
A vector of primary variables.
Definition: common/properties.hh:49
Property to specify the type of a problem which has to be solved.
Definition: common/properties.hh:57
Definition: common/properties.hh:102
Template which always yields a false value.
Definition: typetraits.hh:35
Definition: variablesbackend.hh:43
The interface of the coupling manager for multi domain problems.
Definition: multidomain/couplingmanager.hh:60
void updateCoupledVariables(Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, UpdatableElementVolVars &elemVolVars, UpdatableFluxVarCache &elemFluxVarsCache)
update variables of domain i that depend on variables in domain j after the coupling context has been...
Definition: multidomain/couplingmanager.hh:220
const SubSolutionVector< i > & curSol(Dune::index_constant< i > domainIdx) const
the solution vector of the subproblem
Definition: multidomain/couplingmanager.hh:359
void extendJacobianPattern(Dune::index_constant< id > domainI, JacobianPattern &pattern) const
extend the jacobian pattern of the diagonal block of domain i by those entries that are not already i...
Definition: multidomain/couplingmanager.hh:144
decltype(auto) curSol() const
the solution vector of the coupled problem
Definition: multidomain/couplingmanager.hh:383
void attachSolution(SolutionVectorStorage &curSol)
Attach a solution vector stored outside of this class.
Definition: multidomain/couplingmanager.hh:334
void setSubProblem(std::shared_ptr< SubProblem > problem, Dune::index_constant< i > domainIdx)
set a pointer to one of the sub problems
Definition: multidomain/couplingmanager.hh:312
decltype(auto) curSol()
the solution vector of the coupled problem
Definition: multidomain/couplingmanager.hh:370
void setSubProblems(const std::tuple< std::shared_ptr< SubProblems >... > &problems)
set the pointers to the sub problems
Definition: multidomain/couplingmanager.hh:299
const Problem< i > & problem(Dune::index_constant< i > domainIdx) const
Return a reference to the sub problem.
Definition: multidomain/couplingmanager.hh:321
void bindCouplingContext(Dune::index_constant< i > domainI, const Element< i > &elementI, const Assembler &assembler)
prepares all data and variables that are necessary to evaluate the residual of the element of domain ...
Definition: multidomain/couplingmanager.hh:169
std::vector< std::size_t > CouplingStencilType
default type used for coupling element stencils
Definition: multidomain/couplingmanager.hh:76
SubSolutionVector< i > & curSol(Dune::index_constant< i > domainIdx)
the solution vector of the subproblem
Definition: multidomain/couplingmanager.hh:350
void updateSolution(const SolutionVector &curSol)
Updates the entire solution vector, e.g. before assembly or after grid adaption Overload might want t...
Definition: multidomain/couplingmanager.hh:231
void evalAdditionalDomainDerivatives(Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, const typename LocalAssemblerI::LocalResidual::ElementResidualVector &origResiduals, JacobianMatrixDiagBlock &A, GridVariables &gridVariables)
evaluate additional derivatives of the element residual of a domain with respect to dofs in the same ...
Definition: multidomain/couplingmanager.hh:276
decltype(auto) numericEpsilon(Dune::index_constant< i >, const std::string &paramGroup) const
return the numeric epsilon used for deflecting primary variables of coupled domain i
Definition: multidomain/couplingmanager.hh:287
CouplingManager()
Default constructor.
Definition: multidomain/couplingmanager.hh:94
const CouplingStencilType< i, j > & couplingStencil(Dune::index_constant< i > domainI, const Element< i > &elementI, Dune::index_constant< j > domainJ) const
returns an iteratable container of all indices of degrees of freedom of domain j that couple with / i...
Definition: multidomain/couplingmanager.hh:126
typename Traits::template TupleOfSharedPtr< SubSolutionVector > SolutionVectorStorage
the type in which the solution vector is stored in the manager
Definition: multidomain/couplingmanager.hh:83
typename Traits::SolutionVector SolutionVector
the type of the solution vector
Definition: multidomain/couplingmanager.hh:79
Declares all properties used in Dumux.