My Project
GasLiftSingleWellGeneric.hpp
1/*
2 Copyright 2020 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_GASLIFT_SINGLE_WELL_GENERIC_HEADER_INCLUDED
21#define OPM_GASLIFT_SINGLE_WELL_GENERIC_HEADER_INCLUDED
22
23#include <dune/common/version.hh>
24#include <dune/common/parallel/mpihelper.hh>
25
26#include <opm/core/props/BlackoilPhases.hpp>
27
28#include <opm/input/eclipse/Schedule/Well/WellProductionControls.hpp>
29#include <opm/simulators/wells/GasLiftGroupInfo.hpp>
30#include <opm/simulators/wells/GasLiftCommon.hpp>
31#include <opm/simulators/wells/GroupState.hpp>
32
33#include <functional>
34#include <optional>
35#include <string>
36#include <tuple>
37#include <vector>
38#include <utility>
39
40namespace Opm
41{
42
43class DeferredLogger;
44class GasLiftWell;
45class GasLiftWellState;
46class Schedule;
47class SummaryState;
48class WellInterfaceGeneric;
49class WellState;
50class GroupState;
51
53{
54protected:
55 static constexpr int Water = BlackoilPhases::Aqua;
56 static constexpr int Oil = BlackoilPhases::Liquid;
57 static constexpr int Gas = BlackoilPhases::Vapour;
58 static constexpr int NUM_PHASES = 3;
59 static constexpr double ALQ_EPSILON = 1e-8;
60
61public:
62 using GLiftSyncGroups = std::set<int>;
63 using Rate = GasLiftGroupInfo::Rate;
64 struct GradInfo
65 {
66 GradInfo() { }
67
68 GradInfo(double grad_, double new_oil_rate_, bool oil_is_limited_,
69 double new_gas_rate_, bool gas_is_limited_,
70 double new_water_rate_, bool water_is_limited_,
71 double alq_, bool alq_is_limited_) :
72 grad{grad_},
73 new_oil_rate{new_oil_rate_},
74 oil_is_limited{oil_is_limited_},
75 new_gas_rate{new_gas_rate_},
76 gas_is_limited{gas_is_limited_},
77 new_water_rate{new_water_rate_},
78 water_is_limited{water_is_limited_},
79 alq{alq_},
80 alq_is_limited{alq_is_limited_} {}
81 double grad;
82 double new_oil_rate;
83 bool oil_is_limited;
84 double new_gas_rate;
85 bool gas_is_limited;
86 double new_water_rate;
87 bool water_is_limited;
88 double alq;
89 bool alq_is_limited;
90 };
91
92 virtual ~GasLiftSingleWellGeneric() = default;
93
94 const std::string& name() const { return well_name_; }
95
96 std::optional<GradInfo> calcIncOrDecGradient(double oil_rate, double gas_rate,
97 double water_rate,
98 double alq,
99 const std::string& gr_name_dont_limit,
100 bool increase,
101 bool debug_output = true
102 ) const;
103
104 std::unique_ptr<GasLiftWellState> runOptimize(const int iteration_idx);
105
106 virtual const WellInterfaceGeneric& getWell() const = 0;
107
108protected:
110 DeferredLogger& deferred_logger,
111 WellState& well_state,
112 const GroupState& group_state,
113 const Well& ecl_well,
114 const SummaryState& summary_state,
115 GasLiftGroupInfo& group_info,
116 const PhaseUsage& phase_usage,
117 const Schedule& schedule,
118 const int report_step_idx,
119 GLiftSyncGroups& sync_groups,
120 const Parallel::Communication& comm,
121 bool glift_debug
122 );
123
124 struct LimitedRates;
126 {
127 BasicRates(const BasicRates& rates) :
128 oil{rates.oil},
129 gas{rates.gas},
130 water{rates.water},
131 bhp_is_limited{rates.bhp_is_limited}
132 {}
133 BasicRates(double oil_, double gas_, double water_, bool bhp_is_limited_) :
134 oil{oil_},
135 gas{gas_},
136 water{water_},
137 bhp_is_limited{bhp_is_limited_}
138 {}
139 BasicRates& operator=(const BasicRates& rates) {
140 oil = rates.oil;
141 gas = rates.gas;
142 water = rates.water;
143 bhp_is_limited = rates.bhp_is_limited;
144 return *this;
145 }
146 // This copy constructor cannot be defined inline here since LimitedRates
147 // has not been defined yet (it is defined below). Instead it is defined in
148 // in the .cpp file
149 BasicRates(const LimitedRates& rates);
150 double operator[](Rate rate_type) const {
151 switch (rate_type) {
152 case Rate::oil:
153 return this->oil;
154 case Rate::gas:
155 return this->gas;
156 case Rate::water:
157 return this->water;
158 case Rate::liquid:
159 return this->oil + this->water;
160 default:
161 throw std::runtime_error("This should not happen");
162 }
163 }
164
165 double oil, gas, water;
166 bool bhp_is_limited;
167 };
168
169 struct LimitedRates : public BasicRates
170 {
171 enum class LimitType {well, group, none};
173 double oil_, double gas_, double water_,
174 bool oil_is_limited_, bool gas_is_limited_,
175 bool water_is_limited_, bool bhp_is_limited_,
176 std::optional<Rate> oil_limiting_target_,
177 std::optional<Rate> water_limiting_target_
178 ) :
179 BasicRates(oil_, gas_, water_, bhp_is_limited_),
180 oil_is_limited{oil_is_limited_},
181 gas_is_limited{gas_is_limited_},
182 water_is_limited{water_is_limited_},
183 oil_limiting_target{oil_limiting_target_},
184 water_limiting_target{water_limiting_target_}
185 {
186 set_initial_limit_type_();
187 }
188
190 const BasicRates& rates,
191 bool oil_is_limited_, bool gas_is_limited_,
192 bool water_is_limited_
193 ) :
194 BasicRates(rates),
195 oil_is_limited{oil_is_limited_},
196 gas_is_limited{gas_is_limited_},
197 water_is_limited{water_is_limited_}
198 {
199 set_initial_limit_type_();
200 }
201
202 bool limited() const {
203 return oil_is_limited || gas_is_limited || water_is_limited;
204 }
205 // For a given ALQ value, were the rates limited due to group targets
206 // or due to well targets?
207 LimitType limit_type;
208 bool oil_is_limited;
209 bool gas_is_limited;
210 bool water_is_limited;
211 std::optional<Rate> oil_limiting_target;
212 std::optional<Rate> water_limiting_target;
213 private:
214 void set_initial_limit_type_() {
215 limit_type = limited() ? LimitType::well : LimitType::none;
216 }
217 };
218
220 {
221 OptimizeState( GasLiftSingleWellGeneric& parent_, bool increase_ ) :
222 parent{parent_},
223 increase{increase_},
224 it{0},
225 stop_iteration{false},
226 bhp{-1}
227 {}
228
230 bool increase;
231 int it;
232 bool stop_iteration;
233 double bhp;
234
235 std::pair<std::optional<double>,bool> addOrSubtractAlqIncrement(double alq);
236 double calcEcoGradient(double oil_rate, double new_oil_rate,
237 double gas_rate, double new_gas_rate);
238 bool checkAlqOutsideLimits(double alq, double oil_rate);
239 bool checkEcoGradient(double gradient);
240 bool checkOilRateExceedsTarget(double oil_rate);
241 bool checkRatesViolated(const LimitedRates& rates) const;
242 void debugShowIterationInfo(double alq);
243 double getBhpWithLimit();
244 void warn_(std::string msg) {parent.displayWarning_(msg);}
245 };
246 bool checkGroupALQrateExceeded(double delta_alq, const std::string& gr_name_dont_limit = "") const;
247 bool checkGroupTotalRateExceeded(double delta_alq, double delta_gas_rate) const;
248
249 std::pair<std::optional<double>, bool> addOrSubtractAlqIncrement_(
250 double alq, bool increase) const;
251 double calcEcoGradient_(double oil_rate, double new_oil_rate,
252 double gas_rate, double new_gas_rate, bool increase) const;
253 bool checkALQequal_(double alq1, double alq2) const;
254 bool checkGroupTargetsViolated(
255 const BasicRates& rates, const BasicRates& new_rates) const;
256 bool checkInitialALQmodified_(double alq, double initial_alq) const;
257 virtual bool checkThpControl_() const = 0;
258 virtual std::optional<double> computeBhpAtThpLimit_(double alq, bool debug_output = true) const = 0;
259 std::pair<std::optional<double>,double> computeConvergedBhpAtThpLimitByMaybeIncreasingALQ_() const;
260 std::pair<std::optional<BasicRates>,double> computeInitialWellRates_() const;
261 std::optional<LimitedRates> computeLimitedWellRatesWithALQ_(double alq) const;
262 virtual BasicRates computeWellRates_(double bhp, bool bhp_is_limited, bool debug_output = true) const = 0;
263 std::optional<BasicRates> computeWellRatesWithALQ_(double alq) const;
264 void debugCheckNegativeGradient_(double grad, double alq, double new_alq,
265 double oil_rate, double new_oil_rate,
266 double gas_rate, double new_gas_rate,
267 bool increase) const;
268 void debugPrintWellStateRates() const;
269 void debugShowAlqIncreaseDecreaseCounts_();
270 void debugShowBhpAlqTable_();
271 void debugShowLimitingTargets_(const LimitedRates& rates) const;
272 void debugShowProducerControlMode() const;
273 void debugShowStartIteration_(double alq, bool increase, double oil_rate);
274 void debugShowTargets_();
275 void displayDebugMessage_(const std::string& msg) const override;
276 void displayWarning_(const std::string& warning);
277 std::pair<double, bool> getBhpWithLimit_(double bhp) const;
278 std::pair<double, bool> getGasRateWithLimit_(
279 const BasicRates& rates) const;
280 std::pair<double, bool> getGasRateWithGroupLimit_(
281 double new_gas_rate, double gas_rate, const std::string& gr_name_dont_limit) const;
282 std::pair<std::optional<LimitedRates>,double> getInitialRatesWithLimit_() const;
283 LimitedRates getLimitedRatesFromRates_(const BasicRates& rates) const;
284 std::tuple<double,double,bool,bool> getLiquidRateWithGroupLimit_(
285 const double new_oil_rate, const double oil_rate,
286 const double new_water_rate, const double water_rate, const std::string& gr_name_dont_limit) const;
287 std::pair<double, bool> getOilRateWithGroupLimit_(
288 double new_oil_rate, double oil_rate, const std::string& gr_name_dont_limit) const;
289 std::pair<double, bool> getOilRateWithLimit_(const BasicRates& rates) const;
290 std::pair<double, std::optional<Rate>> getOilRateWithLimit2_(
291 const BasicRates& rates) const;
292 double getProductionTarget_(Rate rate) const;
293 double getRate_(Rate rate_type, const BasicRates& rates) const;
294 std::pair<double, std::optional<Rate>> getRateWithLimit_(
295 Rate rate_type, const BasicRates& rates) const;
296 std::tuple<double, const std::string*, double> getRateWithGroupLimit_(
297 Rate rate_type, const double new_rate, const double old_rate, const std::string& gr_name_dont_limit) const;
298 std::pair<double, bool> getWaterRateWithGroupLimit_(
299 double new_water_rate, double water_rate, const std::string& gr_name_dont_limit) const;
300 std::pair<double, bool> getWaterRateWithLimit_(const BasicRates& rates) const;
301 std::pair<double, std::optional<Rate>> getWaterRateWithLimit2_(
302 const BasicRates& rates) const;
303 BasicRates getWellStateRates_() const;
304 bool hasProductionControl_(Rate rate) const;
305 std::pair<LimitedRates, double> increaseALQtoPositiveOilRate_(
306 double alq, const LimitedRates& orig_rates) const;
307 std::pair<LimitedRates, double> increaseALQtoMinALQ_(
308 double alq, const LimitedRates& orig_rates) const;
309 void logSuccess_(double alq,
310 const int iteration_idx);
311 std::pair<LimitedRates, double> maybeAdjustALQbeforeOptimizeLoop_(
312 const LimitedRates& rates, double alq, bool increase) const;
313 std::pair<LimitedRates, double> reduceALQtoGroupAlqLimits_(
314 double alq, const LimitedRates& rates) const;
315 std::pair<LimitedRates, double> reduceALQtoGroupTarget(
316 double alq, const LimitedRates& rates) const;
317 std::pair<LimitedRates, double> reduceALQtoWellTarget_(
318 double alq, const LimitedRates& rates) const;
319 std::unique_ptr<GasLiftWellState> runOptimize1_();
320 std::unique_ptr<GasLiftWellState> runOptimize2_();
321 std::unique_ptr<GasLiftWellState> runOptimizeLoop_(bool increase);
322 void setAlqMinRate_(const GasLiftWell& well);
323 std::unique_ptr<GasLiftWellState> tryIncreaseLiftGas_();
324 std::unique_ptr<GasLiftWellState> tryDecreaseLiftGas_();
325 void updateGroupRates_(
326 const LimitedRates& rates,
327 const LimitedRates& new_rates,
328 double delta_alq) const;
329 LimitedRates updateRatesToGroupLimits_(
330 const BasicRates& rates, const LimitedRates& new_rates, const std::string& gr_name = "") const;
331 void updateWellStateAlqFixedValue_(const GasLiftWell& well);
332 bool useFixedAlq_(const GasLiftWell& well);
333 void debugInfoGroupRatesExceedTarget(
334 Rate rate_type, const std::string& gr_name, double rate, double target) const;
335 void warnMaxIterationsExceeded_();
336
337 const Well& ecl_well_;
338 const SummaryState& summary_state_;
339 GasLiftGroupInfo& group_info_;
340 const PhaseUsage& phase_usage_;
341 GLiftSyncGroups& sync_groups_;
342 const WellProductionControls controls_;
343
344 double increment_;
345 double max_alq_;
346 double min_alq_;
347 double orig_alq_;
348
349 double alpha_w_;
350 double alpha_g_;
351 double eco_grad_;
352
353 int gas_pos_;
354 int oil_pos_;
355 int water_pos_;
356
357 int max_iterations_;
358
359 std::string well_name_;
360
361 const GasLiftWell* gl_well_;
362
363 bool optimize_;
364 bool debug_limit_increase_decrease_;
365 bool debug_abort_if_decrease_and_oil_is_limited_ = false;
366 bool debug_abort_if_increase_and_gas_is_limited_ = false;
367};
368
369} // namespace Opm
370
371#endif // OPM_GASLIFT_SINGLE_WELL_GENERIC_HEADER_INCLUDED
Definition: DeferredLogger.hpp:57
Definition: GasLiftCommon.hpp:32
Definition: GasLiftGroupInfo.hpp:50
Definition: GasLiftSingleWellGeneric.hpp:53
Definition: GroupState.hpp:34
Definition: WellInterfaceGeneric.hpp:51
The state of a set of wells, tailored for use by the fully implicit blackoil simulator.
Definition: WellState.hpp:60
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
Definition: GasLiftSingleWellGeneric.hpp:126
Definition: GasLiftSingleWellGeneric.hpp:65
Definition: GasLiftSingleWellGeneric.hpp:170
Definition: GasLiftSingleWellGeneric.hpp:220
Definition: BlackoilPhases.hpp:46