OutputBlackoilModule.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
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 2 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 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
27#ifndef OPM_OUTPUT_BLACK_OIL_MODULE_HPP
28#define OPM_OUTPUT_BLACK_OIL_MODULE_HPP
29
30#include <dune/common/fvector.hh>
31
32#include <opm/grid/CpGrid.hpp>
33#include <opm/grid/LookUpData.hh>
34
36
37#include <opm/common/Exceptions.hpp>
38#include <opm/common/TimingMacros.hpp>
39#include <opm/common/OpmLog/OpmLog.hpp>
40#include <opm/common/utility/Visitor.hpp>
41
42#include <opm/input/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
43
44#include <opm/material/common/Valgrind.hpp>
45#include <opm/material/fluidmatrixinteractions/EclEpsScalingPoints.hpp>
46#include <opm/material/fluidstates/BlackOilFluidState.hpp>
47#include <opm/material/fluidsystems/BlackOilFluidSystem.hpp>
48
53
54#include <opm/output/data/Cells.hpp>
55#include <opm/output/data/RegionVariableMapping.hpp>
56
57#include <opm/output/eclipse/EclipseIO.hpp>
58#include <opm/output/eclipse/Inplace.hpp>
59#include <opm/output/eclipse/RegionVariableCollection.hpp>
60
65
66#include <algorithm>
67#include <array>
68#include <cassert>
69#include <cstddef>
70#include <functional>
71#include <limits>
72#include <map>
73#include <set>
74#include <span>
75#include <stdexcept>
76#include <string>
77#include <type_traits>
78#include <utility>
79#include <vector>
80
81namespace Opm {
82
83// forward declaration
84template <class TypeTag>
85class EcfvDiscretization;
86
87namespace detail {
89 template <typename... T>
90 constexpr void ignoreUnused(T&&...) noexcept {}
91}
92
99template <class TypeTag>
100class OutputBlackOilModule : public GenericOutputModule<GetPropType<TypeTag, Properties::FluidSystem>>
101{
111 using FluidState = typename IntensiveQuantities::FluidState;
113 using Element = typename GridView::template Codim<0>::Entity;
114 using ElementIterator = typename GridView::template Codim<0>::Iterator;
117 using Dir = FaceDir::DirEnum;
123
124 static constexpr int conti0EqIdx = Indices::conti0EqIdx;
125 static constexpr int numPhases = FluidSystem::numPhases;
126 static constexpr int oilPhaseIdx = FluidSystem::oilPhaseIdx;
127 static constexpr int gasPhaseIdx = FluidSystem::gasPhaseIdx;
128 static constexpr int waterPhaseIdx = FluidSystem::waterPhaseIdx;
129 static constexpr int gasCompIdx = FluidSystem::gasCompIdx;
130 static constexpr int oilCompIdx = FluidSystem::oilCompIdx;
131 static constexpr int waterCompIdx = FluidSystem::waterCompIdx;
132 static constexpr EnergyModules energyModuleType = getPropValue<TypeTag, Properties::EnergyModuleType>();
133 static constexpr bool enableBioeffects = getPropValue<TypeTag, Properties::EnableBioeffects>();
134 static constexpr bool enableExtbo = getPropValue<TypeTag, Properties::EnableExtbo>();
135 static constexpr bool enableFoam = getPropValue<TypeTag, Properties::EnableFoam>();
136 static constexpr bool enablePolymer = getPropValue<TypeTag, Properties::EnablePolymer>();
137 static constexpr bool enableSolvent = getPropValue<TypeTag, Properties::EnableSolvent>();
138 enum { enableMICP = Indices::enableMICP };
139 enum { enableVapwat = getPropValue<TypeTag, Properties::EnableVapwat>() };
140 enum { enableDisgasInWater = getPropValue<TypeTag, Properties::EnableDisgasInWater>() };
141 static constexpr bool enableDissolvedGas =
142 Indices::compositionSwitchIdx != std::numeric_limits<unsigned>::max();
143
144 template<class VectorType>
145 static Scalar value_or_zero(int idx, const VectorType& v)
146 {
147 if (idx == -1) {
148 return 0.0;
149 }
150 return v.empty() ? 0.0 : v[idx];
151 }
152
153public:
154 OutputBlackOilModule(const Simulator& simulator,
155 const SummaryConfig& smryCfg,
156 const CollectDataOnIORankType& collectOnIORank)
157 : BaseType(simulator.vanguard().eclState(),
158 simulator.vanguard().schedule(),
159 smryCfg,
160 simulator.vanguard().summaryState(),
162 [this](const int idx)
163 { return simulator_.problem().eclWriter().collectOnIORank().localIdxToGlobalIdx(idx); },
164 [&collectOnIORank](const int idx)
165 { return collectOnIORank.isCartIdxOnThisRank(idx); },
166 simulator.vanguard().grid().comm(),
167 energyModuleType == EnergyModules::FullyImplicitThermal ||
168 energyModuleType == EnergyModules::SequentialImplicitThermal,
169 energyModuleType == EnergyModules::ConstantTemperature,
170 getPropValue<TypeTag, Properties::EnableMech>(),
171 getPropValue<TypeTag, Properties::EnableSolvent>(),
172 getPropValue<TypeTag, Properties::EnablePolymer>(),
173 getPropValue<TypeTag, Properties::EnableFoam>(),
174 getPropValue<TypeTag, Properties::EnableBrine>(),
175 getPropValue<TypeTag, Properties::EnableSaltPrecipitation>(),
176 getPropValue<TypeTag, Properties::EnableExtbo>(),
177 getPropValue<TypeTag, Properties::EnableBioeffects>(),
178 getPropValue<TypeTag, Properties::EnableGeochemistry>())
179 , simulator_(simulator)
180 , collectOnIORank_(collectOnIORank)
181 {
182 // The region arrays arrive on the (unrefined) input grid, but everything
183 // downstream indexes them by leaf cell. With an LGR the leaf has more
184 // cells, so map each onto it here - a refined cell inherits its parent's
185 // region - before anything looks at them. Identity without LGRs.
186 this->mapRegionsOntoLeaf_();
187
188 for (auto& region_pair : this->regions_) {
189 this->createLocalRegion_(region_pair.second);
190 }
191
192 auto isCartIdxOnThisRank = [&collectOnIORank](const int idx) {
193 return collectOnIORank.isCartIdxOnThisRank(idx);
194 };
195
196 this->setupBlockData(isCartIdxOnThisRank);
197
198 // Allocate slots for LB* summary nodes that name a cell inside an LGR.
199 // Runs per-rank (serial and parallel): each rank allocates only the LGR
200 // leaf cells it owns, so lgrBlockData_ is per-rank disjoint and the
201 // gather to the I/O rank is a clean union. Ownership is taken from the
202 // same InteriorEntity leaf-level decision the fill walk uses, collected
203 // here once. Empty for runs without LB* requests (zero non-LGR cost);
204 // constexpr-elided on non-CpGrid builds (LGRs are CpGrid-only).
205 std::set<std::pair<int, int>> ownedLgrCells;
206 std::map<std::string, int> lgrNameToLevel;
207 if constexpr (std::is_same_v<Grid, Dune::CpGrid>) {
208 // CpGrid name->level map is replicated on every rank (unlike the
209 // root-only EclipseState input grid).
210 lgrNameToLevel = simulator.vanguard().grid().getLgrNameToLevel();
211 for (const auto& element : elements(simulator.gridView())) {
212 const int level = element.level();
213 if (level > 0 && element.partitionType() == Dune::InteriorEntity) {
214 const int levelCompressed = element.getLevelElem().index();
215 const int levelCart = simulator.vanguard()
216 .levelCartesianIndexMapper()
217 .cartesianIndex(levelCompressed, level);
218 ownedLgrCells.emplace(level, levelCart);
219 }
220 }
221 }
222 this->setupLgrBlockData(
223 lgrNameToLevel,
224 [&ownedLgrCells](const int level, const int levelCart) {
225 return ownedLgrCells.count(std::make_pair(level, levelCart)) > 0;
226 });
227
228 if (! Parameters::Get<Parameters::OwnerCellsFirst>()) {
229 const std::string msg = "The output code does not support --owner-cells-first=false.";
230 if (collectOnIORank.isIORank()) {
231 OpmLog::error(msg);
232 }
233 OPM_THROW_NOLOG(std::runtime_error, msg);
234 }
235
236 if (smryCfg.match("[FB]PP[OGW]") || smryCfg.match("RPP[OGW]*")) {
237 auto rset = this->eclState_.fieldProps().fip_regions();
238 rset.push_back("PVTNUM");
239
240 // RegionPhasePoreVolAverage indexes by leaf cell. The FIP entries of
241 // regions_ are already on the leaf (mapRegionsOntoLeaf_ does that), so
242 // serve those directly. PVTNUM is not an FIP region and is deliberately
243 // kept out of regions_, so it needs its own leaf-mapped copy - with LGRs
244 // a refined cell inherits its parent's PVTNUM; identity without.
245 // decltype(auto) keeps the required "reference to vector" semantics.
246 const LookUpData<Grid, GridView> lookUpData(this->simulator_.gridView());
247 auto pvtnum = lookUpData.template assignFieldPropsIntOnLeaf<int>(
248 this->eclState_.fieldProps(), "PVTNUM", /*needsTranslation=*/false);
249
251 .emplace(this->simulator_.gridView().comm(),
252 FluidSystem::numPhases, rset,
253 [&regions = std::as_const(this->regions_),
254 pvtnum = std::move(pvtnum)]
255 (const std::string& rsetName) -> decltype(auto)
256 {
257 return (rsetName == "PVTNUM")
258 ? static_cast<const std::vector<int>&>(pvtnum)
259 : static_cast<const std::vector<int>&>(regions.at(rsetName));
260 });
261 }
262 }
263
268 void
269 allocBuffers(const unsigned bufferSize,
270 const unsigned reportStepNum,
271 const bool substep,
272 const bool log,
273 const bool forceRestartFieldAllocation)
274 {
275 if (! std::is_same<Discretization, EcfvDiscretization<TypeTag>>::value) {
276 return;
277 }
278
279 const auto& problem = this->simulator_.problem();
280
281 this->doAllocBuffers(bufferSize,
282 reportStepNum,
283 substep,
284 log,
285 forceRestartFieldAllocation,
286 &problem.materialLawManager().hysteresisConfig(),
287 problem.eclWriter().getOutputNnc().front().size());
288 }
289
291 void setupExtractors(const bool isSubStep,
292 const int reportStepNum)
293 {
294 this->setupElementExtractors_();
295 this->setupBlockExtractors_(isSubStep, reportStepNum);
296 }
297
300 {
301 this->extractors_.clear();
302 this->blockExtractors_.clear();
303 this->extraBlockExtractors_.clear();
304 this->lgrBlockExtractors_.clear();
305 }
306
310 void assignToSolution(data::Solution& sol) override
311 {
313
314 // Quantities that only the black-oil formulation reports.
315 using M = UnitSystem::measure;
316
317 this->assignBuffer(sol, "BO", M::oil_formation_volume_factor,
318 this->formationVolFactor_[oilPhaseIdx], oilPhaseIdx);
319 this->assignBuffer(sol, "1OVERBO", M::oil_inverse_formation_volume_factor,
320 this->invB_[oilPhaseIdx], oilPhaseIdx);
321
322 this->assignBuffer(sol, "BG", M::gas_formation_volume_factor,
323 this->formationVolFactor_[gasPhaseIdx], gasPhaseIdx);
324 this->assignBuffer(sol, "1OVERBG", M::gas_inverse_formation_volume_factor,
325 this->invB_[gasPhaseIdx], gasPhaseIdx);
326
327 this->assignBuffer(sol, "OILKR", M::identity,
328 this->relativePermeability_[oilPhaseIdx], oilPhaseIdx);
329 this->assignBuffer(sol, "GASKR", M::identity,
330 this->relativePermeability_[gasPhaseIdx], gasPhaseIdx);
331 this->assignBuffer(sol, "PBUB", M::pressure, this->bubblePointPressure_);
332 this->assignBuffer(sol, "PDEW", M::pressure, this->dewPointPressure_);
333 this->assignBuffer(sol, "RS", M::gas_oil_ratio, this->rs_);
334 this->assignBuffer(sol, "RV", M::oil_gas_ratio, this->rv_);
335
336 // avoid output with generic fluid system and disabled water phase
337 if constexpr (numPhases > 2) {
338 this->assignBuffer(sol, "BW", M::water_formation_volume_factor,
339 this->formationVolFactor_[waterPhaseIdx], waterPhaseIdx);
340 this->assignBuffer(sol, "1OVERBW", M::water_inverse_formation_volume_factor,
341 this->invB_[waterPhaseIdx], waterPhaseIdx);
342
343 this->assignBuffer(sol, "WATKR", M::identity,
344 this->relativePermeability_[waterPhaseIdx], waterPhaseIdx);
345 }
346
347 // The phase densities and viscosities are named by the emitting
348 // module: these are the black-oil array names.
350 .oilDensity = "OIL_DEN",
351 .gasDensity = "GAS_DEN",
352 .waterDensity = "WAT_DEN",
353 .oilViscosity = "OIL_VISC",
354 .gasViscosity = "GAS_VISC",
355 .waterViscosity = "WAT_VISC",
356 });
357 }
358
363 void processElement(const ElementContext& elemCtx)
364 {
365 OPM_TIMEBLOCK_LOCAL(processElement, Subsystem::Output);
366 if (!std::is_same<Discretization, EcfvDiscretization<TypeTag>>::value) {
367 return;
368 }
369
370 if (this->extractors_.empty()) {
371 assert(0);
372 }
373
374 const auto& matLawManager = simulator_.problem().materialLawManager();
375
376 for (unsigned dofIdx = 0; dofIdx < elemCtx.numPrimaryDof(/*timeIdx=*/0); ++dofIdx) {
377 const auto& intQuants = elemCtx.intensiveQuantities(dofIdx, /*timeIdx=*/0);
378 const auto& fs = intQuants.fluidState();
379 const auto globalDofIdx = elemCtx.globalSpaceIndex(dofIdx, /*timeIdx=*/0);
380
381 typename Extractor::HysteresisParams hysterParams;
382 if (matLawManager.enableHysteresis()) {
383 if (FluidSystem::phaseIsActive(oilPhaseIdx) && FluidSystem::phaseIsActive(waterPhaseIdx)) {
384 matLawManager.oilWaterHysteresisParams(hysterParams.somax,
385 hysterParams.swmax,
386 hysterParams.swmin,
387 globalDofIdx);
388 }
389 if (FluidSystem::phaseIsActive(oilPhaseIdx) && FluidSystem::phaseIsActive(gasPhaseIdx)) {
390 matLawManager.gasOilHysteresisParams(hysterParams.sgmax,
391 hysterParams.shmax,
392 hysterParams.somin,
393 globalDofIdx);
394 }
395 }
396
397 const typename Extractor::Context ectx{
398 globalDofIdx,
399 elemCtx.primaryVars(dofIdx, /*timeIdx=*/0).pvtRegionIndex(),
400 elemCtx.simulator().episodeIndex(),
401 fs,
402 intQuants,
403 hysterParams
404 };
405
406 Extractor::process(ectx, extractors_);
407 }
408 }
409
410 void processElementBlockData(const ElementContext& elemCtx)
411 {
412 OPM_TIMEBLOCK_LOCAL(processElementBlockData, Subsystem::Output);
413 if (!std::is_same<Discretization, EcfvDiscretization<TypeTag>>::value) {
414 return;
415 }
416
417 if (this->blockExtractors_.empty() &&
418 this->extraBlockExtractors_.empty() &&
419 this->lgrBlockExtractors_.empty())
420 {
421 return;
422 }
423
424 // For EcfvDiscretization there is one degree of freedom per
425 // element, so the element's level determines whether the cell
426 // sits in the global grid (level == 0) or inside a local grid
427 // refinement (level > 0). Branching here keeps the global B*
428 // lookup off the LGR-cell path and vice versa; non-LGR runs
429 // never enter the LGR branch.
430 const auto& element = elemCtx.element();
431 const int level = element.level();
432
433 for (unsigned dofIdx = 0; dofIdx < elemCtx.numPrimaryDof(/*timeIdx=*/0); ++dofIdx) {
434 const auto globalDofIdx = elemCtx.globalSpaceIndex(dofIdx, /*timeIdx=*/0);
435
436 const std::vector<typename BlockExtractor::Exec>* be_extractors = nullptr;
437 const std::vector<typename BlockExtractor::Exec>* bee_extractors = nullptr;
438 const std::vector<typename BlockExtractor::Exec>* lgr_extractors = nullptr;
439
440 if (level == 0) {
441 const auto cartesianIdx = elemCtx.simulator().vanguard().cartesianIndex(globalDofIdx);
442 const auto be_it = this->blockExtractors_.find(cartesianIdx);
443 const auto bee_it = this->extraBlockExtractors_.find(cartesianIdx);
444 if (be_it != this->blockExtractors_.end()) { be_extractors = &be_it->second; }
445 if (bee_it != this->extraBlockExtractors_.end()) { bee_extractors = &bee_it->second; }
446 }
447 else if constexpr (std::is_same_v<Grid, Dune::CpGrid>) {
448 // Cells inside a local grid refinement. LGRs are a
449 // CpGrid-only feature today; ALU and Polyhedral grids
450 // keep element.level() at 0 and never enter this
451 // branch, but the constexpr if filters it out at
452 // compile time on those builds so the CpGrid-specific
453 // getLevelElem() call never has to be instantiated
454 // for an unsupported entity type. Ownership at the
455 // leaf level is decided by Dune::InteriorEntity; the
456 // global-Cartesian isCartIdxOnThisRank predicate used
457 // at setup time does not apply to LGR cells (they do
458 // not inhabit the global Cartesian space).
459 const auto level_it = this->lgrBlockExtractors_.find(level);
460 if (level_it != this->lgrBlockExtractors_.end() &&
461 element.partitionType() == Dune::InteriorEntity)
462 {
463 const int levelCompressed = element.getLevelElem().index();
464 const int levelCart = elemCtx.simulator().vanguard()
465 .levelCartesianIndexMapper()
466 .cartesianIndex(levelCompressed, level);
467 const auto cell_it = level_it->second.find(levelCart);
468 if (cell_it != level_it->second.end()) {
469 lgr_extractors = &cell_it->second;
470 }
471 }
472 }
473
474 if (be_extractors == nullptr &&
475 bee_extractors == nullptr &&
476 lgr_extractors == nullptr)
477 {
478 continue;
479 }
480
481 const auto& intQuants = elemCtx.intensiveQuantities(dofIdx, /*timeIdx=*/0);
482 const auto& fs = intQuants.fluidState();
483
484 const typename BlockExtractor::Context ectx{
485 globalDofIdx,
486 dofIdx,
487 fs,
488 intQuants,
489 elemCtx,
490 };
491
492 if (be_extractors != nullptr) { BlockExtractor::process(*be_extractors, ectx); }
493 if (bee_extractors != nullptr) { BlockExtractor::process(*bee_extractors, ectx); }
494 if (lgr_extractors != nullptr) { BlockExtractor::process(*lgr_extractors, ectx); }
495 }
496 }
497
498 void outputFipAndResvLog(const Inplace& inplace,
499 const std::size_t reportStepNum,
500 double elapsed,
501 boost::posix_time::ptime currentDate,
502 const bool substep,
503 const Parallel::Communication& comm)
504 {
505
506 if (comm.rank() != 0) {
507 return;
508 }
509
510 // For report step 0 we use the RPTSOL config, else derive from RPTSCHED
511 std::unique_ptr<FIPConfig> fipSched;
512 if (reportStepNum > 0) {
513 const auto& rpt = this->schedule_[reportStepNum-1].rpt_config.get();
514 fipSched = std::make_unique<FIPConfig>(rpt);
515 }
516 const FIPConfig& fipc = reportStepNum == 0 ? this->eclState_.getEclipseConfig().fip()
517 : *fipSched;
518
519 if (!substep && !this->forceDisableFipOutput_ && fipc.output(FIPConfig::OutputField::FIELD)) {
520
521 this->logOutput_.timeStamp("BALANCE", elapsed, reportStepNum, currentDate);
522
523 const auto& initial_inplace = *this->initialInplace();
524 this->logOutput_.fip(inplace, initial_inplace, "");
525
526 if (fipc.output(FIPConfig::OutputField::FIPNUM)) {
527 this->logOutput_.fip(inplace, initial_inplace, "FIPNUM");
528
529 if (fipc.output(FIPConfig::OutputField::RESV))
530 this->logOutput_.fipResv(inplace, "FIPNUM");
531 }
532
533 if (fipc.output(FIPConfig::OutputField::FIP)) {
534 for (const auto& reg : this->regions_) {
535 if (reg.first != "FIPNUM") {
536 std::ostringstream ss;
537 ss << "BAL" << reg.first.substr(3);
538 this->logOutput_.timeStamp(ss.str(), elapsed, reportStepNum, currentDate);
539 this->logOutput_.fip(inplace, initial_inplace, reg.first);
540
541 if (fipc.output(FIPConfig::OutputField::RESV))
542 this->logOutput_.fipResv(inplace, reg.first);
543 }
544 }
545 }
546 }
547 }
548
549 void outputFipAndResvLogToCSV(const std::size_t reportStepNum,
550 const bool substep,
551 const Parallel::Communication& comm)
552 {
553 if (comm.rank() != 0) {
554 return;
555 }
556
557 if ((reportStepNum == 0) && (!substep) &&
558 (this->schedule_.initialReportConfiguration().has_value()) &&
559 (this->schedule_.initialReportConfiguration()->contains("CSVFIP"))) {
560
561 std::ostringstream csv_stream;
562
563 this->logOutput_.csv_header(csv_stream);
564
565 const auto& initial_inplace = *this->initialInplace();
566
567 this->logOutput_.fip_csv(csv_stream, initial_inplace, "FIPNUM");
568
569 for (const auto& reg : this->regions_) {
570 if (reg.first != "FIPNUM") {
571 this->logOutput_.fip_csv(csv_stream, initial_inplace, reg.first);
572 }
573 }
574
575 const IOConfig& io = this->eclState_.getIOConfig();
576 auto csv_fname = io.getOutputDir() + "/" + io.getBaseName() + ".CSV";
577
578 std::ofstream outputFile(csv_fname);
579
580 outputFile << csv_stream.str();
581
582 outputFile.close();
583 }
584 }
585
614 template <class ActiveIndex, class CartesianIndex>
615 void processFluxes(const ElementContext& elemCtx,
616 ActiveIndex&& activeIndex,
617 CartesianIndex&& cartesianIndex)
618 {
619 OPM_TIMEBLOCK_LOCAL(processFluxes, Subsystem::Output);
620 const auto identifyCell = [&activeIndex, &cartesianIndex](const Element& elem)
622 {
623 const auto cellIndex = activeIndex(elem);
624
625 return {
626 static_cast<int>(cellIndex),
627 cartesianIndex(cellIndex),
628 elem.partitionType() == Dune::InteriorEntity
629 };
630 };
631
632 const auto timeIdx = 0u;
633 const auto& stencil = elemCtx.stencil(timeIdx);
634 const auto numInteriorFaces = elemCtx.numInteriorFaces(timeIdx);
635
636 for (auto scvfIdx = 0 * numInteriorFaces; scvfIdx < numInteriorFaces; ++scvfIdx) {
637 const auto& face = stencil.interiorFace(scvfIdx);
638 const auto left = identifyCell(stencil.element(face.interiorIndex()));
639 const auto right = identifyCell(stencil.element(face.exteriorIndex()));
640
641 const auto rates = this->
642 getComponentSurfaceRates(elemCtx, face.area(), scvfIdx, timeIdx);
643
644 this->interRegionFlows_.addConnection(left, right, rates);
645 }
646 }
647
653 {
654 // Inter-region flow rates. Note: ".clear()" prepares to accumulate
655 // contributions per bulk connection between FIP regions.
656 this->interRegionFlows_.clear();
657 }
658
663 {
665 }
666
671 {
672 return this->interRegionFlows_;
673 }
674
675 template <class FluidState>
676 void assignToFluidState(FluidState& fs, unsigned elemIdx) const
677 {
678 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
679 if (this->saturation_[phaseIdx].empty())
680 continue;
681
682 fs.setSaturation(phaseIdx, this->saturation_[phaseIdx][elemIdx]);
683 }
684
685 if (!this->fluidPressure_.empty()) {
686 // this assumes that capillary pressures only depend on the phase saturations
687 // and possibly on temperature. (this is always the case for ECL problems.)
688 std::array<Scalar, numPhases> pc = {0};
689 const MaterialLawParams& matParams = simulator_.problem().materialLawParams(elemIdx);
690 MaterialLaw::capillaryPressures(pc, matParams, fs);
691 Valgrind::CheckDefined(this->fluidPressure_[elemIdx]);
692 Valgrind::CheckDefined(pc);
693 const auto& pressure = this->fluidPressure_[elemIdx];
694 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
695 if (!FluidSystem::phaseIsActive(phaseIdx))
696 continue;
697
698 if (Indices::oilEnabled)
699 fs.setPressure(phaseIdx, pressure + (pc[phaseIdx] - pc[oilPhaseIdx]));
700 else if (Indices::gasEnabled)
701 fs.setPressure(phaseIdx, pressure + (pc[phaseIdx] - pc[gasPhaseIdx]));
702 else if (Indices::waterEnabled)
703 //single (water) phase
704 fs.setPressure(phaseIdx, pressure);
705 }
706 }
707
708 if constexpr (energyModuleType != EnergyModules::NoTemperature) {
709 if (!this->temperature_.empty())
710 fs.setTemperature(this->temperature_[elemIdx]);
711 }
712 if constexpr (enableDissolvedGas) {
713 if (!this->rs_.empty())
714 fs.setRs(this->rs_[elemIdx]);
715 if (!this->rv_.empty())
716 fs.setRv(this->rv_[elemIdx]);
717 }
718 if constexpr (enableDisgasInWater) {
719 if (!this->rsw_.empty())
720 fs.setRsw(this->rsw_[elemIdx]);
721 }
722 if constexpr (enableVapwat) {
723 if (!this->rvw_.empty())
724 fs.setRvw(this->rvw_[elemIdx]);
725 }
726 }
727
728 void initHysteresisParams(Simulator& simulator, unsigned elemIdx) const
729 {
730 if (!this->soMax_.empty())
731 simulator.problem().setMaxOilSaturation(elemIdx, this->soMax_[elemIdx]);
732
733 if (simulator.problem().materialLawManager().enableHysteresis()) {
734 auto& matLawManager = simulator.problem().materialLawManager();
735
736 if (FluidSystem::phaseIsActive(oilPhaseIdx)
737 && FluidSystem::phaseIsActive(waterPhaseIdx)) {
738 Scalar somax = 2.0;
739 Scalar swmax = -2.0;
740 Scalar swmin = 2.0;
741
742 if (matLawManager.enableNonWettingHysteresis()) {
743 if (!this->soMax_.empty()) {
744 somax = this->soMax_[elemIdx];
745 }
746 }
747 if (matLawManager.enableWettingHysteresis()) {
748 if (!this->swMax_.empty()) {
749 swmax = this->swMax_[elemIdx];
750 }
751 }
752 if (matLawManager.enablePCHysteresis()) {
753 if (!this->swmin_.empty()) {
754 swmin = this->swmin_[elemIdx];
755 }
756 }
757 matLawManager.setOilWaterHysteresisParams(
758 somax, swmax, swmin, elemIdx);
759 }
760 if (FluidSystem::phaseIsActive(oilPhaseIdx)
761 && FluidSystem::phaseIsActive(gasPhaseIdx)) {
762 Scalar sgmax = 2.0;
763 Scalar shmax = -2.0;
764 Scalar somin = 2.0;
765
766 if (matLawManager.enableNonWettingHysteresis()) {
767 if (!this->sgmax_.empty()) {
768 sgmax = this->sgmax_[elemIdx];
769 }
770 }
771 if (matLawManager.enableWettingHysteresis()) {
772 if (!this->shmax_.empty()) {
773 shmax = this->shmax_[elemIdx];
774 }
775 }
776 if (matLawManager.enablePCHysteresis()) {
777 if (!this->somin_.empty()) {
778 somin = this->somin_[elemIdx];
779 }
780 }
781 matLawManager.setGasOilHysteresisParams(
782 sgmax, shmax, somin, elemIdx);
783 }
784
785 }
786
787 if (simulator_.vanguard().eclState().fieldProps().has_double("SWATINIT")) {
788 simulator.problem().materialLawManager().applyRestartSwatInit(elemIdx, this->ppcw_[elemIdx]);
789 }
790 }
791
792 void updateFluidInPlace(const ElementContext& elemCtx)
793 {
794 for (unsigned dofIdx = 0; dofIdx < elemCtx.numPrimaryDof(/*timeIdx=*/0); ++dofIdx) {
795 updateFluidInPlace_(elemCtx, dofIdx);
796 }
797 }
798
799 void updateFluidInPlace(const unsigned globalDofIdx,
800 const IntensiveQuantities& intQuants,
801 const double totVolume)
802 {
803 this->updateFluidInPlace_(globalDofIdx, intQuants, totVolume);
804 }
805
807 void setRestart(const data::Solution& sol,
808 const unsigned elemIdx,
809 const unsigned globalDofIndex) override
810 {
811 BaseType::setRestart(sol, elemIdx, globalDofIndex);
812
813 auto assign = [&sol, elemIdx, globalDofIndex](const std::string& name,
814 ScalarBuffer& data)
815 {
816 if (!data.empty() && sol.has(name)) {
817 data[elemIdx] = sol.data<double>(name)[globalDofIndex];
818 }
819 };
820
821 assign("RS", rs_);
822 assign("RV", rv_);
823 }
824
825protected:
828 void allocFormulationBuffers(std::map<std::string, int>& rstKeywords,
829 const unsigned bufferSize) override
830 {
831 // RS and RV are allocated whenever the fluid system supports them,
832 // not only when the restart keyword asks for them.
833 BaseType::allocBufferIfRequested(rstKeywords, bufferSize, rs_, "RS",
834 FluidSystem::enableDissolvedGas(), true);
835 BaseType::allocBufferIfRequested(rstKeywords, bufferSize, rv_, "RV",
836 FluidSystem::enableVaporizedOil(), true);
837
838 // PBPD requests the bubble and the dew point pressure together.
839 if (BaseType::allocBufferIfRequested(rstKeywords, bufferSize,
840 bubblePointPressure_, "PBPD", true)) {
841 dewPointPressure_.resize(bufferSize, 0.0);
842 }
843
844 // "WOG" ordering, as used by the shared module's keyword naming.
845 constexpr auto phaseChar = std::string_view{"WOG"};
846 for (unsigned phase = 0; phase < numPhases; ++phase) {
847 if (!FluidSystem::phaseIsActive(phase)) {
848 continue;
849 }
850
851 // The black-oil phase ordering is the one the keyword names
852 // assume, so no reordering is needed here.
853 BaseType::allocBufferIfRequested(rstKeywords, bufferSize,
854 this->formationVolFactor_[phase],
855 std::string("B") + phaseChar[phase], true);
856
857 BaseType::allocBufferIfRequested(rstKeywords, bufferSize, this->invB_[phase],
858 std::string("1OVERB") + phaseChar[phase], true);
859
860 BaseType::allocBufferIfRequested(rstKeywords, bufferSize, relativePermeability_[phase],
861 std::string("KR") + phaseChar[phase], true);
862 }
863 }
864
865private:
866
867 // Buffers of the quantities only the black-oil formulation produces.
868 using ScalarBuffer = typename BaseType::ScalarBuffer;
869 ScalarBuffer rs_;
870 ScalarBuffer rv_;
871 ScalarBuffer bubblePointPressure_;
872 ScalarBuffer dewPointPressure_;
873 std::array<ScalarBuffer, numPhases> formationVolFactor_;
874 std::array<ScalarBuffer, numPhases> invB_;
875 std::array<ScalarBuffer, numPhases> relativePermeability_;
876 template <typename T>
877 using RemoveCVR = std::remove_cv_t<std::remove_reference_t<T>>;
878
879 template <typename, class = void>
880 struct HasGeoMech : public std::false_type {};
881
882 template <typename Problem>
883 struct HasGeoMech<
884 Problem, std::void_t<decltype(std::declval<Problem>().geoMechModel())>
885 > : public std::true_type {};
886
887 template <typename, class = void>
888 struct HasGeochemistry : public std::false_type {};
889
890 template <typename Problem>
891 struct HasGeochemistry<
892 Problem, std::void_t<decltype(std::declval<Problem>().geochemistryModel())>
893 > : public std::true_type {};
894
895 bool isDefunctParallelWell(const std::string& wname) const override
896 {
897 if (simulator_.gridView().comm().size() == 1)
898 return false;
899 const auto& parallelWells = simulator_.vanguard().parallelWells();
900 std::pair<std::string, bool> value {wname, true};
901 auto candidate = std::lower_bound(parallelWells.begin(), parallelWells.end(), value);
902 return candidate == parallelWells.end() || *candidate != value;
903 }
904
905 bool isOwnedByCurrentRank(const std::string& wname) const override
906 {
907 return this->simulator_.problem().wellModel().isOwner(wname);
908 }
909
910 bool isOnCurrentRank(const std::string& wname) const override
911 {
912 return this->simulator_.problem().wellModel().hasLocalCells(wname);
913 }
914
915 void updateFluidInPlace_(const ElementContext& elemCtx, const unsigned dofIdx)
916 {
917 const auto& intQuants = elemCtx.intensiveQuantities(dofIdx, /*timeIdx=*/0);
918 const unsigned globalDofIdx = elemCtx.globalSpaceIndex(dofIdx, /*timeIdx=*/0);
919 const auto totVolume = elemCtx.simulator().model().dofTotalVolume(globalDofIdx);
920
921 this->updateFluidInPlace_(globalDofIdx, intQuants, totVolume);
922 }
923
924 void updateFluidInPlace_(const unsigned globalDofIdx,
925 const IntensiveQuantities& intQuants,
926 const double totVolume)
927 {
928 OPM_TIMEBLOCK_LOCAL(updateFluidInPlace, Subsystem::Output);
929
930 this->updateTotalVolumesAndPressures_(globalDofIdx,
931 intQuants,
932 totVolume,
933 intQuants.referencePorosity());
934
935 if (this->computeFip_) {
936 this->updatePhaseInplaceVolumes_(globalDofIdx, intQuants, totVolume);
937 }
938 }
939
944 void mapRegionsOntoLeaf_()
945 {
946 const LookUpData<Grid, GridView> lookUpData(simulator_.gridView());
947 const auto numLeaf = simulator_.gridView().size(0);
948
949 std::vector<int> onLeaf(numLeaf, 0);
950 for (auto& [name, region] : this->regions_) {
951 std::size_t elemIdx = 0;
952 for (const auto& elem : elements(simulator_.gridView())) {
953 onLeaf[elemIdx++] = lookUpData(elem, region);
954 }
955
956 region = onLeaf;
957 }
958 }
959
960 void createLocalRegion_(std::vector<int>& region)
961 {
962 std::size_t elemIdx = 0;
963 for (const auto& elem : elements(simulator_.gridView())) {
964 if (elem.partitionType() != Dune::InteriorEntity) {
965 region[elemIdx] = 0;
966 }
967
968 ++elemIdx;
969 }
970 }
971
972 template <typename FluidState>
973 void aggregateAverageDensityContributions_(const FluidState& fs,
974 const unsigned int globalDofIdx,
975 const double porv)
976 {
977 auto pvCellValue = RegionPhasePoreVolAverage::CellValue{};
978 pvCellValue.porv = porv;
979
980 for (auto phaseIdx = 0*FluidSystem::numPhases;
981 phaseIdx < FluidSystem::numPhases; ++phaseIdx)
982 {
983 if (! FluidSystem::phaseIsActive(phaseIdx)) {
984 continue;
985 }
986
987 pvCellValue.value = getValue(fs.density(phaseIdx));
988 pvCellValue.sat = getValue(fs.saturation(phaseIdx));
989
991 ->addCell(globalDofIdx,
993 pvCellValue);
994 }
995 }
996
1012 data::InterRegFlowMap::FlowRates
1013 getComponentSurfaceRates(const ElementContext& elemCtx,
1014 const Scalar faceArea,
1015 const std::size_t scvfIdx,
1016 const std::size_t timeIdx) const
1017 {
1018 using Component = data::InterRegFlowMap::Component;
1019
1020 auto rates = data::InterRegFlowMap::FlowRates {};
1021
1022 const auto& extQuant = elemCtx.extensiveQuantities(scvfIdx, timeIdx);
1023
1024 const auto alpha = getValue(extQuant.extrusionFactor()) * faceArea;
1025
1026 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
1027 const auto& up = elemCtx
1028 .intensiveQuantities(extQuant.upstreamIndex(oilPhaseIdx), timeIdx);
1029
1030 const auto pvtReg = up.pvtRegionIndex();
1031
1032 const auto bO = getValue(getInvB_<FluidSystem, FluidState, Scalar>
1033 (up.fluidState(), oilPhaseIdx, pvtReg));
1034
1035 const auto qO = alpha * bO * getValue(extQuant.volumeFlux(oilPhaseIdx));
1036
1037 rates[Component::Oil] += qO;
1038
1039 if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
1040 const auto Rs = getValue(
1041 BlackOil::getRs_<FluidSystem, FluidState, Scalar>
1042 (up.fluidState(), pvtReg));
1043
1044 rates[Component::Gas] += qO * Rs;
1045 rates[Component::Disgas] += qO * Rs;
1046 }
1047 }
1048
1049 if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
1050 const auto& up = elemCtx
1051 .intensiveQuantities(extQuant.upstreamIndex(gasPhaseIdx), timeIdx);
1052
1053 const auto pvtReg = up.pvtRegionIndex();
1054
1055 const auto bG = getValue(getInvB_<FluidSystem, FluidState, Scalar>
1056 (up.fluidState(), gasPhaseIdx, pvtReg));
1057
1058 const auto qG = alpha * bG * getValue(extQuant.volumeFlux(gasPhaseIdx));
1059
1060 rates[Component::Gas] += qG;
1061
1062 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
1063 const auto Rv = getValue(
1064 BlackOil::getRv_<FluidSystem, FluidState, Scalar>
1065 (up.fluidState(), pvtReg));
1066
1067 rates[Component::Oil] += qG * Rv;
1068 rates[Component::Vapoil] += qG * Rv;
1069 }
1070 }
1071
1072 if (FluidSystem::phaseIsActive(waterPhaseIdx)) {
1073 const auto& up = elemCtx
1074 .intensiveQuantities(extQuant.upstreamIndex(waterPhaseIdx), timeIdx);
1075
1076 const auto pvtReg = up.pvtRegionIndex();
1077
1078 const auto bW = getValue(getInvB_<FluidSystem, FluidState, Scalar>
1079 (up.fluidState(), waterPhaseIdx, pvtReg));
1080
1081 rates[Component::Water] +=
1082 alpha * bW * getValue(extQuant.volumeFlux(waterPhaseIdx));
1083 }
1084
1085 return rates;
1086 }
1087
1088 void updatePhaseInplaceVolumes_(const unsigned globalDofIdx,
1089 const IntensiveQuantities& intQuants,
1090 const double totVolume)
1091 {
1092 std::array<Scalar, FluidSystem::numPhases> fip {};
1093 std::array<Scalar, FluidSystem::numPhases> fipr{}; // at reservoir condition
1094
1095 const auto& fs = intQuants.fluidState();
1096 const auto pv = totVolume * intQuants.porosity().value();
1097
1098 for (unsigned phaseIdx = 0; phaseIdx < FluidSystem::numPhases; ++phaseIdx) {
1099 if (!FluidSystem::phaseIsActive(phaseIdx)) {
1100 continue;
1101 }
1102
1103 const auto b = getValue(fs.invB(phaseIdx));
1104 const auto s = getValue(fs.saturation(phaseIdx));
1105
1106 fipr[phaseIdx] = s * pv;
1107 fip [phaseIdx] = b * fipr[phaseIdx];
1108 }
1109
1110 this->fipC_.assignVolumesSurface(globalDofIdx, fip);
1111 this->fipC_.assignVolumesReservoir(globalDofIdx,
1112 fs.saltConcentration().value(),
1113 fipr);
1114
1115 if (FluidSystem::phaseIsActive(oilPhaseIdx) &&
1116 FluidSystem::phaseIsActive(gasPhaseIdx))
1117 {
1118 this->updateOilGasDistribution(globalDofIdx, fs, fip);
1119 }
1120
1121 if (FluidSystem::phaseIsActive(waterPhaseIdx) &&
1122 FluidSystem::phaseIsActive(gasPhaseIdx))
1123 {
1124 this->updateGasWaterDistribution(globalDofIdx, fs, fip);
1125 }
1126
1127 if (FluidSystem::phaseIsActive(gasPhaseIdx) &&
1128 this->fipC_.hasCo2InGas())
1129 {
1130 this->updateCO2InGas(globalDofIdx, pv, intQuants);
1131 }
1132
1133 if (this->fipC_.hasCo2InWater() &&
1134 (FluidSystem::phaseIsActive(waterPhaseIdx) ||
1135 FluidSystem::phaseIsActive(oilPhaseIdx)))
1136 {
1137 this->updateCO2InWater(globalDofIdx, pv, fs);
1138 }
1139
1140 if constexpr(enableBioeffects) {
1141 const auto surfVolWat = pv * getValue(fs.saturation(waterPhaseIdx)) *
1142 getValue(fs.invB(waterPhaseIdx));
1143 if (this->fipC_.hasMicrobialMass()) {
1144 this->updateMicrobialMass(globalDofIdx, intQuants, surfVolWat);
1145 }
1146 if (this->fipC_.hasBiofilmMass()) {
1147 this->updateBiofilmMass(globalDofIdx, intQuants, totVolume);
1148 }
1149 if constexpr(enableMICP) {
1150 if (this->fipC_.hasOxygenMass()) {
1151 this->updateOxygenMass(globalDofIdx, intQuants, surfVolWat);
1152 }
1153 if (this->fipC_.hasUreaMass()) {
1154 this->updateUreaMass(globalDofIdx, intQuants, surfVolWat);
1155 }
1156 if (this->fipC_.hasCalciteMass()) {
1157 this->updateCalciteMass(globalDofIdx, intQuants, totVolume);
1158 }
1159 }
1160 }
1161
1162 if (this->fipC_.hasWaterMass() && FluidSystem::phaseIsActive(waterPhaseIdx))
1163 {
1164 this->updateWaterMass(globalDofIdx, fs, fip);
1165 }
1166 }
1167
1168 template <typename FluidState, typename FIPArray>
1169 void updateOilGasDistribution(const unsigned globalDofIdx,
1170 const FluidState& fs,
1171 const FIPArray& fip)
1172 {
1173 // Gas dissolved in oil and vaporized oil
1174 const auto gasInPlaceLiquid = getValue(fs.Rs()) * fip[oilPhaseIdx];
1175 const auto oilInPlaceGas = getValue(fs.Rv()) * fip[gasPhaseIdx];
1176
1177 this->fipC_.assignOilGasDistribution(globalDofIdx, gasInPlaceLiquid, oilInPlaceGas);
1178 }
1179
1180 template <typename FluidState, typename FIPArray>
1181 void updateGasWaterDistribution(const unsigned globalDofIdx,
1182 const FluidState& fs,
1183 const FIPArray& fip)
1184 {
1185 // Gas dissolved in water and vaporized water
1186 const auto gasInPlaceWater = getValue(fs.Rsw()) * fip[waterPhaseIdx];
1187 const auto waterInPlaceGas = getValue(fs.Rvw()) * fip[gasPhaseIdx];
1188
1189 this->fipC_.assignGasWater(globalDofIdx, fip, gasInPlaceWater, waterInPlaceGas);
1190 }
1191
1192 template <typename IntensiveQuantities>
1193 void updateCO2InGas(const unsigned globalDofIdx,
1194 const double pv,
1195 const IntensiveQuantities& intQuants)
1196 {
1197 const auto& materialLawManager = this->simulator_.problem().materialLawManager();
1198 const auto& scaledDrainageInfo = materialLawManager.oilWaterScaledEpsInfoDrainage(globalDofIdx);
1199
1200 const auto& fs = intQuants.fluidState();
1201 Scalar sgcr = scaledDrainageInfo.Sgcr;
1202 if (materialLawManager.enableHysteresis()) {
1203 const auto& matParams = simulator_.problem().materialLawParams(globalDofIdx);
1204 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maximumTrapping*/false);
1205 }
1206
1207 Scalar trappedGasSaturation = scaledDrainageInfo.Sgcr;
1208 if (this->fipC_.has(Inplace::Phase::CO2MassInGasPhaseMaximumTrapped) ||
1209 this->fipC_.has(Inplace::Phase::CO2MassInGasPhaseMaximumUnTrapped))
1210 {
1211 if (materialLawManager.enableHysteresis()) {
1212 const auto& matParams = simulator_.problem().materialLawParams(globalDofIdx);
1213 // Get the maximum trapped gas saturation
1214 trappedGasSaturation = MaterialLaw::trappedGasSaturation(matParams, /*maximumTrapping*/true);
1215 }
1216 }
1217
1218 const Scalar sg = getValue(fs.saturation(gasPhaseIdx));
1219 Scalar strandedGasSaturation = scaledDrainageInfo.Sgcr;
1220 if (this->fipC_.has(Inplace::Phase::CO2MassInGasPhaseEffectiveTrapped) ||
1221 this->fipC_.has(Inplace::Phase::CO2MassInGasPhaseEffectiveUnTrapped))
1222 {
1223 if (materialLawManager.enableHysteresis()) {
1224 const auto& matParams = simulator_.problem().materialLawParams(globalDofIdx);
1225 const double krg = getValue(intQuants.relativePermeability(gasPhaseIdx));
1226 strandedGasSaturation = MaterialLaw::strandedGasSaturation(matParams, sg, krg);
1227 }
1228 }
1229
1230 const typename FIPContainer<FluidSystem>::Co2InGasInput v{
1231 pv,
1232 sg,
1233 sgcr,
1234 getValue(fs.density(gasPhaseIdx)),
1235 FluidSystem::phaseIsActive(waterPhaseIdx)
1236 ? FluidSystem::convertRvwToXgW(getValue(fs.Rvw()), fs.pvtRegionIndex())
1237 : FluidSystem::convertRvToXgO(getValue(fs.Rv()), fs.pvtRegionIndex()),
1238 FluidSystem::molarMass(gasCompIdx, fs.pvtRegionIndex()),
1239 trappedGasSaturation,
1240 strandedGasSaturation,
1241 };
1242
1243 this->fipC_.assignCo2InGas(globalDofIdx, v);
1244 }
1245
1246 template <typename FluidState>
1247 void updateCO2InWater(const unsigned globalDofIdx,
1248 const double pv,
1249 const FluidState& fs)
1250 {
1251 const auto co2InWater = FluidSystem::phaseIsActive(oilPhaseIdx)
1252 ? this->co2InWaterFromOil(fs, pv)
1253 : this->co2InWaterFromWater(fs, pv);
1254
1255 const Scalar mM = FluidSystem::molarMass(gasCompIdx, fs.pvtRegionIndex());
1256
1257 this->fipC_.assignCo2InWater(globalDofIdx, co2InWater, mM);
1258 }
1259
1260 template <typename FluidState>
1261 Scalar co2InWaterFromWater(const FluidState& fs, const double pv) const
1262 {
1263 const double rhow = getValue(fs.density(waterPhaseIdx));
1264 const double sw = getValue(fs.saturation(waterPhaseIdx));
1265 const double xwG = FluidSystem::convertRswToXwG(getValue(fs.Rsw()), fs.pvtRegionIndex());
1266
1267 const Scalar mM = FluidSystem::molarMass(gasCompIdx, fs.pvtRegionIndex());
1268
1269 return xwG * pv * rhow * sw / mM;
1270 }
1271
1272 template <typename FluidState>
1273 Scalar co2InWaterFromOil(const FluidState& fs, const double pv) const
1274 {
1275 const double rhoo = getValue(fs.density(oilPhaseIdx));
1276 const double so = getValue(fs.saturation(oilPhaseIdx));
1277 const double xoG = FluidSystem::convertRsToXoG(getValue(fs.Rs()), fs.pvtRegionIndex());
1278
1279 const Scalar mM = FluidSystem::molarMass(gasCompIdx, fs.pvtRegionIndex());
1280
1281 return xoG * pv * rhoo * so / mM;
1282 }
1283
1284 template <typename FluidState, typename FIPArray>
1285 void updateWaterMass(const unsigned globalDofIdx,
1286 const FluidState& fs,
1287 const FIPArray& fip
1288 )
1289 {
1290 const Scalar rhoW = FluidSystem::referenceDensity(waterPhaseIdx, fs.pvtRegionIndex());
1291
1292 this->fipC_.assignWaterMass(globalDofIdx, fip, rhoW);
1293 }
1294
1295 template <typename IntensiveQuantities>
1296 void updateMicrobialMass(const unsigned globalDofIdx,
1297 const IntensiveQuantities& intQuants,
1298 const double surfVolWat)
1299 {
1300 const Scalar mass = surfVolWat * intQuants.microbialConcentration().value();
1301
1302 this->fipC_.assignMicrobialMass(globalDofIdx, mass);
1303 }
1304
1305 template <typename IntensiveQuantities>
1306 void updateOxygenMass(const unsigned globalDofIdx,
1307 const IntensiveQuantities& intQuants,
1308 const double surfVolWat)
1309 {
1310 const Scalar mass = surfVolWat * intQuants.oxygenConcentration().value();
1311
1312 this->fipC_.assignOxygenMass(globalDofIdx, mass);
1313 }
1314
1315 template <typename IntensiveQuantities>
1316 void updateUreaMass(const unsigned globalDofIdx,
1317 const IntensiveQuantities& intQuants,
1318 const double surfVolWat)
1319 {
1320 const Scalar mass = surfVolWat * intQuants.ureaConcentration().value();
1321
1322 this->fipC_.assignUreaMass(globalDofIdx, mass);
1323 }
1324
1325 template <typename IntensiveQuantities>
1326 void updateBiofilmMass(const unsigned globalDofIdx,
1327 const IntensiveQuantities& intQuants,
1328 const double totVolume)
1329 {
1330 const Scalar mass = totVolume * intQuants.biofilmMass().value();
1331
1332 this->fipC_.assignBiofilmMass(globalDofIdx, mass);
1333 }
1334
1335 template <typename IntensiveQuantities>
1336 void updateCalciteMass(const unsigned globalDofIdx,
1337 const IntensiveQuantities& intQuants,
1338 const double totVolume)
1339 {
1340 const Scalar mass = totVolume * intQuants.calciteMass().value();
1341
1342 this->fipC_.assignCalciteMass(globalDofIdx, mass);
1343 }
1344
1346 void setupElementExtractors_()
1347 {
1348 using Entry = typename Extractor::Entry;
1349 using Context = typename Extractor::Context;
1350 using ScalarEntry = typename Extractor::ScalarEntry;
1351 using PhaseEntry = typename Extractor::PhaseEntry;
1352
1353 const bool hasResidual = simulator_.model().linearizer().residual().size() > 0;
1354 const auto& hysteresisConfig = simulator_.problem().materialLawManager().hysteresisConfig();
1355
1356 auto extractors = std::array{
1357 Entry{PhaseEntry{&this->saturation_,
1358 [](const unsigned phase, const Context& ectx)
1359 { return getValue(ectx.fs.saturation(phase)); }
1360 }
1361 },
1362 Entry{PhaseEntry{&this->formationVolFactor_,
1363 [](const unsigned phase, const Context& ectx)
1364 {
1365 constexpr auto zero = Scalar{};
1366
1367 const auto recipFVF = getValue(ectx.fs.invB(phase));
1368
1369 return (recipFVF > zero)
1370 ? static_cast<Scalar>(1) / recipFVF
1371 : zero; // Convenient lie. Could possibly 'throw' here.
1372 }
1373 }
1374 },
1375 Entry{PhaseEntry{&this->invB_,
1376 [](const unsigned phase, const Context& ectx)
1377 { return getValue(ectx.fs.invB(phase)); }
1378 }
1379 },
1380 Entry{PhaseEntry{&this->density_,
1381 [](const unsigned phase, const Context& ectx)
1382 { return getValue(ectx.fs.density(phase)); }
1383 }
1384 },
1385 Entry{PhaseEntry{&this->relativePermeability_,
1386 [](const unsigned phase, const Context& ectx)
1387 { return getValue(ectx.intQuants.relativePermeability(phase)); }
1388 }
1389 },
1390 Entry{PhaseEntry{&this->viscosity_,
1391 [this](const unsigned phaseIdx, const Context& ectx)
1392 {
1394 if constexpr (enableExtbo) {
1395 if (this->extboC_.allocated() && phaseIdx == oilPhaseIdx) {
1396 return getValue(ectx.intQuants.oilViscosity());
1397 }
1398 else if (this->extboC_.allocated() && phaseIdx == gasPhaseIdx) {
1399 return getValue(ectx.intQuants.gasViscosity());
1400 }
1401 }
1402 return getValue(ectx.fs.viscosity(phaseIdx));
1403 }
1404 }
1405 },
1406 Entry{PhaseEntry{&this->residual_,
1407 [&modelResid = this->simulator_.model().linearizer().residual()]
1408 (const unsigned phaseIdx, const Context& ectx)
1409 {
1410 const unsigned sIdx = FluidSystem::solventComponentIndex(phaseIdx);
1411 const unsigned activeCompIdx = FluidSystem::canonicalToActiveCompIdx(sIdx);
1412 return modelResid[ectx.globalDofIdx][activeCompIdx];
1413 }
1414 },
1415 hasResidual
1416 },
1417 Entry{ScalarEntry{&this->rockCompPorvMultiplier_,
1418 [&problem = this->simulator_.problem()](const Context& ectx)
1419 {
1420 return problem.template
1421 rockCompPoroMultiplier<Scalar>(ectx.intQuants,
1422 ectx.globalDofIdx);
1423 }
1424 }
1425 },
1426 Entry{ScalarEntry{&this->rockCompTransMultiplier_,
1427 [&problem = this->simulator_.problem()](const Context& ectx)
1428 {
1429 return problem.
1430 template rockCompTransMultiplier<Scalar>(ectx.intQuants,
1431 ectx.globalDofIdx);
1432 }}
1433 },
1434 Entry{ScalarEntry{&this->minimumOilPressure_,
1435 [&problem = this->simulator_.problem()](const Context& ectx)
1436 {
1437 return std::min(getValue(ectx.fs.pressure(oilPhaseIdx)),
1438 problem.minOilPressure(ectx.globalDofIdx));
1439 }
1440 }
1441 },
1442 Entry{ScalarEntry{&this->bubblePointPressure_,
1443 [&failedCells = this->failedCellsPb_,
1444 &vanguard = this->simulator_.vanguard()](const Context& ectx)
1445 {
1446 try {
1447 return getValue(
1448 FluidSystem::bubblePointPressure(ectx.fs,
1449 ectx.intQuants.pvtRegionIndex())
1450 );
1451 } catch (const NumericalProblem&) {
1452 const auto cartesianIdx = vanguard.cartesianIndex(ectx.globalDofIdx);
1453 failedCells.push_back(cartesianIdx);
1454 return Scalar{0};
1455 }
1456 }
1457 }
1458 },
1459 Entry{ScalarEntry{&this->dewPointPressure_,
1460 [&failedCells = this->failedCellsPd_,
1461 &vanguard = this->simulator_.vanguard()](const Context& ectx)
1462 {
1463 try {
1464 return getValue(
1465 FluidSystem::dewPointPressure(ectx.fs,
1466 ectx.intQuants.pvtRegionIndex())
1467 );
1468 } catch (const NumericalProblem&) {
1469 const auto cartesianIdx = vanguard.cartesianIndex(ectx.globalDofIdx);
1470 failedCells.push_back(cartesianIdx);
1471 return Scalar{0};
1472 }
1473 }
1474 }
1475 },
1476 Entry{ScalarEntry{&this->overburdenPressure_,
1477 [&problem = simulator_.problem()](const Context& ectx)
1478 { return problem.overburdenPressure(ectx.globalDofIdx); }
1479 }
1480 },
1481 Entry{ScalarEntry{&this->temperature_,
1482 [](const Context& ectx)
1483 { return getValue(ectx.fs.temperature(oilPhaseIdx)); }
1484 }
1485 },
1486 Entry{ScalarEntry{&this->sSol_,
1487 [](const Context& ectx)
1488 {
1489 if constexpr (enableSolvent) {
1490 return getValue(ectx.intQuants.solventSaturation());
1491 }
1492 else {
1493 return Scalar{0};
1494 }
1495 }
1496 }
1497 },
1498 Entry{ScalarEntry{&this->rswSol_,
1499 [](const Context& ectx)
1500 {
1501 if constexpr (enableSolvent) {
1502 return getValue(ectx.intQuants.rsSolw());
1503 }
1504 else {
1505 return Scalar{0};
1506 }
1507 }
1508 }
1509 },
1510 Entry{ScalarEntry{&this->cPolymer_,
1511 [](const Context& ectx)
1512 {
1513 if constexpr (enablePolymer) {
1514 return getValue(ectx.intQuants.polymerConcentration());
1515 }
1516 else {
1517 return Scalar{0};
1518 }
1519 }
1520 }
1521 },
1522 Entry{ScalarEntry{&this->cFoam_,
1523 [](const Context& ectx)
1524 {
1525 if constexpr (enableFoam) {
1526 return getValue(ectx.intQuants.foamConcentration());
1527 }
1528 else {
1529 return Scalar{0};
1530 }
1531 }
1532 }
1533 },
1534 Entry{ScalarEntry{&this->cSalt_,
1535 [](const Context& ectx)
1536 { return getValue(ectx.fs.saltConcentration()); }
1537 }
1538 },
1539 Entry{ScalarEntry{&this->pSalt_,
1540 [](const Context& ectx)
1541 { return getValue(ectx.fs.saltSaturation()); }
1542 }
1543 },
1544 Entry{ScalarEntry{&this->permFact_,
1545 [](const Context& ectx)
1546 { return getValue(ectx.intQuants.permFactor()); }
1547 }
1548 },
1549 Entry{ScalarEntry{&this->rPorV_,
1550 [&model = this->simulator_.model()](const Context& ectx)
1551 {
1552 const auto totVolume = model.dofTotalVolume(ectx.globalDofIdx);
1553 return totVolume * getValue(ectx.intQuants.porosity());
1554 }
1555 }
1556 },
1557 Entry{ScalarEntry{&this->rs_,
1558 [](const Context& ectx)
1559 { return getValue(ectx.fs.Rs()); }
1560 }
1561 },
1562 Entry{ScalarEntry{&this->rv_,
1563 [](const Context& ectx)
1564 { return getValue(ectx.fs.Rv()); }
1565 }
1566 },
1567 Entry{ScalarEntry{&this->rsw_,
1568 [](const Context& ectx)
1569 { return getValue(ectx.fs.Rsw()); }
1570 }
1571 },
1572 Entry{ScalarEntry{&this->rvw_,
1573 [](const Context& ectx)
1574 { return getValue(ectx.fs.Rvw()); }
1575 }
1576 },
1577 Entry{ScalarEntry{&this->ppcw_,
1578 [&matLawManager = this->simulator_.problem().materialLawManager()]
1579 (const Context& ectx)
1580 {
1581 return matLawManager.swatinitMaxPc(ectx.globalDofIdx);
1582 }
1583 }
1584 },
1585 Entry{ScalarEntry{&this->drsdtcon_,
1586 [&problem = this->simulator_.problem()](const Context& ectx)
1587 {
1588 return problem.drsdtcon(ectx.globalDofIdx,
1589 ectx.episodeIndex);
1590 }
1591 }
1592 },
1593 Entry{ScalarEntry{&this->pcgw_,
1594 [](const Context& ectx)
1595 {
1596 return getValue(ectx.fs.pressure(gasPhaseIdx)) -
1597 getValue(ectx.fs.pressure(waterPhaseIdx));
1598 }
1599 }
1600 },
1601 Entry{ScalarEntry{&this->pcow_,
1602 [](const Context& ectx)
1603 {
1604 return getValue(ectx.fs.pressure(oilPhaseIdx)) -
1605 getValue(ectx.fs.pressure(waterPhaseIdx));
1606 }
1607 }
1608 },
1609 Entry{ScalarEntry{&this->pcog_,
1610 [](const Context& ectx)
1611 {
1612 return getValue(ectx.fs.pressure(gasPhaseIdx)) -
1613 getValue(ectx.fs.pressure(oilPhaseIdx));
1614 }
1615 }
1616 },
1617 Entry{ScalarEntry{&this->fluidPressure_,
1618 [](const Context& ectx)
1619 {
1620 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
1621 // Output oil pressure as default
1622 return getValue(ectx.fs.pressure(oilPhaseIdx));
1623 }
1624 else if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
1625 // Output gas if oil is not present
1626 return getValue(ectx.fs.pressure(gasPhaseIdx));
1627 }
1628 else {
1629 // Output water if neither oil nor gas is present
1630 return getValue(ectx.fs.pressure(waterPhaseIdx));
1631 }
1632 }
1633 }
1634 },
1635 Entry{ScalarEntry{&this->gasDissolutionFactor_,
1636 [&problem = this->simulator_.problem()](const Context& ectx)
1637 {
1638 const Scalar SoMax = problem.maxOilSaturation(ectx.globalDofIdx);
1639 return FluidSystem::template
1640 saturatedDissolutionFactor<FluidState, Scalar>(ectx.fs,
1641 oilPhaseIdx,
1642 ectx.pvtRegionIdx,
1643 SoMax);
1644 }
1645 }
1646 },
1647 Entry{ScalarEntry{&this->oilVaporizationFactor_,
1648 [&problem = this->simulator_.problem()](const Context& ectx)
1649 {
1650 const Scalar SoMax = problem.maxOilSaturation(ectx.globalDofIdx);
1651 return FluidSystem::template
1652 saturatedDissolutionFactor<FluidState, Scalar>(ectx.fs,
1653 gasPhaseIdx,
1654 ectx.pvtRegionIdx,
1655 SoMax);
1656 }
1657 }
1658 },
1659 Entry{ScalarEntry{&this->gasDissolutionFactorInWater_,
1660 [&problem = this->simulator_.problem()](const Context& ectx)
1661 {
1662 const Scalar SwMax = problem.maxWaterSaturation(ectx.globalDofIdx);
1663 return FluidSystem::template
1664 saturatedDissolutionFactor<FluidState, Scalar>(ectx.fs,
1665 waterPhaseIdx,
1666 ectx.pvtRegionIdx,
1667 SwMax);
1668 }
1669 }
1670 },
1671 Entry{ScalarEntry{&this->waterVaporizationFactor_,
1672 [](const Context& ectx)
1673 {
1674 return FluidSystem::template
1675 saturatedVaporizationFactor<FluidState, Scalar>(ectx.fs,
1676 gasPhaseIdx,
1677 ectx.pvtRegionIdx);
1678 }
1679 }
1680 },
1681 Entry{ScalarEntry{&this->gasFormationVolumeFactor_,
1682 [](const Context& ectx)
1683 {
1684 return 1.0 / FluidSystem::template
1685 inverseFormationVolumeFactor<FluidState, Scalar>(ectx.fs,
1686 gasPhaseIdx,
1687 ectx.pvtRegionIdx);
1688 }
1689 }
1690 },
1691 Entry{ScalarEntry{&this->saturatedOilFormationVolumeFactor_,
1692 [](const Context& ectx)
1693 {
1694 return 1.0 / FluidSystem::template
1695 saturatedInverseFormationVolumeFactor<FluidState, Scalar>(ectx.fs,
1696 oilPhaseIdx,
1697 ectx.pvtRegionIdx);
1698 }
1699 }
1700 },
1701 Entry{ScalarEntry{&this->oilSaturationPressure_,
1702 [](const Context& ectx)
1703 {
1704 return FluidSystem::template
1705 saturationPressure<FluidState, Scalar>(ectx.fs,
1706 oilPhaseIdx,
1707 ectx.pvtRegionIdx);
1708 }
1709 }
1710 },
1711 Entry{ScalarEntry{&this->soMax_,
1712 [&problem = this->simulator_.problem()](const Context& ectx)
1713 {
1714 return std::max(getValue(ectx.fs.saturation(oilPhaseIdx)),
1715 problem.maxOilSaturation(ectx.globalDofIdx));
1716 }
1717 },
1718 !hysteresisConfig.enableHysteresis()
1719 },
1720 Entry{ScalarEntry{&this->swMax_,
1721 [&problem = this->simulator_.problem()](const Context& ectx)
1722 {
1723 return std::max(getValue(ectx.fs.saturation(waterPhaseIdx)),
1724 problem.maxWaterSaturation(ectx.globalDofIdx));
1725 }
1726 },
1727 !hysteresisConfig.enableHysteresis()
1728 },
1729 Entry{ScalarEntry{&this->soMax_,
1730 [](const Context& ectx)
1731 { return ectx.hParams.somax; }
1732 },
1733 hysteresisConfig.enableHysteresis() &&
1734 hysteresisConfig.enableNonWettingHysteresis() &&
1735 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1736 FluidSystem::phaseIsActive(waterPhaseIdx)
1737 },
1738 Entry{ScalarEntry{&this->swMax_,
1739 [](const Context& ectx)
1740 { return ectx.hParams.swmax; }
1741 },
1742 hysteresisConfig.enableHysteresis() &&
1743 hysteresisConfig.enableWettingHysteresis() &&
1744 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1745 FluidSystem::phaseIsActive(waterPhaseIdx)
1746 },
1747 Entry{ScalarEntry{&this->swmin_,
1748 [](const Context& ectx)
1749 { return ectx.hParams.swmin; }
1750 },
1751 hysteresisConfig.enableHysteresis() &&
1752 hysteresisConfig.enablePCHysteresis() &&
1753 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1754 FluidSystem::phaseIsActive(waterPhaseIdx)
1755 },
1756 Entry{ScalarEntry{&this->sgmax_,
1757 [](const Context& ectx)
1758 { return ectx.hParams.sgmax; }
1759 },
1760 hysteresisConfig.enableHysteresis() &&
1761 hysteresisConfig.enableNonWettingHysteresis() &&
1762 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1763 FluidSystem::phaseIsActive(gasPhaseIdx)
1764 },
1765 Entry{ScalarEntry{&this->shmax_,
1766 [](const Context& ectx)
1767 { return ectx.hParams.shmax; }
1768 },
1769 hysteresisConfig.enableHysteresis() &&
1770 hysteresisConfig.enableWettingHysteresis() &&
1771 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1772 FluidSystem::phaseIsActive(gasPhaseIdx)
1773 },
1774 Entry{ScalarEntry{&this->somin_,
1775 [](const Context& ectx)
1776 { return ectx.hParams.somin; }
1777 },
1778 hysteresisConfig.enableHysteresis() &&
1779 hysteresisConfig.enablePCHysteresis() &&
1780 FluidSystem::phaseIsActive(oilPhaseIdx) &&
1781 FluidSystem::phaseIsActive(gasPhaseIdx)
1782 },
1783 Entry{[&model = this->simulator_.model(), this](const Context& ectx)
1784 {
1785 // Note: We intentionally exclude effects of rock
1786 // compressibility by using referencePorosity() here.
1787 const auto porv = ectx.intQuants.referencePorosity()
1788 * model.dofTotalVolume(ectx.globalDofIdx);
1789
1790 this->aggregateAverageDensityContributions_(ectx.fs, ectx.globalDofIdx,
1791 static_cast<double>(porv));
1792 }, this->regionAvgDensity_.has_value()
1793 },
1794 Entry{[&extboC = this->extboC_](const Context& ectx)
1795 {
1796 detail::ignoreUnused(extboC);
1797 if constexpr (enableExtbo) {
1798 extboC.assignVolumes(ectx.globalDofIdx,
1799 ectx.intQuants.xVolume().value(),
1800 ectx.intQuants.yVolume().value());
1801 extboC.assignZFraction(ectx.globalDofIdx,
1802 ectx.intQuants.zFraction().value());
1803
1804 const Scalar stdVolOil = getValue(ectx.fs.saturation(oilPhaseIdx)) *
1805 getValue(ectx.fs.invB(oilPhaseIdx)) +
1806 getValue(ectx.fs.saturation(gasPhaseIdx)) *
1807 getValue(ectx.fs.invB(gasPhaseIdx)) *
1808 getValue(ectx.fs.Rv());
1809 const Scalar stdVolGas = getValue(ectx.fs.saturation(gasPhaseIdx)) *
1810 getValue(ectx.fs.invB(gasPhaseIdx)) *
1811 (1.0 - ectx.intQuants.yVolume().value()) +
1812 getValue(ectx.fs.saturation(oilPhaseIdx)) *
1813 getValue(ectx.fs.invB(oilPhaseIdx)) *
1814 getValue(ectx.fs.Rs()) *
1815 (1.0 - ectx.intQuants.xVolume().value());
1816 const Scalar stdVolCo2 = getValue(ectx.fs.saturation(gasPhaseIdx)) *
1817 getValue(ectx.fs.invB(gasPhaseIdx)) *
1818 ectx.intQuants.yVolume().value() +
1819 getValue(ectx.fs.saturation(oilPhaseIdx)) *
1820 getValue(ectx.fs.invB(oilPhaseIdx)) *
1821 getValue(ectx.fs.Rs()) *
1822 ectx.intQuants.xVolume().value();
1823 const Scalar rhoO = FluidSystem::referenceDensity(oilPhaseIdx, ectx.pvtRegionIdx);
1824 const Scalar rhoG = FluidSystem::referenceDensity(gasPhaseIdx, ectx.pvtRegionIdx);
1825 const Scalar rhoCO2 = ectx.intQuants.zRefDensity();
1826 const Scalar stdMassTotal = 1.0e-10 + stdVolOil * rhoO + stdVolGas * rhoG + stdVolCo2 * rhoCO2;
1827 extboC.assignMassFractions(ectx.globalDofIdx,
1828 stdVolGas * rhoG / stdMassTotal,
1829 stdVolOil * rhoO / stdMassTotal,
1830 stdVolCo2 * rhoCO2 / stdMassTotal);
1831
1832 if (extboC.phaseMassFractionsRequested()) {
1833 const auto [oil, gas] = extboC.phaseSolventMassFractions({
1834 .oilSaturation = getValue(ectx.fs.saturation(oilPhaseIdx)),
1835 .gasSaturation = getValue(ectx.fs.saturation(gasPhaseIdx)),
1836 .rs = getValue(ectx.fs.Rs()),
1837 .rv = getValue(ectx.fs.Rv()),
1838 .xVolume = ectx.intQuants.xVolume().value(),
1839 .yVolume = ectx.intQuants.yVolume().value(),
1840 .oilDensity = rhoO,
1841 .gasDensity = rhoG,
1842 .solventDensity = rhoCO2,
1843 });
1844 extboC.assignPhaseMassFractions(ectx.globalDofIdx, oil, gas);
1845 }
1846 }
1847 }, this->extboC_.allocated()
1848 },
1849 Entry{[&bioeffectsC = this->bioeffectsC_](const Context& ectx)
1850 {
1851 detail::ignoreUnused(bioeffectsC);
1852 if constexpr (enableBioeffects) {
1853 bioeffectsC.assign(ectx.globalDofIdx,
1854 ectx.intQuants.microbialConcentration().value(),
1855 ectx.intQuants.biofilmVolumeFraction().value());
1856 if (Indices::enableMICP) {
1857 bioeffectsC.assign(ectx.globalDofIdx,
1858 ectx.intQuants.oxygenConcentration().value(),
1859 ectx.intQuants.ureaConcentration().value(),
1860 ectx.intQuants.calciteVolumeFraction().value());
1861 }
1862 }
1863 }, this->bioeffectsC_.allocated()
1864 },
1865 Entry{[&runspec = this->eclState_.runspec(),
1866 &CO2H2C = this->CO2H2C_](const Context& ectx)
1867 {
1868 const auto xwg = FluidSystem::convertRswToXwG(getValue(ectx.fs.Rsw()), ectx.pvtRegionIdx);
1869 const auto xgw = FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.pvtRegionIdx);
1870 CO2H2C.assign(ectx.globalDofIdx,
1871 FluidSystem::convertXwGToxwG(xwg, ectx.pvtRegionIdx),
1872 FluidSystem::convertXgWToxgW(xgw, ectx.pvtRegionIdx),
1873 runspec.co2Storage());
1874 }, this->CO2H2C_.allocated()
1875 },
1876 Entry{[&rftC = this->rftC_,
1877 &vanguard = this->simulator_.vanguard()](const Context& ectx)
1878 {
1879 const auto cartesianIdx = vanguard.cartesianIndex(ectx.globalDofIdx);
1880 rftC.assign(cartesianIdx,
1881 [&fs = ectx.fs]()
1882 {
1883 // Reference phase pressure, as for PRESSURE/BPR:
1884 // oil if present, otherwise gas, otherwise water.
1885 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
1886 return getValue(fs.pressure(oilPhaseIdx));
1887 }
1888 else if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
1889 return getValue(fs.pressure(gasPhaseIdx));
1890 }
1891 else {
1892 return getValue(fs.pressure(waterPhaseIdx));
1893 }
1894 },
1895 [&fs = ectx.fs]() { return getValue(fs.saturation(waterPhaseIdx)); },
1896 [&fs = ectx.fs]() { return getValue(fs.saturation(gasPhaseIdx)); });
1897 }
1898 },
1899 Entry{[&tC = this->tracerC_,
1900 &tM = this->simulator_.problem().tracerModel()](const Context& ectx)
1901 {
1902 tC.assignFreeConcentrations(ectx.globalDofIdx,
1903 [gIdx = ectx.globalDofIdx, &tM](const unsigned tracerIdx)
1904 { return tM.freeTracerConcentration(tracerIdx, gIdx); });
1905 tC.assignSolConcentrations(ectx.globalDofIdx,
1906 [gIdx = ectx.globalDofIdx, &tM](const unsigned tracerIdx)
1907 { return tM.solTracerConcentration(tracerIdx, gIdx); });
1908 }
1909 },
1910 Entry{[&flowsInf = this->simulator_.problem().model().linearizer().getFlowsInfo(),
1911 &flowsC = this->flowsC_,
1912 &vanguard = this->simulator_.vanguard()](const Context& ectx)
1913 {
1914 const auto gas_idx = Indices::gasEnabled ?
1915 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(gasCompIdx) : -1;
1916 const auto oil_idx = Indices::oilEnabled ?
1917 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(oilCompIdx) : -1;
1918 const auto water_idx = Indices::waterEnabled ?
1919 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(waterCompIdx) : -1;
1920 const auto& flowsInfos = flowsInf[ectx.globalDofIdx];
1921 if (!flowsC.blockFlows().empty()) {
1922 const std::vector<int>& blockIdxs = flowsC.blockFlows();
1923 const unsigned cartesianIdx = vanguard.cartesianIndex(ectx.globalDofIdx);
1924 if (std::ranges::binary_search(blockIdxs, cartesianIdx)) {
1925 const auto compIdxs = std::array{ gasCompIdx, oilCompIdx, waterCompIdx };
1926 const auto compEnabled = std::array{ Indices::gasEnabled, Indices::oilEnabled, Indices::waterEnabled };
1927 for (const auto& flowsInfo : flowsInfos) {
1928 if (flowsInfo.faceId < 0) {
1929 continue;
1930 }
1931 for (unsigned ii = 0; ii < compIdxs.size(); ++ii) {
1932 if (!compEnabled[ii]) {
1933 continue;
1934 }
1935 if (flowsC.hasBlockFlowValue(cartesianIdx, flowsInfo.faceId, compIdxs[ii])) {
1936 flowsC.assignBlockFlows(flowsC.blockFlowsIds(cartesianIdx, flowsInfo.faceId, compIdxs[ii]),
1937 flowsInfo.faceId,
1938 compIdxs[ii],
1939 flowsInfo.flow[conti0EqIdx
1940 + FluidSystem::canonicalToActiveCompIdx(compIdxs[ii])]);
1941 }
1942 }
1943 }
1944 }
1945 }
1946 else {
1947 for (const auto& flowsInfo : flowsInfos) {
1948 flowsC.assignFlows(ectx.globalDofIdx,
1949 flowsInfo.faceId,
1950 flowsInfo.nncId,
1951 value_or_zero(gas_idx, flowsInfo.flow),
1952 value_or_zero(oil_idx, flowsInfo.flow),
1953 value_or_zero(water_idx, flowsInfo.flow));
1954 }
1955 }
1956 }, !this->simulator_.problem().model().linearizer().getFlowsInfo().empty()
1957 },
1958 Entry{[&floresInf = this->simulator_.problem().model().linearizer().getFloresInfo(),
1959 &flowsC = this->flowsC_](const Context& ectx)
1960 {
1961 const auto gas_idx = Indices::gasEnabled ?
1962 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(gasCompIdx) : -1;
1963 const auto oil_idx = Indices::oilEnabled ?
1964 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(oilCompIdx) : -1;
1965 const auto water_idx = Indices::waterEnabled ?
1966 conti0EqIdx + FluidSystem::canonicalToActiveCompIdx(waterCompIdx) : -1;
1967 const auto& floresInfos = floresInf[ectx.globalDofIdx];
1968 for (const auto& floresInfo : floresInfos) {
1969 flowsC.assignFlores(ectx.globalDofIdx,
1970 floresInfo.faceId,
1971 floresInfo.nncId,
1972 value_or_zero(gas_idx, floresInfo.flow),
1973 value_or_zero(oil_idx, floresInfo.flow),
1974 value_or_zero(water_idx, floresInfo.flow));
1975 }
1976 }, !this->simulator_.problem().model().linearizer().getFloresInfo().empty()
1977 },
1978 Entry{[&velocityInf = this->simulator_.problem().model().linearizer().getVelocityInfo(),
1979 &flowsC = this->flowsC_,
1980 &vanguard = this->simulator_.vanguard()](const Context& ectx)
1981 {
1982 const auto& velocityInfos = velocityInf[ectx.globalDofIdx];
1983 const std::vector<int>& blockIdxs = flowsC.blockVelocity();
1984 const unsigned cartesianIdx = vanguard.cartesianIndex(ectx.globalDofIdx);
1985 if (std::ranges::binary_search(blockIdxs, cartesianIdx)) {
1986 const auto compIdxs = std::array{ gasCompIdx, oilCompIdx, waterCompIdx };
1987 const auto compEnabled = std::array{ Indices::gasEnabled, Indices::oilEnabled, Indices::waterEnabled };
1988 for (const auto& velocityInfo : velocityInfos) {
1989 if (velocityInfo.faceId < 0) {
1990 continue;
1991 }
1992 for (unsigned ii = 0; ii < compIdxs.size(); ++ii) {
1993 if (!compEnabled[ii]) {
1994 continue;
1995 }
1996 if (flowsC.hasBlockVelocityValue(cartesianIdx, velocityInfo.faceId, compIdxs[ii])) {
1997 flowsC.assignBlockVelocity(flowsC.blockVelocityIds(cartesianIdx, velocityInfo.faceId, compIdxs[ii]),
1998 velocityInfo.faceId,
1999 compIdxs[ii],
2000 velocityInfo.velocity[conti0EqIdx
2001 + FluidSystem::canonicalToActiveCompIdx(compIdxs[ii])]);
2002 }
2003 }
2004 }
2005 }
2006 }, !this->flowsC_.blockVelocity().empty() &&
2007 !this->simulator_.problem().model().linearizer().getVelocityInfo().empty()
2008 },
2009 // hack to make the intial output of rs and rv Ecl compatible.
2010 // For cells with swat == 1 Ecl outputs; rs = rsSat and rv=rvSat, in all but the initial step
2011 // where it outputs rs and rv values calculated by the initialization. To be compatible we overwrite
2012 // rs and rv with the values computed in the initially.
2013 // Volume factors, densities and viscosities need to be recalculated with the updated rs and rv values.
2014 Entry{ScalarEntry{&this->rv_,
2015 [&problem = this->simulator_.problem()](const Context& ectx)
2016 { return problem.initialFluidState(ectx.globalDofIdx).Rv(); }
2017 },
2018 simulator_.episodeIndex() < 0 &&
2019 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2020 FluidSystem::phaseIsActive(gasPhaseIdx)
2021 },
2022 Entry{ScalarEntry{&this->rs_,
2023 [&problem = this->simulator_.problem()](const Context& ectx)
2024 { return problem.initialFluidState(ectx.globalDofIdx).Rs(); }
2025 },
2026 simulator_.episodeIndex() < 0 &&
2027 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2028 FluidSystem::phaseIsActive(gasPhaseIdx)
2029 },
2030 Entry{ScalarEntry{&this->rsw_,
2031 [&problem = this->simulator_.problem()](const Context& ectx)
2032 { return problem.initialFluidState(ectx.globalDofIdx).Rsw(); }
2033 },
2034 simulator_.episodeIndex() < 0 &&
2035 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2036 FluidSystem::phaseIsActive(gasPhaseIdx)
2037 },
2038 Entry{ScalarEntry{&this->rvw_,
2039 [&problem = this->simulator_.problem()](const Context& ectx)
2040 { return problem.initialFluidState(ectx.globalDofIdx).Rvw(); }
2041 },
2042 simulator_.episodeIndex() < 0 &&
2043 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2044 FluidSystem::phaseIsActive(gasPhaseIdx)
2045 },
2046 // re-compute the volume factors, viscosities and densities if asked for
2047 Entry{PhaseEntry{&this->density_,
2048 [&problem = this->simulator_.problem()](const unsigned phase,
2049 const Context& ectx)
2050 {
2051 const auto& fsInitial = problem.initialFluidState(ectx.globalDofIdx);
2052 return FluidSystem::density(fsInitial,
2053 phase,
2054 ectx.intQuants.pvtRegionIndex());
2055 }
2056 },
2057 simulator_.episodeIndex() < 0 &&
2058 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2059 FluidSystem::phaseIsActive(gasPhaseIdx)
2060 },
2061 Entry{PhaseEntry{&this->formationVolFactor_,
2062 [&problem = this->simulator_.problem()]
2063 (const unsigned phase, const Context& ectx)
2064 {
2065 constexpr auto zero = Scalar{};
2066
2067 const auto& fsInitial = problem.initialFluidState(ectx.globalDofIdx);
2068
2069 const auto recipFVF = getValue
2070 (FluidSystem::inverseFormationVolumeFactor
2071 (fsInitial, phase, ectx.intQuants.pvtRegionIndex()));
2072
2073 return (recipFVF > zero)
2074 ? static_cast<Scalar>(1) / recipFVF
2075 : zero; // Convenient lie. Could possibly 'throw' here.
2076 }
2077 },
2078 simulator_.episodeIndex() < 0 &&
2079 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2080 FluidSystem::phaseIsActive(gasPhaseIdx)
2081 },
2082 Entry{PhaseEntry{&this->invB_,
2083 [&problem = this->simulator_.problem()](const unsigned phase,
2084 const Context& ectx)
2085 {
2086 const auto& fsInitial = problem.initialFluidState(ectx.globalDofIdx);
2087 return FluidSystem::inverseFormationVolumeFactor(fsInitial,
2088 phase,
2089 ectx.intQuants.pvtRegionIndex());
2090 }
2091 },
2092 simulator_.episodeIndex() < 0 &&
2093 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2094 FluidSystem::phaseIsActive(gasPhaseIdx)
2095 },
2096 Entry{PhaseEntry{&this->viscosity_,
2097 [&problem = this->simulator_.problem()](const unsigned phase,
2098 const Context& ectx)
2099 {
2100 const auto& fsInitial = problem.initialFluidState(ectx.globalDofIdx);
2101 return FluidSystem::viscosity(fsInitial,
2102 phase,
2103 ectx.intQuants.pvtRegionIndex());
2104 }
2105 },
2106 simulator_.episodeIndex() < 0 &&
2107 FluidSystem::phaseIsActive(oilPhaseIdx) &&
2108 FluidSystem::phaseIsActive(gasPhaseIdx)
2109 },
2110 };
2111
2112 // Setup active extractors
2113 this->extractors_ = Extractor::removeInactive(extractors);
2114
2115 // Geochemistry
2116 if constexpr (getPropValue<TypeTag, Properties::EnableGeochemistry>()) {
2117 if (this->geochemC_.allocated()) {
2118 this->extractors_.emplace_back(
2119 [&gC = this->geochemC_,
2120 &gM = this->simulator_.problem().geochemistryModel()](const Context& ectx)
2121 {
2122 gC.assignSpeciesConcentrations(
2123 ectx.globalDofIdx,
2124 [gIdx = ectx.globalDofIdx, &gM](const unsigned speciesIdx)
2125 { return gM.speciesConcentration(speciesIdx, gIdx); }
2126 );
2127 gC.assignMineralConcentrations(
2128 ectx.globalDofIdx,
2129 [gIdx = ectx.globalDofIdx, &gM](const unsigned mineralIdx)
2130 { return gM.mineralConcentration(mineralIdx, gIdx); }
2131 );
2132 gC.assignPH(ectx.globalDofIdx, gM.PH(ectx.globalDofIdx));
2133 },
2134 true
2135 );
2136 }
2137 }
2138
2139 // Geomechanics
2140 if constexpr (getPropValue<TypeTag, Properties::EnableMech>()) {
2141 if (this->mech_.allocated()) {
2142 this->extractors_.emplace_back(
2143 [&mech = this->mech_,
2144 &model = simulator_.problem().geoMechModel()](const Context& ectx)
2145 {
2146 mech.assignDelStress(ectx.globalDofIdx,
2147 model.delstress(ectx.globalDofIdx));
2148
2149 mech.assignDisplacement(ectx.globalDofIdx,
2150 model.disp(ectx.globalDofIdx, /*include_fracture*/true));
2151
2152 // is the tresagii stress which make rock fracture
2153 mech.assignFracStress(ectx.globalDofIdx,
2154 model.fractureStress(ectx.globalDofIdx));
2155
2156 mech.assignLinStress(ectx.globalDofIdx,
2157 model.linstress(ectx.globalDofIdx));
2158
2159 mech.assignPotentialForces(ectx.globalDofIdx,
2160 model.mechPotentialForce(ectx.globalDofIdx),
2161 model.mechPotentialPressForce(ectx.globalDofIdx),
2162 model.mechPotentialTempForce(ectx.globalDofIdx));
2163
2164 mech.assignStrain(ectx.globalDofIdx,
2165 model.strain(ectx.globalDofIdx, /*include_fracture*/true));
2166
2167 // Total stress is not stored but calculated result is Voigt notation
2168 mech.assignStress(ectx.globalDofIdx,
2169 model.stress(ectx.globalDofIdx, /*include_fracture*/true));
2170
2171 // Only assign traction if TRACT/TRACT- is requested
2172 if (mech.enableTraction()) {
2173 mech.assignTraction(ectx.globalDofIdx,
2174 model.traction(ectx.globalDofIdx));
2175 }
2176 },
2177 true
2178 );
2179 }
2180 if (this->tpsaC_.allocated()) {
2181 this->extractors_.emplace_back(
2182 [&tpsaC = this->tpsaC_,
2183 &model = simulator_.problem().geoMechModel()](const Context& ectx) {
2184 tpsaC.assignRotation(ectx.globalDofIdx,
2185 model.rotation(ectx.globalDofIdx));
2186
2187 tpsaC.assignSolidPressure(ectx.globalDofIdx,
2188 model.solidPressure(ectx.globalDofIdx));
2189 },
2190 true
2191 );
2192 }
2193 }
2194 }
2195
2197 void setupBlockExtractors_(const bool isSubStep,
2198 const int reportStepNum)
2199 {
2200 using Entry = typename BlockExtractor::Entry;
2201 using Context = typename BlockExtractor::Context;
2202 using PhaseEntry = typename BlockExtractor::PhaseEntry;
2203 using ScalarEntry = typename BlockExtractor::ScalarEntry;
2204 using TensorEntry = typename BlockExtractor::TensorEntry;
2205
2206 using namespace std::string_view_literals;
2207
2208 const auto pressure_handler =
2209 Entry{ScalarEntry{std::vector{"BPR"sv, "BPRESSUR"sv},
2210 [](const Context& ectx)
2211 {
2212 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2213 return getValue(ectx.fs.pressure(oilPhaseIdx));
2214 }
2215 else if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
2216 return getValue(ectx.fs.pressure(gasPhaseIdx));
2217 }
2218 else { //if (FluidSystem::phaseIsActive(waterPhaseIdx))
2219 return getValue(ectx.fs.pressure(waterPhaseIdx));
2220 }
2221 }
2222 }
2223 };
2224
2225 const auto handlers = std::array{
2226 pressure_handler,
2227 Entry{PhaseEntry{std::array{
2228 std::array{"BWSAT"sv, "BOSAT"sv, "BGSAT"sv},
2229 std::array{"BSWAT"sv, "BSOIL"sv, "BSGAS"sv}
2230 },
2231 [](const unsigned phaseIdx, const Context& ectx)
2232 {
2233 return getValue(ectx.fs.saturation(phaseIdx));
2234 }
2235 }
2236 },
2237 Entry{ScalarEntry{"BNSAT",
2238 [](const Context& ectx)
2239 {
2240 if constexpr (enableSolvent) {
2241 return ectx.intQuants.solventSaturation().value();
2242 }
2243 else {
2244 return Scalar{0};
2245 }
2246 }
2247 }
2248 },
2249 Entry{ScalarEntry{std::vector{"BTCNFHEA"sv, "BTEMP"sv},
2250 [](const Context& ectx)
2251 {
2252 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2253 return getValue(ectx.fs.temperature(oilPhaseIdx));
2254 }
2255 else if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
2256 return getValue(ectx.fs.temperature(gasPhaseIdx));
2257 }
2258 else { //if (FluidSystem::phaseIsActive(waterPhaseIdx))
2259 return getValue(ectx.fs.temperature(waterPhaseIdx));
2260 }
2261 }
2262 }
2263 },
2264 Entry{PhaseEntry{std::array{
2265 std::array{"BWKR"sv, "BOKR"sv, "BGKR"sv},
2266 std::array{"BKRW"sv, "BKRO"sv, "BKRG"sv}
2267 },
2268 [](const unsigned phaseIdx, const Context& ectx)
2269 {
2270 return getValue(ectx.intQuants.relativePermeability(phaseIdx));
2271 }
2272 }
2273 },
2274 Entry{ScalarEntry{"BKROG",
2275 [&problem = this->simulator_.problem()](const Context& ectx)
2276 {
2277 const auto& materialParams =
2278 problem.materialLawParams(ectx.elemCtx,
2279 ectx.dofIdx,
2280 /* timeIdx = */ 0);
2281 return getValue(MaterialLaw::template
2282 relpermOilInOilGasSystem<Evaluation>(materialParams,
2283 ectx.fs));
2284 }
2285 }
2286 },
2287 Entry{ScalarEntry{"BKROW",
2288 [&problem = this->simulator_.problem()](const Context& ectx)
2289 {
2290 const auto& materialParams = problem.materialLawParams(ectx.elemCtx,
2291 ectx.dofIdx,
2292 /* timeIdx = */ 0);
2293 return getValue(MaterialLaw::template
2294 relpermOilInOilWaterSystem<Evaluation>(materialParams,
2295 ectx.fs));
2296 }
2297 }
2298 },
2299 Entry{ScalarEntry{"BWPC",
2300 [](const Context& ectx)
2301 {
2302 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2303 return getValue(ectx.fs.pressure(oilPhaseIdx)) -
2304 getValue(ectx.fs.pressure(waterPhaseIdx));
2305 }
2306 else if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
2307 return getValue(ectx.fs.pressure(gasPhaseIdx)) -
2308 getValue(ectx.fs.pressure(waterPhaseIdx));
2309 }
2310 else {
2311 return Scalar(0.0);
2312 }
2313 }
2314 }
2315 },
2316 Entry{ScalarEntry{"BGPC",
2317 [](const Context& ectx)
2318 {
2319 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2320 return getValue(ectx.fs.pressure(gasPhaseIdx)) -
2321 getValue(ectx.fs.pressure(oilPhaseIdx));
2322 }
2323 else if (FluidSystem::phaseIsActive(waterPhaseIdx)) {
2324 return getValue(ectx.fs.pressure(gasPhaseIdx)) -
2325 getValue(ectx.fs.pressure(waterPhaseIdx));
2326 }
2327 else {
2328 return Scalar(0.0);
2329 }
2330 }
2331 }
2332 },
2333 Entry{ScalarEntry{"BWPR",
2334 [](const Context& ectx)
2335 {
2336 return getValue(ectx.fs.pressure(waterPhaseIdx));
2337 }
2338 }
2339 },
2340 Entry{ScalarEntry{"BGPR",
2341 [](const Context& ectx)
2342 {
2343 return getValue(ectx.fs.pressure(gasPhaseIdx));
2344 }
2345 }
2346 },
2347 Entry{PhaseEntry{std::array{
2348 std::array{"BVWAT"sv, "BVOIL"sv, "BVGAS"sv},
2349 std::array{"BWVIS"sv, "BOVIS"sv, "BGVIS"sv}
2350 },
2351 [](const unsigned phaseIdx, const Context& ectx)
2352 {
2353 return getValue(ectx.fs.viscosity(phaseIdx));
2354 }
2355 }
2356 },
2357 Entry{PhaseEntry{std::array{
2358 std::array{"BWDEN"sv, "BODEN"sv, "BGDEN"sv},
2359 std::array{"BDENW"sv, "BDENO"sv, "BDENG"sv}
2360 },
2361 [](const unsigned phaseIdx, const Context& ectx)
2362 {
2363 return getValue(ectx.fs.density(phaseIdx));
2364 }
2365 }
2366 },
2367 Entry{ScalarEntry{"BFLOGI",
2368 [&flowsC = this->flowsC_,
2369 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2370 {
2371 const unsigned index = !flowsC.blockFlows().empty() ?
2372 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2373 FaceDir::ToIntersectionIndex(Dir::XPlus), gasCompIdx) : ectx.globalDofIdx;
2374 return flowsC.getFlow(index, Dir::XPlus, gasCompIdx);
2375 }
2376 }
2377 },
2378 Entry{ScalarEntry{"BFLOGI-",
2379 [&flowsC = this->flowsC_,
2380 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2381 {
2382 const unsigned index = !flowsC.blockFlows().empty() ?
2383 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2384 FaceDir::ToIntersectionIndex(Dir::XMinus), gasCompIdx) : ectx.globalDofIdx;
2385 return flowsC.getFlow(index, Dir::XMinus, gasCompIdx);
2386 }
2387 }
2388 },
2389 Entry{ScalarEntry{"BFLOGJ",
2390 [&flowsC = this->flowsC_,
2391 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2392 {
2393 const unsigned index = !flowsC.blockFlows().empty() ?
2394 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2395 FaceDir::ToIntersectionIndex(Dir::YPlus), gasCompIdx) : ectx.globalDofIdx;
2396 return flowsC.getFlow(index, Dir::YPlus, gasCompIdx);
2397 }
2398 }
2399 },
2400 Entry{ScalarEntry{"BFLOGJ-",
2401 [&flowsC = this->flowsC_,
2402 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2403 {
2404 const unsigned index = !flowsC.blockFlows().empty() ?
2405 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2406 FaceDir::ToIntersectionIndex(Dir::YMinus), gasCompIdx) : ectx.globalDofIdx;
2407 return flowsC.getFlow(index, Dir::YMinus, gasCompIdx);
2408 }
2409 }
2410 },
2411 Entry{ScalarEntry{"BFLOGK",
2412 [&flowsC = this->flowsC_,
2413 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2414 {
2415 const unsigned index = !flowsC.blockFlows().empty() ?
2416 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2417 FaceDir::ToIntersectionIndex(Dir::ZPlus), gasCompIdx) : ectx.globalDofIdx;
2418 return flowsC.getFlow(index, Dir::ZPlus, gasCompIdx);
2419 }
2420 }
2421 },
2422 Entry{ScalarEntry{"BFLOGK-",
2423 [&flowsC = this->flowsC_,
2424 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2425 {
2426 const unsigned index = !flowsC.blockFlows().empty() ?
2427 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2428 FaceDir::ToIntersectionIndex(Dir::ZMinus), gasCompIdx) : ectx.globalDofIdx;
2429 return flowsC.getFlow(index, Dir::ZMinus, gasCompIdx);
2430 }
2431 }
2432 },
2433 Entry{ScalarEntry{"BFLOOI",
2434 [&flowsC = this->flowsC_,
2435 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2436 {
2437 const unsigned index = !flowsC.blockFlows().empty() ?
2438 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2439 FaceDir::ToIntersectionIndex(Dir::XPlus), oilCompIdx) : ectx.globalDofIdx;
2440 return flowsC.getFlow(index, Dir::XPlus, oilCompIdx);
2441 }
2442 }
2443 },
2444 Entry{ScalarEntry{"BFLOOI-",
2445 [&flowsC = this->flowsC_,
2446 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2447 {
2448 const unsigned index = !flowsC.blockFlows().empty() ?
2449 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2450 FaceDir::ToIntersectionIndex(Dir::XMinus), oilCompIdx) : ectx.globalDofIdx;
2451 return flowsC.getFlow(index, Dir::XMinus, oilCompIdx);
2452 }
2453 }
2454 },
2455 Entry{ScalarEntry{"BFLOOJ",
2456 [&flowsC = this->flowsC_,
2457 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2458 {
2459 const unsigned index = !flowsC.blockFlows().empty() ?
2460 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2461 FaceDir::ToIntersectionIndex(Dir::YPlus), oilCompIdx) : ectx.globalDofIdx;
2462 return flowsC.getFlow(index, Dir::YPlus, oilCompIdx);
2463 }
2464 }
2465 },
2466 Entry{ScalarEntry{"BFLOOJ-",
2467 [&flowsC = this->flowsC_,
2468 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2469 {
2470 const unsigned index = !flowsC.blockFlows().empty() ?
2471 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2472 FaceDir::ToIntersectionIndex(Dir::YMinus), oilCompIdx) : ectx.globalDofIdx;
2473 return flowsC.getFlow(index, Dir::YMinus, oilCompIdx);
2474 }
2475 }
2476 },
2477 Entry{ScalarEntry{"BFLOOK",
2478 [&flowsC = this->flowsC_,
2479 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2480 {
2481 const unsigned index = !flowsC.blockFlows().empty() ?
2482 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2483 FaceDir::ToIntersectionIndex(Dir::ZPlus), oilCompIdx) : ectx.globalDofIdx;
2484 return flowsC.getFlow(index, Dir::ZPlus, oilCompIdx);
2485 }
2486 }
2487 },
2488 Entry{ScalarEntry{"BFLOOK-",
2489 [&flowsC = this->flowsC_,
2490 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2491 {
2492 const unsigned index = !flowsC.blockFlows().empty() ?
2493 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2494 FaceDir::ToIntersectionIndex(Dir::ZMinus), oilCompIdx) : ectx.globalDofIdx;
2495 return flowsC.getFlow(index, Dir::ZMinus, oilCompIdx);
2496 }
2497 }
2498 },
2499 Entry{ScalarEntry{"BFLOWI",
2500 [&flowsC = this->flowsC_,
2501 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2502 {
2503 const unsigned index = !flowsC.blockFlows().empty() ?
2504 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2505 FaceDir::ToIntersectionIndex(Dir::XPlus), waterCompIdx) : ectx.globalDofIdx;
2506 return flowsC.getFlow(index, Dir::XPlus, waterCompIdx);
2507 }
2508 }
2509 },
2510 Entry{ScalarEntry{"BFLOWI-",
2511 [&flowsC = this->flowsC_,
2512 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2513 {
2514 const unsigned index = !flowsC.blockFlows().empty() ?
2515 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2516 FaceDir::ToIntersectionIndex(Dir::XMinus), waterCompIdx) : ectx.globalDofIdx;
2517 return flowsC.getFlow(index, Dir::XMinus, waterCompIdx);
2518 }
2519 }
2520 },
2521 Entry{ScalarEntry{"BFLOWJ",
2522 [&flowsC = this->flowsC_,
2523 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2524 {
2525 const unsigned index = !flowsC.blockFlows().empty() ?
2526 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2527 FaceDir::ToIntersectionIndex(Dir::YPlus), waterCompIdx) : ectx.globalDofIdx;
2528 return flowsC.getFlow(index, Dir::YPlus, waterCompIdx);
2529 }
2530 }
2531 },
2532 Entry{ScalarEntry{"BFLOWJ-",
2533 [&flowsC = this->flowsC_,
2534 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2535 {
2536 const unsigned index = !flowsC.blockFlows().empty() ?
2537 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2538 FaceDir::ToIntersectionIndex(Dir::YMinus), waterCompIdx) : ectx.globalDofIdx;
2539 return flowsC.getFlow(index, Dir::YMinus, waterCompIdx);
2540 }
2541 }
2542 },
2543 Entry{ScalarEntry{"BFLOWK",
2544 [&flowsC = this->flowsC_,
2545 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2546 {
2547 const unsigned index = !flowsC.blockFlows().empty() ?
2548 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2549 FaceDir::ToIntersectionIndex(Dir::ZPlus), waterCompIdx) : ectx.globalDofIdx;
2550 return flowsC.getFlow(index, Dir::ZPlus, waterCompIdx);
2551 }
2552 }
2553 },
2554 Entry{ScalarEntry{"BFLOWK-",
2555 [&flowsC = this->flowsC_,
2556 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2557 {
2558 const unsigned index = !flowsC.blockFlows().empty() ?
2559 flowsC.blockFlowsIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2560 FaceDir::ToIntersectionIndex(Dir::ZMinus), waterCompIdx) : ectx.globalDofIdx;
2561 return flowsC.getFlow(index, Dir::ZMinus, waterCompIdx);
2562 }
2563 }
2564 },
2565 Entry{ScalarEntry{"BVELGI",
2566 [&flowsC = this->flowsC_,
2567 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2568 {
2569 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2570 FaceDir::ToIntersectionIndex(Dir::XPlus), gasCompIdx);
2571 return flowsC.getVelocity(index, Dir::XPlus, gasCompIdx);
2572 }
2573 }
2574 },
2575 Entry{ScalarEntry{"BVELGI-",
2576 [&flowsC = this->flowsC_,
2577 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2578 {
2579 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2580 FaceDir::ToIntersectionIndex(Dir::XMinus), gasCompIdx);
2581 return flowsC.getVelocity(index, Dir::XMinus, gasCompIdx);
2582 }
2583 }
2584 },
2585 Entry{ScalarEntry{"BVELGJ",
2586 [&flowsC = this->flowsC_,
2587 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2588 {
2589 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2590 FaceDir::ToIntersectionIndex(Dir::YPlus), gasCompIdx);
2591 return flowsC.getVelocity(index, Dir::YPlus, gasCompIdx);
2592 }
2593 }
2594 },
2595 Entry{ScalarEntry{"BVELGJ-",
2596 [&flowsC = this->flowsC_,
2597 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2598 {
2599 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2600 FaceDir::ToIntersectionIndex(Dir::YMinus), gasCompIdx);
2601 return flowsC.getVelocity(index, Dir::YMinus, gasCompIdx);
2602 }
2603 }
2604 },
2605 Entry{ScalarEntry{"BVELGK",
2606 [&flowsC = this->flowsC_,
2607 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2608 {
2609 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2610 FaceDir::ToIntersectionIndex(Dir::ZPlus), gasCompIdx);
2611 return flowsC.getVelocity(index, Dir::ZPlus, gasCompIdx);
2612 }
2613 }
2614 },
2615 Entry{ScalarEntry{"BVELGK-",
2616 [&flowsC = this->flowsC_,
2617 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2618 {
2619 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2620 FaceDir::ToIntersectionIndex(Dir::ZMinus), gasCompIdx);
2621 return flowsC.getVelocity(index, Dir::ZMinus, gasCompIdx);
2622 }
2623 }
2624 },
2625 Entry{ScalarEntry{"BVELOI",
2626 [&flowsC = this->flowsC_,
2627 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2628 {
2629 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2630 FaceDir::ToIntersectionIndex(Dir::XPlus), oilCompIdx);
2631 return flowsC.getVelocity(index, Dir::XPlus, oilCompIdx);
2632 }
2633 }
2634 },
2635 Entry{ScalarEntry{"BVELOI-",
2636 [&flowsC = this->flowsC_,
2637 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2638 {
2639 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2640 FaceDir::ToIntersectionIndex(Dir::XMinus), oilCompIdx);
2641 return flowsC.getVelocity(index, Dir::XMinus, oilCompIdx);
2642 }
2643 }
2644 },
2645 Entry{ScalarEntry{"BVELOJ",
2646 [&flowsC = this->flowsC_,
2647 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2648 {
2649 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2650 FaceDir::ToIntersectionIndex(Dir::YPlus), oilCompIdx);
2651 return flowsC.getVelocity(index, Dir::YPlus, oilCompIdx);
2652 }
2653 }
2654 },
2655 Entry{ScalarEntry{"BVELOJ-",
2656 [&flowsC = this->flowsC_,
2657 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2658 {
2659 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2660 FaceDir::ToIntersectionIndex(Dir::YMinus), oilCompIdx);
2661 return flowsC.getVelocity(index, Dir::YMinus, oilCompIdx);
2662 }
2663 }
2664 },
2665 Entry{ScalarEntry{"BVELOK",
2666 [&flowsC = this->flowsC_,
2667 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2668 {
2669 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2670 FaceDir::ToIntersectionIndex(Dir::ZPlus), oilCompIdx);
2671 return flowsC.getVelocity(index, Dir::ZPlus, oilCompIdx);
2672 }
2673 }
2674 },
2675 Entry{ScalarEntry{"BVELOK-",
2676 [&flowsC = this->flowsC_,
2677 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2678 {
2679 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2680 FaceDir::ToIntersectionIndex(Dir::ZMinus), oilCompIdx);
2681 return flowsC.getVelocity(index, Dir::ZMinus, oilCompIdx);
2682 }
2683 }
2684 },
2685 Entry{ScalarEntry{"BVELWI",
2686 [&flowsC = this->flowsC_,
2687 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2688 {
2689 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2690 FaceDir::ToIntersectionIndex(Dir::XPlus), waterCompIdx);
2691 return flowsC.getVelocity(index, Dir::XPlus, waterCompIdx);
2692 }
2693 }
2694 },
2695 Entry{ScalarEntry{"BVELWI-",
2696 [&flowsC = this->flowsC_,
2697 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2698 {
2699 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2700 FaceDir::ToIntersectionIndex(Dir::XMinus), waterCompIdx);
2701 return flowsC.getVelocity(index, Dir::XMinus, waterCompIdx);
2702 }
2703 }
2704 },
2705 Entry{ScalarEntry{"BVELWJ",
2706 [&flowsC = this->flowsC_,
2707 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2708 {
2709 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2710 FaceDir::ToIntersectionIndex(Dir::YPlus), waterCompIdx);
2711 return flowsC.getVelocity(index, Dir::YPlus, waterCompIdx);
2712 }
2713 }
2714 },
2715 Entry{ScalarEntry{"BVELWJ-",
2716 [&flowsC = this->flowsC_,
2717 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2718 {
2719 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2720 FaceDir::ToIntersectionIndex(Dir::YMinus), waterCompIdx);
2721 return flowsC.getVelocity(index, Dir::YMinus, waterCompIdx);
2722 }
2723 }
2724 },
2725 Entry{ScalarEntry{"BVELWK",
2726 [&flowsC = this->flowsC_,
2727 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2728 {
2729 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2730 FaceDir::ToIntersectionIndex(Dir::ZPlus), waterCompIdx);
2731 return flowsC.getVelocity(index, Dir::ZPlus, waterCompIdx);
2732 }
2733 }
2734 },
2735 Entry{ScalarEntry{"BVELWK-",
2736 [&flowsC = this->flowsC_,
2737 &vanguard = this->simulator_.vanguard()](const Context& ectx)
2738 {
2739 const unsigned index = flowsC.blockVelocityIds(vanguard.cartesianIndex(ectx.globalDofIdx),
2740 FaceDir::ToIntersectionIndex(Dir::ZMinus), waterCompIdx);
2741 return flowsC.getVelocity(index, Dir::ZMinus, waterCompIdx);
2742 }
2743 }
2744 },
2745 Entry{ScalarEntry{"BRPV",
2746 [&model = this->simulator_.model()](const Context& ectx)
2747 {
2748 return getValue(ectx.intQuants.porosity()) *
2749 model.dofTotalVolume(ectx.globalDofIdx);
2750 }
2751 }
2752 },
2753 Entry{PhaseEntry{std::array{"BWPV"sv, "BOPV"sv, "BGPV"sv},
2754 [&model = this->simulator_.model()](const unsigned phaseIdx,
2755 const Context& ectx)
2756 {
2757 return getValue(ectx.fs.saturation(phaseIdx)) *
2758 getValue(ectx.intQuants.porosity()) *
2759 model.dofTotalVolume(ectx.globalDofIdx);
2760 }
2761 }
2762 },
2763 Entry{ScalarEntry{"BRS",
2764 [](const Context& ectx)
2765 {
2766 return getValue(ectx.fs.Rs());
2767 }
2768 }
2769 },
2770 Entry{ScalarEntry{"BRV",
2771 [](const Context& ectx)
2772 {
2773 return getValue(ectx.fs.Rv());
2774 }
2775 }
2776 },
2777 Entry{ScalarEntry{"BOIP",
2778 [&model = this->simulator_.model()](const Context& ectx)
2779 {
2780 return (getValue(ectx.fs.invB(oilPhaseIdx)) *
2781 getValue(ectx.fs.saturation(oilPhaseIdx)) +
2782 getValue(ectx.fs.Rv()) *
2783 getValue(ectx.fs.invB(gasPhaseIdx)) *
2784 getValue(ectx.fs.saturation(gasPhaseIdx))) *
2785 model.dofTotalVolume(ectx.globalDofIdx) *
2786 getValue(ectx.intQuants.porosity());
2787 }
2788 }
2789 },
2790 Entry{ScalarEntry{"BGIP",
2791 [&model = this->simulator_.model()](const Context& ectx)
2792 {
2793 Scalar result = getValue(ectx.fs.invB(gasPhaseIdx)) *
2794 getValue(ectx.fs.saturation(gasPhaseIdx));
2795
2796 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2797 result += getValue(ectx.fs.Rs()) *
2798 getValue(ectx.fs.invB(oilPhaseIdx)) *
2799 getValue(ectx.fs.saturation(oilPhaseIdx));
2800 }
2801 else {
2802 result += getValue(ectx.fs.Rsw()) *
2803 getValue(ectx.fs.invB(waterPhaseIdx)) *
2804 getValue(ectx.fs.saturation(waterPhaseIdx));
2805 }
2806
2807 return result *
2808 model.dofTotalVolume(ectx.globalDofIdx) *
2809 getValue(ectx.intQuants.porosity());
2810 }
2811 }
2812 },
2813 Entry{ScalarEntry{"BWIP",
2814 [&model = this->simulator_.model()](const Context& ectx)
2815 {
2816 return getValue(ectx.fs.invB(waterPhaseIdx)) *
2817 getValue(ectx.fs.saturation(waterPhaseIdx)) *
2818 model.dofTotalVolume(ectx.globalDofIdx) *
2819 getValue(ectx.intQuants.porosity());
2820 }
2821 }
2822 },
2823 Entry{ScalarEntry{"BOIPL",
2824 [&model = this->simulator_.model()](const Context& ectx)
2825 {
2826 return getValue(ectx.fs.invB(oilPhaseIdx)) *
2827 getValue(ectx.fs.saturation(oilPhaseIdx)) *
2828 model.dofTotalVolume(ectx.globalDofIdx) *
2829 getValue(ectx.intQuants.porosity());
2830 }
2831 }
2832 },
2833 Entry{ScalarEntry{"BGIPL",
2834 [&model = this->simulator_.model()](const Context& ectx)
2835 {
2836 Scalar result;
2837 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2838 result = getValue(ectx.fs.Rs()) *
2839 getValue(ectx.fs.invB(oilPhaseIdx)) *
2840 getValue(ectx.fs.saturation(oilPhaseIdx));
2841 }
2842 else {
2843 result = getValue(ectx.fs.Rsw()) *
2844 getValue(ectx.fs.invB(waterPhaseIdx)) *
2845 getValue(ectx.fs.saturation(waterPhaseIdx));
2846 }
2847 return result *
2848 model.dofTotalVolume(ectx.globalDofIdx) *
2849 getValue(ectx.intQuants.porosity());
2850 }
2851 }
2852 },
2853 Entry{ScalarEntry{"BGIPG",
2854 [&model = this->simulator_.model()](const Context& ectx)
2855 {
2856 return getValue(ectx.fs.invB(gasPhaseIdx)) *
2857 getValue(ectx.fs.saturation(gasPhaseIdx)) *
2858 model.dofTotalVolume(ectx.globalDofIdx) *
2859 getValue(ectx.intQuants.porosity());
2860 }
2861 }
2862 },
2863 Entry{ScalarEntry{"BOIPG",
2864 [&model = this->simulator_.model()](const Context& ectx)
2865 {
2866 return getValue(ectx.fs.Rv()) *
2867 getValue(ectx.fs.invB(gasPhaseIdx)) *
2868 getValue(ectx.fs.saturation(gasPhaseIdx)) *
2869 model.dofTotalVolume(ectx.globalDofIdx) *
2870 getValue(ectx.intQuants.porosity());
2871 }
2872 }
2873 },
2874 Entry{PhaseEntry{std::array{"BPPW"sv, "BPPO"sv, "BPPG"sv},
2875 [&simConfig = this->eclState_.getSimulationConfig(),
2876 &grav = this->simulator_.problem().gravity(),
2877 &regionAvgDensity = this->regionAvgDensity_,
2878 &problem = this->simulator_.problem(),
2879 &regions = this->regions_](const unsigned phaseIdx, const Context& ectx)
2880 {
2881 auto phase = RegionPhasePoreVolAverage::Phase{};
2882 phase.ix = phaseIdx;
2883
2884 // Note different region handling here. FIPNUM is
2885 // one-based, but we need zero-based lookup in
2886 // DatumDepth. On the other hand, pvtRegionIndex is
2887 // zero-based but we need one-based lookup in
2888 // RegionPhasePoreVolAverage.
2889
2890 // Subtract one to convert FIPNUM to region index.
2891 const auto datum = simConfig.datumDepths()(regions["FIPNUM"][ectx.dofIdx] - 1);
2892
2893 // Add one to convert region index to region ID.
2894 const auto region = RegionPhasePoreVolAverage::Region {
2895 ectx.elemCtx.primaryVars(ectx.dofIdx, /*timeIdx=*/0).pvtRegionIndex() + 1
2896 };
2897
2898 const auto density = regionAvgDensity->value("PVTNUM", phase, region);
2899
2900 const auto press = getValue(ectx.fs.pressure(phase.ix));
2901 const auto dz = problem.dofCenterDepth(ectx.globalDofIdx) - datum;
2902 return press - density*dz*grav[GridView::dimensionworld - 1];
2903 }
2904 }
2905 },
2906 Entry{ScalarEntry{"BAMIP",
2907 [&model = this->simulator_.model()](const Context& ectx)
2908 {
2909 const Scalar rhoW = FluidSystem::referenceDensity(waterPhaseIdx,
2910 ectx.intQuants.pvtRegionIndex());
2911 return getValue(ectx.fs.invB(waterPhaseIdx)) *
2912 getValue(ectx.fs.saturation(waterPhaseIdx)) *
2913 rhoW *
2914 model.dofTotalVolume(ectx.globalDofIdx) *
2915 getValue(ectx.intQuants.porosity());
2916 }
2917 }
2918 },
2919 Entry{ScalarEntry{"BMMIP",
2920 [&model = this->simulator_.model()](const Context& ectx)
2921 {
2922 if constexpr (enableBioeffects) {
2923 return getValue(ectx.intQuants.microbialConcentration()) *
2924 getValue(ectx.fs.saturation(waterPhaseIdx)) *
2925 getValue(ectx.intQuants.porosity()) *
2926 model.dofTotalVolume(ectx.globalDofIdx);
2927 }
2928 else {
2929 return Scalar{0};
2930 }
2931 }
2932 }
2933 },
2934 Entry{ScalarEntry{"BMOIP",
2935 [&model = this->simulator_.model()](const Context& ectx)
2936 {
2937 if constexpr (enableBioeffects) {
2938 return getValue(ectx.intQuants.oxygenConcentration()) *
2939 getValue(ectx.intQuants.porosity()) *
2940 model.dofTotalVolume(ectx.globalDofIdx);
2941 }
2942 else {
2943 return Scalar{0};
2944 }
2945 }
2946 }
2947 },
2948 Entry{ScalarEntry{"BMUIP",
2949 [&model = this->simulator_.model()](const Context& ectx)
2950 {
2951 if constexpr (enableBioeffects) {
2952 return getValue(ectx.intQuants.ureaConcentration()) *
2953 getValue(ectx.intQuants.porosity()) *
2954 model.dofTotalVolume(ectx.globalDofIdx);
2955 }
2956 else {
2957 return Scalar{0};
2958 }
2959 }
2960 }
2961 },
2962 Entry{ScalarEntry{"BMBIP",
2963 [&model = this->simulator_.model()](const Context& ectx)
2964 {
2965 if constexpr (enableBioeffects) {
2966 return model.dofTotalVolume(ectx.globalDofIdx) *
2967 getValue(ectx.intQuants.biofilmMass());
2968 }
2969 else {
2970 return Scalar{0};
2971 }
2972 }
2973 }
2974 },
2975 Entry{ScalarEntry{"BMCIP",
2976 [&model = this->simulator_.model()](const Context& ectx)
2977 {
2978 if constexpr (enableBioeffects) {
2979 return model.dofTotalVolume(ectx.globalDofIdx) *
2980 getValue(ectx.intQuants.calciteMass());
2981 }
2982 else {
2983 return Scalar{0};
2984 }
2985 }
2986 }
2987 },
2988 Entry{ScalarEntry{"BGMIP",
2989 [&model = this->simulator_.model()](const Context& ectx)
2990 {
2991 Scalar result = getValue(ectx.fs.invB(gasPhaseIdx)) *
2992 getValue(ectx.fs.saturation(gasPhaseIdx));
2993
2994 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
2995 result += getValue(ectx.fs.Rs()) *
2996 getValue(ectx.fs.invB(oilPhaseIdx)) *
2997 getValue(ectx.fs.saturation(oilPhaseIdx));
2998 }
2999 else {
3000 result += getValue(ectx.fs.Rsw()) *
3001 getValue(ectx.fs.invB(waterPhaseIdx)) *
3002 getValue(ectx.fs.saturation(waterPhaseIdx));
3003 }
3004 const Scalar rhoG = FluidSystem::referenceDensity(gasPhaseIdx,
3005 ectx.intQuants.pvtRegionIndex());
3006 return result *
3007 model.dofTotalVolume(ectx.globalDofIdx) *
3008 getValue(ectx.intQuants.porosity()) *
3009 rhoG;
3010 }
3011 }
3012 },
3013 Entry{ScalarEntry{"BGMGP",
3014 [&model = this->simulator_.model()](const Context& ectx)
3015 {
3016 const Scalar rhoG = FluidSystem::referenceDensity(gasPhaseIdx,
3017 ectx.intQuants.pvtRegionIndex());
3018 return getValue(ectx.fs.invB(gasPhaseIdx)) *
3019 getValue(ectx.fs.saturation(gasPhaseIdx)) *
3020 model.dofTotalVolume(ectx.globalDofIdx) *
3021 getValue(ectx.intQuants.porosity()) *
3022 rhoG;
3023 }
3024 }
3025 },
3026 Entry{ScalarEntry{"BGMDS",
3027 [&model = this->simulator_.model()](const Context& ectx)
3028 {
3029 Scalar result;
3030 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
3031 result = getValue(ectx.fs.Rs()) *
3032 getValue(ectx.fs.invB(oilPhaseIdx)) *
3033 getValue(ectx.fs.saturation(oilPhaseIdx));
3034 }
3035 else {
3036 result = getValue(ectx.fs.Rsw()) *
3037 getValue(ectx.fs.invB(waterPhaseIdx)) *
3038 getValue(ectx.fs.saturation(waterPhaseIdx));
3039 }
3040 const Scalar rhoG = FluidSystem::referenceDensity(gasPhaseIdx,
3041 ectx.intQuants.pvtRegionIndex());
3042 return result *
3043 model.dofTotalVolume(ectx.globalDofIdx) *
3044 getValue(ectx.intQuants.porosity()) *
3045 rhoG;
3046 }
3047 }
3048 },
3049 Entry{ScalarEntry{"BGMST",
3050 [&model = this->simulator_.model(),
3051 &problem = this->simulator_.problem()](const Context& ectx)
3052 {
3053 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3054 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3055 Scalar strandedGas = scaledDrainageInfo.Sgcr;
3056 if (problem.materialLawManager().enableHysteresis()) {
3057 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3058 const Scalar krg = getValue(ectx.intQuants.relativePermeability(gasPhaseIdx));
3059 strandedGas = MaterialLaw::strandedGasSaturation(matParams, sg, krg);
3060 }
3061 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3062 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3063 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3064 return (1.0 - xgW) *
3065 model.dofTotalVolume(ectx.globalDofIdx) *
3066 getValue(ectx.intQuants.porosity()) *
3067 getValue(ectx.fs.density(gasPhaseIdx)) *
3068 std::min(strandedGas, sg);
3069 }
3070 }
3071 },
3072 Entry{ScalarEntry{"BGMUS",
3073 [&model = this->simulator_.model(),
3074 &problem = this->simulator_.problem()](const Context& ectx)
3075 {
3076 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3077 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3078 Scalar strandedGas = scaledDrainageInfo.Sgcr;
3079 if (problem.materialLawManager().enableHysteresis()) {
3080 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3081 const Scalar krg = getValue(ectx.intQuants.relativePermeability(gasPhaseIdx));
3082 strandedGas = MaterialLaw::strandedGasSaturation(matParams, sg, krg);
3083 }
3084 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3085 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3086 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3087 return (1.0 - xgW) *
3088 model.dofTotalVolume(ectx.globalDofIdx) *
3089 getValue(ectx.intQuants.porosity()) *
3090 getValue(ectx.fs.density(gasPhaseIdx)) *
3091 std::max(Scalar{0.0}, sg - strandedGas);
3092 }
3093 }
3094 },
3095 Entry{ScalarEntry{"BGMTR",
3096 [&model = this->simulator_.model(),
3097 &problem = this->simulator_.problem()](const Context& ectx)
3098 {
3099 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3100 Scalar trappedGas = scaledDrainageInfo.Sgcr;
3101 if (problem.materialLawManager().enableHysteresis()) {
3102 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3103 trappedGas = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/true);
3104 }
3105 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3106 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3107 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3108 return (1.0 - xgW) *
3109 model.dofTotalVolume(ectx.globalDofIdx) *
3110 getValue(ectx.intQuants.porosity()) *
3111 getValue(ectx.fs.density(gasPhaseIdx)) *
3112 std::min(trappedGas, getValue(ectx.fs.saturation(gasPhaseIdx)));
3113 }
3114 }
3115 },
3116 Entry{ScalarEntry{"BGMMO",
3117 [&model = this->simulator_.model(),
3118 &problem = this->simulator_.problem()](const Context& ectx)
3119 {
3120 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3121 Scalar trappedGas = scaledDrainageInfo.Sgcr;
3122 if (problem.materialLawManager().enableHysteresis()) {
3123 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3124 trappedGas = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/true);
3125 }
3126 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3127 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3128 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3129 return (1.0 - xgW) *
3130 model.dofTotalVolume(ectx.globalDofIdx) *
3131 getValue(ectx.intQuants.porosity()) *
3132 getValue(ectx.fs.density(gasPhaseIdx)) *
3133 std::max(Scalar{0.0}, getValue(ectx.fs.saturation(gasPhaseIdx)) - trappedGas);
3134 }
3135 }
3136 },
3137 Entry{ScalarEntry{"BGKTR",
3138 [&model = this->simulator_.model(),
3139 &problem = this->simulator_.problem()](const Context& ectx)
3140 {
3141 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3142 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3143 Scalar sgcr = scaledDrainageInfo.Sgcr;
3144 if (problem.materialLawManager().enableHysteresis()) {
3145 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3146 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3147 }
3148 if (sg > sgcr) {
3149 return 0.0;
3150 }
3151 else {
3152 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3153 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3154 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3155 return (1.0 - xgW) *
3156 model.dofTotalVolume(ectx.globalDofIdx) *
3157 getValue(ectx.intQuants.porosity()) *
3158 getValue(ectx.fs.density(gasPhaseIdx)) *
3159 getValue(ectx.fs.saturation(gasPhaseIdx));
3160 }
3161 }
3162 }
3163 },
3164 Entry{ScalarEntry{"BGKMO",
3165 [&model = this->simulator_.model(),
3166 &problem = this->simulator_.problem()](const Context& ectx)
3167 {
3168 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3169 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3170 Scalar sgcr = scaledDrainageInfo.Sgcr;
3171 if (problem.materialLawManager().enableHysteresis()) {
3172 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3173 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3174 }
3175 if (sgcr >= sg) {
3176 return 0.0;
3177 }
3178 else {
3179 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3180 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3181 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3182 return (1.0 - xgW) *
3183 model.dofTotalVolume(ectx.globalDofIdx) *
3184 getValue(ectx.intQuants.porosity()) *
3185 getValue(ectx.fs.density(gasPhaseIdx)) *
3186 getValue(ectx.fs.saturation(gasPhaseIdx));
3187 }
3188 }
3189 }
3190 },
3191 Entry{ScalarEntry{"BGCDI",
3192 [&model = this->simulator_.model(),
3193 &problem = this->simulator_.problem()](const Context& ectx)
3194 {
3195 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3196 Scalar sgcr = scaledDrainageInfo.Sgcr;
3197 if (problem.materialLawManager().enableHysteresis()) {
3198 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3199 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3200 }
3201 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3202 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3203 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3204 return (1.0 - xgW) *
3205 model.dofTotalVolume(ectx.globalDofIdx) *
3206 getValue(ectx.intQuants.porosity()) *
3207 getValue(ectx.fs.density(gasPhaseIdx)) *
3208 std::min(sgcr, getValue(ectx.fs.saturation(gasPhaseIdx))) /
3209 FluidSystem::molarMass(gasCompIdx, ectx.intQuants.pvtRegionIndex());
3210 }
3211 }
3212 },
3213 Entry{ScalarEntry{"BGCDM",
3214 [&model = this->simulator_.model(),
3215 &problem = this->simulator_.problem()](const Context& ectx)
3216 {
3217 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3218 Scalar sgcr = scaledDrainageInfo.Sgcr;
3219 if (problem.materialLawManager().enableHysteresis()) {
3220 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3221 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3222 }
3223 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3224 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3225 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3226 return (1.0 - xgW) *
3227 model.dofTotalVolume(ectx.globalDofIdx) *
3228 getValue(ectx.intQuants.porosity()) *
3229 getValue(ectx.fs.density(gasPhaseIdx)) *
3230 std::max(Scalar{0.0}, getValue(ectx.fs.saturation(gasPhaseIdx)) - sgcr) /
3231 FluidSystem::molarMass(gasCompIdx, ectx.intQuants.pvtRegionIndex());
3232 }
3233 }
3234 },
3235 Entry{ScalarEntry{"BGKDI",
3236 [&model = this->simulator_.model(),
3237 &problem = this->simulator_.problem()](const Context& ectx)
3238 {
3239 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3240 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3241 Scalar sgcr = scaledDrainageInfo.Sgcr;
3242 if (problem.materialLawManager().enableHysteresis()) {
3243 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3244 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3245 }
3246 if (sg > sgcr) {
3247 return 0.0;
3248 }
3249 else {
3250 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3251 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3252 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3253 return (1.0 - xgW) *
3254 model.dofTotalVolume(ectx.globalDofIdx) *
3255 getValue(ectx.intQuants.porosity()) *
3256 getValue(ectx.fs.density(gasPhaseIdx)) *
3257 getValue(ectx.fs.saturation(gasPhaseIdx)) /
3258 FluidSystem::molarMass(gasCompIdx, ectx.intQuants.pvtRegionIndex());
3259 }
3260 }
3261 }
3262 },
3263 Entry{ScalarEntry{"BGKDM",
3264 [&model = this->simulator_.model(),
3265 &problem = this->simulator_.problem()](const Context& ectx)
3266 {
3267 const auto& scaledDrainageInfo = problem.materialLawManager().oilWaterScaledEpsInfoDrainage(ectx.dofIdx);
3268 const Scalar sg = getValue(ectx.fs.saturation(gasPhaseIdx));
3269 Scalar sgcr = scaledDrainageInfo.Sgcr;
3270 if (problem.materialLawManager().enableHysteresis()) {
3271 const auto& matParams = problem.materialLawParams(ectx.dofIdx);
3272 sgcr = MaterialLaw::trappedGasSaturation(matParams, /*maxTrapping*/false);
3273 }
3274 if (sgcr >= sg) {
3275 return 0.0;
3276 }
3277 else {
3278 const Scalar xgW = FluidSystem::phaseIsActive(waterPhaseIdx) ?
3279 FluidSystem::convertRvwToXgW(getValue(ectx.fs.Rvw()), ectx.intQuants.pvtRegionIndex())
3280 : FluidSystem::convertRvToXgO(getValue(ectx.fs.Rv()), ectx.intQuants.pvtRegionIndex());
3281 return (1.0 - xgW) *
3282 model.dofTotalVolume(ectx.globalDofIdx) *
3283 getValue(ectx.intQuants.porosity()) *
3284 getValue(ectx.fs.density(gasPhaseIdx)) *
3285 getValue(ectx.fs.saturation(gasPhaseIdx)) /
3286 FluidSystem::molarMass(gasCompIdx, ectx.intQuants.pvtRegionIndex());
3287 }
3288 }
3289 }
3290 },
3291 Entry{ScalarEntry{"BWCD",
3292 [&model = this->simulator_.model()](const Context& ectx)
3293 {
3294 Scalar result;
3295 if (FluidSystem::phaseIsActive(oilPhaseIdx)) {
3296 result = getValue(ectx.fs.Rs()) *
3297 getValue(ectx.fs.invB(oilPhaseIdx)) *
3298 getValue(ectx.fs.saturation(oilPhaseIdx));
3299 }
3300 else {
3301 result = getValue(ectx.fs.Rsw()) *
3302 getValue(ectx.fs.invB(waterPhaseIdx)) *
3303 getValue(ectx.fs.saturation(waterPhaseIdx));
3304 }
3305 const Scalar rhoG = FluidSystem::referenceDensity(gasPhaseIdx,
3306 ectx.intQuants.pvtRegionIndex());
3307 return result *
3308 model.dofTotalVolume(ectx.globalDofIdx) *
3309 getValue(ectx.intQuants.porosity()) *
3310 rhoG /
3311 FluidSystem::molarMass(gasCompIdx, ectx.intQuants.pvtRegionIndex());
3312 }
3313 }
3314 },
3315 Entry{ScalarEntry{"BWIPG",
3316 [&model = this->simulator_.model()](const Context& ectx)
3317 {
3318 Scalar result = 0.0;
3319 if (FluidSystem::phaseIsActive(gasPhaseIdx)) {
3320 result = getValue(ectx.fs.Rvw()) *
3321 getValue(ectx.fs.invB(gasPhaseIdx)) *
3322 getValue(ectx.fs.saturation(gasPhaseIdx));
3323 }
3324 return result *
3325 model.dofTotalVolume(ectx.globalDofIdx) *
3326 getValue(ectx.intQuants.porosity());
3327 }
3328 }
3329 },
3330 Entry{ScalarEntry{"BWIPL",
3331 [&model = this->simulator_.model()](const Context& ectx)
3332 {
3333 return getValue(ectx.fs.invB(waterPhaseIdx)) *
3334 getValue(ectx.fs.saturation(waterPhaseIdx)) *
3335 model.dofTotalVolume(ectx.globalDofIdx) *
3336 getValue(ectx.intQuants.porosity());
3337 }
3338 }
3339 },
3340 };
3341
3342 auto blockHandlers = std::span<const Entry>{handlers};
3343 auto mechHandlers = std::vector<Entry>{};
3344 if constexpr (getPropValue<TypeTag, Properties::EnableMech>()) {
3345 if (this->mech_.allocated()) {
3346 mechHandlers.assign(handlers.begin(), handlers.end());
3347 mechHandlers.push_back(
3348 Entry{TensorEntry{"BSTRSS",
3349 [&model = this->simulator_.problem().geoMechModel()]
3350 (const VoigtIndex index, const Context& ectx)
3351 {
3352 return model.stress(ectx.globalDofIdx, /*include_fracture*/true)[index];
3353 }
3354 }
3355 });
3356 blockHandlers = mechHandlers;
3357 }
3358 }
3359
3360 this->blockExtractors_ = BlockExtractor::setupExecMap(this->blockData_, blockHandlers);
3361
3362 // The LGR-cell extractors reuse the same handler list -- the
3363 // physics is identical, only the cell identification differs.
3364 // Empty for runs without LB* requests (zero non-LGR cost).
3365 this->lgrBlockExtractors_ =
3366 BlockExtractor::setupLgrExecMap(this->lgrBlockData_, blockHandlers);
3367
3368 this->extraBlockData_.clear();
3369 if (reportStepNum > 0 && !isSubStep) {
3370 // check we need extra block pressures for RPTSCHED
3371 const auto& rpt = this->schedule_[reportStepNum - 1].rpt_config.get();
3372 if (rpt.contains("WELLS") && rpt.at("WELLS") > 1) {
3373 this->setupExtraBlockData(reportStepNum,
3374 [&c = this->collectOnIORank_](const int idx)
3375 { return c.isCartIdxOnThisRank(idx); });
3376
3377 const auto extraHandlers = std::array{
3378 pressure_handler,
3379 };
3380
3381 this->extraBlockExtractors_ = BlockExtractor::setupExecMap(this->extraBlockData_, extraHandlers);
3382 }
3383 }
3384 }
3385
3386 const Simulator& simulator_;
3387 const CollectDataOnIORankType& collectOnIORank_;
3388 std::vector<typename Extractor::Entry> extractors_;
3389 typename BlockExtractor::ExecMap blockExtractors_;
3390 typename BlockExtractor::ExecMap extraBlockExtractors_;
3391
3392 // Per-LGR-cell extractor executor map. Outer key is the grid level,
3393 // inner key is the level-local linearised Cartesian cell index, so
3394 // the per-DOF lookup in processElementBlockData short-circuits O(1)
3395 // on levels with no LB* requests. Empty for non-LGR runs.
3396 typename BlockExtractor::LgrExecMap lgrBlockExtractors_;
3397};
3398
3399} // namespace Opm
3400
3401#endif // OPM_OUTPUT_BLACK_OIL_MODULE_HPP
Common output functionality shared by simulator formulations.
Declares the properties required by the black oil model.
Definition: CollectDataOnIORank.hpp:57
The base class for the element-centered finite-volume discretization scheme.
Definition: ecfvdiscretization.hh:160
void assignMicrobialMass(const unsigned globalDofIdx, const Scalar microbialMass)
void assignCalciteMass(const unsigned globalDofIdx, const Scalar calciteMass)
bool hasCo2InGas() const
void assignCo2InWater(const unsigned globalDofIdx, const Scalar co2InWater, const Scalar mM)
void assignVolumesSurface(const unsigned globalDofIdx, const std::array< Scalar, numPhases > &fip)
bool has(const Inplace::Phase phase) const
bool hasMicrobialMass() const
void assignWaterMass(const unsigned globalDofIdx, const std::array< Scalar, numPhases > &fip, const Scalar rhoW)
void assignCo2InGas(const unsigned globalDofIdx, const Co2InGasInput &v)
bool hasOxygenMass() const
void assignVolumesReservoir(const unsigned globalDofIdx, const Scalar saltConcentration, const std::array< Scalar, numPhases > &fipr)
void assignOxygenMass(const unsigned globalDofIdx, const Scalar oxygenMass)
bool hasUreaMass() const
void assignOilGasDistribution(const unsigned globalDofIdx, const Scalar gasInPlaceLiquid, const Scalar oilInPlaceGas)
void assignBiofilmMass(const unsigned globalDofIdx, const Scalar biofilmMass)
bool hasWaterMass() const
bool hasCo2InWater() const
void assignUreaMass(const unsigned globalDofIdx, const Scalar ureaMass)
bool hasCalciteMass() const
bool hasBiofilmMass() const
void assignGasWater(const unsigned globalDofIdx, const std::array< Scalar, numPhases > &fip, const Scalar gasInPlaceWater, const Scalar waterInPlaceGas)
const std::vector< int > blockVelocity() const
Definition: FlowsContainer.hpp:103
Definition: GenericOutputModule.hpp:91
const Schedule & schedule_
Definition: GenericOutputModule.hpp:537
ScalarBuffer oilVaporizationFactor_
Definition: GenericOutputModule.hpp:614
bool forceDisableFipOutput_
Definition: GenericOutputModule.hpp:558
RFTContainer< GetPropType< TypeTag, Properties::FluidSystem > > rftC_
Definition: GenericOutputModule.hpp:643
ScalarBuffer saturatedOilFormationVolumeFactor_
Definition: GenericOutputModule.hpp:619
GeochemistryContainer< Scalar > geochemC_
Definition: GenericOutputModule.hpp:635
ScalarBuffer rockCompPorvMultiplier_
Definition: GenericOutputModule.hpp:617
TracerContainer< GetPropType< TypeTag, Properties::FluidSystem > > tracerC_
Definition: GenericOutputModule.hpp:637
void doAllocBuffers(unsigned bufferSize, unsigned reportStepNum, const bool substep, const bool log, const bool forceRestartFieldAllocation, const EclHysteresisConfig *hysteresisConfig, unsigned numOutputNnc=0, std::map< std::string, int > rstKeywords={})
CO2H2Container< Scalar > CO2H2C_
Definition: GenericOutputModule.hpp:622
std::array< ScalarBuffer, numPhases > residual_
Definition: GenericOutputModule.hpp:639
ScalarBuffer sSol_
Definition: GenericOutputModule.hpp:598
std::map< std::tuple< std::string, int, int >, double > lgrBlockData_
Definition: GenericOutputModule.hpp:656
void setupExtraBlockData(const std::size_t reportStepNum, std::function< bool(int)> isCartIdxOnThisRank)
std::vector< int > failedCellsPd_
Definition: GenericOutputModule.hpp:584
ScalarBuffer shmax_
Definition: GenericOutputModule.hpp:609
ScalarBuffer sgmax_
Definition: GenericOutputModule.hpp:608
ScalarBuffer rockCompTransMultiplier_
Definition: GenericOutputModule.hpp:620
void setupBlockData(std::function< bool(int)> isCartIdxOnThisRank)
ScalarBuffer minimumOilPressure_
Definition: GenericOutputModule.hpp:618
ScalarBuffer temperature_
Definition: GenericOutputModule.hpp:592
ScalarBuffer pcow_
Definition: GenericOutputModule.hpp:624
ScalarBuffer drsdtcon_
Definition: GenericOutputModule.hpp:597
std::optional< RegionPhasePoreVolAverage > regionAvgDensity_
Definition: GenericOutputModule.hpp:661
ScalarBuffer permFact_
Definition: GenericOutputModule.hpp:604
ScalarBuffer overburdenPressure_
Definition: GenericOutputModule.hpp:595
TpsaContainer< Scalar > tpsaC_
Definition: GenericOutputModule.hpp:629
ScalarBuffer rPorV_
Definition: GenericOutputModule.hpp:590
std::array< ScalarBuffer, numPhases > viscosity_
Definition: GenericOutputModule.hpp:633
void setupLgrBlockData(const std::map< std::string, int > &lgrNameToLevel, const std::function< bool(int, int)> &isLgrCellOnThisRank)
ScalarBuffer rvw_
Definition: GenericOutputModule.hpp:594
FIPContainer< GetPropType< TypeTag, Properties::FluidSystem > > fipC_
Definition: GenericOutputModule.hpp:571
ScalarBuffer cPolymer_
Definition: GenericOutputModule.hpp:600
void assignBuffer(data::Solution &sol, std::string_view name, UnitSystem::measure measure, std::vector< Scalar > &buffer)
static bool allocBufferIfRequested(std::map< std::string, int > &rstKeywords, unsigned bufferSize, std::vector< Scalar > &buffer, std::string_view kw, bool supported, bool required=false)
std::unordered_map< std::string, std::vector< int > > regions_
Definition: GenericOutputModule.hpp:572
ScalarBuffer swmin_
Definition: GenericOutputModule.hpp:611
FlowsContainer< GetPropType< TypeTag, Properties::FluidSystem > > flowsC_
Definition: GenericOutputModule.hpp:641
std::map< std::pair< std::string, int >, double > blockData_
Definition: GenericOutputModule.hpp:646
ScalarBuffer gasDissolutionFactor_
Definition: GenericOutputModule.hpp:613
MechContainer< Scalar > mech_
Definition: GenericOutputModule.hpp:628
BioeffectsContainer< Scalar > bioeffectsC_
Definition: GenericOutputModule.hpp:621
ExtboContainer< Scalar > extboC_
Definition: GenericOutputModule.hpp:605
ScalarBuffer gasDissolutionFactorInWater_
Definition: GenericOutputModule.hpp:615
ScalarBuffer pcgw_
Definition: GenericOutputModule.hpp:623
LogOutputHelper< Scalar > logOutput_
Definition: GenericOutputModule.hpp:543
void assignPhaseProperties(data::Solution &sol, const PhasePropertyNames &names)
Move the phase density and viscosity buffers to sol under names.
std::vector< Scalar > ScalarBuffer
Definition: GenericOutputModule.hpp:325
ScalarBuffer somin_
Definition: GenericOutputModule.hpp:610
void updateTotalVolumesAndPressures_(const unsigned globalDofIdx, const IntensiveQuantities &intQuants, const double totVolume, const Scalar referencePorosity)
Definition: GenericOutputModule.hpp:362
InterRegFlowMap interRegionFlows_
Definition: GenericOutputModule.hpp:542
ScalarBuffer soMax_
Definition: GenericOutputModule.hpp:606
std::array< ScalarBuffer, numPhases > density_
Definition: GenericOutputModule.hpp:632
ScalarBuffer cFoam_
Definition: GenericOutputModule.hpp:601
ScalarBuffer waterVaporizationFactor_
Definition: GenericOutputModule.hpp:616
std::array< ScalarBuffer, numPhases > saturation_
Definition: GenericOutputModule.hpp:631
ScalarBuffer rsw_
Definition: GenericOutputModule.hpp:593
ScalarBuffer oilSaturationPressure_
Definition: GenericOutputModule.hpp:596
const EclipseState & eclState_
Definition: GenericOutputModule.hpp:536
ScalarBuffer ppcw_
Definition: GenericOutputModule.hpp:612
virtual void assignToSolution(data::Solution &sol)
Move all buffers to data::Solution.
ScalarBuffer cSalt_
Definition: GenericOutputModule.hpp:602
ScalarBuffer pcog_
Definition: GenericOutputModule.hpp:625
const Inplace * initialInplace() const
Definition: GenericOutputModule.hpp:274
ScalarBuffer swMax_
Definition: GenericOutputModule.hpp:607
ScalarBuffer rswSol_
Definition: GenericOutputModule.hpp:599
std::map< std::pair< std::string, int >, double > extraBlockData_
Definition: GenericOutputModule.hpp:649
ScalarBuffer fluidPressure_
Definition: GenericOutputModule.hpp:591
ScalarBuffer gasFormationVolumeFactor_
Definition: GenericOutputModule.hpp:586
virtual void setRestart(const data::Solution &sol, unsigned elemIdx, unsigned globalDofIndex)
std::vector< int > failedCellsPb_
Definition: GenericOutputModule.hpp:583
ScalarBuffer pSalt_
Definition: GenericOutputModule.hpp:603
Inter-region flow accumulation maps for all region definition arrays.
Definition: InterRegFlows.hpp:179
void addConnection(const Cell &source, const Cell &destination, const data::InterRegFlowMap::FlowRates &rates)
void clear()
Clear all internal buffers, but preserve allocated capacity.
Output module for the results black oil model writing in ECL binary format.
Definition: OutputBlackoilModule.hpp:101
void processElement(const ElementContext &elemCtx)
Modify the internal buffers according to the intensive quanties relevant for an element.
Definition: OutputBlackoilModule.hpp:363
void initializeFluxData()
Prepare for capturing connection fluxes, particularly to account for inter-region flows.
Definition: OutputBlackoilModule.hpp:652
void setupExtractors(const bool isSubStep, const int reportStepNum)
Setup list of active element-level data extractors.
Definition: OutputBlackoilModule.hpp:291
void processFluxes(const ElementContext &elemCtx, ActiveIndex &&activeIndex, CartesianIndex &&cartesianIndex)
Capture connection fluxes, particularly to account for inter-region flows.
Definition: OutputBlackoilModule.hpp:615
void setRestart(const data::Solution &sol, const unsigned elemIdx, const unsigned globalDofIndex) override
Restore the buffers this module owns from a restart file.
Definition: OutputBlackoilModule.hpp:807
void clearExtractors()
Clear list of active element-level data extractors.
Definition: OutputBlackoilModule.hpp:299
void allocBuffers(const unsigned bufferSize, const unsigned reportStepNum, const bool substep, const bool log, const bool forceRestartFieldAllocation)
Allocate memory for the scalar fields we would like to write to ECL output files.
Definition: OutputBlackoilModule.hpp:269
void outputFipAndResvLogToCSV(const std::size_t reportStepNum, const bool substep, const Parallel::Communication &comm)
Definition: OutputBlackoilModule.hpp:549
void allocFormulationBuffers(std::map< std::string, int > &rstKeywords, const unsigned bufferSize) override
Allocate the buffers of the quantities only the black-oil formulation produces, keyed on their restar...
Definition: OutputBlackoilModule.hpp:828
void assignToFluidState(FluidState &fs, unsigned elemIdx) const
Definition: OutputBlackoilModule.hpp:676
void assignToSolution(data::Solution &sol) override
Move all buffers to data::Solution.
Definition: OutputBlackoilModule.hpp:310
void initHysteresisParams(Simulator &simulator, unsigned elemIdx) const
Definition: OutputBlackoilModule.hpp:728
void updateFluidInPlace(const ElementContext &elemCtx)
Definition: OutputBlackoilModule.hpp:792
OutputBlackOilModule(const Simulator &simulator, const SummaryConfig &smryCfg, const CollectDataOnIORankType &collectOnIORank)
Definition: OutputBlackoilModule.hpp:154
void outputFipAndResvLog(const Inplace &inplace, const std::size_t reportStepNum, double elapsed, boost::posix_time::ptime currentDate, const bool substep, const Parallel::Communication &comm)
Definition: OutputBlackoilModule.hpp:498
const InterRegFlowMap & getInterRegFlows() const
Get read-only access to collection of inter-region flows.
Definition: OutputBlackoilModule.hpp:670
void processElementBlockData(const ElementContext &elemCtx)
Definition: OutputBlackoilModule.hpp:410
void finalizeFluxData()
Finalize capturing connection fluxes.
Definition: OutputBlackoilModule.hpp:662
void updateFluidInPlace(const unsigned globalDofIdx, const IntensiveQuantities &intQuants, const double totVolume)
Definition: OutputBlackoilModule.hpp:799
Declare the properties used by the infrastructure code of the finite volume discretizations.
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Phase
Phase indices for reservoir coupling, we currently only support black-oil phases (oil,...
Definition: ReservoirCoupling.hpp:184
constexpr void ignoreUnused(T &&...) noexcept
Utility to silence "unused variable" warnings in lambdas.
Definition: OutputBlackoilModule.hpp:90
Definition: blackoilbioeffectsmodules.hh:45
std::string moduleVersionName()
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
This file provides the infrastructure to retrieve run-time parameters.
The Opm property system, traits with inheritance.
Definition: GenericOutputModule.hpp:410
std::string_view oilDensity
Definition: GenericOutputModule.hpp:411
Minimal characteristics of a cell from a simulation grid.
Definition: InterRegFlows.hpp:50
Context passed to element extractor functions.
Definition: OutputExtractor.hpp:208
Wrapping struct holding types used for block-level data extraction.
Definition: OutputExtractor.hpp:195
std::variant< ScalarEntry, PhaseEntry, TensorEntry > Entry
Descriptor for extractors.
Definition: OutputExtractor.hpp:262
std::unordered_map< int, std::vector< Exec > > ExecMap
A map of extraction executors, keyed by cartesian cell index.
Definition: OutputExtractor.hpp:277
static LgrExecMap setupLgrExecMap(std::map< std::tuple< std::string, int, int >, double > &lgrBlockData, const std::span< const Entry > handlers)
Setup an LGR-cell extractor executor map.
Definition: OutputExtractor.hpp:463
static ExecMap setupExecMap(std::map< std::pair< std::string, int >, double > &blockData, const std::span< const Entry > handlers)
Setup an extractor executor map from a map of evaluations to perform.
Definition: OutputExtractor.hpp:410
std::unordered_map< int, std::unordered_map< int, std::vector< Exec > > > LgrExecMap
Definition: OutputExtractor.hpp:452
static void process(const std::vector< Exec > &blockExtractors, const Context &ectx)
Process a list of block extractors.
Definition: OutputExtractor.hpp:435
Context passed to extractor functions.
Definition: OutputExtractor.hpp:77
int episodeIndex
Current report step.
Definition: OutputExtractor.hpp:80
Struct holding hysteresis parameters.
Definition: OutputExtractor.hpp:66
Scalar somin
Min oil saturation.
Definition: OutputExtractor.hpp:72
Scalar swmin
Min water saturation.
Definition: OutputExtractor.hpp:69
Scalar swmax
Max water saturation.
Definition: OutputExtractor.hpp:68
Scalar shmax
Max something.
Definition: OutputExtractor.hpp:71
Scalar sgmax
Max gas saturation.
Definition: OutputExtractor.hpp:70
Scalar somax
Max oil saturation.
Definition: OutputExtractor.hpp:67
Wrapping struct holding types used for element-level data extraction.
Definition: OutputExtractor.hpp:57
static void process(const Context &ectx, const std::vector< Entry > &extractors)
Process the given extractor entries.
Definition: OutputExtractor.hpp:160
static std::vector< Entry > removeInactive(std::array< Entry, size > &input)
Obtain vector of active extractors from an array of extractors.
Definition: OutputExtractor.hpp:123