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// Improve IDE experience
24#ifndef OPM_BLACKOILWELLMODEL_HEADER_INCLUDED
25#define OPM_BLACKOILWELLMODEL_IMPL_HEADER_INCLUDED
26#include <config.h>
28#endif
29
32#include <opm/grid/utility/cartesianToCompressed.hpp>
33
34#include <opm/input/eclipse/Units/UnitSystem.hpp>
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/WellTestConfig.hpp>
44
45#if COMPILE_BDA_BRIDGE
47#endif
48
49#if HAVE_MPI
51#endif
52
53#include <algorithm>
54#include <cassert>
55#include <iomanip>
56#include <utility>
57#include <optional>
58
59#include <fmt/format.h>
60
61namespace Opm {
62 template<typename TypeTag>
64 BlackoilWellModel(Simulator& simulator, const PhaseUsage& phase_usage)
65 : BlackoilWellModelGeneric<Scalar>(simulator.vanguard().schedule(),
66 simulator.vanguard().summaryState(),
67 simulator.vanguard().eclState(),
68 phase_usage,
69 simulator.gridView().comm())
70 , simulator_(simulator)
71 {
72 this->terminal_output_ = ((simulator.gridView().comm().rank() == 0) &&
73 Parameters::get<TypeTag, Properties::EnableTerminalOutput>());
74
75 local_num_cells_ = simulator_.gridView().size(0);
76
77 // Number of cells the global grid view
78 global_num_cells_ = simulator_.vanguard().globalNumCells();
79
80 {
81 auto& parallel_wells = simulator.vanguard().parallelWells();
82
83 this->parallel_well_info_.reserve(parallel_wells.size());
84 for( const auto& name_bool : parallel_wells) {
85 this->parallel_well_info_.emplace_back(name_bool, grid().comm());
86 }
87 }
88
89 this->alternative_well_rate_init_ =
90 Parameters::get<TypeTag, Properties::AlternativeWellRateInit>();
91
92 this->wbpCalculationService_
93 .localCellIndex([this](const std::size_t globalIndex)
94 { return this->compressedIndexForInterior(globalIndex); })
95 .evalCellSource([this](const int localCell,
96 PAvgDynamicSourceData::SourceDataSpan<Scalar> sourceTerms)
97 {
98 using Item = typename PAvgDynamicSourceData::SourceDataSpan<Scalar>::Item;
99
100 const auto* intQuants = this->simulator_.model()
101 .cachedIntensiveQuantities(localCell, /*timeIndex = */0);
102 const auto& fs = intQuants->fluidState();
103
104 sourceTerms.set(Item::PoreVol, intQuants->porosity().value() *
105 this->simulator_.model().dofTotalVolume(localCell));
106
107 constexpr auto io = FluidSystem::oilPhaseIdx;
108 constexpr auto ig = FluidSystem::gasPhaseIdx;
109 constexpr auto iw = FluidSystem::waterPhaseIdx;
110
111 // Ideally, these would be 'constexpr'.
112 const auto haveOil = FluidSystem::phaseIsActive(io);
113 const auto haveGas = FluidSystem::phaseIsActive(ig);
114 const auto haveWat = FluidSystem::phaseIsActive(iw);
115
116 auto weightedPhaseDensity = [&fs](const auto ip)
117 {
118 return fs.saturation(ip).value() * fs.density(ip).value();
119 };
120
121 if (haveOil) { sourceTerms.set(Item::Pressure, fs.pressure(io).value()); }
122 else if (haveGas) { sourceTerms.set(Item::Pressure, fs.pressure(ig).value()); }
123 else { sourceTerms.set(Item::Pressure, fs.pressure(iw).value()); }
124
125 // Strictly speaking, assumes SUM(s[p]) == 1.
126 auto rho = 0.0;
127 if (haveOil) { rho += weightedPhaseDensity(io); }
128 if (haveGas) { rho += weightedPhaseDensity(ig); }
129 if (haveWat) { rho += weightedPhaseDensity(iw); }
130
131 sourceTerms.set(Item::MixtureDensity, rho);
132 });
133 }
134
135 template<typename TypeTag>
137 BlackoilWellModel(Simulator& simulator) :
138 BlackoilWellModel(simulator, phaseUsageFromDeck(simulator.vanguard().eclState()))
139 {}
140
141
142 template<typename TypeTag>
143 void
145 init()
146 {
147 extractLegacyCellPvtRegionIndex_();
148 extractLegacyDepth_();
149
150 gravity_ = simulator_.problem().gravity()[2];
151
152 this->initial_step_ = true;
153
154 // add the eWoms auxiliary module for the wells to the list
155 simulator_.model().addAuxiliaryModule(this);
156
157 is_cell_perforated_.resize(local_num_cells_, false);
158 }
159
160
161 template<typename TypeTag>
162 void
164 initWellContainer(const int reportStepIdx)
165 {
166 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
167 + ScheduleEvents::NEW_WELL;
168 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
169 for (auto& wellPtr : this->well_container_) {
170 const bool well_opened_this_step = this->report_step_starts_ && events.hasEvent(wellPtr->name(), effective_events_mask);
171 wellPtr->init(&this->phase_usage_, this->depth_, this->gravity_,
172 this->local_num_cells_, this->B_avg_, well_opened_this_step);
173 }
174 }
175
176
177 template<typename TypeTag>
178 void
180 addNeighbors(std::vector<NeighborSet>& neighbors) const
181 {
182 if (!param_.matrix_add_well_contributions_) {
183 return;
184 }
185
186 // Create cartesian to compressed mapping
187 const auto& schedule_wells = this->schedule().getWellsatEnd();
188
189 // initialize the additional cell connections introduced by wells.
190 for (const auto& well : schedule_wells)
191 {
192 std::vector<int> wellCells = this->getCellsForConnections(well);
193 for (int cellIdx : wellCells) {
194 neighbors[cellIdx].insert(wellCells.begin(),
195 wellCells.end());
196 }
197 }
198 }
199
200
201 template<typename TypeTag>
202 void
205 {
207 for (const auto& well: well_container_) {
208 // Modifiy the Jacobian with explicit Schur complement
209 // contributions if requested.
210 if (param_.matrix_add_well_contributions_) {
211 well->addWellContributions(jacobian);
212 }
213 // Apply as Schur complement the well residual to reservoir residuals:
214 // r = r - duneC_^T * invDuneD_ * resWell_
215 well->apply(res);
216 }
217 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::linearize failed: ",
218 simulator_.gridView().comm());
219 }
220
221
222 template<typename TypeTag>
223 void
225 linearizeDomain(const Domain& domain, SparseMatrixAdapter& jacobian, GlobalEqVector& res)
226 {
227 // Note: no point in trying to do a parallel gathering
228 // try/catch here, as this function is not called in
229 // parallel but for each individual domain of each rank.
230 for (const auto& well: well_container_) {
231 if (well_domain_.at(well->name()) == domain.index) {
232 // Modifiy the Jacobian with explicit Schur complement
233 // contributions if requested.
234 if (param_.matrix_add_well_contributions_) {
235 well->addWellContributions(jacobian);
236 }
237 // Apply as Schur complement the well residual to reservoir residuals:
238 // r = r - duneC_^T * invDuneD_ * resWell_
239 well->apply(res);
240 }
241 }
242 }
243
244
245 template<typename TypeTag>
246 void
248 beginReportStep(const int timeStepIdx)
249 {
250 DeferredLogger local_deferredLogger{};
251
252 this->report_step_starts_ = true;
253
254
255 this->rateConverter_ = std::make_unique<RateConverterType>
256 (this->phase_usage_, std::vector<int>(this->local_num_cells_, 0));
257
258 {
259 // WELPI scaling runs at start of report step.
260 const auto enableWellPIScaling = true;
261 this->initializeLocalWellStructure(timeStepIdx, enableWellPIScaling);
262 }
263
264 this->initializeGroupStructure(timeStepIdx);
265
266 const auto& comm = this->simulator_.vanguard().grid().comm();
267
269 {
270 // Create facility for calculating reservoir voidage volumes for
271 // purpose of RESV controls.
272 this->rateConverter_->template defineState<ElementContext>(this->simulator_);
273
274 // Update VFP properties.
275 {
276 const auto& sched_state = this->schedule()[timeStepIdx];
277
278 this->vfp_properties_ = std::make_unique<VFPProperties>
279 (sched_state.vfpinj(), sched_state.vfpprod(), this->wellState());
280 }
281 }
282 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
283 "beginReportStep() failed: ",
284 this->terminal_output_, comm)
285
286 // Store the current well and group states in order to recover in
287 // the case of failed iterations
288 this->commitWGState();
289
290 this->wellStructureChangedDynamically_ = false;
291 }
292
293
294
295
296
297 template <typename TypeTag>
298 void
300 initializeLocalWellStructure(const int reportStepIdx,
301 const bool enableWellPIScaling)
302 {
303 DeferredLogger local_deferredLogger{};
304
305 const auto& comm = this->simulator_.vanguard().grid().comm();
306
307 // Wells_ecl_ holds this rank's wells, both open and stopped/shut.
308 this->wells_ecl_ = this->getLocalWells(reportStepIdx);
309 this->local_parallel_well_info_ =
310 this->createLocalParallelWellInfo(this->wells_ecl_);
311
312 // At least initializeWellState() might be throw an exception in
313 // UniformTabulated2DFunction. Playing it safe by extending the
314 // scope a bit.
316 {
317 this->initializeWellPerfData();
318 this->initializeWellState(reportStepIdx);
319 this->initializeWBPCalculationService();
320
321 if (this->param_.use_multisegment_well_ && this->anyMSWellOpenLocal()) {
322 this->wellState().initWellStateMSWell(this->wells_ecl_, &this->prevWellState());
323 }
324
325 this->initializeWellProdIndCalculators();
326
327 if (enableWellPIScaling && this->schedule()[reportStepIdx].events()
328 .hasEvent(ScheduleEvents::Events::WELL_PRODUCTIVITY_INDEX))
329 {
330 this->runWellPIScaling(reportStepIdx, local_deferredLogger);
331 }
332 }
333 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
334 "Failed to initialize local well structure: ",
335 this->terminal_output_, comm)
336 }
337
338
339
340
341
342 template <typename TypeTag>
343 void
345 initializeGroupStructure(const int reportStepIdx)
346 {
347 DeferredLogger local_deferredLogger{};
348
349 const auto& comm = this->simulator_.vanguard().grid().comm();
350
352 {
353 const auto& fieldGroup =
354 this->schedule().getGroup("FIELD", reportStepIdx);
355
357 this->schedule(),
358 this->summaryState(),
359 reportStepIdx,
360 this->groupState());
361
362 // Define per region average pressure calculators for use by
363 // pressure maintenance groups (GPMAINT keyword).
364 if (this->schedule()[reportStepIdx].has_gpmaint()) {
366 (fieldGroup,
367 this->schedule(),
368 reportStepIdx,
369 this->eclState_.fieldProps(),
370 this->phase_usage_,
371 this->regionalAveragePressureCalculator_);
372 }
373 }
374 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
375 "Failed to initialize group structure: ",
376 this->terminal_output_, comm)
377 }
378
379
380
381
382
383 // called at the beginning of a time step
384 template<typename TypeTag>
385 void
388 {
389 OPM_TIMEBLOCK(beginTimeStep);
390
391 this->updateAverageFormationFactor();
392
393 DeferredLogger local_deferredLogger;
394
395 this->switched_prod_groups_.clear();
396 this->switched_inj_groups_.clear();
397
398 if (this->wellStructureChangedDynamically_) {
399 // Something altered the well structure/topology. Possibly
400 // WELSPECS/COMPDAT and/or WELOPEN run from an ACTIONX block.
401 // Reconstruct the local wells to account for the new well
402 // structure.
403 const auto reportStepIdx =
404 this->simulator_.episodeIndex();
405
406 // Disable WELPI scaling when well structure is updated in the
407 // middle of a report step.
408 const auto enableWellPIScaling = false;
409
410 this->initializeLocalWellStructure(reportStepIdx, enableWellPIScaling);
411 this->initializeGroupStructure(reportStepIdx);
412
413 this->commitWGState();
414
415 // Reset topology flag to signal that we've handled this
416 // structure change. That way we don't end up here in
417 // subsequent calls to beginTimeStep() unless there's a new
418 // dynamic change to the well structure during a report step.
419 this->wellStructureChangedDynamically_ = false;
420 }
421
422 this->resetWGState();
423
424 const int reportStepIdx = simulator_.episodeIndex();
425 this->updateAndCommunicateGroupData(reportStepIdx,
426 simulator_.model().newtonMethod().numIterations());
427
428 this->wellState().updateWellsDefaultALQ(this->wells_ecl_, this->summaryState());
429 this->wellState().gliftTimeStepInit();
430
431 const double simulationTime = simulator_.time();
433 {
434 // test wells
435 wellTesting(reportStepIdx, simulationTime, local_deferredLogger);
436
437 // create the well container
438 createWellContainer(reportStepIdx);
439
440 // Wells are active if they are active wells on at least one process.
441 const Grid& grid = simulator_.vanguard().grid();
442 this->wells_active_ = grid.comm().max(!this->well_container_.empty());
443
444 // do the initialization for all the wells
445 // TODO: to see whether we can postpone of the intialization of the well containers to
446 // optimize the usage of the following several member variables
447 this->initWellContainer(reportStepIdx);
448
449 // update the updated cell flag
450 std::fill(is_cell_perforated_.begin(), is_cell_perforated_.end(), false);
451 for (auto& well : well_container_) {
452 well->updatePerforatedCell(is_cell_perforated_);
453 }
454
455 // calculate the efficiency factors for each well
456 this->calculateEfficiencyFactors(reportStepIdx);
457
458 if constexpr (has_polymer_)
459 {
460 if (PolymerModule::hasPlyshlog() || getPropValue<TypeTag, Properties::EnablePolymerMW>() ) {
461 this->setRepRadiusPerfLength();
462 }
463 }
464
465 }
466 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger, "beginTimeStep() failed: ",
467 this->terminal_output_, simulator_.vanguard().grid().comm());
468
469 for (auto& well : well_container_) {
470 well->setVFPProperties(this->vfp_properties_.get());
471 well->setGuideRate(&this->guideRate_);
472 }
473
474 this->updateInjFCMult(local_deferredLogger);
475
476 // Close completions due to economic reasons
477 for (auto& well : well_container_) {
478 well->closeCompletions(this->wellTestState());
479 }
480
481 // we need the inj_multiplier from the previous time step
482 this->initInjMult();
483
484 const auto& summaryState = simulator_.vanguard().summaryState();
485 if (alternative_well_rate_init_) {
486 // Update the well rates of well_state_, if only single-phase rates, to
487 // have proper multi-phase rates proportional to rates at bhp zero.
488 // This is done only for producers, as injectors will only have a single
489 // nonzero phase anyway.
490 for (auto& well : well_container_) {
491 const bool zero_target = well->stopppedOrZeroRateTarget(summaryState, this->wellState());
492 if (well->isProducer() && !zero_target) {
493 well->updateWellStateRates(simulator_, this->wellState(), local_deferredLogger);
494 }
495 }
496 }
497
498 for (auto& well : well_container_) {
499 if (well->isVFPActive(local_deferredLogger)){
500 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
501 }
502 }
503
504 // calculate the well potentials
505 try {
506 this->updateWellPotentials(reportStepIdx,
507 /*onlyAfterEvent*/true,
508 simulator_.vanguard().summaryConfig(),
509 local_deferredLogger);
510 } catch ( std::runtime_error& e ) {
511 const std::string msg = "A zero well potential is returned for output purposes. ";
512 local_deferredLogger.warning("WELL_POTENTIAL_CALCULATION_FAILED", msg);
513 }
514
515 //update guide rates
516 const auto& comm = simulator_.vanguard().grid().comm();
517 std::vector<Scalar> pot(this->numPhases(), 0.0);
518 const Group& fieldGroup = this->schedule().getGroup("FIELD", reportStepIdx);
520 this->schedule(),
521 summaryState,
522 this->phase_usage_,
523 reportStepIdx,
524 simulationTime,
525 this->wellState(),
526 this->groupState(),
527 comm,
528 &this->guideRate_,
529 pot,
530 local_deferredLogger);
531 std::string exc_msg;
532 auto exc_type = ExceptionType::NONE;
533 // update gpmaint targets
534 if (this->schedule_[reportStepIdx].has_gpmaint()) {
535 for (auto& calculator : regionalAveragePressureCalculator_) {
536 calculator.second->template defineState<ElementContext>(simulator_);
537 }
538 const double dt = simulator_.timeStepSize();
540 this->schedule_,
541 regionalAveragePressureCalculator_,
542 reportStepIdx,
543 dt,
544 this->wellState(),
545 this->groupState());
546 }
547 try {
548 // Compute initial well solution for new wells and injectors that change injection type i.e. WAG.
549 for (auto& well : well_container_) {
550 const uint64_t effective_events_mask = ScheduleEvents::WELL_STATUS_CHANGE
551 + ScheduleEvents::INJECTION_TYPE_CHANGED
552 + ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER
553 + ScheduleEvents::NEW_WELL;
554
555 const auto& events = this->schedule()[reportStepIdx].wellgroup_events();
556 const bool event = this->report_step_starts_ && events.hasEvent(well->name(), effective_events_mask);
557 const bool dyn_status_change = this->wellState().well(well->name()).status
558 != this->prevWellState().well(well->name()).status;
559
560 if (event || dyn_status_change) {
561 try {
562 well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), local_deferredLogger);
563 well->calculateExplicitQuantities(simulator_, this->wellState(), local_deferredLogger);
564 well->solveWellEquation(simulator_, this->wellState(), this->groupState(), local_deferredLogger);
565 } catch (const std::exception& e) {
566 const std::string msg = "Compute initial well solution for new well " + well->name() + " failed. Continue with zero initial rates";
567 local_deferredLogger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
568 }
569 }
570 }
571 }
572 // Catch clauses for all errors setting exc_type and exc_msg
573 OPM_PARALLEL_CATCH_CLAUSE(exc_type, exc_msg);
574
575 if (exc_type != ExceptionType::NONE) {
576 const std::string msg = "Compute initial well solution for new wells failed. Continue with zero initial rates";
577 local_deferredLogger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
578 }
579
580 logAndCheckForExceptionsAndThrow(local_deferredLogger,
581 exc_type, "beginTimeStep() failed: " + exc_msg, this->terminal_output_, comm);
582
583 }
584
585 template<typename TypeTag>
586 void
588 const double simulationTime,
589 DeferredLogger& deferred_logger)
590 {
591 for (const std::string& well_name : this->getWellsForTesting(timeStepIdx, simulationTime)) {
592 const Well& wellEcl = this->schedule().getWell(well_name, timeStepIdx);
593 if (wellEcl.getStatus() == Well::Status::SHUT)
594 continue;
595
596 WellInterfacePtr well = createWellForWellTest(well_name, timeStepIdx, deferred_logger);
597 // some preparation before the well can be used
598 well->init(&this->phase_usage_, depth_, gravity_, local_num_cells_, B_avg_, true);
599
600 Scalar well_efficiency_factor = wellEcl.getEfficiencyFactor();
601 WellGroupHelpers<Scalar>::accumulateGroupEfficiencyFactor(this->schedule().getGroup(wellEcl.groupName(),
602 timeStepIdx),
603 this->schedule(),
604 timeStepIdx,
605 well_efficiency_factor);
606
607 well->setWellEfficiencyFactor(well_efficiency_factor);
608 well->setVFPProperties(this->vfp_properties_.get());
609 well->setGuideRate(&this->guideRate_);
610
611 // initialize rates/previous rates to prevent zero fractions in vfp-interpolation
612 if (well->isProducer()) {
613 well->updateWellStateRates(simulator_, this->wellState(), deferred_logger);
614 }
615 if (well->isVFPActive(deferred_logger)) {
616 well->setPrevSurfaceRates(this->wellState(), this->prevWellState());
617 }
618
619 try {
620 well->wellTesting(simulator_, simulationTime, this->wellState(),
621 this->groupState(), this->wellTestState(), deferred_logger);
622 } catch (const std::exception& e) {
623 const std::string msg = fmt::format("Exception during testing of well: {}. The well will not open.\n Exception message: {}", wellEcl.name(), e.what());
624 deferred_logger.warning("WELL_TESTING_FAILED", msg);
625 }
626 }
627 }
628
629
630
631
632
633 // called at the end of a report step
634 template<typename TypeTag>
635 void
638 {
639 // Clear the communication data structures for above values.
640 for (auto&& pinfo : this->local_parallel_well_info_)
641 {
642 pinfo.get().clear();
643 }
644 }
645
646
647
648
649
650 // called at the end of a report step
651 template<typename TypeTag>
654 lastReport() const {return last_report_; }
655
656
657
658
659
660 // called at the end of a time step
661 template<typename TypeTag>
662 void
664 timeStepSucceeded(const double simulationTime, const double dt)
665 {
666 this->closed_this_step_.clear();
667
668 // time step is finished and we are not any more at the beginning of an report step
669 this->report_step_starts_ = false;
670 const int reportStepIdx = simulator_.episodeIndex();
671
672 DeferredLogger local_deferredLogger;
673 for (const auto& well : well_container_) {
674 if (getPropValue<TypeTag, Properties::EnablePolymerMW>() && well->isInjector()) {
675 well->updateWaterThroughput(dt, this->wellState());
676 }
677 }
678 // update connection transmissibility factor and d factor (if applicable) in the wellstate
679 for (const auto& well : well_container_) {
680 well->updateConnectionTransmissibilityFactor(simulator_, this->wellState().well(well->indexOfWell()));
681 well->updateConnectionDFactor(simulator_, this->wellState().well(well->indexOfWell()));
682 }
683
684 if (Indices::waterEnabled) {
685 this->updateFiltrationParticleVolume(dt, FluidSystem::waterPhaseIdx);
686 }
687
688 // at the end of the time step, updating the inj_multiplier saved in WellState for later use
689 this->updateInjMult(local_deferredLogger);
690
691 // report well switching
692 for (const auto& well : well_container_) {
693 well->reportWellSwitching(this->wellState().well(well->indexOfWell()), local_deferredLogger);
694 }
695 // report group switching
696 if (this->terminal_output_) {
697
698 for (const auto& [name, to] : this->switched_prod_groups_) {
699 const Group::ProductionCMode& oldControl = this->prevWGState().group_state.production_control(name);
700 std::string from = Group::ProductionCMode2String(oldControl);
701 if (to != from) {
702 std::string msg = " Production Group " + name
703 + " control mode changed from ";
704 msg += from;
705 msg += " to " + to;
706 local_deferredLogger.info(msg);
707 }
708 }
709 for (const auto& [key, to] : this->switched_inj_groups_) {
710 const std::string& name = key.first;
711 const Opm::Phase& phase = key.second;
712
713 const Group::InjectionCMode& oldControl = this->prevWGState().group_state.injection_control(name, phase);
714 std::string from = Group::InjectionCMode2String(oldControl);
715 if (to != from) {
716 std::string msg = " Injection Group " + name
717 + " control mode changed from ";
718 msg += from;
719 msg += " to " + to;
720 local_deferredLogger.info(msg);
721 }
722 }
723 }
724
725 // update the rate converter with current averages pressures etc in
726 rateConverter_->template defineState<ElementContext>(simulator_);
727
728 // calculate the well potentials
729 try {
730 this->updateWellPotentials(reportStepIdx,
731 /*onlyAfterEvent*/false,
732 simulator_.vanguard().summaryConfig(),
733 local_deferredLogger);
734 } catch ( std::runtime_error& e ) {
735 const std::string msg = "A zero well potential is returned for output purposes. ";
736 local_deferredLogger.warning("WELL_POTENTIAL_CALCULATION_FAILED", msg);
737 }
738
739 updateWellTestState(simulationTime, this->wellTestState());
740
741 // check group sales limits at the end of the timestep
742 const Group& fieldGroup = this->schedule_.getGroup("FIELD", reportStepIdx);
743 this->checkGEconLimits(fieldGroup, simulationTime,
744 simulator_.episodeIndex(), local_deferredLogger);
745 this->checkGconsaleLimits(fieldGroup, this->wellState(),
746 simulator_.episodeIndex(), local_deferredLogger);
747
748 this->calculateProductivityIndexValues(local_deferredLogger);
749
750 this->commitWGState();
751
752 const Opm::Parallel::Communication& comm = grid().comm();
753 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
754 if (this->terminal_output_) {
755 global_deferredLogger.logMessages();
756 }
757
758 //reporting output temperatures
759 this->computeWellTemperature();
760 }
761
762
763 template<typename TypeTag>
764 void
767 unsigned elemIdx) const
768 {
769 rate = 0;
770
771 if (!is_cell_perforated_[elemIdx])
772 return;
773
774 for (const auto& well : well_container_)
775 well->addCellRates(rate, elemIdx);
776 }
777
778
779 template<typename TypeTag>
780 template <class Context>
781 void
784 const Context& context,
785 unsigned spaceIdx,
786 unsigned timeIdx) const
787 {
788 rate = 0;
789 int elemIdx = context.globalSpaceIndex(spaceIdx, timeIdx);
790
791 if (!is_cell_perforated_[elemIdx])
792 return;
793
794 for (const auto& well : well_container_)
795 well->addCellRates(rate, elemIdx);
796 }
797
798
799
800 template<typename TypeTag>
801 void
803 initializeWellState(const int timeStepIdx)
804 {
805 std::vector<Scalar> cellPressures(this->local_num_cells_, 0.0);
806 ElementContext elemCtx(simulator_);
807
808 const auto& gridView = simulator_.vanguard().gridView();
809
811 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
812 elemCtx.updatePrimaryStencil(elem);
813 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
814
815 const auto& fs = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0).fluidState();
816 // copy of get perfpressure in Standard well except for value
817 Scalar& perf_pressure = cellPressures[elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0)];
818 if (Indices::oilEnabled) {
819 perf_pressure = fs.pressure(FluidSystem::oilPhaseIdx).value();
820 } else if (Indices::waterEnabled) {
821 perf_pressure = fs.pressure(FluidSystem::waterPhaseIdx).value();
822 } else {
823 perf_pressure = fs.pressure(FluidSystem::gasPhaseIdx).value();
824 }
825 }
826 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::initializeWellState() failed: ", simulator_.vanguard().grid().comm());
827
828 this->wellState().init(cellPressures, this->schedule(), this->wells_ecl_,
829 this->local_parallel_well_info_, timeStepIdx,
830 &this->prevWellState(), this->well_perf_data_,
831 this->summaryState());
832 }
833
834
835
836
837
838 template<typename TypeTag>
839 void
841 createWellContainer(const int report_step)
842 {
843 DeferredLogger local_deferredLogger;
844
845 const int nw = this->numLocalWells();
846
847 well_container_.clear();
848
849 if (nw > 0) {
850 well_container_.reserve(nw);
851
852 for (int w = 0; w < nw; ++w) {
853 const Well& well_ecl = this->wells_ecl_[w];
854
855 if (!well_ecl.hasConnections()) {
856 // No connections in this well. Nothing to do.
857 continue;
858 }
859
860 const std::string& well_name = well_ecl.name();
861 const auto well_status = this->schedule()
862 .getWell(well_name, report_step).getStatus();
863
864 if ((well_ecl.getStatus() == Well::Status::SHUT) ||
865 (well_status == Well::Status::SHUT))
866 {
867 // Due to ACTIONX the well might have been closed behind our back.
868 if (well_ecl.getStatus() != Well::Status::SHUT) {
869 this->closed_this_step_.insert(well_name);
870 this->wellState().shutWell(w);
871 }
872
873 continue;
874 }
875
876 // A new WCON keywords can re-open a well that was closed/shut due to Physical limit
877 if (this->wellTestState().well_is_closed(well_name)) {
878 // The well was shut this timestep, we are most likely retrying
879 // a timestep without the well in question, after it caused
880 // repeated timestep cuts. It should therefore not be opened,
881 // even if it was new or received new targets this report step.
882 const bool closed_this_step = (this->wellTestState().lastTestTime(well_name) == simulator_.time());
883 // TODO: more checking here, to make sure this standard more specific and complete
884 // maybe there is some WCON keywords will not open the well
885 auto& events = this->wellState().well(w).events;
886 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
887 if (!closed_this_step) {
888 this->wellTestState().open_well(well_name);
889 this->wellTestState().open_completions(well_name);
890 }
891 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
892 }
893 }
894
895 // TODO: should we do this for all kinds of closing reasons?
896 // something like wellTestState().hasWell(well_name)?
897 bool wellIsStopped = false;
898 if (this->wellTestState().well_is_closed(well_name))
899 {
900 if (well_ecl.getAutomaticShutIn()) {
901 // shut wells are not added to the well container
902 this->wellState().shutWell(w);
903 continue;
904 } else {
905 if (!well_ecl.getAllowCrossFlow()) {
906 // stopped wells where cross flow is not allowed
907 // are not added to the well container
908 this->wellState().shutWell(w);
909 continue;
910 }
911 // stopped wells are added to the container but marked as stopped
912 this->wellState().stopWell(w);
913 wellIsStopped = true;
914 }
915 }
916
917 // shut wells with zero rante constraints and disallowing
918 if (!well_ecl.getAllowCrossFlow()) {
919 const bool any_zero_rate_constraint = well_ecl.isProducer()
920 ? well_ecl.productionControls(this->summaryState_).anyZeroRateConstraint()
921 : well_ecl.injectionControls(this->summaryState_).anyZeroRateConstraint();
922 if (any_zero_rate_constraint) {
923 // Treat as shut, do not add to container.
924 local_deferredLogger.debug(fmt::format(" Well {} gets shut due to having zero rate constraint and disallowing crossflow ", well_ecl.name()) );
925 this->wellState().shutWell(w);
926 continue;
927 }
928 }
929
930 if (well_status == Well::Status::STOP) {
931 this->wellState().stopWell(w);
932 wellIsStopped = true;
933 }
934
935 well_container_.emplace_back(this->createWellPointer(w, report_step));
936
937 if (wellIsStopped)
938 well_container_.back()->stopWell();
939 }
940 }
941
942 // Collect log messages and print.
943
944 const Opm::Parallel::Communication& comm = grid().comm();
945 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
946 if (this->terminal_output_) {
947 global_deferredLogger.logMessages();
948 }
949
950 this->well_container_generic_.clear();
951 for (auto& w : well_container_)
952 this->well_container_generic_.push_back(w.get());
953
954 const auto& network = this->schedule()[report_step].network();
955 if (network.active() && !this->node_pressures_.empty()) {
956 for (auto& well: this->well_container_generic_) {
957 // Producers only, since we so far only support the
958 // "extended" network model (properties defined by
959 // BRANPROP and NODEPROP) which only applies to producers.
960 if (well->isProducer()) {
961 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
962 if (it != this->node_pressures_.end()) {
963 // The well belongs to a group which has a network nodal pressure,
964 // set the dynamic THP constraint based on the network nodal pressure
965 const Scalar nodal_pressure = it->second;
966 well->setDynamicThpLimit(nodal_pressure);
967 }
968 }
969 }
970 }
971
972 this->registerOpenWellsForWBPCalculation();
973 }
974
975
976
977
978
979 template <typename TypeTag>
982 createWellPointer(const int wellID, const int report_step) const
983 {
984 const auto is_multiseg = this->wells_ecl_[wellID].isMultiSegment();
985
986 if (! (this->param_.use_multisegment_well_ && is_multiseg)) {
987 return this->template createTypedWellPointer<StandardWell<TypeTag>>(wellID, report_step);
988 }
989 else {
990 return this->template createTypedWellPointer<MultisegmentWell<TypeTag>>(wellID, report_step);
991 }
992 }
993
994
995
996
997
998 template <typename TypeTag>
999 template <typename WellType>
1000 std::unique_ptr<WellType>
1002 createTypedWellPointer(const int wellID, const int time_step) const
1003 {
1004 // Use the pvtRegionIdx from the top cell
1005 const auto& perf_data = this->well_perf_data_[wellID];
1006
1007 // Cater for case where local part might have no perforations.
1008 const auto pvtreg = perf_data.empty()
1009 ? 0 : this->pvt_region_idx_[perf_data.front().cell_index];
1010
1011 const auto& parallel_well_info = this->local_parallel_well_info_[wellID].get();
1012 const auto global_pvtreg = parallel_well_info.broadcastFirstPerforationValue(pvtreg);
1013
1014 return std::make_unique<WellType>(this->wells_ecl_[wellID],
1015 parallel_well_info,
1016 time_step,
1017 this->param_,
1018 *this->rateConverter_,
1019 global_pvtreg,
1020 this->numComponents(),
1021 this->numPhases(),
1022 wellID,
1023 perf_data);
1024 }
1025
1026
1027
1028
1029
1030 template<typename TypeTag>
1033 createWellForWellTest(const std::string& well_name,
1034 const int report_step,
1035 DeferredLogger& deferred_logger) const
1036 {
1037 // Finding the location of the well in wells_ecl
1038 const int nw_wells_ecl = this->wells_ecl_.size();
1039 int index_well_ecl = 0;
1040 for (; index_well_ecl < nw_wells_ecl; ++index_well_ecl) {
1041 if (well_name == this->wells_ecl_[index_well_ecl].name()) {
1042 break;
1043 }
1044 }
1045 // It should be able to find in wells_ecl.
1046 if (index_well_ecl == nw_wells_ecl) {
1047 OPM_DEFLOG_THROW(std::logic_error,
1048 fmt::format("Could not find well {} in wells_ecl ", well_name),
1049 deferred_logger);
1050 }
1051
1052 return this->createWellPointer(index_well_ecl, report_step);
1053 }
1054
1055
1056
1057 template<typename TypeTag>
1058 void
1061 const double dt = this->simulator_.timeStepSize();
1062 // TODO: should we also have the group and network backed-up here in case the solution did not get converged?
1063 auto& well_state = this->wellState();
1064 const std::size_t max_iter = param_.network_max_iterations_;
1065 bool converged = false;
1066 std::size_t iter = 0;
1067 bool changed_well_group = false;
1068 do {
1069 changed_well_group = updateWellControlsAndNetwork(true, dt, deferred_logger);
1070 assembleWellEqWithoutIteration(dt, deferred_logger);
1071 converged = this->getWellConvergence(this->B_avg_, true).converged() && !changed_well_group;
1072 if (converged) {
1073 break;
1074 }
1075 ++iter;
1076 for (auto& well : this->well_container_) {
1077 const auto& summary_state = this->simulator_.vanguard().summaryState();
1078 well->solveEqAndUpdateWellState(summary_state, well_state, deferred_logger);
1079 }
1080 this->initPrimaryVariablesEvaluation();
1081 } while (iter < max_iter);
1082
1083 if (!converged) {
1084 const std::string msg = fmt::format("Initial (pre-step) network balance did not get converged with {} iterations, "
1085 "unconverged network balance result will be used", max_iter);
1086 deferred_logger.warning(msg);
1087 } else {
1088 const std::string msg = fmt::format("Initial (pre-step) network balance converged with {} iterations", iter);
1089 deferred_logger.debug(msg);
1090 }
1091 }
1092
1093
1094
1095
1096 template<typename TypeTag>
1097 void
1099 assemble(const int iterationIdx,
1100 const double dt)
1101 {
1102
1103 DeferredLogger local_deferredLogger;
1104 if (this->glift_debug) {
1105 const std::string msg = fmt::format(
1106 "assemble() : iteration {}" , iterationIdx);
1107 this->gliftDebug(msg, local_deferredLogger);
1108 }
1109 last_report_ = SimulatorReportSingle();
1110 Dune::Timer perfTimer;
1111 perfTimer.start();
1112 this->closed_offending_wells_.clear();
1113
1114 {
1115 const int episodeIdx = simulator_.episodeIndex();
1116 const auto& network = this->schedule()[episodeIdx].network();
1117 if (!this->wellsActive() && !network.active()) {
1118 return;
1119 }
1120 }
1121
1122 if (iterationIdx == 0 && this->wellsActive()) {
1123 // try-catch is needed here as updateWellControls
1124 // contains global communication and has either to
1125 // be reached by all processes or all need to abort
1126 // before.
1128 {
1129 calculateExplicitQuantities(local_deferredLogger);
1130 prepareTimeStep(local_deferredLogger);
1131 }
1132 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1133 "assemble() failed (It=0): ",
1134 this->terminal_output_, grid().comm());
1135 }
1136
1137 const bool well_group_control_changed = updateWellControlsAndNetwork(false, dt, local_deferredLogger);
1138
1139 // even when there is no wells active, the network nodal pressure still need to be updated through updateWellControlsAndNetwork()
1140 // but there is no need to assemble the well equations
1141 if ( ! this->wellsActive() ) {
1142 return;
1143 }
1144
1145 assembleWellEqWithoutIteration(dt, local_deferredLogger);
1146
1147 // if group or well control changes we don't consider the
1148 // case converged
1149 last_report_.well_group_control_changed = well_group_control_changed;
1150 last_report_.assemble_time_well += perfTimer.stop();
1151 }
1152
1153
1154
1155
1156 template<typename TypeTag>
1157 bool
1159 updateWellControlsAndNetwork(const bool mandatory_network_balance, const double dt, DeferredLogger& local_deferredLogger)
1160 {
1161 // not necessarily that we always need to update once of the network solutions
1162 bool do_network_update = true;
1163 bool well_group_control_changed = false;
1164 // after certain number of the iterations, we use relaxed tolerance for the network update
1165 const std::size_t iteration_to_relax = param_.network_max_strict_iterations_;
1166 // after certain number of the iterations, we terminate
1167 const std::size_t max_iteration = param_.network_max_iterations_;
1168 std::size_t network_update_iteration = 0;
1169 while (do_network_update) {
1170 if (this->terminal_output_ && (network_update_iteration == iteration_to_relax) ) {
1171 local_deferredLogger.info(" we begin using relaxed tolerance for network update now after " + std::to_string(iteration_to_relax) + " iterations ");
1172 }
1173 const bool relax_network_balance = network_update_iteration >= iteration_to_relax;
1174 std::tie(do_network_update, well_group_control_changed) =
1175 updateWellControlsAndNetworkIteration(mandatory_network_balance, relax_network_balance, dt,local_deferredLogger);
1176 ++network_update_iteration;
1177
1178 if (network_update_iteration >= max_iteration ) {
1179 if (this->terminal_output_) {
1180 local_deferredLogger.info("maximum of " + std::to_string(max_iteration) + " iterations has been used, we stop the network update now. "
1181 "The simulation will continue with unconverged network results");
1182 }
1183 break;
1184 }
1185 }
1186 return well_group_control_changed;
1187 }
1188
1189
1190
1191
1192 template<typename TypeTag>
1193 std::pair<bool, bool>
1195 updateWellControlsAndNetworkIteration(const bool mandatory_network_balance,
1196 const bool relax_network_tolerance,
1197 const double dt,
1198 DeferredLogger& local_deferredLogger)
1199 {
1200 auto [well_group_control_changed, more_network_update] =
1201 updateWellControls(mandatory_network_balance, local_deferredLogger, relax_network_tolerance);
1202
1203 bool alq_updated = false;
1205 {
1206 // Set the well primary variables based on the value of well solutions
1207 initPrimaryVariablesEvaluation();
1208
1209 alq_updated = maybeDoGasLiftOptimize(local_deferredLogger);
1210
1211 prepareWellsBeforeAssembling(dt, local_deferredLogger);
1212 }
1213 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger, "updateWellControlsAndNetworkIteration() failed: ",
1214 this->terminal_output_, grid().comm());
1215
1216 //update guide rates
1217 const int reportStepIdx = simulator_.episodeIndex();
1218 if (alq_updated || BlackoilWellModelGuideRates(*this).
1219 guideRateUpdateIsNeeded(reportStepIdx)) {
1220 const double simulationTime = simulator_.time();
1221 const auto& comm = simulator_.vanguard().grid().comm();
1222 const auto& summaryState = simulator_.vanguard().summaryState();
1223 std::vector<Scalar> pot(this->numPhases(), 0.0);
1224 const Group& fieldGroup = this->schedule().getGroup("FIELD", reportStepIdx);
1226 this->schedule(),
1227 summaryState,
1228 this->phase_usage_,
1229 reportStepIdx,
1230 simulationTime,
1231 this->wellState(),
1232 this->groupState(),
1233 comm,
1234 &this->guideRate_,
1235 pot,
1236 local_deferredLogger);
1237 }
1238
1239 return {more_network_update, well_group_control_changed};
1240 }
1241
1242
1243
1244 template<typename TypeTag>
1245 void
1247 assembleDomain([[maybe_unused]] const int iterationIdx,
1248 const double dt,
1249 const Domain& domain)
1250 {
1251 last_report_ = SimulatorReportSingle();
1252 Dune::Timer perfTimer;
1253 perfTimer.start();
1254
1255 {
1256 const int episodeIdx = simulator_.episodeIndex();
1257 const auto& network = this->schedule()[episodeIdx].network();
1258 if (!this->wellsActive() && !network.active()) {
1259 return;
1260 }
1261 }
1262
1263 // We assume that calculateExplicitQuantities() and
1264 // prepareTimeStep() have been called already for the entire
1265 // well model, so we do not need to do it here (when
1266 // iterationIdx is 0).
1267
1268 DeferredLogger local_deferredLogger;
1269 updateWellControlsDomain(local_deferredLogger, domain);
1270 initPrimaryVariablesEvaluationDomain(domain);
1271 assembleWellEqDomain(dt, domain, local_deferredLogger);
1272
1273 // TODO: errors here must be caught higher up, as this method is not called in parallel.
1274 // We will log errors on rank 0, but not other ranks for now.
1275 if (this->terminal_output_) {
1276 local_deferredLogger.logMessages();
1277 }
1278
1279 last_report_.converged = true;
1280 last_report_.assemble_time_well += perfTimer.stop();
1281 }
1282
1283
1284 template<typename TypeTag>
1285 bool
1288 {
1289 bool do_glift_optimization = false;
1290 int num_wells_changed = 0;
1291 const double simulation_time = simulator_.time();
1292 const Scalar min_wait = simulator_.vanguard().schedule().glo(simulator_.episodeIndex()).min_wait();
1293 // We only optimize if a min_wait time has past.
1294 // If all_newton is true we still want to optimize several times pr timestep
1295 // i.e. we also optimize if check simulation_time == last_glift_opt_time_
1296 // that is when the last_glift_opt_time is already updated with the current time step
1297 if ( simulation_time == this->last_glift_opt_time_ || simulation_time >= (this->last_glift_opt_time_ + min_wait)) {
1298 do_glift_optimization = true;
1299 this->last_glift_opt_time_ = simulation_time;
1300 }
1301
1302 if (do_glift_optimization) {
1303 GLiftOptWells glift_wells;
1304 GLiftProdWells prod_wells;
1305 GLiftWellStateMap state_map;
1306 // NOTE: To make GasLiftGroupInfo (see below) independent of the TypeTag
1307 // associated with *this (i.e. BlackoilWellModel<TypeTag>) we observe
1308 // that GasLiftGroupInfo's only dependence on *this is that it needs to
1309 // access the eclipse Wells in the well container (the eclipse Wells
1310 // themselves are independent of the TypeTag).
1311 // Hence, we extract them from the well container such that we can pass
1312 // them to the GasLiftGroupInfo constructor.
1313 GLiftEclWells ecl_well_map;
1314 initGliftEclWellMap(ecl_well_map);
1315 GasLiftGroupInfo group_info {
1316 ecl_well_map,
1317 simulator_.vanguard().schedule(),
1318 simulator_.vanguard().summaryState(),
1319 simulator_.episodeIndex(),
1320 simulator_.model().newtonMethod().numIterations(),
1321 this->phase_usage_,
1322 deferred_logger,
1323 this->wellState(),
1324 this->groupState(),
1325 simulator_.vanguard().grid().comm(),
1326 this->glift_debug
1327 };
1328 group_info.initialize();
1329 gasLiftOptimizationStage1(deferred_logger, prod_wells, glift_wells,
1330 group_info, state_map);
1331 this->gasLiftOptimizationStage2(deferred_logger, prod_wells, glift_wells,
1332 group_info, state_map, simulator_.episodeIndex());
1333 if (this->glift_debug) {
1334 this->gliftDebugShowALQ(deferred_logger);
1335 }
1336 num_wells_changed = glift_wells.size();
1337 }
1338 num_wells_changed = this->comm_.sum(num_wells_changed);
1339 return num_wells_changed > 0;
1340 }
1341
1342 template<typename TypeTag>
1343 void
1346 GLiftProdWells &prod_wells, GLiftOptWells &glift_wells,
1347 GasLiftGroupInfo &group_info, GLiftWellStateMap &state_map)
1348 {
1349 auto comm = simulator_.vanguard().grid().comm();
1350 int num_procs = comm.size();
1351 // NOTE: Gas lift optimization stage 1 seems to be difficult
1352 // to do in parallel since the wells are optimized on different
1353 // processes and each process needs to know the current ALQ allocated
1354 // to each group it is a memeber of in order to check group limits and avoid
1355 // allocating more ALQ than necessary. (Surplus ALQ is removed in
1356 // stage 2). In stage1, as each well is adding ALQ, the current group ALQ needs
1357 // to be communicated to the other processes. But there is no common
1358 // synchronization point that all process will reach in the
1359 // runOptimizeLoop_() in GasLiftSingleWell.cpp.
1360 //
1361 // TODO: Maybe a better solution could be invented by distributing
1362 // wells according to certain parent groups. Then updated group rates
1363 // might not have to be communicated to the other processors.
1364
1365 // Currently, the best option seems to be to run this part sequentially
1366 // (not in parallel).
1367 //
1368 // TODO: The simplest approach seems to be if a) one process could take
1369 // ownership of all the wells (the union of all the wells in the
1370 // well_container_ of each process) then this process could do the
1371 // optimization, while the other processes could wait for it to
1372 // finish (e.g. comm.barrier()), or alternatively, b) if all
1373 // processes could take ownership of all the wells. Then there
1374 // would be no need for synchronization here..
1375 //
1376 for (int i = 0; i< num_procs; i++) {
1377 int num_rates_to_sync = 0; // communication variable
1378 GLiftSyncGroups groups_to_sync;
1379 if (comm.rank() == i) {
1380 // Run stage1: Optimize single wells while also checking group limits
1381 for (const auto& well : well_container_) {
1382 // NOTE: Only the wells in "group_info" needs to be optimized
1383 if (group_info.hasWell(well->name())) {
1384 gasLiftOptimizationStage1SingleWell(
1385 well.get(), deferred_logger, prod_wells, glift_wells,
1386 group_info, state_map, groups_to_sync
1387 );
1388 }
1389 }
1390 num_rates_to_sync = groups_to_sync.size();
1391 }
1392 num_rates_to_sync = comm.sum(num_rates_to_sync);
1393 if (num_rates_to_sync > 0) {
1394 std::vector<int> group_indexes;
1395 group_indexes.reserve(num_rates_to_sync);
1396 std::vector<Scalar> group_alq_rates;
1397 group_alq_rates.reserve(num_rates_to_sync);
1398 std::vector<Scalar> group_oil_rates;
1399 group_oil_rates.reserve(num_rates_to_sync);
1400 std::vector<Scalar> group_gas_rates;
1401 group_gas_rates.reserve(num_rates_to_sync);
1402 std::vector<Scalar> group_water_rates;
1403 group_water_rates.reserve(num_rates_to_sync);
1404 if (comm.rank() == i) {
1405 for (auto idx : groups_to_sync) {
1406 auto [oil_rate, gas_rate, water_rate, alq] = group_info.getRates(idx);
1407 group_indexes.push_back(idx);
1408 group_oil_rates.push_back(oil_rate);
1409 group_gas_rates.push_back(gas_rate);
1410 group_water_rates.push_back(water_rate);
1411 group_alq_rates.push_back(alq);
1412 }
1413 } else {
1414 group_indexes.resize(num_rates_to_sync);
1415 group_oil_rates.resize(num_rates_to_sync);
1416 group_gas_rates.resize(num_rates_to_sync);
1417 group_water_rates.resize(num_rates_to_sync);
1418 group_alq_rates.resize(num_rates_to_sync);
1419 }
1420#if HAVE_MPI
1421 Parallel::MpiSerializer ser(comm);
1422 ser.broadcast(i, group_indexes, group_oil_rates,
1423 group_gas_rates, group_water_rates, group_alq_rates);
1424#endif
1425 if (comm.rank() != i) {
1426 for (int j=0; j<num_rates_to_sync; j++) {
1427 group_info.updateRate(group_indexes[j],
1428 group_oil_rates[j], group_gas_rates[j], group_water_rates[j], group_alq_rates[j]);
1429 }
1430 }
1431 if (this->glift_debug) {
1432 int counter = 0;
1433 if (comm.rank() == i) {
1434 counter = this->wellState().gliftGetDebugCounter();
1435 }
1436 counter = comm.sum(counter);
1437 if (comm.rank() != i) {
1438 this->wellState().gliftSetDebugCounter(counter);
1439 }
1440 }
1441 }
1442 }
1443 }
1444
1445 // NOTE: this method cannot be const since it passes this->wellState()
1446 // (see below) to the GasLiftSingleWell constructor which accepts WellState
1447 // as a non-const reference..
1448 template<typename TypeTag>
1449 void
1452 DeferredLogger& deferred_logger,
1453 GLiftProdWells &prod_wells, GLiftOptWells &glift_wells,
1454 GasLiftGroupInfo &group_info, GLiftWellStateMap &state_map,
1455 GLiftSyncGroups& sync_groups)
1456 {
1457 const auto& summary_state = simulator_.vanguard().summaryState();
1458 std::unique_ptr<GasLiftSingleWell> glift
1459 = std::make_unique<GasLiftSingleWell>(
1460 *well, simulator_, summary_state,
1461 deferred_logger, this->wellState(), this->groupState(),
1462 group_info, sync_groups, this->comm_, this->glift_debug);
1463 auto state = glift->runOptimize(
1464 simulator_.model().newtonMethod().numIterations());
1465 if (state) {
1466 state_map.insert({well->name(), std::move(state)});
1467 glift_wells.insert({well->name(), std::move(glift)});
1468 return;
1469 }
1470 prod_wells.insert({well->name(), well});
1471 }
1472
1473
1474 template<typename TypeTag>
1475 void
1478 {
1479 for ( const auto& well: well_container_ ) {
1480 ecl_well_map.try_emplace(
1481 well->name(), &(well->wellEcl()), well->indexOfWell());
1482 }
1483 }
1484
1485
1486 template<typename TypeTag>
1487 void
1489 assembleWellEq(const double dt, DeferredLogger& deferred_logger)
1490 {
1491 for (auto& well : well_container_) {
1492 well->assembleWellEq(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1493 }
1494 }
1495
1496
1497 template<typename TypeTag>
1498 void
1500 assembleWellEqDomain(const double dt, const Domain& domain, DeferredLogger& deferred_logger)
1501 {
1502 for (auto& well : well_container_) {
1503 if (well_domain_.at(well->name()) == domain.index) {
1504 well->assembleWellEq(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1505 }
1506 }
1507 }
1508
1509
1510 template<typename TypeTag>
1511 void
1513 prepareWellsBeforeAssembling(const double dt, DeferredLogger& deferred_logger)
1514 {
1515 for (auto& well : well_container_) {
1516 well->prepareWellBeforeAssembling(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1517 }
1518 }
1519
1520
1521 template<typename TypeTag>
1522 void
1524 assembleWellEqWithoutIteration(const double dt, DeferredLogger& deferred_logger)
1525 {
1526 // We make sure that all processes throw in case there is an exception
1527 // on one of them (WetGasPvt::saturationPressure might throw if not converged)
1529
1530 for (auto& well: well_container_) {
1531 well->assembleWellEqWithoutIteration(simulator_, dt, this->wellState(), this->groupState(),
1532 deferred_logger);
1533 }
1534 OPM_END_PARALLEL_TRY_CATCH_LOG(deferred_logger, "BlackoilWellModel::assembleWellEqWithoutIteration failed: ",
1535 this->terminal_output_, grid().comm());
1536
1537 }
1538
1539
1540 template<typename TypeTag>
1541 void
1543 apply(BVector& r) const
1544 {
1545 for (auto& well : well_container_) {
1546 well->apply(r);
1547 }
1548 }
1549
1550
1551 // Ax = A x - C D^-1 B x
1552 template<typename TypeTag>
1553 void
1555 apply(const BVector& x, BVector& Ax) const
1556 {
1557 for (auto& well : well_container_) {
1558 well->apply(x, Ax);
1559 }
1560 }
1561
1562#if COMPILE_BDA_BRIDGE
1563 template<typename TypeTag>
1564 void
1566 getWellContributions(WellContributions& wellContribs) const
1567 {
1568 // prepare for StandardWells
1570
1571 for(unsigned int i = 0; i < well_container_.size(); i++){
1572 auto& well = well_container_[i];
1573 std::shared_ptr<StandardWell<TypeTag> > derived = std::dynamic_pointer_cast<StandardWell<TypeTag> >(well);
1574 if (derived) {
1575 wellContribs.addNumBlocks(derived->linSys().getNumBlocks());
1576 }
1577 }
1578
1579 // allocate memory for data from StandardWells
1580 wellContribs.alloc();
1581
1582 for(unsigned int i = 0; i < well_container_.size(); i++){
1583 auto& well = well_container_[i];
1584 // maybe WellInterface could implement addWellContribution()
1585 auto derived_std = std::dynamic_pointer_cast<StandardWell<TypeTag>>(well);
1586 if (derived_std) {
1587 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1588 } else {
1589 auto derived_ms = std::dynamic_pointer_cast<MultisegmentWell<TypeTag> >(well);
1590 if (derived_ms) {
1591 derived_ms->linSys().extract(wellContribs);
1592 } else {
1593 OpmLog::warning("Warning unknown type of well");
1594 }
1595 }
1596 }
1597 }
1598#endif
1599
1600 // Ax = Ax - alpha * C D^-1 B x
1601 template<typename TypeTag>
1602 void
1604 applyScaleAdd(const Scalar alpha, const BVector& x, BVector& Ax) const
1605 {
1606 if (this->well_container_.empty()) {
1607 return;
1608 }
1609
1610 if( scaleAddRes_.size() != Ax.size() ) {
1611 scaleAddRes_.resize( Ax.size() );
1612 }
1613
1614 scaleAddRes_ = 0.0;
1615 // scaleAddRes_ = - C D^-1 B x
1616 apply( x, scaleAddRes_ );
1617 // Ax = Ax + alpha * scaleAddRes_
1618 Ax.axpy( alpha, scaleAddRes_ );
1619 }
1620
1621 template<typename TypeTag>
1622 void
1625 {
1626 for ( const auto& well: well_container_ ) {
1627 well->addWellContributions(jacobian);
1628 }
1629 }
1630
1631 template<typename TypeTag>
1632 void
1634 addWellPressureEquations(PressureMatrix& jacobian, const BVector& weights,const bool use_well_weights) const
1635 {
1636 int nw = this->numLocalWellsEnd();
1637 int rdofs = local_num_cells_;
1638 for ( int i = 0; i < nw; i++ ){
1639 int wdof = rdofs + i;
1640 jacobian[wdof][wdof] = 1.0;// better scaling ?
1641 }
1642
1643 for ( const auto& well : well_container_ ) {
1644 well->addWellPressureEquations(jacobian, weights, pressureVarIndex, use_well_weights, this->wellState());
1645 }
1646 }
1647
1648 template <typename TypeTag>
1651 std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress) const
1652 {
1653 // NB this loop may write multiple times to the same element
1654 // if a cell is perforated by more than one well, so it should
1655 // not be OpenMP-parallelized.
1656 for (const auto& well : well_container_) {
1657 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1658 continue;
1659 }
1660 const auto& cells = well->cells();
1661 const auto& rates = well->connectionRates();
1662 for (unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1663 unsigned cellIdx = cells[perfIdx];
1664 auto rate = rates[perfIdx];
1665 rate *= -1.0;
1666 VectorBlockType res(0.0);
1667 using MatrixBlockType = typename SparseMatrixAdapter::MatrixBlock;
1668 MatrixBlockType bMat(0.0);
1669 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1670 residual[cellIdx] += res;
1671 *diagMatAddress[cellIdx] += bMat;
1672 }
1673 }
1674 }
1675
1676
1677 template<typename TypeTag>
1678 void
1681 {
1682 int nw = this->numLocalWellsEnd();
1683 int rdofs = local_num_cells_;
1684 for(int i=0; i < nw; i++){
1685 int wdof = rdofs + i;
1686 jacobian.entry(wdof,wdof) = 1.0;// better scaling ?
1687 }
1688 std::vector<std::vector<int>> wellconnections = this->getMaxWellConnections();
1689 for(int i=0; i < nw; i++){
1690 const auto& perfcells = wellconnections[i];
1691 for(int perfcell : perfcells){
1692 int wdof = rdofs + i;
1693 jacobian.entry(wdof,perfcell) = 0.0;
1694 jacobian.entry(perfcell, wdof) = 0.0;
1695 }
1696 }
1697 }
1698
1699
1700 template<typename TypeTag>
1701 void
1704 {
1705 DeferredLogger local_deferredLogger;
1707 {
1708 const auto& summary_state = simulator_.vanguard().summaryState();
1709 for (auto& well : well_container_) {
1710 well->recoverWellSolutionAndUpdateWellState(summary_state, x, this->wellState(), local_deferredLogger);
1711 }
1712 }
1713 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1714 "recoverWellSolutionAndUpdateWellState() failed: ",
1715 this->terminal_output_, simulator_.vanguard().grid().comm());
1716 }
1717
1718
1719 template<typename TypeTag>
1720 void
1723 {
1724 // Note: no point in trying to do a parallel gathering
1725 // try/catch here, as this function is not called in
1726 // parallel but for each individual domain of each rank.
1727 DeferredLogger local_deferredLogger;
1728 const auto& summary_state = this->simulator_.vanguard().summaryState();
1729 for (auto& well : well_container_) {
1730 if (well_domain_.at(well->name()) == domain.index) {
1731 well->recoverWellSolutionAndUpdateWellState(summary_state, x,
1732 this->wellState(),
1733 local_deferredLogger);
1734 }
1735 }
1736 // TODO: avoid losing the logging information that could
1737 // be stored in the local_deferredlogger in a parallel case.
1738 if (this->terminal_output_) {
1739 local_deferredLogger.logMessages();
1740 }
1741 }
1742
1743
1744 template<typename TypeTag>
1745 void
1748 {
1749 for (auto& well : well_container_) {
1750 well->initPrimaryVariablesEvaluation();
1751 }
1752 }
1753
1754
1755 template<typename TypeTag>
1756 void
1759 {
1760 for (auto& well : well_container_) {
1761 if (well_domain_.at(well->name()) == domain.index) {
1762 well->initPrimaryVariablesEvaluation();
1763 }
1764 }
1765 }
1766
1767
1768
1769
1770
1771
1772 template<typename TypeTag>
1775 getDomainWellConvergence(const Domain& domain,
1776 const std::vector<Scalar>& B_avg,
1777 DeferredLogger& local_deferredLogger) const
1778 {
1779 const auto& summary_state = simulator_.vanguard().summaryState();
1780 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1781 const bool relax_tolerance = iterationIdx > param_.strict_outer_iter_wells_;
1782
1783 ConvergenceReport report;
1784 for (const auto& well : well_container_) {
1785 if ((well_domain_.at(well->name()) == domain.index)) {
1786 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1787 report += well->getWellConvergence(summary_state,
1788 this->wellState(),
1789 B_avg,
1790 local_deferredLogger,
1791 relax_tolerance);
1792 } else {
1793 ConvergenceReport xreport;
1794 using CR = ConvergenceReport;
1795 xreport.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1796 report += xreport;
1797 }
1798 }
1799 }
1800
1801 // Log debug messages for NaN or too large residuals.
1802 if (this->terminal_output_) {
1803 for (const auto& f : report.wellFailures()) {
1804 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1805 local_deferredLogger.debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1806 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1807 local_deferredLogger.debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1808 }
1809 }
1810 }
1811 return report;
1812 }
1813
1814
1815
1816
1817
1818 template<typename TypeTag>
1821 getWellConvergence(const std::vector<Scalar>& B_avg, bool checkWellGroupControls) const
1822 {
1823
1824 DeferredLogger local_deferredLogger;
1825 // Get global (from all processes) convergence report.
1826 ConvergenceReport local_report;
1827 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1828 for (const auto& well : well_container_) {
1829 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1830 const auto& summary_state = simulator_.vanguard().summaryState();
1831 local_report += well->getWellConvergence(
1832 summary_state, this->wellState(), B_avg, local_deferredLogger,
1833 iterationIdx > param_.strict_outer_iter_wells_);
1834 } else {
1835 ConvergenceReport report;
1836 using CR = ConvergenceReport;
1837 report.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1838 local_report += report;
1839 }
1840 }
1841
1842 const Opm::Parallel::Communication comm = grid().comm();
1843 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
1844 ConvergenceReport report = gatherConvergenceReport(local_report, comm);
1845
1846 // the well_group_control_changed info is already communicated
1847 if (checkWellGroupControls) {
1848 report.setWellGroupTargetsViolated(this->lastReport().well_group_control_changed);
1849 }
1850
1851 if (this->terminal_output_) {
1852 global_deferredLogger.logMessages();
1853 }
1854 // Log debug messages for NaN or too large residuals.
1855 if (this->terminal_output_) {
1856 for (const auto& f : report.wellFailures()) {
1857 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1858 OpmLog::debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1859 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1860 OpmLog::debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1861 }
1862 }
1863 }
1864 return report;
1865 }
1866
1867
1868
1869
1870
1871 template<typename TypeTag>
1872 void
1874 calculateExplicitQuantities(DeferredLogger& deferred_logger) const
1875 {
1876 // TODO: checking isOperableAndSolvable() ?
1877 for (auto& well : well_container_) {
1878 well->calculateExplicitQuantities(simulator_, this->wellState(), deferred_logger);
1879 }
1880 }
1881
1882
1883
1884
1885
1886 template<typename TypeTag>
1887 std::pair<bool, bool>
1889 updateWellControls(const bool mandatory_network_balance, DeferredLogger& deferred_logger, const bool relax_network_tolerance)
1890 {
1891 const int episodeIdx = simulator_.episodeIndex();
1892 const auto& network = this->schedule()[episodeIdx].network();
1893 if (!this->wellsActive() && !network.active()) {
1894 return {false, false};
1895 }
1896
1897 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1898 const auto& comm = simulator_.vanguard().grid().comm();
1899 this->updateAndCommunicateGroupData(episodeIdx, iterationIdx);
1900
1901 // network related
1902 bool more_network_update = false;
1903 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx) || mandatory_network_balance) {
1904 const auto local_network_imbalance = this->updateNetworkPressures(episodeIdx);
1905 const Scalar network_imbalance = comm.max(local_network_imbalance);
1906 const auto& balance = this->schedule()[episodeIdx].network_balance();
1907 constexpr Scalar relaxation_factor = 10.0;
1908 const Scalar tolerance = relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance() : balance.pressure_tolerance();
1909 more_network_update = this->networkActive() && network_imbalance > tolerance;
1910 }
1911
1912 bool changed_well_group = false;
1913 // Check group individual constraints.
1914 const int nupcol = this->schedule()[episodeIdx].nupcol();
1915 // don't switch group control when iterationIdx > nupcol
1916 // to avoid oscilations between group controls
1917 if (iterationIdx <= nupcol) {
1918 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1919 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1920 }
1921 // Check wells' group constraints and communicate.
1922 bool changed_well_to_group = false;
1923 {
1924 // For MS Wells a linear solve is performed below and the matrix might be singular.
1925 // We need to communicate the exception thrown to the others and rethrow.
1927 for (const auto& well : well_container_) {
1929 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1930 if (changed_well) {
1931 changed_well_to_group = changed_well || changed_well_to_group;
1932 }
1933 }
1934 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1935 simulator_.gridView().comm());
1936 }
1937
1938 changed_well_to_group = comm.sum(static_cast<int>(changed_well_to_group));
1939 if (changed_well_to_group) {
1940 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1941 changed_well_group = true;
1942 }
1943
1944 // Check individual well constraints and communicate.
1945 bool changed_well_individual = false;
1946 {
1947 // For MS Wells a linear solve is performed below and the matrix might be singular.
1948 // We need to communicate the exception thrown to the others and rethrow.
1950 for (const auto& well : well_container_) {
1952 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1953 if (changed_well) {
1954 changed_well_individual = changed_well || changed_well_individual;
1955 }
1956 }
1957 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1958 simulator_.gridView().comm());
1959 }
1960
1961 changed_well_individual = comm.sum(static_cast<int>(changed_well_individual));
1962 if (changed_well_individual) {
1963 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1964 changed_well_group = true;
1965 }
1966
1967 // update wsolvent fraction for REIN wells
1968 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1969 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1970
1971 return { changed_well_group, more_network_update };
1972 }
1973
1974 template<typename TypeTag>
1975 void
1977 updateWellControlsDomain(DeferredLogger& deferred_logger, const Domain& domain)
1978 {
1979 if ( !this->wellsActive() ) return ;
1980
1981 // TODO: decide on and implement an approach to handling of
1982 // group controls, network and similar for domain solves.
1983
1984 // Check only individual well constraints and communicate.
1985 for (const auto& well : well_container_) {
1986 if (well_domain_.at(well->name()) == domain.index) {
1988 well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1989 }
1990 }
1991 }
1992
1993
1994
1995
1996
1997 template <typename TypeTag>
1998 void
2001 {
2002 this->wbpCalcMap_.clear();
2003 this->wbpCalcMap_.resize(this->wells_ecl_.size());
2004
2005 this->registerOpenWellsForWBPCalculation();
2006
2007 auto wellID = std::size_t{0};
2008 for (const auto& well : this->wells_ecl_) {
2009 this->wbpCalcMap_[wellID].wbpCalcIdx_ = this->wbpCalculationService_
2010 .createCalculator(well,
2011 this->local_parallel_well_info_[wellID],
2012 this->conn_idx_map_[wellID].local(),
2013 this->makeWellSourceEvaluatorFactory(wellID));
2014
2015 ++wellID;
2016 }
2017
2018 this->wbpCalculationService_.defineCommunication();
2019 }
2020
2021
2022
2023
2024
2025 template <typename TypeTag>
2026 data::WellBlockAveragePressures
2029 {
2030 auto wbpResult = data::WellBlockAveragePressures{};
2031
2032 using Calculated = PAvgCalculator::Result::WBPMode;
2033 using Output = data::WellBlockAvgPress::Quantity;
2034
2035 this->wbpCalculationService_.collectDynamicValues();
2036
2037 const auto numWells = this->wells_ecl_.size();
2038 for (auto wellID = 0*numWells; wellID < numWells; ++wellID) {
2039 const auto calcIdx = this->wbpCalcMap_[wellID].wbpCalcIdx_;
2040 const auto& well = this->wells_ecl_[wellID];
2041
2042 if (! well.hasRefDepth()) {
2043 // Can't perform depth correction without at least a
2044 // fall-back datum depth.
2045 continue;
2046 }
2047
2048 this->wbpCalculationService_
2049 .inferBlockAveragePressures(calcIdx, well.pavg(),
2050 this->gravity_,
2051 well.getWPaveRefDepth());
2052
2053 const auto& result = this->wbpCalculationService_
2054 .averagePressures(calcIdx);
2055
2056 auto& reported = wbpResult.values[well.name()];
2057
2058 reported[Output::WBP] = result.value(Calculated::WBP);
2059 reported[Output::WBP4] = result.value(Calculated::WBP4);
2060 reported[Output::WBP5] = result.value(Calculated::WBP5);
2061 reported[Output::WBP9] = result.value(Calculated::WBP9);
2062 }
2063
2064 return wbpResult;
2065 }
2066
2067
2068
2069
2070
2071 template <typename TypeTag>
2074 makeWellSourceEvaluatorFactory(const std::vector<Well>::size_type wellIdx) const
2075 {
2076 using Span = PAvgDynamicSourceData::SourceDataSpan<Scalar>;
2077 using Item = typename Span::Item;
2078
2079 return [wellIdx, this]() -> ParallelWBPCalculation::Evaluator
2080 {
2081 if (! this->wbpCalcMap_[wellIdx].openWellIdx_.has_value()) {
2082 // Well is stopped/shut. Return evaluator for stopped wells.
2083 return []([[maybe_unused]] const int connIdx, Span sourceTerm)
2084 {
2085 // Well/connection is stopped/shut. Set all items to
2086 // zero.
2087
2088 sourceTerm
2089 .set(Item::Pressure , 0.0)
2090 .set(Item::PoreVol , 0.0)
2091 .set(Item::MixtureDensity, 0.0);
2092 };
2093 }
2094
2095 // Well is open. Return an evaluator for open wells/open connections.
2096 return [this, wellPtr = this->well_container_[*this->wbpCalcMap_[wellIdx].openWellIdx_].get()]
2097 (const int connIdx, Span sourceTerm)
2098 {
2099 // Note: The only item which actually matters for the WBP
2100 // calculation at the well reservoir connection level is the
2101 // mixture density. Set other items to zero.
2102
2103 const auto& connIdxMap =
2104 this->conn_idx_map_[wellPtr->indexOfWell()];
2105
2106 const auto rho = wellPtr->
2107 connectionDensity(connIdxMap.global(connIdx),
2108 connIdxMap.open(connIdx));
2109
2110 sourceTerm
2111 .set(Item::Pressure , 0.0)
2112 .set(Item::PoreVol , 0.0)
2113 .set(Item::MixtureDensity, rho);
2114 };
2115 };
2116 }
2117
2118
2119
2120
2121
2122 template <typename TypeTag>
2123 void
2126 {
2127 assert (this->wbpCalcMap_.size() == this->wells_ecl_.size());
2128
2129 for (auto& wbpCalc : this->wbpCalcMap_) {
2130 wbpCalc.openWellIdx_.reset();
2131 }
2132
2133 auto openWellIdx = typename std::vector<WellInterfacePtr>::size_type{0};
2134 for (const auto* openWell : this->well_container_generic_) {
2135 this->wbpCalcMap_[openWell->indexOfWell()].openWellIdx_ = openWellIdx++;
2136 }
2137 }
2138
2139
2140
2141
2142
2143 template<typename TypeTag>
2144 void
2146 updateAndCommunicate(const int reportStepIdx,
2147 const int iterationIdx,
2148 DeferredLogger& deferred_logger)
2149 {
2150 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx);
2151
2152 // updateWellStateWithTarget might throw for multisegment wells hence we
2153 // have a parallel try catch here to thrown on all processes.
2155 // if a well or group change control it affects all wells that are under the same group
2156 for (const auto& well : well_container_) {
2157 well->updateWellStateWithTarget(simulator_, this->groupState(),
2158 this->wellState(), deferred_logger);
2159 }
2160 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAndCommunicate failed: ",
2161 simulator_.gridView().comm())
2162 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx);
2163 }
2164
2165 template<typename TypeTag>
2166 bool
2168 updateGroupControls(const Group& group,
2169 DeferredLogger& deferred_logger,
2170 const int reportStepIdx,
2171 const int iterationIdx)
2172 {
2173 bool changed = false;
2174 bool changed_hc = this->checkGroupHigherConstraints( group, deferred_logger, reportStepIdx);
2175 if (changed_hc) {
2176 changed = true;
2177 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2178 }
2179
2180 bool changed_individual =
2182 updateGroupIndividualControl(group,
2183 reportStepIdx,
2184 this->switched_inj_groups_,
2185 this->switched_prod_groups_,
2186 this->closed_offending_wells_,
2187 this->groupState(),
2188 this->wellState(),
2189 deferred_logger);
2190
2191 if (changed_individual) {
2192 changed = true;
2193 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2194 }
2195 // call recursively down the group hierarchy
2196 for (const std::string& groupName : group.groups()) {
2197 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
2198 changed = changed || changed_this;
2199 }
2200 return changed;
2201 }
2202
2203 template<typename TypeTag>
2204 void
2206 updateWellTestState(const double& simulationTime, WellTestState& wellTestState) const
2207 {
2208 DeferredLogger local_deferredLogger;
2209 for (const auto& well : well_container_) {
2210 const auto& wname = well->name();
2211 const auto wasClosed = wellTestState.well_is_closed(wname);
2212 well->checkWellOperability(simulator_, this->wellState(), local_deferredLogger);
2213 well->updateWellTestState(this->wellState().well(wname), simulationTime, /*writeMessageToOPMLog=*/ true, wellTestState, local_deferredLogger);
2214
2215 if (!wasClosed && wellTestState.well_is_closed(wname)) {
2216 this->closed_this_step_.insert(wname);
2217 }
2218 }
2219
2220 const Opm::Parallel::Communication comm = grid().comm();
2221 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
2222
2223 for (const auto& [group_name, to] : this->closed_offending_wells_) {
2224 if (!this->wasDynamicallyShutThisTimeStep(to.second)) {
2225 wellTestState.close_well(to.second, WellTestConfig::Reason::GROUP, simulationTime);
2226 this->updateClosedWellsThisStep(to.second);
2227 const std::string msg =
2228 fmt::format("Procedure on exceeding {} limit is WELL for group {}. Well {} is {}.",
2229 to.first,
2230 group_name,
2231 to.second,
2232 "shut");
2233 global_deferredLogger.info(msg);
2234 }
2235 }
2236
2237 if (this->terminal_output_) {
2238 global_deferredLogger.logMessages();
2239 }
2240 }
2241
2242
2243 template<typename TypeTag>
2244 void
2246 const WellState<Scalar>& well_state_copy,
2247 std::string& exc_msg,
2248 ExceptionType::ExcEnum& exc_type,
2249 DeferredLogger& deferred_logger)
2250 {
2251 const int np = this->numPhases();
2252 std::vector<Scalar> potentials;
2253 const auto& well = well_container_[widx];
2254 std::string cur_exc_msg;
2255 auto cur_exc_type = ExceptionType::NONE;
2256 try {
2257 well->computeWellPotentials(simulator_, well_state_copy, potentials, deferred_logger);
2258 }
2259 // catch all possible exception and store type and message.
2260 OPM_PARALLEL_CATCH_CLAUSE(cur_exc_type, cur_exc_msg);
2261 if (cur_exc_type != ExceptionType::NONE) {
2262 exc_msg += fmt::format("\nFor well {}: {}", well->name(), cur_exc_msg);
2263 }
2264 exc_type = std::max(exc_type, cur_exc_type);
2265 // Store it in the well state
2266 // potentials is resized and set to zero in the beginning of well->ComputeWellPotentials
2267 // and updated only if sucessfull. i.e. the potentials are zero for exceptions
2268 auto& ws = this->wellState().well(well->indexOfWell());
2269 for (int p = 0; p < np; ++p) {
2270 // make sure the potentials are positive
2271 ws.well_potentials[p] = std::max(0.0, potentials[p]);
2272 }
2273 }
2274
2275
2276
2277 template <typename TypeTag>
2278 void
2281 {
2282 for (const auto& wellPtr : this->well_container_) {
2283 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2284 }
2285 }
2286
2287
2288
2289
2290
2291 template <typename TypeTag>
2292 void
2294 calculateProductivityIndexValuesShutWells(const int reportStepIdx,
2295 DeferredLogger& deferred_logger)
2296 {
2297 // For the purpose of computing PI/II values, it is sufficient to
2298 // construct StandardWell instances only. We don't need to form
2299 // well objects that honour the 'isMultisegment()' flag of the
2300 // corresponding "this->wells_ecl_[shutWell]".
2301
2302 for (const auto& shutWell : this->local_shut_wells_) {
2303 if (!this->wells_ecl_[shutWell].hasConnections()) {
2304 // No connections in this well. Nothing to do.
2305 continue;
2306 }
2307
2308 auto wellPtr = this->template createTypedWellPointer
2309 <StandardWell<TypeTag>>(shutWell, reportStepIdx);
2310
2311 wellPtr->init(&this->phase_usage_, this->depth_, this->gravity_,
2312 this->local_num_cells_, this->B_avg_, true);
2313
2314 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2315 }
2316 }
2317
2318
2319
2320
2321
2322 template <typename TypeTag>
2323 void
2326 DeferredLogger& deferred_logger)
2327 {
2328 wellPtr->updateProductivityIndex(this->simulator_,
2329 this->prod_index_calc_[wellPtr->indexOfWell()],
2330 this->wellState(),
2331 deferred_logger);
2332 }
2333
2334
2335
2336 template<typename TypeTag>
2337 void
2339 prepareTimeStep(DeferredLogger& deferred_logger)
2340 {
2341 // Check if there is a network with active prediction wells at this time step.
2342 const auto episodeIdx = simulator_.episodeIndex();
2343 this->updateNetworkActiveState(episodeIdx);
2344
2345 // Rebalance the network initially if any wells in the network have status changes
2346 // (Need to check this before clearing events)
2347 const bool do_prestep_network_rebalance = this->needPreStepNetworkRebalance(episodeIdx);
2348
2349 for (const auto& well : well_container_) {
2350 auto& events = this->wellState().well(well->indexOfWell()).events;
2351 if (events.hasEvent(WellState<Scalar>::event_mask)) {
2352 well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), deferred_logger);
2353 const auto& summary_state = simulator_.vanguard().summaryState();
2354 well->updatePrimaryVariables(summary_state, this->wellState(), deferred_logger);
2355 well->initPrimaryVariablesEvaluation();
2356 // There is no new well control change input within a report step,
2357 // so next time step, the well does not consider to have effective events anymore.
2358 events.clearEvent(WellState<Scalar>::event_mask);
2359 }
2360 // these events only work for the first time step within the report step
2361 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2362 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2363 }
2364 // solve the well equation initially to improve the initial solution of the well model
2365 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2366 try {
2367 well->solveWellEquation(simulator_, this->wellState(), this->groupState(), deferred_logger);
2368 } catch (const std::exception& e) {
2369 const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the previous rates";
2370 deferred_logger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
2371 }
2372 }
2373 // If we're using local well solves that include control switches, they also update
2374 // operability, so reset before main iterations begin
2375 well->resetWellOperability();
2376 }
2377 updatePrimaryVariables(deferred_logger);
2378
2379 // Actually do the pre-step network rebalance, using the updated well states and initial solutions
2380 if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger);
2381 }
2382
2383 template<typename TypeTag>
2384 void
2387 {
2388 std::vector< Scalar > B_avg(numComponents(), Scalar() );
2389 const auto& grid = simulator_.vanguard().grid();
2390 const auto& gridView = grid.leafGridView();
2391 ElementContext elemCtx(simulator_);
2392
2394 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2395 elemCtx.updatePrimaryStencil(elem);
2396 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
2397
2398 const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
2399 const auto& fs = intQuants.fluidState();
2400
2401 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2402 {
2403 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2404 continue;
2405 }
2406
2407 const unsigned compIdx = Indices::canonicalToActiveComponentIndex(FluidSystem::solventComponentIndex(phaseIdx));
2408 auto& B = B_avg[ compIdx ];
2409
2410 B += 1 / fs.invB(phaseIdx).value();
2411 }
2412 if constexpr (has_solvent_) {
2413 auto& B = B_avg[solventSaturationIdx];
2414 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2415 }
2416 }
2417 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAverageFormationFactor() failed: ", grid.comm())
2418
2419 // compute global average
2420 grid.comm().sum(B_avg.data(), B_avg.size());
2421 for (auto& bval : B_avg)
2422 {
2423 bval /= global_num_cells_;
2424 }
2425 B_avg_ = B_avg;
2426 }
2427
2428
2429
2430
2431
2432 template<typename TypeTag>
2433 void
2436 {
2437 for (const auto& well : well_container_) {
2438 const auto& summary_state = simulator_.vanguard().summaryState();
2439 well->updatePrimaryVariables(summary_state, this->wellState(), deferred_logger);
2440 }
2441 }
2442
2443 template<typename TypeTag>
2444 void
2446 {
2447 const auto& grid = simulator_.vanguard().grid();
2448 const auto& eclProblem = simulator_.problem();
2449 const unsigned numCells = grid.size(/*codim=*/0);
2450
2451 this->pvt_region_idx_.resize(numCells);
2452 for (unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2453 this->pvt_region_idx_[cellIdx] =
2454 eclProblem.pvtRegionIndex(cellIdx);
2455 }
2456 }
2457
2458 // The number of components in the model.
2459 template<typename TypeTag>
2460 int
2462 {
2463 // The numComponents here does not reflect the actual number of the components in the system.
2464 // It more or less reflects the number of mass conservation equations for the well equations.
2465 // For example, in the current formulation, we do not have the polymer conservation equation
2466 // in the well equations. As a result, for an oil-water-polymer system, this function will return 2.
2467 // In some way, it makes this function appear to be confusing from its name, and we need
2468 // to revisit/revise this function again when extending the variants of system that flow can simulate.
2469 int numComp = this->numPhases() < 3 ? this->numPhases() : FluidSystem::numComponents;
2470 if constexpr (has_solvent_) {
2471 numComp++;
2472 }
2473 return numComp;
2474 }
2475
2476 template<typename TypeTag>
2477 void
2479 {
2480 const auto& eclProblem = simulator_.problem();
2481 depth_.resize(local_num_cells_);
2482 for (unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2483 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2484 }
2485 }
2486
2487 template<typename TypeTag>
2490 getWell(const std::string& well_name) const
2491 {
2492 // finding the iterator of the well in wells_ecl
2493 auto well = std::find_if(well_container_.begin(),
2494 well_container_.end(),
2495 [&well_name](const WellInterfacePtr& elem)->bool {
2496 return elem->name() == well_name;
2497 });
2498
2499 assert(well != well_container_.end());
2500
2501 return *well;
2502 }
2503
2504 template<typename TypeTag>
2505 bool
2507 hasWell(const std::string& well_name) const
2508 {
2509 return std::any_of(well_container_.begin(), well_container_.end(),
2510 [&well_name](const WellInterfacePtr& elem) -> bool
2511 {
2512 return elem->name() == well_name;
2513 });
2514 }
2515
2516
2517
2518
2519 template <typename TypeTag>
2520 int
2522 reportStepIndex() const
2523 {
2524 return std::max(this->simulator_.episodeIndex(), 0);
2525 }
2526
2527
2528
2529
2530
2531 template<typename TypeTag>
2532 void
2534 calcRates(const int fipnum,
2535 const int pvtreg,
2536 const std::vector<Scalar>& production_rates,
2537 std::vector<Scalar>& resv_coeff)
2538 {
2539 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2540 }
2541
2542 template<typename TypeTag>
2543 void
2545 calcInjRates(const int fipnum,
2546 const int pvtreg,
2547 std::vector<Scalar>& resv_coeff)
2548 {
2549 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2550 }
2551
2552
2553 template <typename TypeTag>
2554 void
2557 {
2558 if (!has_energy_)
2559 return;
2560
2561 int np = this->numPhases();
2562 Scalar cellInternalEnergy;
2563 Scalar cellBinv;
2564 Scalar cellDensity;
2565 Scalar perfPhaseRate;
2566 const int nw = this->numLocalWells();
2567 for (auto wellID = 0*nw; wellID < nw; ++wellID) {
2568 const Well& well = this->wells_ecl_[wellID];
2569 if (well.isInjector())
2570 continue;
2571
2572 std::array<Scalar,2> weighted{0.0,0.0};
2573 auto& [weighted_temperature, total_weight] = weighted;
2574
2575 auto& well_info = this->local_parallel_well_info_[wellID].get();
2576 auto& ws = this->wellState().well(wellID);
2577 auto& perf_data = ws.perf_data;
2578 auto& perf_phase_rate = perf_data.phase_rates;
2579
2580 using int_type = decltype(this->well_perf_data_[wellID].size());
2581 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2582 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2583 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, /*timeIdx=*/0);
2584 const auto& fs = intQuants.fluidState();
2585
2586 // we on only have one temperature pr cell any phaseIdx will do
2587 Scalar cellTemperatures = fs.temperature(/*phaseIdx*/0).value();
2588
2589 Scalar weight_factor = 0.0;
2590 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2591 {
2592 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2593 continue;
2594 }
2595 cellInternalEnergy = fs.enthalpy(phaseIdx).value() - fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2596 cellBinv = fs.invB(phaseIdx).value();
2597 cellDensity = fs.density(phaseIdx).value();
2598 perfPhaseRate = perf_phase_rate[ perf*np + phaseIdx ];
2599 weight_factor += cellDensity * perfPhaseRate/cellBinv * cellInternalEnergy/cellTemperatures;
2600 }
2601 total_weight += weight_factor;
2602 weighted_temperature += weight_factor * cellTemperatures;
2603 }
2604 well_info.communication().sum(weighted.data(), 2);
2605 this->wellState().well(wellID).temperature = weighted_temperature/total_weight;
2606 }
2607 }
2608
2609
2610 template <typename TypeTag>
2611 void
2613 logPrimaryVars() const
2614 {
2615 std::ostringstream os;
2616 for (const auto& w : well_container_) {
2617 os << w->name() << ":";
2618 auto pv = w->getPrimaryVars();
2619 for (const Scalar v : pv) {
2620 os << ' ' << v;
2621 }
2622 os << '\n';
2623 }
2624 OpmLog::debug(os.str());
2625 }
2626
2627
2628
2629 template <typename TypeTag>
2630 std::vector<typename BlackoilWellModel<TypeTag>::Scalar>
2632 getPrimaryVarsDomain(const Domain& domain) const
2633 {
2634 std::vector<Scalar> ret;
2635 for (const auto& well : well_container_) {
2636 if (well_domain_.at(well->name()) == domain.index) {
2637 const auto& pv = well->getPrimaryVars();
2638 ret.insert(ret.end(), pv.begin(), pv.end());
2639 }
2640 }
2641 return ret;
2642 }
2643
2644
2645
2646 template <typename TypeTag>
2647 void
2649 setPrimaryVarsDomain(const Domain& domain, const std::vector<Scalar>& vars)
2650 {
2651 std::size_t offset = 0;
2652 for (auto& well : well_container_) {
2653 if (well_domain_.at(well->name()) == domain.index) {
2654 int num_pri_vars = well->setPrimaryVars(vars.begin() + offset);
2655 offset += num_pri_vars;
2656 }
2657 }
2658 assert(offset == vars.size());
2659 }
2660
2661
2662
2663 template <typename TypeTag>
2664 void
2666 setupDomains(const std::vector<Domain>& domains)
2667 {
2669 // TODO: This loop nest may be slow for very large numbers of
2670 // domains and wells, but that has not been observed on tests so
2671 // far. Using the partition vector instead would be faster if we
2672 // need to change.
2673 for (const auto& wellPtr : this->well_container_) {
2674 const int first_well_cell = wellPtr->cells().front();
2675 for (const auto& domain : domains) {
2676 auto cell_present = [&domain](const auto cell)
2677 {
2678 return std::binary_search(domain.cells.begin(),
2679 domain.cells.end(), cell);
2680 };
2681
2682 if (cell_present(first_well_cell)) {
2683 // Assuming that if the first well cell is found in a domain,
2684 // then all of that well's cells are in that same domain.
2685 well_domain_[wellPtr->name()] = domain.index;
2686
2687 // Verify that all of that well's cells are in that same domain.
2688 for (int well_cell : wellPtr->cells()) {
2689 if (! cell_present(well_cell)) {
2690 OPM_THROW(std::runtime_error,
2691 fmt::format("Well '{}' found on multiple domains.",
2692 wellPtr->name()));
2693 }
2694 }
2695 }
2696 }
2697 }
2698 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::setupDomains(): well found on multiple domains.",
2699 simulator_.gridView().comm());
2700
2701 // Write well/domain info to the DBG file.
2702 const Opm::Parallel::Communication& comm = grid().comm();
2703 const int rank = comm.rank();
2704 DeferredLogger local_log;
2705 if (!well_domain_.empty()) {
2706 std::ostringstream os;
2707 os << "Well name Rank Domain\n";
2708 for (const auto& [wname, domain] : well_domain_) {
2709 os << wname << std::setw(19 - wname.size()) << rank << std::setw(12) << domain << '\n';
2710 }
2711 local_log.debug(os.str());
2712 }
2713 auto global_log = gatherDeferredLogger(local_log, comm);
2714 if (this->terminal_output_) {
2715 global_log.logMessages();
2716 }
2717 }
2718
2719} // namespace Opm
#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:182
#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:172
#define OPM_PARALLEL_CATCH_CLAUSE(obptc_exc_type, obptc_exc_msg)
Inserts catch classes for the parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:146
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:138
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:41
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:101
ParallelWBPCalculation::EvaluatorFactory makeWellSourceEvaluatorFactory(const std::vector< Well >::size_type wellIdx) const
Definition: BlackoilWellModel_impl.hpp:2074
void initializeGroupStructure(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:345
void apply(BVector &r) const
Definition: BlackoilWellModel_impl.hpp:1543
void prepareTimeStep(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2339
bool updateGroupControls(const Group &group, DeferredLogger &deferred_logger, const int reportStepIdx, const int iterationIdx)
Definition: BlackoilWellModel_impl.hpp:2168
void calcRates(const int fipnum, const int pvtreg, const std::vector< Scalar > &production_rates, std::vector< Scalar > &resv_coeff) override
Definition: BlackoilWellModel_impl.hpp:2534
WellInterfacePtr createWellPointer(const int wellID, const int report_step) const
Definition: BlackoilWellModel_impl.hpp:982
WellInterfacePtr getWell(const std::string &well_name) const
Definition: BlackoilWellModel_impl.hpp:2490
void init()
Definition: BlackoilWellModel_impl.hpp:145
typename BlackoilWellModelGeneric< Scalar >::GLiftProdWells GLiftProdWells
Definition: BlackoilWellModel.hpp:118
void updateAndCommunicate(const int reportStepIdx, const int iterationIdx, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2146
typename GasLiftSingleWellGeneric::GLiftSyncGroups GLiftSyncGroups
Definition: BlackoilWellModel.hpp:122
void doPreStepNetworkRebalance(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1060
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: BlackoilWellModel.hpp:112
std::vector< Scalar > getPrimaryVarsDomain(const Domain &domain) const
Definition: BlackoilWellModel_impl.hpp:2632
void initWellContainer(const int reportStepIdx) override
Definition: BlackoilWellModel_impl.hpp:164
void addReservoirSourceTerms(GlobalEqVector &residual, std::vector< typename SparseMatrixAdapter::MatrixBlock * > &diagMatAddress) const
Definition: BlackoilWellModel_impl.hpp:1650
void beginReportStep(const int time_step)
Definition: BlackoilWellModel_impl.hpp:248
void assembleWellEqDomain(const double dt, const Domain &domain, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1500
void applyScaleAdd(const Scalar alpha, const BVector &x, BVector &Ax) const
Definition: BlackoilWellModel_impl.hpp:1604
void gasLiftOptimizationStage1SingleWell(WellInterface< TypeTag > *well, DeferredLogger &deferred_logger, GLiftProdWells &prod_wells, GLiftOptWells &glift_wells, GasLiftGroupInfo &group_info, GLiftWellStateMap &state_map, GLiftSyncGroups &groups_to_sync)
Definition: BlackoilWellModel_impl.hpp:1451
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: BlackoilWellModel.hpp:109
GetPropType< TypeTag, Properties::Grid > Grid
Definition: BlackoilWellModel.hpp:106
void updateWellControlsDomain(DeferredLogger &deferred_logger, const Domain &domain)
Definition: BlackoilWellModel_impl.hpp:1977
void assembleWellEq(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1489
void linearizeDomain(const Domain &domain, SparseMatrixAdapter &jacobian, GlobalEqVector &res)
Definition: BlackoilWellModel_impl.hpp:225
int reportStepIndex() const
Definition: BlackoilWellModel_impl.hpp:2522
void calculateProductivityIndexValues(DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:2280
void extractLegacyDepth_()
Definition: BlackoilWellModel_impl.hpp:2478
void extractLegacyCellPvtRegionIndex_()
Definition: BlackoilWellModel_impl.hpp:2445
void updateAverageFormationFactor()
Definition: BlackoilWellModel_impl.hpp:2386
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: BlackoilWellModel.hpp:111
void calcInjRates(const int fipnum, const int pvtreg, std::vector< Scalar > &resv_coeff) override
Definition: BlackoilWellModel_impl.hpp:2545
void initializeWellState(const int timeStepIdx)
Definition: BlackoilWellModel_impl.hpp:803
void assemble(const int iterationIdx, const double dt)
Definition: BlackoilWellModel_impl.hpp:1099
void computeWellTemperature()
Definition: BlackoilWellModel_impl.hpp:2556
void addWellPressureEquations(PressureMatrix &jacobian, const BVector &weights, const bool use_well_weights) const
Definition: BlackoilWellModel_impl.hpp:1634
void logPrimaryVars() const
Definition: BlackoilWellModel_impl.hpp:2613
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:654
bool updateWellControlsAndNetwork(const bool mandatory_network_balance, const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModel_impl.hpp:1159
GetPropType< TypeTag, Properties::SparseMatrixAdapter > SparseMatrixAdapter
Definition: BlackoilWellModel.hpp:115
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1624
WellInterfacePtr createWellForWellTest(const std::string &well_name, const int report_step, DeferredLogger &deferred_logger) const
Definition: BlackoilWellModel_impl.hpp:1033
typename BlackoilWellModelGeneric< Scalar >::GLiftOptWells GLiftOptWells
Definition: BlackoilWellModel.hpp:117
void computePotentials(const std::size_t widx, const WellState< Scalar > &well_state_copy, std::string &exc_msg, ExceptionType::ExcEnum &exc_type, DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:2245
std::pair< bool, bool > updateWellControls(const bool mandatory_network_balance, DeferredLogger &deferred_logger, const bool relax_network_tolerance=false)
Definition: BlackoilWellModel_impl.hpp:1889
void recoverWellSolutionAndUpdateWellStateDomain(const BVector &x, const Domain &domain)
Definition: BlackoilWellModel_impl.hpp:1722
void assembleDomain(const int iterationIdx, const double dt, const Domain &domain)
Definition: BlackoilWellModel_impl.hpp:1247
bool hasWell(const std::string &well_name) const
Definition: BlackoilWellModel_impl.hpp:2507
void assembleWellEqWithoutIteration(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1524
data::WellBlockAveragePressures computeWellBlockAveragePressures() const
Definition: BlackoilWellModel_impl.hpp:2028
typename GasLiftGroupInfo::GLiftEclWells GLiftEclWells
Definition: BlackoilWellModel.hpp:121
void initPrimaryVariablesEvaluation() const
Definition: BlackoilWellModel_impl.hpp:1747
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, 1, 1 > > PressureMatrix
Definition: BlackoilWellModel.hpp:342
void computeTotalRatesForDof(RateVector &rate, unsigned globalIdx) const
Definition: BlackoilWellModel_impl.hpp:766
void beginTimeStep()
Definition: BlackoilWellModel_impl.hpp:387
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: BlackoilWellModel.hpp:113
void calculateExplicitQuantities(DeferredLogger &deferred_logger) const
Definition: BlackoilWellModel_impl.hpp:1874
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:135
void updatePrimaryVariables(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2435
GetPropType< TypeTag, Properties::GlobalEqVector > GlobalEqVector
Definition: BlackoilWellModel.hpp:114
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:300
std::pair< bool, bool > updateWellControlsAndNetworkIteration(const bool mandatory_network_balance, const bool relax_network_tolerance, const double dt, DeferredLogger &local_deferredLogger)
Definition: BlackoilWellModel_impl.hpp:1195
void addNeighbors(std::vector< NeighborSet > &neighbors) const override
Definition: BlackoilWellModel_impl.hpp:180
BlackoilWellModel(Simulator &simulator)
Definition: BlackoilWellModel_impl.hpp:137
void wellTesting(const int timeStepIdx, const double simulationTime, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:587
ConvergenceReport getWellConvergence(const std::vector< Scalar > &B_avg, const bool checkWellGroupControls=false) const
Definition: BlackoilWellModel_impl.hpp:1821
void registerOpenWellsForWBPCalculation()
Definition: BlackoilWellModel_impl.hpp:2125
void linearize(SparseMatrixAdapter &jacobian, GlobalEqVector &res) override
Definition: BlackoilWellModel_impl.hpp:204
void initGliftEclWellMap(GLiftEclWells &ecl_well_map)
Definition: BlackoilWellModel_impl.hpp:1477
void gasLiftOptimizationStage1(DeferredLogger &deferred_logger, GLiftProdWells &prod_wells, GLiftOptWells &glift_wells, GasLiftGroupInfo &group_info, GLiftWellStateMap &state_map)
Definition: BlackoilWellModel_impl.hpp:1345
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:664
std::unique_ptr< WellType > createTypedWellPointer(const int wellID, const int time_step) const
Definition: BlackoilWellModel_impl.hpp:1002
void setPrimaryVarsDomain(const Domain &domain, const std::vector< Scalar > &vars)
Definition: BlackoilWellModel_impl.hpp:2649
std::shared_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:240
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:841
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:136
bool maybeDoGasLiftOptimize(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1287
void updateWellTestState(const double &simulationTime, WellTestState &wellTestState) const
upate the wellTestState related to economic limits
Definition: BlackoilWellModel_impl.hpp:2206
ConvergenceReport getDomainWellConvergence(const Domain &domain, const std::vector< Scalar > &B_avg, DeferredLogger &local_deferredLogger) const
Definition: BlackoilWellModel_impl.hpp:1775
void initializeWBPCalculationService()
Definition: BlackoilWellModel_impl.hpp:2000
void initPrimaryVariablesEvaluationDomain(const Domain &domain) const
Definition: BlackoilWellModel_impl.hpp:1758
void recoverWellSolutionAndUpdateWellState(const BVector &x)
Definition: BlackoilWellModel_impl.hpp:1703
void setupDomains(const std::vector< Domain > &domains)
Definition: BlackoilWellModel_impl.hpp:2666
void addWellPressureEquationsStruct(PressureMatrix &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1680
void calculateProductivityIndexValuesShutWells(const int reportStepIdx, DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:2294
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:637
void prepareWellsBeforeAssembling(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1513
typename BlackoilWellModelGeneric< Scalar >::GLiftWellStateMap GLiftWellStateMap
Definition: BlackoilWellModel.hpp:120
int numComponents() const
Definition: BlackoilWellModel_impl.hpp:2461
Definition: ConvergenceReport.hpp:38
void setWellFailed(const WellFailure &wf)
Definition: ConvergenceReport.hpp:130
void setWellGroupTargetsViolated(const bool wellGroupTargetsViolated)
Definition: ConvergenceReport.hpp:142
const std::vector< WellFailure > & wellFailures() const
Definition: ConvergenceReport.hpp:192
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)
Definition: GasLiftGroupInfo.hpp:45
void updateRate(int idx, double oil_rate, double gas_rate, double water_rate, double alq)
std::tuple< double, double, double, double > getRates(const int group_idx) const
bool hasWell(const std::string &well_name)
Class for serializing and broadcasting data using MPI.
Definition: MPISerializer.hpp:31
void broadcast(T &data, int root=0)
Serialize and broadcast on root process, de-serialize on others.
Definition: MPISerializer.hpp:46
ParallelPAvgDynamicSourceData::Evaluator Evaluator
Callback for evaluating WBPn source terms on the current MPI rank.
Definition: ParallelWBPCalculation.hpp:57
std::function< Evaluator()> EvaluatorFactory
Definition: ParallelWBPCalculation.hpp:62
Definition: StandardWell.hpp:59
virtual void init(const PhaseUsage *phase_usage_arg, const std::vector< double > &depth_arg, const double gravity_arg, const int num_cells, const std::vector< Scalar > &B_avg, const bool changed_to_open_this_step) override
Definition: StandardWell_impl.hpp:96
Definition: WellContributions.hpp:52
void setBlockSize(unsigned int dim, unsigned int dim_wells)
void alloc()
Allocate memory for the StandardWells.
void addNumBlocks(unsigned int numBlocks)
static void setCmodeGroup(const Group &group, const Schedule &schedule, const SummaryState &summaryState, const int reportStepIdx, GroupState< Scalar > &group_state)
static void setRegionAveragePressureCalculator(const Group &group, const Schedule &schedule, const int reportStepIdx, const FieldPropsManager &fp, const PhaseUsage &pu, std::map< std::string, std::unique_ptr< AverageRegionalPressureType > > &regionalAveragePressureCalculator)
static void updateGuideRates(const Group &group, const Schedule &schedule, const SummaryState &summary_state, const PhaseUsage &pu, int report_step, double sim_time, WellState< Scalar > &well_state, const GroupState< Scalar > &group_state, const Parallel::Communication &comm, GuideRate *guide_rate, std::vector< Scalar > &pot, DeferredLogger &deferred_logger)
static void updateGpMaintTargetForGroups(const Group &group, const Schedule &schedule, const RegionalValues &regional_values, const int reportStepIdx, const double dt, const WellState< Scalar > &well_state, GroupState< Scalar > &group_state)
static void accumulateGroupEfficiencyFactor(const Group &group, const Schedule &schedule, const int reportStepIdx, Scalar &factor)
int indexOfWell() const
Index of well in the wells struct and wellState.
const std::string & name() const
Well name.
Definition: WellInterface.hpp:75
virtual void updateProductivityIndex(const Simulator &simulator, const WellProdIndexCalculator &wellPICalc, WellState< Scalar > &well_state, DeferredLogger &deferred_logger) const =0
bool updateWellControl(const Simulator &simulator, const IndividualOrGroup iog, WellState< Scalar > &well_state, const GroupState< Scalar > &group_state, DeferredLogger &deferred_logger)
Definition: WellInterface_impl.hpp:194
Definition: WellState.hpp:62
ExcEnum
Definition: DeferredLogger.hpp:45
@ NONE
Definition: DeferredLogger.hpp:46
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: BlackoilPhases.hpp:27
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)
PhaseUsage phaseUsageFromDeck(const EclipseState &eclipseState)
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
Definition: SubDomain.hpp:62
int index
Definition: SubDomain.hpp:65