version 3.10-dev
flux/upwindscheme.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//
12#ifndef DUMUX_DISCRETIZATION_UPWINDSCHEME_HH
13#define DUMUX_DISCRETIZATION_UPWINDSCHEME_HH
14
17
18namespace Dumux {
19
21template<class GridGeometry, class DiscretizationMethod>
23
29template<class GridGeometry>
31
32namespace Detail {
33
35template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar>
36Scalar upwindSchemeMultiplier(const ElemVolVars& elemVolVars,
37 const SubControlVolumeFace& scvf,
38 const UpwindTermFunction& upwindTerm,
39 Scalar flux, int phaseIdx)
40{
41 // TODO: pass this from outside?
42 static const Scalar upwindWeight = getParamFromGroup<Scalar>(elemVolVars.gridVolVars().problem().paramGroup(), "Flux.UpwindWeight");
43
44 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
45 const auto& outsideVolVars = elemVolVars[scvf.outsideScvIdx()];
46
47 using std::signbit;
48 if (signbit(flux)) // if sign of flux is negative
49 return (upwindWeight*upwindTerm(outsideVolVars)
50 + (1.0 - upwindWeight)*upwindTerm(insideVolVars));
51 else
52 return (upwindWeight*upwindTerm(insideVolVars)
53 + (1.0 - upwindWeight)*upwindTerm(outsideVolVars));
54}
55
56} // end namespace Detail
57
59template<class GridGeometry, class DM>
60class UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CVFE<DM>>
61{
62public:
64 template<class FluxVariables, class UpwindTermFunction, class Scalar>
65 static Scalar apply(const FluxVariables& fluxVars,
66 const UpwindTermFunction& upwindTerm,
67 Scalar flux, int phaseIdx)
68 {
69 return apply(fluxVars.elemVolVars(), fluxVars.scvFace(), upwindTerm, flux, phaseIdx);
70 }
71
73 template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar>
74 static Scalar apply(const ElemVolVars& elemVolVars,
75 const SubControlVolumeFace& scvf,
76 const UpwindTermFunction& upwindTerm,
77 Scalar flux, int phaseIdx)
78 {
79 return flux*multiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
80 }
81
83 template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar>
84 static Scalar multiplier(const ElemVolVars& elemVolVars,
85 const SubControlVolumeFace& scvf,
86 const UpwindTermFunction& upwindTerm,
87 Scalar flux, int phaseIdx)
88 {
89 return Detail::upwindSchemeMultiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
90 }
91};
92
94template<class GridGeometry>
95class UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CCTpfa>
96{
97 using GridView = typename GridGeometry::GridView;
98 static constexpr int dim = GridView::dimension;
99 static constexpr int dimWorld = GridView::dimensionworld;
100
101public:
103 template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar>
104 static Scalar multiplier(const ElemVolVars& elemVolVars,
105 const SubControlVolumeFace& scvf,
106 const UpwindTermFunction& upwindTerm,
107 Scalar flux, int phaseIdx)
108 {
109 static_assert(dim == dimWorld, "Multiplier cannot be computed on surface grids using cell-centered scheme!");
110 return Detail::upwindSchemeMultiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
111 }
112
114 template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar>
115 static Scalar apply(const ElemVolVars& elemVolVars,
116 const SubControlVolumeFace& scvf,
117 const UpwindTermFunction& upwindTerm,
118 Scalar flux, int phaseIdx)
119 {
120 static_assert(dim == dimWorld, "This upwind scheme cannot be used on surface grids using cell-centered schemes!");
121 return flux*multiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
122 }
123
124 // For surface and network grids (dim < dimWorld) we have to do a special upwind scheme
125 template<class FluxVariables, class UpwindTermFunction, class Scalar>
126 static Scalar apply(const FluxVariables& fluxVars,
127 const UpwindTermFunction& upwindTerm,
128 Scalar flux, int phaseIdx)
129 {
130 const auto& scvf = fluxVars.scvFace();
131 const auto& elemVolVars = fluxVars.elemVolVars();
132
133 // standard scheme
134 if constexpr (dim == dimWorld)
135 return apply(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
136
137 // on non-branching points the standard scheme works
138 if (scvf.numOutsideScvs() == 1)
139 return flux*Detail::upwindSchemeMultiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx);
140
141 // handle branching points with a more complicated upwind scheme
142 // we compute a flux-weighted average of all inflowing branches
143 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
144
145 Scalar branchingPointUpwindTerm = 0.0;
146 Scalar sumUpwindFluxes = 0.0;
147
148 // if the inside flux is positive (outflow) do fully upwind and return flux
149 using std::signbit;
150 if (!signbit(flux))
151 return upwindTerm(insideVolVars)*flux;
152 else
153 sumUpwindFluxes += flux;
154
155 for (unsigned int i = 0; i < scvf.numOutsideScvs(); ++i)
156 {
157 // compute the outside flux
158 const auto& fvGeometry = fluxVars.fvGeometry();
159 const auto outsideScvIdx = scvf.outsideScvIdx(i);
160 const auto outsideElement = fvGeometry.gridGeometry().element(outsideScvIdx);
161 const auto& flippedScvf = fvGeometry.flipScvf(scvf.index(), i);
162
163 using AdvectionType = typename FluxVariables::AdvectionType;
164 const auto outsideFlux = AdvectionType::flux(fluxVars.problem(),
165 outsideElement,
166 fvGeometry,
167 elemVolVars,
168 flippedScvf,
169 phaseIdx,
170 fluxVars.elemFluxVarsCache());
171
172 if (!signbit(outsideFlux))
173 branchingPointUpwindTerm += upwindTerm(elemVolVars[outsideScvIdx])*outsideFlux;
174 else
175 sumUpwindFluxes += outsideFlux;
176 }
177
178 // the flux might be zero
179 if (sumUpwindFluxes != 0.0)
180 branchingPointUpwindTerm /= -sumUpwindFluxes;
181 else
182 branchingPointUpwindTerm = 0.0;
183
184 // upwind scheme (always do fully upwind at branching points)
185 // a weighting here would lead to an error since the derivation is based on a fully upwind scheme
186 // TODO How to implement a weight of e.g. 0.5
187 if (signbit(flux))
188 return flux*branchingPointUpwindTerm;
189 else
190 return flux*upwindTerm(insideVolVars);
191 }
192};
193
195template<class GridGeometry>
196class UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CCMpfa>
197: public UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CCTpfa> {};
198
199} // end namespace Dumux
200
201#endif
static Scalar apply(const FluxVariables &fluxVars, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
Definition: flux/upwindscheme.hh:126
static Scalar apply(const ElemVolVars &elemVolVars, const SubControlVolumeFace &scvf, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
applies a simple upwind scheme to the precalculated advective flux across the given scvf
Definition: flux/upwindscheme.hh:115
static Scalar multiplier(const ElemVolVars &elemVolVars, const SubControlVolumeFace &scvf, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
returns the upwind factor which is multiplied to the advective flux across the given scvf
Definition: flux/upwindscheme.hh:104
static Scalar multiplier(const ElemVolVars &elemVolVars, const SubControlVolumeFace &scvf, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
returns the upwind factor which is multiplied to the advective flux across the given scvf
Definition: flux/upwindscheme.hh:84
static Scalar apply(const ElemVolVars &elemVolVars, const SubControlVolumeFace &scvf, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
applies a simple upwind scheme to the precalculated advective flux across the given scvf
Definition: flux/upwindscheme.hh:74
static Scalar apply(const FluxVariables &fluxVars, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
applies a simple upwind scheme to the precalculated advective flux
Definition: flux/upwindscheme.hh:65
Forward declaration of the upwind scheme implementation.
Definition: flux/upwindscheme.hh:22
The available discretization methods in Dumux.
Scalar upwindSchemeMultiplier(const ElemVolVars &elemVolVars, const SubControlVolumeFace &scvf, const UpwindTermFunction &upwindTerm, Scalar flux, int phaseIdx)
returns the upwind factor which is multiplied to the advective flux across the given scvf
Definition: flux/upwindscheme.hh:36
Definition: adapt.hh:17
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.