Line | Branch | Exec | Source |
---|---|---|---|
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-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder | ||
5 | // SPDX-License-Identifier: GPL-3.0-or-later | ||
6 | // | ||
7 | /*! | ||
8 | * \file | ||
9 | * \ingroup Flux | ||
10 | * \brief Base class for the upwind scheme | ||
11 | */ | ||
12 | #ifndef DUMUX_DISCRETIZATION_UPWINDSCHEME_HH | ||
13 | #define DUMUX_DISCRETIZATION_UPWINDSCHEME_HH | ||
14 | |||
15 | #include <dumux/common/parameters.hh> | ||
16 | #include <dumux/discretization/method.hh> | ||
17 | |||
18 | namespace Dumux { | ||
19 | |||
20 | //! Forward declaration of the upwind scheme implementation | ||
21 | template<class GridGeometry, class DiscretizationMethod> | ||
22 | class UpwindSchemeImpl; | ||
23 | |||
24 | /*! | ||
25 | * \ingroup Flux | ||
26 | * \brief The upwind scheme used for the advective fluxes. | ||
27 | * This depends on the chosen discretization method. | ||
28 | */ | ||
29 | template<class GridGeometry> | ||
30 | using UpwindScheme = UpwindSchemeImpl<GridGeometry, typename GridGeometry::DiscretizationMethod>; | ||
31 | |||
32 | namespace Detail { | ||
33 | |||
34 | //! returns the upwind factor which is multiplied to the advective flux across the given scvf | ||
35 | template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar> | ||
36 | 6515076698 | Scalar 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 |
5/6✓ Branch 0 taken 1153 times.
✓ Branch 1 taken 3380472951 times.
✓ Branch 3 taken 824 times.
✓ Branch 4 taken 329 times.
✓ Branch 6 taken 824 times.
✗ Branch 7 not taken.
|
6515076698 | static const Scalar upwindWeight = getParamFromGroup<Scalar>(elemVolVars.gridVolVars().problem().paramGroup(), "Flux.UpwindWeight"); |
43 | |||
44 |
2/2✓ Branch 0 taken 16844570 times.
✓ Branch 1 taken 1419069292 times.
|
6515076698 | const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()]; |
45 |
3/3✓ Branch 1 taken 1711684359 times.
✓ Branch 2 taken 1042140569 times.
✓ Branch 0 taken 626648036 times.
|
6515076698 | const auto& outsideVolVars = elemVolVars[scvf.outsideScvIdx()]; |
46 | |||
47 | using std::signbit; | ||
48 |
2/2✓ Branch 0 taken 1529066979 times.
✓ Branch 1 taken 1851407125 times.
|
6515076698 | if (signbit(flux)) // if sign of flux is negative |
49 | 2945155010 | return (upwindWeight*upwindTerm(outsideVolVars) | |
50 |
1/2✓ Branch 0 taken 18432007 times.
✗ Branch 1 not taken.
|
2982019024 | + (1.0 - upwindWeight)*upwindTerm(insideVolVars)); |
51 | else | ||
52 | 3569921688 | return (upwindWeight*upwindTerm(insideVolVars) | |
53 |
1/2✓ Branch 0 taken 13906721 times.
✗ Branch 1 not taken.
|
3597735130 | + (1.0 - upwindWeight)*upwindTerm(outsideVolVars)); |
54 | } | ||
55 | |||
56 | } // end namespace Detail | ||
57 | |||
58 | //! Upwind scheme for control-volume finite element methods (uses the standard scheme) | ||
59 | template<class GridGeometry, class DM> | ||
60 | class UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CVFE<DM>> | ||
61 | { | ||
62 | public: | ||
63 | //! applies a simple upwind scheme to the precalculated advective flux | ||
64 | template<class FluxVariables, class UpwindTermFunction, class Scalar> | ||
65 | 1435890822 | static Scalar apply(const FluxVariables& fluxVars, | |
66 | const UpwindTermFunction& upwindTerm, | ||
67 | Scalar flux, int phaseIdx) | ||
68 | { | ||
69 | 1435890822 | return apply(fluxVars.elemVolVars(), fluxVars.scvFace(), upwindTerm, flux, phaseIdx); | |
70 | } | ||
71 | |||
72 | //! applies a simple upwind scheme to the precalculated advective flux across the given scvf | ||
73 | template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar> | ||
74 | 1435890822 | static Scalar apply(const ElemVolVars& elemVolVars, | |
75 | const SubControlVolumeFace& scvf, | ||
76 | const UpwindTermFunction& upwindTerm, | ||
77 | Scalar flux, int phaseIdx) | ||
78 | { | ||
79 | 1435890822 | return flux*multiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx); | |
80 | } | ||
81 | |||
82 | //! returns the upwind factor which is multiplied to the advective flux across the given scvf | ||
83 | template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar> | ||
84 | 1435913862 | static Scalar multiplier(const ElemVolVars& elemVolVars, | |
85 | const SubControlVolumeFace& scvf, | ||
86 | const UpwindTermFunction& upwindTerm, | ||
87 | Scalar flux, int phaseIdx) | ||
88 | { | ||
89 | 1435913862 | return Detail::upwindSchemeMultiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx); | |
90 | } | ||
91 | }; | ||
92 | |||
93 | //! Upwind scheme for the cell-centered tpfa scheme | ||
94 | template<class GridGeometry> | ||
95 | class 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 | |||
101 | public: | ||
102 | //! returns the upwind factor which is multiplied to the advective flux across the given scvf | ||
103 | template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar> | ||
104 | 1623004168 | 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 | 1622999752 | return Detail::upwindSchemeMultiplier(elemVolVars, scvf, upwindTerm, flux, phaseIdx); | |
111 | } | ||
112 | |||
113 | //! applies a simple upwind scheme to the precalculated advective flux across the given scvf | ||
114 | template<class ElemVolVars, class SubControlVolumeFace, class UpwindTermFunction, class Scalar> | ||
115 | 1604590012 | 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 | 1622970064 | 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 | 1950457037 | static Scalar apply(const FluxVariables& fluxVars, | |
127 | const UpwindTermFunction& upwindTerm, | ||
128 | Scalar flux, int phaseIdx) | ||
129 | { | ||
130 |
2/2✓ Branch 0 taken 15290356 times.
✓ Branch 1 taken 858991 times.
|
1950457037 | const auto& scvf = fluxVars.scvFace(); |
131 |
2/2✓ Branch 0 taken 15290356 times.
✓ Branch 1 taken 858991 times.
|
1950457037 | const auto& elemVolVars = fluxVars.elemVolVars(); |
132 | |||
133 | // standard scheme | ||
134 | if constexpr (dim == dimWorld) | ||
135 | 1622970064 | return apply(elemVolVars, scvf, upwindTerm, flux, phaseIdx); | |
136 | |||
137 | // on non-branching points the standard scheme works | ||
138 |
2/2✓ Branch 0 taken 15290356 times.
✓ Branch 1 taken 858991 times.
|
327486973 | if (scvf.numOutsideScvs() == 1) |
139 | 321556074 | 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 | 5930899 | const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()]; | |
144 | |||
145 | 5930899 | Scalar branchingPointUpwindTerm = 0.0; | |
146 |
2/2✓ Branch 0 taken 638946 times.
✓ Branch 1 taken 220045 times.
|
5930899 | Scalar sumUpwindFluxes = 0.0; |
147 | |||
148 | // if the inside flux is positive (outflow) do fully upwind and return flux | ||
149 | using std::signbit; | ||
150 |
2/2✓ Branch 0 taken 638946 times.
✓ Branch 1 taken 220045 times.
|
5930899 | if (!signbit(flux)) |
151 | 2688169 | return upwindTerm(insideVolVars)*flux; | |
152 | else | ||
153 | 3242730 | sumUpwindFluxes += flux; | |
154 | |||
155 |
2/2✓ Branch 0 taken 710563 times.
✓ Branch 1 taken 220045 times.
|
13007109 | for (unsigned int i = 0; i < scvf.numOutsideScvs(); ++i) |
156 | { | ||
157 | // compute the outside flux | ||
158 | 9764379 | const auto& fvGeometry = fluxVars.fvGeometry(); | |
159 | 9764379 | const auto outsideScvIdx = scvf.outsideScvIdx(i); | |
160 | 9764379 | const auto outsideElement = fvGeometry.gridGeometry().element(outsideScvIdx); | |
161 | 9764379 | const auto& flippedScvf = fvGeometry.flipScvf(scvf.index(), i); | |
162 | |||
163 | using AdvectionType = typename FluxVariables::AdvectionType; | ||
164 |
2/2✓ Branch 1 taken 667991 times.
✓ Branch 2 taken 42572 times.
|
17511657 | const auto outsideFlux = AdvectionType::flux(fluxVars.problem(), |
165 | outsideElement, | ||
166 | fvGeometry, | ||
167 | elemVolVars, | ||
168 | flippedScvf, | ||
169 | phaseIdx, | ||
170 | fluxVars.elemFluxVarsCache()); | ||
171 | |||
172 |
2/2✓ Branch 0 taken 667991 times.
✓ Branch 1 taken 42572 times.
|
9764379 | if (!signbit(outsideFlux)) |
173 | 5058863 | branchingPointUpwindTerm += upwindTerm(elemVolVars[outsideScvIdx])*outsideFlux; | |
174 | else | ||
175 | 4705516 | sumUpwindFluxes += outsideFlux; | |
176 | } | ||
177 | |||
178 | // the flux might be zero | ||
179 |
1/2✓ Branch 0 taken 220045 times.
✗ Branch 1 not taken.
|
3242730 | if (sumUpwindFluxes != 0.0) |
180 | 3242606 | 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 | 3242730 | if (signbit(flux)) | |
188 | 3242730 | return flux*branchingPointUpwindTerm; | |
189 | else | ||
190 | return flux*upwindTerm(insideVolVars); | ||
191 | } | ||
192 | }; | ||
193 | |||
194 | //! Upwind scheme for cell-centered mpfa schemes | ||
195 | template<class GridGeometry> | ||
196 | class UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CCMpfa> | ||
197 | : public UpwindSchemeImpl<GridGeometry, DiscretizationMethods::CCTpfa> {}; | ||
198 | |||
199 | } // end namespace Dumux | ||
200 | |||
201 | #endif | ||
202 |