volumevariables.hh 20.1 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 = elemSol[scv.localDofIndex()];
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
                                                 paramCache,
                                                 knownPhaseIdx);
266
        }
267
        else if (phasePresence == secondPhaseOnly)
268
        {
269

270
            Dune::FieldVector<Scalar, ModelTraits::numComponents()> moleFrac;
271

272
273
            moleFrac[comp0Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp0Idx];
274

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

281
            moleFrac[comp1Idx] = 1 - sumMoleFracOtherComponents;
282
283

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

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
289
290
291
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
292
                                             phase1Idx);
293
        }
294
        else if (phasePresence == firstPhaseOnly)
Martin Schneider's avatar
Martin Schneider committed
295
        {
296
297
298
            // 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;
299

300
301
302
303
            moleFrac[comp1Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp1Idx];
            for (int compIdx = numMajorComponents; compIdx < ModelTraits::numComponents(); ++compIdx)
            {
304
                moleFrac[compIdx] = priVars[compIdx];
305

306
                sumMoleFracOtherComponents += moleFrac[compIdx];
307
            }
308

309
            moleFrac[comp0Idx] = 1 - sumMoleFracOtherComponents;
310
311

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

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
317
318
319
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
320
                                             phase0Idx);
321
322
        }
        paramCache.updateAll(fluidState);
323
        for (int phaseIdx = 0; phaseIdx < ModelTraits::numPhases(); ++phaseIdx)
324
325
        {
            Scalar rho = FluidSystem::density(fluidState, paramCache, phaseIdx);
326
            Scalar rhoMolar = FluidSystem::molarDensity(fluidState, paramCache, phaseIdx);
327
            Scalar mu = FluidSystem::viscosity(fluidState, paramCache, phaseIdx);
328
            Scalar h = EnergyVolVars::enthalpy(fluidState, paramCache, phaseIdx);
329
330

            fluidState.setDensity(phaseIdx, rho);
331
            fluidState.setMolarDensity(phaseIdx, rhoMolar);
332
            fluidState.setViscosity(phaseIdx, mu);
Timo Koch's avatar
Timo Koch committed
333
            fluidState.setEnthalpy(phaseIdx, h);
334
335
336
337
338
339
340
341
342
        }
    }

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

343
344
345
346
347
348
    /*!
     * \brief Returns the phase state for the control-volume.
     */
    const SolidState &solidState() const
    { return solidState_; }

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    /*!
     * \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
365
    { return fluidState_.density(phaseIdx); }
Timo Koch's avatar
Timo Koch committed
366
367
368
369
370
371
372
373

    /*!
     * \brief Returns the kinematic viscosity of a given phase within the
     *        control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar viscosity(int phaseIdx) const
374
    { return fluidState_.viscosity(phaseIdx); }
375
376

    /*!
377
     * \brief Returns the molar density of a given phase within the
378
379
380
381
382
     *        control volume.
     *
     * \param phaseIdx The phase index
     */
    Scalar molarDensity(int phaseIdx) const
383
    {
384
385
386
387
388
        if (phaseIdx < ModelTraits::numPhases())
            return fluidState_.molarDensity(phaseIdx);

        else
            DUNE_THROW(Dune::InvalidStateException, "Invalid phase index " << phaseIdx);
389
    }
390
391
392
393
394
395
396
397

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

    /*!
     * \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
417
    { return mobility_[phaseIdx]; }
418
419
420
421
422
423

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

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

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

438
439

    /*!
Martin Schneider's avatar
Martin Schneider committed
440
     * \brief Returns the diffusion coefficient
441
     */
442
443
444
445
446
447
448
    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
449
            DUNE_THROW(Dune::InvalidStateException, "Diffusion coefficient called for phaseIdx = compIdx");
450
    }
451
452
453
454
455
456
457
458

     /*!
      * \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
459
     { return fluidState_.massFraction(phaseIdx, compIdx); }
460
461
462
463
464
465
466
467

     /*!
      * \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
468
     { return fluidState_.moleFraction(phaseIdx, compIdx); }
469
470
471

protected:
    FluidState fluidState_;
472
    SolidState solidState_;
473
474

private:
475
476
477
478
479
480
481
    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
482
            DUNE_THROW(Dune::InvalidStateException, "Diffusion coefficient for phaseIdx = compIdx doesn't exist");
483
484
    }

485
    Scalar pc_;                     //!< The capillary pressure
486
487
    Scalar porosity_;               //!< Effective porosity within the control volume
    PermeabilityType permeability_; //!> Effective permeability within the control volume
488
489
    Scalar mobility_[ModelTraits::numPhases()]; //!< Effective mobility within the control volume
    std::array<std::array<Scalar, ModelTraits::numComponents()-1>, ModelTraits::numPhases()> diffCoefficient_;
490

491
492
};

493
} // end namespace Dumux
494
495

#endif