volumevariables.hh 20.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// -*- 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    *
 *   the Free Software Foundation, either version 2 of the License, or       *
 *   (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 TwoPNCModel
22
23
24
25
26
27
28
29
30
31
 * \brief Contains the quantities which are constant within a
 *        finite volume in the two-phase, n-component model.
 */
#ifndef DUMUX_2PNC_VOLUME_VARIABLES_HH
#define DUMUX_2PNC_VOLUME_VARIABLES_HH

#include <iostream>
#include <vector>

#include <dumux/common/math.hh>
Timo Koch's avatar
Timo Koch committed
32
#include <dumux/discretization/methods.hh>
33

34
#include <dumux/porousmediumflow/volumevariables.hh>
35
36
37
#include <dumux/porousmediumflow/nonisothermal/volumevariables.hh>

#include <dumux/material/fluidstates/compositional.hh>
Timo Koch's avatar
Timo Koch committed
38
#include <dumux/material/solidstates/updatesolidvolumefractions.hh>
39
#include <dumux/material/constraintsolvers/computefromreferencephase.hh>
40
#include <dumux/material/constraintsolvers/misciblemultiphasecomposition.hh>
41

42
#include <dumux/porousmediumflow/2p/formulation.hh>
43

44
namespace Dumux {
45
46
47
48
49
50

/*!
 * \ingroup TwoPNCModel
 * \brief Contains the quantities which are are constant within a
 *        finite volume in the two-phase, n-component model.
 */
51
52
template <class Traits>
class TwoPNCVolumeVariables
53
: public PorousMediumFlowVolumeVariables<Traits>
Katharina Heck's avatar
Katharina Heck committed
54
, public EnergyVolumeVariables<Traits, TwoPNCVolumeVariables<Traits> >
55
{
56
57
    using ParentType = PorousMediumFlowVolumeVariables<Traits>;
    using EnergyVolVars = EnergyVolumeVariables<Traits, TwoPNCVolumeVariables<Traits> >;
58
59
60
    using Scalar = typename Traits::PrimaryVariables::value_type;
    using PermeabilityType = typename Traits::PermeabilityType;
    using FS = typename Traits::FluidSystem;
61
    using ModelTraits = typename Traits::ModelTraits;
Katharina Heck's avatar
Katharina Heck committed
62
    static constexpr int numFluidComps = ParentType::numComponents();
63
64
    enum
    {
65
        numMajorComponents = ModelTraits::numPhases(),
66
67

        // phase indices
68
69
        phase0Idx = FS::phase0Idx,
        phase1Idx = FS::phase1Idx,
70
71

        // component indices
72
73
        comp0Idx = FS::comp0Idx,
        comp1Idx = FS::comp1Idx,
74
75

        // phase presence enums
76
77
78
        secondPhaseOnly = ModelTraits::Indices::secondPhaseOnly,
        firstPhaseOnly = ModelTraits::Indices::firstPhaseOnly,
        bothPhases = ModelTraits::Indices::bothPhases,
79
80

        // primary variable indices
81
82
        pressureIdx = ModelTraits::Indices::pressureIdx,
        switchIdx = ModelTraits::Indices::switchIdx
83
84
    };

85
86
    static constexpr auto formulation = ModelTraits::priVarFormulation();
    static constexpr bool setFirstPhaseMoleFractions = ModelTraits::setMoleFractionsForFirstPhase();
87
88
89

    using MiscibleMultiPhaseComposition = Dumux::MiscibleMultiPhaseComposition<Scalar, FS>;
    using ComputeFromReferencePhase = Dumux::ComputeFromReferencePhase<Scalar, FS>;
90

91

92
public:
93
94
95
96
    //! export fluid state type
    using FluidState = typename Traits::FluidState;
    //! export fluid system type
    using FluidSystem = typename Traits::FluidSystem;
97
98
99
100
    //! export type of solid state
    using SolidState = typename Traits::SolidState;
    //! export type of solid system
    using SolidSystem = typename Traits::SolidSystem;
101

102
103
104
105
    //! return whether moles or masses are balanced
    static constexpr bool useMoles() { return Traits::ModelTraits::useMoles(); }
    //! return the two-phase formulation used here
    static constexpr TwoPFormulation priVarFormulation() { return formulation; }
106

107
    // check for permissive specifications
108
    static_assert(useMoles(), "use moles has to be set true in the 2pnc model");
109
110
    static_assert(ModelTraits::numPhases() == 2, "NumPhases set in the model is not two!");
    static_assert((formulation == TwoPFormulation::p0s1 || formulation == TwoPFormulation::p1s0), "Chosen TwoPFormulation not supported!");
111
112

    /*!
113
114
115
116
117
118
119
120
     * \brief Update all quantities for a given control volume
     *
     * \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
    */
121
122
    template<class ElemSol, class Problem, class Element, class Scv>
    void update(const ElemSol &elemSol,
123
124
                const Problem &problem,
                const Element &element,
125
                const Scv& scv)
126
    {
127
        ParentType::update(elemSol, problem, element, scv);
128
129

        completeFluidState(elemSol, problem, element, scv, fluidState_, solidState_);
130
131
132
133

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

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

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        const int wPhaseIdx = problem.spatialParams().template wettingPhase<FluidSystem>(element, scv, elemSol);
        const int nPhaseIdx = 1 - wPhaseIdx;

        // mobilities -> require wetting phase saturation as parameter!
        mobility_[wPhaseIdx] = MaterialLaw::krw(matParams, saturation(wPhaseIdx))/fluidState_.viscosity(wPhaseIdx);
        mobility_[nPhaseIdx] = MaterialLaw::krn(matParams, saturation(wPhaseIdx))/fluidState_.viscosity(nPhaseIdx);

        // binary diffusion coefficients
        for (unsigned int compJIdx = 0; compJIdx < ModelTraits::numComponents(); ++compJIdx)
        {
            if(compJIdx != comp0Idx)
                setDiffusionCoefficient_( phase0Idx, compJIdx,
                                          FluidSystem::binaryDiffusionCoefficient(fluidState_,
                                                                                  paramCache,
                                                                                  phase0Idx,
                                                                                  comp0Idx,
                                                                                  compJIdx) );
            if(compJIdx != comp1Idx)
                setDiffusionCoefficient_( phase1Idx, compJIdx,
                                          FluidSystem::binaryDiffusionCoefficient(fluidState_,
                                                                                  paramCache,
                                                                                  phase1Idx,
                                                                                  comp1Idx,
                                                                                  compJIdx) );
167
        }
168

169
        // calculate the remaining quantities
Katharina Heck's avatar
Katharina Heck committed
170
        updateSolidVolumeFractions(elemSol, problem, element, scv, solidState_, numFluidComps);
171
        EnergyVolVars::updateSolidEnergyParams(elemSol, problem, element, scv, solidState_);
172
        permeability_ = problem.spatialParams().permeability(element, scv, elemSol);
173
174
    }

175
    /*!
176
177
178
179
180
181
182
183
184
     * \brief Complete the fluid state
     *
     * \param elemSol A vector containing all primary variables connected to the element
     * \param problem The problem
     * \param element The element
     * \param scv The sub control volume
     * \param fluidState The fluid state
     *
     * Set temperature, saturations, capillary pressures, viscosities, densities and enthalpies.
185
     */
186
    template<class ElemSol, class Problem, class Element, class Scv>
187
188
189
190
    void completeFluidState(const ElemSol& elemSol,
                            const Problem& problem,
                            const Element& element,
                            const Scv& scv,
191
192
                            FluidState& fluidState,
                            SolidState& solidState)
193
    {
194
        EnergyVolVars::updateTemperature(elemSol, problem, element, scv, fluidState, solidState);
195

196
        const auto& priVars = ParentType::extractDofPriVars(elemSol, scv);
197
        const auto phasePresence = priVars.state();
198

199
200
201
202
        using MaterialLaw = typename Problem::SpatialParams::MaterialLaw;
        const auto& materialParams = problem.spatialParams().materialLawParams(element, scv, elemSol);
        const int wPhaseIdx = problem.spatialParams().template wettingPhase<FluidSystem>(element, scv, elemSol);
        fluidState.setWettingPhase(wPhaseIdx);
203

204
205
        // set the saturations
        if (phasePresence == secondPhaseOnly)
206
        {
207
208
            fluidState.setSaturation(phase0Idx, 0.0);
            fluidState.setSaturation(phase1Idx, 1.0);
209
        }
210
        else if (phasePresence == firstPhaseOnly)
211
        {
212
213
            fluidState.setSaturation(phase0Idx, 1.0);
            fluidState.setSaturation(phase1Idx, 0.0);
214
        }
215
216
        else if (phasePresence == bothPhases)
        {
217
218
219
220
221
            if (formulation == TwoPFormulation::p0s1)
            {
                fluidState.setSaturation(phase1Idx, priVars[switchIdx]);
                fluidState.setSaturation(phase0Idx, 1 - priVars[switchIdx]);
            }
Timo Koch's avatar
Timo Koch committed
222
            else
223
224
225
226
            {
                fluidState.setSaturation(phase0Idx, priVars[switchIdx]);
                fluidState.setSaturation(phase1Idx, 1 - priVars[switchIdx]);
            }
227
        }
228
229
        else
            DUNE_THROW(Dune::InvalidStateException, "phasePresence: " << phasePresence << " is invalid.");
230

231
232
233
        // set pressures of the fluid phases
        pc_ = MaterialLaw::pc(materialParams, fluidState.saturation(wPhaseIdx));
        if (formulation == TwoPFormulation::p0s1)
234
        {
235
236
237
            fluidState.setPressure(phase0Idx, priVars[pressureIdx]);
            fluidState.setPressure(phase1Idx, (wPhaseIdx == phase0Idx) ? priVars[pressureIdx] + pc_
                                                                       : priVars[pressureIdx] - pc_);
238
        }
239
240
        else
        {
241
242
243
            fluidState.setPressure(phase1Idx, priVars[pressureIdx]);
            fluidState.setPressure(phase0Idx, (wPhaseIdx == phase0Idx) ? priVars[pressureIdx] - pc_
                                                                       : priVars[pressureIdx] + pc_);
244
        }
245
246

        // calculate the phase compositions
247
        typename FluidSystem::ParameterCache paramCache;
248
249

        // now comes the tricky part: calculate phase composition
250
251
        if (phasePresence == bothPhases)
        {
252
            // both phases are present, phase composition results from
253
254
            // the first <-> second phase equilibrium. This is the job
            // of the "MiscibleMultiPhaseComposition" constraint solver
255
256

            // set the known mole fractions in the fluidState so that they
257
            // can be used by the MiscibleMultiPhaseComposition constraint solver
258

259
260
            const int knownPhaseIdx = setFirstPhaseMoleFractions ? phase0Idx : phase1Idx;
            for (int compIdx = numMajorComponents; compIdx < ModelTraits::numComponents(); ++compIdx)
261
                fluidState.setMoleFraction(knownPhaseIdx, compIdx, priVars[compIdx]);
262

263
            MiscibleMultiPhaseComposition::solve(fluidState,
264
265
266
267
                                                 paramCache,
                                                 /*setViscosity=*/true,
                                                 /*setEnthalpy=*/false,
                                                 knownPhaseIdx);
268
        }
269
        else if (phasePresence == secondPhaseOnly)
270
        {
271

272
            Dune::FieldVector<Scalar, ModelTraits::numComponents()> moleFrac;
273

274
275
            moleFrac[comp0Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp0Idx];
276

277
278
            for (int compIdx = numMajorComponents; compIdx < ModelTraits::numComponents(); ++compIdx)
            {
279
                moleFrac[compIdx] = priVars[compIdx];
Martin Schneider's avatar
Martin Schneider committed
280
                sumMoleFracOtherComponents += moleFrac[compIdx];
281
            }
282

283
            moleFrac[comp1Idx] = 1 - sumMoleFracOtherComponents;
284
285

            // Set fluid state mole fractions
286
287
            for (int compIdx = 0; compIdx < ModelTraits::numComponents(); ++compIdx)
                fluidState.setMoleFraction(phase1Idx, compIdx, moleFrac[compIdx]);
288
289
290

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
291
292
293
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
294
                                             phase1Idx,
295
296
                                             /*setViscosity=*/true,
                                             /*setEnthalpy=*/false);
297
        }
298
        else if (phasePresence == firstPhaseOnly)
Martin Schneider's avatar
Martin Schneider committed
299
        {
300
301
302
            // only the first phase is present, i.e. first phase composition
            // is stored explicitly. extract _mass_ fractions in the second phase
            Dune::FieldVector<Scalar, ModelTraits::numComponents()> moleFrac;
303

304
305
306
307
            moleFrac[comp1Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp1Idx];
            for (int compIdx = numMajorComponents; compIdx < ModelTraits::numComponents(); ++compIdx)
            {
308
                moleFrac[compIdx] = priVars[compIdx];
309

310
                sumMoleFracOtherComponents += moleFrac[compIdx];
311
            }
312

313
            moleFrac[comp0Idx] = 1 - sumMoleFracOtherComponents;
314
315

            // convert mass to mole fractions and set the fluid state
316
317
            for (int compIdx = 0; compIdx < ModelTraits::numComponents(); ++compIdx)
                fluidState.setMoleFraction(phase0Idx, compIdx, moleFrac[compIdx]);
318
319
320

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
321
322
323
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
324
                                             phase0Idx,
325
326
                                             /*setViscosity=*/true,
                                             /*setEnthalpy=*/false);
327
328
        }
        paramCache.updateAll(fluidState);
329
        for (int phaseIdx = 0; phaseIdx < ModelTraits::numPhases(); ++phaseIdx)
330
331
332
        {
            Scalar rho = FluidSystem::density(fluidState, paramCache, phaseIdx);
            Scalar mu = FluidSystem::viscosity(fluidState, paramCache, phaseIdx);
333
            Scalar h = EnergyVolVars::enthalpy(fluidState, paramCache, phaseIdx);
334
335
336

            fluidState.setDensity(phaseIdx, rho);
            fluidState.setViscosity(phaseIdx, mu);
Timo Koch's avatar
Timo Koch committed
337
            fluidState.setEnthalpy(phaseIdx, h);
338
339
340
341
342
343
344
345
346
        }
    }

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

347
348
349
350
351
352
    /*!
     * \brief Returns the phase state for the control-volume.
     */
    const SolidState &solidState() const
    { return solidState_; }

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
    /*!
     * \brief Returns the saturation of a given phase within
     *        the control volume in \f$[-]\f$.
     *
     * \param phaseIdx The phase index
     */
    Scalar saturation(int phaseIdx) const
    { return fluidState_.saturation(phaseIdx); }

    /*!
     * \brief Returns the mass density of a given phase within the
     *        control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar density(int phaseIdx) const
369
    { return fluidState_.density(phaseIdx); }
Timo Koch's avatar
Timo Koch committed
370
371
372
373
374
375
376
377

    /*!
     * \brief Returns the kinematic viscosity of a given phase within the
     *        control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar viscosity(int phaseIdx) const
378
    { return fluidState_.viscosity(phaseIdx); }
379
380
381
382
383
384
385
386

    /*!
     * \brief Returns the mass density of a given phase within the
     *        control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar molarDensity(int phaseIdx) const
387
    { return fluidState_.molarDensity(phaseIdx); }
388
389
390
391
392
393
394
395

    /*!
     * \brief Returns the effective pressure of a given phase within
     *        the control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar pressure(int phaseIdx) const
396
    { return fluidState_.pressure(phaseIdx); }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

    /*!
     * \brief Returns temperature inside the sub-control volume.
     *
     * 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 effective mobility of a given phase within
     *        the control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar mobility(int phaseIdx) const
415
    { return mobility_[phaseIdx]; }
416
417
418
419
420
421

    /*!
     * \brief Returns the effective capillary pressure within the control volume
     *        in \f$[kg/(m*s^2)=N/m^2=Pa]\f$.
     */
    Scalar capillaryPressure() const
422
    { return pc_; }
423
424
425
426
427

    /*!
     * \brief Returns the average porosity within the control volume.
     */
    Scalar porosity() const
428
    { return solidState_.porosity();  }
429

430
431
432
    /*!
     * \brief Returns the permeability within the control volume.
     */
433
    const PermeabilityType& permeability() const
434
435
    { return permeability_; }

436
437

    /*!
Martin Schneider's avatar
Martin Schneider committed
438
     * \brief Returns the diffusion coefficient
439
     */
440
441
442
443
444
445
446
    Scalar diffusionCoefficient(int phaseIdx, int compIdx) const
    {
        if (compIdx < phaseIdx)
            return diffCoefficient_[phaseIdx][compIdx];
        else if (compIdx > phaseIdx)
            return diffCoefficient_[phaseIdx][compIdx-1];
        else
Thomas Fetzer's avatar
Thomas Fetzer committed
447
            DUNE_THROW(Dune::InvalidStateException, "Diffusion coefficient called for phaseIdx = compIdx");
448
    }
449
450
451
452
453
454
455
456

    /*!
     * \brief Returns the molarity of a component in the phase
     *
     * \param phaseIdx the index of the fluid phase
     * \param compIdx the index of the component
     */
     Scalar molarity(int phaseIdx, int compIdx) const // [moles/m^3]
457
    { return fluidState_.molarity(phaseIdx, compIdx);}
458
459
460
461
462
463
464
465

     /*!
      * \brief Returns the mass fraction of a component in the phase
      *
      * \param phaseIdx the index of the fluid phase
      * \param compIdx the index of the component
      */
     Scalar massFraction(int phaseIdx, int compIdx) const
466
     { return fluidState_.massFraction(phaseIdx, compIdx); }
467
468
469
470
471
472
473
474

     /*!
      * \brief Returns the mole fraction of a component in the phase
      *
      * \param phaseIdx the index of the fluid phase
      * \param compIdx the index of the component
      */
     Scalar moleFraction(int phaseIdx, int compIdx) const
475
     { return fluidState_.moleFraction(phaseIdx, compIdx); }
476
477
478

protected:
    FluidState fluidState_;
479
    SolidState solidState_;
480
481

private:
482
483
484
485
486
487
488
    void setDiffusionCoefficient_(int phaseIdx, int compIdx, Scalar d)
    {
        if (compIdx < phaseIdx)
            diffCoefficient_[phaseIdx][compIdx] = std::move(d);
        else if (compIdx > phaseIdx)
            diffCoefficient_[phaseIdx][compIdx-1] = std::move(d);
        else
Thomas Fetzer's avatar
Thomas Fetzer committed
489
            DUNE_THROW(Dune::InvalidStateException, "Diffusion coefficient for phaseIdx = compIdx doesn't exist");
490
491
    }

492
    Scalar pc_;                     //!< The capillary pressure
493
494
    Scalar porosity_;               //!< Effective porosity within the control volume
    PermeabilityType permeability_; //!> Effective permeability within the control volume
495
496
    Scalar mobility_[ModelTraits::numPhases()]; //!< Effective mobility within the control volume
    std::array<std::array<Scalar, ModelTraits::numComponents()-1>, ModelTraits::numPhases()> diffCoefficient_;
497

498
499
};

500
} // end namespace Dumux
501
502

#endif