23#ifndef OPM_INIT_STATE_EQUIL_IMPL_HPP
24#define OPM_INIT_STATE_EQUIL_IMPL_HPP
26#include <dune/grid/common/mcmgmapper.hh>
28#include <opm/common/OpmLog/OpmLog.hpp>
30#include <opm/grid/utility/RegionMapping.hpp>
32#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
33#include <opm/input/eclipse/EclipseState/Tables/PbvdTable.hpp>
34#include <opm/input/eclipse/EclipseState/Tables/PdvdTable.hpp>
35#include <opm/input/eclipse/EclipseState/Tables/RsconstTable.hpp>
36#include <opm/input/eclipse/EclipseState/Tables/RsvdTable.hpp>
37#include <opm/input/eclipse/EclipseState/Tables/RtempvdTable.hpp>
38#include <opm/input/eclipse/EclipseState/Tables/RvvdTable.hpp>
39#include <opm/input/eclipse/EclipseState/Tables/RvwvdTable.hpp>
40#include <opm/input/eclipse/EclipseState/Tables/SaltvdTable.hpp>
41#include <opm/input/eclipse/EclipseState/Tables/SaltpvdTable.hpp>
43#include <opm/input/eclipse/Units/UnitSystem.hpp>
45#include <opm/material/fluidmatrixinteractions/EclMaterialLawManager.hpp>
46#include <opm/material/fluidsystems/BlackOilFluidSystem.hpp>
53#include <fmt/format.h>
68template <
typename CellRange,
class Scalar>
70 const std::vector<std::pair<Scalar, Scalar>>&
cellZMinMax,
72 std::array<Scalar,2>& span)
74 span[0] = std::numeric_limits<Scalar>::max();
75 span[1] = std::numeric_limits<Scalar>::lowest();
85 for (
const auto& cell : cells) {
89 span[0] = comm.min(span[0]);
90 span[1] = comm.max(span[1]);
96 const int numIntervals,
97 std::vector<std::pair<Scalar, Scalar>>& subdiv)
99 const auto h = (right - left) / numIntervals;
102 for (
auto i = 0*numIntervals; i < numIntervals; ++i) {
103 const auto start = end;
104 end = left + (i + 1)*h;
106 subdiv.emplace_back((start + end) / 2, h);
110template <
typename CellID,
typename Scalar>
111std::vector<std::pair<Scalar, Scalar>>
113 const std::pair<Scalar, Scalar> topbot,
114 const int numIntervals)
116 auto subdiv = std::vector<std::pair<Scalar, Scalar>>{};
117 subdiv.reserve(2 * numIntervals);
119 if (topbot.first > topbot.second) {
120 throw std::out_of_range {
121 "Negative thickness (inverted top/bottom faces) in cell "
127 2*numIntervals, subdiv);
132template <
class Scalar,
class Element>
135 typedef typename Element::Geometry Geometry;
136 static constexpr int zCoord = Element::dimension - 1;
139 const Geometry& geometry = element.geometry();
140 const int corners = geometry.corners();
141 for (
int i=0; i < corners; ++i)
142 zz += geometry.corner(i)[zCoord];
147template <
class Scalar,
class Element>
150 typedef typename Element::Geometry Geometry;
151 static constexpr int xCoord = Element::dimension - 3;
152 static constexpr int yCoord = Element::dimension - 2;
157 const Geometry& geometry = element.geometry();
158 const int corners = geometry.corners();
159 for (
int i=0; i < corners; ++i) {
160 xx += geometry.corner(i)[xCoord];
161 yy += geometry.corner(i)[yCoord];
163 return std::make_pair(xx/corners, yy/corners);
166template <
class Scalar,
class Element>
167std::pair<Scalar,Scalar>
cellZSpan(
const Element& element)
169 typedef typename Element::Geometry Geometry;
170 static constexpr int zCoord = Element::dimension - 1;
174 const Geometry& geometry = element.geometry();
175 const int corners = geometry.corners();
176 assert(corners == 8);
177 for (
int i=0; i < 4; ++i)
178 bot += geometry.corner(i)[zCoord];
179 for (
int i=4; i < corners; ++i)
180 top += geometry.corner(i)[zCoord];
182 return std::make_pair(bot/4, top/4);
185template <
class Scalar,
class Element>
188 typedef typename Element::Geometry Geometry;
189 static constexpr int zCoord = Element::dimension - 1;
190 const Geometry& geometry = element.geometry();
191 const int corners = geometry.corners();
192 assert(corners == 8);
193 auto min = std::numeric_limits<Scalar>::max();
194 auto max = std::numeric_limits<Scalar>::lowest();
197 for (
int i=0; i < corners; ++i) {
198 min = std::min(min,
static_cast<Scalar
>(geometry.corner(i)[zCoord]));
199 max = std::max(max,
static_cast<Scalar
>(geometry.corner(i)[zCoord]));
201 return std::make_pair(min, max);
204template<
class Scalar>
206 Scalar& dipAngle, Scalar& dipAzimuth)
208 const auto& Xc = cellCorners.
X;
209 const auto& Yc = cellCorners.
Y;
210 const auto& Zc = cellCorners.
Z;
212 Scalar v1x = Xc[1] - Xc[0];
213 Scalar v1y = Yc[1] - Yc[0];
214 Scalar v1z = Zc[1] - Zc[0];
216 Scalar v2x = Xc[2] - Xc[0];
217 Scalar v2y = Yc[2] - Yc[0];
218 Scalar v2z = Zc[2] - Zc[0];
221 Scalar nx = v1y * v2z - v1z * v2y;
222 Scalar ny = v1z * v2x - v1x * v2z;
223 Scalar nz = v1x * v2y - v1y * v2x;
226 Scalar norm = std::hypot(nx, ny, nz);
234 dipAngle = std::acos(std::abs(nz));
237 if (std::abs(nx) > 1e-10 || std::abs(ny) > 1e-10) {
238 dipAzimuth = std::atan2(ny, nx);
240 dipAzimuth = std::fmod(dipAzimuth + 2*std::numbers::pi_v<Scalar>, 2*std::numbers::pi_v<Scalar>);
246 const Scalar maxDip = std::numbers::pi_v<Scalar>/2 -
static_cast<Scalar
>(1e-6);
247 dipAngle = std::min(dipAngle, maxDip);
255template <
class Scalar,
class Element>
258 typedef typename Element::Geometry Geometry;
259 const Geometry& geometry = element.geometry();
260 static constexpr int zCoord = Element::dimension - 1;
261 static constexpr int yCoord = Element::dimension - 2;
262 static constexpr int xCoord = Element::dimension - 3;
263 const int corners = geometry.corners();
264 assert(corners == 8);
265 std::array<Scalar, 8> X {};
266 std::array<Scalar, 8> Y {};
267 std::array<Scalar, 8> Z {};
269 for (
int i = 0; i < corners; ++i) {
270 auto corner = geometry.corner(i);
271 X[i] = corner[xCoord];
272 Y[i] = corner[yCoord];
273 Z[i] = corner[zCoord];
279template<
class Scalar>
281 Scalar dipAngle, Scalar dipAzimuth,
282 const std::array<Scalar, 3>& referencePoint)
289 Scalar dx = x - referencePoint[0];
290 Scalar dy = y - referencePoint[1];
291 Scalar dz = z - referencePoint[2];
294 if (std::abs(dipAngle) < 1e-10) {
295 return referencePoint[2] + dz;
299 Scalar pointAzimuth = std::atan2(dy, dx);
302 Scalar azimuthDiff = pointAzimuth - dipAzimuth;
305 Scalar lateralDist = std::hypot(dx, dy);
308 Scalar lateralInDipDir = lateralDist * std::cos(azimuthDiff);
313 Scalar tvd = referencePoint[2] + dz * std::cos(dipAngle) + lateralInDipDir * std::sin(dipAngle);
318template<
class Scalar,
class RHS>
320 const std::array<Scalar,2>& span,
326 const Scalar h = stepsize();
327 const Scalar h2 = h / 2;
328 const Scalar h6 = h / 6;
334 f_.push_back(f(span_[0], y0));
336 for (
int i = 0; i < N; ++i) {
337 const Scalar x = span_[0] + i*h;
338 const Scalar y = y_.back();
340 const Scalar k1 = f_[i];
341 const Scalar k2 = f(x + h2, y + h2*k1);
342 const Scalar k3 = f(x + h2, y + h2*k2);
343 const Scalar k4 = f(x + h, y + h*k3);
345 y_.push_back(y + h6*(k1 + 2*(k2 + k3) + k4));
346 f_.push_back(f(x + h, y_.back()));
349 assert (y_.size() ==
typename std::vector<Scalar>::size_type(N + 1));
352template<
class Scalar,
class RHS>
358 const Scalar h = stepsize();
359 int i = (x - span_[0]) / h;
362 if (i < 0) { i = 0; }
363 if (N_ <= i) { i = N_ - 1; }
367 const Scalar t = (x - (span_[0] + i*h)) / h;
369 const Scalar y0 = y_[i], y1 = y_[i + 1];
370 const Scalar f0 = f_[i], f1 = f_[i + 1];
372 Scalar u = (1 - 2*t) * (y1 - y0);
373 u += h * ((t - 1)*f0 + t*f1);
375 u += (1 - t)*y0 + t*y1;
380template<
class Scalar,
class RHS>
384 return (span_[1] - span_[0]) / N_;
387namespace PhasePressODE {
389template<
class Flu
idSystem>
391Water(
const TabulatedFunction& tempVdTable,
392 const TabulatedFunction& saltVdTable,
393 const int pvtRegionIdx,
394 const Scalar normGrav)
395 : tempVdTable_(tempVdTable)
396 , saltVdTable_(saltVdTable)
397 , pvtRegionIdx_(pvtRegionIdx)
402template<
class Flu
idSystem>
403typename Water<FluidSystem>::Scalar
406 const Scalar press)
const
408 return this->density(depth, press) * g_;
411template<
class Flu
idSystem>
412typename Water<FluidSystem>::Scalar
415 const Scalar press)
const
418 Scalar saltConcentration = saltVdTable_.eval(depth,
true);
419 Scalar temp = tempVdTable_.eval(depth,
true);
420 Scalar rho = FluidSystem::waterPvt().inverseFormationVolumeFactor(pvtRegionIdx_,
425 rho *= FluidSystem::referenceDensity(FluidSystem::waterPhaseIdx, pvtRegionIdx_);
429template<
class Flu
idSystem,
class RS>
431Oil(
const TabulatedFunction& tempVdTable,
433 const int pvtRegionIdx,
434 const Scalar normGrav)
435 : tempVdTable_(tempVdTable)
437 , pvtRegionIdx_(pvtRegionIdx)
442template<
class Flu
idSystem,
class RS>
443typename Oil<FluidSystem,RS>::Scalar
446 const Scalar press)
const
448 return this->density(depth, press) * g_;
451template<
class Flu
idSystem,
class RS>
452typename Oil<FluidSystem,RS>::Scalar
455 const Scalar press)
const
457 const Scalar temp = tempVdTable_.eval(depth,
true);
459 if (FluidSystem::enableDissolvedGas() || FluidSystem::enableConstantRs())
460 rs = rs_(depth, press, temp);
463 if (rs >= FluidSystem::oilPvt().saturatedGasDissolutionFactor(pvtRegionIdx_, temp, press)) {
464 bOil = FluidSystem::oilPvt().saturatedInverseFormationVolumeFactor(pvtRegionIdx_, temp, press);
467 bOil = FluidSystem::oilPvt().inverseFormationVolumeFactor(pvtRegionIdx_, temp, press, rs);
469 Scalar rho = bOil * FluidSystem::referenceDensity(FluidSystem::oilPhaseIdx, pvtRegionIdx_);
470 if (FluidSystem::enableDissolvedGas() || FluidSystem::enableConstantRs()) {
471 rho += rs * bOil * FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, pvtRegionIdx_);
477template<
class Flu
idSystem,
class RV,
class RVW>
479Gas(
const TabulatedFunction& tempVdTable,
482 const int pvtRegionIdx,
483 const Scalar normGrav)
484 : tempVdTable_(tempVdTable)
487 , pvtRegionIdx_(pvtRegionIdx)
492template<
class Flu
idSystem,
class RV,
class RVW>
493typename Gas<FluidSystem,RV,RVW>::Scalar
496 const Scalar press)
const
498 return this->density(depth, press) * g_;
501template<
class Flu
idSystem,
class RV,
class RVW>
502typename Gas<FluidSystem,RV,RVW>::Scalar
505 const Scalar press)
const
507 const Scalar temp = tempVdTable_.eval(depth,
true);
509 if (FluidSystem::enableVaporizedOil())
510 rv = rv_(depth, press, temp);
513 if (FluidSystem::enableVaporizedWater())
514 rvw = rvw_(depth, press, temp);
518 if (FluidSystem::enableVaporizedOil() && FluidSystem::enableVaporizedWater()) {
519 if (rv >= FluidSystem::gasPvt().saturatedOilVaporizationFactor(pvtRegionIdx_, temp, press)
520 && rvw >= FluidSystem::gasPvt().saturatedWaterVaporizationFactor(pvtRegionIdx_, temp, press))
522 bGas = FluidSystem::gasPvt().saturatedInverseFormationVolumeFactor(pvtRegionIdx_, temp, press);
524 bGas = FluidSystem::gasPvt().inverseFormationVolumeFactor(pvtRegionIdx_, temp, press, rv, rvw);
526 Scalar rho = bGas * FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, pvtRegionIdx_);
527 rho += rv * bGas * FluidSystem::referenceDensity(FluidSystem::oilPhaseIdx, pvtRegionIdx_)
528 + rvw * bGas * FluidSystem::referenceDensity(FluidSystem::waterPhaseIdx, pvtRegionIdx_);
532 if (FluidSystem::enableVaporizedOil()){
533 if (rv >= FluidSystem::gasPvt().saturatedOilVaporizationFactor(pvtRegionIdx_, temp, press)) {
534 bGas = FluidSystem::gasPvt().saturatedInverseFormationVolumeFactor(pvtRegionIdx_, temp, press);
536 bGas = FluidSystem::gasPvt().inverseFormationVolumeFactor(pvtRegionIdx_,
542 Scalar rho = bGas * FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, pvtRegionIdx_);
543 rho += rv * bGas * FluidSystem::referenceDensity(FluidSystem::oilPhaseIdx, pvtRegionIdx_);
547 if (FluidSystem::enableVaporizedWater()){
548 if (rvw >= FluidSystem::gasPvt().saturatedWaterVaporizationFactor(pvtRegionIdx_, temp, press)) {
549 bGas = FluidSystem::gasPvt().saturatedInverseFormationVolumeFactor(pvtRegionIdx_, temp, press);
552 bGas = FluidSystem::gasPvt().inverseFormationVolumeFactor(pvtRegionIdx_,
558 Scalar rho = bGas * FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, pvtRegionIdx_);
559 rho += rvw * bGas * FluidSystem::referenceDensity(FluidSystem::waterPhaseIdx, pvtRegionIdx_);
564 bGas = FluidSystem::gasPvt().inverseFormationVolumeFactor(pvtRegionIdx_, temp,
568 Scalar rho = bGas * FluidSystem::referenceDensity(FluidSystem::gasPhaseIdx, pvtRegionIdx_);
575template<
class Flu
idSystem,
class Region>
577PressureTable<FluidSystem,Region>::
578PressureFunction<ODE>::PressureFunction(
const ODE& ode,
584 this->value_[Direction::Up] = std::make_unique<Distribution>
585 (ode, VSpan {{ ic.depth, span[0] }}, ic.pressure, nsample);
587 this->value_[Direction::Down] = std::make_unique<Distribution>
588 (ode, VSpan {{ ic.depth, span[1] }}, ic.pressure, nsample);
591template<
class Flu
idSystem,
class Region>
593PressureTable<FluidSystem,Region>::
594PressureFunction<ODE>::PressureFunction(
const PressureFunction& rhs)
595 : initial_(rhs.initial_)
597 this->value_[Direction::Up] =
598 std::make_unique<Distribution>(*rhs.value_[Direction::Up]);
600 this->value_[Direction::Down] =
601 std::make_unique<Distribution>(*rhs.value_[Direction::Down]);
604template<
class Flu
idSystem,
class Region>
606typename PressureTable<FluidSystem,Region>::template PressureFunction<ODE>&
611 this->initial_ = rhs.initial_;
613 this->value_[Direction::Up] =
614 std::make_unique<Distribution>(*rhs.value_[Direction::Up]);
616 this->value_[Direction::Down] =
617 std::make_unique<Distribution>(*rhs.value_[Direction::Down]);
622template<
class Flu
idSystem,
class Region>
624typename PressureTable<FluidSystem,Region>::template PressureFunction<ODE>&
629 this->initial_ = rhs.initial_;
630 this->value_ = std::move(rhs.value_);
635template<
class Flu
idSystem,
class Region>
638PressureTable<FluidSystem,Region>::
639PressureFunction<ODE>::
640value(
const Scalar depth)
const
642 if (depth < this->initial_.depth) {
644 return (*this->value_[Direction::Up])(depth);
646 else if (depth > this->initial_.depth) {
648 return (*this->value_[Direction::Down])(depth);
652 return this->initial_.pressure;
657template<
class Flu
idSystem,
class Region>
658template<
typename PressFunc>
659void PressureTable<FluidSystem,Region>::
660checkPtr(
const PressFunc* phasePress,
661 const std::string& phaseName)
const
663 if (phasePress !=
nullptr) {
return; }
665 throw std::invalid_argument {
666 "Phase pressure function for \"" + phaseName
667 +
"\" most not be null"
671template<
class Flu
idSystem,
class Region>
672typename PressureTable<FluidSystem,Region>::Strategy
673PressureTable<FluidSystem,Region>::
674selectEquilibrationStrategy(
const Region& reg)
const
676 if (!this->oilActive()) {
677 if (reg.datum() > reg.zwoc()) {
678 return &PressureTable::equil_WOG;
680 return &PressureTable::equil_GOW;
683 if (reg.datum() > reg.zwoc()) {
684 return &PressureTable::equil_WOG;
686 else if (reg.datum() < reg.zgoc()) {
687 return &PressureTable::equil_GOW;
690 return &PressureTable::equil_OWG;
694template<
class Flu
idSystem,
class Region>
695void PressureTable<FluidSystem,Region>::
696copyInPointers(
const PressureTable& rhs)
698 if (rhs.oil_ !=
nullptr) {
699 this->oil_ = std::make_unique<OPress>(*rhs.oil_);
702 if (rhs.gas_ !=
nullptr) {
703 this->gas_ = std::make_unique<GPress>(*rhs.gas_);
706 if (rhs.wat_ !=
nullptr) {
707 this->wat_ = std::make_unique<WPress>(*rhs.wat_);
711template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
714 const std::vector<Scalar>& swatInit)
715 : matLawMgr_(matLawMgr)
716 , swatInit_ (swatInit)
720template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
723 : matLawMgr_(rhs.matLawMgr_)
724 , swatInit_ (rhs.swatInit_)
726 , press_ (rhs.press_)
729 this->setEvaluationPoint(*rhs.evalPt_.position,
731 *rhs.evalPt_.ptable);
734template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
741 this->setEvaluationPoint(x, reg, ptable);
742 this->initializePhaseQuantities();
744 if (ptable.
gasActive()) { this->deriveGasSat(); }
746 if (ptable.
waterActive()) { this->deriveWaterSat(); }
749 if (this->isOverlappingTransition()) {
750 this->fixUnphysicalTransition();
753 if (ptable.
oilActive()) { this->deriveOilSat(); }
755 this->accountForScaledSaturations();
760template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
764 const PTable& ptable)
766 this->evalPt_.position = &x;
767 this->evalPt_.region = ®
768 this->evalPt_.ptable = &ptable;
771template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
772void PhaseSaturations<MaterialLawManager,FluidSystem,Region,CellID>::
773initializePhaseQuantities()
776 this->press_.reset();
778 const auto depth = this->evalPt_.position->depth;
779 const auto& ptable = *this->evalPt_.ptable;
781 if (ptable.oilActive()) {
782 this->press_.oil = ptable.oil(depth);
785 if (ptable.gasActive()) {
786 this->press_.gas = ptable.gas(depth);
789 if (ptable.waterActive()) {
790 this->press_.water = ptable.water(depth);
794template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
795void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::deriveOilSat()
797 this->sat_.oil = 1.0 - this->sat_.water - this->sat_.gas;
800template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
801void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::deriveGasSat()
803 auto& sg = this->sat_.gas;
805 const auto isIncr =
true;
806 const auto oilActive = this->evalPt_.ptable->oilActive();
808 if (this->isConstCapPress(this->gasPos())) {
812 const auto gas_contact = oilActive? this->evalPt_.region->zgoc() : this->evalPt_.region->zwoc();
813 sg = this->fromDepthTable(gas_contact,
814 this->gasPos(), isIncr);
824 const auto pw = oilActive? this->press_.oil : this->press_.water;
825 const auto pcgo = this->press_.gas - pw;
826 sg = this->invertCapPress(pcgo, this->gasPos(), isIncr);
830template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
831void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::deriveWaterSat()
833 auto& sw = this->sat_.water;
835 const auto oilActive = this->evalPt_.ptable->oilActive();
838 sw = 1.0 - this->sat_.gas;
841 const auto isIncr =
false;
843 if (this->isConstCapPress(this->waterPos())) {
847 sw = this->fromDepthTable(this->evalPt_.region->zwoc(),
848 this->waterPos(), isIncr);
860 const auto pcow = this->press_.oil - this->press_.water;
862 if (this->swatInit_.empty()) {
863 sw = this->invertCapPress(pcow, this->waterPos(), isIncr);
866 auto [swout, newSwatInit] = this->applySwatInit(pcow);
868 sw = this->invertCapPress(pcow, this->waterPos(), isIncr);
877template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
878void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
879fixUnphysicalTransition()
881 auto& sg = this->sat_.gas;
882 auto& sw = this->sat_.water;
890 const auto pcgw = this->press_.gas - this->press_.water;
891 if (! this->swatInit_.empty()) {
895 auto [swout, newSwatInit] = this->applySwatInit(pcgw, sw);
897 const auto isIncr =
false;
898 sw = this->invertCapPress(pcgw, this->waterPos(), isIncr);
905 sw = satFromSumOfPcs<FluidSystem>
906 (this->matLawMgr_, this->waterPos(), this->gasPos(),
907 this->evalPt_.position->cell, pcgw);
910 this->fluidState_.setSaturation(this->oilPos(), 1.0 - sw - sg);
911 this->fluidState_.setSaturation(this->gasPos(), sg);
912 this->fluidState_.setSaturation(this->waterPos(), this->evalPt_
913 .ptable->waterActive() ? sw : 0.0);
916 this->computeMaterialLawCapPress();
917 this->press_.oil = this->press_.gas - this->materialLawCapPressGasOil();
920template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
921void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
922accountForScaledSaturations()
924 const auto gasActive = this->evalPt_.ptable->gasActive();
925 const auto watActive = this->evalPt_.ptable->waterActive();
926 const auto oilActive = this->evalPt_.ptable->oilActive();
928 auto sg = gasActive? this->sat_.gas : 0.0;
929 auto sw = watActive? this->sat_.water : 0.0;
930 auto so = oilActive? this->sat_.oil : 0.0;
932 this->fluidState_.setSaturation(this->waterPos(), sw);
933 this->fluidState_.setSaturation(this->oilPos(), so);
934 this->fluidState_.setSaturation(this->gasPos(), sg);
936 const auto& scaledDrainageInfo = this->matLawMgr_
937 .oilWaterScaledEpsInfoDrainage(this->evalPt_.position->cell);
939 const auto thresholdSat = 1.0e-6;
940 if (watActive && ((sw + thresholdSat) > scaledDrainageInfo.Swu)) {
944 this->fluidState_.setSaturation(this->waterPos(), scaledDrainageInfo.Swu);
946 this->fluidState_.setSaturation(this->oilPos(), so + sw - scaledDrainageInfo.Swu);
947 }
else if (gasActive) {
948 this->fluidState_.setSaturation(this->gasPos(), sg + sw - scaledDrainageInfo.Swu);
950 sw = scaledDrainageInfo.Swu;
951 this->computeMaterialLawCapPress();
955 this->press_.oil = this->press_.water + this->materialLawCapPressOilWater();
958 this->press_.gas = this->press_.water + this->materialLawCapPressGasWater();
962 if (gasActive && ((sg + thresholdSat) > scaledDrainageInfo.Sgu)) {
966 this->fluidState_.setSaturation(this->gasPos(), scaledDrainageInfo.Sgu);
968 this->fluidState_.setSaturation(this->oilPos(), so + sg - scaledDrainageInfo.Sgu);
969 }
else if (watActive) {
970 this->fluidState_.setSaturation(this->waterPos(), sw + sg - scaledDrainageInfo.Sgu);
972 sg = scaledDrainageInfo.Sgu;
973 this->computeMaterialLawCapPress();
977 this->press_.oil = this->press_.gas - this->materialLawCapPressGasOil();
980 this->press_.water = this->press_.gas - this->materialLawCapPressGasWater();
984 if (watActive && ((sw - thresholdSat) < scaledDrainageInfo.Swl)) {
988 this->fluidState_.setSaturation(this->waterPos(), scaledDrainageInfo.Swl);
990 this->fluidState_.setSaturation(this->oilPos(), so + sw - scaledDrainageInfo.Swl);
991 }
else if (gasActive) {
992 this->fluidState_.setSaturation(this->gasPos(), sg + sw - scaledDrainageInfo.Swl);
994 sw = scaledDrainageInfo.Swl;
995 this->computeMaterialLawCapPress();
999 this->press_.water = this->press_.oil - this->materialLawCapPressOilWater();
1002 this->press_.water = this->press_.gas - this->materialLawCapPressGasWater();
1006 if (gasActive && ((sg - thresholdSat) < scaledDrainageInfo.Sgl)) {
1010 this->fluidState_.setSaturation(this->gasPos(), scaledDrainageInfo.Sgl);
1012 this->fluidState_.setSaturation(this->oilPos(), so + sg - scaledDrainageInfo.Sgl);
1013 }
else if (watActive) {
1014 this->fluidState_.setSaturation(this->waterPos(), sw + sg - scaledDrainageInfo.Sgl);
1016 sg = scaledDrainageInfo.Sgl;
1017 this->computeMaterialLawCapPress();
1021 this->press_.gas = this->press_.oil + this->materialLawCapPressGasOil();
1024 this->press_.gas = this->press_.water + this->materialLawCapPressGasWater();
1029template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1030std::pair<typename FluidSystem::Scalar, bool>
1031PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1032applySwatInit(
const Scalar pcow)
1034 return this->applySwatInit(pcow, this->swatInit_[this->evalPt_.position->cell]);
1037template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1038std::pair<typename FluidSystem::Scalar, bool>
1039PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1040applySwatInit(
const Scalar pcow,
const Scalar sw)
1042 return this->matLawMgr_.applySwatinit(this->evalPt_.position->cell, pcow, sw);
1045template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1046void PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1047computeMaterialLawCapPress()
1049 const auto& matParams = this->matLawMgr_
1050 .materialLawParams(this->evalPt_.position->cell);
1052 this->matLawCapPress_.fill(0.0);
1053 MaterialLaw::capillaryPressures(this->matLawCapPress_,
1054 matParams, this->fluidState_);
1057template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1058typename FluidSystem::Scalar
1059PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1060materialLawCapPressGasOil()
const
1062 return this->matLawCapPress_[this->oilPos()]
1063 + this->matLawCapPress_[this->gasPos()];
1066template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1067typename FluidSystem::Scalar
1068PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1069materialLawCapPressOilWater()
const
1071 return this->matLawCapPress_[this->oilPos()]
1072 - this->matLawCapPress_[this->waterPos()];
1075template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1076typename FluidSystem::Scalar
1077PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1078materialLawCapPressGasWater()
const
1080 return this->matLawCapPress_[this->gasPos()]
1081 - this->matLawCapPress_[this->waterPos()];
1084template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1085bool PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1086isConstCapPress(
const PhaseIdx phaseIdx)
const
1088 return isConstPc<FluidSystem>
1089 (this->matLawMgr_, phaseIdx, this->evalPt_.position->cell);
1092template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1093bool PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1094isOverlappingTransition()
const
1096 return this->evalPt_.ptable->gasActive()
1097 && this->evalPt_.ptable->waterActive()
1098 && ((this->sat_.gas + this->sat_.water) > 1.0);
1101template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1102typename FluidSystem::Scalar
1103PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1104fromDepthTable(
const Scalar contactdepth,
1105 const PhaseIdx phasePos,
1106 const bool isincr)
const
1108 return satFromDepth<FluidSystem>
1109 (this->matLawMgr_, this->evalPt_.position->depth,
1110 contactdepth,
static_cast<int>(phasePos),
1111 this->evalPt_.position->cell, isincr);
1114template <
class MaterialLawManager,
class Flu
idSystem,
class Region,
typename CellID>
1115typename FluidSystem::Scalar
1116PhaseSaturations<MaterialLawManager, FluidSystem, Region, CellID>::
1117invertCapPress(
const Scalar pc,
1118 const PhaseIdx phasePos,
1119 const bool isincr)
const
1121 return satFromPc<FluidSystem>
1122 (this->matLawMgr_,
static_cast<int>(phasePos),
1123 this->evalPt_.position->cell, pc, isincr);
1126template<
class Flu
idSystem,
class Region>
1129 const int samplePoints)
1131 , nsample_(samplePoints)
1135template <
class Flu
idSystem,
class Region>
1138 : gravity_(rhs.gravity_)
1139 , nsample_(rhs.nsample_)
1141 this->copyInPointers(rhs);
1144template <
class Flu
idSystem,
class Region>
1147 : gravity_(rhs.gravity_)
1148 , nsample_(rhs.nsample_)
1149 , oil_ (std::move(rhs.oil_))
1150 , gas_ (std::move(rhs.gas_))
1151 , wat_ (std::move(rhs.wat_))
1155template <
class Flu
idSystem,
class Region>
1160 this->gravity_ = rhs.gravity_;
1161 this->nsample_ = rhs.nsample_;
1162 this->copyInPointers(rhs);
1167template <
class Flu
idSystem,
class Region>
1172 this->gravity_ = rhs.gravity_;
1173 this->nsample_ = rhs.nsample_;
1175 this->oil_ = std::move(rhs.oil_);
1176 this->gas_ = std::move(rhs.gas_);
1177 this->wat_ = std::move(rhs.wat_);
1182template <
class Flu
idSystem,
class Region>
1188 auto equil = this->selectEquilibrationStrategy(reg);
1190 (this->*equil)(reg, span);
1193template <
class Flu
idSystem,
class Region>
1197 return FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx);
1200template <
class Flu
idSystem,
class Region>
1204 return FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx);
1207template <
class Flu
idSystem,
class Region>
1211 return FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx);
1214template <
class Flu
idSystem,
class Region>
1215typename FluidSystem::Scalar
1219 this->checkPtr(this->oil_.get(),
"OIL");
1221 return this->oil_->value(depth);
1224template <
class Flu
idSystem,
class Region>
1225typename FluidSystem::Scalar
1229 this->checkPtr(this->gas_.get(),
"GAS");
1231 return this->gas_->value(depth);
1235template <
class Flu
idSystem,
class Region>
1236typename FluidSystem::Scalar
1240 this->checkPtr(this->wat_.get(),
"WATER");
1242 return this->wat_->value(depth);
1245template <
class Flu
idSystem,
class Region>
1247equil_WOG(
const Region& reg,
const VSpan& span)
1252 if (! this->waterActive()) {
1253 throw std::invalid_argument {
1254 "Don't know how to interpret EQUIL datum depth in "
1255 "WATER zone in model without active water phase"
1260 const auto ic =
typename WPress::InitCond {
1261 reg.datum(), reg.pressure()
1264 this->makeWatPressure(ic, reg, span);
1267 if (this->oilActive()) {
1269 const auto ic =
typename OPress::InitCond {
1271 this->water(reg.zwoc()) + reg.pcowWoc()
1274 this->makeOilPressure(ic, reg, span);
1277 if (this->gasActive() && this->oilActive()) {
1279 const auto ic =
typename GPress::InitCond {
1281 this->oil(reg.zgoc()) + reg.pcgoGoc()
1284 this->makeGasPressure(ic, reg, span);
1285 }
else if (this->gasActive() && !this->oilActive()) {
1287 const auto ic =
typename GPress::InitCond {
1289 this->water(reg.zwoc()) + reg.pcowWoc()
1291 this->makeGasPressure(ic, reg, span);
1295template <
class Flu
idSystem,
class Region>
1296void PressureTable<FluidSystem, Region>::
1297equil_GOW(
const Region& reg,
const VSpan& span)
1302 if (! this->gasActive()) {
1303 throw std::invalid_argument {
1304 "Don't know how to interpret EQUIL datum depth in "
1305 "GAS zone in model without active gas phase"
1310 const auto ic =
typename GPress::InitCond {
1311 reg.datum(), reg.pressure()
1314 this->makeGasPressure(ic, reg, span);
1317 if (this->oilActive()) {
1319 const auto ic =
typename OPress::InitCond {
1321 this->gas(reg.zgoc()) - reg.pcgoGoc()
1323 this->makeOilPressure(ic, reg, span);
1326 if (this->waterActive() && this->oilActive()) {
1328 const auto ic =
typename WPress::InitCond {
1330 this->oil(reg.zwoc()) - reg.pcowWoc()
1333 this->makeWatPressure(ic, reg, span);
1334 }
else if (this->waterActive() && !this->oilActive()) {
1336 const auto ic =
typename WPress::InitCond {
1338 this->gas(reg.zwoc()) - reg.pcowWoc()
1340 this->makeWatPressure(ic, reg, span);
1344template <
class Flu
idSystem,
class Region>
1345void PressureTable<FluidSystem, Region>::
1346equil_OWG(
const Region& reg,
const VSpan& span)
1351 if (! this->oilActive()) {
1352 throw std::invalid_argument {
1353 "Don't know how to interpret EQUIL datum depth in "
1354 "OIL zone in model without active oil phase"
1359 const auto ic =
typename OPress::InitCond {
1360 reg.datum(), reg.pressure()
1363 this->makeOilPressure(ic, reg, span);
1366 if (this->waterActive()) {
1368 const auto ic =
typename WPress::InitCond {
1370 this->oil(reg.zwoc()) - reg.pcowWoc()
1373 this->makeWatPressure(ic, reg, span);
1376 if (this->gasActive()) {
1378 const auto ic =
typename GPress::InitCond {
1380 this->oil(reg.zgoc()) + reg.pcgoGoc()
1382 this->makeGasPressure(ic, reg, span);
1386template <
class Flu
idSystem,
class Region>
1387void PressureTable<FluidSystem, Region>::
1388makeOilPressure(
const typename OPress::InitCond& ic,
1392 const auto drho = OilPressODE {
1393 reg.tempVdTable(), reg.dissolutionCalculator(),
1394 reg.pvtIdx(), this->gravity_
1397 this->oil_ = std::make_unique<OPress>(drho, ic, this->nsample_, span);
1400template <
class Flu
idSystem,
class Region>
1401void PressureTable<FluidSystem, Region>::
1402makeGasPressure(
const typename GPress::InitCond& ic,
1406 const auto drho = GasPressODE {
1407 reg.tempVdTable(), reg.evaporationCalculator(), reg.waterEvaporationCalculator(),
1408 reg.pvtIdx(), this->gravity_
1411 this->gas_ = std::make_unique<GPress>(drho, ic, this->nsample_, span);
1414template <
class Flu
idSystem,
class Region>
1415void PressureTable<FluidSystem, Region>::
1416makeWatPressure(
const typename WPress::InitCond& ic,
1420 const auto drho = WatPressODE {
1421 reg.tempVdTable(), reg.saltVdTable(), reg.pvtIdx(), this->gravity_
1424 this->wat_ = std::make_unique<WPress>(drho, ic, this->nsample_, span);
1429namespace DeckDependent {
1431std::vector<EquilRecord>
1434 const auto& init = state.getInitConfig();
1436 if(!init.hasEquil()) {
1437 throw std::domain_error(
"Deck does not provide equilibration data.");
1440 const auto& equil = init.getEquil();
1441 return { equil.begin(), equil.end() };
1444template<
class Gr
idView>
1447 const GridView& gridview)
1449 std::vector<int> eqlnum(gridview.size(0), 0);
1451 if (eclipseState.fieldProps().has_int(
"EQLNUM")) {
1452 const auto& e = eclipseState.fieldProps().get_int(
"EQLNUM");
1453 std::ranges::transform(e, eqlnum.begin(), [](
int n) { return n - 1; });
1456 const int num_regions = eclipseState.getTableManager().getEqldims().getNumEquilRegions();
1457 if (std::ranges::any_of(eqlnum, [num_regions](
int n){
return n >= num_regions;})) {
1458 throw std::runtime_error(
"Values larger than maximum Equil regions " +
1461 if (std::ranges::any_of(eqlnum, [](
int n){
return n < 0;})) {
1462 throw std::runtime_error(
"zero or negative values provided in EQLNUM");
1469template<
class FluidSystem,
1472 class ElementMapper,
1473 class CartesianIndexMapper>
1474template<
class MaterialLawManager>
1475InitialStateComputer<FluidSystem,
1479 CartesianIndexMapper>::
1480InitialStateComputer(MaterialLawManager& materialLawManager,
1481 const EclipseState& eclipseState,
1483 const GridView& gridView,
1484 const CartesianIndexMapper& cartMapper,
1486 const int num_pressure_points,
1487 const bool applySwatInit)
1488 : temperature_(grid.size(0), eclipseState.getTableManager().rtemp()),
1489 saltConcentration_(grid.size(0)),
1490 saltSaturation_(grid.size(0)),
1491 pp_(FluidSystem::numPhases,
1492 std::vector<Scalar>(grid.size(0))),
1493 sat_(FluidSystem::numPhases,
1494 std::vector<Scalar>(grid.size(0))),
1498 cartesianIndexMapper_(cartMapper),
1499 num_pressure_points_(num_pressure_points)
1502 if (applySwatInit) {
1503 if (eclipseState.fieldProps().has_double(
"SWATINIT")) {
1504 if constexpr (std::is_same_v<Scalar,double>) {
1505 swatInit_ = eclipseState.fieldProps().get_double(
"SWATINIT");
1507 const auto& input = eclipseState.fieldProps().get_double(
"SWATINIT");
1508 swatInit_.resize(input.size());
1509 std::ranges::copy(input, swatInit_.begin());
1516 const auto& num_aquifers = eclipseState.aquifer().numericalAquifers();
1517 updateCellProps_(gridView, num_aquifers);
1520 const std::vector<EquilRecord> rec =
getEquil(eclipseState);
1521 const auto& tables = eclipseState.getTableManager();
1523 const RegionMapping<> eqlmap(
equilnum(eclipseState, grid));
1524 const int invalidRegion = -1;
1525 regionPvtIdx_.resize(rec.size(), invalidRegion);
1526 setRegionPvtIdx(eclipseState, eqlmap);
1529 rsFunc_.reserve(rec.size());
1531 auto getArray = [](
const std::vector<double>& input)
1533 if constexpr (std::is_same_v<Scalar,double>) {
1536 std::vector<Scalar> output;
1537 output.resize(input.size());
1538 std::ranges::copy(input, output.begin());
1543 if (FluidSystem::enableDissolvedGas()) {
1544 for (std::size_t i = 0; i < rec.size(); ++i) {
1545 if (eqlmap.cells(i).empty()) {
1549 const int pvtIdx = regionPvtIdx_[i];
1550 if (!rec[i].liveOilInitConstantRs()) {
1551 const TableContainer& rsvdTables = tables.getRsvdTables();
1552 const TableContainer& pbvdTables = tables.getPbvdTables();
1553 if (rsvdTables.size() > 0) {
1554 const RsvdTable& rsvdTable = rsvdTables.getTable<RsvdTable>(i);
1555 auto depthColumn = getArray(rsvdTable.getColumn(
"DEPTH").vectorCopy());
1556 auto rsColumn = getArray(rsvdTable.getColumn(
"RS").vectorCopy());
1558 depthColumn, rsColumn));
1559 }
else if (pbvdTables.size() > 0) {
1560 const PbvdTable& pbvdTable = pbvdTables.getTable<PbvdTable>(i);
1561 auto depthColumn = getArray(pbvdTable.getColumn(
"DEPTH").vectorCopy());
1562 auto pbubColumn = getArray(pbvdTable.getColumn(
"PBUB").vectorCopy());
1564 depthColumn, pbubColumn));
1567 throw std::runtime_error(
"Cannot initialise: RSVD or PBVD table not available.");
1572 if (rec[i].gasOilContactDepth() != rec[i].datumDepth()) {
1573 throw std::runtime_error(
"Cannot initialise: when no explicit RSVD table is given, \n"
1574 "datum depth must be at the gas-oil-contact. "
1575 "In EQUIL region "+
std::to_string(i + 1)+
" (counting from 1), this does not hold.");
1577 const Scalar pContact = rec[i].datumDepthPressure();
1578 const Scalar TContact = 273.15 + 20;
1583 else if (FluidSystem::enableConstantRs() && tables.hasTables(
"RSCONST")) {
1584 const auto& rsconstTables = tables.getRsconstTables();
1586 if (rsconstTables.empty()) {
1587 for (std::size_t i = 0; i < rec.size(); ++i) {
1593 const auto& rsconstTable = rsconstTables.getTable<RsconstTable>(0);
1595 const auto rsConst = rsconstTable.getRsColumn().front();
1596 const auto pBub = rsconstTable.getPbubColumn().front();
1598 const auto& units = eclipseState.getUnits();
1600 OpmLog::info(fmt::format(
"Using RSCONST keyword: Rs = {:.2} [{}], Pb = {:.2} [{}]",
1601 units.from_si(UnitSystem::measure::gas_oil_ratio, rsConst),
1602 units.name (UnitSystem::measure::gas_oil_ratio),
1603 units.from_si(UnitSystem::measure::pressure, pBub),
1604 units.name (UnitSystem::measure::pressure)));
1606 for (std::size_t i = 0; i < rec.size(); ++i) {
1612 for (std::size_t i = 0; i < rec.size(); ++i) {
1618 rvFunc_.reserve(rec.size());
1619 if (FluidSystem::enableVaporizedOil()) {
1620 for (std::size_t i = 0; i < rec.size(); ++i) {
1621 if (eqlmap.cells(i).empty()) {
1625 const int pvtIdx = regionPvtIdx_[i];
1626 if (!rec[i].wetGasInitConstantRv()) {
1627 const TableContainer& rvvdTables = tables.getRvvdTables();
1628 const TableContainer& pdvdTables = tables.getPdvdTables();
1630 if (rvvdTables.size() > 0) {
1631 const RvvdTable& rvvdTable = rvvdTables.getTable<RvvdTable>(i);
1632 auto depthColumn = getArray(rvvdTable.getColumn(
"DEPTH").vectorCopy());
1633 auto rvColumn = getArray(rvvdTable.getColumn(
"RV").vectorCopy());
1635 depthColumn, rvColumn));
1636 }
else if (pdvdTables.size() > 0) {
1637 const PdvdTable& pdvdTable = pdvdTables.getTable<PdvdTable>(i);
1638 auto depthColumn = getArray(pdvdTable.getColumn(
"DEPTH").vectorCopy());
1639 auto pdewColumn = getArray(pdvdTable.getColumn(
"PDEW").vectorCopy());
1641 depthColumn, pdewColumn));
1643 throw std::runtime_error(
"Cannot initialise: RVVD or PDCD table not available.");
1647 if (rec[i].gasOilContactDepth() != rec[i].datumDepth()) {
1648 throw std::runtime_error(
1649 "Cannot initialise: when no explicit RVVD table is given, \n"
1650 "datum depth must be at the gas-oil-contact. "
1651 "In EQUIL region "+
std::to_string(i + 1)+
" (counting from 1), this does not hold.");
1653 const Scalar pContact = rec[i].datumDepthPressure() + rec[i].gasOilContactCapillaryPressure();
1654 const Scalar TContact = 273.15 + 20;
1660 for (std::size_t i = 0; i < rec.size(); ++i) {
1665 rvwFunc_.reserve(rec.size());
1666 if (FluidSystem::enableVaporizedWater()) {
1667 for (std::size_t i = 0; i < rec.size(); ++i) {
1668 if (eqlmap.cells(i).empty()) {
1672 const int pvtIdx = regionPvtIdx_[i];
1673 if (!rec[i].humidGasInitConstantRvw()) {
1674 const TableContainer& rvwvdTables = tables.getRvwvdTables();
1676 if (rvwvdTables.size() > 0) {
1677 const RvwvdTable& rvwvdTable = rvwvdTables.getTable<RvwvdTable>(i);
1678 auto depthColumn = getArray(rvwvdTable.getColumn(
"DEPTH").vectorCopy());
1679 auto rvwvdColumn = getArray(rvwvdTable.getColumn(
"RVWVD").vectorCopy());
1681 depthColumn, rvwvdColumn));
1683 throw std::runtime_error(
"Cannot initialise: RVWVD table not available.");
1687 const auto oilActive = FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx);
1689 if (rec[i].gasOilContactDepth() != rec[i].datumDepth()) {
1691 const auto msg =
"No explicit RVWVD table is given for EQUIL region " +
std::to_string(i + 1) +
". \n"
1692 "and datum depth is not at the gas-oil-contact. \n"
1693 "Rvw is set to 0.0 in all cells. \n";
1694 OpmLog::warning(msg);
1698 const Scalar pContact = rec[i].datumDepthPressure() + rec[i].gasOilContactCapillaryPressure();
1699 const Scalar TContact = 273.15 + 20;
1706 if (rec[i].waterOilContactDepth() != rec[i].datumDepth()) {
1708 const auto msg =
"No explicit RVWVD table is given for EQUIL region " +
std::to_string(i + 1) +
". \n"
1709 "and datum depth is not at the gas-water-contact. \n"
1710 "Rvw is set to 0.0 in all cells. \n";
1711 OpmLog::warning(msg);
1714 const Scalar pContact = rec[i].datumDepthPressure() + rec[i].waterOilContactCapillaryPressure();
1715 const Scalar TContact = 273.15 + 20;
1723 for (std::size_t i = 0; i < rec.size(); ++i) {
1729 updateInitialTemperature_(eclipseState, eqlmap);
1732 updateInitialSaltConcentration_(eclipseState, eqlmap);
1735 updateInitialSaltSaturation_(eclipseState, eqlmap);
1738 const auto& comm = grid.comm();
1739 calcPressSatRsRv(eqlmap, rec, materialLawManager, gridView, comm, grav);
1742 applyNumericalAquifers_(gridView, num_aquifers,
1743 eclipseState.runspec().co2Storage() ||
1744 eclipseState.runspec().h2Storage());
1750template<
class FluidSystem,
1753 class ElementMapper,
1754 class CartesianIndexMapper>
1760 CartesianIndexMapper>::
1761updateInitialTemperature_(
const EclipseState& eclState,
const RMap& reg)
1763 const int numEquilReg = rsFunc_.size();
1764 tempVdTable_.resize(numEquilReg);
1765 const auto& tables = eclState.getTableManager();
1766 if (!tables.hasTables(
"RTEMPVD")) {
1767 std::vector<Scalar> x = {0.0,1.0};
1768 std::vector<Scalar> y = {
static_cast<Scalar
>(tables.rtemp()),
1769 static_cast<Scalar
>(tables.rtemp())};
1770 for (
auto& table : this->tempVdTable_) {
1771 table.setXYContainers(x, y);
1774 const TableContainer& tempvdTables = tables.getRtempvdTables();
1775 for (std::size_t i = 0; i < tempvdTables.size(); ++i) {
1776 const RtempvdTable& tempvdTable = tempvdTables.getTable<RtempvdTable>(i);
1777 tempVdTable_[i].setXYContainers(tempvdTable.getDepthColumn(), tempvdTable.getTemperatureColumn());
1778 const auto& cells = reg.cells(i);
1779 for (
const auto& cell : cells) {
1780 const Scalar depth = cellCenterDepth_[cell];
1781 this->temperature_[cell] = tempVdTable_[i].eval(depth,
true);
1787template<
class FluidSystem,
1790 class ElementMapper,
1791 class CartesianIndexMapper>
1793void InitialStateComputer<FluidSystem,
1797 CartesianIndexMapper>::
1798updateInitialSaltConcentration_(
const EclipseState& eclState,
const RMap& reg)
1800 const int numEquilReg = rsFunc_.size();
1801 saltVdTable_.resize(numEquilReg);
1802 const auto& tables = eclState.getTableManager();
1803 const TableContainer& saltvdTables = tables.getSaltvdTables();
1806 if (saltvdTables.empty()) {
1807 std::vector<Scalar> x = {0.0,1.0};
1808 std::vector<Scalar> y = {0.0,0.0};
1809 for (
auto& table : this->saltVdTable_) {
1810 table.setXYContainers(x, y);
1813 for (std::size_t i = 0; i < saltvdTables.size(); ++i) {
1814 const SaltvdTable& saltvdTable = saltvdTables.getTable<SaltvdTable>(i);
1815 saltVdTable_[i].setXYContainers(saltvdTable.getDepthColumn(), saltvdTable.getSaltColumn());
1817 const auto& cells = reg.cells(i);
1818 for (
const auto& cell : cells) {
1819 const Scalar depth = cellCenterDepth_[cell];
1820 this->saltConcentration_[cell] = saltVdTable_[i].eval(depth,
true);
1826template<
class FluidSystem,
1829 class ElementMapper,
1830 class CartesianIndexMapper>
1832void InitialStateComputer<FluidSystem,
1836 CartesianIndexMapper>::
1837updateInitialSaltSaturation_(
const EclipseState& eclState,
const RMap& reg)
1839 const int numEquilReg = rsFunc_.size();
1840 saltpVdTable_.resize(numEquilReg);
1841 const auto& tables = eclState.getTableManager();
1842 const TableContainer& saltpvdTables = tables.getSaltpvdTables();
1844 for (std::size_t i = 0; i < saltpvdTables.size(); ++i) {
1845 const SaltpvdTable& saltpvdTable = saltpvdTables.getTable<SaltpvdTable>(i);
1846 saltpVdTable_[i].setXYContainers(saltpvdTable.getDepthColumn(), saltpvdTable.getSaltpColumn());
1848 const auto& cells = reg.cells(i);
1849 for (
const auto& cell : cells) {
1850 const Scalar depth = cellCenterDepth_[cell];
1851 this->saltSaturation_[cell] = saltpVdTable_[i].eval(depth,
true);
1856template<
class FluidSystem,
1859 class ElementMapper,
1860 class CartesianIndexMapper>
1861void InitialStateComputer<FluidSystem,
1865 CartesianIndexMapper>::
1866updateCellProps_(
const GridView& gridView,
1867 const NumericalAquifers& aquifer)
1869 ElementMapper elemMapper(gridView, Dune::mcmgElementLayout());
1870 int numElements = gridView.size(0);
1871 cellCenterDepth_.resize(numElements);
1872 cellCenterXY_.resize(numElements);
1873 cellCorners_.resize(numElements);
1874 cellZSpan_.resize(numElements);
1875 cellZMinMax_.resize(numElements);
1877 auto elemIt = gridView.template begin<0>();
1878 const auto& elemEndIt = gridView.template end<0>();
1879 const auto num_aqu_cells = aquifer.allAquiferCells();
1880 for (; elemIt != elemEndIt; ++elemIt) {
1881 const Element& element = *elemIt;
1882 const unsigned int elemIdx = elemMapper.index(element);
1883 cellCenterDepth_[elemIdx] = Details::cellCenterDepth<Scalar>(element);
1884 cellCenterXY_[elemIdx] = Details::cellCenterXY<Scalar>(element);
1885 cellCorners_[elemIdx] = Details::getCellCornerXY<Scalar>(element);
1886 const auto cartIx = cartesianIndexMapper_.cartesianIndex(elemIdx);
1887 cellZSpan_[elemIdx] = Details::cellZSpan<Scalar>(element);
1888 cellZMinMax_[elemIdx] = Details::cellZMinMax<Scalar>(element);
1889 if (!num_aqu_cells.empty()) {
1890 const auto search = num_aqu_cells.find(cartIx);
1891 if (search != num_aqu_cells.end()) {
1892 const auto* aqu_cell = num_aqu_cells.at(cartIx);
1893 const Scalar depth_change_num_aqu = aqu_cell->depth - cellCenterDepth_[elemIdx];
1894 cellCenterDepth_[elemIdx] += depth_change_num_aqu;
1895 cellZSpan_[elemIdx].first += depth_change_num_aqu;
1896 cellZSpan_[elemIdx].second += depth_change_num_aqu;
1897 cellZMinMax_[elemIdx].first += depth_change_num_aqu;
1898 cellZMinMax_[elemIdx].second += depth_change_num_aqu;
1904template<
class FluidSystem,
1907 class ElementMapper,
1908 class CartesianIndexMapper>
1909void InitialStateComputer<FluidSystem,
1913 CartesianIndexMapper>::
1914applyNumericalAquifers_(
const GridView& gridView,
1915 const NumericalAquifers& aquifer,
1916 const bool co2store_or_h2store)
1918 const auto num_aqu_cells = aquifer.allAquiferCells();
1919 if (num_aqu_cells.empty())
return;
1922 bool oil_as_brine = co2store_or_h2store && FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx);
1923 const auto watPos = oil_as_brine? FluidSystem::oilPhaseIdx : FluidSystem::waterPhaseIdx;
1924 if (!FluidSystem::phaseIsActive(watPos)){
1925 throw std::logic_error {
"Water phase has to be active for numerical aquifer case" };
1928 ElementMapper elemMapper(gridView, Dune::mcmgElementLayout());
1929 auto elemIt = gridView.template begin<0>();
1930 const auto& elemEndIt = gridView.template end<0>();
1931 const auto oilPos = FluidSystem::oilPhaseIdx;
1932 const auto gasPos = FluidSystem::gasPhaseIdx;
1933 for (; elemIt != elemEndIt; ++elemIt) {
1934 const Element& element = *elemIt;
1935 const unsigned int elemIdx = elemMapper.index(element);
1936 const auto cartIx = cartesianIndexMapper_.cartesianIndex(elemIdx);
1937 const auto search = num_aqu_cells.find(cartIx);
1938 if (search != num_aqu_cells.end()) {
1940 this->sat_[watPos][elemIdx] = 1.;
1942 if (!co2store_or_h2store && FluidSystem::phaseIsActive(oilPos)) {
1943 this->sat_[oilPos][elemIdx] = 0.;
1946 if (FluidSystem::phaseIsActive(gasPos)) {
1947 this->sat_[gasPos][elemIdx] = 0.;
1949 const auto* aqu_cell = num_aqu_cells.at(cartIx);
1950 const auto msg = fmt::format(
"FOR AQUIFER CELL AT ({}, {}, {}) OF NUMERICAL "
1951 "AQUIFER {}, WATER SATURATION IS SET TO BE UNITY",
1952 aqu_cell->I+1, aqu_cell->J+1, aqu_cell->K+1, aqu_cell->aquifer_id);
1957 if (aqu_cell->init_pressure) {
1958 const Scalar pres = *(aqu_cell->init_pressure);
1959 this->pp_[watPos][elemIdx] = pres;
1960 if (FluidSystem::phaseIsActive(gasPos)) {
1961 this->pp_[gasPos][elemIdx] = pres;
1963 if (FluidSystem::phaseIsActive(oilPos)) {
1964 this->pp_[oilPos][elemIdx] = pres;
1971template<
class FluidSystem,
1974 class ElementMapper,
1975 class CartesianIndexMapper>
1977void InitialStateComputer<FluidSystem,
1981 CartesianIndexMapper>::
1982setRegionPvtIdx(
const EclipseState& eclState,
const RMap& reg)
1984 const auto& pvtnumData = eclState.fieldProps().get_int(
"PVTNUM");
1986 for (
const auto& r : reg.activeRegions()) {
1987 const auto& cells = reg.cells(r);
1988 regionPvtIdx_[r] = pvtnumData[*cells.begin()] - 1;
1992template<
class FluidSystem,
1995 class ElementMapper,
1996 class CartesianIndexMapper>
1997template<
class RMap,
class MaterialLawManager,
class Comm>
1998void InitialStateComputer<FluidSystem,
2002 CartesianIndexMapper>::
2003calcPressSatRsRv(
const RMap& reg,
2004 const std::vector<EquilRecord>& rec,
2005 MaterialLawManager& materialLawManager,
2006 const GridView& gridView,
2010 using PhaseSat = Details::PhaseSaturations<
2011 MaterialLawManager, FluidSystem, EquilReg<Scalar>,
typename RMap::CellId
2014 auto ptable = Details::PressureTable<FluidSystem, EquilReg<Scalar>>{ grav, this->num_pressure_points_ };
2015 auto psat = PhaseSat { materialLawManager, this->swatInit_ };
2016 auto vspan = std::array<Scalar, 2>{};
2018 std::vector<int> regionIsEmpty(rec.size(), 0);
2019 for (std::size_t r = 0; r < rec.size(); ++r) {
2020 const auto& cells = reg.cells(r);
2024 const auto acc = rec[r].initializationTargetAccuracy();
2028 if (cells.empty()) {
2029 regionIsEmpty[r] = 1;
2032 const auto eqreg = EquilReg {
2033 rec[r], this->rsFunc_[r], this->rvFunc_[r], this->rvwFunc_[r],
2034 this->tempVdTable_[r], this->saltVdTable_[r], this->regionPvtIdx_[r]
2037 vspan[0] = std::min(vspan[0], std::min(eqreg.zgoc(), eqreg.zwoc()));
2038 vspan[1] = std::max(vspan[1], std::max(eqreg.zgoc(), eqreg.zwoc()));
2039 ptable.equilibrate(eqreg, vspan);
2042 this->equilibrateTiltedFaultBlock(cells, eqreg, gridView, acc, ptable, psat);
2044 else if (acc == 0) {
2045 if (cells.empty()) {
2046 regionIsEmpty[r] = 1;
2049 const auto eqreg = EquilReg {
2050 rec[r], this->rsFunc_[r], this->rvFunc_[r], this->rvwFunc_[r],
2051 this->tempVdTable_[r], this->saltVdTable_[r], this->regionPvtIdx_[r]
2053 vspan[0] = std::min(vspan[0], std::min(eqreg.zgoc(), eqreg.zwoc()));
2054 vspan[1] = std::max(vspan[1], std::max(eqreg.zgoc(), eqreg.zwoc()));
2055 ptable.equilibrate(eqreg, vspan);
2057 this->equilibrateCellCentres(cells, eqreg, ptable, psat);
2060 if (cells.empty()) {
2061 regionIsEmpty[r] = 1;
2064 const auto eqreg = EquilReg {
2065 rec[r], this->rsFunc_[r], this->rvFunc_[r], this->rvwFunc_[r],
2066 this->tempVdTable_[r], this->saltVdTable_[r], this->regionPvtIdx_[r]
2068 vspan[0] = std::min(vspan[0], std::min(eqreg.zgoc(), eqreg.zwoc()));
2069 vspan[1] = std::max(vspan[1], std::max(eqreg.zgoc(), eqreg.zwoc()));
2070 ptable.equilibrate(eqreg, vspan);
2072 this->equilibrateHorizontal(cells, eqreg, -acc, ptable, psat);
2075 comm.min(regionIsEmpty.data(),regionIsEmpty.size());
2076 if (comm.rank() == 0) {
2077 for (std::size_t r = 0; r < rec.size(); ++r) {
2078 if (regionIsEmpty[r])
2080 +
" has no active cells");
2085template<
class FluidSystem,
2088 class ElementMapper,
2089 class CartesianIndexMapper>
2090template<
class CellRange,
class EquilibrationMethod>
2091void InitialStateComputer<FluidSystem,
2095 CartesianIndexMapper>::
2096cellLoop(
const CellRange& cells,
2097 EquilibrationMethod&& eqmethod)
2099 const auto oilPos = FluidSystem::oilPhaseIdx;
2100 const auto gasPos = FluidSystem::gasPhaseIdx;
2101 const auto watPos = FluidSystem::waterPhaseIdx;
2103 const auto oilActive = FluidSystem::phaseIsActive(oilPos);
2104 const auto gasActive = FluidSystem::phaseIsActive(gasPos);
2105 const auto watActive = FluidSystem::phaseIsActive(watPos);
2107 auto pressures = Details::PhaseQuantityValue<Scalar>{};
2108 auto saturations = Details::PhaseQuantityValue<Scalar>{};
2113 for (
const auto& cell : cells) {
2114 eqmethod(cell, pressures, saturations, Rs, Rv, Rvw);
2117 this->pp_ [oilPos][cell] = pressures.oil;
2118 this->sat_[oilPos][cell] = saturations.oil;
2122 this->pp_ [gasPos][cell] = pressures.gas;
2123 this->sat_[gasPos][cell] = saturations.gas;
2127 this->pp_ [watPos][cell] = pressures.water;
2128 this->sat_[watPos][cell] = saturations.water;
2131 if (oilActive && gasActive) {
2132 this->rs_[cell] =
Rs;
2133 this->rv_[cell] =
Rv;
2136 if (watActive && gasActive) {
2137 this->rvw_[cell] =
Rvw;
2142template<
class FluidSystem,
2145 class ElementMapper,
2146 class CartesianIndexMapper>
2147template<
class CellRange,
class PressTable,
class PhaseSat>
2148void InitialStateComputer<FluidSystem,
2152 CartesianIndexMapper>::
2153equilibrateCellCentres(
const CellRange& cells,
2154 const EquilReg<Scalar>& eqreg,
2155 const PressTable& ptable,
2158 using CellPos =
typename PhaseSat::Position;
2159 using CellID = std::remove_cv_t<std::remove_reference_t<
2160 decltype(std::declval<CellPos>().cell)>>;
2161 this->cellLoop(cells, [
this, &eqreg, &ptable, &psat]
2163 Details::PhaseQuantityValue<Scalar>& pressures,
2164 Details::PhaseQuantityValue<Scalar>& saturations,
2167 Scalar& Rvw) ->
void
2169 const auto pos = CellPos {
2170 cell, cellCenterDepth_[cell]
2173 saturations = psat.deriveSaturations(pos, eqreg, ptable);
2174 pressures = psat.correctedPhasePressures();
2176 const auto temp = this->temperature_[cell];
2178 Rs = eqreg.dissolutionCalculator()
2179 (pos.depth, pressures.oil, temp, saturations.gas);
2181 Rv = eqreg.evaporationCalculator()
2182 (pos.depth, pressures.gas, temp, saturations.oil);
2184 Rvw = eqreg.waterEvaporationCalculator()
2185 (pos.depth, pressures.gas, temp, saturations.water);
2189template<
class FluidSystem,
2192 class ElementMapper,
2193 class CartesianIndexMapper>
2194template<
class CellRange,
class PressTable,
class PhaseSat>
2195void InitialStateComputer<FluidSystem,
2199 CartesianIndexMapper>::
2200equilibrateHorizontal(
const CellRange& cells,
2201 const EquilReg<Scalar>& eqreg,
2203 const PressTable& ptable,
2206 using CellPos =
typename PhaseSat::Position;
2207 using CellID = std::remove_cv_t<std::remove_reference_t<
2208 decltype(std::declval<CellPos>().cell)>>;
2210 this->cellLoop(cells, [
this, acc, &eqreg, &ptable, &psat]
2212 Details::PhaseQuantityValue<Scalar>& pressures,
2213 Details::PhaseQuantityValue<Scalar>& saturations,
2216 Scalar& Rvw) ->
void
2219 saturations.reset();
2221 Scalar totfrac = 0.0;
2223 const auto pos = CellPos { cell, depth };
2225 saturations.axpy(psat.deriveSaturations(pos, eqreg, ptable), frac);
2226 pressures .axpy(psat.correctedPhasePressures(), frac);
2232 saturations /= totfrac;
2233 pressures /= totfrac;
2236 const auto pos = CellPos {
2237 cell, cellCenterDepth_[cell]
2240 saturations = psat.deriveSaturations(pos, eqreg, ptable);
2241 pressures = psat.correctedPhasePressures();
2244 const auto temp = this->temperature_[cell];
2245 const auto cz = cellCenterDepth_[cell];
2247 Rs = eqreg.dissolutionCalculator()
2248 (cz, pressures.oil, temp, saturations.gas);
2250 Rv = eqreg.evaporationCalculator()
2251 (cz, pressures.gas, temp, saturations.oil);
2253 Rvw = eqreg.waterEvaporationCalculator()
2254 (cz, pressures.gas, temp, saturations.water);
2258template<
class Flu
idSystem,
class Gr
id,
class Gr
idView,
class ElementMapper,
class CartesianIndexMapper>
2259template<
class CellRange,
class PressTable,
class PhaseSat>
2260void InitialStateComputer<FluidSystem, Grid, GridView, ElementMapper, CartesianIndexMapper>::
2261equilibrateTiltedFaultBlockSimple(
const CellRange& cells,
2262 const EquilReg<Scalar>& eqreg,
2263 const GridView& gridView,
2265 const PressTable& ptable,
2268 using CellPos =
typename PhaseSat::Position;
2269 using CellID = std::remove_cv_t<std::remove_reference_t<
2270 decltype(std::declval<CellPos>().cell)>>;
2272 this->cellLoop(cells, [
this, acc, &eqreg, &ptable, &psat, &gridView]
2274 Details::PhaseQuantityValue<Scalar>& pressures,
2275 Details::PhaseQuantityValue<Scalar>& saturations,
2278 Scalar& Rvw) ->
void
2281 saturations.reset();
2282 Scalar totalWeight = 0.0;
2285 const auto& [zmin, zmax] = cellZMinMax_[cell];
2286 const Scalar cellThickness = zmax - zmin;
2287 const Scalar halfThickness = cellThickness / 2.0;
2290 Scalar dipAngle, dipAzimuth;
2294 std::array<Scalar, 3> referencePoint = {
2295 cellCenterXY_[cell].first,
2296 cellCenterXY_[cell].second,
2297 cellCenterDepth_[cell]
2301 const int numLevelsPerHalf = std::min(20, acc);
2304 std::vector<std::pair<Scalar, Scalar>> levels;
2307 for (
int side = 0; side < 2; ++side) {
2308 Scalar halfStart = (side == 0) ? zmin : zmin + halfThickness;
2310 for (
int i = 0; i < numLevelsPerHalf; ++i) {
2312 Scalar depth = halfStart + (i + 0.5) * (halfThickness / numLevelsPerHalf);
2316 Scalar crossSectionWeight = (halfThickness / numLevelsPerHalf);
2319 if (std::abs(dipAngle) > 1e-10) {
2320 crossSectionWeight /= std::cos(dipAngle);
2323 levels.emplace_back(depth, crossSectionWeight);
2327 for (
const auto& [depth, weight] : levels) {
2329 const auto& [x, y] = cellCenterXY_[cell];
2331 depth, x, y, dipAngle, dipAzimuth, referencePoint);
2333 const auto pos = CellPos{cell, tvd};
2335 auto localSaturations = psat.deriveSaturations(pos, eqreg, ptable);
2336 auto localPressures = psat.correctedPhasePressures();
2339 saturations.axpy(localSaturations, weight);
2340 pressures.axpy(localPressures, weight);
2341 totalWeight += weight;
2345 if (totalWeight > 1e-10) {
2346 saturations /= totalWeight;
2347 pressures /= totalWeight;
2350 const auto& [x, y] = cellCenterXY_[cell];
2352 cellCenterDepth_[cell], x, y, dipAngle, dipAzimuth, referencePoint);
2353 const auto pos = CellPos{cell, tvdCenter};
2354 saturations = psat.deriveSaturations(pos, eqreg, ptable);
2355 pressures = psat.correctedPhasePressures();
2359 const auto temp = this->temperature_[cell];
2360 const auto& [x, y] = cellCenterXY_[cell];
2362 cellCenterDepth_[cell], x, y, dipAngle, dipAzimuth, referencePoint);
2364 Rs = eqreg.dissolutionCalculator()(tvdCenter, pressures.oil, temp, saturations.gas);
2365 Rv = eqreg.evaporationCalculator()(tvdCenter, pressures.gas, temp, saturations.oil);
2366 Rvw = eqreg.waterEvaporationCalculator()(tvdCenter, pressures.gas, temp, saturations.water);
2370template<
class Flu
idSystem,
class Gr
id,
class Gr
idView,
class ElementMapper,
class CartesianIndexMapper>
2371template<
class CellRange,
class PressTable,
class PhaseSat>
2372void InitialStateComputer<FluidSystem, Grid, GridView, ElementMapper, CartesianIndexMapper>::
2373equilibrateTiltedFaultBlock(
const CellRange& cells,
2374 const EquilReg<Scalar>& eqreg,
2375 const GridView& gridView,
2377 const PressTable& ptable,
2380 using CellPos =
typename PhaseSat::Position;
2381 using CellID = std::remove_cv_t<std::remove_reference_t<
2382 decltype(std::declval<CellPos>().cell)>>;
2384 std::vector<typename GridView::template Codim<0>::Entity> entityMap(gridView.size(0));
2385 for (
const auto& entity : entities(gridView, Dune::Codim<0>())) {
2386 CellID idx = gridView.indexSet().index(entity);
2387 entityMap[idx] = entity;
2391 auto polygonArea = [](
const std::vector<std::array<Scalar, 2>>& pts) {
2392 if (pts.size() < 3)
return Scalar(0);
2394 for (
size_t i = 0; i < pts.size(); ++i) {
2395 size_t j = (i + 1) % pts.size();
2396 area += pts[i][0] * pts[j][1] - pts[j][0] * pts[i][1];
2398 return std::abs(area) * Scalar(0.5);
2402 auto computeCrossSectionArea = [&](
const CellID cell, Scalar depth) -> Scalar {
2404 const auto& entity = entityMap[cell];
2405 const auto& geometry = entity.geometry();
2406 const int numCorners = geometry.corners();
2408 std::vector<std::array<Scalar, 3>> corners(numCorners);
2409 for (
int i = 0; i < numCorners; ++i) {
2410 const auto& corner = geometry.corner(i);
2411 corners[i] = {
static_cast<Scalar
>(corner[0]),
static_cast<Scalar
>(corner[1]),
static_cast<Scalar
>(corner[2])};
2415 std::vector<std::array<Scalar, 2>> intersectionPoints;
2416 const Scalar tol = 1e-10;
2419 for (
size_t i = 0; i < corners.size(); ++i) {
2420 for (
size_t j = i + 1; j < corners.size(); ++j) {
2421 Scalar za = corners[i][2];
2422 Scalar zb = corners[j][2];
2424 if ((za - depth) * (zb - depth) <= 0.0 && std::abs(za - zb) > tol) {
2426 Scalar t = (depth - za) / (zb - za);
2427 Scalar x = corners[i][0] + t * (corners[j][0] - corners[i][0]);
2428 Scalar y = corners[i][1] + t * (corners[j][1] - corners[i][1]);
2429 intersectionPoints.push_back({x, y});
2435 if (intersectionPoints.size() > 1) {
2436 auto pointsEqual = [tol](
const std::array<Scalar, 2>& a,
const std::array<Scalar, 2>& b) {
2437 return std::abs(a[0] - b[0]) < tol && std::abs(a[1] - b[1]) < tol;
2440 intersectionPoints.erase(
2441 std::unique(intersectionPoints.begin(), intersectionPoints.end(), pointsEqual),
2442 intersectionPoints.end()
2446 if (intersectionPoints.size() < 3) {
2452 Scalar cx = 0, cy = 0;
2453 for (
const auto& p : intersectionPoints) {
2454 cx += p[0]; cy += p[1];
2456 cx /= intersectionPoints.size();
2457 cy /= intersectionPoints.size();
2460 auto angleCompare = [cx, cy](
const std::array<Scalar, 2>& a,
const std::array<Scalar, 2>& b) {
2461 return std::atan2(a[1] - cy, a[0] - cx) < std::atan2(b[1] - cy, b[0] - cx);
2464 std::ranges::sort(intersectionPoints, angleCompare);
2466 return polygonArea(intersectionPoints);
2468 }
catch (
const std::exception& e) {
2473 auto cellProcessor = [
this, acc, &eqreg, &ptable, &psat, &computeCrossSectionArea]
2475 Details::PhaseQuantityValue<Scalar>& pressures,
2476 Details::PhaseQuantityValue<Scalar>& saturations,
2479 Scalar&
Rvw) ->
void
2482 saturations.reset();
2483 Scalar totalWeight = 0.0;
2485 const auto& zmin = this->cellZMinMax_[cell].first;
2486 const auto& zmax = this->cellZMinMax_[cell].second;
2487 const Scalar cellThickness = zmax - zmin;
2488 const Scalar halfThickness = cellThickness / 2.0;
2491 Scalar dipAngle, dipAzimuth;
2495 std::array<Scalar, 3> referencePoint = {
2496 this->cellCenterXY_[cell].first,
2497 this->cellCenterXY_[cell].second,
2498 cellCenterDepth_[cell]
2502 const int numLevelsPerHalf = std::min(20, acc);
2505 std::vector<std::pair<Scalar, Scalar>> levels;
2508 for (
int side = 0; side < 2; ++side) {
2509 Scalar halfStart = (side == 0) ? zmin : zmin + halfThickness;
2511 for (
int i = 0; i < numLevelsPerHalf; ++i) {
2513 Scalar depth = halfStart + (i + 0.5) * (halfThickness / numLevelsPerHalf);
2516 Scalar crossSectionArea = computeCrossSectionArea(cell, depth);
2519 Scalar weight = crossSectionArea * (halfThickness / numLevelsPerHalf);
2521 levels.emplace_back(depth, weight);
2526 bool hasValidAreas =
false;
2527 for (
const auto& level : levels) {
2528 if (level.second > 1e-10) {
2529 hasValidAreas =
true;
2534 if (!hasValidAreas) {
2537 for (
int side = 0; side < 2; ++side) {
2538 Scalar halfStart = (side == 0) ? zmin : zmin + halfThickness;
2539 for (
int i = 0; i < numLevelsPerHalf; ++i) {
2540 Scalar depth = halfStart + (i + 0.5) * (halfThickness / numLevelsPerHalf);
2541 Scalar weight = (halfThickness / numLevelsPerHalf);
2542 if (std::abs(dipAngle) > 1e-10) {
2543 weight /= std::cos(dipAngle);
2545 levels.emplace_back(depth, weight);
2550 for (
const auto& level : levels) {
2551 Scalar depth = level.first;
2552 Scalar weight = level.second;
2555 const auto& xy = this->cellCenterXY_[cell];
2557 depth, xy.first, xy.second, dipAngle, dipAzimuth, referencePoint);
2559 const auto pos = CellPos{cell, tvd};
2561 auto localSaturations = psat.deriveSaturations(pos, eqreg, ptable);
2562 auto localPressures = psat.correctedPhasePressures();
2565 saturations.axpy(localSaturations, weight);
2566 pressures.axpy(localPressures, weight);
2567 totalWeight += weight;
2570 if (totalWeight > 1e-10) {
2571 saturations /= totalWeight;
2572 pressures /= totalWeight;
2575 const auto& xy = this->cellCenterXY_[cell];
2577 this->cellCenterDepth_[cell], xy.first, xy.second, dipAngle, dipAzimuth, referencePoint);
2578 const auto pos = CellPos{cell, tvdCenter};
2579 saturations = psat.deriveSaturations(pos, eqreg, ptable);
2580 pressures = psat.correctedPhasePressures();
2584 const auto temp = this->temperature_[cell];
2585 const auto& xy = this->cellCenterXY_[cell];
2587 this->cellCenterDepth_[cell], xy.first, xy.second, dipAngle, dipAzimuth, referencePoint);
2589 Rs = eqreg.dissolutionCalculator()(tvdCenter, pressures.oil, temp, saturations.gas);
2590 Rv = eqreg.evaporationCalculator()(tvdCenter, pressures.gas, temp, saturations.oil);
2591 Rvw = eqreg.waterEvaporationCalculator()(tvdCenter, pressures.gas, temp, saturations.water);
2594 this->cellLoop(cells, cellProcessor);
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:197
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
Auxiliary routines that to solve the ODEs that emerge from the hydrostatic equilibrium problem.
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: FlexibleSolver_impl.hpp:394
Routines that actually solve the ODEs that emerge from the hydrostatic equilibrium problem.
Definition: InitStateEquil.hpp:704
Definition: InitStateEquil.hpp:151
Gas(const TabulatedFunction &tempVdTable, const RV &rv, const RVW &rvw, const int pvtRegionIdx, const Scalar normGrav)
Definition: InitStateEquil_impl.hpp:479
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquil_impl.hpp:495
Definition: InitStateEquil.hpp:126
Oil(const TabulatedFunction &tempVdTable, const RS &rs, const int pvtRegionIdx, const Scalar normGrav)
Definition: InitStateEquil_impl.hpp:431
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquil_impl.hpp:445
Definition: InitStateEquil.hpp:101
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquil_impl.hpp:405
Water(const TabulatedFunction &tempVdTable, const TabulatedFunction &saltVdTable, const int pvtRegionIdx, const Scalar normGrav)
Definition: InitStateEquil_impl.hpp:391
Definition: InitStateEquil.hpp:399
const PhaseQuantityValue< Scalar > & deriveSaturations(const Position &x, const Region ®, const PTable &ptable)
Definition: InitStateEquil_impl.hpp:737
PhaseSaturations(MaterialLawManager &matLawMgr, const std::vector< Scalar > &swatInit)
Definition: InitStateEquil_impl.hpp:713
Definition: InitStateEquil.hpp:180
PressureTable & operator=(const PressureTable &rhs)
Definition: InitStateEquil_impl.hpp:1158
Scalar water(const Scalar depth) const
Definition: InitStateEquil_impl.hpp:1238
Scalar gas(const Scalar depth) const
Definition: InitStateEquil_impl.hpp:1227
bool waterActive() const
Predicate for whether or not water is an active phase.
Definition: InitStateEquil_impl.hpp:1209
bool gasActive() const
Predicate for whether or not gas is an active phase.
Definition: InitStateEquil_impl.hpp:1202
Scalar oil(const Scalar depth) const
Definition: InitStateEquil_impl.hpp:1217
std::array< Scalar, 2 > VSpan
Definition: InitStateEquil.hpp:183
bool oilActive() const
Predicate for whether or not oil is an active phase.
Definition: InitStateEquil_impl.hpp:1195
typename FluidSystem::Scalar Scalar
Definition: InitStateEquil.hpp:182
void equilibrate(const Region ®, const VSpan &span)
Definition: InitStateEquil_impl.hpp:1184
PressureTable(const Scalar gravity, const int samplePoints=2000)
Definition: InitStateEquil_impl.hpp:1128
Definition: InitStateEquil.hpp:80
Scalar operator()(const Scalar x) const
Definition: InitStateEquil_impl.hpp:354
RK4IVP(const RHS &f, const std::array< Scalar, 2 > &span, const Scalar y0, const int N)
Definition: InitStateEquil_impl.hpp:319
Definition: EquilibrationHelpers.hpp:135
Definition: EquilibrationHelpers.hpp:216
Definition: EquilibrationHelpers.hpp:269
Definition: EquilibrationHelpers.hpp:612
Definition: EquilibrationHelpers.hpp:162
Definition: EquilibrationHelpers.hpp:322
Definition: EquilibrationHelpers.hpp:376
std::vector< EquilRecord > getEquil(const EclipseState &state)
Definition: InitStateEquil_impl.hpp:1432
std::vector< int > equilnum(const EclipseState &eclipseState, const GridView &gridview)
Definition: InitStateEquil_impl.hpp:1446
std::pair< Scalar, Scalar > cellZMinMax(const Element &element)
Definition: InitStateEquil_impl.hpp:186
Scalar cellCenterDepth(const Element &element)
Definition: InitStateEquil_impl.hpp:133
std::pair< Scalar, Scalar > cellZSpan(const Element &element)
Definition: InitStateEquil_impl.hpp:167
CellCornerData< Scalar > getCellCornerXY(const Element &element)
Definition: InitStateEquil_impl.hpp:256
void verticalExtent(const CellRange &cells, const std::vector< std::pair< Scalar, Scalar > > &cellZMinMax, const Parallel::Communication &comm, std::array< Scalar, 2 > &span)
Definition: InitStateEquil_impl.hpp:69
std::pair< Scalar, Scalar > cellCenterXY(const Element &element)
Definition: InitStateEquil_impl.hpp:148
Scalar calculateTrueVerticalDepth(Scalar z, Scalar x, Scalar y, Scalar dipAngle, Scalar dipAzimuth, const std::array< Scalar, 3 > &referencePoint)
Definition: InitStateEquil_impl.hpp:280
void subdivisionCentrePoints(const Scalar left, const Scalar right, const int numIntervals, std::vector< std::pair< Scalar, Scalar > > &subdiv)
Definition: InitStateEquil_impl.hpp:94
std::vector< std::pair< Scalar, Scalar > > horizontalSubdivision(const CellID cell, const std::pair< Scalar, Scalar > topbot, const int numIntervals)
Definition: InitStateEquil_impl.hpp:112
void computeBlockDip(const CellCornerData< Scalar > &cellCorners, Scalar &dipAngle, Scalar &dipAzimuth)
Definition: InitStateEquil_impl.hpp:205
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilbioeffectsmodules.hh:45
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
Definition: InitStateEquil.hpp:61
std::array< Scalar, 8 > X
Definition: InitStateEquil.hpp:62
std::array< Scalar, 8 > Y
Definition: InitStateEquil.hpp:63
std::array< Scalar, 8 > Z
Definition: InitStateEquil.hpp:64
Simple set of per-phase (named by primary component) quantities.
Definition: InitStateEquil.hpp:351
Definition: InitStateEquil.hpp:405