volumevariables.hh 19.7 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 TwoPNCModel
22
23
24
 * \brief Contains the quantities which are constant within a
 *        finite volume in the two-phase, n-component model.
 */
25

26
27
28
29
30
31
32
#ifndef DUMUX_2PNC_VOLUME_VARIABLES_HH
#define DUMUX_2PNC_VOLUME_VARIABLES_HH

#include <iostream>
#include <vector>

#include <dumux/common/math.hh>
33
#include <dumux/discretization/method.hh>
34

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

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

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

45
46
#include "primaryvariableswitch.hh"

47
namespace Dumux {
48
49
50
51
52
53

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

        // phase indices
71
72
        phase0Idx = FS::phase0Idx,
        phase1Idx = FS::phase1Idx,
73
74

        // component indices
75
76
        comp0Idx = FS::comp0Idx,
        comp1Idx = FS::comp1Idx,
77
78

        // phase presence enums
79
80
81
        secondPhaseOnly = ModelTraits::Indices::secondPhaseOnly,
        firstPhaseOnly = ModelTraits::Indices::firstPhaseOnly,
        bothPhases = ModelTraits::Indices::bothPhases,
82
83

        // primary variable indices
84
85
        pressureIdx = ModelTraits::Indices::pressureIdx,
        switchIdx = ModelTraits::Indices::switchIdx
86
87
    };

88
89
    static constexpr auto formulation = ModelTraits::priVarFormulation();
    static constexpr bool setFirstPhaseMoleFractions = ModelTraits::setMoleFractionsForFirstPhase();
90
91
92

    using MiscibleMultiPhaseComposition = Dumux::MiscibleMultiPhaseComposition<Scalar, FS>;
    using ComputeFromReferencePhase = Dumux::ComputeFromReferencePhase<Scalar, FS>;
93
    using EffDiffModel = typename Traits::EffectiveDiffusivityModel;
94
    using DiffusionCoefficients = typename Traits::DiffusionType::DiffusionCoefficientsContainer;
95

96
public:
97
    //! Export fluid state type
98
    using FluidState = typename Traits::FluidState;
99
    //! Export fluid system type
100
    using FluidSystem = typename Traits::FluidSystem;
101
    //! Export type of solid state
102
    using SolidState = typename Traits::SolidState;
103
    //! Export type of solid system
104
    using SolidSystem = typename Traits::SolidSystem;
105
    //! Export the primary variable switch
106
    using PrimaryVariableSwitch = TwoPNCPrimaryVariableSwitch;
107

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

113
    // check for permissive specifications
114
    static_assert(useMoles(), "use moles has to be set true in the 2pnc model");
