24#ifndef DUMUX_PNM_BOUNDARYFLUX_HH
25#define DUMUX_PNM_BOUNDARYFLUX_HH
31#include <unordered_map>
35#include <dune/common/exceptions.hh>
46template<
class Gr
idVariables,
class LocalRes
idual,
class SolutionVector>
49 using Problem = std::decay_t<decltype(std::declval<LocalResidual>().problem())>;
51 using GridView =
typename GridGeometry::GridView;
52 using Element =
typename GridView::template Codim<0>::Entity;
53 using FVElementGeometry =
typename GridGeometry::LocalView;
54 using SubControlVolume =
typename FVElementGeometry::SubControlVolume;
58 using NumEqVector =
typename SolutionVector::block_type;
59 static constexpr auto numEq = NumEqVector::dimension;
64 NumEqVector totalFlux;
65 std::unordered_map<std::size_t, NumEqVector> fluxPerPore;
68 friend std::ostream&
operator<< (std::ostream& stream,
const Result& result)
70 stream << result.totalFlux;
75 const auto& operator[] (
int eqIdx)
const
76 {
return totalFlux[eqIdx]; }
79 operator NumEqVector()
const
85 using Scalar =
typename GridVariables::Scalar;
88 const LocalResidual& localResidual,
89 const SolutionVector& sol)
90 : localResidual_(localResidual)
91 , gridVariables_(gridVariables)
93 , isStationary_(localResidual.isStationary())
95 const auto numDofs = localResidual_.problem().gridGeometry().numDofs();
96 isConsidered_.resize(numDofs,
false);
97 boundaryFluxes_.resize(numDofs);
106 template<
class Label>
107 Result
getFlux(
const std::vector<Label>& labels,
const bool verbose =
false)
const
110 auto restriction = [&] (
const SubControlVolume& scv)
112 const Label poreLabel = localResidual_.problem().gridGeometry().poreLabel(scv.dofIndex());
113 return std::any_of(labels.begin(), labels.end(),
114 [&](
const Label l){ return l == poreLabel; });
117 std::fill(boundaryFluxes_.begin(), boundaryFluxes_.end(), NumEqVector(0.0));
118 std::fill(isConsidered_.begin(), isConsidered_.end(),
false);
121 for (
const auto& element : elements(localResidual_.problem().gridGeometry().gridView()))
122 getFlux(element, restriction, verbose);
125 result.totalFlux = std::accumulate(boundaryFluxes_.begin(), boundaryFluxes_.end(), NumEqVector(0.0));;
126 for (
int i = 0; i < isConsidered_.size(); ++i)
128 if (isConsidered_[i])
129 result.fluxPerPore[i] = boundaryFluxes_[i];
142 Result
getFlux(std::string_view minMax,
const int coord,
const bool verbose =
false)
const
144 if (!(minMax ==
"min" || minMax ==
"max"))
145 DUNE_THROW(Dune::InvalidStateException,
146 "second argument must be either 'min' or 'max' (string) !");
149 auto onMeasuringBoundary = [&] (
const Scalar pos)
151 return ( (minMax ==
"min" && pos < localResidual_.problem().gridGeometry().bBoxMin()[coord] + eps) ||
152 (minMax ==
"max" && pos > localResidual_.problem().gridGeometry().bBoxMax()[coord] - eps) );
156 auto restriction = [&] (
const SubControlVolume& scv)
158 bool considerAllDirections = coord < 0 ? true :
false;
161 bool considerScv = localResidual_.problem().gridGeometry().dofOnBoundary(scv.dofIndex()) && onMeasuringBoundary(scv.dofPosition()[coord]);
165 if (considerScv && !considerAllDirections)
167 const auto& pos = scv.dofPosition();
168 if (!(pos[coord] < localResidual_.problem().gridGeometry().bBoxMin()[coord] + eps || pos[coord] > localResidual_.problem().gridGeometry().bBoxMax()[coord] -eps ))
175 std::fill(boundaryFluxes_.begin(), boundaryFluxes_.end(), NumEqVector(0.0));
176 std::fill(isConsidered_.begin(), isConsidered_.end(),
false);
179 for (
const auto& element : elements(localResidual_.problem().gridGeometry().gridView()))
180 getFlux(element, restriction, verbose);
183 result.totalFlux = std::accumulate(boundaryFluxes_.begin(), boundaryFluxes_.end(), NumEqVector(0.0));;
184 for (
int i = 0; i < isConsidered_.size(); ++i)
186 if (isConsidered_[i])
187 result.fluxPerPore[i] = boundaryFluxes_[i];
201 template<
class RestrictingFunction>
203 RestrictingFunction considerScv,
204 const bool verbose =
false)
const
206 NumEqVector flux(0.0);
211 auto fvGeometry =
localView(localResidual_.problem().gridGeometry());
212 fvGeometry.bind(element);
214 auto curElemVolVars =
localView(gridVariables_.curGridVolVars());
215 curElemVolVars.bind(element, fvGeometry, sol_);
217 auto prevElemVolVars =
localView(gridVariables_.prevGridVolVars());
219 prevElemVolVars.bindElement(element, fvGeometry, sol_);
221 auto elemFluxVarsCache =
localView(gridVariables_.gridFluxVarsCache());
222 elemFluxVarsCache.bindElement(element, fvGeometry, curElemVolVars);
225 elemBcTypes.
update(localResidual_.problem(), element, fvGeometry);
227 auto residual = localResidual_.evalFluxAndSource(element, fvGeometry, curElemVolVars, elemFluxVarsCache, elemBcTypes);
230 residual += localResidual_.evalStorage(element, fvGeometry, prevElemVolVars, curElemVolVars);
232 for (
auto&& scv : scvs(fvGeometry))
235 if (considerScv(scv))
237 isConsidered_[scv.dofIndex()] =
true;
242 flux -= residual[scv.indexInElement()];
244 boundaryFluxes_[scv.dofIndex()] -= residual[scv.indexInElement()];
247 std::cout <<
"SCV of element " << scv.elementIndex() <<
" at vertex " << scv.dofIndex() <<
" has flux: " << residual[scv.indexInElement()] << std::endl;
254 const LocalResidual localResidual_;
255 const GridVariables& gridVariables_;
256 const SolutionVector& sol_;
258 mutable std::vector<bool> isConsidered_;
259 mutable std::vector<NumEqVector> boundaryFluxes_;
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
auto operator<<(std::ostream &os, const T &t) -> std::enable_if_t< decltype(Detail::hasName(t))::value, std::ostream & >
Return the class name of the tagged type calling t.name()
Definition: tag.hh:59
Definition: discretization/porenetwork/fvelementgeometry.hh:33
std::decay_t< decltype(std::declval< Problem >().gridGeometry())> GridGeometry
Definition: common/typetraits/problem.hh:45
typename Detail::template ProblemTraits< Problem, GridGeometry::discMethod >::BoundaryTypes BoundaryTypes
Definition: common/typetraits/problem.hh:46
This class stores an array of BoundaryTypes objects.
Definition: box/elementboundarytypes.hh:39
void update(const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry)
Update the boundary types for all vertices of an element.
Definition: box/elementboundarytypes.hh:52
Class for the calculation of fluxes at the boundary of pore-network models.
Definition: boundaryflux.hh:48
BoundaryFlux(const GridVariables &gridVariables, const LocalResidual &localResidual, const SolutionVector &sol)
Definition: boundaryflux.hh:87
NumEqVector getFlux(const Element &element, RestrictingFunction considerScv, const bool verbose=false) const
Returns the cumulative flux in , or of several pore throats at a given location on the boundary.
Definition: boundaryflux.hh:202
Result getFlux(std::string_view minMax, const int coord, const bool verbose=false) const
Returns the cumulative flux in , or of several pore throats at a given location on the boundary.
Definition: boundaryflux.hh:142
typename GridVariables::Scalar Scalar
Definition: boundaryflux.hh:85
Result getFlux(const std::vector< Label > &labels, const bool verbose=false) const
Returns the cumulative flux in , or of several pore throats for a given list of pore labels to cons...
Definition: boundaryflux.hh:107
Type traits for problem classes.
Boundary types gathered on an element.