3.4
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 auto fvGeometry = localView(problem_().gridGeometry());
73 auto elemVolVars = localView(gridVariables_.curGridVolVars());
74 std::vector<bool> poreVisited(problem_().gridGeometry().numDofs(), false);
75 std::vector<Scalar> weights(averagedQuantityInfo_.size(), 0.0);
76
77 for (const auto& element : elements(problem_().gridGeometry().gridView()))
78 {
79 fvGeometry.bind(element);
80 elemVolVars.bind(element, fvGeometry, sol_);
81
82 for (int scvIdx = 0; scvIdx < fvGeometry.numScv(); ++scvIdx)
83 {
84 static constexpr auto dofCodim = std::decay_t<decltype(problem_().gridGeometry().gridView())>::dimension;
85 const int dofIdxGlobal = problem_().gridGeometry().vertexMapper().subIndex(element, scvIdx, dofCodim);
86
87 if (poreVisited[dofIdxGlobal])
88 continue;
89 else if (!dofsToNeglect.empty() && std::any_of(dofsToNeglect.begin(), dofsToNeglect.end(), [&](int dofIdx){ return dofIdx == dofIdxGlobal; }))
90 continue;
91 else
92 {
93 const auto& volVars = elemVolVars[scvIdx];
94 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
95 {
96 const Scalar weight = averagedQuantityInfo_[i].weight(volVars);
97 averagedQuantity_[i] += averagedQuantityInfo_[i].quantity(volVars) * weight;
98 weights[i] += weight;
99 }
100 poreVisited[dofIdxGlobal] = true;
101 }
102 }
103 }
104
105 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
106 averagedQuantity_[i] /= weights[i];
107 }
108
109 const Scalar& operator[](const std::string& name) const
110 {
111 for (int i = 0; i < averagedQuantityInfo_.size(); ++i)
112 if (averagedQuantityInfo_[i].name == name)
113 return averagedQuantity_[i];
114
115 DUNE_THROW(Dune::InvalidStateException, name + " not found");
116 }
117
118private:
119
120 std::vector<AveragedQuantityInfo> averagedQuantityInfo_;
121 std::vector<Scalar> averagedQuantity_;
122
123 const auto& problem_()
124 { return gridVariables_.curGridVolVars().problem(); }
125
126 const GridVariables& gridVariables_;
127 const SolutionVector& sol_;
128};
129
130} // end Dumux::PoreNetwork
131
132#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:33
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:109