version 3.11-dev
Loading...
Searching...
No Matches
embedded/extendedsourcestencil.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//
12
13#ifndef DUMUX_MULTIDOMAIN_EMBEDDED_EXTENDEDSOURCESTENCIL_HH
14#define DUMUX_MULTIDOMAIN_EMBEDDED_EXTENDEDSOURCESTENCIL_HH
15
16#include <vector>
17
18#include <dune/common/indices.hh>
19
25
28
30
36template<class CouplingManager>
38{
39 using MDTraits = typename CouplingManager::MultiDomainTraits;
40
41 template<std::size_t id> using SubDomainTypeTag = typename MDTraits::template SubDomain<id>::TypeTag;
42 template<std::size_t id> using GridGeometry = GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>;
43 template<std::size_t id> using GridView = typename GridGeometry<id>::GridView;
44 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
45
46 static constexpr auto bulkIdx = typename MDTraits::template SubDomain<0>::Index();
47 static constexpr auto lowDimIdx = typename MDTraits::template SubDomain<1>::Index();
48
49 template<std::size_t id>
50 static constexpr bool isCVFE()
52public:
59 template<std::size_t id, class JacobianPattern>
60 void extendJacobianPattern(const CouplingManager& couplingManager, Dune::index_constant<id> domainI, JacobianPattern& pattern) const
61 {
62 // add additional dof dependencies
63 for (const auto& element : elements(couplingManager.gridView(domainI)))
64 {
65 const auto& dofs = extendedSourceStencil_(couplingManager, domainI, element);
66 if constexpr (isCVFE<domainI>())
67 {
68 const auto eIdx = gridDiscretization(couplingManager.problem(domainI)).elementMapper().index(element);
69 for (const auto globalI : couplingManager.elementDofIndices(domainI, eIdx))
70 for (const auto globalJ : dofs)
71 pattern.add(globalI, globalJ);
72 }
73 else
74 {
75 const auto globalI = gridDiscretization(couplingManager.problem(domainI)).elementMapper().index(element);
76 for (const auto globalJ : dofs)
77 pattern.add(globalI, globalJ);
78 }
79 }
80 }
81
88 template<std::size_t i, class LocalAssemblerI, class JacobianMatrixDiagBlock, class GridVariables>
90 Dune::index_constant<i> domainI,
91 const LocalAssemblerI& localAssemblerI,
92 JacobianMatrixDiagBlock& A,
93 GridVariables& gridVariables) const
94 {
95 const auto& curSolI = couplingManager.curSol(domainI);
96 constexpr auto numEq = std::decay_t<decltype(curSolI[0])>::size();
97 using PrimaryVariable = typename std::decay_t<decltype(curSolI[0])>::value_type;
98 const auto& elementI = localAssemblerI.element();
99
100 // only do something if we have an extended stencil
101 if (extendedSourceStencil_(couplingManager, domainI, elementI).empty())
102 return;
103
104 // compute the undeflected residual (source only!)
105 const auto origResidual = localAssemblerI.evalLocalSourceResidual(elementI);
106
107 // compute derivate for all additional dofs in the circle stencil
108 for (const auto dofIndex : extendedSourceStencil_(couplingManager, domainI, elementI))
109 {
110 auto partialDerivs = origResidual;
111 const auto origPriVars = curSolI[dofIndex];
112 auto priVars = origPriVars;
113
114 // calculate derivatives w.r.t to the privars at the dof at hand
115 for (int pvIdx = 0; pvIdx < numEq; pvIdx++)
116 {
117 // reset partial derivatives
118 partialDerivs = 0.0;
119
120 const auto evalResiduals = [&](const PrimaryVariable priVar)
121 {
122 // update the coupling context (solution vector and recompute element residual)
123 priVars[pvIdx] = priVar;
124 couplingManager.updateCouplingContext(domainI, localAssemblerI, domainI, dofIndex, priVars, pvIdx);
125 return localAssemblerI.evalLocalSourceResidual(elementI);
126 };
127
128 // derive the residuals numerically
129 static const NumericEpsilon<PrimaryVariable, numEq> eps_{localAssemblerI.problem().paramGroup()};
130 static const int numDiffMethod = getParamFromGroup<int>(localAssemblerI.problem().paramGroup(), "Assembly.NumericDifferenceMethod");
132 evalResiduals, priVars[pvIdx], partialDerivs, origResidual, eps_(priVars[pvIdx], pvIdx), numDiffMethod
133 );
134
135 // update the global stiffness matrix with the current partial derivatives
136 const auto& elemDiscI = [&]() -> decltype(auto) {
137 if constexpr (requires { localAssemblerI.elemDisc(); })
138 return localAssemblerI.elemDisc();
139 else
140 return localAssemblerI.fvGeometry();
141 }();
142 if constexpr (isCVFE<i>())
143 {
144 for (const auto& localDof : localDofs(elemDiscI))
145 {
146 for (int eqIdx = 0; eqIdx < numEq; eqIdx++)
147 {
148 // A[i][col][eqIdx][pvIdx] is the rate of change of
149 // the residual of equation 'eqIdx' at dof 'i'
150 // depending on the primary variable 'pvIdx' at dof
151 // 'col'.
152 A[localDof.dofIndex()][dofIndex][eqIdx][pvIdx] += partialDerivs[localDof.index()][eqIdx];
153 }
154 }
155 }
156 else
157 {
158 for (const auto& scvJ : scvs(elemDiscI))
159 for (int eqIdx = 0; eqIdx < numEq; eqIdx++)
160 A[scvJ.dofIndex()][dofIndex][eqIdx][pvIdx] += partialDerivs[scvJ.indexInElement()][eqIdx];
161 }
162
163 // restore the current element solution
164 priVars[pvIdx] = origPriVars[pvIdx];
165
166 // restore the original coupling context
167 couplingManager.updateCouplingContext(domainI, localAssemblerI, domainI, dofIndex, origPriVars, pvIdx);
168 }
169 }
170 }
171
173 void clear() { sourceStencils_.clear(); }
174
176 typename CouplingManager::template CouplingStencils<bulkIdx>& stencil()
177 { return sourceStencils_; }
178
180 const typename CouplingManager::template CouplingStencils<bulkIdx>& stencil() const
181 { return sourceStencils_; }
182
183private:
185 template<std::size_t id>
186 const auto& extendedSourceStencil_(const CouplingManager& couplingManager, Dune::index_constant<id> domainId, const Element<id>& element) const
187 {
188 if constexpr (domainId == bulkIdx)
189 {
190 const auto bulkElementIdx = gridDiscretization(couplingManager.problem(bulkIdx)).elementMapper().index(element);
191 if (auto stencil = sourceStencils_.find(bulkElementIdx); stencil != sourceStencils_.end())
192 return stencil->second;
193 }
194
195 return couplingManager.emptyStencil(domainId);
196 }
197
199 typename CouplingManager::template CouplingStencils<bulkIdx> sourceStencils_;
200};
201
202} // end namespace Dumux::EmbeddedCoupling
203
204#endif
The interface of the coupling manager for multi domain problems.
Definition multidomain/couplingmanager.hh:37
const Problem< i > & problem(Dune::index_constant< i > domainIdx) const
Return a reference to the sub problem.
Definition multidomain/couplingmanager.hh:318
SubSolutionVector< i > & curSol(Dune::index_constant< i > domainIdx)
the solution vector of the subproblem
Definition multidomain/couplingmanager.hh:347
A class managing an extended source stencil.
Definition embedded/extendedsourcestencil.hh:38
void evalAdditionalDomainDerivatives(CouplingManager &couplingManager, Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, JacobianMatrixDiagBlock &A, GridVariables &gridVariables) const
evaluate additional derivatives of the element residual of a domain with respect to dofs in the same ...
Definition embedded/extendedsourcestencil.hh:89
void extendJacobianPattern(const CouplingManager &couplingManager, 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 embedded/extendedsourcestencil.hh:60
const CouplingManager::template CouplingStencils< bulkIdx > & stencil() const
return a const reference to the stencil
Definition embedded/extendedsourcestencil.hh:180
void clear()
clear the internal data
Definition embedded/extendedsourcestencil.hh:173
CouplingManager::template CouplingStencils< bulkIdx > & stencil()
return a reference to the stencil
Definition embedded/extendedsourcestencil.hh:176
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition numericdifferentiation.hh:56
A helper class for local assemblers using numeric differentiation to determine the epsilon.
Definition numericepsilon.hh:33
Defines all properties used in Dumux.
Type traits for classes providing a grid discretization.
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:171
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:168
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
decltype(auto) gridDiscretization(const T &t, Args &&... args)
The grid discretization.
Definition griddiscretization.hh:98
Class representing dofs on elements for control-volume finite element schemes.
The available discretization methods in Dumux.
constexpr bool isCVFE
Definition method.hh:67
Definition circlepoints.hh:24
std::ranges::range auto scvs(const FVElementGeometry &fvGeometry, const LocalDof &localDof)
Definition localdof.hh:82
auto localDofs(const FVElementGeometry &fvGeometry)
range over local dofs
Definition localdof.hh:50
A class for numeric differentiation.
An adapter class for local assemblers using numeric differentiation.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.