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>
81template <
class Scalar>
82Scalar
evalDepthTable(
const Tabulated1DFunction<Scalar>& table,
const Scalar depth)
84 return table.eval(std::clamp(depth, table.xMin(), table.xMax()));
91template <
class Flu
idSystem>
95 using Scalar =
typename FluidSystem::Scalar;
96 using CompVec = std::array<Scalar, FluidSystem::numComponents>;
102 const unsigned phaseIdx,
103 const CompositionalConfig::EOSType eosType,
105 : composition_(std::move(composition))
106 , tempVdTable_(tempVdTable)
107 , phaseIdx_(phaseIdx)
115 const CompVec z = composition_(depth);
118 CompositionalFluidState<Scalar, FluidSystem> fs;
119 fs.setTemperature(temp);
120 fs.setPressure(FluidSystem::oilPhaseIdx, press);
121 fs.setPressure(FluidSystem::gasPhaseIdx, press);
122 for (
unsigned compIdx = 0; compIdx < FluidSystem::numComponents; ++compIdx) {
123 fs.setMoleFraction(phaseIdx_, compIdx, z[compIdx]);
126 typename FluidSystem::template ParameterCache<Scalar> paramCache(eosType_);
127 paramCache.updatePhase(fs, phaseIdx_);
129 return FluidSystem::density(fs, paramCache, phaseIdx_) * g_;
136 CompositionalConfig::EOSType eosType_;
142template <
class Flu
idSystem>
146 using Scalar =
typename FluidSystem::Scalar;
150 const CompositionalConfig::EOSType eosType,
152 : tempVdTable_(tempVdTable)
160 CompositionalFluidState<Scalar, FluidSystem> fs;
162 fs.setPressure(FluidSystem::waterPhaseIdx, press);
164 typename FluidSystem::template ParameterCache<Scalar> paramCache(eosType_);
166 return FluidSystem::density(fs, paramCache, FluidSystem::waterPhaseIdx) * g_;
171 CompositionalConfig::EOSType eosType_;
212template <
class Flu
idSystem>
216 using Scalar =
typename FluidSystem::Scalar;
217 using FluidState = CompositionalFluidState<Scalar, FluidSystem>;
232 const CompositionalConfig::EOSType eosType,
234 const std::vector<int>& eqlnum,
237 const int numSamplePoints,
238 const std::vector<Scalar>& connateWater = {},
239 const std::vector<Scalar>& maxWater = {})
241 , connateWater_(connateWater)
242 , maxWater_(maxWater)
244 const auto& records = inputState.getInitConfig().getEquil();
245 const auto& tables = inputState.getTableManager();
247 if (!tables.hasTables(
"ZMFVD") && !tables.hasTables(
"COMPVD")) {
248 OPM_THROW(std::runtime_error,
249 "Equilibration of a compositional model requires the composition "
250 "versus depth from the ZMFVD or the COMPVD keyword.");
255 OPM_THROW(std::runtime_error,
256 fmt::format(
"EQLNUM contains {} entries for {} cell depths.",
259 for (std::size_t cell = 0; cell < eqlnum.size(); ++cell) {
260 const auto region = eqlnum[cell];
261 if (region < 0 || std::cmp_greater_equal(region, records.size())) {
262 OPM_THROW(std::runtime_error,
263 fmt::format(
"Cell {} has EQLNUM {} outside the {} "
264 "equilibration regions.",
265 cell, region + 1, records.size()));
270 for (
const auto& [name, limits] : {std::pair{
"connate water", std::cref(connateWater)},
271 std::pair{
"maximum water", std::cref(maxWater)}}) {
272 if (!limits.get().empty() &&
274 OPM_THROW(std::runtime_error,
275 fmt::format(
"The {} saturation has {} entries for {} cell depths.",
281 std::vector<Region> regions;
282 regions.reserve(records.size());
283 for (std::size_t r = 0; r < records.size(); ++r) {
284 regions.push_back(setupRegion(records.getRecord(r), tables,
cellCenterDepth,
285 eqlnum, comm, gravity, numSamplePoints, r));
291 referencePressures_[cell] =
292 assignCell(fluidStates_[cell], regions[eqlnum[cell]],
cellCenterDepth[cell], cell);
297 {
return fluidStates_; }
300 {
return fluidStates_; }
306 {
return referencePressures_; }
309 using CompVec = std::array<Scalar, FluidSystem::numComponents>;
310 using TabulatedFunction = Tabulated1DFunction<Scalar>;
316 static constexpr int numComponents = FluidSystem::numComponents;
321 static constexpr Scalar constantTableSpan{1.0};
325 static constexpr Scalar minimumSpanExtent{1.0};
333 std::string_view compositionKeyword;
335 std::optional<unsigned> statedPhaseIdx{};
337 unsigned nominalPhaseIdx{FluidSystem::oilPhaseIdx};
346 std::vector<TabulatedFunction> vaporVdTable;
348 CompVec vaporComposition{};
351 std::vector<TabulatedFunction> compositionVdTable;
352 TabulatedFunction tempVdTable;
353 std::optional<PressFunc> oilPressure;
354 std::optional<PressFunc> gasPressure;
360 bool anchoredAtWaterContact{
false};
361 std::optional<WaterPressFunc> waterPressure;
366 static std::array<Scalar, 2> waterContactSpan(
const Region& reg,
367 const std::array<Scalar, 2>& span)
369 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
370 return {std::min(span[0], reg.zwoc), std::max(span[1], reg.zwoc)};
376 static CompVec vaporComposition(
const Region& reg,
const Scalar depth)
380 for (
int c = 0; c < numComponents; ++c) {
385 OPM_THROW(std::runtime_error,
386 fmt::format(
"The COMPVD vapour composition vanishes at depth {} m.", depth));
388 std::ranges::transform(z, z.begin(), [sum](
const Scalar zc) { return zc / sum; });
392 static CompVec composition(
const Region& reg,
const Scalar depth)
396 for (
int c = 0; c < numComponents; ++c) {
401 OPM_THROW(std::runtime_error,
402 fmt::format(
"The composition vanishes at depth {} m.", depth));
404 std::ranges::transform(z, z.begin(), [sum](
const Scalar zc) { return zc / sum; });
410 template <
class Table>
411 static void setupComposition(std::vector<TabulatedFunction>& out,
413 const std::vector<std::size_t>& rows)
415 const auto& depthCol = table.getDepthColumn();
417 std::vector<Scalar> depths;
418 depths.reserve(rows.size() + 1);
419 for (
const auto row : rows) {
420 depths.push_back(depthCol[row]);
424 const bool constantComposition = (depths.size() == 1);
425 if (constantComposition) {
426 depths.push_back(depths.front() + constantTableSpan);
429 out.resize(numComponents);
430 for (
int c = 0; c < numComponents; ++c) {
431 const auto& col = table.getMoleFractionColumn(c);
432 std::vector<Scalar> values;
433 values.reserve(depths.size());
434 for (
const auto row : rows) {
435 values.push_back(col[row]);
437 if (constantComposition) {
438 values.push_back(values.front());
440 out[c].setXYContainers(depths, values);
446 static std::optional<std::size_t> sourceTable(
const TableContainer& container,
447 const std::size_t regionIdx)
449 const auto& byIndex = container.tables();
450 const auto after = byIndex.upper_bound(regionIdx);
451 if (after == byIndex.begin()) {
454 return std::prev(after)->first;
458 static std::vector<std::size_t> allRows(
const std::size_t count)
460 std::vector<std::size_t> rows(count);
461 std::iota(rows.begin(), rows.end(), std::size_t{0});
471 static Scalar zoneBoundary(
const CompvdTable& compvd,
472 const std::vector<std::size_t>& vaporRows,
473 const std::vector<std::size_t>& liquidRows,
475 const std::size_t regionIdx)
477 if (vaporRows.empty() || liquidRows.empty()) {
478 OPM_THROW(std::runtime_error,
479 fmt::format(
"The COMPVD table of region {} names both phases but has "
480 "no row for one of them.", regionIdx + 1));
483 const auto& depth = compvd.getDepthColumn();
484 const Scalar lastVapor = depth[vaporRows.back()];
485 const Scalar firstLiquid = depth[liquidRows.front()];
486 if (lastVapor > firstLiquid) {
487 OPM_THROW(std::runtime_error,
488 fmt::format(
"The COMPVD table of region {} has a vapour row at {} m "
489 "below its liquid row at {} m. All vapour rows must lie "
490 "above the liquid rows.",
491 regionIdx + 1, lastVapor, firstLiquid));
494 const Scalar boundary = std::clamp(reg.zgoc, lastVapor, firstLiquid);
495 if (boundary == reg.zgoc) {
499 if (reg.initType != 1) {
500 OPM_THROW(std::runtime_error,
501 fmt::format(
"The COMPVD table of region {} puts its vapour rows down to "
502 "{} m and its liquid rows from {} m, which do not meet at "
503 "the gas-oil contact at {} m. EQUIL item 10 = {} takes the "
504 "contact as the reference depth, so the rows must agree "
506 regionIdx + 1, lastVapor, firstLiquid, reg.zgoc,
510 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas-oil contact at {} m lies "
511 "outside the COMPVD phase change between the last vapour "
512 "row at {} m and the first liquid row at {} m. With EQUIL "
513 "item 10 = 1 the rows give the composition versus depth, "
514 "so the gas zone ends at {} m instead.",
515 regionIdx + 1, reg.zgoc, lastVapor, firstLiquid, boundary));
520 static std::vector<std::size_t> rowsOfPhase(
const CompvdTable& compvd,
523 const auto& flags = compvd.phaseFlags();
524 std::vector<std::size_t> rows;
525 for (std::size_t r = 0; r < flags.size(); ++r) {
526 if (flags[r] == phase) {
535 static std::optional<unsigned> statedPhase(
const CompvdTable& compvd,
536 const std::size_t regionIdx)
538 const auto& flags = compvd.phaseFlags();
543 const auto first = flags.front();
544 if (std::ranges::any_of(flags, [first](
const auto f) {
return f != first; })) {
545 OpmLog::info(fmt::format(
"Equilibration region {}: COMPVD names both phases, "
546 "so the composition of each depth decides the phase.",
551 return (first == CompvdTable::Phase::Vapor)
552 ? FluidSystem::gasPhaseIdx : FluidSystem::oilPhaseIdx;
556 static bool compositionVariesBetween(
const Region& reg,
562 constexpr Scalar sameComposition{1.0e-10};
564 const CompVec a = composition(reg, depthA);
565 const CompVec b = composition(reg, depthB);
566 return !std::ranges::equal(a, b, [](
const Scalar x,
const Scalar y) {
567 return std::abs(x - y) <= sameComposition;
571 Region setupRegion(
const EquilRecord& record,
572 const TableManager& tables,
574 const std::vector<int>& eqlnum,
577 const int numSamplePoints,
578 const std::size_t regionIdx)
const
582 reg.initType = record.compositionalInitType();
583 if (reg.initType != 1 && reg.initType != 3) {
584 OPM_THROW(std::runtime_error,
585 fmt::format(
"Compositional initialization type {} (EQUIL item 10) is "
586 "not supported for region {}; only type 1 (total "
587 "composition) and type 3 (liquid composition) are.",
588 reg.initType, regionIdx + 1));
591 if (record.gasOilContactCapillaryPressure() != 0.0) {
592 OPM_THROW(std::runtime_error,
593 fmt::format(
"Compositional equilibration only supports zero gas-oil "
594 "contact capillary pressure (EQUIL item 6); region {} "
597 unit::convert::to(record.gasOilContactCapillaryPressure(),
601 if (
const auto accuracy = record.initializationTargetAccuracy(); accuracy != 0) {
602 OPM_THROW(std::runtime_error,
603 fmt::format(
"Compositional equilibration only supports cell-centre "
604 "initialization (EQUIL item 9 = 0); region {} specifies {}.",
605 regionIdx + 1, accuracy));
608 reg.zgoc = record.gasOilContactDepth();
613 const bool deckHasZmfvd = tables.hasTables(
"ZMFVD");
614 const bool deckHasCompvd = tables.hasTables(
"COMPVD");
615 const bool statesZmfvd = deckHasZmfvd &&
616 tables.getZmfvdTables().hasTable(regionIdx);
617 const bool statesCompvd = deckHasCompvd &&
618 tables.getCompvdTables().hasTable(regionIdx);
620 if (statesZmfvd && statesCompvd) {
621 OPM_THROW(std::runtime_error,
622 fmt::format(
"Region {} has both a ZMFVD and a COMPVD composition "
623 "versus depth; give only one of them.", regionIdx + 1));
626 std::optional<std::size_t> zmfvdAt;
627 std::optional<std::size_t> compvdAt;
628 if (deckHasZmfvd && deckHasCompvd) {
629 if (!statesZmfvd && !statesCompvd) {
630 OPM_THROW(std::runtime_error,
631 fmt::format(
"Region {} has neither a ZMFVD nor a COMPVD "
632 "composition versus depth. A deck using both "
633 "keywords has to give every region a record of "
634 "its own, as neither can be inherited.",
637 (statesZmfvd ? zmfvdAt : compvdAt) = regionIdx;
639 else if (deckHasZmfvd) {
640 zmfvdAt = sourceTable(tables.getZmfvdTables(), regionIdx);
642 else if (deckHasCompvd) {
643 compvdAt = sourceTable(tables.getCompvdTables(), regionIdx);
646 if (!zmfvdAt.has_value() && !compvdAt.has_value()) {
647 OPM_THROW(std::runtime_error,
648 fmt::format(
"Region {} has neither a ZMFVD nor a COMPVD composition "
649 "versus depth.", regionIdx + 1));
652 if (zmfvdAt.has_value()) {
653 reg.compositionKeyword =
"ZMFVD";
655 tables.getZmfvdTables().template getTable<ZmfvdTable>(*zmfvdAt);
656 setupComposition(reg.compositionVdTable, zmfvd,
657 allRows(zmfvd.getDepthColumn().size()));
660 reg.compositionKeyword =
"COMPVD";
662 tables.getCompvdTables().template getTable<CompvdTable>(*compvdAt);
663 reg.statedPhaseIdx = statedPhase(compvd, regionIdx);
665 if (reg.statedPhaseIdx.has_value()) {
668 if ((reg.initType == 3) &&
669 (*reg.statedPhaseIdx == FluidSystem::gasPhaseIdx)) {
670 OPM_THROW(std::runtime_error,
671 fmt::format(
"Region {} states a vapour composition in COMPVD "
672 "while EQUIL item 10 is 3, which takes the liquid "
673 "composition at the gas-oil contact.",
676 setupComposition(reg.compositionVdTable, compvd,
677 allRows(compvd.getDepthColumn().size()));
682 const auto vaporRows = rowsOfPhase(compvd, CompvdTable::Phase::Vapor);
683 const auto liquidRows = rowsOfPhase(compvd, CompvdTable::Phase::Liquid);
684 reg.zgoc = zoneBoundary(compvd, vaporRows, liquidRows, reg, regionIdx);
686 reg.lastVaporDepth = compvd.getDepthColumn()[vaporRows.back()];
687 setupComposition(reg.vaporVdTable, compvd, vaporRows);
688 setupComposition(reg.compositionVdTable, compvd, liquidRows);
692 if (tables.hasTables(
"RTEMPVD")) {
693 const auto& rtempvd =
694 tables.getRtempvdTables().template getTable<RtempvdTable>(regionIdx);
695 std::vector<Scalar> tempDepths(rtempvd.getDepthColumn().begin(),
696 rtempvd.getDepthColumn().end());
697 const auto& tempCol = rtempvd.getTemperatureColumn();
698 std::vector<Scalar> temps(tempCol.begin(), tempCol.end());
701 if (tempDepths.size() == 1) {
702 tempDepths.push_back(tempDepths.front() + constantTableSpan);
703 temps.push_back(temps.front());
705 reg.tempVdTable.setXYContainers(tempDepths, temps);
708 const std::vector<Scalar> tempDepths{
Scalar{0}, constantTableSpan};
709 const std::vector<Scalar> temps(tempDepths.size(), tables.rtemp());
710 reg.tempVdTable.setXYContainers(tempDepths, temps);
714 auto span = std::array{std::numeric_limits<Scalar>::max(),
715 std::numeric_limits<Scalar>::lowest()};
717 if (std::cmp_equal(eqlnum[cell], regionIdx)) {
722 span[0] = comm.min(span[0]);
723 span[1] = comm.max(span[1]);
724 if (span[0] > span[1]) {
728 if (span[1] - span[0] < minimumSpanExtent) {
729 span = {span[0] - minimumSpanExtent, span[1] + minimumSpanExtent};
732 const bool waterActive = FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx);
734 reg.zwoc = record.waterOilContactDepth();
742 Scalar hcDatum = record.datumDepth();
743 Scalar hcPressure = record.datumDepthPressure();
744 const bool datumInWater = waterActive && (record.datumDepth() > reg.zwoc);
748 reg.anchoredAtWaterContact = waterActive && (record.datumDepth() >= reg.zwoc);
751 integrateWaterPressure(reg, span, gravity, numSamplePoints,
752 record.datumDepth(), record.datumDepthPressure());
754 hcPressure = reg.waterPressure->value(reg.zwoc)
755 + record.waterOilContactCapillaryPressure();
756 OpmLog::info(fmt::format(
"Equilibration region {}: the datum at {} m lies below the "
757 "water-oil contact at {} m, so it gives the water pressure; "
758 "the hydrocarbon pressure at the contact is {:.5} bar.",
759 regionIdx + 1, record.datumDepth(), reg.zwoc,
760 hcPressure / 1.0e5));
763 if (reg.initType == 1) {
764 setupSinglePhaseRegion(reg, span, gravity, numSamplePoints, regionIdx,
765 hcDatum, hcPressure);
773 OPM_THROW(std::runtime_error,
774 fmt::format(
"Compositional equilibration of region {} places the "
775 "datum at {} m, below the water-oil contact at {} m, "
776 "while EQUIL item 10 is 3. Put the datum in the "
777 "hydrocarbon column or use item 10 = 1.",
778 regionIdx + 1, record.datumDepth(), reg.zwoc));
780 setupTwoPhaseRegion(reg, record, span, gravity, numSamplePoints, regionIdx);
784 if (waterActive && !datumInWater) {
785 setupWaterZone(reg, record, span, gravity, numSamplePoints, regionIdx);
787 else if (waterActive) {
788 OpmLog::info(fmt::format(
"Equilibration region {}: the water-oil contact "
789 "is at {} m.", regionIdx + 1, reg.zwoc));
801 void setupWaterZone(Region& reg,
802 const EquilRecord& record,
803 const std::array<Scalar, 2>& span,
805 const int numSamplePoints,
806 const std::size_t regionIdx)
const
810 if (!reg.oilPressure.has_value() && !reg.gasPressure.has_value()) {
815 const bool gasAtContact = reg.gasPressure.has_value()
816 && (!reg.oilPressure.has_value() || (reg.zwoc <= reg.zgoc));
817 const auto& hcPressure = gasAtContact ? reg.gasPressure : reg.oilPressure;
818 const Scalar pcow = record.waterOilContactCapillaryPressure();
819 const Scalar pContact = hcPressure->value(reg.zwoc) - pcow;
821 integrateWaterPressure(reg, span, gravity, numSamplePoints, reg.zwoc, pContact);
823 OpmLog::info(fmt::format(
"Equilibration region {}: the water-oil contact "
824 "is at {} m.", regionIdx + 1, reg.zwoc));
829 void integrateWaterPressure(Region& reg,
830 const std::array<Scalar, 2>& span,
832 const int numSamplePoints,
834 const Scalar pressure)
const
836 const WaterODE ode(reg.tempVdTable, eosType_, gravity);
837 reg.waterPressure.emplace(ode,
838 typename WaterPressFunc::InitCond{depth, pressure},
839 numSamplePoints, waterContactSpan(reg, span));
844 static Scalar waterLimit(
const std::vector<Scalar>& limits,
845 const std::size_t cell,
848 return limits.empty() ? fallback : limits[cell];
865 void setupSinglePhaseRegion(Region& reg,
866 const std::array<Scalar, 2>& span,
868 const int numSamplePoints,
869 const std::size_t regionIdx,
871 const Scalar datumPressure)
const
874 setupTwoZoneRegion(reg, span, gravity, numSamplePoints, regionIdx,
875 datum, datumPressure);
884 const bool gasAtDatum = reg.anchoredAtWaterContact ? (datum <= reg.zgoc)
885 : (datum < reg.zgoc);
886 const auto phaseIdx = reg.statedPhaseIdx.value_or(
887 gasAtDatum ? FluidSystem::gasPhaseIdx : FluidSystem::oilPhaseIdx);
891 if ((reg.zgoc > span[0]) && (reg.zgoc < span[1]) &&
892 !compositionVariesBetween(reg, span[0], reg.zgoc) &&
893 !compositionVariesBetween(reg, reg.zgoc, span[1])) {
894 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas-oil contact "
895 "at {} m lies inside a type-1 region, but the "
896 "composition does not vary across the contact. "
897 "Compositional variation is required for proper "
898 "phase labeling.", regionIdx + 1, reg.zgoc));
901 const ODE ode([®](
const Scalar depth) {
return composition(reg, depth); },
902 reg.tempVdTable, phaseIdx, eosType_, gravity);
903 reg.oilPressure.emplace(ode,
904 typename PressFunc::InitCond{datum, datumPressure},
905 numSamplePoints, waterContactSpan(reg, span));
906 reg.nominalPhaseIdx = phaseIdx;
908 OpmLog::info(fmt::format(
"Equilibration region {}: pressure integrated with one "
909 "EOS root and the total composition from {} "
910 "(EQUIL item 10 = 1).",
911 regionIdx + 1, reg.compositionKeyword));
917 void setupTwoZoneRegion(Region& reg,
918 const std::array<Scalar, 2>& span,
920 const int numSamplePoints,
921 const std::size_t regionIdx,
923 const Scalar datumPressure)
const
925 const ODE liquidOde([®](
const Scalar depth) {
return composition(reg, depth); },
926 reg.tempVdTable, FluidSystem::oilPhaseIdx, eosType_, gravity);
927 const ODE gasOde([®](
const Scalar depth) {
return vaporComposition(reg, depth); },
928 reg.tempVdTable, FluidSystem::gasPhaseIdx, eosType_, gravity);
933 const auto pressureSpan = waterContactSpan(reg, span);
934 const std::array<Scalar, 2> datumSpan{std::min(pressureSpan[0], reg.zgoc),
935 std::max(pressureSpan[1], reg.zgoc)};
936 if ((reg.zgoc < span[0]) || (reg.zgoc > span[1])) {
937 OpmLog::warning(fmt::format(
"Equilibration region {}: the gas zone ends at {} m, "
938 "outside the cells of the region, so the COMPVD "
939 "rows of one phase describe no cell.",
944 if (datum < reg.zgoc) {
945 reg.gasPressure.emplace(gasOde,
946 typename PressFunc::InitCond{datum, datumPressure},
947 numSamplePoints, datumSpan);
948 reg.oilPressure.emplace(liquidOde,
949 typename PressFunc::InitCond{
950 reg.zgoc, reg.gasPressure->value(reg.zgoc)},
951 numSamplePoints, pressureSpan);
954 reg.oilPressure.emplace(liquidOde,
955 typename PressFunc::InitCond{datum, datumPressure},
956 numSamplePoints, datumSpan);
957 reg.gasPressure.emplace(gasOde,
958 typename PressFunc::InitCond{
959 reg.zgoc, reg.oilPressure->value(reg.zgoc)},
960 numSamplePoints, pressureSpan);
963 OpmLog::info(fmt::format(
"Equilibration region {}: COMPVD gives a gas zone above "
964 "{} m and a liquid one below it "
965 "(EQUIL item 10 = 1).",
977 void setupTwoPhaseRegion(Region& reg,
978 const EquilRecord& record,
979 const std::array<Scalar, 2>& span,
981 const int numSamplePoints,
982 const std::size_t regionIdx)
const
984 const Scalar inputReferenceDepth = record.datumDepth();
985 if (inputReferenceDepth != reg.zgoc) {
986 OpmLog::warning(fmt::format(
"Equilibration region {}: the reference depth {} m "
987 "does not coincide with the gas-oil contact when "
988 "EQUIL item 10 is 3; resetting it to the contact "
990 regionIdx + 1, inputReferenceDepth, reg.zgoc));
993 const CompVec liquid = composition(reg, reg.zgoc);
997 if (!SaturationPressure<Scalar, FluidSystem>::bubblePressure(liquid, temp, eosType_,
999 OPM_THROW(std::runtime_error,
1000 fmt::format(
"The saturation pressure calculation at the gas-oil "
1001 "contact of region {} did not converge.", regionIdx + 1));
1003 reg.vaporComposition = vapor;
1010 constexpr Scalar oneAtmosphere = unit::atm;
1011 const Scalar inputPressure = record.datumDepthPressure();
1012 const bool resetToPsat = record.setToSaturationPressure()
1013 && (std::abs(inputPressure - psat) >= oneAtmosphere);
1014 const Scalar referencePressure = resetToPsat ? psat : inputPressure;
1016 OpmLog::info(fmt::format(
"Equilibration region {}: two phases, liquid composition "
1017 "specified (EQUIL item 10 is 3). The saturation pressure "
1018 "at the gas-oil contact ({} m) is {:.6g} bar.",
1019 regionIdx + 1, reg.zgoc,
1020 unit::convert::to(psat, unit::barsa)));
1023 OpmLog::warning(fmt::format(
"Equilibration region {}: the datum pressure {:.6g} bar "
1024 "differs from the saturation pressure {:.6g} bar at the "
1025 "gas-oil contact by one atmosphere or more; the "
1026 "saturation pressure is used instead.",
1028 unit::convert::to(inputPressure, unit::barsa),
1029 unit::convert::to(psat, unit::barsa)));
1032 const ODE oilOde([®](
const Scalar depth) {
return composition(reg, depth); },
1033 reg.tempVdTable, FluidSystem::oilPhaseIdx, eosType_, gravity);
1034 reg.oilPressure.emplace(oilOde,
1035 typename PressFunc::InitCond{reg.zgoc, referencePressure},
1036 numSamplePoints, waterContactSpan(reg, span));
1043 gasComposition = [®](
const Scalar depth) {
return vaporComposition(reg, depth); };
1046 gasComposition = [vapor](
const Scalar) {
return vapor; };
1048 const ODE gasOde(gasComposition,
1049 reg.tempVdTable, FluidSystem::gasPhaseIdx, eosType_, gravity);
1050 reg.gasPressure.emplace(gasOde,
1051 typename PressFunc::InitCond{reg.zgoc, referencePressure},
1052 numSamplePoints, waterContactSpan(reg, span));
1060 static bool isInGasZone(
const Region& reg,
const Scalar depth)
1062 if (reg.twoZone && (reg.initType == 1) && (depth <= reg.lastVaporDepth)) {
1065 return ((reg.initType == 3) || reg.twoZone) && (depth < reg.zgoc);
1069 const std::size_t cell)
const
1071 const bool inGasZone = isInGasZone(reg, depth);
1073 const CompVec z = [®, depth, inGasZone]() {
1075 return composition(reg, depth);
1079 return reg.twoZone ? vaporComposition(reg, depth) : reg.vaporComposition;
1081 const auto& pressFunc = inGasZone ? reg.gasPressure : reg.oilPressure;
1082 if (!pressFunc.has_value()) {
1083 OPM_THROW(std::runtime_error,
1084 "Evaluating the equilibrated pressure of a region without cells.");
1091 const Scalar hydrocarbonPressure = pressFunc->value(depth);
1092 const bool inWaterZone = (depth > reg.zwoc) && reg.waterPressure.has_value();
1093 const Scalar press = inWaterZone ? reg.waterPressure->value(depth)
1094 : hydrocarbonPressure;
1097 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
1098 if (FluidSystem::phaseIsActive(phaseIdx)) {
1099 fs.setPressure(phaseIdx, hydrocarbonPressure);
1100 fs.setSaturation(phaseIdx, 0.0);
1107 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
1111 sWat = (depth > reg.zwoc) ? waterLimit(maxWater_, cell,
Scalar{1})
1112 : waterLimit(connateWater_, cell,
Scalar{0});
1113 fs.setSaturation(FluidSystem::waterPhaseIdx, sWat);
1114 if (reg.waterPressure.has_value()) {
1115 fs.setPressure(FluidSystem::waterPhaseIdx, reg.waterPressure->value(depth));
1122 fs.setSaturation(inGasZone ? FluidSystem::gasPhaseIdx : reg.nominalPhaseIdx,
1125 for (
int c = 0; c < numComponents; ++c) {
1126 fs.setMoleFraction(c, z[c]);
1131 CompositionalConfig::EOSType eosType_;
1135 std::vector<Scalar> connateWater_;
1136 std::vector<Scalar> maxWater_;
1137 std::vector<FluidState> fluidStates_;
1138 std::vector<Scalar> referencePressures_;
#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:93
Tabulated1DFunction< Scalar > TabulatedFunction
Definition: InitStateEquilComp.hpp:98
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquilComp.hpp:112
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:95
std::array< Scalar, FluidSystem::numComponents > CompVec
Definition: InitStateEquilComp.hpp:96
std::function< CompVec(Scalar)> CompositionFunction
Definition: InitStateEquilComp.hpp:97
EosDensityODE(CompositionFunction composition, const TabulatedFunction &tempVdTable, const unsigned phaseIdx, const CompositionalConfig::EOSType eosType, const Scalar normGrav)
Definition: InitStateEquilComp.hpp:100
Definition: InitStateEquilComp.hpp:144
Scalar operator()(const Scalar depth, const Scalar press) const
Definition: InitStateEquilComp.hpp:157
WaterDensityODE(const TabulatedFunction &tempVdTable, const CompositionalConfig::EOSType eosType, const Scalar normGrav)
Definition: InitStateEquilComp.hpp:149
Tabulated1DFunction< Scalar > TabulatedFunction
Definition: InitStateEquilComp.hpp:147
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:146
Computes the initial state of a compositional model from hydrostatic equilibrium (the EQUIL and ZMFVD...
Definition: InitStateEquilComp.hpp:214
CompositionalFluidState< Scalar, FluidSystem > FluidState
Definition: InitStateEquilComp.hpp:217
typename FluidSystem::Scalar Scalar
Definition: InitStateEquilComp.hpp:216
const std::vector< Scalar > & referencePressures() const
Definition: InitStateEquilComp.hpp:305
const std::vector< FluidState > & fluidStates() const
Definition: InitStateEquilComp.hpp:299
InitialStateComputer(const EclipseState &inputState, const CompositionalConfig::EOSType eosType, const std::vector< Scalar > &cellCenterDepth, const std::vector< int > &eqlnum, const Parallel::Communication &comm, const Scalar gravity, const int numSamplePoints, const std::vector< Scalar > &connateWater={}, const std::vector< Scalar > &maxWater={})
Definition: InitStateEquilComp.hpp:231
std::vector< FluidState > & fluidStates()
Definition: InitStateEquilComp.hpp:296
Definition: PressureFunction.hpp:128
Scalar evalDepthTable(const Tabulated1DFunction< Scalar > &table, const Scalar depth)
Definition: InitStateEquilComp.hpp:82
Definition: InitStateEquilComp.hpp:74
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