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 
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  );
73  void runOptimize();
74 private:
75  void addOrRemoveALQincrement_(
76  GradMap& grad_map, const std::string& well_name, bool add);
77  std::optional<GradInfo> calcIncOrDecGrad_(
78  const std::string name, const GasLiftSingleWell& gs_well, bool increase);
79  bool checkRateAlreadyLimited_(GasLiftWellState& state, bool increase);
80  GradInfo deleteDecGradItem_(const std::string& name);
81  GradInfo deleteIncGradItem_(const std::string& name);
82  GradInfo deleteGrad_(const std::string& name, bool increase);
83  void displayDebugMessage_(const std::string& msg);
84  void displayDebugMessage2B_(const std::string& msg);
85  void displayDebugMessage_(const std::string& msg, const std::string& group_name);
86  void displayWarning_(const std::string& msg, const std::string& group_name);
87  void displayWarning_(const std::string& msg);
88  std::tuple<double, double, double> getCurrentGroupRates_(
89  const Group& group);
90  std::array<double,3> getCurrentGroupRatesRecursive_(
91  const Group& group);
92  std::tuple<double, double, double> getCurrentWellRates_(
93  const std::string& well_name, const std::string& group_name);
94  std::vector<GasLiftSingleWell *> getGroupGliftWells_(
95  const Group& group);
96  void getGroupGliftWellsRecursive_(
97  const Group& group, std::vector<GasLiftSingleWell *>& wells);
98  std::pair<double, double> getStdWellRates_(const WellInterfaceGeneric& well);
99  void optimizeGroup_(const Group& group);
100  void optimizeGroupsRecursive_(const Group& group);
101  void recalculateGradientAndUpdateData_(
102  GradPairItr& grad_itr, bool increase,
103  std::vector<GradPair>& grads, std::vector<GradPair>& other_grads);
104  void redistributeALQ_(
105  std::vector<GasLiftSingleWell *>& wells, const Group& group,
106  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
107  void removeSurplusALQ_(
108  const Group& group,
109  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
110  void saveGrad_(GradMap& map, const std::string& name, GradInfo& grad);
111  void saveDecGrad_(const std::string& name, GradInfo& grad);
112  void saveIncGrad_(const std::string& name, GradInfo& grad);
113  void sortGradients_(std::vector<GradPair>& grads);
114  std::optional<GradInfo> updateGrad_(
115  const std::string& name, GradInfo& grad, bool increase);
116  void updateGradVector_(
117  const std::string& name, std::vector<GradPair>& grads, double grad);
118  void mpiSyncGlobalGradVector_(std::vector<GradPair>& grads_global) const;
119  void mpiSyncLocalToGlobalGradVector_(
120  const std::vector<GradPair>& grads_local,
121  std::vector<GradPair>& grads_global) const;
122 
123 
124  DeferredLogger& deferred_logger_;
125  WellState& well_state_;
126  GLiftProdWells& prod_wells_;
127  GLiftOptWells& stage1_wells_;
128  GLiftWellStateMap& well_state_map_;
129 
130  int report_step_idx_;
131  const SummaryState& summary_state_;
132  const Schedule& schedule_;
133  const GasLiftOpt& glo_;
134  const Parallel::Communication& comm_;
135  GradMap inc_grads_;
136  GradMap dec_grads_;
137  bool debug_;
138  int max_iterations_ = 1000;
139  //int time_step_idx_;
140 
141  struct OptimizeState {
142  OptimizeState(GasLiftStage2& parent_, const Group& group_ ) :
143  parent{parent_},
144  group{group_},
145  it{0}
146  {}
147  GasLiftStage2& parent;
148  const Group& group;
149  int it;
150 
151  using GradInfo = typename GasLiftStage2::GradInfo;
152  using GradPair = typename GasLiftStage2::GradPair;
153  using GradPairItr = typename GasLiftStage2::GradPairItr;
154  using GradMap = typename GasLiftStage2::GradMap;
155  void calculateEcoGradients(std::vector<GasLiftSingleWell *>& wells,
156  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
157  bool checkAtLeastTwoWells(std::vector<GasLiftSingleWell *>& wells);
158  void debugShowIterationInfo();
159  std::pair<std::optional<GradPairItr>,std::optional<GradPairItr>>
160  getEcoGradients(
161  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads);
162  void recalculateGradients(
163  std::vector<GradPair>& inc_grads, std::vector<GradPair>& dec_grads,
164  GradPairItr& min_dec_grad_itr, GradPairItr &max_inc_grad_itr);
165  void redistributeALQ( GradPairItr& min_dec_grad, GradPairItr& max_inc_grad);
166 
167  private:
168  void displayDebugMessage_(const std::string& msg);
169  void displayWarning_(const std::string& msg);
170 
171  };
172 
173  struct SurplusState {
174  SurplusState( GasLiftStage2& parent_, const Group& group_,
175  double oil_rate_, double gas_rate_, double alq_, double min_eco_grad_,
176  double oil_target_, double gas_target_,
177  std::optional<double> max_glift_) :
178  parent{parent_},
179  group{group_},
180  oil_rate{oil_rate_},
181  gas_rate{gas_rate_},
182  alq{alq_},
183  min_eco_grad{min_eco_grad_},
184  oil_target{oil_target_},
185  gas_target{gas_target_},
186  max_glift{max_glift_},
187  it{0}
188  {}
189  GasLiftStage2 &parent;
190  const Group &group;
191  double oil_rate;
192  double gas_rate;
193  double alq;
194  const double min_eco_grad;
195  const double oil_target;
196  const double gas_target;
197  std::optional<double> max_glift;
198  int it;
199 
200  void addOrRemoveALQincrement(
201  GradMap &grad_map, const std::string& well_name, bool add);
202  bool checkALQlimit();
203  bool checkEcoGradient(const std::string& well_name, double eco_grad);
204  bool checkGasTarget();
205  bool checkOilTarget();
206  void updateRates(const std::string& name);
207  };
208 };
209 
210 } // namespace Opm
211 
212 #endif // OPM_GASLIFT_STAGE2_HEADER_INCLUDED
Definition: DeferredLogger.hpp:57
Definition: GasLiftSingleWellGeneric.hpp:51
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:26
Definition: GasLiftSingleWellGeneric.hpp:61