GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: dumux/dumux/flux/box/fickslaw.hh
Date: 2025-04-12 19:19:20
Exec Total Coverage
Lines: 50 51 98.0%
Functions: 204 204 100.0%
Branches: 53 59 89.8%

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 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392040974 times.
406462612 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392040974 times.
406462612 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
92 406462612 const auto& outsideVolVars = elemVolVars[scvf.outsideScvIdx()];
93
94 // evaluate gradX at integration point and interpolate density
95 406462612 const auto& fluxVarsCache = elemFluxVarsCache[scvf];
96 406462612 const auto& shapeValues = fluxVarsCache.shapeValues();
97
98 // density interpolation
99 406462612 Scalar rho(0.0);
100
2/2
✓ Branch 0 taken 1571894476 times.
✓ Branch 1 taken 399242350 times.
2007200364 for (auto&& scv : scvs(fvGeometry))
101
1/2
✓ Branch 0 taken 28224000 times.
✗ Branch 1 not taken.
1628961752 rho += massOrMolarDensity(elemVolVars[scv], referenceSystem, phaseIdx)*shapeValues[scv.indexInElement()][0];
102
103 406462612 ComponentFluxVector componentFlux(0.0);
104
2/2
✓ Branch 0 taken 866305724 times.
✓ Branch 1 taken 399242350 times.
1287208860 for (int compIdx = 0; compIdx < numComponents; compIdx++)
105 {
106 if constexpr (!FluidSystem::isTracerFluidSystem())
107
4/4
✓ Branch 0 taken 394309950 times.
✓ Branch 1 taken 463915774 times.
✓ Branch 2 taken 32279600 times.
✓ Branch 3 taken 40597600 times.
929569048 if (compIdx == FluidSystem::getMainComponent(phaseIdx))
108 401861012 continue;
109
110 486965236 const auto diffCoeff = averageDiffusionCoefficient_(phaseIdx, compIdx, insideVolVars, outsideVolVars, problem, scvf);
111
112 // compute the diffusive flux
113
2/2
✓ Branch 0 taken 36682800 times.
✓ Branch 1 taken 25376400 times.
1802271032 const auto massOrMoleFrac = [&](const SubControlVolume& scv){ return massOrMoleFraction(elemVolVars[scv], referenceSystem, phaseIdx, compIdx); };
114
2/2
✓ Branch 1 taken 15974400 times.
✓ Branch 2 taken 7987200 times.
478885236 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 561600 times.
323865256 if (BalanceEqOpts::mainComponentIsBalanced(phaseIdx))
119
2/2
✓ Branch 0 taken 15974400 times.
✓ Branch 1 taken 7987200 times.
486218036 componentFlux[FluidSystem::getMainComponent(phaseIdx)] -= componentFlux[compIdx];
120 }
121 406462612 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
2/2
✓ Branch 0 taken 80000 times.
✓ Branch 1 taken 20000 times.
100000 for (auto&& scv : scvs(fvGeometry))
137 80000 rho += massOrMolarDensity(elemVolVars[scv], referenceSystem, phaseIdx)*shapeValues[scv.indexInElement()][0];
138
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20000 times.
20000 const auto& insideVolVars = elemVolVars[scvf.insideScvIdx()];
140 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 40000 const auto diffCoeff = averageDiffusionCoefficient_(phaseIdx, compIdx, insideVolVars, outsideVolVars, problem, scvf);
150
151 40000 ti[compIdx].resize(fvGeometry.numScv());
152
2/2
✓ Branch 0 taken 160000 times.
✓ Branch 1 taken 40000 times.
200000 for (auto&& scv : scvs(fvGeometry))
153 320000 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 478925236 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
2/2
✓ Branch 1 taken 286039338 times.
✓ Branch 2 taken 177545636 times.
478925236 auto [insideDiffCoeff, outsideDiffCoeff] = diffusionCoefficientsAtInterface_(phaseIdx, compIdx, insideVV, outsideVV);
167
168 // scale by extrusion factor
169
2/2
✓ Branch 0 taken 286039338 times.
✓ Branch 1 taken 177545636 times.
478925236 insideDiffCoeff *= insideVV.extrusionFactor();
170
4/4
✓ Branch 0 taken 290079338 times.
✓ Branch 1 taken 181585636 times.
✓ Branch 2 taken 20000 times.
✓ Branch 3 taken 20000 times.
478925236 outsideDiffCoeff *= outsideVV.extrusionFactor();
171
172 // the resulting averaged diffusion tensor
173
5/6
✓ Branch 0 taken 290079338 times.
✓ Branch 1 taken 181585636 times.
✓ Branch 2 taken 20000 times.
✓ Branch 3 taken 20000 times.
✓ Branch 5 taken 40000 times.
✗ Branch 6 not taken.
482985236 return faceTensorAverage(insideDiffCoeff, outsideDiffCoeff, scvf.unitOuterNormal());
174 }
175
176 static std::pair<Scalar, Scalar>
177
4/4
✓ Branch 0 taken 20014400 times.
✓ Branch 1 taken 28663200 times.
✓ Branch 2 taken 20000 times.
✓ Branch 3 taken 20000 times.
478925236 diffusionCoefficientsAtInterface_([[maybe_unused]] const int phaseIdx, const int compIdx,
178 const VolumeVariables& insideVV, const VolumeVariables& outsideVV)
179 {
180 if constexpr (!FluidSystem::isTracerFluidSystem())
181 {
182 470805236 const auto mainCompIdx = FluidSystem::getMainComponent(phaseIdx);
183 470805236 const auto insideDiffCoeff = insideVV.effectiveDiffusionCoefficient(phaseIdx, mainCompIdx, compIdx);
184 470805236 const auto outsideDiffCoeff = outsideVV.effectiveDiffusionCoefficient(phaseIdx, mainCompIdx, compIdx);
185 470805236 return { std::move(insideDiffCoeff), std::move(outsideDiffCoeff) };
186 }
187 else
188 {
189 8120000 const auto insideDiffCoeff = insideVV.effectiveDiffusionCoefficient(0, 0, compIdx);
190
4/4
✓ Branch 0 taken 4040000 times.
✓ Branch 1 taken 4040000 times.
✓ Branch 2 taken 20000 times.
✓ Branch 3 taken 20000 times.
8120000 const auto outsideDiffCoeff = outsideVV.effectiveDiffusionCoefficient(0, 0, compIdx);
191
4/4
✓ Branch 0 taken 4040000 times.
✓ Branch 1 taken 4040000 times.
✓ Branch 2 taken 20000 times.
✓ Branch 3 taken 20000 times.
8120000 return { std::move(insideDiffCoeff), std::move(outsideDiffCoeff) };
192 }
193 }
194
195 template<class EvaluateVariable, class Tensor>
196 471664974 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 471664974 Dune::FieldVector<Scalar, dimWorld> gradX(0.0);
203
3/3
✓ Branch 0 taken 1717880032 times.
✓ Branch 1 taken 495275112 times.
✓ Branch 2 taken 10276800 times.
2259495482 for (auto&& scv : scvs(fvGeometry))
204 3476869400 gradX.axpy(massOrMoleFraction(scv), fluxVarsCache.gradN(scv.indexInElement()));
205 471664974 return -1.0*preFactor*vtmv(scvf.unitOuterNormal(), D, gradX)*Extrusion::area(fvGeometry, scvf);
206 }
207 };
208
209 } // end namespace Dumux
210
211 #endif
212