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
71#include <fmt/format.h>
74 template<
typename TypeTag>
81 simulator.vanguard().summaryState(),
82 simulator.vanguard().eclState(),
84 simulator.gridView().comm())
85 , simulator_(simulator)
86 , guide_rate_handler_{
88 simulator.vanguard().schedule(),
89 simulator.vanguard().summaryState(),
90 simulator.vanguard().grid().comm()
92 , 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>
201 this->groupStateHelper().setReportStep(timeStepIdx);
202 this->report_step_starts_ =
true;
203 this->report_step_start_events_ = this->schedule()[timeStepIdx].wellgroup_events();
205 this->rateConverter_ = std::make_unique<RateConverterType>
206 (std::vector<int>(this->local_num_cells_, 0));
210 const auto enableWellPIScaling =
true;
211 this->initializeLocalWellStructure(timeStepIdx, enableWellPIScaling);
214 this->initializeGroupStructure(timeStepIdx);
216 const auto& comm = this->simulator_.vanguard().grid().comm();
222 this->rateConverter_->template defineState<ElementContext>(this->simulator_);
226 const auto& sched_state = this->schedule()[timeStepIdx];
228 this->vfp_properties_ = std::make_unique<VFPProperties<Scalar, IndexTraits>>
229 (sched_state.vfpinj(), sched_state.vfpprod(), this->wellState());
236 this->commitWGState();
238 this->wellStructureChangedDynamically_ =
false;
245 template <
typename TypeTag>
249 const bool enableWellPIScaling)
251 auto logger_guard = this->groupStateHelper().pushLogger();
252 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
254 const auto& comm = this->simulator_.vanguard().grid().comm();
257 this->wells_ecl_ = this->getLocalWells(reportStepIdx);
258 this->local_parallel_well_info_ =
259 this->createLocalParallelWellInfo(this->wells_ecl_);
266 this->initializeWellPerfData();
267 this->initializeWellState(reportStepIdx);
268 this->wbp_.initializeWBPCalculationService();
270 if (this->param_.use_multisegment_well_ && this->anyMSWellOpenLocal()) {
271 this->wellState().initWellStateMSWell(this->wells_ecl_, &this->prevWellState());
274 this->initializeWellProdIndCalculators();
276 if (enableWellPIScaling && this->schedule()[reportStepIdx].events()
277 .hasEvent(ScheduleEvents::Events::WELL_PRODUCTIVITY_INDEX))
279 this->runWellPIScaling(reportStepIdx, local_deferredLogger);
283 "Failed to initialize local well structure: ",
284 this->terminal_output_, comm)
291 template <
typename TypeTag>
296 const auto& comm = this->simulator_.vanguard().grid().comm();
300 const auto& fieldGroup =
301 this->schedule().getGroup(
"FIELD", reportStepIdx);
303 this->groupStateHelper().setCmodeGroup(fieldGroup);
307 if (this->schedule()[reportStepIdx].has_gpmaint()) {
308 this->groupStateHelper().setRegionAveragePressureCalculator(
310 this->eclState_.fieldProps(),
311 this->regionalAveragePressureCalculator_
323 template<
typename TypeTag>
328 OPM_TIMEBLOCK(beginTimeStep);
330 this->updateAverageFormationFactor();
332 auto logger_guard = this->groupStateHelper().pushLogger();
333 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
335#ifdef RESERVOIR_COUPLING_ENABLED
336 auto rescoup_logger_guard = this->setupRescoupScopedLogger(local_deferredLogger);
339 this->switched_prod_groups_.clear();
340 this->switched_inj_groups_.clear();
342 if (this->wellStructureChangedDynamically_) {
347 const auto reportStepIdx =
348 this->simulator_.episodeIndex();
352 const auto enableWellPIScaling =
false;
354 this->initializeLocalWellStructure(reportStepIdx, enableWellPIScaling);
355 this->initializeGroupStructure(reportStepIdx);
357 this->commitWGState();
363 this->wellStructureChangedDynamically_ =
false;
366 this->resetWGState();
367 const int reportStepIdx = simulator_.episodeIndex();
369 this->wellState().updateWellsDefaultALQ(this->schedule(), reportStepIdx, this->summaryState());
370 this->wellState().gliftTimeStepInit();
372 const double simulationTime = simulator_.time();
376 wellTesting(reportStepIdx, simulationTime, local_deferredLogger);
379 createWellContainer(reportStepIdx);
381#ifdef RESERVOIR_COUPLING_ENABLED
382 if (this->isReservoirCouplingMaster()) {
383 if (this->reservoirCouplingMaster().isFirstSubstepOfSyncTimestep()) {
384 this->receiveSlaveGroupData();
391 this->updateAndCommunicateGroupData(reportStepIdx,
392 simulator_.model().newtonMethod().numIterations(),
393 param_.nupcol_group_rate_tolerance_,
false);
396 const Grid& grid = simulator_.vanguard().grid();
397 this->wells_active_ = grid.comm().max(!this->well_container_.empty());
402 this->initWellContainer(reportStepIdx);
405 std::fill(is_cell_perforated_.begin(), is_cell_perforated_.end(),
false);
406 for (
auto& well : well_container_) {
407 well->updatePerforatedCell(is_cell_perforated_);
411 this->calculateEfficiencyFactors(reportStepIdx);
413 if constexpr (has_polymer_)
415 if (PolymerModule::hasPlyshlog() || getPropValue<TypeTag, Properties::EnablePolymerMW>() ) {
416 this->setRepRadiusPerfLength();
423 this->terminal_output_, simulator_.vanguard().grid().comm());
425 for (
auto& well : well_container_) {
426 well->setVFPProperties(this->vfp_properties_.get());
427 well->setGuideRate(&this->guideRate_);
430 this->updateFiltrationModelsPreStep(local_deferredLogger);
433 for (
auto& well : well_container_) {
434 well->closeCompletions(this->wellTestState());
440 if (alternative_well_rate_init_) {
445 for (
const auto& well : well_container_) {
446 if (well->isProducer() && !well->wellIsStopped()) {
447 well->initializeProducerWellState(simulator_, this->wellState(), local_deferredLogger);
452 for (
const auto& well : well_container_) {
453 if (well->isVFPActive(local_deferredLogger)){
454 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
458 this->updateWellPotentials(reportStepIdx,
460 simulator_.vanguard().summaryConfig(),
461 local_deferredLogger);
462 }
catch ( std::runtime_error& e ) {
463 const std::string msg =
"A zero well potential is returned for output purposes. ";
464 local_deferredLogger.warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
467 this->guide_rate_handler_.updateGuideRates(
468 reportStepIdx, simulationTime, this->wellState(), this->groupState()
470#ifdef RESERVOIR_COUPLING_ENABLED
471 if (this->isReservoirCouplingSlave()) {
472 if (this->reservoirCouplingSlave().isFirstSubstepOfSyncTimestep()) {
473 this->sendSlaveGroupDataToMaster();
474 this->receiveGroupTargetsFromMaster(reportStepIdx);
481 if (this->schedule_[reportStepIdx].has_gpmaint()) {
482 for (
const auto& calculator : regionalAveragePressureCalculator_) {
483 calculator.second->template defineState<ElementContext>(simulator_);
485 const double dt = simulator_.timeStepSize();
486 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", reportStepIdx);
487 this->groupStateHelper().updateGpMaintTargetForGroups(fieldGroup,
488 regionalAveragePressureCalculator_,
492 this->updateAndCommunicateGroupData(reportStepIdx,
493 simulator_.model().newtonMethod().numIterations(),
494 param_.nupcol_group_rate_tolerance_,
498 for (
auto& well : well_container_) {
499 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
500 + ScheduleEvents::INJECTION_TYPE_CHANGED
501 + ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER
502 + ScheduleEvents::NEW_WELL;
504 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
505 const bool event = this->report_step_starts_ && events.hasEvent(well->name(), effective_events_mask);
506 const bool dyn_status_change = this->wellState().well(well->name()).status
507 != this->prevWellState().well(well->name()).status;
509 if (event || dyn_status_change) {
511 well->scaleSegmentRatesAndPressure(this->wellState());
512 well->calculateExplicitQuantities(simulator_, this->groupStateHelper());
513 well->updateWellStateWithTarget(simulator_, this->groupStateHelper(), this->wellState());
514 well->updatePrimaryVariables(this->groupStateHelper());
515 well->solveWellEquation(
516 simulator_, this->groupStateHelper(), this->wellState()
518 }
catch (
const std::exception& e) {
519 const std::string msg =
"Compute initial well solution for new well " + well->name() +
" failed. Continue with zero initial rates";
520 local_deferredLogger.warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
528#ifdef RESERVOIR_COUPLING_ENABLED
529 if (this->isReservoirCouplingMaster()) {
530 this->sendMasterGroupTargetsToSlaves();
535 const std::string msg =
"Compute initial well solution for new wells failed. Continue with zero initial rates";
536 local_deferredLogger.warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
539 const auto& comm = simulator_.vanguard().grid().comm();
541 exc_type,
"beginTimeStep() failed: " + exc_msg, this->terminal_output_, comm);
545#ifdef RESERVOIR_COUPLING_ENABLED
555 template<
typename TypeTag>
556 std::optional<ReservoirCoupling::ScopedLoggerGuard>
559 if (this->isReservoirCouplingMaster()) {
561 this->reservoirCouplingMaster().logger(),
564 }
else if (this->isReservoirCouplingSlave()) {
565 return ReservoirCoupling::ScopedLoggerGuard{
566 this->reservoirCouplingSlave().logger(),
573 template<
typename TypeTag>
575 BlackoilWellModel<TypeTag>::
576 receiveSlaveGroupData()
578 assert(this->isReservoirCouplingMaster());
579 RescoupReceiveSlaveGroupData<Scalar, IndexTraits> slave_group_data_receiver{
580 this->groupStateHelper(),
582 slave_group_data_receiver.receiveSlaveGroupData();
585 template<
typename TypeTag>
587 BlackoilWellModel<TypeTag>::
588 sendSlaveGroupDataToMaster()
590 assert(this->isReservoirCouplingSlave());
591 RescoupSendSlaveGroupData<Scalar, IndexTraits> slave_group_data_sender{this->groupStateHelper()};
592 slave_group_data_sender.sendSlaveGroupDataToMaster();
595 template<
typename TypeTag>
597 BlackoilWellModel<TypeTag>::
598 sendMasterGroupTargetsToSlaves()
601 RescoupTargetCalculator<Scalar, IndexTraits> target_calculator{
602 this->guide_rate_handler_,
603 this->groupStateHelper()
605 target_calculator.calculateMasterGroupTargetsAndSendToSlaves();
608 template<
typename TypeTag>
610 BlackoilWellModel<TypeTag>::
611 receiveGroupTargetsFromMaster(
int reportStepIdx)
613 RescoupReceiveGroupTargets<Scalar, IndexTraits> target_receiver{
614 this->guide_rate_handler_,
619 target_receiver.receiveGroupTargetsFromMaster();
624 template<
typename TypeTag>
627 const double simulationTime,
630 for (
const std::string& well_name : this->getWellsForTesting(timeStepIdx, simulationTime)) {
631 const Well& wellEcl = this->schedule().getWell(well_name, timeStepIdx);
632 if (wellEcl.getStatus() == Well::Status::SHUT)
635 WellInterfacePtr well = createWellForWellTest(well_name, timeStepIdx, deferred_logger);
637 well->init(depth_, gravity_, B_avg_,
true);
639 Scalar well_efficiency_factor = wellEcl.getEfficiencyFactor() *
640 this->wellState().getGlobalEfficiencyScalingFactor(well_name);
641 this->groupStateHelper().accumulateGroupEfficiencyFactor(
642 this->schedule().getGroup(wellEcl.groupName(), timeStepIdx),
643 well_efficiency_factor
646 well->setWellEfficiencyFactor(well_efficiency_factor);
647 well->setVFPProperties(this->vfp_properties_.get());
648 well->setGuideRate(&this->guideRate_);
651 if (well->isProducer() && alternative_well_rate_init_) {
652 well->initializeProducerWellState(simulator_, this->wellState(), deferred_logger);
654 if (well->isVFPActive(deferred_logger)) {
655 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
658 const auto& network = this->schedule()[timeStepIdx].network();
659 if (network.active()) {
660 this->network_.initializeWell(*well);
664 GLiftEclWells ecl_well_map;
665 gaslift_.initGliftEclWellMap(well_container_, ecl_well_map);
666 well->wellTesting(simulator_,
668 this->groupStateHelper(),
670 this->wellTestState(),
672 this->well_open_times_);
673 }
catch (
const std::exception& e) {
674 const std::string msg = fmt::format(
"Exception during testing of well: {}. The well will not open.\n Exception message: {}", wellEcl.name(), e.what());
675 deferred_logger.
warning(
"WELL_TESTING_FAILED", msg);
681 template<
typename TypeTag>
687 for (
auto&& pinfo : this->local_parallel_well_info_)
698 template<
typename TypeTag>
708 template<
typename TypeTag>
713 this->closed_this_step_.clear();
716 this->report_step_starts_ =
false;
717 const int reportStepIdx = simulator_.episodeIndex();
719 auto logger_guard = this->groupStateHelper().pushLogger();
720 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
721 for (
const auto& well : well_container_) {
722 if (getPropValue<TypeTag, Properties::EnablePolymerMW>() && well->isInjector()) {
723 well->updateWaterThroughput(dt, this->wellState());
727 for (
const auto& well : well_container_) {
728 well->updateConnectionTransmissibilityFactor(simulator_, this->wellState().well(well->indexOfWell()));
729 well->updateConnectionDFactor(simulator_, this->wellState().well(well->indexOfWell()));
732 if (Indices::waterEnabled) {
733 this->updateFiltrationModelsPostStep(dt, FluidSystem::waterPhaseIdx, local_deferredLogger);
737 this->updateInjMult(local_deferredLogger);
740 for (
const auto& well : well_container_) {
741 well->reportWellSwitching(this->wellState().well(well->indexOfWell()), local_deferredLogger);
744 if (this->terminal_output_) {
745 this->reportGroupSwitching(local_deferredLogger);
749 rateConverter_->template defineState<ElementContext>(simulator_);
753 this->updateWellPotentials(reportStepIdx,
755 simulator_.vanguard().summaryConfig(),
756 local_deferredLogger);
757 }
catch ( std::runtime_error& e ) {
758 const std::string msg =
"A zero well potential is returned for output purposes. ";
759 local_deferredLogger.warning(
"WELL_POTENTIAL_CALCULATION_FAILED", msg);
762 updateWellTestState(simulationTime, this->wellTestState());
765 const Group& fieldGroup = this->schedule_.getGroup(
"FIELD", reportStepIdx);
766 this->checkGEconLimits(fieldGroup, simulationTime,
767 simulator_.episodeIndex(), local_deferredLogger);
768 this->checkGconsaleLimits(fieldGroup, this->wellState(),
769 simulator_.episodeIndex(), local_deferredLogger);
771 this->calculateProductivityIndexValues(local_deferredLogger);
773 const auto& glo = this->schedule().glo(reportStepIdx);
774 this->updateNONEProductionGroups(glo, local_deferredLogger);
776 this->commitWGState();
779 this->computeWellTemperature();
783 template<
typename TypeTag>
787 unsigned elemIdx)
const
791 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
795 rate = cellRates_.at(elemIdx);
799 template<
typename TypeTag>
800 template <
class Context>
804 const Context& context,
806 unsigned timeIdx)
const
809 int elemIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
811 if (!is_cell_perforated_[elemIdx] || cellRates_.count(elemIdx) == 0) {
815 rate = cellRates_.at(elemIdx);
820 template<
typename TypeTag>
825 const auto pressIx = []()
827 if (Indices::oilEnabled) {
return FluidSystem::oilPhaseIdx; }
828 if (Indices::waterEnabled) {
return FluidSystem::waterPhaseIdx; }
830 return FluidSystem::gasPhaseIdx;
833 auto cellPressures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
834 auto cellTemperatures = std::vector<Scalar>(this->local_num_cells_,
Scalar{0});
837 const auto& gridView = this->simulator_.vanguard().gridView();
840 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
841 elemCtx.updatePrimaryStencil(elem);
842 elemCtx.updatePrimaryIntensiveQuantities(0);
844 const auto ix = elemCtx.globalSpaceIndex(0, 0);
845 const auto& fs = elemCtx.intensiveQuantities(0, 0).fluidState();
847 cellPressures[ix] = fs.pressure(pressIx).value();
848 cellTemperatures[ix] = fs.temperature(0).value();
851 this->simulator_.vanguard().grid().comm());
853 this->wellState().init(cellPressures, cellTemperatures, this->schedule(), this->wells_ecl_,
854 this->local_parallel_well_info_, timeStepIdx,
855 &this->prevWellState(), this->well_perf_data_,
856 this->summaryState(), simulator_.vanguard().enableDistributedWells());
863 template<
typename TypeTag>
868 auto logger_guard = this->groupStateHelper().pushLogger();
869 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
871 const int nw = this->numLocalWells();
873 well_container_.clear();
876 well_container_.reserve(nw);
878 const auto& wmatcher = this->schedule().wellMatcher(report_step);
879 const auto& wcycle = this->schedule()[report_step].wcycle.get();
883 std::for_each(this->wells_ecl_.begin(), this->wells_ecl_.end(),
884 [
this, &wg_events = this->report_step_start_events_](
const auto& well_ecl)
886 if (!well_ecl.hasConnections()) {
891 constexpr auto events_mask = ScheduleEvents::WELL_STATUS_CHANGE |
892 ScheduleEvents::REQUEST_OPEN_WELL |
893 ScheduleEvents::REQUEST_SHUT_WELL;
894 const bool well_event =
895 this->report_step_starts_ &&
896 wg_events.hasEvent(well_ecl.name(), events_mask);
905 if (well_ecl.getStatus() == WellStatus::OPEN) {
906 this->well_open_times_.insert_or_assign(well_ecl.name(),
907 this->simulator_.time());
908 this->well_close_times_.erase(well_ecl.name());
909 }
else if (well_ecl.getStatus() == WellStatus::SHUT) {
910 this->well_close_times_.insert_or_assign(well_ecl.name(),
911 this->simulator_.time());
912 this->well_open_times_.erase(well_ecl.name());
918 const auto cycle_states = wcycle.wellStatus(this->simulator_.time(),
920 this->well_open_times_,
921 this->well_close_times_);
923 for (
int w = 0; w < nw; ++w) {
924 const Well& well_ecl = this->wells_ecl_[w];
926 if (!well_ecl.hasConnections()) {
931 const std::string& well_name = well_ecl.name();
932 const auto well_status = this->schedule()
933 .getWell(well_name, report_step).getStatus();
935 const bool shut_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
936 && well_status == Well::Status::SHUT;
937 const bool open_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
938 && well_status == Well::Status::OPEN;
939 const auto& ws = this->wellState().well(well_name);
941 if (shut_event && ws.status != Well::Status::SHUT) {
942 this->closed_this_step_.insert(well_name);
943 this->wellState().shutWell(w);
944 }
else if (open_event && ws.status != Well::Status::OPEN) {
945 this->wellState().openWell(w);
949 if (this->wellTestState().well_is_closed(well_name)) {
954 const bool closed_this_step = (this->wellTestState().lastTestTime(well_name) == simulator_.time());
957 auto& events = this->wellState().well(w).events;
958 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
959 if (!closed_this_step) {
960 this->wellTestState().open_well(well_name);
961 this->wellTestState().open_completions(well_name);
962 this->well_open_times_.insert_or_assign(well_name,
963 this->simulator_.time());
964 this->well_close_times_.erase(well_name);
966 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
972 if (this->wellTestState().well_is_closed(well_name))
974 if (well_ecl.getAutomaticShutIn()) {
976 this->wellState().shutWell(w);
977 this->well_close_times_.erase(well_name);
978 this->well_open_times_.erase(well_name);
981 if (!well_ecl.getAllowCrossFlow()) {
984 this->wellState().shutWell(w);
985 this->well_close_times_.erase(well_name);
986 this->well_open_times_.erase(well_name);
990 this->wellState().stopWell(w);
995 if (!well_ecl.getAllowCrossFlow()) {
996 const bool any_zero_rate_constraint = well_ecl.isProducer()
997 ? well_ecl.productionControls(this->summaryState_).anyZeroRateConstraint()
998 : well_ecl.injectionControls(this->summaryState_).anyZeroRateConstraint();
999 if (any_zero_rate_constraint) {
1001 local_deferredLogger.debug(fmt::format(
" Well {} gets shut due to having zero rate constraint and disallowing crossflow ", well_ecl.name()) );
1002 this->wellState().shutWell(w);
1003 this->well_close_times_.erase(well_name);
1004 this->well_open_times_.erase(well_name);
1009 if (!wcycle.empty()) {
1010 const auto it = cycle_states.find(well_name);
1011 if (it != cycle_states.end()) {
1012 if (!it->second || well_status == Well::Status::SHUT) {
1014 if (well_status == Well::Status::SHUT) {
1015 this->well_open_times_.erase(well_name);
1016 this->well_close_times_.erase(well_name);
1018 this->wellState().shutWell(w);
1021 this->wellState().openWell(w);
1027 if (ws.status == Well::Status::SHUT) {
1031 well_container_.emplace_back(this->createWellPointer(w, report_step));
1033 if (ws.status == Well::Status::STOP) {
1034 well_container_.back()->stopWell();
1035 this->well_close_times_.erase(well_name);
1036 this->well_open_times_.erase(well_name);
1040 if (!wcycle.empty()) {
1041 const auto schedule_open =
1042 [&wg_events = this->report_step_start_events_](
const std::string& name)
1044 return wg_events.hasEvent(name, ScheduleEvents::REQUEST_OPEN_WELL);
1046 for (
const auto& [wname, wscale] : wcycle.efficiencyScale(this->simulator_.time(),
1047 this->simulator_.timeStepSize(),
1049 this->well_open_times_,
1052 this->wellState().updateEfficiencyScalingFactor(wname, wscale);
1053 this->schedule_.add_event(ScheduleEvents::WELLGROUP_EFFICIENCY_UPDATE, report_step);
1058 this->well_container_generic_.clear();
1059 for (
auto& w : well_container_) {
1060 this->well_container_generic_.push_back(w.get());
1063 this->network_.initialize(report_step);
1065 this->wbp_.registerOpenWellsForWBPCalculation();
1072 template <
typename TypeTag>
1073 typename BlackoilWellModel<TypeTag>::WellInterfacePtr
1077 const auto is_multiseg = this->wells_ecl_[wellID].isMultiSegment();
1079 if (! (this->param_.use_multisegment_well_ && is_multiseg)) {
1080 return this->
template createTypedWellPointer<StandardWell<TypeTag>>(wellID, report_step);
1083 return this->
template createTypedWellPointer<MultisegmentWell<TypeTag>>(wellID, report_step);
1091 template <
typename TypeTag>
1092 template <
typename WellType>
1093 std::unique_ptr<WellType>
1098 const auto& perf_data = this->well_perf_data_[wellID];
1101 const auto pvtreg = perf_data.
empty()
1102 ? 0 : this->pvt_region_idx_[perf_data.front().cell_index];
1104 const auto& parallel_well_info = this->local_parallel_well_info_[wellID].get();
1105 const auto global_pvtreg = parallel_well_info.broadcastFirstPerforationValue(pvtreg);
1107 return std::make_unique<WellType>(this->wells_ecl_[wellID],
1111 *this->rateConverter_,
1113 this->numConservationQuantities(),
1123 template<
typename TypeTag>
1127 const int report_step,
1131 const auto it = std::find_if(this->wells_ecl_.begin(),
1132 this->wells_ecl_.end(),
1133 [&well_name](
const auto& w)
1134 { return well_name == w.name(); });
1136 if (it == this->wells_ecl_.end()) {
1138 fmt::format(
"Could not find well {} in wells_ecl ", well_name),
1142 const int pos =
static_cast<int>(std::distance(this->wells_ecl_.begin(), it));
1143 return this->createWellPointer(pos, report_step);
1148 template<
typename TypeTag>
1155 auto logger_guard = this->groupStateHelper().pushLogger();
1156 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
1159 if (gaslift_.terminalOutput()) {
1160 const std::string msg =
1161 fmt::format(
"assemble() : iteration {}" , iterationIdx);
1162 gaslift_.gliftDebug(msg, local_deferredLogger);
1166 Dune::Timer perfTimer;
1168 this->closed_offending_wells_.clear();
1171 const int episodeIdx = simulator_.episodeIndex();
1172 const auto& network = this->schedule()[episodeIdx].network();
1173 if (!this->wellsActive() && !network.active()) {
1178 if (iterationIdx == 0 && this->wellsActive()) {
1179 OPM_TIMEBLOCK(firstIterationAssmble);
1186 calculateExplicitQuantities();
1187 prepareTimeStep(local_deferredLogger);
1190 "assemble() failed (It=0): ",
1191 this->terminal_output_, grid().comm());
1194 const bool well_group_control_changed = updateWellControlsAndNetwork(
false, dt, local_deferredLogger);
1198 if ( ! this->wellsActive() ) {
1202 assembleWellEqWithoutIteration(dt);
1208 last_report_.well_group_control_changed = well_group_control_changed;
1209 last_report_.assemble_time_well += perfTimer.stop();
1215 template<
typename TypeTag>
1224 bool do_network_update =
true;
1225 bool well_group_control_changed =
false;
1226 Scalar network_imbalance = 0.0;
1228 const std::size_t iteration_to_relax = param_.network_max_strict_outer_iterations_;
1230 const std::size_t max_iteration = param_.network_max_outer_iterations_;
1231 std::size_t network_update_iteration = 0;
1232 network_needs_more_balancing_force_another_newton_iteration_ =
false;
1233 while (do_network_update) {
1234 if (network_update_iteration >= max_iteration ) {
1236 const int episodeIdx = simulator_.episodeIndex();
1237 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1238 if (this->network_.shouldBalance(episodeIdx, iterationIdx + 1)) {
1239 if (this->terminal_output_) {
1240 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update, \n"
1241 "and try again after the next Newton iteration (imbalance = {:.2e} bar, ctrl_change = {})",
1242 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1243 local_deferredLogger.
debug(msg);
1247 network_needs_more_balancing_force_another_newton_iteration_ =
true;
1249 if (this->terminal_output_) {
1250 const std::string msg = fmt::format(
"Maximum of {:d} network iterations has been used and we stop the update. \n"
1251 "The simulator will continue with unconverged network results (imbalance = {:.2e} bar, ctrl_change = {})",
1252 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1253 local_deferredLogger.
info(msg);
1258 if (this->terminal_output_ && (network_update_iteration == iteration_to_relax) ) {
1259 local_deferredLogger.
debug(
"We begin using relaxed tolerance for network update now after " +
std::to_string(iteration_to_relax) +
" iterations ");
1261 const bool relax_network_balance = network_update_iteration >= iteration_to_relax;
1263 const bool optimize_gas_lift = ( (network_update_iteration + 1) < std::max(max_iteration,
static_cast<std::size_t
>(2)) );
1264 std::tie(well_group_control_changed, do_network_update, network_imbalance) =
1265 updateWellControlsAndNetworkIteration(mandatory_network_balance, relax_network_balance, optimize_gas_lift, dt,local_deferredLogger);
1266 ++network_update_iteration;
1268 return well_group_control_changed;
1274 template<
typename TypeTag>
1275 std::tuple<bool, bool, typename BlackoilWellModel<TypeTag>::Scalar>
1278 const bool relax_network_tolerance,
1279 const bool optimize_gas_lift,
1284 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1285 const int reportStepIdx = simulator_.episodeIndex();
1286 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx,
1287 param_.nupcol_group_rate_tolerance_,
true);
1292 bool well_group_control_changed = updateWellControls(local_deferredLogger);
1293 const auto [more_inner_network_update, network_imbalance] =
1294 this->network_.update(mandatory_network_balance,
1295 local_deferredLogger,
1296 relax_network_tolerance);
1298 bool alq_updated =
false;
1301 if (optimize_gas_lift) {
1304 const bool updatePotentials = (this->network_.shouldBalance(reportStepIdx, iterationIdx) ||
1305 mandatory_network_balance);
1306 alq_updated = gaslift_.maybeDoGasLiftOptimize(simulator_,
1308 this->network_.nodePressures(),
1312 local_deferredLogger);
1314 prepareWellsBeforeAssembling(dt);
1317 "updateWellControlsAndNetworkIteration() failed: ",
1318 this->terminal_output_, grid().comm());
1322 guideRateUpdateIsNeeded(reportStepIdx)) {
1323 const double simulationTime = simulator_.time();
1327 this->guide_rate_handler_.updateGuideRates(
1328 reportStepIdx, simulationTime, this->wellState(), this->groupState()
1333 const bool more_network_update = this->network_.shouldBalance(reportStepIdx, iterationIdx) &&
1334 (more_inner_network_update || well_group_control_changed || alq_updated);
1335 return {well_group_control_changed, more_network_update, network_imbalance};
1338 template<
typename TypeTag>
1344 for (
auto& well : well_container_) {
1345 well->assembleWellEq(simulator_, dt, this->groupStateHelper(), this->wellState());
1350 template<
typename TypeTag>
1356 for (
auto& well : well_container_) {
1357 well->prepareWellBeforeAssembling(
1358 simulator_, dt, this->groupStateHelper(), this->wellState()
1364 template<
typename TypeTag>
1370 auto& deferred_logger = this->groupStateHelper().deferredLogger();
1375 for (
auto& well: well_container_) {
1376 well->assembleWellEqWithoutIteration(simulator_, this->groupStateHelper(), dt, this->wellState(),
1380 this->terminal_output_, grid().comm());
1384 template<
typename TypeTag>
1391 for (
const auto& well : well_container_) {
1392 well->addCellRates(cellRates_);
1396 template<
typename TypeTag>
1403 for (
const auto& well : well_container_) {
1404 const auto it = well_domain_map.find(well->name());
1405 if (it != well_domain_map.end() && it->second == domainIndex) {
1406 well->addCellRates(cellRates_);
1411#if COMPILE_GPU_BRIDGE
1412 template<
typename TypeTag>
1420 for(
unsigned int i = 0; i < well_container_.size(); i++){
1421 auto& well = well_container_[i];
1424 wellContribs.
addNumBlocks(derived->linSys().getNumBlocks());
1429 wellContribs.
alloc();
1431 for(
unsigned int i = 0; i < well_container_.size(); i++){
1432 auto& well = well_container_[i];
1434 auto derived_std =
dynamic_cast<StandardWell<TypeTag>*
>(well.get());
1436 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1438 auto derived_ms =
dynamic_cast<MultisegmentWell<TypeTag>*
>(well.get());
1440 derived_ms->linSys().extract(wellContribs);
1442 OpmLog::warning(
"Warning unknown type of well");
1449 template<
typename TypeTag>
1454 for (
const auto& well: well_container_ ) {
1459 template<
typename TypeTag>
1464 const bool use_well_weights)
const
1466 int nw = this->numLocalWellsEnd();
1467 int rdofs = local_num_cells_;
1468 for (
int i = 0; i < nw; i++ ) {
1469 int wdof = rdofs + i;
1470 jacobian[wdof][wdof] = 1.0;
1473 for (
const auto& well : well_container_) {
1474 well->addWellPressureEquations(jacobian,
1482 template <
typename TypeTag>
1485 const std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress)
const
1490 for (
const auto& well : well_container_) {
1491 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1494 const auto& cells = well->cells();
1495 const auto& rates = well->connectionRates();
1496 for (
unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1497 unsigned cellIdx = cells[perfIdx];
1498 auto rate = rates[perfIdx];
1501 using MatrixBlockType =
typename SparseMatrixAdapter::MatrixBlock;
1502 MatrixBlockType bMat(0.0);
1503 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1504 residual[cellIdx] += res;
1505 *diagMatAddress[cellIdx] += bMat;
1511 template<
typename TypeTag>
1516 int nw = this->numLocalWellsEnd();
1517 int rdofs = local_num_cells_;
1518 for (
int i = 0; i < nw; ++i) {
1519 int wdof = rdofs + i;
1520 jacobian.entry(wdof,wdof) = 1.0;
1522 const auto wellconnections = this->getMaxWellConnections();
1523 for (
int i = 0; i < nw; ++i) {
1524 const auto& perfcells = wellconnections[i];
1525 for (
int perfcell : perfcells) {
1526 int wdof = rdofs + i;
1527 jacobian.entry(wdof, perfcell) = 0.0;
1528 jacobian.entry(perfcell, wdof) = 0.0;
1534 template<
typename TypeTag>
1539 auto loggerGuard = this->groupStateHelper().pushLogger();
1542 for (
const auto& well : well_container_) {
1543 const auto& cells = well->cells();
1544 x_local_.resize(cells.size());
1546 for (
size_t i = 0; i < cells.size(); ++i) {
1547 x_local_[i] = x[cells[i]];
1549 well->recoverWellSolutionAndUpdateWellState(simulator_, x_local_,
1550 this->groupStateHelper(), this->wellState());
1554 simulator_.vanguard().grid().comm());
1558 template<
typename TypeTag>
1564 OPM_THROW(std::logic_error,
"Attempt to call NLDD method without a NLDD solver");
1567 return nldd_->recoverWellSolutionAndUpdateWellState(x, domainIdx);
1571 template<
typename TypeTag>
1574 getWellConvergence(
const std::vector<Scalar>& B_avg,
bool checkWellGroupControlsAndNetwork)
const
1578 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1580 auto logger_guard = this->groupStateHelper().pushLogger();
1581 for (
const auto& well : well_container_) {
1582 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1583 local_report += well->getWellConvergence(
1584 this->groupStateHelper(), B_avg,
1585 iterationIdx > param_.strict_outer_iter_wells_);
1589 report.
setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1590 local_report += report;
1598 if (checkWellGroupControlsAndNetwork) {
1604 if (this->terminal_output_) {
1607 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1608 OpmLog::debug(
"NaN residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1609 }
else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1610 OpmLog::debug(
"Too large residual found with phase " +
std::to_string(f.phase()) +
" for well " + f.wellName());
1621 template<
typename TypeTag>
1627 for (
auto& well : well_container_) {
1636 template<
typename TypeTag>
1642 if (!this->wellsActive()) {
1645 const int episodeIdx = simulator_.episodeIndex();
1646 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1647 const auto& comm = simulator_.vanguard().grid().comm();
1649 bool changed_well_group =
false;
1650 const Group& fieldGroup = this->schedule().getGroup(
"FIELD", episodeIdx);
1653 const std::size_t max_iter = param_.well_group_constraints_max_iterations_;
1654 while(!changed_well_group && iter < max_iter) {
1655 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1658 bool changed_well_to_group =
false;
1660 OPM_TIMEBLOCK(UpdateWellControls);
1664 for (
const auto& well : well_container_) {
1667 simulator_, mode, this->groupStateHelper(), this->wellState()
1670 changed_well_to_group = changed_well || changed_well_to_group;
1674 simulator_.gridView().comm());
1677 changed_well_to_group = comm.sum(
static_cast<int>(changed_well_to_group));
1678 if (changed_well_to_group) {
1679 updateAndCommunicate(episodeIdx, iterationIdx);
1680 changed_well_group =
true;
1684 bool changed_well_individual =
false;
1689 for (
const auto& well : well_container_) {
1692 simulator_, mode, this->groupStateHelper(), this->wellState()
1695 changed_well_individual = changed_well || changed_well_individual;
1699 simulator_.gridView().comm());
1702 changed_well_individual = comm.sum(
static_cast<int>(changed_well_individual));
1703 if (changed_well_individual) {
1704 updateAndCommunicate(episodeIdx, iterationIdx);
1705 changed_well_group =
true;
1711 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1713 return changed_well_group;
1717 template<
typename TypeTag>
1721 const int iterationIdx)
1723 this->updateAndCommunicateGroupData(reportStepIdx,
1725 param_.nupcol_group_rate_tolerance_,
1732 for (
const auto& well : well_container_) {
1734 const auto& ws = this->wellState().well(well->indexOfWell());
1735 if (ws.production_cmode == Well::ProducerCMode::GRUP ||
1736 ws.injection_cmode == Well::InjectorCMode::GRUP)
1738 well->updateWellStateWithTarget(
1739 simulator_, this->groupStateHelper(), this->wellState()
1744 simulator_.gridView().comm())
1745 this->updateAndCommunicateGroupData(reportStepIdx,
1747 param_.nupcol_group_rate_tolerance_,
1751 template<
typename TypeTag>
1756 const int reportStepIdx,
1757 const int iterationIdx)
1760 bool changed =
false;
1762 const int nupcol = this->schedule()[reportStepIdx].nupcol();
1763 const int max_number_of_group_switches = param_.max_number_of_group_switches_;
1764 const bool update_group_switching_log = iterationIdx >= nupcol;
1765 const bool changed_hc = this->checkGroupHigherConstraints(group, deferred_logger, reportStepIdx, max_number_of_group_switches, update_group_switching_log);
1768 updateAndCommunicate(reportStepIdx, iterationIdx);
1771 bool changed_individual =
1773 updateGroupIndividualControl(group,
1775 max_number_of_group_switches,
1776 update_group_switching_log,
1777 this->switched_inj_groups_,
1778 this->switched_prod_groups_,
1779 this->closed_offending_wells_,
1784 if (changed_individual) {
1786 updateAndCommunicate(reportStepIdx, iterationIdx);
1789 for (
const std::string& groupName : group.groups()) {
1790 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
1791 changed = changed || changed_this;
1796 template<
typename TypeTag>
1802 auto logger_guard = this->groupStateHelper().pushLogger();
1803 auto& local_deferredLogger = this->groupStateHelper().deferredLogger();
1804 for (
const auto& well : well_container_) {
1805 const auto& wname = well->name();
1806 const auto wasClosed = wellTestState.well_is_closed(wname);
1807 well->checkWellOperability(simulator_,
1809 this->groupStateHelper());
1810 const bool under_zero_target =
1811 well->wellUnderZeroGroupRateTarget(this->groupStateHelper());
1812 well->updateWellTestState(this->wellState().well(wname),
1817 local_deferredLogger);
1819 if (!wasClosed && wellTestState.well_is_closed(wname)) {
1820 this->closed_this_step_.insert(wname);
1823 const WellEconProductionLimits& econ_production_limits = well->wellEcl().getEconLimits();
1824 if (econ_production_limits.validFollowonWell()) {
1825 const auto episode_idx = simulator_.episodeIndex();
1826 const auto follow_on_well = econ_production_limits.followonWell();
1827 if (!this->schedule().hasWell(follow_on_well, episode_idx)) {
1828 const auto msg = fmt::format(
"Well {} was closed. But the given follow on well {} does not exist."
1829 "The simulator continues without opening a follow on well.",
1830 wname, follow_on_well);
1831 local_deferredLogger.warning(msg);
1833 auto& ws = this->wellState().well(follow_on_well);
1834 const bool success = ws.updateStatus(WellStatus::OPEN);
1836 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} opens instead.", wname, follow_on_well);
1837 local_deferredLogger.info(msg);
1839 const auto msg = fmt::format(
"Well {} was closed. The follow on well {} is already open.", wname, follow_on_well);
1840 local_deferredLogger.warning(msg);
1847 for (
const auto& [group_name, to] : this->closed_offending_wells_) {
1848 if (this->hasOpenLocalWell(to.second) &&
1849 !this->wasDynamicallyShutThisTimeStep(to.second))
1851 wellTestState.close_well(to.second,
1852 WellTestConfig::Reason::GROUP,
1854 this->updateClosedWellsThisStep(to.second);
1855 const std::string msg =
1856 fmt::format(
"Procedure on exceeding {} limit is WELL for group {}. "
1862 local_deferredLogger.info(msg);
1868 template<
typename TypeTag>
1872 std::string& exc_msg,
1876 const int np = this->numPhases();
1877 std::vector<Scalar> potentials;
1878 const auto& well = well_container_[widx];
1879 std::string cur_exc_msg;
1882 well->computeWellPotentials(simulator_, well_state_copy, this->groupStateHelper(), potentials);
1887 exc_msg += fmt::format(
"\nFor well {}: {}", well->name(), cur_exc_msg);
1889 exc_type = std::max(exc_type, cur_exc_type);
1893 auto& ws = this->wellState().well(well->indexOfWell());
1894 for (
int p = 0; p < np; ++p) {
1896 ws.well_potentials[p] = std::max(
Scalar{0.0}, potentials[p]);
1902 template <
typename TypeTag>
1907 for (
const auto& wellPtr : this->well_container_) {
1908 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
1916 template <
typename TypeTag>
1927 for (
const auto& shutWell : this->local_shut_wells_) {
1928 if (!this->wells_ecl_[shutWell].hasConnections()) {
1933 auto wellPtr = this->
template createTypedWellPointer
1936 wellPtr->
init(this->depth_, this->gravity_, this->B_avg_,
true);
1938 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
1946 template <
typename TypeTag>
1960 template<
typename TypeTag>
1966 const auto episodeIdx = simulator_.episodeIndex();
1967 this->network_.updateActiveState(episodeIdx);
1971 const bool do_prestep_network_rebalance =
1972 param_.pre_solve_network_ && this->network_.needPreStepRebalance(episodeIdx);
1974 for (
const auto& well : well_container_) {
1975 auto& events = this->wellState().well(well->indexOfWell()).events;
1977 well->updateWellStateWithTarget(
1978 simulator_, this->groupStateHelper(), this->wellState()
1986 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
1987 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
1990 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
1992 well->solveWellEquation(
1993 simulator_, this->groupStateHelper(), this->wellState()
1995 }
catch (
const std::exception& e) {
1996 const std::string msg =
"Compute initial well solution for " + well->name() +
" initially failed. Continue with the previous rates";
1997 deferred_logger.
warning(
"WELL_INITIAL_SOLVE_FAILED", msg);
2002 well->resetWellOperability();
2004 updatePrimaryVariables();
2007 if (do_prestep_network_rebalance) {
2008 network_.doPreStepRebalance(deferred_logger);
2012 template<
typename TypeTag>
2017 std::vector< Scalar > B_avg(numConservationQuantities(),
Scalar() );
2018 const auto& grid = simulator_.vanguard().grid();
2019 const auto& gridView = grid.leafGridView();
2023 for (
const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2024 elemCtx.updatePrimaryStencil(elem);
2025 elemCtx.updatePrimaryIntensiveQuantities(0);
2027 const auto& intQuants = elemCtx.intensiveQuantities(0, 0);
2028 const auto& fs = intQuants.fluidState();
2030 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2032 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2036 const unsigned compIdx = FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
2037 auto& B = B_avg[ compIdx ];
2039 B += 1 / fs.invB(phaseIdx).value();
2041 if constexpr (has_solvent_) {
2042 auto& B = B_avg[solventSaturationIdx];
2043 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2049 grid.comm().sum(B_avg.data(), B_avg.size());
2050 B_avg_.resize(B_avg.size());
2051 std::transform(B_avg.begin(), B_avg.end(), B_avg_.begin(),
2052 [gcells = global_num_cells_](
const auto bval)
2053 { return bval / gcells; });
2060 template<
typename TypeTag>
2065 for (
const auto& well : well_container_) {
2070 template<
typename TypeTag>
2074 const auto& grid = simulator_.vanguard().
grid();
2075 const auto& eclProblem = simulator_.problem();
2076 const unsigned numCells = grid.size(0);
2078 this->pvt_region_idx_.resize(numCells);
2079 for (
unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2080 this->pvt_region_idx_[cellIdx] =
2081 eclProblem.pvtRegionIndex(cellIdx);
2086 template<
typename TypeTag>
2099 return this->numPhases() + has_solvent_;
2102 template<
typename TypeTag>
2106 const auto& eclProblem = simulator_.problem();
2107 depth_.resize(local_num_cells_);
2108 for (
unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2109 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2113 template<
typename TypeTag>
2116 getWell(
const std::string& well_name)
const
2119 auto well = std::find_if(well_container_.begin(),
2120 well_container_.end(),
2122 return elem->name() == well_name;
2125 assert(well != well_container_.end());
2130 template <
typename TypeTag>
2135 return std::max(this->simulator_.episodeIndex(), 0);
2142 template<
typename TypeTag>
2147 const std::vector<Scalar>& production_rates,
2148 std::vector<Scalar>& resv_coeff)
const
2150 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2153 template<
typename TypeTag>
2158 std::vector<Scalar>& resv_coeff)
const
2160 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2164 template <
typename TypeTag>
2169 if constexpr (energyModuleType_ == EnergyModules::FullyImplicitThermal ||
2170 energyModuleType_ == EnergyModules::SequentialImplicitThermal) {
2171 int np = this->numPhases();
2172 Scalar cellInternalEnergy;
2176 const int nw = this->numLocalWells();
2177 for (
auto wellID = 0*nw; wellID < nw; ++wellID) {
2178 const Well& well = this->wells_ecl_[wellID];
2179 auto& ws = this->wellState().well(wellID);
2180 if (well.isInjector()) {
2181 if (ws.status != WellStatus::STOP) {
2182 this->wellState().well(wellID).temperature = well.inj_temperature();
2187 std::array<Scalar,2> weighted{0.0,0.0};
2188 auto& [weighted_temperature, total_weight] = weighted;
2190 auto& well_info = this->local_parallel_well_info_[wellID].get();
2191 auto& perf_data = ws.perf_data;
2192 auto& perf_phase_rate = perf_data.phase_rates;
2194 using int_type =
decltype(this->well_perf_data_[wellID].size());
2195 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2196 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2197 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, 0);
2198 const auto& fs = intQuants.fluidState();
2201 Scalar cellTemperatures = fs.temperature(0).value();
2203 Scalar weight_factor = 0.0;
2204 for (
unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
2205 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2208 cellInternalEnergy = fs.enthalpy(phaseIdx).value() -
2209 fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2210 cellBinv = fs.invB(phaseIdx).value();
2211 cellDensity = fs.density(phaseIdx).value();
2212 perfPhaseRate = perf_phase_rate[perf*np + phaseIdx];
2213 weight_factor += cellDensity * perfPhaseRate / cellBinv * cellInternalEnergy / cellTemperatures;
2215 weight_factor = std::abs(weight_factor) + 1e-13;
2216 total_weight += weight_factor;
2217 weighted_temperature += weight_factor * cellTemperatures;
2219 well_info.communication().sum(weighted.data(), 2);
2220 this->wellState().well(wellID).temperature = weighted_temperature / total_weight;
2226 template <
typename TypeTag>
2230 const auto reportStepIdx =
static_cast<unsigned int>(this->reportStepIndex());
2231 const auto& trMod = this->simulator_.problem().tracerModel();
2237 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:96
const Parallel::Communication & comm() const
Definition: BlackoilWellModelGeneric.hpp:223
BlackoilWellModelWBP< GetPropType< TypeTag, Properties::Scalar >, GetPropType< TypeTag, Properties::FluidSystem >::IndexTraitsType > wbp_
Definition: BlackoilWellModelGeneric.hpp:519
std::vector< ParallelWellInfo< GetPropType< TypeTag, Properties::Scalar > > > parallel_well_info_
Definition: BlackoilWellModelGeneric.hpp:546
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:294
void init()
Definition: BlackoilWellModel_impl.hpp:163
const Simulator & simulator() const
Definition: BlackoilWellModel.hpp:370
std::vector< Scalar > depth_
Definition: BlackoilWellModel.hpp:494
std::size_t global_num_cells_
Definition: BlackoilWellModel.hpp:490
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModel.hpp:107
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:103
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:129
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: BlackoilWellModel.hpp:104
GetPropType< TypeTag, Properties::Grid > Grid
Definition: BlackoilWellModel.hpp:101
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModel.hpp:106
void initializeWellState(const int timeStepIdx)
Definition: BlackoilWellModel_impl.hpp:823
const Grid & grid() const
Definition: BlackoilWellModel.hpp:367
void updatePrimaryVariables()
Definition: BlackoilWellModel_impl.hpp:2063
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:701
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1452
void calculateExplicitQuantities() const
Definition: BlackoilWellModel_impl.hpp:1624
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > PressureMatrix
Definition: BlackoilWellModel.hpp:289
bool empty() const
Definition: BlackoilWellModel.hpp:334
void computeTotalRatesForDof(RateVector &rate, unsigned globalIdx) const
Definition: BlackoilWellModel_impl.hpp:786
void beginTimeStep()
Definition: BlackoilWellModel_impl.hpp:326
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: BlackoilWellModel.hpp:108
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:248
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:130
BlackoilWellModel(Simulator &simulator)
Definition: BlackoilWellModel_impl.hpp:76
void wellTesting(const int timeStepIdx, const double simulationTime, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:626
typename FluidSystem::IndexTraitsType IndexTraits
Definition: BlackoilWellModel.hpp:114
std::size_t local_num_cells_
Definition: BlackoilWellModel.hpp:492
bool alternative_well_rate_init_
Definition: BlackoilWellModel.hpp:495
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:711
Simulator & simulator_
Definition: BlackoilWellModel.hpp:464
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:866
std::unique_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:187
int compressedIndexForInterior(int cartesian_cell_idx) const override
get compressed index for interior cells (-1, otherwise
Definition: BlackoilWellModel.hpp:342
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:684
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:80
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: 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:189
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
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