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/parser/eclipse/EclipseState/Schedule/GasLiftOpt.hpp>
29 #include <opm/parser/eclipse/EclipseState/Schedule/Well/Well.hpp>
30 #include <opm/simulators/wells/GasLiftGroupInfo.hpp>
31 
32 #include <functional>
33 #include <optional>
34 #include <string>
35 #include <tuple>
36 #include <vector>
37 #include <utility>
38 
39 namespace Opm
40 {
41 
42 class DeferredLogger;
43 class GasLiftWellState;
44 class Schedule;
45 class SummaryState;
46 class WellInterfaceGeneric;
47 class WellState;
48 class GroupState;
49 
51 {
52 protected:
53  static const int Water = BlackoilPhases::Aqua;
54  static const int Oil = BlackoilPhases::Liquid;
55  static const int Gas = BlackoilPhases::Vapour;
56  static constexpr double ALQ_EPSILON = 1e-8;
57 
58 public:
59  using GLiftSyncGroups = std::set<int>;
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 alq_, bool alq_is_limited_) :
67  grad{grad_},
68  new_oil_rate{new_oil_rate_},
69  oil_is_limited{oil_is_limited_},
70  new_gas_rate{new_gas_rate_},
71  gas_is_limited{gas_is_limited_},
72  alq{alq_},
73  alq_is_limited{alq_is_limited_} {}
74  double grad;
75  double new_oil_rate;
76  bool oil_is_limited;
77  double new_gas_rate;
78  bool gas_is_limited;
79  double alq;
80  bool alq_is_limited;
81  };
82 
83  virtual ~GasLiftSingleWellGeneric() = default;
84 
85  const std::string& name() const { return well_name_; }
86 
87  std::optional<GradInfo> calcIncOrDecGradient(double oil_rate, double gas_rate,
88  double alq, bool increase) const;
89 
90  std::unique_ptr<GasLiftWellState> runOptimize(const int iteration_idx);
91 
92  virtual const WellInterfaceGeneric& getStdWell() const = 0;
93 
94 protected:
96  DeferredLogger &deferred_logger,
97  WellState &well_state,
98  const GroupState& group_state,
99  const Well& ecl_well,
100  const SummaryState& summary_state,
101  GasLiftGroupInfo &group_info,
102  const Schedule& schedule,
103  const int report_step_idx,
104  GLiftSyncGroups &sync_groups
105  );
106 
108  {
109  OptimizeState( GasLiftSingleWellGeneric& parent_, bool increase_ ) :
110  parent{parent_},
111  increase{increase_},
112  it{0},
113  stop_iteration{false},
114  bhp{-1}
115  {}
116 
117  GasLiftSingleWellGeneric& parent;
118  bool increase;
119  int it;
120  bool stop_iteration;
121  double bhp;
122 
123  std::pair<std::optional<double>,bool> addOrSubtractAlqIncrement(double alq);
124  double calcEcoGradient(double oil_rate, double new_oil_rate,
125  double gas_rate, double new_gas_rate);
126  bool checkAlqOutsideLimits(double alq, double oil_rate);
127  bool checkEcoGradient(double gradient);
128  bool checkGroupALQrateExceeded(double delta_alq);
129  bool checkGroupTargetsViolated(double delta_oil, double delta_gas);
130  std::tuple<double,double,double>
131  reduceALQtoGroupTarget(double alq, double oil_rate, double gas_rate, std::vector<double> &potentials);
132  bool checkNegativeOilRate(double oil_rate);
133  bool checkThpControl();
134  bool checkOilRateExceedsTarget(double oil_rate);
135  bool checkRate(double rate, double limit, const std::string &rate_str) const;
136  bool checkWellRatesViolated(std::vector<double> &potentials);
137  bool computeBhpAtThpLimit(double alq);
138  void debugShowIterationInfo(double alq);
139  double getBhpWithLimit();
140  void updateGroupRates(double delta_oil, double delta_gas, double delta_alq);
141  void warn_(std::string msg) {parent.displayWarning_(msg);}
142  };
143 
144  std::pair<std::optional<double>, bool>
145  addOrSubtractAlqIncrement_(double alq, bool increase) const;
146 
147  double calcEcoGradient_(double oil_rate, double new_oil_rate,
148  double gas_rate, double new_gas_rate, bool increase) const;
149 
150  bool checkALQequal_(double alq1, double alq2) const;
151  bool checkInitialALQmodified_(double alq, double initial_alq) const;
152 
153  bool checkWellRatesViolated_(std::vector<double>& potentials,
154  const std::function<bool(double, double, const std::string &)>& callback,
155  bool increase);
156 
157  virtual std::optional<double> computeBhpAtThpLimit_(double alq) const = 0;
158  virtual void computeWellRates_(double bhp,
159  std::vector<double>& potentials,
160  bool debug_output = true) const = 0;
161 
162  bool computeInitialWellRates_(std::vector<double>& potentials);
163 
164  void debugCheckNegativeGradient_(double grad, double alq, double new_alq,
165  double oil_rate, double new_oil_rate, double gas_rate,
166  double new_gas_rate, bool increase) const;
167 
168  void debugShowAlqIncreaseDecreaseCounts_();
169 
170  void debugShowBhpAlqTable_();
171 
172  void debugShowStartIteration_(double alq, bool increase, double oil_rate);
173 
174  void debugShowTargets_();
175 
176  void displayDebugMessage_(const std::string& msg) const;
177  void displayWarning_(const std::string& warning);
178 
179  std::pair<double, bool> getBhpWithLimit_(double bhp) const;
180  std::pair<double, bool> getGasRateWithLimit_(const std::vector<double>& potentials) const;
181  std::tuple<double,double,bool,bool>
182  getInitialRatesWithLimit_(const std::vector<double>& potentials);
183  std::pair<double, bool> getOilRateWithLimit_(const std::vector<double>& potentials) const;
184 
185  std::tuple<double,double,bool,bool,double>
186  increaseALQtoPositiveOilRate_(double alq,
187  double oil_rate,
188  double gas_rate,
189  bool oil_is_limited,
190  bool gas_is_limited,
191  std::vector<double>& potentials);
192 
193  std::tuple<double,double,bool,bool,double>
194  increaseALQtoMinALQ_(double alq,
195  double oil_rate,
196  double gas_rate,
197  bool oil_is_limited,
198  bool gas_is_limited,
199  std::vector<double>& potentials);
200 
201  void logSuccess_(double alq,
202  const int iteration_idx);
203  std::tuple<double,double,double,bool,bool>
204  maybeAdjustALQbeforeOptimizeLoop_(
205  bool increase, double alq, double oil_rate, double gas_rate,
206  bool oil_is_limited, bool gas_is_limited, std::vector<double> &potentials);
207  std::tuple<double,double,bool,bool,double>
208  reduceALQtoOilTarget_(double alq, double oil_rate, double gas_rate,
209  bool oil_is_limited, bool gas_is_limited, std::vector<double> &potentials);
210 
211  std::unique_ptr<GasLiftWellState> runOptimize1_();
212  std::unique_ptr<GasLiftWellState> runOptimize2_();
213  std::unique_ptr<GasLiftWellState> runOptimizeLoop_(bool increase);
214 
215  void setAlqMinRate_(const GasLiftOpt::Well& well);
216 
217  std::unique_ptr<GasLiftWellState> tryIncreaseLiftGas_();
218  std::unique_ptr<GasLiftWellState> tryDecreaseLiftGas_();
219 
220  void updateWellStateAlqFixedValue_(const GasLiftOpt::Well& well);
221 
222  bool useFixedAlq_(const GasLiftOpt::Well& well);
223 
224  void warnMaxIterationsExceeded_();
225 
226  DeferredLogger& deferred_logger_;
227  WellState& well_state_;
228  const GroupState& group_state_;
229  const Well& ecl_well_;
230  const SummaryState& summary_state_;
231  GasLiftGroupInfo& group_info_;
232  GLiftSyncGroups& sync_groups_;
233  const Well::ProductionControls controls_;
234 
235  double increment_;
236  double max_alq_;
237  double min_alq_;
238  double orig_alq_;
239 
240  double alpha_w_;
241  double alpha_g_;
242  double eco_grad_;
243 
244  int gas_pos_;
245  int oil_pos_;
246  int water_pos_;
247 
248  int max_iterations_;
249  int num_phases_;
250 
251  std::string well_name_;
252 
253  const GasLiftOpt::Well* gl_well_;
254 
255  bool optimize_;
256  bool debug_; // extra debug output
257  bool debug_limit_increase_decrease_;
258  bool debug_abort_if_decrease_and_oil_is_limited_ = false;
259  bool debug_abort_if_increase_and_gas_is_limited_ = false;
260 };
261 
262 } // namespace Opm
263 
264 #endif // OPM_GASLIFT_SINGLE_WELL_GENERIC_HEADER_INCLUDED
Definition: DeferredLogger.hpp:57
Definition: GasLiftGroupInfo.hpp:46
Definition: GasLiftSingleWellGeneric.hpp:51
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:56
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:26
Definition: GasLiftSingleWellGeneric.hpp:61
Definition: GasLiftSingleWellGeneric.hpp:108