22#include <opm/input/eclipse/Schedule/ScheduleTypes.hpp>
23#include <opm/simulators/utils/DeferredLoggingErrorHelpers.hpp>
24#include <opm/simulators/wells/GroupState.hpp>
25#include <opm/simulators/wells/TargetCalculator.hpp>
27#include <dune/common/version.hh>
33 template<
typename TypeTag>
39 const RateConverterType& rate_converter,
40 const int pvtRegionIdx,
41 const int num_components,
43 const int index_of_well,
44 const std::vector<PerforationData>& perf_data)
56 connectionRates_.resize(this->number_of_perforations_);
58 if constexpr (has_solvent || has_zFraction) {
59 if (well.isInjector()) {
60 auto injectorType = this->well_ecl_.injectorType();
61 if (injectorType == InjectorType::GAS) {
62 this->wsolvent_ = this->well_ecl_.getSolventFraction();
69 template<
typename TypeTag>
73 const std::vector<double>& ,
74 const double gravity_arg,
76 const std::vector< Scalar >& B_avg,
77 const bool changed_to_open_this_step)
79 this->phase_usage_ = phase_usage_arg;
80 this->gravity_ = gravity_arg;
82 this->changed_to_open_this_step_ = changed_to_open_this_step;
88 template<
typename TypeTag>
90 WellInterface<TypeTag>::
93 if constexpr (has_polymer) {
94 auto injectorType = this->well_ecl_.injectorType();
96 if (injectorType == InjectorType::WATER) {
97 WellPolymerProperties polymer = this->well_ecl_.getPolymerProperties();
98 const double polymer_injection_concentration = polymer.m_polymerConcentration;
99 return polymer_injection_concentration;
113 template<
typename TypeTag>
115 WellInterface<TypeTag>::
118 if constexpr (has_foam) {
119 auto injectorType = this->well_ecl_.injectorType();
121 if (injectorType == InjectorType::GAS) {
122 WellFoamProperties fprop = this->well_ecl_.getFoamProperties();
123 return fprop.m_foamConcentration;
135 template<
typename TypeTag>
137 WellInterface<TypeTag>::
140 if constexpr (has_brine) {
141 auto injectorType = this->well_ecl_.injectorType();
143 if (injectorType == InjectorType::WATER) {
144 WellBrineProperties fprop = this->well_ecl_.getBrineProperties();
145 return fprop.m_saltConcentration;
155 template<
typename TypeTag>
157 WellInterface<TypeTag>::
160 if constexpr (has_micp) {
161 auto injectorType = this->well_ecl_.injectorType();
163 if (injectorType == InjectorType::WATER) {
164 WellMICPProperties microbes = this->well_ecl_.getMICPProperties();
165 const double microbial_injection_concentration = microbes.m_microbialConcentration;
166 return microbial_injection_concentration;
176 template<
typename TypeTag>
178 WellInterface<TypeTag>::
181 if constexpr (has_micp) {
182 auto injectorType = this->well_ecl_.injectorType();
184 if (injectorType == InjectorType::WATER) {
185 WellMICPProperties oxygen = this->well_ecl_.getMICPProperties();
186 const double oxygen_injection_concentration = oxygen.m_oxygenConcentration;
187 return oxygen_injection_concentration;
203 template<
typename TypeTag>
205 WellInterface<TypeTag>::
208 if constexpr (has_micp) {
209 auto injectorType = this->well_ecl_.injectorType();
211 if (injectorType == InjectorType::WATER) {
212 WellMICPProperties urea = this->well_ecl_.getMICPProperties();
213 const double urea_injection_concentration = urea.m_ureaConcentration / 10.;
214 return urea_injection_concentration;
224 template<
typename TypeTag>
226 WellInterface<TypeTag>::
227 updateWellControl(
const Simulator& ebos_simulator,
228 const IndividualOrGroup iog,
229 WellState& well_state,
230 const GroupState& group_state,
231 DeferredLogger& deferred_logger)
233 if (this->wellIsStopped()) {
237 const auto& summaryState = ebos_simulator.vanguard().summaryState();
238 const auto& schedule = ebos_simulator.vanguard().schedule();
239 const auto& well = this->well_ecl_;
240 auto& ws = well_state.well(this->index_of_well_);
242 if (well.isInjector()) {
243 from = Well::InjectorCMode2String(ws.injection_cmode);
245 from = Well::ProducerCMode2String(ws.production_cmode);
247 bool oscillating = std::count(this->well_control_log_.begin(), this->well_control_log_.end(), from) >= param_.max_number_of_well_switches_;
251 bool output = std::count(this->well_control_log_.begin(), this->well_control_log_.end(), from) == param_.max_number_of_well_switches_;
253 std::ostringstream ss;
254 ss <<
" The control model for well " << this->name()
255 <<
" is oscillating\n"
256 <<
" We don't allow for more than "
257 << param_.max_number_of_well_switches_
258 <<
" switches. The control is kept at " << from;
259 deferred_logger.info(ss.str());
261 this->well_control_log_.push_back(from);
265 bool changed =
false;
266 if (iog == IndividualOrGroup::Individual) {
267 changed = this->checkIndividualConstraints(ws, summaryState, deferred_logger);
268 }
else if (iog == IndividualOrGroup::Group) {
269 changed = this->checkGroupConstraints(well_state, group_state, schedule, summaryState, deferred_logger);
271 assert(iog == IndividualOrGroup::Both);
272 changed = this->checkConstraints(well_state, group_state, schedule, summaryState, deferred_logger);
274 Parallel::Communication cc = ebos_simulator.vanguard().grid().comm();
278 if (well.isInjector()) {
279 to = Well::InjectorCMode2String(ws.injection_cmode);
281 to = Well::ProducerCMode2String(ws.production_cmode);
283 std::ostringstream ss;
284 ss <<
" Switching control mode for well " << this->name()
288 ss <<
" on rank " << cc.rank();
290 deferred_logger.debug(ss.str());
292 this->well_control_log_.push_back(from);
293 updateWellStateWithTarget(ebos_simulator, group_state, well_state, deferred_logger);
294 updatePrimaryVariables(well_state, deferred_logger);
302 template<
typename TypeTag>
304 WellInterface<TypeTag>::
305 wellTesting(
const Simulator& simulator,
306 const double simulation_time,
307 WellState& well_state,
308 const GroupState& group_state,
309 WellTestState& well_test_state,
310 DeferredLogger& deferred_logger)
312 deferred_logger.info(
" well " + this->name() +
" is being tested");
314 WellState well_state_copy = well_state;
315 auto& ws = well_state_copy.well(this->indexOfWell());
317 updateWellStateWithTarget(simulator, group_state, well_state_copy, deferred_logger);
318 calculateExplicitQuantities(simulator, well_state_copy, deferred_logger);
319 updatePrimaryVariables(well_state_copy, deferred_logger);
320 initPrimaryVariablesEvaluation();
322 if (this->isProducer()) {
323 gliftBeginTimeStepWellTestUpdateALQ(simulator, well_state_copy, deferred_logger);
326 WellTestState welltest_state_temp;
328 bool testWell =
true;
333 const size_t original_number_closed_completions = welltest_state_temp.num_closed_completions();
334 bool converged = solveWellForTesting(simulator, well_state_copy, group_state, deferred_logger);
336 const auto msg = fmt::format(
"WTEST: Well {} is not solvable (physical)", this->name());
337 deferred_logger.debug(msg);
342 updateWellOperability(simulator, well_state_copy, deferred_logger);
343 if ( !this->isOperableAndSolvable() ) {
344 const auto msg = fmt::format(
"WTEST: Well {} is not operable (physical)", this->name());
345 deferred_logger.debug(msg);
349 std::vector<double> potentials;
351 computeWellPotentials(simulator, well_state_copy, potentials, deferred_logger);
352 }
catch (
const std::exception& e) {
353 const std::string msg = std::string(
"well ") + this->name() + std::string(
": computeWellPotentials() failed during testing for re-opening: ") + e.what();
354 deferred_logger.info(msg);
357 const int np = well_state_copy.numPhases();
358 for (
int p = 0; p < np; ++p) {
359 ws.well_potentials[p] = std::max(0.0, potentials[p]);
361 this->updateWellTestState(well_state_copy.well(this->indexOfWell()), simulation_time,
false, welltest_state_temp, deferred_logger);
362 this->closeCompletions(welltest_state_temp);
368 if ( welltest_state_temp.num_closed_wells() > 0 ||
369 (original_number_closed_completions == welltest_state_temp.num_closed_completions()) ) {
375 if (!welltest_state_temp.well_is_closed(this->name())) {
376 well_test_state.open_well(this->name());
378 std::string msg = std::string(
"well ") + this->name() + std::string(
" is re-opened");
379 deferred_logger.info(msg);
382 for (
auto& completion : this->well_ecl_.getCompletions()) {
383 if (!welltest_state_temp.completion_is_closed(this->name(), completion.first))
384 well_test_state.open_completion(this->name(), completion.first);
388 well_state = well_state_copy;
395 template<
typename TypeTag>
397 WellInterface<TypeTag>::
398 iterateWellEquations(
const Simulator& ebosSimulator,
400 WellState& well_state,
401 const GroupState& group_state,
402 DeferredLogger& deferred_logger)
404 const auto& summary_state = ebosSimulator.vanguard().summaryState();
405 const auto inj_controls = this->well_ecl_.isInjector() ? this->well_ecl_.injectionControls(summary_state) : Well::InjectionControls(0);
406 const auto prod_controls = this->well_ecl_.isProducer() ? this->well_ecl_.productionControls(summary_state) : Well::ProductionControls(0);
407 bool converged =
false;
409 converged = this->iterateWellEqWithControl(ebosSimulator, dt, inj_controls, prod_controls, well_state, group_state, deferred_logger);
410 }
catch (NumericalIssue& e ) {
411 const std::string msg =
"Inner well iterations failed for well " + this->name() +
" Treat the well as unconverged. ";
412 deferred_logger.warning(
"INNER_ITERATION_FAILED", msg);
419 template<
typename TypeTag>
421 WellInterface<TypeTag>::
422 solveWellForTesting(
const Simulator& ebosSimulator, WellState& well_state,
const GroupState& group_state,
423 DeferredLogger& deferred_logger)
426 const WellState well_state0 = well_state;
427 const double dt = ebosSimulator.timeStepSize();
428 const auto& summary_state = ebosSimulator.vanguard().summaryState();
429 const bool has_thp_limit = this->wellHasTHPConstraints(summary_state);
431 well_state.well(this->indexOfWell()).production_cmode = Well::ProducerCMode::THP;
433 well_state.well(this->indexOfWell()).production_cmode = Well::ProducerCMode::BHP;
435 const bool converged = iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
437 deferred_logger.debug(
"WellTest: Well equation for well " + this->name() +
" converged");
440 const int max_iter = param_.max_welleq_iter_;
441 deferred_logger.debug(
"WellTest: Well equation for well " + this->name() +
" failed converging in "
442 + std::to_string(max_iter) +
" iterations");
443 well_state = well_state0;
448 template<
typename TypeTag>
450 WellInterface<TypeTag>::
451 solveWellEquation(
const Simulator& ebosSimulator,
452 WellState& well_state,
453 const GroupState& group_state,
454 DeferredLogger& deferred_logger)
456 if (!this->isOperableAndSolvable() && !this->wellIsStopped())
460 const WellState well_state0 = well_state;
461 const double dt = ebosSimulator.timeStepSize();
462 bool converged = iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
472 auto& ws = well_state.well(this->indexOfWell());
473 bool thp_control =
false;
474 if (this->well_ecl_.isInjector()) {
475 thp_control = ws.injection_cmode == Well::InjectorCMode::THP;
477 ws.injection_cmode = Well::InjectorCMode::BHP;
478 this->well_control_log_.push_back(Well::InjectorCMode2String(Well::InjectorCMode::THP));
481 thp_control = ws.production_cmode == Well::ProducerCMode::THP;
483 ws.production_cmode = Well::ProducerCMode::BHP;
484 this->well_control_log_.push_back(Well::ProducerCMode2String(Well::ProducerCMode::THP));
488 const std::string msg = std::string(
"The newly opened well ") + this->name()
489 + std::string(
" with THP control did not converge during inner iterations, we try again with bhp control");
490 deferred_logger.debug(msg);
491 converged = this->iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
496 const int max_iter = param_.max_welleq_iter_;
497 deferred_logger.debug(
"Compute initial well solution for well " + this->name() +
". Failed to converge in "
498 + std::to_string(max_iter) +
" iterations");
499 well_state = well_state0;
505 template <
typename TypeTag>
507 WellInterface<TypeTag>::
508 assembleWellEq(
const Simulator& ebosSimulator,
510 WellState& well_state,
511 const GroupState& group_state,
512 DeferredLogger& deferred_logger)
514 const bool old_well_operable = this->operability_status_.isOperableAndSolvable();
516 if (param_.check_well_operability_iter_)
517 checkWellOperability(ebosSimulator, well_state, deferred_logger);
520 const int iteration_idx = ebosSimulator.model().newtonMethod().numIterations();
521 if (iteration_idx < param_.max_niter_inner_well_iter_ || this->well_ecl_.isMultiSegment()) {
522 this->operability_status_.solvable =
true;
523 bool converged = this->iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
527 if (param_.shut_unsolvable_wells_)
528 this->operability_status_.solvable =
false;
531 if (this->operability_status_.has_negative_potentials) {
532 auto well_state_copy = well_state;
533 std::vector<double> potentials;
535 computeWellPotentials(ebosSimulator, well_state_copy, potentials, deferred_logger);
536 }
catch (
const std::exception& e) {
537 const std::string msg = std::string(
"well ") + this->name() + std::string(
": computeWellPotentials() failed during attempt to recompute potentials for well : ") + e.what();
538 deferred_logger.info(msg);
539 this->operability_status_.has_negative_potentials =
true;
541 auto& ws = well_state.well(this->indexOfWell());
542 const int np = well_state.numPhases();
543 for (
int p = 0; p < np; ++p) {
544 ws.well_potentials[p] = std::max(0.0, potentials[p]);
547 this->changed_to_open_this_step_ =
false;
548 const bool well_operable = this->operability_status_.isOperableAndSolvable();
550 if (!well_operable && old_well_operable) {
551 if (this->well_ecl_.getAutomaticShutIn()) {
552 deferred_logger.info(
" well " + this->name() +
" gets SHUT during iteration ");
554 if (!this->wellIsStopped()) {
555 deferred_logger.info(
" well " + this->name() +
" gets STOPPED during iteration ");
557 changed_to_stopped_this_step_ =
true;
560 }
else if (well_operable && !old_well_operable) {
561 deferred_logger.info(
" well " + this->name() +
" gets REVIVED during iteration ");
563 changed_to_stopped_this_step_ =
false;
564 this->changed_to_open_this_step_ =
true;
567 const auto& summary_state = ebosSimulator.vanguard().summaryState();
568 const auto inj_controls = this->well_ecl_.isInjector() ? this->well_ecl_.injectionControls(summary_state) : Well::InjectionControls(0);
569 const auto prod_controls = this->well_ecl_.isProducer() ? this->well_ecl_.productionControls(summary_state) : Well::ProductionControls(0);
570 assembleWellEqWithoutIteration(ebosSimulator, dt, inj_controls, prod_controls, well_state, group_state, deferred_logger);
573 template<
typename TypeTag>
575 WellInterface<TypeTag>::isPressureControlled(
const WellState& well_state)
const
577 bool thp_controlled_well =
false;
578 bool bhp_controlled_well =
false;
579 const auto& ws = well_state.well(this->index_of_well_);
580 if (this->isInjector()) {
581 const Well::InjectorCMode& current = ws.injection_cmode;
582 if (current == Well::InjectorCMode::THP) {
583 thp_controlled_well =
true;
585 if (current == Well::InjectorCMode::BHP) {
586 bhp_controlled_well =
true;
589 const Well::ProducerCMode& current = ws.production_cmode;
590 if (current == Well::ProducerCMode::THP) {
591 thp_controlled_well =
true;
593 if (current == Well::ProducerCMode::BHP) {
594 bhp_controlled_well =
true;
597 bool ispressureControlled = (bhp_controlled_well || thp_controlled_well);
598 return ispressureControlled;
601 template<
typename TypeTag>
603 WellInterface<TypeTag>::addCellRates(RateVector& rates,
int cellIdx)
const
605 if(!this->isOperableAndSolvable() && !this->wellIsStopped())
608 for (
int perfIdx = 0; perfIdx < this->number_of_perforations_; ++perfIdx) {
609 if (this->cells()[perfIdx] == cellIdx) {
610 for (
int i = 0; i < RateVector::dimension; ++i) {
611 rates[i] += connectionRates_[perfIdx][i];
617 template<
typename TypeTag>
618 typename WellInterface<TypeTag>::Scalar
619 WellInterface<TypeTag>::volumetricSurfaceRateForConnection(
int cellIdx,
int phaseIdx)
const {
620 for (
int perfIdx = 0; perfIdx < this->number_of_perforations_; ++perfIdx) {
621 if (this->cells()[perfIdx] == cellIdx) {
622 const unsigned activeCompIdx = Indices::canonicalToActiveComponentIndex(FluidSystem::solventComponentIndex(phaseIdx));
623 return connectionRates_[perfIdx][activeCompIdx].value();
627 OPM_THROW(std::invalid_argument,
"The well with name " + this->name()
628 +
" does not perforate cell " + std::to_string(cellIdx));
635 template<
typename TypeTag>
637 WellInterface<TypeTag>::
638 checkWellOperability(
const Simulator& ebos_simulator,
639 const WellState& well_state,
640 DeferredLogger& deferred_logger)
643 if (!param_.check_well_operability_) {
647 if (this->wellIsStopped() && !changed_to_stopped_this_step_) {
651 updateWellOperability(ebos_simulator, well_state, deferred_logger);
652 if (!this->operability_status_.isOperableAndSolvable()) {
653 this->operability_status_.use_vfpexplicit =
true;
654 deferred_logger.debug(
"EXPLICIT_LOOKUP_VFP",
655 "well not operable, trying with explicit vfp lookup: " + this->name());
656 updateWellOperability(ebos_simulator, well_state, deferred_logger);
660 template<
typename TypeTag>
662 WellInterface<TypeTag>::
663 gliftBeginTimeStepWellTestUpdateALQ(
const Simulator& ebos_simulator,
664 WellState& well_state,
665 DeferredLogger& deferred_logger)
667 const auto& summary_state = ebos_simulator.vanguard().summaryState();
668 const auto& well_name = this->name();
669 if (!this->wellHasTHPConstraints(summary_state)) {
670 const std::string msg = fmt::format(
"GLIFT WTEST: Well {} does not have THP constraints", well_name);
671 deferred_logger.info(msg);
674 const auto& well_ecl = this->wellEcl();
675 const auto& schedule = ebos_simulator.vanguard().schedule();
676 auto report_step_idx = ebos_simulator.episodeIndex();
677 const auto& glo = schedule.glo(report_step_idx);
678 if (!glo.has_well(well_name)) {
679 const std::string msg = fmt::format(
680 "GLIFT WTEST: Well {} : Gas Lift not activated: "
681 "WLIFTOPT is probably missing. Skipping.", well_name);
682 deferred_logger.info(msg);
685 const auto& gl_well = glo.well(well_name);
686 auto& max_alq_optional = gl_well.max_rate();
688 if (max_alq_optional) {
689 max_alq = *max_alq_optional;
692 const auto& controls = well_ecl.productionControls(summary_state);
693 const auto& table = this->vfpProperties()->getProd()->getTable(controls.vfp_table_number);
694 const auto& alq_values = table.getALQAxis();
695 max_alq = alq_values.back();
697 well_state.setALQ(well_name, max_alq);
698 const std::string msg = fmt::format(
699 "GLIFT WTEST: Well {} : Setting ALQ to max value: {}",
701 deferred_logger.info(msg);
704 template<
typename TypeTag>
706 WellInterface<TypeTag>::
707 updateWellOperability(
const Simulator& ebos_simulator,
708 const WellState& well_state,
709 DeferredLogger& deferred_logger)
711 this->operability_status_.resetOperability();
713 bool thp_controlled = this->isInjector() ? well_state.well(this->index_of_well_).injection_cmode == Well::InjectorCMode::THP:
714 well_state.well(this->index_of_well_).production_cmode == Well::ProducerCMode::THP;
715 bool bhp_controlled = this->isInjector() ? well_state.well(this->index_of_well_).injection_cmode == Well::InjectorCMode::BHP:
716 well_state.well(this->index_of_well_).production_cmode == Well::ProducerCMode::BHP;
720 bool check_thp = thp_controlled || this->operability_status_.thp_limit_violated_but_not_switched;
721 if (check_thp || bhp_controlled) {
722 updateIPR(ebos_simulator, deferred_logger);
723 checkOperabilityUnderBHPLimit(well_state, ebos_simulator, deferred_logger);
727 checkOperabilityUnderTHPLimit(ebos_simulator, well_state, deferred_logger);
732 template<
typename TypeTag>
734 WellInterface<TypeTag>::
735 updateWellStateWithTarget(
const Simulator& ebos_simulator,
736 const GroupState& group_state,
737 WellState& well_state,
738 DeferredLogger& deferred_logger)
const
742 const auto& well = this->well_ecl_;
743 const int well_index = this->index_of_well_;
744 auto& ws = well_state.well(well_index);
746 const int np = well_state.numPhases();
747 const auto& summaryState = ebos_simulator.vanguard().summaryState();
748 const auto& schedule = ebos_simulator.vanguard().schedule();
750 if (this->wellIsStopped()) {
751 for (
int p = 0; p<np; ++p) {
752 ws.surface_rates[p] = 0;
758 if (this->isInjector() )
760 const auto& controls = well.injectionControls(summaryState);
762 InjectorType injectorType = controls.injector_type;
764 switch (injectorType) {
765 case InjectorType::WATER:
767 phasePos = pu.phase_pos[BlackoilPhases::Aqua];
770 case InjectorType::OIL:
772 phasePos = pu.phase_pos[BlackoilPhases::Liquid];
775 case InjectorType::GAS:
777 phasePos = pu.phase_pos[BlackoilPhases::Vapour];
781 OPM_DEFLOG_THROW(std::runtime_error,
"Expected WATER, OIL or GAS as type for injectors " + this->name(), deferred_logger );
784 const auto current = ws.injection_cmode;
787 case Well::InjectorCMode::RATE:
789 ws.surface_rates[phasePos] = (1.0 - this->rsRvInj()) * controls.surface_rate;
790 if(this->rsRvInj() > 0) {
791 if (injectorType == InjectorType::OIL && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
792 ws.surface_rates[pu.phase_pos[BlackoilPhases::Vapour]] = controls.surface_rate * this->rsRvInj();
793 }
else if (injectorType == InjectorType::GAS && FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
794 ws.surface_rates[pu.phase_pos[BlackoilPhases::Liquid]] = controls.surface_rate * this->rsRvInj();
796 OPM_DEFLOG_THROW(std::runtime_error,
"Expected OIL or GAS as type for injectors when RS/RV (item 10) is non-zero " + this->name(), deferred_logger );
802 case Well::InjectorCMode::RESV:
804 std::vector<double> convert_coeff(this->number_of_phases_, 1.0);
805 this->rateConverter_.calcCoeff( 0, this->pvtRegionIdx_, convert_coeff);
806 const double coeff = convert_coeff[phasePos];
807 ws.surface_rates[phasePos] = controls.reservoir_rate/coeff;
811 case Well::InjectorCMode::THP:
813 auto rates = ws.surface_rates;
814 double bhp = this->calculateBhpFromThp(well_state, rates, well, summaryState, this->getRefDensity(), deferred_logger);
816 ws.thp = this->getTHPConstraint(summaryState);
821 double total_rate = std::accumulate(rates.begin(), rates.end(), 0.0);
822 if (total_rate <= 0.0)
823 ws.surface_rates = ws.well_potentials;
827 case Well::InjectorCMode::BHP:
829 ws.bhp = controls.bhp_limit;
830 double total_rate = 0.0;
831 for (
int p = 0; p<np; ++p) {
832 total_rate += ws.surface_rates[p];
837 if (total_rate <= 0.0)
838 ws.surface_rates = ws.well_potentials;
842 case Well::InjectorCMode::GRUP:
844 assert(well.isAvailableForGroupControl());
845 const auto& group = schedule.getGroup(well.groupName(), this->currentStep());
846 const double efficiencyFactor = well.getEfficiencyFactor();
847 std::optional<double> target =
848 this->getGroupInjectionTargetRate(group,
857 ws.surface_rates[phasePos] = *target;
860 case Well::InjectorCMode::CMODE_UNDEFINED:
862 OPM_DEFLOG_THROW(std::runtime_error,
"Well control must be specified for well " + this->name(), deferred_logger );
872 ws.surface_rates[phasePos] = std::max(1.e-7, ws.surface_rates[phasePos]);
877 const auto current = ws.production_cmode;
878 const auto& controls = well.productionControls(summaryState);
880 case Well::ProducerCMode::ORAT:
882 double current_rate = -ws.surface_rates[ pu.phase_pos[Oil] ];
885 if (current_rate > 0.0) {
886 for (
int p = 0; p<np; ++p) {
887 ws.surface_rates[p] *= controls.oil_rate/current_rate;
890 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
891 double control_fraction = fractions[pu.phase_pos[Oil]];
892 if (control_fraction != 0.0) {
893 for (
int p = 0; p<np; ++p) {
894 ws.surface_rates[p] = - fractions[p] * controls.oil_rate/control_fraction;
900 case Well::ProducerCMode::WRAT:
902 double current_rate = -ws.surface_rates[ pu.phase_pos[Water] ];
905 if (current_rate > 0.0) {
906 for (
int p = 0; p<np; ++p) {
907 ws.surface_rates[p] *= controls.water_rate/current_rate;
910 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
911 double control_fraction = fractions[pu.phase_pos[Water]];
912 if (control_fraction != 0.0) {
913 for (
int p = 0; p<np; ++p) {
914 ws.surface_rates[p] = - fractions[p] * controls.water_rate/control_fraction;
920 case Well::ProducerCMode::GRAT:
922 double current_rate = -ws.surface_rates[pu.phase_pos[Gas] ];
925 if (current_rate > 0.0) {
926 for (
int p = 0; p<np; ++p) {
927 ws.surface_rates[p] *= controls.gas_rate/current_rate;
930 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
931 double control_fraction = fractions[pu.phase_pos[Gas]];
932 if (control_fraction != 0.0) {
933 for (
int p = 0; p<np; ++p) {
934 ws.surface_rates[p] = - fractions[p] * controls.gas_rate/control_fraction;
942 case Well::ProducerCMode::LRAT:
944 double current_rate = -ws.surface_rates[ pu.phase_pos[Water] ]
945 - ws.surface_rates[ pu.phase_pos[Oil] ];
948 if (current_rate > 0.0) {
949 for (
int p = 0; p<np; ++p) {
950 ws.surface_rates[p] *= controls.liquid_rate/current_rate;
953 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
954 double control_fraction = fractions[pu.phase_pos[Water]] + fractions[pu.phase_pos[Oil]];
955 if (control_fraction != 0.0) {
956 for (
int p = 0; p<np; ++p) {
957 ws.surface_rates[p] = - fractions[p] * controls.liquid_rate / control_fraction;
963 case Well::ProducerCMode::CRAT:
965 OPM_DEFLOG_THROW(std::runtime_error,
"CRAT control not supported " << this->name(), deferred_logger);
967 case Well::ProducerCMode::RESV:
969 std::vector<double> convert_coeff(this->number_of_phases_, 1.0);
970 this->rateConverter_.calcCoeff( 0, this->pvtRegionIdx_, convert_coeff);
971 double total_res_rate = 0.0;
972 for (
int p = 0; p<np; ++p) {
973 total_res_rate -= ws.surface_rates[p] * convert_coeff[p];
975 if (controls.prediction_mode) {
978 if (total_res_rate > 0.0) {
979 for (
int p = 0; p<np; ++p) {
980 ws.surface_rates[p] *= controls.resv_rate/total_res_rate;
983 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
984 for (
int p = 0; p<np; ++p) {
985 ws.surface_rates[p] = - fractions[p] * controls.resv_rate / convert_coeff[p];
989 std::vector<double> hrates(this->number_of_phases_,0.);
990 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
991 hrates[pu.phase_pos[Water]] = controls.water_rate;
993 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
994 hrates[pu.phase_pos[Oil]] = controls.oil_rate;
996 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
997 hrates[pu.phase_pos[Gas]] = controls.gas_rate;
999 std::vector<double> hrates_resv(this->number_of_phases_,0.);
1000 this->rateConverter_.calcReservoirVoidageRates( 0, this->pvtRegionIdx_, hrates, hrates_resv);
1001 double target = std::accumulate(hrates_resv.begin(), hrates_resv.end(), 0.0);
1004 if (total_res_rate > 0.0) {
1005 for (
int p = 0; p<np; ++p) {
1006 ws.surface_rates[p] *= target/total_res_rate;
1009 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
1010 for (
int p = 0; p<np; ++p) {
1011 ws.surface_rates[p] = - fractions[p] * target / convert_coeff[p];
1018 case Well::ProducerCMode::BHP:
1020 ws.bhp = controls.bhp_limit;
1021 double total_rate = 0.0;
1022 for (
int p = 0; p<np; ++p) {
1023 total_rate -= ws.surface_rates[p];
1028 if (total_rate <= 0.0){
1029 for (
int p = 0; p<np; ++p) {
1030 ws.surface_rates[p] = -ws.well_potentials[p];
1035 case Well::ProducerCMode::THP:
1037 auto rates = ws.surface_rates;
1038 this->adaptRatesForVFP(rates);
1039 double bhp = this->calculateBhpFromThp(well_state, rates, well, summaryState, this->getRefDensity(), deferred_logger);
1041 ws.thp = this->getTHPConstraint(summaryState);
1046 double total_rate = -std::accumulate(rates.begin(), rates.end(), 0.0);
1047 if (total_rate <= 0.0){
1048 for (
int p = 0; p<np; ++p) {
1049 ws.surface_rates[p] = -ws.well_potentials[p];
1054 case Well::ProducerCMode::GRUP:
1056 assert(well.isAvailableForGroupControl());
1057 const auto& group = schedule.getGroup(well.groupName(), this->currentStep());
1058 const double efficiencyFactor = well.getEfficiencyFactor();
1059 double scale = this->getGroupProductionTargetRate(group,
1068 for (
int p = 0; p<np; ++p) {
1069 ws.surface_rates[p] *= scale;
1071 ws.trivial_target =
false;
1073 ws.trivial_target =
true;
1077 case Well::ProducerCMode::CMODE_UNDEFINED:
1078 case Well::ProducerCMode::NONE:
1080 OPM_DEFLOG_THROW(std::runtime_error,
"Well control must be specified for well " + this->name() , deferred_logger);
1088 template<
typename TypeTag>
1090 WellInterface<TypeTag>::
1091 initialWellRateFractions(
const Simulator& ebosSimulator,
const WellState& well_state)
const
1093 const int np = this->number_of_phases_;
1094 std::vector<double> scaling_factor(np);
1095 const auto& ws = well_state.well(this->index_of_well_);
1097 double total_potentials = 0.0;
1098 for (
int p = 0; p<np; ++p) {
1099 total_potentials += ws.well_potentials[p];
1101 if (total_potentials > 0) {
1102 for (
int p = 0; p<np; ++p) {
1103 scaling_factor[p] = ws.well_potentials[p] / total_potentials;
1105 return scaling_factor;
1109 double total_tw = 0;
1110 const int nperf = this->number_of_perforations_;
1111 for (
int perf = 0; perf < nperf; ++perf) {
1112 total_tw += this->well_index_[perf];
1114 for (
int perf = 0; perf < nperf; ++perf) {
1115 const int cell_idx = this->well_cells_[perf];
1116 const auto& intQuants = *(ebosSimulator.model().cachedIntensiveQuantities(cell_idx, 0));
1117 const auto& fs = intQuants.fluidState();
1118 const double well_tw_fraction = this->well_index_[perf] / total_tw;
1119 double total_mobility = 0.0;
1120 for (
int p = 0; p < np; ++p) {
1121 int ebosPhaseIdx = this->flowPhaseToEbosPhaseIdx(p);
1122 total_mobility += fs.invB(ebosPhaseIdx).value() * intQuants.mobility(ebosPhaseIdx).value();
1124 for (
int p = 0; p < np; ++p) {
1125 int ebosPhaseIdx = this->flowPhaseToEbosPhaseIdx(p);
1126 scaling_factor[p] += well_tw_fraction * fs.invB(ebosPhaseIdx).value() * intQuants.mobility(ebosPhaseIdx).value() / total_mobility;
1129 return scaling_factor;
1134 template <
typename TypeTag>
1143 auto& ws = well_state.well(this->index_of_well_);
1144 int nonzero_rate_index = -1;
1145 const double floating_point_error_epsilon = 1e-14;
1146 for (
int p = 0; p < this->number_of_phases_; ++p) {
1147 if (std::abs(ws.surface_rates[p]) > floating_point_error_epsilon) {
1148 if (nonzero_rate_index == -1) {
1149 nonzero_rate_index = p;
1158 std::vector<double> well_q_s = computeCurrentWellRates(ebosSimulator, deferred_logger);
1160 if (nonzero_rate_index == -1) {
1163 for (
int p = 0; p < this->number_of_phases_; ++p) {
1164 ws.surface_rates[p] = well_q_s[this->flowPhaseToEbosCompIdx(p)];
1170 const double initial_nonzero_rate = ws.surface_rates[nonzero_rate_index];
1171 const int comp_idx_nz = this->flowPhaseToEbosCompIdx(nonzero_rate_index);
1172 for (
int p = 0; p < this->number_of_phases_; ++p) {
1173 if (p != nonzero_rate_index) {
1174 const int comp_idx = this->flowPhaseToEbosCompIdx(p);
1175 double& rate = ws.surface_rates[p];
1176 rate = (initial_nonzero_rate/well_q_s[comp_idx_nz]) * (well_q_s[comp_idx]);
1180 template<
typename TypeTag>
1181 typename WellInterface<TypeTag>::Eval
1185 if (Indices::oilEnabled) {
1186 pressure = fs.pressure(FluidSystem::oilPhaseIdx);
1188 if (Indices::waterEnabled) {
1189 pressure = fs.pressure(FluidSystem::waterPhaseIdx);
1191 pressure = fs.pressure(FluidSystem::gasPhaseIdx);
Definition: DeferredLogger.hpp:57
Class encapsulating some information about parallel wells.
Definition: ParallelWellInfo.hpp:243
Definition: WellInterfaceIndices.hpp:35
Definition: WellInterface.hpp:72
void updateWellStateRates(const Simulator &ebosSimulator, WellState &well_state, DeferredLogger &deferred_logger) const
Modify the well_state's rates if there is only one nonzero rate.
Definition: WellInterface_impl.hpp:1137
WellInterface(const Well &well, const ParallelWellInfo &pw_info, const int time_step, const ModelParameters ¶m, const RateConverterType &rate_converter, const int pvtRegionIdx, const int num_components, const int num_phases, const int index_of_well, const std::vector< PerforationData > &perf_data)
Constructor.
Definition: WellInterface_impl.hpp:35
The state of a set of wells, tailored for use by the fully implicit blackoil simulator.
Definition: WellState.hpp:56
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
PhaseUsage phaseUsage(const Phases &phases)
Determine the active phases.
Definition: phaseUsageFromDeck.cpp:37
Solver parameters for the BlackoilModel.
Definition: BlackoilModelParametersEbos.hpp:327
Definition: BlackoilPhases.hpp:46