My Project
GasLiftStage2.hpp
1 /*
2  Copyright 2021 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_STAGE2_HEADER_INCLUDED
21 #define OPM_GASLIFT_STAGE2_HEADER_INCLUDED
22 
23 #include <opm/core/props/BlackoilPhases.hpp>
24 #include <opm/simulators/wells/GasLiftSingleWellGeneric.hpp>
25 
26 #include <dune/common/version.hh>
27 #include <dune/common/parallel/mpihelper.hh>
28 
29 #include <iterator>
30 #include <map>
31 #include <memory>
32 #include <optional>
33 #include <string>
34 #include <tuple>
35 #include <vector>
36 
37 namespace Opm
38 {
39 
40 class DeferredLogger;
41 class GasLiftOpt;
42 class GasLiftWellState;
43 class Group;
44 class Schedule;
45 class WellInterfaceGeneric;
46 class WellState;
47 
48 class GasLiftStage2 : public GasLiftCommon {
50  using GLiftOptWells = std::map<std::string,std::unique_ptr<GasLiftSingleWell>>;
51  using GLiftProdWells = std::map<std::string,const WellInterfaceGeneric*>;
52  using GLiftWellStateMap = std::map<std::string,std::unique_ptr<GasLiftWellState>>;
53  using GradPair = std::pair<std::string, double>;
54  using GradPairItr = std::vector<GradPair>::iterator;
55  using GradInfo = typename GasLiftSingleWellGeneric::GradInfo;
56  using GradMap = std::map<std::string, GradInfo>;
57  using MPIComm = typename Dune::MPIHelper::MPICommunicator;
58  static const int Water = BlackoilPhases::Aqua;
59  static const int Oil = BlackoilPhases::Liquid;
60  static const int Gas = BlackoilPhases::Vapour;
61 public:
63  const int report_step_idx,
64  const Parallel::Communication& comm,
65  const Schedule& schedule,
66  const SummaryState& summary_state,
67  DeferredLogger& deferred_logger,
68  WellState& well_state,
69  GLiftProdWells& prod_wells,
70  GLiftOptWells& glift_wells,
71  GLiftWellStateMap& state_map,
72  bool glift_debug
73  );
74  void runOptimize();
75 protected:
76  void addOrRemoveALQincrement_(
77  GradMap& grad_map, const std::string& well_name, bool add);
78  std::optional<GradInfo> calcIncOrDecGrad_(
79  const std::string name, const GasLiftSingleWell& gs_well, bool increase);
80  bool checkRateAlreadyLimited_(GasLiftWellState& state, bool increase);
81  GradInfo deleteDecGradItem_(const std::string& name);
82  GradInfo deleteIncGradItem_(const std::string& name);
83  GradInfo deleteGrad_(const std::string& name, bool increase);
84  void displayDebugMessage_(const std::string& msg) const override;
85  void displayDebugMessage2B_(const std::string& msg);
86  void displayDebugMessage_(const std::string& msg, const std::string& group_name);
87  void displayWarning_(const std::string& msg, const std::string& group_name);
88  void displayWarning_(const std::string& msg);
89  std::tuple<double, double, double> getCurrentGroupRates_(
90  const Group& group);
91  std::array<double,3> getCurrentGroupRatesRecursive_(
92  const Group& group);
93  std::tuple<double, double, double> getCurrentWellRates_(
94  const std::string& well_name, const std::string& group_name);
95  std::vector<GasLiftSingleWell *> getGroupGliftWells_(
96  const Group& group);
97  void getGroupGliftWellsRecursive_(
98  const Group& group, std::vector<GasLiftSingleWell *>& wells);
99  std::pair<double, double> getWellRates_(const WellInterfaceGeneric& well);
100  void optimizeGroup_(const Group& group);
101  void optimizeGroupsRecursive_(const Group& group);
102  void recalculateGradientAndUpdateData_(
103  GradPairItr& grad_itr, bool increase,
104  std::vector<GradPair>& grads, std::vector<GradPair>& other_grads);
105  void redistributeALQ_(
106  std::vector<GasLiftSingleWell *>& wells, const Group& group,
107  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
108  void removeSurplusALQ_(
109  const Group& group,
110  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
111  void saveGrad_(GradMap& map, const std::string& name, GradInfo& grad);
112  void saveDecGrad_(const std::string& name, GradInfo& grad);
113  void saveIncGrad_(const std::string& name, GradInfo& grad);
114  void sortGradients_(std::vector<GradPair>& grads);
115  std::optional<GradInfo> updateGrad_(
116  const std::string& name, GradInfo& grad, bool increase);
117  void updateGradVector_(
118  const std::string& name, std::vector<GradPair>& grads, double grad);
119  void mpiSyncGlobalGradVector_(std::vector<GradPair>& grads_global) const;
120  void mpiSyncLocalToGlobalGradVector_(
121  const std::vector<GradPair>& grads_local,
122  std::vector<GradPair>& grads_global) const;
123 
124 
125  GLiftProdWells& prod_wells_;
126  GLiftOptWells& stage1_wells_;
127  GLiftWellStateMap& well_state_map_;
128 
129  int report_step_idx_;
130  const SummaryState& summary_state_;
131  const Schedule& schedule_;
132  const GasLiftOpt& glo_;
133  GradMap inc_grads_;
134  GradMap dec_grads_;
135  int max_iterations_ = 1000;
136  //int time_step_idx_;
137 
138  struct OptimizeState {
139  OptimizeState(GasLiftStage2& parent_, const Group& group_ ) :
140  parent{parent_},
141  group{group_},
142  it{0}
143  {}
144  GasLiftStage2& parent;
145  const Group& group;
146  int it;
147 
148  using GradInfo = typename GasLiftStage2::GradInfo;
149  using GradPair = typename GasLiftStage2::GradPair;
150  using GradPairItr = typename GasLiftStage2::GradPairItr;
151  using GradMap = typename GasLiftStage2::GradMap;
152  void calculateEcoGradients(std::vector<GasLiftSingleWell *>& wells,
153  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
154  bool checkAtLeastTwoWells(std::vector<GasLiftSingleWell *>& wells);
155  void debugShowIterationInfo();
156  std::pair<std::optional<GradPairItr>,std::optional<GradPairItr>>
157  getEcoGradients(
158  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
159  void recalculateGradients(
160  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads,
161  GradPairItr& min_dec_grad_itr, GradPairItr &max_inc_grad_itr);
162  void redistributeALQ( GradPairItr& min_dec_grad, GradPairItr& max_inc_grad);
163 
164  private:
165  void displayDebugMessage_(const std::string& msg);
166  void displayWarning_(const std::string& msg);
167 
168  };
169 
170  struct SurplusState {
171  SurplusState( GasLiftStage2& parent_, const Group& group_,
172  double oil_rate_, double gas_rate_, double alq_, double min_eco_grad_,
173  double oil_target_, double gas_target_,
174  std::optional<double> max_glift_) :
175  parent{parent_},
176  group{group_},
177  oil_rate{oil_rate_},
178  gas_rate{gas_rate_},
179  alq{alq_},
180  min_eco_grad{min_eco_grad_},
181  oil_target{oil_target_},
182  gas_target{gas_target_},
183  max_glift{max_glift_},
184  it{0}
185  {}
186  GasLiftStage2 &parent;
187  const Group &group;
188  double oil_rate;
189  double gas_rate;
190  double alq;
191  const double min_eco_grad;
192  const double oil_target;
193  const double gas_target;
194  std::optional<double> max_glift;
195  int it;
196 
197  void addOrRemoveALQincrement(
198  GradMap &grad_map, const std::string& well_name, bool add);
199  bool checkALQlimit();
200  bool checkEcoGradient(const std::string& well_name, double eco_grad);
201  bool checkGasTarget();
202  bool checkOilTarget();
203  void updateRates(const std::string& name);
204  };
205 };
206 
207 } // namespace Opm
208 
209 #endif // OPM_GASLIFT_STAGE2_HEADER_INCLUDED
Definition: DeferredLogger.hpp:57
Definition: GasLiftCommon.hpp:32
Definition: GasLiftSingleWellGeneric.hpp:52
Definition: GasLiftStage2.hpp:48
Definition: GasLiftWellState.hpp:29
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:27
Definition: GasLiftSingleWellGeneric.hpp:64
Definition: GasLiftStage2.hpp:138
Definition: GasLiftStage2.hpp:170