23#ifndef OPM_BLACKOILWELLMODEL_NETWORK_IMPL_HEADER_INCLUDED
24#define OPM_BLACKOILWELLMODEL_NETWORK_IMPL_HEADER_INCLUDED
27#ifndef OPM_BLACKOILWELLMODEL_NETWORK_HEADER_INCLUDED
32#include <opm/common/TimingMacros.hpp>
33#include <opm/common/utility/numeric/RootFinders.hpp>
35#include <opm/input/eclipse/Units/Units.hpp>
42#include <fmt/format.h>
48template<
typename TypeTag>
52 , well_model_(well_model)
55template<
typename TypeTag>
61 const double dt = well_model_.simulator().timeStepSize();
64 auto& well_state = well_model_.wellState();
66 const bool changed_well_group =
67 well_model_.updateWellControlsAndNetwork(
true,
70 well_model_.assembleWellEqWithoutIteration(dt);
71 const bool converged =
72 well_model_.getWellConvergence(well_model_.B_avg(),
true).converged() &&
76 for (
auto& well : this->well_model_) {
77 well->solveEqAndUpdateWellState(well_model_.simulator(),
78 well_model_.groupStateHelper(),
82 well_model_.simulator().vanguard().grid().comm());
85 deferred_logger.
warning(
"Initial (pre-step) network balance did not converge.");
89template<
typename TypeTag>
90std::tuple<bool, typename BlackoilWellModelNetwork<TypeTag>::Scalar>
92update(
const bool mandatory_network_balance,
94 const bool relax_network_tolerance)
97 const int episodeIdx = well_model_.simulator().episodeIndex();
102 const auto& comm = well_model_.simulator().vanguard().grid().comm();
105 Scalar network_imbalance = 0.0;
106 bool more_network_update =
false;
107 if (this->shouldBalance(episodeIdx) || mandatory_network_balance) {
108 OPM_TIMEBLOCK(BalanceNetwork);
109 const double dt = well_model_.simulator().timeStepSize();
111 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
112 const int max_number_of_sub_iterations =
113 well_model_.param().network_max_sub_iterations_;
114 const Scalar network_pressure_update_damping_factor =
115 well_model_.param().network_pressure_update_damping_factor_;
116 const Scalar network_max_pressure_update =
117 well_model_.param().network_max_pressure_update_in_bars_ * unit::barsa;
118 bool more_network_sub_update =
false;
119 for (
int i = 0; i < max_number_of_sub_iterations; i++) {
120 const auto local_network_imbalance =
121 this->updatePressures(episodeIdx,
122 network_pressure_update_damping_factor,
123 network_max_pressure_update);
124 network_imbalance = comm.max(local_network_imbalance);
125 const auto& balance = well_model_.schedule()[episodeIdx].network_balance();
126 constexpr Scalar relaxation_factor = 10.0;
127 const Scalar tolerance =
128 relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance()
129 : balance.pressure_tolerance();
130 more_network_sub_update = this->active() && network_imbalance > tolerance;
131#ifdef RESERVOIR_COUPLING_ENABLED
132 if (well_model_.isReservoirCouplingMaster()) {
136 well_model_.rescoupHelper().maybeExchangeNetworkSubIterationWithSlaves();
139 if (!more_network_sub_update) {
143 for (
const auto& well : well_model_) {
144 if (!well->wellEcl().predictionMode()) {
150 if (!domain.has_value()) {
154 const auto it = this->nodePressures(*domain).find(well->wellEcl().groupName());
155 if (it != this->nodePressures(*domain).end()) {
156 well->prepareWellBeforeAssembling(well_model_.simulator(),
158 well_model_.groupStateHelper(),
159 well_model_.wellState());
169 if (well->isInjector()) {
170 auto& ws = well_model_.wellState().well(well->indexOfWell());
171 if (ws.injection_cmode == Well::InjectorCMode::THP) {
172 const int np = well_model_.numPhases();
173 for (
int p = 0; p < np; ++p) {
174 ws.well_potentials[p] =
175 std::max(Scalar{0.0}, ws.surface_rates[p]);
181 well_model_.updateAndCommunicateGroupData(episodeIdx,
true);
183 more_network_update = more_network_sub_update || well_group_thp_updated;
185 return { more_network_update, network_imbalance };
188template <
typename TypeTag>
194 const int reportStepIdx = well_model_.simulator().episodeIndex();
199 const auto& network = well_model_.schedule()[reportStepIdx].network();
200 const auto& balance = well_model_.schedule()[reportStepIdx].network_balance();
201 const Scalar thp_tolerance = balance.thp_tolerance();
203 if (!network.active()) {
207 auto& well_state = well_model_.wellState();
208 auto& group_state = well_model_.groupState();
210 bool well_group_thp_updated =
false;
211 for (
const std::string& nodeName : network.node_names()) {
212 const bool has_choke = network.node(nodeName).as_choke();
214 const auto& summary_state = well_model_.simulator().vanguard().summaryState();
215 const Group& group = well_model_.schedule().getGroup(nodeName, reportStepIdx);
218 std::vector<Scalar> resv_coeff(Indices::numPhases, 1.0);
220 const auto ctrl = group.productionControls(summary_state);
221 auto cmode_tmp = ctrl.cmode;
222 Scalar target_tmp{0.0};
223 bool fld_none =
false;
228 const Scalar efficiencyFactor = 1.0;
229 const Group& parentGroup = well_model_.schedule().getGroup(group.parent(), reportStepIdx);
230 auto target = well_model_.groupStateHelper().
231 getAutoChokeGroupProductionTargetRate(group,
235 target_tmp = target.first;
236 cmode_tmp = target.second;
239 TargetCalculatorType tcalc{well_model_.groupStateHelper(), resv_coeff, group};
243 target_tmp = well_model_.groupStateHelper().getProductionGroupTarget(group);
246 const Scalar orig_target = target_tmp;
248 auto mismatch = [&] (
auto group_thp) {
249 Scalar group_rate(0.0);
251 for (
auto& well : well_model_) {
252 std::string well_name = well->name();
253 auto& ws = well_state.well(well_name);
254 if (group.hasWell(well_name)) {
257 if (well->wellEcl().vfp_table_number() > 0) {
258 well->setDynamicThpLimit(group_thp);
260 const auto inj_controls = Well::InjectionControls(0);
265 const auto prod_controls =
266 well->productionControlsWithWeldraw(summary_state, ws);
267 well->iterateWellEqWithSwitching(well_model_.simulator(),
271 well_model_.groupStateHelper(),
276 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
280 return (group_rate - orig_target)/orig_target;
283 const auto upbranch = network.uptree_branch(nodeName);
284 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
285 const Scalar nodal_pressure = it->second;
286 Scalar well_group_thp = nodal_pressure;
288 std::optional<Scalar> autochoke_thp;
289 if (
auto iter = this->well_group_thp_calc_.find(nodeName);
290 iter != this->well_group_thp_calc_.end())
292 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
297 std::array<Scalar, 2> range_initial;
298 if (!autochoke_thp.has_value()){
299 Scalar min_thp, max_thp;
301 std::string node_name = nodeName;
302 while (!network.node(node_name).terminal_pressure().has_value()) {
303 auto branch = network.uptree_branch(node_name).value();
304 node_name = branch.uptree_node();
306 min_thp = network.node(node_name).terminal_pressure().value();
307 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
310 std::array<Scalar, 2> range = {Scalar{0.9}*min_thp, Scalar{1.1}*max_thp};
311 std::optional<Scalar> appr_sol;
312 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch,
318 local_deferredLogger);
321 range_initial = {min_thp, max_thp};
324 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
327 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
328 std::array<Scalar, 2>{Scalar{0.9} * autochoke_thp.value(),
329 Scalar{1.1} * autochoke_thp.value()} : range_initial;
331 std::optional<Scalar> approximate_solution;
332 const Scalar tolerance1 = thp_tolerance;
333 local_deferredLogger.
debug(
"Using brute force search to bracket the group THP");
334 const bool finding_bracket = WellBhpThpCalculatorType::
335 bruteForceBracketCommonTHP(mismatch,
339 approximate_solution,
341 local_deferredLogger);
343 if (approximate_solution.has_value()) {
344 autochoke_thp = *approximate_solution;
345 local_deferredLogger.
debug(
"Approximate group THP value found: " +
347 }
else if (finding_bracket) {
348 const Scalar tolerance2 = thp_tolerance;
349 const int max_iteration_solve = 100;
351 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
363 autochoke_thp.reset();
364 local_deferredLogger.
debug(
"Group THP solve failed due to bracketing failure");
367 if (autochoke_thp.has_value()) {
368 this->well_group_thp_calc_[nodeName] = autochoke_thp.value();
372 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
375 for (
auto& well : well_model_) {
376 std::string well_name = well->name();
378 if (well->isInjector() || !well->wellEcl().predictionMode())
383 if (group.hasWell(well_name)
384 && well->wellEcl().vfp_table_number() > 0) {
385 well->setDynamicThpLimit(well_group_thp);
387 const auto& ws = well_model_.wellState().well(well->indexOfWell());
388 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
390 well->prepareWellBeforeAssembling(well_model_.simulator(),
392 well_model_.groupStateHelper(),
393 well_model_.wellState());
398 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName)
399 ? group_state.well_group_thp(nodeName)
401 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
402 well_group_thp_updated =
true;
403 group_state.update_well_group_thp(nodeName, well_group_thp);
407 return well_group_thp_updated;
#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
Class for handling the blackoil well network model.
Definition: BlackoilWellModelNetworkGeneric.hpp:107
bool computeWellGroupThp(const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModelNetwork_impl.hpp:191
std::tuple< bool, Scalar > update(const bool mandatory_network_balance, DeferredLogger &deferred_logger, const bool relax_network_tolerance=false)
Definition: BlackoilWellModelNetwork_impl.hpp:92
BlackoilWellModelNetwork(BlackoilWellModel< TypeTag > &well_model)
Definition: BlackoilWellModelNetwork_impl.hpp:50
void doPreStepRebalance(DeferredLogger &deferred_logger)
Definition: BlackoilWellModelNetwork_impl.hpp:58
Class for handling the blackoil well model.
Definition: BlackoilWellModel.hpp:101
Definition: DeferredLogger.hpp:57
void warning(const std::string &tag, const std::string &message)
void debug(const std::string &tag, const std::string &message)
Definition: TargetCalculator.hpp:39
Class for computing BHP limits.
Definition: WellBhpThpCalculator.hpp:41
@ NONE
Definition: DeferredLogger.hpp:46
std::optional< NetworkDomain > domainForWell(const Well &well)
Definition: BlackoilWellModelNetworkGeneric.hpp:73
bool anyNetworkActive(const Schedule &schedule, const int timeStepIdx)
Helper to check if any network (production, gas injection, water injection) is active at a given time...
Definition: blackoilbioeffectsmodules.hh:45
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)