volumevariables.hh 20.3 KB
Newer Older
1
2
3
4
5
6
7
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
8
 *   the Free Software Foundation, either version 3 of the License, or       *
9
10
11
12
13
14
15
16
17
18
19
20
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
21
 * \ingroup CO2Model
22
23
24
 * \brief Contains the quantities which are constant within a
 *        finite volume in the CO2 model.
 */
25

26
27
28
#ifndef DUMUX_CO2_VOLUME_VARIABLES_HH
#define DUMUX_CO2_VOLUME_VARIABLES_HH

Bernd Flemisch's avatar
Bernd Flemisch committed
29
30
#include <array>

31
32
33
#include <dune/common/exceptions.hh>

#include <dumux/porousmediumflow/volumevariables.hh>
34
#include <dumux/porousmediumflow/2p/formulation.hh>
Bernd Flemisch's avatar
Bernd Flemisch committed
35
#include <dumux/porousmediumflow/nonisothermal/volumevariables.hh>
Timo Koch's avatar
Timo Koch committed
36
#include <dumux/material/solidstates/updatesolidvolumefractions.hh>
37

38
39
#include "primaryvariableswitch.hh"

40
41
namespace Dumux {

42
/*!
43
 * \ingroup CO2Model
44
45
46
 * \brief Contains the quantities which are are constant within a
 *        finite volume in the CO2 model.
 */
47
48
template <class Traits>
class TwoPTwoCCO2VolumeVariables
49
: public PorousMediumFlowVolumeVariables<Traits>
Katharina Heck's avatar
Katharina Heck committed
50
, public EnergyVolumeVariables<Traits, TwoPTwoCCO2VolumeVariables<Traits> >
51
{
52
53
    using ParentType = PorousMediumFlowVolumeVariables< Traits>;
    using EnergyVolVars = EnergyVolumeVariables<Traits, TwoPTwoCCO2VolumeVariables<Traits> >;
54
55
56

    using Scalar = typename Traits::PrimaryVariables::value_type;
    using ModelTraits = typename Traits::ModelTraits;
57
    static constexpr int numFluidComps = ParentType::numFluidComponents();
58

59
60
61
    // component indices
    enum
    {
62
63
64
65
        comp0Idx = Traits::FluidSystem::comp0Idx,
        comp1Idx = Traits::FluidSystem::comp1Idx,
        phase0Idx = Traits::FluidSystem::phase0Idx,
        phase1Idx = Traits::FluidSystem::phase1Idx
66
67
    };

68
69
70
    // phase presence indices
    enum
    {
71
72
        firstPhaseOnly = ModelTraits::Indices::firstPhaseOnly,
        secondPhaseOnly = ModelTraits::Indices::secondPhaseOnly,
73
        bothPhases = ModelTraits::Indices::bothPhases
74
75
76
    };

    // primary variable indices
77
78
79
80
    enum
    {
        switchIdx = ModelTraits::Indices::switchIdx,
        pressureIdx = ModelTraits::Indices::pressureIdx
81
82
    };

83
    // formulation
84
    static constexpr auto formulation = ModelTraits::priVarFormulation();
85

86
    // type used for the permeability
87
    using PermeabilityType = typename Traits::PermeabilityType;
88
89
90

    // type used for the diffusion coefficients
    using EffDiffModel = typename Traits::EffectiveDiffusivityModel;
91
    using DiffusionCoefficients = typename Traits::DiffusionType::DiffusionCoefficientsContainer;
92

93
94
public:
    //! The type of the object returned by the fluidState() method
95
96
97
    using FluidState = typename Traits::FluidState;
    //! The fluid system used here
    using FluidSystem = typename Traits::FluidSystem;
98
99
    //! Export the indices
    using Indices = typename ModelTraits::Indices;
100
    //! Export type of solid state
101
    using SolidState = typename Traits::SolidState;
102
    //! Export type of solid system
103
    using SolidSystem = typename Traits::SolidSystem;
104
    //! Export the type of the primary variable switch
105
    using PrimaryVariableSwitch = TwoPTwoCCO2PrimaryVariableSwitch;
106

107

108
    //! Return whether moles or masses are balanced
109
    static constexpr bool useMoles() { return ModelTraits::useMoles(); }
110
    //! Return the two-phase formulation used here
111
112
113
    static constexpr TwoPFormulation priVarFormulation() { return formulation; }

    // check for permissive combinations
114
115
    static_assert(ModelTraits::numFluidPhases() == 2, "NumPhases set in the model is not two!");
    static_assert(ModelTraits::numFluidComponents() == 2, "NumComponents set in the model is not two!");
116
    static_assert((formulation == TwoPFormulation::p0s1 || formulation == TwoPFormulation::p1s0), "Chosen TwoPFormulation not supported!");
117
118

    /*!
119
     * \brief Updates all quantities for a given control volume.
120
121
122
123
124
125
126
127
128
129
130
     *
     * \param elemSol A vector containing all primary variables connected to the element
     * \param problem The object specifying the problem which ought to
     *                be simulated
     * \param element An element which contains part of the control volume
     * \param scv The sub control volume
    */
    template<class ElemSol, class Problem, class Element, class Scv>
    void update(const ElemSol& elemSol, const Problem& problem, const Element& element, const Scv& scv)
    {
        ParentType::update(elemSol, problem, element, scv);
131
        completeFluidState(elemSol, problem, element, scv, fluidState_, solidState_);
Timo Koch's avatar
Timo Koch committed
132

133
134
135
136
137
138
139
        // Second instance of a parameter cache. Could be avoided if
        // diffusion coefficients also became part of the fluid state.
        typename FluidSystem::ParameterCache paramCache;
        paramCache.updateAll(fluidState_);

        using MaterialLaw = typename Problem::SpatialParams::MaterialLaw;
        const auto& matParams = problem.spatialParams().materialLawParams(element, scv, elemSol);
140

141
142
        const int wPhaseIdx = fluidState_.wettingPhase();
        const int nPhaseIdx = 1 - wPhaseIdx;
143
144

        // relative permeabilities -> require wetting phase saturation as parameter!
145
146
        relativePermeability_[wPhaseIdx] = MaterialLaw::krw(matParams, saturation(wPhaseIdx));
        relativePermeability_[nPhaseIdx] = MaterialLaw::krn(matParams, saturation(wPhaseIdx));
147
148

        // porosity & permeabilty
Katharina Heck's avatar
Katharina Heck committed
149
        updateSolidVolumeFractions(elemSol, problem, element, scv, solidState_, numFluidComps);
150
        EnergyVolVars::updateSolidEnergyParams(elemSol, problem, element, scv, solidState_);
151
        permeability_ = problem.spatialParams().permeability(element, scv, elemSol);
152

153
154
155
156
157
158
        auto getEffectiveDiffusionCoefficient = [&](int phaseIdx, int compIIdx, int compJIdx)
        {
            return EffDiffModel::effectiveDiffusionCoefficient(*this, phaseIdx, compIIdx, compJIdx);
        };

        effectiveDiffCoeff_.update(getEffectiveDiffusionCoefficient);
159

160
        EnergyVolVars::updateEffectiveThermalConductivity();
161
162
163
    }

    /*!
164
     * \brief Completes the fluid state.
165
     *
166
167
168
     * \note TODO: This is a lot of copy paste from the 2p2c: factor out code!
     *
     * \param elemSol A vector containing all primary variables connected to the element
169
170
171
172
173
     * \param problem The object specifying the problem which ought to be simulated
     * \param element An element which contains part of the control volume
     * \param scv The sub-control volume
     * \param fluidState A container with the current (physical) state of the fluid
     * \param solidState A container with the current (physical) state of the solid
174
175
176
177
     *
     * Set temperature, saturations, capillary pressures, viscosities, densities and enthalpies.
     */
    template<class ElemSol, class Problem, class Element, class Scv>
178
179
180
181
    void completeFluidState(const ElemSol& elemSol,
                            const Problem& problem,
                            const Element& element,
                            const Scv& scv,
182
183
                            FluidState& fluidState,
                            SolidState& solidState)
184
    {
185
        EnergyVolVars::updateTemperature(elemSol, problem, element, scv, fluidState, solidState);
Timo Koch's avatar
Timo Koch committed
186

187
        const auto& priVars = elemSol[scv.localDofIndex()];
Timo Koch's avatar
Timo Koch committed
188
189
        const auto phasePresence = priVars.state();

190
191
        using MaterialLaw = typename Problem::SpatialParams::MaterialLaw;
        const auto& materialParams = problem.spatialParams().materialLawParams(element, scv, elemSol);
192
193
        const auto wPhaseIdx = problem.spatialParams().template wettingPhase<FluidSystem>(element, scv, elemSol);
        fluidState.setWettingPhase(wPhaseIdx);
194

Timo Koch's avatar
Timo Koch committed
195
        // set the saturations
196
197
198
199
200
201
202
203
204
205
        if (phasePresence == secondPhaseOnly)
        {
            fluidState.setSaturation(phase0Idx, 0.0);
            fluidState.setSaturation(phase1Idx, 1.0);
        }
        else if (phasePresence == firstPhaseOnly)
        {
            fluidState.setSaturation(phase0Idx, 1.0);
            fluidState.setSaturation(phase1Idx, 0.0);
        }
206
207
        else if (phasePresence == bothPhases)
        {
208
209
210
211
212
            if (formulation == TwoPFormulation::p0s1)
            {
                fluidState.setSaturation(phase1Idx, priVars[switchIdx]);
                fluidState.setSaturation(phase0Idx, 1 - priVars[switchIdx]);
            }
213
            else
214
215
216
217
            {
                fluidState.setSaturation(phase0Idx, priVars[switchIdx]);
                fluidState.setSaturation(phase1Idx, 1 - priVars[switchIdx]);
            }
Timo Koch's avatar
Timo Koch committed
218
        }
219
220
        else
            DUNE_THROW(Dune::InvalidStateException, "Invalid phase presence.");
Timo Koch's avatar
Timo Koch committed
221

222
        // set pressures of the fluid phases
223
        pc_ = MaterialLaw::pc(materialParams, fluidState.saturation(wPhaseIdx));
224
225
226
        if (formulation == TwoPFormulation::p0s1)
        {
            fluidState.setPressure(phase0Idx, priVars[pressureIdx]);
227
            fluidState.setPressure(phase1Idx, (wPhaseIdx == phase0Idx) ? priVars[pressureIdx] + pc_
228
                                                                       : priVars[pressureIdx] - pc_);
Timo Koch's avatar
Timo Koch committed
229
        }
230
231
232
        else
        {
            fluidState.setPressure(phase1Idx, priVars[pressureIdx]);
233
            fluidState.setPressure(phase0Idx, (wPhaseIdx == phase0Idx) ? priVars[pressureIdx] - pc_
234
                                                                       : priVars[pressureIdx] + pc_);
Timo Koch's avatar
Timo Koch committed
235
236
237
238
239
240
241
242
243
244
        }

        // calculate the phase compositions
        typename FluidSystem::ParameterCache paramCache;
        // both phases are present
        if (phasePresence == bothPhases)
        {
            //Get the equilibrium mole fractions from the FluidSystem and set them in the fluidState
            //xCO2 = equilibrium mole fraction of CO2 in the liquid phase
            //yH2O = equilibrium mole fraction of H2O in the gas phase
245
246
            const auto xwCO2 = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase0Idx);
            const auto xgH2O = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase1Idx);
Timo Koch's avatar
Timo Koch committed
247
248
            const auto xwH2O = 1 - xwCO2;
            const auto xgCO2 = 1 - xgH2O;
249
250
251
252
            fluidState.setMoleFraction(phase0Idx, comp0Idx, xwH2O);
            fluidState.setMoleFraction(phase0Idx, comp1Idx, xwCO2);
            fluidState.setMoleFraction(phase1Idx, comp0Idx, xgH2O);
            fluidState.setMoleFraction(phase1Idx, comp1Idx, xgCO2);
Timo Koch's avatar
Timo Koch committed
253
254
255
256
        }

