22#include <opm/common/Exceptions.hpp>
24#include <opm/input/eclipse/Schedule/ScheduleTypes.hpp>
25#include <opm/simulators/utils/DeferredLoggingErrorHelpers.hpp>
26#include <opm/simulators/wells/GroupState.hpp>
27#include <opm/simulators/wells/TargetCalculator.hpp>
28#include <opm/simulators/wells/WellBhpThpCalculator.hpp>
29#include <opm/simulators/wells/WellHelpers.hpp>
31#include <dune/common/version.hh>
33#include <fmt/format.h>
39 template<
typename TypeTag>
46 const int pvtRegionIdx,
50 const std::vector<PerforationData>& perf_data)
62 connectionRates_.resize(this->number_of_perforations_);
64 if constexpr (has_solvent || has_zFraction) {
65 if (well.isInjector()) {
68 this->wsolvent_ = this->well_ecl_.getSolventFraction();
75 template<
typename TypeTag>
79 const std::vector<double>& ,
82 const std::vector< Scalar >&
B_avg,
94 template<
typename TypeTag>
96 WellInterface<TypeTag>::
99 if constexpr (has_polymer) {
100 return this->wpolymer_();
110 template<
typename TypeTag>
112 WellInterface<TypeTag>::
115 if constexpr (has_foam) {
116 return this->wfoam_();
124 template<
typename TypeTag>
126 WellInterface<TypeTag>::
129 if constexpr (has_brine) {
130 return this->wsalt_();
136 template<
typename TypeTag>
138 WellInterface<TypeTag>::
141 if constexpr (has_micp) {
142 return this->wmicrobes_();
148 template<
typename TypeTag>
150 WellInterface<TypeTag>::
153 if constexpr (has_micp) {
154 return this->woxygen_();
166 template<
typename TypeTag>
168 WellInterface<TypeTag>::
171 if constexpr (has_micp) {
172 return this->wurea_();
178 template<
typename TypeTag>
180 WellInterface<TypeTag>::
181 updateWellControl(
const Simulator& ebos_simulator,
182 const IndividualOrGroup iog,
183 WellState& well_state,
184 const GroupState& group_state,
185 DeferredLogger& deferred_logger)
187 const auto& summary_state = ebos_simulator.vanguard().summaryState();
188 if (this->stopppedOrZeroRateTarget(summary_state, well_state)) {
192 const auto& summaryState = ebos_simulator.vanguard().summaryState();
193 const auto& schedule = ebos_simulator.vanguard().schedule();
194 const auto& well = this->well_ecl_;
195 auto& ws = well_state.well(this->index_of_well_);
197 if (well.isInjector()) {
198 from = WellInjectorCMode2String(ws.injection_cmode);
200 from = WellProducerCMode2String(ws.production_cmode);
202 bool oscillating = std::count(this->well_control_log_.begin(), this->well_control_log_.end(), from) >= param_.max_number_of_well_switches_;
206 bool output = std::count(this->well_control_log_.begin(), this->well_control_log_.end(), from) == param_.max_number_of_well_switches_;
208 std::ostringstream ss;
209 ss <<
" The control model for well " << this->name()
210 <<
" is oscillating\n"
211 <<
" We don't allow for more than "
212 << param_.max_number_of_well_switches_
213 <<
" switches. The control is kept at " << from;
214 deferred_logger.info(ss.str());
216 this->well_control_log_.push_back(from);
220 bool changed =
false;
221 if (iog == IndividualOrGroup::Individual) {
222 changed = this->checkIndividualConstraints(ws, summaryState, deferred_logger);
223 }
else if (iog == IndividualOrGroup::Group) {
224 changed = this->checkGroupConstraints(well_state, group_state, schedule, summaryState, deferred_logger);
226 assert(iog == IndividualOrGroup::Both);
227 changed = this->checkConstraints(well_state, group_state, schedule, summaryState, deferred_logger);
229 Parallel::Communication cc = ebos_simulator.vanguard().grid().comm();
233 if (well.isInjector()) {
234 to = WellInjectorCMode2String(ws.injection_cmode);
236 to = WellProducerCMode2String(ws.production_cmode);
238 std::ostringstream ss;
239 ss <<
" Switching control mode for well " << this->name()
243 ss <<
" on rank " << cc.rank();
245 deferred_logger.debug(ss.str());
247 this->well_control_log_.push_back(from);
248 updateWellStateWithTarget(ebos_simulator, group_state, well_state, deferred_logger);
249 updatePrimaryVariables(summaryState, well_state, deferred_logger);
257 template<
typename TypeTag>
259 WellInterface<TypeTag>::
260 wellTesting(
const Simulator& simulator,
261 const double simulation_time,
262 WellState& well_state,
263 const GroupState& group_state,
264 WellTestState& well_test_state,
265 DeferredLogger& deferred_logger)
267 deferred_logger.info(
" well " + this->name() +
" is being tested");
269 WellState well_state_copy = well_state;
270 auto& ws = well_state_copy.well(this->indexOfWell());
272 updateWellStateWithTarget(simulator, group_state, well_state_copy, deferred_logger);
273 calculateExplicitQuantities(simulator, well_state_copy, deferred_logger);
274 const auto& summary_state = simulator.vanguard().summaryState();
275 updatePrimaryVariables(summary_state, well_state_copy, deferred_logger);
276 initPrimaryVariablesEvaluation();
278 if (this->isProducer()) {
279 gliftBeginTimeStepWellTestUpdateALQ(simulator, well_state_copy, deferred_logger);
282 WellTestState welltest_state_temp;
284 bool testWell =
true;
289 const size_t original_number_closed_completions = welltest_state_temp.num_closed_completions();
290 bool converged = solveWellForTesting(simulator, well_state_copy, group_state, deferred_logger);
292 const auto msg = fmt::format(
"WTEST: Well {} is not solvable (physical)", this->name());
293 deferred_logger.debug(msg);
298 updateWellOperability(simulator, well_state_copy, deferred_logger);
299 if ( !this->isOperableAndSolvable() ) {
300 const auto msg = fmt::format(
"WTEST: Well {} is not operable (physical)", this->name());
301 deferred_logger.debug(msg);
304 std::vector<double> potentials;
306 computeWellPotentials(simulator, well_state_copy, potentials, deferred_logger);
307 }
catch (
const std::exception& e) {
308 const std::string msg = std::string(
"well ") + this->name() + std::string(
": computeWellPotentials() failed during testing for re-opening: ") + e.what();
309 deferred_logger.info(msg);
312 const int np = well_state_copy.numPhases();
313 for (
int p = 0; p < np; ++p) {
314 ws.well_potentials[p] = std::max(0.0, potentials[p]);
316 this->updateWellTestState(well_state_copy.well(this->indexOfWell()), simulation_time,
false, welltest_state_temp, deferred_logger);
317 this->closeCompletions(welltest_state_temp);
323 if ( welltest_state_temp.num_closed_wells() > 0 ||
324 (original_number_closed_completions == welltest_state_temp.num_closed_completions()) ) {
330 if (!welltest_state_temp.well_is_closed(this->name())) {
331 well_test_state.open_well(this->name());
333 std::string msg = std::string(
"well ") + this->name() + std::string(
" is re-opened");
334 deferred_logger.info(msg);
337 for (
auto& completion : this->well_ecl_.getCompletions()) {
338 if (!welltest_state_temp.completion_is_closed(this->name(), completion.first))
339 well_test_state.open_completion(this->name(), completion.first);
343 well_state = well_state_copy;
350 template<
typename TypeTag>
352 WellInterface<TypeTag>::
353 iterateWellEquations(
const Simulator& ebosSimulator,
355 WellState& well_state,
356 const GroupState& group_state,
357 DeferredLogger& deferred_logger)
359 const auto& summary_state = ebosSimulator.vanguard().summaryState();
360 const auto inj_controls = this->well_ecl_.isInjector() ? this->well_ecl_.injectionControls(summary_state) : Well::InjectionControls(0);
361 const auto prod_controls = this->well_ecl_.isProducer() ? this->well_ecl_.productionControls(summary_state) : Well::ProductionControls(0);
362 bool converged =
false;
364 converged = this->iterateWellEqWithControl(ebosSimulator, dt, inj_controls, prod_controls, well_state, group_state, deferred_logger);
365 }
catch (NumericalProblem& e ) {
366 const std::string msg =
"Inner well iterations failed for well " + this->name() +
" Treat the well as unconverged. ";
367 deferred_logger.warning(
"INNER_ITERATION_FAILED", msg);
374 template<
typename TypeTag>
376 WellInterface<TypeTag>::
377 solveWellForTesting(
const Simulator& ebosSimulator, WellState& well_state,
const GroupState& group_state,
378 DeferredLogger& deferred_logger)
381 const WellState well_state0 = well_state;
382 const double dt = ebosSimulator.timeStepSize();
383 const auto& summary_state = ebosSimulator.vanguard().summaryState();
384 const bool has_thp_limit = this->wellHasTHPConstraints(summary_state);
386 well_state.well(this->indexOfWell()).production_cmode = Well::ProducerCMode::THP;
388 well_state.well(this->indexOfWell()).production_cmode = Well::ProducerCMode::BHP;
390 const bool converged = iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
392 deferred_logger.debug(
"WellTest: Well equation for well " + this->name() +
" converged");
395 const int max_iter = param_.max_welleq_iter_;
396 deferred_logger.debug(
"WellTest: Well equation for well " + this->name() +
" failed converging in "
397 + std::to_string(max_iter) +
" iterations");
398 well_state = well_state0;
403 template<
typename TypeTag>
405 WellInterface<TypeTag>::
406 solveWellEquation(
const Simulator& ebosSimulator,
407 WellState& well_state,
408 const GroupState& group_state,
409 DeferredLogger& deferred_logger)
411 if (!this->isOperableAndSolvable() && !this->wellIsStopped())
415 const WellState well_state0 = well_state;
416 const double dt = ebosSimulator.timeStepSize();
417 bool converged = iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
427 auto& ws = well_state.well(this->indexOfWell());
428 bool thp_control =
false;
429 if (this->well_ecl_.isInjector()) {
430 thp_control = ws.injection_cmode == Well::InjectorCMode::THP;
432 ws.injection_cmode = Well::InjectorCMode::BHP;
433 this->well_control_log_.push_back(WellInjectorCMode2String(Well::InjectorCMode::THP));
436 thp_control = ws.production_cmode == Well::ProducerCMode::THP;
438 ws.production_cmode = Well::ProducerCMode::BHP;
439 this->well_control_log_.push_back(WellProducerCMode2String(Well::ProducerCMode::THP));
443 const std::string msg = std::string(
"The newly opened well ") + this->name()
444 + std::string(
" with THP control did not converge during inner iterations, we try again with bhp control");
445 deferred_logger.debug(msg);
446 converged = this->iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
451 const int max_iter = param_.max_welleq_iter_;
452 deferred_logger.debug(
"Compute initial well solution for well " + this->name() +
". Failed to converge in "
453 + std::to_string(max_iter) +
" iterations");
454 well_state = well_state0;
460 template <
typename TypeTag>
462 WellInterface<TypeTag>::
463 assembleWellEq(
const Simulator& ebosSimulator,
465 WellState& well_state,
466 const GroupState& group_state,
467 DeferredLogger& deferred_logger)
469 const bool old_well_operable = this->operability_status_.isOperableAndSolvable();
471 if (param_.check_well_operability_iter_)
472 checkWellOperability(ebosSimulator, well_state, deferred_logger);
475 const int iteration_idx = ebosSimulator.model().newtonMethod().numIterations();
476 if (iteration_idx < param_.max_niter_inner_well_iter_ || this->well_ecl_.isMultiSegment()) {
477 this->operability_status_.solvable =
true;
478 bool converged = this->iterateWellEquations(ebosSimulator, dt, well_state, group_state, deferred_logger);
482 if (param_.shut_unsolvable_wells_)
483 this->operability_status_.solvable =
false;
486 if (this->operability_status_.has_negative_potentials) {
487 auto well_state_copy = well_state;
488 std::vector<double> potentials;
490 computeWellPotentials(ebosSimulator, well_state_copy, potentials, deferred_logger);
491 }
catch (
const std::exception& e) {
492 const std::string msg = std::string(
"well ") + this->name() + std::string(
": computeWellPotentials() failed during attempt to recompute potentials for well : ") + e.what();
493 deferred_logger.info(msg);
494 this->operability_status_.has_negative_potentials =
true;
496 auto& ws = well_state.well(this->indexOfWell());
497 const int np = well_state.numPhases();
498 for (
int p = 0; p < np; ++p) {
499 ws.well_potentials[p] = std::max(0.0, potentials[p]);
502 this->changed_to_open_this_step_ =
false;
503 const bool well_operable = this->operability_status_.isOperableAndSolvable();
505 if (!well_operable && old_well_operable) {
506 if (this->well_ecl_.getAutomaticShutIn()) {
507 deferred_logger.info(
" well " + this->name() +
" gets SHUT during iteration ");
509 if (!this->wellIsStopped()) {
510 deferred_logger.info(
" well " + this->name() +
" gets STOPPED during iteration ");
512 changed_to_stopped_this_step_ =
true;
515 }
else if (well_operable && !old_well_operable) {
516 deferred_logger.info(
" well " + this->name() +
" gets REVIVED during iteration ");
518 changed_to_stopped_this_step_ =
false;
519 this->changed_to_open_this_step_ =
true;
522 const auto& summary_state = ebosSimulator.vanguard().summaryState();
523 const auto inj_controls = this->well_ecl_.isInjector() ? this->well_ecl_.injectionControls(summary_state) : Well::InjectionControls(0);
524 const auto prod_controls = this->well_ecl_.isProducer() ? this->well_ecl_.productionControls(summary_state) : Well::ProductionControls(0);
525 assembleWellEqWithoutIteration(ebosSimulator, dt, inj_controls, prod_controls, well_state, group_state, deferred_logger);
528 template<
typename TypeTag>
530 WellInterface<TypeTag>::addCellRates(RateVector& rates,
int cellIdx)
const
532 if(!this->isOperableAndSolvable() && !this->wellIsStopped())
535 for (
int perfIdx = 0; perfIdx < this->number_of_perforations_; ++perfIdx) {
536 if (this->cells()[perfIdx] == cellIdx) {
537 for (
int i = 0; i < RateVector::dimension; ++i) {
538 rates[i] += connectionRates_[perfIdx][i];
544 template<
typename TypeTag>
545 typename WellInterface<TypeTag>::Scalar
546 WellInterface<TypeTag>::volumetricSurfaceRateForConnection(
int cellIdx,
int phaseIdx)
const {
547 for (
int perfIdx = 0; perfIdx < this->number_of_perforations_; ++perfIdx) {
548 if (this->cells()[perfIdx] == cellIdx) {
549 const unsigned activeCompIdx = Indices::canonicalToActiveComponentIndex(FluidSystem::solventComponentIndex(phaseIdx));
550 return connectionRates_[perfIdx][activeCompIdx].value();
554 OPM_THROW(std::invalid_argument,
"The well with name " + this->name()
555 +
" does not perforate cell " + std::to_string(cellIdx));
562 template<
typename TypeTag>
564 WellInterface<TypeTag>::
565 checkWellOperability(
const Simulator& ebos_simulator,
566 const WellState& well_state,
567 DeferredLogger& deferred_logger)
570 if (!param_.check_well_operability_) {
574 if (this->wellIsStopped() && !changed_to_stopped_this_step_) {
578 updateWellOperability(ebos_simulator, well_state, deferred_logger);
579 if (!this->operability_status_.isOperableAndSolvable()) {
580 this->operability_status_.use_vfpexplicit =
true;
581 deferred_logger.debug(
"EXPLICIT_LOOKUP_VFP",
582 "well not operable, trying with explicit vfp lookup: " + this->name());
583 updateWellOperability(ebos_simulator, well_state, deferred_logger);
587 template<
typename TypeTag>
589 WellInterface<TypeTag>::
590 gliftBeginTimeStepWellTestUpdateALQ(
const Simulator& ebos_simulator,
591 WellState& well_state,
592 DeferredLogger& deferred_logger)
594 const auto& summary_state = ebos_simulator.vanguard().summaryState();
595 const auto& well_name = this->name();
596 if (!this->wellHasTHPConstraints(summary_state)) {
597 const std::string msg = fmt::format(
"GLIFT WTEST: Well {} does not have THP constraints", well_name);
598 deferred_logger.info(msg);
601 const auto& well_ecl = this->wellEcl();
602 const auto& schedule = ebos_simulator.vanguard().schedule();
603 auto report_step_idx = ebos_simulator.episodeIndex();
604 const auto& glo = schedule.glo(report_step_idx);
605 if (!glo.has_well(well_name)) {
606 const std::string msg = fmt::format(
607 "GLIFT WTEST: Well {} : Gas Lift not activated: "
608 "WLIFTOPT is probably missing. Skipping.", well_name);
609 deferred_logger.info(msg);
612 const auto& gl_well = glo.well(well_name);
613 auto& max_alq_optional = gl_well.max_rate();
615 if (max_alq_optional) {
616 max_alq = *max_alq_optional;
619 const auto& controls = well_ecl.productionControls(summary_state);
620 const auto& table = this->vfpProperties()->getProd()->getTable(controls.vfp_table_number);
621 const auto& alq_values = table.getALQAxis();
622 max_alq = alq_values.back();
624 well_state.setALQ(well_name, max_alq);
625 const std::string msg = fmt::format(
626 "GLIFT WTEST: Well {} : Setting ALQ to max value: {}",
628 deferred_logger.info(msg);
631 template<
typename TypeTag>
633 WellInterface<TypeTag>::
634 updateWellOperability(
const Simulator& ebos_simulator,
635 const WellState& well_state,
636 DeferredLogger& deferred_logger)
638 this->operability_status_.resetOperability();
640 bool thp_controlled = this->isInjector() ? well_state.well(this->index_of_well_).injection_cmode == Well::InjectorCMode::THP:
641 well_state.well(this->index_of_well_).production_cmode == Well::ProducerCMode::THP;
642 bool bhp_controlled = this->isInjector() ? well_state.well(this->index_of_well_).injection_cmode == Well::InjectorCMode::BHP:
643 well_state.well(this->index_of_well_).production_cmode == Well::ProducerCMode::BHP;
647 bool check_thp = thp_controlled || this->operability_status_.thp_limit_violated_but_not_switched;
648 if (check_thp || bhp_controlled) {
649 updateIPR(ebos_simulator, deferred_logger);
650 checkOperabilityUnderBHPLimit(well_state, ebos_simulator, deferred_logger);
654 checkOperabilityUnderTHPLimit(ebos_simulator, well_state, deferred_logger);
659 template<
typename TypeTag>
661 WellInterface<TypeTag>::
662 updateWellStateWithTarget(
const Simulator& ebos_simulator,
663 const GroupState& group_state,
664 WellState& well_state,
665 DeferredLogger& deferred_logger)
const
669 const auto& well = this->well_ecl_;
670 const int well_index = this->index_of_well_;
671 auto& ws = well_state.well(well_index);
673 const int np = well_state.numPhases();
674 const auto& summaryState = ebos_simulator.vanguard().summaryState();
675 const auto& schedule = ebos_simulator.vanguard().schedule();
677 if (this->wellIsStopped()) {
678 for (
int p = 0; p<np; ++p) {
679 ws.surface_rates[p] = 0;
685 if (this->isInjector() )
687 const auto& controls = well.injectionControls(summaryState);
689 InjectorType injectorType = controls.injector_type;
691 switch (injectorType) {
692 case InjectorType::WATER:
694 phasePos = pu.phase_pos[BlackoilPhases::Aqua];
697 case InjectorType::OIL:
699 phasePos = pu.phase_pos[BlackoilPhases::Liquid];
702 case InjectorType::GAS:
704 phasePos = pu.phase_pos[BlackoilPhases::Vapour];
708 OPM_DEFLOG_THROW(std::runtime_error,
"Expected WATER, OIL or GAS as type for injectors " + this->name(), deferred_logger );
711 const auto current = ws.injection_cmode;
714 case Well::InjectorCMode::RATE:
716 ws.surface_rates[phasePos] = (1.0 - this->rsRvInj()) * controls.surface_rate;
717 if(this->rsRvInj() > 0) {
718 if (injectorType == InjectorType::OIL && FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
719 ws.surface_rates[pu.phase_pos[BlackoilPhases::Vapour]] = controls.surface_rate * this->rsRvInj();
720 }
else if (injectorType == InjectorType::GAS && FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
721 ws.surface_rates[pu.phase_pos[BlackoilPhases::Liquid]] = controls.surface_rate * this->rsRvInj();
723 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 );
729 case Well::InjectorCMode::RESV:
731 std::vector<double> convert_coeff(this->number_of_phases_, 1.0);
732 this->rateConverter_.calcCoeff( 0, this->pvtRegionIdx_, convert_coeff);
733 const double coeff = convert_coeff[phasePos];
734 ws.surface_rates[phasePos] = controls.reservoir_rate/coeff;
738 case Well::InjectorCMode::THP:
740 auto rates = ws.surface_rates;
741 double bhp = WellBhpThpCalculator(*this).calculateBhpFromThp(well_state,
745 this->getRefDensity(),
748 ws.thp = this->getTHPConstraint(summaryState);
753 double total_rate = std::accumulate(rates.begin(), rates.end(), 0.0);
754 if (total_rate <= 0.0)
755 ws.surface_rates = ws.well_potentials;
759 case Well::InjectorCMode::BHP:
761 ws.bhp = controls.bhp_limit;
762 double total_rate = 0.0;
763 for (
int p = 0; p<np; ++p) {
764 total_rate += ws.surface_rates[p];
769 if (total_rate <= 0.0)
770 ws.surface_rates = ws.well_potentials;
774 case Well::InjectorCMode::GRUP:
776 assert(well.isAvailableForGroupControl());
777 const auto& group = schedule.getGroup(well.groupName(), this->currentStep());
778 const double efficiencyFactor = well.getEfficiencyFactor();
779 std::optional<double> target =
780 this->getGroupInjectionTargetRate(group,
789 ws.surface_rates[phasePos] = *target;
792 case Well::InjectorCMode::CMODE_UNDEFINED:
794 OPM_DEFLOG_THROW(std::runtime_error,
"Well control must be specified for well " + this->name(), deferred_logger );
804 ws.surface_rates[phasePos] = std::max(1.e-7, ws.surface_rates[phasePos]);
809 const auto current = ws.production_cmode;
810 const auto& controls = well.productionControls(summaryState);
812 case Well::ProducerCMode::ORAT:
814 double current_rate = -ws.surface_rates[ pu.phase_pos[Oil] ];
817 if (current_rate > 0.0) {
818 for (
int p = 0; p<np; ++p) {
819 ws.surface_rates[p] *= controls.oil_rate/current_rate;
822 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
823 double control_fraction = fractions[pu.phase_pos[Oil]];
824 if (control_fraction != 0.0) {
825 for (
int p = 0; p<np; ++p) {
826 ws.surface_rates[p] = - fractions[p] * controls.oil_rate/control_fraction;
832 case Well::ProducerCMode::WRAT:
834 double current_rate = -ws.surface_rates[ pu.phase_pos[Water] ];
837 if (current_rate > 0.0) {
838 for (
int p = 0; p<np; ++p) {
839 ws.surface_rates[p] *= controls.water_rate/current_rate;
842 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
843 double control_fraction = fractions[pu.phase_pos[Water]];
844 if (control_fraction != 0.0) {
845 for (
int p = 0; p<np; ++p) {
846 ws.surface_rates[p] = - fractions[p] * controls.water_rate/control_fraction;
852 case Well::ProducerCMode::GRAT:
854 double current_rate = -ws.surface_rates[pu.phase_pos[Gas] ];
857 if (current_rate > 0.0) {
858 for (
int p = 0; p<np; ++p) {
859 ws.surface_rates[p] *= controls.gas_rate/current_rate;
862 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
863 double control_fraction = fractions[pu.phase_pos[Gas]];
864 if (control_fraction != 0.0) {
865 for (
int p = 0; p<np; ++p) {
866 ws.surface_rates[p] = - fractions[p] * controls.gas_rate/control_fraction;
874 case Well::ProducerCMode::LRAT:
876 double current_rate = -ws.surface_rates[ pu.phase_pos[Water] ]
877 - ws.surface_rates[ pu.phase_pos[Oil] ];
880 if (current_rate > 0.0) {
881 for (
int p = 0; p<np; ++p) {
882 ws.surface_rates[p] *= controls.liquid_rate/current_rate;
885 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
886 double control_fraction = fractions[pu.phase_pos[Water]] + fractions[pu.phase_pos[Oil]];
887 if (control_fraction != 0.0) {
888 for (
int p = 0; p<np; ++p) {
889 ws.surface_rates[p] = - fractions[p] * controls.liquid_rate / control_fraction;
895 case Well::ProducerCMode::CRAT:
897 OPM_DEFLOG_THROW(std::runtime_error,
898 fmt::format(
"CRAT control not supported, well {}", this->name()),
901 case Well::ProducerCMode::RESV:
903 std::vector<double> convert_coeff(this->number_of_phases_, 1.0);
904 this->rateConverter_.calcCoeff( 0, this->pvtRegionIdx_, ws.surface_rates, convert_coeff);
905 double total_res_rate = 0.0;
906 for (
int p = 0; p<np; ++p) {
907 total_res_rate -= ws.surface_rates[p] * convert_coeff[p];
909 if (controls.prediction_mode) {
912 if (total_res_rate > 0.0) {
913 for (
int p = 0; p<np; ++p) {
914 ws.surface_rates[p] *= controls.resv_rate/total_res_rate;
917 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
918 for (
int p = 0; p<np; ++p) {
919 ws.surface_rates[p] = - fractions[p] * controls.resv_rate / convert_coeff[p];
923 std::vector<double> hrates(this->number_of_phases_,0.);
924 if (FluidSystem::phaseIsActive(FluidSystem::waterPhaseIdx)) {
925 hrates[pu.phase_pos[Water]] = controls.water_rate;
927 if (FluidSystem::phaseIsActive(FluidSystem::oilPhaseIdx)) {
928 hrates[pu.phase_pos[Oil]] = controls.oil_rate;
930 if (FluidSystem::phaseIsActive(FluidSystem::gasPhaseIdx)) {
931 hrates[pu.phase_pos[Gas]] = controls.gas_rate;
933 std::vector<double> hrates_resv(this->number_of_phases_,0.);
934 this->rateConverter_.calcReservoirVoidageRates( 0, this->pvtRegionIdx_, hrates, hrates_resv);
935 double target = std::accumulate(hrates_resv.begin(), hrates_resv.end(), 0.0);
938 if (total_res_rate > 0.0) {
939 for (
int p = 0; p<np; ++p) {
940 ws.surface_rates[p] *= target/total_res_rate;
943 const std::vector<double> fractions = initialWellRateFractions(ebos_simulator, well_state);
944 for (
int p = 0; p<np; ++p) {
945 ws.surface_rates[p] = - fractions[p] * target / convert_coeff[p];
952 case Well::ProducerCMode::BHP:
954 ws.bhp = controls.bhp_limit;
955 double total_rate = 0.0;
956 for (
int p = 0; p<np; ++p) {
957 total_rate -= ws.surface_rates[p];
962 if (total_rate <= 0.0){
963 for (
int p = 0; p<np; ++p) {
964 ws.surface_rates[p] = -ws.well_potentials[p];
969 case Well::ProducerCMode::THP:
971 const bool update_success = updateWellStateWithTHPTargetProd(ebos_simulator, well_state, deferred_logger);
973 if (!update_success) {
977 auto rates = ws.surface_rates;
978 this->adaptRatesForVFP(rates);
979 const double bhp = WellBhpThpCalculator(*this).calculateBhpFromThp(
980 well_state, rates, well, summaryState, this->getRefDensity(), deferred_logger);
982 ws.thp = this->getTHPConstraint(summaryState);
986 const double total_rate = -std::accumulate(rates.begin(), rates.end(), 0.0);
987 if (total_rate <= 0.0) {
988 for (
int p = 0; p < this->number_of_phases_; ++p) {
989 ws.surface_rates[p] = -ws.well_potentials[p];
995 case Well::ProducerCMode::GRUP:
997 assert(well.isAvailableForGroupControl());
998 const auto& group = schedule.getGroup(well.groupName(), this->currentStep());
999 const double efficiencyFactor = well.getEfficiencyFactor();
1000 double scale = this->getGroupProductionTargetRate(group,
1010 for (
int p = 0; p<np; ++p) {
1011 ws.surface_rates[p] *= scale;
1013 ws.trivial_target =
false;
1015 ws.trivial_target =
true;
1019 case Well::ProducerCMode::CMODE_UNDEFINED:
1020 case Well::ProducerCMode::NONE:
1022 OPM_DEFLOG_THROW(std::runtime_error,
"Well control must be specified for well " + this->name() , deferred_logger);
1030 template<
typename TypeTag>
1032 WellInterface<TypeTag>::
1033 initialWellRateFractions(
const Simulator& ebosSimulator,
const WellState& well_state)
const
1035 const int np = this->number_of_phases_;
1036 std::vector<double> scaling_factor(np);
1037 const auto& ws = well_state.well(this->index_of_well_);
1039 double total_potentials = 0.0;
1040 for (
int p = 0; p<np; ++p) {
1041 total_potentials += ws.well_potentials[p];
1043 if (total_potentials > 0) {
1044 for (
int p = 0; p<np; ++p) {
1045 scaling_factor[p] = ws.well_potentials[p] / total_potentials;
1047 return scaling_factor;
1051 double total_tw = 0;
1052 const int nperf = this->number_of_perforations_;
1053 for (
int perf = 0; perf < nperf; ++perf) {
1054 total_tw += this->well_index_[perf];
1056 for (
int perf = 0; perf < nperf; ++perf) {
1057 const int cell_idx = this->well_cells_[perf];
1058 const auto& intQuants = ebosSimulator.model().intensiveQuantities(cell_idx, 0);
1059 const auto& fs = intQuants.fluidState();
1060 const double well_tw_fraction = this->well_index_[perf] / total_tw;
1061 double total_mobility = 0.0;
1062 for (
int p = 0; p < np; ++p) {
1063 int ebosPhaseIdx = this->flowPhaseToEbosPhaseIdx(p);
1064 total_mobility += fs.invB(ebosPhaseIdx).value() * intQuants.mobility(ebosPhaseIdx).value();
1066 for (
int p = 0; p < np; ++p) {
1067 int ebosPhaseIdx = this->flowPhaseToEbosPhaseIdx(p);
1068 scaling_factor[p] += well_tw_fraction * fs.invB(ebosPhaseIdx).value() * intQuants.mobility(ebosPhaseIdx).value() / total_mobility;
1071 return scaling_factor;
1076 template <
typename TypeTag>
1085 auto&
ws = well_state.well(this->index_of_well_);
1088 for (
int p = 0;
p < this->number_of_phases_; ++
p) {
1105 for (
int p = 0;
p < this->number_of_phases_; ++
p) {
1106 ws.surface_rates[
p] =
well_q_s[this->flowPhaseToEbosCompIdx(
p)];
1114 for (
int p = 0;
p < this->number_of_phases_; ++
p) {
1116 const int comp_idx = this->flowPhaseToEbosCompIdx(
p);
1117 double&
rate =
ws.surface_rates[
p];
1122 template<
typename TypeTag>
1123 typename WellInterface<TypeTag>::Eval
1126 if constexpr (Indices::oilEnabled) {
1127 return fs.pressure(FluidSystem::oilPhaseIdx);
1128 }
else if constexpr (Indices::waterEnabled) {
1129 return fs.pressure(FluidSystem::waterPhaseIdx);
1131 return fs.pressure(FluidSystem::gasPhaseIdx);
Definition AquiferInterface.hpp:35
Definition DeferredLogger.hpp:57
Class encapsulating some information about parallel wells.
Definition ParallelWellInfo.hpp:184
Definition WellInterfaceIndices.hpp:33
Definition WellInterface.hpp:74
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:1079
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:41
The state of a set of wells, tailored for use by the fully implicit blackoil simulator.
Definition WellState.hpp:60
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