3.2-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * 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_EMBEDDED_EXTENDEDSOURCESTENCIL_HH
26#define DUMUX_MULTIDOMAIN_EMBEDDED_EXTENDEDSOURCESTENCIL_HH
27
28#include <vector>
29
30#include <dune/common/indices.hh>
31
36
37namespace Dumux {
38namespace EmbeddedCoupling {
39
45template<class CouplingManager>
47{
48 using MDTraits = typename CouplingManager::MultiDomainTraits;
49 using Scalar = typename MDTraits::Scalar;
50
51 template<std::size_t id> using SubDomainTypeTag = typename MDTraits::template SubDomain<id>::TypeTag;
52 template<std::size_t id> using GridGeometry = GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>;
53 template<std::size_t id> using GridView = typename GridGeometry<id>::GridView;
54 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
55
56 static constexpr auto bulkIdx = typename MDTraits::template SubDomain<0>::Index();
57 static constexpr auto lowDimIdx = typename MDTraits::template SubDomain<1>::Index();
58
59 template<std::size_t id>
60 static constexpr bool isBox()
61 { return GridGeometry<id>::discMethod == DiscretizationMethod::box; }
62public:
69 template<std::size_t id, class JacobianPattern>
70 void extendJacobianPattern(const CouplingManager& couplingManager, Dune::index_constant<id> domainI, JacobianPattern& pattern) const
71 {
72 // add additional dof dependencies
73 for (const auto& element : elements(couplingManager.gridView(domainI)))
74 {
75 const auto& dofs = extendedSourceStencil_(couplingManager, domainI, element);
76
77 if (isBox<domainI>())
78 {
79 for (int i = 0; i < element.subEntities(GridView<domainI>::dimension); ++i)
80 for (const auto globalJ : dofs)
81 pattern.add(couplingManager.problem(domainI).gridGeometry().vertexMapper().subIndex(element, i, GridView<domainI>::dimension), globalJ);
82 }
83 else
84 {
85 const auto globalI = couplingManager.problem(domainI).gridGeometry().elementMapper().index(element);
86 for (const auto globalJ : dofs)
87 pattern.add(globalI, globalJ);
88 }
89 }
90 }
91
99 template<std::size_t i, class LocalAssemblerI, class SolutionVector, class JacobianMatrixDiagBlock, class GridVariables>
101 Dune::index_constant<i> domainI,
102 const LocalAssemblerI& localAssemblerI,
103 const SolutionVector& curSol,
104 JacobianMatrixDiagBlock& A,
105 GridVariables& gridVariables) const
106 {
107 constexpr auto numEq = std::decay_t<decltype(curSol[domainI][0])>::dimension;
108 const auto& elementI = localAssemblerI.element();
109
110 // only do something if we have an extended stencil
111 if (extendedSourceStencil_(couplingManager, domainI, elementI).empty())
112 return;
113
114 // compute the undeflected residual (source only!)
115 const auto origResidual = localAssemblerI.evalLocalSourceResidual(elementI);
116
117 // compute derivate for all additional dofs in the circle stencil
118 for (const auto dofIndex : extendedSourceStencil_(couplingManager, domainI, elementI))
119 {
120 auto partialDerivs = origResidual;
121 const auto origPriVars = curSol[domainI][dofIndex];
122
123 // calculate derivatives w.r.t to the privars at the dof at hand
124 for (int pvIdx = 0; pvIdx < numEq; pvIdx++)
125 {
126 // reset partial derivatives
127 partialDerivs = 0.0;
128
129 auto evalResiduals = [&](Scalar priVar)
130 {
131 // update the coupling context (solution vector and recompute element residual)
132 auto priVars = origPriVars;
133 priVars[pvIdx] = priVar;
134 couplingManager.updateCouplingContext(domainI, localAssemblerI, domainI, dofIndex, priVars, pvIdx);
135 return localAssemblerI.evalLocalSourceResidual(elementI);
136 };
137
138 // derive the residuals numerically
139 static const int numDiffMethod = getParam<int>("Assembly.NumericDifferenceMethod");
140 NumericDifferentiation::partialDerivative(evalResiduals, curSol[domainI][dofIndex][pvIdx],
141 partialDerivs, origResidual, numDiffMethod);
142
143 // update the global stiffness matrix with the current partial derivatives
144 for (auto&& scvJ : scvs(localAssemblerI.fvGeometry()))
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[scvJ.dofIndex()][dofIndex][eqIdx][pvIdx] += partialDerivs[scvJ.indexInElement()][eqIdx];
153 }
154 }
155
156 // restore the original coupling context
157 couplingManager.updateCouplingContext(domainI, localAssemblerI, domainI, dofIndex, origPriVars, pvIdx);
158 }
159 }
160 }
161
163 typename CouplingManager::CouplingStencils& stencil()
164 { return sourceStencils_; }
165
166private:
168 const std::vector<std::size_t>& extendedSourceStencil_(const CouplingManager& couplingManager, Dune::index_constant<0> bulkDomain, const Element<0>& bulkElement) const
169 {
170 const auto bulkElementIdx = couplingManager.problem(bulkIdx).gridGeometry().elementMapper().index(bulkElement);
171 if (sourceStencils_.count(bulkElementIdx))
172 return sourceStencils_.at(bulkElementIdx);
173 else
174 return couplingManager.emptyStencil();
175 }
176
178 const std::vector<std::size_t>& extendedSourceStencil_(const CouplingManager& couplingManager, Dune::index_constant<1> bulkDomain, const Element<1>& lowDimElement) const
179 { return couplingManager.emptyStencil(); }
180
182 typename CouplingManager::CouplingStencils sourceStencils_;
183};
184
185} // end namespace EmbeddedCoupling
186} // end namespace Dumux
187
188#endif
A class for numeric differentiation.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
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:152
Definition: adapt.hh:29
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:149
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with repect to a function parameter.
Definition: numericdifferentiation.hh:61
Definition: common/properties.hh:113
Definition: multidomain/couplingmanager.hh:46
const Problem< i > & problem(Dune::index_constant< i > domainIdx) const
Return a reference to the sub problem.
Definition: multidomain/couplingmanager.hh:264
A class managing an extended source stencil.
Definition: extendedsourcestencil.hh:47
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: extendedsourcestencil.hh:70
void evalAdditionalDomainDerivatives(CouplingManager &couplingManager, Dune::index_constant< i > domainI, const LocalAssemblerI &localAssemblerI, const SolutionVector &curSol, JacobianMatrixDiagBlock &A, GridVariables &gridVariables) const
evaluate additional derivatives of the element residual of a domain with respect to dofs in the same ...
Definition: extendedsourcestencil.hh:100
CouplingManager::CouplingStencils & stencil()
return a reference to the stencil
Definition: extendedsourcestencil.hh:163
Declares all properties used in Dumux.