31#ifndef OPM_INIT_STATE_EQUIL_COMP_HPP
32#define OPM_INIT_STATE_EQUIL_COMP_HPP
34#include <opm/common/ErrorMacros.hpp>
35#include <opm/common/OpmLog/OpmLog.hpp>
37#include <opm/material/common/Tabulated1DFunction.hpp>
38#include <opm/material/constraintsolvers/SaturationPressure.hpp>
39#include <opm/material/fluidstates/CompositionalFluidState.hpp>
41#include <opm/input/eclipse/EclipseState/Compositional/CompositionalConfig.hpp>
42#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
43#include <opm/input/eclipse/EclipseState/InitConfig/Equil.hpp>
44#include <opm/input/eclipse/EclipseState/Tables/CompvdTable.hpp>
45#include <opm/input/eclipse/EclipseState/Tables/RtempvdTable.hpp>
46#include <opm/input/eclipse/EclipseState/Tables/SwfnTable.hpp>
47#include <opm/input/eclipse/EclipseState/Tables/TableContainer.hpp>
48#include <opm/input/eclipse/EclipseState/Tables/TableManager.hpp>
49#include <opm/input/eclipse/EclipseState/Tables/ZmfvdTable.hpp>
50#include <opm/input/eclipse/Units/Units.hpp>
56#include <fmt/format.h>
57#include <fmt/ranges.h>
83template <
class Scalar>
84Scalar
evalDepthTable(
const Tabulated1DFunction<Scalar>& table,
const Scalar depth)
86 return table.eval(std::clamp(depth, table.xMin(), table.xMax()));
93template <
class Flu
idSystem>
97 using Scalar =
typename FluidSystem::Scalar;
98 using CompVec = std::array<Scalar, FluidSystem::numComponents>;
104 const unsigned phaseIdx,
105 const CompositionalConfig::EOSType eosType,
107 : composition_(std::move(composition))
108 , tempVdTable_(tempVdTable)
109 , phaseIdx_(phaseIdx)
117 const CompVec z = composition_(depth);
120 CompositionalFluidState<Scalar, FluidSystem> fs;
121 fs.setTemperature(temp);
122 fs.setPressure(FluidSystem::oilPhaseIdx, press);
123 fs.setPressure(FluidSystem::gasPhaseIdx, press);
124 for (
unsigned compIdx = 0; compIdx < FluidSystem::numComponents; ++compIdx) {
125 fs.setMoleFraction(phaseIdx_, compIdx, z[compIdx]);
128 typename FluidSystem::template ParameterCache<Scalar> paramCache(eosType_);
129 paramCache.updatePhase(fs, phaseIdx_);
131 return FluidSystem::density(fs, paramCache, phaseIdx_) * g_;
138 CompositionalConfig::EOSType eosType_;
144template <
class Flu
idSystem>
148 using Scalar =
typename FluidSystem::Scalar;
152 const CompositionalConfig::EOSType eosType,
153 const unsigned pvtRegion,
155 : tempVdTable_(tempVdTable)
157 , pvtRegion_(pvtRegion)
164 CompositionalFluidState<Scalar, FluidSystem> fs;
166 fs.setPressure(FluidSystem::waterPhaseIdx, press);
168 typename FluidSystem::template ParameterCache<Scalar> paramCache(eosType_);
169 paramCache.setPvtRegionIndex(pvtRegion_);
171 return FluidSystem::density(fs, paramCache, FluidSystem::waterPhaseIdx) * g_;
176 CompositionalConfig::EOSType eosType_;
238template <
class Flu
idSystem>
242 using Scalar =
typename FluidSystem::Scalar;
243 using FluidState = CompositionalFluidState<Scalar, FluidSystem>;
260 const CompositionalConfig::EOSType eosType,
262 const std::vector<int>& eqlnum,
263 const std::vector<int>& pvtnum,
266 const int numSamplePoints,
267 const std::vector<Scalar>& connateWater = {},
268 const std::vector<Scalar>& maxWater = {})
270 , connateWater_(connateWater)
271 , maxWater_(maxWater)
273 const auto& records = inputState.getInitConfig().getEquil();
274 const auto& tables = inputState.getTableManager();
276 if (!tables.hasTables(
"ZMFVD") && !tables.hasTables(
"COMPVD")) {
277 OPM_THROW(std::runtime_error,
278 "Equilibration of a compositional model requires the composition "
279 "versus depth from the ZMFVD or the COMPVD keyword.");
282 const auto numPvtRegions = tables.getTabdims().getNumPVTTables();
285 OPM_THROW(std::runtime_error,
286 fmt::format(
"EQLNUM contains {} entries for {} cell depths.",
289 for (std::size_t cell = 0; cell < eqlnum.size(); ++cell) {
290 const auto region = eqlnum[cell];
291 if (region < 0 || std::cmp_greater_equal(region, records.size())) {
292 OPM_THROW(std::runtime_error,
293 fmt::format(
"Cell {} has EQLNUM {} outside the {} "
294 "equilibration regions.",
295 cell, region + 1, records.size()));
299 OPM_THROW(std::runtime_error,
300 fmt::format(
"PVTNUM contains {} entries for {} cell depths.",
303 for (std::size_t cell = 0; cell < pvtnum.size(); ++cell) {
304 const auto region = pvtnum[cell];
305 if (region < 0 || std::cmp_greater_equal(region, numPvtRegions)) {
306 OPM_THROW(std::runtime_error,
307 fmt::format(
"Cell {} has PVTNUM {} outside the {} "
309 cell, region + 1, numPvtRegions));
314 for (
const auto& [name, limits] : {std::pair{
"connate water", std::cref(connateWater)},
315 std::pair{
"maximum water", std::cref(maxWater)}}) {
316 if (!limits.get().empty() &&
318 OPM_THROW(std::runtime_error,
319 fmt::format(
"The {} saturation has {} entries for {} cell depths.",
325 const auto pvtRegions =
326 pvtRegionsOfEachRegion(eqlnum, pvtnum, records.size(), numPvtRegions, comm);
327 std::vector<Region> regions;
328 regions.reserve(records.size());
329 for (std::size_t r = 0; r < records.size(); ++r) {
330 regions.push_back(setupRegion(records.getRecord(r), tables, pvtRegions[r],
332 numSamplePoints, r));
335 const bool waterActive = FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx);
340 const auto& reg = regions[eqlnum[cell]];
341 const auto pvtRegion = pvtnum.empty() ? 0u :
static_cast<unsigned>(pvtnum[cell]);
342 referencePressures_[cell] = assignCell(fluidStates_[cell], reg,
347 if ((reg.initType == 1) && !(waterActive && (
cellCenterDepth[cell] > reg.zwoc))) {
348 twoPhaseCheckRegions_[cell] = eqlnum[cell];
354 {
return fluidStates_; }
357 {
return fluidStates_; }
363 {
return referencePressures_; }
370 {
return twoPhaseCheckRegions_; }
373 {
return twoPhaseCheckRegions_; }
376 using CompVec = std::array<Scalar, FluidSystem::numComponents>;
377 using TabulatedFunction = Tabulated1DFunction<Scalar>;
383 static constexpr int numComponents = FluidSystem::numComponents;
388 static constexpr Scalar constantTableSpan{1.0};
392 static constexpr Scalar minimumSpanExtent{1.0};
398 std::vector<unsigned> pvtRegions;
402 std::string_view compositionKeyword;
404 std::optional<unsigned> statedPhaseIdx{};
406 unsigned nominalPhaseIdx{FluidSystem::oilPhaseIdx};
415 std::vector<TabulatedFunction> vaporVdTable;
417 CompVec vaporComposition{};
419 std::vector<TabulatedFunction> compositionVdTable;
420 TabulatedFunction tempVdTable;
421 std::optional<PressFunc> oilPressure;
422 std::optional<PressFunc> gasPressure;
428 bool anchoredAtWaterContact{
false};
430 std::map<unsigned, WaterPressFunc> waterPressure;
435 static std::array<Scalar, 2> waterContactSpan(
const Region& reg,
436 const std::array<Scalar, 2>& span)
438 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
439 return {std::min(span[0], reg.zwoc), std::max(span[1], reg.zwoc)};
445 static CompVec vaporComposition(
const Region& reg,
const Scalar depth)
449 for (
int c = 0; c < numComponents; ++c) {
454 OPM_THROW(std::runtime_error,
455 fmt::format(
"The COMPVD vapour composition vanishes at depth {} m.", depth));
457 std::ranges::transform(z, z.begin(), [sum](
const Scalar zc) { return zc / sum; });
461 static CompVec composition(
const Region& reg,
const Scalar depth)
465 for (
int c = 0; c < numComponents; ++c) {
470 OPM_THROW(std::runtime_error,
471 fmt::format(
"The composition vanishes at depth {} m.", depth));
473 std::ranges::transform(z, z.begin(), [sum](
const Scalar zc) { return zc / sum; });
479 template <
class Table>
480 static void setupComposition(std::vector<TabulatedFunction>& out,
482 const std::vector<std::size_t>& rows)
484 const auto& depthCol = table.getDepthColumn();
486 std::vector<Scalar> depths;
487 depths.reserve(rows.size() + 1);
488 for (
const auto row : rows) {
489 depths.push_back(depthCol[row]);
493 const bool constantComposition = (depths.size() == 1);
494 if (constantComposition) {
495 depths.push_back(depths.front() + constantTableSpan);
498 out.resize(numComponents);
499 for (
int c = 0; c < numComponents; ++c) {
500 const auto& col = table.getMoleFractionColumn(c);
501 std::vector<Scalar> values;
502 values.reserve(depths.size());
503 for (
const auto row : rows) {
504 values.push_back(col[row]);
506 if (constantComposition) {
507 values.push_back(values.front());
509 out[c].setXYContainers(depths, values);
515 static std::optional<std::size_t> sourceTable(
const TableContainer& container,
516 const std::size_t regionIdx)
518 const auto& byIndex = container.tables();
519 const auto after = byIndex.upper_bound(regionIdx);
520 if (after == byIndex.begin()) {
523 return std::prev(after)->first;
527 static std::vector<std::size_t> allRows(
const std::size_t count)
529 std::vector<std::size_t> rows(count);
530 std::iota(rows.begin(), rows.end(), std::size_t{0});
540 static Scalar zoneBoundary(
const CompvdTable& compvd,
541 const std::vector<std::size_t>& vaporRows,
542 const std::vector<std::size_t>& liquidRows,
544 const std::size_t regionIdx)
546 if (vaporRows.empty() || liquidRows.empty()) {
547 OPM_THROW(std::runtime_error,
548 fmt::format(
"The COMPVD table of region {} names both phases but has "
549 "no row for one of them.", regionIdx + 1));
552 const auto& depth = compvd.getDepthColumn();
553 const Scalar lastVapor = depth[vaporRows.back()];
554 const Scalar firstLiquid = depth[liquidRows.front()];
555 if (lastVapor > firstLiquid) {
556 OPM_THROW(std::runtime_error,
557 fmt::format(
"The COMPVD table of region {} has a vapour row at {} m "
558 "below its liquid row at {} m. All vapour rows must lie "
559 "above the liquid rows.",
560 regionIdx + 1, lastVapor, firstLiquid));
563 const Scalar boundary = std::clamp(reg.zgoc, lastVapor, firstLiquid);
564 if (boundary == reg.zgoc) {
568 if (reg.initType != 1) {
569 OPM_THROW(std::runtime_error,
570 fmt::format(
"The COMPVD table of region {} puts its vapour rows down to "
571 "{} m and its liquid rows from {} m, which do not meet at "
572 "the gas-oil contact at {} m. EQUIL item 10 = {} takes the "
573 "contact as the reference depth, so the rows must agree "
575 regionIdx + 1, lastVapor, firstLiquid, reg.zgoc,
579 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas-oil contact at {} m lies "
580 "outside the COMPVD phase change between the last vapour "
581 "row at {} m and the first liquid row at {} m. With EQUIL "
582 "item 10 = 1 the rows give the composition versus depth, "
583 "so the gas zone ends at {} m instead.",
584 regionIdx + 1, reg.zgoc, lastVapor, firstLiquid, boundary));
589 static std::vector<std::size_t> rowsOfPhase(
const CompvdTable& compvd,
592 const auto& flags = compvd.phaseFlags();
593 std::vector<std::size_t> rows;
594 for (std::size_t r = 0; r < flags.size(); ++r) {
595 if (flags[r] == phase) {
604 static std::optional<unsigned> statedPhase(
const CompvdTable& compvd,
605 const std::size_t regionIdx)
607 const auto& flags = compvd.phaseFlags();
612 const auto first = flags.front();
613 if (std::ranges::any_of(flags, [first](
const auto f) {
return f != first; })) {
614 OpmLog::info(fmt::format(
"Equilibration region {}: COMPVD names both phases, "
615 "so the contact and row labels determine the gas and "
621 return (first == CompvdTable::Phase::Vapor)
622 ? FluidSystem::gasPhaseIdx : FluidSystem::oilPhaseIdx;
626 static bool compositionVariesBetween(
const Region& reg,
632 constexpr Scalar sameComposition{1.0e-10};
634 const CompVec a = composition(reg, depthA);
635 const CompVec b = composition(reg, depthB);
636 return !std::ranges::equal(a, b, [](
const Scalar x,
const Scalar y) {
637 return std::abs(x - y) <= sameComposition;
643 static std::vector<std::vector<unsigned>>
644 pvtRegionsOfEachRegion(
const std::vector<int>& eqlnum,
645 const std::vector<int>& pvtnum,
646 const std::size_t numRegions,
647 const std::size_t numPvtRegions,
652 std::vector<int> present(numRegions * numPvtRegions, 0);
653 for (std::size_t cell = 0; cell < eqlnum.size(); ++cell) {
654 const auto pvtRegion = pvtnum.empty() ? 0 : pvtnum[cell];
655 present[
static_cast<std::size_t
>(eqlnum[cell]) * numPvtRegions
656 +
static_cast<std::size_t
>(pvtRegion)] = 1;
658 comm.max(present.data(),
static_cast<int>(present.size()));
660 std::vector<std::vector<unsigned>> pvtRegions(numRegions);
661 for (std::size_t r = 0; r < numRegions; ++r) {
662 for (std::size_t p = 0; p < numPvtRegions; ++p) {
663 if (present[r * numPvtRegions + p] != 0) {
664 pvtRegions[r].push_back(
static_cast<unsigned>(p));
671 Region setupRegion(
const EquilRecord& record,
672 const TableManager& tables,
673 const std::vector<unsigned>& pvtRegions,
675 const std::vector<int>& eqlnum,
676 const std::vector<int>& pvtnum,
679 const int numSamplePoints,
680 const std::size_t regionIdx)
const
684 reg.pvtRegions = pvtRegions;
685 reg.initType = record.compositionalInitType();
686 if (reg.initType != 1 && reg.initType != 3) {
687 OPM_THROW(std::runtime_error,
688 fmt::format(
"Compositional initialization type {} (EQUIL item 10) is "
689 "not supported for region {}; only type 1 (total "
690 "composition) and type 3 (liquid composition) are.",
691 reg.initType, regionIdx + 1));
694 if (record.gasOilContactCapillaryPressure() != 0.0) {
695 OPM_THROW(std::runtime_error,
696 fmt::format(
"Compositional equilibration only supports zero gas-oil "
697 "contact capillary pressure (EQUIL item 6); region {} "
700 unit::convert::to(record.gasOilContactCapillaryPressure(),
704 if (
const auto accuracy = record.initializationTargetAccuracy(); accuracy != 0) {
705 OPM_THROW(std::runtime_error,
706 fmt::format(
"Compositional equilibration only supports cell-centre "
707 "initialization (EQUIL item 9 = 0); region {} specifies {}.",
708 regionIdx + 1, accuracy));
711 reg.zgoc = record.gasOilContactDepth();
716 const bool deckHasZmfvd = tables.hasTables(
"ZMFVD");
717 const bool deckHasCompvd = tables.hasTables(
"COMPVD");
718 const bool statesZmfvd = deckHasZmfvd &&
719 tables.getZmfvdTables().hasTable(regionIdx);
720 const bool statesCompvd = deckHasCompvd &&
721 tables.getCompvdTables().hasTable(regionIdx);
723 if (statesZmfvd && statesCompvd) {
724 OPM_THROW(std::runtime_error,
725 fmt::format(
"Region {} has both a ZMFVD and a COMPVD composition "
726 "versus depth; give only one of them.", regionIdx + 1));
729 std::optional<std::size_t> zmfvdAt;
730 std::optional<std::size_t> compvdAt;
731 if (deckHasZmfvd && deckHasCompvd) {
732 if (!statesZmfvd && !statesCompvd) {
733 OPM_THROW(std::runtime_error,
734 fmt::format(
"Region {} has neither a ZMFVD nor a COMPVD "
735 "composition versus depth. A deck using both "
736 "keywords has to give every region a record of "
737 "its own, as neither can be inherited.",
740 (statesZmfvd ? zmfvdAt : compvdAt) = regionIdx;
742 else if (deckHasZmfvd) {
743 zmfvdAt = sourceTable(tables.getZmfvdTables(), regionIdx);
745 else if (deckHasCompvd) {
746 compvdAt = sourceTable(tables.getCompvdTables(), regionIdx);
749 if (!zmfvdAt.has_value() && !compvdAt.has_value()) {
750 OPM_THROW(std::runtime_error,
751 fmt::format(
"Region {} has neither a ZMFVD nor a COMPVD composition "
752 "versus depth.", regionIdx + 1));
755 if (zmfvdAt.has_value()) {
756 reg.compositionKeyword =
"ZMFVD";
758 tables.getZmfvdTables().template getTable<ZmfvdTable>(*zmfvdAt);
759 setupComposition(reg.compositionVdTable, zmfvd,
760 allRows(zmfvd.getDepthColumn().size()));
763 reg.compositionKeyword =
"COMPVD";
765 tables.getCompvdTables().template getTable<CompvdTable>(*compvdAt);
766 reg.statedPhaseIdx = statedPhase(compvd, regionIdx);
768 if (reg.statedPhaseIdx.has_value()) {
771 if ((reg.initType == 3) &&
772 (*reg.statedPhaseIdx == FluidSystem::gasPhaseIdx)) {
773 OPM_THROW(std::runtime_error,
774 fmt::format(
"Region {} states a vapour composition in COMPVD "
775 "while EQUIL item 10 is 3, which takes the liquid "
776 "composition at the gas-oil contact.",
779 setupComposition(reg.compositionVdTable, compvd,
780 allRows(compvd.getDepthColumn().size()));
788 const auto vaporRows = rowsOfPhase(compvd, CompvdTable::Phase::Vapor);
789 const auto liquidRows = rowsOfPhase(compvd, CompvdTable::Phase::Liquid);
790 reg.zgoc = zoneBoundary(compvd, vaporRows, liquidRows, reg, regionIdx);
792 reg.lastVaporDepth = compvd.getDepthColumn()[vaporRows.back()];
793 if (reg.initType == 1) {
794 setupComposition(reg.compositionVdTable, compvd,
795 allRows(compvd.getDepthColumn().size()));
798 setupComposition(reg.vaporVdTable, compvd, vaporRows);
799 setupComposition(reg.compositionVdTable, compvd, liquidRows);
804 if (tables.hasTables(
"RTEMPVD")) {
805 const auto& rtempvd =
806 tables.getRtempvdTables().template getTable<RtempvdTable>(regionIdx);
807 std::vector<Scalar> tempDepths(rtempvd.getDepthColumn().begin(),
808 rtempvd.getDepthColumn().end());
809 const auto& tempCol = rtempvd.getTemperatureColumn();
810 std::vector<Scalar> temps(tempCol.begin(), tempCol.end());
813 if (tempDepths.size() == 1) {
814 tempDepths.push_back(tempDepths.front() + constantTableSpan);
815 temps.push_back(temps.front());
817 reg.tempVdTable.setXYContainers(tempDepths, temps);
820 const std::vector<Scalar> tempDepths{
Scalar{0}, constantTableSpan};
821 const std::vector<Scalar> temps(tempDepths.size(), tables.rtemp());
822 reg.tempVdTable.setXYContainers(tempDepths, temps);
826 auto span = std::array{std::numeric_limits<Scalar>::max(),
827 std::numeric_limits<Scalar>::lowest()};
829 if (std::cmp_equal(eqlnum[cell], regionIdx)) {
834 span[0] = comm.min(span[0]);
835 span[1] = comm.max(span[1]);
836 if (span[0] > span[1]) {
840 const Scalar deepestCellDepth = span[1];
841 if (span[1] - span[0] < minimumSpanExtent) {
842 span = {span[0] - minimumSpanExtent, span[1] + minimumSpanExtent};
845 const bool waterActive = FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx);
846 std::vector<unsigned> waterPvtRegions;
848 reg.zwoc = record.waterOilContactDepth();
849 if (pvtRegions.size() > 1) {
853 std::vector<int> present(pvtRegions.back() + 1, 0);
855 if (std::cmp_equal(eqlnum[cell], regionIdx) &&
857 present[pvtnum[cell]] = 1;
860 comm.max(present.data(),
static_cast<int>(present.size()));
862 for (
const auto pvtRegion : pvtRegions) {
863 if (present[pvtRegion] != 0) {
864 waterPvtRegions.push_back(pvtRegion);
875 Scalar hcDatum = record.datumDepth();
876 Scalar hcPressure = record.datumDepthPressure();
877 const bool datumInWater = waterActive && (record.datumDepth() > reg.zwoc);
881 reg.anchoredAtWaterContact = waterActive && (record.datumDepth() >= reg.zwoc);
888 const auto& datumPvtRegions =
889 waterPvtRegions.empty() ? reg.pvtRegions : waterPvtRegions;
890 integrateWaterPressure(reg, datumPvtRegions, span, gravity, numSamplePoints,
891 record.datumDepth(), record.datumDepthPressure());
893 hcPressure = waterContactPressure(reg, record.datumDepth(), regionIdx)
894 + record.waterOilContactCapillaryPressure();
895 OpmLog::info(fmt::format(
"Equilibration region {}: the datum at {} m lies below the "
896 "water-oil contact at {} m, so it gives the water pressure; "
897 "the hydrocarbon pressure at the contact is {:.5} bar.",
898 regionIdx + 1, record.datumDepth(), reg.zwoc,
899 hcPressure / 1.0e5));
902 if (reg.initType == 1) {
903 setupSinglePhaseRegion(reg, span, gravity, numSamplePoints, regionIdx,
904 hcDatum, hcPressure);
912 OPM_THROW(std::runtime_error,
913 fmt::format(
"Compositional equilibration of region {} places the "
914 "datum at {} m, below the water-oil contact at {} m, "
915 "while EQUIL item 10 is 3. Put the datum in the "
916 "hydrocarbon column or use item 10 = 1.",
917 regionIdx + 1, record.datumDepth(), reg.zwoc));
919 setupTwoPhaseRegion(reg, record, span, gravity, numSamplePoints, regionIdx);
924 setupWaterZone(reg, record, span, gravity, numSamplePoints, regionIdx);
927 if (waterPvtRegions.size() > 1) {
928 Scalar minPressure = std::numeric_limits<Scalar>::max();
929 Scalar maxPressure = std::numeric_limits<Scalar>::lowest();
930 for (
const auto pvtRegion : waterPvtRegions) {
931 const Scalar pressure = reg.waterPressure.at(pvtRegion).value(deepestCellDepth);
932 minPressure = std::min(minPressure, pressure);
933 maxPressure = std::max(maxPressure, pressure);
935 const Scalar difference = maxPressure - minPressure;
936 const Scalar scale = std::max(std::abs(minPressure), std::abs(maxPressure));
937 if (difference > 1.0e-10 * scale) {
938 auto displayedRegions = waterPvtRegions;
939 for (
auto& pvtRegion : displayedRegions) {
942 OpmLog::warning(fmt::format(
943 "Equilibration region {} has water-zone cells in PVT regions {} "
944 "with pressures differing by {:.6g} bar at {} m. If those cells "
945 "connect (including numerical-aquifer NNCs), initial crossflow "
946 "may occur. Check PVTNUM and initial pressures.",
947 regionIdx + 1, fmt::join(displayedRegions,
", "),
948 unit::convert::to(difference, unit::barsa), deepestCellDepth));
958 void setupWaterZone(Region& reg,
959 const EquilRecord& record,
960 const std::array<Scalar, 2>& span,
962 const int numSamplePoints,
963 const std::size_t regionIdx)
const
967 if (!reg.oilPressure.has_value() && !reg.gasPressure.has_value()) {
972 const bool gasAtContact = reg.gasPressure.has_value()
973 && (!reg.oilPressure.has_value() || (reg.zwoc <= reg.zgoc));
974 const auto& hcPressure = gasAtContact ? reg.gasPressure : reg.oilPressure;
975 const Scalar pcow = record.waterOilContactCapillaryPressure();
976 const Scalar pContact = hcPressure->value(reg.zwoc) - pcow;
978 integrateWaterPressure(reg, reg.pvtRegions, span, gravity, numSamplePoints,
981 OpmLog::info(fmt::format(
"Equilibration region {}: the water-oil contact "
982 "is at {} m.", regionIdx + 1, reg.zwoc));
987 void integrateWaterPressure(Region& reg,
988 const std::vector<unsigned>& pvtRegions,
989 const std::array<Scalar, 2>& span,
991 const int numSamplePoints,
993 const Scalar pressure)
const
995 for (
const auto pvtRegion : pvtRegions) {
996 const WaterODE ode(reg.tempVdTable, eosType_, pvtRegion, gravity);
997 reg.waterPressure.try_emplace(pvtRegion, ode,
998 typename WaterPressFunc::InitCond{depth, pressure},
999 numSamplePoints, waterContactSpan(reg, span));
1006 static Scalar waterContactPressure(
const Region& reg,
1008 const std::size_t regionIdx)
1012 constexpr Scalar samePressure{1.0e-10};
1014 const auto& [firstPvtRegion, firstColumn] = *reg.waterPressure.begin();
1015 const Scalar pressure = firstColumn.value(reg.zwoc);
1016 for (
const auto& [pvtRegion, column] : reg.waterPressure) {
1017 const Scalar other = column.value(reg.zwoc);
1018 if (std::abs(other - pressure) > samePressure * pressure) {
1019 OPM_THROW(std::runtime_error,
1020 fmt::format(
"Equilibration region {} places the datum at {} m, below "
1021 "the water-oil contact at {} m, but the water of its PVT "
1022 "regions {} and {} reaches the contact at {:.6g} and "
1023 "{:.6g} bar. Put the datum in the hydrocarbon column.",
1024 regionIdx + 1, datum, reg.zwoc,
1025 firstPvtRegion + 1, pvtRegion + 1,
1026 unit::convert::to(pressure, unit::barsa),
1027 unit::convert::to(other, unit::barsa)));
1035 static Scalar waterLimit(
const std::vector<Scalar>& limits,
1036 const std::size_t cell,
1039 return limits.empty() ? fallback : limits[cell];
1056 void setupSinglePhaseRegion(Region& reg,
1057 const std::array<Scalar, 2>& span,
1059 const int numSamplePoints,
1060 const std::size_t regionIdx,
1062 const Scalar datumPressure)
const
1065 setupTwoZoneRegion(reg, span, gravity, numSamplePoints, regionIdx,
1066 datum, datumPressure);
1075 const bool gasAtDatum = reg.anchoredAtWaterContact ? (datum <= reg.zgoc)
1076 : (datum < reg.zgoc);
1077 const auto phaseIdx = reg.statedPhaseIdx.value_or(
1078 gasAtDatum ? FluidSystem::gasPhaseIdx : FluidSystem::oilPhaseIdx);
1083 const Scalar hydrocarbonBottom = FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)
1084 ? std::min(span[1], reg.zwoc) : span[1];
1085 if ((reg.zgoc > span[0]) && (reg.zgoc < hydrocarbonBottom) &&
1086 !compositionVariesBetween(reg, span[0], reg.zgoc) &&
1087 !compositionVariesBetween(reg, reg.zgoc, hydrocarbonBottom)) {
1088 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas-oil contact "
1089 "at {} m lies inside a type-1 region, but the "
1090 "composition does not vary across the contact. "
1091 "Compositional variation is required for proper "
1092 "phase labeling.", regionIdx + 1, reg.zgoc));
1095 const ODE ode([®](
const Scalar depth) {
return composition(reg, depth); },
1096 reg.tempVdTable, phaseIdx, eosType_, gravity);
1097 reg.oilPressure.emplace(ode,
1098 typename PressFunc::InitCond{datum, datumPressure},
1099 numSamplePoints, waterContactSpan(reg, span));
1100 reg.nominalPhaseIdx = phaseIdx;
1102 OpmLog::info(fmt::format(
"Equilibration region {}: pressure integrated with one "
1103 "EOS root and the total composition from {} "
1104 "(EQUIL item 10 = 1).",
1105 regionIdx + 1, reg.compositionKeyword));
1111 void setupTwoZoneRegion(Region& reg,
1112 const std::array<Scalar, 2>& span,
1114 const int numSamplePoints,
1115 const std::size_t regionIdx,
1117 const Scalar datumPressure)
const
1119 const auto z = [®](
const Scalar depth) {
return composition(reg, depth); };
1120 const ODE liquidOde(z, reg.tempVdTable, FluidSystem::oilPhaseIdx, eosType_, gravity);
1121 const ODE gasOde(z, reg.tempVdTable, FluidSystem::gasPhaseIdx, eosType_, gravity);
1126 const auto pressureSpan = waterContactSpan(reg, span);
1127 const std::array<Scalar, 2> datumSpan{std::min(pressureSpan[0], reg.zgoc),
1128 std::max(pressureSpan[1], reg.zgoc)};
1129 if ((reg.zgoc < span[0]) || (reg.zgoc > span[1])) {
1130 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas zone ends at {} m, "
1131 "outside the cells of the region, so all of them "
1132 "lie in the {} zone.",
1135 (reg.zgoc < span[0]) ?
"liquid" :
"gas"));
1138 if (datum < reg.zgoc) {
1139 reg.gasPressure.emplace(gasOde,
1140 typename PressFunc::InitCond{datum, datumPressure},
1141 numSamplePoints, datumSpan);
1142 reg.oilPressure.emplace(liquidOde,
1143 typename PressFunc::InitCond{
1144 reg.zgoc, reg.gasPressure->value(reg.zgoc)},
1145 numSamplePoints, pressureSpan);
1148 reg.oilPressure.emplace(liquidOde,
1149 typename PressFunc::InitCond{datum, datumPressure},
1150 numSamplePoints, datumSpan);
1151 reg.gasPressure.emplace(gasOde,
1152 typename PressFunc::InitCond{
1153 reg.zgoc, reg.oilPressure->value(reg.zgoc)},
1154 numSamplePoints, pressureSpan);
1157 OpmLog::info(fmt::format(
"Equilibration region {}: COMPVD gives a gas zone above "
1158 "{} m and a liquid one below it "
1159 "(EQUIL item 10 = 1).",
1171 void setupTwoPhaseRegion(Region& reg,
1172 const EquilRecord& record,
1173 const std::array<Scalar, 2>& span,
1175 const int numSamplePoints,
1176 const std::size_t regionIdx)
const
1178 const Scalar inputReferenceDepth = record.datumDepth();
1179 if (inputReferenceDepth != reg.zgoc) {
1180 OpmLog::warning(fmt::format(
"Equilibration region {}: the reference depth {} m "
1181 "does not coincide with the gas-oil contact when "
1182 "EQUIL item 10 is 3; resetting it to the contact "
1184 regionIdx + 1, inputReferenceDepth, reg.zgoc));
1187 const CompVec liquid = composition(reg, reg.zgoc);
1191 if (!SaturationPressure<Scalar, FluidSystem>::bubblePressure(liquid, temp, eosType_,
1193 OPM_THROW(std::runtime_error,
1194 fmt::format(
"The saturation pressure calculation at the gas-oil "
1195 "contact of region {} did not converge.", regionIdx + 1));
1197 reg.vaporComposition = vapor;
1204 constexpr Scalar oneAtmosphere = unit::atm;
1205 const Scalar inputPressure = record.datumDepthPressure();
1206 const bool resetToPsat = record.setToSaturationPressure()
1207 && (std::abs(inputPressure - psat) >= oneAtmosphere);
1208 const Scalar referencePressure = resetToPsat ? psat : inputPressure;
1210 OpmLog::info(fmt::format(
"Equilibration region {}: two phases, liquid composition "
1211 "specified (EQUIL item 10 is 3). The saturation pressure "
1212 "at the gas-oil contact ({} m) is {:.6g} bar.",
1213 regionIdx + 1, reg.zgoc,
1214 unit::convert::to(psat, unit::barsa)));
1217 OpmLog::warning(fmt::format(
"Equilibration region {}: the datum pressure {:.6g} bar "
1218 "differs from the saturation pressure {:.6g} bar at the "
1219 "gas-oil contact by one atmosphere or more; the "
1220 "saturation pressure is used instead.",
1222 unit::convert::to(inputPressure, unit::barsa),
1223 unit::convert::to(psat, unit::barsa)));
1226 const ODE oilOde([®](
const Scalar depth) {
return composition(reg, depth); },
1227 reg.tempVdTable, FluidSystem::oilPhaseIdx, eosType_, gravity);
1228 reg.oilPressure.emplace(oilOde,
1229 typename PressFunc::InitCond{reg.zgoc, referencePressure},
1230 numSamplePoints, waterContactSpan(reg, span));
1237 gasComposition = [®](
const Scalar depth) {
return vaporComposition(reg, depth); };
1240 gasComposition = [vapor](
const Scalar) {
return vapor; };
1242 const ODE gasOde(gasComposition,
1243 reg.tempVdTable, FluidSystem::gasPhaseIdx, eosType_, gravity);
1244 reg.gasPressure.emplace(gasOde,
1245 typename PressFunc::InitCond{reg.zgoc, referencePressure},
1246 numSamplePoints, waterContactSpan(reg, span));
1254 static bool isInGasZone(
const Region& reg,
const Scalar depth)
1256 if (reg.twoZone && (reg.initType == 1) && (depth <= reg.lastVaporDepth)) {
1259 return ((reg.initType == 3) || reg.twoZone) && (depth < reg.zgoc);
1262 Scalar assignCell(
FluidState& fs,
const Region& reg,
const unsigned pvtRegion,
1263 const Scalar depth,
const std::size_t cell)
const
1265 const bool inGasZone = isInGasZone(reg, depth);
1267 const CompVec z = [®, depth, inGasZone]() {
1269 if (!inGasZone || (reg.initType == 1)) {
1270 return composition(reg, depth);
1274 return reg.twoZone ? vaporComposition(reg, depth) : reg.vaporComposition;
1276 const auto& pressFunc = inGasZone ? reg.gasPressure : reg.oilPressure;
1277 if (!pressFunc.has_value()) {
1278 OPM_THROW(std::runtime_error,
1279 "Evaluating the equilibrated pressure of a region without cells.");
1286 const Scalar hydrocarbonPressure = pressFunc->value(depth);
1288 const auto water = reg.waterPressure.find(pvtRegion);
1289 const WaterPressFunc* waterPressure =
1290 (water != reg.waterPressure.end()) ? &water->second :
nullptr;
1291 const bool inWaterZone = (depth > reg.zwoc) && (waterPressure !=
nullptr);
1292 const Scalar press = inWaterZone ? waterPressure->value(depth)
1293 : hydrocarbonPressure;
1296 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
1297 if (FluidSystem::phaseIsActive(phaseIdx)) {
1298 fs.setPressure(phaseIdx, hydrocarbonPressure);
1299 fs.setSaturation(phaseIdx, 0.0);
1306 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
1310 sWat = (depth > reg.zwoc) ? waterLimit(maxWater_, cell,
Scalar{1})
1311 : waterLimit(connateWater_, cell,
Scalar{0});
1312 fs.setSaturation(FluidSystem::waterPhaseIdx, sWat);
1313 if (waterPressure !=
nullptr) {
1314 fs.setPressure(FluidSystem::waterPhaseIdx, waterPressure->value(depth));
1321 fs.setSaturation(inGasZone ? FluidSystem::gasPhaseIdx : reg.nominalPhaseIdx,
1324 for (
int c = 0; c < numComponents; ++c) {
1325 fs.setMoleFraction(c, z[c]);
1330 CompositionalConfig::EOSType eosType_;
1334 std::vector<Scalar> connateWater_;
1335 std::vector<Scalar> maxWater_;
1336 std::vector<FluidState> fluidStates_;
1337 std::vector<Scalar> referencePressures_;
1338 std::vector<int> twoPhaseCheckRegions_;
1355template <
class Scalar,
class Liqu
idFraction>
1358 const LiquidFraction& liquidFraction,
1359 const std::size_t numRegions,
1365 constexpr Scalar minReportedFraction{1.0e-3};
1370 std::vector<Scalar> shallowest(numRegions, std::numeric_limits<Scalar>::max());
1371 std::vector<Scalar> deepest(numRegions, std::numeric_limits<Scalar>::lowest());
1372 std::vector<Scalar> leastFraction(numRegions, std::numeric_limits<Scalar>::max());
1373 std::vector<Scalar> mostFraction(numRegions, std::numeric_limits<Scalar>::lowest());
1374 for (std::size_t cell = 0; cell < checkRegions.size(); ++cell) {
1375 if (checkRegions[cell] < 0) {
1378 const Scalar liquid = liquidFraction(cell);
1379 const Scalar fraction = std::min(liquid, Scalar{1} - liquid);
1381 if (!(fraction >= minReportedFraction)) {
1384 const auto r =
static_cast<std::size_t
>(checkRegions[cell]);
1386 shallowest[r] = std::min(shallowest[r], depth);
1387 deepest[r] = std::max(deepest[r], depth);
1388 leastFraction[r] = std::min(leastFraction[r], fraction);
1389 mostFraction[r] = std::max(mostFraction[r], fraction);
1391 const auto count =
static_cast<int>(numRegions);
1392 comm.min(shallowest.data(), count);
1393 comm.max(deepest.data(), count);
1394 comm.min(leastFraction.data(), count);
1395 comm.max(mostFraction.data(), count);
1398 const auto depthRange = [](
const Scalar top,
const Scalar bottom) {
1399 const auto shallow = fmt::format(
"{:.2f} m", top);
1400 const auto deep = fmt::format(
"{:.2f} m", bottom);
1401 return (shallow == deep) ?
"at " + shallow :
"between " + shallow +
" and " + deep;
1403 for (std::size_t r = 0; r < numRegions; ++r) {
1404 if (shallowest[r] > deepest[r]) {
1409 const auto least = fmt::format(
"{:.3g}%", 100 * leastFraction[r]);
1410 const auto most = fmt::format(
"{:.3g}%", 100 * mostFraction[r]);
1411 OpmLog::warning(fmt::format(
"Equilibration region {}: cells {} split into gas and "
1412 "liquid at their initial pressure, with the smaller phase "
1413 "holding {} of the hydrocarbon moles. With EQUIL "
1414 "item 10 = 1, that pressure comes from the density of a "
1415 "single phase, so the gas and liquid in these cells are "
1416 "not in hydrostatic equilibrium and may move up or down "
1417 "once the simulation starts.",
1419 depthRange(shallowest[r], deepest[r]),
1420 (least == most) ? least : least +
" to " + most));
#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
The ODE integrator and phase-pressure function used to solve the hydrostatic equilibrium problem,...
Definition: InitStateEquilComp.hpp:95
Tabulated1DFunction< Scalar > TabulatedFunction
Definition: InitStateEquilComp.hpp:100
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquilComp.hpp:114
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:97
std::array< Scalar, FluidSystem::numComponents > CompVec
Definition: InitStateEquilComp.hpp:98
std::function< CompVec(Scalar)> CompositionFunction
Definition: InitStateEquilComp.hpp:99
EosDensityODE(CompositionFunction composition, const TabulatedFunction &tempVdTable, const unsigned phaseIdx, const CompositionalConfig::EOSType eosType, const Scalar normGrav)
Definition: InitStateEquilComp.hpp:102
Definition: InitStateEquilComp.hpp:146
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquilComp.hpp:161
Tabulated1DFunction< Scalar > TabulatedFunction
Definition: InitStateEquilComp.hpp:149
WaterDensityODE(const TabulatedFunction &tempVdTable, const CompositionalConfig::EOSType eosType, const unsigned pvtRegion, const Scalar normGrav)
Definition: InitStateEquilComp.hpp:151
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:148
Computes the initial state of a compositional model from hydrostatic equilibrium (the EQUIL and ZMFVD...
Definition: InitStateEquilComp.hpp:240
CompositionalFluidState< Scalar, FluidSystem > FluidState
Definition: InitStateEquilComp.hpp:243
std::vector< int > & twoPhaseCheckRegions()
Definition: InitStateEquilComp.hpp:369
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:242
InitialStateComputer(const EclipseState &inputState, const CompositionalConfig::EOSType eosType, const std::vector< Scalar > &cellCenterDepth, const std::vector< int > &eqlnum, const std::vector< int > &pvtnum, const Parallel::Communication &comm, const Scalar gravity, const int numSamplePoints, const std::vector< Scalar > &connateWater={}, const std::vector< Scalar > &maxWater={})
Definition: InitStateEquilComp.hpp:259
const std::vector< int > & twoPhaseCheckRegions() const
Definition: InitStateEquilComp.hpp:372
const std::vector< Scalar > & referencePressures() const
Definition: InitStateEquilComp.hpp:362
const std::vector< FluidState > & fluidStates() const
Definition: InitStateEquilComp.hpp:356
std::vector< FluidState > & fluidStates()
Definition: InitStateEquilComp.hpp:353
Definition: PressureFunction.hpp:128
Scalar evalDepthTable(const Tabulated1DFunction< Scalar > &table, const Scalar depth)
Definition: InitStateEquilComp.hpp:84
Definition: InitStateEquilComp.hpp:76
void reportTwoPhaseCells(const std::vector< int > &checkRegions, const std::vector< Scalar > &cellCenterDepth, const LiquidFraction &liquidFraction, const std::size_t numRegions, const Parallel::Communication &comm)
Definition: InitStateEquilComp.hpp:1356
Scalar cellCenterDepth(const Element &element)
Definition: InitStateEquil_impl.hpp:134
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:183