28#ifndef OPM_FLASH_INTENSIVE_QUANTITIES_HH
29#define OPM_FLASH_INTENSIVE_QUANTITIES_HH
31#include <dune/common/fmatrix.hh>
32#include <dune/common/fvector.hh>
34#include <opm/common/OpmLog/OpmLog.hpp>
36#include <opm/input/eclipse/EclipseState/Grid/FaceDir.hpp>
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>
51#include <fmt/format.h>
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
72 using ParentType = GetPropType<TypeTag, Properties::DiscIntensiveQuantities>;
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>;
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};
92 static constexpr bool waterEnabled = Indices::waterEnabled;
94 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
96 using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
97 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
98 using FlashSolver = GetPropType<TypeTag, Properties::FlashSolver>;
100 using ComponentVector = Dune::FieldVector<Evaluation, numComponents>;
101 using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
105 using FluxIntensiveQuantities =
typename FluxModule::FluxIntensiveQuantities;
109 using FluidState = CompositionalFluidState<Evaluation, FluidSystem, enableEnergy>;
129 void update(
const ElementContext& elemCtx,
unsigned dofIdx,
unsigned timeIdx)
131 ParentType::update(elemCtx, dofIdx, timeIdx);
132 EnergyIntensiveQuantities::updateTemperatures_(fluidState_, elemCtx, dofIdx, timeIdx);
134 const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
135 const auto& problem = elemCtx.problem();
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>());
142 ComponentVector z(0.);
144 Evaluation lastZ = 1.0;
145 for (
unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
146 z[compIdx] = priVars.makeEvaluation(z0Idx + compIdx, timeIdx);
149 z[numComponents - 1] = lastZ;
151 Evaluation sumz = 0.0;
152 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
165 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
166 fluidState_.setMoleFraction(compIdx, z[compIdx]);
169 Evaluation p = priVars.makeEvaluation(pressure0Idx, timeIdx);
170 for (
int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
171 fluidState_.setPressure(phaseIdx, p);
175 const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
177 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
178 const Evaluation& Ktmp = hint->fluidState().K(compIdx);
179 fluidState_.setKvalue(compIdx, Ktmp);
181 const Evaluation& Ltmp = hint->fluidState().L();
182 fluidState_.setLvalue(Ltmp);
184 else if (timeIdx == 0 && elemCtx.thermodynamicHint(dofIdx, 1)) {
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);
191 const Evaluation& Ltmp = hint2->fluidState().L();
192 fluidState_.setLvalue(Ltmp);
195 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
196 const Evaluation Ktmp = fluidState_.wilsonK_(compIdx);
197 fluidState_.setKvalue(compIdx, Ktmp);
199 const Evaluation& Ltmp = -1.0;
200 fluidState_.setLvalue(Ltmp);
206 if (flashVerbosity >= 1) {
207 OpmLog::debug(fmt::format(
"Updating the intensive quantities for cell {}",
208 elemCtx.globalSpaceIndex(dofIdx, timeIdx)));
210 const auto& eos_type = problem.getEosType();
211 FlashSolver::solve(fluidState_, ptFlashMethod, flashTolerance, eos_type, flashVerbosity);
213 if (flashVerbosity >= 5) {
214 std::string phaseCompositions;
215 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
217 std::back_inserter(phaseCompositions),
218 " component {}: x = {}, y = {}\n",
220 getValue(fluidState_.moleFraction(FluidSystem::oilPhaseIdx, compIdx)),
221 getValue(fluidState_.moleFraction(FluidSystem::gasPhaseIdx, compIdx)));
223 OpmLog::debug(fmt::format(
"After the flash for cell {}: liquid fraction = {}\n{}",
224 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
225 getValue(fluidState_.L()),
230 typename FluidSystem::template ParameterCache<Evaluation> paramCache(eos_type);
231 paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
232 paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);
236 if constexpr (waterEnabled) {
237 Sw = priVars.makeEvaluation(water0Idx, timeIdx);
239 const Evaluation L = fluidState_.L();
240 const Evaluation Vm_L = paramCache.correctedMolarVolume(FluidSystem::oilPhaseIdx);
241 const Evaluation Vm_V = paramCache.correctedMolarVolume(FluidSystem::gasPhaseIdx);
250 Evaluation hydrocarbon = 1 - Sw;
253 hasHydrocarbon_ = getValue(hydrocarbon) > Scalar{0};
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);
263 fluidState_.setSaturation(FluidSystem::oilPhaseIdx, So);
264 fluidState_.setSaturation(FluidSystem::gasPhaseIdx, Sg);
265 if constexpr (waterEnabled) {
267 fluidState_.setSaturation(FluidSystem::waterPhaseIdx, Sw);
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);
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),
296 const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
299 MaterialLaw::relativePermeabilities(relativePermeability_,
300 materialParams, fluidState_);
301 Valgrind::CheckDefined(relativePermeability_);
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))
308 paramCache.updatePhase(fluidState_, phaseIdx);
311 const Evaluation& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
313 fluidState_.setViscosity(phaseIdx, mu);
315 mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
316 Valgrind::CheckDefined(mobility_[phaseIdx]);
318 const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
319 fluidState_.setDensity(phaseIdx, rho);
327 porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
328 Valgrind::CheckDefined(porosity_);
331 intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
334 FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
337 EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
340 DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
347 {
return fluidState_; }
352 {
return hasHydrocarbon_; }
358 if (!FluidSystem::phaseIsActive(phaseIdx)) {
361 if (!hasHydrocarbon_) {
362 return phaseIdx == FluidSystem::waterPhaseIdx ? Scalar{1} : Scalar{0};
364 return getValue(fluidState_.saturation(phaseIdx));
375 {
return intrinsicPerm_; }
381 {
return relativePermeability_[phaseIdx]; }
386 const Evaluation&
mobility(
unsigned phaseIdx)
const
387 {
return mobility_[phaseIdx]; }
393 const Evaluation&
mobility(
unsigned phaseIdx, FaceDir::DirEnum)
const
394 {
return mobility_[phaseIdx]; }
407 {
return porosity_; }
410 bool hasHydrocarbon_{
true};
411 DimMatrix intrinsicPerm_;
413 Evaluation porosity_;
414 std::array<Evaluation,numPhases> relativePermeability_;
415 std::array<Evaluation,numPhases> mobility_;
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
FlashIntensiveQuantities()=default
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.