        // only the nonwetting phase is present, i.e. nonwetting phase
        // composition is stored explicitly.
257
        else if (phasePresence == secondPhaseOnly)
Timo Koch's avatar
Timo Koch committed
258
        {
259
            if( useMoles() ) // mole-fraction formulation
260
261
            {
                // set the fluid state
262
263
                fluidState.setMoleFraction(phase1Idx, comp0Idx, priVars[switchIdx]);
                fluidState.setMoleFraction(phase1Idx, comp1Idx, 1-priVars[switchIdx]);
264
                // TODO give values for non-existing wetting phase
265
                const auto xwCO2 = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase0Idx);
Timo Koch's avatar
Timo Koch committed
266
                const auto xwH2O = 1 - xwCO2;
267
268
                fluidState.setMoleFraction(phase0Idx, comp1Idx, xwCO2);
                fluidState.setMoleFraction(phase0Idx, comp0Idx, xwH2O);
269
270
271
272
            }
            else // mass-fraction formulation
            {
                // setMassFraction() has only to be called 1-numComponents times
273
                fluidState.setMassFraction(phase1Idx, comp0Idx, priVars[switchIdx]);
274
                // TODO give values for non-existing wetting phase
275
                const auto xwCO2 = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase0Idx);
Timo Koch's avatar
Timo Koch committed
276
                const auto xwH2O = 1 - xwCO2;
277
278
                fluidState.setMoleFraction(phase0Idx, comp1Idx, xwCO2);
                fluidState.setMoleFraction(phase0Idx, comp0Idx, xwH2O);
279
            }
