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/material/Constants.hpp>
35#include <opm/material/common/Valgrind.hpp>
36#include <opm/material/fluidstates/CompositionalFluidState.hpp>
58template <
class TypeTag>
59class FlashIntensiveQuantities
60 :
public GetPropType<TypeTag, Properties::DiscIntensiveQuantities>
61 ,
public DiffusionIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableDiffusion>() >
62 ,
public EnergyIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableEnergy>() >
63 ,
public GetPropType<TypeTag, Properties::FluxModule>::FluxIntensiveQuantities
65 using ParentType = GetPropType<TypeTag, Properties::DiscIntensiveQuantities>;
67 using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
68 using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
69 using MaterialLawParams = GetPropType<TypeTag, Properties::MaterialLawParams>;
70 using Indices = GetPropType<TypeTag, Properties::Indices>;
71 using FluxModule = GetPropType<TypeTag, Properties::FluxModule>;
72 using GridView = GetPropType<TypeTag, Properties::GridView>;
73 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
76 enum { z0Idx = Indices::z0Idx };
77 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
78 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
79 enum { enableDiffusion = getPropValue<TypeTag, Properties::EnableDiffusion>() };
80 enum { enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>() };
81 enum { dimWorld = GridView::dimensionworld };
82 enum { pressure0Idx = Indices::pressure0Idx };
83 enum { water0Idx = Indices::water0Idx};
85 static constexpr bool waterEnabled = Indices::waterEnabled;
87 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
88 using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
89 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
90 using FlashSolver = GetPropType<TypeTag, Properties::FlashSolver>;
92 using ComponentVector = Dune::FieldVector<Evaluation, numComponents>;
93 using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
97 using FluxIntensiveQuantities =
typename FluxModule::FluxIntensiveQuantities;
101 using FluidState = CompositionalFluidState<Evaluation, FluidSystem, enableEnergy>;
112 void update(
const ElementContext& elemCtx,
unsigned dofIdx,
unsigned timeIdx)
114 ParentType::update(elemCtx, dofIdx, timeIdx);
115 EnergyIntensiveQuantities::updateTemperatures_(fluidState_, elemCtx, dofIdx, timeIdx);
117 const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
118 const auto& problem = elemCtx.problem();
120 const Scalar flashTolerance = Parameters::Get<Parameters::FlashTolerance<Scalar>>();
121 const int flashVerbosity = Parameters::Get<Parameters::FlashVerbosity>();
122 const std::string flashTwoPhaseMethod = Parameters::Get<Parameters::FlashTwoPhaseMethod>();
127 ComponentVector z(0.);
129 Evaluation lastZ = 1.0;
130 for (
unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
131 z[compIdx] = priVars.makeEvaluation(z0Idx + compIdx, timeIdx);
134 z[numComponents - 1] = lastZ;
136 Evaluation sumz = 0.0;
137 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
138 z[compIdx] = max(z[compIdx], 1e-8);
144 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
145 fluidState_.setMoleFraction(compIdx, z[compIdx]);
148 Evaluation p = priVars.makeEvaluation(pressure0Idx, timeIdx);
149 for (
int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
150 fluidState_.setPressure(phaseIdx, p);
154 const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
156 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
157 const Evaluation& Ktmp = hint->fluidState().K(compIdx);
158 fluidState_.setKvalue(compIdx, Ktmp);
160 const Evaluation& Ltmp = hint->fluidState().L();
161 fluidState_.setLvalue(Ltmp);
163 else if (timeIdx == 0 && elemCtx.thermodynamicHint(dofIdx, 1)) {
165 const auto& hint2 = elemCtx.thermodynamicHint(dofIdx, 1);
166 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
167 const Evaluation& Ktmp = hint2->fluidState().K(compIdx);
168 fluidState_.setKvalue(compIdx, Ktmp);
170 const Evaluation& Ltmp = hint2->fluidState().L();
171 fluidState_.setLvalue(Ltmp);
174 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
175 const Evaluation Ktmp = fluidState_.wilsonK_(compIdx);
176 fluidState_.setKvalue(compIdx, Ktmp);
178 const Evaluation& Ltmp = -1.0;
179 fluidState_.setLvalue(Ltmp);
185 if (flashVerbosity >= 1) {
186 const int spatialIdx = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
187 std::cout <<
" updating the intensive quantities for Cell " << spatialIdx << std::endl;
189 const auto& eos_type = problem.getEosType();
190 FlashSolver::solve(fluidState_, flashTwoPhaseMethod, flashTolerance, eos_type, flashVerbosity);
192 if (flashVerbosity >= 5) {
194 const int spatialIdx = elemCtx.globalSpaceIndex(dofIdx, timeIdx);
195 std::cout <<
" \n After flash solve for cell " << spatialIdx << std::endl;
196 ComponentVector x, y;
197 for (
unsigned comp_idx = 0; comp_idx < numComponents; ++comp_idx) {
198 x[comp_idx] = fluidState_.moleFraction(FluidSystem::oilPhaseIdx, comp_idx);
199 y[comp_idx] = fluidState_.moleFraction(FluidSystem::gasPhaseIdx, comp_idx);
201 for (
unsigned comp_idx = 0; comp_idx < numComponents; ++comp_idx) {
202 std::cout <<
" x for component: " << comp_idx <<
" is:" << std::endl;
203 std::cout << x[comp_idx] << std::endl;
205 std::cout <<
" y for component: " << comp_idx <<
"is:" << std::endl;
206 std::cout << y[comp_idx] << std::endl;
208 const Evaluation& L = fluidState_.L();
209 std::cout <<
" L is:" << std::endl;
210 std::cout << L << std::endl;
214 typename FluidSystem::template ParameterCache<Evaluation> paramCache(eos_type);
215 paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
217 const Scalar R = Opm::Constants<Scalar>::R;
218 const Evaluation Z_L = (paramCache.molarVolume(FluidSystem::oilPhaseIdx) *
219 fluidState_.pressure(FluidSystem::oilPhaseIdx)) /
220 (R * fluidState_.temperature(FluidSystem::oilPhaseIdx));
221 paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);
222 const Evaluation Z_V = (paramCache.molarVolume(FluidSystem::gasPhaseIdx) *
223 fluidState_.pressure(FluidSystem::gasPhaseIdx)) /
224 (R * fluidState_.temperature(FluidSystem::gasPhaseIdx));
228 if constexpr (waterEnabled) {
229 Sw = priVars.makeEvaluation(water0Idx, timeIdx);
231 const Evaluation L = fluidState_.L();
232 Evaluation So = max((1 - Sw) * (L * Z_L / ( L * Z_L + (1 - L) * Z_V)), 0.0);
233 Evaluation Sg = max(1 - So - Sw, 0.0);
234 const Scalar sumS = getValue(So) + getValue(Sg) + getValue(Sw);
238 fluidState_.setSaturation(FluidSystem::oilPhaseIdx, So);
239 fluidState_.setSaturation(FluidSystem::gasPhaseIdx, Sg);
240 if constexpr (waterEnabled) {
242 fluidState_.setSaturation(FluidSystem::waterPhaseIdx, Sw);
245 fluidState_.setCompressFactor(FluidSystem::oilPhaseIdx, Z_L);
246 fluidState_.setCompressFactor(FluidSystem::gasPhaseIdx, Z_V);
249 if (flashVerbosity >= 5) {
250 std::cout <<
"So = " << So << std::endl;
251 std::cout <<
"Sg = " << Sg << std::endl;
252 std::cout <<
"Z_L = " << Z_L << std::endl;
253 std::cout <<
"Z_V = " << Z_V << std::endl;
259 const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
262 MaterialLaw::relativePermeabilities(relativePermeability_,
263 materialParams, fluidState_);
264 Valgrind::CheckDefined(relativePermeability_);
267 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
268 if (phaseIdx ==
static_cast<unsigned int>(FluidSystem::oilPhaseIdx) ||
269 phaseIdx ==
static_cast<unsigned int>(FluidSystem::gasPhaseIdx))
271 paramCache.updatePhase(fluidState_, phaseIdx);
274 const Evaluation& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
276 fluidState_.setViscosity(phaseIdx, mu);
278 mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
279 Valgrind::CheckDefined(mobility_[phaseIdx]);
281 const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
282 fluidState_.setDensity(phaseIdx, rho);
290 porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
291 Valgrind::CheckDefined(porosity_);
294 intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
297 FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
300 EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
303 DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
310 {
return fluidState_; }
316 {
return intrinsicPerm_; }
322 {
return relativePermeability_[phaseIdx]; }
327 const Evaluation&
mobility(
unsigned phaseIdx)
const
328 {
return mobility_[phaseIdx]; }
334 {
return porosity_; }
337 DimMatrix intrinsicPerm_;
339 Evaluation porosity_;
340 std::array<Evaluation,numPhases> relativePermeability_;
341 std::array<Evaluation,numPhases> mobility_;
Provides the volumetric quantities required for the calculation of molecular diffusive fluxes.
Definition: diffusionmodule.hh:145
Provides the volumetric quantities required for the energy equation.
Definition: energymodule.hh:540
Contains the intensive quantities of the flash-based compositional multi-phase model.
Definition: flash/flashintensivequantities.hh:60
const Evaluation & mobility(unsigned phaseIdx) const
Returns the effective mobility of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:327
const DimMatrix & intrinsicPermeability() const
Returns the intrinsic permeability tensor a degree of freedom.
Definition: ptflash/flashintensivequantities.hh:315
FlashIntensiveQuantities(const FlashIntensiveQuantities &other)=default
const FluidState & fluidState() const
Returns the phase state for the control-volume.
Definition: ptflash/flashintensivequantities.hh:309
const Evaluation & porosity() const
Returns the average porosity within the control volume.
Definition: ptflash/flashintensivequantities.hh:333
const Evaluation & relativePermeability(unsigned phaseIdx) const
Returns the relative permeability of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:321
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:112
FlashIntensiveQuantities & operator=(const FlashIntensiveQuantities &other)=default
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: 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 parameters for the compositional multi-phase model based on flash calculations.