13#ifndef DUMUX_PNM_UTILITES_HH
14#define DUMUX_PNM_UTILITES_HH
26template<
class Gr
idVariables,
class SolutionVector>
29 using Scalar =
typename GridVariables::VolumeVariables::PrimaryVariables::value_type;
30 using VolumeVariables =
typename GridVariables::VolumeVariables;
32 struct AveragedQuantityInfo
34 std::function<Scalar(
const VolumeVariables&)> quantity;
35 std::function<Scalar(
const VolumeVariables&)> weight;
42 const SolutionVector& sol)
43 : gridVariables_(gridVariables)
48 std::function<Scalar(
const VolumeVariables&)>&& w,
49 const std::string& name)
51 averagedQuantityInfo_.push_back(AveragedQuantityInfo{q, w, name});
52 averagedQuantity_.push_back(Scalar());
55 void eval(
const std::vector<std::size_t>& dofsToNeglect = std::vector<std::size_t>())
57 for (
auto& q : averagedQuantity_)
60 std::vector<bool> poreVisited(problem_().gridGeometry().numDofs(),
false);
61 std::vector<Scalar> weights(averagedQuantityInfo_.size(), 0.0);
63 auto fvGeometry =
localView(problem_().gridGeometry());
64 auto elemVolVars =
localView(gridVariables_.curGridVolVars());
66 for (
const auto& element : elements(problem_().gridGeometry().gridView()))
68 fvGeometry.bind(element);
69 elemVolVars.bind(element, fvGeometry, sol_);
71 for (
int scvIdx = 0; scvIdx < fvGeometry.numScv(); ++scvIdx)
73 static constexpr auto dofCodim = std::decay_t<
decltype(problem_().gridGeometry().gridView())>::dimension;
74 const int dofIdxGlobal = problem_().gridGeometry().vertexMapper().subIndex(element, scvIdx, dofCodim);
76 if (poreVisited[dofIdxGlobal])
78 else if (!dofsToNeglect.empty() && std::any_of(dofsToNeglect.begin(), dofsToNeglect.end(), [&](
int dofIdx){ return dofIdx == dofIdxGlobal; }))
82 const auto& volVars = elemVolVars[scvIdx];
83 for (
int i = 0; i < averagedQuantityInfo_.size(); ++i)
85 const Scalar weight = averagedQuantityInfo_[i].weight(volVars);
86 averagedQuantity_[i] += averagedQuantityInfo_[i].quantity(volVars) * weight;
89 poreVisited[dofIdxGlobal] =
true;
94 for (
int i = 0; i < averagedQuantityInfo_.size(); ++i)
95 averagedQuantity_[i] /= weights[i];
100 for (
int i = 0; i < averagedQuantityInfo_.size(); ++i)
101 if (averagedQuantityInfo_[i].name == name)
102 return averagedQuantity_[i];
104 DUNE_THROW(Dune::InvalidStateException, name +
" not found");
109 std::vector<AveragedQuantityInfo> averagedQuantityInfo_;
110 std::vector<Scalar> averagedQuantity_;
112 const auto& problem_()
113 {
return gridVariables_.curGridVolVars().problem(); }
115 const GridVariables& gridVariables_;
116 const SolutionVector& sol_;
Calculates averaged values of the network solution.
Definition: utilities.hh:28
AveragedValues(const GridVariables &gridVariables, const SolutionVector &sol)
Definition: utilities.hh:41
void addAveragedQuantity(std::function< Scalar(const VolumeVariables &)> &&q, std::function< Scalar(const VolumeVariables &)> &&w, const std::string &name)
Definition: utilities.hh:47
void eval(const std::vector< std::size_t > &dofsToNeglect=std::vector< std::size_t >())
Definition: utilities.hh:55
const Scalar & operator[](const std::string &name) const
Definition: utilities.hh:98
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:26
Definition: discretization/porenetwork/fvelementgeometry.hh:24
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.