Timo Koch's avatar
Timo Koch committed
280
        }
281

Timo Koch's avatar
Timo Koch committed
282
283
        // only the wetting phase is present, i.e. wetting phase
        // composition is stored explicitly.
284
        else if (phasePresence == firstPhaseOnly)
Timo Koch's avatar
Timo Koch committed
285
        {
286
            if( useMoles() ) // mole-fraction formulation
Timo Koch's avatar
Timo Koch committed
287
288
            {
                // convert mass to mole fractions and set the fluid state
289
290
                fluidState.setMoleFraction(phase0Idx, comp0Idx, 1-priVars[switchIdx]);
                fluidState.setMoleFraction(phase0Idx, comp1Idx, priVars[switchIdx]);
Timo Koch's avatar
Timo Koch committed
291
                //  TODO give values for non-existing nonwetting phase
292
                Scalar xnH2O = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase1Idx);
Timo Koch's avatar
Timo Koch committed
293
                Scalar xnCO2 = 1 - xnH2O;
294
295
                fluidState.setMoleFraction(phase1Idx, comp1Idx, xnCO2);
                fluidState.setMoleFraction(phase1Idx, comp0Idx, xnH2O);
Timo Koch's avatar
Timo Koch committed
296
297
298
299
            }
            else // mass-fraction formulation
            {
                // setMassFraction() has only to be called 1-numComponents times
300
                fluidState.setMassFraction(phase0Idx, comp1Idx, priVars[switchIdx]);
Timo Koch's avatar
Timo Koch committed
301
                //  TODO give values for non-existing nonwetting phase
302
                Scalar xnH2O = FluidSystem::equilibriumMoleFraction(fluidState, paramCache, phase1Idx);
Timo Koch's avatar
Timo Koch committed
303
                Scalar xnCO2 = 1 - xnH2O;
304
305
                fluidState.setMoleFraction(phase1Idx, comp1Idx, xnCO2);
                fluidState.setMoleFraction(phase1Idx, comp0Idx, xnH2O);
Timo Koch's avatar
Timo Koch committed
306
307
308
            }
        }

