23#ifndef OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
24#define OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
27#ifndef OPM_BLACKOILWELLMODEL_HEADER_INCLUDED
32#include <opm/grid/utility/cartesianToCompressed.hpp>
33#include <opm/common/utility/numeric/RootFinders.hpp>
35#include <opm/input/eclipse/Schedule/Network/Balance.hpp>
36#include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
37#include <opm/input/eclipse/Schedule/Well/PAvgDynamicSourceData.hpp>
38#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
39#include <opm/input/eclipse/Schedule/Well/WellTestConfig.hpp>
40#include <opm/input/eclipse/Schedule/Well/WellEconProductionLimits.hpp>
42#include <opm/input/eclipse/Units/UnitSystem.hpp>
55#ifdef RESERVOIR_COUPLING_ENABLED
73#include <fmt/format.h>
76 template<
typename TypeTag>
82 simulator.vanguard().summaryState(),
83 simulator.vanguard().eclState(),
85 simulator.gridView().comm())
86 , simulator_(simulator)
87 , guide_rate_handler_{
89 simulator.vanguard().schedule(),
90 simulator.vanguard().summaryState(),
91 simulator.vanguard().grid().comm()
93 , gaslift_(this->terminal_output_)
101 auto& parallel_wells =
simulator.vanguard().parallelWells();
104 for(
const auto& name_bool : parallel_wells) {
110 Parameters::Get<Parameters::AlternativeWellRateInit>();
112 using SourceDataSpan =
113 typename PAvgDynamicSourceData<Scalar>::template SourceDataSpan<Scalar>;
116 [
this](
const std::size_t globalIndex)
118 [
this](
const int localCell, SourceDataSpan sourceTerms)
120 using Item =
typename SourceDataSpan::Item;
122 const auto* intQuants = this->
simulator_.model()
123 .cachedIntensiveQuantities(localCell, 0);
124 const auto& fs = intQuants->fluidState();
127 .set(Item::PoreVol, intQuants->porosity().value() *
128 this->
simulator_.model().dofTotalVolume(localCell))
129 .set(Item::Depth, this->
depth_[localCell]);
131 constexpr auto io = FluidSystem::oilPhaseIdx;
132 constexpr auto ig = FluidSystem::gasPhaseIdx;
133 constexpr auto iw = FluidSystem::waterPhaseIdx;
136 const auto haveOil = FluidSystem::phaseIsActive(io);
137 const auto haveGas = FluidSystem::phaseIsActive(ig);
138 const auto haveWat = FluidSystem::phaseIsActive(iw);
140 auto weightedPhaseDensity = [&fs](
const auto ip)
142 return fs.saturation(ip).value() * fs.density(ip).value();
145 if (haveOil) { sourceTerms.set(Item::Pressure, fs.pressure(io).value()); }
146 else if (haveGas) { sourceTerms.set(Item::Pressure, fs.pressure(ig).value()); }
147 else { sourceTerms.set(Item::Pressure, fs.pressure(iw).value()); }
151 if (haveOil) { rho += weightedPhaseDensity(io); }
152 if (haveGas) { rho += weightedPhaseDensity(ig); }
153 if (haveWat) { rho += weightedPhaseDensity(iw); }
155 sourceTerms.set(Item::MixtureDensity, rho);
160 template<
typename TypeTag>
165 extractLegacyCellPvtRegionIndex_();
166 extractLegacyDepth_();
168 gravity_ = simulator_.problem().gravity()[2];
170 this->initial_step_ =
true;
173 simulator_.model().addAuxiliaryModule(
this);
175 is_cell_perforated_.resize(local_num_cells_,
false);
179 template<
typename TypeTag>
184 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
185 + ScheduleEvents::NEW_WELL;
186 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
187 for (
auto& wellPtr : this->well_container_) {
188 const bool well_opened_this_step = this->report_step_starts_ &&
189 events.hasEvent(wellPtr->name(),
190 effective_events_mask);
191 wellPtr->init(this->depth_, this->gravity_,
192 this->B_avg_, well_opened_this_step);
196 template<
typename TypeTag>
203 this->wgHelper().setReportStep(timeStepIdx);
204 this->report_step_starts_ =
true;
205 this->report_step_start_events_ = this->schedule()[timeStepIdx].wellgroup_events();
207 this->rateConverter_ = std::make_unique<RateConverterType>
208 (std::vector<int>(this->local_num_cells_, 0));
212 const auto enableWellPIScaling =
true;
213 this->initializeLocalWellStructure(timeStepIdx, enableWellPIScaling);
216 this->initializeGroupStructure(timeStepIdx);
218 const auto& comm = this->simulator_.vanguard().grid().comm();
224 this->rateConverter_->template defineState<ElementContext>(this->simulator_);
228 const auto& sched_state = this->schedule()[timeStepIdx];
230 this->vfp_properties_ = std::make_unique<VFPProperties<Scalar, IndexTraits>>
231 (sched_state.vfpinj(), sched_state.vfpprod(), this->wellState());
235 "beginReportStep() failed: ",
236 this->terminal_output_, comm)
240 this->commitWGState();
242 this->wellStructureChangedDynamically_ =
false;
249 template <
typename TypeTag>
253 const bool enableWellPIScaling)
257 const auto& comm = this->simulator_.vanguard().grid().comm();
260 this->wells_ecl_ = this->getLocalWells(reportStepIdx);
261 this->local_parallel_well_info_ =
262 this->createLocalParallelWellInfo(this->wells_ecl_);
269 this->initializeWellPerfData();
270 this->initializeWellState(reportStepIdx);
271 this->wbp_.initializeWBPCalculationService();
273 if (this->param_.use_multisegment_well_ && this->anyMSWellOpenLocal()) {
274 this->wellState().initWellStateMSWell(this->wells_ecl_, &this->prevWellState());
277 this->initializeWellProdIndCalculators();
279 if (enableWellPIScaling && this->schedule()[reportStepIdx].events()
280 .hasEvent(ScheduleEvents::Events::WELL_PRODUCTIVITY_INDEX))
282 this->runWellPIScaling(reportStepIdx, local_deferredLogger);
286 "Failed to initialize local well structure: ",
287 this->terminal_output_, comm)
294 template <
typename TypeTag>
301 const auto& comm = this->simulator_.vanguard().grid().comm();
305 const auto& fieldGroup =
306 this->schedule().getGroup(
"FIELD", reportStepIdx);
308 this->wgHelper().setCmodeGroup(fieldGroup, this->groupState());
312 if (this->schedule()[reportStepIdx].has_gpmaint()) {
313 this->wgHelper().setRegionAveragePressureCalculator(
315 this->eclState_.fieldProps(),
316 this->regionalAveragePressureCalculator_
321 "Failed to initialize group structure: ",
322 this->terminal_output_, comm)
330 template<
typename TypeTag>
335 OPM_TIMEBLOCK(beginTimeStep);
337 this->updateAverageFormationFactor();
341#ifdef RESERVOIR_COUPLING_ENABLED
342 auto rescoup_logger_guard = this->setupRescoupScopedLogger(local_deferredLogger);
344 this->switched_prod_groups_.clear();
345 this->switched_inj_groups_.clear();
347 if (this->wellStructureChangedDynamically_) {
352 const auto reportStepIdx =
353 this->simulator_.episodeIndex();
357 const auto enableWellPIScaling =
false;
359 this->initializeLocalWellStructure(reportStepIdx, enableWellPIScaling);
360 this->initializeGroupStructure(reportStepIdx);
362 this->commitWGState();
368 this->wellStructureChangedDynamically_ =
false;
371 this->resetWGState();
372 const int reportStepIdx = simulator_.episodeIndex();
374 this->wellState().updateWellsDefaultALQ(this->schedule(), reportStepIdx, this->summaryState());
375 this->wellState().gliftTimeStepInit();
377 const double simulationTime = simulator_.time();
381 wellTesting(reportStepIdx, simulationTime, local_deferredLogger);
384 createWellContainer(reportStepIdx);
386#ifdef RESERVOIR_COUPLING_ENABLED
388 if (this->isReservoirCouplingMaster()) {
389 this->receiveSlaveGroupData();
395 this->updateAndCommunicateGroupData(reportStepIdx,
396 simulator_.model().newtonMethod().numIterations(),
397 param_.nupcol_group_rate_tolerance_,
false,
398 local_deferredLogger);
401 const Grid& grid = simulator_.vanguard().grid();
402 this->wells_active_ = grid.comm().max(!this->well_container_.empty());
407 this->initWellContainer(reportStepIdx);
410 std::fill(is_cell_perforated_.begin(), is_cell_perforated_.end(),
false);
411 for (
auto& well : well_container_) {
412 well->updatePerforatedCell(is_cell_perforated_);
416 this->calculateEfficiencyFactors(reportStepIdx);
418 if constexpr (has_polymer_)
420 if (PolymerModule::hasPlyshlog() || getPropValue<TypeTag, Properties::EnablePolymerMW>() ) {
421 this->setRepRadiusPerfLength();
428 this->terminal_output_, simulator_.vanguard().grid().comm());
430 for (
auto& well : well_container_) {
431 well->setVFPProperties(this->vfp_properties_.get());
432 well->setGuideRate(&this->guideRate_);
435 this->updateFiltrationModelsPreStep(local_deferredLogger);
438 for (
auto& well : well_container_) {
439 well->closeCompletions(this->wellTestState());
445 if (alternative_well_rate_init_) {
450 for (
const auto& well : well_container_) {
451 if (well->isProducer() && !well->wellIsStopped()) {
452 well->initializeProducerWellState(simulator_, this->wellState(), local_deferredLogger);
457 for (
const auto& well : well_container_) {
458 if (well->isVFPActive(local_deferredLogger)){
459 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
463 this->updateWellPotentials(reportStepIdx,
465 simulator_.vanguard().summaryConfig(),
466 local_deferredLogger);
467 }
catch ( std::runtime_error& e ) {
468 const std::string msg =
"A zero well potential is returned for output purposes. ";
469 local_deferredLogger.
warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
471 this->guide_rate_handler_.setLogger(&local_deferredLogger);
473 this->guide_rate_handler_.updateGuideRates(
474 reportStepIdx, simulationTime, this->wellState(), this->groupState()
476#ifdef RESERVOIR_COUPLING_ENABLED
477 if (this->isReservoirCouplingSlave()) {
478 this->sendSlaveGroupDataToMaster();
484 if (this->schedule_[reportStepIdx].has_gpmaint()) {
485 for (
const auto& calculator : regionalAveragePressureCalculator_) {
486 calculator.second->template defineState<ElementContext>(simulator_);
488 const double dt = simulator_.timeStepSize();
489 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", reportStepIdx);
490 this->wgHelper().updateGpMaintTargetForGroups(fieldGroup,
491 regionalAveragePressureCalculator_,
496 this->updateAndCommunicateGroupData(reportStepIdx,
497 simulator_.model().newtonMethod().numIterations(),
498 param_.nupcol_group_rate_tolerance_,
500 local_deferredLogger);
503 for (
auto& well : well_container_) {
504 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
505 + ScheduleEvents::INJECTION_TYPE_CHANGED
506 + ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER
507 + ScheduleEvents::NEW_WELL;
509 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
510 const bool event = this->report_step_starts_ && events.hasEvent(well->name(), effective_events_mask);
511 const bool dyn_status_change = this->wellState().well(well->name()).status
512 != this->prevWellState().well(well->name()).status;
514 if (event || dyn_status_change) {
516 well->scaleSegmentRatesAndPressure(this->wellState());
517 well->calculateExplicitQuantities(simulator_, this->wellState(), local_deferredLogger);
518 well->updateWellStateWithTarget(simulator_, this->wgHelper(), this->wellState(), local_deferredLogger);
519 well->updatePrimaryVariables(simulator_, this->wellState(), local_deferredLogger);
520 well->solveWellEquation(
521 simulator_, this->wgHelper(), this->wellState(), local_deferredLogger
523 }
catch (
const std::exception& e) {
524 const std::string msg =
"Compute initial well solution for new well " + well->name() +
" failed. Continue with zero initial rates";
525 local_deferredLogger.
warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
534 const std::string msg =
"Compute initial well solution for new wells failed. Continue with zero initial rates";
535 local_deferredLogger.
warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
538 const auto& comm = simulator_.vanguard().grid().comm();
540 exc_type,
"beginTimeStep() failed: " + exc_msg, this->terminal_output_, comm);
544#ifdef RESERVOIR_COUPLING_ENABLED
554 template<
typename TypeTag>
555 std::optional<ReservoirCoupling::ScopedLoggerGuard>
558 if (this->isReservoirCouplingMaster()) {
560 this->reservoirCouplingMaster().getLogger(),
563 }
else if (this->isReservoirCouplingSlave()) {
564 return ReservoirCoupling::ScopedLoggerGuard{
565 this->reservoirCouplingSlave().getLogger(),
572 template<
typename TypeTag>
577 assert(this->isReservoirCouplingMaster());
578 RescoupReceiveSlaveGroupData<Scalar, IndexTraits> slave_group_data_receiver{
581 slave_group_data_receiver.receiveSlaveGroupData();
584 template<
typename TypeTag>
589 assert(this->isReservoirCouplingSlave());
590 RescoupSendSlaveGroupData<Scalar, IndexTraits> slave_group_data_sender{this->wgHelper()};
591 slave_group_data_sender.sendSlaveGroupDataToMaster();
595 template<
typename TypeTag>
598 const double simulationTime,
601 for (
const std::string& well_name : this->getWellsForTesting(timeStepIdx, simulationTime)) {
602 const Well& wellEcl = this->schedule().getWell(well_name, timeStepIdx);
603 if (wellEcl.getStatus() == Well::Status::SHUT)
606 WellInterfacePtr well = createWellForWellTest(well_name, timeStepIdx, deferred_logger);
608 well->init(depth_, gravity_, B_avg_,
true);
610 Scalar well_efficiency_factor = wellEcl.getEfficiencyFactor() *
611 this->wellState().getGlobalEfficiencyScalingFactor(well_name);
612 this->wgHelper().accumulateGroupEfficiencyFactor(
613 this->schedule().getGroup(wellEcl.groupName(), timeStepIdx),
614 well_efficiency_factor
617 well->setWellEfficiencyFactor(well_efficiency_factor);
618 well->setVFPProperties(this->vfp_properties_.get());
619 well->setGuideRate(&this->guideRate_);
622 if (well->isProducer() && alternative_well_rate_init_) {
623 well->initializeProducerWellState(simulator_, this->wellState(), deferred_logger);
625 if (well->isVFPActive(deferred_logger)) {
626 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
629 const auto& network = this->schedule()[timeStepIdx].network();
630 if (network.active() && !this->node_pressures_.empty()) {
631 if (well->isProducer()) {
632 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
633 if (it != this->node_pressures_.end()) {
636 const Scalar nodal_pressure = it->second;
637 well->setDynamicThpLimit(nodal_pressure);
643 GLiftEclWells ecl_well_map;
644 gaslift_.initGliftEclWellMap(well_container_, ecl_well_map);
645 well->wellTesting(simulator_,
649 this->wellTestState(),
651 this->well_open_times_,
653 }
catch (
const std::exception& e) {
654 const std::string msg = fmt::format(
"Exception during testing of well: {}. The well will not open.\n Exception message: {}", wellEcl.name(), e.what());
655 deferred_logger.
warning(
"WELL_TESTING_FAILED", msg);
665 template<
typename TypeTag>
671 for (
auto&& pinfo : this->local_parallel_well_info_)
682 template<
typename TypeTag>
692 template<
typename TypeTag>
697 this->closed_this_step_.clear();
700 this->report_step_starts_ =
false;
701 const int reportStepIdx = simulator_.episodeIndex();
704 for (
const auto& well : well_container_) {
705 if (getPropValue<TypeTag, Properties::EnablePolymerMW>() && well->isInjector()) {
706 well->updateWaterThroughput(dt, this->wellState());
710 for (
const auto& well : well_container_) {
711 well->updateConnectionTransmissibilityFactor(simulator_, this->wellState().well(well->indexOfWell()));
712 well->updateConnectionDFactor(simulator_, this->wellState().well(well->indexOfWell()));
715 if (Indices::waterEnabled) {
716 this->updateFiltrationModelsPostStep(dt, FluidSystem::waterPhaseIdx, local_deferredLogger);
720 this->updateInjMult(local_deferredLogger);
723 for (
const auto& well : well_container_) {
724 well->reportWellSwitching(this->wellState().well(well->indexOfWell()), local_deferredLogger);
727 if (this->terminal_output_) {
728 this->reportGroupSwitching(local_deferredLogger);
732 rateConverter_->template defineState<ElementContext>(simulator_);
736 this->updateWellPotentials(reportStepIdx,
738 simulator_.vanguard().summaryConfig(),
739 local_deferredLogger);
740 }
catch ( std::runtime_error& e ) {
741 const std::string msg =
"A zero well potential is returned for output purposes. ";
742 local_deferredLogger.
warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
745 updateWellTestState(simulationTime, this->wellTestState());
748 const Group& fieldGroup = this->schedule_.getGroup(
"FIELD", reportStepIdx);
749 this->checkGEconLimits(fieldGroup, simulationTime,
750 simulator_.episodeIndex(), local_deferredLogger);
751 this->checkGconsaleLimits(fieldGroup, this->wellState(),
752 simulator_.episodeIndex(), local_deferredLogger);
754 this->calculateProductivityIndexValues(local_deferredLogger);
756 this->commitWGState();
760 if (this->terminal_output_) {
765 this->computeWellTemperature();
769 template<
typename TypeTag>
773 unsigned elemIdx)
const
777 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
781 rate = cellRates_.at(elemIdx);
785 template<
typename TypeTag>
786 template <
class Context>
790 const Context& context,
792 unsigned timeIdx)
const
795 int elemIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
797 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
801 rate = cellRates_.at(elemIdx);
806 template<
typename TypeTag>
811 const auto pressIx = []()
813 if (Indices::oilEnabled) {
return FluidSystem::oilPhaseIdx; }
814 if (Indices::waterEnabled) {
return FluidSystem::waterPhaseIdx; }
816 return FluidSystem::gasPhaseIdx;
819 auto cellPressures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
820 auto cellTemperatures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
823 const auto& gridView = this->simulator_.vanguard().gridView();
826 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
827 elemCtx.updatePrimaryStencil(elem);
828 elemCtx.updatePrimaryIntensiveQuantities(0);
830 const auto ix = elemCtx.globalSpaceIndex(0, 0);
831 const auto& fs = elemCtx.intensiveQuantities(0, 0).fluidState();
833 cellPressures[ix] = fs.pressure(pressIx).value();
834 cellTemperatures[ix] = fs.temperature(0).value();
837 this->simulator_.vanguard().grid().comm());
839 this->wellState().init(cellPressures, cellTemperatures, this->schedule(), this->wells_ecl_,
840 this->local_parallel_well_info_, timeStepIdx,
841 &this->prevWellState(), this->well_perf_data_,
842 this->summaryState(), simulator_.vanguard().enableDistributedWells());
849 template<
typename TypeTag>
856 const int nw = this->numLocalWells();
858 well_container_.clear();
861 well_container_.reserve(nw);
863 const auto& wmatcher = this->schedule().wellMatcher(report_step);
864 const auto& wcycle = this->schedule()[report_step].wcycle.get();
868 std::for_each(this->wells_ecl_.begin(), this->wells_ecl_.end(),
869 [
this, &wg_events = this->report_step_start_events_](
const auto& well_ecl)
871 if (!well_ecl.hasConnections()) {
876 constexpr auto events_mask = ScheduleEvents::WELL_STATUS_CHANGE |
877 ScheduleEvents::REQUEST_OPEN_WELL |
878 ScheduleEvents::REQUEST_SHUT_WELL;
879 const bool well_event =
880 this->report_step_starts_ &&
881 wg_events.hasEvent(well_ecl.name(), events_mask);
890 if (well_ecl.getStatus() == WellStatus::OPEN) {
891 this->well_open_times_.insert_or_assign(well_ecl.name(),
892 this->simulator_.time());
893 this->well_close_times_.erase(well_ecl.name());
894 }
else if (well_ecl.getStatus() == WellStatus::SHUT) {
895 this->well_close_times_.insert_or_assign(well_ecl.name(),
896 this->simulator_.time());
897 this->well_open_times_.erase(well_ecl.name());
903 const auto cycle_states = wcycle.wellStatus(this->simulator_.time(),
905 this->well_open_times_,
906 this->well_close_times_);
908 for (
int w = 0; w < nw; ++w) {
909 const Well& well_ecl = this->wells_ecl_[w];
911 if (!well_ecl.hasConnections()) {
916 const std::string& well_name = well_ecl.name();
917 const auto well_status = this->schedule()
918 .getWell(well_name, report_step).getStatus();
920 const bool shut_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
921 && well_status == Well::Status::SHUT;
922 const bool open_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
923 && well_status == Well::Status::OPEN;
924 const auto& ws = this->wellState().well(well_name);
926 if (shut_event && ws.status != Well::Status::SHUT) {
927 this->closed_this_step_.insert(well_name);
928 this->wellState().shutWell(w);
929 }
else if (open_event && ws.status != Well::Status::OPEN) {
930 this->wellState().openWell(w);
934 if (this->wellTestState().well_is_closed(well_name)) {
939 const bool closed_this_step = (this->wellTestState().lastTestTime(well_name) == simulator_.time());
942 auto& events = this->wellState().well(w).events;
943 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
944 if (!closed_this_step) {
945 this->wellTestState().open_well(well_name);
946 this->wellTestState().open_completions(well_name);
947 this->well_open_times_.insert_or_assign(well_name,
948 this->simulator_.time());
949 this->well_close_times_.erase(well_name);
951 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
957 if (this->wellTestState().well_is_closed(well_name))
959 if (well_ecl.getAutomaticShutIn()) {
961 this->wellState().shutWell(w);
962 this->well_close_times_.erase(well_name);
963 this->well_open_times_.erase(well_name);
966 if (!well_ecl.getAllowCrossFlow()) {
969 this->wellState().shutWell(w);
970 this->well_close_times_.erase(well_name);
971 this->well_open_times_.erase(well_name);
975 this->wellState().stopWell(w);
980 if (!well_ecl.getAllowCrossFlow()) {
981 const bool any_zero_rate_constraint = well_ecl.isProducer()
982 ? well_ecl.productionControls(this->summaryState_).anyZeroRateConstraint()
983 : well_ecl.injectionControls(this->summaryState_).anyZeroRateConstraint();
984 if (any_zero_rate_constraint) {
986 local_deferredLogger.
debug(fmt::format(
" Well {} gets shut due to having zero rate constraint and disallowing crossflow ", well_ecl.name()) );
987 this->wellState().shutWell(w);
988 this->well_close_times_.erase(well_name);
989 this->well_open_times_.erase(well_name);
994 if (!wcycle.empty()) {
995 const auto it = cycle_states.find(well_name);
996 if (it != cycle_states.end()) {
997 if (!it->second || well_status == Well::Status::SHUT) {
999 if (well_status == Well::Status::SHUT) {
1000 this->well_open_times_.erase(well_name);
1001 this->well_close_times_.erase(well_name);
1003 this->wellState().shutWell(w);
1006 this->wellState().openWell(w);
1012 if (ws.status == Well::Status::SHUT) {
1016 well_container_.emplace_back(this->createWellPointer(w, report_step));
1018 if (ws.status == Well::Status::STOP) {
1019 well_container_.back()->stopWell();
1020 this->well_close_times_.erase(well_name);
1021 this->well_open_times_.erase(well_name);
1025 if (!wcycle.empty()) {
1026 const auto schedule_open =
1027 [&wg_events = this->report_step_start_events_](
const std::string& name)
1029 return wg_events.hasEvent(name, ScheduleEvents::REQUEST_OPEN_WELL);
1031 for (
const auto& [wname, wscale] : wcycle.efficiencyScale(this->simulator_.time(),
1032 this->simulator_.timeStepSize(),
1034 this->well_open_times_,
1037 this->wellState().updateEfficiencyScalingFactor(wname, wscale);
1038 this->schedule_.add_event(ScheduleEvents::WELLGROUP_EFFICIENCY_UPDATE, report_step);
1047 if (this->terminal_output_) {
1051 this->well_container_generic_.clear();
1052 for (
auto& w : well_container_)
1053 this->well_container_generic_.push_back(w.get());
1055 const auto& network = this->schedule()[report_step].network();
1056 if (network.active() && !this->node_pressures_.empty()) {
1057 for (
auto& well: this->well_container_generic_) {
1061 if (well->isProducer()) {
1062 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
1063 if (it != this->node_pressures_.end()) {
1066 const Scalar nodal_pressure = it->second;
1067 well->setDynamicThpLimit(nodal_pressure);
1073 this->wbp_.registerOpenWellsForWBPCalculation();
1080 template <
typename TypeTag>
1081 typename BlackoilWellModel<TypeTag>::WellInterfacePtr
1085 const auto is_multiseg = this->wells_ecl_[wellID].isMultiSegment();
1087 if (! (this->param_.use_multisegment_well_ && is_multiseg)) {
1088 return this->
template createTypedWellPointer<StandardWell<TypeTag>>(wellID, report_step);
1091 return this->
template createTypedWellPointer<MultisegmentWell<TypeTag>>(wellID, report_step);
1099 template <
typename TypeTag>
1100 template <
typename WellType>
1101 std::unique_ptr<WellType>
1106 const auto& perf_data = this->well_perf_data_[wellID];
1109 const auto pvtreg = perf_data.
empty()
1110 ? 0 : this->pvt_region_idx_[perf_data.front().cell_index];
1112 const auto& parallel_well_info = this->local_parallel_well_info_[wellID].get();
1113 const auto global_pvtreg = parallel_well_info.broadcastFirstPerforationValue(pvtreg);
1115 return std::make_unique<WellType>(this->wells_ecl_[wellID],
1119 *this->rateConverter_,
1121 this->numConservationQuantities(),
1131 template<
typename TypeTag>
1135 const int report_step,
1139 const auto it = std::find_if(this->wells_ecl_.begin(),
1140 this->wells_ecl_.end(),
1141 [&well_name](
const auto& w)
1142 { return well_name == w.name(); });
1144 if (it == this->wells_ecl_.end()) {
1146 fmt::format(
"Could not find well {} in wells_ecl ", well_name),
1150 const int pos =
static_cast<int>(std::distance(this->wells_ecl_.begin(), it));
1151 return this->createWellPointer(pos, report_step);
1156 template<
typename TypeTag>
1162 const double dt = this->simulator_.timeStepSize();
1164 auto& well_state = this->wellState();
1166 const bool changed_well_group = updateWellControlsAndNetwork(
true, dt, deferred_logger);
1167 assembleWellEqWithoutIteration(dt, deferred_logger);
1168 const bool converged = this->getWellConvergence(this->B_avg_,
true).converged() && !changed_well_group;
1171 for (
auto& well : this->well_container_) {
1172 well->solveEqAndUpdateWellState(simulator_, well_state, deferred_logger);
1175 this->simulator_.vanguard().grid().comm());
1178 const std::string msg = fmt::format(
"Initial (pre-step) network balance did not converge.");
1186 template<
typename TypeTag>
1195 this->guide_rate_handler_.setLogger(&local_deferredLogger);
1197 if (gaslift_.terminalOutput()) {
1198 const std::string msg =
1199 fmt::format(
"assemble() : iteration {}" , iterationIdx);
1200 gaslift_.gliftDebug(msg, local_deferredLogger);
1204 Dune::Timer perfTimer;
1206 this->closed_offending_wells_.clear();
1209 const int episodeIdx = simulator_.episodeIndex();
1210 const auto& network = this->schedule()[episodeIdx].network();
1211 if (!this->wellsActive() && !network.active()) {
1216 if (iterationIdx == 0 && this->wellsActive()) {
1217 OPM_TIMEBLOCK(firstIterationAssmble);
1224 calculateExplicitQuantities(local_deferredLogger);
1225 prepareTimeStep(local_deferredLogger);
1228 "assemble() failed (It=0): ",
1229 this->terminal_output_, grid().comm());
1232 const bool well_group_control_changed = updateWellControlsAndNetwork(
false, dt, local_deferredLogger);
1236 if ( ! this->wellsActive() ) {
1240 assembleWellEqWithoutIteration(dt, local_deferredLogger);
1246 last_report_.well_group_control_changed = well_group_control_changed;
1247 last_report_.assemble_time_well += perfTimer.stop();
1253 template<
typename TypeTag>
1262 bool do_network_update =
true;
1263 bool well_group_control_changed =
false;
1264 Scalar network_imbalance = 0.0;
1266 const std::size_t iteration_to_relax = param_.network_max_strict_outer_iterations_;
1268 const std::size_t max_iteration = param_.network_max_outer_iterations_;
1269 std::size_t network_update_iteration = 0;
1270 network_needs_more_balancing_force_another_newton_iteration_ =
false;
1271 while (do_network_update) {
1272 if (network_update_iteration >= max_iteration ) {
1274 const int episodeIdx = simulator_.episodeIndex();
1275 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1276 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx + 1)) {
1277 if (this->terminal_output_) {
1278 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update, \n"
1279 "and try again after the next Newton iteration (imbalance = {:.2e} bar, ctrl_change = {})",
1280 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1281 local_deferredLogger.
debug(msg);
1285 network_needs_more_balancing_force_another_newton_iteration_ =
true;
1287 if (this->terminal_output_) {
1288 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update. \n"
1289 "The simulator will continue with unconverged network results (imbalance = {:.2e} bar, ctrl_change = {})",
1290 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1291 local_deferredLogger.
info(msg);
1296 if (this->terminal_output_ && (network_update_iteration == iteration_to_relax) ) {
1297 local_deferredLogger.
debug(
"We begin using relaxed tolerance for network update now after " +
std::to_string(iteration_to_relax) +
" iterations ");
1299 const bool relax_network_balance = network_update_iteration >= iteration_to_relax;
1301 const bool optimize_gas_lift = ( (network_update_iteration + 1) < std::max(max_iteration,
static_cast<std::size_t
>(2)) );
1302 std::tie(well_group_control_changed, do_network_update, network_imbalance) =
1303 updateWellControlsAndNetworkIteration(mandatory_network_balance, relax_network_balance, optimize_gas_lift, dt,local_deferredLogger);
1304 ++network_update_iteration;
1306 return well_group_control_changed;
1312 template<
typename TypeTag>
1313 std::tuple<bool, bool, typename BlackoilWellModel<TypeTag>::Scalar>
1316 const bool relax_network_tolerance,
1317 const bool optimize_gas_lift,
1322 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1323 const int reportStepIdx = simulator_.episodeIndex();
1324 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx,
1325 param_.nupcol_group_rate_tolerance_,
true, local_deferredLogger);
1330 bool well_group_control_changed = updateWellControls(local_deferredLogger);
1331 const auto [more_inner_network_update, network_imbalance] =
1332 updateNetworks(mandatory_network_balance,
1333 local_deferredLogger,
1334 relax_network_tolerance);
1337 bool alq_updated =
false;
1340 if (optimize_gas_lift) {
1343 const bool updatePotentials = (this->shouldBalanceNetwork(reportStepIdx, iterationIdx) || mandatory_network_balance);
1344 alq_updated = gaslift_.maybeDoGasLiftOptimize(simulator_,
1346 this->node_pressures_,
1350 local_deferredLogger);
1352 prepareWellsBeforeAssembling(dt, local_deferredLogger);
1355 "updateWellControlsAndNetworkIteration() failed: ",
1356 this->terminal_output_, grid().comm());
1360 guideRateUpdateIsNeeded(reportStepIdx)) {
1361 const double simulationTime = simulator_.time();
1365 this->guide_rate_handler_.updateGuideRates(
1366 reportStepIdx, simulationTime, this->wellState(), this->groupState()
1371 const bool more_network_update = this->shouldBalanceNetwork(reportStepIdx, iterationIdx) &&
1372 (more_inner_network_update || well_group_control_changed || alq_updated);
1373 return {well_group_control_changed, more_network_update, network_imbalance};
1379 template <
typename TypeTag>
1385 const int reportStepIdx = this->simulator_.episodeIndex();
1386 const auto& network = this->schedule()[reportStepIdx].network();
1387 const auto& balance = this->schedule()[reportStepIdx].network_balance();
1388 const Scalar thp_tolerance = balance.thp_tolerance();
1390 if (!network.active()) {
1394 auto& well_state = this->wellState();
1395 auto& group_state = this->groupState();
1397 bool well_group_thp_updated =
false;
1398 for (
const std::string& nodeName : network.node_names()) {
1399 const bool has_choke = network.node(nodeName).as_choke();
1401 const auto& summary_state = this->simulator_.vanguard().summaryState();
1402 const Group& group = this->schedule().getGroup(nodeName, reportStepIdx);
1405 std::vector<Scalar> resv_coeff(Indices::numPhases, 1.0);
1406 Scalar gratTargetFromSales = 0.0;
1407 if (group_state.has_grat_sales_target(group.name()))
1408 gratTargetFromSales = group_state.grat_sales_target(group.name());
1410 const auto ctrl = group.productionControls(summary_state);
1411 auto cmode_tmp = ctrl.cmode;
1413 bool fld_none =
false;
1418 const Scalar efficiencyFactor = 1.0;
1419 const Group& parentGroup = this->schedule().getGroup(group.parent(), reportStepIdx);
1430 local_deferredLogger);
1431 target_tmp = target.first;
1432 cmode_tmp = target.second;
1434 const auto cmode = cmode_tmp;
1436 TargetCalculatorType tcalc(cmode, FluidSystem::phaseUsage(), resv_coeff,
1437 gratTargetFromSales, nodeName, group_state,
1438 group.has_gpmaint_control(cmode));
1442 target_tmp = tcalc.groupTarget(ctrl, local_deferredLogger);
1445 const Scalar orig_target = target_tmp;
1447 auto mismatch = [&] (
auto group_thp) {
1450 for (
auto& well : this->well_container_) {
1451 std::string well_name = well->name();
1452 auto& ws = well_state.well(well_name);
1453 if (group.hasWell(well_name)) {
1454 well->setDynamicThpLimit(group_thp);
1455 const Well& well_ecl = this->wells_ecl_[well->indexOfWell()];
1456 const auto inj_controls = Well::InjectionControls(0);
1457 const auto prod_controls = well_ecl.productionControls(summary_state);
1458 well->iterateWellEqWithSwitching(
1459 this->simulator_, dt, inj_controls, prod_controls, this->wgHelper(), this->wellState(), local_deferredLogger,
false,
false
1461 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
1465 return (group_rate - orig_target)/orig_target;
1468 const auto upbranch = network.uptree_branch(nodeName);
1469 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
1470 const Scalar nodal_pressure = it->second;
1471 Scalar well_group_thp = nodal_pressure;
1473 std::optional<Scalar> autochoke_thp;
1474 if (
auto iter = this->well_group_thp_calc_.find(nodeName); iter != this->well_group_thp_calc_.end()) {
1475 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
1480 std::array<Scalar, 2> range_initial;
1481 if (!autochoke_thp.has_value()){
1484 std::string node_name = nodeName;
1485 while (!network.node(node_name).terminal_pressure().has_value()) {
1486 auto branch = network.uptree_branch(node_name).value();
1487 node_name = branch.uptree_node();
1489 min_thp = network.node(node_name).terminal_pressure().value();
1490 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
1493 std::array<Scalar, 2> range = {
Scalar{0.9}*min_thp,
Scalar{1.1}*max_thp};
1494 std::optional<Scalar> appr_sol;
1495 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low1, high1, appr_sol, 0.0, local_deferredLogger);
1498 range_initial = {min_thp, max_thp};
1501 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
1503 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
1504 std::array<Scalar, 2>{
Scalar{0.9} * autochoke_thp.value(),
1505 Scalar{1.1} * autochoke_thp.value()} : range_initial;
1507 std::optional<Scalar> approximate_solution;
1508 const Scalar tolerance1 = thp_tolerance;
1509 local_deferredLogger.
debug(
"Using brute force search to bracket the group THP");
1510 const bool finding_bracket = WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low, high, approximate_solution, tolerance1, local_deferredLogger);
1512 if (approximate_solution.has_value()) {
1513 autochoke_thp = *approximate_solution;
1514 local_deferredLogger.
debug(
"Approximate group THP value found: " +
std::to_string(autochoke_thp.value()));
1515 }
else if (finding_bracket) {
1516 const Scalar tolerance2 = thp_tolerance;
1517 const int max_iteration_solve = 100;
1519 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
1520 solve(mismatch, low, high, max_iteration_solve, tolerance2, iteration);
1525 autochoke_thp.reset();
1526 local_deferredLogger.
debug(
"Group THP solve failed due to bracketing failure");
1529 if (autochoke_thp.has_value()) {
1530 well_group_thp_calc_[nodeName] = autochoke_thp.value();
1533 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
1536 for (
auto& well : this->well_container_) {
1537 std::string well_name = well->name();
1539 if (well->isInjector() || !well->wellEcl().predictionMode())
1542 if (group.hasWell(well_name)) {
1543 well->setDynamicThpLimit(well_group_thp);
1545 const auto& ws = this->wellState().well(well->indexOfWell());
1546 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
1548 well->prepareWellBeforeAssembling(
1549 this->simulator_, dt, this->wgHelper(), this->wellState(), local_deferredLogger
1555 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName) ? group_state.well_group_thp(nodeName) : 1e30;
1556 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
1557 well_group_thp_updated =
true;
1558 group_state.update_well_group_thp(nodeName, well_group_thp);
1562 return well_group_thp_updated;
1565 template<
typename TypeTag>
1571 for (
auto& well : well_container_) {
1572 well->assembleWellEq(simulator_, dt, this->wgHelper(), deferred_logger);
1577 template<
typename TypeTag>
1583 for (
auto& well : well_container_) {
1584 well->prepareWellBeforeAssembling(
1585 simulator_, dt, this->wgHelper(), this->wellState(), deferred_logger
1591 template<
typename TypeTag>
1601 for (
auto& well: well_container_) {
1602 well->assembleWellEqWithoutIteration(simulator_, dt, this->wellState(), this->groupState(),
1606 this->terminal_output_, grid().comm());
1610 template<
typename TypeTag>
1617 for (
const auto& well : well_container_) {
1618 well->addCellRates(cellRates_);
1622 template<
typename TypeTag>
1629 for (
const auto& well : well_container_) {
1630 const auto it = well_domain_map.find(well->name());
1631 if (it != well_domain_map.end() && it->second == domainIndex) {
1632 well->addCellRates(cellRates_);
1637#if COMPILE_GPU_BRIDGE
1638 template<
typename TypeTag>
1646 for(
unsigned int i = 0; i < well_container_.size(); i++){
1647 auto& well = well_container_[i];
1650 wellContribs.
addNumBlocks(derived->linSys().getNumBlocks());
1655 wellContribs.
alloc();
1657 for(
unsigned int i = 0; i < well_container_.size(); i++){
1658 auto& well = well_container_[i];
1660 auto derived_std =
dynamic_cast<StandardWell<TypeTag>*
>(well.get());
1662 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1664 auto derived_ms =
dynamic_cast<MultisegmentWell<TypeTag>*
>(well.get());
1666 derived_ms->linSys().extract(wellContribs);
1668 OpmLog::warning(
"Warning unknown type of well");
1675 template<
typename TypeTag>
1680 for (
const auto& well: well_container_ ) {
1685 template<
typename TypeTag>
1690 const bool use_well_weights)
const
1692 int nw = this->numLocalWellsEnd();
1693 int rdofs = local_num_cells_;
1694 for (
int i = 0; i < nw; i++ ) {
1695 int wdof = rdofs + i;
1696 jacobian[wdof][wdof] = 1.0;
1699 for (
const auto& well : well_container_) {
1700 well->addWellPressureEquations(jacobian,
1708 template <
typename TypeTag>
1711 const std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress)
const
1716 for (
const auto& well : well_container_) {
1717 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1720 const auto& cells = well->cells();
1721 const auto& rates = well->connectionRates();
1722 for (
unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1723 unsigned cellIdx = cells[perfIdx];
1724 auto rate = rates[perfIdx];
1727 using MatrixBlockType =
typename SparseMatrixAdapter::MatrixBlock;
1728 MatrixBlockType bMat(0.0);
1729 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1730 residual[cellIdx] += res;
1731 *diagMatAddress[cellIdx] += bMat;
1737 template<
typename TypeTag>
1742 int nw = this->numLocalWellsEnd();
1743 int rdofs = local_num_cells_;
1744 for (
int i = 0; i < nw; ++i) {
1745 int wdof = rdofs + i;
1746 jacobian.entry(wdof,wdof) = 1.0;
1748 const auto wellconnections = this->getMaxWellConnections();
1749 for (
int i = 0; i < nw; ++i) {
1750 const auto& perfcells = wellconnections[i];
1751 for (
int perfcell : perfcells) {
1752 int wdof = rdofs + i;
1753 jacobian.entry(wdof, perfcell) = 0.0;
1754 jacobian.entry(perfcell, wdof) = 0.0;
1760 template<
typename TypeTag>
1768 for (
const auto& well : well_container_) {
1769 const auto& cells = well->cells();
1770 x_local_.resize(cells.size());
1772 for (
size_t i = 0; i < cells.size(); ++i) {
1773 x_local_[i] = x[cells[i]];
1775 well->recoverWellSolutionAndUpdateWellState(simulator_, x_local_,
1776 this->wellState(), local_deferredLogger);
1780 "recoverWellSolutionAndUpdateWellState() failed: ",
1781 this->terminal_output_, simulator_.vanguard().grid().comm());
1785 template<
typename TypeTag>
1791 OPM_THROW(std::logic_error,
"Attempt to call NLDD method without a NLDD solver");
1794 return nldd_->recoverWellSolutionAndUpdateWellState(x, domainIdx);
1798 template<
typename TypeTag>
1801 getWellConvergence(
const std::vector<Scalar>& B_avg,
bool checkWellGroupControlsAndNetwork)
const
1807 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1808 for (
const auto& well : well_container_) {
1809 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1810 local_report += well->getWellConvergence(
1811 simulator_, this->wellState(), B_avg, local_deferredLogger,
1812 iterationIdx > param_.strict_outer_iter_wells_);
1816 report.
setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1817 local_report += report;
1825 if (checkWellGroupControlsAndNetwork) {
1831 if (this->terminal_output_) {
1836 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1837 OpmLog::debug(
"NaN residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1838 }
else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1839 OpmLog::debug(
"Too large residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1850 template<
typename TypeTag>
1856 for (
auto& well : well_container_) {
1865 template<
typename TypeTag>
1871 if (!this->wellsActive()) {
1874 const int episodeIdx = simulator_.episodeIndex();
1875 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1876 const auto& comm = simulator_.vanguard().grid().comm();
1878 bool changed_well_group =
false;
1879 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", episodeIdx);
1882 const std::size_t max_iter = param_.well_group_constraints_max_iterations_;
1883 while(!changed_well_group && iter < max_iter) {
1884 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1887 bool changed_well_to_group =
false;
1889 OPM_TIMEBLOCK(UpdateWellControls);
1893 for (
const auto& well : well_container_) {
1896 simulator_, mode, this->wgHelper(), this->wellState(), deferred_logger
1899 changed_well_to_group = changed_well || changed_well_to_group;
1903 simulator_.gridView().comm());
1906 changed_well_to_group = comm.sum(
static_cast<int>(changed_well_to_group));
1907 if (changed_well_to_group) {
1908 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1909 changed_well_group =
true;
1913 bool changed_well_individual =
false;
1918 for (
const auto& well : well_container_) {
1921 simulator_, mode, this->wgHelper(), this->wellState(), deferred_logger
1924 changed_well_individual = changed_well || changed_well_individual;
1928 simulator_.gridView().comm());
1931 changed_well_individual = comm.sum(
static_cast<int>(changed_well_individual));
1932 if (changed_well_individual) {
1933 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1934 changed_well_group =
true;
1940 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1942 return changed_well_group;
1946 template<
typename TypeTag>
1947 std::tuple<bool, typename BlackoilWellModel<TypeTag>::Scalar>
1951 const bool relax_network_tolerance)
1954 const int episodeIdx = simulator_.episodeIndex();
1955 const auto& network = this->schedule()[episodeIdx].network();
1956 if (!this->wellsActive() && !network.active()) {
1957 return {
false, 0.0};
1960 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1961 const auto& comm = simulator_.vanguard().grid().comm();
1964 Scalar network_imbalance = 0.0;
1965 bool more_network_update =
false;
1966 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx) || mandatory_network_balance) {
1967 OPM_TIMEBLOCK(BalanceNetwork);
1968 const double dt = this->simulator_.timeStepSize();
1970 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
1971 const int max_number_of_sub_iterations = param_.network_max_sub_iterations_;
1972 const Scalar network_pressure_update_damping_factor = param_.network_pressure_update_damping_factor_;
1973 const Scalar network_max_pressure_update = param_.network_max_pressure_update_in_bars_ * unit::barsa;
1974 bool more_network_sub_update =
false;
1975 for (
int i = 0; i < max_number_of_sub_iterations; i++) {
1976 const auto local_network_imbalance = this->updateNetworkPressures(episodeIdx, network_pressure_update_damping_factor, network_max_pressure_update);
1977 network_imbalance = comm.max(local_network_imbalance);
1978 const auto& balance = this->schedule()[episodeIdx].network_balance();
1979 constexpr Scalar relaxation_factor = 10.0;
1980 const Scalar tolerance = relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance() : balance.pressure_tolerance();
1981 more_network_sub_update = this->networkActive() && network_imbalance > tolerance;
1982 if (!more_network_sub_update)
1985 for (
const auto& well : well_container_) {
1986 if (well->isInjector() || !well->wellEcl().predictionMode())
1989 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
1990 if (it != this->node_pressures_.end()) {
1991 well->prepareWellBeforeAssembling(this->simulator_, dt, this->wgHelper(), this->wellState(), deferred_logger);
1994 this->updateAndCommunicateGroupData(episodeIdx, iterationIdx, param_.nupcol_group_rate_tolerance_,
1995 true, deferred_logger);
1997 more_network_update = more_network_sub_update || well_group_thp_updated;
1999 return { more_network_update, network_imbalance };
2003 template<
typename TypeTag>
2007 const int iterationIdx,
2010 this->updateAndCommunicateGroupData(reportStepIdx,
2012 param_.nupcol_group_rate_tolerance_,
2020 for (
const auto& well : well_container_) {
2022 const auto& ws = this->wellState().well(well->indexOfWell());
2023 if (ws.production_cmode == Well::ProducerCMode::GRUP ||
2024 ws.injection_cmode == Well::InjectorCMode::GRUP)
2026 well->updateWellStateWithTarget(
2027 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2032 simulator_.gridView().comm())
2033 this->updateAndCommunicateGroupData(reportStepIdx,
2035 param_.nupcol_group_rate_tolerance_,
2040 template<
typename TypeTag>
2045 const int reportStepIdx,
2046 const int iterationIdx)
2049 bool changed =
false;
2051 const int nupcol = this->schedule()[reportStepIdx].nupcol();
2052 const int max_number_of_group_switches = param_.max_number_of_group_switches_;
2053 const bool update_group_switching_log = iterationIdx >= nupcol;
2054 const bool changed_hc = this->checkGroupHigherConstraints(group, deferred_logger, reportStepIdx, max_number_of_group_switches, update_group_switching_log);
2057 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2060 bool changed_individual =
2062 updateGroupIndividualControl(group,
2064 max_number_of_group_switches,
2065 update_group_switching_log,
2066 this->switched_inj_groups_,
2067 this->switched_prod_groups_,
2068 this->closed_offending_wells_,
2073 if (changed_individual) {
2075 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2078 for (
const std::string& groupName : group.groups()) {
2079 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
2080 changed = changed || changed_this;
2085 template<
typename TypeTag>
2092 for (
const auto& well : well_container_) {
2093 const auto& wname = well->name();
2094 const auto wasClosed = wellTestState.well_is_closed(wname);
2095 well->checkWellOperability(simulator_,
2098 local_deferredLogger);
2099 const bool under_zero_target =
2100 well->wellUnderZeroGroupRateTarget(this->simulator_,
2102 local_deferredLogger);
2103 well->updateWellTestState(this->wellState().well(wname),
2108 local_deferredLogger);
2110 if (!wasClosed && wellTestState.well_is_closed(wname)) {
2111 this->closed_this_step_.insert(wname);
2114 const WellEconProductionLimits& econ_production_limits = well->wellEcl().getEconLimits();
2115 if (econ_production_limits.validFollowonWell()) {
2116 const auto episode_idx = simulator_.episodeIndex();
2117 const auto follow_on_well = econ_production_limits.followonWell();
2118 if (!this->schedule().hasWell(follow_on_well, episode_idx)) {
2119 const auto msg = fmt::format(
"Well {} was closed. But the given follow on well {} does not exist."
2120 "The simulator continues without opening a follow on well.",
2121 wname, follow_on_well);
2122 local_deferredLogger.
warning(msg);
2124 auto& ws = this->wellState().well(follow_on_well);
2125 const bool success = ws.updateStatus(WellStatus::OPEN);
2127 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} opens instead.", wname, follow_on_well);
2128 local_deferredLogger.
info(msg);
2130 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} is already open.", wname, follow_on_well);
2131 local_deferredLogger.
warning(msg);
2138 for (
const auto& [group_name, to] : this->closed_offending_wells_) {
2139 if (this->hasOpenLocalWell(to.second) &&
2140 !this->wasDynamicallyShutThisTimeStep(to.second))
2142 wellTestState.close_well(to.second,
2143 WellTestConfig::Reason::GROUP,
2145 this->updateClosedWellsThisStep(to.second);
2146 const std::string msg =
2147 fmt::format(
"Procedure on exceeding {} limit is WELL for group {}. "
2153 local_deferredLogger.
info(msg);
2161 if (this->terminal_output_) {
2167 template<
typename TypeTag>
2171 std::string& exc_msg,
2176 const int np = this->numPhases();
2177 std::vector<Scalar> potentials;
2178 const auto& well = well_container_[widx];
2179 std::string cur_exc_msg;
2182 well->computeWellPotentials(simulator_, well_state_copy, this->wgHelper(), potentials, deferred_logger);
2187 exc_msg += fmt::format(
"\nFor well {}: {}", well->name(), cur_exc_msg);
2189 exc_type = std::max(exc_type, cur_exc_type);
2193 auto& ws = this->wellState().well(well->indexOfWell());
2194 for (
int p = 0; p < np; ++p) {
2196 ws.well_potentials[p] = std::max(
Scalar{0.0}, potentials[p]);
2202 template <
typename TypeTag>
2207 for (
const auto& wellPtr : this->well_container_) {
2208 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2216 template <
typename TypeTag>
2227 for (
const auto& shutWell : this->local_shut_wells_) {
2228 if (!this->wells_ecl_[shutWell].hasConnections()) {
2233 auto wellPtr = this->
template createTypedWellPointer
2236 wellPtr->
init(this->depth_, this->gravity_, this->B_avg_,
true);
2238 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2246 template <
typename TypeTag>
2260 template<
typename TypeTag>
2266 const auto episodeIdx = simulator_.episodeIndex();
2267 this->updateNetworkActiveState(episodeIdx);
2271 const bool do_prestep_network_rebalance = param_.pre_solve_network_ && this->needPreStepNetworkRebalance(episodeIdx);
2273 for (
const auto& well : well_container_) {
2274 auto& events = this->wellState().well(well->indexOfWell()).events;
2276 well->updateWellStateWithTarget(
2277 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2279 well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger);
2285 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2286 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2289 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2291 well->solveWellEquation(
2292 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2294 }
catch (
const std::exception& e) {
2295 const std::string msg =
"Compute initial well solution for " + well->name() +
" initially failed. Continue with the previous rates";
2296 deferred_logger.
warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
2301 well->resetWellOperability();
2303 updatePrimaryVariables(deferred_logger);
2306 if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger);
2309 template<
typename TypeTag>
2314 std::vector< Scalar > B_avg(numConservationQuantities(),
Scalar() );
2315 const auto& grid = simulator_.vanguard().grid();
2316 const auto& gridView = grid.leafGridView();
2320 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2321 elemCtx.updatePrimaryStencil(elem);
2322 elemCtx.updatePrimaryIntensiveQuantities(0);
2324 const auto& intQuants = elemCtx.intensiveQuantities(0, 0);
2325 const auto& fs = intQuants.fluidState();
2327 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2329 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2333 const unsigned compIdx = FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
2334 auto& B = B_avg[ compIdx ];
2336 B += 1 / fs.invB(phaseIdx).value();
2338 if constexpr (has_solvent_) {
2339 auto& B = B_avg[solventSaturationIdx];
2340 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2346 grid.comm().sum(B_avg.data(), B_avg.size());
2347 B_avg_.resize(B_avg.size());
2348 std::transform(B_avg.begin(), B_avg.end(), B_avg_.begin(),
2349 [gcells = global_num_cells_](
const auto bval)
2350 { return bval / gcells; });
2357 template<
typename TypeTag>
2362 for (
const auto& well : well_container_) {
2367 template<
typename TypeTag>
2371 const auto& grid = simulator_.vanguard().
grid();
2372 const auto& eclProblem = simulator_.problem();
2373 const unsigned numCells = grid.size(0);
2375 this->pvt_region_idx_.resize(numCells);
2376 for (
unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2377 this->pvt_region_idx_[cellIdx] =
2378 eclProblem.pvtRegionIndex(cellIdx);
2383 template<
typename TypeTag>
2396 return this->numPhases() + has_solvent_;
2399 template<
typename TypeTag>
2403 const auto& eclProblem = simulator_.problem();
2404 depth_.resize(local_num_cells_);
2405 for (
unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2406 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2410 template<
typename TypeTag>
2413 getWell(
const std::string& well_name)
const
2416 auto well = std::find_if(well_container_.begin(),
2417 well_container_.end(),
2419 return elem->name() == well_name;
2422 assert(well != well_container_.end());
2427 template <
typename TypeTag>
2432 return std::max(this->simulator_.episodeIndex(), 0);
2439 template<
typename TypeTag>
2444 const std::vector<Scalar>& production_rates,
2445 std::vector<Scalar>& resv_coeff)
2447 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2450 template<
typename TypeTag>
2455 std::vector<Scalar>& resv_coeff)
2457 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2461 template <
typename TypeTag>
2466 if constexpr (has_energy_) {
2467 int np = this->numPhases();
2468 Scalar cellInternalEnergy;
2472 const int nw = this->numLocalWells();
2473 for (
auto wellID = 0*nw; wellID < nw; ++wellID) {
2474 const Well& well = this->wells_ecl_[wellID];
2475 auto& ws = this->wellState().well(wellID);
2476 if (well.isInjector()) {
2477 if (ws.status != WellStatus::STOP) {
2478 this->wellState().well(wellID).temperature = well.inj_temperature();
2483 std::array<Scalar,2> weighted{0.0,0.0};
2484 auto& [weighted_temperature, total_weight] = weighted;
2486 auto& well_info = this->local_parallel_well_info_[wellID].get();
2487 auto& perf_data = ws.perf_data;
2488 auto& perf_phase_rate = perf_data.phase_rates;
2490 using int_type =
decltype(this->well_perf_data_[wellID].size());
2491 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2492 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2493 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, 0);
2494 const auto& fs = intQuants.fluidState();
2497 Scalar cellTemperatures = fs.temperature(0).value();
2499 Scalar weight_factor = 0.0;
2500 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
2501 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2504 cellInternalEnergy = fs.enthalpy(phaseIdx).value() -
2505 fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2506 cellBinv = fs.invB(phaseIdx).value();
2507 cellDensity = fs.density(phaseIdx).value();
2508 perfPhaseRate = perf_phase_rate[perf*np + phaseIdx];
2509 weight_factor += cellDensity * perfPhaseRate / cellBinv * cellInternalEnergy / cellTemperatures;
2511 weight_factor = std::abs(weight_factor) + 1e-13;
2512 total_weight += weight_factor;
2513 weighted_temperature += weight_factor * cellTemperatures;
2515 well_info.communication().sum(weighted.data(), 2);
2516 this->wellState().well(wellID).temperature = weighted_temperature / total_weight;
2522 template <
typename TypeTag>
2526 const auto reportStepIdx =
static_cast<unsigned int>(this->reportStepIndex());
2527 const auto& trMod = this->simulator_.problem().tracerModel();
2533 this->assignMswTracerRates(wsrpt, trMod.getMswTracerRates(), reportStepIdx);
#define OPM_END_PARALLEL_TRY_CATCH_LOG(obptc_logger, obptc_prefix, obptc_output, comm)
Catch exception, log, and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:202
#define OPM_DEFLOG_THROW(Exception, message, deferred_logger)
Definition: DeferredLoggingErrorHelpers.hpp:45
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:192
#define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg)
Inserts catch classes for the parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:166
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:158
void logAndCheckForExceptionsAndThrow(Opm::DeferredLogger &deferred_logger, Opm::ExceptionType::ExcEnum exc_type, const std::string &message, const bool terminal_output, Opm::Parallel::Communication comm)
Definition: DeferredLoggingErrorHelpers.hpp:111
Class for handling constraints for the blackoil well model.
Definition: BlackoilWellModelConstraints.hpp:42
Class for handling the gaslift in the blackoil well model.
Definition: BlackoilWellModelGasLift.hpp:96
Class for handling the blackoil well model.
Definition: BlackoilWellModelGeneric.hpp:95
const Parallel::Communication & comm() const
Definition: BlackoilWellModelGeneric.hpp:236
BlackoilWellModelWBP< GetPropType< TypeTag, Properties::Scalar >, GetPropType< TypeTag, Properties::FluidSystem >::IndexTraitsType > wbp_
Definition: BlackoilWellModelGeneric.hpp:530
std::vector< ParallelWellInfo< GetPropType< TypeTag, Properties::Scalar > > > parallel_well_info_
Definition: BlackoilWellModelGeneric.hpp:559
Class for handling the guide rates in the blackoil well model.
Definition: BlackoilWellModelGuideRates.hpp:47
Class for handling the blackoil well model.
Definition: BlackoilWellModel.hpp:103
void initializeGroupStructure(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:297
void init()
Definition: BlackoilWellModel_impl.hpp:163
const Simulator & simulator() const
Definition: BlackoilWellModel.hpp:379
std::vector< Scalar > depth_
Definition: BlackoilWellModel.hpp:458
std::size_t global_num_cells_
Definition: BlackoilWellModel.hpp:454
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModel.hpp:112
void initWellContainer(const int reportStepIdx) override
Definition: BlackoilWellModel_impl.hpp:182
void beginReportStep(const int time_step)
Definition: BlackoilWellModel_impl.hpp:199
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: BlackoilWellModel.hpp:108
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:133
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: BlackoilWellModel.hpp:109
GetPropType< TypeTag, Properties::Grid > Grid
Definition: BlackoilWellModel.hpp:106
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModel.hpp:111
void initializeWellState(const int timeStepIdx)
Definition: BlackoilWellModel_impl.hpp:809
const Grid & grid() const
Definition: BlackoilWellModel.hpp:376
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:685
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1678
std::optional< ReservoirCoupling::ScopedLoggerGuard > setupRescoupScopedLogger(DeferredLogger &local_logger)
Setup RAII guard for reservoir coupling logger.
void sendSlaveGroupDataToMaster()
Send comprehensive slave group data to master.
void receiveSlaveGroupData()
Receive comprehensive slave group data from slaves.
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > PressureMatrix
Definition: BlackoilWellModel.hpp:298
bool empty() const
Definition: BlackoilWellModel.hpp:343
void computeTotalRatesForDof(RateVector &rate, unsigned globalIdx) const
Definition: BlackoilWellModel_impl.hpp:772
void beginTimeStep()
Definition: BlackoilWellModel_impl.hpp:333
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: BlackoilWellModel.hpp:113
void calculateExplicitQuantities(DeferredLogger &deferred_logger) const
Definition: BlackoilWellModel_impl.hpp:1853
void updatePrimaryVariables(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2360
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:252
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:134
BlackoilWellModel(Simulator &simulator)
Definition: BlackoilWellModel_impl.hpp:78
void wellTesting(const int timeStepIdx, const double simulationTime, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:597
typename FluidSystem::IndexTraitsType IndexTraits
Definition: BlackoilWellModel.hpp:119
std::size_t local_num_cells_
Definition: BlackoilWellModel.hpp:456
bool alternative_well_rate_init_
Definition: BlackoilWellModel.hpp:459
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:695
Simulator & simulator_
Definition: BlackoilWellModel.hpp:428
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:852
std::unique_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:191
int compressedIndexForInterior(int cartesian_cell_idx) const override
get compressed index for interior cells (-1, otherwise
Definition: BlackoilWellModel.hpp:351
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:668
void initializeSources(typename ParallelWBPCalculation< Scalar >::GlobalToLocal index, typename ParallelWBPCalculation< Scalar >::Evaluator eval)
Definition: ConvergenceReport.hpp:38
void setWellFailed(const WellFailure &wf)
Definition: ConvergenceReport.hpp:272
void setWellGroupTargetsViolated(const bool wellGroupTargetsViolated)
Definition: ConvergenceReport.hpp:290
const std::vector< WellFailure > & wellFailures() const
Definition: ConvergenceReport.hpp:380
void setNetworkNotYetBalancedForceAnotherNewtonIteration(const bool network_needs_more_balancing_force_another_newton_iteration)
Definition: ConvergenceReport.hpp:295
Definition: DeferredLogger.hpp:57
void info(const std::string &tag, const std::string &message)
void warning(const std::string &tag, const std::string &message)
void debug(const std::string &tag, const std::string &message)
std::map< std::string, std::pair< const Well *, int > > GLiftEclWells
Definition: GasLiftGroupInfo.hpp:64
Guard for managing DeferredLogger lifecycle in ReservoirCoupling.
Definition: ReservoirCoupling.hpp:75
Definition: StandardWell.hpp:60
virtual void init(const std::vector< Scalar > &depth_arg, const Scalar gravity_arg, const std::vector< Scalar > &B_avg, const bool changed_to_open_this_step) override
Definition: StandardWell_impl.hpp:76
Definition: TargetCalculator.hpp:43
Class for computing BHP limits.
Definition: WellBhpThpCalculator.hpp:41
Definition: WellContributions.hpp:51
void alloc()
Allocate memory for the StandardWells.
void setBlockSize(unsigned int dim, unsigned int dim_wells)
void addNumBlocks(unsigned int numBlocks)
Class for computing well group controls.
Definition: WellGroupControls.hpp:49
int indexOfWell() const
Index of well in the wells struct and wellState.
Definition: WellInterface.hpp:77
bool updateWellControl(const Simulator &simulator, const IndividualOrGroup iog, const WellGroupHelperType &wgHelper, WellStateType &well_state, DeferredLogger &deferred_logger)
Definition: WellInterface_impl.hpp:189
virtual void updateProductivityIndex(const Simulator &simulator, const WellProdIndexCalculator< Scalar > &wellPICalc, WellStateType &well_state, DeferredLogger &deferred_logger) const =0
Definition: WellState.hpp:66
ExcEnum
Definition: DeferredLogger.hpp:45
@ NONE
Definition: DeferredLogger.hpp:46
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilbioeffectsmodules.hh:43
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Parallel::Communication communicator)
Create a global log combining local logs.
ConvergenceReport gatherConvergenceReport(const ConvergenceReport &local_report, Parallel::Communication communicator)
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:34