version 3.10-dev
utilities.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//
13#ifndef DUMUX_PNM_UTILITES_HH
14#define DUMUX_PNM_UTILITES_HH
15
16#include <cmath>
17#include <vector>
19
20namespace Dumux::PoreNetwork {
21
26template<class GridVariables, class SolutionVector>
28{
29 using Scalar = typename GridVariables::VolumeVariables::PrimaryVariables::value_type;
30 using VolumeVariables = typename GridVariables::VolumeVariables;
31
32 struct AveragedQuantityInfo
33 {
34 std::function<Scalar(const VolumeVariables&)> quantity;
35 std::function<Scalar(const VolumeVariables&)> weight;
36 std::string name;
37 };
38
39public:
40
41 AveragedValues(const GridVariables& gridVariables,
42 const SolutionVector& sol)
43 : gridVariables_(gridVariables)
44 , sol_(sol)
45 {}
46
47 void addAveragedQuantity(std::function<Scalar(const VolumeVariables&)>&& q,
48 std::function<Scalar(const VolumeVariables&)>&& w,
49 const std::string& name)
50 {
51 averagedQuantityInfo_.push_back(AveragedQuantityInfo{q, w, name});
52 averagedQuantity_.push_back(Scalar());
53 }
54
55 void eval(const std::vector<std::size_t>& dofsToNeglect = std::vector<std::size_t>())
56 {
57 for (auto& q : averagedQuantity_)
58 q = 0.0;
59
60 std::vector<bool> poreVisited(problem_().gridGeometry().numDofs(), false);
61 std::vector<Scalar> weights(averagedQuantityInfo_.size(), 0.0);
62
63 auto fvGeometry = localView(problem_().gridGeometry());
64 auto elemVolVars = localView(gridVariables_.curGridVolVars());
65
66 for (const auto& element : elements(problem_().gridGeometry().gridView()))
67 {
68 fvGeometry.bind(element);
69 elemVolVars.bind(element, fvGeometry, sol_);
70
71 for (int scvIdx = 0; scvIdx < fvGeometry.numScv(); ++scvIdx)
72 {
73 static constexpr auto dofCodim = std::decay_t<decltype(problem_().gridGeometry().gridView())>::dimension;
74 const int dofIdxGlobal = problem_().gridGeometry().vertexMapper().subIndex(element, scvIdx, dofCodim);
75
76 if (poreVisited[dofIdxGlobal])
77 continue;
78 else if (!dofsToNeglect.empty() && std::any_of(dofsToNeglect.begin(), dofsToNeglect.end(), [&](int dofIdx){ return dofIdx == dofIdxGlobal; }))
79 continue;
80 else
81 {
82 const auto& volVars = elemVolVars[scvIdx];
83 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
84 {
85 const Scalar weight = averagedQuantityInfo_[i].weight(volVars);
86 averagedQuantity_[i] += averagedQuantityInfo_[i].quantity(volVars) * weight;
87 weights[i] += weight;
88 }
89 poreVisited[dofIdxGlobal] = true;
90 }
91 }
92 }
93
94 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
95 averagedQuantity_[i] /= weights[i];
96 }
97
98 const Scalar& operator[](const std::string& name) const
99 {
100 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
101 if (averagedQuantityInfo_[i].name == name)
102 return averagedQuantity_[i];
103
104 DUNE_THROW(Dune::InvalidStateException, name + " not found");
105 }
106
107private:
108
109 std::vector<AveragedQuantityInfo> averagedQuantityInfo_;
110 std::vector<Scalar> averagedQuantity_;
111
112 const auto& problem_()
113 { return gridVariables_.curGridVolVars().problem(); }
114
115 const GridVariables& gridVariables_;
116 const SolutionVector& sol_;
117};
118
119} // end Dumux::PoreNetwork
120
121#endif
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.