309
        for (int phaseIdx = 0; phaseIdx < ModelTraits::numFluidPhases(); ++phaseIdx)
Timo Koch's avatar
Timo Koch committed
310
311
312
313
314
        {
            // set the viscosity and desity here if constraintsolver is not used
            paramCache.updateComposition(fluidState, phaseIdx);
            const Scalar rho = FluidSystem::density(fluidState, paramCache, phaseIdx);
            fluidState.setDensity(phaseIdx, rho);
315
316
            const Scalar rhoMolar = FluidSystem::molarDensity(fluidState, phaseIdx);
            fluidState.setMolarDensity(phaseIdx, rhoMolar);
Timo Koch's avatar
Timo Koch committed
317
318
319
320
            const Scalar mu = FluidSystem::viscosity(fluidState, paramCache, phaseIdx);
            fluidState.setViscosity(phaseIdx,mu);

            // compute and set the enthalpy
321
            Scalar h = EnergyVolVars::enthalpy(fluidState, paramCache, phaseIdx);
Timo Koch's avatar
Timo Koch committed
322
323
            fluidState.setEnthalpy(phaseIdx, h);
        }
324
    }
325
326
327
328
329
330
331

    /*!
     * \brief Returns the phase state within the control volume.
     */
    const FluidState &fluidState() const
    { return fluidState_; }

332
333
334
335
336
337
    /*!
     * \brief Returns the phase state for the control volume.
     */
    const SolidState &solidState() const
    { return solidState_; }

