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/GridHelpers.hpp>
29#include <opm/grid/utility/cartesianToCompressed.hpp>
31#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
32#include <opm/input/eclipse/EclipseState/Grid/RegionSetMatcher.hpp>
33#include <opm/input/eclipse/EclipseState/SummaryConfig/SummaryConfig.hpp>
35#include <opm/input/eclipse/Schedule/Action/State.hpp>
36#include <opm/input/eclipse/Schedule/Schedule.hpp>
37#include <opm/input/eclipse/Schedule/SummaryState.hpp>
38#include <opm/input/eclipse/Schedule/UDQ/UDQConfig.hpp>
39#include <opm/input/eclipse/Schedule/UDQ/UDQState.hpp>
40#include <opm/input/eclipse/Schedule/Well/WellMatcher.hpp>
42#include <opm/input/eclipse/Units/UnitSystem.hpp>
44#include <opm/output/eclipse/EclipseIO.hpp>
45#include <opm/output/eclipse/RestartValue.hpp>
46#include <opm/output/eclipse/Summary.hpp>
66#include <unordered_map>
86bool directVerticalNeighbors(
const std::array<int, 3>& cartDims,
87 const std::unordered_map<int,int>& cartesianToActive,
88 int smallGlobalIndex,
int largeGlobalIndex)
90 assert(smallGlobalIndex <= largeGlobalIndex);
91 std::array<int, 3> ijk1, ijk2;
92 auto globalToIjk = [cartDims](
int gc) {
93 std::array<int, 3> ijk;
94 ijk[0] = gc % cartDims[0];
96 ijk[1] = gc % cartDims[1];
97 ijk[2] = gc / cartDims[1];
100 ijk1 = globalToIjk(smallGlobalIndex);
101 ijk2 = globalToIjk(largeGlobalIndex);
102 assert(ijk2[2]>=ijk1[2]);
104 if ( ijk1[0] == ijk2[0] && ijk1[1] == ijk2[1] && (ijk2[2] - ijk1[2]) > 1)
106 assert((largeGlobalIndex-smallGlobalIndex)%(cartDims[0]*cartDims[1])==0);
107 for (
int gi = smallGlobalIndex + cartDims[0] * cartDims[1]; gi < largeGlobalIndex;
108 gi += cartDims[0] * cartDims[1] )
110 if ( cartesianToActive.find( gi ) != cartesianToActive.end() )
120std::unordered_map<std::string, Opm::data::InterRegFlowMap>
123 auto maps = std::unordered_map<std::string, Opm::data::InterRegFlowMap>{};
125 const auto& regionNames = map.
names();
127 const auto nmap = regionNames.size();
130 for (
auto mapID = 0*nmap; mapID < nmap; ++mapID) {
131 maps.emplace(regionNames[mapID], std::move(flows[mapID]));
139 Opm::Action::State actionState_;
140 Opm::WellTestState wtestState_;
141 Opm::SummaryState summaryState_;
142 Opm::UDQState udqState_;
143 Opm::EclipseIO& eclIO_;
145 std::optional<int> timeStepNum_;
147 double secondsElapsed_;
148 Opm::RestartValue restartValue_;
149 bool writeDoublePrecision_;
151 explicit EclWriteTasklet(
const Opm::Action::State& actionState,
152 const Opm::WellTestState& wtestState,
153 const Opm::SummaryState& summaryState,
154 const Opm::UDQState& udqState,
155 Opm::EclipseIO& eclIO,
157 std::optional<int> timeStepNum,
159 double secondsElapsed,
160 Opm::RestartValue restartValue,
161 bool writeDoublePrecision)
162 : actionState_(actionState)
163 , wtestState_(wtestState)
164 , summaryState_(summaryState)
165 , udqState_(udqState)
167 , reportStepNum_(reportStepNum)
168 , timeStepNum_(timeStepNum)
169 , isSubStep_(isSubStep)
170 , secondsElapsed_(secondsElapsed)
171 , restartValue_(std::move(restartValue))
172 , writeDoublePrecision_(writeDoublePrecision)
178 this->eclIO_.writeTimeStep(this->actionState_,
182 this->reportStepNum_,
184 this->secondsElapsed_,
185 std::move(this->restartValue_),
186 this->writeDoublePrecision_,
196template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
199 const EclipseState& eclState,
200 const SummaryConfig& summaryConfig,
202 const EquilGrid* equilGrid,
203 const GridView& gridView,
206 bool enableAsyncOutput,
208 : collectOnIORank_(grid,
213 summaryConfig.fip_regions_interreg_flow())
215 , gridView_ (gridView)
216 , schedule_ (schedule)
217 , eclState_ (eclState)
218 , cartMapper_ (cartMapper)
219 , equilCartMapper_(equilCartMapper)
220 , equilGrid_ (equilGrid)
223 this->
eclIO_ = std::make_unique<EclipseIO>
225 UgGridHelpers::createEclipseGrid(*equilGrid,
eclState_.getInputGrid()),
226 this->schedule_, summaryConfig,
"", enableEsmry);
231 int numWorkerThreads = 0;
233 numWorkerThreads = 1;
239template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
247template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
251 if (collectOnIORank_.isIORank()) {
252 std::map<std::string, std::vector<int>> integerVectors;
253 if (collectOnIORank_.isParallel()) {
254 integerVectors.emplace(
"MPI_RANK", collectOnIORank_.globalRanks());
257 eclIO_->writeInitial(*this->outputTrans_,
260 this->outputTrans_.reset();
263template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
268 if (collectOnIORank_.isIORank()) {
269 auto cartMap = cartesianToCompressed(equilGrid_->size(0), UgGridHelpers::globalCell(*equilGrid_));
270 computeTrans_(cartMap, map);
271 exportNncStructure_(cartMap, map);
275 if (collectOnIORank_.isParallel()) {
276 const auto& comm = grid_.comm();
283template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
286computeTrans_(
const std::unordered_map<int,int>& cartesianToActive,
287 const std::function<
unsigned int(
unsigned int)>& map)
const
290 outputTrans_ = std::make_unique<data::Solution>();
293 const auto& cartMapper = *equilCartMapper_;
294 const auto& cartDims = cartMapper.cartesianDimensions();
296 auto createCellData = [&cartDims]() {
297 return data::CellData{
298 UnitSystem::measure::transmissibility,
299 std::vector<double>(cartDims[0] * cartDims[1] * cartDims[2], 0.0),
300 data::TargetType::INIT
304 outputTrans_->clear();
305 outputTrans_->emplace(
"TRANX", createCellData());
306 outputTrans_->emplace(
"TRANY", createCellData());
307 outputTrans_->emplace(
"TRANZ", createCellData());
309 auto& tranx = this->outputTrans_->at(
"TRANX");
310 auto& trany = this->outputTrans_->at(
"TRANY");
311 auto& tranz = this->outputTrans_->at(
"TRANZ");
313 using GlobalGridView =
typename EquilGrid::LeafGridView;
314 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
315 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
316 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
318 auto isNumAquCell = [numAquCell = this->eclState_.aquifer().hasNumericalAquifer()
319 ? this->eclState_.aquifer().numericalAquifers().allAquiferCellIds()
320 : std::vector<std::size_t>{}]
321 (
const std::size_t cellIdx)
323 return std::binary_search(numAquCell.begin(), numAquCell.end(), cellIdx);
326 for (
const auto& elem : elements(globalGridView)) {
327 for (
const auto& is : intersections(globalGridView, elem)) {
332 unsigned c1 = globalElemMapper.index(is.inside());
333 unsigned c2 = globalElemMapper.index(is.outside());
339 const int cartIdx1 = cartMapper.cartesianIndex( c1 );
340 const int cartIdx2 = cartMapper.cartesianIndex( c2 );
342 if (isNumAquCell(cartIdx1) || isNumAquCell(cartIdx2)) {
351 assert(cartIdx1 <= cartIdx2);
352 int gc1 = std::min(cartIdx1, cartIdx2);
353 int gc2 = std::max(cartIdx1, cartIdx2);
361 if (gc2 - gc1 == 1 && cartDims[0] > 1 ) {
362 tranx.template data<double>()[gc1] = globalTrans().transmissibility(c1, c2);
366 if (gc2 - gc1 == cartDims[0] && cartDims[1] > 1) {
367 trany.template data<double>()[gc1] = globalTrans().transmissibility(c1, c2);
371 if ( gc2 - gc1 == cartDims[0]*cartDims[1] ||
372 directVerticalNeighbors(cartDims, cartesianToActive, gc1, gc2))
373 tranz.template data<double>()[gc1] = globalTrans().transmissibility(c1, c2);
378template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
380EclGenericWriter<Grid,EquilGrid,GridView,ElementMapper,Scalar>::
381exportNncStructure_(
const std::unordered_map<int,int>& cartesianToActive,
382 const std::function<
unsigned int(
unsigned int)>& map)
const
384 auto isNumAquCell = [numAquCell = this->eclState_.aquifer().hasNumericalAquifer()
385 ? this->eclState_.aquifer().numericalAquifers().allAquiferCellIds()
386 : std::vector<std::size_t>{}]
387 (
const std::size_t cellIdx)
389 return std::binary_search(numAquCell.begin(), numAquCell.end(), cellIdx);
392 auto isNumAquConn = [&isNumAquCell](
const std::size_t cellIdx1,
393 const std::size_t cellIdx2)
395 return isNumAquCell(cellIdx1) || isNumAquCell(cellIdx2);
398 auto isCartesianNeighbour = [nx = this->eclState_.getInputGrid().getNX(),
399 ny = this->eclState_.getInputGrid().getNY()]
400 (
const std::size_t cellIdx1,
const std::size_t cellIdx2)
402 const auto cellDiff = cellIdx2 - cellIdx1;
404 return (cellDiff == 1)
406 || (cellDiff == nx * ny);
409 auto activeCell = [&cartesianToActive](
const std::size_t cellIdx)
411 auto pos = cartesianToActive.find(cellIdx);
412 return (pos == cartesianToActive.end()) ? -1 : pos->second;
415 const auto& nncData = this->eclState_.getInputNNC().input();
416 const auto& unitSystem = this->eclState_.getDeckUnitSystem();
418 for (
const auto& entry : nncData) {
427 assert (entry.cell2 >= entry.cell1);
429 if (! isCartesianNeighbour(entry.cell1, entry.cell2) ||
430 isNumAquConn(entry.cell1, entry.cell2))
435 const auto c1 = activeCell(entry.cell1);
436 const auto c2 = activeCell(entry.cell2);
438 if ((c1 < 0) || (c2 < 0)) {
444 const auto trans = this->globalTrans().transmissibility(c1, c2);
445 const auto tt = unitSystem
446 .from_si(UnitSystem::measure::transmissibility, trans);
451 if (std::isnormal(tt) && ! (tt < 1.0e-6)) {
452 this->outputNnc_.emplace_back(entry.cell1, entry.cell2, trans);
457 auto isDirectNeighbours = [&isCartesianNeighbour, &cartesianToActive,
458 cartDims = &this->cartMapper_.cartesianDimensions()]
459 (
const std::size_t cellIdx1,
const std::size_t cellIdx2)
461 return isCartesianNeighbour(cellIdx1, cellIdx2)
462 || directVerticalNeighbors(*cartDims, cartesianToActive, cellIdx1, cellIdx2);
465 using GlobalGridView =
typename EquilGrid::LeafGridView;
466 using GlobElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GlobalGridView>;
467 const GlobalGridView& globalGridView = this->equilGrid_->leafGridView();
468 const GlobElementMapper globalElemMapper { globalGridView, Dune::mcmgElementLayout() };
471 const auto& equilCartMapper = *equilCartMapper_;
472 for (
const auto& elem : elements(globalGridView)) {
473 for (
const auto& is : intersections(globalGridView, elem)) {
478 unsigned c1 = globalElemMapper.index(is.inside());
479 unsigned c2 = globalElemMapper.index(is.outside());
484 std::size_t cc1 = equilCartMapper.cartesianIndex( c1 );
485 std::size_t cc2 = equilCartMapper.cartesianIndex( c2 );
496 if (isNumAquConn(cc1, cc2) || ! isDirectNeighbours(cc1, cc2)) {
499 auto t = this->globalTrans().transmissibility(c1, c2);
500 auto candidate = std::lower_bound(nncData.begin(), nncData.end(),
501 NNCdata { cc1, cc2, 0.0 });
503 while ((candidate != nncData.end()) &&
504 (candidate->cell1 == cc1) &&
505 (candidate->cell2 == cc2))
507 t -= candidate->trans;
516 const auto tt = unitSystem
517 .from_si(UnitSystem::measure::transmissibility, t);
519 if (std::isnormal(tt) && (tt > 1.0e-12)) {
520 this->outputNnc_.emplace_back(cc1, cc2, t);
526 return this->outputNnc_;
529template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
532 const std::optional<int> timeStepNum,
533 const bool isSubStep,
534 data::Solution&& localCellData,
535 data::Wells&& localWellData,
536 data::GroupAndNetworkValues&& localGroupAndNetworkData,
537 data::Aquifers&& localAquiferData,
538 WellTestState&& localWTestState,
539 const Action::State& actionState,
540 const UDQState& udqState,
541 const SummaryState& summaryState,
542 const std::vector<Scalar>& thresholdPressure,
545 bool doublePrecision,
551 const auto isParallel = this->collectOnIORank_.isParallel();
552 const bool needsReordering = this->collectOnIORank_.doesNeedReordering();
554 RestartValue restartValue {
555 (isParallel || needsReordering)
556 ? this->collectOnIORank_.globalCellData()
557 : std::move(localCellData),
559 isParallel ? this->collectOnIORank_.globalWellData()
560 : std::move(localWellData),
562 isParallel ? this->collectOnIORank_.globalGroupAndNetworkData()
563 : std::move(localGroupAndNetworkData),
565 isParallel ? this->collectOnIORank_.globalAquiferData()
566 : std::move(localAquiferData)
569 if (eclState_.getSimulationConfig().useThresholdPressure()) {
570 restartValue.addExtra(
"THRESHPR", UnitSystem::measure::pressure,
576 restartValue.addExtra(
"OPMEXTRA", std::vector<double>(1, nextStepSize));
581 const auto flowsn_global = isParallel ? this->collectOnIORank_.globalFlowsn() : std::move(flowsn);
582 for (
const auto& flows : flowsn_global) {
583 if (flows.name.empty())
585 if (flows.name ==
"FLOGASN+") {
586 restartValue.addExtra(flows.name, UnitSystem::measure::gas_surface_rate, flows.values);
588 restartValue.addExtra(flows.name, UnitSystem::measure::liquid_surface_rate, flows.values);
593 const auto floresn_global = isParallel ? this->collectOnIORank_.globalFloresn() : std::move(floresn);
594 for (
const auto& flores : floresn_global) {
595 if (flores.name.empty()) {
598 restartValue.addExtra(flores.name, UnitSystem::measure::rate, flores.values);
604 this->taskletRunner_->barrier();
607 if (this->taskletRunner_->failure()) {
608 throw std::runtime_error(
"Failure in the TaskletRunner while writing output.");
612 auto eclWriteTasklet = std::make_shared<EclWriteTasklet>(
614 isParallel ? this->collectOnIORank_.globalWellTestState() : std::move(localWTestState),
615 summaryState, udqState, *this->eclIO_,
616 reportStepNum, timeStepNum, isSubStep, curTime, std::move(restartValue), doublePrecision);
619 this->taskletRunner_->dispatch(std::move(eclWriteTasklet));
622template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
625 const Scalar curTime,
626 const data::Wells& localWellData,
627 const data::WellBlockAveragePressures& localWBPData,
628 const data::GroupAndNetworkValues& localGroupAndNetworkData,
629 const std::map<int,data::AquiferData>& localAquiferData,
630 const std::map<std::pair<std::string, int>,
double>& blockData,
631 const std::map<std::string, double>& miscSummaryData,
632 const std::map<std::string, std::vector<double>>& regionData,
633 const Inplace& inplace,
634 const Inplace& initialInPlace,
636 SummaryState& summaryState,
639 if (collectOnIORank_.isIORank()) {
640 const auto& summary = eclIO_->summary();
642 const auto& wellData = this->collectOnIORank_.isParallel()
643 ? this->collectOnIORank_.globalWellData()
646 const auto& wbpData = this->collectOnIORank_.isParallel()
647 ? this->collectOnIORank_.globalWBPData()
650 const auto& groupAndNetworkData = this->collectOnIORank_.isParallel()
651 ? this->collectOnIORank_.globalGroupAndNetworkData()
652 : localGroupAndNetworkData;
654 const auto& aquiferData = this->collectOnIORank_.isParallel()
655 ? this->collectOnIORank_.globalAquiferData()
658 summary.eval(summaryState,
670 getInterRegFlowsAsMap(interRegFlows));
676 const auto udq_step = reportStepNum - 1;
678 this->schedule_.getUDQConfig(udq_step)
681 this->schedule_.wellMatcher(udq_step),
682 this->schedule_.segmentMatcherFactory(udq_step),
683 [es = std::cref(this->eclState_)]() {
684 return std::make_unique<RegionSetMatcher>
685 (es.get().fipRegionStatistics());
687 summaryState, udqState);
691 if (collectOnIORank_.isParallel()) {
698template<
class Gr
id,
class EquilGr
id,
class Gr
idView,
class ElementMapper,
class Scalar>
703 assert (globalTrans_);
704 return *globalTrans_;
Definition: CollectDataOnIORank.hpp:49
bool isIORank() const
Definition: CollectDataOnIORank.hpp:125
Definition: EclGenericWriter.hpp:65
const EclipseState & eclState_
Definition: EclGenericWriter.hpp:156
void doWriteOutput(const int reportStepNum, const std::optional< int > timeStepNum, const bool isSubStep, 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:531
CollectDataOnIORankType collectOnIORank_
Definition: EclGenericWriter.hpp:152
void extractOutputTransAndNNC(const std::function< unsigned int(unsigned int)> &map)
Definition: EclGenericWriter_impl.hpp:266
std::unique_ptr< TaskletRunner > taskletRunner_
Definition: EclGenericWriter.hpp:158
void writeInit()
Definition: EclGenericWriter_impl.hpp:249
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::string, double > &miscSummaryData, const std::map< std::string, std::vector< double > > ®ionData, const Inplace &inplace, const Inplace &initialInPlace, const InterRegFlowMap &interRegFlows, SummaryState &summaryState, UDQState &udqState)
Definition: EclGenericWriter_impl.hpp:624
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:198
const EclipseIO & eclIO() const
Definition: EclGenericWriter_impl.hpp:241
const TransmissibilityType & globalTrans() const
Definition: EclGenericWriter_impl.hpp:701
std::unique_ptr< EclipseIO > eclIO_
Definition: EclGenericWriter.hpp:157
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
Class for serializing and broadcasting data using MPI.
Definition: MPISerializer.hpp:31
void append(T &data, int root=0)
Serialize and broadcast on root process, de-serialize and append on others.
Definition: MPISerializer.hpp:106
void broadcast(T &data, int root=0)
Serialize and broadcast on root process, de-serialize on others.
Definition: MPISerializer.hpp:46
The base class for tasklets.
Definition: tasklets.hpp:44
Handles where a given tasklet is run.
Definition: tasklets.hpp:90
Definition: Transmissibility.hpp:54
Definition: blackoilboundaryratevector.hh:37