115
    static_assert(ModelTraits::numFluidPhases() == 2, "NumPhases 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
     *
     * \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
    */
127
128
    template<class ElemSol, class Problem, class Element, class Scv>
    void update(const ElemSol &elemSol,
129
130
                const Problem &problem,
                const Element &element,
131
                const Scv& scv)
132
    {
133
        ParentType::update(elemSol, problem, element, scv);
134
135

        completeFluidState(elemSol, problem, element, scv, fluidState_, solidState_);
136
137
138
139

        /////////////
        // calculate the remaining quantities
        /////////////
140

141
142
        using MaterialLaw = typename Problem::SpatialParams::MaterialLaw;
        const auto& matParams = problem.spatialParams().materialLawParams(element, scv, elemSol);
143
144
        const int wPhaseIdx = fluidState_.wettingPhase();
        const int nPhaseIdx = 1 - wPhaseIdx;
145
146

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

        //update porosity before calculating the effective properties depending on it
        updateSolidVolumeFractions(elemSol, problem, element, scv, solidState_, numFluidComps);
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
161
        // calculate the remaining quantities
        EnergyVolVars::updateSolidEnergyParams(elemSol, problem, element, scv, solidState_);
162
        permeability_ = problem.spatialParams().permeability(element, scv, elemSol);
163
        EnergyVolVars::updateEffectiveThermalConductivity();
164
165
    }

166
    /*!
167
     * \brief Sets complete fluid state.
168
169
     *
     * \param elemSol A vector containing all primary variables connected to the element
170
171
172
173
174
     * \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
175
176
     *
     * Set temperature, saturations, capillary pressures, viscosities, densities and enthalpies.
177
     */
178
    template<class ElemSol, class Problem, class Element, class Scv>
179
180
181
182
    void completeFluidState(const ElemSol& elemSol,
                            const Problem& problem,
                            const Element& element,
                            const Scv& scv,
183
184
                            FluidState& fluidState,
                            SolidState& solidState)
185
    {
186
        EnergyVolVars::updateTemperature(elemSol, problem, element, scv, fluidState, solidState);
187

188
        const auto& priVars = elemSol[scv.localDofIndex()];
189
        const auto phasePresence = priVars.state();
190

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

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

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

        // calculate the phase compositions
239
        typename FluidSystem::ParameterCache paramCache;
240
241

        // now comes the tricky part: calculate phase composition
242
243
        if (phasePresence == bothPhases)
        {
244
            // both phases are present, phase composition results from
245
246
            // the first <-> second phase equilibrium. This is the job
            // of the "MiscibleMultiPhaseComposition" constraint solver
247
248

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

251
            const int knownPhaseIdx = setFirstPhaseMoleFractions ? phase0Idx : phase1Idx;
252
            for (int compIdx = numMajorComponents; compIdx <  ModelTraits::numFluidComponents(); ++compIdx)
253
                fluidState.setMoleFraction(knownPhaseIdx, compIdx, priVars[compIdx]);
254

255
            MiscibleMultiPhaseComposition::solve(fluidState,
256
257
                                                 paramCache,
                                                 knownPhaseIdx);
258
        }
259
        else if (phasePresence == secondPhaseOnly)
260
        {
261

262
            Dune::FieldVector<Scalar,  ModelTraits::numFluidComponents()> moleFrac;
263

264
265
            moleFrac[comp0Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp0Idx];
266

267
            for (int compIdx = numMajorComponents; compIdx <  ModelTraits::numFluidComponents(); ++compIdx)
268
            {
269
                moleFrac[compIdx] = priVars[compIdx];
Martin Schneider's avatar
Martin Schneider committed
270
                sumMoleFracOtherComponents += moleFrac[compIdx];
271
            }
272

273
            moleFrac[comp1Idx] = 1 - sumMoleFracOtherComponents;
274
275

            // Set fluid state mole fractions
276
            for (int compIdx = 0; compIdx <  ModelTraits::numFluidComponents(); ++compIdx)
277
                fluidState.setMoleFraction(phase1Idx, compIdx, moleFrac[compIdx]);
278
279
280

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
281
282
283
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
284
                                             phase1Idx);
285
        }
286
        else if (phasePresence == firstPhaseOnly)
Martin Schneider's avatar
Martin Schneider committed
287
        {
288
289
            // only the first phase is present, i.e. first phase composition
            // is stored explicitly. extract _mass_ fractions in the second phase
290
            Dune::FieldVector<Scalar,  ModelTraits::numFluidComponents()> moleFrac;
291

292
293
            moleFrac[comp1Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp1Idx];
294
            for (int compIdx = numMajorComponents; compIdx <  ModelTraits::numFluidComponents(); ++compIdx)
295
            {
296
                moleFrac[compIdx] = priVars[compIdx];
297

298
                sumMoleFracOtherComponents += moleFrac[compIdx];
299
            }
300

301
            moleFrac[comp0Idx] = 1 - sumMoleFracOtherComponents;
302
303

            // convert mass to mole fractions and set the fluid state
304
            for (int compIdx = 0; compIdx <  ModelTraits::numFluidComponents(); ++compIdx)
305
                fluidState.setMoleFraction(phase0Idx, compIdx, moleFrac[compIdx]);
306
307
308

            // calculate the composition of the remaining phases (as
            // well as the densities of all phases). this is the job
309
310
311
            // of the "ComputeFromReferencePhase" constraint solver
            ComputeFromReferencePhase::solve(fluidState,
                                             paramCache,
312
                                             phase0Idx);
313
314
        }
        paramCache.updateAll(fluidState);
315
        for (int phaseIdx = 0; phaseIdx < ModelTraits::numFluidPhases(); ++phaseIdx)
316
317
        {
            Scalar rho = FluidSystem::density(fluidState, paramCache, phaseIdx);
318
            Scalar rhoMolar = FluidSystem::molarDensity(fluidState, paramCache, phaseIdx);
319
            Scalar mu = FluidSystem::viscosity(fluidState, paramCache, phaseIdx);
320
            Scalar h = EnergyVolVars::enthalpy(fluidState, paramCache, phaseIdx);
321
322

            fluidState.setDensity(phaseIdx, rho);
323
            fluidState.setMolarDensity(phaseIdx, rhoMolar);
324
            fluidState.setViscosity(phaseIdx, mu);
Timo Koch's avatar
Timo Koch committed
325
            fluidState.setEnthalpy(phaseIdx, h);
326
327
328
329
330
331
332
333
334
        }
    }

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

335
336
337
338
339
340
    /*!
     * \brief Returns the phase state for the control-volume.
     */
    const SolidState &solidState() const
    { return solidState_; }

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

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
        if (phaseIdx < ModelTraits::numFluidPhases())
385
386
387
388
            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
440
441
    /*!
     * \brief Returns the binary diffusion coefficients for a phase in \f$[m^2/s]\f$.
     */
    Scalar diffusionCoefficient(int phaseIdx, int compIIdx, int compJIdx) const
442
443
444
445
446
    {
        typename FluidSystem::ParameterCache paramCache;
        paramCache.updatePhase(fluidState_, phaseIdx);
        return FluidSystem::binaryDiffusionCoefficient(fluidState_, paramCache, phaseIdx, compIIdx, compJIdx);
    }
447

448
449
450
    /*!
     * \brief Returns the effective diffusion coefficients for a phase in \f$[m^2/s]\f$.
     */
451
452
    Scalar effectiveDiffusionCoefficient(int phaseIdx, int compIIdx, int compJIdx) const
    { return effectiveDiffCoeff_(phaseIdx, compIIdx, compJIdx); }
453

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

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

472
473
474
    /*!
     * \brief Returns the wetting phase index
     */
475
    int wettingPhase() const
476
    {  return fluidState_.wettingPhase(); }
477

478
479
protected:
    FluidState fluidState_;
480
    SolidState solidState_;
481
482

private:
483

484
485
486
487
    Scalar pc_;                     // The capillary pressure
    Scalar porosity_;               // Effective porosity within the control volume
    PermeabilityType permeability_; // Effective permeability within the control volume
    Scalar mobility_[ModelTraits::numFluidPhases()]; // Effective mobility within the control volume
488
    DiffusionCoefficients effectiveDiffCoeff_;
489

490
491
};

492
} // end namespace Dumux
493
494

#endif