volumevariables.hh 19.8 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
102
    //! Export the indices
    using Indices = typename ModelTraits::Indices;
103
    //! Export type of solid state
104
    using SolidState = typename Traits::SolidState;
105
    //! Export type of solid system
106
    using SolidSystem = typename Traits::SolidSystem;
107
    //! Export the primary variable switch
108
    using PrimaryVariableSwitch = TwoPNCPrimaryVariableSwitch;
109

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

115
    // check for permissive specifications
116
    static_assert(useMoles(), "use moles has to be set true in the 2pnc model");
117
    static_assert(ModelTraits::numFluidPhases() == 2, "NumPhases set in the model is not two!");
118
    static_assert((formulation == TwoPFormulation::p0s1 || formulation == TwoPFormulation::p1s0), "Chosen TwoPFormulation not supported!");
119
120

    /*!
121
     * \brief Updates all quantities for a given control volume.
122
123
124
125
126
127
128
     *
     * \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
    */
129
130
    template<class ElemSol, class Problem, class Element, class Scv>
    void update(const ElemSol &elemSol,
131
132
                const Problem &problem,
                const Element &element,
133
                const Scv& scv)
134
    {
135
        ParentType::update(elemSol, problem, element, scv);
136
137

        completeFluidState(elemSol, problem, element, scv, fluidState_, solidState_);
138
139
140
141

        /////////////
        // calculate the remaining quantities
        /////////////
142

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

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

        //update porosity before calculating the effective properties depending on it
        updateSolidVolumeFractions(elemSol, problem, element, scv, solidState_, numFluidComps);
154

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

        effectiveDiffCoeff_.update(getEffectiveDiffusionCoefficient);
161

162
163
        // calculate the remaining quantities
        EnergyVolVars::updateSolidEnergyParams(elemSol, problem, element, scv, solidState_);
164
        permeability_ = problem.spatialParams().permeability(element, scv, elemSol);
165
        EnergyVolVars::updateEffectiveThermalConductivity();
166
167
    }

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

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

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

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

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

        // calculate the phase compositions
241
        typename FluidSystem::ParameterCache paramCache;
242
243

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

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

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

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

264
            Dune::FieldVector<Scalar,  ModelTraits::numFluidComponents()> moleFrac;
265

266
267
            moleFrac[comp0Idx] = priVars[switchIdx];
            Scalar sumMoleFracOtherComponents = moleFrac[comp0Idx];
268

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

275
            moleFrac[comp1Idx] = 1 - sumMoleFracOtherComponents;
276
277

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

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

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

300
                sumMoleFracOtherComponents += moleFrac[compIdx];
301
            }
302

303
            moleFrac[comp0Idx] = 1 - sumMoleFracOtherComponents;
304
305

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

440
441
442
443
    /*!
     * \brief Returns the binary diffusion coefficients for a phase in \f$[m^2/s]\f$.
     */
    Scalar diffusionCoefficient(int phaseIdx, int compIIdx, int compJIdx) const
444
445
446
447
448
    {
        typename FluidSystem::ParameterCache paramCache;
        paramCache.updatePhase(fluidState_, phaseIdx);
        return FluidSystem::binaryDiffusionCoefficient(fluidState_, paramCache, phaseIdx, compIIdx, compJIdx);
    }
449

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

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

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

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

480
481
protected:
    FluidState fluidState_;
482
    SolidState solidState_;
483
484

private:
485

486
487
488
489
    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
490
    DiffusionCoefficients effectiveDiffCoeff_;
491

492
493
};

494
} // end namespace Dumux
495
496

#endif