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