blackoilintensivequantities.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  Copyright (C) 2010-2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
26 #ifndef EWOMS_BLACK_OIL_INTENSIVE_QUANTITIES_HH
27 #define EWOMS_BLACK_OIL_INTENSIVE_QUANTITIES_HH
28 
29 #include "blackoilproperties.hh"
30 
31 #include <opm/material/fluidstates/CompositionalFluidState.hpp>
32 #include <dune/common/fmatrix.hh>
33 
34 namespace Ewoms {
42 template <class TypeTag>
44  : public GET_PROP_TYPE(TypeTag, DiscIntensiveQuantities)
45  , public GET_PROP_TYPE(TypeTag, FluxModule)::FluxIntensiveQuantities
46 {
47  typedef typename GET_PROP_TYPE(TypeTag, DiscIntensiveQuantities) ParentType;
48 
49  typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
50  typedef typename GET_PROP_TYPE(TypeTag, Evaluation) Evaluation;
51  typedef typename GET_PROP_TYPE(TypeTag, FluidSystem) FluidSystem;
52  typedef typename GET_PROP_TYPE(TypeTag, MaterialLaw) MaterialLaw;
53  typedef typename GET_PROP_TYPE(TypeTag, ElementContext) ElementContext;
54  typedef typename GET_PROP_TYPE(TypeTag, PrimaryVariables) PrimaryVariables;
55  typedef typename GET_PROP_TYPE(TypeTag, BlackOilFluidState) FluidState;
56  typedef typename GET_PROP_TYPE(TypeTag, Indices) Indices;
57  typedef typename GET_PROP_TYPE(TypeTag, GridView) GridView;
58  typedef typename GET_PROP_TYPE(TypeTag, FluxModule) FluxModule;
59 
60  enum { numPhases = GET_PROP_VALUE(TypeTag, NumPhases) };
61  enum { numComponents = GET_PROP_VALUE(TypeTag, NumComponents) };
62  enum { waterCompIdx = FluidSystem::waterCompIdx };
63  enum { oilCompIdx = FluidSystem::oilCompIdx };
64  enum { gasCompIdx = FluidSystem::gasCompIdx };
65  enum { waterPhaseIdx = FluidSystem::waterPhaseIdx };
66  enum { oilPhaseIdx = FluidSystem::oilPhaseIdx };
67  enum { gasPhaseIdx = FluidSystem::gasPhaseIdx };
68  enum { dimWorld = GridView::dimensionworld };
69 
70  typedef Opm::MathToolbox<Evaluation> Toolbox;
71  typedef Dune::FieldMatrix<Scalar, dimWorld, dimWorld> DimMatrix;
72  typedef typename FluxModule::FluxIntensiveQuantities FluxIntensiveQuantities;
73 
74 public:
78  void update(const ElementContext &elemCtx, int dofIdx, int timeIdx)
79  {
80  ParentType::update(elemCtx, dofIdx, timeIdx);
81 
82  fluidState_.setTemperature(elemCtx.problem().temperature(elemCtx, dofIdx, timeIdx));
83 
84  const auto& problem = elemCtx.problem();
85  const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
86 
87  int pvtRegionIdx = priVars.pvtRegionIndex();
88 
89  // extract the water and the gas saturations for convenience
90  Evaluation Sw = priVars.makeEvaluation(Indices::waterSaturationIdx, timeIdx);
91 
92  Evaluation Sg;
93  if (priVars.switchingVarMeaning() == PrimaryVariables::GasSaturation)
94  Sg = priVars.makeEvaluation(Indices::switchIdx, timeIdx);
95  else if (priVars.switchingVarMeaning() == PrimaryVariables::OilMoleFractionInGas)
96  Sg = 1 - Sw;
97  else {
98  assert(priVars.switchingVarMeaning() == PrimaryVariables::GasMoleFractionInOil);
99  Sg = 0.0;
100  }
101 
102  fluidState_.setSaturation(waterPhaseIdx, Sw);
103  fluidState_.setSaturation(gasPhaseIdx, Sg);
104  fluidState_.setSaturation(oilPhaseIdx, 1 - Sw - Sg);
105 
106  // reference phase (-> gas) pressure
107  Evaluation pg = priVars.makeEvaluation(Indices::gasPressureIdx, timeIdx);
108 
109  // now we compute all phase pressures
110  typedef typename GET_PROP_TYPE(TypeTag, MaterialLaw) MaterialLaw;
111  Evaluation pC[numPhases];
112  const auto &materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
113  MaterialLaw::capillaryPressures(pC, materialParams, fluidState_);
114  for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
115  fluidState_.setPressure(phaseIdx, pg + (pC[phaseIdx] - pC[gasPhaseIdx]));
116  if (fluidState_.pressure(phaseIdx) < 1e5) {
117  OPM_THROW(Opm::NumericalProblem,
118  "All pressures must be at least 1 bar.");
119  }
120  }
121 
122  // oil phase temperature and pressure
123  const auto& T = fluidState_.temperature(oilPhaseIdx);
124 
125  // update phase compositions. first, set everything to 0...
126  for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx)
127  for (int compIdx = 0; compIdx < numComponents; ++compIdx)
128  fluidState_.setMoleFraction(phaseIdx, compIdx, 0.0);
129 
130  // ... then set the default composition of all phases (default = assume immscibility)
131  fluidState_.setMoleFraction(oilPhaseIdx, oilCompIdx, 1.0);
132  fluidState_.setMoleFraction(gasPhaseIdx, gasCompIdx, 1.0);
133  fluidState_.setMoleFraction(waterPhaseIdx, waterCompIdx, 1.0);
134 
135  // take the meaning of the switiching primary variable into account for the gas
136  // and oil phase compositions
137  if (priVars.switchingVarMeaning() == PrimaryVariables::GasSaturation) {
138  // if the gas and oil phases are present, we use the compositions of the
139  // gas-saturated oil and oil-saturated gas.
140  Evaluation xoG = 0.0;
141  if (FluidSystem::enableDissolvedGas())
142  xoG = FluidSystem::saturatedOilGasMoleFraction(T, pg, pvtRegionIdx);
143  fluidState_.setMoleFraction(oilPhaseIdx, gasCompIdx, xoG);
144  fluidState_.setMoleFraction(oilPhaseIdx, oilCompIdx, 1 - xoG);
145 
146  Evaluation xgO = 0.0;
147  if (FluidSystem::enableVaporizedOil())
148  xgO = FluidSystem::saturatedGasOilMoleFraction(T, pg, pvtRegionIdx);
149  fluidState_.setMoleFraction(gasPhaseIdx, gasCompIdx, 1 - xgO);
150  fluidState_.setMoleFraction(gasPhaseIdx, oilCompIdx, xgO);
151  }
152  else if (priVars.switchingVarMeaning() == PrimaryVariables::GasMoleFractionInOil) {
153  // if the switching variable is the mole fraction of the gas component in the
154  // oil phase, we can directly set the composition of the oil phase
155  const auto& xoG = priVars.makeEvaluation(Indices::switchIdx, timeIdx);
156 
157  fluidState_.setMoleFraction(oilPhaseIdx, gasCompIdx, xoG);
158  fluidState_.setMoleFraction(oilPhaseIdx, oilCompIdx, 1 - xoG);
159 
160  Evaluation xgO = 0.0;
161  if (FluidSystem::enableVaporizedOil())
162  xgO = FluidSystem::saturatedGasOilMoleFraction(T, pg, pvtRegionIdx);
163  fluidState_.setMoleFraction(gasPhaseIdx, gasCompIdx, 1 - xgO);
164  fluidState_.setMoleFraction(gasPhaseIdx, oilCompIdx, xgO);
165  }
166  else {
167  assert(priVars.switchingVarMeaning() == PrimaryVariables::OilMoleFractionInGas);
168 
169  // if the switching variable is the mole fraction of the oil component in the
170  // gas phase, we can directly set the composition of the gas phase
171  const auto& xgO = priVars.makeEvaluation(Indices::switchIdx, timeIdx);
172 
173  fluidState_.setMoleFraction(gasPhaseIdx, gasCompIdx, 1 - xgO);
174  fluidState_.setMoleFraction(gasPhaseIdx, oilCompIdx, xgO);
175 
176  Evaluation xoG = 0.0;
177  if (FluidSystem::enableDissolvedGas())
178  xoG = FluidSystem::saturatedOilGasMoleFraction(T, pg, pvtRegionIdx);
179  fluidState_.setMoleFraction(oilPhaseIdx, gasCompIdx, xoG);
180  fluidState_.setMoleFraction(oilPhaseIdx, oilCompIdx, 1 - xoG);
181  }
182 
183  // calculate relative permeabilities
184  MaterialLaw::relativePermeabilities(relativePermeability_, materialParams, fluidState_);
185  Valgrind::CheckDefined(relativePermeability_);
186 
187  typedef typename GET_PROP_TYPE(TypeTag, FluidSystem) FluidSystem;
188  typename FluidSystem::ParameterCache paramCache;
189  paramCache.setRegionIndex(pvtRegionIdx);
190  paramCache.updateAll(fluidState_);
191 
192  // set the phase densities and viscosities
193  for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
194  const auto& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
195  fluidState_.setDensity(phaseIdx, rho);
196 
197  const auto& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
198  fluidState_.setViscosity(phaseIdx, mu);
199 
200  mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
201  }
202 
203  // retrieve the porosity from the problem
204  porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
205 
206  // the porosity must be modified by the compressibility of the
207  // rock...
208  Scalar rockCompressibility = problem.rockCompressibility(elemCtx, dofIdx, timeIdx);
209  if (rockCompressibility > 0.0) {
210  Scalar rockRefPressure = problem.rockReferencePressure(elemCtx, dofIdx, timeIdx);
211  Evaluation x = rockCompressibility*(pg - rockRefPressure);
212  porosity_ *= 1.0 + x + 0.5*x*x;
213  }
214 
215  // now get the intrinsic permeability
216  intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
217 
218  // update the quantities which are required by the chosen
219  // velocity model
220  FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
221 
222 #ifndef NDEBUG
223  // some safety checks in debug mode
224  for (int phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) {
225  assert(std::isfinite(Toolbox::value(fluidState_.density(phaseIdx))));
226  assert(std::isfinite(Toolbox::value(fluidState_.saturation(phaseIdx))));
227  assert(std::isfinite(Toolbox::value(fluidState_.temperature(phaseIdx))));
228  assert(std::isfinite(Toolbox::value(fluidState_.pressure(phaseIdx))));
229  assert(std::isfinite(Toolbox::value(fluidState_.viscosity(phaseIdx))));
230  assert(std::isfinite(Toolbox::value(relativePermeability_[phaseIdx])));
231  for (int compIdx = 0; compIdx < numComponents; ++ compIdx)
232  assert(std::isfinite(Toolbox::value(fluidState_.moleFraction(phaseIdx, compIdx))));
233  }
234  assert(std::isfinite(intrinsicPerm_.frobenius_norm()));
235  assert(std::isfinite(Toolbox::value(porosity_)));
236 #endif
237  }
238 
242  const FluidState &fluidState() const
243  { return fluidState_; }
244 
248  const DimMatrix &intrinsicPermeability() const
249  { return intrinsicPerm_; }
250 
254  const Evaluation& relativePermeability(int phaseIdx) const
255  { return relativePermeability_[phaseIdx]; }
256 
260  const Evaluation& mobility(int phaseIdx) const
261  { return mobility_[phaseIdx]; }
262 
266  const Evaluation& porosity() const
267  { return porosity_; }
268 
269 private:
270  FluidState fluidState_;
271  Evaluation porosity_;
272  DimMatrix intrinsicPerm_;
273  Evaluation relativePermeability_[numPhases];
274  Evaluation mobility_[numPhases];
275 };
276 
277 } // namespace Ewoms
278 
279 #endif
const DimMatrix & intrinsicPermeability() const
Returns the intrinsic permeability tensor a degree of freedom.
Definition: blackoilintensivequantities.hh:248
#define GET_PROP_VALUE(TypeTag, PropTagName)
Access the value attribute of a property for a type tag.
Definition: propertysystem.hh:468
const Evaluation & porosity() const
Returns the average porosity within the control volume.
Definition: blackoilintensivequantities.hh:266
#define GET_PROP_TYPE(TypeTag, PropTagName)
Access the type attribute of a property for a type tag.
Definition: propertysystem.hh:485
void update(const ElementContext &elemCtx, int dofIdx, int timeIdx)
Definition: blackoilintensivequantities.hh:78
Declares the properties required by the black oil model.
const FluidState & fluidState() const
Returns the phase state for the control-volume.
Definition: blackoilintensivequantities.hh:242
Contains the quantities which are are constant within a finite volume in the black-oil model...
Definition: blackoilintensivequantities.hh:43
Definition: baseauxiliarymodule.hh:35
const Evaluation & relativePermeability(int phaseIdx) const
Returns the relative permeability of a given phase within the control volume.
Definition: blackoilintensivequantities.hh:254
const Evaluation & mobility(int phaseIdx) const
Returns the effective mobility of a given phase within the control volume.
Definition: blackoilintensivequantities.hh:260