338
    /*!
Kilian Weishaupt's avatar
Kilian Weishaupt committed
339
     * \brief Returns the average molar mass \f$\mathrm{[kg/mol]}\f$ of the fluid phase.
340
341
342
343
344
     *
     * \param phaseIdx The phase index
     */
    Scalar averageMolarMass(int phaseIdx) const
    { return fluidState_.averageMolarMass(phaseIdx); }
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
    /*!
     * \brief Returns the saturation of a given phase within
     *        the control volume in \f$[-]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar saturation(const int phaseIdx) const
    { return fluidState_.saturation(phaseIdx); }

    /*!
     * \brief Returns the mass fraction of a given component in a
     *        given phase within the control volume in \f$[-]\f$.
     *
     * \param phaseIdx The phase index
     * \param compIdx The component index
     */
    Scalar massFraction(const int phaseIdx, const int compIdx) const
    { return fluidState_.massFraction(phaseIdx, compIdx); }

    /*!
     * \brief Returns the mole fraction of a given component in a
     *        given phase within the control volume in \f$[-]\f$.
     *
     * \param phaseIdx The phase index
     * \param compIdx The component index
     */
    Scalar moleFraction(const int phaseIdx, const int compIdx) const
    { return fluidState_.moleFraction(phaseIdx, compIdx); }

    /*!
     * \brief Returns the mass density of a given phase within the
     *        control volume in \f$[kg/m^3]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar density(const int phaseIdx) const
    { return fluidState_.density(phaseIdx); }

    /*!
     * \brief Returns the dynamic viscosity of the fluid within the
     *        control volume in \f$\mathrm{[Pa s]}\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar viscosity(const int phaseIdx) const
    { return fluidState_.viscosity(phaseIdx); }

    /*!
     * \brief Returns the mass density of a given phase within the
     *        control volume in \f$[mol/m^3]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar molarDensity(const int phaseIdx) const
400
    { return fluidState_.molarDensity(phaseIdx) ; }
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

    /*!
     * \brief Returns the effective pressure of a given phase within
     *        the control volume in \f$[kg/(m*s^2)=N/m^2=Pa]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar pressure(const int phaseIdx) const
    { return fluidState_.pressure(phaseIdx); }

    /*!
     * \brief Returns temperature within the control volume in \f$[K]\f$.
     *
     * Note that we assume thermodynamic equilibrium, i.e. the
     * temperature of the rock matrix and of all fluid phases are
     * identical.
     */
    Scalar temperature() const
    { return fluidState_.temperature(/*phaseIdx=*/0); }

    /*!
     * \brief Returns the relative permeability of a given phase within
     *        the control volume in \f$[-]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar relativePermeability(const int phaseIdx) const
    { return relativePermeability_[phaseIdx]; }

    /*!
     * \brief Returns the effective mobility of a given phase within
     *        the control volume in \f$[s*m/kg]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar mobility(const int phaseIdx) const
    { return relativePermeability_[phaseIdx]/fluidState_.viscosity(phaseIdx); }

    /*!
     * \brief Returns the effective capillary pressure within the control volume
     *        in \f$[kg/(m*s^2)=N/m^2=Pa]\f$.
     */
    Scalar capillaryPressure() const
444
    { return fluidState_.pressure(phase1Idx) - fluidState_.pressure(phase0Idx); }
445
446
447
448
449

    /*!
     * \brief Returns the average porosity within the control volume in \f$[-]\f$.
     */
    Scalar porosity() const
450
    { return solidState_.porosity(); }
451
452
453
454
455
456
457

    /*!
     * \brief Returns the average permeability within the control volume in \f$[m^2]\f$.
     */
    const PermeabilityType& permeability() const
    { return permeability_; }

458
459
460
461
    /*!
     * \brief Returns the binary diffusion coefficients for a phase in \f$[m^2/s]\f$.
     */
    Scalar diffusionCoefficient(int phaseIdx, int compIIdx, int compJIdx) const
462
463
464
465
466
    {
        typename FluidSystem::ParameterCache paramCache;
        paramCache.updatePhase(fluidState_, phaseIdx);
        return FluidSystem::binaryDiffusionCoefficient(fluidState_, paramCache, phaseIdx, compIIdx, compJIdx);
    }
467

468
469
470
    /*!
     * \brief Returns the effective diffusion coefficients for a phase in \f$[m^2/s]\f$.
     */
471
472
473
    Scalar effectiveDiffusionCoefficient(int phaseIdx, int compIIdx, int compJIdx) const
    { return effectiveDiffCoeff_(phaseIdx, compIIdx, compJIdx); }

474
475
476
477

    /*!
     * \brief Returns the wetting phase index
     */
478
    int wettingPhase() const
479
    { return fluidState_.wettingPhase(); }
480

481
482
private:
    FluidState fluidState_;
483
    SolidState solidState_;
484
485
    Scalar pc_;                     // The capillary pressure
    PermeabilityType permeability_; // Effective permeability within the control volume
486

487
    // Relative permeability within the control volume
488
    std::array<Scalar, ModelTraits::numFluidPhases()> relativePermeability_;
489

490
    // Effective diffusion coefficients for the phases
491
    DiffusionCoefficients effectiveDiffCoeff_;
492
493
};

Timo Koch's avatar
Timo Koch committed
494
} // end namespace Dumux
495
496

#endif