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,
1347 GLiftOptWells &glift_wells,
1348 GasLiftGroupInfo<Scalar>& group_info,
1349 GLiftWellStateMap& state_map)
1350 {
1351 auto comm = simulator_.vanguard().grid().comm();
1352 int num_procs = comm.size();
1353 // NOTE: Gas lift optimization stage 1 seems to be difficult
1354 // to do in parallel since the wells are optimized on different
1355 // processes and each process needs to know the current ALQ allocated
1356 // to each group it is a memeber of in order to check group limits and avoid
1357 // allocating more ALQ than necessary. (Surplus ALQ is removed in
1358 // stage 2). In stage1, as each well is adding ALQ, the current group ALQ needs
1359 // to be communicated to the other processes. But there is no common
1360 // synchronization point that all process will reach in the
1361 // runOptimizeLoop_() in GasLiftSingleWell.cpp.
1362 //
1363 // TODO: Maybe a better solution could be invented by distributing
1364 // wells according to certain parent groups. Then updated group rates
1365 // might not have to be communicated to the other processors.
1366
1367 // Currently, the best option seems to be to run this part sequentially
1368 // (not in parallel).
1369 //
1370 // TODO: The simplest approach seems to be if a) one process could take
1371 // ownership of all the wells (the union of all the wells in the
1372 // well_container_ of each process) then this process could do the
1373 // optimization, while the other processes could wait for it to
1374 // finish (e.g. comm.barrier()), or alternatively, b) if all
1375 // processes could take ownership of all the wells. Then there
1376 // would be no need for synchronization here..
1377 //
1378 for (int i = 0; i< num_procs; i++) {
1379 int num_rates_to_sync = 0; // communication variable
1380 GLiftSyncGroups groups_to_sync;
1381 if (comm.rank() == i) {
1382 // Run stage1: Optimize single wells while also checking group limits
1383 for (const auto& well : well_container_) {
1384 // NOTE: Only the wells in "group_info" needs to be optimized
1385 if (group_info.hasWell(well->name())) {
1386 gasLiftOptimizationStage1SingleWell(
1387 well.get(), deferred_logger, prod_wells, glift_wells,
1388 group_info, state_map, groups_to_sync
1389 );
1390 }
1391 }
1392 num_rates_to_sync = groups_to_sync.size();
1393 }
1394 num_rates_to_sync = comm.sum(num_rates_to_sync);
1395 if (num_rates_to_sync > 0) {
1396 std::vector<int> group_indexes;
1397 group_indexes.reserve(num_rates_to_sync);
1398 std::vector<Scalar> group_alq_rates;
1399 group_alq_rates.reserve(num_rates_to_sync);
1400 std::vector<Scalar> group_oil_rates;
1401 group_oil_rates.reserve(num_rates_to_sync);
1402 std::vector<Scalar> group_gas_rates;
1403 group_gas_rates.reserve(num_rates_to_sync);
1404 std::vector<Scalar> group_water_rates;
1405 group_water_rates.reserve(num_rates_to_sync);
1406 if (comm.rank() == i) {
1407 for (auto idx : groups_to_sync) {
1408 auto [oil_rate, gas_rate, water_rate, alq] = group_info.getRates(idx);
1409 group_indexes.push_back(idx);
1410 group_oil_rates.push_back(oil_rate);
1411 group_gas_rates.push_back(gas_rate);
1412 group_water_rates.push_back(water_rate);
1413 group_alq_rates.push_back(alq);
1414 }
1415 } else {
1416 group_indexes.resize(num_rates_to_sync);
1417 group_oil_rates.resize(num_rates_to_sync);
1418 group_gas_rates.resize(num_rates_to_sync);
1419 group_water_rates.resize(num_rates_to_sync);
1420 group_alq_rates.resize(num_rates_to_sync);
1421 }
1422#if HAVE_MPI
1423 Parallel::MpiSerializer ser(comm);
1424 ser.broadcast(i, group_indexes, group_oil_rates,
1425 group_gas_rates, group_water_rates, group_alq_rates);
1426#endif
1427 if (comm.rank() != i) {
1428 for (int j=0; j<num_rates_to_sync; j++) {
1429 group_info.updateRate(group_indexes[j],
1430 group_oil_rates[j], group_gas_rates[j], group_water_rates[j], group_alq_rates[j]);
1431 }
1432 }
1433 if (this->glift_debug) {
1434 int counter = 0;
1435 if (comm.rank() == i) {
1436 counter = this->wellState().gliftGetDebugCounter();
1437 }
1438 counter = comm.sum(counter);
1439 if (comm.rank() != i) {
1440 this->wellState().gliftSetDebugCounter(counter);
1441 }
1442 }
1443 }
1444 }
1445 }
1446
1447 // NOTE: this method cannot be const since it passes this->wellState()
1448 // (see below) to the GasLiftSingleWell constructor which accepts WellState
1449 // as a non-const reference..
1450 template<typename TypeTag>
1451 void
1454 DeferredLogger& deferred_logger,
1455 GLiftProdWells& prod_wells,
1456 GLiftOptWells& glift_wells,
1457 GasLiftGroupInfo<Scalar>& group_info,
1458 GLiftWellStateMap& state_map,
1459 GLiftSyncGroups& sync_groups)
1460 {
1461 const auto& summary_state = simulator_.vanguard().summaryState();
1462 std::unique_ptr<GasLiftSingleWell> glift
1463 = std::make_unique<GasLiftSingleWell>(
1464 *well, simulator_, summary_state,
1465 deferred_logger, this->wellState(), this->groupState(),
1466 group_info, sync_groups, this->comm_, this->glift_debug);
1467 auto state = glift->runOptimize(
1468 simulator_.model().newtonMethod().numIterations());
1469 if (state) {
1470 state_map.insert({well->name(), std::move(state)});
1471 glift_wells.insert({well->name(), std::move(glift)});
1472 return;
1473 }
1474 prod_wells.insert({well->name(), well});
1475 }
1476
1477
1478 template<typename TypeTag>
1479 void
1482 {
1483 for ( const auto& well: well_container_ ) {
1484 ecl_well_map.try_emplace(
1485 well->name(), &(well->wellEcl()), well->indexOfWell());
1486 }
1487 }
1488
1489
1490 template<typename TypeTag>
1491 void
1493 assembleWellEq(const double dt, DeferredLogger& deferred_logger)
1494 {
1495 for (auto& well : well_container_) {
1496 well->assembleWellEq(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1497 }
1498 }
1499
1500
1501 template<typename TypeTag>
1502 void
1504 assembleWellEqDomain(const double dt, const Domain& domain, DeferredLogger& deferred_logger)
1505 {
1506 for (auto& well : well_container_) {
1507 if (well_domain_.at(well->name()) == domain.index) {
1508 well->assembleWellEq(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1509 }
1510 }
1511 }
1512
1513
1514 template<typename TypeTag>
1515 void
1517 prepareWellsBeforeAssembling(const double dt, DeferredLogger& deferred_logger)
1518 {
1519 for (auto& well : well_container_) {
1520 well->prepareWellBeforeAssembling(simulator_, dt, this->wellState(), this->groupState(), deferred_logger);
1521 }
1522 }
1523
1524
1525 template<typename TypeTag>
1526 void
1528 assembleWellEqWithoutIteration(const double dt, DeferredLogger& deferred_logger)
1529 {
1530 // We make sure that all processes throw in case there is an exception
1531 // on one of them (WetGasPvt::saturationPressure might throw if not converged)
1533
1534 for (auto& well: well_container_) {
1535 well->assembleWellEqWithoutIteration(simulator_, dt, this->wellState(), this->groupState(),
1536 deferred_logger);
1537 }
1538 OPM_END_PARALLEL_TRY_CATCH_LOG(deferred_logger, "BlackoilWellModel::assembleWellEqWithoutIteration failed: ",
1539 this->terminal_output_, grid().comm());
1540
1541 }
1542
1543
1544 template<typename TypeTag>
1545 void
1547 apply(BVector& r) const
1548 {
1549 for (auto& well : well_container_) {
1550 well->apply(r);
1551 }
1552 }
1553
1554
1555 // Ax = A x - C D^-1 B x
1556 template<typename TypeTag>
1557 void
1559 apply(const BVector& x, BVector& Ax) const
1560 {
1561 for (auto& well : well_container_) {
1562 well->apply(x, Ax);
1563 }
1564 }
1565
1566#if COMPILE_BDA_BRIDGE
1567 template<typename TypeTag>
1568 void
1570 getWellContributions(WellContributions& wellContribs) const
1571 {
1572 // prepare for StandardWells
1574
1575 for(unsigned int i = 0; i < well_container_.size(); i++){
1576 auto& well = well_container_[i];
1577 std::shared_ptr<StandardWell<TypeTag> > derived = std::dynamic_pointer_cast<StandardWell<TypeTag> >(well);
1578 if (derived) {
1579 wellContribs.addNumBlocks(derived->linSys().getNumBlocks());
1580 }
1581 }
1582
1583 // allocate memory for data from StandardWells
1584 wellContribs.alloc();
1585
1586 for(unsigned int i = 0; i < well_container_.size(); i++){
1587 auto& well = well_container_[i];
1588 // maybe WellInterface could implement addWellContribution()
1589 auto derived_std = std::dynamic_pointer_cast<StandardWell<TypeTag>>(well);
1590 if (derived_std) {
1591 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1592 } else {
1593 auto derived_ms = std::dynamic_pointer_cast<MultisegmentWell<TypeTag> >(well);
1594 if (derived_ms) {
1595 derived_ms->linSys().extract(wellContribs);
1596 } else {
1597 OpmLog::warning("Warning unknown type of well");
1598 }
1599 }
1600 }
1601 }
1602#endif
1603
1604 // Ax = Ax - alpha * C D^-1 B x
1605 template<typename TypeTag>
1606 void
1608 applyScaleAdd(const Scalar alpha, const BVector& x, BVector& Ax) const
1609 {
1610 if (this->well_container_.empty()) {
1611 return;
1612 }
1613
1614 if( scaleAddRes_.size() != Ax.size() ) {
1615 scaleAddRes_.resize( Ax.size() );
1616 }
1617
1618 scaleAddRes_ = 0.0;
1619 // scaleAddRes_ = - C D^-1 B x
1620 apply( x, scaleAddRes_ );
1621 // Ax = Ax + alpha * scaleAddRes_
1622 Ax.axpy( alpha, scaleAddRes_ );
1623 }
1624
1625 template<typename TypeTag>
1626 void
1629 {
1630 for ( const auto& well: well_container_ ) {
1631 well->addWellContributions(jacobian);
1632 }
1633 }
1634
1635 template<typename TypeTag>
1636 void
1638 addWellPressureEquations(PressureMatrix& jacobian, const BVector& weights,const bool use_well_weights) const
1639 {
1640 int nw = this->numLocalWellsEnd();
1641 int rdofs = local_num_cells_;
1642 for ( int i = 0; i < nw; i++ ){
1643 int wdof = rdofs + i;
1644 jacobian[wdof][wdof] = 1.0;// better scaling ?
1645 }
1646
1647 for ( const auto& well : well_container_ ) {
1648 well->addWellPressureEquations(jacobian, weights, pressureVarIndex, use_well_weights, this->wellState());
1649 }
1650 }
1651
1652 template <typename TypeTag>
1655 std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress) const
1656 {
1657 // NB this loop may write multiple times to the same element
1658 // if a cell is perforated by more than one well, so it should
1659 // not be OpenMP-parallelized.
1660 for (const auto& well : well_container_) {
1661 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1662 continue;
1663 }
1664 const auto& cells = well->cells();
1665 const auto& rates = well->connectionRates();
1666 for (unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1667 unsigned cellIdx = cells[perfIdx];
1668 auto rate = rates[perfIdx];
1669 rate *= -1.0;
1670 VectorBlockType res(0.0);
1671 using MatrixBlockType = typename SparseMatrixAdapter::MatrixBlock;
1672 MatrixBlockType bMat(0.0);
1673 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1674 residual[cellIdx] += res;
1675 *diagMatAddress[cellIdx] += bMat;
1676 }
1677 }
1678 }
1679
1680
1681 template<typename TypeTag>
1682 void
1685 {
1686 int nw = this->numLocalWellsEnd();
1687 int rdofs = local_num_cells_;
1688 for(int i=0; i < nw; i++){
1689 int wdof = rdofs + i;
1690 jacobian.entry(wdof,wdof) = 1.0;// better scaling ?
1691 }
1692 std::vector<std::vector<int>> wellconnections = this->getMaxWellConnections();
1693 for(int i=0; i < nw; i++){
1694 const auto& perfcells = wellconnections[i];
1695 for(int perfcell : perfcells){
1696 int wdof = rdofs + i;
1697 jacobian.entry(wdof,perfcell) = 0.0;
1698 jacobian.entry(perfcell, wdof) = 0.0;
1699 }
1700 }
1701 }
1702
1703
1704 template<typename TypeTag>
1705 void
1708 {
1709 DeferredLogger local_deferredLogger;
1711 {
1712 const auto& summary_state = simulator_.vanguard().summaryState();
1713 for (auto& well : well_container_) {
1714 well->recoverWellSolutionAndUpdateWellState(summary_state, x, this->wellState(), local_deferredLogger);
1715 }
1716 }
1717 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1718 "recoverWellSolutionAndUpdateWellState() failed: ",
1719 this->terminal_output_, simulator_.vanguard().grid().comm());
1720 }
1721
1722
1723 template<typename TypeTag>
1724 void
1727 {
1728 // Note: no point in trying to do a parallel gathering
1729 // try/catch here, as this function is not called in
1730 // parallel but for each individual domain of each rank.
1731 DeferredLogger local_deferredLogger;
1732 const auto& summary_state = this->simulator_.vanguard().summaryState();
1733 for (auto& well : well_container_) {
1734 if (well_domain_.at(well->name()) == domain.index) {
1735 well->recoverWellSolutionAndUpdateWellState(summary_state, x,
1736 this->wellState(),
1737 local_deferredLogger);
1738 }
1739 }
1740 // TODO: avoid losing the logging information that could
1741 // be stored in the local_deferredlogger in a parallel case.
1742 if (this->terminal_output_) {
1743 local_deferredLogger.logMessages();
1744 }
1745 }
1746
1747
1748 template<typename TypeTag>
1749 void
1752 {
1753 for (auto& well : well_container_) {
1754 well->initPrimaryVariablesEvaluation();
1755 }
1756 }
1757
1758
1759 template<typename TypeTag>
1760 void
1763 {
1764 for (auto& well : well_container_) {
1765 if (well_domain_.at(well->name()) == domain.index) {
1766 well->initPrimaryVariablesEvaluation();
1767 }
1768 }
1769 }
1770
1771
1772
1773
1774
1775
1776 template<typename TypeTag>
1779 getDomainWellConvergence(const Domain& domain,
1780 const std::vector<Scalar>& B_avg,
1781 DeferredLogger& local_deferredLogger) const
1782 {
1783 const auto& summary_state = simulator_.vanguard().summaryState();
1784 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1785 const bool relax_tolerance = iterationIdx > param_.strict_outer_iter_wells_;
1786
1787 ConvergenceReport report;
1788 for (const auto& well : well_container_) {
1789 if ((well_domain_.at(well->name()) == domain.index)) {
1790 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1791 report += well->getWellConvergence(summary_state,
1792 this->wellState(),
1793 B_avg,
1794 local_deferredLogger,
1795 relax_tolerance);
1796 } else {
1797 ConvergenceReport xreport;
1798 using CR = ConvergenceReport;
1799 xreport.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1800 report += xreport;
1801 }
1802 }
1803 }
1804
1805 // Log debug messages for NaN or too large residuals.
1806 if (this->terminal_output_) {
1807 for (const auto& f : report.wellFailures()) {
1808 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1809 local_deferredLogger.debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1810 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1811 local_deferredLogger.debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1812 }
1813 }
1814 }
1815 return report;
1816 }
1817
1818
1819
1820
1821
1822 template<typename TypeTag>
1825 getWellConvergence(const std::vector<Scalar>& B_avg, bool checkWellGroupControls) const
1826 {
1827
1828 DeferredLogger local_deferredLogger;
1829 // Get global (from all processes) convergence report.
1830 ConvergenceReport local_report;
1831 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1832 for (const auto& well : well_container_) {
1833 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1834 const auto& summary_state = simulator_.vanguard().summaryState();
1835 local_report += well->getWellConvergence(
1836 summary_state, this->wellState(), B_avg, local_deferredLogger,
1837 iterationIdx > param_.strict_outer_iter_wells_);
1838 } else {
1839 ConvergenceReport report;
1840 using CR = ConvergenceReport;
1841 report.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1842 local_report += report;
1843 }
1844 }
1845
1846 const Opm::Parallel::Communication comm = grid().comm();
1847 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
1848 ConvergenceReport report = gatherConvergenceReport(local_report, comm);
1849
1850 // the well_group_control_changed info is already communicated
1851 if (checkWellGroupControls) {
1852 report.setWellGroupTargetsViolated(this->lastReport().well_group_control_changed);
1853 }
1854
1855 if (this->terminal_output_) {
1856 global_deferredLogger.logMessages();
1857 }
1858 // Log debug messages for NaN or too large residuals.
1859 if (this->terminal_output_) {
1860 for (const auto& f : report.wellFailures()) {
1861 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1862 OpmLog::debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1863 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1864 OpmLog::debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1865 }
1866 }
1867 }
1868 return report;
1869 }
1870
1871
1872
1873
1874
1875 template<typename TypeTag>
1876 void
1878 calculateExplicitQuantities(DeferredLogger& deferred_logger) const
1879 {
1880 // TODO: checking isOperableAndSolvable() ?
1881 for (auto& well : well_container_) {
1882 well->calculateExplicitQuantities(simulator_, this->wellState(), deferred_logger);
1883 }
1884 }
1885
1886
1887
1888
1889
1890 template<typename TypeTag>
1891 std::pair<bool, bool>
1893 updateWellControls(const bool mandatory_network_balance, DeferredLogger& deferred_logger, const bool relax_network_tolerance)
1894 {
1895 const int episodeIdx = simulator_.episodeIndex();
1896 const auto& network = this->schedule()[episodeIdx].network();
1897 if (!this->wellsActive() && !network.active()) {
1898 return {false, false};
1899 }
1900
1901 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1902 const auto& comm = simulator_.vanguard().grid().comm();
1903 this->updateAndCommunicateGroupData(episodeIdx, iterationIdx);
1904
1905 // network related
1906 bool more_network_update = false;
1907 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx) || mandatory_network_balance) {
1908 const auto local_network_imbalance = this->updateNetworkPressures(episodeIdx);
1909 const Scalar network_imbalance = comm.max(local_network_imbalance);
1910 const auto& balance = this->schedule()[episodeIdx].network_balance();
1911 constexpr Scalar relaxation_factor = 10.0;
1912 const Scalar tolerance = relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance() : balance.pressure_tolerance();
1913 more_network_update = this->networkActive() && network_imbalance > tolerance;
1914 }
1915
1916 bool changed_well_group = false;
1917 // Check group individual constraints.
1918 const int nupcol = this->schedule()[episodeIdx].nupcol();
1919 // don't switch group control when iterationIdx > nupcol
1920 // to avoid oscilations between group controls
1921 if (iterationIdx <= nupcol) {
1922 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1923 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1924 }
1925 // Check wells' group constraints and communicate.
1926 bool changed_well_to_group = false;
1927 {
1928 // For MS Wells a linear solve is performed below and the matrix might be singular.
1929 // We need to communicate the exception thrown to the others and rethrow.
1931 for (const auto& well : well_container_) {
1933 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1934 if (changed_well) {
1935 changed_well_to_group = changed_well || changed_well_to_group;
1936 }
1937 }
1938 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1939 simulator_.gridView().comm());
1940 }
1941
1942 changed_well_to_group = comm.sum(static_cast<int>(changed_well_to_group));
1943 if (changed_well_to_group) {
1944 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1945 changed_well_group = true;
1946 }
1947
1948 // Check individual well constraints and communicate.
1949 bool changed_well_individual = false;
1950 {
1951 // For MS Wells a linear solve is performed below and the matrix might be singular.
1952 // We need to communicate the exception thrown to the others and rethrow.
1954 for (const auto& well : well_container_) {
1956 const bool changed_well = well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1957 if (changed_well) {
1958 changed_well_individual = changed_well || changed_well_individual;
1959 }
1960 }
1961 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1962 simulator_.gridView().comm());
1963 }
1964
1965 changed_well_individual = comm.sum(static_cast<int>(changed_well_individual));
1966 if (changed_well_individual) {
1967 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1968 changed_well_group = true;
1969 }
1970
1971 // update wsolvent fraction for REIN wells
1972 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1973 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1974
1975 return { changed_well_group, more_network_update };
1976 }
1977
1978 template<typename TypeTag>
1979 void
1981 updateWellControlsDomain(DeferredLogger& deferred_logger, const Domain& domain)
1982 {
1983 if ( !this->wellsActive() ) return ;
1984
1985 // TODO: decide on and implement an approach to handling of
1986 // group controls, network and similar for domain solves.
1987
1988 // Check only individual well constraints and communicate.
1989 for (const auto& well : well_container_) {
1990 if (well_domain_.at(well->name()) == domain.index) {
1992 well->updateWellControl(simulator_, mode, this->wellState(), this->groupState(), deferred_logger);
1993 }
1994 }
1995 }
1996
1997
1998
1999
2000
2001 template <typename TypeTag>
2002 void
2005 {
2006 this->wbpCalcMap_.clear();
2007 this->wbpCalcMap_.resize(this->wells_ecl_.size());
2008
2009 this->registerOpenWellsForWBPCalculation();
2010
2011 auto wellID = std::size_t{0};
2012 for (const auto& well : this->wells_ecl_) {
2013 this->wbpCalcMap_[wellID].wbpCalcIdx_ = this->wbpCalculationService_
2014 .createCalculator(well,
2015 this->local_parallel_well_info_[wellID],
2016 this->conn_idx_map_[wellID].local(),
2017 this->makeWellSourceEvaluatorFactory(wellID));
2018
2019 ++wellID;
2020 }
2021
2022 this->wbpCalculationService_.defineCommunication();
2023 }
2024
2025
2026
2027
2028
2029 template <typename TypeTag>
2030 data::WellBlockAveragePressures
2033 {
2034 auto wbpResult = data::WellBlockAveragePressures{};
2035
2036 using Calculated = PAvgCalculator::Result::WBPMode;
2037 using Output = data::WellBlockAvgPress::Quantity;
2038
2039 this->wbpCalculationService_.collectDynamicValues();
2040
2041 const auto numWells = this->wells_ecl_.size();
2042 for (auto wellID = 0*numWells; wellID < numWells; ++wellID) {
2043 const auto calcIdx = this->wbpCalcMap_[wellID].wbpCalcIdx_;
2044 const auto& well = this->wells_ecl_[wellID];
2045
2046 if (! well.hasRefDepth()) {
2047 // Can't perform depth correction without at least a
2048 // fall-back datum depth.
2049 continue;
2050 }
2051
2052 this->wbpCalculationService_
2053 .inferBlockAveragePressures(calcIdx, well.pavg(),
2054 this->gravity_,
2055 well.getWPaveRefDepth());
2056
2057 const auto& result = this->wbpCalculationService_
2058 .averagePressures(calcIdx);
2059
2060 auto& reported = wbpResult.values[well.name()];
2061
2062 reported[Output::WBP] = result.value(Calculated::WBP);
2063 reported[Output::WBP4] = result.value(Calculated::WBP4);
2064 reported[Output::WBP5] = result.value(Calculated::WBP5);
2065 reported[Output::WBP9] = result.value(Calculated::WBP9);
2066 }
2067
2068 return wbpResult;
2069 }
2070
2071
2072
2073
2074
2075 template <typename TypeTag>
2078 makeWellSourceEvaluatorFactory(const std::vector<Well>::size_type wellIdx) const
2079 {
2080 using Span = PAvgDynamicSourceData::SourceDataSpan<Scalar>;
2081 using Item = typename Span::Item;
2082
2083 return [wellIdx, this]() -> ParallelWBPCalculation::Evaluator
2084 {
2085 if (! this->wbpCalcMap_[wellIdx].openWellIdx_.has_value()) {
2086 // Well is stopped/shut. Return evaluator for stopped wells.
2087 return []([[maybe_unused]] const int connIdx, Span sourceTerm)
2088 {
2089 // Well/connection is stopped/shut. Set all items to
2090 // zero.
2091
2092 sourceTerm
2093 .set(Item::Pressure , 0.0)
2094 .set(Item::PoreVol , 0.0)
2095 .set(Item::MixtureDensity, 0.0);
2096 };
2097 }
2098
2099 // Well is open. Return an evaluator for open wells/open connections.
2100 return [this, wellPtr = this->well_container_[*this->wbpCalcMap_[wellIdx].openWellIdx_].get()]
2101 (const int connIdx, Span sourceTerm)
2102 {
2103 // Note: The only item which actually matters for the WBP
2104 // calculation at the well reservoir connection level is the
2105 // mixture density. Set other items to zero.
2106
2107 const auto& connIdxMap =
2108 this->conn_idx_map_[wellPtr->indexOfWell()];
2109
2110 const auto rho = wellPtr->
2111 connectionDensity(connIdxMap.global(connIdx),
2112 connIdxMap.open(connIdx));
2113
2114 sourceTerm
2115 .set(Item::Pressure , 0.0)
2116 .set(Item::PoreVol , 0.0)
2117 .set(Item::MixtureDensity, rho);
2118 };
2119 };
2120 }
2121
2122
2123
2124
2125
2126 template <typename TypeTag>
2127 void
2130 {
2131 assert (this->wbpCalcMap_.size() == this->wells_ecl_.size());
2132
2133 for (auto& wbpCalc : this->wbpCalcMap_) {
2134 wbpCalc.openWellIdx_.reset();
2135 }
2136
2137 auto openWellIdx = typename std::vector<WellInterfacePtr>::size_type{0};
2138 for (const auto* openWell : this->well_container_generic_) {
2139 this->wbpCalcMap_[openWell->indexOfWell()].openWellIdx_ = openWellIdx++;
2140 }
2141 }
2142
2143
2144
2145
2146
2147 template<typename TypeTag>
2148 void
2150 updateAndCommunicate(const int reportStepIdx,
2151 const int iterationIdx,
2152 DeferredLogger& deferred_logger)
2153 {
2154 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx);
2155
2156 // updateWellStateWithTarget might throw for multisegment wells hence we
2157 // have a parallel try catch here to thrown on all processes.
2159 // if a well or group change control it affects all wells that are under the same group
2160 for (const auto& well : well_container_) {
2161 well->updateWellStateWithTarget(simulator_, this->groupState(),
2162 this->wellState(), deferred_logger);
2163 }
2164 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAndCommunicate failed: ",
2165 simulator_.gridView().comm())
2166 this->updateAndCommunicateGroupData(reportStepIdx, iterationIdx);
2167 }
2168
2169 template<typename TypeTag>
2170 bool
2172 updateGroupControls(const Group& group,
2173 DeferredLogger& deferred_logger,
2174 const int reportStepIdx,
2175 const int iterationIdx)
2176 {
2177 bool changed = false;
2178 bool changed_hc = this->checkGroupHigherConstraints( group, deferred_logger, reportStepIdx);
2179 if (changed_hc) {
2180 changed = true;
2181 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2182 }
2183
2184 bool changed_individual =
2186 updateGroupIndividualControl(group,
2187 reportStepIdx,
2188 this->switched_inj_groups_,
2189 this->switched_prod_groups_,
2190 this->closed_offending_wells_,
2191 this->groupState(),
2192 this->wellState(),
2193 deferred_logger);
2194
2195 if (changed_individual) {
2196 changed = true;
2197 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2198 }
2199 // call recursively down the group hierarchy
2200 for (const std::string& groupName : group.groups()) {
2201 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
2202 changed = changed || changed_this;
2203 }
2204 return changed;
2205 }
2206
2207 template<typename TypeTag>
2208 void
2210 updateWellTestState(const double& simulationTime, WellTestState& wellTestState) const
2211 {
2212 DeferredLogger local_deferredLogger;
2213 for (const auto& well : well_container_) {
2214 const auto& wname = well->name();
2215 const auto wasClosed = wellTestState.well_is_closed(wname);
2216 well->checkWellOperability(simulator_, this->wellState(), local_deferredLogger);
2217 well->updateWellTestState(this->wellState().well(wname), simulationTime, /*writeMessageToOPMLog=*/ true, wellTestState, local_deferredLogger);
2218
2219 if (!wasClosed && wellTestState.well_is_closed(wname)) {
2220 this->closed_this_step_.insert(wname);
2221 }
2222 }
2223
2224 const Opm::Parallel::Communication comm = grid().comm();
2225 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
2226
2227 for (const auto& [group_name, to] : this->closed_offending_wells_) {
2228 if (!this->wasDynamicallyShutThisTimeStep(to.second)) {
2229 wellTestState.close_well(to.second, WellTestConfig::Reason::GROUP, simulationTime);
2230 this->updateClosedWellsThisStep(to.second);
2231 const std::string msg =
2232 fmt::format("Procedure on exceeding {} limit is WELL for group {}. Well {} is {}.",
2233 to.first,
2234 group_name,
2235 to.second,
2236 "shut");
2237 global_deferredLogger.info(msg);
2238 }
2239 }
2240
2241 if (this->terminal_output_) {
2242 global_deferredLogger.logMessages();
2243 }
2244 }
2245
2246
2247 template<typename TypeTag>
2248 void
2250 const WellState<Scalar>& well_state_copy,
2251 std::string& exc_msg,
2252 ExceptionType::ExcEnum& exc_type,
2253 DeferredLogger& deferred_logger)
2254 {
2255 const int np = this->numPhases();
2256 std::vector<Scalar> potentials;
2257 const auto& well = well_container_[widx];
2258 std::string cur_exc_msg;
2259 auto cur_exc_type = ExceptionType::NONE;
2260 try {
2261 well->computeWellPotentials(simulator_, well_state_copy, potentials, deferred_logger);
2262 }
2263 // catch all possible exception and store type and message.
2264 OPM_PARALLEL_CATCH_CLAUSE(cur_exc_type, cur_exc_msg);
2265 if (cur_exc_type != ExceptionType::NONE) {
2266 exc_msg += fmt::format("\nFor well {}: {}", well->name(), cur_exc_msg);
2267 }
2268 exc_type = std::max(exc_type, cur_exc_type);
2269 // Store it in the well state
2270 // potentials is resized and set to zero in the beginning of well->ComputeWellPotentials
2271 // and updated only if sucessfull. i.e. the potentials are zero for exceptions
2272 auto& ws = this->wellState().well(well->indexOfWell());
2273 for (int p = 0; p < np; ++p) {
2274 // make sure the potentials are positive
2275 ws.well_potentials[p] = std::max(0.0, potentials[p]);
2276 }
2277 }
2278
2279
2280
2281 template <typename TypeTag>
2282 void
2285 {
2286 for (const auto& wellPtr : this->well_container_) {
2287 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2288 }
2289 }
2290
2291
2292
2293
2294
2295 template <typename TypeTag>
2296 void
2298 calculateProductivityIndexValuesShutWells(const int reportStepIdx,
2299 DeferredLogger& deferred_logger)
2300 {
2301 // For the purpose of computing PI/II values, it is sufficient to
2302 // construct StandardWell instances only. We don't need to form
2303 // well objects that honour the 'isMultisegment()' flag of the
2304 // corresponding "this->wells_ecl_[shutWell]".
2305
2306 for (const auto& shutWell : this->local_shut_wells_) {
2307 if (!this->wells_ecl_[shutWell].hasConnections()) {
2308 // No connections in this well. Nothing to do.
2309 continue;
2310 }
2311
2312 auto wellPtr = this->template createTypedWellPointer
2313 <StandardWell<TypeTag>>(shutWell, reportStepIdx);
2314
2315 wellPtr->init(&this->phase_usage_, this->depth_, this->gravity_,
2316 this->local_num_cells_, this->B_avg_, true);
2317
2318 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2319 }
2320 }
2321
2322
2323
2324
2325
2326 template <typename TypeTag>
2327 void
2330 DeferredLogger& deferred_logger)
2331 {
2332 wellPtr->updateProductivityIndex(this->simulator_,
2333 this->prod_index_calc_[wellPtr->indexOfWell()],
2334 this->wellState(),
2335 deferred_logger);
2336 }
2337
2338
2339
2340 template<typename TypeTag>
2341 void
2343 prepareTimeStep(DeferredLogger& deferred_logger)
2344 {
2345 // Check if there is a network with active prediction wells at this time step.
2346 const auto episodeIdx = simulator_.episodeIndex();
2347 this->updateNetworkActiveState(episodeIdx);
2348
2349 // Rebalance the network initially if any wells in the network have status changes
2350 // (Need to check this before clearing events)
2351 const bool do_prestep_network_rebalance = this->needPreStepNetworkRebalance(episodeIdx);
2352
2353 for (const auto& well : well_container_) {
2354 auto& events = this->wellState().well(well->indexOfWell()).events;
2355 if (events.hasEvent(WellState<Scalar>::event_mask)) {
2356 well->updateWellStateWithTarget(simulator_, this->groupState(), this->wellState(), deferred_logger);
2357 const auto& summary_state = simulator_.vanguard().summaryState();
2358 well->updatePrimaryVariables(summary_state, this->wellState(), deferred_logger);
2359 well->initPrimaryVariablesEvaluation();
2360 // There is no new well control change input within a report step,
2361 // so next time step, the well does not consider to have effective events anymore.
2362 events.clearEvent(WellState<Scalar>::event_mask);
2363 }
2364 // these events only work for the first time step within the report step
2365 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2366 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2367 }
2368 // solve the well equation initially to improve the initial solution of the well model
2369 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2370 try {
2371 well->solveWellEquation(simulator_, this->wellState(), this->groupState(), deferred_logger);
2372 } catch (const std::exception& e) {
2373 const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the previous rates";
2374 deferred_logger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
2375 }
2376 }
2377 // If we're using local well solves that include control switches, they also update
2378 // operability, so reset before main iterations begin
2379 well->resetWellOperability();
2380 }
2381 updatePrimaryVariables(deferred_logger);
2382
2383 // Actually do the pre-step network rebalance, using the updated well states and initial solutions
2384 if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger);
2385 }
2386
2387 template<typename TypeTag>
2388 void
2391 {
2392 std::vector< Scalar > B_avg(numComponents(), Scalar() );
2393 const auto& grid = simulator_.vanguard().grid();
2394 const auto& gridView = grid.leafGridView();
2395 ElementContext elemCtx(simulator_);
2396
2398 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2399 elemCtx.updatePrimaryStencil(elem);
2400 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
2401
2402 const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
2403 const auto& fs = intQuants.fluidState();
2404
2405 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2406 {
2407 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2408 continue;
2409 }
2410
2411 const unsigned compIdx = Indices::canonicalToActiveComponentIndex(FluidSystem::solventComponentIndex(phaseIdx));
2412 auto& B = B_avg[ compIdx ];
2413
2414 B += 1 / fs.invB(phaseIdx).value();
2415 }
2416 if constexpr (has_solvent_) {
2417 auto& B = B_avg[solventSaturationIdx];
2418 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2419 }
2420 }
2421 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAverageFormationFactor() failed: ", grid.comm())
2422
2423 // compute global average
2424 grid.comm().sum(B_avg.data(), B_avg.size());
2425 for (auto& bval : B_avg)
2426 {
2427 bval /= global_num_cells_;
2428 }
2429 B_avg_ = B_avg;
2430 }
2431
2432
2433
2434
2435
2436 template<typename TypeTag>
2437 void
2440 {
2441 for (const auto& well : well_container_) {
2442 const auto& summary_state = simulator_.vanguard().summaryState();
2443 well->updatePrimaryVariables(summary_state, this->wellState(), deferred_logger);
2444 }
2445 }
2446
2447 template<typename TypeTag>
2448 void
2450 {
2451 const auto& grid = simulator_.vanguard().grid();
2452 const auto& eclProblem = simulator_.problem();
2453 const unsigned numCells = grid.size(/*codim=*/0);
2454
2455 this->pvt_region_idx_.resize(numCells);
2456 for (unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2457 this->pvt_region_idx_[cellIdx] =
2458 eclProblem.pvtRegionIndex(cellIdx);
2459 }
2460 }
2461
2462 // The number of components in the model.
2463 template<typename TypeTag>
2464 int
2466 {
2467 // The numComponents here does not reflect the actual number of the components in the system.
2468 // It more or less reflects the number of mass conservation equations for the well equations.
2469 // For example, in the current formulation, we do not have the polymer conservation equation
2470 // in the well equations. As a result, for an oil-water-polymer system, this function will return 2.
2471 // In some way, it makes this function appear to be confusing from its name, and we need
2472 // to revisit/revise this function again when extending the variants of system that flow can simulate.
2473 int numComp = this->numPhases() < 3 ? this->numPhases() : FluidSystem::numComponents;
2474 if constexpr (has_solvent_) {
2475 numComp++;
2476 }
2477 return numComp;
2478 }
2479
2480 template<typename TypeTag>
2481 void
2483 {
2484 const auto& eclProblem = simulator_.problem();
2485 depth_.resize(local_num_cells_);
2486 for (unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2487 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2488 }
2489 }
2490
2491 template<typename TypeTag>
2494 getWell(const std::string& well_name) const
2495 {
2496 // finding the iterator of the well in wells_ecl
2497 auto well = std::find_if(well_container_.begin(),
2498 well_container_.end(),
2499 [&well_name](const WellInterfacePtr& elem)->bool {
2500 return elem->name() == well_name;
2501 });
2502
2503 assert(well != well_container_.end());
2504
2505 return *well;
2506 }
2507
2508 template<typename TypeTag>
2509 bool
2511 hasWell(const std::string& well_name) const
2512 {
2513 return std::any_of(well_container_.begin(), well_container_.end(),
2514 [&well_name](const WellInterfacePtr& elem) -> bool
2515 {
2516 return elem->name() == well_name;
2517 });
2518 }
2519
2520
2521
2522
2523 template <typename TypeTag>
2524 int
2526 reportStepIndex() const
2527 {
2528 return std::max(this->simulator_.episodeIndex(), 0);
2529 }
2530
2531
2532
2533
2534
2535 template<typename TypeTag>
2536 void
2538 calcRates(const int fipnum,
2539 const int pvtreg,
2540 const std::vector<Scalar>& production_rates,
2541 std::vector<Scalar>& resv_coeff)
2542 {
2543 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2544 }
2545
2546 template<typename TypeTag>
2547 void
2549 calcInjRates(const int fipnum,
2550 const int pvtreg,
2551 std::vector<Scalar>& resv_coeff)
2552 {
2553 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2554 }
2555
2556
2557 template <typename TypeTag>
2558 void
2561 {
2562 if (!has_energy_)
2563 return;
2564
2565 int np = this->numPhases();
2566 Scalar cellInternalEnergy;
2567 Scalar cellBinv;
2568 Scalar cellDensity;
2569 Scalar perfPhaseRate;
2570 const int nw = this->numLocalWells();
2571 for (auto wellID = 0*nw; wellID < nw; ++wellID) {
2572 const Well& well = this->wells_ecl_[wellID];
2573 if (well.isInjector())
2574 continue;
2575
2576 std::array<Scalar,2> weighted{0.0,0.0};
2577 auto& [weighted_temperature, total_weight] = weighted;
2578
2579 auto& well_info = this->local_parallel_well_info_[wellID].get();
2580 auto& ws = this->wellState().well(wellID);
2581 auto& perf_data = ws.perf_data;
2582 auto& perf_phase_rate = perf_data.phase_rates;
2583
2584 using int_type = decltype(this->well_perf_data_[wellID].size());
2585 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2586 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2587 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, /*timeIdx=*/0);
2588 const auto& fs = intQuants.fluidState();
2589
2590 // we on only have one temperature pr cell any phaseIdx will do
2591 Scalar cellTemperatures = fs.temperature(/*phaseIdx*/0).value();
2592
2593 Scalar weight_factor = 0.0;
2594 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2595 {
2596 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2597 continue;
2598 }
2599 cellInternalEnergy = fs.enthalpy(phaseIdx).value() - fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2600 cellBinv = fs.invB(phaseIdx).value();
2601 cellDensity = fs.density(phaseIdx).value();
2602 perfPhaseRate = perf_phase_rate[ perf*np + phaseIdx ];
2603 weight_factor += cellDensity * perfPhaseRate/cellBinv * cellInternalEnergy/cellTemperatures;
2604 }
2605 total_weight += weight_factor;
2606 weighted_temperature += weight_factor * cellTemperatures;
2607 }
2608 well_info.communication().sum(weighted.data(), 2);
2609 this->wellState().well(wellID).temperature = weighted_temperature/total_weight;
2610 }
2611 }
2612
2613
2614 template <typename TypeTag>
2615 void
2617 logPrimaryVars() const
2618 {
2619 std::ostringstream os;
2620 for (const auto& w : well_container_) {
2621 os << w->name() << ":";
2622 auto pv = w->getPrimaryVars();
2623 for (const Scalar v : pv) {
2624 os << ' ' << v;
2625 }
2626 os << '\n';
2627 }
2628 OpmLog::debug(os.str());
2629 }
2630
2631
2632
2633 template <typename TypeTag>
2634 std::vector<typename BlackoilWellModel<TypeTag>::Scalar>
2636 getPrimaryVarsDomain(const Domain& domain) const
2637 {
2638 std::vector<Scalar> ret;
2639 for (const auto& well : well_container_) {
2640 if (well_domain_.at(well->name()) == domain.index) {
2641 const auto& pv = well->getPrimaryVars();
2642 ret.insert(ret.end(), pv.begin(), pv.end());
2643 }
2644 }
2645 return ret;
2646 }
2647
2648
2649
2650 template <typename TypeTag>
2651 void
2653 setPrimaryVarsDomain(const Domain& domain, const std::vector<Scalar>& vars)
2654 {
2655 std::size_t offset = 0;
2656 for (auto& well : well_container_) {
2657 if (well_domain_.at(well->name()) == domain.index) {
2658 int num_pri_vars = well->setPrimaryVars(vars.begin() + offset);
2659 offset += num_pri_vars;
2660 }
2661 }
2662 assert(offset == vars.size());
2663 }
2664
2665
2666
2667 template <typename TypeTag>
2668 void
2670 setupDomains(const std::vector<Domain>& domains)
2671 {
2673 // TODO: This loop nest may be slow for very large numbers of
2674 // domains and wells, but that has not been observed on tests so
2675 // far. Using the partition vector instead would be faster if we
2676 // need to change.
2677 for (const auto& wellPtr : this->well_container_) {
2678 const int first_well_cell = wellPtr->cells().front();
2679 for (const auto& domain : domains) {
2680 auto cell_present = [&domain](const auto cell)
2681 {
2682 return std::binary_search(domain.cells.begin(),
2683 domain.cells.end(), cell);
2684 };
2685
2686 if (cell_present(first_well_cell)) {
2687 // Assuming that if the first well cell is found in a domain,
2688 // then all of that well's cells are in that same domain.
2689 well_domain_[wellPtr->name()] = domain.index;
2690
2691 // Verify that all of that well's cells are in that same domain.
2692 for (int well_cell : wellPtr->cells()) {
2693 if (! cell_present(well_cell)) {
2694 OPM_THROW(std::runtime_error,
2695 fmt::format("Well '{}' found on multiple domains.",
2696 wellPtr->name()));
2697 }
2698 }
2699 }
2700 }
2701 }
2702 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::setupDomains(): well found on multiple domains.",
2703 simulator_.gridView().comm());
2704
2705 // Write well/domain info to the DBG file.
2706 const Opm::Parallel::Communication& comm = grid().comm();
2707 const int rank = comm.rank();
2708 DeferredLogger local_log;
2709 if (!well_domain_.empty()) {
2710 std::ostringstream os;
2711 os << "Well name Rank Domain\n";
2712 for (const auto& [wname, domain] : well_domain_) {
2713 os << wname << std::setw(19 - wname.size()) << rank << std::setw(12) << domain << '\n';
2714 }
2715 local_log.debug(os.str());
2716 }
2717 auto global_log = gatherDeferredLogger(local_log, comm);
2718 if (this->terminal_output_) {
2719 global_log.logMessages();
2720 }
2721 }
2722
2723} // 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:2078
void initializeGroupStructure(const int reportStepIdx)
Definition: BlackoilWellModel_impl.hpp:345
void apply(BVector &r) const
Definition: BlackoilWellModel_impl.hpp:1547
void prepareTimeStep(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2343
bool updateGroupControls(const Group &group, DeferredLogger &deferred_logger, const int reportStepIdx, const int iterationIdx)
Definition: BlackoilWellModel_impl.hpp:2172
void calcRates(const int fipnum, const int pvtreg, const std::vector< Scalar > &production_rates, std::vector< Scalar > &resv_coeff) override
Definition: BlackoilWellModel_impl.hpp:2538
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:2494
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:2150
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:2636
typename GasLiftGroupInfo< Scalar >::GLiftEclWells GLiftEclWells
Definition: BlackoilWellModel.hpp:121
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:1654
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:1504
void applyScaleAdd(const Scalar alpha, const BVector &x, BVector &Ax) const
Definition: BlackoilWellModel_impl.hpp:1608
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:1981
void assembleWellEq(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1493
void gasLiftOptimizationStage1SingleWell(WellInterface< TypeTag > *well, DeferredLogger &deferred_logger, GLiftProdWells &prod_wells, GLiftOptWells &glift_wells, GasLiftGroupInfo< Scalar > &group_info, GLiftWellStateMap &state_map, GLiftSyncGroups &groups_to_sync)
Definition: BlackoilWellModel_impl.hpp:1453
void linearizeDomain(const Domain &domain, SparseMatrixAdapter &jacobian, GlobalEqVector &res)
Definition: BlackoilWellModel_impl.hpp:225
int reportStepIndex() const
Definition: BlackoilWellModel_impl.hpp:2526
void calculateProductivityIndexValues(DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:2284
void extractLegacyDepth_()
Definition: BlackoilWellModel_impl.hpp:2482
void extractLegacyCellPvtRegionIndex_()
Definition: BlackoilWellModel_impl.hpp:2449
void updateAverageFormationFactor()
Definition: BlackoilWellModel_impl.hpp:2390
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:2549
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:2560
void addWellPressureEquations(PressureMatrix &jacobian, const BVector &weights, const bool use_well_weights) const
Definition: BlackoilWellModel_impl.hpp:1638
void logPrimaryVars() const
Definition: BlackoilWellModel_impl.hpp:2617
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:1628
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:2249
std::pair< bool, bool > updateWellControls(const bool mandatory_network_balance, DeferredLogger &deferred_logger, const bool relax_network_tolerance=false)
Definition: BlackoilWellModel_impl.hpp:1893
void recoverWellSolutionAndUpdateWellStateDomain(const BVector &x, const Domain &domain)
Definition: BlackoilWellModel_impl.hpp:1726
typename GasLiftSingleWellGeneric< Scalar >::GLiftSyncGroups GLiftSyncGroups
Definition: BlackoilWellModel.hpp:122
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:2511
void assembleWellEqWithoutIteration(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1528
data::WellBlockAveragePressures computeWellBlockAveragePressures() const
Definition: BlackoilWellModel_impl.hpp:2032
void initPrimaryVariablesEvaluation() const
Definition: BlackoilWellModel_impl.hpp:1751
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:1878
Dune::FieldVector< Scalar, numEq > VectorBlockType
Definition: BlackoilWellModel.hpp:135
void updatePrimaryVariables(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2439
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:1825
void registerOpenWellsForWBPCalculation()
Definition: BlackoilWellModel_impl.hpp:2129
void linearize(SparseMatrixAdapter &jacobian, GlobalEqVector &res) override
Definition: BlackoilWellModel_impl.hpp:204
void initGliftEclWellMap(GLiftEclWells &ecl_well_map)
Definition: BlackoilWellModel_impl.hpp:1481
void gasLiftOptimizationStage1(DeferredLogger &deferred_logger, GLiftProdWells &prod_wells, GLiftOptWells &glift_wells, GasLiftGroupInfo< Scalar > &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:2653
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:2210
ConvergenceReport getDomainWellConvergence(const Domain &domain, const std::vector< Scalar > &B_avg, DeferredLogger &local_deferredLogger) const
Definition: BlackoilWellModel_impl.hpp:1779
void initializeWBPCalculationService()
Definition: BlackoilWellModel_impl.hpp:2004
void initPrimaryVariablesEvaluationDomain(const Domain &domain) const
Definition: BlackoilWellModel_impl.hpp:1762
void recoverWellSolutionAndUpdateWellState(const BVector &x)
Definition: BlackoilWellModel_impl.hpp:1707
void setupDomains(const std::vector< Domain > &domains)
Definition: BlackoilWellModel_impl.hpp:2670
void addWellPressureEquationsStruct(PressureMatrix &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1684
void calculateProductivityIndexValuesShutWells(const int reportStepIdx, DeferredLogger &deferred_logger) override
Definition: BlackoilWellModel_impl.hpp:2298
void endReportStep()
Definition: BlackoilWellModel_impl.hpp:637
void prepareWellsBeforeAssembling(const double dt, DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:1517
typename BlackoilWellModelGeneric< Scalar >::GLiftWellStateMap GLiftWellStateMap
Definition: BlackoilWellModel.hpp:120
int numComponents() const
Definition: BlackoilWellModel_impl.hpp:2465
Definition: ConvergenceReport.hpp:38
void setWellFailed(const WellFailure &wf)
Definition: ConvergenceReport.hpp:150
void setWellGroupTargetsViolated(const bool wellGroupTargetsViolated)
Definition: ConvergenceReport.hpp:162
const std::vector< WellFailure > & wellFailures() const
Definition: ConvergenceReport.hpp:244
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
std::tuple< Scalar, Scalar, Scalar, Scalar > getRates(const int group_idx) const
bool hasWell(const std::string &well_name)
void updateRate(int idx, Scalar oil_rate, Scalar gas_rate, Scalar water_rate, Scalar alq)
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)
const std::string & name() const
Well name.
int indexOfWell() const
Index of well in the wells struct and wellState.
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