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>
43#include <fmt/format.h>
47template<
typename TypeTag>
51 , well_model_(well_model)
54template<
typename TypeTag>
60 const double dt = well_model_.simulator().timeStepSize();
63 auto& well_state = well_model_.wellState();
65 const bool changed_well_group =
66 well_model_.updateWellControlsAndNetwork(
true, dt, deferred_logger);
67 well_model_.assembleWellEqWithoutIteration(dt);
68 const bool converged =
69 well_model_.getWellConvergence(well_model_.B_avg(),
true).converged() &&
73 for (
auto& well : this->well_model_) {
74 well->solveEqAndUpdateWellState(well_model_.simulator(),
75 well_model_.groupStateHelper(),
79 well_model_.simulator().vanguard().grid().comm());
82 const std::string msg =
83 fmt::format(
"Initial (pre-step) network balance did not converge.");
88template<
typename TypeTag>
89std::tuple<bool, typename BlackoilWellModelNetwork<TypeTag>::Scalar>
91update(
const bool mandatory_network_balance,
93 const bool relax_network_tolerance)
96 const int episodeIdx = well_model_.simulator().episodeIndex();
97 const auto& network = well_model_.schedule()[episodeIdx].network();
98 if (!well_model_.wellsActive() && !network.active()) {
102 const int iterationIdx = well_model_.simulator().model().newtonMethod().numIterations();
103 const auto& comm = well_model_.simulator().vanguard().grid().comm();
106 Scalar network_imbalance = 0.0;
107 bool more_network_update =
false;
108 if (this->shouldBalance(episodeIdx, iterationIdx) || mandatory_network_balance) {
109 OPM_TIMEBLOCK(BalanceNetwork);
110 const double dt = well_model_.simulator().timeStepSize();
112 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
113 const int max_number_of_sub_iterations =
114 well_model_.param().network_max_sub_iterations_;
115 const Scalar network_pressure_update_damping_factor =
116 well_model_.param().network_pressure_update_damping_factor_;
117 const Scalar network_max_pressure_update =
118 well_model_.param().network_max_pressure_update_in_bars_ * unit::barsa;
119 bool more_network_sub_update =
false;
120 for (
int i = 0; i < max_number_of_sub_iterations; i++) {
121 const auto local_network_imbalance =
122 this->updatePressures(episodeIdx,
123 network_pressure_update_damping_factor,
124 network_max_pressure_update);
125 network_imbalance = comm.max(local_network_imbalance);
126 const auto& balance = well_model_.schedule()[episodeIdx].network_balance();
127 constexpr Scalar relaxation_factor = 10.0;
128 const Scalar tolerance =
129 relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance()
130 : balance.pressure_tolerance();
131 more_network_sub_update = this->active() && network_imbalance > tolerance;
132 if (!more_network_sub_update) {
136 for (
const auto& well : well_model_) {
137 if (well->isInjector() || !well->wellEcl().predictionMode()) {
141 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
142 if (it != this->node_pressures_.end()) {
143 well->prepareWellBeforeAssembling(well_model_.simulator(),
145 well_model_.groupStateHelper(),
146 well_model_.wellState());
149 well_model_.updateAndCommunicateGroupData(episodeIdx,
151 well_model_.param().nupcol_group_rate_tolerance_,
154 more_network_update = more_network_sub_update || well_group_thp_updated;
156 return { more_network_update, network_imbalance };
159template <
typename TypeTag>
165 const int reportStepIdx = well_model_.simulator().episodeIndex();
166 const auto& network = well_model_.schedule()[reportStepIdx].network();
167 const auto& balance = well_model_.schedule()[reportStepIdx].network_balance();
168 const Scalar thp_tolerance = balance.thp_tolerance();
170 if (!network.active()) {
174 auto& well_state = well_model_.wellState();
175 auto& group_state = well_model_.groupState();
177 bool well_group_thp_updated =
false;
178 for (
const std::string& nodeName : network.node_names()) {
179 const bool has_choke = network.node(nodeName).as_choke();
181 const auto& summary_state = well_model_.simulator().vanguard().summaryState();
182 const Group& group = well_model_.schedule().getGroup(nodeName, reportStepIdx);
185 std::vector<Scalar> resv_coeff(Indices::numPhases, 1.0);
187 const auto ctrl = group.productionControls(summary_state);
188 auto cmode_tmp = ctrl.cmode;
189 Scalar target_tmp{0.0};
190 bool fld_none =
false;
195 const Scalar efficiencyFactor = 1.0;
196 const Group& parentGroup = well_model_.schedule().getGroup(group.parent(), reportStepIdx);
200 well_model_.groupStateHelper(),
203 target_tmp = target.first;
204 cmode_tmp = target.second;
207 TargetCalculatorType tcalc{well_model_.groupStateHelper(), resv_coeff, group};
214 const Scalar orig_target = target_tmp;
216 auto mismatch = [&] (
auto group_thp) {
217 Scalar group_rate(0.0);
219 for (
auto& well : well_model_) {
220 std::string well_name = well->name();
221 auto& ws = well_state.well(well_name);
222 if (group.hasWell(well_name)) {
223 well->setDynamicThpLimit(group_thp);
224 const Well& well_ecl = well_model_.eclWells()[well->indexOfWell()];
225 const auto inj_controls = Well::InjectionControls(0);
226 const auto prod_controls = well_ecl.productionControls(summary_state);
227 well->iterateWellEqWithSwitching(well_model_.simulator(),
231 well_model_.groupStateHelper(),
236 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
240 return (group_rate - orig_target)/orig_target;
243 const auto upbranch = network.uptree_branch(nodeName);
244 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
245 const Scalar nodal_pressure = it->second;
246 Scalar well_group_thp = nodal_pressure;
248 std::optional<Scalar> autochoke_thp;
249 if (
auto iter = this->well_group_thp_calc_.find(nodeName);
250 iter != this->well_group_thp_calc_.end())
252 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
257 std::array<Scalar, 2> range_initial;
258 if (!autochoke_thp.has_value()){
259 Scalar min_thp, max_thp;
261 std::string node_name = nodeName;
262 while (!network.node(node_name).terminal_pressure().has_value()) {
263 auto branch = network.uptree_branch(node_name).value();
264 node_name = branch.uptree_node();
266 min_thp = network.node(node_name).terminal_pressure().value();
267 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
270 std::array<Scalar, 2> range = {Scalar{0.9}*min_thp, Scalar{1.1}*max_thp};
271 std::optional<Scalar> appr_sol;
272 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch,
278 local_deferredLogger);
281 range_initial = {min_thp, max_thp};
284 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
287 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
288 std::array<Scalar, 2>{Scalar{0.9} * autochoke_thp.value(),
289 Scalar{1.1} * autochoke_thp.value()} : range_initial;
291 std::optional<Scalar> approximate_solution;
292 const Scalar tolerance1 = thp_tolerance;
293 local_deferredLogger.
debug(
"Using brute force search to bracket the group THP");
294 const bool finding_bracket = WellBhpThpCalculatorType::
295 bruteForceBracketCommonTHP(mismatch,
299 approximate_solution,
301 local_deferredLogger);
303 if (approximate_solution.has_value()) {
304 autochoke_thp = *approximate_solution;
305 local_deferredLogger.
debug(
"Approximate group THP value found: " +
307 }
else if (finding_bracket) {
308 const Scalar tolerance2 = thp_tolerance;
309 const int max_iteration_solve = 100;
311 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
323 autochoke_thp.reset();
324 local_deferredLogger.
debug(
"Group THP solve failed due to bracketing failure");
327 if (autochoke_thp.has_value()) {
328 well_group_thp_calc_[nodeName] = autochoke_thp.value();
332 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
335 for (
auto& well : well_model_) {
336 std::string well_name = well->name();
338 if (well->isInjector() || !well->wellEcl().predictionMode())
341 if (group.hasWell(well_name)) {
342 well->setDynamicThpLimit(well_group_thp);
344 const auto& ws = well_model_.wellState().well(well->indexOfWell());
345 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
347 well->prepareWellBeforeAssembling(well_model_.simulator(),
349 well_model_.groupStateHelper(),
350 well_model_.wellState());
355 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName)
356 ? group_state.well_group_thp(nodeName)
358 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
359 well_group_thp_updated =
true;
360 group_state.update_well_group_thp(nodeName, well_group_thp);
364 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:192
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:158
Class for handling the blackoil well network model.
Definition: BlackoilWellModelNetworkGeneric.hpp:49
bool computeWellGroupThp(const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModelNetwork_impl.hpp:162
std::tuple< bool, Scalar > update(const bool mandatory_network_balance, DeferredLogger &deferred_logger, const bool relax_network_tolerance=false)
Definition: BlackoilWellModelNetwork_impl.hpp:91
BlackoilWellModelNetwork(BlackoilWellModel< TypeTag > &well_model)
Definition: BlackoilWellModelNetwork_impl.hpp:49
void doPreStepRebalance(DeferredLogger &deferred_logger)
Definition: BlackoilWellModelNetwork_impl.hpp:57
Class for handling the blackoil well model.
Definition: BlackoilWellModel.hpp:98
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:45
Scalar groupTarget() const
Class for computing BHP limits.
Definition: WellBhpThpCalculator.hpp:41
static std::pair< Scalar, Group::ProductionCMode > getAutoChokeGroupProductionTargetRate(const Group &bottom_group, const Group &group, const GroupStateHelperType &groupStateHelper, const std::vector< Scalar > &resv_coeff, Scalar efficiencyFactor)
@ NONE
Definition: DeferredLogger.hpp:46
Definition: blackoilbioeffectsmodules.hh:43
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)