22#define OPM_BLACKOILWELLMODEL_GASLIFT_IMPL_HEADER_INCLUDED
23#define OPM_BLACKOILWELLMODEL_GASLIFT_IMPL_HEADER_INCLUDED
26#ifndef OPM_BLACKOILWELLMODEL_GASLIFT_HEADER_INCLUDED
30#include <opm/common/TimingMacros.hpp>
40template<
typename TypeTag>
44 const std::vector<WellInterfacePtr>& well_container,
45 const std::map<std::string, Scalar>& node_pressures,
46 const bool updatePotentials,
52 const auto& glo = simulator.vanguard().schedule().glo(simulator.episodeIndex());
56 bool do_glift_optimization =
false;
57 int num_wells_changed = 0;
58 const double simulation_time = simulator.time();
59 const Scalar min_wait = glo.min_wait();
64 if (simulation_time == this->last_glift_opt_time_ ||
65 simulation_time >= (this->last_glift_opt_time_ + min_wait))
67 do_glift_optimization =
true;
68 this->last_glift_opt_time_ = simulation_time;
72 if (updatePotentials) {
73 updateWellPotentials(simulator, well_container, node_pressures, wellState, deferred_logger);
76 if (do_glift_optimization) {
88 initGliftEclWellMap(well_container, ecl_well_map);
91 simulator.vanguard().schedule(),
92 simulator.vanguard().summaryState(),
93 simulator.episodeIndex(),
94 simulator.model().newtonMethod().numIterations(),
98 simulator.vanguard().grid().comm(),
101 group_info.initialize();
103 gasLiftOptimizationStage1(simulator,
113 this->gasLiftOptimizationStage2(simulator.vanguard().gridView().comm(),
114 simulator.vanguard().schedule(),
115 simulator.vanguard().summaryState(),
122 simulator.episodeIndex(),
125 if constexpr (glift_debug) {
126 std::vector<WellInterfaceGeneric<Scalar, IndexTraits>*> wc;
127 wc.reserve(well_container.size());
128 std::transform(well_container.begin(), well_container.end(),
129 std::back_inserter(wc),
131 { return static_cast<WellInterfaceGeneric<Scalar, IndexTraits>*>(w.get()); });
132 this->gliftDebugShowALQ(wc,
136 num_wells_changed = glift_wells.size();
138 num_wells_changed = simulator.vanguard().gridView().comm().sum(num_wells_changed);
139 return num_wells_changed > 0;
142template<
typename TypeTag>
146 const std::vector<WellInterfacePtr>& well_container,
147 WellStateType& wellState,
149 GLiftProdWells& prod_wells,
150 GLiftOptWells &glift_wells,
152 GLiftWellStateMap& state_map,
156 auto comm = simulator.
vanguard().grid().comm();
157 int num_procs = comm.size();
183 for (
int i = 0; i< num_procs; i++) {
184 int num_rates_to_sync = 0;
185 GLiftSyncGroups groups_to_sync;
186 if (comm.rank() == i) {
188 for (
const auto& well : well_container) {
190 if (group_info.
hasWell(well->name())) {
191 gasLiftOptimizationStage1SingleWell(well.get(),
203 num_rates_to_sync = groups_to_sync.size();
206 OPM_TIMEBLOCK(WaitForGasLiftSyncGroups);
207 num_rates_to_sync = comm.sum(num_rates_to_sync);
209 if (num_rates_to_sync > 0) {
210 OPM_TIMEBLOCK(GasLiftSyncGroups);
211 std::vector<int> group_indexes;
212 group_indexes.reserve(num_rates_to_sync);
213 std::vector<Scalar> group_alq_rates;
214 group_alq_rates.reserve(num_rates_to_sync);
215 std::vector<Scalar> group_oil_rates;
216 group_oil_rates.reserve(num_rates_to_sync);
217 std::vector<Scalar> group_gas_rates;
218 group_gas_rates.reserve(num_rates_to_sync);
219 std::vector<Scalar> group_water_rates;
220 group_water_rates.reserve(num_rates_to_sync);
221 if (comm.rank() == i) {
222 for (
auto idx : groups_to_sync) {
223 auto [oil_rate, gas_rate, water_rate, alq] = group_info.
getRates(idx);
224 group_indexes.push_back(idx);
225 group_oil_rates.push_back(oil_rate);
226 group_gas_rates.push_back(gas_rate);
227 group_water_rates.push_back(water_rate);
228 group_alq_rates.push_back(alq);
231 group_indexes.resize(num_rates_to_sync);
232 group_oil_rates.resize(num_rates_to_sync);
233 group_gas_rates.resize(num_rates_to_sync);
234 group_water_rates.resize(num_rates_to_sync);
235 group_alq_rates.resize(num_rates_to_sync);
238 Parallel::MpiSerializer ser(comm);
239 ser.broadcast(Parallel::RootRank{i}, group_indexes, group_oil_rates,
240 group_gas_rates, group_water_rates, group_alq_rates);
242 if (comm.rank() != i) {
243 for (
int j = 0; j < num_rates_to_sync; ++j) {
247 group_water_rates[j],
251 if constexpr (glift_debug) {
253 if (comm.rank() == i) {
254 counter = wellState.gliftGetDebugCounter();
256 counter = comm.sum(counter);
257 if (comm.rank() != i) {
258 wellState.gliftSetDebugCounter(counter);
268template<
typename TypeTag>
270BlackoilWellModelGasLift<TypeTag>::
271gasLiftOptimizationStage1SingleWell(WellInterface<TypeTag>* well,
272 const Simulator& simulator,
273 WellStateType& wellState,
274 GroupState<Scalar>& groupState,
275 GLiftProdWells& prod_wells,
276 GLiftOptWells& glift_wells,
277 GasLiftGroupInfo<Scalar, IndexTraits>& group_info,
278 GLiftWellStateMap& state_map,
279 GLiftSyncGroups& sync_groups,
280 DeferredLogger& deferred_logger)
283 const auto& summary_state = simulator.vanguard().summaryState();
284 auto glift = std::make_unique<GasLiftSingleWell<TypeTag>>(*well,
292 simulator.vanguard().gridView().comm(),
294 auto state = glift->runOptimize(simulator.model().newtonMethod().numIterations());
296 state_map.emplace(well->name(), std::move(state));
297 glift_wells.emplace(well->name(), std::move(glift));
300 prod_wells.insert({well->name(), well});
303template<
typename TypeTag>
309 for (
const auto& well : well_container) {
310 ecl_well_map.try_emplace(well->name(), &well->wellEcl(), well->indexOfWell());
314template<
typename TypeTag>
318 const std::vector<WellInterfacePtr>& well_container,
319 const std::map<std::string, Scalar>& node_pressures,
320 WellStateType& wellState,
323 auto well_state_copy = wellState;
324 const int np = wellState.numPhases();
327 for (
const auto& well : well_container) {
328 if (well->isInjector() || !well->wellEcl().predictionMode())
331 const auto it = node_pressures.find(well->wellEcl().groupName());
332 if (it != node_pressures.end()) {
333 std::string cur_exc_msg;
336 std::vector<Scalar> potentials;
337 well->computeWellPotentials(simulator, well_state_copy, potentials, deferred_logger);
338 auto& ws = wellState.well(well->indexOfWell());
339 for (
int p = 0; p < np; ++p) {
341 ws.well_potentials[p] = std::max(Scalar{0.0}, potentials[p]);
347 exc_msg += fmt::format(
"\nFor well {}: {}", well->name(), cur_exc_msg);
349 exc_type = std::max(exc_type, cur_exc_type);
353 const std::string msg =
"Updating well potentials after network balancing failed. Continue with current potentials";
354 deferred_logger.
warning(
"WELL_POT_SOLVE_AFTER_NETWORK_FAILED", msg + exc_msg);
#define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg)
Inserts catch classes for the parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:166
Class for handling the gaslift in the blackoil well model.
Definition: BlackoilWellModelGasLift.hpp:96
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModelGasLift.hpp:108
bool maybeDoGasLiftOptimize(const Simulator &simulator, const std::vector< WellInterfacePtr > &well_container, const std::map< std::string, Scalar > &node_pressures, const bool updatePotentials, WellStateType &wellState, GroupState< Scalar > &groupState, DeferredLogger &deferred_logger)
Definition: BlackoilWellModelGasLift_impl.hpp:43
static void initGliftEclWellMap(const std::vector< WellInterfacePtr > &well_container, GLiftEclWells &ecl_well_map)
Definition: BlackoilWellModelGasLift_impl.hpp:306
typename Base::GLiftWellStateMap GLiftWellStateMap
Definition: BlackoilWellModelGasLift.hpp:107
typename Base::GLiftOptWells GLiftOptWells
Definition: BlackoilWellModelGasLift.hpp:104
typename GasLiftGroupInfo< Scalar, IndexTraits >::GLiftEclWells GLiftEclWells
Definition: BlackoilWellModelGasLift.hpp:103
typename Base::GLiftProdWells GLiftProdWells
Definition: BlackoilWellModelGasLift.hpp:105
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModelGasLift.hpp:98
Definition: DeferredLogger.hpp:57
void warning(const std::string &tag, const std::string &message)
Definition: GasLiftGroupInfo.hpp:46
bool hasWell(const std::string &well_name)
std::tuple< Scalar, Scalar, Scalar, Scalar > getRates(const int group_idx) const
void updateRate(int idx, Scalar oil_rate, Scalar gas_rate, Scalar water_rate, Scalar alq)
Definition: GroupState.hpp:41
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:84
Vanguard & vanguard()
Return a reference to the grid manager of simulation.
Definition: simulator.hh:234
Definition: WellState.hpp:66
@ NONE
Definition: DeferredLogger.hpp:46
Definition: blackoilboundaryratevector.hh:39