version 3.8
multidomain/facet/box/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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_BOX_FACETCOUPLING_MANAGER_HH
13#define DUMUX_BOX_FACETCOUPLING_MANAGER_HH
14
15#include <algorithm>
16#include <cassert>
17
24
25namespace Dumux {
26
38template<class MDTraits, class CouplingMapper, std::size_t bulkDomainId, std::size_t lowDimDomainId>
39class FacetCouplingManager<MDTraits, CouplingMapper, bulkDomainId, lowDimDomainId, DiscretizationMethods::Box>
40: public virtual CouplingManager< MDTraits >
41{
43
44 // convenience aliases and instances of the two domain ids
45 using BulkIdType = typename MDTraits::template SubDomain<bulkDomainId>::Index;
46 using LowDimIdType = typename MDTraits::template SubDomain<lowDimDomainId>::Index;
47 static constexpr auto bulkId = BulkIdType();
48 static constexpr auto lowDimId = LowDimIdType();
49
50 // the sub-domain type tags
51 template<std::size_t id> using SubDomainTypeTag = typename MDTraits::template SubDomain<id>::TypeTag;
52
53 // further types specific to the sub-problems
54 template<std::size_t id> using PrimaryVariables = GetPropType<SubDomainTypeTag<id>, Properties::PrimaryVariables>;
55 template<std::size_t id> using Problem = GetPropType<SubDomainTypeTag<id>, Properties::Problem>;
56 template<std::size_t id> using NumEqVector = Dumux::NumEqVector<PrimaryVariables<id>>;
57 template<std::size_t id> using ElementBoundaryTypes = GetPropType<SubDomainTypeTag<id>, Properties::ElementBoundaryTypes>;
58 template<std::size_t id> using LocalResidual = typename MDTraits::template SubDomain<id>::LocalResidual;
59
60 template<std::size_t id> using GridGeometry = GetPropType<SubDomainTypeTag<id>, Properties::GridGeometry>;
61 template<std::size_t id> using FVElementGeometry = typename GridGeometry<id>::LocalView;
62 template<std::size_t id> using SubControlVolume = typename GridGeometry<id>::SubControlVolume;
63 template<std::size_t id> using SubControlVolumeFace = typename GridGeometry<id>::SubControlVolumeFace;
64 template<std::size_t id> using GridView = typename GridGeometry<id>::GridView;
65 template<std::size_t id> using Element = typename GridView<id>::template Codim<0>::Entity;
66 template<std::size_t id> using GridIndexType = typename GridView<id>::IndexSet::IndexType;
67
68 template<std::size_t id> using GridVariables = GetPropType<SubDomainTypeTag<id>, Properties::GridVariables>;
69 template<std::size_t id> using GridVolumeVariables = typename GridVariables<id>::GridVolumeVariables;
70 template<std::size_t id> using ElementVolumeVariables = typename GridVolumeVariables<id>::LocalView;
71 template<std::size_t id> using VolumeVariables = typename ElementVolumeVariables<id>::VolumeVariables;
72 template<std::size_t id> using GridFluxVariablesCache = typename GridVariables<id>::GridFluxVariablesCache;
73 template<std::size_t id> using ElementFluxVariablesCache = typename GridFluxVariablesCache<id>::LocalView;
74
75 // extract corresponding grid ids from the mapper
76 static constexpr int bulkDim = GridView<bulkDomainId>::dimension;
77 static constexpr int lowDimDim = GridView<lowDimDomainId>::dimension;
78 static constexpr auto bulkGridId = CouplingMapper::template gridId<bulkDim>();
79 static constexpr auto lowDimGridId = CouplingMapper::template gridId<lowDimDim>();
80
81 static constexpr bool lowDimUsesBox = GridGeometry<lowDimId>::discMethod == DiscretizationMethods::box;
82
89 struct BulkCouplingContext
90 {
91 bool isSet;
92 GridIndexType< bulkId > elementIdx;
93 std::vector< FVElementGeometry<lowDimId> > lowDimFvGeometries;
94 std::vector< std::vector<VolumeVariables<lowDimId>> > lowDimElemVolVars;
95
96 void reset()
97 {
98 lowDimFvGeometries.clear();
99 lowDimElemVolVars.clear();
100 isSet = false;
101 }
102 };
103
114 struct LowDimCouplingContext
115 {
116 private:
117 // For dim == dimworld in bulk domain, we know there is always going
118 // to be two bulk neighbors for a lower-dimensional element. Choose
119 // the container type depending on this
120 static constexpr int bulkDimWorld = GridView<bulkId>::dimensionworld;
121 static constexpr bool bulkIsSurfaceGrid = bulkDim != bulkDimWorld;
122
123 template< class T >
124 using ContainerType = typename std::conditional< bulkIsSurfaceGrid,
125 std::vector< T >,
126 std::array< T, 2 > >::type;
127
128 public:
129 bool isSet;
130 GridIndexType< lowDimId > elementIdx;
131 ContainerType< std::unique_ptr<FVElementGeometry<bulkId>> > bulkFvGeometries;
132 ContainerType< std::unique_ptr<ElementVolumeVariables<bulkId>> > bulkElemVolVars;
133 ContainerType< std::unique_ptr<ElementFluxVariablesCache<bulkId>> > bulkElemFluxVarsCache;
134 std::unique_ptr< LocalResidual<bulkId> > bulkLocalResidual;
135 ContainerType< ElementBoundaryTypes<bulkId> > bulkElemBcTypes;
136
137 void reset()
138 {
139 isSet = false;
140 auto resetPtr = [&] (auto&& uniquePtr) { uniquePtr.reset(); };
141 std::for_each(bulkFvGeometries.begin(), bulkFvGeometries.end(), resetPtr);
142 std::for_each(bulkElemVolVars.begin(), bulkElemVolVars.end(), resetPtr);
143 std::for_each(bulkElemFluxVarsCache.begin(), bulkElemFluxVarsCache.end(), resetPtr);
144 bulkLocalResidual.reset();
145 }
146
147 // resize containers for surface grids
148 template<bool s = bulkIsSurfaceGrid, std::enable_if_t<s, int> = 0>
149 void resize(std::size_t numEmbedments)
150 {
151 bulkFvGeometries.resize(numEmbedments);
152 bulkElemVolVars.resize(numEmbedments);
153 bulkElemFluxVarsCache.resize(numEmbedments);
154 bulkElemBcTypes.resize(numEmbedments);
155 }
156
157 // no memory reservation necessary for non-surface grids
158 template<bool s = bulkIsSurfaceGrid, std::enable_if_t<!s, int> = 0>
159 void resize(std::size_t numEmbedments)
160 {}
161 };
162
163public:
164
166 template<std::size_t i, std::size_t j = (i == bulkId) ? lowDimId : bulkId>
167 using CouplingStencilType = typename CouplingMapper::template Stencil< CouplingMapper::template gridId<GridView<j>::dimension>() >;
168
170 using SolutionVector = typename MDTraits::SolutionVector;
171
180 void init(std::shared_ptr< Problem<bulkId> > bulkProblem,
181 std::shared_ptr< Problem<lowDimId> > lowDimProblem,
182 std::shared_ptr< CouplingMapper > couplingMapper,
183 const SolutionVector& curSol)
184 {
185 couplingMapperPtr_ = couplingMapper;
186
187 // set the sub problems
188 this->setSubProblem(bulkProblem, bulkId);
189 this->setSubProblem(lowDimProblem, lowDimId);
190
191 // copy the solution vector
192 ParentType::updateSolution(curSol);
193
194 // determine all bulk elements that couple to low dim elements
195 const auto& bulkMap = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
196 bulkElemIsCoupled_.assign(bulkProblem->gridGeometry().gridView().size(0), false);
197 std::for_each( bulkMap.begin(),
198 bulkMap.end(),
199 [&] (const auto& entry) { bulkElemIsCoupled_[entry.first] = true; });
200 }
201
206 const Element<bulkId>& element,
207 LowDimIdType domainJ) const
208 {
209 const auto eIdx = this->problem(domainI).gridGeometry().elementMapper().index(element);
210
211 if (bulkElemIsCoupled_[eIdx])
212 {
213 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
214 auto it = map.find(eIdx);
215 assert(it != map.end());
216 return it->second.couplingStencil;
217 }
218
219 return getEmptyStencil(lowDimId);
220 }
221
226 const Element<lowDimId>& element,
227 BulkIdType domainJ) const
228 {
229 const auto eIdx = this->problem(lowDimId).gridGeometry().elementMapper().index(element);
230
231 const auto& map = couplingMapperPtr_->couplingMap(lowDimGridId, bulkGridId);
232 auto it = map.find(eIdx);
233 if (it != map.end()) return it->second.couplingStencil;
234 else return getEmptyStencil(bulkId);
235 }
236
241 bool isCoupled(const Element<bulkId>& element,
242 const SubControlVolumeFace<bulkId>& scvf) const
243 { return scvf.interiorBoundary(); }
244
249 bool isOnInteriorBoundary(const Element<bulkId>& element,
250 const SubControlVolumeFace<bulkId>& scvf) const
251 { return isCoupled(element, scvf); }
252
256 const VolumeVariables<lowDimId>& getLowDimVolVars(const Element<bulkId>& element,
257 const SubControlVolumeFace<bulkId>& scvf) const
258 {
259 const auto eIdx = this->problem(bulkId).gridGeometry().elementMapper().index(element);
260 assert(bulkContext_.isSet);
261 assert(bulkElemIsCoupled_[eIdx]);
262
263 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
264 const auto& couplingData = map.find(eIdx)->second;
265
266 // search the low dim element idx this scvf is embedded in
267 // and find out the local index of the scv it couples to
268 unsigned int coupledScvIdx;
269 auto it = std::find_if( couplingData.elementToScvfMap.begin(),
270 couplingData.elementToScvfMap.end(),
271 [&] (auto& dataPair)
272 {
273 const auto& scvfList = dataPair.second;
274 auto it = std::find(scvfList.begin(), scvfList.end(), scvf.index());
275 coupledScvIdx = std::distance(scvfList.begin(), it);
276 return it != scvfList.end();
277 } );
278
279 assert(it != couplingData.elementToScvfMap.end());
280 const auto lowDimElemIdx = it->first;
281 const auto& s = map.find(bulkContext_.elementIdx)->second.couplingElementStencil;
282 const auto& idxInContext = std::distance( s.begin(), std::find(s.begin(), s.end(), lowDimElemIdx) );
283 assert(std::find(s.begin(), s.end(), lowDimElemIdx) != s.end());
284
285 if (lowDimUsesBox)
286 return bulkContext_.lowDimElemVolVars[idxInContext][coupledScvIdx];
287 else
288 return bulkContext_.lowDimElemVolVars[idxInContext][0];
289 }
290
294 const Element<lowDimId> getLowDimElement(const Element<bulkId>& element,
295 const SubControlVolumeFace<bulkId>& scvf) const
296 {
297 const auto lowDimElemIdx = getLowDimElementIndex(element, scvf);
298 return this->problem(lowDimId).gridGeometry().element(lowDimElemIdx);
299 }
300
304 const GridIndexType<lowDimId> getLowDimElementIndex(const Element<bulkId>& element,
305 const SubControlVolumeFace<bulkId>& scvf) const
306 {
307 const auto eIdx = this->problem(bulkId).gridGeometry().elementMapper().index(element);
308
309 assert(bulkElemIsCoupled_[eIdx]);
310 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
311 const auto& couplingData = map.at(eIdx);
312
313 // search the low dim element idx this scvf is embedded in
314 auto it = std::find_if( couplingData.elementToScvfMap.begin(),
315 couplingData.elementToScvfMap.end(),
316 [&] (auto& dataPair)
317 {
318 const auto& scvfList = dataPair.second;
319 auto it = std::find(scvfList.begin(), scvfList.end(), scvf.index());
320 return it != scvfList.end();
321 } );
322
323 assert(it != couplingData.elementToScvfMap.end());
324 return it->first;
325 }
326
333 template< class BulkLocalAssembler >
334 typename LocalResidual<bulkId>::ElementResidualVector
336 const BulkLocalAssembler& bulkLocalAssembler,
337 LowDimIdType,
338 GridIndexType<lowDimId> dofIdxGlobalJ)
339 {
340 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
341
342 assert(bulkContext_.isSet);
343 assert(bulkElemIsCoupled_[bulkContext_.elementIdx]);
344 assert(map.find(bulkContext_.elementIdx) != map.end());
345 assert(bulkContext_.elementIdx == this->problem(bulkId).gridGeometry().elementMapper().index(bulkLocalAssembler.element()));
346
347 const auto& fvGeometry = bulkLocalAssembler.fvGeometry();
348 typename LocalResidual<bulkId>::ElementResidualVector res(fvGeometry.numScv());
349 res = 0.0;
350
351 // compute fluxes across the coupling scvfs
352 const auto& couplingScvfs = map.find(bulkContext_.elementIdx)->second.dofToCouplingScvfMap.at(dofIdxGlobalJ);
353 for (auto scvfIdx : couplingScvfs)
354 {
355 const auto& scvf = fvGeometry.scvf(scvfIdx);
356 const auto& insideScv = fvGeometry.scv(scvf.insideScvIdx());
357 res[insideScv.localDofIndex()] += evalBulkFluxes_( bulkLocalAssembler.element(),
358 bulkLocalAssembler.fvGeometry(),
359 bulkLocalAssembler.curElemVolVars(),
360 bulkLocalAssembler.elemBcTypes(),
361 bulkLocalAssembler.elemFluxVarsCache(),
362 bulkLocalAssembler.localResidual(),
363 std::array<GridIndexType<bulkId>, 1>({scvfIdx}) );
364 }
365
366 return res;
367 }
368
375 template< class LowDimLocalAssembler >
376 typename LocalResidual<lowDimId>::ElementResidualVector
378 const LowDimLocalAssembler& lowDimLocalAssembler,
379 BulkIdType,
380 GridIndexType<bulkId> dofIdxGlobalJ)
381 {
382 // make sure this is called for the element for which the context was set
383 assert(lowDimContext_.isSet);
384 assert(this->problem(lowDimId).gridGeometry().elementMapper().index(lowDimLocalAssembler.element()) == lowDimContext_.elementIdx);
385
386 // evaluate sources for all scvs in lower-dimensional element
387 typename LocalResidual<lowDimId>::ElementResidualVector res(lowDimLocalAssembler.fvGeometry().numScv());
388 res = 0.0;
389 for (const auto& scv : scvs(lowDimLocalAssembler.fvGeometry()))
390 res[scv.localDofIndex()] -= evalSourcesFromBulk(lowDimLocalAssembler.element(),
391 lowDimLocalAssembler.fvGeometry(),
392 lowDimLocalAssembler.curElemVolVars(),
393 scv);
394 return res;
395 }
396
400 NumEqVector<lowDimId> evalSourcesFromBulk(const Element<lowDimId>& element,
401 const FVElementGeometry<lowDimId>& fvGeometry,
402 const ElementVolumeVariables<lowDimId>& elemVolVars,
403 const SubControlVolume<lowDimId>& scv)
404 {
405 // make sure the this is called for the element of the context
406 assert(this->problem(lowDimId).gridGeometry().elementMapper().index(element) == lowDimContext_.elementIdx);
407
408 NumEqVector<lowDimId> sources(0.0);
409 const auto& map = couplingMapperPtr_->couplingMap(lowDimGridId, bulkGridId);
410 auto it = map.find(lowDimContext_.elementIdx);
411 if (it == map.end())
412 return sources;
413
414 assert(lowDimContext_.isSet);
415 for (unsigned int i = 0; i < it->second.embedments.size(); ++i)
416 {
417 const auto& embedment = it->second.embedments[i];
418
419 // list of scvfs in the bulk domain whose fluxes enter this scv
420 // if low dim domain uses tpfa, this is all scvfs lying on this element
421 // if it uses box, it is the one scvf coinciding with the given scv
422 const auto& coincidingScvfs = embedment.second;
423 const auto& scvfList = lowDimUsesBox ? std::vector<GridIndexType<lowDimId>>{ coincidingScvfs[scv.localDofIndex()] }
424 : coincidingScvfs;
425
426 sources += evalBulkFluxes_(this->problem(bulkId).gridGeometry().element(embedment.first),
427 *(lowDimContext_.bulkFvGeometries[i]),
428 *(lowDimContext_.bulkElemVolVars[i]),
429 lowDimContext_.bulkElemBcTypes[i],
430 *(lowDimContext_.bulkElemFluxVarsCache[i]),
431 *lowDimContext_.bulkLocalResidual,
432 scvfList);
433 }
434
435 return sources;
436 }
437
443 template< class Assembler >
444 void bindCouplingContext(BulkIdType, const Element<bulkId>& element, const Assembler& assembler)
445 {
446 // clear context
447 bulkContext_.reset();
448
449 // set index in context in any case
450 const auto bulkElemIdx = this->problem(bulkId).gridGeometry().elementMapper().index(element);
451 bulkContext_.elementIdx = bulkElemIdx;
452
453 // if element is coupled, actually set the context
454 if (bulkElemIsCoupled_[bulkElemIdx])
455 {
456 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
457
458 auto it = map.find(bulkElemIdx); assert(it != map.end());
459 const auto& elementStencil = it->second.couplingElementStencil;
460 bulkContext_.lowDimFvGeometries.reserve(elementStencil.size());
461 bulkContext_.lowDimElemVolVars.reserve(elementStencil.size());
462
463 // local view on the bulk fv geometry
464 const auto bulkFvGeometry = localView(this->problem(bulkId).gridGeometry()).bindElement(element);
465
466 const auto& ldGridGeometry = this->problem(lowDimId).gridGeometry();
467 auto fvGeom = localView(ldGridGeometry);
468 for (const auto lowDimElemIdx : elementStencil)
469 {
470 const auto& ldSol = assembler.isImplicit() ? this->curSol(lowDimId) : assembler.prevSol()[lowDimId];
471 const auto elemJ = ldGridGeometry.element(lowDimElemIdx);
472 fvGeom.bindElement(elemJ);
473
474 std::vector<VolumeVariables<lowDimId>> elemVolVars(fvGeom.numScv());
475 const auto& coupledScvfIndices = it->second.elementToScvfMap.at(lowDimElemIdx);
476 makeCoupledLowDimElemVolVars_(element, bulkFvGeometry, elemJ, fvGeom, ldSol, coupledScvfIndices, elemVolVars);
477
478 bulkContext_.isSet = true;
479 bulkContext_.lowDimFvGeometries.emplace_back( std::move(fvGeom) );
480 bulkContext_.lowDimElemVolVars.emplace_back( std::move(elemVolVars) );
481 }
482 }
483 }
484
494 template< class Assembler >
495 void bindCouplingContext(LowDimIdType, const Element<lowDimId>& element, const Assembler& assembler)
496 {
497 // reset contexts
498 bulkContext_.reset();
499 lowDimContext_.reset();
500
501 // set index in context in any case
502 const auto lowDimElemIdx = this->problem(lowDimId).gridGeometry().elementMapper().index(element);
503 lowDimContext_.elementIdx = lowDimElemIdx;
504
505 const auto& map = couplingMapperPtr_->couplingMap(lowDimGridId, bulkGridId);
506 auto it = map.find(lowDimElemIdx);
507
508 // if element is coupled, actually set the context
509 if (it != map.end())
510 {
511 // first bind the low dim context for the first neighboring bulk element
512 // this will set up the lower-dimensional data on this current lowdim element
513 // which will be enough to compute the fluxes in all neighboring elements
514 const auto& bulkGridGeom = this->problem(bulkId).gridGeometry();
515 const auto bulkElem = bulkGridGeom.element(it->second.embedments[0].first);
516 bindCouplingContext(bulkId, bulkElem, assembler);
517
518 // reserve space in the context and bind local views of neighbors
519 const auto& embedments = it->second.embedments;
520 const auto numEmbedments = embedments.size();
521 lowDimContext_.resize(numEmbedments);
522
523 auto bulkFvGeom = localView(bulkGridGeom);
524 auto bulkElemVolVars = localView(assembler.gridVariables(bulkId).curGridVolVars());
525 auto bulkElemFluxVarsCache = localView(assembler.gridVariables(bulkId).gridFluxVarsCache());
526
527 for (unsigned int i = 0; i < numEmbedments; ++i)
528 {
529 const auto& bulkSol = assembler.isImplicit() ? this->curSol(bulkId) : assembler.prevSol()[bulkId];
530 const auto curBulkElem = bulkGridGeom.element(embedments[i].first);
531
532 bulkFvGeom.bind(curBulkElem);
533 bulkElemVolVars.bind(curBulkElem, bulkFvGeom, bulkSol);
534 bulkElemFluxVarsCache.bind(curBulkElem, bulkFvGeom, bulkElemVolVars);
535
536 lowDimContext_.isSet = true;
537 lowDimContext_.bulkElemBcTypes[i].update(this->problem(bulkId), curBulkElem, bulkFvGeom);
538 lowDimContext_.bulkFvGeometries[i] = std::make_unique< FVElementGeometry<bulkId> >( std::move(bulkFvGeom) );
539 lowDimContext_.bulkElemVolVars[i] = std::make_unique< ElementVolumeVariables<bulkId> >( std::move(bulkElemVolVars) );
540 lowDimContext_.bulkElemFluxVarsCache[i] = std::make_unique< ElementFluxVariablesCache<bulkId> >( std::move(bulkElemFluxVarsCache) );
541 }
542
543 // finally, set the local residual
544 lowDimContext_.bulkLocalResidual = std::make_unique< LocalResidual<bulkId> >(assembler.localResidual(bulkId));
545 }
546 }
547
552 template< class BulkLocalAssembler >
553 void updateCouplingContext(BulkIdType domainI,
554 const BulkLocalAssembler& bulkLocalAssembler,
555 LowDimIdType domainJ,
556 GridIndexType<lowDimId> dofIdxGlobalJ,
557 const PrimaryVariables<lowDimId>& priVarsJ,
558 unsigned int pvIdxJ)
559 {
560 // communicate deflected solution
561 ParentType::updateCouplingContext(domainI, bulkLocalAssembler, domainJ, dofIdxGlobalJ, priVarsJ, pvIdxJ);
562
563 // Since coupling only occurs via the fluxes, the context does not
564 // have to be updated in explicit time discretization schemes, where
565 // they are strictly evaluated on the old time level
566 if (!bulkLocalAssembler.isImplicit())
567 return;
568
569 // skip the rest if context is empty
570 if (bulkContext_.isSet)
571 {
572 const auto& map = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
573 const auto& couplingEntry = map.at(bulkContext_.elementIdx);
574 const auto& couplingElemStencil = couplingEntry.couplingElementStencil;
575 const auto& ldGridGeometry = this->problem(lowDimId).gridGeometry();
576
577 // find the low-dim elements in coupling stencil, where this dof is contained in
578 const auto couplingElements = [&] ()
579 {
580 if (lowDimUsesBox)
581 {
582 std::vector< Element<lowDimId> > lowDimElems;
583 std::for_each( couplingElemStencil.begin(), couplingElemStencil.end(),
584 [&] (auto lowDimElemIdx)
585 {
586 auto element = ldGridGeometry.element(lowDimElemIdx);
587 for (unsigned int i = 0; i < element.geometry().corners(); ++i)
588 {
589 const auto dofIdx = ldGridGeometry.vertexMapper().subIndex(element, i, lowDimDim);
590 if (dofIdxGlobalJ == dofIdx) { lowDimElems.emplace_back( std::move(element) ); break; }
591 }
592 } );
593 return lowDimElems;
594 }
595 // dof index = element index for cc schemes
596 else
597 return std::vector<Element<lowDimId>>( {ldGridGeometry.element(dofIdxGlobalJ)} );
598 } ();
599
600 // update all necessary vol vars in context
601 for (const auto& element : couplingElements)
602 {
603 // find index in coupling context
604 const auto eIdxGlobal = ldGridGeometry.elementMapper().index(element);
605 auto it = std::find(couplingElemStencil.begin(), couplingElemStencil.end(), eIdxGlobal);
606 const auto idxInContext = std::distance(couplingElemStencil.begin(), it);
607 assert(it != couplingElemStencil.end());
608
609 auto& elemVolVars = bulkContext_.lowDimElemVolVars[idxInContext];
610 const auto& fvGeom = bulkContext_.lowDimFvGeometries[idxInContext];
611 const auto& coupledScvfIndices = couplingEntry.elementToScvfMap.at(eIdxGlobal);
612 makeCoupledLowDimElemVolVars_(bulkLocalAssembler.element(), bulkLocalAssembler.fvGeometry(),
613 element, fvGeom, this->curSol(lowDimId), coupledScvfIndices, elemVolVars);
614 }
615 }
616 }
617
622 template< class BulkLocalAssembler >
623 void updateCouplingContext(BulkIdType domainI,
624 const BulkLocalAssembler& bulkLocalAssembler,
625 BulkIdType domainJ,
626 GridIndexType<bulkId> dofIdxGlobalJ,
627 const PrimaryVariables<bulkId>& priVarsJ,
628 unsigned int pvIdxJ)
629 {
630 // communicate deflected solution
631 ParentType::updateCouplingContext(domainI, bulkLocalAssembler, domainJ, dofIdxGlobalJ, priVarsJ, pvIdxJ);
632 }
633
639 template< class LowDimLocalAssembler >
640 void updateCouplingContext(LowDimIdType domainI,
641 const LowDimLocalAssembler& lowDimLocalAssembler,
642 BulkIdType domainJ,
643 GridIndexType<bulkId> dofIdxGlobalJ,
644 const PrimaryVariables<bulkId>& priVarsJ,
645 unsigned int pvIdxJ)
646 {
647 // communicate deflected solution
648 ParentType::updateCouplingContext(domainI, lowDimLocalAssembler, domainJ, dofIdxGlobalJ, priVarsJ, pvIdxJ);
649
650 // Since coupling only occurs via the fluxes, the context does not
651 // have to be updated in explicit time discretization schemes, where
652 // they are strictly evaluated on the old time level
653 if (!lowDimLocalAssembler.isImplicit())
654 return;
655
656 // skip the rest if context is empty
657 if (lowDimContext_.isSet)
658 {
659 assert(lowDimContext_.elementIdx == this->problem(lowDimId).gridGeometry().elementMapper().index(lowDimLocalAssembler.element()));
660
661 const auto& map = couplingMapperPtr_->couplingMap(lowDimGridId, bulkGridId);
662 auto it = map.find(lowDimContext_.elementIdx);
663
664 assert(it != map.end());
665 const auto& embedments = it->second.embedments;
666 const auto& bulkGridGeometry = this->problem(bulkId).gridGeometry();
667
668 // TODO more efficient (only one dof update per embedment)
669 // update the elem volvars in context of those elements that contain globalJ
670 unsigned int embedmentIdx = 0;
671 for (const auto& embedment : embedments)
672 {
673 const auto elementJ = bulkGridGeometry.element(embedment.first);
674
675 for (unsigned int i = 0; i < elementJ.subEntities(bulkDim); ++i)
676 {
677 const auto dofIdx = bulkGridGeometry.vertexMapper().subIndex(elementJ, i, bulkDim);
678 if (dofIdx == dofIdxGlobalJ)
679 {
680 // element contains the deflected dof
681 const auto& fvGeom = *lowDimContext_.bulkFvGeometries[embedmentIdx];
682 (*lowDimContext_.bulkElemVolVars[embedmentIdx]).bindElement(elementJ, fvGeom, this->curSol(bulkId));
683 }
684 }
685
686 // keep track of embedments
687 embedmentIdx++;
688 }
689 }
690 }
691
698 template< class LowDimLocalAssembler >
699 void updateCouplingContext(LowDimIdType domainI,
700 const LowDimLocalAssembler& lowDimLocalAssembler,
701 LowDimIdType domainJ,
702 GridIndexType<lowDimId> dofIdxGlobalJ,
703 const PrimaryVariables<lowDimId>& priVarsJ,
704 unsigned int pvIdxJ)
705 {
706 // communicate deflected solution
707 ParentType::updateCouplingContext(domainI, lowDimLocalAssembler, domainJ, dofIdxGlobalJ, priVarsJ, pvIdxJ);
708
709 // Since coupling only occurs via the fluxes, the context does not
710 // have to be updated in explicit time discretization schemes, where
711 // they are strictly evaluated on the old time level
712 if (!lowDimLocalAssembler.isImplicit())
713 return;
714
715 // skip the rest if context is empty
716 if (lowDimContext_.isSet)
717 {
718 assert(bulkContext_.isSet);
719 assert(lowDimContext_.elementIdx == this->problem(lowDimId).gridGeometry().elementMapper().index(lowDimLocalAssembler.element()));
720
721 // update the corresponding vol vars in the bulk context
722 const auto& bulkMap = couplingMapperPtr_->couplingMap(bulkGridId, lowDimGridId);
723 const auto& bulkCouplingEntry = bulkMap.at(bulkContext_.elementIdx);
724 const auto& couplingElementStencil = bulkCouplingEntry.couplingElementStencil;
725 auto it = std::find(couplingElementStencil.begin(), couplingElementStencil.end(), lowDimContext_.elementIdx);
726 assert(it != couplingElementStencil.end());
727 const auto idxInContext = std::distance(couplingElementStencil.begin(), it);
728
729 // get neighboring bulk element from the bulk context (is the same element as first entry in low dim context)
730 const auto& bulkElement = this->problem(bulkId).gridGeometry().element(bulkContext_.elementIdx);
731 const auto& bulkFvGeometry = *lowDimContext_.bulkFvGeometries[0];
732
733 auto& elemVolVars = bulkContext_.lowDimElemVolVars[idxInContext];
734 const auto& fvGeom = bulkContext_.lowDimFvGeometries[idxInContext];
735 const auto& element = lowDimLocalAssembler.element();
736 const auto& scvfIndices = bulkCouplingEntry.elementToScvfMap.at(lowDimContext_.elementIdx);
737
738 makeCoupledLowDimElemVolVars_(bulkElement, bulkFvGeometry, element, fvGeom,
739 this->curSol(lowDimId), scvfIndices, elemVolVars);
740 }
741 }
742
744 template<std::size_t id, std::enable_if_t<(id == bulkId || id == lowDimId), int> = 0>
745 const typename CouplingMapper::template Stencil<id>&
746 getEmptyStencil(Dune::index_constant<id>) const
747 { return std::get<(id == bulkId ? 0 : 1)>(emptyStencilTuple_); }
748
749private:
750
755 template<class LowDimSolution, class ScvfIdxStorage>
756 void makeCoupledLowDimElemVolVars_(const Element<bulkId>& bulkElement,
757 const FVElementGeometry<bulkId>& bulkFvGeometry,
758 const Element<lowDimId>& lowDimElement,
759 const FVElementGeometry<lowDimId>& lowDimFvGeometry,
760 const LowDimSolution& lowDimSol,
761 const ScvfIdxStorage& coupledBulkScvfIndices,
762 std::vector<VolumeVariables<lowDimId>>& elemVolVars)
763 {
764 const auto lowDimElemSol = elementSolution(lowDimElement, lowDimSol, lowDimFvGeometry.gridGeometry());
765
766 // for tpfa, make only one volume variables object for the element
767 // for the update, use the first (and only - for tpfa) scv of the element
768 if (!lowDimUsesBox)
769 elemVolVars[0].update(lowDimElemSol, this->problem(lowDimId), lowDimElement, *scvs(lowDimFvGeometry).begin());
770 // for box, make vol vars either:
771 // - in each scv center, which geometrically coincides with the scvf integration point (Neumann coupling)
772 // - at the vertex position (Dirichlet coupling)
773 else
774 {
775 // recall that the scvfs in the coupling map were ordered such
776 // that the i-th scvf in the map couples to the i-th lowdim scv
777 for (unsigned int i = 0; i < coupledBulkScvfIndices.size(); ++i)
778 {
779 const auto& scvf = bulkFvGeometry.scvf(coupledBulkScvfIndices[i]);
780 const auto bcTypes = this->problem(bulkId).interiorBoundaryTypes(bulkElement, scvf);
781 if (bcTypes.hasOnlyNeumann())
782 FacetCoupling::makeInterpolatedVolVars(elemVolVars[i], this->problem(lowDimId), lowDimSol,
783 lowDimFvGeometry, lowDimElement, lowDimElement.geometry(),
784 scvf.ipGlobal());
785 else
786 elemVolVars[i].update( lowDimElemSol, this->problem(lowDimId), lowDimElement, lowDimFvGeometry.scv(i) );
787 }
788 }
789 }
790
792 template<class BulkScvfIndices>
793 NumEqVector<bulkId> evalBulkFluxes_(const Element<bulkId>& elementI,
794 const FVElementGeometry<bulkId>& fvGeometry,
795 const ElementVolumeVariables<bulkId>& elemVolVars,
796 const ElementBoundaryTypes<bulkId>& elemBcTypes,
797 const ElementFluxVariablesCache<bulkId>& elemFluxVarsCache,
798 const LocalResidual<bulkId>& localResidual,
799 const BulkScvfIndices& scvfIndices) const
800 {
801
802 NumEqVector<bulkId> coupledFluxes(0.0);
803 for (const auto& scvfIdx : scvfIndices)
804 coupledFluxes += localResidual.evalFlux(this->problem(bulkId),
805 elementI,
806 fvGeometry,
807 elemVolVars,
808 elemBcTypes,
809 elemFluxVarsCache,
810 fvGeometry.scvf(scvfIdx));
811 return coupledFluxes;
812 }
813
814 std::shared_ptr<CouplingMapper> couplingMapperPtr_;
815
818 std::vector<bool> bulkElemIsCoupled_;
819
821 using BulkStencil = typename CouplingMapper::template Stencil<bulkId>;
822 using LowDimStencil = typename CouplingMapper::template Stencil<lowDimId>;
823 std::tuple<BulkStencil, LowDimStencil> emptyStencilTuple_;
824
826 BulkCouplingContext bulkContext_;
827 LowDimCouplingContext lowDimContext_;
828};
829
830} // end namespace Dumux
831
832#endif
The interface of the coupling manager for multi domain problems.
Definition: multidomain/couplingmanager.hh:48
void bindCouplingContext(BulkIdType, const Element< bulkId > &element, const Assembler &assembler)
For the assembly of the element residual of a bulk domain element we need to prepare all variables of...
Definition: multidomain/facet/box/couplingmanager.hh:444
void updateCouplingContext(BulkIdType domainI, const BulkLocalAssembler &bulkLocalAssembler, LowDimIdType domainJ, GridIndexType< lowDimId > dofIdxGlobalJ, const PrimaryVariables< lowDimId > &priVarsJ, unsigned int pvIdxJ)
After deflecting the solution of the lower-dimensional domain, we have to update the element volume v...
Definition: multidomain/facet/box/couplingmanager.hh:553
void bindCouplingContext(LowDimIdType, const Element< lowDimId > &element, const Assembler &assembler)
For the assembly of the element residual of a bulk domain element we need to prepare the local views ...
Definition: multidomain/facet/box/couplingmanager.hh:495
bool isOnInteriorBoundary(const Element< bulkId > &element, const SubControlVolumeFace< bulkId > &scvf) const
returns true if a bulk scvf coincides with a facet element.
Definition: multidomain/facet/box/couplingmanager.hh:249
const CouplingMapper::template Stencil< id > & getEmptyStencil(Dune::index_constant< id >) const
Empty stencil to be returned for elements that aren't coupled.
Definition: multidomain/facet/box/couplingmanager.hh:746
void init(std::shared_ptr< Problem< bulkId > > bulkProblem, std::shared_ptr< Problem< lowDimId > > lowDimProblem, std::shared_ptr< CouplingMapper > couplingMapper, const SolutionVector &curSol)
Initialize the coupling manager.
Definition: multidomain/facet/box/couplingmanager.hh:180
const CouplingStencilType< lowDimId > & couplingStencil(LowDimIdType domainI, const Element< lowDimId > &element, BulkIdType domainJ) const
The coupling stencil of the lower-dimensional domain with the bulk domain.
Definition: multidomain/facet/box/couplingmanager.hh:225
const GridIndexType< lowDimId > getLowDimElementIndex(const Element< bulkId > &element, const SubControlVolumeFace< bulkId > &scvf) const
returns the index of the lower-dimensional element coinciding with a bulk scvf.
Definition: multidomain/facet/box/couplingmanager.hh:304
typename CouplingMapper::template Stencil< CouplingMapper::template gridId< GridView< j >::dimension >() > CouplingStencilType
types used for coupling stencils
Definition: multidomain/facet/box/couplingmanager.hh:167
LocalResidual< bulkId >::ElementResidualVector evalCouplingResidual(BulkIdType, const BulkLocalAssembler &bulkLocalAssembler, LowDimIdType, GridIndexType< lowDimId > dofIdxGlobalJ)
Evaluates the coupling element residual of a bulk domain element with respect to a dof in the lower-d...
Definition: multidomain/facet/box/couplingmanager.hh:335
const VolumeVariables< lowDimId > & getLowDimVolVars(const Element< bulkId > &element, const SubControlVolumeFace< bulkId > &scvf) const
returns the vol vars of a lower-dimensional element coinciding with a bulk scvf.
Definition: multidomain/facet/box/couplingmanager.hh:256
void updateCouplingContext(BulkIdType domainI, const BulkLocalAssembler &bulkLocalAssembler, BulkIdType domainJ, GridIndexType< bulkId > dofIdxGlobalJ, const PrimaryVariables< bulkId > &priVarsJ, unsigned int pvIdxJ)
Update the coupling context for a derivative bulk -> bulk. Here, we simply have to update the solutio...
Definition: multidomain/facet/box/couplingmanager.hh:623
void updateCouplingContext(LowDimIdType domainI, const LowDimLocalAssembler &lowDimLocalAssembler, BulkIdType domainJ, GridIndexType< bulkId > dofIdxGlobalJ, const PrimaryVariables< bulkId > &priVarsJ, unsigned int pvIdxJ)
After deflecting the solution of the bulk domain, we have to update the element volume variables of t...
Definition: multidomain/facet/box/couplingmanager.hh:640
const Element< lowDimId > getLowDimElement(const Element< bulkId > &element, const SubControlVolumeFace< bulkId > &scvf) const
returns the lower-dimensional element coinciding with a bulk scvf.
Definition: multidomain/facet/box/couplingmanager.hh:294
void updateCouplingContext(LowDimIdType domainI, const LowDimLocalAssembler &lowDimLocalAssembler, LowDimIdType domainJ, GridIndexType< lowDimId > dofIdxGlobalJ, const PrimaryVariables< lowDimId > &priVarsJ, unsigned int pvIdxJ)
After deflecting the solution of the lower-dimensional domain has been deflected during the assembly ...
Definition: multidomain/facet/box/couplingmanager.hh:699
typename MDTraits::SolutionVector SolutionVector
the type of the solution vector
Definition: multidomain/facet/box/couplingmanager.hh:170
LocalResidual< lowDimId >::ElementResidualVector evalCouplingResidual(LowDimIdType, const LowDimLocalAssembler &lowDimLocalAssembler, BulkIdType, GridIndexType< bulkId > dofIdxGlobalJ)
Evaluates the coupling element residual of a lower-dimensional domain element with respect to a dof i...
Definition: multidomain/facet/box/couplingmanager.hh:377
bool isCoupled(const Element< bulkId > &element, const SubControlVolumeFace< bulkId > &scvf) const
returns true if a bulk scvf flux depends on data in the facet domain.
Definition: multidomain/facet/box/couplingmanager.hh:241
NumEqVector< lowDimId > evalSourcesFromBulk(const Element< lowDimId > &element, const FVElementGeometry< lowDimId > &fvGeometry, const ElementVolumeVariables< lowDimId > &elemVolVars, const SubControlVolume< lowDimId > &scv)
Computes the sources in a lower-dimensional element stemming from the bulk domain.
Definition: multidomain/facet/box/couplingmanager.hh:400
const CouplingStencilType< bulkId > & couplingStencil(BulkIdType domainI, const Element< bulkId > &element, LowDimIdType domainJ) const
The coupling stencil of a given bulk domain element.
Definition: multidomain/facet/box/couplingmanager.hh:205
Implementation for the coupling manager between two domains of dimension d and (d-1) for models consi...
Definition: multidomain/facet/couplingmanager.hh:83
Defines all properties used in Dumux.
Element solution classes and factory functions.
typename NumEqVectorTraits< PrimaryVariables >::type NumEqVector
A vector with the same size as numbers of equations This is the default implementation and has to be ...
Definition: numeqvector.hh:34
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:26
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethods::cctpfa||GridGeometry::discMethod==DiscretizationMethods::ccmpfa, CCElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for cell-centered schemes.
Definition: cellcentered/elementsolution.hh:101
void makeInterpolatedVolVars(VolumeVariables &volVars, const Problem &problem, const SolutionVector &sol, const FVGeometry &fvGeometry, const typename FVGeometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const typename FVGeometry::GridGeometry::GridView::template Codim< 0 >::Entity::Geometry &elemGeom, const typename FVGeometry::GridGeometry::GridView::template Codim< 0 >::Entity::Geometry::GlobalCoordinate &pos)
Free function that allows the creation of a volume variables object interpolated to a given position ...
Definition: multidomain/facet/couplingmanager.hh:40
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:282
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:296
The available discretization methods in Dumux.
The interface of the coupling manager for multi domain problems.
constexpr Box box
Definition: method.hh:147
CVFE< CVFEMethods::PQ1 > Box
Definition: method.hh:94
Definition: adapt.hh:17
A helper to deduce a vector with the same size as numbers of equations.