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