3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_PNM_UTILITES_HH
26#define DUMUX_PNM_UTILITES_HH
27
28#include <cmath>
29#include <vector>
31
32namespace Dumux::PoreNetwork {
33
38template<class GridVariables, class SolutionVector>
40{
41 using Scalar = typename GridVariables::VolumeVariables::PrimaryVariables::value_type;
42 using VolumeVariables = typename GridVariables::VolumeVariables;
43
44 struct AveragedQuantityInfo
45 {
46 std::function<Scalar(const VolumeVariables&)> quantity;
47 std::function<Scalar(const VolumeVariables&)> weight;
48 std::string name;
49 };
50
51public:
52
53 AveragedValues(const GridVariables& gridVariables,
54 const SolutionVector& sol)
55 : gridVariables_(gridVariables)
56 , sol_(sol)
57 {}
58
59 void addAveragedQuantity(std::function<Scalar(const VolumeVariables&)>&& q,
60 std::function<Scalar(const VolumeVariables&)>&& w,
61 const std::string& name)
62 {
63 averagedQuantityInfo_.push_back(AveragedQuantityInfo{q, w, name});
64 averagedQuantity_.push_back(Scalar());
65 }
66
67 void eval(const std::vector<std::size_t>& dofsToNeglect = std::vector<std::size_t>())
68 {
69 for (auto& q : averagedQuantity_)
70 q = 0.0;
71
72 std::vector<bool> poreVisited(problem_().gridGeometry().numDofs(), false);
73 std::vector<Scalar> weights(averagedQuantityInfo_.size(), 0.0);
74
75 auto fvGeometry = localView(problem_().gridGeometry());
76 auto elemVolVars = localView(gridVariables_.curGridVolVars());
77
78 for (const auto& element : elements(problem_().gridGeometry().gridView()))
79 {
80 fvGeometry.bind(element);
81 elemVolVars.bind(element, fvGeometry, sol_);
82
83 for (int scvIdx = 0; scvIdx < fvGeometry.numScv(); ++scvIdx)
84 {
85 static constexpr auto dofCodim = std::decay_t<decltype(problem_().gridGeometry().gridView())>::dimension;
86 const int dofIdxGlobal = problem_().gridGeometry().vertexMapper().subIndex(element, scvIdx, dofCodim);
87
88 if (poreVisited[dofIdxGlobal])
89 continue;
90 else if (!dofsToNeglect.empty() && std::any_of(dofsToNeglect.begin(), dofsToNeglect.end(), [&](int dofIdx){ return dofIdx == dofIdxGlobal; }))
91 continue;
92 else
93 {
94 const auto& volVars = elemVolVars[scvIdx];
95 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
96 {
97 const Scalar weight = averagedQuantityInfo_[i].weight(volVars);
98 averagedQuantity_[i] += averagedQuantityInfo_[i].quantity(volVars) * weight;
99 weights[i] += weight;
100 }
101 poreVisited[dofIdxGlobal] = true;
102 }
103 }
104 }
105
106 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
107 averagedQuantity_[i] /= weights[i];
108 }
109
110 const Scalar& operator[](const std::string& name) const
111 {
112 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
113 if (averagedQuantityInfo_[i].name == name)
114 return averagedQuantity_[i];
115
116 DUNE_THROW(Dune::InvalidStateException, name + " not found");
117 }
118
119private:
120
121 std::vector<AveragedQuantityInfo> averagedQuantityInfo_;
122 std::vector<Scalar> averagedQuantity_;
123
124 const auto& problem_()
125 { return gridVariables_.curGridVolVars().problem(); }
126
127 const GridVariables& gridVariables_;
128 const SolutionVector& sol_;
129};
130
131} // end Dumux::PoreNetwork
132
133#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
Definition: discretization/porenetwork/fvelementgeometry.hh:34
Calculates averaged values of the network solution.
Definition: utilities.hh:40
AveragedValues(const GridVariables &gridVariables, const SolutionVector &sol)
Definition: utilities.hh:53
void addAveragedQuantity(std::function< Scalar(const VolumeVariables &)> &&q, std::function< Scalar(const VolumeVariables &)> &&w, const std::string &name)
Definition: utilities.hh:59
void eval(const std::vector< std::size_t > &dofsToNeglect=std::vector< std::size_t >())
Definition: utilities.hh:67
const Scalar & operator[](const std::string &name) const
Definition: utilities.hh:110