BlackoilWellModelNetwork_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2016 - 2019 SINTEF Digital, Mathematics & Cybernetics.
3 Copyright 2016 - 2018 Equinor ASA.
4 Copyright 2017 Dr. Blatt - HPC-Simulation-Software & Services
5 Copyright 2016 - 2018 Norce AS
6
7 This file is part of the Open Porous Media project (OPM).
8
9 OPM is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OPM is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OPM. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#ifndef OPM_BLACKOILWELLMODEL_NETWORK_IMPL_HEADER_INCLUDED
24#define OPM_BLACKOILWELLMODEL_NETWORK_IMPL_HEADER_INCLUDED
25
26// Improve IDE experience
27#ifndef OPM_BLACKOILWELLMODEL_NETWORK_HEADER_INCLUDED
28#include <config.h>
30#endif
31
32#include <opm/common/TimingMacros.hpp>
33#include <opm/common/utility/numeric/RootFinders.hpp>
34
35#include <opm/input/eclipse/Units/Units.hpp>
36
42
43#include <fmt/format.h>
44
45namespace Opm {
46
47template<typename TypeTag>
50 : BaseType(well_model)
51 , well_model_(well_model)
52{}
53
54template<typename TypeTag>
55void
57doPreStepRebalance(DeferredLogger& deferred_logger)
58{
59 OPM_TIMEFUNCTION();
60 const double dt = well_model_.simulator().timeStepSize();
61 // TODO: should we also have the group and network backed-up
62 // here in case the solution did not get converged?
63 auto& well_state = well_model_.wellState();
64
65 const bool changed_well_group =
66 well_model_.updateWellControlsAndNetwork(true, dt, deferred_logger);
67 well_model_.assembleWellEqWithoutIteration(dt, deferred_logger);
68 const bool converged =
69 well_model_.getWellConvergence(well_model_.B_avg(), true).converged() &&
70 !changed_well_group;
71
73 for (auto& well : this->well_model_) {
74 well->solveEqAndUpdateWellState(well_model_.simulator(),
75 well_model_.groupStateHelper(),
76 well_state,
77 deferred_logger);
78 }
79 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModelNetwork::doPreStepRebalance() failed: ",
80 well_model_.simulator().vanguard().grid().comm());
81
82 if (!converged) {
83 const std::string msg =
84 fmt::format("Initial (pre-step) network balance did not converge.");
85 deferred_logger.warning(msg);
86 }
87}
88
89template<typename TypeTag>
90std::tuple<bool, typename BlackoilWellModelNetwork<TypeTag>::Scalar>
92update(const bool mandatory_network_balance,
93 DeferredLogger& deferred_logger,
94 const bool relax_network_tolerance)
95{
96 OPM_TIMEFUNCTION();
97 const int episodeIdx = well_model_.simulator().episodeIndex();
98 const auto& network = well_model_.schedule()[episodeIdx].network();
99 if (!well_model_.wellsActive() && !network.active()) {
100 return {false, 0.0};
101 }
102
103 const int iterationIdx = well_model_.simulator().model().newtonMethod().numIterations();
104 const auto& comm = well_model_.simulator().vanguard().grid().comm();
105
106 // network related
107 Scalar network_imbalance = 0.0;
108 bool more_network_update = false;
109 if (this->shouldBalance(episodeIdx, iterationIdx) || mandatory_network_balance) {
110 OPM_TIMEBLOCK(BalanceNetwork);
111 const double dt = well_model_.simulator().timeStepSize();
112 // Calculate common THP for subsea manifold well group (item 3 of NODEPROP set to YES)
113 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
114 const int max_number_of_sub_iterations =
115 well_model_.param().network_max_sub_iterations_;
116 const Scalar network_pressure_update_damping_factor =
117 well_model_.param().network_pressure_update_damping_factor_;
118 const Scalar network_max_pressure_update =
119 well_model_.param().network_max_pressure_update_in_bars_ * unit::barsa;
120 bool more_network_sub_update = false;
121 for (int i = 0; i < max_number_of_sub_iterations; i++) {
122 const auto local_network_imbalance =
123 this->updatePressures(episodeIdx,
124 network_pressure_update_damping_factor,
125 network_max_pressure_update);
126 network_imbalance = comm.max(local_network_imbalance);
127 const auto& balance = well_model_.schedule()[episodeIdx].network_balance();
128 constexpr Scalar relaxation_factor = 10.0;
129 const Scalar tolerance =
130 relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance()
131 : balance.pressure_tolerance();
132 more_network_sub_update = this->active() && network_imbalance > tolerance;
133 if (!more_network_sub_update) {
134 break;
135 }
136
137 for (const auto& well : well_model_) {
138 if (well->isInjector() || !well->wellEcl().predictionMode()) {
139 continue;
140 }
141
142 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
143 if (it != this->node_pressures_.end()) {
144 well->prepareWellBeforeAssembling(well_model_.simulator(),
145 dt,
146 well_model_.groupStateHelper(),
147 well_model_.wellState(),
148 deferred_logger);
149 }
150 }
151 well_model_.updateAndCommunicateGroupData(episodeIdx,
152 iterationIdx,
153 well_model_.param().nupcol_group_rate_tolerance_,
154 /*update_wellgrouptarget*/ true,
155 deferred_logger);
156 }
157 more_network_update = more_network_sub_update || well_group_thp_updated;
158 }
159 return { more_network_update, network_imbalance };
160}
161
162template <typename TypeTag>
163bool
165computeWellGroupThp(const double dt, DeferredLogger& local_deferredLogger)
166{
167 OPM_TIMEFUNCTION();
168 const int reportStepIdx = well_model_.simulator().episodeIndex();
169 const auto& network = well_model_.schedule()[reportStepIdx].network();
170 const auto& balance = well_model_.schedule()[reportStepIdx].network_balance();
171 const Scalar thp_tolerance = balance.thp_tolerance();
172
173 if (!network.active()) {
174 return false;
175 }
176
177 auto& well_state = well_model_.wellState();
178 auto& group_state = well_model_.groupState();
179
180 bool well_group_thp_updated = false;
181 for (const std::string& nodeName : network.node_names()) {
182 const bool has_choke = network.node(nodeName).as_choke();
183 if (has_choke) {
184 const auto& summary_state = well_model_.simulator().vanguard().summaryState();
185 const Group& group = well_model_.schedule().getGroup(nodeName, reportStepIdx);
186
187 //TODO: Auto choke combined with RESV control is not supported
188 std::vector<Scalar> resv_coeff(Indices::numPhases, 1.0);
189
190 const auto ctrl = group.productionControls(summary_state);
191 auto cmode_tmp = ctrl.cmode;
192 Scalar target_tmp{0.0};
193 bool fld_none = false;
194 if (cmode_tmp == Group::ProductionCMode::FLD || cmode_tmp == Group::ProductionCMode::NONE) {
195 fld_none = true;
196 // Target is set for an ancestor group. Target for autochoke group to be
197 // derived via group guide rates
198 const Scalar efficiencyFactor = 1.0;
199 const Group& parentGroup = well_model_.schedule().getGroup(group.parent(), reportStepIdx);
202 parentGroup,
203 well_model_.groupStateHelper(),
204 well_model_.schedule(),
205 summary_state,
206 resv_coeff,
207 efficiencyFactor,
208 reportStepIdx,
209 &well_model_.guideRate(),
210 local_deferredLogger);
211 target_tmp = target.first;
212 cmode_tmp = target.second;
213 }
215 TargetCalculatorType tcalc{well_model_.groupStateHelper(), resv_coeff, group};
216 if (!fld_none)
217 {
218 // Target is set for the autochoke group itself
219 target_tmp = tcalc.groupTarget(local_deferredLogger);
220 }
221
222 const Scalar orig_target = target_tmp;
223
224 auto mismatch = [&] (auto group_thp) {
225 Scalar group_rate(0.0);
226 Scalar rate(0.0);
227 for (auto& well : well_model_) {
228 std::string well_name = well->name();
229 auto& ws = well_state.well(well_name);
230 if (group.hasWell(well_name)) {
231 well->setDynamicThpLimit(group_thp);
232 const Well& well_ecl = well_model_.eclWells()[well->indexOfWell()];
233 const auto inj_controls = Well::InjectionControls(0);
234 const auto prod_controls = well_ecl.productionControls(summary_state);
235 well->iterateWellEqWithSwitching(well_model_.simulator(),
236 dt,
237 inj_controls,
238 prod_controls,
239 well_model_.groupStateHelper(),
240 well_state,
241 local_deferredLogger,
242 /*fixed_control=*/false,
243 /*fixed_status=*/false,
244 /*solving_with_zero_rate=*/false);
245 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
246 group_rate += rate;
247 }
248 }
249 return (group_rate - orig_target)/orig_target;
250 };
251
252 const auto upbranch = network.uptree_branch(nodeName);
253 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
254 const Scalar nodal_pressure = it->second;
255 Scalar well_group_thp = nodal_pressure;
256
257 std::optional<Scalar> autochoke_thp;
258 if (auto iter = this->well_group_thp_calc_.find(nodeName);
259 iter != this->well_group_thp_calc_.end())
260 {
261 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
262 }
263
264 using WellBhpThpCalculatorType = WellBhpThpCalculator<Scalar, IndexTraits>;
265 //Find an initial bracket
266 std::array<Scalar, 2> range_initial;
267 if (!autochoke_thp.has_value()){
268 Scalar min_thp, max_thp;
269 // Retrieve the terminal pressure of the associated root of the manifold group
270 std::string node_name = nodeName;
271 while (!network.node(node_name).terminal_pressure().has_value()) {
272 auto branch = network.uptree_branch(node_name).value();
273 node_name = branch.uptree_node();
274 }
275 min_thp = network.node(node_name).terminal_pressure().value();
276 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
277 // Narrow down the bracket
278 Scalar low1, high1;
279 std::array<Scalar, 2> range = {Scalar{0.9}*min_thp, Scalar{1.1}*max_thp};
280 std::optional<Scalar> appr_sol;
281 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch,
282 range,
283 low1,
284 high1,
285 appr_sol,
286 0.0,
287 local_deferredLogger);
288 min_thp = low1;
289 max_thp = high1;
290 range_initial = {min_thp, max_thp};
291 }
292
293 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
294 // The bracket is based on the initial bracket or
295 // on a range based on a previous calculated group thp
296 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
297 std::array<Scalar, 2>{Scalar{0.9} * autochoke_thp.value(),
298 Scalar{1.1} * autochoke_thp.value()} : range_initial;
299 Scalar low, high;
300 std::optional<Scalar> approximate_solution;
301 const Scalar tolerance1 = thp_tolerance;
302 local_deferredLogger.debug("Using brute force search to bracket the group THP");
303 const bool finding_bracket = WellBhpThpCalculatorType::
304 bruteForceBracketCommonTHP(mismatch,
305 range,
306 low,
307 high,
308 approximate_solution,
309 tolerance1,
310 local_deferredLogger);
311
312 if (approximate_solution.has_value()) {
313 autochoke_thp = *approximate_solution;
314 local_deferredLogger.debug("Approximate group THP value found: " +
315 std::to_string(autochoke_thp.value()));
316 } else if (finding_bracket) {
317 const Scalar tolerance2 = thp_tolerance;
318 const int max_iteration_solve = 100;
319 int iteration = 0;
320 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
321 solve(mismatch,
322 low,
323 high,
324 max_iteration_solve,
325 tolerance2,
326 iteration);
327 local_deferredLogger.debug(" bracket = [" + std::to_string(low) + ", " +
328 std::to_string(high) + "], " +
329 "iteration = " + std::to_string(iteration));
330 local_deferredLogger.debug("Group THP value = " + std::to_string(autochoke_thp.value()));
331 } else {
332 autochoke_thp.reset();
333 local_deferredLogger.debug("Group THP solve failed due to bracketing failure");
334 }
335 }
336 if (autochoke_thp.has_value()) {
337 well_group_thp_calc_[nodeName] = autochoke_thp.value();
338 // Note: The node pressure of the auto-choke node is set
339 // to well_group_thp in computeNetworkPressures()
340 // and must be larger or equal to the pressure of the uptree node of its branch.
341 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
342 }
343
344 for (auto& well : well_model_) {
345 std::string well_name = well->name();
346
347 if (well->isInjector() || !well->wellEcl().predictionMode())
348 continue;
349
350 if (group.hasWell(well_name)) {
351 well->setDynamicThpLimit(well_group_thp);
352 }
353 const auto& ws = well_model_.wellState().well(well->indexOfWell());
354 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
355 if (thp_is_limit) {
356 well->prepareWellBeforeAssembling(well_model_.simulator(),
357 dt,
358 well_model_.groupStateHelper(),
359 well_model_.wellState(),
360 local_deferredLogger);
361 }
362 }
363
364 // Use the group THP in computeNetworkPressures().
365 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName)
366 ? group_state.well_group_thp(nodeName)
367 : 1e30;
368 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
369 well_group_thp_updated = true;
370 group_state.update_well_group_thp(nodeName, well_group_thp);
371 }
372 }
373 }
374 return well_group_thp_updated;
375}
376
377} // namespace Opm
378
379#endif // OPM_BLACKOILWELLMODEL_NETWORK_IMPL_HEADER_INCLUDED
#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:165
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: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(DeferredLogger &deferred_logger) 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 Schedule &schedule, const SummaryState &summaryState, const std::vector< Scalar > &resv_coeff, Scalar efficiencyFactor, const int reportStepIdx, const GuideRate *guideRate, DeferredLogger &deferred_logger)
@ NONE
Definition: DeferredLogger.hpp:46
Definition: blackoilbioeffectsmodules.hh:43
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)