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, false, false
1460 );
1461 rate = -tcalc.calcModeRateFromRates(ws.surface_rates);
1462 group_rate += rate;
1463 }
1464 }
1465 return (group_rate - orig_target)/orig_target;
1466 };
1467
1468 const auto upbranch = network.uptree_branch(nodeName);
1469 const auto it = this->node_pressures_.find((*upbranch).uptree_node());
1470 const Scalar nodal_pressure = it->second;
1471 Scalar well_group_thp = nodal_pressure;
1472
1473 std::optional<Scalar> autochoke_thp;
1474 if (auto iter = this->well_group_thp_calc_.find(nodeName); iter != this->well_group_thp_calc_.end()) {
1475 autochoke_thp = this->well_group_thp_calc_.at(nodeName);
1476 }
1477
1478 using WellBhpThpCalculatorType = WellBhpThpCalculator<Scalar, IndexTraits>;
1479 //Find an initial bracket
1480 std::array<Scalar, 2> range_initial;
1481 if (!autochoke_thp.has_value()){
1482 Scalar min_thp, max_thp;
1483 // Retrieve the terminal pressure of the associated root of the manifold group
1484 std::string node_name = nodeName;
1485 while (!network.node(node_name).terminal_pressure().has_value()) {
1486 auto branch = network.uptree_branch(node_name).value();
1487 node_name = branch.uptree_node();
1488 }
1489 min_thp = network.node(node_name).terminal_pressure().value();
1490 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, min_thp, max_thp);
1491 // Narrow down the bracket
1492 Scalar low1, high1;
1493 std::array<Scalar, 2> range = {Scalar{0.9}*min_thp, Scalar{1.1}*max_thp};
1494 std::optional<Scalar> appr_sol;
1495 WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low1, high1, appr_sol, 0.0, local_deferredLogger);
1496 min_thp = low1;
1497 max_thp = high1;
1498 range_initial = {min_thp, max_thp};
1499 }
1500
1501 if (!autochoke_thp.has_value() || autochoke_thp.value() > nodal_pressure) {
1502 // The bracket is based on the initial bracket or on a range based on a previous calculated group thp
1503 std::array<Scalar, 2> range = autochoke_thp.has_value() ?
1504 std::array<Scalar, 2>{Scalar{0.9} * autochoke_thp.value(),
1505 Scalar{1.1} * autochoke_thp.value()} : range_initial;
1506 Scalar low, high;
1507 std::optional<Scalar> approximate_solution;
1508 const Scalar tolerance1 = thp_tolerance;
1509 local_deferredLogger.debug("Using brute force search to bracket the group THP");
1510 const bool finding_bracket = WellBhpThpCalculatorType::bruteForceBracketCommonTHP(mismatch, range, low, high, approximate_solution, tolerance1, local_deferredLogger);
1511
1512 if (approximate_solution.has_value()) {
1513 autochoke_thp = *approximate_solution;
1514 local_deferredLogger.debug("Approximate group THP value found: " + std::to_string(autochoke_thp.value()));
1515 } else if (finding_bracket) {
1516 const Scalar tolerance2 = thp_tolerance;
1517 const int max_iteration_solve = 100;
1518 int iteration = 0;
1519 autochoke_thp = RegulaFalsiBisection<ThrowOnError>::
1520 solve(mismatch, low, high, max_iteration_solve, tolerance2, iteration);
1521 local_deferredLogger.debug(" bracket = [" + std::to_string(low) + ", " + std::to_string(high) + "], " +
1522 "iteration = " + std::to_string(iteration));
1523 local_deferredLogger.debug("Group THP value = " + std::to_string(autochoke_thp.value()));
1524 } else {
1525 autochoke_thp.reset();
1526 local_deferredLogger.debug("Group THP solve failed due to bracketing failure");
1527 }
1528 }
1529 if (autochoke_thp.has_value()) {
1530 well_group_thp_calc_[nodeName] = autochoke_thp.value();
1531 // Note: The node pressure of the auto-choke node is set to well_group_thp in computeNetworkPressures()
1532 // and must be larger or equal to the pressure of the uptree node of its branch.
1533 well_group_thp = std::max(autochoke_thp.value(), nodal_pressure);
1534 }
1535
1536 for (auto& well : this->well_container_) {
1537 std::string well_name = well->name();
1538
1539 if (well->isInjector() || !well->wellEcl().predictionMode())
1540 continue;
1541
1542 if (group.hasWell(well_name)) {
1543 well->setDynamicThpLimit(well_group_thp);
1544 }
1545 const auto& ws = this->wellState().well(well->indexOfWell());
1546 const bool thp_is_limit = ws.production_cmode == Well::ProducerCMode::THP;
1547 if (thp_is_limit) {
1548 well->prepareWellBeforeAssembling(
1549 this->simulator_, dt, this->wgHelper(), this->wellState(), local_deferredLogger
1550 );
1551 }
1552 }
1553
1554 // Use the group THP in computeNetworkPressures().
1555 const auto& current_well_group_thp = group_state.is_autochoke_group(nodeName) ? group_state.well_group_thp(nodeName) : 1e30;
1556 if (std::abs(current_well_group_thp - well_group_thp) > balance.pressure_tolerance()) {
1557 well_group_thp_updated = true;
1558 group_state.update_well_group_thp(nodeName, well_group_thp);
1559 }
1560 }
1561 }
1562 return well_group_thp_updated;
1563 }
1564
1565 template<typename TypeTag>
1566 void
1568 assembleWellEq(const double dt, DeferredLogger& deferred_logger)
1569 {
1570 OPM_TIMEFUNCTION();
1571 for (auto& well : well_container_) {
1572 well->assembleWellEq(simulator_, dt, this->wgHelper(), deferred_logger);
1573 }
1574 }
1575
1576
1577 template<typename TypeTag>
1578 void
1580 prepareWellsBeforeAssembling(const double dt, DeferredLogger& deferred_logger)
1581 {
1582 OPM_TIMEFUNCTION();
1583 for (auto& well : well_container_) {
1584 well->prepareWellBeforeAssembling(
1585 simulator_, dt, this->wgHelper(), this->wellState(), deferred_logger
1586 );
1587 }
1588 }
1589
1590
1591 template<typename TypeTag>
1592 void
1594 assembleWellEqWithoutIteration(const double dt, DeferredLogger& deferred_logger)
1595 {
1596 OPM_TIMEFUNCTION();
1597 // We make sure that all processes throw in case there is an exception
1598 // on one of them (WetGasPvt::saturationPressure might throw if not converged)
1600
1601 for (auto& well: well_container_) {
1602 well->assembleWellEqWithoutIteration(simulator_, dt, this->wellState(), this->groupState(),
1603 deferred_logger);
1604 }
1605 OPM_END_PARALLEL_TRY_CATCH_LOG(deferred_logger, "BlackoilWellModel::assembleWellEqWithoutIteration failed: ",
1606 this->terminal_output_, grid().comm());
1607
1608 }
1609
1610 template<typename TypeTag>
1611 void
1614 {
1615 // Pre-compute cell rates for all wells
1616 cellRates_.clear();
1617 for (const auto& well : well_container_) {
1618 well->addCellRates(cellRates_);
1619 }
1620 }
1621
1622 template<typename TypeTag>
1623 void
1625 updateCellRatesForDomain(int domainIndex, const std::map<std::string, int>& well_domain_map)
1626 {
1627 // Pre-compute cell rates only for wells in the specified domain
1628 cellRates_.clear();
1629 for (const auto& well : well_container_) {
1630 const auto it = well_domain_map.find(well->name());
1631 if (it != well_domain_map.end() && it->second == domainIndex) {
1632 well->addCellRates(cellRates_);
1633 }
1634 }
1635 }
1636
1637#if COMPILE_GPU_BRIDGE
1638 template<typename TypeTag>
1639 void
1642 {
1643 // prepare for StandardWells
1645
1646 for(unsigned int i = 0; i < well_container_.size(); i++){
1647 auto& well = well_container_[i];
1648 auto derived = dynamic_cast<StandardWell<TypeTag>*>(well.get());
1649 if (derived) {
1650 wellContribs.addNumBlocks(derived->linSys().getNumBlocks());
1651 }
1652 }
1653
1654 // allocate memory for data from StandardWells
1655 wellContribs.alloc();
1656
1657 for(unsigned int i = 0; i < well_container_.size(); i++){
1658 auto& well = well_container_[i];
1659 // maybe WellInterface could implement addWellContribution()
1660 auto derived_std = dynamic_cast<StandardWell<TypeTag>*>(well.get());
1661 if (derived_std) {
1662 derived_std->linSys().extract(derived_std->numStaticWellEq, wellContribs);
1663 } else {
1664 auto derived_ms = dynamic_cast<MultisegmentWell<TypeTag>*>(well.get());
1665 if (derived_ms) {
1666 derived_ms->linSys().extract(wellContribs);
1667 } else {
1668 OpmLog::warning("Warning unknown type of well");
1669 }
1670 }
1671 }
1672 }
1673#endif
1674
1675 template<typename TypeTag>
1676 void
1678 addWellContributions(SparseMatrixAdapter& jacobian) const
1679 {
1680 for ( const auto& well: well_container_ ) {
1681 well->addWellContributions(jacobian);
1682 }
1683 }
1684
1685 template<typename TypeTag>
1686 void
1689 const BVector& weights,
1690 const bool use_well_weights) const
1691 {
1692 int nw = this->numLocalWellsEnd();
1693 int rdofs = local_num_cells_;
1694 for ( int i = 0; i < nw; i++ ) {
1695 int wdof = rdofs + i;
1696 jacobian[wdof][wdof] = 1.0;// better scaling ?
1697 }
1698
1699 for (const auto& well : well_container_) {
1700 well->addWellPressureEquations(jacobian,
1701 weights,
1702 pressureVarIndex,
1703 use_well_weights,
1704 this->wellState());
1705 }
1706 }
1707
1708 template <typename TypeTag>
1710 addReservoirSourceTerms(GlobalEqVector& residual,
1711 const std::vector<typename SparseMatrixAdapter::MatrixBlock*>& diagMatAddress) const
1712 {
1713 // NB this loop may write multiple times to the same element
1714 // if a cell is perforated by more than one well, so it should
1715 // not be OpenMP-parallelized.
1716 for (const auto& well : well_container_) {
1717 if (!well->isOperableAndSolvable() && !well->wellIsStopped()) {
1718 continue;
1719 }
1720 const auto& cells = well->cells();
1721 const auto& rates = well->connectionRates();
1722 for (unsigned perfIdx = 0; perfIdx < rates.size(); ++perfIdx) {
1723 unsigned cellIdx = cells[perfIdx];
1724 auto rate = rates[perfIdx];
1725 rate *= -1.0;
1726 VectorBlockType res(0.0);
1727 using MatrixBlockType = typename SparseMatrixAdapter::MatrixBlock;
1728 MatrixBlockType bMat(0.0);
1729 simulator_.model().linearizer().setResAndJacobi(res, bMat, rate);
1730 residual[cellIdx] += res;
1731 *diagMatAddress[cellIdx] += bMat;
1732 }
1733 }
1734 }
1735
1736
1737 template<typename TypeTag>
1738 void
1741 {
1742 int nw = this->numLocalWellsEnd();
1743 int rdofs = local_num_cells_;
1744 for (int i = 0; i < nw; ++i) {
1745 int wdof = rdofs + i;
1746 jacobian.entry(wdof,wdof) = 1.0;// better scaling ?
1747 }
1748 const auto wellconnections = this->getMaxWellConnections();
1749 for (int i = 0; i < nw; ++i) {
1750 const auto& perfcells = wellconnections[i];
1751 for (int perfcell : perfcells) {
1752 int wdof = rdofs + i;
1753 jacobian.entry(wdof, perfcell) = 0.0;
1754 jacobian.entry(perfcell, wdof) = 0.0;
1755 }
1756 }
1757 }
1758
1759
1760 template<typename TypeTag>
1761 void
1764 {
1765 DeferredLogger local_deferredLogger;
1767 {
1768 for (const auto& well : well_container_) {
1769 const auto& cells = well->cells();
1770 x_local_.resize(cells.size());
1771
1772 for (size_t i = 0; i < cells.size(); ++i) {
1773 x_local_[i] = x[cells[i]];
1774 }
1775 well->recoverWellSolutionAndUpdateWellState(simulator_, x_local_,
1776 this->wellState(), local_deferredLogger);
1777 }
1778 }
1779 OPM_END_PARALLEL_TRY_CATCH_LOG(local_deferredLogger,
1780 "recoverWellSolutionAndUpdateWellState() failed: ",
1781 this->terminal_output_, simulator_.vanguard().grid().comm());
1782 }
1783
1784
1785 template<typename TypeTag>
1786 void
1788 recoverWellSolutionAndUpdateWellStateDomain(const BVector& x, const int domainIdx)
1789 {
1790 if (!nldd_) {
1791 OPM_THROW(std::logic_error, "Attempt to call NLDD method without a NLDD solver");
1792 }
1793
1794 return nldd_->recoverWellSolutionAndUpdateWellState(x, domainIdx);
1795 }
1796
1797
1798 template<typename TypeTag>
1801 getWellConvergence(const std::vector<Scalar>& B_avg, bool checkWellGroupControlsAndNetwork) const
1802 {
1803
1804 DeferredLogger local_deferredLogger;
1805 // Get global (from all processes) convergence report.
1806 ConvergenceReport local_report;
1807 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1808 for (const auto& well : well_container_) {
1809 if (well->isOperableAndSolvable() || well->wellIsStopped()) {
1810 local_report += well->getWellConvergence(
1811 simulator_, this->wellState(), B_avg, local_deferredLogger,
1812 iterationIdx > param_.strict_outer_iter_wells_);
1813 } else {
1814 ConvergenceReport report;
1815 using CR = ConvergenceReport;
1816 report.setWellFailed({CR::WellFailure::Type::Unsolvable, CR::Severity::Normal, -1, well->name()});
1817 local_report += report;
1818 }
1819 }
1820
1821 const Opm::Parallel::Communication comm = grid().comm();
1822 DeferredLogger global_deferredLogger = gatherDeferredLogger(local_deferredLogger, comm);
1823 ConvergenceReport report = gatherConvergenceReport(local_report, comm);
1824
1825 if (checkWellGroupControlsAndNetwork) {
1826 // the well_group_control_changed info is already communicated
1827 report.setWellGroupTargetsViolated(this->lastReport().well_group_control_changed);
1828 report.setNetworkNotYetBalancedForceAnotherNewtonIteration(network_needs_more_balancing_force_another_newton_iteration_);
1829 }
1830
1831 if (this->terminal_output_) {
1832 global_deferredLogger.logMessages();
1833
1834 // Log debug messages for NaN or too large residuals.
1835 for (const auto& f : report.wellFailures()) {
1836 if (f.severity() == ConvergenceReport::Severity::NotANumber) {
1837 OpmLog::debug("NaN residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1838 } else if (f.severity() == ConvergenceReport::Severity::TooLarge) {
1839 OpmLog::debug("Too large residual found with phase " + std::to_string(f.phase()) + " for well " + f.wellName());
1840 }
1841 }
1842 }
1843 return report;
1844 }
1845
1846
1847
1848
1849
1850 template<typename TypeTag>
1851 void
1853 calculateExplicitQuantities(DeferredLogger& deferred_logger) const
1854 {
1855 // TODO: checking isOperableAndSolvable() ?
1856 for (auto& well : well_container_) {
1857 well->calculateExplicitQuantities(simulator_, this->wellState(), deferred_logger);
1858 }
1859 }
1860
1861
1862
1863
1864
1865 template<typename TypeTag>
1866 bool
1868 updateWellControls(DeferredLogger& deferred_logger)
1869 {
1870 OPM_TIMEFUNCTION();
1871 if (!this->wellsActive()) {
1872 return false;
1873 }
1874 const int episodeIdx = simulator_.episodeIndex();
1875 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1876 const auto& comm = simulator_.vanguard().grid().comm();
1877 size_t iter = 0;
1878 bool changed_well_group = false;
1879 const Group& fieldGroup = this->schedule().getGroup("FIELD", episodeIdx);
1880 // Check group individual constraints.
1881 // iterate a few times to make sure all constraints are honored
1882 const std::size_t max_iter = param_.well_group_constraints_max_iterations_;
1883 while(!changed_well_group && iter < max_iter) {
1884 changed_well_group = updateGroupControls(fieldGroup, deferred_logger, episodeIdx, iterationIdx);
1885
1886 // Check wells' group constraints and communicate.
1887 bool changed_well_to_group = false;
1888 {
1889 OPM_TIMEBLOCK(UpdateWellControls);
1890 // For MS Wells a linear solve is performed below and the matrix might be singular.
1891 // We need to communicate the exception thrown to the others and rethrow.
1893 for (const auto& well : well_container_) {
1895 const bool changed_well = well->updateWellControl(
1896 simulator_, mode, this->wgHelper(), this->wellState(), deferred_logger
1897 );
1898 if (changed_well) {
1899 changed_well_to_group = changed_well || changed_well_to_group;
1900 }
1901 }
1902 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1903 simulator_.gridView().comm());
1904 }
1905
1906 changed_well_to_group = comm.sum(static_cast<int>(changed_well_to_group));
1907 if (changed_well_to_group) {
1908 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1909 changed_well_group = true;
1910 }
1911
1912 // Check individual well constraints and communicate.
1913 bool changed_well_individual = false;
1914 {
1915 // For MS Wells a linear solve is performed below and the matrix might be singular.
1916 // We need to communicate the exception thrown to the others and rethrow.
1918 for (const auto& well : well_container_) {
1920 const bool changed_well = well->updateWellControl(
1921 simulator_, mode, this->wgHelper(), this->wellState(), deferred_logger
1922 );
1923 if (changed_well) {
1924 changed_well_individual = changed_well || changed_well_individual;
1925 }
1926 }
1927 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel: updating well controls failed: ",
1928 simulator_.gridView().comm());
1929 }
1930
1931 changed_well_individual = comm.sum(static_cast<int>(changed_well_individual));
1932 if (changed_well_individual) {
1933 updateAndCommunicate(episodeIdx, iterationIdx, deferred_logger);
1934 changed_well_group = true;
1935 }
1936 iter++;
1937 }
1938
1939 // update wsolvent fraction for REIN wells
1940 this->updateWsolvent(fieldGroup, episodeIdx, this->nupcolWellState());
1941
1942 return changed_well_group;
1943 }
1944
1945
1946 template<typename TypeTag>
1947 std::tuple<bool, typename BlackoilWellModel<TypeTag>::Scalar>
1949 updateNetworks(const bool mandatory_network_balance,
1950 DeferredLogger& deferred_logger,
1951 const bool relax_network_tolerance)
1952 {
1953 OPM_TIMEFUNCTION();
1954 const int episodeIdx = simulator_.episodeIndex();
1955 const auto& network = this->schedule()[episodeIdx].network();
1956 if (!this->wellsActive() && !network.active()) {
1957 return {false, 0.0};
1958 }
1959
1960 const int iterationIdx = simulator_.model().newtonMethod().numIterations();
1961 const auto& comm = simulator_.vanguard().grid().comm();
1962
1963 // network related
1964 Scalar network_imbalance = 0.0;
1965 bool more_network_update = false;
1966 if (this->shouldBalanceNetwork(episodeIdx, iterationIdx) || mandatory_network_balance) {
1967 OPM_TIMEBLOCK(BalanceNetwork);
1968 const double dt = this->simulator_.timeStepSize();
1969 // Calculate common THP for subsea manifold well group (item 3 of NODEPROP set to YES)
1970 const bool well_group_thp_updated = computeWellGroupThp(dt, deferred_logger);
1971 const int max_number_of_sub_iterations = param_.network_max_sub_iterations_;
1972 const Scalar network_pressure_update_damping_factor = param_.network_pressure_update_damping_factor_;
1973 const Scalar network_max_pressure_update = param_.network_max_pressure_update_in_bars_ * unit::barsa;
1974 bool more_network_sub_update = false;
1975 for (int i = 0; i < max_number_of_sub_iterations; i++) {
1976 const auto local_network_imbalance = this->updateNetworkPressures(episodeIdx, network_pressure_update_damping_factor, network_max_pressure_update);
1977 network_imbalance = comm.max(local_network_imbalance);
1978 const auto& balance = this->schedule()[episodeIdx].network_balance();
1979 constexpr Scalar relaxation_factor = 10.0;
1980 const Scalar tolerance = relax_network_tolerance ? relaxation_factor * balance.pressure_tolerance() : balance.pressure_tolerance();
1981 more_network_sub_update = this->networkActive() && network_imbalance > tolerance;
1982 if (!more_network_sub_update)
1983 break;
1984
1985 for (const auto& well : well_container_) {
1986 if (well->isInjector() || !well->wellEcl().predictionMode())
1987 continue;
1988
1989 const auto it = this->node_pressures_.find(well->wellEcl().groupName());
1990 if (it != this->node_pressures_.end()) {
1991 well->prepareWellBeforeAssembling(this->simulator_, dt, this->wgHelper(), this->wellState(), deferred_logger);
1992 }
1993 }
1994 this->updateAndCommunicateGroupData(episodeIdx, iterationIdx, param_.nupcol_group_rate_tolerance_,
1995 /*update_wellgrouptarget*/ true, deferred_logger);
1996 }
1997 more_network_update = more_network_sub_update || well_group_thp_updated;
1998 }
1999 return { more_network_update, network_imbalance };
2000 }
2001
2002
2003 template<typename TypeTag>
2004 void
2006 updateAndCommunicate(const int reportStepIdx,
2007 const int iterationIdx,
2008 DeferredLogger& deferred_logger)
2009 {
2010 this->updateAndCommunicateGroupData(reportStepIdx,
2011 iterationIdx,
2012 param_.nupcol_group_rate_tolerance_,
2013 /*update_wellgrouptarget*/ true,
2014 deferred_logger);
2015
2016 // updateWellStateWithTarget might throw for multisegment wells hence we
2017 // have a parallel try catch here to thrown on all processes.
2019 // if a well or group change control it affects all wells that are under the same group
2020 for (const auto& well : well_container_) {
2021 // We only want to update wells under group-control here
2022 const auto& ws = this->wellState().well(well->indexOfWell());
2023 if (ws.production_cmode == Well::ProducerCMode::GRUP ||
2024 ws.injection_cmode == Well::InjectorCMode::GRUP)
2025 {
2026 well->updateWellStateWithTarget(
2027 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2028 );
2029 }
2030 }
2031 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAndCommunicate failed: ",
2032 simulator_.gridView().comm())
2033 this->updateAndCommunicateGroupData(reportStepIdx,
2034 iterationIdx,
2035 param_.nupcol_group_rate_tolerance_,
2036 /*update_wellgrouptarget*/ true,
2037 deferred_logger);
2038 }
2039
2040 template<typename TypeTag>
2041 bool
2043 updateGroupControls(const Group& group,
2044 DeferredLogger& deferred_logger,
2045 const int reportStepIdx,
2046 const int iterationIdx)
2047 {
2048 OPM_TIMEFUNCTION();
2049 bool changed = false;
2050 // restrict the number of group switches but only after nupcol iterations.
2051 const int nupcol = this->schedule()[reportStepIdx].nupcol();
2052 const int max_number_of_group_switches = param_.max_number_of_group_switches_;
2053 const bool update_group_switching_log = iterationIdx >= nupcol;
2054 const bool changed_hc = this->checkGroupHigherConstraints(group, deferred_logger, reportStepIdx, max_number_of_group_switches, update_group_switching_log);
2055 if (changed_hc) {
2056 changed = true;
2057 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2058 }
2059
2060 bool changed_individual =
2062 updateGroupIndividualControl(group,
2063 reportStepIdx,
2064 max_number_of_group_switches,
2065 update_group_switching_log,
2066 this->switched_inj_groups_,
2067 this->switched_prod_groups_,
2068 this->closed_offending_wells_,
2069 this->groupState(),
2070 this->wellState(),
2071 deferred_logger);
2072
2073 if (changed_individual) {
2074 changed = true;
2075 updateAndCommunicate(reportStepIdx, iterationIdx, deferred_logger);
2076 }
2077 // call recursively down the group hierarchy
2078 for (const std::string& groupName : group.groups()) {
2079 bool changed_this = updateGroupControls( this->schedule().getGroup(groupName, reportStepIdx), deferred_logger, reportStepIdx,iterationIdx);
2080 changed = changed || changed_this;
2081 }
2082 return changed;
2083 }
2084
2085 template<typename TypeTag>
2086 void
2088 updateWellTestState(const double simulationTime, WellTestState& wellTestState)
2089 {
2090 OPM_TIMEFUNCTION();
2091 DeferredLogger local_deferredLogger;
2092 for (const auto& well : well_container_) {
2093 const auto& wname = well->name();
2094 const auto wasClosed = wellTestState.well_is_closed(wname);
2095 well->checkWellOperability(simulator_,
2096 this->wellState(),
2097 this->wgHelper(),
2098 local_deferredLogger);
2099 const bool under_zero_target =
2100 well->wellUnderZeroGroupRateTarget(this->simulator_,
2101 this->wellState(),
2102 local_deferredLogger);
2103 well->updateWellTestState(this->wellState().well(wname),
2104 simulationTime,
2105 /*writeMessageToOPMLog=*/ true,
2106 under_zero_target,
2107 wellTestState,
2108 local_deferredLogger);
2109
2110 if (!wasClosed && wellTestState.well_is_closed(wname)) {
2111 this->closed_this_step_.insert(wname);
2112
2113 // maybe open a new well
2114 const WellEconProductionLimits& econ_production_limits = well->wellEcl().getEconLimits();
2115 if (econ_production_limits.validFollowonWell()) {
2116 const auto episode_idx = simulator_.episodeIndex();
2117 const auto follow_on_well = econ_production_limits.followonWell();
2118 if (!this->schedule().hasWell(follow_on_well, episode_idx)) {
2119 const auto msg = fmt::format("Well {} was closed. But the given follow on well {} does not exist."
2120 "The simulator continues without opening a follow on well.",
2121 wname, follow_on_well);
2122 local_deferredLogger.warning(msg);
2123 }
2124 auto& ws = this->wellState().well(follow_on_well);
2125 const bool success = ws.updateStatus(WellStatus::OPEN);
2126 if (success) {
2127 const auto msg = fmt::format("Well {} was closed. The follow on well {} opens instead.", wname, follow_on_well);
2128 local_deferredLogger.info(msg);
2129 } else {
2130 const auto msg = fmt::format("Well {} was closed. The follow on well {} is already open.", wname, follow_on_well);
2131 local_deferredLogger.warning(msg);
2132 }
2133 }
2134
2135 }
2136 }
2137
2138 for (const auto& [group_name, to] : this->closed_offending_wells_) {
2139 if (this->hasOpenLocalWell(to.second) &&
2140 !this->wasDynamicallyShutThisTimeStep(to.second))
2141 {
2142 wellTestState.close_well(to.second,
2143 WellTestConfig::Reason::GROUP,
2144 simulationTime);
2145 this->updateClosedWellsThisStep(to.second);
2146 const std::string msg =
2147 fmt::format("Procedure on exceeding {} limit is WELL for group {}. "
2148 "Well {} is {}.",
2149 to.first,
2150 group_name,
2151 to.second,
2152 "shut");
2153 local_deferredLogger.info(msg);
2154 }
2155 }
2156
2157 const Opm::Parallel::Communication comm = grid().comm();
2158 DeferredLogger global_deferredLogger =
2159 gatherDeferredLogger(local_deferredLogger, comm);
2160
2161 if (this->terminal_output_) {
2162 global_deferredLogger.logMessages();
2163 }
2164 }
2165
2166
2167 template<typename TypeTag>
2168 void
2170 const WellState<Scalar, IndexTraits>& well_state_copy,
2171 std::string& exc_msg,
2172 ExceptionType::ExcEnum& exc_type,
2173 DeferredLogger& deferred_logger)
2174 {
2175 OPM_TIMEFUNCTION();
2176 const int np = this->numPhases();
2177 std::vector<Scalar> potentials;
2178 const auto& well = well_container_[widx];
2179 std::string cur_exc_msg;
2180 auto cur_exc_type = ExceptionType::NONE;
2181 try {
2182 well->computeWellPotentials(simulator_, well_state_copy, this->wgHelper(), potentials, deferred_logger);
2183 }
2184 // catch all possible exception and store type and message.
2185 OPM_PARALLEL_CATCH_CLAUSE(cur_exc_type, cur_exc_msg);
2186 if (cur_exc_type != ExceptionType::NONE) {
2187 exc_msg += fmt::format("\nFor well {}: {}", well->name(), cur_exc_msg);
2188 }
2189 exc_type = std::max(exc_type, cur_exc_type);
2190 // Store it in the well state
2191 // potentials is resized and set to zero in the beginning of well->ComputeWellPotentials
2192 // and updated only if sucessfull. i.e. the potentials are zero for exceptions
2193 auto& ws = this->wellState().well(well->indexOfWell());
2194 for (int p = 0; p < np; ++p) {
2195 // make sure the potentials are positive
2196 ws.well_potentials[p] = std::max(Scalar{0.0}, potentials[p]);
2197 }
2198 }
2199
2200
2201
2202 template <typename TypeTag>
2203 void
2206 {
2207 for (const auto& wellPtr : this->well_container_) {
2208 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2209 }
2210 }
2211
2212
2213
2214
2215
2216 template <typename TypeTag>
2217 void
2219 calculateProductivityIndexValuesShutWells(const int reportStepIdx,
2220 DeferredLogger& deferred_logger)
2221 {
2222 // For the purpose of computing PI/II values, it is sufficient to
2223 // construct StandardWell instances only. We don't need to form
2224 // well objects that honour the 'isMultisegment()' flag of the
2225 // corresponding "this->wells_ecl_[shutWell]".
2226
2227 for (const auto& shutWell : this->local_shut_wells_) {
2228 if (!this->wells_ecl_[shutWell].hasConnections()) {
2229 // No connections in this well. Nothing to do.
2230 continue;
2231 }
2232
2233 auto wellPtr = this->template createTypedWellPointer
2234 <StandardWell<TypeTag>>(shutWell, reportStepIdx);
2235
2236 wellPtr->init(this->depth_, this->gravity_, this->B_avg_, true);
2237
2238 this->calculateProductivityIndexValues(wellPtr.get(), deferred_logger);
2239 }
2240 }
2241
2242
2243
2244
2245
2246 template <typename TypeTag>
2247 void
2250 DeferredLogger& deferred_logger)
2251 {
2252 wellPtr->updateProductivityIndex(this->simulator_,
2253 this->prod_index_calc_[wellPtr->indexOfWell()],
2254 this->wellState(),
2255 deferred_logger);
2256 }
2257
2258
2259
2260 template<typename TypeTag>
2261 void
2263 prepareTimeStep(DeferredLogger& deferred_logger)
2264 {
2265 // Check if there is a network with active prediction wells at this time step.
2266 const auto episodeIdx = simulator_.episodeIndex();
2267 this->updateNetworkActiveState(episodeIdx);
2268
2269 // Rebalance the network initially if any wells in the network have status changes
2270 // (Need to check this before clearing events)
2271 const bool do_prestep_network_rebalance = param_.pre_solve_network_ && this->needPreStepNetworkRebalance(episodeIdx);
2272
2273 for (const auto& well : well_container_) {
2274 auto& events = this->wellState().well(well->indexOfWell()).events;
2275 if (events.hasEvent(WellState<Scalar, IndexTraits>::event_mask)) {
2276 well->updateWellStateWithTarget(
2277 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2278 );
2279 well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger);
2280 // There is no new well control change input within a report step,
2281 // so next time step, the well does not consider to have effective events anymore.
2283 }
2284 // these events only work for the first time step within the report step
2285 if (events.hasEvent(ScheduleEvents::REQUEST_OPEN_WELL)) {
2286 events.clearEvent(ScheduleEvents::REQUEST_OPEN_WELL);
2287 }
2288 // solve the well equation initially to improve the initial solution of the well model
2289 if (param_.solve_welleq_initially_ && well->isOperableAndSolvable()) {
2290 try {
2291 well->solveWellEquation(
2292 simulator_, this->wgHelper(), this->wellState(), deferred_logger
2293 );
2294 } catch (const std::exception& e) {
2295 const std::string msg = "Compute initial well solution for " + well->name() + " initially failed. Continue with the previous rates";
2296 deferred_logger.warning("WELL_INITIAL_SOLVE_FAILED", msg);
2297 }
2298 }
2299 // If we're using local well solves that include control switches, they also update
2300 // operability, so reset before main iterations begin
2301 well->resetWellOperability();
2302 }
2303 updatePrimaryVariables(deferred_logger);
2304
2305 // Actually do the pre-step network rebalance, using the updated well states and initial solutions
2306 if (do_prestep_network_rebalance) doPreStepNetworkRebalance(deferred_logger);
2307 }
2308
2309 template<typename TypeTag>
2310 void
2313 {
2314 std::vector< Scalar > B_avg(numConservationQuantities(), Scalar() );
2315 const auto& grid = simulator_.vanguard().grid();
2316 const auto& gridView = grid.leafGridView();
2317 ElementContext elemCtx(simulator_);
2318
2320 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
2321 elemCtx.updatePrimaryStencil(elem);
2322 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
2323
2324 const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
2325 const auto& fs = intQuants.fluidState();
2326
2327 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx)
2328 {
2329 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2330 continue;
2331 }
2332
2333 const unsigned compIdx = FluidSystem::canonicalToActiveCompIdx(FluidSystem::solventComponentIndex(phaseIdx));
2334 auto& B = B_avg[ compIdx ];
2335
2336 B += 1 / fs.invB(phaseIdx).value();
2337 }
2338 if constexpr (has_solvent_) {
2339 auto& B = B_avg[solventSaturationIdx];
2340 B += 1 / intQuants.solventInverseFormationVolumeFactor().value();
2341 }
2342 }
2343 OPM_END_PARALLEL_TRY_CATCH("BlackoilWellModel::updateAverageFormationFactor() failed: ", grid.comm())
2344
2345 // compute global average
2346 grid.comm().sum(B_avg.data(), B_avg.size());
2347 B_avg_.resize(B_avg.size());
2348 std::transform(B_avg.begin(), B_avg.end(), B_avg_.begin(),
2349 [gcells = global_num_cells_](const auto bval)
2350 { return bval / gcells; });
2351 }
2352
2353
2354
2355
2356
2357 template<typename TypeTag>
2358 void
2361 {
2362 for (const auto& well : well_container_) {
2363 well->updatePrimaryVariables(simulator_, this->wellState(), deferred_logger);
2364 }
2365 }
2366
2367 template<typename TypeTag>
2368 void
2370 {
2371 const auto& grid = simulator_.vanguard().grid();
2372 const auto& eclProblem = simulator_.problem();
2373 const unsigned numCells = grid.size(/*codim=*/0);
2374
2375 this->pvt_region_idx_.resize(numCells);
2376 for (unsigned cellIdx = 0; cellIdx < numCells; ++cellIdx) {
2377 this->pvt_region_idx_[cellIdx] =
2378 eclProblem.pvtRegionIndex(cellIdx);
2379 }
2380 }
2381
2382 // The number of components in the model.
2383 template<typename TypeTag>
2384 int
2386 {
2387 // The numPhases() functions returns 1-3, depending on which
2388 // of the (oil, water, gas) phases are active. For each of those phases,
2389 // if the phase is active the corresponding component is present and
2390 // conserved.
2391 // Apart from (oil, water, gas), in the current well model only solvent
2392 // is explicitly modelled as a conserved quantity (polymer, energy, salt
2393 // etc. are not), unlike the reservoir part where all such quantities are
2394 // conserved. This function must therefore be updated when/if we add
2395 // more conserved quantities in the well model.
2396 return this->numPhases() + has_solvent_;
2397 }
2398
2399 template<typename TypeTag>
2400 void
2402 {
2403 const auto& eclProblem = simulator_.problem();
2404 depth_.resize(local_num_cells_);
2405 for (unsigned cellIdx = 0; cellIdx < local_num_cells_; ++cellIdx) {
2406 depth_[cellIdx] = eclProblem.dofCenterDepth(cellIdx);
2407 }
2408 }
2409
2410 template<typename TypeTag>
2413 getWell(const std::string& well_name) const
2414 {
2415 // finding the iterator of the well in wells_ecl
2416 auto well = std::find_if(well_container_.begin(),
2417 well_container_.end(),
2418 [&well_name](const WellInterfacePtr& elem)->bool {
2419 return elem->name() == well_name;
2420 });
2421
2422 assert(well != well_container_.end());
2423
2424 return **well;
2425 }
2426
2427 template <typename TypeTag>
2428 int
2430 reportStepIndex() const
2431 {
2432 return std::max(this->simulator_.episodeIndex(), 0);
2433 }
2434
2435
2436
2437
2438
2439 template<typename TypeTag>
2440 void
2442 calcResvCoeff(const int fipnum,
2443 const int pvtreg,
2444 const std::vector<Scalar>& production_rates,
2445 std::vector<Scalar>& resv_coeff)
2446 {
2447 rateConverter_->calcCoeff(fipnum, pvtreg, production_rates, resv_coeff);
2448 }
2449
2450 template<typename TypeTag>
2451 void
2453 calcInjResvCoeff(const int fipnum,
2454 const int pvtreg,
2455 std::vector<Scalar>& resv_coeff)
2456 {
2457 rateConverter_->calcInjCoeff(fipnum, pvtreg, resv_coeff);
2458 }
2459
2460
2461 template <typename TypeTag>
2462 void
2465 {
2466 if constexpr (has_energy_) {
2467 int np = this->numPhases();
2468 Scalar cellInternalEnergy;
2469 Scalar cellBinv;
2470 Scalar cellDensity;
2471 Scalar perfPhaseRate;
2472 const int nw = this->numLocalWells();
2473 for (auto wellID = 0*nw; wellID < nw; ++wellID) {
2474 const Well& well = this->wells_ecl_[wellID];
2475 auto& ws = this->wellState().well(wellID);
2476 if (well.isInjector()) {
2477 if (ws.status != WellStatus::STOP) {
2478 this->wellState().well(wellID).temperature = well.inj_temperature();
2479 continue;
2480 }
2481 }
2482
2483 std::array<Scalar,2> weighted{0.0,0.0};
2484 auto& [weighted_temperature, total_weight] = weighted;
2485
2486 auto& well_info = this->local_parallel_well_info_[wellID].get();
2487 auto& perf_data = ws.perf_data;
2488 auto& perf_phase_rate = perf_data.phase_rates;
2489
2490 using int_type = decltype(this->well_perf_data_[wellID].size());
2491 for (int_type perf = 0, end_perf = this->well_perf_data_[wellID].size(); perf < end_perf; ++perf) {
2492 const int cell_idx = this->well_perf_data_[wellID][perf].cell_index;
2493 const auto& intQuants = simulator_.model().intensiveQuantities(cell_idx, /*timeIdx=*/0);
2494 const auto& fs = intQuants.fluidState();
2495
2496 // we on only have one temperature pr cell any phaseIdx will do
2497 Scalar cellTemperatures = fs.temperature(/*phaseIdx*/0).value();
2498
2499 Scalar weight_factor = 0.0;
2500 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
2501 if (!FluidSystem::phaseIsActive(phaseIdx)) {
2502 continue;
2503 }
2504 cellInternalEnergy = fs.enthalpy(phaseIdx).value() -
2505 fs.pressure(phaseIdx).value() / fs.density(phaseIdx).value();
2506 cellBinv = fs.invB(phaseIdx).value();
2507 cellDensity = fs.density(phaseIdx).value();
2508 perfPhaseRate = perf_phase_rate[perf*np + phaseIdx];
2509 weight_factor += cellDensity * perfPhaseRate / cellBinv * cellInternalEnergy / cellTemperatures;
2510 }
2511 weight_factor = std::abs(weight_factor) + 1e-13;
2512 total_weight += weight_factor;
2513 weighted_temperature += weight_factor * cellTemperatures;
2514 }
2515 well_info.communication().sum(weighted.data(), 2);
2516 this->wellState().well(wellID).temperature = weighted_temperature / total_weight;
2517 }
2518 }
2519 }
2520
2521
2522 template <typename TypeTag>
2524 assignWellTracerRates(data::Wells& wsrpt) const
2525 {
2526 const auto reportStepIdx = static_cast<unsigned int>(this->reportStepIndex());
2527 const auto& trMod = this->simulator_.problem().tracerModel();
2528
2529 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellTracerRates(), reportStepIdx);
2530 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellFreeTracerRates(), reportStepIdx);
2531 BlackoilWellModelGeneric<Scalar, IndexTraits>::assignWellTracerRates(wsrpt, trMod.getWellSolTracerRates(), reportStepIdx);
2532
2533 this->assignMswTracerRates(wsrpt, trMod.getMswTracerRates(), reportStepIdx);
2534 }
2535
2536} // namespace Opm
2537
2538#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:379
std::vector< Scalar > depth_
Definition: BlackoilWellModel.hpp:458
std::size_t global_num_cells_
Definition: BlackoilWellModel.hpp:454
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:133
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:376
const SimulatorReportSingle & lastReport() const
Definition: BlackoilWellModel_impl.hpp:685
void addWellContributions(SparseMatrixAdapter &jacobian) const
Definition: BlackoilWellModel_impl.hpp:1678
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:298
bool empty() const
Definition: BlackoilWellModel.hpp:343
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:1853
void updatePrimaryVariables(DeferredLogger &deferred_logger)
Definition: BlackoilWellModel_impl.hpp:2360
void initializeLocalWellStructure(const int reportStepIdx, const bool enableWellPIScaling)
Definition: BlackoilWellModel_impl.hpp:252
Dune::BlockVector< VectorBlockType > BVector
Definition: BlackoilWellModel.hpp:134
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:456
bool alternative_well_rate_init_
Definition: BlackoilWellModel.hpp:459
void timeStepSucceeded(const double simulationTime, const double dt)
Definition: BlackoilWellModel_impl.hpp:695
Simulator & simulator_
Definition: BlackoilWellModel.hpp:428
void createWellContainer(const int report_step) override
Definition: BlackoilWellModel_impl.hpp:852
std::unique_ptr< WellInterface< TypeTag > > WellInterfacePtr
Definition: BlackoilWellModel.hpp:191
int compressedIndexForInterior(int cartesian_cell_idx) const override
get compressed index for interior cells (-1, otherwise
Definition: BlackoilWellModel.hpp:351
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