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>
34#include <opm/input/eclipse/Schedule/Network/Balance.hpp>
35#include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
36#include <opm/input/eclipse/Schedule/Well/PAvgDynamicSourceData.hpp>
37#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
38#include <opm/input/eclipse/Schedule/Well/WellTestConfig.hpp>
39#include <opm/input/eclipse/Schedule/Well/WellEconProductionLimits.hpp>
41#include <opm/input/eclipse/Units/UnitSystem.hpp>
51#ifdef RESERVOIR_COUPLING_ENABLED
74#include <fmt/format.h>
77 template<
typename TypeTag>
84 simulator.vanguard().summaryState(),
85 simulator.vanguard().eclState(),
87 simulator.gridView().comm())
88 , simulator_(simulator)
89 , guide_rate_handler_{
91 simulator.vanguard().schedule(),
92 simulator.vanguard().summaryState(),
93 simulator.vanguard().grid().comm()
95 , gaslift_(this->terminal_output_)
104 auto& parallel_wells =
simulator.vanguard().parallelWells();
107 for(
const auto& name_bool : parallel_wells) {
113 Parameters::Get<Parameters::AlternativeWellRateInit>();
115 using SourceDataSpan =
116 typename PAvgDynamicSourceData<Scalar>::template SourceDataSpan<Scalar>;
119 [
this](
const std::size_t globalIndex)
121 [
this](
const int localCell, SourceDataSpan sourceTerms)
123 using Item =
typename SourceDataSpan::Item;
125 const auto* intQuants = this->
simulator_.model()
126 .cachedIntensiveQuantities(localCell, 0);
127 const auto& fs = intQuants->fluidState();
130 .set(Item::PoreVol, intQuants->porosity().value() *
131 this->
simulator_.model().dofTotalVolume(localCell))
132 .set(Item::Depth, this->
depth_[localCell]);
134 constexpr auto io = FluidSystem::oilPhaseIdx;
135 constexpr auto ig = FluidSystem::gasPhaseIdx;
136 constexpr auto iw = FluidSystem::waterPhaseIdx;
139 const auto haveOil = FluidSystem::phaseIsActive(io);
140 const auto haveGas = FluidSystem::phaseIsActive(ig);
141 const auto haveWat = FluidSystem::phaseIsActive(iw);
143 auto weightedPhaseDensity = [&fs](
const auto ip)
145 return fs.saturation(ip).value() * fs.density(ip).value();
148 if (haveOil) { sourceTerms.set(Item::Pressure, fs.pressure(io).value()); }
149 else if (haveGas) { sourceTerms.set(Item::Pressure, fs.pressure(ig).value()); }
150 else { sourceTerms.set(Item::Pressure, fs.pressure(iw).value()); }
154 if (haveOil) { rho += weightedPhaseDensity(io); }
155 if (haveGas) { rho += weightedPhaseDensity(ig); }
156 if (haveWat) { rho += weightedPhaseDensity(iw); }
158 sourceTerms.set(Item::MixtureDensity, rho);
163 template<
typename TypeTag>
168 extractLegacyCellPvtRegionIndex_();
169 extractLegacyDepth_();
171 gravity_ = simulator_.problem().gravity()[2];
173 this->initial_step_ =
true;
176 simulator_.model().addAuxiliaryModule(
this);
178 is_cell_perforated_.resize(local_num_cells_,
false);
182 template<
typename TypeTag>
187 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
188 + ScheduleEvents::NEW_WELL;
189 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
190 for (
auto& wellPtr : this->well_container_) {
191 const bool well_opened_this_step = this->report_step_starts_ &&
192 events.hasEvent(wellPtr->name(),
193 effective_events_mask);
194 wellPtr->init(this->depth_, this->gravity_,
195 this->B_avg_, well_opened_this_step);
199 template<
typename TypeTag>
204 this->groupStateHelper().setReportStep(timeStepIdx);
205 this->report_step_starts_ =
true;
206 this->report_step_start_events_ = this->schedule()[timeStepIdx].wellgroup_events();
208 this->rateConverter_ = std::make_unique<RateConverterType>
209 (std::vector<int>(this->local_num_cells_, 0));
213 const auto enableWellPIScaling =
true;
214 this->initializeLocalWellStructure(timeStepIdx, enableWellPIScaling);
217 this->initializeGroupStructure(timeStepIdx);
219 const auto& comm = this->simulator_.vanguard().grid().comm();
225 this->rateConverter_->template defineState<ElementContext>(this->simulator_);
229 const auto& sched_state = this->schedule()[timeStepIdx];
231 this->vfp_properties_ = std::make_unique<VFPProperties<Scalar, IndexTraits>>
232 (sched_state.vfpinj(), sched_state.vfpprod(), this->wellState());
239 this->commitWGState();
241 this->wellStructureChangedDynamically_ =
false;
248 template <
typename TypeTag>
252 const bool enableWellPIScaling)
254 auto logger_guard = this->groupStateHelper().pushLogger();
255 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
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>
299 const auto& comm = this->simulator_.vanguard().grid().comm();
303 const auto& fieldGroup =
304 this->schedule().getGroup(
"FIELD", reportStepIdx);
306 this->groupStateHelper().setCmodeGroup(fieldGroup);
310 if (this->schedule()[reportStepIdx].has_gpmaint()) {
311 this->groupStateHelper().setRegionAveragePressureCalculator(
313 this->eclState_.fieldProps(),
314 this->regionalAveragePressureCalculator_
326 template<
typename TypeTag>
331 OPM_TIMEBLOCK(beginTimeStep);
333 this->updateAverageFormationFactor();
335 auto logger_guard = this->groupStateHelper().pushLogger();
336 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
338#ifdef RESERVOIR_COUPLING_ENABLED
339 auto rescoup_logger_guard = this->setupRescoupScopedLogger(local_deferredLogger);
342 this->switched_prod_groups_.clear();
343 this->switched_inj_groups_.clear();
345 if (this->wellStructureChangedDynamically_) {
350 const auto reportStepIdx =
351 this->simulator_.episodeIndex();
355 const auto enableWellPIScaling =
false;
357 this->initializeLocalWellStructure(reportStepIdx, enableWellPIScaling);
358 this->initializeGroupStructure(reportStepIdx);
360 this->commitWGState();
366 this->wellStructureChangedDynamically_ =
false;
369 this->resetWGState();
370 const int reportStepIdx = simulator_.episodeIndex();
372 this->wellState().updateWellsDefaultALQ(this->schedule(), reportStepIdx, this->summaryState());
373 this->wellState().gliftTimeStepInit();
375 const double simulationTime = simulator_.time();
376 const auto& iterCtx = simulator_.problem().iterationContext();
380 wellTesting(reportStepIdx, simulationTime, local_deferredLogger);
383 createWellContainer(reportStepIdx);
385#ifdef RESERVOIR_COUPLING_ENABLED
386 if (this->isReservoirCouplingMaster()) {
387 if (this->reservoirCouplingMaster().isFirstSubstepOfSyncTimestep()) {
388 this->receiveSlaveGroupData();
395 this->updateAndCommunicateGroupData(reportStepIdx,
397 param_.nupcol_group_rate_tolerance_,
false);
400 const Grid& grid = simulator_.vanguard().grid();
401 this->wells_active_ = grid.comm().max(!this->well_container_.empty());
406 this->initWellContainer(reportStepIdx);
409 std::fill(is_cell_perforated_.begin(), is_cell_perforated_.end(),
false);
410 for (
auto& well : well_container_) {
411 well->updatePerforatedCell(is_cell_perforated_);
415 this->calculateEfficiencyFactors(reportStepIdx);
417 if constexpr (has_polymer_)
419 if (PolymerModule::hasPlyshlog() || getPropValue<TypeTag, Properties::EnablePolymerMW>() ) {
420 this->setRepRadiusPerfLength();
427 this->terminal_output_, simulator_.vanguard().grid().comm());
429 for (
auto& well : well_container_) {
430 well->setVFPProperties(this->vfp_properties_.get());
431 well->setGuideRate(&this->guideRate_);
434 this->updateFiltrationModelsPreStep(local_deferredLogger);
437 for (
auto& well : well_container_) {
438 well->closeCompletions(this->wellTestState());
444 if (alternative_well_rate_init_) {
449 for (
const auto& well : well_container_) {
450 if (well->isProducer() && !well->wellIsStopped()) {
451 well->initializeProducerWellState(simulator_, this->wellState(), local_deferredLogger);
456 for (
const auto& well : well_container_) {
457 if (well->isVFPActive(local_deferredLogger)){
458 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
462 this->updateWellPotentials(reportStepIdx,
464 simulator_.vanguard().summaryConfig(),
465 local_deferredLogger);
466 }
catch ( std::runtime_error& e ) {
467 const std::string msg =
"A zero well potential is returned for output purposes. ";
468 local_deferredLogger.warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
471 this->guide_rate_handler_.updateGuideRates(
472 reportStepIdx, simulationTime, this->wellState(), this->groupState()
474 bool slave_needs_well_solution =
false;
475#ifdef RESERVOIR_COUPLING_ENABLED
476 if (this->isReservoirCouplingSlave()) {
477 if (this->reservoirCouplingSlave().isFirstSubstepOfSyncTimestep()) {
478 this->sendSlaveGroupDataToMaster();
479 this->receiveGroupConstraintsFromMaster();
480 this->groupStateHelper().updateSlaveGroupCmodesFromMaster();
481 this->reservoirCouplingSlave().markSlaveGroupsInSchedule(
482 this->schedule_, reportStepIdx);
483 slave_needs_well_solution =
true;
490 if (this->schedule_[reportStepIdx].has_gpmaint()) {
491 for (
const auto& calculator : regionalAveragePressureCalculator_) {
492 calculator.second->template defineState<ElementContext>(simulator_);
494 const double dt = simulator_.timeStepSize();
495 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", reportStepIdx);
497 this->groupStateHelper().updateGpMaintTargetForGroups(fieldGroup,
498 regionalAveragePressureCalculator_,
504 this->updateAndCommunicateGroupData(reportStepIdx,
506 param_.nupcol_group_rate_tolerance_,
510 for (
auto& well : well_container_) {
511 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
512 + ScheduleEvents::INJECTION_TYPE_CHANGED
513 + ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER
514 + ScheduleEvents::NEW_WELL;
516 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
517 const bool event = this->report_step_starts_ && events.hasEvent(well->name(), effective_events_mask);
518 const bool dyn_status_change = this->wellState().well(well->name()).status
519 != this->prevWellState().well(well->name()).status;
521 if (event || dyn_status_change || slave_needs_well_solution) {
523 well->scaleSegmentRatesAndPressure(this->wellState());
524 well->calculateExplicitQuantities(simulator_, this->groupStateHelper());
525 well->updateWellStateWithTarget(simulator_, this->groupStateHelper(), this->wellState());
526 well->updatePrimaryVariables(this->groupStateHelper());
527 well->solveWellEquation(
528 simulator_, this->groupStateHelper(), this->wellState()
530 }
catch (
const std::exception& e) {
531 const std::string msg =
"Compute initial well solution for new well " + well->name() +
" failed. Continue with zero initial rates";
532 local_deferredLogger.warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
540#ifdef RESERVOIR_COUPLING_ENABLED
541 if (this->isReservoirCouplingSlave()) {
542 if (slave_needs_well_solution) {
543 this->updateAndCommunicateGroupData(reportStepIdx,
545 param_.nupcol_group_rate_tolerance_,
547 this->sendSlaveGroupDataToMaster();
552#ifdef RESERVOIR_COUPLING_ENABLED
553 if (this->isReservoirCouplingMaster()) {
554 if (this->reservoirCouplingMaster().isFirstSubstepOfSyncTimestep()) {
555 this->sendMasterGroupConstraintsToSlaves();
556 this->receiveSlaveGroupData();
562 const std::string msg =
"Compute initial well solution for new wells failed. Continue with zero initial rates";
563 local_deferredLogger.warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
566 const auto& comm = simulator_.vanguard().grid().comm();
568 exc_type,
"beginTimeStep() failed: " + exc_msg, this->terminal_output_, comm);
572#ifdef RESERVOIR_COUPLING_ENABLED
582 template<
typename TypeTag>
583 std::optional<ReservoirCoupling::ScopedLoggerGuard>
586 if (this->isReservoirCouplingMaster()) {
588 this->reservoirCouplingMaster().logger(),
591 }
else if (this->isReservoirCouplingSlave()) {
592 return ReservoirCoupling::ScopedLoggerGuard{
593 this->reservoirCouplingSlave().logger(),
600 template<
typename TypeTag>
602 BlackoilWellModel<TypeTag>::
603 receiveSlaveGroupData()
605 assert(this->isReservoirCouplingMaster());
606 RescoupReceiveSlaveGroupData<Scalar, IndexTraits> slave_group_data_receiver{
607 this->groupStateHelper(),
609 slave_group_data_receiver.receiveSlaveGroupData();
612 template<
typename TypeTag>
614 BlackoilWellModel<TypeTag>::
615 sendSlaveGroupDataToMaster()
617 assert(this->isReservoirCouplingSlave());
618 RescoupSendSlaveGroupData<Scalar, IndexTraits> slave_group_data_sender{this->groupStateHelper()};
619 slave_group_data_sender.sendSlaveGroupDataToMaster();
622 template<
typename TypeTag>
624 BlackoilWellModel<TypeTag>::
625 sendMasterGroupConstraintsToSlaves()
628 RescoupConstraintsCalculator<Scalar, IndexTraits> constraints_calculator{
629 this->guide_rate_handler_,
630 this->groupStateHelper()
632 constraints_calculator.calculateMasterGroupConstraintsAndSendToSlaves();
635 template<
typename TypeTag>
637 BlackoilWellModel<TypeTag>::
638 receiveGroupConstraintsFromMaster()
640 RescoupReceiveGroupConstraints<Scalar, IndexTraits> constraint_receiver{
641 this->guide_rate_handler_,
642 this->groupStateHelper()
644 constraint_receiver.receiveGroupConstraintsFromMaster();
647 template<
typename TypeTag>
649 BlackoilWellModel<TypeTag>::
650 rescoupSyncSummaryData()
662 if (this->isReservoirCouplingMaster()) {
663 if (this->reservoirCouplingMaster().needsSlaveDataReceive()) {
664 this->receiveSlaveGroupData();
665 this->reservoirCouplingMaster().setNeedsSlaveDataReceive(
false);
668 if (this->isReservoirCouplingSlave()) {
669 if (this->reservoirCouplingSlave().isLastSubstepOfSyncTimestep()) {
670 this->sendSlaveGroupDataToMaster();
677 template<
typename TypeTag>
680 const double simulationTime,
683 for (
const std::string& well_name : this->getWellsForTesting(timeStepIdx, simulationTime)) {
684 const Well& wellEcl = this->schedule().getWell(well_name, timeStepIdx);
685 if (wellEcl.getStatus() == Well::Status::SHUT)
688 WellInterfacePtr well = createWellForWellTest(well_name, timeStepIdx, deferred_logger);
690 well->init(depth_, gravity_, B_avg_,
true);
692 Scalar well_efficiency_factor = wellEcl.getEfficiencyFactor() *
693 this->wellState().getGlobalEfficiencyScalingFactor(well_name);
694 this->groupStateHelper().accumulateGroupEfficiencyFactor(
695 this->schedule().getGroup(wellEcl.groupName(), timeStepIdx),
696 well_efficiency_factor
699 well->setWellEfficiencyFactor(well_efficiency_factor);
700 well->setVFPProperties(this->vfp_properties_.get());
701 well->setGuideRate(&this->guideRate_);
704 if (well->isProducer() && alternative_well_rate_init_) {
705 well->initializeProducerWellState(simulator_, this->wellState(), deferred_logger);
707 if (well->isVFPActive(deferred_logger)) {
708 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
711 const auto& network = this->schedule()[timeStepIdx].network();
712 if (network.active()) {
713 this->network_.initializeWell(*well);
717 GLiftEclWells ecl_well_map;
718 gaslift_.initGliftEclWellMap(well_container_, ecl_well_map);
719 well->wellTesting(simulator_,
721 this->groupStateHelper(),
723 this->wellTestState(),
725 this->well_open_times_);
726 }
catch (
const std::exception& e) {
727 const std::string msg =
728 fmt::format(fmt::runtime(
"Exception during testing of well: {}. The well will not open.\n"
729 "Exception message: {}"), wellEcl.name(), e.what());
730 deferred_logger.
warning(
"WELL_TESTING_FAILED", msg);
736 template<
typename TypeTag>
742 for (
auto&& pinfo : this->local_parallel_well_info_)
753 template<
typename TypeTag>
763 template<
typename TypeTag>
768 this->closed_this_step_.clear();
771 this->report_step_starts_ =
false;
772 const int reportStepIdx = simulator_.episodeIndex();
774 auto logger_guard = this->groupStateHelper().pushLogger();
775 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
776 for (
const auto& well : well_container_) {
777 if (getPropValue<TypeTag, Properties::EnablePolymerMW>() && well->isInjector()) {
778 well->updateWaterThroughput(dt, this->wellState());
782 for (
const auto& well : well_container_) {
783 well->updateConnectionTransmissibilityFactor(simulator_, this->wellState().well(well->indexOfWell()));
784 well->updateConnectionDFactor(simulator_, this->wellState().well(well->indexOfWell()));
787 if (Indices::waterEnabled) {
788 this->updateFiltrationModelsPostStep(dt, FluidSystem::waterPhaseIdx, local_deferredLogger);
792 this->updateInjMult(local_deferredLogger);
795 for (
const auto& well : well_container_) {
796 well->reportWellSwitching(this->wellState().well(well->indexOfWell()), local_deferredLogger);
799 if (this->terminal_output_) {
800 this->reportGroupSwitching(local_deferredLogger);
804 rateConverter_->template defineState<ElementContext>(simulator_);
808 this->updateWellPotentials(reportStepIdx,
810 simulator_.vanguard().summaryConfig(),
811 local_deferredLogger);
812 }
catch ( std::runtime_error& e ) {
813 const std::string msg =
"A zero well potential is returned for output purposes. ";
814 local_deferredLogger.warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
817 updateWellTestState(simulationTime, this->wellTestState());
820 const Group& fieldGroup = this->schedule_.getGroup(
"FIELD", reportStepIdx);
821 this->checkGEconLimits(fieldGroup, simulationTime,
822 simulator_.episodeIndex(), local_deferredLogger);
823 this->checkGconsaleLimits(fieldGroup, this->wellState(),
824 simulator_.episodeIndex(), local_deferredLogger);
826 this->calculateProductivityIndexValues(local_deferredLogger);
828 this->groupStateHelper().updateNONEProductionGroups();
830#ifdef RESERVOIR_COUPLING_ENABLED
831 this->rescoupSyncSummaryData();
833 this->commitWGState();
836 this->computeWellTemperature();
840 template<
typename TypeTag>
844 unsigned elemIdx)
const
848 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
852 rate = cellRates_.at(elemIdx);
856 template<
typename TypeTag>
857 template <
class Context>
861 const Context& context,
863 unsigned timeIdx)
const
866 int elemIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
868 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
872 rate = cellRates_.at(elemIdx);
877 template<
typename TypeTag>
882 const auto pressIx = []()
884 if (Indices::oilEnabled) {
return FluidSystem::oilPhaseIdx; }
885 if (Indices::waterEnabled) {
return FluidSystem::waterPhaseIdx; }
887 return FluidSystem::gasPhaseIdx;
890 auto cellPressures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
891 auto cellTemperatures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
894 const auto& gridView = this->simulator_.vanguard().gridView();
897 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
898 elemCtx.updatePrimaryStencil(elem);
899 elemCtx.updatePrimaryIntensiveQuantities(0);
901 const auto ix = elemCtx.globalSpaceIndex(0, 0);
902 const auto& fs = elemCtx.intensiveQuantities(0, 0).fluidState();
904 cellPressures[ix] = fs.pressure(pressIx).value();
905 cellTemperatures[ix] = fs.temperature(0).value();
908 this->simulator_.vanguard().grid().comm());
910 this->wellState().init(cellPressures, cellTemperatures, this->schedule(), this->wells_ecl_,
911 this->local_parallel_well_info_, timeStepIdx,
912 &this->prevWellState(), this->well_perf_data_,
913 this->summaryState(), simulator_.vanguard().enableDistributedWells());
920 template<
typename TypeTag>
925 auto logger_guard = this->groupStateHelper().pushLogger();
926 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
928 const int nw = this->numLocalWells();
930 well_container_.clear();
933 well_container_.reserve(nw);
935 const auto& wmatcher = this->schedule().wellMatcher(report_step);
936 const auto& wcycle = this->schedule()[report_step].wcycle.get();
940 std::ranges::for_each(this->wells_ecl_,
941 [
this, &wg_events = this->report_step_start_events_](
const auto& well_ecl)
943 if (!well_ecl.hasConnections()) {
948 constexpr auto events_mask = ScheduleEvents::WELL_STATUS_CHANGE |
949 ScheduleEvents::REQUEST_OPEN_WELL |
950 ScheduleEvents::REQUEST_SHUT_WELL;
951 const bool well_event =
952 this->report_step_starts_ &&
953 wg_events.hasEvent(well_ecl.name(), events_mask);
962 if (well_ecl.getStatus() == WellStatus::OPEN) {
963 this->well_open_times_.insert_or_assign(well_ecl.name(),
964 this->simulator_.time());
965 this->well_close_times_.erase(well_ecl.name());
966 }
else if (well_ecl.getStatus() == WellStatus::SHUT) {
967 this->well_close_times_.insert_or_assign(well_ecl.name(),
968 this->simulator_.time());
969 this->well_open_times_.erase(well_ecl.name());
975 const auto cycle_states = wcycle.wellStatus(this->simulator_.time(),
977 this->well_open_times_,
978 this->well_close_times_);
980 for (
int w = 0; w < nw; ++w) {
981 const Well& well_ecl = this->wells_ecl_[w];
983 if (!well_ecl.hasConnections()) {
988 const std::string& well_name = well_ecl.name();
989 const auto well_status = this->schedule()
990 .getWell(well_name, report_step).getStatus();
992 const bool shut_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
993 && well_status == Well::Status::SHUT;
994 const bool open_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
995 && well_status == Well::Status::OPEN;
996 const auto& ws = this->wellState().well(well_name);
998 if (shut_event && ws.status != Well::Status::SHUT) {
999 this->closed_this_step_.insert(well_name);
1000 this->wellState().shutWell(w);
1001 }
else if (open_event && ws.status != Well::Status::OPEN) {
1002 this->wellState().openWell(w);
1006 if (this->wellTestState().well_is_closed(well_name)) {
1011 const bool closed_this_step = (this->wellTestState().lastTestTime(well_name) == simulator_.time());
1014 auto& events = this->wellState().well(w).events;
1015 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
1016 if (!closed_this_step) {
1017 this->wellTestState().open_well(well_name);
1018 this->wellTestState().open_completions(well_name);
1019 this->well_open_times_.insert_or_assign(well_name,
1020 this->simulator_.time());
1021 this->well_close_times_.erase(well_name);
1023 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
1029 if (this->wellTestState().well_is_closed(well_name))
1031 if (well_ecl.getAutomaticShutIn()) {
1033 this->wellState().shutWell(w);
1034 this->well_close_times_.erase(well_name);
1035 this->well_open_times_.erase(well_name);
1038 if (!well_ecl.getAllowCrossFlow()) {
1041 this->wellState().shutWell(w);
1042 this->well_close_times_.erase(well_name);
1043 this->well_open_times_.erase(well_name);
1047 this->wellState().stopWell(w);
1052 if (!well_ecl.getAllowCrossFlow()) {
1053 const bool any_zero_rate_constraint = well_ecl.isProducer()
1054 ? well_ecl.productionControls(this->summaryState_).anyZeroRateConstraint()
1055 : well_ecl.injectionControls(this->summaryState_).anyZeroRateConstraint();
1056 if (any_zero_rate_constraint) {
1058 local_deferredLogger.debug(fmt::format(fmt::runtime(
" Well {} gets shut due to having zero rate constraint and disallowing crossflow "), well_ecl.name()));
1059 this->wellState().shutWell(w);
1060 this->well_close_times_.erase(well_name);
1061 this->well_open_times_.erase(well_name);
1066 if (!wcycle.empty()) {
1067 const auto it = cycle_states.find(well_name);
1068 if (it != cycle_states.end()) {
1069 if (!it->second || well_status == Well::Status::SHUT) {
1071 if (well_status == Well::Status::SHUT) {
1072 this->well_open_times_.erase(well_name);
1073 this->well_close_times_.erase(well_name);
1075 this->wellState().shutWell(w);
1078 this->wellState().openWell(w);
1084 if (ws.status == Well::Status::SHUT) {
1088 well_container_.emplace_back(this->createWellPointer(w, report_step));
1090 if (ws.status == Well::Status::STOP) {
1091 well_container_.back()->stopWell();
1092 this->well_close_times_.erase(well_name);
1093 this->well_open_times_.erase(well_name);
1097 if (!wcycle.empty()) {
1098 const auto schedule_open =
1099 [&wg_events = this->report_step_start_events_](
const std::string& name)
1101 return wg_events.hasEvent(name, ScheduleEvents::REQUEST_OPEN_WELL);
1103 for (
const auto& [wname, wscale] : wcycle.efficiencyScale(this->simulator_.time(),
1104 this->simulator_.timeStepSize(),
1106 this->well_open_times_,
1109 this->wellState().updateEfficiencyScalingFactor(wname, wscale);
1110 this->schedule_.add_event(ScheduleEvents::WELLGROUP_EFFICIENCY_UPDATE, report_step);
1115 this->well_container_generic_.clear();
1116 for (
auto& w : well_container_) {
1117 this->well_container_generic_.push_back(w.get());
1120 this->network_.initialize(report_step);
1122 this->wbp_.registerOpenWellsForWBPCalculation();
1129 template <
typename TypeTag>
1134 const auto is_multiseg = this->wells_ecl_[wellID].isMultiSegment();
1136 if (! (this->param_.use_multisegment_well_ && is_multiseg)) {
1137 return this->
template createTypedWellPointer<StandardWell<TypeTag>>(wellID, report_step);
1140 return this->
template createTypedWellPointer<MultisegmentWell<TypeTag>>(wellID, report_step);
1148 template <
typename TypeTag>
1149 template <
typename WellType>
1150 std::unique_ptr<WellType>
1155 const auto& perf_data = this->well_perf_data_[wellID];
1158 const auto pvtreg = perf_data.empty()
1159 ? 0 : this->pvt_region_idx_[perf_data.front().cell_index];
1161 const auto& parallel_well_info = this->local_parallel_well_info_[wellID].get();
1162 const auto global_pvtreg = parallel_well_info.broadcastFirstPerforationValue(pvtreg);
1164 return std::make_unique<WellType>(this->wells_ecl_[wellID],
1168 *this->rateConverter_,
1170 this->numConservationQuantities(),
1180 template<
typename TypeTag>
1184 const int report_step,
1189 std::ranges::find_if(this->wells_ecl_,
1190 [&well_name](
const auto& w)
1191 {
return well_name == w.name(); });
1193 if (it == this->wells_ecl_.end()) {
1195 fmt::format(fmt::runtime(
"Could not find well {} in wells_ecl"), well_name),
1199 const int pos =
static_cast<int>(std::distance(this->wells_ecl_.begin(), it));
1200 return this->createWellPointer(pos, report_step);
1205 template<
typename TypeTag>
1211 auto logger_guard = this->groupStateHelper().pushLogger();
1212 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
1214 const auto& iterCtx = simulator_.problem().iterationContext();
1217 if (gaslift_.terminalOutput()) {
1218 const std::string msg =
1219 fmt::format(fmt::runtime(
"assemble() : iteration {}"), iterCtx.iteration());
1220 gaslift_.gliftDebug(msg, local_deferredLogger);
1224 Dune::Timer perfTimer;
1226 this->closed_offending_wells_.clear();
1229 const int episodeIdx = simulator_.episodeIndex();
1230 const auto& network = this->schedule()[episodeIdx].network();
1231 if (!this->wellsActive() && !network.active()) {
1237 if (iterCtx.needsTimestepInit() && this->wellsActive()) {
1238 OPM_TIMEBLOCK(firstIterationAssemble);
1245 calculateExplicitQuantities();
1246 prepareTimeStep(local_deferredLogger);
1249 "assemble() failed during well initialization: ",
1250 this->terminal_output_, grid().comm());
1253 const bool well_group_control_changed = updateWellControlsAndNetwork(
false, dt, local_deferredLogger);
1257 if ( ! this->wellsActive() ) {
1261 assembleWellEqWithoutIteration(dt);
1267 last_report_.well_group_control_changed = well_group_control_changed;
1268 last_report_.assemble_time_well += perfTimer.stop();
1274 template<
typename TypeTag>
1283 bool do_network_update =
true;
1284 bool well_group_control_changed =
false;
1285 Scalar network_imbalance = 0.0;
1287 const std::size_t iteration_to_relax = param_.network_max_strict_outer_iterations_;
1289 const std::size_t max_iteration = param_.network_max_outer_iterations_;
1290 std::size_t network_update_iteration = 0;
1291 network_needs_more_balancing_force_another_newton_iteration_ =
false;
1292 while (do_network_update) {
1293 if (network_update_iteration >= max_iteration ) {
1295 const int episodeIdx = simulator_.episodeIndex();
1296 const auto& iterCtx = simulator_.problem().iterationContext();
1297 if (this->network_.willBalanceOnNextIteration(episodeIdx, iterCtx)) {
1298 if (this->terminal_output_) {
1299 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update, \n"
1300 "and try again after the next Newton iteration (imbalance = {:.2e} bar)",
1301 max_iteration, network_imbalance*1.0e-5);
1302 local_deferredLogger.
debug(msg);
1306 network_needs_more_balancing_force_another_newton_iteration_ =
true;
1308 if (this->terminal_output_) {
1309 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update. \n"
1310 "The simulator will continue with unconverged network results (imbalance = {:.2e} bar)",
1311 max_iteration, network_imbalance*1.0e-5);
1312 local_deferredLogger.
info(msg);
1317 if (this->terminal_output_ && (network_update_iteration == iteration_to_relax) ) {
1318 local_deferredLogger.
debug(
"We begin using relaxed tolerance for network update now after " +
std::to_string(iteration_to_relax) +
" iterations ");
1320 const bool relax_network_balance = network_update_iteration >= iteration_to_relax;
1322 const bool optimize_gas_lift = ( (network_update_iteration + 1) < std::max(max_iteration,
static_cast<std::size_t
>(2)) );
1323 std::tie(well_group_control_changed, do_network_update, network_imbalance) =
1324 updateWellControlsAndNetworkIteration(mandatory_network_balance, relax_network_balance, optimize_gas_lift, dt,local_deferredLogger);
1325 ++network_update_iteration;
1327 return well_group_control_changed;
1333 template<
typename TypeTag>
1334 std::tuple<bool, bool, typename BlackoilWellModel<TypeTag>::Scalar>
1337 const bool relax_network_tolerance,
1338 const bool optimize_gas_lift,
1343 const auto& iterCtx = simulator_.problem().iterationContext();
1344 const int reportStepIdx = simulator_.episodeIndex();
1345 this->updateAndCommunicateGroupData(reportStepIdx, iterCtx,
1346 param_.nupcol_group_rate_tolerance_,
true);
1351 bool well_group_control_changed = updateWellControls(local_deferredLogger);
1352 const auto [more_inner_network_update, network_imbalance] =
1353 this->network_.update(mandatory_network_balance,
1354 local_deferredLogger,
1355 relax_network_tolerance);
1357 bool alq_updated =
false;
1360 if (optimize_gas_lift) {
1363 const bool updatePotentials = (this->network_.shouldBalance(reportStepIdx, iterCtx) ||
1364 mandatory_network_balance);
1365 alq_updated = gaslift_.maybeDoGasLiftOptimize(simulator_,
1367 this->network_.nodePressures(),
1371 local_deferredLogger);
1373 prepareWellsBeforeAssembling(dt);
1376 "updateWellControlsAndNetworkIteration() failed: ",
1377 this->terminal_output_, grid().comm());
1381 guideRateUpdateIsNeeded(reportStepIdx)) {
1382 const double simulationTime = simulator_.time();
1386 this->guide_rate_handler_.updateGuideRates(
1387 reportStepIdx, simulationTime, this->wellState(), this->groupState()
1392 const bool more_network_update = this->network_.shouldBalance(reportStepIdx, iterCtx) &&
1393 (more_inner_network_update || alq_updated);
1394 return {well_group_control_changed, more_network_update, network_imbalance};
1397 template<
typename TypeTag>
1403 for (
auto& well : well_container_) {
1404 well->assembleWellEq(simulator_, dt, this->groupStateHelper(), this->wellState());
1409 template<
typename TypeTag>
1415 for (
auto& well : well_container_) {
1416 well->prepareWellBeforeAssembling(
1417 simulator_, dt, this->groupStateHelper(), this->wellState()
1423 template<
typename TypeTag>
1429 auto& deferred_logger = this->groupStateHelper().deferredLogger();
1434 for (
auto& well: well_container_) {
1435 well->assembleWellEqWithoutIteration(simulator_, this->groupStateHelper(), dt, this->wellState(),
1439 this->terminal_output_, grid().comm());
1443 template<
typename TypeTag>
1450 for (
const auto& well : well_container_) {
1451 well->addCellRates(cellRates_);
1455 template<
typename TypeTag>
1462 for (
const auto& well : well_container_) {
1463 const auto it = well_domain_map.find(well->name());
1464 if (it != well_domain_map.end() && it->second == domainIndex) {
1465 well->addCellRates(cellRates_);
1470#if COMPILE_GPU_BRIDGE
1471 template<
typename TypeTag>
1479 for(
unsigned int i = 0; i < well_container_.size(); i++){
1480 auto& well = well_container_[i];
1483 wellContribs.
addNumBlocks(derived->linSys().getNumBlocks());
1488 wellContribs.
alloc();
1490 for(
unsigned int i = 0; i < well_container_.size(); i++){
1491 auto& well = well_container_[i];
1493 auto derived_std =
dynamic_cast<StandardWell<TypeTag>*
>(well.get());
1495 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1497 auto derived_ms =
dynamic_cast<MultisegmentWell<TypeTag>*
>(well.get());
1499 derived_ms->linSys().extract(wellContribs);
1501 OpmLog::warning(
"Warning unknown type of well");
1508 template<
typename TypeTag>
1513 for (
const auto& well: well_container_ ) {
1514 well->addWellContributions(jacobian);
1518 template<
typename TypeTag>
1523 const bool use_well_weights)
const
1525 int nw = this->numLocalWellsEnd();
1526 int rdofs = local_num_cells_;
1527 for (
int i = 0; i < nw; i++ ) {
1528 int wdof = rdofs + i;
1529 jacobian[wdof][wdof] = 1.0;
1532 for (
const auto& well : well_container_) {
1533 well->addWellPressureEquations(jacobian,
1541 template <
typename TypeTag>
1544 const std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress)
const
1549 for (
const auto& well : well_container_) {
1550 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1553 const auto& cells = well->cells();
1554 const auto& rates = well->connectionRates();
1555 for (
unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1556 unsigned cellIdx = cells[perfIdx];
1557 auto rate = rates[perfIdx];
1560 using MatrixBlockType =
typename SparseMatrixAdapter::MatrixBlock;
1561 MatrixBlockType bMat(0.0);
1562 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1563 residual[cellIdx] += res;
1564 *diagMatAddress[cellIdx] += bMat;
1570 template<
typename TypeTag>
1575 int nw = this->numLocalWellsEnd();
1576 int rdofs = local_num_cells_;
1577 for (
int i = 0; i < nw; ++i) {
1578 int wdof = rdofs + i;
1579 jacobian.entry(wdof,wdof) = 1.0;
1581 const auto wellconnections = this->getMaxWellConnections();
1582 for (
int i = 0; i < nw; ++i) {
1583 const auto& perfcells = wellconnections[i];
1584 for (
int perfcell : perfcells) {
1585 int wdof = rdofs + i;
1586 jacobian.entry(wdof, perfcell) = 0.0;
1587 jacobian.entry(perfcell, wdof) = 0.0;
1593 template<
typename TypeTag>
1598 auto loggerGuard = this->groupStateHelper().pushLogger();
1601 for (
const auto& well : well_container_) {
1602 const auto& cells = well->cells();
1603 x_local_.resize(cells.size());
1605 for (
size_t i = 0; i < cells.size(); ++i) {
1606 x_local_[i] = x[cells[i]];
1608 well->recoverWellSolutionAndUpdateWellState(simulator_, x_local_,
1609 this->groupStateHelper(), this->wellState());
1613 simulator_.vanguard().grid().comm());
1617 template<
typename TypeTag>
1623 OPM_THROW(std::logic_error,
"Attempt to call NLDD method without a NLDD solver");
1626 return nldd_->recoverWellSolutionAndUpdateWellState(x, domainIdx);
1630 template<
typename TypeTag>
1633 getWellConvergence(
const std::vector<Scalar>& B_avg,
bool checkWellGroupControlsAndNetwork)
const
1637 const auto& iterCtx = simulator_.problem().iterationContext();
1638 const bool relaxTolerance = iterCtx.shouldRelax(param_.strict_outer_iter_wells_ + 1);
1640 auto logger_guard = this->groupStateHelper().pushLogger();
1641 for (
const auto& well : well_container_) {
1642 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1643 local_report += well->getWellConvergence(
1644 this->groupStateHelper(), B_avg,
1649 report.
setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1650 local_report += report;
1658 if (checkWellGroupControlsAndNetwork) {
1664 if (this->terminal_output_) {
1668 OpmLog::debug(
"NaN residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1670 OpmLog::debug(
"Too large residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1681 template<
typename TypeTag>
1687 for (
auto& well : well_container_) {
1688 well->calculateExplicitQuantities(simulator_, this->groupStateHelper());
1696 template<
typename TypeTag>
1702 if (!this->wellsActive()) {
1705 const int episodeIdx = simulator_.episodeIndex();
1706 const auto& comm = simulator_.vanguard().grid().comm();
1708 bool changed_well_group =
false;
1709 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", episodeIdx);
1712 const std::size_t max_iter = param_.well_group_constraints_max_iterations_;
1713 while(!changed_well_group && iter < max_iter) {
1714 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx);
1717 bool changed_well_to_group =
false;
1719 OPM_TIMEBLOCK(UpdateWellControls);
1723 for (
const auto& well : well_container_) {
1726 simulator_, mode, this->groupStateHelper(), this->wellState()
1729 changed_well_to_group = changed_well || changed_well_to_group;
1733 simulator_.gridView().comm());
1736 changed_well_to_group = comm.sum(
static_cast<int>(changed_well_to_group));
1737 if (changed_well_to_group) {
1738 updateAndCommunicate(episodeIdx);
1739 changed_well_group =
true;
1743 bool changed_well_individual =
false;
1748 for (
const auto& well : well_container_) {
1751 simulator_, mode, this->groupStateHelper(), this->wellState()
1754 changed_well_individual = changed_well || changed_well_individual;
1758 simulator_.gridView().comm());
1761 changed_well_individual = comm.sum(
static_cast<int>(changed_well_individual));
1762 if (changed_well_individual) {
1763 updateAndCommunicate(episodeIdx);
1764 changed_well_group =
true;
1770 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1772 return changed_well_group;
1776 template<
typename TypeTag>
1781 const auto& iterCtx = simulator_.problem().iterationContext();
1782 this->updateAndCommunicateGroupData(reportStepIdx,
1784 param_.nupcol_group_rate_tolerance_,
1791 for (
const auto& well : well_container_) {
1793 const auto& ws = this->wellState().well(well->indexOfWell());
1794 if (ws.production_cmode == Well::ProducerCMode::GRUP ||
1795 ws.injection_cmode == Well::InjectorCMode::GRUP)
1797 well->updateWellStateWithTarget(
1798 simulator_, this->groupStateHelper(), this->wellState()
1803 simulator_.gridView().comm())
1804 this->updateAndCommunicateGroupData(reportStepIdx,
1806 param_.nupcol_group_rate_tolerance_,
1810 template<
typename TypeTag>
1815 const int reportStepIdx)
1818 const auto& iterCtx = simulator_.problem().iterationContext();
1819 bool changed =
false;
1821 const int nupcol = this->schedule()[reportStepIdx].nupcol();
1822 const int max_number_of_group_switches = param_.max_number_of_group_switches_;
1823 const bool update_group_switching_log = !iterCtx.withinNupcol(nupcol);
1824 const bool changed_hc = this->checkGroupHigherConstraints(group, deferred_logger, reportStepIdx, max_number_of_group_switches, update_group_switching_log);
1827 updateAndCommunicate(reportStepIdx);
1830 bool changed_individual =
1832 updateGroupIndividualControl(group,
1834 max_number_of_group_switches,
1835 update_group_switching_log,
1836 this->switched_inj_groups_,
1837 this->switched_prod_groups_,
1838 this->closed_offending_wells_,
1843 if (changed_individual) {
1845 updateAndCommunicate(reportStepIdx);
1848 for (
const std::string& groupName : group.groups()) {
1849 bool changed_this = updateGroupControls(this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx);
1850 changed = changed || changed_this;
1855 template<
typename TypeTag>
1861 auto logger_guard = this->groupStateHelper().pushLogger();
1862 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
1863 for (
const auto& well : well_container_) {
1864 const auto& wname = well->name();
1865 const auto wasClosed = wellTestState.well_is_closed(wname);
1866 well->checkWellOperability(simulator_,
1868 this->groupStateHelper());
1869 const bool under_zero_target =
1870 well->wellUnderZeroGroupRateTarget(this->groupStateHelper());
1871 well->updateWellTestState(this->wellState().well(wname),
1876 local_deferredLogger);
1878 if (!wasClosed && wellTestState.well_is_closed(wname)) {
1879 this->closed_this_step_.insert(wname);
1882 const WellEconProductionLimits& econ_production_limits = well->wellEcl().getEconLimits();
1883 if (econ_production_limits.validFollowonWell()) {
1884 const auto episode_idx = simulator_.episodeIndex();
1885 const auto follow_on_well = econ_production_limits.followonWell();
1886 if (!this->schedule().hasWell(follow_on_well, episode_idx)) {
1887 const auto msg = fmt::format(
"Well {} was closed. But the given follow on well {} does not exist."
1888 "The simulator continues without opening a follow on well.",
1889 wname, follow_on_well);
1890 local_deferredLogger.warning(msg);
1892 auto& ws = this->wellState().well(follow_on_well);
1893 const bool success = ws.updateStatus(WellStatus::OPEN);
1895 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} opens instead.", wname, follow_on_well);
1896 local_deferredLogger.info(msg);
1898 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} is already open.", wname, follow_on_well);
1899 local_deferredLogger.warning(msg);
1906 for (
const auto& [group_name, to] : this->closed_offending_wells_) {
1907 if (this->hasOpenLocalWell(to.second) &&
1908 !this->wasDynamicallyShutThisTimeStep(to.second))
1910 wellTestState.close_well(to.second,
1911 WellTestConfig::Reason::GROUP,
1913 this->updateClosedWellsThisStep(to.second);
1914 const std::string msg =
1915 fmt::format(
"Procedure on exceeding {} limit is WELL for group {}. "
1921 local_deferredLogger.info(msg);
1927 template<
typename TypeTag>
1931 std::string& exc_msg,
1935 const int np = this->numPhases();
1936 std::vector<Scalar> potentials;
1937 const auto& well = well_container_[widx];
1938 std::string cur_exc_msg;
1941 well->computeWellPotentials(simulator_, well_state_copy, this->groupStateHelper(), potentials);
1946 exc_msg += fmt::format(
"\nFor well {}: {}", well->name(), cur_exc_msg);
1948 exc_type = std::max(exc_type, cur_exc_type);
1952 auto& ws = this->wellState().well(well->indexOfWell());
1953 for (
int p = 0; p < np; ++p) {
1955 ws.well_potentials[p] = std::max(
Scalar{0.0}, potentials[p]);
1961 template <
typename TypeTag>
1966 for (
const auto& wellPtr : this->well_container_) {
1967 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
1975 template <
typename TypeTag>
1986 for (
const auto& shutWell : this->local_shut_wells_) {
1987 if (!this->wells_ecl_[shutWell].hasConnections()) {
1992 auto wellPtr = this->
template createTypedWellPointer
1995 wellPtr->
init(this->depth_, this->gravity_, this->B_avg_,
true);
1997 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2005 template <
typename TypeTag>
2019 template<
typename TypeTag>
2025 const auto episodeIdx = simulator_.episodeIndex();
2026 this->network_.updateActiveState(episodeIdx);
2030 const bool do_prestep_network_rebalance =
2031 param_.pre_solve_network_ && this->network_.needPreStepRebalance(episodeIdx);
2033 for (
const auto& well : well_container_) {
2034 auto& events = this->wellState().well(well->indexOfWell()).events;
2036 well->updateWellStateWithTarget(
2037 simulator_, this->groupStateHelper(), this->wellState()
2039 well->updatePrimaryVariables(this->groupStateHelper());
2045 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2046 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2049 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2051 well->solveWellEquation(
2052 simulator_, this->groupStateHelper(), this->wellState()
2054 }
catch (
const std::exception& e) {
2055 const std::string msg =
"Compute initial well solution for " + well->name() +
" initially failed. Continue with the previous rates";
2056 deferred_logger.
warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
2061 well->resetWellOperability();
2063 updatePrimaryVariables();
2066 if (do_prestep_network_rebalance) {
2067 network_.doPreStepRebalance(deferred_logger);
2071 template<
typename TypeTag>
2076 std::vector< Scalar > B_avg(numConservationQuantities(),
Scalar() );
2077 const auto& grid = simulator_.vanguard().grid();
2078 const auto& gridView = grid.leafGridView();
2082 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2083 elemCtx.updatePrimaryStencil(elem);
2084 elemCtx.updatePrimaryIntensiveQuantities(0);
2086 const auto& intQuants = elemCtx.intensiveQuantities(0, 0);
2087 const auto& fs = intQuants.fluidState();
2089 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2091 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2095 const unsigned compIdx = FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
2096 auto& B = B_avg[ compIdx ];
2098 B += 1 / fs.invB(phaseIdx).value();
2100 if constexpr (has_solvent_) {
2101 auto& B = B_avg[solventSaturationIdx];
2102 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2108 grid.comm().sum(B_avg.data(), B_avg.size());
2109 B_avg_.resize(B_avg.size());
2110 std::ranges::transform(B_avg, B_avg_.begin(),
2111 [gcells = global_num_cells_](
const auto bval)
2112 { return bval / gcells; });
2119 template<
typename TypeTag>
2124 for (
const auto& well : well_container_) {
2125 well->updatePrimaryVariables(this->groupStateHelper());
2129 template<
typename TypeTag>
2133 const auto& grid = simulator_.vanguard().grid();
2134 const auto& eclProblem = simulator_.problem();
2135 const unsigned numCells = grid.size(0);
2137 this->pvt_region_idx_.resize(numCells);
2138 for (
unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2139 this->pvt_region_idx_[cellIdx] =
2140 eclProblem.pvtRegionIndex(cellIdx);
2145 template<
typename TypeTag>
2158 return this->numPhases() + has_solvent_;
2161 template<
typename TypeTag>
2165 const auto& eclProblem = simulator_.problem();
2166 depth_.resize(local_num_cells_);
2167 for (
unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2168 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2172 template<
typename TypeTag>
2175 getWell(
const std::string& well_name)
const
2179 std::ranges::find_if(well_container_,
2181 {
return elem->name() == well_name; });
2183 assert(well != well_container_.end());
2188 template <
typename TypeTag>
2193 return std::max(this->simulator_.episodeIndex(), 0);
2200 template<
typename TypeTag>
2205 const std::vector<Scalar>& production_rates,
2206 std::vector<Scalar>& resv_coeff)
const
2208 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2211 template<
typename TypeTag>
2216 std::vector<Scalar>& resv_coeff)
const
2218 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2222 template <
typename TypeTag>
2227 if constexpr (energyModuleType_ == EnergyModules::FullyImplicitThermal) {
2228 const int np = this->numPhases();
2229 const int nw = this->numLocalWells();
2230 for (
auto wellID = 0*nw; wellID < nw; ++wellID) {
2231 const Well& well = this->wells_ecl_[wellID];
2232 auto& ws = this->wellState().well(wellID);
2233 if (well.isInjector()) {
2234 if (ws.status != WellStatus::STOP) {
2235 this->wellState().well(wellID).temperature = well.inj_temperature();
2239 std::array<Scalar,2> weighted{0.0,0.0};
2240 auto& [weighted_temperature, total_weight] = weighted;
2241 const auto& well_info = this->local_parallel_well_info_[wellID].get();
2242 using int_type =
decltype(this->well_perf_data_[wellID].size());
2243 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2244 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2245 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, 0);
2246 const auto& fs = intQuants.fluidState();
2247 Scalar weight_factor = computeTemperatureWeightFactor(perf, np, fs, ws);
2248 total_weight += weight_factor;
2249 weighted_temperature += weight_factor * fs.temperature(0).value();
2251 well_info.communication().sum(weighted.data(), 2);
2252 this->wellState().well(wellID).temperature = weighted_temperature / total_weight;
2258 template <
typename TypeTag>
2262 const auto reportStepIdx =
static_cast<unsigned int>(this->reportStepIndex());
2263 const auto& trMod = this->simulator_.problem().tracerModel();
2269 this->assignMswTracerRates(wsrpt, trMod.getMswTracerRates(), reportStepIdx);
2272 template <
typename TypeTag>
2273 void BlackoilWellModel<TypeTag>::
2274 assignWellSpeciesRates(data::Wells& wsrpt)
const
2276 const auto reportStepIdx =
static_cast<unsigned int>(this->reportStepIndex());
2277 const auto& geochemMod = this->simulator_.problem().geochemistryModel();
2281 this->assignMswTracerRates(wsrpt, geochemMod.getMswSpeciesRates(), reportStepIdx);
2284 template <
typename TypeTag>
2285 [[nodiscard]]
auto BlackoilWellModel<TypeTag>::rsConstInfo() const
2286 -> typename WellState<Scalar,IndexTraits>::RsConstInfo
2288 if (! FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx) ||
2289 ! FluidSystem::enableConstantRs())
2294 const auto& rsConstTables = this->eclState_
2295 .getTableManager().getRsconstTables();
2297 if (rsConstTables.empty() ||
2298 (rsConstTables[0].numRows() != std::size_t{1}))
2303 const auto rsConst = rsConstTables[0].getColumn(0).front();
2305 return {
true,
static_cast<Scalar
>(rsConst) };
#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:97
const Parallel::Communication & comm() const
Definition: BlackoilWellModelGeneric.hpp:224
BlackoilWellModelWBP< GetPropType< TypeTag, Properties::Scalar >, GetPropType< TypeTag, Properties::FluidSystem >::IndexTraitsType > wbp_
Definition: BlackoilWellModelGeneric.hpp:518
std::vector< ParallelWellInfo< GetPropType< TypeTag, Properties::Scalar > > > parallel_well_info_
Definition: BlackoilWellModelGeneric.hpp:545
void assignWellTracerRates(data::Wells &wsrpt, const WellTracerRates &wellTracerRates, const unsigned reportStep) const
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:98
void initializeGroupStructure(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:297
void calcResvCoeff(const int fipnum, const int pvtreg, const std::vector< Scalar > &production_rates, std::vector< Scalar > &resv_coeff) const override
Definition: BlackoilWellModel_impl.hpp:2203
void prepareTimeStep(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2022
std::tuple< bool, bool, Scalar > updateWellControlsAndNetworkIteration(const bool mandatory_network_balance, const bool relax_network_tolerance, const bool optimize_gas_lift, const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModel_impl.hpp:1336
WellInterfacePtr createWellPointer(const int wellID, const int report_step) const
Definition: BlackoilWellModel_impl.hpp:1132
void prepareWellsBeforeAssembling(const double dt)
Definition: BlackoilWellModel_impl.hpp:1412
void init()
Definition: BlackoilWellModel_impl.hpp:166
const Simulator & simulator() const
Definition: BlackoilWellModel.hpp:370
std::vector< Scalar > depth_
Definition: BlackoilWellModel.hpp:517
std::size_t global_num_cells_
Definition: BlackoilWellModel.hpp:513
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModel.hpp:107
void initWellContainer(const int reportStepIdx) override
Definition: BlackoilWellModel_impl.hpp:185
void beginReportStep(const int time_step)
Definition: BlackoilWellModel_impl.hpp:202
const WellInterface< TypeTag > & getWell(const std::string &well_name) const
Definition: BlackoilWellModel_impl.hpp:2175
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: BlackoilWellModel.hpp:103
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:130
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: BlackoilWellModel.hpp:104
GetPropType< TypeTag, Properties::Grid > Grid
Definition: BlackoilWellModel.hpp:101
int numConservationQuantities() const
Definition: BlackoilWellModel_impl.hpp:2147
bool updateWellControls(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1699
int reportStepIndex() const
Definition: BlackoilWellModel_impl.hpp:2191
void calculateProductivityIndexValues(DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:1964
void extractLegacyDepth_()
Definition: BlackoilWellModel_impl.hpp:2163
void extractLegacyCellPvtRegionIndex_()
Definition: BlackoilWellModel_impl.hpp:2131
void recoverWellSolutionAndUpdateWellStateDomain(const BVector &x, const int domainIdx)
Definition: BlackoilWellModel_impl.hpp:1620
void updateAverageFormationFactor()
Definition: BlackoilWellModel_impl.hpp:2074
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModel.hpp:106
void initializeWellState(const int timeStepIdx)
Definition: BlackoilWellModel_impl.hpp:880
const Grid & grid() const
Definition: BlackoilWellModel.hpp:367
void updatePrimaryVariables()
Definition: BlackoilWellModel_impl.hpp:2122
void computeWellTemperature()
Definition: BlackoilWellModel_impl.hpp:2225
void addWellPressureEquations(PressureMatrix &jacobian, const BVector &weights, const bool use_well_weights) const
Definition: BlackoilWellModel_impl.hpp:1521
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:756
bool updateWellControlsAndNetwork(const bool mandatory_network_balance, const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModel_impl.hpp:1277
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1511
void assembleWellEq(const double dt)
Definition: BlackoilWellModel_impl.hpp:1400
WellInterfacePtr createWellForWellTest(const std::string &well_name, const int report_step, DeferredLogger &deferred_logger) const
Definition: BlackoilWellModel_impl.hpp:1183
void calculateExplicitQuantities() const
Definition: BlackoilWellModel_impl.hpp:1684
void updateAndCommunicate(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:1779
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > PressureMatrix
Definition: BlackoilWellModel.hpp:289
void computeTotalRatesForDof(RateVector &rate, unsigned globalIdx) const
Definition: BlackoilWellModel_impl.hpp:843
void beginTimeStep()
Definition: BlackoilWellModel_impl.hpp:329
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: BlackoilWellModel.hpp:108
bool updateGroupControls(const Group &group, DeferredLogger &deferred_logger, const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:1813
void calcInjResvCoeff(const int fipnum, const int pvtreg, std::vector< Scalar > &resv_coeff) const override
Definition: BlackoilWellModel_impl.hpp:2214
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:251
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:131
BlackoilWellModel(Simulator &simulator)
Definition: BlackoilWellModel_impl.hpp:79
void wellTesting(const int timeStepIdx, const double simulationTime, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:679
ConvergenceReport getWellConvergence(const std::vector< Scalar > &B_avg, const bool checkWellGroupControlsAndNetwork=false) const
Definition: BlackoilWellModel_impl.hpp:1633
typename FluidSystem::IndexTraitsType IndexTraits
Definition: BlackoilWellModel.hpp:114
void updateCellRatesForDomain(int domainIndex, const std::map< std::string, int > &well_domain_map)
Definition: BlackoilWellModel_impl.hpp:1458
void assembleWellEqWithoutIteration(const double dt)
Definition: BlackoilWellModel_impl.hpp:1426
void updateCellRates()
Definition: BlackoilWellModel_impl.hpp:1446
void assemble(const double dt)
Definition: BlackoilWellModel_impl.hpp:1208
std::size_t local_num_cells_
Definition: BlackoilWellModel.hpp:515
bool alternative_well_rate_init_
Definition: BlackoilWellModel.hpp:518
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:766
std::unique_ptr< WellType > createTypedWellPointer(const int wellID, const int time_step) const
Definition: BlackoilWellModel_impl.hpp:1152
void computePotentials(const std::size_t widx, const WellState< Scalar, IndexTraits > &well_state_copy, std::string &exc_msg, ExceptionType::ExcEnum &exc_type) override
Definition: BlackoilWellModel_impl.hpp:1929
Simulator & simulator_
Definition: BlackoilWellModel.hpp:487
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:923
std::unique_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:187
void updateWellTestState(const double simulationTime, WellTestState &wellTestState)
upate the wellTestState related to economic limits
Definition: BlackoilWellModel_impl.hpp:1858
void addReservoirSourceTerms(GlobalEqVector &residual, const std::vector< typename SparseMatrixAdapter::MatrixBlock * > &diagMatAddress) const
Definition: BlackoilWellModel_impl.hpp:1543
int compressedIndexForInterior(int cartesian_cell_idx) const override
get compressed index for interior cells (-1, otherwise
Definition: BlackoilWellModel.hpp:342
void recoverWellSolutionAndUpdateWellState(const BVector &x)
Definition: BlackoilWellModel_impl.hpp:1596
void addWellPressureEquationsStruct(PressureMatrix &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1573
void calculateProductivityIndexValuesShutWells(const int reportStepIdx, DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:1978
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:739
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:65
Guard for managing DeferredLogger lifecycle in ReservoirCoupling.
Definition: ReservoirCoupling.hpp:88
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:77
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)
int indexOfWell() const
Index of well in the wells struct and wellState.
Definition: WellInterface.hpp:77
virtual void updateProductivityIndex(const Simulator &simulator, const WellProdIndexCalculator< Scalar > &wellPICalc, WellStateType &well_state, DeferredLogger &deferred_logger) const =0
bool updateWellControl(const Simulator &simulator, const IndividualOrGroup iog, const GroupStateHelperType &groupStateHelper, WellStateType &well_state)
Definition: WellInterface_impl.hpp:190
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:45
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