GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/flux/box/fickslaw.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 49 50 98.0%
Functions: 150 213 70.4%
Branches: 90 103 87.4%

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-FileCopyrightInfo: 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 BoxFlux
10 * \brief This file contains the data which is required to calculate
11 * diffusive fluxes due to molecular diffusion with Fick's law.
12 */
13 #ifndef DUMUX_DISCRETIZATION_BOX_FICKS_LAW_HH
14 #define DUMUX_DISCRETIZATION_BOX_FICKS_LAW_HH
15
16 #include <dune/common/fvector.hh>
17 #include <dune/common/fmatrix.hh>
18
19 #include <dumux/common/math.hh>
20 #include <dumux/common/properties.hh>
21 #include <dumux/discretization/method.hh>
22 #include <dumux/discretization/extrusion.hh>
23
24 #include <dumux/flux/fickiandiffusioncoefficients.hh>
25 #include <dumux/flux/referencesystemformulation.hh>
26 #include <dumux/flux/facetensoraverage.hh>
27
28 namespace Dumux {
29
30 // forward declaration
31 template<class TypeTag, class DiscretizationMethod, ReferenceSystemFormulation referenceSystem>
32 class FicksLawImplementation;
33
34 /*!
35 * \ingroup BoxFlux
36 * \brief Specialization of Fick's Law for the box method.
37 */
38 template <class TypeTag, ReferenceSystemFormulation referenceSystem>
39 class FicksLawImplementation<TypeTag, DiscretizationMethods::Box, referenceSystem>
40 {
41 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
42 using Problem = GetPropType<TypeTag, Properties::Problem>;
43 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
44 using VolumeVariables = GetPropType<TypeTag, Properties::VolumeVariables>;
45 using GridGeometry = GetPropType<TypeTag, Properties::GridGeometry>;
46 using FVElementGeometry = typename GridGeometry::LocalView;
47 using SubControlVolume = typename GridGeometry::SubControlVolume;
48 using SubControlVolumeFace = typename GridGeometry::SubControlVolumeFace;
49 using Extrusion = Extrusion_t<GridGeometry>;
50 using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView;
51 using GridFluxVariablesCache = GetPropType<TypeTag, Properties::GridFluxVariablesCache>;
52 using ElementFluxVariablesCache = typename GridFluxVariablesCache::LocalView;
53 using FluxVarCache = typename GridFluxVariablesCache::FluxVariablesCache;
54 using BalanceEqOpts = GetPropType<TypeTag, Properties::BalanceEqOpts>;
55 using GridView = typename GetPropType<TypeTag, Properties::GridGeometry>::GridView;
56 using Element = typename GridView::template Codim<0>::Entity;
57 using ModelTraits = GetPropType<TypeTag, Properties::ModelTraits>;
58
59 enum { dim = GridView::dimension} ;
60 enum { dimWorld = GridView::dimensionworld} ;
61 enum
62 {
63 numPhases = ModelTraits::numFluidPhases(),
64 numComponents = ModelTraits::numFluidComponents()
65 };
66 using DimWorldMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
67 using ComponentFluxVector = Dune::FieldVector<Scalar, numComponents>;
68
69 public:
70 using DiffusionCoefficientsContainer = FickianDiffusionCoefficients<Scalar, numPhases, numComponents>;
71
72 //return the reference system
73 static constexpr ReferenceSystemFormulation referenceSystemFormulation()
74 { return referenceSystem; }
75
76 /*!
77 * \brief Returns the diffusive fluxes of all components within
78 * a fluid phase across the given sub-control volume face.
79 * The computed fluxes are given in mole/s or kg/s, depending
80 * on the template parameter ReferenceSystemFormulation.
81 */
82 405837172 static ComponentFluxVector flux(const Problem& problem,
83 const Element& element,
84 const FVElementGeometry& fvGeometry,
85 const ElementVolumeVariables& elemVolVars,
86 const SubControlVolumeFace& scvf,
87 const int phaseIdx,
88 const ElementFluxVariablesCache& elemFluxVarsCache)
89 {
90 // get inside and outside diffusion tensors and calculate the harmonic mean
91
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 391415534 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 391415534 times.
811674344 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391415534 times.
405837172 const auto& outsideVolVars = elemVolVars[scvf.outsideScvIdx()];
93
94 // evaluate gradX at integration point and interpolate density
95 405837172 const auto& fluxVarsCache = elemFluxVarsCache[scvf];
96 405837172 const auto& shapeValues = fluxVarsCache.shapeValues();
97
98 // density interpolation
99 405837172 Scalar rho(0.0);
100
4/4
✓ Branch 0 taken 1569392716 times.
✓ Branch 1 taken 398616910 times.
✓ Branch 2 taken 1569392716 times.
✓ Branch 3 taken 398616910 times.
4008146328 for (auto&& scv : scvs(fvGeometry))
101
2/4
✓ Branch 0 taken 28224000 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28224000 times.
✗ Branch 3 not taken.
6392943968 rho += massOrMolarDensity(elemVolVars[scv], referenceSystem, phaseIdx)*shapeValues[scv.indexInElement()][0];
102
103 405837172 ComponentFluxVector componentFlux(0.0);
104
2/2
✓ Branch 0 taken 865027844 times.
✓ Branch 1 taken 398616910 times.
1285305540 for (int compIdx = 0; compIdx < numComponents; compIdx++)
105 {
106 if constexpr (!FluidSystem::isTracerFluidSystem())
107
4/4
✓ Branch 0 taken 393657510 times.
✓ Branch 1 taken 463290334 times.
✓ Branch 2 taken 32279600 times.
✓ Branch 3 taken 40597600 times.
928291168 if (compIdx == FluidSystem::getMainComponent(phaseIdx))
108 401208572 continue;
109
110 486339796 const auto diffCoeff = averageDiffusionCoefficient_(phaseIdx, compIdx, insideVolVars, outsideVolVars, problem, scvf);
111
112 // compute the diffusive flux
113
4/4
✓ Branch 0 taken 53600400 times.
✓ Branch 1 taken 8458800 times.
✓ Branch 2 taken 53600400 times.
✓ Branch 3 taken 8458800 times.
3459017944 const auto massOrMoleFrac = [&](const SubControlVolume& scv){ return massOrMoleFraction(elemVolVars[scv], referenceSystem, phaseIdx, compIdx); };
114
2/2
✓ Branch 1 taken 15974400 times.
✓ Branch 2 taken 24623200 times.
486339796 componentFlux[compIdx] = discreteFlux_(fvGeometry, scvf, fluxVarsCache, massOrMoleFrac, diffCoeff, rho);
115
116 // if the main component is balanced subtract the same flux from there (conservation)
117 if constexpr (!FluidSystem::isTracerFluidSystem())
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 588600 times.
470179796 if (BalanceEqOpts::mainComponentIsBalanced(phaseIdx))
119
4/4
✓ Branch 0 taken 15974400 times.
✓ Branch 1 taken 24623200 times.
✓ Branch 2 taken 15974400 times.
✓ Branch 3 taken 24623200 times.
1433396788 componentFlux[FluidSystem::getMainComponent(phaseIdx)] -= componentFlux[compIdx];
120 }
121 405837172 return componentFlux;
122 }
123
124 // compute transmissibilities ti for analytical jacobians
125 static std::array<std::vector<Scalar>, numComponents>
126 20000 calculateTransmissibilities(const Problem& problem,
127 const Element& element,
128 const FVElementGeometry& fvGeometry,
129 const ElementVolumeVariables& elemVolVars,
130 const SubControlVolumeFace& scvf,
131 const FluxVarCache& fluxVarCache,
132 const int phaseIdx)
133 {
134 20000 Scalar rho(0.0);
135 20000 const auto& shapeValues = fluxVarCache.shapeValues();
136
4/4
✓ Branch 0 taken 80000 times.
✓ Branch 1 taken 20000 times.
✓ Branch 2 taken 80000 times.
✓ Branch 3 taken 20000 times.
200000 for (auto&& scv : scvs(fvGeometry))
137 320000 rho += massOrMolarDensity(elemVolVars[scv], referenceSystem, phaseIdx)*shapeValues[scv.indexInElement()][0];
138
139
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20000 times.
40000 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20000 times.
20000 const auto& outsideVolVars = elemVolVars[scvf.outsideScvIdx()];
141
142 20000 std::array<std::vector<Scalar>, numComponents> ti;
143
2/2
✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 20000 times.
60000 for (int compIdx = 0; compIdx < numComponents; compIdx++)
144 {
145 if constexpr (!FluidSystem::isTracerFluidSystem())
146 if (compIdx == FluidSystem::getMainComponent(phaseIdx))
147 continue;
148
149 80000 const auto diffCoeff = averageDiffusionCoefficient_(phaseIdx, compIdx, insideVolVars, outsideVolVars, problem, scvf);
150
151
2/4
✓ Branch 1 taken 40000 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40000 times.
✗ Branch 5 not taken.
80000 ti[compIdx].resize(fvGeometry.numScv());
152
4/4
✓ Branch 0 taken 160000 times.
✓ Branch 1 taken 40000 times.
✓ Branch 2 taken 160000 times.
✓ Branch 3 taken 40000 times.
400000 for (auto&& scv : scvs(fvGeometry))
153 1120000 ti[compIdx][scv.indexInElement()] = -rho*vtmv(scvf.unitOuterNormal(), diffCoeff, fluxVarCache.gradN(scv.indexInElement()))*Extrusion::area(fvGeometry, scvf);
154 }
155
156 20000 return ti;
157 }
158
159 private:
160 static Scalar averageDiffusionCoefficient_(const int phaseIdx, const int compIdx,
161 const VolumeVariables& insideVV, const VolumeVariables& outsideVV,
162 const Problem& problem,
163 const SubControlVolumeFace& scvf)
164 {
165 // effective diffusion tensors
166
6/6
✓ Branch 0 taken 2020000 times.
✓ Branch 1 taken 2020000 times.
✓ Branch 2 taken 2020000 times.
✓ Branch 3 taken 2020000 times.
✓ Branch 4 taken 20000 times.
✓ Branch 5 taken 20000 times.
8120000 auto [insideDiffCoeff, outsideDiffCoeff] = diffusionCoefficientsAtInterface_(phaseIdx, compIdx, insideVV, outsideVV);
167
168 // scale by extrusion factor
169 8120000 insideDiffCoeff *= insideVV.extrusionFactor();
170 8120000 outsideDiffCoeff *= outsideVV.extrusionFactor();
171
172 // the resulting averaged diffusion tensor
173
13/15
✓ Branch 0 taken 2020000 times.
✓ Branch 1 taken 2020000 times.
✓ Branch 2 taken 2020000 times.
✓ Branch 3 taken 2020000 times.
✓ Branch 4 taken 2020000 times.
✓ Branch 5 taken 2020000 times.
✓ Branch 6 taken 2020000 times.
✓ Branch 7 taken 2020000 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 20000 times.
✓ Branch 10 taken 20000 times.
✓ Branch 11 taken 20000 times.
✓ Branch 12 taken 20000 times.
✓ Branch 14 taken 40000 times.
✗ Branch 15 not taken.
20300000 return faceTensorAverage(insideDiffCoeff, outsideDiffCoeff, scvf.unitOuterNormal());
174 }
175
176 static std::pair<Scalar, Scalar>
177 470179796 diffusionCoefficientsAtInterface_([[maybe_unused]] const int phaseIdx, const int compIdx,
178 const VolumeVariables& insideVV, const VolumeVariables& outsideVV)
179 {
180 if constexpr (!FluidSystem::isTracerFluidSystem())
181 {
182
2/2
✓ Branch 0 taken 15974400 times.
✓ Branch 1 taken 24623200 times.
470179796 const auto mainCompIdx = FluidSystem::getMainComponent(phaseIdx);
183 470179796 const auto insideDiffCoeff = insideVV.effectiveDiffusionCoefficient(phaseIdx, mainCompIdx, compIdx);
184 470179796 const auto outsideDiffCoeff = outsideVV.effectiveDiffusionCoefficient(phaseIdx, mainCompIdx, compIdx);
185 940359592 return { std::move(insideDiffCoeff), std::move(outsideDiffCoeff) };
186 }
187 else
188 {
189
6/6
✓ Branch 0 taken 2020000 times.
✓ Branch 1 taken 2020000 times.
✓ Branch 2 taken 2020000 times.
✓ Branch 3 taken 2020000 times.
✓ Branch 4 taken 20000 times.
✓ Branch 5 taken 20000 times.
8120000 const auto insideDiffCoeff = insideVV.effectiveDiffusionCoefficient(0, 0, compIdx);
190
6/6
✓ Branch 0 taken 2020000 times.
✓ Branch 1 taken 2020000 times.
✓ Branch 2 taken 2020000 times.
✓ Branch 3 taken 2020000 times.
✓ Branch 4 taken 20000 times.
✓ Branch 5 taken 20000 times.
8120000 const auto outsideDiffCoeff = outsideVV.effectiveDiffusionCoefficient(0, 0, compIdx);
191
12/12
✓ Branch 0 taken 2020000 times.
✓ Branch 1 taken 2020000 times.
✓ Branch 2 taken 2020000 times.
✓ Branch 3 taken 2020000 times.
✓ Branch 4 taken 2020000 times.
✓ Branch 5 taken 2020000 times.
✓ Branch 6 taken 2020000 times.
✓ Branch 7 taken 2020000 times.
✓ Branch 8 taken 20000 times.
✓ Branch 9 taken 20000 times.
✓ Branch 10 taken 20000 times.
✓ Branch 11 taken 20000 times.
16240000 return { std::move(insideDiffCoeff), std::move(outsideDiffCoeff) };
192 }
193 }
194
195 template<class EvaluateVariable, class Tensor>
196 471039534 static Scalar discreteFlux_(const FVElementGeometry& fvGeometry,
197 const SubControlVolumeFace& scvf,
198 const FluxVarCache& fluxVarsCache,
199 const EvaluateVariable& massOrMoleFraction,
200 const Tensor& D, const Scalar preFactor)
201 {
202 471039534 Dune::FieldVector<Scalar, dimWorld> gradX(0.0);
203
4/4
✓ Branch 0 taken 1756485472 times.
✓ Branch 1 taken 463819272 times.
✓ Branch 2 taken 1756485472 times.
✓ Branch 3 taken 463819272 times.
4512736564 for (auto&& scv : scvs(fvGeometry))
204 5415713104 gradX.axpy(massOrMoleFraction(scv), fluxVarsCache.gradN(scv.indexInElement()));
205 1040870684 return -1.0*preFactor*vtmv(scvf.unitOuterNormal(), D, gradX)*Extrusion::area(fvGeometry, scvf);
206 }
207 };
208
209 } // end namespace Dumux
210
211 #endif
212