LgrOutputHelpers.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_GRID_CPGRID_LGROUTPUTHELPERS_HEADER_INCLUDED
21#define OPM_GRID_CPGRID_LGROUTPUTHELPERS_HEADER_INCLUDED
22
23#include <opm/grid/CpGrid.hpp>
25
26#include <opm/input/eclipse/Units/UnitSystem.hpp>
27#include <opm/output/data/Cells.hpp>
28#include <opm/output/data/Solution.hpp>
29#include <opm/output/eclipse/RestartValue.hpp>
30
31#include <cstddef> // for std::size_t
32#include <utility> // for std::move
33#include <type_traits> // for std::is_same_v
34#include <vector>
35
36namespace Opm
37{
38namespace Lgr
39{
40
53 int level);
54
62template <typename Container>
63Container reorderForOutput(const Container& simulatorContainer,
64 const std::vector<int>& toOutput);
65
87 const std::vector<std::vector<int>>& toOutput_refinedLevels,
88 const Opm::data::Solution& leafSolution,
89 std::vector<Opm::data::Solution>&);
90
102template <typename Grid>
103void extractRestartValueLevelGrids(const Grid& grid,
104 const Opm::RestartValue& leafRestartValue,
105 std::vector<Opm::RestartValue>& restartValue_levels);
106
107} // namespace Lgr
108} // namespace Opm
109
110template <typename Container>
111Container Opm::Lgr::reorderForOutput(const Container& simulatorContainer,
112 const std::vector<int>& toOutput)
113{
114 // Use toOutput to reorder simulatorContainer
115 Container outputContainer;
116 outputContainer.resize(toOutput.size());
117 for (std::size_t i = 0; i < toOutput.size(); ++i) {
118 outputContainer[i] = simulatorContainer[toOutput[i]];
119 }
120 return outputContainer;
121}
122
123template <typename Grid>
125 const Opm::RestartValue& leafRestartValue,
126 std::vector<Opm::RestartValue>& restartValue_levels)
127{
128 if constexpr (std::is_same_v<Grid, Dune::CpGrid>) {
129
130 int maxLevel = grid.maxLevel();
131 restartValue_levels.resize(maxLevel+1); // level 0, 1, ..., max level
132
133 // To store leafRestartValue.extra data in the order expected
134 // by outout files (increasing level Cartesian indices)
135 std::vector<std::vector<int>> toOutput_refinedLevels{};
136 toOutput_refinedLevels.resize(maxLevel); // exclude level zero (does not need reordering)
137
138 const Opm::LevelCartesianIndexMapper<Dune::CpGrid> levelCartMapp(grid);
139 for (int level = 1; level <= maxLevel; ++level) { // exclude level zero (does not need reordering)
140 toOutput_refinedLevels[level-1] = mapLevelIndicesToCartesianOutputOrder(grid, levelCartMapp, level);
141 }
142
143 std::vector<Opm::data::Solution> dataSolutionLevels{};
145 toOutput_refinedLevels,
146 leafRestartValue.solution,
147 dataSolutionLevels);
148
149 for (int level = 0; level <= maxLevel; ++level) { // exclude level zero (does not need reordering)
150 restartValue_levels[level] = Opm::RestartValue(std::move(dataSolutionLevels[level]),
151 leafRestartValue.wells,
152 leafRestartValue.grp_nwrk,
153 leafRestartValue.aquifer,
154 level);
155 }
156
157 for (const auto& [rst_key, leafVector] : leafRestartValue.extra) {
158
159 std::vector<std::vector<double>> levelVectors{};
160 levelVectors.resize(maxLevel+1);
161
162 // Parent cells do not appear on the leaf grid -> get rubbish value
163 // std::numeric_limits<double>::max()
164 for (int level = 0; level <= maxLevel; ++level) {
165 levelVectors[level].resize(grid.levelGridView(level).size(0),
166 std::numeric_limits<double>::max());
167 }
168 // For level cells that appear in the leaf, extract the data value from leafVector
169 // and assign it to the equivalent level cell.
170 for (const auto& element : Dune::elements(grid.leafGridView())) {
171 levelVectors[element.level()][element.getLevelElem().index()] = leafVector[element.index()];
172 }
173
174 // Use toOutput_levels to reorder in ascending level cartesian indices
175 for (int level = 1; level<=maxLevel; ++level) { // exclude level zero (does not need reordering)
176 levelVectors[level] = Opm::Lgr::reorderForOutput(levelVectors[level], toOutput_refinedLevels[level-1]);
177 }
178
179 for (int level = 0; level <= maxLevel; ++level) {
180 restartValue_levels[level].addExtra(rst_key.key, rst_key.dim, std::move(levelVectors[level]));
181 }
182 }
183 }
184}
185
186#endif // OPM_GRID_CPGRID_LGROUTPUTHELPERS_HEADER_INCLUDED
[ provides Dune::Grid ]
Definition: CpGrid.hpp:203
Definition: cpgrid/LevelCartesianIndexMapper.hpp:53
void extractSolutionLevelGrids(const Dune::CpGrid &grid, const std::vector< std::vector< int > > &toOutput_refinedLevels, const Opm::data::Solution &leafSolution, std::vector< Opm::data::Solution > &)
Extracts and organizes solution data for all grid refinement levels.
void extractRestartValueLevelGrids(const Grid &grid, const Opm::RestartValue &leafRestartValue, std::vector< Opm::RestartValue > &restartValue_levels)
Constructs restart-value containers for all grid refinement levels.
Definition: LgrOutputHelpers.hpp:124
Container reorderForOutput(const Container &simulatorContainer, const std::vector< int > &toOutput)
Reorder data from a simulation container into the order assumed by output for refined level grids.
Definition: LgrOutputHelpers.hpp:111
std::vector< int > mapLevelIndicesToCartesianOutputOrder(const Dune::CpGrid &grid, const Opm::LevelCartesianIndexMapper< Dune::CpGrid > &levelCartMapp, int level)
Builds a mapping from level element indices to the Cartesian ordering required by output files.
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26