28#ifndef OPM_BLACKOIL_DIFFUSION_MODULE_HH
29#define OPM_BLACKOIL_DIFFUSION_MODULE_HH
33#include <opm/material/common/Valgrind.hpp>
35#include <dune/common/fvector.hh>
47template <
class TypeTag,
bool enableDiffusion>
50template <
class TypeTag,
bool enableDiffusion>
56template <
class TypeTag>
69 static void initFromState(
const EclipseState&)
84 template <
class Context>
95template <
class TypeTag>
104 enum { numPhases = FluidSystem::numPhases };
105 enum { numComponents = FluidSystem::numComponents };
106 enum { conti0EqIdx = Indices::conti0EqIdx };
108 using Toolbox = MathToolbox<Evaluation>;
118 static void initFromState(
const EclipseState& eclState)
120 use_mole_fraction_ = eclState.getTableManager().diffMoleFraction();
152 template <
class Context>
154 unsigned spaceIdx,
unsigned timeIdx)
157 if(!FluidSystem::enableDiffusion())
159 const auto& extQuants = context.extensiveQuantities(spaceIdx, timeIdx);
160 const auto& fluidStateI = context.intensiveQuantities(extQuants.interiorIndex(), timeIdx).fluidState();
161 const auto& fluidStateJ = context.intensiveQuantities(extQuants.exteriorIndex(), timeIdx).fluidState();
162 const auto& diffusivity = extQuants.diffusivity();
163 const auto& effectiveDiffusionCoefficient = extQuants.effectiveDiffusionCoefficient();
164 addDiffusiveFlux(flux, fluidStateI, fluidStateJ, diffusivity, effectiveDiffusionCoefficient);
167 template<
class Flu
idState,
class EvaluationArray>
169 const FluidState& fluidStateI,
170 const FluidState& fluidStateJ,
171 const Evaluation& diffusivity,
172 const EvaluationArray& effectiveDiffusionCoefficient)
174 unsigned pvtRegionIndex = fluidStateI.pvtRegionIndex();
175 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
176 if (!FluidSystem::phaseIsActive(phaseIdx)) {
181 if (!FluidSystem::enableDissolvedGasInWater() && FluidSystem::waterPhaseIdx == phaseIdx) {
186 if (FluidSystem::gasPhaseIdx == phaseIdx && !FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
191 Evaluation bSAvg = fluidStateI.saturation(phaseIdx) * fluidStateI.invB(phaseIdx);
192 bSAvg += Toolbox::value(fluidStateJ.saturation(phaseIdx)) * Toolbox::value(fluidStateJ.invB(phaseIdx));
198 Evaluation convFactor = 1.0;
199 Evaluation diffR = 0.0;
200 if (FluidSystem::enableDissolvedGas() && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx) && phaseIdx == FluidSystem::oilPhaseIdx) {
201 Evaluation rsAvg = (fluidStateI.Rs() + Toolbox::value(fluidStateJ.Rs())) / 2;
202 convFactor = 1.0 / (toFractionGasOil(pvtRegionIndex) + rsAvg);
203 diffR = fluidStateI.Rs() - Toolbox::value(fluidStateJ.Rs());
205 if (FluidSystem::enableVaporizedOil() && FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx) && phaseIdx == FluidSystem::gasPhaseIdx) {
206 Evaluation rvAvg = (fluidStateI.Rv() + Toolbox::value(fluidStateJ.Rv())) / 2;
207 convFactor = toFractionGasOil(pvtRegionIndex) / (1.0 + rvAvg*toFractionGasOil(pvtRegionIndex));
208 diffR = fluidStateI.Rv() - Toolbox::value(fluidStateJ.Rv());
210 if (FluidSystem::enableDissolvedGasInWater() && phaseIdx == FluidSystem::waterPhaseIdx) {
211 Evaluation rsAvg = (fluidStateI.Rsw() + Toolbox::value(fluidStateJ.Rsw())) / 2;
212 convFactor = 1.0 / (toFractionGasWater(pvtRegionIndex) + rsAvg);
213 diffR = fluidStateI.Rsw() - Toolbox::value(fluidStateJ.Rsw());
215 if (FluidSystem::enableVaporizedWater() && phaseIdx == FluidSystem::gasPhaseIdx) {
216 Evaluation rvAvg = (fluidStateI.Rvw() + Toolbox::value(fluidStateJ.Rvw())) / 2;
217 convFactor = toFractionGasWater(pvtRegionIndex)/ (1.0 + rvAvg*toFractionGasWater(pvtRegionIndex));
218 diffR = fluidStateI.Rvw() - Toolbox::value(fluidStateJ.Rvw());
222 unsigned solventCompIdx = FluidSystem::solventComponentIndex(phaseIdx);
223 unsigned activeSolventCompIdx = Indices::canonicalToActiveComponentIndex(solventCompIdx);
224 flux[conti0EqIdx + activeSolventCompIdx] +=
229 * effectiveDiffusionCoefficient[phaseIdx][solventCompIdx];
232 unsigned soluteCompIdx = FluidSystem::soluteComponentIndex(phaseIdx);
233 unsigned activeSoluteCompIdx = Indices::canonicalToActiveComponentIndex(soluteCompIdx);
234 flux[conti0EqIdx + activeSoluteCompIdx] +=
239 * effectiveDiffusionCoefficient[phaseIdx][soluteCompIdx];
244 static Scalar toFractionGasOil (
unsigned regionIdx) {
245 Scalar mMOil = use_mole_fraction_? FluidSystem::molarMass(FluidSystem::oilCompIdx, regionIdx) : 1;
246 Scalar rhoO = FluidSystem::referenceDensity(FluidSystem::oilPhaseIdx, regionIdx);
247 Scalar mMGas = use_mole_fraction_? FluidSystem::molarMass(FluidSystem::gasCompIdx, regionIdx) : 1;
248 Scalar rhoG = FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, regionIdx);
249 return rhoO * mMGas / (rhoG * mMOil);
251 static Scalar toFractionGasWater (
unsigned regionIdx) {
252 Scalar mMWater = use_mole_fraction_? FluidSystem::molarMass(FluidSystem::waterCompIdx, regionIdx) : 1;
253 Scalar rhoW = FluidSystem::referenceDensity(FluidSystem::waterPhaseIdx, regionIdx);
254 Scalar mMGas = use_mole_fraction_? FluidSystem::molarMass(FluidSystem::gasCompIdx, regionIdx) : 1;
255 Scalar rhoG = FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, regionIdx);
256 return rhoW * mMGas / (rhoG * mMWater);
259 static bool use_mole_fraction_;
262template <
typename TypeTag>
264BlackOilDiffusionModule<TypeTag, true>::use_mole_fraction_;
273template <
class TypeTag,
bool enableDiffusion>
279template <
class TypeTag>
293 throw std::logic_error(
"Method tortuosity() does not make sense "
294 "if diffusion is disabled");
303 throw std::logic_error(
"Method diffusionCoefficient() does not "
304 "make sense if diffusion is disabled");
313 throw std::logic_error(
"Method effectiveDiffusionCoefficient() "
314 "does not make sense if diffusion is disabled");
322 template <
class Flu
idState>
324 typename FluidSystem::template ParameterCache<typename FluidState::Scalar>&,
325 const ElementContext&,
334template <
class TypeTag>
342 enum { numPhases = FluidSystem::numPhases };
343 enum { numComponents = FluidSystem::numComponents };
355 if (
this == &rhs)
return *
this;
357 if (FluidSystem::enableDiffusion()) {
358 std::copy(rhs.tortuosity_, rhs.tortuosity_ + numPhases, tortuosity_);
359 for (
size_t i = 0; i < numPhases; ++i) {
360 std::copy(rhs.diffusionCoefficient_[i],
361 rhs.diffusionCoefficient_[i]+numComponents,
362 diffusionCoefficient_[i]);
372 {
return diffusionCoefficient_[phaseIdx][compIdx]; }
379 {
return tortuosity_[phaseIdx]; }
389 static bool enableTortuosity =
false;
390 if (enableTortuosity)
391 return tortuosity_[phaseIdx] * diffusionCoefficient_[phaseIdx][compIdx];
393 return diffusionCoefficient_[phaseIdx][compIdx];
401 template <
class Flu
idState>
403 typename FluidSystem::template ParameterCache<typename FluidState::Scalar>& paramCache,
404 const ElementContext& elemCtx,
409 if(!FluidSystem::enableDiffusion())
411 const auto& intQuants = elemCtx.intensiveQuantities(dofIdx, timeIdx);
412 update_(fluidState, paramCache, intQuants);
415 template<
class Flu
idState>
417 typename FluidSystem::template ParameterCache<typename FluidState::Scalar>& paramCache,
418 const IntensiveQuantities& intQuants) {
419 using Toolbox = MathToolbox<Evaluation>;
421 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
422 if (!FluidSystem::phaseIsActive(phaseIdx)) {
427 if (!FluidSystem::enableDissolvedGasInWater() && FluidSystem::waterPhaseIdx == phaseIdx) {
434 constexpr double myeps = 0.0001;
435 const Evaluation& base =
438 * intQuants.fluidState().saturation(phaseIdx));
439 tortuosity_[phaseIdx] =
440 1.0 / (intQuants.porosity() * intQuants.porosity())
441 * Toolbox::pow(base, 10.0/3.0);
443 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
444 diffusionCoefficient_[phaseIdx][compIdx] =
445 FluidSystem::diffusionCoefficient(fluidState,
454 Evaluation tortuosity_[numPhases];
455 Evaluation diffusionCoefficient_[numPhases][numComponents];
464template <
class TypeTag,
bool enableDiffusion>
465class BlackOilDiffusionExtensiveQuantities;
470template <
class TypeTag>
487 template <
class Context,
class Flu
idState>
501 throw std::logic_error(
"The method diffusivity() does not "
502 "make sense if diffusion is disabled.");
515 throw std::logic_error(
"The method effectiveDiffusionCoefficient() "
516 "does not make sense if diffusion is disabled.");
524template <
class TypeTag>
532 using Toolbox = MathToolbox<Evaluation>;
535 enum { dimWorld = GridView::dimensionworld };
536 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
537 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
539 using DimVector = Dune::FieldVector<Scalar, dimWorld>;
540 using DimEvalVector = Dune::FieldVector<Evaluation, dimWorld>;
548 void update_(
const ElementContext& elemCtx,
unsigned faceIdx,
unsigned timeIdx)
551 if(!FluidSystem::enableDiffusion())
554 const auto& stencil = elemCtx.stencil(timeIdx);
555 const auto& face = stencil.interiorFace(faceIdx);
556 const auto& extQuants = elemCtx.extensiveQuantities(faceIdx, timeIdx);
557 const auto& intQuantsInside = elemCtx.intensiveQuantities(extQuants.interiorIndex(), timeIdx);
558 const auto& intQuantsOutside = elemCtx.intensiveQuantities(extQuants.exteriorIndex(), timeIdx);
560 const Scalar diffusivity = elemCtx.problem().diffusivity(elemCtx, face.interiorIndex(), face.exteriorIndex());
561 const Scalar faceArea = face.area();
562 diffusivity_ = diffusivity / faceArea;
563 update(effectiveDiffusionCoefficient_, intQuantsInside, intQuantsOutside);
564 Valgrind::CheckDefined(diffusivity_);
569 const IntensiveQuantities& intQuantsInside,
570 const IntensiveQuantities& intQuantsOutside) {
572 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
573 if (!FluidSystem::phaseIsActive(phaseIdx)) {
577 if (!FluidSystem::enableDissolvedGasInWater() && FluidSystem::waterPhaseIdx == phaseIdx) {
580 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
583 effectiveDiffusionCoefficient[phaseIdx][compIdx] = 0.5 *
584 ( intQuantsInside.effectiveDiffusionCoefficient(phaseIdx, compIdx) +
585 intQuantsOutside.effectiveDiffusionCoefficient(phaseIdx, compIdx) );
586 Valgrind::CheckDefined(effectiveDiffusionCoefficient[phaseIdx][compIdx]);
591 template <
class Context,
class Flu
idState>
597 throw std::runtime_error(
"Not implemented: Diffusion across boundary not implemented for blackoil");
608 {
return diffusivity_; }
618 {
return effectiveDiffusionCoefficient_[phaseIdx][compIdx]; }
621 return effectiveDiffusionCoefficient_;
626 EvaluationArray effectiveDiffusionCoefficient_;
const Evaluation & effectiveDiffusionCoefficient(unsigned, unsigned) const
The effective diffusion coeffcient of a component in a fluid phase at the face's integration point.
Definition: blackoildiffusionmodule.hh:512
const Scalar & diffusivity() const
The diffusivity the face.
Definition: blackoildiffusionmodule.hh:499
void update_(const ElementContext &, unsigned, unsigned)
Update the quantities required to calculate the diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:482
void updateBoundary_(const Context &, unsigned, unsigned, const FluidState &)
Definition: blackoildiffusionmodule.hh:488
Provides the quantities required to calculate diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:526
static void update(EvaluationArray &effectiveDiffusionCoefficient, const IntensiveQuantities &intQuantsInside, const IntensiveQuantities &intQuantsOutside)
Definition: blackoildiffusionmodule.hh:568
Evaluation[numPhases][numComponents] EvaluationArray
Definition: blackoildiffusionmodule.hh:542
const Evaluation & effectiveDiffusionCoefficient(unsigned phaseIdx, unsigned compIdx) const
The effective diffusion coeffcient of a component in a fluid phase at the face's integration point.
Definition: blackoildiffusionmodule.hh:617
const auto & effectiveDiffusionCoefficient() const
Definition: blackoildiffusionmodule.hh:620
void updateBoundary_(const Context &, unsigned, unsigned, const FluidState &)
Definition: blackoildiffusionmodule.hh:592
void update_(const ElementContext &elemCtx, unsigned faceIdx, unsigned timeIdx)
Update the quantities required to calculate the diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:548
const Scalar & diffusivity() const
The diffusivity of the face.
Definition: blackoildiffusionmodule.hh:607
Provides the quantities required to calculate diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:51
Scalar effectiveDiffusionCoefficient(unsigned, unsigned) const
Returns the effective molecular diffusion coefficient of the porous medium for a component in a phase...
Definition: blackoildiffusionmodule.hh:311
Scalar diffusionCoefficient(unsigned, unsigned) const
Returns the molecular diffusion coefficient for a component in a phase.
Definition: blackoildiffusionmodule.hh:301
void update_(FluidState &, typename FluidSystem::template ParameterCache< typename FluidState::Scalar > &, const ElementContext &, unsigned, unsigned)
Update the quantities required to calculate diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:323
Scalar tortuosity(unsigned) const
Returns the tortuousity of the sub-domain of a fluid phase in the porous medium.
Definition: blackoildiffusionmodule.hh:291
void update_(FluidState &fluidState, typename FluidSystem::template ParameterCache< typename FluidState::Scalar > ¶mCache, const ElementContext &elemCtx, unsigned dofIdx, unsigned timeIdx)
Update the quantities required to calculate diffusive mass fluxes.
Definition: blackoildiffusionmodule.hh:402
Evaluation diffusionCoefficient(unsigned phaseIdx, unsigned compIdx) const
Returns the molecular diffusion coefficient for a component in a phase.
Definition: blackoildiffusionmodule.hh:371
Evaluation tortuosity(unsigned phaseIdx) const
Returns the tortuousity of the sub-domain of a fluid phase in the porous medium.
Definition: blackoildiffusionmodule.hh:378
BlackOilDiffusionIntensiveQuantities(BlackOilDiffusionIntensiveQuantities &&) noexcept=default
void update_(FluidState &fluidState, typename FluidSystem::template ParameterCache< typename FluidState::Scalar > ¶mCache, const IntensiveQuantities &intQuants)
Definition: blackoildiffusionmodule.hh:416
Evaluation effectiveDiffusionCoefficient(unsigned phaseIdx, unsigned compIdx) const
Returns the effective molecular diffusion coefficient of the porous medium for a component in a phase...
Definition: blackoildiffusionmodule.hh:385
BlackOilDiffusionIntensiveQuantities()=default
Provides the volumetric quantities required for the calculation of molecular diffusive fluxes.
Definition: blackoildiffusionmodule.hh:274
static void registerParameters()
Register all run-time parameters for the diffusion module.
Definition: blackoildiffusionmodule.hh:77
static void addDiffusiveFlux(RateVector &, const Context &, unsigned, unsigned)
Adds the diffusive mass flux flux to the flux vector over a flux integration point.
Definition: blackoildiffusionmodule.hh:85
static void addDiffusiveFlux(RateVector &flux, const Context &context, unsigned spaceIdx, unsigned timeIdx)
Adds the mass flux due to molecular diffusion to the flux vector over the integration point....
Definition: blackoildiffusionmodule.hh:153
static void addDiffusiveFlux(RateVector &flux, const FluidState &fluidStateI, const FluidState &fluidStateJ, const Evaluation &diffusivity, const EvaluationArray &effectiveDiffusionCoefficient)
Definition: blackoildiffusionmodule.hh:168
static void registerParameters()
Register all run-time parameters for the diffusion module.
Definition: blackoildiffusionmodule.hh:127
Provides the auxiliary methods required for consideration of the diffusion equation.
Definition: blackoildiffusionmodule.hh:48
Declare the properties used by the infrastructure code of the finite volume discretizations.
Definition: blackoilboundaryratevector.hh:37
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:235