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/GSatProd.hpp>
26#include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
27#include <opm/input/eclipse/Schedule/Schedule.hpp>
28#include <opm/input/eclipse/Schedule/ScheduleState.hpp>
29#include <opm/input/eclipse/Schedule/VFPProdTable.hpp>
30#include <opm/input/eclipse/Units/Units.hpp>
32#include <opm/output/data/Groups.hpp>
39#include <fmt/format.h>
54template<
typename Scalar>
68 template<
typename Scalar,
typename IndexTraits,
typename Table>
69 bool clampToTableAxes(
const Table& table, std::vector<Scalar>& rates, Scalar& up_press)
72 const auto& thp_axis = table.getTHPAxis();
73 const Scalar thp_lo = thp_axis.front();
74 const Scalar thp_hi = thp_axis.back();
75 if (up_press < thp_lo || up_press > thp_hi) {
76 up_press = std::clamp(up_press, thp_lo, thp_hi);
79 const auto& flo_axis = table.getFloAxis();
80 const Scalar flo = std::abs(
getFlo(table,
81 rates[IndexTraits::waterPhaseIdx],
82 rates[IndexTraits::oilPhaseIdx],
83 rates[IndexTraits::gasPhaseIdx]));
84 const Scalar flo_hi = flo_axis.back();
85 if (flo > flo_hi && flo > 0.0) {
86 const Scalar s = flo_hi / flo;
87 std::ranges::transform(rates, rates.begin(), [s](
const auto r) { return s * r; });
89 }
else if (flo < flo_axis.front()) {
90 const Scalar flo_lo = flo_axis.front();
92 const Scalar s = flo_lo / flo;
93 std::ranges::transform(rates, rates.begin(), [s](
const auto r) { return s * r; });
97 switch (table.getFloType()) {
98 case VFPInjTable::FLO_TYPE::FLO_OIL:
99 rates[IndexTraits::oilPhaseIdx] = flo_lo;
101 case VFPInjTable::FLO_TYPE::FLO_WAT:
102 rates[IndexTraits::waterPhaseIdx] = flo_lo;
104 case VFPInjTable::FLO_TYPE::FLO_GAS:
105 rates[IndexTraits::gasPhaseIdx] = flo_lo;
117template<
typename Scalar,
typename IndexTraits,
typename VfpProperties>
121template<
typename Scalar,
typename IndexTraits>
127 std::ranges::transform(rates, rates.begin(), [](
const auto r) { return -r; });
130 template <
class GroupState>
132 const std::string& node)
137 template <
class GroupState>
138 static const std::vector<Scalar>
140 const std::string& node)
145 template<
typename Branch>
148 std::vector<Scalar> rates,
150 const Branch& upbranch,
151 const UnitSystem& unit_system)
156 const auto& table = vfp_props.
getTable(table_id);
157 const auto alq_type = table.getALQType();
158 const auto dimension = VFPProdTable::ALQDimension(alq_type, unit_system);
159 const Scalar alq = upbranch.alq_value(dimension).value_or(0.0);
167 rates[IndexTraits::waterPhaseIdx],
168 rates[IndexTraits::oilPhaseIdx],
169 rates[IndexTraits::gasPhaseIdx],
181template<
typename Scalar,
typename IndexTraits>
188 template <
class GroupState>
190 const std::string& node)
195 template <
class GroupState>
196 static const std::vector<Scalar>
198 const std::string& node)
203 template<
typename Branch>
206 std::vector<Scalar> rates,
212 result.
clamped = detail::clampToTableAxes<Scalar, IndexTraits>(vfp_props.
getTable(table_id), rates, up_press);
214 rates[IndexTraits::waterPhaseIdx],
215 rates[IndexTraits::oilPhaseIdx],
216 rates[IndexTraits::gasPhaseIdx],
226template<
typename GenericWellModel,
typename VfpProperties,
typename Communication = Parallel::Communication>
231 const Network::ExtNetwork& network,
232 const VfpProperties& vfp_props,
233 const UnitSystem& unit_system,
234 const int report_step_idx,
236 : well_model_(well_model)
238 , vfp_props_(vfp_props)
239 , unit_system_(unit_system)
240 , report_step_idx_(report_step_idx)
245 using Scalar =
typename GenericWellModel::Scalar;
247 std::pair<std::map<std::string, Scalar>, std::map<std::string, data::BranchData>>
run()
249 const auto roots = network_.roots();
250 for (
const auto& root : roots) {
256 const auto [root_to_child_nodes, leaf_nodes] = collectTreeNodes(root.get().name());
260 auto node_inflows = initializeLeafInflows(leaf_nodes);
266 accumulateInflows(root_to_child_nodes, node_inflows);
270 computeNodePressures(root_to_child_nodes, node_inflows);
272#ifdef OPM_NETWORK_PRESSURE_TRACE
275 OpmLog::debug(
"Network pressure computation completed for root " + root.get().name() +
". Node pressures:");
276 for (
const auto& [node, pressure] : node_pressures_) {
277 OpmLog::debug(
"Network node " + node +
" pressure: " +
std::to_string(pressure/1e5) +
" bar");
279 OpmLog::debug(
"Node inflows:");
280 for (
const auto& [node, inflows] : node_inflows) {
281 OpmLog::debug(
"Network node " + node +
" inflows: "
288 return {node_pressures_, branch_data_};
296 return invalid_nodes_;
300 std::pair<std::vector<std::string>, std::set<std::string>>
301 collectTreeNodes(
const std::string& root)
const
303 std::stack<std::string> children;
304 std::set<std::string> leaf_nodes;
305 std::vector<std::string> root_to_child_nodes;
307 while (!children.empty()) {
308 const auto node = children.top();
310 root_to_child_nodes.push_back(node);
311 auto branches = network_.downtree_branches(node);
312 if (branches.empty()) {
313 leaf_nodes.insert(node);
315 for (
const auto& branch : branches) {
316 children.push(branch.downtree_node());
320 assert(children.empty());
321 return {root_to_child_nodes, leaf_nodes};
324 std::map<std::string, std::vector<Scalar>>
325 initializeLeafInflows(
const std::set<std::string>& leaf_nodes)
const
327 std::map<std::string, std::vector<Scalar>> node_inflows;
328 const std::vector<Scalar> zero_rates(3, 0.0);
330 for (
const auto& node : leaf_nodes) {
335 using Calc = NetworkVfpPressureCalculator<Scalar, IndexTraits, VfpProperties>;
336 if (!Calc::hasLeafNodeRate(well_model_.groupStateHelper().groupState(), node)) {
337 node_inflows[node] = zero_rates;
341 node_inflows[node] = Calc::leafNodeRate(well_model_.groupStateHelper().groupState(),
343 if (network_.node(node).add_gas_lift_gas()) {
344 addGasLiftGas(node, node_inflows[node]);
351 void addGasLiftGas(
const std::string& node,
352 std::vector<Scalar>& rates)
const
354 const auto& group = well_model_.schedule().getGroup(node, report_step_idx_);
355 const auto& well_state = well_model_.groupStateHelper().wellState();
358 for (
const std::string& wellname : group.wells()) {
359 const Well& well = well_model_.schedule().getWell(wellname, report_step_idx_);
360 if (well.isInjector() || !well_state.isOpen(wellname)) {
364 const Scalar efficiency = well.getEfficiencyFactor(
true)
365 * well_state.getGlobalEfficiencyScalingFactor(wellname);
366 const auto& well_index = well_state.index(wellname);
367 if (well_index.has_value() &&
368 well_state.wellIsOwned(well_index.value(), wellname))
370 alq += well_state.well(wellname).alq_state.get() * efficiency;
379 alq = comm_.sum(alq);
382 if (group.hasSatelliteProduction()) {
383 const auto gsrate = this->well_model_
384 .schedule()[this->report_step_idx_]
385 .satelliteProduction(node)
386 .getRate(GSatProd::Rate::GLift, this->well_model_.summaryState());
388 alq +=
static_cast<Scalar>(gsrate);
391 rates[IndexTraits::gasPhaseIdx] += alq;
394 void accumulateInflows(
const std::vector<std::string>& root_to_child_nodes,
395 std::map<std::string, std::vector<Scalar>>& node_inflows)
const
397 const auto child_to_root_nodes = std::ranges::reverse_view(root_to_child_nodes);
399 for (
const auto& node : child_to_root_nodes) {
400 const auto upbranch = network_.uptree_branch(node);
404 std::vector<Scalar>& up = node_inflows[(*upbranch).uptree_node()];
405 const std::vector<Scalar>& down = node_inflows[node];
407 const Scalar efficiency = network_.node(node).efficiency();
409 up = std::vector<Scalar>(down.size(), 0.0);
411 assert(up.size() == down.size());
412 for (std::size_t ii = 0; ii < up.size(); ++ii) {
413 up[ii] += efficiency * down[ii];
418 void computeNodePressures(
const std::vector<std::string>& root_to_child_nodes,
419 const std::map<std::string, std::vector<Scalar>>& node_inflows)
421 for (
const auto& node : root_to_child_nodes) {
423 if (node_pressures_.find(node) != node_pressures_.end()) {
427 const auto terminal_pressure = network_.node(node).terminal_pressure();
428 const auto upbranch = network_.uptree_branch(node);
429 assert(upbranch || terminal_pressure);
430 using Calc = NetworkVfpPressureCalculator<Scalar, IndexTraits, VfpProperties>;
432 if (terminal_pressure) {
433 node_pressures_[node] = *terminal_pressure;
436 const Scalar up_press = node_pressures_[(*upbranch).uptree_node()];
437 auto rates = node_inflows.at(node);
438 branch_data_.try_emplace(node,
439 *terminal_pressure - up_press,
440 rates[IndexTraits::oilPhaseIdx],
441 rates[IndexTraits::waterPhaseIdx],
442 rates[IndexTraits::gasPhaseIdx]);
445 branch_data_.emplace(node, data::BranchData{0.0, 0.0, 0.0, 0.0});
450 const std::string& up_node = (*upbranch).uptree_node();
451 const Scalar up_press = node_pressures_[up_node];
453 if (invalid_nodes_.count(up_node) > 0) {
454 invalid_nodes_.insert(node);
456 const auto vfp_table = (*upbranch).vfp_table();
459 if (network_.node(node).as_choke()) {
461 node_pressures_[node] = well_model_.groupStateHelper().groupState().well_group_thp(node);
463 node_pressures_[node] = up_press;
465 auto rates = node_inflows.at(node);
466 branch_data_.try_emplace(node,
467 node_pressures_[node] - up_press,
468 rates[IndexTraits::oilPhaseIdx],
469 rates[IndexTraits::waterPhaseIdx],
470 rates[IndexTraits::gasPhaseIdx]);
474 OPM_TIMEBLOCK(NetworkVfpCalculations);
475 auto rates = node_inflows.at(node);
476 assert(rates.size() == 3);
477 Calc::prepareRates(rates);
478 const auto branch = Calc::compute(vfp_props_, *vfp_table, rates, up_press, *upbranch, unit_system_);
481 const Scalar node_pressure = branch.valid ? branch.pressure : up_press;
483 invalid_nodes_.insert(node);
485 if (!branch.valid || branch.clamped) {
486 OpmLog::debug(fmt::format(
"Network branch {} -> {}: VFP table {} {} at rates ({:.4g}, {:.4g}, {:.4g}) sm3/d, "
487 "upstream pressure {:.2f} bar",
488 up_node, node, *vfp_table,
489 branch.valid ?
"lookup clamped to the table axes" :
"has no solution",
490 rates[IndexTraits::waterPhaseIdx] * unit::day,
491 rates[IndexTraits::oilPhaseIdx] * unit::day,
492 rates[IndexTraits::gasPhaseIdx] * unit::day,
493 up_press / unit::barsa));
495 node_pressures_[node] = node_pressure;
497 branch_data_.try_emplace(node,
498 node_pressure - up_press,
499 -rates[IndexTraits::oilPhaseIdx],
500 -rates[IndexTraits::waterPhaseIdx],
501 -rates[IndexTraits::gasPhaseIdx]);
505 const GenericWellModel& well_model_;
506 const Network::ExtNetwork& network_;
507 const VfpProperties& vfp_props_;
508 const UnitSystem& unit_system_;
509 const int report_step_idx_;
511 std::map<std::string, Scalar> node_pressures_;
512 std::map<std::string, data::BranchData> branch_data_;
513 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:228
const std::set< std::string > & invalidNodes() const
Definition: BlackoilWellModelNetworkPressureComputation.hpp:294
typename GenericWellModel::Scalar Scalar
Definition: BlackoilWellModelNetworkPressureComputation.hpp:245
std::pair< std::map< std::string, Scalar >, std::map< std::string, data::BranchData > > run()
Definition: BlackoilWellModelNetworkPressureComputation.hpp:247
GenericWellModel::IndexTraits IndexTraits
Definition: BlackoilWellModelNetworkPressureComputation.hpp:246
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)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:230
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 clampToTableAxes(const Table &table, std::vector< Scalar > &rates, Scalar &up_press)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:69
T getFlo(const VFPProdTable &table, const T &aqua, const T &liquid, const T &vapour)
Definition: blackoilbioeffectsmodules.hh:45
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
Result of a single network branch VFP lookup.
Definition: BlackoilWellModelNetworkPressureComputation.hpp:56
bool valid
Definition: BlackoilWellModelNetworkPressureComputation.hpp:60
Scalar pressure
Definition: BlackoilWellModelNetworkPressureComputation.hpp:57
bool clamped
Definition: BlackoilWellModelNetworkPressureComputation.hpp:62
static void prepareRates(std::vector< Scalar > &)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:184
static bool hasLeafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:189
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:204
static const std::vector< Scalar > leafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:197
static const std::vector< Scalar > leafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:139
static void prepareRates(std::vector< Scalar > &rates)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:124
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:146
static bool hasLeafNodeRate(const GroupState &group_state, const std::string &node)
Definition: BlackoilWellModelNetworkPressureComputation.hpp:131
Helper class to insulate the NetworkPressureComputation class from the differences between production...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:118