BlackoilWellModelRescoup_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_BLACKOILWELLMODEL_RESCOUP_IMPL_HEADER_INCLUDED
21#define OPM_BLACKOILWELLMODEL_RESCOUP_IMPL_HEADER_INCLUDED
22
23// Improve IDE experience
24#ifndef OPM_BLACKOILWELLMODEL_RESCOUP_HEADER_INCLUDED
25#include <config.h>
27#endif
28
29#ifdef RESERVOIR_COUPLING_ENABLED
30
31#include <opm/common/TimingMacros.hpp>
32
33#include <opm/input/eclipse/EclipseState/Grid/RegionSetMatcher.hpp>
34#include <opm/input/eclipse/Schedule/ResCoup/ReservoirCouplingInfo.hpp>
35#include <opm/input/eclipse/Schedule/Schedule.hpp>
36#include <opm/input/eclipse/Schedule/SummaryState.hpp>
37#include <opm/input/eclipse/Schedule/UDQ/UDQConfig.hpp>
38#include <opm/input/eclipse/Schedule/UDQ/UDQDefine.hpp>
39#include <opm/input/eclipse/Schedule/UDQ/UDQEnums.hpp>
40#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
41#include <opm/input/eclipse/Units/UnitSystem.hpp>
42
50
51#include <algorithm>
52#include <array>
53#include <cassert>
54#include <cstddef>
55#include <memory>
56#include <optional>
57#include <string>
58#include <tuple>
59
60namespace Opm {
61
62// Constructor
63// -----------
64template<typename TypeTag>
65BlackoilWellModelRescoup<TypeTag>::
66BlackoilWellModelRescoup(BlackoilWellModel<TypeTag>& well_model)
67 : well_model_{well_model}
68 , network_{well_model.network()}
69 , simulator_{well_model.simulator()}
70 , param_{well_model.param()}
71{}
72
73// Public methods alphabetically
74// ------------------------------
75
76template<typename TypeTag>
77void
78BlackoilWellModelRescoup<TypeTag>::
79evalGroupAndFieldUDQs()
80{
81 const int reportStepIdx = this->groupStateHelper().reportStepIdx();
82 auto& deferred_logger = this->groupStateHelper().deferredLogger();
83 // UDQs are normally evaluated at the end of a time step. A slave UDQ
84 // that depends on a target imposed by the master would then lag one
85 // step behind, so re-evaluate the group and field level UDQs now that
86 // this step's targets have arrived. Well and segment level UDQs are
87 // left alone: their inputs are not available yet. So is any
88 // "UPDATE NEXT" DEFINE: it is a one-shot that the ordinary end-of-step
89 // evaluation is meant to consume, and this extra evaluation must not
90 // use it up early.
91 const auto select = [](const UDQDefine& def) {
92 const auto var_type = def.var_type();
93 return ((var_type == UDQVarType::GROUP_VAR) || (var_type == UDQVarType::FIELD_VAR))
94 && (def.status().first != UDQUpdate::NEXT);
95 };
96 const auto& schedule = this->schedule();
98 {
99 schedule[reportStepIdx].udq().eval(
100 reportStepIdx,
101 schedule.wellMatcher(reportStepIdx),
102 schedule[reportStepIdx].group_order(),
103 schedule.segmentMatcherFactory(reportStepIdx),
104 [es = std::cref(this->simulator_.vanguard().eclState())]() {
105 return std::make_unique<RegionSetMatcher>(es.get().fipRegionStatistics());
106 },
107 this->simulator_.vanguard().summaryState(),
108 this->simulator_.vanguard().udqState(),
109 select);
110 }
111 OPM_END_PARALLEL_TRY_CATCH_LOG(deferred_logger,
112 "Failed to evaluate UDQs for reservoir coupling slave: ",
113 this->well_model_.terminalOutput(),
114 this->simulator_.vanguard().grid().comm())
115}
116
117template<typename TypeTag>
118bool
119BlackoilWellModelRescoup<TypeTag>::
120masterIsInCoupledNetworkIteration() const
121{
122 return this->isReservoirCouplingMaster()
123 && this->reservoirCouplingMaster().isFirstSubstepOfSyncTimestep()
124 && this->masterNetworkHasMasterGroupLeaves()
125 && !this->lastSentMasterGroupNodePressuresIsFinal();
126}
127
128
129template<typename TypeTag>
130bool
131BlackoilWellModelRescoup<TypeTag>::
132masterNetworkHasMasterGroupLeaves() const
133{
134 // Query the parsed Schedule topology (`network.has_node(...)`) rather than the runtime
135 // `node_pressures_` map. The runtime map is empty on the very first beginTimeStep call
136 // (network_.update has not yet run for this substep).
137 if (!this->isReservoirCouplingMaster()) return false;
138 const auto& rcm = this->reservoirCouplingMaster();
139 const auto num_slaves = rcm.numSlaves();
140 for (std::size_t s = 0; s < num_slaves; ++s) {
141 if (!rcm.slaveIsCoupled(s)) continue;
142 if (this->masterNetworkHasMasterGroupLeavesForSlave_(s)) {
143 return true;
144 }
145 }
146 return false;
147}
148
149template<typename TypeTag>
150void
151BlackoilWellModelRescoup<TypeTag>::
152maybeExchangeNetworkOuterIterationWithSlaves(bool more_network_update)
153{
154 if (!this->masterIsInCoupledNetworkIteration()) {
155 return;
156 }
157 const bool is_final = !more_network_update;
158 // In tight coupling the inner sub-loop (maybeExchangeNetworkSubIterationWithSlaves)
159 // performs every non-final exchange, so here we only emit the single
160 // terminating (is_final) message -- the non-final case would just resend the
161 // same node pressures the last sub-iteration already shipped, and get the same
162 // rates back. The retained final send still covers the case where the network
163 // is not balanced this iteration (the inner loop never runs). In loose coupling
164 // this per-outer send is the sole master<->slave coupling and runs every outer
165 // iteration.
166 if (is_final || !this->well_model_.useTightRcNetworkCoupling()) {
167 // send the pressures freshly computed by network_.update() to all activated slaves
168 this->sendMasterGroupNodePressuresToSlaves(is_final);
169 if (!is_final) {
170 // receive slaves' updated network_surface_rates for the next outer iteration.
171 this->receiveSlaveGroupData();
172 this->refreshAndSendGroupConstraints();
173 }
174 }
175}
176
177template<typename TypeTag>
178void
179BlackoilWellModelRescoup<TypeTag>::
180maybeExchangeNetworkSubIterationWithSlaves()
181{
182 // Called for rescoup master for the tight master-slave coupling (--rc-network-loose-coupling=false)
183 // mode at the sub-iteration level: Send node pressures and receive slave rates per master inner
184 // network sub-iteration.
185 // In the loose mode (--rc-network-loose-coupling=true) the exchange happens only once
186 // per master outer iteration (see updateWellControlsAndNetworkIteration), so
187 // the inner loop converges the node pressures against a frozen slave rate.
188 // This loose mode is faster but has been observed in some cases to overshoot to a value that
189 // incorrectly shuts the slave wells in.
190 // To avoid this, the tight coupling is therefore the default coupling.
191 //
192 // NOTE: This is always a non-final exchange. The inner sub-iteration loop cannot
193 // know whether the master's outer network loop will iterate again (the inner
194 // loop ending on max_sub_iter, or on a THP update, still leaves
195 // more_inner_network_update true), so it must not declare termination here.
196 // The single is_final = true send is owned by the per-outer send in
197 // updateWellControlsAndNetworkIteration() (when the outer loop converges)
198 // and by sendSlaveNetworkLoopTerminationSignal_() (when the outer loop hits
199 // max_iter).
200 if (!this->well_model_.useTightRcNetworkCoupling()) {
201 return;
202 }
203 if (!this->masterIsInCoupledNetworkIteration()) {
204 return;
205 }
206 this->sendMasterGroupNodePressuresToSlaves(/*is_final=*/false);
207 this->receiveSlaveGroupData();
208 this->refreshAndSendGroupConstraints();
209}
210
211template<typename TypeTag>
212void
213BlackoilWellModelRescoup<TypeTag>::
214receiveCoupledNetworkActiveStatus()
215{
216 OPM_TIMEFUNCTION();
217 assert(this->isReservoirCouplingSlave());
218 this->reservoirCouplingSlave().receiveCoupledNetworkActiveStatusFromMaster();
219 if (!this->reservoirCouplingSlave().connectedToMasterCoupledNetwork()) {
220 // No master pressures this sync step: the node pressures are the slave's own again.
221 this->network_.setNodePressureOverrides({});
222 for (auto& well : this->wellContainer()) {
223 well->setLogAllControlSwitches(false);
224 }
225 }
226}
227
228template<typename TypeTag>
229void
230BlackoilWellModelRescoup<TypeTag>::
231receiveGroupConstraintsFromMaster()
232{
233 OPM_TIMEFUNCTION();
234 RescoupReceiveGroupConstraints<Scalar, IndexTraits> constraint_receiver{
235 this->well_model_.guideRateHandler(),
236 this->groupStateHelper()
237 };
238 constraint_receiver.receiveGroupConstraintsFromMaster();
239}
240
241template<typename TypeTag>
242void
243BlackoilWellModelRescoup<TypeTag>::
244receiveMasterGroupNodePressuresFromMaster()
245{
246 OPM_TIMEFUNCTION();
247 assert(this->isReservoirCouplingSlave());
248 auto& rescoup_slave = this->reservoirCouplingSlave();
249 const auto [num_pressures, is_final] =
250 rescoup_slave.receiveNumMasterGroupNodePressuresFromMaster();
251 if (num_pressures > 0) {
252 rescoup_slave.receiveMasterGroupNodePressuresFromMaster(num_pressures);
253 }
254 // Apply pressures as dynamic THP limits on every producer whose group
255 // has a master-supplied pressure *and* whose group the slave's own deck
256 // placed in its own surface network as a fixed-pressure node. Wells in
257 // master groups that are not network leaves are not touched either (no
258 // entry in the map). Mirrors the standard local-network apply pattern in
259 // BlackoilWellModelNetworkGeneric::updatePressures(): when the well is
260 // currently THP-controlled, also write the new THP into the WellState
261 // directly, because setDynamicThpLimit() alone leaves the active
262 // control's THP value stale and the subsequent well-solve would
263 // converge against the old THP.
264 //
265 // The node-pressure exchange is a boundary condition between two
266 // networks: the master's network ends at the master groups, and the
267 // pressure it computes there is the fixed pressure of the corresponding
268 // slave group's node in the *slave's* network. A slave that declares no
269 // network has no such node, and its wells keep the THP limits from their
270 // own WCONPROD records -- so the messages are still received (the
271 // protocol is unchanged) but nothing is imposed.
272 //
273 // The pressure is also the fixed pressure of the node in the slave's own network (see
274 // BlackoilWellModelNetworkGeneric::setNodePressureOverrides()), so that the pressures of
275 // the nodes below it, which are the THP limits of the wells in groups below the node, are
276 // the master's pressure plus the pressure drops in the slave's branches. Without it the
277 // slave's own network update, which follows in every iteration, sets the THP limits of
278 // the wells from the node's own fixed pressure, and the well control update (before it)
279 // and the well solve (after it) would see different limits. The direct write below only
280 // reaches wells in a group that is the node itself.
281 const auto& pressures = rescoup_slave.masterGroupNodePressures();
282 std::map<std::string, Scalar> overrides;
283 for (const auto& [group_name, pressure] : pressures) {
284 if (this->slaveGroupIsFixedPressureNodeInOwnNetwork_(group_name)) {
285 overrides[group_name] = pressure;
286 }
287 }
288 this->network_.setNodePressureOverrides(overrides);
289 // A well whose THP limit comes from the master's pressure, in the node itself or in a node
290 // below it, can sit on the boundary between its group target and that limit: on THP
291 // control its rate exceeds the target, on GRUP the THP it needs is below the limit, and
292 // it switches between the two in every exchange while the master's pressure follows the
293 // rates. Flow's limit on the number of well
294 // switches ends this, but only counts switches after NUPCOL iterations, which the
295 // exchanges (made before the Newton iterations) never reach. Count the switches
296 // during the exchange so that such a well is kept on THP control.
297 for (auto& well : this->wellContainer()) {
298 well->setLogAllControlSwitches(false);
299 }
300 if (pressures.empty()) return;
301 const auto& summary_state = this->well_model_.summaryState();
302 auto& well_state = this->wellState();
303 for (auto& well : this->wellContainer()) {
304 if (!well->isProducer() || !well->wellEcl().predictionMode()) continue;
305 const auto& group_name = well->wellEcl().groupName();
306 if (this->wellIsBelowOverriddenNode_(group_name, overrides)) {
307 well->setLogAllControlSwitches(!is_final);
308 }
309 const auto it = pressures.find(group_name);
310 if (it == pressures.end()) continue;
311 if (!this->slaveGroupIsFixedPressureNodeInOwnNetwork_(group_name)) continue;
312 well->setDynamicThpLimit(it->second);
313 auto& ws = well_state[well->indexOfWell()];
314 // The exchange is made in the first substep of a sync step only. The well objects are
315 // rebuilt at the start of every substep, and a limit that is not in the well state is
316 // lost then (see BlackoilWellModelNetworkGeneric::initializeWell()), so the later
317 // substeps would solve the wells against the limit of their own deck.
318 ws.network_thp_limit = it->second;
319 if (ws.production_cmode == Well::ProducerCMode::THP) {
320 ws.thp = well->getTHPConstraint(summary_state);
321 }
322 }
323}
324
325template<typename TypeTag>
326void
327BlackoilWellModelRescoup<TypeTag>::
328receiveRefreshedGroupConstraintsFromMaster()
329{
330 // The counterpart of refreshAndSendGroupConstraints() on the master. The
331 // constraints received replace the ones in force, so the slave group
332 // control modes, the targets and limits in force and the UDQs built on
333 // them follow suit.
334 this->receiveGroupConstraintsFromMaster();
335 this->groupStateHelper().updateSlaveGroupCmodesFromMaster();
336 this->refreshSlaveGroupTargets();
337}
338
339template<typename TypeTag>
340void
341BlackoilWellModelRescoup<TypeTag>::
342receiveSlaveGroupData()
343{
344 OPM_TIMEFUNCTION();
345 assert(this->isReservoirCouplingMaster());
346 RescoupReceiveSlaveGroupData<Scalar, IndexTraits> slave_group_data_receiver{
347 this->groupStateHelper(),
348 };
349 slave_group_data_receiver.receiveSlaveGroupData();
350}
351
352template<typename TypeTag>
353void
354BlackoilWellModelRescoup<TypeTag>::
355refreshAndSendGroupConstraints()
356{
357 // Called right after a receiveSlaveGroupData() within a sync step: after
358 // the slaves' initial well solve at the start of the step, and inside the
359 // network iteration. Fold the rates just received into the master's group
360 // state and ship constraints recomputed from them to the slaves, replacing
361 // the ones they are currently holding. The constraints sent first in the
362 // sync step are computed from the slave rates before the slaves solved
363 // their wells, which lag behind in two ways:
364 // - a group whose producers are not under group control has its current
365 // rate subtracted from the parent's target (see
366 // RescoupConstraintsCalculator::excludeAndRedistributeProductionTargets_()),
367 // and for wells that open this step that rate is not known before the
368 // solve, so the siblings would share the full parent target;
369 // - a target derived from slave production (GCONINJE REIN, SALE or VREP)
370 // would be built from the previous sync step's production.
371 const int report_step_idx = this->well_model_.simulator().episodeIndex();
372 this->well_model_.updateAndCommunicateGroupData(
373 report_step_idx, /*update_wellgrouptarget=*/false);
374 this->sendMasterGroupConstraintsToSlaves();
375}
376
377template<typename TypeTag>
378void
379BlackoilWellModelRescoup<TypeTag>::
380refreshSlaveGroupTargets()
381{
382 // The deck's own GCONINJE and GCONPROD limits may be UDAs, resolved from
383 // the summary state, and the UDQ evaluation below may change them. So
384 // prime, evaluate the selected UDQs, and work the targets in force out
385 // once more with the UDQs current, as the well control path will see
386 // them. The second pass repeats the loops over the slave groups, not
387 // the UDQ evaluation.
388 this->storeSlaveGroupInjectionTargets();
389 this->storeSlaveGroupProductionTargets();
390 this->evalGroupAndFieldUDQs();
391 this->storeSlaveGroupInjectionTargets();
392 this->storeSlaveGroupProductionTargets();
393}
394
395template<typename TypeTag>
396void
397BlackoilWellModelRescoup<TypeTag>::
398rescoupSyncSummaryData()
399{
400 // Reservoir coupling: exchange production data between slaves and master.
401 //
402 // Master side: after its first substep, the master blocks here until all
403 // slaves have completed the sync step and sent their production data.
404 // This ensures evalSummaryState() (called next in endTimeStep) and all
405 // subsequent master substeps have correct slave production rates.
406 //
407 // Slave side: on the last substep of the sync step, the slave sends its
408 // production data to the master. The master is already waiting at this
409 // point (blocked on MPI_Recv from its first substep's timeStepSucceeded).
410 // The group rates are first recomputed from the converged well rates: the
411 // group state holds the rates of the NUPCOL well state, which is frozen
412 // after the first NUPCOL Newton iterations of the step. The step has
413 // converged, so there is nothing for NUPCOL to stabilise any more, and the
414 // master uses these rates for its summary output and for its own substeps
415 // until the next sync step. (The data sent at the start of a sync step is
416 // computed in the first Newton iteration, where the NUPCOL well state is the
417 // current one.)
418 if (this->isReservoirCouplingMaster()) {
419 if (this->reservoirCouplingMaster().needsSlaveDataReceive()) {
420 // The rates held before the receive are the slaves' start-of-step rates, which
421 // GRUPMAST item 4 compares with the end-of-step rates. They come from the last
422 // receive of the sync step, so they are the converged start state: after the
423 // slaves' initial well solve and any network iterations. The first receive is
424 // too early, wells that open at the start of the step still have zero rate then.
425 this->reservoirCouplingMaster().saveGroupFlowsAtStartOfSyncStep();
426 this->receiveSlaveGroupData();
427 // The gas production of the master groups has changed, and the
428 // limited gas consumption in the summary output with it.
429 this->well_model_.updateGasConsumptionFromWellState(
430 this->well_model_.simulator().episodeIndex());
431 this->reservoirCouplingMaster().updateFlowChangeTimeStepLimit();
432 this->reservoirCouplingMaster().setNeedsSlaveDataReceive(false);
433 }
434 }
435 if (this->isReservoirCouplingSlave()) {
436 if (this->reservoirCouplingSlave().isLastSubstepOfSyncTimestep()) {
437 this->well_model_.updateGroupRatesFromWellState(
438 this->well_model_.simulator().episodeIndex());
439 this->sendSlaveGroupDataToMaster();
440 }
441 }
442}
443
444template<typename TypeTag>
445void
446BlackoilWellModelRescoup<TypeTag>::
447sendCoupledNetworkActiveStatus()
448{
449 OPM_TIMEFUNCTION();
450 assert(this->isReservoirCouplingMaster());
451 // Send to each activated slave a single bool: "are you connected to the
452 // master's cross-rescoup network this sync timestep?", i.e. does this
453 // slave have a master group that is a leaf in the master network. This
454 // is per-slave: a slave with no leaf in the master network does not
455 // participate even if other slaves do. Sent as a dedicated one-element
456 // MPI message; the per-iteration node-pressure sends inside
457 // updateWellControlsAndNetworkIteration() carry their own is_final flag
458 // in the NumMasterGroupNodePressures header.
459 auto& rescoup_master = this->reservoirCouplingMaster();
460 const auto num_slaves = rescoup_master.numSlaves();
461 bool any_connected = false;
462 for (std::size_t slave_idx = 0; slave_idx < num_slaves; ++slave_idx) {
463 if (rescoup_master.slaveIsCoupled(slave_idx)) {
464 const bool connected =
465 this->masterNetworkHasMasterGroupLeavesForSlave_(slave_idx);
466 any_connected = any_connected || connected;
467 rescoup_master.sendCoupledNetworkActiveStatusToSlave(slave_idx, connected);
468 }
469 }
470 // Mirror the *global* active state into the master's own is_final flag:
471 // the master iterates the exchange iff at least one slave is connected.
472 // (This must stay global -- a per-slave value would break the master's
473 // own updateWellControlsAndNetworkIteration() gate.)
474 this->last_sent_master_group_node_pressures_is_final_ = !any_connected;
475}
476
477template<typename TypeTag>
478void
479BlackoilWellModelRescoup<TypeTag>::
480sendMasterGroupConstraintsToSlaves()
481{
482 OPM_TIMEFUNCTION();
483 // This function is called by the master process to send the group
484 // constraints to the slaves. The "will the master iterate cross-rescoup?"
485 // flag is shipped separately by sendCoupledNetworkActiveStatus().
486 RescoupConstraintsCalculator<Scalar, IndexTraits> constraints_calculator{
487 this->well_model_.guideRateHandler(),
488 this->groupStateHelper()
489 };
490 constraints_calculator.calculateMasterGroupConstraintsAndSendToSlaves();
491}
492
493template<typename TypeTag>
494void
495BlackoilWellModelRescoup<TypeTag>::
496sendMasterGroupNodePressuresToSlaves(bool is_final)
497{
498 OPM_TIMEFUNCTION();
499 assert(this->isReservoirCouplingMaster());
500 auto& rescoup_master = this->reservoirCouplingMaster();
501 const auto& node_pressures = this->network_.nodePressures();
502 const auto num_slaves = rescoup_master.numSlaves();
503 for (std::size_t slave_idx = 0; slave_idx < num_slaves; ++slave_idx) {
504 if (!rescoup_master.slaveIsCoupled(slave_idx)) continue;
505 std::vector<typename ReservoirCoupling::MasterGroupNodePressure<Scalar>> pressures;
506 const auto& master_groups = rescoup_master.getMasterGroupNamesForSlave(slave_idx);
507 for (std::size_t i = 0; i < master_groups.size(); ++i) {
508 const auto it = node_pressures.find(master_groups[i]);
509 if (it != node_pressures.end()) {
510 pressures.push_back({i, it->second});
511 }
512 }
513 rescoup_master.sendNumMasterGroupNodePressuresToSlave(
514 slave_idx, pressures.size(), is_final);
515 if (!pressures.empty()) {
516 rescoup_master.sendMasterGroupNodePressuresToSlave(slave_idx, pressures);
517 }
518 }
519 this->last_sent_master_group_node_pressures_is_final_ = is_final;
520}
521
522template<typename TypeTag>
523void
524BlackoilWellModelRescoup<TypeTag>::
525sendSlaveGroupDataToMaster()
526{
527 OPM_TIMEFUNCTION();
528 assert(this->isReservoirCouplingSlave());
529 RescoupSendSlaveGroupData<Scalar, IndexTraits> slave_group_data_sender{
530 this->groupStateHelper(), this->well_model_.nupcolWellState()};
531 slave_group_data_sender.sendSlaveGroupDataToMaster();
532}
533
534// Automatically manages the lifecycle of the DeferredLogger pointer
535// in the reservoir coupling logger. Ensures the logger is properly
536// cleared when it goes out of scope, preventing dangling pointer issues:
537//
538// - The ScopedLoggerGuard constructor sets the logger pointer
539// - When the guard goes out of scope, the destructor clears the pointer
540// - Move semantics transfer ownership safely when returning from this function
541// - The moved-from guard is "nullified" and its destructor does nothing
542// - Only the final guard in the caller will clear the logger
543template<typename TypeTag>
544std::optional<ReservoirCoupling::ScopedLoggerGuard>
545BlackoilWellModelRescoup<TypeTag>::
546setupScopedLogger(DeferredLogger& local_logger)
547{
548 if (this->isReservoirCouplingMaster()) {
549 return ReservoirCoupling::ScopedLoggerGuard{
550 this->reservoirCouplingMaster().logger(),
551 &local_logger
552 };
553 } else if (this->isReservoirCouplingSlave()) {
554 return ReservoirCoupling::ScopedLoggerGuard{
555 this->reservoirCouplingSlave().logger(),
556 &local_logger
557 };
558 }
559 return std::nullopt;
560}
561
562template<typename TypeTag>
563void
564BlackoilWellModelRescoup<TypeTag>::
565storeSlaveGroupInjectionTargets()
566{
567 const int reportStepIdx = this->groupStateHelper().reportStepIdx();
568 // The injection target that applies to a slave group is not something
569 // the slave's schedule can know on its own: it depends on the target
570 // the master imposes at every synchronization step and on the group's
571 // GRUPSLAV filter flag, which says whether the master's limit, the
572 // deck's own GCONINJE limit, or the smaller of the two is in force.
573 // Work out that effective target and keep it on the slave, where the
574 // summary writer picks it up and reports it as GGIRT/GWIRT. Only
575 // surface rate targets map onto these keywords.
576 //
577 // A UDQ that refers to the target reads it from the summary state, so
578 // the summary state is primed with the same value, in output units,
579 // ahead of the UDQ evaluation that follows. When nothing is in force
580 // the slot is primed with the schedule's own target instead -- what the
581 // end-of-step summary evaluation will report -- so that a target in
582 // force on an earlier sync step cannot linger there.
583 using M = UnitSystem::measure;
584 auto& summary_state = this->simulator_.vanguard().summaryState();
585 const auto& units = this->simulator_.vanguard().eclState().getUnits();
586 auto& slave = this->reservoirCouplingSlave();
587 const auto& rescoup = this->schedule()[reportStepIdx].rescoup();
588 // The surface-rate group injection target vectors, which is all of them:
589 // there is no oil equivalent, so a master oil injection target reaches the
590 // control path but has nothing to be reported as; and GVIRT is a reservoir
591 // volume target, which cannot be derived from what the master sends, since
592 // it converts every injection mode to a surface rate before sending (see
593 // RescoupConstraintsCalculator::calculateSlaveGroupInjectionTargets_()).
594 // A slave UDQ naming GVIRT for a slave group therefore still reads the
595 // schedule, as it did before reservoir coupling could report a target at
596 // all.
597 const auto targets = std::array {
598 std::tuple { Phase::GAS, std::string{"GGIRT"}, M::gas_surface_rate },
599 std::tuple { Phase::WATER, std::string{"GWIRT"}, M::liquid_surface_rate },
600 };
601 auto& in_force = slave.effectiveInjectionTargets();
602 in_force.clear();
603 for (std::size_t i = 0; i < slave.numSlaveGroups(); ++i) {
604 const auto& gname = slave.slaveGroupIdxToGroupName(i);
605 for (const auto& [phase, keyword, unit] : targets) {
606 const auto effective = this->effectiveSlaveGroupInjectionTarget_(
607 gname, phase, reportStepIdx, rescoup, summary_state);
608 if (effective.has_value()) {
609 in_force[gname][phase] = *effective;
610 summary_state.update_group_var(gname, keyword, units.from_si(unit, *effective));
611 }
612 else {
613 summary_state.update_group_var(
614 gname, keyword,
615 units.from_si(unit, this->scheduleInjectionTarget_(gname, phase, reportStepIdx, summary_state)));
616 }
617 }
618 }
619}
620
621template<typename TypeTag>
622void
623BlackoilWellModelRescoup<TypeTag>::
624storeSlaveGroupProductionTargets()
625{
626 const int reportStepIdx = this->groupStateHelper().reportStepIdx();
627 // As storeSlaveGroupInjectionTargets(), for the production rate limits.
628 // The master sends a slave group a limit for every rate type: the target
629 // of its active control mode, and its share of the other limits found up
630 // the master's group tree. The GRUPSLAV flag says how each combines with
631 // the deck's own GCONPROD limit. Work out the limit in force for each
632 // rate type, keep it on the slave for the summary writer, and prime the
633 // summary state for the UDQ evaluation that follows -- with the
634 // schedule's own limit when nothing is in force.
635 //
636 // The values are primed in the measures the summary evaluators use.
637 // RESV is left out: GVPRT does not report a group target, it adds up the
638 // reservoir volume rate targets of the group's wells.
639 using M = UnitSystem::measure;
640 using CMode = Group::ProductionCMode;
641 auto& summary_state = this->simulator_.vanguard().summaryState();
642 const auto& units = this->simulator_.vanguard().eclState().getUnits();
643 auto& slave = this->reservoirCouplingSlave();
644 const auto& rescoup = this->schedule()[reportStepIdx].rescoup();
645 const auto targets = std::array {
646 std::tuple { CMode::ORAT, std::string{"GOPRT"}, M::rate },
647 std::tuple { CMode::WRAT, std::string{"GWPRT"}, M::rate },
648 std::tuple { CMode::GRAT, std::string{"GGPRT"}, M::gas_surface_rate },
649 std::tuple { CMode::LRAT, std::string{"GLPRT"}, M::rate },
650 };
651 auto& in_force = slave.effectiveProductionTargets();
652 in_force.clear();
653 for (std::size_t i = 0; i < slave.numSlaveGroups(); ++i) {
654 const auto& gname = slave.slaveGroupIdxToGroupName(i);
655 for (const auto& [cmode, keyword, unit] : targets) {
656 const auto effective = this->effectiveSlaveGroupProductionTarget_(
657 gname, cmode, reportStepIdx, rescoup, summary_state);
658 if (effective.has_value()) {
659 in_force[gname][cmode] = *effective;
660 summary_state.update_group_var(gname, keyword, units.from_si(unit, *effective));
661 }
662 else {
663 summary_state.update_group_var(
664 gname, keyword,
665 units.from_si(unit, this->scheduleProductionTarget_(gname, cmode, reportStepIdx, summary_state)));
666 }
667 }
668 }
669}
670
671template<typename TypeTag>
672void
673BlackoilWellModelRescoup<TypeTag>::
674updateSlaveGroupCmodesAfterStateReset()
675{
676 // The master's group control modes are put into the slave groups by
677 // updateSlaveGroupCmodesFromMaster() in the first substep of a sync step, and
678 // that is not repeated when the substep is retried after a failure. The modes
679 // restored by beginTimeStep() are then the old ones, while the master's new
680 // targets are still held, so put the control modes of the held targets back;
681 // otherwise the group targets cannot be used. Without a retry the restored
682 // modes already match the held targets, except for a group that has switched to
683 // a rate limit of its own: it is put back on the master's target here, and
684 // switches again if that limit is still broken. In the first substep of a sync
685 // step, beginTimeStep() sets the modes again later, after the slave has received
686 // the master's new targets.
687 this->groupStateHelper().updateSlaveGroupCmodesFromMaster();
688}
689
690// Private methods alphabetically
691// ------------------------------
692
693template<typename TypeTag>
694std::optional<typename BlackoilWellModelRescoup<TypeTag>::Scalar>
695BlackoilWellModelRescoup<TypeTag>::
696effectiveSlaveGroupInjectionTarget_(const std::string& gname,
697 const Phase phase,
698 const int reportStepIdx,
699 const ReservoirCoupling::CouplingInfo& rescoup,
700 const SummaryState& summary_state) const
701{
702 using FilterFlag = ReservoirCoupling::GrupSlav::FilterFlag;
703 const auto& slave = this->reservoirCouplingSlave();
704 if (! slave.hasMasterInjectionTarget(gname, phase)) {
705 return std::nullopt;
706 }
707 const auto [master_target, cmode] = slave.masterInjectionTarget(gname, phase);
708 if (cmode != Group::InjectionCMode::RATE) {
709 return std::nullopt;
710 }
711
712 // Same fallback as GroupStateHelper::getInjectionFilterFlag_(): a slave
713 // group without a GRUPSLAV record follows the master.
714 auto filter = FilterFlag::MAST;
715 if (rescoup.hasGrupSlav(gname)) {
716 const auto& grup_slav = rescoup.grupSlav(gname);
717 filter = (phase == Phase::GAS) ? grup_slav.gasInjFlag()
718 : (phase == Phase::WATER) ? grup_slav.waterInjFlag()
719 : grup_slav.oilInjFlag();
720 }
721 if (filter == FilterFlag::SLAV) {
722 // The deck's own limit applies; the summary evaluator reads it from
723 // the schedule as for any other group.
724 return std::nullopt;
725 }
726 if (filter == FilterFlag::BOTH) {
727 // As in GroupStateHelper::getInjectionGroupTarget(): the deck's own rate
728 // takes part only when its GCONINJE gives one, since a defaulted rate
729 // item is zero.
730 const auto& group = this->schedule().getGroup(gname, reportStepIdx);
731 if (group.has_control(phase, Group::InjectionCMode::RATE)) {
732 return std::min(master_target,
733 this->scheduleInjectionTarget_(gname, phase, reportStepIdx, summary_state));
734 }
735 }
736 return master_target;
737}
738
739template<typename TypeTag>
740std::optional<typename BlackoilWellModelRescoup<TypeTag>::Scalar>
741BlackoilWellModelRescoup<TypeTag>::
742effectiveSlaveGroupProductionTarget_(const std::string& gname,
743 const Group::ProductionCMode cmode,
744 const int reportStepIdx,
745 const ReservoirCoupling::CouplingInfo& rescoup,
746 const SummaryState& summary_state) const
747{
748 // The rule GroupStateHelper::getEffectiveProductionLimit_() applies in
749 // the control path, with one difference. That method is only reached
750 // for a rate type the group has a GCONPROD limit of its own for, while
751 // a limit is reported here for every rate type, so the deck's own limit
752 // only takes part under BOTH when the group has one.
753 using CMode = Group::ProductionCMode;
754 using FilterFlag = ReservoirCoupling::GrupSlav::FilterFlag;
755 const auto& slave = this->reservoirCouplingSlave();
756 if (! slave.hasMasterProductionLimits(gname)) {
757 return std::nullopt;
758 }
759 const auto& limits = slave.masterProductionLimits(gname);
760 const Scalar master_limit = (cmode == CMode::ORAT) ? limits.oil_limit
761 : (cmode == CMode::WRAT) ? limits.water_limit
762 : (cmode == CMode::GRAT) ? limits.gas_limit
763 : limits.liquid_limit;
764 if (master_limit < 0) {
765 // No limit of this type up the master's group tree: the deck's own
766 // limit applies, and the summary evaluator reads it from the schedule.
767 return std::nullopt;
768 }
769
770 // Same mapping and fallback as GroupStateHelper::getProductionFilterFlag_():
771 // the second production flag of GRUPSLAV covers both water and liquid, and
772 // a slave group without a GRUPSLAV record follows the master.
773 auto filter = FilterFlag::MAST;
774 if (rescoup.hasGrupSlav(gname)) {
775 const auto& grup_slav = rescoup.grupSlav(gname);
776 filter = (cmode == CMode::ORAT) ? grup_slav.oilProdFlag()
777 : (cmode == CMode::GRAT) ? grup_slav.gasProdFlag()
778 : grup_slav.liquidProdFlag();
779 }
780 if (filter == FilterFlag::SLAV) {
781 return std::nullopt;
782 }
783 if (filter == FilterFlag::BOTH) {
784 const auto& group = this->schedule().getGroup(gname, reportStepIdx);
785 if (group.has_control(cmode)) {
786 return std::min(master_limit,
787 this->scheduleProductionTarget_(gname, cmode, reportStepIdx, summary_state));
788 }
789 }
790 return master_limit;
791}
792
793template<typename TypeTag>
794bool
795BlackoilWellModelRescoup<TypeTag>::
796masterNetworkHasMasterGroupLeavesForSlave_(std::size_t slave_idx) const
797{
798 // See masterNetworkHasMasterGroupLeaves() for why the parsed Schedule
799 // topology is queried rather than the runtime node_pressures_ map.
800 if (!this->isReservoirCouplingMaster()) return false;
801 const int episodeIdx = this->simulator_.episodeIndex();
802 const auto& network = this->schedule()[episodeIdx].network();
803 if (!network.active()) return false;
804 const auto& rcm = this->reservoirCouplingMaster();
805 for (const auto& mg : rcm.getMasterGroupNamesForSlave(slave_idx)) {
806 if (network.has_node(mg)) {
807 return true;
808 }
809 }
810 return false;
811}
812
813template<typename TypeTag>
814typename BlackoilWellModelRescoup<TypeTag>::Scalar
815BlackoilWellModelRescoup<TypeTag>::
816scheduleInjectionTarget_(const std::string& gname,
817 const Phase phase,
818 const int reportStepIdx,
819 const SummaryState& summary_state) const
820{
821 const auto& group = this->schedule().getGroup(gname, reportStepIdx);
822 if (! group.hasInjectionControl(phase)) {
823 return Scalar{0};
824 }
825 return static_cast<Scalar>(group.injectionControls(phase, summary_state).surface_max_rate);
826}
827
828template<typename TypeTag>
829typename BlackoilWellModelRescoup<TypeTag>::Scalar
830BlackoilWellModelRescoup<TypeTag>::
831scheduleProductionTarget_(const std::string& gname,
832 const Group::ProductionCMode cmode,
833 const int reportStepIdx,
834 const SummaryState& summary_state) const
835{
836 using CMode = Group::ProductionCMode;
837 const auto controls = this->schedule().getGroup(gname, reportStepIdx)
838 .productionControls(summary_state);
839 switch (cmode) {
840 case CMode::ORAT: return static_cast<Scalar>(controls.oil_target);
841 case CMode::WRAT: return static_cast<Scalar>(controls.water_target);
842 case CMode::GRAT: return static_cast<Scalar>(controls.gas_target);
843 case CMode::LRAT: return static_cast<Scalar>(controls.liquid_target);
844 default: return Scalar{0};
845 }
846}
847
848template<typename TypeTag>
849bool
850BlackoilWellModelRescoup<TypeTag>::
851slaveGroupIsFixedPressureNodeInOwnNetwork_(const std::string& group_name) const
852{
853 const int episodeIdx = this->simulator_.episodeIndex();
854 const auto& network = this->schedule()[episodeIdx].network();
855 if (!network.active()) return false;
856 if (!network.has_node(group_name)) return false;
857 return network.node(group_name).terminal_pressure().has_value();
858}
859
860template<typename TypeTag>
861bool
862BlackoilWellModelRescoup<TypeTag>::
863wellIsBelowOverriddenNode_(const std::string& group_name,
864 const std::map<std::string, Scalar>& overrides) const
865{
866 if (overrides.empty()) return false;
867 const int episodeIdx = this->simulator_.episodeIndex();
868 const auto& network = this->schedule()[episodeIdx].network();
869 if (!network.active() || !network.has_node(group_name)) return false;
870 return nodeFollowsOverriddenNode(network, group_name, overrides);
871}
872
873
874} // namespace Opm
875
876#endif // RESERVOIR_COUPLING_ENABLED
877#endif // OPM_BLACKOILWELLMODEL_RESCOUP_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:207
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
if(!linsolver_)
Definition: FlexibleSolver_impl.hpp:338
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:184
Definition: blackoilbioeffectsmodules.hh:45
bool nodeFollowsOverriddenNode(const Network::ExtNetwork &network, std::string node, const std::map< std::string, Scalar > &overrides)
Whether the pressure of a node follows from one of the overridden nodes (see the terminal pressure ov...
Definition: BlackoilWellModelNetworkPressureComputation.hpp:304