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>
73#include <unordered_map>
93bool directVerticalNeighbors(
const std::array<int, 3>& cartDims,
94 const std::unordered_map<int,int>& cartesianToActive,
95 int smallGlobalIndex,
int largeGlobalIndex)
97 assert(smallGlobalIndex <= largeGlobalIndex);
98 std::array<int, 3> ijk1, ijk2;
99 auto globalToIjk = [cartDims](
int gc) {
100 std::array<int, 3> ijk;
101 ijk[0] = gc % cartDims[0];
103 ijk[1] = gc % cartDims[1];
104 ijk[2] = gc / cartDims[1];
107 ijk1 = globalToIjk(smallGlobalIndex);
108 ijk2 = globalToIjk(largeGlobalIndex);
109 assert(ijk2[2]>=ijk1[2]);
111 if ( ijk1[0] == ijk2[0] && ijk1[1] == ijk2[1] && (ijk2[2] - ijk1[2]) > 1)
113 assert((largeGlobalIndex-smallGlobalIndex)%(cartDims[0]*cartDims[1])==0);
114 for (
int gi = smallGlobalIndex + cartDims[0] * cartDims[1]; gi < largeGlobalIndex;
115 gi += cartDims[0] * cartDims[1] )
117 if ( cartesianToActive.find( gi ) != cartesianToActive.end() )
127std::unordered_map<std::string, Opm::data::InterRegFlowMap>
130 auto maps = std::unordered_map<std::string, Opm::data::InterRegFlowMap>{};
132 const auto& regionNames = map.
names();
134 const auto nmap = regionNames.size();
137 for (
auto mapID = 0*nmap; mapID < nmap; ++mapID) {
138 maps.emplace(regionNames[mapID], std::move(flows[mapID]));
146 Opm::Action::State actionState_;
147 Opm::WellTestState wtestState_;
148 Opm::SummaryState summaryState_;
149 Opm::UDQState udqState_;
150 Opm::EclipseIO& eclIO_;
152 std::optional<int> timeStepNum_;
154 double secondsElapsed_;
155 std::vector<Opm::RestartValue> restartValue_;
156 bool writeDoublePrecision_;
158 bool forcedSimulationFinished_;
160 explicit EclWriteTasklet(
const Opm::Action::State& actionState,
161 const Opm::WellTestState& wtestState,
162 const Opm::SummaryState& summaryState,
163 const Opm::UDQState& udqState,
164 Opm::EclipseIO& eclIO,
166 std::optional<int> timeStepNum,
168 double secondsElapsed,
169 std::vector<Opm::RestartValue> restartValue,
170 bool writeDoublePrecision,
171 bool forcedSimulationFinished)
172 : actionState_(actionState)
173 , wtestState_(wtestState)
174 , summaryState_(summaryState)
175 , udqState_(udqState)
177 , reportStepNum_(reportStepNum)
178 , timeStepNum_(timeStepNum)
179 , isSubStep_(isSubStep)
180 , secondsElapsed_(secondsElapsed)
181 , restartValue_(std::move(restartValue))
182 , writeDoublePrecision_(writeDoublePrecision)
183 , forcedSimulationFinished_(forcedSimulationFinished)
189 if (this->restartValue_.size() == 1) {
190 this->eclIO_.writeTimeStep(this->actionState_,
194 this->reportStepNum_,
196 this->secondsElapsed_,
197 std::move(this->restartValue_.back()),
198 this->writeDoublePrecision_,
200 forcedSimulationFinished_);
203 this->eclIO_.writeTimeStep(this->actionState_,
207 this->reportStepNum_,
209 this->secondsElapsed_,
210 std::move(this->restartValue_),
211 this->writeDoublePrecision_,
213 forcedSimulationFinished_);
222template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
225 const EclipseState& eclState,
226 const SummaryConfig& summaryConfig,
228 const EquilGrid* equilGrid,
229 const GridView& gridView,
232 bool enableAsyncOutput,
234 : collectOnIORank_(grid,
239 summaryConfig.fip_regions_interreg_flow())
241 , gridView_ (gridView)
242 , schedule_ (schedule)
243 , eclState_ (eclState)
244 , cartMapper_ (cartMapper)
245 , equilCartMapper_(equilCartMapper)
246 , equilGrid_ (equilGrid)
252 this->eclIO_ = std::make_unique<EclipseIO>
254 UgGridHelpers::createEclipseGrid(*equilGrid,
eclState_.getInputGrid()),
255 this->schedule_, summaryConfig,
"", enableEsmry);
260 int numWorkerThreads = 0;
262 numWorkerThreads = 1;
268template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
276template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
280 if (collectOnIORank_.isIORank()) {
281 std::map<std::string, std::vector<int>> integerVectors;
282 if (collectOnIORank_.isParallel()) {
283 integerVectors.emplace(
"MPI_RANK", collectOnIORank_.globalRanks());
286 if (
const auto& lgrs = this->eclState_.getLgrs(); lgrs.size() > 0) {
288 const auto nncCollection = Opm::NNCCollection::fromLGROutputContainers(this->outputNnc_,
289 this->outputNncGlobalLocal_,
290 this->outputAmalgamatedNnc_);
291 eclIO_->writeInitial(*this->outputTrans_,
295 eclIO_->writeInitial(*this->outputTrans_,
297 this->outputNnc_.front());
299 this->outputTrans_.reset();
303template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
308 if (collectOnIORank_.isIORank()) {
309 constexpr bool equilGridIsCpGrid = std::is_same_v<EquilGrid, Dune::CpGrid>;
311 const auto levelCartMapp = this->createLevelCartMapp_<equilGridIsCpGrid>();
312 const auto levelCartToLevelCompressed = this->createCartesianToActiveMaps_<equilGridIsCpGrid>(levelCartMapp);
313 auto computeLevelIndices = this->computeLevelIndices_<equilGridIsCpGrid>();
314 auto computeLevelCartIdx = this->computeLevelCartIdx_<equilGridIsCpGrid>(levelCartMapp, *(this->equilCartMapper_));
315 auto computeLevelCartDimensions = this->computeLevelCartDimensions_<equilGridIsCpGrid>(levelCartMapp, *(this->equilCartMapper_));
316 auto computeOriginIndices = this->computeOriginIndices_<equilGridIsCpGrid>();
318 computeTrans_(levelCartToLevelCompressed, map, computeLevelIndices,
319 computeLevelCartIdx, computeLevelCartDimensions, computeOriginIndices);
320 exportNncStructure_(levelCartToLevelCompressed, map, computeLevelIndices, computeLevelCartIdx,
321 computeLevelCartDimensions, computeOriginIndices);
325 if (collectOnIORank_.isParallel()) {
326 const auto& comm = grid_.comm();
333template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
338 const auto& numAquCell = this->eclState_.aquifer().hasNumericalAquifer()
339 ? this->eclState_.aquifer().numericalAquifers().allAquiferCellIds()
340 : std::vector<std::size_t>{};
342 return std::ranges::binary_search(numAquCell.begin(), numAquCell.end(), cartIdx);
345template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
347EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
348isNumAquConn_(
const std::size_t cartIdx1,
349 const std::size_t cartIdx2)
const
351 return isNumAquCell_(cartIdx1) || isNumAquCell_(cartIdx2);
354template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
355template<
bool equilGr
idIsCpGr
id>
357EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
358createLevelCartMapp_()
const
360 if constexpr (equilGridIsCpGrid) {
366template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
367template<
bool equilGr
idIsCpGr
id>
368std::vector<std::unordered_map<int,int>>
369EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
372 if constexpr (equilGridIsCpGrid) {
373 if (this->equilGrid_->maxLevel()) {
374 return Opm::Lgr::levelCartesianToLevelCompressedMaps(*this->equilGrid_, levelCartMapp); }
376 return std::vector<std::unordered_map<int,int>>{ cartesianToCompressed(equilGrid_->size(0), UgGridHelpers::globalCell(*equilGrid_)) };
379 return std::vector<std::unordered_map<int,int>>{ cartesianToCompressed(equilGrid_->size(0), UgGridHelpers::globalCell(*equilGrid_)) };
382template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
383template<
bool equilGr
idIsCpGr
id>
384std::function<std::array<int,3>(
int)>
385EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
389 if constexpr (equilGridIsCpGrid) {
390 return [&](
int level)
392 return levelCartMapp.cartesianDimensions(level);
396 return [&]([[maybe_unused]]
int level)
399 return equilCartMapp.cartesianDimensions();
404template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
405template<
bool equilGr
idIsCpGr
id>
406std::function<int(
int,
int)>
407EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
411 if constexpr (equilGridIsCpGrid) {
412 return [&](
int levelCompressedIdx,
415 return levelCartMapp.cartesianIndex(levelCompressedIdx, level);
419 return [&](
int levelCompressedIdx,
420 [[maybe_unused]]
int level)
423 return equilCartMapp.cartesianIndex(levelCompressedIdx);
428template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
429template <
bool equilGr
idIsCpGr
id>
431EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
432computeLevelIndices_()
const
434 if constexpr (equilGridIsCpGrid) {
435 return [](
const auto& intersection,
439 return std::pair{intersection.inside().getLevelElem().index(), intersection.outside().getLevelElem().index()};
443 return [](
const auto&,
444 const auto& intersectionInsideLeafIdx,
445 const auto& intersectionOutsideLeafIdx)
447 return std::pair{intersectionInsideLeafIdx, intersectionOutsideLeafIdx};
452template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
453template <
bool equilGr
idIsCpGr
id>
455EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
456computeOriginIndices_()
const
458 if constexpr (equilGridIsCpGrid) {
459 return [](
const auto& intersection,
463 return std::pair{intersection.inside().getOrigin().index(), intersection.outside().getOrigin().index()};
467 return [](
const auto&,
468 const auto& intersectionInsideLeafIdx,
469 const auto& intersectionOutsideLeafIdx)
471 return std::pair{intersectionInsideLeafIdx, intersectionOutsideLeafIdx};
476template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
478EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
479allocateLevelTrans_(
const std::array<int,3>& levelCartDims,
480 data::Solution& levelTrans)
const
482 auto createLevelCellData = [&levelCartDims]() {
483 return Opm::data::CellData{
484 Opm::UnitSystem::measure::transmissibility,
485 std::vector<double>(levelCartDims[0] * levelCartDims[1] * levelCartDims[2], 0.0),
486 Opm::data::TargetType::INIT
491 levelTrans.emplace(
"TRANX", createLevelCellData());
492 levelTrans.emplace(
"TRANY", createLevelCellData());
493 levelTrans.emplace(
"TRANZ", createLevelCellData());
496template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
497template<
typename LevelIndicesFunction,
typename OriginIndicesFunction>
499EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
500computeTrans_(
const std::vector<std::unordered_map<int,int>>& levelCartToLevelCompressed,
501 const std::function<
unsigned int(
unsigned int)>& map,
502 const LevelIndicesFunction& computeLevelIndices,
503 const std::function<
int(
int,
int)>& computeLevelCartIdx,
504 const std::function<std::array<int,3>(
int)>& computeLevelCartDims,
505 const OriginIndicesFunction& computeOriginIndices)
const
508 outputTrans_ = std::make_unique<std::vector<data::Solution>>(std::vector<data::Solution>{});
511 using GlobalGridView =
typename EquilGrid::LeafGridView;
512 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
513 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
514 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
517 int maxLevel = this->equilGrid_->maxLevel();
519 outputTrans_->resize(maxLevel+1);
521 for (
int level = 0; level <= maxLevel; ++level) {
522 allocateLevelTrans_(computeLevelCartDims(level), this->outputTrans_->at(level));
525 for (
const auto& elem : elements(globalGridView)) {
526 for (
const auto& is : intersections(globalGridView, elem)) {
530 if ( is.inside().level() != is.outside().level() )
534 unsigned c1 = globalElemMapper.index(is.inside());
535 unsigned c2 = globalElemMapper.index(is.outside());
540 int level = is.inside().level();
543 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
545 const int levelCartIdxIn = computeLevelCartIdx(levelInIdx, level);
546 const int levelCartIdxOut = computeLevelCartIdx(levelOutIdx, level);
552 const auto [originInIdx, originOutIdx] = computeOriginIndices(is, c1, c2);
554 const auto originCartIdxIn = computeLevelCartIdx(originInIdx, 0);
555 const auto originCartIdxOut = computeLevelCartIdx(originOutIdx, 0);
558 if (isNumAquCell_(originCartIdxIn) || isNumAquCell_(originCartIdxOut)) {
568 const auto minLevelCartIdx = std::min(levelCartIdxIn, levelCartIdxOut);
569 const auto maxLevelCartIdx = std::max(levelCartIdxIn, levelCartIdxOut);
571 const auto& levelCartDims = computeLevelCartDims(level);
579 if (maxLevelCartIdx - minLevelCartIdx == 1 && levelCartDims[0] > 1 ) {
580 outputTrans_->at(level).at(
"TRANX").template data<double>()[minLevelCartIdx] = globalTrans().transmissibility(c1, c2);
584 if (maxLevelCartIdx - minLevelCartIdx == levelCartDims[0] && levelCartDims[1] > 1) {
585 outputTrans_->at(level).at(
"TRANY").template data<double>()[minLevelCartIdx] = globalTrans().transmissibility(c1, c2);
589 if ( maxLevelCartIdx - minLevelCartIdx == levelCartDims[0]*levelCartDims[1] ||
590 directVerticalNeighbors(levelCartDims,
591 levelCartToLevelCompressed[level],
594 outputTrans_->at(level).at(
"TRANZ").template data<double>()[minLevelCartIdx] = globalTrans().transmissibility(c1, c2);
600template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
602EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
603isCartesianNeighbour_(
const std::array<int,3>& levelCartDims,
604 const std::size_t levelCartIdx1,
605 const std::size_t levelCartIdx2)
const
607 const int diff = levelCartIdx2 - levelCartIdx1;
610 || (diff == levelCartDims[0])
611 || (diff == (levelCartDims[0] * levelCartDims[1]));
614template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
616EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
617isDirectNeighbours_(
const std::unordered_map<int,int>& levelCartesianToActive,
618 const std::array<int,3>& levelCartDims,
619 const std::size_t levelCartIdx1,
620 const std::size_t levelCartIdx2)
const
622 return isCartesianNeighbour_(levelCartDims, levelCartIdx1, levelCartIdx2)
623 || directVerticalNeighbors(levelCartDims, levelCartesianToActive, levelCartIdx1, levelCartIdx2);
626template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
628EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
629activeCell_(
const std::unordered_map<int,int>& levelCartToLevelCompressed,
630 const std::size_t levelCartIdx)
const
632 auto pos = levelCartToLevelCompressed.find(levelCartIdx);
633 return (pos == levelCartToLevelCompressed.end()) ? -1 : pos->second;
636template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
638EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
639allocateAllNncs_(
int maxLevel)
const
641 this->outputNnc_.resize(maxLevel+1);
649 this->outputNncGlobalLocal_.resize(maxLevel);
657 this->outputAmalgamatedNnc_.resize(maxLevel-1);
658 for (
int i = 0; i < maxLevel-1; ++i) {
659 this->outputAmalgamatedNnc_[i].resize(maxLevel-1-i);
664template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
665template<
typename LevelIndicesFunction,
typename OriginIndicesFunction>
666std::vector<std::vector<NNCdata>>
667EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
668exportNncStructure_(
const std::vector<std::unordered_map<int,int>>& levelCartToLevelCompressed,
669 const std::function<
unsigned int(
unsigned int)>& map,
670 const LevelIndicesFunction& computeLevelIndices,
671 const std::function<
int(
int,
int)>& computeLevelCartIdx,
672 const std::function<std::array<int,3>(
int)>& computeLevelCartDims,
673 const OriginIndicesFunction& computeOriginIndices)
const
675 const auto& nncData = this->eclState_.getInputNNC().input();
676 const auto& nncEdit = this->eclState_.getInputNNC().edit();
677 const auto& nncEditr = this->eclState_.getInputNNC().editr();
678 const auto& unitSystem = this->eclState_.getDeckUnitSystem();
679 const auto& transMult = this->eclState_.getTransMult();
682 const auto& equilCartMapper = *equilCartMapper_;
684 const auto& level0CartDims = equilCartMapper.cartesianDimensions();
686 int maxLevel = this->equilGrid_->maxLevel();
687 allocateAllNncs_(maxLevel);
689 using GlobalGridView =
typename EquilGrid::LeafGridView;
690 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
691 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
692 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
694 for (
const auto& elem : elements(globalGridView)) {
695 for (
const auto& is : intersections(globalGridView, elem)) {
700 unsigned c1 = globalElemMapper.index(is.inside());
701 unsigned c2 = globalElemMapper.index(is.outside());
706 if ( is.inside().level() != is.outside().level() ) {
708 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
710 const int levelIn = is.inside().level();
711 const int levelOut = is.outside().level();
713 auto levelCartIdxIn = computeLevelCartIdx(levelInIdx, levelIn);
714 auto levelCartIdxOut = computeLevelCartIdx(levelOutIdx, levelOut);
717 std::pair<int,int> smallerPair = {levelIn, levelCartIdxIn},
718 largerPair = {levelOut, levelCartIdxOut};
719 if (smallerPair.first > largerPair.first) {
720 std::swap(smallerPair, largerPair);
723 const auto& [smallerLevel, smallerLevelCartIdx] = smallerPair;
724 const auto& [largerLevel, largerLevelCartIdx] = largerPair;
726 auto t = this->globalTrans().transmissibility(c1, c2);
733 const auto tt = unitSystem
734 .from_si(UnitSystem::measure::transmissibility, t);
736 if (std::isnormal(tt) && (tt > 1.0e-12)) {
738 if (smallerLevel == 0) {
739 this->outputNncGlobalLocal_[largerLevel-1].emplace_back(smallerLevelCartIdx, largerLevelCartIdx, t);
742 assert(smallerLevel >= 1);
743 this->outputAmalgamatedNnc_[smallerLevel-1][largerLevel-smallerLevel-1].emplace_back(smallerLevelCartIdx, largerLevelCartIdx, t);
749 assert(is.inside().level() == is.outside().level());
750 const int level = is.inside().level();
756 const auto [originInIdx, originOutIdx] = computeOriginIndices(is, c1, c2);
758 const std::size_t originCartIdxIn = computeLevelCartIdx(originInIdx, 0);
759 const std::size_t originCartIdxOut = computeLevelCartIdx(originOutIdx, 0);
762 const auto& [levelInIdx, levelOutIdx] = computeLevelIndices(is, c1, c2);
764 auto levelCartIdxIn = computeLevelCartIdx(levelInIdx, level);
765 auto levelCartIdxOut = computeLevelCartIdx(levelOutIdx, level);
767 if ( levelCartIdxOut < levelCartIdxIn )
768 std::swap(levelCartIdxIn, levelCartIdxOut);
776 const auto& levelCartDims = computeLevelCartDims(level);
779 assert(!isNumAquConn_(originCartIdxIn, originCartIdxOut) || level == 0);
781 if (isNumAquConn_(originCartIdxIn, originCartIdxOut) ||
782 ! isDirectNeighbours_(levelCartToLevelCompressed[level],
784 levelCartIdxIn, levelCartIdxOut)) {
787 auto t = this->globalTrans().transmissibility(c1, c2);
790 auto candidate = std::lower_bound(nncData.begin(), nncData.end(),
791 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
792 const auto transMlt = transMult.getRegionMultiplierNNC(originCartIdxIn, originCartIdxOut);
793 bool foundNncEditr =
false;
795 while ((candidate != nncData.end()) &&
796 (candidate->cell1 == originCartIdxIn) &&
797 (candidate->cell2 == originCartIdxOut))
799 auto trans = candidate->trans;
801 if (! nncEditr.empty()) {
802 auto it = std::lower_bound(nncEditr.begin(), nncEditr.end(),
803 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
804 foundNncEditr = it != nncEditr.end() && it->cell1 == originCartIdxIn && it->cell2 == originCartIdxOut;
810 if (! nncEdit.empty()) {
811 auto it = std::lower_bound(nncEdit.begin(), nncEdit.end(),
812 NNCdata { originCartIdxIn, originCartIdxOut, 0.0 });
813 if (it != nncEdit.end() && it->cell1 == originCartIdxIn && it->cell2 == originCartIdxOut) {
831 const auto tt = unitSystem
832 .from_si(UnitSystem::measure::transmissibility, t);
834 if (std::isnormal(tt) && (tt > 1.0e-12)) {
835 this->outputNnc_[level].emplace_back(levelCartIdxIn, levelCartIdxOut, t);
843 std::vector<NNCdata> inputedNnc{};
844 const auto generatedNnc = outputNnc_[0];
848 for (
const auto& entry : nncData) {
857 assert (entry.cell2 >= entry.cell1);
859 if (! isCartesianNeighbour_(level0CartDims, entry.cell1, entry.cell2) ||
860 isNumAquConn_(entry.cell1, entry.cell2))
862 bool foundNncEdit =
false;
863 auto trans = entry.trans;
864 if (! nncEdit.empty()) {
865 auto it = std::lower_bound(nncEdit.begin(), nncEdit.end(),
866 NNCdata {entry.cell1, entry.cell2, 0.0 });
867 if (it != nncEdit.end() && it->cell1 == entry.cell1 && it->cell2 == entry.cell2) {
872 if (! foundNncEdit) {
876 const auto c1 = activeCell_(levelCartToLevelCompressed[0], entry.cell1);
877 const auto c2 = activeCell_(levelCartToLevelCompressed[0], entry.cell2);
879 if ((c1 < 0) || (c2 < 0)) {
885 trans = this->globalTrans().transmissibility(c1, c2);
887 if (! generatedNnc.empty()) {
888 for (
const auto& generated : generatedNnc) {
889 if (entry.cell1 == generated.cell1 && entry.cell2 == generated.cell2) {
890 trans -= generated.trans;
896 const auto tt = unitSystem
897 .from_si(UnitSystem::measure::transmissibility, trans);
902 if (std::isnormal(tt) && ! (tt < 1.0e-6)) {
903 inputedNnc.emplace_back(entry.cell1, entry.cell2, trans);
908 this->outputNnc_[0].insert(this->outputNnc_[0].begin(), inputedNnc.begin(), inputedNnc.end());
909 return this->outputNnc_;
912template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
915 const std::optional<int> timeStepNum,
916 const bool isSubStep,
917 const bool isForcedFinalOutput,
918 data::Solution&& localCellData,
919 data::Wells&& localWellData,
920 data::GroupAndNetworkValues&& localGroupAndNetworkData,
921 data::Aquifers&& localAquiferData,
922 WellTestState&& localWTestState,
923 const Action::State& actionState,
924 const UDQState& udqState,
925 const SummaryState& summaryState,
926 const std::vector<Scalar>& thresholdPressure,
929 bool doublePrecision,
935 const auto isParallel = this->collectOnIORank_.isParallel();
936 const bool needsReordering = this->collectOnIORank_.doesNeedReordering();
938 RestartValue restartValue {
939 (isParallel || needsReordering)
940 ? this->collectOnIORank_.globalCellData()
941 : std::move(localCellData),
943 isParallel ? this->collectOnIORank_.globalWellData()
944 : std::move(localWellData),
946 isParallel ? this->collectOnIORank_.globalGroupAndNetworkData()
947 : std::move(localGroupAndNetworkData),
949 isParallel ? this->collectOnIORank_.globalAquiferData()
950 : std::move(localAquiferData)
953 if (eclState_.getSimulationConfig().useThresholdPressure()) {
954 restartValue.addExtra(
"THRESHPR", UnitSystem::measure::pressure,
960 restartValue.addExtra(
"OPMEXTRA", std::vector<double>(1, nextStepSize));
965 const auto flowsn_global = isParallel ? this->collectOnIORank_.globalFlowsn() : std::move(flowsn);
966 for (
const auto& flows : flowsn_global) {
967 if (flows.name.empty())
969 if (flows.name ==
"FLOGASN+") {
970 restartValue.addExtra(flows.name, UnitSystem::measure::gas_surface_rate, flows.values);
972 restartValue.addExtra(flows.name, UnitSystem::measure::liquid_surface_rate, flows.values);
977 const auto floresn_global = isParallel ? this->collectOnIORank_.globalFloresn() : std::move(floresn);
978 for (
const auto& flores : floresn_global) {
979 if (flores.name.empty()) {
982 restartValue.addExtra(flores.name, UnitSystem::measure::rate, flores.values);
986 std::vector<Opm::RestartValue> restartValues{};
988 if ( !isParallel && !needsReordering && (this->eclState_.getLgrs().size()>0) && (this->grid_.maxLevel()>0) ) {
993 Opm::Lgr::extractRestartValueLevelGrids<Grid>(this->grid_, restartValue, restartValues);
996 restartValues.reserve(1);
997 restartValues.push_back(std::move(restartValue));
1003 this->taskletRunner_->barrier();
1006 if (this->taskletRunner_->failure()) {
1007 throw std::runtime_error(
"Failure in the TaskletRunner while writing output.");
1011 auto eclWriteTasklet = std::make_shared<EclWriteTasklet>(
1013 isParallel ? this->collectOnIORank_.globalWellTestState() : std::move(localWTestState),
1014 summaryState, udqState, *this->eclIO_,
1015 reportStepNum, timeStepNum, isSubStep, curTime, std::move(restartValues), doublePrecision,
1016 isForcedFinalOutput);
1019 this->taskletRunner_->dispatch(std::move(eclWriteTasklet));
1022template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1025 const Scalar curTime,
1026 const data::Wells& localWellData,
1027 const data::WellBlockAveragePressures& localWBPData,
1028 const data::GroupAndNetworkValues& localGroupAndNetworkData,
1029 const std::map<int,data::AquiferData>& localAquiferData,
1030 const std::map<std::pair<std::string, int>,
double>& blockData,
1031 const std::map<std::tuple<std::string, int, int>,
double>& lgrBlockData,
1032 const std::map<std::string, double>& miscSummaryData,
1033 const std::map<std::string, std::vector<double>>& regionData,
1034 const data::RegionVariableMapping& regVarMap,
1035 const RegionVariableCollection& regVars,
1036 const Inplace& inplace,
1037 const Inplace* initialInPlace,
1039 SummaryState& summaryState,
1041 const data::ReservoirCouplingGroupRates* rcGroupRates)
1043 if (collectOnIORank_.isIORank()) {
1044 const auto& wellData = this->collectOnIORank_.isParallel()
1045 ? this->collectOnIORank_.globalWellData()
1048 const auto& wbpData = this->collectOnIORank_.isParallel()
1049 ? this->collectOnIORank_.globalWBPData()
1052 const auto& groupAndNetworkData = this->collectOnIORank_.isParallel()
1053 ? this->collectOnIORank_.globalGroupAndNetworkData()
1054 : localGroupAndNetworkData;
1056 const auto& aquiferData = this->collectOnIORank_.isParallel()
1057 ? this->collectOnIORank_.globalAquiferData()
1060 const auto interreg_flows = getInterRegFlowsAsMap(interRegFlows);
1062 const auto values = out::Summary::DynamicSimulatorState {
1063 .well_solution = &wellData,
1065 .group_and_nwrk_solution = &groupAndNetworkData,
1066 .single_values = &miscSummaryData,
1067 .region_values = ®ionData,
1068 .reg_var_map = ®VarMap,
1069 .reg_var_coll = ®Vars,
1070 .block_values = &blockData,
1071 .aquifer_values = &aquiferData,
1072 .interreg_flows = &interreg_flows,
1073 .rc_group_rates = rcGroupRates,
1075 .current = &inplace,
1076 .initial = initialInPlace
1078 .lgr_block_values = &lgrBlockData
1081 this->eclIO_->summary()
1082 .eval(reportStepNum, curTime, values, summaryState);
1088 const auto udq_step = reportStepNum - 1;
1090 this->schedule_[udq_step].udq()
1092 this->schedule_.wellMatcher(udq_step),
1093 this->schedule_[udq_step].group_order(),
1094 this->schedule_.segmentMatcherFactory(udq_step),
1095 [es = std::cref(this->eclState_)]() {
1096 return std::make_unique<RegionSetMatcher>
1097 (es.get().fipRegionStatistics());
1099 summaryState, udqState);
1103 if (collectOnIORank_.isParallel()) {
1105 ser.
append(summaryState);
1110template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
1115 assert (globalTrans_);
1116 return *globalTrans_;
Definition: CollectDataOnIORank.hpp:50
bool isIORank() const
Definition: CollectDataOnIORank.hpp:131
Definition: EclGenericWriter.hpp:74
std::vector< std::vector< NNCdata > > outputNnc_
Definition: EclGenericWriter.hpp:186
const EclipseState & eclState_
Definition: EclGenericWriter.hpp:170
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:1024
CollectDataOnIORankType collectOnIORank_
Definition: EclGenericWriter.hpp:166
void extractOutputTransAndNNC(const std::function< unsigned int(unsigned int)> &map)
Definition: EclGenericWriter_impl.hpp:306
std::unique_ptr< TaskletRunner > taskletRunner_
Definition: EclGenericWriter.hpp:172
void writeInit()
Definition: EclGenericWriter_impl.hpp:278
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:224
const EclipseIO & eclIO() const
Definition: EclGenericWriter_impl.hpp:270
const TransmissibilityType & globalTrans() const
Definition: EclGenericWriter_impl.hpp:1113
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:914
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:52
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:54
Definition: blackoilbioeffectsmodules.hh:45
Avoid mistakes in calls to broadcast() by wrapping the root argument in an explicit type.
Definition: MPISerializer.hpp:33