LeafGridAssembler.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026 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#ifndef OPM_GRID_REFINEMENT_LEAFGRIDASSEMBLER_HEADER_INCLUDED
20#define OPM_GRID_REFINEMENT_LEAFGRIDASSEMBLER_HEADER_INCLUDED
21
26
27#include <opm/common/utility/SparseTable.hpp>
28
29#include <dune/common/fvector.hh>
30#include <dune/common/parallel/mpihelper.hh>
31
32#include <array>
33#include <map>
34#include <memory>
35#include <set>
36#include <tuple>
37#include <vector>
38
39namespace Dune
40{
41namespace cpgrid
42{
43class CpGridData;
44}
45}
46
47namespace Opm
48{
49namespace Refinement
50{
51
114{
115public:
119 {
120 int grid; // 0 = level zero, b+1 = level grid of box b
121 int index; // cell or face index in that grid
122 };
123
134 LeafGridAssembler(const std::vector<std::shared_ptr<Dune::cpgrid::CpGridData>>& storage,
135 const std::vector<BlockRefinement>& requests,
136 const std::array<int,3>& parentDims,
137 const double* coord,
138 const double* zcorn,
139 const int* actnum,
140 Dune::MPIHelper::MPICommunicator comm);
141
144 std::shared_ptr<Dune::cpgrid::CpGridData> build();
145
146private:
147 // TODO: store the box's level index (b + 1) here, and give SourceRef a boxIndex(), to
148 // drop the b + 1 and grid - 1 conversions in the stages.
149 struct BoxData
150 {
152 int cornerMask{0}; // cornerSlotMask of the level grid
153 std::array<int,3> factors;
154 std::array<AxisSubdivision,3> subs;
155 std::array<AxisPositions,3> pos;
156 bool graded{false};
157 std::array<int,3> refinedDims;
158 // children of each parent cell, ordered by cartesianIndexInParentCell
159 std::map<int, std::vector<int>> childrenOfParent;
160 // level corner -> corner of the parent grid (or -1)
161 std::vector<int> cornerEquiv;
162 // level corner -> leaf pool index
163 std::vector<int> cornerToLeaf; // written by buildCornerPool
164 // level cell/face -> leaf index
165 std::vector<int> cellToLeaf;
166 std::vector<int> faceToLeaf; // written by buildLeafFaces
167 };
168
169 struct SyntheticFace
170 {
171 int boxCell;
172 int neighborCell; // coarse, or a child of another box
173 std::vector<int> points; // leaf corner indices
174 Dune::FieldVector<double,3> normal; // unit, along +axis (CpGrid convention)
175 Dune::FieldVector<double,3> center;
176 double area;
177 enum face_tag tag;
178 int side; // box boundary side: -1 low, +1 high
179 };
180
181 // The constructor's lookups; each reads the members declared before the one it
182 // initialises.
183 std::vector<int> computeParentBoxOf() const;
184 std::vector<int> computeCompressed0() const;
185 std::vector<int> computeBoxOfCell() const;
186
187 // The stages, in the order build() runs them.
188 void collectBoxData();
189 void buildCornerPool();
190 void buildLeafCells();
191 void refuseThrowsLeavingBoxes() const;
192 void findDroppedFaces();
193 void findFaultedSides();
194 void buildLeafFaces();
195 void buildSyntheticFaces();
196 void buildLeafTopology();
197 void computeCellData();
198 void buildParallelStructures();
199 void recordLeafMetadata();
200
201 // Used by several stages.
203 bool nested(int b) const;
205 int boxOfParentGridCell(int b, int cell) const;
207 int leafOfParentGridCell(int b, int cell) const;
209 std::array<int,3> parentGridIJK(int b, int cell) const;
211 int nestedNeighborOf(int b, int parent, int axis, int side) const;
213 int outsideNeighborOf(int parent, int axis, int side) const;
218 int childAcross(int b, const std::array<int,3>& lattice, int axis, int side,
219 int nb, int neighbor) const;
220
221 // Const access for the stages after the one that writes each member.
222 const std::vector<BoxData>& boxes() const { return boxes_; }
223 const std::vector<std::vector<int>>& nestedBoxOf() const { return nestedBoxOf_; }
224 const std::vector<SourceRef>& leafCells() const { return leafCells_; }
225 const std::vector<int>& leafIdxOfCell0() const { return leafIdxOfCell0_; }
226 const std::vector<std::vector<int>>& leafIdxOfLevelCell() const { return leafIdxOfLevelCell_; }
227 const std::vector<std::tuple<int, std::vector<int>>>& level0ToChildren() const { return level0ToChildren_; }
228 int numLeafCells() const { return numLeafCells_; }
229 const std::vector<bool>& faceDropped() const { return faceDropped_; }
230 const std::set<std::tuple<int,int,int>>& faultedSides() const { return faultedSides_; }
231 const std::map<std::tuple<int,int,int>, CellRange>& acrossRange() const { return acrossRange_; }
232 const std::vector<SourceRef>& leafFaces() const { return leafFaces_; }
233 const std::vector<int>& mosaicOutside() const { return mosaicOutside_; }
234 const std::vector<std::map<int,int>>& refinedCartToCell() const { return refinedCartToCell_; }
235 int numSourceFaces() const { return numSourceFaces_; }
236 const std::vector<SyntheticFace>& syntheticFaces() const { return syntheticFaces_; }
237 int numLeafFaces() const { return numLeafFaces_; }
238 int numLeafCorners() const { return numLeafCorners_; }
239
240 // The input.
241 const std::vector<std::shared_ptr<Dune::cpgrid::CpGridData>>& storage_;
242 const std::vector<BlockRefinement>& requests_;
243 const std::array<int,3> parentDims_;
244 const double* const coord_;
245 const double* const zcorn_;
246 const int* const actnum_;
247 const Dune::MPIHelper::MPICommunicator comm_;
248
249 // Written by the constructor: the box tree and the basic level-zero lookups.
251 const int numBoxes_;
252 const std::array<int,3> dims0_;
253 const int numCells0_;
254 const int numCorners0_;
255 // The parent box of each box, -1 for a box that refines level zero.
256 const std::vector<int> parentBoxOf_;
257 // The level-zero cell at each Cartesian index of level zero, or -1.
258 const std::vector<int> compressed0_;
259 // The box of each level-zero cell, or -1.
260 const std::vector<int> boxOfCell_;
261 const std::vector<std::array<int,8>>& cellToPoint0_;
262 const Dune::cpgrid::OrientedEntityTable<1,0>& faceToCell0_;
263 const Opm::SparseTable<int>& faceToPoint0_;
266 const int numFaces0_;
267
268 // Written by collectBoxData.
269 std::vector<BoxData> boxes_;
270 int mask0_{0}; // cornerSlotMask of level zero
271 // [parent box][cell of its level grid] -> nested (child) box index, or -1
272 std::vector<std::vector<int>> nestedBoxOf_;
273
274 // Written by buildCornerPool. The later stages fill the leaf.
275 std::shared_ptr<Dune::cpgrid::CpGridData> leaf_;
276 // leafCornerOrigin_[corner] = {level, index}: the grid where the leaf corner first
277 // appears, and its index there; {0, corner} for the corners of level zero.
278 std::vector<std::array<int,2>> leafCornerOrigin_;
279 int numLeafCorners_{0}; // updated by buildSyntheticFaces
280
281 // Written by buildLeafCells.
282 std::vector<SourceRef> leafCells_;
283 std::vector<int> leafIdxOfCell0_;
284 std::vector<std::vector<int>> leafIdxOfLevelCell_;
285 // level0ToChildren_[c] = {level, children}: the immediate children of level-zero cell c in
286 // its box's level grid ({level, {}} if the box's MINPV removed them all), or {-1, {}}.
287 // TODO: nested vectors are slow to fill and read; a SparseTable<int> with the level stored
288 // alongside would do better, here and for nestedParentToChildren_.
289 std::vector<std::tuple<int, std::vector<int>>> level0ToChildren_;
290 int numLeafCells_{0};
291 std::map<int, std::vector<std::tuple<int, std::vector<int>>>> nestedParentToChildren_;
292
293 // Written by findDroppedFaces: by level-zero face, whether the leaf drops it.
294 std::vector<bool> faceDropped_;
295
296 // Written by outsideNeighborOf(): its answers, by (level-zero cell, axis 0..2, side -1
297 // or +1); top-level boxes only.
298 mutable std::map<std::tuple<int,int,int>, int> verifiedNeighbor_;
299
300 // Written by findFaultedSides: the faulted sides, as (top-level box index, axis 0..2,
301 // side -1 or +1).
302 std::set<std::tuple<int,int,int>> faultedSides_;
303 // By (box index, axis, side), for top-level and nested boxes: the IJK range, in the
304 // grid the box refines, of the cells really across that box side, from that grid's
305 // faces: a throw taller than one cell next to the box puts them outside the box's
306 // own layers.
307 std::map<std::tuple<int,int,int>, CellRange> acrossRange_;
308
309 // Written by buildLeafFaces.
310 std::vector<SourceRef> leafFaces_;
311 // The leaf cell across a face that its source grid sees as a box boundary face (an
312 // unrefined cell of the grid the box refines, or a child of another box), aligned
313 // with leafFaces_; -1 otherwise.
314 std::vector<int> mosaicOutside_;
315 std::vector<std::map<int,int>> refinedCartToCell_;
316 int numSourceFaces_{0};
317
318 // Written by buildSyntheticFaces.
319 std::vector<SyntheticFace> syntheticFaces_;
320 int numLeafFaces_{0};
321
322 // Written by computeCellData; recordLeafMetadata moves them into the leaf.
323 std::vector<int> leafGlobalCell_;
324 std::vector<std::array<int,2>> leafChildToParent_;
325 std::vector<int> leafCartesianIndexInParentCell_;
326 std::vector<std::array<int,2>> leafToLevel_;
327
328 bool built_{false};
329};
330
331} // namespace Refinement
332} // namespace Opm
333
334#endif // OPM_GRID_REFINEMENT_LEAFGRIDASSEMBLER_HEADER_INCLUDED
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:144
A class design to hold a variable with a value for each entity of the given codimension,...
Definition: EntityRep.hpp:316
Definition: LeafGridAssembler.hpp:114
LeafGridAssembler(const std::vector< std::shared_ptr< Dune::cpgrid::CpGridData > > &storage, const std::vector< BlockRefinement > &requests, const std::array< int, 3 > &parentDims, const double *coord, const double *zcorn, const int *actnum, Dune::MPIHelper::MPICommunicator comm)
std::shared_ptr< Dune::cpgrid::CpGridData > build()
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
T area(const Point< T, 2 > *c)
Definition: Volumes.hpp:119
std::array< int, 3 > refinedDims(const BlockRefinement &request)
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26
face_tag
Definition: preprocess.h:66
Definition: LeafGridAssembler.hpp:119
int index
Definition: LeafGridAssembler.hpp:121
int grid
Definition: LeafGridAssembler.hpp:120
double b(const Dune::FieldVector< ct, dimworld > &, double)
Definition: transportproblem2.hh:25