ptflash/flashintensivequantities.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 OPM_FLASH_INTENSIVE_QUANTITIES_HH
29#define OPM_FLASH_INTENSIVE_QUANTITIES_HH
30
31#include <dune/common/fmatrix.hh>
32#include <dune/common/fvector.hh>
33
34#include <opm/common/OpmLog/OpmLog.hpp>
35
36#include <opm/input/eclipse/EclipseState/Grid/FaceDir.hpp>
37
38#include <opm/material/Constants.hpp>
39#include <opm/material/common/Valgrind.hpp>
40#include <opm/material/constraintsolvers/PTFlashMethod.hpp>
41#include <opm/material/fluidstates/CompositionalFluidState.hpp>
42
45
47
50
51#include <fmt/format.h>
52
53#include <array>
54#include <iterator>
55#include <string>
56
57namespace Opm {
58
65template <class TypeTag>
66class FlashIntensiveQuantities
67 : public GetPropType<TypeTag, Properties::DiscIntensiveQuantities>
68 , public DiffusionIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableDiffusion>() >
69 , public EnergyIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableEnergy>() >
70 , public GetPropType<TypeTag, Properties::FluxModule>::FluxIntensiveQuantities
71{
72 using ParentType = GetPropType<TypeTag, Properties::DiscIntensiveQuantities>;
73
74 using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
75 using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
76 using MaterialLawParams = GetPropType<TypeTag, Properties::MaterialLawParams>;
77 using Indices = GetPropType<TypeTag, Properties::Indices>;
78 using FluxModule = GetPropType<TypeTag, Properties::FluxModule>;
79 using GridView = GetPropType<TypeTag, Properties::GridView>;
80 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
81
82 // primary variable indices
83 enum { z0Idx = Indices::z0Idx };
84 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
85 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
86 static constexpr bool enableDiffusion = getPropValue<TypeTag, Properties::EnableDiffusion>();
87 static constexpr bool enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>();
88 enum { dimWorld = GridView::dimensionworld };
89 enum { pressure0Idx = Indices::pressure0Idx };
90 enum { water0Idx = Indices::water0Idx};
91
92 static constexpr bool waterEnabled = Indices::waterEnabled;
93
94 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
95
96 using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
97 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
98 using FlashSolver = GetPropType<TypeTag, Properties::FlashSolver>;
99
100 using ComponentVector = Dune::FieldVector<Evaluation, numComponents>;
101 using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
102
103 using DiffusionIntensiveQuantities = ::Opm::DiffusionIntensiveQuantities<TypeTag, enableDiffusion>;
104 using EnergyIntensiveQuantities = ::Opm::EnergyIntensiveQuantities<TypeTag, enableEnergy>;
105 using FluxIntensiveQuantities = typename FluxModule::FluxIntensiveQuantities;
106
107public:
109 using FluidState = CompositionalFluidState<Evaluation, FluidSystem, enableEnergy>;
110
113 static constexpr Scalar compositionFloor = 1.0e-8;
114
118 static constexpr Scalar hydrocarbonFloor = compositionFloor;
119
121
123
125
129 void update(const ElementContext& elemCtx, unsigned dofIdx, unsigned timeIdx)
130 {
131 ParentType::update(elemCtx, dofIdx, timeIdx);
132 EnergyIntensiveQuantities::updateTemperatures_(fluidState_, elemCtx, dofIdx, timeIdx);
133
134 const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
135 const auto& problem = elemCtx.problem();
136
137 const Scalar flashTolerance = Parameters::Get<Parameters::FlashTolerance<Scalar>>();
138 const int flashVerbosity = Parameters::Get<Parameters::FlashVerbosity>();
139 const auto ptFlashMethod =
140 ptFlashMethodFromString(Parameters::Get<Parameters::FlashTwoPhaseMethod>());
141 // Extract the overall component mole fractions; the last fraction is dependent.
142 ComponentVector z(0.);
143 {
144 Evaluation lastZ = 1.0;
145 for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
146 z[compIdx] = priVars.makeEvaluation(z0Idx + compIdx, timeIdx);
147 lastZ -= z[compIdx];
148 }
149 z[numComponents - 1] = lastZ;
150
151 Evaluation sumz = 0.0;
152 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
153 // Clamp only the value; preserve derivatives. Replacing the Evaluation with
154 // max() when the bound applies removes composition derivatives from a
155 // vanished component's conservation equation and makes the cell Jacobian
156 // block singular.
157 if (z[compIdx] < compositionFloor) {
158 z[compIdx].setValue(compositionFloor);
159 }
160 sumz += z[compIdx];
161 }
162 z /= sumz;
163 }
164
165 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
166 fluidState_.setMoleFraction(compIdx, z[compIdx]);
167 }
168
169 Evaluation p = priVars.makeEvaluation(pressure0Idx, timeIdx);
170 for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
171 fluidState_.setPressure(phaseIdx, p);
172 }
173
174 // Get initial K and L from storage initially (if enabled)
175 const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
176 if (hint) {
177 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
178 const Evaluation& Ktmp = hint->fluidState().K(compIdx);
179 fluidState_.setKvalue(compIdx, Ktmp);
180 }
181 const Evaluation& Ltmp = hint->fluidState().L();
182 fluidState_.setLvalue(Ltmp);
183 }
184 else if (timeIdx == 0 && elemCtx.thermodynamicHint(dofIdx, 1)) {
185 // checking the storage cache
186 const auto& hint2 = elemCtx.thermodynamicHint(dofIdx, 1);
187 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
188 const Evaluation& Ktmp = hint2->fluidState().K(compIdx);
189 fluidState_.setKvalue(compIdx, Ktmp);
190 }
191 const Evaluation& Ltmp = hint2->fluidState().L();
192 fluidState_.setLvalue(Ltmp);
193 }
194 else {
195 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
196 const Evaluation Ktmp = fluidState_.wilsonK_(compIdx);
197 fluidState_.setKvalue(compIdx, Ktmp);
198 }
199 const Evaluation& Ltmp = -1.0;
200 fluidState_.setLvalue(Ltmp);
201 }
202
204 // Compute the phase compositions and densities
206 if (flashVerbosity >= 1) {
207 OpmLog::debug(fmt::format("Updating the intensive quantities for cell {}",
208 elemCtx.globalSpaceIndex(dofIdx, timeIdx)));
209 }
210 const auto& eos_type = problem.getEosType();
211 FlashSolver::solve(fluidState_, ptFlashMethod, flashTolerance, eos_type, flashVerbosity);
212
213 if (flashVerbosity >= 5) {
214 std::string phaseCompositions;
215 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
216 fmt::format_to(
217 std::back_inserter(phaseCompositions),
218 " component {}: x = {}, y = {}\n",
219 compIdx,
220 getValue(fluidState_.moleFraction(FluidSystem::oilPhaseIdx, compIdx)),
221 getValue(fluidState_.moleFraction(FluidSystem::gasPhaseIdx, compIdx)));
222 }
223 OpmLog::debug(fmt::format("After the flash for cell {}: liquid fraction = {}\n{}",
224 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
225 getValue(fluidState_.L()),
226 phaseCompositions));
227 }
228
229 // Update phases
230 typename FluidSystem::template ParameterCache<Evaluation> paramCache(eos_type);
231 // the water properties of the cell's PVT region
232 paramCache.setPvtRegionIndex(problem.pvtRegionIndex(elemCtx, dofIdx, timeIdx));
233 paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
234 paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);
235
236 // Update saturation
237 Evaluation Sw = 0.0;
238 if constexpr (waterEnabled) {
239 Sw = priVars.makeEvaluation(water0Idx, timeIdx);
240 }
241 const Evaluation L = fluidState_.L();
242 const Evaluation Vm_L = paramCache.correctedMolarVolume(FluidSystem::oilPhaseIdx);
243 const Evaluation Vm_V = paramCache.correctedMolarVolume(FluidSystem::gasPhaseIdx);
244
245 // Every component storage term is proportional to the pore-space share
246 // occupied by hydrocarbons. If a cell holds only water, a zero share
247 // removes the composition from the residual: the component equations
248 // then depend on no composition variable, and the cell's diagonal
249 // Jacobian block is singular. Every equilibrated cell below the
250 // water-oil contact starts in that state. Floor the value while retaining
251 // its derivatives, as for the composition above.
252 Evaluation hydrocarbon = 1 - Sw;
253 // Remember physical presence before regularization. A positive share,
254 // even below the floor, still represents hydrocarbon in the cell.
255 hasHydrocarbon_ = getValue(hydrocarbon) > Scalar{0};
256 if (hydrocarbon < hydrocarbonFloor) {
257 hydrocarbon.setValue(hydrocarbonFloor);
258 }
259 Evaluation So = max(hydrocarbon * (L * Vm_L / ( L * Vm_L + (1 - L) * Vm_V)), 0.0);
260 Evaluation Sg = max(hydrocarbon - So, 0.0);
261 const Scalar sumS = getValue(So) + getValue(Sg) + getValue(Sw);
262 So /= sumS;
263 Sg /= sumS;
264
265 fluidState_.setSaturation(FluidSystem::oilPhaseIdx, So);
266 fluidState_.setSaturation(FluidSystem::gasPhaseIdx, Sg);
267 if constexpr (waterEnabled) {
268 Sw /= sumS;
269 fluidState_.setSaturation(FluidSystem::waterPhaseIdx, Sw);
270 }
271
272 // The compressibility factor comes from the unshifted EOS root, unlike
273 // the saturations above.
274 const Scalar R = Opm::Constants<Scalar>::R;
275 const Evaluation Z_L = (paramCache.molarVolume(FluidSystem::oilPhaseIdx) *
276 fluidState_.pressure(FluidSystem::oilPhaseIdx)) /
277 (R * fluidState_.temperature(FluidSystem::oilPhaseIdx));
278 const Evaluation Z_V = (paramCache.molarVolume(FluidSystem::gasPhaseIdx) *
279 fluidState_.pressure(FluidSystem::gasPhaseIdx)) /
280 (R * fluidState_.temperature(FluidSystem::gasPhaseIdx));
281 fluidState_.setCompressFactor(FluidSystem::oilPhaseIdx, Z_L);
282 fluidState_.setCompressFactor(FluidSystem::gasPhaseIdx, Z_V);
283
284 if (flashVerbosity >= 5) {
285 OpmLog::debug(fmt::format("Flash phase properties for cell {}: "
286 "oil saturation = {}, gas saturation = {}, "
287 "oil molar volume = {}, gas molar volume = {}",
288 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
289 getValue(So),
290 getValue(Sg),
291 getValue(Vm_L),
292 getValue(Vm_V)));
293 }
294
296 // Compute rel. perm and viscosity and densities
298 const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
299
300 // calculate relative permeability
301 MaterialLaw::relativePermeabilities(relativePermeability_,
302 materialParams, fluidState_);
303 Valgrind::CheckDefined(relativePermeability_);
304
305 // set the phase viscosity and density
306 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
307 if (phaseIdx == static_cast<unsigned int>(FluidSystem::oilPhaseIdx) ||
308 phaseIdx == static_cast<unsigned int>(FluidSystem::gasPhaseIdx))
309 {
310 paramCache.updatePhase(fluidState_, phaseIdx);
311 }
312
313 const Evaluation& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
314
315 fluidState_.setViscosity(phaseIdx, mu);
316
317 mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
318 Valgrind::CheckDefined(mobility_[phaseIdx]);
319
320 const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
321 fluidState_.setDensity(phaseIdx, rho);
322 }
323
325 // Compute the remaining quantities
327
328 // porosity, scaled with the pressure by the rock compressibility of the
329 // cell's region as in the black-oil model
330 porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
331 const Scalar rockCompressibility = problem.rockCompressibility(elemCtx, dofIdx, timeIdx);
332 if (rockCompressibility > 0.0) {
333 const Scalar rockRefPressure = problem.rockReferencePressure(elemCtx, dofIdx, timeIdx);
334 const Evaluation x = rockCompressibility * (p - rockRefPressure);
335 porosity_ *= 1.0 + x + 0.5 * x * x;
336 }
337 Valgrind::CheckDefined(porosity_);
338
339 // intrinsic permeability
340 intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
341
342 // update the quantities specific for the velocity model
343 FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
344
345 // energy related quantities
346 EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
347
348 // update the diffusion specific quantities of the intensive quantities
349 DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
350 }
351
355 const FluidState& fluidState() const
356 { return fluidState_; }
357
360 bool hasHydrocarbon() const
361 { return hasHydrocarbon_; }
362
365 Scalar saturationForOutput(unsigned phaseIdx) const
366 {
367 if (!FluidSystem::phaseIsActive(phaseIdx)) {
368 return Scalar{0};
369 }
370 if (!hasHydrocarbon_) {
371 return phaseIdx == FluidSystem::waterPhaseIdx ? Scalar{1} : Scalar{0};
372 }
373 return getValue(fluidState_.saturation(phaseIdx));
374 }
375
377 bool phaseIsPresent(unsigned phaseIdx) const
378 { return saturationForOutput(phaseIdx) > Scalar{0}; }
379
383 const DimMatrix& intrinsicPermeability() const
384 { return intrinsicPerm_; }
385
389 const Evaluation& relativePermeability(unsigned phaseIdx) const
390 { return relativePermeability_[phaseIdx]; }
391
395 const Evaluation& mobility(unsigned phaseIdx) const
396 { return mobility_[phaseIdx]; }
397
402 const Evaluation& mobility(unsigned phaseIdx, FaceDir::DirEnum) const
403 { return mobility_[phaseIdx]; }
404
410 { return 1.0; }
411
415 const Evaluation& porosity() const
416 { return porosity_; }
417
418private:
419 bool hasHydrocarbon_{true};
420 DimMatrix intrinsicPerm_;
421 FluidState fluidState_;
422 Evaluation porosity_;
423 std::array<Evaluation,numPhases> relativePermeability_;
424 std::array<Evaluation,numPhases> mobility_;
425};
426
427} // namespace Opm
428
429#endif
Provides the volumetric quantities required for the calculation of molecular diffusive fluxes.
Definition: diffusionmodule.hh:143
Provides the volumetric quantities required for the energy equation.
Definition: energymodule.hh:536
Contains the intensive quantities of the flash-based compositional multi-phase model.
Definition: flash/flashintensivequantities.hh:60
bool phaseIsPresent(unsigned phaseIdx) const
Presence for reporting, independent of the numerical hydrocarbon floor.
Definition: ptflash/flashintensivequantities.hh:377
const Evaluation & mobility(unsigned phaseIdx) const
Returns the effective mobility of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:395
const DimMatrix & intrinsicPermeability() const
Returns the intrinsic permeability tensor a degree of freedom.
Definition: ptflash/flashintensivequantities.hh:383
static constexpr Scalar hydrocarbonFloor
Definition: ptflash/flashintensivequantities.hh:118
bool hasHydrocarbon() const
Definition: ptflash/flashintensivequantities.hh:360
FlashIntensiveQuantities(const FlashIntensiveQuantities &other)=default
Scalar saturationForOutput(unsigned phaseIdx) const
Definition: ptflash/flashintensivequantities.hh:365
const FluidState & fluidState() const
Returns the phase state for the control-volume.
Definition: ptflash/flashintensivequantities.hh:355
static constexpr Scalar compositionFloor
Definition: ptflash/flashintensivequantities.hh:113
const Evaluation & porosity() const
Returns the average porosity within the control volume.
Definition: ptflash/flashintensivequantities.hh:415
const Evaluation & mobility(unsigned phaseIdx, FaceDir::DirEnum) const
Mobility across a face. Directional relative permeabilities are not supported, so the face direction ...
Definition: ptflash/flashintensivequantities.hh:402
const Evaluation & relativePermeability(unsigned phaseIdx) const
Returns the relative permeability of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:389
CompositionalFluidState< Evaluation, FluidSystem, enableEnergy > FluidState
The type of the object returned by the fluidState() method.
Definition: flash/flashintensivequantities.hh:93
void update(const ElementContext &elemCtx, unsigned dofIdx, unsigned timeIdx)
Definition: ptflash/flashintensivequantities.hh:129
FlashIntensiveQuantities & operator=(const FlashIntensiveQuantities &other)=default
Scalar rockCompTransMultiplier() const
Transmissibility multiplier from rock compaction. Compositional runs reject ROCKCOMP,...
Definition: ptflash/flashintensivequantities.hh:409
Classes required for molecular diffusion.
Contains the classes required to consider energy as a conservation quantity in a multi-phase module.
Declares the properties required by the compositional multi-phase model based on flash calculations.
Definition: blackoilbioeffectsmodules.hh:45
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 parameters for the compositional multi-phase model based on flash calculations.