BlackoilWellModel_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2016 - 2019 SINTEF Digital, Mathematics & Cybernetics.
3 Copyright 2016 - 2018 Equinor ASA.
4 Copyright 2017 Dr. Blatt - HPC-Simulation-Software & Services
5 Copyright 2016 - 2018 Norce AS
6
7 This file is part of the Open Porous Media project (OPM).
8
9 OPM is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OPM is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OPM. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#ifndef OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
24#define OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
25
26// Improve IDE experience
27#ifndef OPM_BLACKOILWELLMODEL_HEADER_INCLUDED
28#include <config.h>
30#endif
31
32#include <opm/grid/utility/cartesianToCompressed.hpp>
33#include <opm/common/utility/numeric/RootFinders.hpp>
34
35#include <opm/input/eclipse/Schedule/Network/Balance.hpp>
36#include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp>
37#include <opm/input/eclipse/Schedule/Well/PAvgDynamicSourceData.hpp>
38#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
39#include <opm/input/eclipse/Schedule/Well/WellTestConfig.hpp>
40#include <opm/input/eclipse/Schedule/Well/WellEconProductionLimits.hpp>
41
42#include <opm/input/eclipse/Units/UnitSystem.hpp>
43
54
57
58#if COMPILE_GPU_BRIDGE
60#endif
61
62#include <algorithm>
63#include <cassert>
64#include <iomanip>
65#include <utility>
66#include <optional>
67
68#include <fmt/format.h>
69
70namespace Opm {
71 template<typename TypeTag>
74 : WellConnectionModule(*this, simulator.gridView().comm())
75 , BlackoilWellModelGeneric<Scalar, IndexTraits>(simulator.vanguard().schedule(),
76 gaslift_,
77 simulator.vanguard().summaryState(),
78 simulator.vanguard().eclState(),
79 FluidSystem::phaseUsage(),
80 simulator.gridView().comm())
81 , simulator_(simulator)
82 , guide_rate_handler_{
83 *this,
84 simulator.vanguard().schedule(),
85 simulator.vanguard().summaryState(),
86 simulator.vanguard().grid().comm()
87 }
88 , gaslift_(this->terminal_output_)
89 {
90 local_num_cells_ = simulator_.gridView().size(0);
91
92 // Number of cells the global grid view
93 global_num_cells_ = simulator_.vanguard().globalNumCells();
94
95 {
96 auto& parallel_wells = simulator.vanguard().parallelWells();
97
98 this->parallel_well_info_.reserve(parallel_wells.size());
99 for( const auto& name_bool : parallel_wells) {
100 this->parallel_well_info_.emplace_back(name_bool, grid().comm());
101 }
102 }
103
105 Parameters::Get<Parameters::AlternativeWellRateInit>();
106
107 using SourceDataSpan =
108 typename PAvgDynamicSourceData<Scalar>::template SourceDataSpan<Scalar>;
109
111 [this](const std::size_t globalIndex)
112 { return this->compressedIndexForInterior(globalIndex); },
113 [this](const int localCell, SourceDataSpan sourceTerms)
114 {
115 using Item = typename SourceDataSpan::Item;
116
117 const auto* intQuants = this->simulator_.model()
118 .cachedIntensiveQuantities(localCell, /*timeIndex = */0);
119 const auto& fs = intQuants->fluidState();
120
121 sourceTerms
122 .set(Item::PoreVol, intQuants->porosity().value() *
123 this->simulator_.model().dofTotalVolume(localCell))
124 .set(Item::Depth, this->depth_[localCell]);
125
126 constexpr auto io = FluidSystem::oilPhaseIdx;
127 constexpr auto ig = FluidSystem::gasPhaseIdx;
128 constexpr auto iw = FluidSystem::waterPhaseIdx;
129
130 // Ideally, these would be 'constexpr'.
131 const auto haveOil = FluidSystem::phaseIsActive(io);
132 const auto haveGas = FluidSystem::phaseIsActive(ig);
133 const auto haveWat = FluidSystem::phaseIsActive(iw);
134
135 auto weightedPhaseDensity = [&fs](const auto ip)
136 {
137 return fs.saturation(ip).value() * fs.density(ip).value();
138 };
139
140 if (haveOil) { sourceTerms.set(Item::Pressure, fs.pressure(io).value()); }
141 else if (haveGas) { sourceTerms.set(Item::Pressure, fs.pressure(ig).value()); }
142 else { sourceTerms.set(Item::Pressure, fs.pressure(iw).value()); }
143
144 // Strictly speaking, assumes SUM(s[p]) == 1.
145 auto rho = 0.0;
146 if (haveOil) { rho += weightedPhaseDensity(io); }
147 if (haveGas) { rho += weightedPhaseDensity(ig); }
148 if (haveWat) { rho += weightedPhaseDensity(iw); }
149
150 sourceTerms.set(Item::MixtureDensity, rho);
151 }
152 );
153 }
154
155 template<typename TypeTag>
156 void
158 init()
159 {
160 extractLegacyCellPvtRegionIndex_();
161 extractLegacyDepth_();
162
163 gravity_ = simulator_.problem().gravity()[2];
164
165 this->initial_step_ = true;
166
167 // add the eWoms auxiliary module for the wells to the list
168 simulator_.model().addAuxiliaryModule(this);
169
170 is_cell_perforated_.resize(local_num_cells_, false);
171 }
172
173
174 template<typename TypeTag>
175 void
177 initWellContainer(const int reportStepIdx)
178 {
179 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
180 + ScheduleEvents::NEW_WELL;
181 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
182 for (auto& wellPtr : this->well_container_) {
183 const bool well_opened_this_step = this->report_step_starts_ &&
184 events.hasEvent(wellPtr->name(),
185 effective_events_mask);
186 wellPtr->init(this->depth_, this->gravity_,
187 this->B_avg_, well_opened_this_step);
188 }
189 }
190
191 template<typename TypeTag>
192 void
194 beginReportStep(const int timeStepIdx)
195 {
196 DeferredLogger local_deferredLogger{};
197
198 this->report_step_starts_ = true;
199 this->report_step_start_events_ = this->schedule()[timeStepIdx].wellgroup_events();
200
201 this->rateConverter_ = std::make_unique<RateConverterType>
202 (std::vector<int>(this->local_num_cells_, 0));
203
204 {
205 // WELPI scaling runs at start of report step.
206 const auto enableWellPIScaling = true;
207 this->initializeLocalWellStructure(timeStepIdx, enableWellPIScaling);
208 }
209
210 this->initializeGroupStructure(timeStepIdx);
211
212 const auto& comm = this->simulator_.vanguard().grid().comm();
213
215 {
216 // Create facility for calculating reservoir voidage volumes for
217 // purpose of RESV controls.
218 this->rateConverter_->template defineState<ElementContext>(this->simulator_);
219
220 // Update VFP properties.
221 {
222 const auto& sched_state = this->schedule()[timeStepIdx];
223
224 this->vfp_properties_ = std::make_unique<VFPProperties<Scalar, IndexTraits>>
225 (sched_state.vfpinj(), sched_state.vfpprod(), this->wellState());
226 }
227 }
228 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
229 "beginReportStep() failed: ",
230 this->terminal_output_, comm)
231
232 // Store the current well and group states in order to recover in
233 // the case of failed iterations
234 this->commitWGState();
235
236 this->wellStructureChangedDynamically_ = false;
237 }
238
239
240
241
242
243 template <typename TypeTag>
244 void
246 initializeLocalWellStructure(const int reportStepIdx,
247 const bool enableWellPIScaling)
248 {
249 DeferredLogger local_deferredLogger{};
250
251 const auto& comm = this->simulator_.vanguard().grid().comm();
252
253 // Wells_ecl_ holds this rank's wells, both open and stopped/shut.
254 this->wells_ecl_ = this->getLocalWells(reportStepIdx);
255 this->local_parallel_well_info_ =
256 this->createLocalParallelWellInfo(this->wells_ecl_);
257
258 // At least initializeWellState() might be throw an exception in
259 // UniformTabulated2DFunction. Playing it safe by extending the
260 // scope a bit.
262 {
263 this->initializeWellPerfData();
264 this->initializeWellState(reportStepIdx);
265 this->wbp_.initializeWBPCalculationService();
266
267 if (this->param_.use_multisegment_well_ && this->anyMSWellOpenLocal()) {
268 this->wellState().initWellStateMSWell(this->wells_ecl_, &this->prevWellState());
269 }
270
271 this->initializeWellProdIndCalculators();
272
273 if (enableWellPIScaling && this->schedule()[reportStepIdx].events()
274 .hasEvent(ScheduleEvents::Events::WELL_PRODUCTIVITY_INDEX))
275 {
276 this->runWellPIScaling(reportStepIdx, local_deferredLogger);
277 }
278 }
279 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
280 "Failed to initialize local well structure: ",
281 this->terminal_output_, comm)
282 }
283
284
285
286
287
288 template <typename TypeTag>
289 void
291 initializeGroupStructure(const int reportStepIdx)
292 {
293 DeferredLogger local_deferredLogger{};
294
295 const auto& comm = this->simulator_.vanguard().grid().comm();
296
298 {
299 const auto& fieldGroup =
300 this->schedule().getGroup("FIELD", reportStepIdx);
301
302 WellGroupHelpersType::setCmodeGroup(fieldGroup,
303 this->schedule(),
304 this->summaryState(),
305 reportStepIdx,
306 this->groupState());
307
308 // Define per region average pressure calculators for use by
309 // pressure maintenance groups (GPMAINT keyword).
310 if (this->schedule()[reportStepIdx].has_gpmaint()) {
311 WellGroupHelpersType::setRegionAveragePressureCalculator(fieldGroup,
312 this->schedule(),
313 reportStepIdx,
314 this->eclState_.fieldProps(),
315 this->regionalAveragePressureCalculator_);
316 }
317 }
318 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
319 "Failed to initialize group structure: ",
320 this->terminal_output_, comm)
321 }
322
323
324
325
326
327 // called at the beginning of a time step
328 template<typename TypeTag>
329 void
332 {
333 OPM_TIMEBLOCK(beginTimeStep);
334
335 this->updateAverageFormationFactor();
336
337 DeferredLogger local_deferredLogger;
338
339 this->switched_prod_groups_.clear();
340 this->switched_inj_groups_.clear();
341
342 if (this->wellStructureChangedDynamically_) {
343 // Something altered the well structure/topology. Possibly
344 // WELSPECS/COMPDAT and/or WELOPEN run from an ACTIONX block.
345 // Reconstruct the local wells to account for the new well
346 // structure.
347 const auto reportStepIdx =
348 this->simulator_.episodeIndex();
349
350 // Disable WELPI scaling when well structure is updated in the
351 // middle of a report step.
352 const auto enableWellPIScaling = false;
353
354 this->initializeLocalWellStructure(reportStepIdx, enableWellPIScaling);
355 this->initializeGroupStructure(reportStepIdx);
356
357 this->commitWGState();
358
359 // Reset topology flag to signal that we've handled this
360 // structure change. That way we don't end up here in
361 // subsequent calls to beginTimeStep() unless there's a new
362 // dynamic change to the well structure during a report step.
363 this->wellStructureChangedDynamically_ = false;
364 }
365
366 this->resetWGState();
367
368 const int reportStepIdx = simulator_.episodeIndex();
369
370
371 this->wellState().updateWellsDefaultALQ(this->schedule(), reportStepIdx, this->summaryState());
372 this->wellState().gliftTimeStepInit();
373
374 const double simulationTime = simulator_.time();
376 {
377 // test wells
378 wellTesting(reportStepIdx, simulationTime, local_deferredLogger);
379
380 // create the well container
381 createWellContainer(reportStepIdx);
382
383 // we need to update the group data after the well is created
384 // to make sure we get the correct mapping.
385 this->updateAndCommunicateGroupData(reportStepIdx,
386 simulator_.model().newtonMethod().numIterations(),
387 param_.nupcol_group_rate_tolerance_, /*update_wellgrouptarget*/ false,
388 local_deferredLogger);
389
390 // Wells are active if they are active wells on at least one process.
391 const Grid& grid = simulator_.vanguard().grid();
392 this->wells_active_ = grid.comm().max(!this->well_container_.empty());
393
394 // do the initialization for all the wells
395 // TODO: to see whether we can postpone of the intialization of the well containers to
396 // optimize the usage of the following several member variables
397 this->initWellContainer(reportStepIdx);
398
399 // update the updated cell flag
400 std::fill(is_cell_perforated_.begin(), is_cell_perforated_.end(), false);
401 for (auto& well : well_container_) {
402 well->updatePerforatedCell(is_cell_perforated_);
403 }
404
405 // calculate the efficiency factors for each well
406 this->calculateEfficiencyFactors(reportStepIdx);
407
408 if constexpr (has_polymer_)
409 {
410 if (PolymerModule::hasPlyshlog() || getPropValue<TypeTag, Properties::EnablePolymerMW>() ) {
411 this->setRepRadiusPerfLength();
412 }
413 }
414
415 }
416
417 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger, "beginTimeStep() failed: ",
418 this->terminal_output_, simulator_.vanguard().grid().comm());
419
420 for (auto& well : well_container_) {
421 well->setVFPProperties(this->vfp_properties_.get());
422 well->setGuideRate(&this->guideRate_);
423 }
424
425 this->updateFiltrationModelsPreStep(local_deferredLogger);
426
427 // Close completions due to economic reasons
428 for (auto& well : well_container_) {
429 well->closeCompletions(this->wellTestState());
430 }
431
432 // we need the inj_multiplier from the previous time step
433 this->initInjMult();
434
435 if (alternative_well_rate_init_) {
436 // Update the well rates of well_state_, if only single-phase rates, to
437 // have proper multi-phase rates proportional to rates at bhp zero.
438 // This is done only for producers, as injectors will only have a single
439 // nonzero phase anyway.
440 for (const auto& well : well_container_) {
441 if (well->isProducer() && !well->wellIsStopped()) {
442 well->initializeProducerWellState(simulator_, this->wellState(), local_deferredLogger);
443 }
444 }
445 }
446
447 for (const auto& well : well_container_) {
448 if (well->isVFPActive(local_deferredLogger)){
449 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
450 }
451 }
452 try {
453 this->updateWellPotentials(reportStepIdx,
454 /*onlyAfterEvent*/true,
455 simulator_.vanguard().summaryConfig(),
456 local_deferredLogger);
457 } catch ( std::runtime_error& e ) {
458 const std::string msg = "A zero well potential is returned for output purposes. ";
459 local_deferredLogger.warning("WELL_POTENTIAL_CALCULATION_FAILED", msg);
460 }
461 this->guide_rate_handler_.setLogger(&local_deferredLogger);
462#ifdef RESERVOIR_COUPLING_ENABLED
463 if (this->isReservoirCouplingMaster()) {
464 this->guide_rate_handler_.receiveMasterGroupPotentialsFromSlaves();
465 }
466#endif
467 //update guide rates
468 this->guide_rate_handler_.updateGuideRates(
469 reportStepIdx, simulationTime, this->wellState(), this->groupState()
470 );
471#ifdef RESERVOIR_COUPLING_ENABLED
472 if (this->isReservoirCouplingSlave()) {
473 this->guide_rate_handler_.sendSlaveGroupPotentialsToMaster(this->groupState());
474 }
475#endif
476 std::string exc_msg;
477 auto exc_type = ExceptionType::NONE;
478 // update gpmaint targets
479 if (this->schedule_[reportStepIdx].has_gpmaint()) {
480 for (const auto& calculator : regionalAveragePressureCalculator_) {
481 calculator.second->template defineState<ElementContext>(simulator_);
482 }
483 const double dt = simulator_.timeStepSize();
484 const Group& fieldGroup = this->schedule().getGroup("FIELD", reportStepIdx);
486 this->schedule_,
487 regionalAveragePressureCalculator_,
488 reportStepIdx,
489 dt,
490 this->wellState(),
491 this->groupState());
492 }
493
494 this->updateAndCommunicateGroupData(reportStepIdx,
495 simulator_.model().newtonMethod().numIterations(),
496 param_.nupcol_group_rate_tolerance_,
497 /*update_wellgrouptarget*/ true,
498 local_deferredLogger);
499 try {
500 // Compute initial well solution for new wells and injectors that change injection type i.e. WAG.
501 for (auto& well : well_container_) {
502 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
503 + ScheduleEvents::INJECTION_TYPE_CHANGED
504 + ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER
505 + ScheduleEvents::NEW_WELL;
506
507 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
508 const bool event = this->report_step_starts_ && events.hasEvent(well->name(), effective_events_mask);
509 const bool dyn_status_change = this->wellState().well(well->name()).status
510 != this->prevWellState().well(well->name()).status;
511
512 if (event || dyn_status_change) {
513 try {
514 well->scaleSegmentRatesAndPressure(this->wellState());
515 well->calculateExplicitQuantities(simulator_, this->wellState(), local_deferredLogger);
516 well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), local_deferredLogger);
517 well->updatePrimaryVariables(simulator_, this->wellState(), local_deferredLogger);
518 well->solveWellEquation(simulator_, this->wellState(), this->groupState(), local_deferredLogger);
519 } catch (const std::exception& e) {
520 const std::string msg = "Compute initial well solution for new well " + well->name() + " failed. Continue with zero initial rates";
521 local_deferredLogger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
522 }
523 }
524 }
525 }
526 // Catch clauses for all errors setting exc_type and exc_msg
527 OPM_PARALLEL_CATCH_CLAUSE(exc_type, exc_msg);
528
529 if (exc_type != ExceptionType::NONE) {
530 const std::string msg = "Compute initial well solution for new wells failed. Continue with zero initial rates";
531 local_deferredLogger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
532 }
533
534 const auto& comm = simulator_.vanguard().grid().comm();
535 logAndCheckForExceptionsAndThrow(local_deferredLogger,
536 exc_type, "beginTimeStep() failed: " + exc_msg, this->terminal_output_, comm);
537
538 }
539
540 template<typename TypeTag>
541 void
543 const double simulationTime,
544 DeferredLogger& deferred_logger)
545 {
546 for (const std::string& well_name : this->getWellsForTesting(timeStepIdx, simulationTime)) {
547 const Well& wellEcl = this->schedule().getWell(well_name, timeStepIdx);
548 if (wellEcl.getStatus() == Well::Status::SHUT)
549 continue;
550
551 WellInterfacePtr well = createWellForWellTest(well_name, timeStepIdx, deferred_logger);
552 // some preparation before the well can be used
553 well->init(depth_, gravity_, B_avg_, true);
554
555 Scalar well_efficiency_factor = wellEcl.getEfficiencyFactor() *
556 this->wellState().getGlobalEfficiencyScalingFactor(well_name);
557 WellGroupHelpersType::accumulateGroupEfficiencyFactor(this->schedule().getGroup(wellEcl.groupName(),
558 timeStepIdx),
559 this->schedule(),
560 timeStepIdx,
561 well_efficiency_factor);
562
563 well->setWellEfficiencyFactor(well_efficiency_factor);
564 well->setVFPProperties(this->vfp_properties_.get());
565 well->setGuideRate(&this->guideRate_);
566
567 // initialize rates/previous rates to prevent zero fractions in vfp-interpolation
568 if (well->isProducer() && alternative_well_rate_init_) {
569 well->initializeProducerWellState(simulator_, this->wellState(), deferred_logger);
570 }
571 if (well->isVFPActive(deferred_logger)) {
572 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
573 }
574
575 const auto& network = this->schedule()[timeStepIdx].network();
576 if (network.active() && !this->node_pressures_.empty()) {
577 if (well->isProducer()) {
578 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
579 if (it != this->node_pressures_.end()) {
580 // The well belongs to a group which has a network nodal pressure,
581 // set the dynamic THP constraint based on the network nodal pressure
582 const Scalar nodal_pressure = it->second;
583 well->setDynamicThpLimit(nodal_pressure);
584 }
585 }
586 }
587 try {
588 using GLiftEclWells = typename GasLiftGroupInfo<Scalar, IndexTraits>::GLiftEclWells;
589 GLiftEclWells ecl_well_map;
590 gaslift_.initGliftEclWellMap(well_container_, ecl_well_map);
591 well->wellTesting(simulator_,
592 simulationTime,
593 this->wellState(),
594 this->groupState(),
595 this->wellTestState(),
596 ecl_well_map,
597 this->well_open_times_,
598 deferred_logger);
599 } catch (const std::exception& e) {
600 const std::string msg = fmt::format("Exception during testing of well: {}. The well will not open.\n Exception message: {}", wellEcl.name(), e.what());
601 deferred_logger.warning("WELL_TESTING_FAILED", msg);
602 }
603 }
604 }
605
606
607
608
609
610 // called at the end of a report step
611 template<typename TypeTag>
612 void
615 {
616 // Clear the communication data structures for above values.
617 for (auto&& pinfo : this->local_parallel_well_info_)
618 {
619 pinfo.get().clear();
620 }
621 }
622
623
624
625
626
627 // called at the end of a report step
628 template<typename TypeTag>
631 lastReport() const {return last_report_; }
632
633
634
635
636
637 // called at the end of a time step
638 template<typename TypeTag>
639 void
641 timeStepSucceeded(const double simulationTime, const double dt)
642 {
643 this->closed_this_step_.clear();
644
645 // time step is finished and we are not any more at the beginning of an report step
646 this->report_step_starts_ = false;
647 const int reportStepIdx = simulator_.episodeIndex();
648
649 DeferredLogger local_deferredLogger;
650 for (const auto& well : well_container_) {
651 if (getPropValue<TypeTag, Properties::EnablePolymerMW>() && well->isInjector()) {
652 well->updateWaterThroughput(dt, this->wellState());
653 }
654 }
655 // update connection transmissibility factor and d factor (if applicable) in the wellstate
656 for (const auto& well : well_container_) {
657 well->updateConnectionTransmissibilityFactor(simulator_, this->wellState().well(well->indexOfWell()));
658 well->updateConnectionDFactor(simulator_, this->wellState().well(well->indexOfWell()));
659 }
660
661 if (Indices::waterEnabled) {
662 this->updateFiltrationModelsPostStep(dt, FluidSystem::waterPhaseIdx, local_deferredLogger);
663 }
664
665 // WINJMULT: At the end of the time step, update the inj_multiplier saved in WellState for later use
666 this->updateInjMult(local_deferredLogger);
667
668 // report well switching
669 for (const auto& well : well_container_) {
670 well->reportWellSwitching(this->wellState().well(well->indexOfWell()), local_deferredLogger);
671 }
672 // report group switching
673 if (this->terminal_output_) {
674 this->reportGroupSwitching(local_deferredLogger);
675 }
676
677 // update the rate converter with current averages pressures etc in
678 rateConverter_->template defineState<ElementContext>(simulator_);
679
680 // calculate the well potentials
681 try {
682 this->updateWellPotentials(reportStepIdx,
683 /*onlyAfterEvent*/false,
684 simulator_.vanguard().summaryConfig(),
685 local_deferredLogger);
686 } catch ( std::runtime_error& e ) {
687 const std::string msg = "A zero well potential is returned for output purposes. ";
688 local_deferredLogger.warning("WELL_POTENTIAL_CALCULATION_FAILED", msg);
689 }
690
691 updateWellTestState(simulationTime, this->wellTestState());
692
693 // check group sales limits at the end of the timestep
694 const Group& fieldGroup = this->schedule_.getGroup("FIELD", reportStepIdx);
695 this->checkGEconLimits(fieldGroup, simulationTime,
696 simulator_.episodeIndex(), local_deferredLogger);
697 this->checkGconsaleLimits(fieldGroup, this->wellState(),
698 simulator_.episodeIndex(), local_deferredLogger);
699
700 this->calculateProductivityIndexValues(local_deferredLogger);
701
702 this->commitWGState();
703
704 const Opm::Parallel::Communication& comm = grid().comm();
705 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
706 if (this->terminal_output_) {
707 global_deferredLogger.logMessages();
708 }
709
710 //reporting output temperatures
711 this->computeWellTemperature();
712 }
713
714
715 template<typename TypeTag>
716 void
719 unsigned elemIdx) const
720 {
721 rate = 0;
722
723 if (!is_cell_perforated_[elemIdx]) {
724 return;
725 }
726
727 for (const auto& well : well_container_)
728 well->addCellRates(rate, elemIdx);
729 }
730
731
732 template<typename TypeTag>
733 template <class Context>
734 void
737 const Context& context,
738 unsigned spaceIdx,
739 unsigned timeIdx) const
740 {
741 rate = 0;
742 int elemIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
743
744 if (!is_cell_perforated_[elemIdx]) {
745 return;
746 }
747
748 for (const auto& well : well_container_)
749 well->addCellRates(rate, elemIdx);
750 }
751
752
753
754 template<typename TypeTag>
755 void
757 initializeWellState(const int timeStepIdx)
758 {
759 const auto pressIx = []()
760 {
761 if (Indices::oilEnabled) { return FluidSystem::oilPhaseIdx; }
762 if (Indices::waterEnabled) { return FluidSystem::waterPhaseIdx; }
763
764 return FluidSystem::gasPhaseIdx;
765 }();
766
767 auto cellPressures = std::vector<Scalar>(this->local_num_cells_, Scalar{0});
768 auto cellTemperatures = std::vector<Scalar>(this->local_num_cells_, Scalar{0});
769
770 auto elemCtx = ElementContext { this->simulator_ };
771 const auto& gridView = this->simulator_.vanguard().gridView();
772
774 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
775 elemCtx.updatePrimaryStencil(elem);
776 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
777
778 const auto ix = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
779 const auto& fs = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0).fluidState();
780
781 cellPressures[ix] = fs.pressure(pressIx).value();
782 cellTemperatures[ix] = fs.temperature(0).value();
783 }
784 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::initializeWellState() failed: ",
785 this->simulator_.vanguard().grid().comm());
786
787 this->wellState().init(cellPressures, cellTemperatures, this->schedule(), this->wells_ecl_,
788 this->local_parallel_well_info_, timeStepIdx,
789 &this->prevWellState(), this->well_perf_data_,
790 this->summaryState(), simulator_.vanguard().enableDistributedWells());
791 }
792
793
794
795
796
797 template<typename TypeTag>
798 void
800 createWellContainer(const int report_step)
801 {
802 DeferredLogger local_deferredLogger;
803
804 const int nw = this->numLocalWells();
805
806 well_container_.clear();
807
808 if (nw > 0) {
809 well_container_.reserve(nw);
810
811 const auto& wmatcher = this->schedule().wellMatcher(report_step);
812 const auto& wcycle = this->schedule()[report_step].wcycle.get();
813
814 // First loop and check for status changes. This is necessary
815 // as wcycle needs the updated open/close times.
816 std::for_each(this->wells_ecl_.begin(), this->wells_ecl_.end(),
817 [this, &wg_events = this->report_step_start_events_](const auto& well_ecl)
818 {
819 if (!well_ecl.hasConnections()) {
820 // No connections in this well. Nothing to do.
821 return;
822 }
823
824 constexpr auto events_mask = ScheduleEvents::WELL_STATUS_CHANGE |
825 ScheduleEvents::REQUEST_OPEN_WELL |
826 ScheduleEvents::REQUEST_SHUT_WELL;
827 const bool well_event =
828 this->report_step_starts_ &&
829 wg_events.hasEvent(well_ecl.name(), events_mask);
830 // WCYCLE is suspendended by explicit SHUT events by the user.
831 // and restarted after explicit OPEN events.
832 // Note: OPEN or SHUT event does not necessary mean the well
833 // actually opened or shut at this point as the simulator could
834 // have done this by operabilty checks and well testing. This
835 // may need further testing and imply code changes to cope with
836 // these corner cases.
837 if (well_event) {
838 if (well_ecl.getStatus() == WellStatus::OPEN) {
839 this->well_open_times_.insert_or_assign(well_ecl.name(),
840 this->simulator_.time());
841 this->well_close_times_.erase(well_ecl.name());
842 } else if (well_ecl.getStatus() == WellStatus::SHUT) {
843 this->well_close_times_.insert_or_assign(well_ecl.name(),
844 this->simulator_.time());
845 this->well_open_times_.erase(well_ecl.name());
846 }
847 }
848 });
849
850 // Grab wcycle states. This needs to run before the schedule gets processed
851 const auto cycle_states = wcycle.wellStatus(this->simulator_.time(),
852 wmatcher,
853 this->well_open_times_,
854 this->well_close_times_);
855
856 for (int w = 0; w < nw; ++w) {
857 const Well& well_ecl = this->wells_ecl_[w];
858
859 if (!well_ecl.hasConnections()) {
860 // No connections in this well. Nothing to do.
861 continue;
862 }
863
864 const std::string& well_name = well_ecl.name();
865 const auto well_status = this->schedule()
866 .getWell(well_name, report_step).getStatus();
867
868 const bool shut_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
869 && well_status == Well::Status::SHUT;
870 const bool open_event = this->wellState().well(w).events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE)
871 && well_status == Well::Status::OPEN;
872 const auto& ws = this->wellState().well(well_name);
873
874 if (shut_event && ws.status != Well::Status::SHUT) {
875 this->closed_this_step_.insert(well_name);
876 this->wellState().shutWell(w);
877 } else if (open_event && ws.status != Well::Status::OPEN) {
878 this->wellState().openWell(w);
879 }
880
881 // A new WCON keywords can re-open a well that was closed/shut due to Physical limit
882 if (this->wellTestState().well_is_closed(well_name)) {
883 // The well was shut this timestep, we are most likely retrying
884 // a timestep without the well in question, after it caused
885 // repeated timestep cuts. It should therefore not be opened,
886 // even if it was new or received new targets this report step.
887 const bool closed_this_step = (this->wellTestState().lastTestTime(well_name) == simulator_.time());
888 // TODO: more checking here, to make sure this standard more specific and complete
889 // maybe there is some WCON keywords will not open the well
890 auto& events = this->wellState().well(w).events;
891 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
892 if (!closed_this_step) {
893 this->wellTestState().open_well(well_name);
894 this->wellTestState().open_completions(well_name);
895 this->well_open_times_.insert_or_assign(well_name,
896 this->simulator_.time());
897 this->well_close_times_.erase(well_name);
898 }
899 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
900 }
901 }
902
903 // TODO: should we do this for all kinds of closing reasons?
904 // something like wellTestState().hasWell(well_name)?
905 if (this->wellTestState().well_is_closed(well_name))
906 {
907 if (well_ecl.getAutomaticShutIn()) {
908 // shut wells are not added to the well container
909 this->wellState().shutWell(w);
910 this->well_close_times_.erase(well_name);
911 this->well_open_times_.erase(well_name);
912 continue;
913 } else {
914 if (!well_ecl.getAllowCrossFlow()) {
915 // stopped wells where cross flow is not allowed
916 // are not added to the well container
917 this->wellState().shutWell(w);
918 this->well_close_times_.erase(well_name);
919 this->well_open_times_.erase(well_name);
920 continue;
921 }
922 // stopped wells are added to the container but marked as stopped
923 this->wellState().stopWell(w);
924 }
925 }
926
927 // shut wells with zero rante constraints and disallowing
928 if (!well_ecl.getAllowCrossFlow()) {
929 const bool any_zero_rate_constraint = well_ecl.isProducer()
930 ? well_ecl.productionControls(this->summaryState_).anyZeroRateConstraint()
931 : well_ecl.injectionControls(this->summaryState_).anyZeroRateConstraint();
932 if (any_zero_rate_constraint) {
933 // Treat as shut, do not add to container.
934 local_deferredLogger.debug(fmt::format(" Well {} gets shut due to having zero rate constraint and disallowing crossflow ", well_ecl.name()) );
935 this->wellState().shutWell(w);
936 this->well_close_times_.erase(well_name);
937 this->well_open_times_.erase(well_name);
938 continue;
939 }
940 }
941
942 if (!wcycle.empty()) {
943 const auto it = cycle_states.find(well_name);
944 if (it != cycle_states.end()) {
945 if (!it->second || well_status == Well::Status::SHUT) {
946 // If well is shut in schedule we keep it shut
947 if (well_status == Well::Status::SHUT) {
948 this->well_open_times_.erase(well_name);
949 this->well_close_times_.erase(well_name);
950 }
951 this->wellState().shutWell(w);
952 continue;
953 } else {
954 this->wellState().openWell(w);
955 }
956 }
957 }
958
959 // We dont add SHUT wells to the container
960 if (ws.status == Well::Status::SHUT) {
961 continue;
962 }
963
964 well_container_.emplace_back(this->createWellPointer(w, report_step));
965
966 if (ws.status == Well::Status::STOP) {
967 well_container_.back()->stopWell();
968 this->well_close_times_.erase(well_name);
969 this->well_open_times_.erase(well_name);
970 }
971 }
972
973 if (!wcycle.empty()) {
974 const auto schedule_open =
975 [&wg_events = this->report_step_start_events_](const std::string& name)
976 {
977 return wg_events.hasEvent(name, ScheduleEvents::REQUEST_OPEN_WELL);
978 };
979 for (const auto& [wname, wscale] : wcycle.efficiencyScale(this->simulator_.time(),
980 this->simulator_.timeStepSize(),
981 wmatcher,
982 this->well_open_times_,
983 schedule_open))
984 {
985 this->wellState().updateEfficiencyScalingFactor(wname, wscale);
986 this->schedule_.add_event(ScheduleEvents::WELLGROUP_EFFICIENCY_UPDATE, report_step);
987 }
988 }
989 }
990
991 // Collect log messages and print.
992
993 const Opm::Parallel::Communication& comm = grid().comm();
994 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
995 if (this->terminal_output_) {
996 global_deferredLogger.logMessages();
997 }
998
999 this->well_container_generic_.clear();
1000 for (auto& w : well_container_)
1001 this->well_container_generic_.push_back(w.get());
1002
1003 const auto& network = this->schedule()[report_step].network();
1004 if (network.active() && !this->node_pressures_.empty()) {
1005 for (auto& well: this->well_container_generic_) {
1006 // Producers only, since we so far only support the
1007 // "extended" network model (properties defined by
1008 // BRANPROP and NODEPROP) which only applies to producers.
1009 if (well->isProducer()) {
1010 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
1011 if (it != this->node_pressures_.end()) {
1012 // The well belongs to a group which has a network nodal pressure,
1013 // set the dynamic THP constraint based on the network nodal pressure
1014 const Scalar nodal_pressure = it->second;
1015 well->setDynamicThpLimit(nodal_pressure);
1016 }
1017 }
1018 }
1019 }
1020
1021 this->wbp_.registerOpenWellsForWBPCalculation();
1022 }
1023
1024
1025
1026
1027
1028 template <typename TypeTag>
1029 typename BlackoilWellModel<TypeTag>::WellInterfacePtr
1031 createWellPointer(const int wellID, const int report_step) const
1032 {
1033 const auto is_multiseg = this->wells_ecl_[wellID].isMultiSegment();
1034
1035 if (! (this->param_.use_multisegment_well_ && is_multiseg)) {
1036 return this->template createTypedWellPointer<StandardWell<TypeTag>>(wellID, report_step);
1037 }
1038 else {
1039 return this->template createTypedWellPointer<MultisegmentWell<TypeTag>>(wellID, report_step);
1040 }
1041 }
1042
1043
1044
1045
1046
1047 template <typename TypeTag>
1048 template <typename WellType>
1049 std::unique_ptr<WellType>
1051 createTypedWellPointer(const int wellID, const int time_step) const
1052 {
1053 // Use the pvtRegionIdx from the top cell
1054 const auto& perf_data = this->well_perf_data_[wellID];
1055
1056 // Cater for case where local part might have no perforations.
1057 const auto pvtreg = perf_data.empty()
1058 ? 0 : this->pvt_region_idx_[perf_data.front().cell_index];
1059
1060 const auto& parallel_well_info = this->local_parallel_well_info_[wellID].get();
1061 const auto global_pvtreg = parallel_well_info.broadcastFirstPerforationValue(pvtreg);
1062
1063 return std::make_unique<WellType>(this->wells_ecl_[wellID],
1064 parallel_well_info,
1065 time_step,
1066 this->param_,
1067 *this->rateConverter_,
1068 global_pvtreg,
1069 this->numConservationQuantities(),
1070 this->numPhases(),
1071 wellID,
1072 perf_data);
1073 }
1074
1075
1076
1077
1078
1079 template<typename TypeTag>
1082 createWellForWellTest(const std::string& well_name,
1083 const int report_step,
1084 DeferredLogger& deferred_logger) const
1085 {
1086 // Finding the location of the well in wells_ecl
1087 const auto it = std::find_if(this->wells_ecl_.begin(),
1088 this->wells_ecl_.end(),
1089 [&well_name](const auto& w)
1090 { return well_name == w.name(); });
1091 // It should be able to find in wells_ecl.
1092 if (it == this->wells_ecl_.end()) {
1093 OPM_DEFLOG_THROW(std::logic_error,
1094 fmt::format("Could not find well {} in wells_ecl ", well_name),
1095 deferred_logger);
1096 }
1097
1098 const int pos = static_cast<int>(std::distance(this->wells_ecl_.begin(), it));
1099 return this->createWellPointer(pos, report_step);
1100 }
1101
1102
1103
1104 template<typename TypeTag>
1105 void
1108 {
1109 OPM_TIMEFUNCTION();
1110 const double dt = this->simulator_.timeStepSize();
1111 // TODO: should we also have the group and network backed-up here in case the solution did not get converged?
1112 auto& well_state = this->wellState();
1113
1114 const bool changed_well_group = updateWellControlsAndNetwork(true, dt, deferred_logger);
1115 assembleWellEqWithoutIteration(dt, deferred_logger);
1116 const bool converged = this->getWellConvergence(this->B_avg_, true).converged() && !changed_well_group;
1117
1119 for (auto& well : this->well_container_) {
1120 well->solveEqAndUpdateWellState(simulator_, well_state, deferred_logger);
1121 }
1122 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::doPreStepNetworkRebalance() failed: ",
1123 this->simulator_.vanguard().grid().comm());
1124
1125 if (!converged) {
1126 const std::string msg = fmt::format("Initial (pre-step) network balance did not converge.");
1127 deferred_logger.warning(msg);
1128 }
1129 }
1130
1131
1132
1133
1134 template<typename TypeTag>
1135 void
1137 assemble(const int iterationIdx,
1138 const double dt)
1139 {
1140 OPM_TIMEFUNCTION();
1141 DeferredLogger local_deferredLogger;
1142
1143 this->guide_rate_handler_.setLogger(&local_deferredLogger);
1145 if (gaslift_.terminalOutput()) {
1146 const std::string msg =
1147 fmt::format("assemble() : iteration {}" , iterationIdx);
1148 gaslift_.gliftDebug(msg, local_deferredLogger);
1149 }
1150 }
1151 last_report_ = SimulatorReportSingle();
1152 Dune::Timer perfTimer;
1153 perfTimer.start();
1154 this->closed_offending_wells_.clear();
1155
1156 {
1157 const int episodeIdx = simulator_.episodeIndex();
1158 const auto& network = this->schedule()[episodeIdx].network();
1159 if (!this->wellsActive() && !network.active()) {
1160 return;
1161 }
1162 }
1163
1164 if (iterationIdx == 0 && this->wellsActive()) {
1165 OPM_TIMEBLOCK(firstIterationAssmble);
1166 // try-catch is needed here as updateWellControls
1167 // contains global communication and has either to
1168 // be reached by all processes or all need to abort
1169 // before.
1171 {
1172 calculateExplicitQuantities(local_deferredLogger);
1173 prepareTimeStep(local_deferredLogger);
1174 }
1175 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1176 "assemble() failed (It=0): ",
1177 this->terminal_output_, grid().comm());
1178 }
1179
1180 const bool well_group_control_changed = updateWellControlsAndNetwork(false, dt, local_deferredLogger);
1181
1182 // even when there is no wells active, the network nodal pressure still need to be updated through updateWellControlsAndNetwork()
1183 // but there is no need to assemble the well equations
1184 if ( ! this->wellsActive() ) {
1185 return;
1186 }
1187
1188 assembleWellEqWithoutIteration(dt, local_deferredLogger);
1189
1190 // if group or well control changes we don't consider the
1191 // case converged
1192 last_report_.well_group_control_changed = well_group_control_changed;
1193 last_report_.assemble_time_well += perfTimer.stop();
1194 }
1195
1196
1197
1198
1199 template<typename TypeTag>
1200 bool
1202 updateWellControlsAndNetwork(const bool mandatory_network_balance,
1203 const double dt,
1204 DeferredLogger& local_deferredLogger)
1205 {
1206 OPM_TIMEFUNCTION();
1207 // not necessarily that we always need to update once of the network solutions
1208 bool do_network_update = true;
1209 bool well_group_control_changed = false;
1210 Scalar network_imbalance = 0.0;
1211 // after certain number of the iterations, we use relaxed tolerance for the network update
1212 const std::size_t iteration_to_relax = param_.network_max_strict_outer_iterations_;
1213 // after certain number of the iterations, we terminate
1214 const std::size_t max_iteration = param_.network_max_outer_iterations_;
1215 std::size_t network_update_iteration = 0;
1216 network_needs_more_balancing_force_another_newton_iteration_ = false;
1217 while (do_network_update) {
1218 if (network_update_iteration >= max_iteration ) {
1219 // only output to terminal if we at the last newton iterations where we try to balance the network.
1220 const int episodeIdx = simulator_.episodeIndex();
1221 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1222 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx + 1)) {
1223 if (this->terminal_output_) {
1224 const std::string msg = fmt::format("Maximum of {:d} network iterations has been used and we stop the update, \n"
1225 "and try again after the next Newton iteration (imbalance = {:.2e} bar, ctrl_change = {})",
1226 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1227 local_deferredLogger.debug(msg);
1228 }
1229 // To avoid stopping the newton iterations too early, before the network is converged,
1230 // we need to report it
1231 network_needs_more_balancing_force_another_newton_iteration_ = true;
1232 } else {
1233 if (this->terminal_output_) {
1234 const std::string msg = fmt::format("Maximum of {:d} network iterations has been used and we stop the update. \n"
1235 "The simulator will continue with unconverged network results (imbalance = {:.2e} bar, ctrl_change = {})",
1236 max_iteration, network_imbalance*1.0e-5, well_group_control_changed);
1237 local_deferredLogger.info(msg);
1238 }
1239 }
1240 break;
1241 }
1242 if (this->terminal_output_ && (network_update_iteration == iteration_to_relax) ) {
1243 local_deferredLogger.debug("We begin using relaxed tolerance for network update now after " + std::to_string(iteration_to_relax) + " iterations ");
1244 }
1245 const bool relax_network_balance = network_update_iteration >= iteration_to_relax;
1246 // Never optimize gas lift in last iteration, to allow network convergence (unless max_iter < 2)
1247 const bool optimize_gas_lift = ( (network_update_iteration + 1) < std::max(max_iteration, static_cast<std::size_t>(2)) );
1248 std::tie(well_group_control_changed, do_network_update, network_imbalance) =
1249 updateWellControlsAndNetworkIteration(mandatory_network_balance, relax_network_balance, optimize_gas_lift, dt,local_deferredLogger);
1250 ++network_update_iteration;
1251 }
1252 return well_group_control_changed;
1253 }
1254
1255
1256
1257
1258 template<typename TypeTag>
1259 std::tuple<bool, bool, typename BlackoilWellModel<TypeTag>::Scalar>
1261 updateWellControlsAndNetworkIteration(const bool mandatory_network_balance,
1262 const bool relax_network_tolerance,
1263 const bool optimize_gas_lift,
1264 const double dt,
1265 DeferredLogger& local_deferredLogger)
1266 {
1267 OPM_TIMEFUNCTION();
1268 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1269 const int reportStepIdx = simulator_.episodeIndex();
1270 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx,
1271 param_.nupcol_group_rate_tolerance_, /*update_wellgrouptarget*/ true, local_deferredLogger);
1272 // We need to call updateWellControls before we update the network as
1273 // network updates are only done on thp controlled wells.
1274 // Note that well controls are allowed to change during updateNetwork
1275 // and in prepareWellsBeforeAssembling during well solves.
1276 bool well_group_control_changed = updateWellControls(local_deferredLogger);
1277 const auto [more_inner_network_update, network_imbalance] =
1278 updateNetworks(mandatory_network_balance,
1279 local_deferredLogger,
1280 relax_network_tolerance);
1281
1282
1283 bool alq_updated = false;
1285 {
1286 if (optimize_gas_lift) {
1287 // we need to update the potentials if the thp limit as been modified by
1288 // the network balancing
1289 const bool updatePotentials = (this->shouldBalanceNetwork(reportStepIdx, iterationIdx) || mandatory_network_balance);
1290 alq_updated = gaslift_.maybeDoGasLiftOptimize(simulator_,
1291 well_container_,
1292 this->node_pressures_,
1293 updatePotentials,
1294 this->wellState(),
1295 this->groupState(),
1296 local_deferredLogger);
1297 }
1298 prepareWellsBeforeAssembling(dt, local_deferredLogger);
1299 }
1300 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1301 "updateWellControlsAndNetworkIteration() failed: ",
1302 this->terminal_output_, grid().comm());
1303
1304 // update guide rates
1305 if (alq_updated || BlackoilWellModelGuideRates(*this).
1306 guideRateUpdateIsNeeded(reportStepIdx)) {
1307 const double simulationTime = simulator_.time();
1308 // NOTE: For reservoir coupling: Slave group potentials are only communicated
1309 // at the start of the time step, see beginTimeStep(). Here, we assume those
1310 // potentials remain unchanged during the time step when updating guide rates below.
1311 this->guide_rate_handler_.updateGuideRates(
1312 reportStepIdx, simulationTime, this->wellState(), this->groupState()
1313 );
1314 }
1315 // we need to re-iterate the network when the well group controls changed or gaslift/alq is changed or
1316 // the inner iterations are did not converge
1317 const bool more_network_update = this->shouldBalanceNetwork(reportStepIdx, iterationIdx) &&
1318 (more_inner_network_update || well_group_control_changed || alq_updated);
1319 return {well_group_control_changed, more_network_update, network_imbalance};
1320 }
1321
1322 // This function is to be used for well groups in an extended network that act as a subsea manifold
1323 // The wells of such group should have a common THP and total phase rate(s) obeying (if possible)
1324 // the well group constraint set by GCONPROD
1325 template <typename TypeTag>
1326 bool
1328 computeWellGroupThp(const double dt, DeferredLogger& local_deferredLogger)
1329 {
1330 OPM_TIMEFUNCTION();
1331 const int reportStepIdx = this->simulator_.episodeIndex();
1332 const auto& network = this->schedule()[reportStepIdx].network();
1333 const auto& balance = this->schedule()[reportStepIdx].network_balance();
1334 const Scalar thp_tolerance = balance.thp_tolerance();
1335
1336 if (!network.active()) {
1337 return false;
1338 }
1339
1340 auto& well_state = this->wellState();
1341 auto& group_state = this->groupState();
1342
1343 bool well_group_thp_updated = false;
1344 for (const std::string& nodeName : network.node_names()) {
1345 const bool has_choke = network.node(nodeName).as_choke();
1346 if (has_choke) {
1347 const auto& summary_state = this->simulator_.vanguard().summaryState();
1348 const Group& group = this->schedule().getGroup(nodeName, reportStepIdx);
1349
1350 //TODO: Auto choke combined with RESV control is not supported
1351 std::vector<Scalar> resv_coeff(Indices::numPhases, 1.0);
1352 Scalar gratTargetFromSales = 0.0;
1353 if (group_state.has_grat_sales_target(group.name()))
1354 gratTargetFromSales = group_state.grat_sales_target(group.name());
1355
1356 const auto ctrl = group.productionControls(summary_state);
1357 auto cmode_tmp = ctrl.cmode;
1358 Scalar target_tmp{0.0};
1359 bool fld_none = false;
1360 if (cmode_tmp == Group::ProductionCMode::FLD || cmode_tmp == Group::ProductionCMode::NONE) {
1361 fld_none = true;
1362 // Target is set for an ancestor group. Target for autochoke group to be
1363 // derived via group guide rates
1364 const Scalar efficiencyFactor = 1.0;
1365 const Group& parentGroup = this->schedule().getGroup(group.parent(), reportStepIdx);
1367 group.name(),
1368 parentGroup,
1369 well_state,
1370 group_state,
1371 this->schedule(),
1372 summary_state,
1373 resv_coeff,
1374 efficiencyFactor,
1375 reportStepIdx,
1376 &this->guideRate_,
1377 local_deferredLogger);
1378 target_tmp = target.first;
1379 cmode_tmp = target.second;
1380 }
1381 const auto cmode = cmode_tmp;
1382 using TargetCalculatorType = WGHelpers::TargetCalculator<Scalar, IndexTraits>;
1383 TargetCalculatorType tcalc(cmode, FluidSystem::phaseUsage(), resv_coeff,
1384 gratTargetFromSales, nodeName, group_state,
1385 group.has_gpmaint_control(cmode));
1386 if (!fld_none)
1387 {
1388 // Target is set for the autochoke group itself
1389 target_tmp = tcalc.groupTarget(ctrl, local_deferredLogger);
1390 }
1391
1392 const Scalar orig_target = target_tmp;
1393
1394 auto mismatch = [&] (auto group_thp) {
1395 Scalar group_rate(0.0);
1396 Scalar rate(0.0);
1397 for (auto& well : this->well_container_) {
1398 std::string well_name = well->name();
1399 auto& ws = well_state.well(well_name);
1400 if (group.hasWell(well_name)) {
1401 well->setDynamicThpLimit(group_thp);
1402 const Well& well_ecl = this->wells_ecl_[well->indexOfWell()];
1403 const auto inj_controls = Well::InjectionControls(0);
1404 const auto prod_controls = well_ecl.productionControls(summary_state);
1405 well->iterateWellEqWithSwitching(this->simulator_, dt, inj_controls, prod_controls, well_state, group_state, local_deferredLogger, false, false);
1406 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
1407 group_rate += rate;
1408 }
1409 }
1410 return (group_rate - orig_target)/orig_target;
1411 };
1412
1413 const auto upbranch = network.uptree_branch(nodeName);
1414 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
1415 const Scalar nodal_pressure = it->second;
1416 Scalar well_group_thp = nodal_pressure;
1417
1418 std::optional<Scalar> autochoke_thp;
1419 if (auto iter = this->well_group_thp_calc_.find(nodeName); iter != this->well_group_thp_calc_.end()) {
1420 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
1421 }
1422
1423 using WellBhpThpCalculatorType = WellBhpThpCalculator<Scalar, IndexTraits>;
1424 //Find an initial bracket
1425 std::array<Scalar, 2> range_initial;
1426 if (!autochoke_thp.has_value()){
1427 Scalar min_thp, max_thp;
1428 // Retrieve the terminal pressure of the associated root of the manifold group
1429 std::string node_name = nodeName;
1430 while (!network.node(node_name).terminal_pressure().has_value()) {
1431 auto branch = network.uptree_branch(node_name).value();
1432 node_name = branch.uptree_node();
1433 }
1434 min_thp = network.node(node_name).terminal_pressure().value();
1435 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
1436 // Narrow down the bracket
1437 Scalar low1, high1;
1438 std::array<Scalar, 2> range = {Scalar{0.9}*min_thp, Scalar{1.1}*max_thp};
1439 std::optional<Scalar> appr_sol;
1440 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low1, high1, appr_sol, 0.0, local_deferredLogger);
1441 min_thp = low1;
1442 max_thp = high1;
1443 range_initial = {min_thp, max_thp};
1444 }
1445
1446 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
1447 // The bracket is based on the initial bracket or on a range based on a previous calculated group thp
1448 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
1449 std::array<Scalar, 2>{Scalar{0.9} * autochoke_thp.value(),
1450 Scalar{1.1} * autochoke_thp.value()} : range_initial;
1451 Scalar low, high;
1452 std::optional<Scalar> approximate_solution;
1453 const Scalar tolerance1 = thp_tolerance;
1454 local_deferredLogger.debug("Using brute force search to bracket the group THP");
1455 const bool finding_bracket = WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low, high, approximate_solution, tolerance1, local_deferredLogger);
1456
1457 if (approximate_solution.has_value()) {
1458 autochoke_thp = *approximate_solution;
1459 local_deferredLogger.debug("Approximate group THP value found: " + std::to_string(autochoke_thp.value()));
1460 } else if (finding_bracket) {
1461 const Scalar tolerance2 = thp_tolerance;
1462 const int max_iteration_solve = 100;
1463 int iteration = 0;
1464 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
1465 solve(mismatch, low, high, max_iteration_solve, tolerance2, iteration);
1466 local_deferredLogger.debug(" bracket = [" + std::to_string(low) + ", " + std::to_string(high) + "], " +
1467 "iteration = " + std::to_string(iteration));
1468 local_deferredLogger.debug("Group THP value = " + std::to_string(autochoke_thp.value()));
1469 } else {
1470 autochoke_thp.reset();
1471 local_deferredLogger.debug("Group THP solve failed due to bracketing failure");
1472 }
1473 }
1474 if (autochoke_thp.has_value()) {
1475 well_group_thp_calc_[nodeName] = autochoke_thp.value();
1476 // Note: The node pressure of the auto-choke node is set to well_group_thp in computeNetworkPressures()
1477 // and must be larger or equal to the pressure of the uptree node of its branch.
1478 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
1479 }
1480
1481 for (auto& well : this->well_container_) {
1482 std::string well_name = well->name();
1483
1484 if (well->isInjector() || !well->wellEcl().predictionMode())
1485 continue;
1486
1487 if (group.hasWell(well_name)) {
1488 well->setDynamicThpLimit(well_group_thp);
1489 }
1490 const auto& ws = this->wellState().well(well->indexOfWell());
1491 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
1492 if (thp_is_limit) {
1493 well->prepareWellBeforeAssembling(this->simulator_, dt, this->wellState(), this->groupState(), local_deferredLogger);
1494 }
1495 }
1496
1497 // Use the group THP in computeNetworkPressures().
1498 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName) ? group_state.well_group_thp(nodeName) : 1e30;
1499 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
1500 well_group_thp_updated = true;
1501 group_state.update_well_group_thp(nodeName, well_group_thp);
1502 }
1503 }
1504 }
1505 return well_group_thp_updated;
1506 }
1507
1508 template<typename TypeTag>
1509 void
1511 assembleWellEq(const double dt, DeferredLogger& deferred_logger)
1512 {
1513 OPM_TIMEFUNCTION();
1514 for (auto& well : well_container_) {
1515 well->assembleWellEq(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1516 }
1517 }
1518
1519
1520 template<typename TypeTag>
1521 void
1523 prepareWellsBeforeAssembling(const double dt, DeferredLogger& deferred_logger)
1524 {
1525 OPM_TIMEFUNCTION();
1526 for (auto& well : well_container_) {
1527 well->prepareWellBeforeAssembling(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1528 }
1529 }
1530
1531
1532 template<typename TypeTag>
1533 void
1535 assembleWellEqWithoutIteration(const double dt, DeferredLogger& deferred_logger)
1536 {
1537 OPM_TIMEFUNCTION();
1538 // We make sure that all processes throw in case there is an exception
1539 // on one of them (WetGasPvt::saturationPressure might throw if not converged)
1541
1542 for (auto& well: well_container_) {
1543 well->assembleWellEqWithoutIteration(simulator_, dt, this->wellState(), this->groupState(),
1544 deferred_logger);
1545 }
1546 OPM_END_PARALLEL_TRY_CATCH_LOG(deferred_logger, "BlackoilWellModel::assembleWellEqWithoutIteration failed: ",
1547 this->terminal_output_, grid().comm());
1548
1549 }
1550
1551#if COMPILE_GPU_BRIDGE
1552 template<typename TypeTag>
1553 void
1556 {
1557 // prepare for StandardWells
1559
1560 for(unsigned int i = 0; i < well_container_.size(); i++){
1561 auto& well = well_container_[i];
1562 std::shared_ptr<StandardWell<TypeTag> > derived = std::dynamic_pointer_cast<StandardWell<TypeTag> >(well);
1563 if (derived) {
1564 wellContribs.addNumBlocks(derived->linSys().getNumBlocks());
1565 }
1566 }
1567
1568 // allocate memory for data from StandardWells
1569 wellContribs.alloc();
1570
1571 for(unsigned int i = 0; i < well_container_.size(); i++){
1572 auto& well = well_container_[i];
1573 // maybe WellInterface could implement addWellContribution()
1574 auto derived_std = std::dynamic_pointer_cast<StandardWell<TypeTag>>(well);
1575 if (derived_std) {
1576 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1577 } else {
1578 auto derived_ms = std::dynamic_pointer_cast<MultisegmentWell<TypeTag> >(well);
1579 if (derived_ms) {
1580 derived_ms->linSys().extract(wellContribs);
1581 } else {
1582 OpmLog::warning("Warning unknown type of well");
1583 }
1584 }
1585 }
1586 }
1587#endif
1588
1589 template<typename TypeTag>
1590 void
1592 addWellContributions(SparseMatrixAdapter& jacobian) const
1593 {
1594 for ( const auto& well: well_container_ ) {
1595 well->addWellContributions(jacobian);
1596 }
1597 }
1598
1599 template<typename TypeTag>
1600 void
1603 const BVector& weights,
1604 const bool use_well_weights) const
1605 {
1606 int nw = this->numLocalWellsEnd();
1607 int rdofs = local_num_cells_;
1608 for ( int i = 0; i < nw; i++ ) {
1609 int wdof = rdofs + i;
1610 jacobian[wdof][wdof] = 1.0;// better scaling ?
1611 }
1612
1613 for (const auto& well : well_container_) {
1614 well->addWellPressureEquations(jacobian,
1615 weights,
1616 pressureVarIndex,
1617 use_well_weights,
1618 this->wellState());
1619 }
1620 }
1621
1622 template <typename TypeTag>
1624 addReservoirSourceTerms(GlobalEqVector& residual,
1625 const std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress) const
1626 {
1627 // NB this loop may write multiple times to the same element
1628 // if a cell is perforated by more than one well, so it should
1629 // not be OpenMP-parallelized.
1630 for (const auto& well : well_container_) {
1631 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1632 continue;
1633 }
1634 const auto& cells = well->cells();
1635 const auto& rates = well->connectionRates();
1636 for (unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1637 unsigned cellIdx = cells[perfIdx];
1638 auto rate = rates[perfIdx];
1639 rate *= -1.0;
1640 VectorBlockType res(0.0);
1641 using MatrixBlockType = typename SparseMatrixAdapter::MatrixBlock;
1642 MatrixBlockType bMat(0.0);
1643 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1644 residual[cellIdx] += res;
1645 *diagMatAddress[cellIdx] += bMat;
1646 }
1647 }
1648 }
1649
1650
1651 template<typename TypeTag>
1652 void
1655 {
1656 int nw = this->numLocalWellsEnd();
1657 int rdofs = local_num_cells_;
1658 for (int i = 0; i < nw; ++i) {
1659 int wdof = rdofs + i;
1660 jacobian.entry(wdof,wdof) = 1.0;// better scaling ?
1661 }
1662 const auto wellconnections = this->getMaxWellConnections();
1663 for (int i = 0; i < nw; ++i) {
1664 const auto& perfcells = wellconnections[i];
1665 for (int perfcell : perfcells) {
1666 int wdof = rdofs + i;
1667 jacobian.entry(wdof, perfcell) = 0.0;
1668 jacobian.entry(perfcell, wdof) = 0.0;
1669 }
1670 }
1671 }
1672
1673
1674 template<typename TypeTag>
1675 void
1678 {
1679 DeferredLogger local_deferredLogger;
1681 {
1682 for (const auto& well : well_container_) {
1683 const auto& cells = well->cells();
1684 x_local_.resize(cells.size());
1685
1686 for (size_t i = 0; i < cells.size(); ++i) {
1687 x_local_[i] = x[cells[i]];
1688 }
1689 well->recoverWellSolutionAndUpdateWellState(simulator_, x_local_,
1690 this->wellState(), local_deferredLogger);
1691 }
1692 }
1693 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1694 "recoverWellSolutionAndUpdateWellState() failed: ",
1695 this->terminal_output_, simulator_.vanguard().grid().comm());
1696 }
1697
1698
1699 template<typename TypeTag>
1700 void
1702 recoverWellSolutionAndUpdateWellStateDomain(const BVector& x, const int domainIdx)
1703 {
1704 if (!nldd_) {
1705 OPM_THROW(std::logic_error, "Attempt to call NLDD method without a NLDD solver");
1706 }
1707
1708 return nldd_->recoverWellSolutionAndUpdateWellState(x, domainIdx);
1709 }
1710
1711
1712 template<typename TypeTag>
1715 getWellConvergence(const std::vector<Scalar>& B_avg, bool checkWellGroupControlsAndNetwork) const
1716 {
1717
1718 DeferredLogger local_deferredLogger;
1719 // Get global (from all processes) convergence report.
1720 ConvergenceReport local_report;
1721 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1722 for (const auto& well : well_container_) {
1723 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1724 local_report += well->getWellConvergence(
1725 simulator_, this->wellState(), B_avg, local_deferredLogger,
1726 iterationIdx > param_.strict_outer_iter_wells_);
1727 } else {
1728 ConvergenceReport report;
1729 using CR = ConvergenceReport;
1730 report.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1731 local_report += report;
1732 }
1733 }
1734
1735 const Opm::Parallel::Communication comm = grid().comm();
1736 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
1737 ConvergenceReport report = gatherConvergenceReport(local_report, comm);
1738
1739 if (checkWellGroupControlsAndNetwork) {
1740 // the well_group_control_changed info is already communicated
1741 report.setWellGroupTargetsViolated(this->lastReport().well_group_control_changed);
1742 report.setNetworkNotYetBalancedForceAnotherNewtonIteration(network_needs_more_balancing_force_another_newton_iteration_);
1743 }
1744
1745 if (this->terminal_output_) {
1746 global_deferredLogger.logMessages();
1747
1748 // Log debug messages for NaN or too large residuals.
1749 for (const auto& f : report.wellFailures()) {
1750 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1751 OpmLog::debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1752 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1753 OpmLog::debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1754 }
1755 }
1756 }
1757 return report;
1758 }
1759
1760
1761
1762
1763
1764 template<typename TypeTag>
1765 void
1767 calculateExplicitQuantities(DeferredLogger& deferred_logger) const
1768 {
1769 // TODO: checking isOperableAndSolvable() ?
1770 for (auto& well : well_container_) {
1771 well->calculateExplicitQuantities(simulator_, this->wellState(), deferred_logger);
1772 }
1773 }
1774
1775
1776
1777
1778
1779 template<typename TypeTag>
1780 bool
1782 updateWellControls(DeferredLogger& deferred_logger)
1783 {
1784 OPM_TIMEFUNCTION();
1785 if (!this->wellsActive()) {
1786 return false;
1787 }
1788 const int episodeIdx = simulator_.episodeIndex();
1789 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1790 const auto& comm = simulator_.vanguard().grid().comm();
1791 size_t iter = 0;
1792 bool changed_well_group = false;
1793 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1794 // Check group individual constraints.
1795 // iterate a few times to make sure all constraints are honored
1796 const std::size_t max_iter = param_.well_group_constraints_max_iterations_;
1797 while(!changed_well_group && iter < max_iter) {
1798 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1799
1800 // Check wells' group constraints and communicate.
1801 bool changed_well_to_group = false;
1802 {
1803 OPM_TIMEBLOCK(UpdateWellControls);
1804 // For MS Wells a linear solve is performed below and the matrix might be singular.
1805 // We need to communicate the exception thrown to the others and rethrow.
1807 for (const auto& well : well_container_) {
1809 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1810 if (changed_well) {
1811 changed_well_to_group = changed_well || changed_well_to_group;
1812 }
1813 }
1814 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1815 simulator_.gridView().comm());
1816 }
1817
1818 changed_well_to_group = comm.sum(static_cast<int>(changed_well_to_group));
1819 if (changed_well_to_group) {
1820 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1821 changed_well_group = true;
1822 }
1823
1824 // Check individual well constraints and communicate.
1825 bool changed_well_individual = false;
1826 {
1827 // For MS Wells a linear solve is performed below and the matrix might be singular.
1828 // We need to communicate the exception thrown to the others and rethrow.
1830 for (const auto& well : well_container_) {
1832 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1833 if (changed_well) {
1834 changed_well_individual = changed_well || changed_well_individual;
1835 }
1836 }
1837 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1838 simulator_.gridView().comm());
1839 }
1840
1841 changed_well_individual = comm.sum(static_cast<int>(changed_well_individual));
1842 if (changed_well_individual) {
1843 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1844 changed_well_group = true;
1845 }
1846 iter++;
1847 }
1848
1849 // update wsolvent fraction for REIN wells
1850 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1851
1852 return changed_well_group;
1853 }
1854
1855
1856 template<typename TypeTag>
1857 std::tuple<bool, typename BlackoilWellModel<TypeTag>::Scalar>
1859 updateNetworks(const bool mandatory_network_balance,
1860 DeferredLogger& deferred_logger,
1861 const bool relax_network_tolerance)
1862 {
1863 OPM_TIMEFUNCTION();
1864 const int episodeIdx = simulator_.episodeIndex();
1865 const auto& network = this->schedule()[episodeIdx].network();
1866 if (!this->wellsActive() && !network.active()) {
1867 return {false, 0.0};
1868 }
1869
1870 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1871 const auto& comm = simulator_.vanguard().grid().comm();
1872
1873 // network related
1874 Scalar network_imbalance = 0.0;
1875 bool more_network_update = false;
1876 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx) || mandatory_network_balance) {
1877 OPM_TIMEBLOCK(BalanceNetwork);
1878 const double dt = this->simulator_.timeStepSize();
1879 // Calculate common THP for subsea manifold well group (item 3 of NODEPROP set to YES)
1880 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
1881 const int max_number_of_sub_iterations = param_.network_max_sub_iterations_;
1882 const Scalar network_pressure_update_damping_factor = param_.network_pressure_update_damping_factor_;
1883 const Scalar network_max_pressure_update = param_.network_max_pressure_update_in_bars_ * unit::barsa;
1884 bool more_network_sub_update = false;
1885 for (int i = 0; i < max_number_of_sub_iterations; i++) {
1886 const auto local_network_imbalance = this->updateNetworkPressures(episodeIdx, network_pressure_update_damping_factor, network_max_pressure_update);
1887 network_imbalance = comm.max(local_network_imbalance);
1888 const auto& balance = this->schedule()[episodeIdx].network_balance();
1889 constexpr Scalar relaxation_factor = 10.0;
1890 const Scalar tolerance = relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance() : balance.pressure_tolerance();
1891 more_network_sub_update = this->networkActive() && network_imbalance > tolerance;
1892 if (!more_network_sub_update)
1893 break;
1894
1895 for (const auto& well : well_container_) {
1896 if (well->isInjector() || !well->wellEcl().predictionMode())
1897 continue;
1898
1899 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
1900 if (it != this->node_pressures_.end()) {
1901 const auto& ws = this->wellState().well(well->indexOfWell());
1902 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
1903 if (thp_is_limit) {
1904 well->prepareWellBeforeAssembling(this->simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1905 }
1906 }
1907 }
1908 this->updateAndCommunicateGroupData(episodeIdx, iterationIdx, param_.nupcol_group_rate_tolerance_,
1909 /*update_wellgrouptarget*/ true, deferred_logger);
1910 }
1911 more_network_update = more_network_sub_update || well_group_thp_updated;
1912 }
1913 return { more_network_update, network_imbalance };
1914 }
1915
1916
1917 template<typename TypeTag>
1918 void
1920 updateAndCommunicate(const int reportStepIdx,
1921 const int iterationIdx,
1922 DeferredLogger& deferred_logger)
1923 {
1924 this->updateAndCommunicateGroupData(reportStepIdx,
1925 iterationIdx,
1926 param_.nupcol_group_rate_tolerance_,
1927 /*update_wellgrouptarget*/ true,
1928 deferred_logger);
1929
1930 // updateWellStateWithTarget might throw for multisegment wells hence we
1931 // have a parallel try catch here to thrown on all processes.
1933 // if a well or group change control it affects all wells that are under the same group
1934 for (const auto& well : well_container_) {
1935 // We only want to update wells under group-control here
1936 const auto& ws = this->wellState().well(well->indexOfWell());
1937 if (ws.production_cmode == Well::ProducerCMode::GRUP ||
1938 ws.injection_cmode == Well::InjectorCMode::GRUP)
1939 {
1940 well->updateWellStateWithTarget(simulator_, this->groupState(),
1941 this->wellState(), deferred_logger);
1942 }
1943 }
1944 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAndCommunicate failed: ",
1945 simulator_.gridView().comm())
1946 this->updateAndCommunicateGroupData(reportStepIdx,
1947 iterationIdx,
1948 param_.nupcol_group_rate_tolerance_,
1949 /*update_wellgrouptarget*/ true,
1950 deferred_logger);
1951 }
1952
1953 template<typename TypeTag>
1954 bool
1956 updateGroupControls(const Group& group,
1957 DeferredLogger& deferred_logger,
1958 const int reportStepIdx,
1959 const int iterationIdx)
1960 {
1961 OPM_TIMEFUNCTION();
1962 bool changed = false;
1963 // restrict the number of group switches but only after nupcol iterations.
1964 const int nupcol = this->schedule()[reportStepIdx].nupcol();
1965 const int max_number_of_group_switches = param_.max_number_of_group_switches_;
1966 const bool update_group_switching_log = iterationIdx >= nupcol;
1967 const bool changed_hc = this->checkGroupHigherConstraints(group, deferred_logger, reportStepIdx, max_number_of_group_switches, update_group_switching_log);
1968 if (changed_hc) {
1969 changed = true;
1970 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
1971 }
1972
1973 bool changed_individual =
1975 updateGroupIndividualControl(group,
1976 reportStepIdx,
1977 max_number_of_group_switches,
1978 update_group_switching_log,
1979 this->switched_inj_groups_,
1980 this->switched_prod_groups_,
1981 this->closed_offending_wells_,
1982 this->groupState(),
1983 this->wellState(),
1984 deferred_logger);
1985
1986 if (changed_individual) {
1987 changed = true;
1988 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
1989 }
1990 // call recursively down the group hierarchy
1991 for (const std::string& groupName : group.groups()) {
1992 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
1993 changed = changed || changed_this;
1994 }
1995 return changed;
1996 }
1997
1998 template<typename TypeTag>
1999 void
2001 updateWellTestState(const double simulationTime, WellTestState& wellTestState)
2002 {
2003 OPM_TIMEFUNCTION();
2004 DeferredLogger local_deferredLogger;
2005 for (const auto& well : well_container_) {
2006 const auto& wname = well->name();
2007 const auto wasClosed = wellTestState.well_is_closed(wname);
2008 well->checkWellOperability(simulator_,
2009 this->wellState(),
2010 local_deferredLogger);
2011 const bool under_zero_target =
2012 well->wellUnderZeroGroupRateTarget(this->simulator_,
2013 this->wellState(),
2014 local_deferredLogger);
2015 well->updateWellTestState(this->wellState().well(wname),
2016 simulationTime,
2017 /*writeMessageToOPMLog=*/ true,
2018 under_zero_target,
2019 wellTestState,
2020 local_deferredLogger);
2021
2022 if (!wasClosed && wellTestState.well_is_closed(wname)) {
2023 this->closed_this_step_.insert(wname);
2024
2025 // maybe open a new well
2026 const WellEconProductionLimits& econ_production_limits = well->wellEcl().getEconLimits();
2027 if (econ_production_limits.validFollowonWell()) {
2028 const auto episode_idx = simulator_.episodeIndex();
2029 const auto follow_on_well = econ_production_limits.followonWell();
2030 if (!this->schedule().hasWell(follow_on_well, episode_idx)) {
2031 const auto msg = fmt::format("Well {} was closed. But the given follow on well {} does not exist."
2032 "The simulator continues without opening a follow on well.",
2033 wname, follow_on_well);
2034 local_deferredLogger.warning(msg);
2035 }
2036 auto& ws = this->wellState().well(follow_on_well);
2037 const bool success = ws.updateStatus(WellStatus::OPEN);
2038 if (success) {
2039 const auto msg = fmt::format("Well {} was closed. The follow on well {} opens instead.", wname, follow_on_well);
2040 local_deferredLogger.info(msg);
2041 } else {
2042 const auto msg = fmt::format("Well {} was closed. The follow on well {} is already open.", wname, follow_on_well);
2043 local_deferredLogger.warning(msg);
2044 }
2045 }
2046
2047 }
2048 }
2049
2050 for (const auto& [group_name, to] : this->closed_offending_wells_) {
2051 if (this->hasOpenLocalWell(to.second) &&
2052 !this->wasDynamicallyShutThisTimeStep(to.second))
2053 {
2054 wellTestState.close_well(to.second,
2055 WellTestConfig::Reason::GROUP,
2056 simulationTime);
2057 this->updateClosedWellsThisStep(to.second);
2058 const std::string msg =
2059 fmt::format("Procedure on exceeding {} limit is WELL for group {}. "
2060 "Well {} is {}.",
2061 to.first,
2062 group_name,
2063 to.second,
2064 "shut");
2065 local_deferredLogger.info(msg);
2066 }
2067 }
2068
2069 const Opm::Parallel::Communication comm = grid().comm();
2070 DeferredLogger global_deferredLogger =
2071 gatherDeferredLogger(local_deferredLogger, comm);
2072
2073 if (this->terminal_output_) {
2074 global_deferredLogger.logMessages();
2075 }
2076 }
2077
2078
2079 template<typename TypeTag>
2080 void
2082 const WellState<Scalar, IndexTraits>& well_state_copy,
2083 std::string& exc_msg,
2084 ExceptionType::ExcEnum& exc_type,
2085 DeferredLogger& deferred_logger)
2086 {
2087 OPM_TIMEFUNCTION();
2088 const int np = this->numPhases();
2089 std::vector<Scalar> potentials;
2090 const auto& well = well_container_[widx];
2091 std::string cur_exc_msg;
2092 auto cur_exc_type = ExceptionType::NONE;
2093 try {
2094 well->computeWellPotentials(simulator_, well_state_copy, potentials, deferred_logger);
2095 }
2096 // catch all possible exception and store type and message.
2097 OPM_PARALLEL_CATCH_CLAUSE(cur_exc_type, cur_exc_msg);
2098 if (cur_exc_type != ExceptionType::NONE) {
2099 exc_msg += fmt::format("\nFor well {}: {}", well->name(), cur_exc_msg);
2100 }
2101 exc_type = std::max(exc_type, cur_exc_type);
2102 // Store it in the well state
2103 // potentials is resized and set to zero in the beginning of well->ComputeWellPotentials
2104 // and updated only if sucessfull. i.e. the potentials are zero for exceptions
2105 auto& ws = this->wellState().well(well->indexOfWell());
2106 for (int p = 0; p < np; ++p) {
2107 // make sure the potentials are positive
2108 ws.well_potentials[p] = std::max(Scalar{0.0}, potentials[p]);
2109 }
2110 }
2111
2112
2113
2114 template <typename TypeTag>
2115 void
2118 {
2119 for (const auto& wellPtr : this->well_container_) {
2120 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2121 }
2122 }
2123
2124
2125
2126
2127
2128 template <typename TypeTag>
2129 void
2131 calculateProductivityIndexValuesShutWells(const int reportStepIdx,
2132 DeferredLogger& deferred_logger)
2133 {
2134 // For the purpose of computing PI/II values, it is sufficient to
2135 // construct StandardWell instances only. We don't need to form
2136 // well objects that honour the 'isMultisegment()' flag of the
2137 // corresponding "this->wells_ecl_[shutWell]".
2138
2139 for (const auto& shutWell : this->local_shut_wells_) {
2140 if (!this->wells_ecl_[shutWell].hasConnections()) {
2141 // No connections in this well. Nothing to do.
2142 continue;
2143 }
2144
2145 auto wellPtr = this->template createTypedWellPointer
2146 <StandardWell<TypeTag>>(shutWell, reportStepIdx);
2147
2148 wellPtr->init(this->depth_, this->gravity_, this->B_avg_, true);
2149
2150 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2151 }
2152 }
2153
2154
2155
2156
2157
2158 template <typename TypeTag>
2159 void
2162 DeferredLogger& deferred_logger)
2163 {
2164 wellPtr->updateProductivityIndex(this->simulator_,
2165 this->prod_index_calc_[wellPtr->indexOfWell()],
2166 this->wellState(),
2167 deferred_logger);
2168 }
2169
2170
2171
2172 template<typename TypeTag>
2173 void
2175 prepareTimeStep(DeferredLogger& deferred_logger)
2176 {
2177 // Check if there is a network with active prediction wells at this time step.
2178 const auto episodeIdx = simulator_.episodeIndex();
2179 this->updateNetworkActiveState(episodeIdx);
2180
2181 // Rebalance the network initially if any wells in the network have status changes
2182 // (Need to check this before clearing events)
2183 const bool do_prestep_network_rebalance = param_.pre_solve_network_ && this->needPreStepNetworkRebalance(episodeIdx);
2184
2185 for (const auto& well : well_container_) {
2186 auto& events = this->wellState().well(well->indexOfWell()).events;
2187 if (events.hasEvent(WellState<Scalar, IndexTraits>::event_mask)) {
2188 well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), deferred_logger);
2189 well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger);
2190 // There is no new well control change input within a report step,
2191 // so next time step, the well does not consider to have effective events anymore.
2193 }
2194 // these events only work for the first time step within the report step
2195 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2196 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2197 }
2198 // solve the well equation initially to improve the initial solution of the well model
2199 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2200 try {
2201 well->solveWellEquation(simulator_, this->wellState(), this->groupState(), deferred_logger);
2202 } catch (const std::exception& e) {
2203 const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the previous rates";
2204 deferred_logger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
2205 }
2206 }
2207 // If we're using local well solves that include control switches, they also update
2208 // operability, so reset before main iterations begin
2209 well->resetWellOperability();
2210 }
2211 updatePrimaryVariables(deferred_logger);
2212
2213 // Actually do the pre-step network rebalance, using the updated well states and initial solutions
2214 if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger);
2215 }
2216
2217 template<typename TypeTag>
2218 void
2221 {
2222 std::vector< Scalar > B_avg(numConservationQuantities(), Scalar() );
2223 const auto& grid = simulator_.vanguard().grid();
2224 const auto& gridView = grid.leafGridView();
2225 ElementContext elemCtx(simulator_);
2226
2228 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2229 elemCtx.updatePrimaryStencil(elem);
2230 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
2231
2232 const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
2233 const auto& fs = intQuants.fluidState();
2234
2235 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2236 {
2237 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2238 continue;
2239 }
2240
2241 const unsigned compIdx = FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
2242 auto& B = B_avg[ compIdx ];
2243
2244 B += 1 / fs.invB(phaseIdx).value();
2245 }
2246 if constexpr (has_solvent_) {
2247 auto& B = B_avg[solventSaturationIdx];
2248 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2249 }
2250 }
2251 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAverageFormationFactor() failed: ", grid.comm())
2252
2253 // compute global average
2254 grid.comm().sum(B_avg.data(), B_avg.size());
2255 B_avg_.resize(B_avg.size());
2256 std::transform(B_avg.begin(), B_avg.end(), B_avg_.begin(),
2257 [gcells = global_num_cells_](const auto bval)
2258 { return bval / gcells; });
2259 }
2260
2261
2262
2263
2264
2265 template<typename TypeTag>
2266 void
2269 {
2270 for (const auto& well : well_container_) {
2271 well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger);
2272 }
2273 }
2274
2275 template<typename TypeTag>
2276 void
2278 {
2279 const auto& grid = simulator_.vanguard().grid();
2280 const auto& eclProblem = simulator_.problem();
2281 const unsigned numCells = grid.size(/*codim=*/0);
2282
2283 this->pvt_region_idx_.resize(numCells);
2284 for (unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2285 this->pvt_region_idx_[cellIdx] =
2286 eclProblem.pvtRegionIndex(cellIdx);
2287 }
2288 }
2289
2290 // The number of components in the model.
2291 template<typename TypeTag>
2292 int
2294 {
2295 // The numPhases() functions returns 1-3, depending on which
2296 // of the (oil, water, gas) phases are active. For each of those phases,
2297 // if the phase is active the corresponding component is present and
2298 // conserved.
2299 // Apart from (oil, water, gas), in the current well model only solvent
2300 // is explicitly modelled as a conserved quantity (polymer, energy, salt
2301 // etc. are not), unlike the reservoir part where all such quantities are
2302 // conserved. This function must therefore be updated when/if we add
2303 // more conserved quantities in the well model.
2304 return this->numPhases() + has_solvent_;
2305 }
2306
2307 template<typename TypeTag>
2308 void
2310 {
2311 const auto& eclProblem = simulator_.problem();
2312 depth_.resize(local_num_cells_);
2313 for (unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2314 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2315 }
2316 }
2317
2318 template<typename TypeTag>
2321 getWell(const std::string& well_name) const
2322 {
2323 // finding the iterator of the well in wells_ecl
2324 auto well = std::find_if(well_container_.begin(),
2325 well_container_.end(),
2326 [&well_name](const WellInterfacePtr& elem)->bool {
2327 return elem->name() == well_name;
2328 });
2329
2330 assert(well != well_container_.end());
2331
2332 return *well;
2333 }
2334
2335 template <typename TypeTag>
2336 int
2338 reportStepIndex() const
2339 {
2340 return std::max(this->simulator_.episodeIndex(), 0);
2341 }
2342
2343
2344
2345
2346
2347 template<typename TypeTag>
2348 void
2350 calcResvCoeff(const int fipnum,
2351 const int pvtreg,
2352 const std::vector<Scalar>& production_rates,
2353 std::vector<Scalar>& resv_coeff)
2354 {
2355 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2356 }
2357
2358 template<typename TypeTag>
2359 void
2361 calcInjResvCoeff(const int fipnum,
2362 const int pvtreg,
2363 std::vector<Scalar>& resv_coeff)
2364 {
2365 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2366 }
2367
2368
2369 template <typename TypeTag>
2370 void
2373 {
2374 if constexpr (has_energy_) {
2375 int np = this->numPhases();
2376 Scalar cellInternalEnergy;
2377 Scalar cellBinv;
2378 Scalar cellDensity;
2379 Scalar perfPhaseRate;
2380 const int nw = this->numLocalWells();
2381 for (auto wellID = 0*nw; wellID < nw; ++wellID) {
2382 const Well& well = this->wells_ecl_[wellID];
2383 auto& ws = this->wellState().well(wellID);
2384 if (well.isInjector()) {
2385 if (ws.status != WellStatus::STOP) {
2386 this->wellState().well(wellID).temperature = well.inj_temperature();
2387 continue;
2388 }
2389 }
2390
2391 std::array<Scalar,2> weighted{0.0,0.0};
2392 auto& [weighted_temperature, total_weight] = weighted;
2393
2394 auto& well_info = this->local_parallel_well_info_[wellID].get();
2395 auto& perf_data = ws.perf_data;
2396 auto& perf_phase_rate = perf_data.phase_rates;
2397
2398 using int_type = decltype(this->well_perf_data_[wellID].size());
2399 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2400 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2401 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, /*timeIdx=*/0);
2402 const auto& fs = intQuants.fluidState();
2403
2404 // we on only have one temperature pr cell any phaseIdx will do
2405 Scalar cellTemperatures = fs.temperature(/*phaseIdx*/0).value();
2406
2407 Scalar weight_factor = 0.0;
2408 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
2409 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2410 continue;
2411 }
2412 cellInternalEnergy = fs.enthalpy(phaseIdx).value() -
2413 fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2414 cellBinv = fs.invB(phaseIdx).value();
2415 cellDensity = fs.density(phaseIdx).value();
2416 perfPhaseRate = perf_phase_rate[perf*np + phaseIdx];
2417 weight_factor += cellDensity * perfPhaseRate / cellBinv * cellInternalEnergy / cellTemperatures;
2418 }
2419 weight_factor = std::abs(weight_factor) + 1e-13;
2420 total_weight += weight_factor;
2421 weighted_temperature += weight_factor * cellTemperatures;
2422 }
2423 well_info.communication().sum(weighted.data(), 2);
2424 this->wellState().well(wellID).temperature = weighted_temperature / total_weight;
2425 }
2426 }
2427 }
2428
2429
2430 template <typename TypeTag>
2432 assignWellTracerRates(data::Wells& wsrpt) const
2433 {
2434 const auto reportStepIdx = static_cast<unsigned int>(this->reportStepIndex());
2435 const auto& trMod = this->simulator_.problem().tracerModel();
2436
2437 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellTracerRates(), reportStepIdx);
2438 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellFreeTracerRates(), reportStepIdx);
2439 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellSolTracerRates(), reportStepIdx);
2440
2441 this->assignMswTracerRates(wsrpt, trMod.getMswTracerRates(), reportStepIdx);
2442 }
2443
2444} // namespace Opm
2445
2446#endif // OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
#define OPM_END_PARALLEL_TRY_CATCH_LOG(obptc_logger, obptc_prefix, obptc_output, comm)
Catch exception, log, and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:202
#define OPM_DEFLOG_THROW(Exception, message, deferred_logger)
Definition: DeferredLoggingErrorHelpers.hpp:45
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:192
#define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg)
Inserts catch classes for the parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:166
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:158
void logAndCheckForExceptionsAndThrow(Opm::DeferredLogger &deferred_logger, Opm::ExceptionType::ExcEnum exc_type, const std::string &message, const bool terminal_output, Opm::Parallel::Communication comm)
Definition: DeferredLoggingErrorHelpers.hpp:111
Class for handling constraints for the blackoil well model.
Definition: BlackoilWellModelConstraints.hpp:42
Class for handling the gaslift in the blackoil well model.
Definition: BlackoilWellModelGasLift.hpp:96
Class for handling the blackoil well model.
Definition: BlackoilWellModelGeneric.hpp:95
BlackoilWellModelWBP< GetPropType< TypeTag, Properties::Scalar >, GetPropType< TypeTag, Properties::FluidSystem >::IndexTraitsType > wbp_
Definition: BlackoilWellModelGeneric.hpp:517
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:46
Class for handling the blackoil well model.
Definition: BlackoilWellModel.hpp:102
void initializeGroupStructure(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:291
void init()
Definition: BlackoilWellModel_impl.hpp:158
const Simulator & simulator() const
Definition: BlackoilWellModel.hpp:374
std::vector< Scalar > depth_
Definition: BlackoilWellModel.hpp:433
std::size_t global_num_cells_
Definition: BlackoilWellModel.hpp:429
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModel.hpp:111
void initWellContainer(const int reportStepIdx) override
Definition: BlackoilWellModel_impl.hpp:177
void beginReportStep(const int time_step)
Definition: BlackoilWellModel_impl.hpp:194
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: BlackoilWellModel.hpp:107
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:132
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: BlackoilWellModel.hpp:108
GetPropType< TypeTag, Properties::Grid > Grid
Definition: BlackoilWellModel.hpp:105
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModel.hpp:110
void initializeWellState(const int timeStepIdx)
Definition: BlackoilWellModel_impl.hpp:757
const Grid & grid() const
Definition: BlackoilWellModel.hpp:371
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:631
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1592
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > PressureMatrix
Definition: BlackoilWellModel.hpp:299
bool empty() const
Definition: BlackoilWellModel.hpp:344
void computeTotalRatesForDof(RateVector &rate, unsigned globalIdx) const
Definition: BlackoilWellModel_impl.hpp:718
void beginTimeStep()
Definition: BlackoilWellModel_impl.hpp:331
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: BlackoilWellModel.hpp:112
void calculateExplicitQuantities(DeferredLogger &deferred_logger) const
Definition: BlackoilWellModel_impl.hpp:1767
void updatePrimaryVariables(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2268
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:246
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:133
BlackoilWellModel(Simulator &simulator)
Definition: BlackoilWellModel_impl.hpp:73
void wellTesting(const int timeStepIdx, const double simulationTime, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:542
typename FluidSystem::IndexTraitsType IndexTraits
Definition: BlackoilWellModel.hpp:119
std::size_t local_num_cells_
Definition: BlackoilWellModel.hpp:431
bool alternative_well_rate_init_
Definition: BlackoilWellModel.hpp:434
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:641
Simulator & simulator_
Definition: BlackoilWellModel.hpp:403
std::shared_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:192
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:800
int compressedIndexForInterior(int cartesian_cell_idx) const override
get compressed index for interior cells (-1, otherwise
Definition: BlackoilWellModel.hpp:352
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:614
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
Definition: StandardWell.hpp:60
virtual void init(const std::vector< Scalar > &depth_arg, const Scalar gravity_arg, const std::vector< Scalar > &B_avg, const bool changed_to_open_this_step) override
Definition: StandardWell_impl.hpp:76
Definition: TargetCalculator.hpp:43
Class for computing BHP limits.
Definition: WellBhpThpCalculator.hpp:41
Definition: WellContributions.hpp:51
void alloc()
Allocate memory for the StandardWells.
void setBlockSize(unsigned int dim, unsigned int dim_wells)
void addNumBlocks(unsigned int numBlocks)
Class for computing well group controls.
Definition: WellGroupControls.hpp:48
static void updateGpMaintTargetForGroups(const Group &group, const Schedule &schedule, const RegionalValues &regional_values, const int reportStepIdx, const double dt, const WellStateType &well_state, GroupState< Scalar > &group_state)
int indexOfWell() const
Index of well in the wells struct and wellState.
Definition: WellInterface.hpp:76
bool updateWellControl(const Simulator &simulator, const IndividualOrGroup iog, WellStateType &well_state, const GroupState< Scalar > &group_state, DeferredLogger &deferred_logger)
Definition: WellInterface_impl.hpp:189
virtual void updateProductivityIndex(const Simulator &simulator, const WellProdIndexCalculator< Scalar > &wellPICalc, WellStateType &well_state, DeferredLogger &deferred_logger) const =0
Definition: WellState.hpp:66
ExcEnum
Definition: DeferredLogger.hpp:45
@ NONE
Definition: DeferredLogger.hpp:46
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilbioeffectsmodules.hh:43
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Parallel::Communication communicator)
Create a global log combining local logs.
ConvergenceReport gatherConvergenceReport(const ConvergenceReport &local_report, Parallel::Communication communicator)
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:34