opm-common
BlackOilFluidSystem_macrotemplate.hpp
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 */
33 #include "opm/material/fluidsystems/blackoilpvt/NullOilPvt.hpp"
34 
35 #include <opm/common/ErrorMacros.hpp>
36 #include <opm/common/TimingMacros.hpp>
37 #include <opm/common/utility/VectorWithDefaultAllocator.hpp>
38 #include <opm/common/utility/gpuDecorators.hpp>
40 
43 
49 #include <opm/material/fluidsystems/PhaseUsageInfo.hpp>
50 
51 #include <array>
52 #include <cstddef>
53 #include <memory>
54 #include <stdexcept>
55 #include <string>
56 #include <string_view>
57 #include <type_traits>
58 #include <vector>
59 
60 namespace Opm {
61 
62 class EclipseState;
63 class Schedule;
64 
71 template <class Scalar, class IndexTraits = BlackOilDefaultFluidSystemIndices,
72  template<typename> typename Storage = VectorWithDefaultAllocator>
73 class FLUIDSYSTEM_CLASSNAME : public BaseFluidSystem<Scalar, FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage> >
74 {
76 
77 public:
78 
79  // The logic here is the following: We test if we are instantiating on the CPU
80  // std::is_same_v<Storage<Scalar>, VectorWithDefaultAllocator<Scalar> == true
81  // and if so, we use the multiplexer classes, if not, we use the concrete types.
82  using GasPvt = std::conditional_t<std::is_same_v<Storage<Scalar>, VectorWithDefaultAllocator<Scalar>>,
85  using OilPvt = std::conditional_t<std::is_same_v<Storage<Scalar>, VectorWithDefaultAllocator<Scalar>>,
87  NullOilPvt<Scalar>>; // Currently do not support on GPU
88  using WaterPvt = std::conditional_t<std::is_same_v<Storage<Scalar>, VectorWithDefaultAllocator<Scalar>>,
91  using IndexTraitsType = IndexTraits;
92 
93  #ifdef COMPILING_STATIC_FLUID_SYSTEM
94  template <class EvaluationT>
96  struct ParameterCache : public NullParameterCache<EvaluationT>
97  {
98  using Evaluation = EvaluationT;
99 
100  public:
101  explicit ParameterCache(unsigned regionIdx = 0)
102  : regionIdx_(regionIdx)
103  {
104  }
105 
113  template <class OtherCache>
114  void assignPersistentData(const OtherCache& other)
115  {
116  regionIdx_ = other.regionIndex();
117  }
118 
126  unsigned regionIndex() const
127  { return regionIdx_; }
128 
136  void setRegionIndex(unsigned val)
137  { regionIdx_ = val; }
138 
139  private:
140  unsigned regionIdx_;
141  };
142 
143  #else
144 
145  // We want to use the exact same ParameterCache class for both the static and nonstatic versions
146  template<class EvaluationT>
147  using ParameterCache =
149  IndexTraits,
150  Storage>::template ParameterCache<EvaluationT>;
151  #endif
152 
153  #ifndef COMPILING_STATIC_FLUID_SYSTEM
154 
159  template<template<typename> typename StorageT>
163  , reservoirTemperature_(other.reservoirTemperature_)
164  , gasPvt_(other.gasPvt_)
165  , oilPvt_(other.oilPvt_)
166  , waterPvt_(other.waterPvt_)
167  , enableDissolvedGas_(other.enableDissolvedGas_)
168  , enableDissolvedGasInWater_(other.enableDissolvedGasInWater_)
169  , enableVaporizedOil_(other.enableVaporizedOil_)
170  , enableVaporizedWater_(other.enableVaporizedWater_)
171  , enableDiffusion_(other.enableDiffusion_)
172  , referenceDensity_(StorageT<typename decltype(referenceDensity_)::value_type>(other.referenceDensity_))
173  , molarMass_(StorageT<typename decltype(molarMass_)::value_type>(other.molarMass_))
174  , diffusionCoefficients_(StorageT<typename decltype(diffusionCoefficients_)::value_type>(other.diffusionCoefficients_))
175  , phaseUsageInfo_(other.phaseUsageInfo_)
176  , isInitialized_(other.isInitialized_)
177  , useSaturatedTables_(other.useSaturatedTables_)
178  , enthalpy_eq_energy_(other.enthalpy_eq_energy_)
179  {
180  }
181 
182  FLUIDSYSTEM_CLASSNAME(Scalar _surfacePressure_,
183  Scalar _surfaceTemperature_,
184  Scalar _reservoirTemperature_,
185  const GasPvt& _gasPvt_,
186  const OilPvt& _oilPvt_,
187  const WaterPvt& _waterPvt_,
188  bool _enableDissolvedGas_,
189  bool _enableDissolvedGasInWater_,
190  bool _enableVaporizedOil_,
191  bool _enableVaporizedWater_,
192  bool _enableConstantRs_,
193  bool _enableDiffusion_,
194  Storage<std::array<Scalar, 3>>&& _referenceDensity_,
195  Storage<std::array<Scalar, 3>>&& _molarMass_,
196  Storage<std::array<Scalar, 3 * 3>>&& _diffusionCoefficients_,
197  const PhaseUsageInfo<IndexTraits>& _phaseUsageInfo_,
198  bool _isInitialized_,
199  bool _useSaturatedTables_,
200  bool _enthalpy_eq_energy_)
201  : surfacePressure(_surfacePressure_)
202  , surfaceTemperature(_surfaceTemperature_)
203  , reservoirTemperature_(_reservoirTemperature_)
204  , gasPvt_(_gasPvt_)
205  , oilPvt_(_oilPvt_)
206  , waterPvt_(_waterPvt_)
207  , enableDissolvedGas_(_enableDissolvedGas_)
208  , enableDissolvedGasInWater_(_enableDissolvedGasInWater_)
209  , enableVaporizedOil_(_enableVaporizedOil_)
210  , enableVaporizedWater_(_enableVaporizedWater_)
211  , enableConstantRs_(_enableConstantRs_)
212  , enableDiffusion_(_enableDiffusion_)
213  , referenceDensity_(std::move(_referenceDensity_))
214  , molarMass_(std::move(_molarMass_))
215  , diffusionCoefficients_(std::move(_diffusionCoefficients_))
216  , phaseUsageInfo_(_phaseUsageInfo_)
217  , isInitialized_(_isInitialized_)
218  , useSaturatedTables_(_useSaturatedTables_)
219  , enthalpy_eq_energy_(_enthalpy_eq_energy_)
220  {
221  }
222 
223  #if HAVE_CUDA
224  template <class ScalarT, class IndexTraitsT>
225  friend FLUIDSYSTEM_CLASSNAME<ScalarT, IndexTraitsT, gpuistl::GpuBuffer>
226  gpuistl::copy_to_gpu(const FLUIDSYSTEM_CLASSNAME<ScalarT, IndexTraitsT>& oldFluidSystem);
227 
228  template <class ScalarT, class IndexTraitsT>
229  friend FLUIDSYSTEM_CLASSNAME<ScalarT, IndexTraitsT, gpuistl::GpuView>
230  gpuistl::make_view(FLUIDSYSTEM_CLASSNAME<ScalarT, IndexTraitsT, gpuistl::GpuBuffer>& oldFluidSystem);
231 
232  #endif // HAVE_CUDA
233  #endif // COMPILING_STATIC_FLUID_SYSTEM
234 
235  #ifdef COMPILING_STATIC_FLUID_SYSTEM
236 
243  template<template<typename> typename StorageT = VectorWithDefaultAllocator>
244  static FLUIDSYSTEM_CLASSNAME_NONSTATIC<Scalar, IndexTraits, StorageT>& getNonStaticInstance()
245  {
246  static FLUIDSYSTEM_CLASSNAME_NONSTATIC<Scalar, IndexTraits, StorageT> instance{FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage>()};
247  // Refresh the singleton from the current static fluid-system state.
248  // The static fluid system can be re-initialised (for example by a
249  // subsequent readDeck call) and the non-static instance must mirror
250  // the current static data, otherwise downstream consumers (e.g. the
251  // GPU intensive-quantities path) end up using stale PVT/density
252  // tables and produce results that no longer match the CPU side.
253  instance = FLUIDSYSTEM_CLASSNAME_NONSTATIC<Scalar, IndexTraits, StorageT>(
254  FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage>());
255  return instance;
256 
257  }
258  #endif
259 
260  /****************************************
261  * Initialization
262  ****************************************/
266  STATIC_OR_NOTHING void initFromState(const EclipseState& eclState, const Schedule& schedule);
267 
276  STATIC_OR_NOTHING void initBegin(std::size_t numPvtRegions);
277 
284  STATIC_OR_DEVICE void setEnableDissolvedGas(bool yesno)
285  { enableDissolvedGas_ = yesno; }
286 
293  STATIC_OR_DEVICE void setEnableVaporizedOil(bool yesno)
294  { enableVaporizedOil_ = yesno; }
295 
302  STATIC_OR_DEVICE void setEnableVaporizedWater(bool yesno)
303  { enableVaporizedWater_ = yesno; }
304 
311  STATIC_OR_DEVICE void setEnableDissolvedGasInWater(bool yesno)
312  { enableDissolvedGasInWater_ = yesno; }
313 
320  STATIC_OR_DEVICE void setEnableConstantRs(bool yesno)
321  { enableConstantRs_ = yesno; }
322 
328  STATIC_OR_DEVICE void setEnableDiffusion(bool yesno)
329  { enableDiffusion_ = yesno; }
330 
336  STATIC_OR_DEVICE void setUseSaturatedTables(bool yesno)
337  { useSaturatedTables_ = yesno; }
338 
342  STATIC_OR_DEVICE void setGasPvt(std::shared_ptr<GasPvt> pvtObj)
343  { gasPvt_ = *pvtObj; }
344 
348  STATIC_OR_DEVICE void setOilPvt(std::shared_ptr<OilPvt> pvtObj)
349  { oilPvt_ = *pvtObj; }
350 
354  STATIC_OR_DEVICE void setWaterPvt(std::shared_ptr<WaterPvt> pvtObj)
355  { waterPvt_ = *pvtObj; }
356 
357  STATIC_OR_DEVICE void setVapPars(const Scalar par1, const Scalar par2)
358  {
359  if (gasPvt_.isActive()) {
360  gasPvt_.setVapPars(par1, par2);
361  }
362  if (oilPvt_.isActive()) {
363  oilPvt_.setVapPars(par1, par2);
364  }
365  if (waterPvt_.isActive()) {
366  waterPvt_.setVapPars(par1, par2);
367  }
368  }
369 
378  STATIC_OR_DEVICE void setReferenceDensities(Scalar rhoOil,
379  Scalar rhoWater,
380  Scalar rhoGas,
381  unsigned regionIdx);
382 
386  STATIC_OR_DEVICE void initEnd();
387 
388  STATIC_OR_DEVICE bool isInitialized() NOTHING_OR_CONST
389  { return isInitialized_; }
390 
391  /****************************************
392  * Generic phase properties
393  ****************************************/
394 
396  static constexpr unsigned numPhases = IndexTraits::numPhases;
397 
399  static constexpr unsigned waterPhaseIdx = IndexTraits::waterPhaseIdx;
401  static constexpr unsigned oilPhaseIdx = IndexTraits::oilPhaseIdx;
403  static constexpr unsigned gasPhaseIdx = IndexTraits::gasPhaseIdx;
404 
406  STATIC_OR_NOTHING Scalar surfacePressure;
407 
409  STATIC_OR_NOTHING Scalar surfaceTemperature;
410 
412  STATIC_OR_NOTHING std::string_view phaseName(unsigned phaseIdx) NOTHING_OR_CONST;
413 
415  STATIC_OR_DEVICE bool isLiquid(unsigned phaseIdx) NOTHING_OR_CONST
416  {
417  assert(phaseIdx < numPhases);
418  return phaseIdx != gasPhaseIdx;
419  }
420 
421  /****************************************
422  * Generic component related properties
423  ****************************************/
424 
426  static constexpr unsigned numComponents = IndexTraits::numComponents;
427 
429  static constexpr int oilCompIdx = IndexTraits::oilCompIdx;
431  static constexpr int waterCompIdx = IndexTraits::waterCompIdx;
433  static constexpr int gasCompIdx = IndexTraits::gasCompIdx;
434 
435 public:
436 
438  STATIC_OR_DEVICE const PhaseUsageInfo<IndexTraits>& phaseUsage() NOTHING_OR_CONST
439  { return phaseUsageInfo_; }
440 
442  STATIC_OR_DEVICE unsigned numActivePhases() NOTHING_OR_CONST
443  { return phaseUsageInfo_.numActivePhases(); }
444 
446  STATIC_OR_DEVICE bool phaseIsActive(unsigned phaseIdx) NOTHING_OR_CONST
447  {
448  return phaseUsageInfo_.phaseIsActive(phaseIdx);
449  }
450 
452  STATIC_OR_DEVICE unsigned solventComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST;
453 
455  STATIC_OR_DEVICE unsigned soluteComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST;
456 
458  STATIC_OR_NOTHING std::string_view componentName(unsigned compIdx) NOTHING_OR_CONST;
459 
461  STATIC_OR_DEVICE Scalar molarMass(unsigned compIdx, unsigned regionIdx = 0) NOTHING_OR_CONST
462  { return molarMass_[regionIdx][compIdx]; }
463 
465  STATIC_OR_DEVICE bool isIdealMixture(unsigned /*phaseIdx*/)
466  {
467  // fugacity coefficients are only pressure dependent -> we
468  // have an ideal mixture
469  return true;
470  }
471 
473  STATIC_OR_DEVICE bool isCompressible(unsigned /*phaseIdx*/) NOTHING_OR_CONST
474  { return true; /* all phases are compressible */ }
475 
477  STATIC_OR_DEVICE bool isIdealGas(unsigned /*phaseIdx*/) NOTHING_OR_CONST
478  { return false; }
479 
480 
481  /****************************************
482  * Black-oil specific properties
483  ****************************************/
489  STATIC_OR_DEVICE std::size_t numRegions() NOTHING_OR_CONST
490  { return molarMass_.size(); }
491 
498  STATIC_OR_DEVICE bool enableDissolvedGas() NOTHING_OR_CONST
499  { return enableDissolvedGas_; }
500 
501 
508  STATIC_OR_DEVICE bool enableDissolvedGasInWater() NOTHING_OR_CONST
509  { return enableDissolvedGasInWater_; }
510 
517  STATIC_OR_DEVICE bool enableVaporizedOil() NOTHING_OR_CONST
518  { return enableVaporizedOil_; }
519 
526  STATIC_OR_DEVICE bool enableVaporizedWater() NOTHING_OR_CONST
527  { return enableVaporizedWater_; }
528 
532  STATIC_OR_DEVICE bool enableConstantRs() NOTHING_OR_CONST
533  { return enableConstantRs_; }
534 
540  STATIC_OR_DEVICE bool enableDiffusion() NOTHING_OR_CONST
541  { return enableDiffusion_; }
542 
548  STATIC_OR_DEVICE bool useSaturatedTables() NOTHING_OR_CONST
549  { return useSaturatedTables_; }
550 
556  STATIC_OR_DEVICE Scalar referenceDensity(unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
557  { return referenceDensity_[regionIdx][phaseIdx]; }
558 
559  /****************************************
560  * thermodynamic quantities (generic version)
561  ****************************************/
563  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
564  STATIC_OR_DEVICE LhsEval density(const FluidState& fluidState,
565  const ParameterCache<ParamCacheEval>& paramCache,
566  unsigned phaseIdx) NOTHING_OR_CONST
567  { return density<FluidState, LhsEval>(fluidState, phaseIdx, paramCache.regionIndex()); }
568 
570  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
571  STATIC_OR_DEVICE LhsEval fugacityCoefficient(const FluidState& fluidState,
572  const ParameterCache<ParamCacheEval>& paramCache,
573  unsigned phaseIdx,
574  unsigned compIdx) NOTHING_OR_CONST
575  {
576  return fugacityCoefficient<FluidState, LhsEval>(fluidState,
577  phaseIdx,
578  compIdx,
579  paramCache.regionIndex());
580  }
581 
583  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
584  STATIC_OR_DEVICE LhsEval viscosity(const FluidState& fluidState,
585  const ParameterCache<ParamCacheEval>& paramCache,
586  unsigned phaseIdx) NOTHING_OR_CONST
587  { return viscosity<FluidState, LhsEval>(fluidState, phaseIdx, paramCache.regionIndex()); }
588 
590  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
591  STATIC_OR_DEVICE LhsEval enthalpy(const FluidState& fluidState,
592  const ParameterCache<ParamCacheEval>& paramCache,
593  unsigned phaseIdx)
594  { return enthalpy<FluidState, LhsEval>(fluidState, phaseIdx, paramCache.regionIndex()); }
595 
596  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
597  STATIC_OR_DEVICE LhsEval internalEnergy(const FluidState& fluidState,
598  const ParameterCache<ParamCacheEval>& paramCache,
599  unsigned phaseIdx) NOTHING_OR_CONST
600  { return internalEnergy<FluidState, LhsEval>(fluidState, phaseIdx, paramCache.regionIndex()); }
601 
602  /****************************************
603  * thermodynamic quantities (black-oil specific version: Note that the PVT region
604  * index is explicitly passed instead of a parameter cache object)
605  ****************************************/
607  template <class FluidState, class LhsEval = typename FluidState::ValueType>
608  STATIC_OR_DEVICE LhsEval density(const FluidState& fluidState,
609  unsigned phaseIdx,
610  unsigned regionIdx) NOTHING_OR_CONST
611  {
612  assert(phaseIdx <= numPhases);
613  assert(regionIdx <= numRegions());
614 
615  const LhsEval& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
616  const LhsEval& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
617  const LhsEval& saltConcentration = BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx);
618 
619  switch (phaseIdx) {
620  case oilPhaseIdx: {
621  if (enableConstantRs()) {
622  // dead oil but positive constant Rs
623  const LhsEval& Rs = oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p);
624  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
625 
626  return
627  bo*referenceDensity(oilPhaseIdx, regionIdx)
628  + Rs*bo*referenceDensity(gasPhaseIdx, regionIdx);
629  }
630 
631  if (enableDissolvedGas()) {
632  // miscible oil
633  const LhsEval& Rs = BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
634  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
635 
636  return
637  bo*referenceDensity(oilPhaseIdx, regionIdx)
638  + Rs*bo*referenceDensity(gasPhaseIdx, regionIdx);
639  }
640 
641  // immiscible oil
642  const LhsEval Rs(0.0);
643  const auto& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
644 
645  return referenceDensity(phaseIdx, regionIdx)*bo;
646  }
647 
648  case gasPhaseIdx: {
650  // gas containing vaporized oil and vaporized water
651  const LhsEval& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
652  const LhsEval& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
653  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
654 
655  return
656  bg*referenceDensity(gasPhaseIdx, regionIdx)
657  + Rv*bg*referenceDensity(oilPhaseIdx, regionIdx)
658  + Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx);
659  }
660  if (enableVaporizedOil()) {
661  // miscible gas
662  const LhsEval Rvw(0.0);
663  const LhsEval& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
664  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
665 
666  return
667  bg*referenceDensity(gasPhaseIdx, regionIdx)
668  + Rv*bg*referenceDensity(oilPhaseIdx, regionIdx);
669  }
670  if (enableVaporizedWater()) {
671  // gas containing vaporized water
672  const LhsEval Rv(0.0);
673  const LhsEval& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
674  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
675 
676  return
677  bg*referenceDensity(gasPhaseIdx, regionIdx)
678  + Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx);
679  }
680 
681  // immiscible gas
682  const LhsEval Rv(0.0);
683  const LhsEval Rvw(0.0);
684  const auto& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
685  return bg*referenceDensity(phaseIdx, regionIdx);
686  }
687 
688  case waterPhaseIdx:
690  // gas miscible in water
691  const LhsEval& Rsw =BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
692  const LhsEval& bw = waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
693  return
694  bw*referenceDensity(waterPhaseIdx, regionIdx)
695  + Rsw*bw*referenceDensity(gasPhaseIdx, regionIdx);
696  }
697  const LhsEval Rsw(0.0);
698  return
699  referenceDensity(waterPhaseIdx, regionIdx)
700  * waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
701  }
702 
703  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
704  }
705 
715  template <class FluidState, class LhsEval = typename FluidState::ValueType>
716  STATIC_OR_DEVICE LhsEval saturatedDensity(const FluidState& fluidState,
717  unsigned phaseIdx,
718  unsigned regionIdx) NOTHING_OR_CONST
719  {
720  assert(phaseIdx <= numPhases);
721  assert(regionIdx <= numRegions());
722 
723  const auto& p = fluidState.pressure(phaseIdx);
724  const auto& T = fluidState.temperature(phaseIdx);
725 
726  switch (phaseIdx) {
727  case oilPhaseIdx: {
728  if (enableConstantRs()) {
729  // dead oil but positive constant Rs
730  const LhsEval& Rs = oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p);
731  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
732 
733  return
734  bo*referenceDensity(oilPhaseIdx, regionIdx)
735  + Rs*bo*referenceDensity(gasPhaseIdx, regionIdx);
736  }
737 
738  if (enableDissolvedGas()) {
739  // miscible oil
740  const LhsEval& Rs = saturatedDissolutionFactor<FluidState, LhsEval>(fluidState, oilPhaseIdx, regionIdx);
741  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
742 
743  return
744  bo*referenceDensity(oilPhaseIdx, regionIdx)
745  + Rs*bo*referenceDensity(gasPhaseIdx, regionIdx);
746  }
747 
748  // immiscible oil
749  const LhsEval Rs(0.0);
750  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
751  return referenceDensity(phaseIdx, regionIdx)*bo;
752  }
753 
754  case gasPhaseIdx: {
756  // gas containing vaporized oil and vaporized water
757  const LhsEval& Rv = saturatedDissolutionFactor<FluidState, LhsEval>(fluidState, gasPhaseIdx, regionIdx);
758  const LhsEval& Rvw = saturatedVaporizationFactor<FluidState, LhsEval>(fluidState, gasPhaseIdx, regionIdx);
759  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
760 
761  return
762  bg*referenceDensity(gasPhaseIdx, regionIdx)
763  + Rv*bg*referenceDensity(oilPhaseIdx, regionIdx)
764  + Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx) ;
765  }
766 
767  if (enableVaporizedOil()) {
768  // miscible gas
769  const LhsEval Rvw(0.0);
770  const LhsEval& Rv = saturatedDissolutionFactor<FluidState, LhsEval>(fluidState, gasPhaseIdx, regionIdx);
771  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
772 
773  return
774  bg*referenceDensity(gasPhaseIdx, regionIdx)
775  + Rv*bg*referenceDensity(oilPhaseIdx, regionIdx);
776  }
777 
778  if (enableVaporizedWater()) {
779  // gas containing vaporized water
780  const LhsEval Rv(0.0);
781  const LhsEval& Rvw = saturatedVaporizationFactor<FluidState, LhsEval>(fluidState, gasPhaseIdx, regionIdx);
782  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
783 
784  return
785  bg*referenceDensity(gasPhaseIdx, regionIdx)
786  + Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx);
787  }
788 
789  // immiscible gas
790  const LhsEval Rv(0.0);
791  const LhsEval Rvw(0.0);
792  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
793 
794  return referenceDensity(phaseIdx, regionIdx)*bg;
795 
796  }
797 
798  case waterPhaseIdx:
799  {
801  // miscible in water
802  const auto& saltConcentration = decay<LhsEval>(fluidState.saltConcentration());
803  const LhsEval& Rsw = saturatedDissolutionFactor<FluidState, LhsEval>(fluidState, waterPhaseIdx, regionIdx);
804  const LhsEval& bw = waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
805  return
806  bw*referenceDensity(waterPhaseIdx, regionIdx)
807  + Rsw*bw*referenceDensity(gasPhaseIdx, regionIdx);
808  }
809  return
810  referenceDensity(waterPhaseIdx, regionIdx)
811  *inverseFormationVolumeFactor<FluidState, LhsEval>(fluidState, waterPhaseIdx, regionIdx);
812  }
813  }
814 
815  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
816  }
817 
826  template <class FluidState, class LhsEval = typename FluidState::ValueType>
827  STATIC_OR_DEVICE LhsEval inverseFormationVolumeFactor(const FluidState& fluidState,
828  unsigned phaseIdx,
829  unsigned regionIdx) NOTHING_OR_CONST
830  {
831  OPM_TIMEBLOCK_LOCAL(inverseFormationVolumeFactor, Subsystem::PvtProps);
832  assert(phaseIdx <= numPhases);
833  assert(regionIdx <= numRegions());
834 
835  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
836  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
837 
838  switch (phaseIdx) {
839  case oilPhaseIdx: {
840  if (enableDissolvedGas()) {
841  const auto& Rs = BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
842  if (useSaturatedTables() && fluidState.saturation(gasPhaseIdx) > 0.0
843  && Rs >= (1.0 - 1e-10)*oilPvt_.saturatedGasDissolutionFactor(regionIdx, scalarValue(T), scalarValue(p)))
844  {
845  return oilPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
846  } else {
847  return oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
848  }
849  }
850 
851  const LhsEval Rs(0.0);
852  return oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
853  }
854  case gasPhaseIdx: {
856  const auto& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
857  const auto& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
858  if (useSaturatedTables() && fluidState.saturation(waterPhaseIdx) > 0.0
859  && Rvw >= (1.0 - 1e-10)*gasPvt_.saturatedWaterVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p))
860  && fluidState.saturation(oilPhaseIdx) > 0.0
861  && Rv >= (1.0 - 1e-10)*gasPvt_.saturatedOilVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
862  {
863  return gasPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
864  } else {
865  return gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
866  }
867  }
868 
869  if (enableVaporizedOil()) {
870  const auto& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
871  if (useSaturatedTables() && fluidState.saturation(oilPhaseIdx) > 0.0
872  && Rv >= (1.0 - 1e-10)*gasPvt_.saturatedOilVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
873  {
874  return gasPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
875  } else {
876  const LhsEval Rvw(0.0);
877  return gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
878  }
879  }
880 
881  if (enableVaporizedWater()) {
882  const auto& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
883  if (useSaturatedTables() && fluidState.saturation(waterPhaseIdx) > 0.0
884  && Rvw >= (1.0 - 1e-10)*gasPvt_.saturatedWaterVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
885  {
886  return gasPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
887  } else {
888  const LhsEval Rv(0.0);
889  return gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
890  }
891  }
892 
893  const LhsEval Rv(0.0);
894  const LhsEval Rvw(0.0);
895  return gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
896  }
897  case waterPhaseIdx:
898  {
899  const auto& saltConcentration = BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx);
901  const auto& Rsw = BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
902  if (useSaturatedTables() && fluidState.saturation(gasPhaseIdx) > 0.0
903  && Rsw >= (1.0 - 1e-10)*waterPvt_.saturatedGasDissolutionFactor(regionIdx, scalarValue(T), scalarValue(p), scalarValue(saltConcentration)))
904  {
905  return waterPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p, saltConcentration);
906  } else {
907  return waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
908  }
909  }
910  const LhsEval Rsw(0.0);
911  return waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
912  }
913  default: OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
914  }
915  }
916 
917  template <class FluidState, class LhsEval = typename FluidState::ValueType>
918  STATIC_OR_DEVICE std::pair<LhsEval, LhsEval>
919  inverseFormationVolumeFactorAndViscosity(const FluidState& fluidState,
920  unsigned phaseIdx,
921  unsigned regionIdx) NOTHING_OR_CONST
922  {
923  switch (phaseIdx) {
924  case oilPhaseIdx:
925  return oilPvt_.inverseFormationVolumeFactorAndViscosity(fluidState, regionIdx);
926  case gasPhaseIdx:
927  return gasPvt_.inverseFormationVolumeFactorAndViscosity(fluidState, regionIdx);
928  case waterPhaseIdx:
929  return waterPvt_.inverseFormationVolumeFactorAndViscosity(fluidState, regionIdx);
930  default:
931  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
932  }
933  }
934 
944  template <class FluidState, class LhsEval = typename FluidState::ValueType>
945  STATIC_OR_DEVICE LhsEval saturatedInverseFormationVolumeFactor(const FluidState& fluidState,
946  unsigned phaseIdx,
947  unsigned regionIdx) NOTHING_OR_CONST
948  {
949  OPM_TIMEBLOCK_LOCAL(saturatedInverseFormationVolumeFactor, Subsystem::PvtProps);
950  assert(phaseIdx <= numPhases);
951  assert(regionIdx <= numRegions());
952 
953  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
954  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
955  const auto& saltConcentration = BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx);
956 
957  switch (phaseIdx) {
958  case oilPhaseIdx: return oilPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
959  case gasPhaseIdx: return gasPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p);
960  case waterPhaseIdx: return waterPvt_.saturatedInverseFormationVolumeFactor(regionIdx, T, p, saltConcentration);
961  default: OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
962  }
963  }
964 
966  template <class FluidState, class LhsEval = typename FluidState::ValueType>
967  STATIC_OR_DEVICE LhsEval fugacityCoefficient(const FluidState& fluidState,
968  unsigned phaseIdx,
969  unsigned compIdx,
970  unsigned regionIdx) NOTHING_OR_CONST
971  {
972  assert(phaseIdx <= numPhases);
973  assert(compIdx <= numComponents);
974  assert(regionIdx <= numRegions());
975 
976  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
977  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
978 
979  // for the fugacity coefficient of the oil component in the oil phase, we use
980  // some pseudo-realistic value for the vapor pressure to ease physical
981  // interpretation of the results
982  const LhsEval phi_oO = 20e3/p;
983 
984  // for the gas component in the gas phase, assume it to be an ideal gas
985  constexpr const Scalar phi_gG = 1.0;
986 
987  // for the fugacity coefficient of the water component in the water phase, we use
988  // the same approach as for the oil component in the oil phase
989  const LhsEval phi_wW = 30e3/p;
990 
991  switch (phaseIdx) {
992  case gasPhaseIdx: // fugacity coefficients for all components in the gas phase
993  switch (compIdx) {
994  case gasCompIdx:
995  return phi_gG;
996 
997  // for the oil component, we calculate the Rv value for saturated gas and Rs
998  // for saturated oil, and compute the fugacity coefficients at the
999  // equilibrium. for this, we assume that in equilibrium the fugacities of the
1000  // oil component is the same in both phases.
1001  case oilCompIdx: {
1002  if (!enableVaporizedOil())
1003  // if there's no vaporized oil, the gas phase is assumed to be
1004  // immiscible with the oil component
1005  return phi_gG*1e6;
1006 
1007  const auto& R_vSat = gasPvt_.saturatedOilVaporizationFactor(regionIdx, T, p);
1008  const auto& X_gOSat = convertRvToXgO(R_vSat, regionIdx);
1009  const auto& x_gOSat = convertXgOToxgO(X_gOSat, regionIdx);
1010 
1011  const auto& R_sSat = oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p);
1012  const auto& X_oGSat = convertRsToXoG(R_sSat, regionIdx);
1013  const auto& x_oGSat = convertXoGToxoG(X_oGSat, regionIdx);
1014  const auto& x_oOSat = 1.0 - x_oGSat;
1015 
1016  const auto& p_o = decay<LhsEval>(fluidState.pressure(oilPhaseIdx));
1017  const auto& p_g = decay<LhsEval>(fluidState.pressure(gasPhaseIdx));
1018 
1019  return phi_oO*p_o*x_oOSat / (p_g*x_gOSat);
1020  }
1021 
1022  case waterCompIdx:
1023  // the water component is assumed to be never miscible with the gas phase
1024  return phi_gG*1e6;
1025 
1026  default:
1027  throw std::logic_error("Invalid component index "+std::to_string(compIdx));
1028  }
1029 
1030  case oilPhaseIdx: // fugacity coefficients for all components in the oil phase
1031  switch (compIdx) {
1032  case oilCompIdx:
1033  return phi_oO;
1034 
1035  // for the oil and water components, we proceed analogous to the gas and
1036  // water components in the gas phase
1037  case gasCompIdx: {
1038  if (!enableDissolvedGas())
1039  // if there's no dissolved gas, the oil phase is assumed to be
1040  // immiscible with the gas component
1041  return phi_oO*1e6;
1042 
1043  const auto& R_vSat = gasPvt_.saturatedOilVaporizationFactor(regionIdx, T, p);
1044  const auto& X_gOSat = convertRvToXgO(R_vSat, regionIdx);
1045  const auto& x_gOSat = convertXgOToxgO(X_gOSat, regionIdx);
1046  const auto& x_gGSat = 1.0 - x_gOSat;
1047 
1048  const auto& R_sSat = oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p);
1049  const auto& X_oGSat = convertRsToXoG(R_sSat, regionIdx);
1050  const auto& x_oGSat = convertXoGToxoG(X_oGSat, regionIdx);
1051 
1052  const auto& p_o = decay<LhsEval>(fluidState.pressure(oilPhaseIdx));
1053  const auto& p_g = decay<LhsEval>(fluidState.pressure(gasPhaseIdx));
1054 
1055  return phi_gG*p_g*x_gGSat / (p_o*x_oGSat);
1056  }
1057 
1058  case waterCompIdx:
1059  return phi_oO*1e6;
1060 
1061  default:
1062  throw std::logic_error("Invalid component index "+std::to_string(compIdx));
1063  }
1064 
1065  case waterPhaseIdx: // fugacity coefficients for all components in the water phase
1066  // the water phase fugacity coefficients are pretty simple: because the water
1067  // phase is assumed to consist entirely from the water component, we just
1068  // need to make sure that the fugacity coefficients for the other components
1069  // are a few orders of magnitude larger than the one of the water
1070  // component. (i.e., the affinity of the gas and oil components to the water
1071  // phase is lower by a few orders of magnitude)
1072  switch (compIdx) {
1073  case waterCompIdx: return phi_wW;
1074  case oilCompIdx: return 1.1e6*phi_wW;
1075  case gasCompIdx: return 1e6*phi_wW;
1076  default:
1077  throw std::logic_error("Invalid component index "+std::to_string(compIdx));
1078  }
1079 
1080  default:
1081  throw std::logic_error("Invalid phase index "+std::to_string(phaseIdx));
1082  }
1083 
1084  OPM_THROW(std::logic_error, "Unhandled phase or component index");
1085  }
1086 
1088  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1089  STATIC_OR_DEVICE LhsEval viscosity(const FluidState& fluidState,
1090  unsigned phaseIdx,
1091  unsigned regionIdx) NOTHING_OR_CONST
1092  {
1093  OPM_TIMEBLOCK_LOCAL(viscosity, Subsystem::PvtProps);
1094  assert(phaseIdx <= numPhases);
1095  assert(regionIdx <= numRegions());
1096 
1097  const LhsEval& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1098  const LhsEval& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1099 
1100  switch (phaseIdx) {
1101  case oilPhaseIdx: {
1102  if (enableDissolvedGas()) {
1103  const auto& Rs = BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1104  if (useSaturatedTables() && fluidState.saturation(gasPhaseIdx) > 0.0
1105  && Rs >= (1.0 - 1e-10)*oilPvt_.saturatedGasDissolutionFactor(regionIdx, scalarValue(T), scalarValue(p)))
1106  {
1107  return oilPvt_.saturatedViscosity(regionIdx, T, p);
1108  } else {
1109  return oilPvt_.viscosity(regionIdx, T, p, Rs);
1110  }
1111  }
1112 
1113  const LhsEval Rs(0.0);
1114  return oilPvt_.viscosity(regionIdx, T, p, Rs);
1115  }
1116 
1117  case gasPhaseIdx: {
1119  const auto& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1120  const auto& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1121  if (useSaturatedTables() && fluidState.saturation(waterPhaseIdx) > 0.0
1122  && Rvw >= (1.0 - 1e-10)*gasPvt_.saturatedWaterVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p))
1123  && fluidState.saturation(oilPhaseIdx) > 0.0
1124  && Rv >= (1.0 - 1e-10)*gasPvt_.saturatedOilVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
1125  {
1126  return gasPvt_.saturatedViscosity(regionIdx, T, p);
1127  } else {
1128  return gasPvt_.viscosity(regionIdx, T, p, Rv, Rvw);
1129  }
1130  }
1131  if (enableVaporizedOil()) {
1132  const auto& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1133  if (useSaturatedTables() && fluidState.saturation(oilPhaseIdx) > 0.0
1134  && Rv >= (1.0 - 1e-10)*gasPvt_.saturatedOilVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
1135  {
1136  return gasPvt_.saturatedViscosity(regionIdx, T, p);
1137  } else {
1138  const LhsEval Rvw(0.0);
1139  return gasPvt_.viscosity(regionIdx, T, p, Rv, Rvw);
1140  }
1141  }
1142  if (enableVaporizedWater()) {
1143  const auto& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1144  if (useSaturatedTables() && fluidState.saturation(waterPhaseIdx) > 0.0
1145  && Rvw >= (1.0 - 1e-10)*gasPvt_.saturatedWaterVaporizationFactor(regionIdx, scalarValue(T), scalarValue(p)))
1146  {
1147  return gasPvt_.saturatedViscosity(regionIdx, T, p);
1148  } else {
1149  const LhsEval Rv(0.0);
1150  return gasPvt_.viscosity(regionIdx, T, p, Rv, Rvw);
1151  }
1152  }
1153 
1154  const LhsEval Rv(0.0);
1155  const LhsEval Rvw(0.0);
1156  return gasPvt_.viscosity(regionIdx, T, p, Rv, Rvw);
1157  }
1158 
1159  case waterPhaseIdx:
1160  {
1161  const LhsEval& saltConcentration = BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx);
1162  if (enableDissolvedGasInWater()) {
1163  const auto& Rsw = BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1164  if (useSaturatedTables() && fluidState.saturation(gasPhaseIdx) > 0.0
1165  && Rsw >= (1.0 - 1e-10)*waterPvt_.saturatedGasDissolutionFactor(regionIdx, scalarValue(T), scalarValue(p), scalarValue(saltConcentration)))
1166  {
1167  return waterPvt_.saturatedViscosity(regionIdx, T, p, saltConcentration);
1168  } else {
1169  return waterPvt_.viscosity(regionIdx, T, p, Rsw, saltConcentration);
1170  }
1171  }
1172  const LhsEval Rsw(0.0);
1173  return waterPvt_.viscosity(regionIdx, T, p, Rsw, saltConcentration);
1174  }
1175  }
1176 
1177  OPM_THROW(std::logic_error, "Unhandled phase index "+std::to_string(phaseIdx));
1178  }
1179 
1180  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1181  STATIC_OR_DEVICE LhsEval internalEnergy(const FluidState& fluidState,
1182  const unsigned phaseIdx,
1183  const unsigned regionIdx) NOTHING_OR_CONST
1184  {
1185  const auto p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1186  const auto T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1187 
1188  switch (phaseIdx) {
1189  case oilPhaseIdx:
1190  if (!oilPvt_.mixingEnergy()) {
1191  return oilPvt_.internalEnergy
1192  (regionIdx, T, p,
1193  BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1194  }
1195  break;
1196 
1197  case waterPhaseIdx:
1198  if (!waterPvt_.mixingEnergy()) {
1199  return waterPvt_.internalEnergy
1200  (regionIdx, T, p,
1201  BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1202  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1203  }
1204  break;
1205 
1206  case gasPhaseIdx:
1207  if (!gasPvt_.mixingEnergy()) {
1208  return gasPvt_.internalEnergy
1209  (regionIdx, T, p,
1210  BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1211  BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1212  }
1213  break;
1214 
1215  default:
1216  OPM_THROW(std::logic_error,
1217  "Phase index " + std::to_string(phaseIdx) +
1218  " does not support internal energy");
1219  }
1220 
1221  return internalMixingTotalEnergy<FluidState,LhsEval>(fluidState, phaseIdx, regionIdx)
1222  / density<FluidState,LhsEval>(fluidState, phaseIdx, regionIdx);
1223  }
1224 
1225 
1226  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1227  STATIC_OR_DEVICE LhsEval internalMixingTotalEnergy(const FluidState& fluidState,
1228  unsigned phaseIdx,
1229  unsigned regionIdx) NOTHING_OR_CONST
1230  {
1231  assert(phaseIdx <= numPhases);
1232  assert(regionIdx <= numRegions());
1233  const LhsEval& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1234  const LhsEval& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1235  const LhsEval& saltConcentration = BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx);
1236  // to avoid putting all thermal into the interface of the multiplexer
1237  switch (phaseIdx) {
1238  case oilPhaseIdx: {
1239  auto oilEnergy = oilPvt_.internalEnergy(regionIdx, T, p,
1240  BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1241  assert(oilPvt_.mixingEnergy());
1242  //mixing energy adsed
1243  if (enableDissolvedGas()) {
1244  // miscible oil
1245  const LhsEval& Rs = BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1246  const LhsEval& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
1247  const auto& gasEnergy =
1248  gasPvt_.internalEnergy(regionIdx, T, p,
1249  BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1250  BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1251  const auto hVapG = gasPvt_.hVap(regionIdx);// pressure correction ? assume equal to energy change
1252  return
1253  oilEnergy*bo*referenceDensity(oilPhaseIdx, regionIdx)
1254  + (gasEnergy-hVapG)*Rs*bo*referenceDensity(gasPhaseIdx, regionIdx);
1255  }
1256 
1257  // immiscible oil
1258  const LhsEval Rs(0.0);
1259  const auto& bo = oilPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rs);
1260 
1261  return oilEnergy*referenceDensity(phaseIdx, regionIdx)*bo;
1262  }
1263 
1264  case gasPhaseIdx: {
1265  const auto& gasEnergy =
1266  gasPvt_.internalEnergy(regionIdx, T, p,
1267  BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1268  BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1269  assert(gasPvt_.mixingEnergy());
1271  const auto& oilEnergy =
1272  oilPvt_.internalEnergy(regionIdx, T, p,
1273  BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1274  const auto waterEnergy =
1275  waterPvt_.internalEnergy(regionIdx, T, p,
1276  BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1277  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1278  // gas containing vaporized oil and vaporized water
1279  const LhsEval& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1280  const LhsEval& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1281  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
1282  const auto hVapO = oilPvt_.hVap(regionIdx);
1283  const auto hVapW = waterPvt_.hVap(regionIdx);
1284  return
1285  gasEnergy*bg*referenceDensity(gasPhaseIdx, regionIdx)
1286  + (oilEnergy+hVapO)*Rv*bg*referenceDensity(oilPhaseIdx, regionIdx)
1287  + (waterEnergy+hVapW)*Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx);
1288  }
1289  if (enableVaporizedOil()) {
1290  const auto& oilEnergy =
1291  oilPvt_.internalEnergy(regionIdx, T, p,
1292  BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1293  // miscible gas
1294  const LhsEval Rvw(0.0);
1295  const LhsEval& Rv = BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1296  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
1297  const auto hVapO = oilPvt_.hVap(regionIdx);
1298  return
1299  gasEnergy*bg*referenceDensity(gasPhaseIdx, regionIdx)
1300  + (oilEnergy+hVapO)*Rv*bg*referenceDensity(oilPhaseIdx, regionIdx);
1301  }
1302  if (enableVaporizedWater()) {
1303  // gas containing vaporized water
1304  const LhsEval Rv(0.0);
1305  const LhsEval& Rvw = BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx);
1306  const LhsEval& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
1307  const auto waterEnergy =
1308  waterPvt_.internalEnergy(regionIdx, T, p,
1309  BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1310  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1311  const auto hVapW = waterPvt_.hVap(regionIdx);
1312  return
1313  gasEnergy*bg*referenceDensity(gasPhaseIdx, regionIdx)
1314  + (waterEnergy+hVapW)*Rvw*bg*referenceDensity(waterPhaseIdx, regionIdx);
1315  }
1316 
1317  // immiscible gas
1318  const LhsEval Rv(0.0);
1319  const LhsEval Rvw(0.0);
1320  const auto& bg = gasPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rv, Rvw);
1321  return gasEnergy*bg*referenceDensity(phaseIdx, regionIdx);
1322  }
1323 
1324  case waterPhaseIdx:
1325  const auto waterEnergy =
1326  waterPvt_.internalEnergy(regionIdx, T, p,
1327  BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1328  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1329  assert(waterPvt_.mixingEnergy());
1330  if (enableDissolvedGasInWater()) {
1331  const auto& gasEnergy =
1332  gasPvt_.internalEnergy(regionIdx, T, p,
1333  BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1334  BlackOil::template getRvw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1335  // gas miscible in water
1336  const LhsEval& Rsw = saturatedDissolutionFactor<FluidState, LhsEval>(fluidState, waterPhaseIdx, regionIdx);
1337  const LhsEval& bw = waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
1338  return
1339  waterEnergy*bw*referenceDensity(waterPhaseIdx, regionIdx)
1340  + gasEnergy*Rsw*bw*referenceDensity(gasPhaseIdx, regionIdx);
1341  }
1342  const LhsEval Rsw(0.0);
1343  return
1344  waterEnergy*referenceDensity(waterPhaseIdx, regionIdx)
1345  * waterPvt_.inverseFormationVolumeFactor(regionIdx, T, p, Rsw, saltConcentration);
1346  }
1347  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1348  }
1349 
1350 
1351 
1353  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1354  STATIC_OR_DEVICE LhsEval enthalpy(const FluidState& fluidState,
1355  unsigned phaseIdx,
1356  unsigned regionIdx) NOTHING_OR_CONST
1357  {
1358  // should preferably not be used values should be taken from intensive quantities fluid state.
1359  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1360  auto energy = internalEnergy<FluidState, LhsEval>(fluidState, phaseIdx, regionIdx);
1361  if(!enthalpy_eq_energy_){
1362  // used for simplified models
1363  energy += p/density<FluidState, LhsEval>(fluidState, phaseIdx, regionIdx);
1364  }
1365  return energy;
1366  }
1367 
1374  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1375  STATIC_OR_DEVICE LhsEval saturatedVaporizationFactor(const FluidState& fluidState,
1376  unsigned phaseIdx,
1377  unsigned regionIdx) NOTHING_OR_CONST
1378  {
1379  assert(phaseIdx <= numPhases);
1380  assert(regionIdx <= numRegions());
1381 
1382  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1383  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1384  const auto& saltConcentration = decay<LhsEval>(fluidState.saltConcentration());
1385 
1386  switch (phaseIdx) {
1387  case oilPhaseIdx: return 0.0;
1388  case gasPhaseIdx: return gasPvt_.saturatedWaterVaporizationFactor(regionIdx, T, p, saltConcentration);
1389  case waterPhaseIdx: return 0.0;
1390  default:
1391  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1392  }
1393  }
1394 
1401  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1402  STATIC_OR_DEVICE LhsEval saturatedDissolutionFactor(const FluidState& fluidState,
1403  unsigned phaseIdx,
1404  unsigned regionIdx,
1405  const LhsEval& maxOilSaturation) NOTHING_OR_CONST
1406  {
1407  OPM_TIMEBLOCK_LOCAL(saturatedDissolutionFactor, Subsystem::PvtProps);
1408  assert(phaseIdx <= numPhases);
1409  assert(regionIdx <= numRegions());
1410 
1411  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1412  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1413  const auto& So = (phaseIdx == waterPhaseIdx) ? 0 : decay<LhsEval>(fluidState.saturation(oilPhaseIdx));
1414 
1415  switch (phaseIdx) {
1416  case oilPhaseIdx: return oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p, So, maxOilSaturation);
1417  case gasPhaseIdx: return gasPvt_.saturatedOilVaporizationFactor(regionIdx, T, p, So, maxOilSaturation);
1418  case waterPhaseIdx: return waterPvt_.saturatedGasDissolutionFactor(regionIdx, T, p,
1419  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1420  default: OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1421  }
1422  }
1423 
1432  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1433  STATIC_OR_DEVICE LhsEval saturatedDissolutionFactor(const FluidState& fluidState,
1434  unsigned phaseIdx,
1435  unsigned regionIdx) NOTHING_OR_CONST
1436  {
1437  OPM_TIMEBLOCK_LOCAL(saturatedDissolutionFactor, Subsystem::PvtProps);
1438  assert(phaseIdx <= numPhases);
1439  assert(regionIdx <= numRegions());
1440 
1441  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1442  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1443 
1444  switch (phaseIdx) {
1445  case oilPhaseIdx: return oilPvt_.saturatedGasDissolutionFactor(regionIdx, T, p);
1446  case gasPhaseIdx: return gasPvt_.saturatedOilVaporizationFactor(regionIdx, T, p);
1447  case waterPhaseIdx: return waterPvt_.saturatedGasDissolutionFactor(regionIdx, T, p,
1448  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1449  default:
1450  OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1451  }
1452  }
1453 
1457  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1458  STATIC_OR_DEVICE LhsEval bubblePointPressure(const FluidState& fluidState,
1459  unsigned regionIdx) NOTHING_OR_CONST
1460  {
1461  return saturationPressure(fluidState, oilPhaseIdx, regionIdx);
1462  }
1463 
1464 
1468  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1469  STATIC_OR_DEVICE LhsEval dewPointPressure(const FluidState& fluidState,
1470  unsigned regionIdx) NOTHING_OR_CONST
1471  {
1472  return saturationPressure(fluidState, gasPhaseIdx, regionIdx);
1473  }
1474 
1485  template <class FluidState, class LhsEval = typename FluidState::ValueType>
1486  STATIC_OR_DEVICE LhsEval saturationPressure(const FluidState& fluidState,
1487  unsigned phaseIdx,
1488  unsigned regionIdx) NOTHING_OR_CONST
1489  {
1490  assert(phaseIdx <= numPhases);
1491  assert(regionIdx <= numRegions());
1492 
1493  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1494 
1495  switch (phaseIdx) {
1496  case oilPhaseIdx: return oilPvt_.saturationPressure(regionIdx, T, BlackOil::template getRs_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1497  case gasPhaseIdx: return gasPvt_.saturationPressure(regionIdx, T, BlackOil::template getRv_<ThisType, FluidState, LhsEval>(fluidState, regionIdx));
1498  case waterPhaseIdx: return waterPvt_.saturationPressure(regionIdx, T,
1499  BlackOil::template getRsw_<ThisType, FluidState, LhsEval>(fluidState, regionIdx),
1500  BlackOil::template getSaltConcentration_<FluidState, LhsEval>(fluidState, regionIdx));
1501  default: OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1502  }
1503  }
1504 
1505  /****************************************
1506  * Auxiliary and convenience methods for the black-oil model
1507  ****************************************/
1512  template <class LhsEval>
1513  STATIC_OR_DEVICE LhsEval convertXoGToRs(const LhsEval& XoG, unsigned regionIdx) NOTHING_OR_CONST
1514  {
1515  Scalar rho_oRef = referenceDensity_[regionIdx][oilPhaseIdx];
1516  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1517 
1518  return XoG/(1.0 - XoG)*(rho_oRef/rho_gRef);
1519  }
1520 
1525  template <class LhsEval>
1526  STATIC_OR_DEVICE LhsEval convertXwGToRsw(const LhsEval& XwG, unsigned regionIdx) NOTHING_OR_CONST
1527  {
1528  Scalar rho_wRef = referenceDensity_[regionIdx][waterPhaseIdx];
1529  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1530 
1531  return XwG/(1.0 - XwG)*(rho_wRef/rho_gRef);
1532  }
1533 
1538  template <class LhsEval>
1539  STATIC_OR_DEVICE LhsEval convertXgOToRv(const LhsEval& XgO, unsigned regionIdx) NOTHING_OR_CONST
1540  {
1541  Scalar rho_oRef = referenceDensity_[regionIdx][oilPhaseIdx];
1542  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1543 
1544  return XgO/(1.0 - XgO)*(rho_gRef/rho_oRef);
1545  }
1546 
1551  template <class LhsEval>
1552  STATIC_OR_DEVICE LhsEval convertXgWToRvw(const LhsEval& XgW, unsigned regionIdx) NOTHING_OR_CONST
1553  {
1554  Scalar rho_wRef = referenceDensity_[regionIdx][waterPhaseIdx];
1555  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1556 
1557  return XgW/(1.0 - XgW)*(rho_gRef/rho_wRef);
1558  }
1559 
1560 
1565  template <class LhsEval>
1566  STATIC_OR_DEVICE LhsEval convertRsToXoG(const LhsEval& Rs, unsigned regionIdx) NOTHING_OR_CONST
1567  {
1568  Scalar rho_oRef = referenceDensity_[regionIdx][oilPhaseIdx];
1569  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1570 
1571  const LhsEval& rho_oG = Rs*rho_gRef;
1572  return rho_oG/(rho_oRef + rho_oG);
1573  }
1574 
1579  template <class LhsEval>
1580  STATIC_OR_DEVICE LhsEval convertRswToXwG(const LhsEval& Rsw, unsigned regionIdx) NOTHING_OR_CONST
1581  {
1582  Scalar rho_wRef = referenceDensity_[regionIdx][waterPhaseIdx];
1583  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1584 
1585  const LhsEval& rho_wG = Rsw*rho_gRef;
1586  return rho_wG/(rho_wRef + rho_wG);
1587  }
1588 
1593  template <class LhsEval>
1594  STATIC_OR_DEVICE LhsEval convertRvToXgO(const LhsEval& Rv, unsigned regionIdx) NOTHING_OR_CONST
1595  {
1596  Scalar rho_oRef = referenceDensity_[regionIdx][oilPhaseIdx];
1597  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1598 
1599  const LhsEval& rho_gO = Rv*rho_oRef;
1600  return rho_gO/(rho_gRef + rho_gO);
1601  }
1602 
1607  template <class LhsEval>
1608  STATIC_OR_DEVICE LhsEval convertRvwToXgW(const LhsEval& Rvw, unsigned regionIdx) NOTHING_OR_CONST
1609  {
1610  Scalar rho_wRef = referenceDensity_[regionIdx][waterPhaseIdx];
1611  Scalar rho_gRef = referenceDensity_[regionIdx][gasPhaseIdx];
1612 
1613  const LhsEval& rho_gW = Rvw*rho_wRef;
1614  return rho_gW/(rho_gRef + rho_gW);
1615  }
1616 
1620  template <class LhsEval>
1621  STATIC_OR_DEVICE LhsEval convertXgWToxgW(const LhsEval& XgW, unsigned regionIdx) NOTHING_OR_CONST
1622  {
1623  Scalar MW = molarMass_[regionIdx][waterCompIdx];
1624  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1625 
1626  return XgW*MG / (MW*(1 - XgW) + XgW*MG);
1627  }
1628 
1632  template <class LhsEval>
1633  STATIC_OR_DEVICE LhsEval convertXwGToxwG(const LhsEval& XwG, unsigned regionIdx) NOTHING_OR_CONST
1634  {
1635  Scalar MW = molarMass_[regionIdx][waterCompIdx];
1636  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1637 
1638  return XwG*MW / (MG*(1 - XwG) + XwG*MW);
1639  }
1640 
1644  template <class LhsEval>
1645  STATIC_OR_DEVICE LhsEval convertXoGToxoG(const LhsEval& XoG, unsigned regionIdx) NOTHING_OR_CONST
1646  {
1647  Scalar MO = molarMass_[regionIdx][oilCompIdx];
1648  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1649 
1650  return XoG*MO / (MG*(1 - XoG) + XoG*MO);
1651  }
1652 
1656  template <class LhsEval>
1657  STATIC_OR_DEVICE LhsEval convertxoGToXoG(const LhsEval& xoG, unsigned regionIdx) NOTHING_OR_CONST
1658  {
1659  Scalar MO = molarMass_[regionIdx][oilCompIdx];
1660  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1661 
1662  return xoG*MG / (xoG*(MG - MO) + MO);
1663  }
1664 
1668  template <class LhsEval>
1669  STATIC_OR_DEVICE LhsEval convertXgOToxgO(const LhsEval& XgO, unsigned regionIdx) NOTHING_OR_CONST
1670  {
1671  Scalar MO = molarMass_[regionIdx][oilCompIdx];
1672  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1673 
1674  return XgO*MG / (MO*(1 - XgO) + XgO*MG);
1675  }
1676 
1680  template <class LhsEval>
1681  STATIC_OR_DEVICE LhsEval convertxgOToXgO(const LhsEval& xgO, unsigned regionIdx) NOTHING_OR_CONST
1682  {
1683  Scalar MO = molarMass_[regionIdx][oilCompIdx];
1684  Scalar MG = molarMass_[regionIdx][gasCompIdx];
1685 
1686  return xgO*MO / (xgO*(MO - MG) + MG);
1687  }
1688 
1696  STATIC_OR_DEVICE const GasPvt& gasPvt() NOTHING_OR_CONST
1697  { return gasPvt_; }
1698 
1706  STATIC_OR_DEVICE const OilPvt& oilPvt() NOTHING_OR_CONST
1707  { return oilPvt_; }
1708 
1716  STATIC_OR_DEVICE const WaterPvt& waterPvt() NOTHING_OR_CONST
1717  { return waterPvt_; }
1718 
1724  STATIC_OR_DEVICE Scalar reservoirTemperature(unsigned = 0) NOTHING_OR_CONST
1725  { return reservoirTemperature_; }
1726 
1732  STATIC_OR_DEVICE void setReservoirTemperature(Scalar value) NOTHING_OR_CONST
1733  { reservoirTemperature_ = value; }
1734 
1735  STATIC_OR_DEVICE short activeToCanonicalPhaseIdx(unsigned activePhaseIdx) NOTHING_OR_CONST;
1736 
1737  STATIC_OR_DEVICE short canonicalToActivePhaseIdx(unsigned phaseIdx) NOTHING_OR_CONST;
1738 
1739  STATIC_OR_DEVICE short activeToCanonicalCompIdx(unsigned activeCompIdx) NOTHING_OR_CONST;
1740 
1741  STATIC_OR_DEVICE short canonicalToActiveCompIdx(unsigned compIdx) NOTHING_OR_CONST;
1742 
1743  STATIC_OR_DEVICE short activePhaseToActiveCompIdx(unsigned activePhaseIdx) NOTHING_OR_CONST;
1744 
1745  STATIC_OR_DEVICE short activeCompToActivePhaseIdx(unsigned activeCompIdx) NOTHING_OR_CONST;
1746 
1748  STATIC_OR_DEVICE Scalar diffusionCoefficient(unsigned compIdx, unsigned phaseIdx, unsigned regionIdx = 0) NOTHING_OR_CONST
1749  { return diffusionCoefficients_[regionIdx][numPhases*compIdx + phaseIdx]; }
1750 
1752  STATIC_OR_DEVICE void setDiffusionCoefficient(Scalar coefficient, unsigned compIdx, unsigned phaseIdx, unsigned regionIdx = 0) NOTHING_OR_CONST
1753  { diffusionCoefficients_[regionIdx][numPhases*compIdx + phaseIdx] = coefficient ; }
1754 
1758  template <class FluidState, class LhsEval = typename FluidState::ValueType, class ParamCacheEval = LhsEval>
1759  STATIC_OR_DEVICE LhsEval diffusionCoefficient(const FluidState& fluidState,
1760  const ParameterCache<ParamCacheEval>& paramCache,
1761  unsigned phaseIdx,
1762  unsigned compIdx) NOTHING_OR_CONST
1763  {
1764  // diffusion is disabled by the user
1765  if(!enableDiffusion())
1766  return 0.0;
1767 
1768  // diffusion coefficients are set, and we use them
1769  if(!diffusionCoefficients_.empty()) {
1770  return diffusionCoefficient(compIdx, phaseIdx, paramCache.regionIndex());
1771  }
1772 
1773  const auto& p = decay<LhsEval>(fluidState.pressure(phaseIdx));
1774  const auto& T = decay<LhsEval>(fluidState.temperature(phaseIdx));
1775  const unsigned regionIdx = paramCache.regionIndex();
1776 
1777  switch (phaseIdx) {
1778  case oilPhaseIdx: return oilPvt().diffusionCoefficient(T, p, compIdx);
1779  case gasPhaseIdx: return gasPvt().diffusionCoefficient(T, p, compIdx);
1780  case waterPhaseIdx: return waterPvt().diffusionCoefficient(T, p, compIdx, regionIdx);
1781  default: OPM_THROW(std::logic_error, "Unhandled phase index " + std::to_string(phaseIdx));
1782  }
1783  }
1784  STATIC_OR_DEVICE void setEnergyEqualEnthalpy(bool enthalpy_eq_energy){
1785  enthalpy_eq_energy_ = enthalpy_eq_energy;
1786  }
1787 
1788  STATIC_OR_DEVICE bool enthalpyEqualEnergy() NOTHING_OR_CONST{
1789  return enthalpy_eq_energy_;
1790  }
1791 
1792 private:
1793  STATIC_OR_NOTHING void resizeArrays_(std::size_t numRegions);
1794 
1795  STATIC_OR_NOTHING Scalar reservoirTemperature_;
1796 
1797  STATIC_OR_NOTHING GasPvt gasPvt_;
1798  STATIC_OR_NOTHING OilPvt oilPvt_;
1799  STATIC_OR_NOTHING WaterPvt waterPvt_;
1800 
1801  STATIC_OR_NOTHING bool enableDissolvedGas_;
1802  STATIC_OR_NOTHING bool enableDissolvedGasInWater_;
1803  STATIC_OR_NOTHING bool enableVaporizedOil_;
1804  STATIC_OR_NOTHING bool enableVaporizedWater_;
1805  STATIC_OR_NOTHING bool enableConstantRs_;
1806  STATIC_OR_NOTHING bool enableDiffusion_;
1807 
1808  // HACK for GCC 4.4: the array size has to be specified using the literal value '3'
1809  // here, because GCC 4.4 seems to be unable to determine the number of phases from
1810  // the BlackOil fluid system in the attribute declaration below...
1811  STATIC_OR_NOTHING Storage<std::array<Scalar, /*numPhases=*/3> > referenceDensity_;
1812  STATIC_OR_NOTHING Storage<std::array<Scalar, /*numComponents=*/3> > molarMass_;
1813  STATIC_OR_NOTHING Storage<std::array<Scalar, /*numComponents=*/3 * /*numPhases=*/3> > diffusionCoefficients_;
1814 
1815  STATIC_OR_NOTHING PhaseUsageInfo<IndexTraits> phaseUsageInfo_;
1816 
1817  STATIC_OR_NOTHING bool isInitialized_;
1818  STATIC_OR_NOTHING bool useSaturatedTables_;
1819  STATIC_OR_NOTHING bool enthalpy_eq_energy_;
1820 
1821  #ifndef COMPILING_STATIC_FLUID_SYSTEM
1822  template<template<typename> typename StorageT>
1823  explicit FLUIDSYSTEM_CLASSNAME(const FLUIDSYSTEM_CLASSNAME_STATIC<Scalar, IndexTraits, StorageT>& other)
1826  , reservoirTemperature_(other.reservoirTemperature_)
1827  , gasPvt_(other.gasPvt_)
1828  , oilPvt_(other.oilPvt_)
1829  , waterPvt_(other.waterPvt_)
1830  , enableDissolvedGas_(other.enableDissolvedGas_)
1831  , enableDissolvedGasInWater_(other.enableDissolvedGasInWater_)
1832  , enableVaporizedOil_(other.enableVaporizedOil_)
1833  , enableVaporizedWater_(other.enableVaporizedWater_)
1834  , enableConstantRs_(other.enableConstantRs_)
1835  , enableDiffusion_(other.enableDiffusion_)
1836  , referenceDensity_(StorageT<typename decltype(referenceDensity_)::value_type>(other.referenceDensity_))
1837  , molarMass_(StorageT<typename decltype(molarMass_)::value_type>(other.molarMass_))
1838  , diffusionCoefficients_(StorageT<typename decltype(diffusionCoefficients_)::value_type>(other.diffusionCoefficients_))
1839  , phaseUsageInfo_(other.phaseUsageInfo_)
1840  , isInitialized_(other.isInitialized_)
1841  , useSaturatedTables_(other.useSaturatedTables_)
1842  , enthalpy_eq_energy_(other.enthalpy_eq_energy_)
1843  {
1844  OPM_ERROR_IF(!other.isInitialized(), "The fluid system must be initialized before it can be copied.");
1845  }
1846 
1847  template<class ScalarT, class IndexTraitsT, template<typename> typename StorageT>
1848  friend class FLUIDSYSTEM_CLASSNAME_STATIC;
1849  #else
1850  template<class ScalarT, class IndexTraitsT, template<typename> typename StorageT>
1851  friend class FLUIDSYSTEM_CLASSNAME_NONSTATIC;
1852  #endif
1853 };
1854 
1855 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1857 initBegin(std::size_t numPvtRegions)
1858 {
1859  isInitialized_ = false;
1860  useSaturatedTables_ = true;
1861 
1862  enableDissolvedGas_ = true;
1863  enableDissolvedGasInWater_ = false;
1864  enableVaporizedOil_ = false;
1865  enableVaporizedWater_ = false;
1866  enableConstantRs_ = false;
1867  enableDiffusion_ = false;
1868 
1869  surfaceTemperature = 273.15 + 15.56; // [K]
1870  surfacePressure = 1.01325e5; // [Pa]
1871  setReservoirTemperature(surfaceTemperature);
1872 
1873  phaseUsageInfo_.initFromPhases(Phases{true, true, true});
1874 
1875  resizeArrays_(numPvtRegions);
1876 }
1877 
1878 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1881  Scalar rhoWater,
1882  Scalar rhoGas,
1883  unsigned regionIdx)
1884 {
1885  referenceDensity_[regionIdx][oilPhaseIdx] = rhoOil;
1886  referenceDensity_[regionIdx][waterPhaseIdx] = rhoWater;
1887  referenceDensity_[regionIdx][gasPhaseIdx] = rhoGas;
1888 }
1889 
1890 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1892 {
1893  // calculate the final 2D functions which are used for interpolation.
1894  const std::size_t num_regions = molarMass_.size();
1895  for (unsigned regionIdx = 0; regionIdx < num_regions; ++regionIdx) {
1896  // calculate molar masses
1897 
1898  // water is simple: 18 g/mol
1899  molarMass_[regionIdx][waterCompIdx] = 18e-3;
1900 
1901  if (phaseIsActive(gasPhaseIdx)) {
1902  // for gas, we take the density at standard conditions and assume it to be ideal
1903  Scalar p = surfacePressure;
1904  Scalar T = surfaceTemperature;
1905  Scalar rho_g = referenceDensity_[/*regionIdx=*/0][gasPhaseIdx];
1906  molarMass_[regionIdx][gasCompIdx] = Constants<Scalar>::R*T*rho_g / p;
1907  }
1908  else
1909  // hydrogen gas. we just set this do avoid NaNs later
1910  molarMass_[regionIdx][gasCompIdx] = 2e-3;
1911 
1912  // finally, for oil phase, we take the molar mass from the spe9 paper
1913  molarMass_[regionIdx][oilCompIdx] = 175e-3; // kg/mol
1914  }
1915 
1916  isInitialized_ = true;
1917 }
1918 
1919 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1921 phaseName(unsigned phaseIdx) NOTHING_OR_CONST
1922 {
1923  switch (phaseIdx) {
1924  case waterPhaseIdx:
1925  return "water";
1926  case oilPhaseIdx:
1927  return "oil";
1928  case gasPhaseIdx:
1929  return "gas";
1930 
1931  default:
1932  OPM_THROW(std::logic_error, "Phase index " + std::to_string(phaseIdx) + " is unknown");
1933  }
1934 }
1935 
1936 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1937 NOTHING_OR_DEVICE unsigned FLUIDSYSTEM_CLASSNAME<Scalar,IndexTraits,Storage>::
1938 solventComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST
1939 {
1940  switch (phaseIdx) {
1941  case waterPhaseIdx:
1942  return waterCompIdx;
1943  case oilPhaseIdx:
1944  return oilCompIdx;
1945  case gasPhaseIdx:
1946  return gasCompIdx;
1947 
1948  default:
1949  OPM_THROW(std::logic_error, "Phase index " + std::to_string(phaseIdx) + " is unknown");
1950  }
1951 }
1952 
1953 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1954 NOTHING_OR_DEVICE unsigned FLUIDSYSTEM_CLASSNAME<Scalar,IndexTraits,Storage>::
1955 soluteComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST
1956 {
1957  switch (phaseIdx) {
1958  case waterPhaseIdx:
1959  if (enableDissolvedGasInWater())
1960  return gasCompIdx;
1961  OPM_THROW(std::logic_error, "The water phase does not have any solutes in the black oil model!");
1962  case oilPhaseIdx:
1963  return gasCompIdx;
1964  case gasPhaseIdx:
1965  if (enableVaporizedWater()) {
1966  return waterCompIdx;
1967  }
1968  return oilCompIdx;
1969 
1970  default:
1971  OPM_THROW(std::logic_error, "Phase index " + std::to_string(phaseIdx) + " is unknown");
1972  }
1973 }
1974 
1975 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1977 componentName(unsigned compIdx) NOTHING_OR_CONST
1978 {
1979  switch (compIdx) {
1980  case waterCompIdx:
1981  return "Water";
1982  case oilCompIdx:
1983  return "Oil";
1984  case gasCompIdx:
1985  return "Gas";
1986 
1987  default:
1988  OPM_THROW(std::logic_error, "Component index " + std::to_string(compIdx) + " is unknown");
1989  }
1990 }
1991 
1992 template <class Scalar, class IndexTraits, template<typename> typename Storage>
1994 activeToCanonicalPhaseIdx(unsigned activePhaseIdx) NOTHING_OR_CONST
1995 {
1996  return phaseUsageInfo_.activeToCanonicalPhaseIdx(activePhaseIdx);
1997 }
1998 
1999 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2000 NOTHING_OR_DEVICE short FLUIDSYSTEM_CLASSNAME<Scalar,IndexTraits, Storage>::
2001 canonicalToActivePhaseIdx(unsigned phaseIdx) NOTHING_OR_CONST
2002 {
2003  return phaseUsageInfo_.canonicalToActivePhaseIdx(phaseIdx);
2004 }
2005 
2006 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2007 NOTHING_OR_DEVICE short FLUIDSYSTEM_CLASSNAME<Scalar,IndexTraits, Storage>::
2008 activeToCanonicalCompIdx(unsigned activeCompIdx) NOTHING_OR_CONST
2009 {
2010  return phaseUsageInfo_.activeToCanonicalCompIdx(activeCompIdx);
2011 }
2012 
2013 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2014 NOTHING_OR_DEVICE short FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage>::
2015 canonicalToActiveCompIdx(unsigned compIdx) NOTHING_OR_CONST
2016 {
2017  return phaseUsageInfo_.canonicalToActiveCompIdx(compIdx);
2018 }
2019 
2020 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2021 NOTHING_OR_DEVICE short FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage>::
2022 activePhaseToActiveCompIdx(unsigned activePhaseIdx) NOTHING_OR_CONST
2023 {
2024  return phaseUsageInfo_.activePhaseToActiveCompIdx(activePhaseIdx);
2025 }
2026 
2027 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2028 NOTHING_OR_DEVICE short FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, Storage>::
2029 activeCompToActivePhaseIdx(unsigned activeCompIdx) NOTHING_OR_CONST
2030 {
2031  return phaseUsageInfo_.activeCompToActivePhaseIdx(activeCompIdx);
2032 }
2033 
2034 template <class Scalar, class IndexTraits, template<typename> typename Storage>
2035 void FLUIDSYSTEM_CLASSNAME<Scalar,IndexTraits,Storage>::
2036 resizeArrays_(std::size_t numRegions)
2037 {
2038  molarMass_.resize(numRegions);
2039  referenceDensity_.resize(numRegions);
2040 }
2041 
2042 #ifdef COMPILING_STATIC_FLUID_SYSTEM
2043 template <typename T> using BOFS = FLUIDSYSTEM_CLASSNAME<T, BlackOilDefaultFluidSystemIndices, VectorWithDefaultAllocator>;
2044 
2045 #define DECLARE_INSTANCE(T) \
2046 template<> PhaseUsageInfo<BlackOilDefaultFluidSystemIndices> BOFS<T>::phaseUsageInfo_;\
2047 template<> T BOFS<T>::surfaceTemperature; \
2048 template<> T BOFS<T>::surfacePressure; \
2049 template<> T BOFS<T>::reservoirTemperature_; \
2050 template<> bool BOFS<T>::enableDissolvedGas_; \
2051 template<> bool BOFS<T>::enableDissolvedGasInWater_; \
2052 template<> bool BOFS<T>::enableVaporizedOil_; \
2053 template<> bool BOFS<T>::enableVaporizedWater_; \
2054 template<> bool BOFS<T>::enableConstantRs_; \
2055 template<> bool BOFS<T>::enableDiffusion_; \
2056 template<> BOFS<T>::OilPvt BOFS<T>::oilPvt_; \
2057 template<> BOFS<T>::GasPvt BOFS<T>::gasPvt_; \
2058 template<> BOFS<T>::WaterPvt BOFS<T>::waterPvt_; \
2059 template<> std::vector<std::array<T, 3>> BOFS<T>::referenceDensity_; \
2060 template<> std::vector<std::array<T, 3>> BOFS<T>::molarMass_; \
2061 template<> std::vector<std::array<T, 9>> BOFS<T>::diffusionCoefficients_; \
2062 template<> bool BOFS<T>::isInitialized_; \
2063 template<> bool BOFS<T>::useSaturatedTables_; \
2064 template<> bool BOFS<T>::enthalpy_eq_energy_;
2065 
2066 DECLARE_INSTANCE(float)
2067 DECLARE_INSTANCE(double)
2068 
2069 #undef DECLARE_INSTANCE
2070 #endif
2071 
2072 #ifndef COMPILING_STATIC_FLUID_SYSTEM
2073 #if HAVE_CUDA
2074 namespace gpuistl
2075 {
2076 
2077 template <class Scalar, class IndexTraits>
2078 FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, GpuBuffer>
2079 copy_to_gpu(const FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits>& oldFluidSystem) {
2080 
2081  using GpuBuffer3Array = GpuBuffer<std::array<Scalar, 3>>;
2082  using GpuBuffer9Array = GpuBuffer<std::array<Scalar, 9>>;
2083 
2084  OPM_ERROR_IF(oldFluidSystem.gasPvt_.approach() != GasPvtApproach::Co2Gas,
2085  fmt::format(fmt::runtime("Incompatible gas PVT approach. Given {}, expected {} (GasPvtApproach::Co2Gas)."),
2086  int(oldFluidSystem.gasPvt_.approach()), int(GasPvtApproach::Co2Gas)));
2087 
2088  OPM_ERROR_IF(oldFluidSystem.waterPvt_.approach() != WaterPvtApproach::BrineCo2,
2089  fmt::format(fmt::runtime("Incompatible water PVT approach. Given {}, expected {} (WaterPvtApproach::BrineCo2)."),
2090  int(oldFluidSystem.waterPvt_.approach()), int(WaterPvtApproach::BrineCo2)));
2091 
2092  OPM_ERROR_IF(oldFluidSystem.oilPvt_.isActive(),
2093  "We currently do not support an active oil phase for the FluidSystem on GPU.");
2094 
2095  auto newGasPvt = copy_to_gpu(oldFluidSystem.gasPvt_.template getRealPvt<GasPvtApproach::Co2Gas>());
2096 
2097  auto newOilPvt = NullOilPvt<Scalar>();
2098 
2099  auto newWaterPvt = copy_to_gpu(oldFluidSystem.waterPvt_.template getRealPvt<WaterPvtApproach::BrineCo2>());
2100 
2101  auto newReferenceDensity = GpuBuffer3Array(oldFluidSystem.referenceDensity_);
2102  auto newMolarMass = GpuBuffer3Array(oldFluidSystem.molarMass_);
2103  auto newDiffusionCoefficients = GpuBuffer9Array(oldFluidSystem.diffusionCoefficients_);
2104 
2105  return FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, GpuBuffer>(
2106  oldFluidSystem.surfacePressure,
2107  oldFluidSystem.surfaceTemperature,
2108  oldFluidSystem.reservoirTemperature_,
2109  newGasPvt,
2110  newOilPvt,
2111  newWaterPvt,
2112  oldFluidSystem.enableDissolvedGas_,
2113  oldFluidSystem.enableDissolvedGasInWater_,
2114  oldFluidSystem.enableVaporizedOil_,
2115  oldFluidSystem.enableVaporizedWater_,
2116  oldFluidSystem.enableConstantRs_,
2117  oldFluidSystem.enableDiffusion_,
2118  std::move(newReferenceDensity),
2119  std::move(newMolarMass),
2120  std::move(newDiffusionCoefficients),
2121  oldFluidSystem.phaseUsageInfo_,
2122  oldFluidSystem.isInitialized_,
2123  oldFluidSystem.useSaturatedTables_,
2124  oldFluidSystem.enthalpy_eq_energy_
2125  );
2126 }
2127 
2128 template <class Scalar, class IndexTraits>
2129 FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, GpuView>
2130 make_view(FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, GpuBuffer>& oldFluidSystem)
2131 {
2132  using Array3 = std::array<Scalar, 3>;
2133  using Array9 = std::array<Scalar, 9>;
2134 
2135  auto newGasPvt = make_view(oldFluidSystem.gasPvt_);
2136  auto newOilPvt = NullOilPvt<Scalar>();
2137  auto newWaterPvt = make_view(oldFluidSystem.waterPvt_);
2138 
2139  auto newReferenceDensity = make_view<Array3>(oldFluidSystem.referenceDensity_);
2140  auto newMolarMass = make_view<Array3>(oldFluidSystem.molarMass_);
2141  auto newDiffusionCoefficients = make_view<Array9>(oldFluidSystem.diffusionCoefficients_);
2142 
2143  return FLUIDSYSTEM_CLASSNAME<Scalar, IndexTraits, GpuView>(
2144  oldFluidSystem.surfacePressure,
2145  oldFluidSystem.surfaceTemperature,
2146  oldFluidSystem.reservoirTemperature_,
2147  newGasPvt,
2148  newOilPvt,
2149  newWaterPvt,
2150  oldFluidSystem.enableDissolvedGas_,
2151  oldFluidSystem.enableDissolvedGasInWater_,
2152  oldFluidSystem.enableVaporizedOil_,
2153  oldFluidSystem.enableVaporizedWater_,
2154  oldFluidSystem.enableConstantRs_,
2155  oldFluidSystem.enableDiffusion_,
2156  std::move(newReferenceDensity),
2157  std::move(newMolarMass),
2158  std::move(newDiffusionCoefficients),
2159  oldFluidSystem.phaseUsageInfo_,
2160  oldFluidSystem.isInitialized_,
2161  oldFluidSystem.useSaturatedTables_,
2162  oldFluidSystem.enthalpy_eq_energy_
2163  );
2164 }
2165 }
2166 #endif // HAVE_CUDA
2167 #endif // COMPILING_STATIC_FLUID_SYSTEM
2168 } // namespace Opm
A fluid system which uses the black-oil model assumptions to calculate termodynamically meaningful qu...
Definition: BlackOilFluidSystem_macrotemplate.hpp:73
This class represents the Pressure-Volume-Temperature relations of the liquid phase for a CO2-Brine s...
Definition: BrineCo2Pvt.hpp:63
This class represents the Pressure-Volume-Temperature relations of the liquid phase for a CO2-Brine s...
STATIC_OR_DEVICE const OilPvt & oilPvt() NOTHING_OR_CONST
Return a reference to the low-level object which calculates the oil phase quantities.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1706
STATIC_OR_DEVICE LhsEval convertXwGToRsw(const LhsEval &XwG, unsigned regionIdx) NOTHING_OR_CONST
Convert the mass fraction of the gas component in the water phase to the corresponding gas dissolutio...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1526
STATIC_OR_DEVICE LhsEval convertXgWToxgW(const LhsEval &XgW, unsigned regionIdx) NOTHING_OR_CONST
Convert a water mass fraction in the gas phase the corresponding mole fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1621
STATIC_OR_DEVICE Scalar referenceDensity(unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the density of a fluid phase at surface pressure [kg/m^3].
Definition: BlackOilFluidSystem_macrotemplate.hpp:556
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...
STATIC_OR_DEVICE void setReservoirTemperature(Scalar value) NOTHING_OR_CONST
Return the temperature of the reservoir.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1732
STATIC_OR_DEVICE LhsEval convertRswToXwG(const LhsEval &Rsw, unsigned regionIdx) NOTHING_OR_CONST
Convert a gas dissolution factor to the the corresponding mass fraction of the gas component in the w...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1580
static constexpr int gasCompIdx
Index of the gas component.
Definition: BlackOilFluidSystem_macrotemplate.hpp:433
STATIC_OR_DEVICE LhsEval convertxgOToXgO(const LhsEval &xgO, unsigned regionIdx) NOTHING_OR_CONST
Convert a oil mole fraction in the gas phase the corresponding mass fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1681
Null object for oil PVT calculations.
Definition: NullOilPvt.hpp:34
STATIC_OR_DEVICE const WaterPvt & waterPvt() NOTHING_OR_CONST
Return a reference to the low-level object which calculates the water phase quantities.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1716
static constexpr int waterCompIdx
Index of the water component.
Definition: BlackOilFluidSystem_macrotemplate.hpp:431
STATIC_OR_DEVICE std::size_t numRegions() NOTHING_OR_CONST
Returns the number of PVT regions which are considered.
Definition: BlackOilFluidSystem_macrotemplate.hpp:489
STATIC_OR_NOTHING void initFromState(const EclipseState &eclState, const Schedule &schedule)
Initialize the fluid system using an ECL deck object.
STATIC_OR_DEVICE LhsEval saturatedVaporizationFactor(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the water vaporization factor of saturated phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1375
STATIC_OR_DEVICE LhsEval dewPointPressure(const FluidState &fluidState, unsigned regionIdx) NOTHING_OR_CONST
Returns the dew point pressure $P_d$ using the current Rv.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1469
STATIC_OR_DEVICE Scalar reservoirTemperature(unsigned=0) NOTHING_OR_CONST
Set the temperature of the reservoir.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1724
STATIC_OR_DEVICE LhsEval convertXgOToRv(const LhsEval &XgO, unsigned regionIdx) NOTHING_OR_CONST
Convert the mass fraction of the oil component in the gas phase to the corresponding oil vaporization...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1539
STATIC_OR_DEVICE void setEnableConstantRs(bool yesno)
Specify whether the fluid system should use constant Rs tables.
Definition: BlackOilFluidSystem_macrotemplate.hpp:320
STATIC_OR_DEVICE LhsEval convertXgWToRvw(const LhsEval &XgW, unsigned regionIdx) NOTHING_OR_CONST
Convert the mass fraction of the water component in the gas phase to the corresponding water vaporiza...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1552
STATIC_OR_DEVICE Scalar molarMass(unsigned compIdx, unsigned regionIdx=0) NOTHING_OR_CONST
Return the molar mass of a component in [kg/mol].
Definition: BlackOilFluidSystem_macrotemplate.hpp:461
STATIC_OR_DEVICE LhsEval enthalpy(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Given a phase&#39;s composition, temperature, pressure and density, calculate its specific enthalpy [J/kg...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1354
STATIC_OR_DEVICE bool enableConstantRs() NOTHING_OR_CONST
Returns whether constant Rs tables should be used.
Definition: BlackOilFluidSystem_macrotemplate.hpp:532
PiecewiseLinearTwoPhaseMaterialParams< TraitsT, ViewType > make_view(PiecewiseLinearTwoPhaseMaterialParams< TraitsT, ContainerType > &params)
this function is intented to make a GPU friendly view of the PiecewiseLinearTwoPhaseMaterialParams ...
Definition: PiecewiseLinearTwoPhaseMaterialParams.hpp:312
This class represents the Pressure-Volume-Temperature relations of the water phase in the black-oil m...
STATIC_OR_DEVICE LhsEval convertXoGToxoG(const LhsEval &XoG, unsigned regionIdx) NOTHING_OR_CONST
Convert a gas mass fraction in the oil phase the corresponding mole fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1645
STATIC_OR_DEVICE LhsEval density(const FluidState &fluidState, const ParameterCache< ParamCacheEval > &paramCache, unsigned phaseIdx) NOTHING_OR_CONST
Calculate the density [kg/m^3] of a fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:564
STATIC_OR_DEVICE bool enableVaporizedWater() NOTHING_OR_CONST
Returns whether the fluid system should consider that the water component can dissolve in the gas pha...
Definition: BlackOilFluidSystem_macrotemplate.hpp:526
STATIC_OR_DEVICE const GasPvt & gasPvt() NOTHING_OR_CONST
Return a reference to the low-level object which calculates the gas phase quantities.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1696
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Convience header to include the gpuistl headers if HAVE_CUDA is defined.
This class represents the Pressure-Volume-Temperature relations of the gas phase in the black-oil mod...
Definition: GasPvtMultiplexer.hpp:108
STATIC_OR_DEVICE void setEnableDissolvedGas(bool yesno)
Specify whether the fluid system should consider that the gas component can dissolve in the oil phase...
Definition: BlackOilFluidSystem_macrotemplate.hpp:284
A parameter cache which does nothing.
STATIC_OR_DEVICE LhsEval saturatedDensity(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Compute the density of a saturated fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:716
STATIC_OR_DEVICE LhsEval convertxoGToXoG(const LhsEval &xoG, unsigned regionIdx) NOTHING_OR_CONST
Convert a gas mole fraction in the oil phase the corresponding mass fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1657
STATIC_OR_DEVICE bool enableVaporizedOil() NOTHING_OR_CONST
Returns whether the fluid system should consider that the oil component can dissolve in the gas phase...
Definition: BlackOilFluidSystem_macrotemplate.hpp:517
Definition: Constants.hpp:41
This macro generates a class HasMember_${MEMBER_NAME} which can be used for template specialization...
STATIC_OR_DEVICE void setEnableDissolvedGasInWater(bool yesno)
Specify whether the fluid system should consider that the gas component can dissolve in the water pha...
Definition: BlackOilFluidSystem_macrotemplate.hpp:311
STATIC_OR_DEVICE bool useSaturatedTables() NOTHING_OR_CONST
Returns whether the saturated tables should be used.
Definition: BlackOilFluidSystem_macrotemplate.hpp:548
STATIC_OR_DEVICE Scalar diffusionCoefficient(unsigned compIdx, unsigned phaseIdx, unsigned regionIdx=0) NOTHING_OR_CONST
Calculate the binary molecular diffusion coefficient for a component in a fluid phase [mol^2 * s / (k...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1748
STATIC_OR_NOTHING std::string_view componentName(unsigned compIdx) NOTHING_OR_CONST
Return the human readable name of a component.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1977
static constexpr unsigned numPhases
Number of fluid phases in the fluid system.
Definition: BlackOilFluidSystem_macrotemplate.hpp:396
STATIC_OR_DEVICE void setOilPvt(std::shared_ptr< OilPvt > pvtObj)
Set the pressure-volume-saturation (PVT) relations for the oil phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:348
STATIC_OR_DEVICE void setUseSaturatedTables(bool yesno)
Specify whether the saturated tables should be used.
Definition: BlackOilFluidSystem_macrotemplate.hpp:336
STATIC_OR_DEVICE LhsEval enthalpy(const FluidState &fluidState, const ParameterCache< ParamCacheEval > &paramCache, unsigned phaseIdx)
Given a phase&#39;s composition, temperature, pressure and density, calculate its specific enthalpy [J/kg...
Definition: BlackOilFluidSystem_macrotemplate.hpp:591
STATIC_OR_DEVICE LhsEval viscosity(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Calculate the dynamic viscosity of a fluid phase [Pa*s].
Definition: BlackOilFluidSystem_macrotemplate.hpp:1089
STATIC_OR_DEVICE void setEnableVaporizedWater(bool yesno)
Specify whether the fluid system should consider that the water component can dissolve in the gas pha...
Definition: BlackOilFluidSystem_macrotemplate.hpp:302
static constexpr unsigned numComponents
Number of chemical species in the fluid system.
Definition: BlackOilFluidSystem_macrotemplate.hpp:426
STATIC_OR_DEVICE LhsEval saturatedDissolutionFactor(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the dissolution factor of a saturated fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1433
Definition: PhaseUsageInfo.hpp:42
STATIC_OR_DEVICE bool enableDissolvedGasInWater() NOTHING_OR_CONST
Returns whether the fluid system should consider that the gas component can dissolve in the water pha...
Definition: BlackOilFluidSystem_macrotemplate.hpp:508
STATIC_OR_DEVICE LhsEval convertXoGToRs(const LhsEval &XoG, unsigned regionIdx) NOTHING_OR_CONST
Convert the mass fraction of the gas component in the oil phase to the corresponding gas dissolution ...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1513
STATIC_OR_DEVICE void initEnd()
Finish initializing the black oil fluid system.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1891
STATIC_OR_DEVICE LhsEval viscosity(const FluidState &fluidState, const ParameterCache< ParamCacheEval > &paramCache, unsigned phaseIdx) NOTHING_OR_CONST
Calculate the dynamic viscosity of a fluid phase [Pa*s].
Definition: BlackOilFluidSystem_macrotemplate.hpp:584
This class represents the Pressure-Volume-Temperature relations of the gas phase for CO2...
Definition: Co2GasPvt.hpp:59
STATIC_OR_DEVICE void setDiffusionCoefficient(Scalar coefficient, unsigned compIdx, unsigned phaseIdx, unsigned regionIdx=0) NOTHING_OR_CONST
Definition: BlackOilFluidSystem_macrotemplate.hpp:1752
STATIC_OR_DEVICE void setGasPvt(std::shared_ptr< GasPvt > pvtObj)
Set the pressure-volume-saturation (PVT) relations for the gas phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:342
This class represents the Pressure-Volume-Temperature relations of the oil phase in the black-oil mod...
Definition: OilPvtMultiplexer.hpp:109
STATIC_OR_DEVICE bool enableDissolvedGas() NOTHING_OR_CONST
Returns whether the fluid system should consider that the gas component can dissolve in the oil phase...
Definition: BlackOilFluidSystem_macrotemplate.hpp:498
This class represents the Pressure-Volume-Temperature relations of the water phase in the black-oil m...
Definition: ConstantCompressibilityBrinePvt.hpp:39
STATIC_OR_DEVICE LhsEval bubblePointPressure(const FluidState &fluidState, unsigned regionIdx) NOTHING_OR_CONST
Returns the bubble point pressure $P_b$ using the current Rs.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1458
The base class for all fluid systems.
Definition: BaseFluidSystem.hpp:42
STATIC_OR_NOTHING std::string_view phaseName(unsigned phaseIdx) NOTHING_OR_CONST
Return the human readable name of a fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1921
static constexpr unsigned waterPhaseIdx
Index of the water phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:399
STATIC_OR_DEVICE LhsEval convertRsToXoG(const LhsEval &Rs, unsigned regionIdx) NOTHING_OR_CONST
Convert a gas dissolution factor to the the corresponding mass fraction of the gas component in the o...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1566
STATIC_OR_DEVICE LhsEval density(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Calculate the density [kg/m^3] of a fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:608
STATIC_OR_DEVICE bool enableDiffusion() NOTHING_OR_CONST
Returns whether the fluid system should consider diffusion.
Definition: BlackOilFluidSystem_macrotemplate.hpp:540
STATIC_OR_DEVICE void setEnableVaporizedOil(bool yesno)
Specify whether the fluid system should consider that the oil component can dissolve in the gas phase...
Definition: BlackOilFluidSystem_macrotemplate.hpp:293
STATIC_OR_DEVICE LhsEval saturatedInverseFormationVolumeFactor(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the formation volume factor of a "saturated" fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:945
static constexpr unsigned gasPhaseIdx
Index of the gas phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:403
STATIC_OR_DEVICE bool isIdealMixture(unsigned)
Returns true if and only if a fluid phase is assumed to be an ideal mixture.
Definition: BlackOilFluidSystem_macrotemplate.hpp:465
PiecewiseLinearTwoPhaseMaterialParams< TraitsT, GPUContainerType > copy_to_gpu(const PiecewiseLinearTwoPhaseMaterialParams< TraitsT > &params)
Move a PiecewiseLinearTwoPhaseMaterialParams-object to the GPU.
Definition: PiecewiseLinearTwoPhaseMaterialParams.hpp:285
STATIC_OR_DEVICE bool isIdealGas(unsigned) NOTHING_OR_CONST
Returns true if and only if a fluid phase is assumed to be an ideal gas.
Definition: BlackOilFluidSystem_macrotemplate.hpp:477
STATIC_OR_DEVICE bool phaseIsActive(unsigned phaseIdx) NOTHING_OR_CONST
Returns whether a fluid phase is active.
Definition: BlackOilFluidSystem_macrotemplate.hpp:446
FLUIDSYSTEM_CLASSNAME(const FLUIDSYSTEM_CLASSNAME< Scalar, IndexTraits, StorageT > &other)
Default copy constructor.
Definition: BlackOilFluidSystem_macrotemplate.hpp:160
The class which specifies the default phase and component indices for the black-oil fluid system...
STATIC_OR_NOTHING Scalar surfacePressure
The pressure at the surface.
Definition: BlackOilFluidSystem_macrotemplate.hpp:406
STATIC_OR_DEVICE unsigned solventComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST
returns the index of "primary" component of a phase (solvent)
Definition: BlackOilFluidSystem_macrotemplate.hpp:1938
STATIC_OR_DEVICE unsigned soluteComponentIndex(unsigned phaseIdx) NOTHING_OR_CONST
returns the index of "secondary" component of a phase (solute)
Definition: BlackOilFluidSystem_macrotemplate.hpp:1955
This class represents the Pressure-Volume-Temperature relations of the oil phase in the black-oil mod...
STATIC_OR_DEVICE bool isLiquid(unsigned phaseIdx) NOTHING_OR_CONST
Return whether a phase is liquid.
Definition: BlackOilFluidSystem_macrotemplate.hpp:415
STATIC_OR_DEVICE LhsEval convertRvToXgO(const LhsEval &Rv, unsigned regionIdx) NOTHING_OR_CONST
Convert an oil vaporization factor to the corresponding mass fraction of the oil component in the gas...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1594
A parameter cache which does nothing.
Definition: NullParameterCache.hpp:39
STATIC_OR_DEVICE void setWaterPvt(std::shared_ptr< WaterPvt > pvtObj)
Set the pressure-volume-saturation (PVT) relations for the water phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:354
static constexpr int oilCompIdx
Index of the oil component.
Definition: BlackOilFluidSystem_macrotemplate.hpp:429
STATIC_OR_DEVICE LhsEval saturationPressure(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the saturation pressure of a given phase [Pa] depending on its composition.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1486
STATIC_OR_DEVICE unsigned numActivePhases() NOTHING_OR_CONST
Returns the number of active fluid phases (i.e., usually three)
Definition: BlackOilFluidSystem_macrotemplate.hpp:442
Some templates to wrap the valgrind client request macros.
STATIC_OR_DEVICE LhsEval inverseFormationVolumeFactor(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx) NOTHING_OR_CONST
Returns the formation volume factor of an "undersaturated" fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:827
Scalar Scalar
The type used for scalar quantities.
Definition: BaseFluidSystem.hpp:48
STATIC_OR_DEVICE LhsEval convertXgOToxgO(const LhsEval &XgO, unsigned regionIdx) NOTHING_OR_CONST
Convert a oil mass fraction in the gas phase the corresponding mole fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1669
STATIC_OR_NOTHING Scalar surfaceTemperature
The temperature at the surface.
Definition: BlackOilFluidSystem_macrotemplate.hpp:409
STATIC_OR_DEVICE void setReferenceDensities(Scalar rhoOil, Scalar rhoWater, Scalar rhoGas, unsigned regionIdx)
Initialize the values of the reference densities.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1880
STATIC_OR_DEVICE const PhaseUsageInfo< IndexTraits > & phaseUsage() NOTHING_OR_CONST
Returns a const reference to PhaseUsageInfo.
Definition: BlackOilFluidSystem_macrotemplate.hpp:438
STATIC_OR_DEVICE LhsEval convertXwGToxwG(const LhsEval &XwG, unsigned regionIdx) NOTHING_OR_CONST
Convert a gas mass fraction in the water phase the corresponding mole fraction.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1633
This class represents the Pressure-Volume-Temperature relations of the gas phase in the black-oil mod...
STATIC_OR_DEVICE LhsEval diffusionCoefficient(const FluidState &fluidState, const ParameterCache< ParamCacheEval > &paramCache, unsigned phaseIdx, unsigned compIdx) NOTHING_OR_CONST
Calculate the binary molecular diffusion coefficient for a component in a fluid phase [mol^2 * s / (k...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1759
STATIC_OR_DEVICE LhsEval fugacityCoefficient(const FluidState &fluidState, const ParameterCache< ParamCacheEval > &paramCache, unsigned phaseIdx, unsigned compIdx) NOTHING_OR_CONST
Calculate the fugacity coefficient [Pa] of an individual component in a fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:571
STATIC_OR_DEVICE LhsEval convertRvwToXgW(const LhsEval &Rvw, unsigned regionIdx) NOTHING_OR_CONST
Convert an water vaporization factor to the corresponding mass fraction of the water component in the...
Definition: BlackOilFluidSystem_macrotemplate.hpp:1608
STATIC_OR_DEVICE bool isCompressible(unsigned) NOTHING_OR_CONST
Returns true if and only if a fluid phase is assumed to be compressible.
Definition: BlackOilFluidSystem_macrotemplate.hpp:473
STATIC_OR_DEVICE LhsEval fugacityCoefficient(const FluidState &fluidState, unsigned phaseIdx, unsigned compIdx, unsigned regionIdx) NOTHING_OR_CONST
Calculate the fugacity coefficient [Pa] of an individual component in a fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:967
STATIC_OR_DEVICE void setEnableDiffusion(bool yesno)
Specify whether the fluid system should consider diffusion.
Definition: BlackOilFluidSystem_macrotemplate.hpp:328
static constexpr unsigned oilPhaseIdx
Index of the oil phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:401
STATIC_OR_NOTHING void initBegin(std::size_t numPvtRegions)
Begin the initialization of the black oil fluid system.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1857
STATIC_OR_DEVICE LhsEval saturatedDissolutionFactor(const FluidState &fluidState, unsigned phaseIdx, unsigned regionIdx, const LhsEval &maxOilSaturation) NOTHING_OR_CONST
Returns the dissolution factor of a saturated fluid phase.
Definition: BlackOilFluidSystem_macrotemplate.hpp:1402
The base class for all fluid systems.
Definition: Runspec.hpp:45
Definition: BlackOilFluidSystemNonStatic.hpp:75