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 paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
232 paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);
233
234 // Update saturation
235 Evaluation Sw = 0.0;
236 if constexpr (waterEnabled) {
237 Sw = priVars.makeEvaluation(water0Idx, timeIdx);
238 }
239 const Evaluation L = fluidState_.L();
240 const Evaluation Vm_L = paramCache.correctedMolarVolume(FluidSystem::oilPhaseIdx);
241 const Evaluation Vm_V = paramCache.correctedMolarVolume(FluidSystem::gasPhaseIdx);
242
243 // Every component storage term is proportional to the pore-space share
244 // occupied by hydrocarbons. If a cell holds only water, a zero share
245 // removes the composition from the residual: the component equations
246 // then depend on no composition variable, and the cell's diagonal
247 // Jacobian block is singular. Every equilibrated cell below the
248 // water-oil contact starts in that state. Floor the value while retaining
249 // its derivatives, as for the composition above.
250 Evaluation hydrocarbon = 1 - Sw;
251 // Remember physical presence before regularization. A positive share,
252 // even below the floor, still represents hydrocarbon in the cell.
253 hasHydrocarbon_ = getValue(hydrocarbon) > Scalar{0};
254 if (hydrocarbon < hydrocarbonFloor) {
255 hydrocarbon.setValue(hydrocarbonFloor);
256 }
257 Evaluation So = max(hydrocarbon * (L * Vm_L / ( L * Vm_L + (1 - L) * Vm_V)), 0.0);
258 Evaluation Sg = max(hydrocarbon - So, 0.0);
259 const Scalar sumS = getValue(So) + getValue(Sg) + getValue(Sw);
260 So /= sumS;
261 Sg /= sumS;
262
263 fluidState_.setSaturation(FluidSystem::oilPhaseIdx, So);
264 fluidState_.setSaturation(FluidSystem::gasPhaseIdx, Sg);
265 if constexpr (waterEnabled) {
266 Sw /= sumS;
267 fluidState_.setSaturation(FluidSystem::waterPhaseIdx, Sw);
268 }
269
270 // The compressibility factor comes from the unshifted EOS root, unlike
271 // the saturations above.
272 const Scalar R = Opm::Constants<Scalar>::R;
273 const Evaluation Z_L = (paramCache.molarVolume(FluidSystem::oilPhaseIdx) *
274 fluidState_.pressure(FluidSystem::oilPhaseIdx)) /
275 (R * fluidState_.temperature(FluidSystem::oilPhaseIdx));
276 const Evaluation Z_V = (paramCache.molarVolume(FluidSystem::gasPhaseIdx) *
277 fluidState_.pressure(FluidSystem::gasPhaseIdx)) /
278 (R * fluidState_.temperature(FluidSystem::gasPhaseIdx));
279 fluidState_.setCompressFactor(FluidSystem::oilPhaseIdx, Z_L);
280 fluidState_.setCompressFactor(FluidSystem::gasPhaseIdx, Z_V);
281
282 if (flashVerbosity >= 5) {
283 OpmLog::debug(fmt::format("Flash phase properties for cell {}: "
284 "oil saturation = {}, gas saturation = {}, "
285 "oil molar volume = {}, gas molar volume = {}",
286 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
287 getValue(So),
288 getValue(Sg),
289 getValue(Vm_L),
290 getValue(Vm_V)));
291 }
292
294 // Compute rel. perm and viscosity and densities
296 const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
297
298 // calculate relative permeability
299 MaterialLaw::relativePermeabilities(relativePermeability_,
300 materialParams, fluidState_);
301 Valgrind::CheckDefined(relativePermeability_);
302
303 // set the phase viscosity and density
304 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
305 if (phaseIdx == static_cast<unsigned int>(FluidSystem::oilPhaseIdx) ||
306 phaseIdx == static_cast<unsigned int>(FluidSystem::gasPhaseIdx))
307 {
308 paramCache.updatePhase(fluidState_, phaseIdx);
309 }
310
311 const Evaluation& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
312
313 fluidState_.setViscosity(phaseIdx, mu);
314
315 mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
316 Valgrind::CheckDefined(mobility_[phaseIdx]);
317
318 const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
319 fluidState_.setDensity(phaseIdx, rho);
320 }
321
323 // Compute the remaining quantities
325
326 // porosity
327 porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
328 Valgrind::CheckDefined(porosity_);
329
330 // intrinsic permeability
331 intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
332
333 // update the quantities specific for the velocity model
334 FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
335
336 // energy related quantities
337 EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
338
339 // update the diffusion specific quantities of the intensive quantities
340 DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
341 }
342
346 const FluidState& fluidState() const
347 { return fluidState_; }
348
351 bool hasHydrocarbon() const
352 { return hasHydrocarbon_; }
353
356 Scalar saturationForOutput(unsigned phaseIdx) const
357 {
358 if (!FluidSystem::phaseIsActive(phaseIdx)) {
359 return Scalar{0};
360 }
361 if (!hasHydrocarbon_) {
362 return phaseIdx == FluidSystem::waterPhaseIdx ? Scalar{1} : Scalar{0};
363 }
364 return getValue(fluidState_.saturation(phaseIdx));
365 }
366
368 bool phaseIsPresent(unsigned phaseIdx) const
369 { return saturationForOutput(phaseIdx) > Scalar{0}; }
370
374 const DimMatrix& intrinsicPermeability() const
375 { return intrinsicPerm_; }
376
380 const Evaluation& relativePermeability(unsigned phaseIdx) const
381 { return relativePermeability_[phaseIdx]; }
382
386 const Evaluation& mobility(unsigned phaseIdx) const
387 { return mobility_[phaseIdx]; }
388
393 const Evaluation& mobility(unsigned phaseIdx, FaceDir::DirEnum) const
394 { return mobility_[phaseIdx]; }
395
401 { return 1.0; }
402
406 const Evaluation& porosity() const
407 { return porosity_; }
408
409private:
410 bool hasHydrocarbon_{true};
411 DimMatrix intrinsicPerm_;
412 FluidState fluidState_;
413 Evaluation porosity_;
414 std::array<Evaluation,numPhases> relativePermeability_;
415 std::array<Evaluation,numPhases> mobility_;
416};
417
418} // namespace Opm
419
420#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:368
const Evaluation & mobility(unsigned phaseIdx) const
Returns the effective mobility of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:386
const DimMatrix & intrinsicPermeability() const
Returns the intrinsic permeability tensor a degree of freedom.
Definition: ptflash/flashintensivequantities.hh:374
static constexpr Scalar hydrocarbonFloor
Definition: ptflash/flashintensivequantities.hh:118
bool hasHydrocarbon() const
Definition: ptflash/flashintensivequantities.hh:351
FlashIntensiveQuantities(const FlashIntensiveQuantities &other)=default
Scalar saturationForOutput(unsigned phaseIdx) const
Definition: ptflash/flashintensivequantities.hh:356
const FluidState & fluidState() const
Returns the phase state for the control-volume.
Definition: ptflash/flashintensivequantities.hh:346
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:406
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:393
const Evaluation & relativePermeability(unsigned phaseIdx) const
Returns the relative permeability of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:380
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:400
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.