ncpprimaryvariables.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef EWOMS_NCP_PRIMARY_VARIABLES_HH
29#define EWOMS_NCP_PRIMARY_VARIABLES_HH
30
31#include <dune/common/fvector.hh>
32
33#include <opm/material/constraintsolvers/NcpFlash.hpp>
34#include <opm/material/densead/Math.hpp>
35#include <opm/material/fluidstates/CompositionalFluidState.hpp>
36
40
41#include <cassert>
42
43namespace Opm {
44
54template <class TypeTag>
56{
57 using ParentType = FvBasePrimaryVariables<TypeTag>;
58
64
66 enum { pressure0Idx = Indices::pressure0Idx };
67 enum { saturation0Idx = Indices::saturation0Idx };
68 enum { fugacity0Idx = Indices::fugacity0Idx };
69
70 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
71 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
72 using ComponentVector = Dune::FieldVector<Scalar, numComponents>;
73
74 enum { enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>() };
76
77 using NcpFlash = ::Opm::NcpFlash<Scalar, FluidSystem>;
78 using Toolbox = MathToolbox<Evaluation>;
79
80public:
82
89
90 using ParentType::operator=;
91
95 template <class FluidState>
96 void assignMassConservative(const FluidState& fluidState,
97 const MaterialLawParams& matParams,
98 bool isInEquilibrium = false)
99 {
100 using FsToolbox = MathToolbox<typename FluidState::Scalar>;
101
102#ifndef NDEBUG
103 // make sure the temperature is the same in all fluid phases
104 for (unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) {
105 assert(fluidState.temperature(0) == fluidState.temperature(phaseIdx));
106 }
107#endif // NDEBUG
108
109 // for the equilibrium case, we don't need complicated
110 // computations.
111 if (isInEquilibrium) {
112 assignNaive(fluidState);
113 return;
114 }
115
116 // use a flash calculation to calculate a fluid state in
117 // thermodynamic equilibrium
118 typename FluidSystem::template ParameterCache<Scalar> paramCache;
119 CompositionalFluidState<Scalar, FluidSystem> fsFlash;
120
121 // use the externally given fluid state as initial value for
122 // the flash calculation
123 fsFlash.assign(fluidState);
124
125 // calculate the phase densities
126 paramCache.updateAll(fsFlash);
127 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
128 const Scalar rho = FluidSystem::density(fsFlash, paramCache, phaseIdx);
129 fsFlash.setDensity(phaseIdx, rho);
130 }
131
132 // calculate the "global molarities"
133 ComponentVector globalMolarities(0.0);
134 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
135 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
136 globalMolarities[compIdx] +=
137 FsToolbox::value(fsFlash.saturation(phaseIdx)) *
138 FsToolbox::value(fsFlash.molarity(phaseIdx, compIdx));
139 }
140 }
141
142 // run the flash calculation
143 NcpFlash::template solve<MaterialLaw>(fsFlash, matParams, paramCache, globalMolarities);
144
145 // use the result to assign the primary variables
146 assignNaive(fsFlash);
147 }
148
152 template <class FluidState>
153 void assignNaive(const FluidState& fluidState, unsigned refPhaseIdx = 0)
154 {
155 using FsToolbox = MathToolbox<typename FluidState::Scalar>;
156
157 // assign the phase temperatures. this is out-sourced to
158 // the energy module
159 EnergyModule::setPriVarTemperatures(*this, fluidState);
160
161 // assign fugacities.
162 typename FluidSystem::template ParameterCache<Scalar> paramCache;
163 paramCache.updatePhase(fluidState, refPhaseIdx);
164 const Scalar pRef = FsToolbox::value(fluidState.pressure(refPhaseIdx));
165 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
166 // we always compute the fugacities because they are quite exotic quantities
167 // and this easily forgotten to be specified
168 const Scalar fugCoeff =
169 FluidSystem::template fugacityCoefficient<FluidState, Scalar>(fluidState,
170 paramCache,
171 refPhaseIdx,
172 compIdx);
173 (*this)[fugacity0Idx + compIdx] =
174 fugCoeff * fluidState.moleFraction(refPhaseIdx, compIdx) * pRef;
175 }
176
177 // assign pressure of first phase
178 (*this)[pressure0Idx] = FsToolbox::value(fluidState.pressure(/*phaseIdx=*/0));
179
180 // assign first M - 1 saturations
181 for (unsigned phaseIdx = 0; phaseIdx < numPhases - 1; ++phaseIdx) {
182 (*this)[saturation0Idx + phaseIdx] = FsToolbox::value(fluidState.saturation(phaseIdx));
183 }
184 }
185};
186
187} // namespace Opm
188
189#endif
Provides the auxiliary methods required for consideration of the energy equation.
Definition: energymodule.hh:54
Represents the primary variables used by the a model.
Definition: fvbaseprimaryvariables.hh:53
Represents the primary variables used by the compositional multi-phase NCP model.
Definition: ncpprimaryvariables.hh:56
void assignNaive(const FluidState &fluidState, unsigned refPhaseIdx=0)
Directly retrieve the primary variables from an arbitrary fluid state.
Definition: ncpprimaryvariables.hh:153
NcpPrimaryVariables & operator=(const NcpPrimaryVariables &value)=default
void assignMassConservative(const FluidState &fluidState, const MaterialLawParams &matParams, bool isInEquilibrium=false)
< Import base class assignment operators.
Definition: ncpprimaryvariables.hh:96
NcpPrimaryVariables(const NcpPrimaryVariables &value)=default
Contains the classes required to consider energy as a conservation quantity in a multi-phase module.
Definition: blackoilboundaryratevector.hh:39
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
Declares the properties required for the NCP compositional multi-phase model.