GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/porousmediumflow/co2/primaryvariableswitch.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 29 61 47.5%
Functions: 5 5 100.0%
Branches: 34 168 20.2%

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 CO2Model
10 * \brief The primary variable switch for the 2p2c-CO2 model
11 */
12
13 #ifndef DUMUX_2P2C_CO2_PRIMARY_VARIABLE_SWITCH_HH
14 #define DUMUX_2P2C_CO2_PRIMARY_VARIABLE_SWITCH_HH
15
16 #include <iostream>
17
18 #include <dumux/porousmediumflow/compositional/primaryvariableswitch.hh>
19 #include <dumux/porousmediumflow/2p/formulation.hh>
20
21 namespace Dumux
22 {
23 /*!
24 * \ingroup CO2Model
25 * \brief The primary variable switch for the 2p2c-CO2 model controlling the phase presence state variable.
26 *
27 * The phase switch occurs when the equilibrium concentration
28 * of a component in a phase is exceeded, instead of the sum of the components in the virtual phase
29 * (the phase which is not present) being greater that unity as done in the 2p2c model.
30 */
31 class TwoPTwoCCO2PrimaryVariableSwitch
32 : public PrimaryVariableSwitch< TwoPTwoCCO2PrimaryVariableSwitch >
33 {
34 using ParentType = PrimaryVariableSwitch< TwoPTwoCCO2PrimaryVariableSwitch >;
35 friend ParentType;
36
37 public:
38 using ParentType::ParentType;
39
40 protected:
41 // perform variable switch at a degree of freedom location
42 template<class VolumeVariables, class IndexType, class GlobalPosition>
43 6251144 bool update_(typename VolumeVariables::PrimaryVariables& priVars,
44 const VolumeVariables& volVars,
45 IndexType dofIdxGlobal,
46 const GlobalPosition& globalPos)
47 {
48 using Scalar = typename VolumeVariables::PrimaryVariables::value_type;
49
50 using FluidSystem = typename VolumeVariables::FluidSystem;
51 static constexpr int phase0Idx = FluidSystem::phase0Idx;
52 static constexpr int phase1Idx = FluidSystem::phase1Idx;
53 static constexpr int comp0Idx = FluidSystem::comp0Idx;
54 static constexpr int comp1Idx = FluidSystem::comp1Idx;
55
56 static constexpr bool useMoles = VolumeVariables::useMoles();
57 static constexpr auto formulation = VolumeVariables::priVarFormulation();
58 static_assert( (formulation == TwoPFormulation::p0s1 || formulation == TwoPFormulation::p1s0),
59 "Chosen TwoPFormulation not supported!");
60
61 using Indices = typename VolumeVariables::Indices;
62 static constexpr int switchIdx = Indices::switchIdx;
63
64 // evaluate primary variable switch
65 6251144 bool wouldSwitch = false;
66 6251144 int phasePresence = priVars.state();
67 6251144 int newPhasePresence = phasePresence;
68
69 // the param cache to evaluate the equilibrium mole fraction
70 typename FluidSystem::ParameterCache paramCache;
71
72 // check if a primary var switch is necessary
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6251144 times.
6251144 if (phasePresence == Indices::secondPhaseOnly)
74 {
75 // calculate wetting component mole fraction in the second phase
76 Scalar xnw = volVars.moleFraction(phase1Idx, comp0Idx);
77 Scalar xnwMax = FluidSystem::equilibriumMoleFraction(volVars.fluidState(), paramCache, phase1Idx);
78
79 // if it is larger than the equilibirum mole fraction switch
80 if(xnw > xnwMax)
81 wouldSwitch = true;
82
83 if (this->wasSwitched_[dofIdxGlobal])
84 xnwMax *= 1.02;
85
86 // if it is larger than the equilibirum mole fraction switch: first phase appears
87 if (xnw > xnwMax)
88 {
89 // wetting phase appears
90 if (this->verbosity() > 1)
91 std::cout << "First phase (" << FluidSystem::phaseName(phase0Idx) << ") appears at dof " << dofIdxGlobal
92 << ", coordinates: " << globalPos
93 << ", x^" << FluidSystem::componentName(comp0Idx) << "_" << FluidSystem::phaseName(phase1Idx) << " > x_equilibrium: "
94 << xnw << " > " << xnwMax << std::endl;
95 newPhasePresence = Indices::bothPhases;
96 if (formulation == TwoPFormulation::p1s0)
97 priVars[switchIdx] = 0.0;
98 else
99 priVars[switchIdx] = 1.0;
100 }
101 }
102
2/2
✓ Branch 0 taken 5955370 times.
✓ Branch 1 taken 295774 times.
6251144 else if (phasePresence == Indices::firstPhaseOnly)
103 {
104 // calculate second component mole fraction in the wetting phase
105 5955370 Scalar xwn = volVars.moleFraction(phase0Idx, comp1Idx);
106 11910740 Scalar xwnMax = FluidSystem::equilibriumMoleFraction(volVars.fluidState(), paramCache, phase0Idx);
107
108 // if it is larger than the equilibirum mole fraction switch
109
2/2
✓ Branch 0 taken 3564 times.
✓ Branch 1 taken 5951806 times.
5955370 if(xwn > xwnMax)
110 3564 wouldSwitch = true;
111
112
6/6
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 5955342 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 5955342 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 5955342 times.
17866110 if (this->wasSwitched_[dofIdxGlobal])
113 28 xwnMax *= 1.02;
114
115 // if it is larger than the equilibirum mole fraction switch second phase appears
116
2/2
✓ Branch 0 taken 3564 times.
✓ Branch 1 taken 5951806 times.
5955370 if(xwn > xwnMax)
117 {
118 // Second phase appears
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3564 times.
3564 if (this->verbosity() > 1)
120 std::cout << "Second phase (" << FluidSystem::phaseName(phase1Idx) << ") appears at dof " << dofIdxGlobal
121 << ", coordinates: " << globalPos
122 << ", x^" << FluidSystem::componentName(comp1Idx) << "_" << FluidSystem::phaseName(phase0Idx) << " > x_equilibrium: "
123 << xwn << " > " << xwnMax << std::endl;
124 3564 newPhasePresence = Indices::bothPhases;
125 if (formulation == TwoPFormulation::p1s0)
126 priVars[switchIdx] = 0.999;
127 else
128 7128 priVars[switchIdx] = 0.001;
129 }
130 }
131 // TODO: this is the same as for the 2p2c model maybe factor out
132
1/2
✓ Branch 0 taken 295774 times.
✗ Branch 1 not taken.
295774 else if (phasePresence == Indices::bothPhases)
133 {
134 295774 Scalar Smin = 0.0;
135
6/6
✓ Branch 0 taken 3564 times.
✓ Branch 1 taken 292210 times.
✓ Branch 2 taken 3564 times.
✓ Branch 3 taken 292210 times.
✓ Branch 4 taken 3564 times.
✓ Branch 5 taken 292210 times.
887322 if (this->wasSwitched_[dofIdxGlobal])
136 3564 Smin = -0.01;
137
138
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 295746 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 295746 times.
591548 if (volVars.saturation(phase1Idx) <= Smin)
139 {
140 28 wouldSwitch = true;
141 // nonwetting phase disappears
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (this->verbosity() > 1)
143 std::cout << "Second phase (" << FluidSystem::phaseName(phase1Idx) << ") disappears at dof " << dofIdxGlobal
144 << ", coordinates: " << globalPos
145 << ", S_" << FluidSystem::phaseName(phase1Idx) << ": " << volVars.saturation(phase1Idx)
146 << std::endl;
147 28 newPhasePresence = Indices::firstPhaseOnly;
148
149 if(useMoles) // mole-fraction formulation
150 priVars[switchIdx] = volVars.moleFraction(phase0Idx, comp1Idx);
151 else // mass-fraction formulation
152 28 priVars[switchIdx] = volVars.massFraction(phase0Idx, comp1Idx);
153 }
154
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 295746 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 295746 times.
591492 else if (volVars.saturation(phase0Idx) <= Smin)
155 {
156 wouldSwitch = true;
157 // wetting phase disappears
158 if (this->verbosity() > 1)
159 std::cout << "First phase (" << FluidSystem::phaseName(phase0Idx) << ") disappears at dof " << dofIdxGlobal
160 << ", coordinates: " << globalPos
161 << ", S_" << FluidSystem::phaseName(phase0Idx) << ": " << volVars.saturation(phase0Idx)
162 << std::endl;
163 newPhasePresence = Indices::secondPhaseOnly;
164
165 if(useMoles) // mole-fraction formulation
166 priVars[switchIdx] = volVars.moleFraction(phase1Idx, comp0Idx);
167 else // mass-fraction formulation
168 priVars[switchIdx] = volVars.massFraction(phase1Idx, comp0Idx);
169 }
170 }
171
172
2/2
✓ Branch 0 taken 3592 times.
✓ Branch 1 taken 6247552 times.
6251144 priVars.setState(newPhasePresence);
173
4/4
✓ Branch 0 taken 3592 times.
✓ Branch 1 taken 6247552 times.
✓ Branch 2 taken 3592 times.
✓ Branch 3 taken 6247552 times.
12502288 this->wasSwitched_[dofIdxGlobal] = wouldSwitch;
174 6251144 return phasePresence != newPhasePresence;
175 }
176 };
177
178 } // end namespace Dumux
179
180 #endif
181