23#ifndef OPM_ECL_GENERIC_WRITER_IMPL_HPP
24#define OPM_ECL_GENERIC_WRITER_IMPL_HPP
26#include <dune/grid/common/mcmgmapper.hh>
28#include <opm/grid/cpgrid/LgrOutputHelpers.hpp>
29#include <opm/grid/GridHelpers.hpp>
30#include <opm/grid/utility/cartesianToCompressed.hpp>
32#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
33#include <opm/input/eclipse/EclipseState/Grid/RegionSetMatcher.hpp>
34#include <opm/input/eclipse/EclipseState/Grid/NNC.hpp>
35#include <opm/input/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
37#include <opm/input/eclipse/Schedule/Action/State.hpp>
38#include <opm/input/eclipse/Schedule/RPTConfig.hpp>
39#include <opm/input/eclipse/Schedule/Schedule.hpp>
40#include <opm/input/eclipse/Schedule/SummaryState.hpp>
41#include <opm/input/eclipse/Schedule/UDQ/UDQConfig.hpp>
42#include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
43#include <opm/input/eclipse/Schedule/Well/WellConnections.hpp>
44#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
46#include <opm/input/eclipse/Units/UnitSystem.hpp>
48#include <opm/output/data/RegionVariableMapping.hpp>
50#include <opm/output/eclipse/EclipseIO.hpp>
51#include <opm/output/eclipse/RegionVariableCollection.hpp>
52#include <opm/output/eclipse/RestartValue.hpp>
53#include <opm/output/eclipse/Summary.hpp>
76#include <unordered_map>
96bool directVerticalNeighbors(
const std::array<int, 3>& cartDims,
97 const std::unordered_map<int,int>& cartesianToActive,
98 int smallGlobalIndex,
int largeGlobalIndex)
100 assert(smallGlobalIndex <= largeGlobalIndex);
101 std::array<int, 3> ijk1, ijk2;
102 auto globalToIjk = [cartDims](
int gc) {
103 std::array<int, 3> ijk;
104 ijk[0] = gc % cartDims[0];
106 ijk[1] = gc % cartDims[1];
107 ijk[2] = gc / cartDims[1];
110 ijk1 = globalToIjk(smallGlobalIndex);
111 ijk2 = globalToIjk(largeGlobalIndex);
112 assert(ijk2[2]>=ijk1[2]);
114 if ( ijk1[0] == ijk2[0] && ijk1[1] == ijk2[1] && (ijk2[2] - ijk1[2]) > 1)
116 assert((largeGlobalIndex-smallGlobalIndex)%(cartDims[0]*cartDims[1])==0);
117 for (
int gi = smallGlobalIndex + cartDims[0] * cartDims[1]; gi < largeGlobalIndex;
118 gi += cartDims[0] * cartDims[1] )
120 if ( cartesianToActive.find( gi ) != cartesianToActive.end() )
130std::unordered_map<std::string, Opm::data::InterRegFlowMap>
133 auto maps = std::unordered_map<std::string, Opm::data::InterRegFlowMap>{};
135 const auto& regionNames = map.
names();
137 const auto nmap = regionNames.size();
140 for (
auto mapID = 0*nmap; mapID < nmap; ++mapID) {
141 maps.emplace(regionNames[mapID], std::move(flows[mapID]));
149 Opm::Action::State actionState_;
150 Opm::WellTestState wtestState_;
151 Opm::SummaryState summaryState_;
152 Opm::UDQState udqState_;
153 Opm::EclipseIO& eclIO_;
155 std::optional<int> timeStepNum_;
157 double secondsElapsed_;
158 std::vector<Opm::RestartValue> restartValue_;
159 bool writeDoublePrecision_;
161 bool forcedSimulationFinished_;
163 explicit EclWriteTasklet(
const Opm::Action::State& actionState,
164 const Opm::WellTestState& wtestState,
165 const Opm::SummaryState& summaryState,
166 const Opm::UDQState& udqState,
167 Opm::EclipseIO& eclIO,
169 std::optional<int> timeStepNum,
171 double secondsElapsed,
172 std::vector<Opm::RestartValue> restartValue,
173 bool writeDoublePrecision,
174 bool forcedSimulationFinished)
175 : actionState_(actionState)
176 , wtestState_(wtestState)
177 , summaryState_(summaryState)
178 , udqState_(udqState)
180 , reportStepNum_(reportStepNum)
181 , timeStepNum_(timeStepNum)
182 , isSubStep_(isSubStep)
183 , secondsElapsed_(secondsElapsed)
184 , restartValue_(std::move(restartValue))
185 , writeDoublePrecision_(writeDoublePrecision)
186 , forcedSimulationFinished_(forcedSimulationFinished)
192 if (this->restartValue_.size() == 1) {
193 this->eclIO_.writeTimeStep(this->actionState_,
197 this->reportStepNum_,
199 this->secondsElapsed_,
200 std::move(this->restartValue_.back()),
201 this->writeDoublePrecision_,
203 forcedSimulationFinished_);
206 this->eclIO_.writeTimeStep(this->actionState_,
210 this->reportStepNum_,
212 this->secondsElapsed_,
213 std::move(this->restartValue_),
214 this->writeDoublePrecision_,
216 forcedSimulationFinished_);
225template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
228 const EclipseState& eclState,
229 const SummaryConfig& summaryConfig,
231 const EquilGrid* equilGrid,
232 const GridView& gridView,
235 bool enableAsyncOutput,
237 : collectOnIORank_(grid,
242 summaryConfig.fip_regions_interreg_flow())
244 , gridView_ (gridView)
245 , schedule_ (schedule)
246 , eclState_ (eclState)
247 , cartMapper_ (cartMapper)
248 , equilCartMapper_(equilCartMapper)
249 , equilGrid_ (equilGrid)
255 this->eclIO_ = std::make_unique<EclipseIO>
257 UgGridHelpers::createEclipseGrid(*equilGrid,
eclState_.getInputGrid()),
258 this->schedule_, summaryConfig,
"", enableEsmry);
263 int numWorkerThreads = 0;
265 numWorkerThreads = 1;
271template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
279template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
283 if (collectOnIORank_.isIORank()) {
284 std::map<std::string, std::vector<int>> integerVectors;
288 if (collectOnIORank_.isParallel() && !collectOnIORank_.globalRanks().empty()) {
289 integerVectors.emplace(
"MPI_RANK", collectOnIORank_.globalRanks());
292 if (
const auto& lgrs = this->eclState_.getLgrs(); lgrs.size() > 0) {
294 const auto nncCollection = Opm::NNCCollection::fromLGROutputContainers(this->outputNnc_,
295 this->outputNncGlobalLocal_,
296 this->outputAmalgamatedNnc_);
297 eclIO_->writeInitial(*this->outputTrans_,
301 eclIO_->writeInitial(*this->outputTrans_,
303 this->outputNnc_.front());
305 this->outputTrans_.reset();
307 this->gatheredLgrTrans_.reset();
310template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
321 if (collectOnIORank_.isIORank()) {
322 constexpr bool equilGridIsCpGrid = std::is_same_v<EquilGrid, Dune::CpGrid>;
324 const auto levelCartMapp = this->createLevelCartMapp_<equilGridIsCpGrid>();
325 const auto levelCartToLevelCompressed = this->createCartesianToActiveMaps_<equilGridIsCpGrid>(levelCartMapp);
326 auto computeLevelIndices = this->computeLevelIndices_<equilGridIsCpGrid>();
327 auto computeLevelCartIdx = this->computeLevelCartIdx_<equilGridIsCpGrid>(levelCartMapp, *(this->equilCartMapper_));
328 auto computeLevelCartDimensions = this->computeLevelCartDimensions_<equilGridIsCpGrid>(levelCartMapp, *(this->equilCartMapper_));
329 auto computeOriginIndices = this->computeOriginIndices_<equilGridIsCpGrid>();
331 computeTrans_(levelCartToLevelCompressed, map, computeLevelIndices,
332 computeLevelCartIdx, computeLevelCartDimensions, computeOriginIndices);
333 exportNncStructure_(levelCartToLevelCompressed, map, computeLevelIndices, computeLevelCartIdx,
334 computeLevelCartDimensions, computeOriginIndices);
341 if (collectOnIORank_.isParallel()) {
342 const auto& comm = grid_.comm();
349template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
354 const auto& numAquCell = this->eclState_.aquifer().hasNumericalAquifer()
355 ? this->eclState_.aquifer().numericalAquifers().allAquiferCellIds()
356 : std::vector<std::size_t>{};
358 return std::ranges::binary_search(numAquCell.begin(), numAquCell.end(), cartIdx);
361template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
363EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
364isNumAquConn_(
const std::size_t cartIdx1,
365 const std::size_t cartIdx2)
const
367 return isNumAquCell_(cartIdx1) || isNumAquCell_(cartIdx2);
370template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
371template<
bool equilGr
idIsCpGr
id>
373EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
374createLevelCartMapp_()
const
376 if constexpr (equilGridIsCpGrid) {
382template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
383template<
bool equilGr
idIsCpGr
id>
384std::vector<std::unordered_map<int,int>>
385EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
388 if constexpr (equilGridIsCpGrid) {
389 if (this->equilGrid_->maxLevel()) {
390 return Opm::Lgr::levelCartesianToLevelCompressedMaps(*this->equilGrid_, levelCartMapp); }
392 return std::vector<std::unordered_map<int,int>>{ cartesianToCompressed(equilGrid_->size(0), UgGridHelpers::globalCell(*equilGrid_)) };
395 return std::vector<std::unordered_map<int,int>>{ cartesianToCompressed(equilGrid_->size(0), UgGridHelpers::globalCell(*equilGrid_)) };
398template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
399template<
bool equilGr
idIsCpGr
id>
400std::function<std::array<int,3>(
int)>
401EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
405 if constexpr (equilGridIsCpGrid) {
406 return [&](
int level)
408 return levelCartMapp.cartesianDimensions(level);
412 return [&]([[maybe_unused]]
int level)
415 return equilCartMapp.cartesianDimensions();
420template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
421template<
bool equilGr
idIsCpGr
id>
422std::function<int(
int,
int)>
423EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
427 if constexpr (equilGridIsCpGrid) {
428 return [&](
int levelCompressedIdx,
431 return levelCartMapp.cartesianIndex(levelCompressedIdx, level);
435 return [&](
int levelCompressedIdx,
436 [[maybe_unused]]
int level)
439 return equilCartMapp.cartesianIndex(levelCompressedIdx);
444template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
445template <
bool equilGr
idIsCpGr
id>
447EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
448computeLevelIndices_()
const
450 if constexpr (equilGridIsCpGrid) {
451 return [](
const auto& intersection,
455 return std::pair{intersection.inside().getLevelElem().index(), intersection.outside().getLevelElem().index()};
459 return [](
const auto&,
460 const auto& intersectionInsideLeafIdx,
461 const auto& intersectionOutsideLeafIdx)
463 return std::pair{intersectionInsideLeafIdx, intersectionOutsideLeafIdx};
468template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
469template <
bool equilGr
idIsCpGr
id>
471EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
472computeOriginIndices_()
const
474 if constexpr (equilGridIsCpGrid) {
475 return [](
const auto& intersection,
479 return std::pair{intersection.inside().getOrigin().index(), intersection.outside().getOrigin().index()};
483 return [](
const auto&,
484 const auto& intersectionInsideLeafIdx,
485 const auto& intersectionOutsideLeafIdx)
487 return std::pair{intersectionInsideLeafIdx, intersectionOutsideLeafIdx};
492template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
494EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
495allocateLevelTrans_(
const std::array<int,3>& levelCartDims,
496 data::Solution& levelTrans)
const
498 auto createLevelCellData = [&levelCartDims]() {
499 return Opm::data::CellData{
500 Opm::UnitSystem::measure::transmissibility,
501 std::vector<double>(levelCartDims[0] * levelCartDims[1] * levelCartDims[2], 0.0),
502 Opm::data::TargetType::INIT
507 levelTrans.emplace(
"TRANX", createLevelCellData());
508 levelTrans.emplace(
"TRANY", createLevelCellData());
509 levelTrans.emplace(
"TRANZ", createLevelCellData());
512template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
513template<
typename LevelIndicesFunction,
typename OriginIndicesFunction>
515EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
516computeTrans_(
const std::vector<std::unordered_map<int,int>>& levelCartToLevelCompressed,
517 const std::function<
unsigned int(
unsigned int)>& map,
518 const LevelIndicesFunction& computeLevelIndices,
519 const std::function<
int(
int,
int)>& computeLevelCartIdx,
520 const std::function<std::array<int,3>(
int)>& computeLevelCartDims,
521 const OriginIndicesFunction& computeOriginIndices)
const
524 outputTrans_ = std::make_unique<std::vector<data::Solution>>(std::vector<data::Solution>{});
527 using GlobalGridView =
typename EquilGrid::LeafGridView;
528 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
529 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
530 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
533 int maxLevel = this->equilGrid_->maxLevel();
535 outputTrans_->resize(maxLevel+1);
537 for (
int level = 0; level <= maxLevel; ++level) {
538 allocateLevelTrans_(computeLevelCartDims(level), this->outputTrans_->at(level));
541 for (
const auto& elem : elements(globalGridView)) {
542 for (
const auto& is : intersections(globalGridView, elem)) {
546 if ( is.inside().level() != is.outside().level() )
550 unsigned c1 = globalElemMapper.index(is.inside());
551 unsigned c2 = globalElemMapper.index(is.outside());
556 int level = is.inside().level();
559 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
561 const int levelCartIdxIn = computeLevelCartIdx(levelInIdx, level);
562 const int levelCartIdxOut = computeLevelCartIdx(levelOutIdx, level);
568 const auto [originInIdx, originOutIdx] = computeOriginIndices(is, c1, c2);
570 const auto originCartIdxIn = computeLevelCartIdx(originInIdx, 0);
571 const auto originCartIdxOut = computeLevelCartIdx(originOutIdx, 0);
574 if (isNumAquCell_(originCartIdxIn) || isNumAquCell_(originCartIdxOut)) {
584 const auto minLevelCartIdx = std::min(levelCartIdxIn, levelCartIdxOut);
585 const auto maxLevelCartIdx = std::max(levelCartIdxIn, levelCartIdxOut);
587 const auto& levelCartDims = computeLevelCartDims(level);
595 if (maxLevelCartIdx - minLevelCartIdx == 1 && levelCartDims[0] > 1 ) {
596 outputTrans_->at(level).at(
"TRANX").template data<double>()[minLevelCartIdx] =
597 gatheredOrGlobalTrans_(std::array{level, minLevelCartIdx, maxLevelCartIdx}, c1, c2);
601 if (maxLevelCartIdx - minLevelCartIdx == levelCartDims[0] && levelCartDims[1] > 1) {
602 outputTrans_->at(level).at(
"TRANY").template data<double>()[minLevelCartIdx] =
603 gatheredOrGlobalTrans_(std::array{level, minLevelCartIdx, maxLevelCartIdx}, c1, c2);
607 if ( maxLevelCartIdx - minLevelCartIdx == levelCartDims[0]*levelCartDims[1] ||
608 directVerticalNeighbors(levelCartDims,
609 levelCartToLevelCompressed[level],
612 outputTrans_->at(level).at(
"TRANZ").template data<double>()[minLevelCartIdx] =
613 gatheredOrGlobalTrans_(std::array{level, minLevelCartIdx, maxLevelCartIdx}, c1, c2);
619template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
621EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
622isCartesianNeighbour_(
const std::array<int,3>& levelCartDims,
623 const std::size_t levelCartIdx1,
624 const std::size_t levelCartIdx2)
const
626 const int diff = levelCartIdx2 - levelCartIdx1;
629 || (diff == levelCartDims[0])
630 || (diff == (levelCartDims[0] * levelCartDims[1]));
633template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
635EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
636isDirectNeighbours_(
const std::unordered_map<int,int>& levelCartesianToActive,
637 const std::array<int,3>& levelCartDims,
638 const std::size_t levelCartIdx1,
639 const std::size_t levelCartIdx2)
const
641 return isCartesianNeighbour_(levelCartDims, levelCartIdx1, levelCartIdx2)
642 || directVerticalNeighbors(levelCartDims, levelCartesianToActive, levelCartIdx1, levelCartIdx2);
645template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
647EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
648activeCell_(
const std::unordered_map<int,int>& levelCartToLevelCompressed,
649 const std::size_t levelCartIdx)
const
651 auto pos = levelCartToLevelCompressed.find(levelCartIdx);
652 return (pos == levelCartToLevelCompressed.end()) ? -1 : pos->second;
655template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
657EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
658allocateAllNncs_(
int maxLevel)
const
660 this->outputNnc_.resize(maxLevel+1);
668 this->outputNncGlobalLocal_.resize(maxLevel);
676 this->outputAmalgamatedNnc_.resize(maxLevel-1);
677 for (
int i = 0; i < maxLevel-1; ++i) {
678 this->outputAmalgamatedNnc_[i].resize(maxLevel-1-i);
683template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
684template<
typename LevelIndicesFunction,
typename OriginIndicesFunction>
685std::vector<std::vector<NNCdata>>
686EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
687exportNncStructure_(
const std::vector<std::unordered_map<int,int>>& levelCartToLevelCompressed,
688 const std::function<
unsigned int(
unsigned int)>& map,
689 const LevelIndicesFunction& computeLevelIndices,
690 const std::function<
int(
int,
int)>& computeLevelCartIdx,
691 const std::function<std::array<int,3>(
int)>& computeLevelCartDims,
692 const OriginIndicesFunction& computeOriginIndices)
const
694 const auto& nncData = this->eclState_.getInputNNC().input();
695 const auto& nncEdit = this->eclState_.getInputNNC().edit();
696 const auto& nncEditr = this->eclState_.getInputNNC().editr();
697 const auto& unitSystem = this->eclState_.getDeckUnitSystem();
698 const auto& transMult = this->eclState_.getTransMult();
701 const auto& equilCartMapper = *equilCartMapper_;
703 const auto& level0CartDims = equilCartMapper.cartesianDimensions();
705 int maxLevel = this->equilGrid_->maxLevel();
706 allocateAllNncs_(maxLevel);
708 using GlobalGridView =
typename EquilGrid::LeafGridView;
709 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
710 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
711 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
713 for (
const auto& elem : elements(globalGridView)) {
714 for (
const auto& is : intersections(globalGridView, elem)) {
719 unsigned c1 = globalElemMapper.index(is.inside());
720 unsigned c2 = globalElemMapper.index(is.outside());
725 if ( is.inside().level() != is.outside().level() ) {
727 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
729 const int levelIn = is.inside().level();
730 const int levelOut = is.outside().level();
732 auto levelCartIdxIn = computeLevelCartIdx(levelInIdx, levelIn);
733 auto levelCartIdxOut = computeLevelCartIdx(levelOutIdx, levelOut);
736 std::pair<int,int> smallerPair = {levelIn, levelCartIdxIn},
737 largerPair = {levelOut, levelCartIdxOut};
738 if (smallerPair.first > largerPair.first) {
739 std::swap(smallerPair, largerPair);
742 const auto& [smallerLevel, smallerLevelCartIdx] = smallerPair;
743 const auto& [largerLevel, largerLevelCartIdx] = largerPair;
745 auto t = this->gatheredOrGlobalTrans_(std::array{smallerLevel, smallerLevelCartIdx,
746 largerLevel, largerLevelCartIdx},
754 const auto tt = unitSystem
755 .from_si(UnitSystem::measure::transmissibility, t);
757 if (std::isnormal(tt) && (tt > 1.0e-12)) {
759 if (smallerLevel == 0) {
760 this->outputNncGlobalLocal_[largerLevel-1].emplace_back(smallerLevelCartIdx, largerLevelCartIdx, t);
763 assert(smallerLevel >= 1);
764 this->outputAmalgamatedNnc_[smallerLevel-1][largerLevel-smallerLevel-1].emplace_back(smallerLevelCartIdx, largerLevelCartIdx, t);
770 assert(is.inside().level() == is.outside().level());
771 const int level = is.inside().level();
777 const auto [originInIdx, originOutIdx] = computeOriginIndices(is, c1, c2);
779 const std::size_t originCartIdxIn = computeLevelCartIdx(originInIdx, 0);
780 const std::size_t originCartIdxOut = computeLevelCartIdx(originOutIdx, 0);
783 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
785 auto levelCartIdxIn = computeLevelCartIdx(levelInIdx, level);
786 auto levelCartIdxOut = computeLevelCartIdx(levelOutIdx, level);
788 if ( levelCartIdxOut < levelCartIdxIn )
789 std::swap(levelCartIdxIn, levelCartIdxOut);
797 const auto& levelCartDims = computeLevelCartDims(level);
800 assert(!isNumAquConn_(originCartIdxIn, originCartIdxOut) || level == 0);
802 if (isNumAquConn_(originCartIdxIn, originCartIdxOut) ||
803 ! isDirectNeighbours_(levelCartToLevelCompressed[level],
805 levelCartIdxIn, levelCartIdxOut)) {
809 auto t = this->gatheredOrGlobalTrans_(std::array{level, levelCartIdxIn, levelCartIdxOut},
813 auto candidate = std::lower_bound(nncData.begin(), nncData.end(),
814 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
815 const auto transMlt = transMult.getRegionMultiplierNNC(originCartIdxIn, originCartIdxOut);
816 bool foundNncEditr =
false;
818 while ((candidate != nncData.end()) &&
819 (candidate->cell1 == originCartIdxIn) &&
820 (candidate->cell2 == originCartIdxOut))
822 auto trans = candidate->trans;
824 if (! nncEditr.empty()) {
825 auto it = std::lower_bound(nncEditr.begin(), nncEditr.end(),
826 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
827 foundNncEditr = it != nncEditr.end() && it->cell1 == originCartIdxIn && it->cell2 == originCartIdxOut;
833 if (! nncEdit.empty()) {
834 auto it = std::lower_bound(nncEdit.begin(), nncEdit.end(),
835 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
836 if (it != nncEdit.end() && it->cell1 == originCartIdxIn && it->cell2 == originCartIdxOut) {
854 const auto tt = unitSystem
855 .from_si(UnitSystem::measure::transmissibility, t);
857 if (std::isnormal(tt) && (tt > 1.0e-12)) {
858 this->outputNnc_[level].emplace_back(levelCartIdxIn, levelCartIdxOut, t);
866 std::vector<NNCdata> inputedNnc{};
867 const auto generatedNnc = outputNnc_[0];
871 for (
const auto& entry : nncData) {
880 assert (entry.cell2 >= entry.cell1);
882 if (! isCartesianNeighbour_(level0CartDims, entry.cell1, entry.cell2) ||
883 isNumAquConn_(entry.cell1, entry.cell2))
885 bool foundNncEdit =
false;
886 auto trans = entry.trans;
887 if (! nncEdit.empty()) {
888 auto it = std::lower_bound(nncEdit.begin(), nncEdit.end(),
889 NNCdata {entry.cell1, entry.cell2, 0.0 });
890 if (it != nncEdit.end() && it->cell1 == entry.cell1 && it->cell2 == entry.cell2) {
895 if (! foundNncEdit) {
899 const auto c1 = activeCell_(levelCartToLevelCompressed[0], entry.cell1);
900 const auto c2 = activeCell_(levelCartToLevelCompressed[0], entry.cell2);
902 if ((c1 < 0) || (c2 < 0)) {
912 const auto key = std::array{0,
static_cast<int>(entry.cell1),
913 static_cast<int>(entry.cell2)};
914 const double* gathered = this->findGatheredTrans_(key);
915 if (!this->gatheredLgrTrans_.has_value() || gathered !=
nullptr) {
916 trans = (gathered !=
nullptr)
918 : this->globalTrans().transmissibility(c1, c2);
920 if (! generatedNnc.empty()) {
921 for (
const auto& generated : generatedNnc) {
922 if (entry.cell1 == generated.cell1 && entry.cell2 == generated.cell2) {
923 trans -= generated.trans;
930 const auto tt = unitSystem
931 .from_si(UnitSystem::measure::transmissibility, trans);
936 if (std::isnormal(tt) && ! (tt < 1.0e-6)) {
937 inputedNnc.emplace_back(entry.cell1, entry.cell2, trans);
942 this->outputNnc_[0].insert(this->outputNnc_[0].begin(), inputedNnc.begin(), inputedNnc.end());
943 return this->outputNnc_;
946template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
949 const std::optional<int> timeStepNum,
950 const bool isSubStep,
951 const bool isForcedFinalOutput,
952 data::Solution&& localCellData,
953 data::Wells&& localWellData,
954 data::GroupAndNetworkValues&& localGroupAndNetworkData,
955 data::Aquifers&& localAquiferData,
956 WellTestState&& localWTestState,
957 const Action::State& actionState,
958 const UDQState& udqState,
959 const SummaryState& summaryState,
960 const std::vector<Scalar>& thresholdPressure,
963 bool doublePrecision,
969 const auto isParallel = this->collectOnIORank_.isParallel();
970 const bool needsReordering = this->collectOnIORank_.doesNeedReordering();
972 RestartValue restartValue {
973 (isParallel || needsReordering)
974 ? this->collectOnIORank_.globalCellData()
975 : std::move(localCellData),
977 isParallel ? this->collectOnIORank_.globalWellData()
978 : std::move(localWellData),
980 isParallel ? this->collectOnIORank_.globalGroupAndNetworkData()
981 : std::move(localGroupAndNetworkData),
983 isParallel ? this->collectOnIORank_.globalAquiferData()
984 : std::move(localAquiferData)
987 if (eclState_.getSimulationConfig().useThresholdPressure()) {
988 restartValue.addExtra(
"THRESHPR", UnitSystem::measure::pressure,
994 restartValue.addExtra(
"OPMEXTRA", std::vector<double>(1, nextStepSize));
999 const auto flowsn_global = isParallel ? this->collectOnIORank_.globalFlowsn() : std::move(flowsn);
1000 for (
const auto& flows : flowsn_global) {
1001 if (flows.name.empty())
1003 if (flows.name ==
"FLOGASN+") {
1004 restartValue.addExtra(flows.name, UnitSystem::measure::gas_surface_rate, flows.values);
1006 restartValue.addExtra(flows.name, UnitSystem::measure::liquid_surface_rate, flows.values);
1011 const auto floresn_global = isParallel ? this->collectOnIORank_.globalFloresn() : std::move(floresn);
1012 for (
const auto& flores : floresn_global) {
1013 if (flores.name.empty()) {
1016 restartValue.addExtra(flores.name, UnitSystem::measure::rate, flores.values);
1020 std::vector<Opm::RestartValue> restartValues{};
1022 if ( !isParallel && !needsReordering && (this->eclState_.getLgrs().size()>0) && (this->grid_.maxLevel()>0) ) {
1027 Opm::Lgr::extractRestartValueLevelGrids<Grid>(this->grid_, restartValue, restartValues);
1030 restartValues.reserve(1);
1031 restartValues.push_back(std::move(restartValue));
1037 this->taskletRunner_->barrier();
1040 if (this->taskletRunner_->failure()) {
1041 throw std::runtime_error(
"Failure in the TaskletRunner while writing output.");
1045 auto eclWriteTasklet = std::make_shared<EclWriteTasklet>(
1047 isParallel ? this->collectOnIORank_.globalWellTestState() : std::move(localWTestState),
1048 summaryState, udqState, *this->eclIO_,
1049 reportStepNum, timeStepNum, isSubStep, curTime, std::move(restartValues), doublePrecision,
1050 isForcedFinalOutput);
1053 this->taskletRunner_->dispatch(std::move(eclWriteTasklet));
1056template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1059 const Scalar curTime,
1060 const data::Wells& localWellData,
1061 const data::WellBlockAveragePressures& localWBPData,
1062 const data::GroupAndNetworkValues& localGroupAndNetworkData,
1063 const std::map<int,data::AquiferData>& localAquiferData,
1064 const std::map<std::pair<std::string, int>,
double>& blockData,
1065 const std::map<std::tuple<std::string, int, int>,
double>& lgrBlockData,
1066 const std::map<std::string, double>& miscSummaryData,
1067 const std::map<std::string, std::vector<double>>& regionData,
1068 const data::RegionVariableMapping& regVarMap,
1069 const RegionVariableCollection& regVars,
1070 const Inplace& inplace,
1071 const Inplace* initialInPlace,
1073 SummaryState& summaryState,
1075 const data::ReservoirCouplingGroupRates* rcGroupRates)
1077 if (collectOnIORank_.isIORank()) {
1078 const auto& wellData = this->collectOnIORank_.isParallel()
1079 ? this->collectOnIORank_.globalWellData()
1082 const auto& wbpData = this->collectOnIORank_.isParallel()
1083 ? this->collectOnIORank_.globalWBPData()
1086 const auto& groupAndNetworkData = this->collectOnIORank_.isParallel()
1087 ? this->collectOnIORank_.globalGroupAndNetworkData()
1088 : localGroupAndNetworkData;
1090 const auto& aquiferData = this->collectOnIORank_.isParallel()
1091 ? this->collectOnIORank_.globalAquiferData()
1094 const auto interreg_flows = getInterRegFlowsAsMap(interRegFlows);
1096 const auto values = out::Summary::DynamicSimulatorState {
1097 .well_solution = &wellData,
1099 .group_and_nwrk_solution = &groupAndNetworkData,
1100 .single_values = &miscSummaryData,
1101 .region_values = ®ionData,
1102 .reg_var_map = ®VarMap,
1103 .reg_var_coll = ®Vars,
1104 .block_values = &blockData,
1105 .aquifer_values = &aquiferData,
1106 .interreg_flows = &interreg_flows,
1107 .rc_group_rates = rcGroupRates,
1109 .current = &inplace,
1110 .initial = initialInPlace
1112 .lgr_block_values = &lgrBlockData
1115 this->eclIO_->summary()
1116 .eval(reportStepNum, curTime, values, summaryState);
1122 const auto udq_step = reportStepNum - 1;
1124 this->schedule_[udq_step].udq()
1126 this->schedule_.wellMatcher(udq_step),
1127 this->schedule_[udq_step].group_order(),
1128 this->schedule_.segmentMatcherFactory(udq_step),
1129 [es = std::cref(this->eclState_)]() {
1130 return std::make_unique<RegionSetMatcher>
1131 (es.get().fipRegionStatistics());
1133 summaryState, udqState);
1137 if (collectOnIORank_.isParallel()) {
1139 ser.
append(summaryState);
1144template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1145template<std::
size_t N>
1150 static_assert(N == 3 || N == 4,
"unknown gathered LGR record shape");
1152 if (!gatheredLgrTrans_.has_value()) {
1156 if constexpr (N == 3) {
1157 return gatheredLgrTrans_->sameLevel.find(key);
1159 return gatheredLgrTrans_->crossLevel.find(key);
1163template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1164template<std::
size_t N>
1171 if (!gatheredLgrTrans_.has_value()) {
1172 return this->globalTrans().transmissibility(c1, c2);
1175 if (
const double* value = this->findGatheredTrans_(key); value !=
nullptr) {
1179 std::string msg =
"Gathered LGR transmissibilities: no value for connection key (";
1180 for (std::size_t j = 0; j < N; ++j) {
1182 msg += (j + 1 < N) ?
", " :
")";
1184 throw std::logic_error { msg };
1187template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1192 assert (globalTrans_);
1193 return *globalTrans_;
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:197
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
Definition: CollectDataOnIORank.hpp:50
bool isIORank() const
Definition: CollectDataOnIORank.hpp:131
Definition: EclGenericWriter.hpp:76
std::vector< std::vector< NNCdata > > outputNnc_
Definition: EclGenericWriter.hpp:213
const EclipseState & eclState_
Definition: EclGenericWriter.hpp:196
double gatheredOrGlobalTrans_(const std::array< int, N > &key, unsigned c1, unsigned c2) const
Definition: EclGenericWriter_impl.hpp:1167
void evalSummary(int reportStepNum, Scalar curTime, const data::Wells &localWellData, const data::WellBlockAveragePressures &localWBPData, const data::GroupAndNetworkValues &localGroupAndNetworkData, const std::map< int, data::AquiferData > &localAquiferData, const std::map< std::pair< std::string, int >, double > &blockData, const std::map< std::tuple< std::string, int, int >, double > &lgrBlockData, const std::map< std::string, double > &miscSummaryData, const std::map< std::string, std::vector< double > > ®ionData, const data::RegionVariableMapping ®VarMap, const RegionVariableCollection ®Vars, const Inplace &inplace, const Inplace *initialInPlace, const InterRegFlowMap &interRegFlows, SummaryState &summaryState, UDQState &udqState, const data::ReservoirCouplingGroupRates *rcGroupRates=nullptr)
Definition: EclGenericWriter_impl.hpp:1058
CollectDataOnIORankType collectOnIORank_
Definition: EclGenericWriter.hpp:192
void extractOutputTransAndNNC(const std::function< unsigned int(unsigned int)> &map)
Definition: EclGenericWriter_impl.hpp:313
std::unique_ptr< TaskletRunner > taskletRunner_
Definition: EclGenericWriter.hpp:198
void writeInit()
Definition: EclGenericWriter_impl.hpp:281
const double * findGatheredTrans_(const std::array< int, N > &key) const
Definition: EclGenericWriter_impl.hpp:1148
EclGenericWriter(const Schedule &schedule, const EclipseState &eclState, const SummaryConfig &summaryConfig, const Grid &grid, const EquilGrid *equilGrid, const GridView &gridView, const Dune::CartesianIndexMapper< Grid > &cartMapper, const Dune::CartesianIndexMapper< EquilGrid > *equilCartMapper, bool enableAsyncOutput, bool enableEsmry)
Definition: EclGenericWriter_impl.hpp:227
const EclipseIO & eclIO() const
Definition: EclGenericWriter_impl.hpp:273
const TransmissibilityType & globalTrans() const
Definition: EclGenericWriter_impl.hpp:1190
void doWriteOutput(const int reportStepNum, const std::optional< int > timeStepNum, const bool isSubStep, const bool forcedSimulationFinished, data::Solution &&localCellData, data::Wells &&localWellData, data::GroupAndNetworkValues &&localGroupAndNetworkData, data::Aquifers &&localAquiferData, WellTestState &&localWTestState, const Action::State &actionState, const UDQState &udqState, const SummaryState &summaryState, const std::vector< Scalar > &thresholdPressure, Scalar curTime, Scalar nextStepSize, bool doublePrecision, bool isFlowsn, std::array< FlowsData< double >, 3 > &&flowsn, bool isFloresn, std::array< FlowsData< double >, 3 > &&floresn)
Definition: EclGenericWriter_impl.hpp:948
Inter-region flow accumulation maps for all region definition arrays.
Definition: InterRegFlows.hpp:179
std::vector< data::InterRegFlowMap > getInterRegFlows() const
const std::vector< std::string > & names() const
Definition: EclGenericWriter.hpp:54
Class for serializing and broadcasting data using MPI.
Definition: MPISerializer.hpp:38
void append(T &data, int root=0)
Serialize and broadcast on root process, de-serialize and append on others.
Definition: MPISerializer.hpp:82
void broadcast(RootRank rootrank, Args &&... args)
Definition: MPISerializer.hpp:47
The base class for tasklets.
Definition: tasklets.hpp:45
Handles where a given tasklet is run.
Definition: tasklets.hpp:93
Definition: Transmissibility.hpp:55
Definition: blackoilbioeffectsmodules.hh:45
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
Avoid mistakes in calls to broadcast() by wrapping the root argument in an explicit type.
Definition: MPISerializer.hpp:33