GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/multidomain/boundary/freeflowporousmedium/ffmomentumpm/couplingmanager_staggered_cctpfa.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 74 85 87.1%
Functions: 9 20 45.0%
Branches: 30 98 30.6%

Line Branch Exec Source
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-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup FreeFlowPorousMediumCoupling
10 * \copydoc Dumux::FFMomentumPMCouplingManagerStaggeredCCTpfa
11 */
12
13 #ifndef DUMUX_MULTIDOMAIN_BOUNDARY_FFPM_FFMMOMENTUMPM_COUPLINGMANAGER_STAGGERED_TPFA_HH
14 #define DUMUX_MULTIDOMAIN_BOUNDARY_FFPM_FFMMOMENTUMPM_COUPLINGMANAGER_STAGGERED_TPFA_HH
15
16 #include <utility>
17 #include <memory>
18
19 #include <dune/common/float_cmp.hh>
20 #include <dune/common/exceptions.hh>
21 #include <dumux/common/properties.hh>
22 #include <dumux/discretization/staggered/elementsolution.hh>
23 #include <dumux/multidomain/couplingmanager.hh>
24
25 #include "couplingmapper_staggered_cctpfa.hh"
26
27 namespace Dumux {
28
29 /*!
30 * \ingroup FreeFlowPorousMediumCoupling
31 * \brief Coupling manager for Stokes and Darcy domains with equal dimension
32 * specialization for staggered-cctpfa coupling.
33 */
34 template<class MDTraits>
35 class FFMomentumPMCouplingManagerStaggeredCCTpfa
36 : public CouplingManager<MDTraits>
37 {
38 using Scalar = typename MDTraits::Scalar;
39 using ParentType = CouplingManager<MDTraits>;
40
41 public:
42 static constexpr auto freeFlowMomentumIndex = typename MDTraits::template SubDomain<0>::Index();
43 static constexpr auto porousMediumIndex = typename MDTraits::template SubDomain<1>::Index();
44
45 using SolutionVectorStorage = typename ParentType::SolutionVectorStorage;
46 private:
47 // obtain the type tags of the sub problems
48 using FreeFlowMomentumTypeTag = typename MDTraits::template SubDomain<freeFlowMomentumIndex>::TypeTag;
49 using PorousMediumTypeTag = typename MDTraits::template SubDomain<porousMediumIndex>::TypeTag;
50
51 using CouplingStencils = std::unordered_map<std::size_t, std::vector<std::size_t> >;
52 using CouplingStencil = CouplingStencils::mapped_type;
53
54 // the sub domain type tags
55 template<std::size_t id>
56 using SubDomainTypeTag = typename MDTraits::template SubDomain<id>::TypeTag;
57
58 template<std::size_t id> using GridView = typename GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>::GridView;
59 template<std::size_t id> using Problem = GetPropType<SubDomainTypeTag<id>, Properties::Problem>;
60 template<std::size_t id> using ElementVolumeVariables = typename GetPropType<SubDomainTypeTag<id>, Properties::GridVolumeVariables>::LocalView;
61 template<std::size_t id> using GridVolumeVariables = GetPropType<SubDomainTypeTag<id>, Properties::GridVolumeVariables>;
62 template<std::size_t id> using VolumeVariables = typename GetPropType<SubDomainTypeTag<id>, Properties::GridVolumeVariables>::VolumeVariables;
63 template<std::size_t id> using GridGeometry = GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>;
64 template<std::size_t id> using FVElementGeometry = typename GridGeometry<id>::LocalView;
65 template<std::size_t id> using GridVariables = GetPropType<SubDomainTypeTag<id>, Properties::GridVariables>;
66 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
67 template<std::size_t id> using PrimaryVariables = GetPropType<SubDomainTypeTag<id>, Properties::PrimaryVariables>;
68 template<std::size_t id> using SubControlVolumeFace = typename FVElementGeometry<id>::SubControlVolumeFace;
69 template<std::size_t id> using SubControlVolume = typename FVElementGeometry<id>::SubControlVolume;
70
71 using VelocityVector = typename Element<freeFlowMomentumIndex>::Geometry::GlobalCoordinate;
72
73 181550 struct FreeFlowMomentumCouplingContext
74 {
75 Element<porousMediumIndex> element;
76 VolumeVariables<porousMediumIndex> volVars;
77 FVElementGeometry<porousMediumIndex> fvGeometry;
78 std::size_t freeFlowMomentumScvfIdx;
79 std::size_t porousMediumScvfIdx;
80 std::size_t porousMediumDofIdx;
81 VelocityVector gravity;
82 };
83
84 6320 struct PorousMediumCouplingContext
85 {
86 Element<freeFlowMomentumIndex> element;
87 FVElementGeometry<freeFlowMomentumIndex> fvGeometry;
88 std::size_t porousMediumScvfIdx;
89 std::size_t freeFlowMomentumScvfIdx;
90 std::size_t freeFlowMomentumDofIdx;
91 VelocityVector faceVelocity;
92 };
93
94 using CouplingMapper = FFMomentumPMCouplingMapperStaggeredCCTpfa<MDTraits, FFMomentumPMCouplingManagerStaggeredCCTpfa<MDTraits>>;
95
96 public:
97
98 /*!
99 * \brief Methods to be accessed by main
100 */
101 // \{
102
103 //! Initialize the coupling manager
104 14 void init(std::shared_ptr<Problem<freeFlowMomentumIndex>> freeFlowMomentumProblem,
105 std::shared_ptr<Problem<porousMediumIndex>> porousMediumProblem,
106 SolutionVectorStorage& curSol)
107 {
108 28 this->setSubProblems(std::make_tuple(freeFlowMomentumProblem, porousMediumProblem));
109 14 this->attachSolution(curSol);
110
111 14 couplingMapper_.update(*this);
112 14 }
113
114 // \}
115
116
117 /*!
118 * \brief Methods to be accessed by the assembly
119 */
120 // \{
121
122 /*!
123 * \brief prepares all data and variables that are necessary to evaluate the residual (called from the local assembler)
124 */
125 template<std::size_t i, class Assembler>
126 void bindCouplingContext(Dune::index_constant<i> domainI, const Element<i>& element, const Assembler& assembler) const
127 {
128
0/18
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
371200 bindCouplingContext_(domainI, element);
129 }
130
131 /*!
132 * \brief Update the coupling context
133 */
134 template<std::size_t i, std::size_t j, class LocalAssemblerI>
135 1510080 void updateCouplingContext(Dune::index_constant<i> domainI,
136 const LocalAssemblerI& localAssemblerI,
137 Dune::index_constant<j> domainJ,
138 std::size_t dofIdxGlobalJ,
139 const PrimaryVariables<j>& priVarsJ,
140 int pvIdxJ)
141 {
142 73987200 this->curSol(domainJ)[dofIdxGlobalJ][pvIdxJ] = priVarsJ[pvIdxJ];
143
144 // we need to update all solution-depenent components of the coupling context
145 // the dof of domain J has been deflected
146
147 // update the faceVelocity in the PorousMediumCouplingContext
148 if constexpr (domainJ == freeFlowMomentumIndex)
149 {
150 // we only need to update if we are assembling the porous medium domain
151 // since the freeflow domain will not use the velocity from the context
152 if constexpr (domainI == porousMediumIndex)
153 {
154 12640 auto& context = std::get<porousMediumIndex>(couplingContext_);
155
4/4
✓ Branch 0 taken 6320 times.
✓ Branch 1 taken 6320 times.
✓ Branch 2 taken 6320 times.
✓ Branch 3 taken 6320 times.
50560 for (auto& c : context)
156 {
157
1/2
✓ Branch 0 taken 6320 times.
✗ Branch 1 not taken.
12640 if (c.freeFlowMomentumDofIdx == dofIdxGlobalJ)
158 {
159 25280 const auto& scvf = localAssemblerI.fvGeometry().scvf(c.porousMediumScvfIdx);
160 12640 c.faceVelocity = faceVelocity(localAssemblerI.element(), scvf);
161 }
162 }
163 }
164 }
165
166 // update volVars in the FreeFlowMomentumCouplingContext
167 else if (domainJ == porousMediumIndex)
168 {
169 1497440 auto& context = std::get<freeFlowMomentumIndex>(couplingContext_);
170
4/4
✓ Branch 0 taken 6320 times.
✓ Branch 1 taken 748720 times.
✓ Branch 2 taken 6320 times.
✓ Branch 3 taken 748720 times.
4504960 for (auto& c : context)
171 {
172
1/2
✓ Branch 0 taken 6320 times.
✗ Branch 1 not taken.
12640 if (c.porousMediumDofIdx == dofIdxGlobalJ)
173 {
174 12640 const auto& ggJ = c.fvGeometry.gridGeometry();
175 12640 const auto& scv = *scvs(c.fvGeometry).begin();
176 25280 const auto elemSol = elementSolution(c.element, this->curSol(domainJ), ggJ);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6320 times.
12640 c.volVars.update(elemSol, this->problem(domainJ), c.element, scv);
178 }
179 }
180 }
181 1510080 }
182
183 // \}
184
185 /*!
186 * \brief Access the coupling context needed for the Stokes domain
187 */
188 template<std::size_t i>
189 178390 const auto& couplingContext(Dune::index_constant<i> domainI,
190 const FVElementGeometry<i>& fvGeometry,
191 const SubControlVolumeFace<i> scvf) const
192 {
193
1/2
✓ Branch 0 taken 178390 times.
✗ Branch 1 not taken.
178390 auto& contexts = std::get<i>(couplingContext_);
194
195
5/10
✓ Branch 0 taken 178390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 178390 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 178390 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 178390 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 178390 times.
✗ Branch 9 not taken.
356780 if (contexts.empty() || couplingContextBoundForElement_[i] != scvf.insideScvIdx())
196 178390 bindCouplingContext_(domainI, fvGeometry);
197
198
2/4
✓ Branch 0 taken 178390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 178390 times.
✗ Branch 3 not taken.
535170 for (const auto& context : contexts)
199 {
200 178390 const auto expectedScvfIdx = domainI == freeFlowMomentumIndex ? context.freeFlowMomentumScvfIdx : context.porousMediumScvfIdx;
201
1/2
✓ Branch 0 taken 178390 times.
✗ Branch 1 not taken.
178390 if (scvf.index() == expectedScvfIdx)
202 178390 return context;
203 }
204
205 DUNE_THROW(Dune::InvalidStateException, "No coupling context found at scvf " << scvf.center());
206 }
207
208 /*!
209 * \brief The coupling stencils
210 */
211 // \{
212
213 /*!
214 * \brief The Stokes cell center coupling stencil w.r.t. Darcy DOFs
215 */
216 const CouplingStencil& couplingStencil(Dune::index_constant<porousMediumIndex> domainI,
217 const Element<porousMediumIndex>& element,
218 Dune::index_constant<freeFlowMomentumIndex> domainJ) const
219 {
220 const auto eIdx = this->problem(domainI).gridGeometry().elementMapper().index(element);
221 return couplingMapper_.couplingStencil(domainI, eIdx, domainJ);
222 }
223
224 /*!
225 * \brief returns an iterable container of all indices of degrees of freedom of domain j
226 * that couple with / influence the residual of the given sub-control volume of domain i
227 *
228 * \param domainI the domain index of domain i
229 * \param elementI the coupled element of domain í
230 * \param scvI the sub-control volume of domain i
231 * \param domainJ the domain index of domain j
232 */
233 const CouplingStencil& couplingStencil(Dune::index_constant<freeFlowMomentumIndex> domainI,
234 const Element<freeFlowMomentumIndex>& elementI,
235 const SubControlVolume<freeFlowMomentumIndex>& scvI,
236 Dune::index_constant<porousMediumIndex> domainJ) const
237 {
238
2/8
✓ Branch 1 taken 1484800 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 540800 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
2025600 return couplingMapper_.couplingStencil(domainI, elementI, scvI, domainJ);
239 }
240
241 using ParentType::evalCouplingResidual;
242
243 /*!
244 * \brief evaluate the coupling residual
245 * special interface for fcstaggered methods
246 */
247 template<class LocalAssemblerI, std::size_t j>
248 decltype(auto) evalCouplingResidual(Dune::index_constant<freeFlowMomentumIndex> domainI,
249 const LocalAssemblerI& localAssemblerI,
250 const SubControlVolume<freeFlowMomentumIndex>& scvI,
251 Dune::index_constant<j> domainJ,
252 std::size_t dofIdxGlobalJ) const
253 {
254
1/6
✓ Branch 1 taken 3160 times.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
6320 return localAssemblerI.evalLocalResidual();
255 }
256
257 // \}
258
259 /*!
260 * \brief Returns whether a given scvf is coupled to the other domain
261 */
262 bool isCoupledLateralScvf(Dune::index_constant<freeFlowMomentumIndex> domainI, const SubControlVolumeFace<freeFlowMomentumIndex>& scvf) const
263 { return couplingMapper_.isCoupledLateralScvf(domainI, scvf); }
264
265 /*!
266 * \brief Returns whether a given scvf is coupled to the other domain
267 */
268 template<std::size_t i>
269 bool isCoupled(Dune::index_constant<i> domainI, const SubControlVolumeFace<i>& scvf) const
270 {
271 if constexpr (i == freeFlowMomentumIndex)
272 return couplingMapper_.isCoupled(domainI, scvf) || couplingMapper_.isCoupledLateralScvf(domainI, scvf);
273 else
274 return couplingMapper_.isCoupled(domainI, scvf);
275 }
276
277 /*!
278 * \brief If the boundary entity is on a coupling boundary
279 * \param domainI the domain index for which to compute the flux
280 * \param scv the sub control volume
281 */
282 bool isCoupled(Dune::index_constant<freeFlowMomentumIndex> domainI,
283 const SubControlVolume<freeFlowMomentumIndex>& scv) const
284 { return couplingMapper_.isCoupled(domainI, scv); }
285
286 /*!
287 * \brief Returns the velocity at a given sub control volume face.
288 */
289 9480 auto faceVelocity(const Element<porousMediumIndex>& element,
290 const SubControlVolumeFace<porousMediumIndex>& scvf) const
291 {
292 // create a unit normal vector oriented in positive coordinate direction
293 9480 auto velocity = scvf.unitOuterNormal();
294 using std::abs;
295 56880 std::for_each(velocity.begin(), velocity.end(), [](auto& v){ v = abs(v); });
296
297 // create the actual velocity vector
298 18960 velocity *= this->curSol(freeFlowMomentumIndex)[
299 couplingMapper_.outsideDofIndex(Dune::index_constant<porousMediumIndex>(), scvf)
300 ];
301
302 9480 return velocity;
303 }
304
305 private:
306 //! Return the volume variables of domain i for a given element and scv
307 template<std::size_t i>
308 181550 VolumeVariables<i> volVars_(Dune::index_constant<i> domainI,
309 const Element<i>& element,
310 const SubControlVolume<i>& scv) const
311 {
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181550 times.
181550 VolumeVariables<i> volVars;
313 363100 const auto elemSol = elementSolution(
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181550 times.
181550 element, this->curSol(domainI), this->problem(domainI).gridGeometry()
315 );
316 363100 volVars.update(elemSol, this->problem(domainI), element, scv);
317 181550 return volVars;
318 }
319
320 /*!
321 * \brief Returns whether a given scvf is coupled to the other domain
322 */
323 template<std::size_t i>
324 bool isCoupledElement_(Dune::index_constant<i> domainI, std::size_t eIdx) const
325 1841580 { return couplingMapper_.isCoupledElement(domainI, eIdx); }
326
327 /*!
328 * \brief prepares all data and variables that are necessary to evaluate the residual of an Darcy element (i.e. Stokes information)
329 */
330 template<std::size_t i>
331 742400 void bindCouplingContext_(Dune::index_constant<i> domainI, const Element<i>& element) const
332 {
333
4/8
✗ Branch 0 not taken.
✓ Branch 1 taken 371200 times.
✓ Branch 3 taken 371200 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 371200 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 371200 times.
✗ Branch 10 not taken.
2227200 const auto fvGeometry = localView(this->problem(domainI).gridGeometry()).bindElement(element);
334
1/2
✓ Branch 1 taken 371200 times.
✗ Branch 2 not taken.
742400 bindCouplingContext_(domainI, fvGeometry);
335 742400 }
336
337 /*!
338 * \brief prepares all data and variables that are necessary to evaluate the residual of an Darcy element (i.e. Stokes information)
339 */
340 template<std::size_t i>
341 920790 void bindCouplingContext_(Dune::index_constant<i> domainI, const FVElementGeometry<i>& fvGeometry) const
342 {
343 920790 auto& context = std::get<domainI>(couplingContext_);
344 920790 context.clear();
345
346 920790 const auto eIdx = this->problem(domainI).gridGeometry().elementMapper().index(fvGeometry.element());
347
348 // do nothing if the element is not coupled to the other domain
349 1470380 if (!isCoupledElement_(domainI, eIdx))
350 return;
351
352 184710 couplingContextBoundForElement_[domainI] = eIdx;
353
354 2563222 for (const auto& scvf : scvfs(fvGeometry))
355 {
356 2375352 if (couplingMapper_.isCoupled(domainI, scvf))
357 {
358 if constexpr (domainI == freeFlowMomentumIndex)
359 {
360 181550 const auto otherElementIdx = couplingMapper_.outsideElementIndex(domainI, scvf);
361 constexpr auto domainJ = Dune::index_constant<1-i>();
362 181550 const auto& otherGridGeometry = this->problem(domainJ).gridGeometry();
363 181550 const auto& otherElement = otherGridGeometry.element(otherElementIdx);
364 544650 auto otherFvGeometry = localView(otherGridGeometry).bindElement(otherElement);
365
366 // there is only one scv for TPFA
367 544650 context.push_back({
368 otherElement,
369 544650 volVars_(domainJ, otherElement, *std::begin(scvs(otherFvGeometry))),
370 181550 std::move(otherFvGeometry),
371 scvf.index(),
372 couplingMapper_.flipScvfIndex(domainI, scvf),
373 otherElementIdx,
374 181550 this->problem(domainJ).spatialParams().gravity(scvf.center())
375 });
376 }
377
378 else if constexpr (domainI == porousMediumIndex)
379 {
380 3160 const auto otherElementIdx = couplingMapper_.outsideElementIndex(domainI, scvf);
381 constexpr auto domainJ = Dune::index_constant<1-i>();
382 3160 const auto& otherGridGeometry = this->problem(domainJ).gridGeometry();
383 3160 const auto& otherElement = otherGridGeometry.element(otherElementIdx);
384 6320 auto otherFvGeometry = localView(otherGridGeometry).bindElement(otherElement);
385
386 3160 const auto otherScvfIdx = couplingMapper_.flipScvfIndex(domainI, scvf);
387 3160 const auto& otherScvf = otherFvGeometry.scvf(otherScvfIdx);
388 6320 const auto& otherScv = otherFvGeometry.scv(otherScvf.insideScvIdx());
389
390 6320 context.push_back({
391 otherElement,
392 3160 std::move(otherFvGeometry),
393 scvf.index(),
394 otherScvfIdx,
395 otherScv.dofIndex(),
396 faceVelocity(fvGeometry.element(), scvf)
397 });
398 }
399 }
400 }
401 }
402
403 mutable std::tuple<std::vector<FreeFlowMomentumCouplingContext>, std::vector<PorousMediumCouplingContext>> couplingContext_;
404 mutable std::array<std::size_t, 2> couplingContextBoundForElement_;
405
406 CouplingMapper couplingMapper_;
407 };
408
409 } // end namespace Dumux
410
411 #endif
412