20#ifndef OPM_BLACKOIL_WELL_MODEL_NETWORK_PRESSURE_COMPUTATION_HPP
21#define OPM_BLACKOIL_WELL_MODEL_NETWORK_PRESSURE_COMPUTATION_HPP
23#include <opm/common/TimingMacros.hpp>
25#include <opm/input/eclipse/Schedule/Group/GConSump.hpp>
26#include <opm/input/eclipse/Schedule/Group/GSatProd.hpp>
27#include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
28#include <opm/input/eclipse/Schedule/Schedule.hpp>
29#include <opm/input/eclipse/Schedule/ScheduleState.hpp>
30#include <opm/input/eclipse/Schedule/SummaryState.hpp>
31#include <opm/input/eclipse/Schedule/VFPProdTable.hpp>
32#include <opm/input/eclipse/Units/Units.hpp>
34#include <opm/output/data/Groups.hpp>
41#include <fmt/format.h>
58template<
typename Scalar>
72 template<
typename Scalar,
typename IndexTraits,
typename Table>
73 bool clampToTableAxes(
const Table& table, std::vector<Scalar>& rates, Scalar& up_press)
76 const auto& thp_axis = table.getTHPAxis();
77 const Scalar thp_lo = thp_axis.front();
78 const Scalar thp_hi = thp_axis.back();
79 if (up_press < thp_lo || up_press > thp_hi) {
80 up_press = std::clamp(up_press, thp_lo, thp_hi);
83 const auto& flo_axis = table.getFloAxis();
84 const Scalar flo = std::abs(
getFlo(table,
85 rates[IndexTraits::waterPhaseIdx],
86 rates[IndexTraits::oilPhaseIdx],
87 rates[IndexTraits::gasPhaseIdx]));
88 const Scalar flo_hi = flo_axis.back();
89 if (flo > flo_hi && flo > 0.0) {
90 const Scalar s = flo_hi / flo;
91 std::ranges::transform(rates, rates.begin(), [s](
const auto r) { return s * r; });
93 }
else if (flo < flo_axis.front()) {
94 const Scalar flo_lo = flo_axis.front();
96 const Scalar s = flo_lo / flo;
97 std::ranges::transform(rates, rates.begin(), [s](
const auto r) { return s * r; });
101 switch (table.getFloType()) {
102 case VFPInjTable::FLO_TYPE::FLO_OIL:
103 rates[IndexTraits::oilPhaseIdx] = flo_lo;
105 case VFPInjTable::FLO_TYPE::FLO_WAT:
106 rates[IndexTraits::waterPhaseIdx] = flo_lo;
108 case VFPInjTable::FLO_TYPE::FLO_GAS:
109 rates[IndexTraits::gasPhaseIdx] = flo_lo;
123 const std::string& name)
125 return network.has_node(name)
126 && (network.uptree_branch(name).has_value()
127 || !network.downtree_branches(name).empty());
143template<
typename Scalar>
144std::map<std::string, Scalar>
146 const ScheduleState& sched_state,
147 const SummaryState& summary_state)
149 std::map<std::string, Scalar> node_rates;
150 if (!network.active()) {
154 const auto& gconsump = sched_state.gconsump();
155 for (
const auto& group_name : gconsump.group_names()) {
156 const auto gc = gconsump.get(group_name, summary_state);
157 const auto net_rate =
static_cast<Scalar
>(gc.consumption_rate - gc.import_rate);
158 if (net_rate == Scalar{0} || gc.network_node == GConSump::NO_NODES) {
162 if (!gc.network_node.empty()) {
164 node_rates[gc.network_node] += net_rate;
169 Scalar efficiency = 1.0;
170 std::string name = group_name;
171 while (sched_state.groups.has(name)) {
173 if (network.downtree_branches(name).empty()) {
174 node_rates[name] += efficiency * net_rate;
178 if (name ==
"FIELD") {
181 const auto& group = sched_state.groups(name);
182 efficiency *= group.getGroupEfficiencyFactor(
true);
183 name = group.parent();
192template<
typename Scalar,
typename IndexTraits,
typename VfpProperties>
196template<
typename Scalar,
typename IndexTraits>
202 std::ranges::transform(rates, rates.begin(), [](
const auto r) { return -r; });
205 template <
class GroupState>
207 const std::string& node)
212 template <
class GroupState>
213 static const std::vector<Scalar>
215 const std::string& node)
220 template<
typename Branch>
223 std::vector<Scalar> rates,
225 const Branch& upbranch,
226 const UnitSystem& unit_system)
231 const auto& table = vfp_props.
getTable(table_id);
232 const auto alq_type = table.getALQType();
233 const auto dimension = VFPProdTable::ALQDimension(alq_type, unit_system);
234 const Scalar alq = upbranch.alq_value(dimension).value_or(0.0);
242 rates[IndexTraits::waterPhaseIdx],
243 rates[IndexTraits::oilPhaseIdx],
244 rates[IndexTraits::gasPhaseIdx],
256template<
typename Scalar,
typename IndexTraits>
263 template <
class GroupState>
265 const std::string& node)
270 template <
class GroupState>
271 static const std::vector<Scalar>
273 const std::string& node)
278 template<
typename Branch>
281 std::vector<Scalar> rates,
287 result.
clamped = detail::clampToTableAxes<Scalar, IndexTraits>(vfp_props.
getTable(table_id), rates, up_press);
289 rates[IndexTraits::waterPhaseIdx],
290 rates[IndexTraits::oilPhaseIdx],
291 rates[IndexTraits::gasPhaseIdx],
303template<
typename Scalar>
306 const std::map<std::string, Scalar>& overrides)
310 if (overrides.count(node) > 0) {
313 if (network.node(node).terminal_pressure().has_value()) {
316 const auto branch = network.uptree_branch(node);
317 if (!branch.has_value()) {
320 node = branch->uptree_node();
327template<
typename GenericWellModel,
typename VfpProperties,
typename Communication = Parallel::Communication>
342 const Network::ExtNetwork& network,
343 const VfpProperties& vfp_props,
344 const UnitSystem& unit_system,
345 const int report_step_idx,
347 const std::map<std::string, typename GenericWellModel::Scalar>*
348 terminal_pressure_overrides =
nullptr,
349 std::map<std::string, typename GenericWellModel::Scalar>
350 node_gas_consumption = {})
351 : well_model_(well_model)
353 , vfp_props_(vfp_props)
354 , unit_system_(unit_system)
355 , report_step_idx_(report_step_idx)
357 , terminal_pressure_overrides_(terminal_pressure_overrides)
358 , node_gas_consumption_(std::move(node_gas_consumption))
362 using Scalar =
typename GenericWellModel::Scalar;
364 std::pair<std::map<std::string, Scalar>, std::map<std::string, data::BranchData>>
run()
366 for (
const auto& root : topRoots()) {
372 const auto [root_to_child_nodes, leaf_nodes] = collectTreeNodes(root);
376 auto node_inflows = initializeLeafInflows(leaf_nodes);
382 accumulateInflows(root_to_child_nodes, node_inflows);
386 computeNodePressures(root_to_child_nodes, node_inflows);
388#ifdef OPM_NETWORK_PRESSURE_TRACE
391 OpmLog::debug(
"Network pressure computation completed for root " + root +
". Node pressures:");
392 for (
const auto& [node, pressure] : node_pressures_) {
393 OpmLog::debug(
"Network node " + node +
" pressure: " +
std::to_string(pressure/1e5) +
" bar");
395 OpmLog::debug(
"Node inflows:");
396 for (
const auto& [node, inflows] : node_inflows) {
397 OpmLog::debug(
"Network node " + node +
" inflows: "
404 return {node_pressures_, branch_data_};
412 return invalid_nodes_;
423 std::vector<std::string> topRoots()
const
425 std::vector<std::string> top_roots;
426 for (
const auto& root : network_.roots()) {
427 auto top = root.get().name();
428 auto visited = std::set<std::string>{top};
430 while (
const auto upbranch = network_.uptree_branch(node)) {
431 node = upbranch->uptree_node();
432 if (!visited.insert(node).second) {
435 if (network_.node(node).terminal_pressure().has_value()) {
439 if (std::ranges::find(top_roots, top) == top_roots.end()) {
440 top_roots.push_back(top);
446 std::pair<std::vector<std::string>, std::set<std::string>>
447 collectTreeNodes(
const std::string& root)
const
449 std::stack<std::string> children;
450 std::set<std::string> leaf_nodes;
451 std::vector<std::string> root_to_child_nodes;
453 while (!children.empty()) {
454 const auto node = children.top();
456 root_to_child_nodes.push_back(node);
457 auto branches = network_.downtree_branches(node);
458 if (branches.empty()) {
459 leaf_nodes.insert(node);
461 for (
const auto& branch : branches) {
462 children.push(branch.downtree_node());
466 assert(children.empty());
467 return {root_to_child_nodes, leaf_nodes};
470 std::map<std::string, std::vector<Scalar>>
471 initializeLeafInflows(
const std::set<std::string>& leaf_nodes)
const
473 std::map<std::string, std::vector<Scalar>> node_inflows;
474 const std::vector<Scalar> zero_rates(3, 0.0);
476 for (
const auto& node : leaf_nodes) {
481 using Calc = NetworkVfpPressureCalculator<Scalar, IndexTraits, VfpProperties>;
482 if (!Calc::hasLeafNodeRate(well_model_.groupStateHelper().groupState(), node)) {
483 node_inflows[node] = zero_rates;
487 node_inflows[node] = Calc::leafNodeRate(well_model_.groupStateHelper().groupState(),
489 if (network_.node(node).add_gas_lift_gas()) {
490 addGasLiftGas(node, node_inflows[node]);
497 void addGasLiftGas(
const std::string& node,
498 std::vector<Scalar>& rates)
const
500 const auto& group = well_model_.schedule().getGroup(node, report_step_idx_);
501 const auto& well_state = well_model_.groupStateHelper().wellState();
504 for (
const std::string& wellname : group.wells()) {
505 const Well& well = well_model_.schedule().getWell(wellname, report_step_idx_);
506 if (well.isInjector() || !well_state.isOpen(wellname)) {
510 const Scalar efficiency = well.getEfficiencyFactor(
true)
511 * well_state.getGlobalEfficiencyScalingFactor(wellname);
512 const auto& well_index = well_state.index(wellname);
513 if (well_index.has_value() &&
514 well_state.wellIsOwned(well_index.value(), wellname))
516 alq += well_state.well(wellname).alq_state.get() * efficiency;
525 alq = comm_.sum(alq);
529 const auto& helper = well_model_.groupStateHelper();
530 if constexpr (
requires { helper.reservoirCouplingMasterGroupGasLiftRate(group,
true); }) {
531 alq += helper.reservoirCouplingMasterGroupGasLiftRate(group,
true);
535 if (group.hasSatelliteProduction()) {
536 const auto gsrate = this->well_model_
537 .schedule()[this->report_step_idx_]
538 .satelliteProduction(node)
539 .getRate(GSatProd::Rate::GLift, this->well_model_.summaryState());
541 alq +=
static_cast<Scalar>(gsrate);
544 rates[IndexTraits::gasPhaseIdx] += alq;
550 void applyGasConsumption(
const std::string& node,
551 std::vector<Scalar>& rates)
const
553 const auto it = node_gas_consumption_.find(node);
554 if (it == node_gas_consumption_.end()) {
558 rates.assign(3, 0.0);
560 if (rates.size() <=
static_cast<std::size_t
>(IndexTraits::gasPhaseIdx)) {
563 auto& gas_rate = rates[IndexTraits::gasPhaseIdx];
564 gas_rate = std::max(gas_rate - it->second,
Scalar{0});
567 void accumulateInflows(
const std::vector<std::string>& root_to_child_nodes,
568 std::map<std::string, std::vector<Scalar>>& node_inflows)
const
570 const auto child_to_root_nodes = std::ranges::reverse_view(root_to_child_nodes);
574 for (
const auto& node : child_to_root_nodes) {
575 applyGasConsumption(node, node_inflows[node]);
576 const auto upbranch = network_.uptree_branch(node);
580 std::vector<Scalar>& up = node_inflows[(*upbranch).uptree_node()];
581 const std::vector<Scalar>& down = node_inflows[node];
583 const Scalar efficiency = network_.node(node).efficiency();
585 up = std::vector<Scalar>(down.size(), 0.0);
587 assert(up.size() == down.size());
588 for (std::size_t ii = 0; ii < up.size(); ++ii) {
589 up[ii] += efficiency * down[ii];
594 void computeNodePressures(
const std::vector<std::string>& root_to_child_nodes,
595 const std::map<std::string, std::vector<Scalar>>& node_inflows)
597 for (
const auto& node : root_to_child_nodes) {
599 if (node_pressures_.find(node) != node_pressures_.end()) {
603 auto terminal_pressure = network_.node(node).terminal_pressure();
604 if (terminal_pressure && terminal_pressure_overrides_) {
607 if (
const auto it = terminal_pressure_overrides_->find(node);
608 it != terminal_pressure_overrides_->end()) {
609 terminal_pressure = it->second;
612 const auto upbranch = network_.uptree_branch(node);
613 assert(upbranch || terminal_pressure);
614 using Calc = NetworkVfpPressureCalculator<Scalar, IndexTraits, VfpProperties>;
616 if (terminal_pressure) {
617 node_pressures_[node] = *terminal_pressure;
618 const auto up_it = upbranch
619 ? node_pressures_.find((*upbranch).uptree_node())
620 : node_pressures_.end();
621 if (up_it != node_pressures_.end()) {
623 const Scalar up_press = up_it->second;
624 auto rates = node_inflows.at(node);
625 branch_data_.try_emplace(node,
626 *terminal_pressure - up_press,
627 rates[IndexTraits::oilPhaseIdx],
628 rates[IndexTraits::waterPhaseIdx],
629 rates[IndexTraits::gasPhaseIdx]);
633 branch_data_.emplace(node, data::BranchData{0.0, 0.0, 0.0, 0.0});
638 const std::string& up_node = (*upbranch).uptree_node();
639 const Scalar up_press = node_pressures_[up_node];
641 if (invalid_nodes_.count(up_node) > 0) {
642 invalid_nodes_.insert(node);
644 const auto vfp_table = (*upbranch).vfp_table();
647 if (network_.node(node).as_choke()) {
649 node_pressures_[node] = well_model_.groupStateHelper().groupState().well_group_thp(node);
651 node_pressures_[node] = up_press;
653 auto rates = node_inflows.at(node);
654 branch_data_.try_emplace(node,
655 node_pressures_[node] - up_press,
656 rates[IndexTraits::oilPhaseIdx],
657 rates[IndexTraits::waterPhaseIdx],
658 rates[IndexTraits::gasPhaseIdx]);
662 OPM_TIMEBLOCK(NetworkVfpCalculations);
663 auto rates = node_inflows.at(node);
664 assert(rates.size() == 3);
665 Calc::prepareRates(rates);
666 const auto branch = Calc::compute(vfp_props_, *vfp_table, rates, up_press, *upbranch, unit_system_);
669 const Scalar node_pressure = branch.valid ? branch.pressure : up_press;
671 invalid_nodes_.insert(node);
673 if (!branch.valid || branch.clamped) {
674 OpmLog::debug(fmt::format(
"Network branch {} -> {}: VFP table {} {} at rates ({:.4g}, {:.4g}, {:.4g}) sm3/d, "
675 "upstream pressure {:.2f} bar",
676 up_node, node, *vfp_table,
677 branch.valid ?
"lookup clamped to the table axes" :
"has no solution",
678 rates[IndexTraits::waterPhaseIdx] * unit::day,
679 rates[IndexTraits::oilPhaseIdx] * unit::day,
680 rates[IndexTraits::gasPhaseIdx] * unit::day,
681 up_press / unit::barsa));
683 node_pressures_[node] = node_pressure;
685 branch_data_.try_emplace(node,
686 node_pressure - up_press,
687 -rates[IndexTraits::oilPhaseIdx],
688 -rates[IndexTraits::waterPhaseIdx],
689 -rates[IndexTraits::gasPhaseIdx]);
693 const GenericWellModel& well_model_;
694 const Network::ExtNetwork& network_;
695 const VfpProperties& vfp_props_;
696 const UnitSystem& unit_system_;
697 const int report_step_idx_;
699 const std::map<std::string, Scalar>* terminal_pressure_overrides_;
700 std::map<std::string, Scalar> node_gas_consumption_;
701 std::map<std::string, Scalar> node_pressures_;
702 std::map<std::string, data::BranchData> branch_data_;
703 std::set<std::string> invalid_nodes_;
Definition: GroupState.hpp:42
bool has_network_leaf_node_production_rates(const std::string &gname) const
const std::vector< Scalar > & network_leaf_node_injection_rates(const std::string &gname) const
bool has_network_leaf_node_injection_rates(const std::string &gname) const
const std::vector< Scalar > & network_leaf_node_production_rates(const std::string &gname) const
Class to compute network pressures using VFP tables, given flow rates for each group and fixed pressu...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:329
NetworkPressureComputation(const GenericWellModel &well_model, const Network::ExtNetwork &network, const VfpProperties &vfp_props, const UnitSystem &unit_system, const int report_step_idx, const Communication &comm, const std::map< std::string, typename GenericWellModel::Scalar > *terminal_pressure_overrides=nullptr, std::map< std::string, typename GenericWellModel::Scalar > node_gas_consumption={})
Definition: BlackoilWellModelNetworkPressureComputation.hpp:341
const std::set< std::string > & invalidNodes() const
Definition: BlackoilWellModelNetworkPressureComputation.hpp:410
typename GenericWellModel::Scalar Scalar
Definition: BlackoilWellModelNetworkPressureComputation.hpp:362
std::pair< std::map< std::string, Scalar >, std::map< std::string, data::BranchData > > run()
Definition: BlackoilWellModelNetworkPressureComputation.hpp:364
GenericWellModel::IndexTraits IndexTraits
Definition: BlackoilWellModelNetworkPressureComputation.hpp:363
Definition: VFPInjProperties.hpp:34
const VFPInjTable & getTable(const int table_id) const
EvalWell bhp(const int table_id, const EvalWell &aqua, const EvalWell &liquid, const EvalWell &vapour, const Scalar thp) const
Definition: VFPProdProperties.hpp:38
EvalWell bhp(const int table_id, const EvalWell &aqua, const EvalWell &liquid, const EvalWell &vapour, const Scalar thp, const Scalar alq, const Scalar explicit_wfr, const Scalar explicit_gfr, const bool use_expvfp) const
const VFPProdTable & getTable(const int table_id) const
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
bool isConnectedNetworkNode(const Network::ExtNetwork &network, const std::string &name)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:122
bool clampToTableAxes(const Table &table, std::vector< Scalar > &rates, Scalar &up_press)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:73
T getFlo(const VFPProdTable &table, const T &aqua, const T &liquid, const T &vapour)
Definition: blackoilbioeffectsmodules.hh:45
bool nodeFollowsOverriddenNode(const Network::ExtNetwork &network, std::string node, const std::map< std::string, Scalar > &overrides)
Whether the pressure of a node follows from one of the overridden nodes (see the terminal pressure ov...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:304
std::map< std::string, Scalar > networkGasConsumptionRates(const Network::ExtNetwork &network, const ScheduleState &sched_state, const SummaryState &summary_state)
Net gas consumption (GCONSUMP consumption minus import, SI units) to remove from the flow at each nod...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:145
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
Result of a single network branch VFP lookup.
Definition: BlackoilWellModelNetworkPressureComputation.hpp:60
bool valid
Definition: BlackoilWellModelNetworkPressureComputation.hpp:64
Scalar pressure
Definition: BlackoilWellModelNetworkPressureComputation.hpp:61
bool clamped
Definition: BlackoilWellModelNetworkPressureComputation.hpp:66
static void prepareRates(std::vector< Scalar > &)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:259
static bool hasLeafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:264
static NetworkBranchPressure< Scalar > compute(const VFPInjProperties< Scalar > &vfp_props, const int table_id, std::vector< Scalar > rates, Scalar up_press, const Branch &, const UnitSystem &)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:279
static const std::vector< Scalar > leafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:272
static const std::vector< Scalar > leafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:214
static void prepareRates(std::vector< Scalar > &rates)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:199
static NetworkBranchPressure< Scalar > compute(const VFPProdProperties< Scalar > &vfp_props, const int table_id, std::vector< Scalar > rates, Scalar up_press, const Branch &upbranch, const UnitSystem &unit_system)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:221
static bool hasLeafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:206
Helper class to insulate the NetworkPressureComputation class from the differences between production...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:193