ElementChunks.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 SINTEF Digital
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_ELEMENT_CHUNKS_HEADER
20#define OPM_ELEMENT_CHUNKS_HEADER
21
23
24#include <cstddef>
25#include <iterator>
26#include <utility>
27#include <vector>
28
29namespace Opm
30{
31
63template <class GridView, class PartitionSet>
65{
66private:
67 using Iter = decltype(std::begin(elements(std::declval<const GridView&>(), PartitionSet())));
68 using Storage = std::vector<Iter>;
69 using StorageIter = decltype(Storage().cbegin());
70
71public:
72 ElementChunks(const GridView& gv,
73 const PartitionSet included_partition,
74 const std::size_t num_chunks)
75 {
76 grid_chunk_iterators_
77 = Opm::createChunkIterators(elements(gv, included_partition), gv.size(0), num_chunks);
78 }
79
80 struct Chunk
81 {
82 Chunk(const Iter& i1, const Iter& i2) : pi_(i1, i2) {}
83 auto begin() const { return pi_.first; }
84 auto end() const { return pi_.second; }
85 std::pair<Iter, Iter> pi_;
86 };
87
88 struct ChunkIterator : public StorageIter
89 {
90 explicit ChunkIterator(const StorageIter& itit) : StorageIter(itit) {}
92 {
93 const StorageIter it = *this;
94 return Chunk{*it, *(it+1)};
95 }
96 };
97
98 auto begin() const
99 {
100 return ChunkIterator(grid_chunk_iterators_.begin());
101 }
102 auto end() const
103 {
104 // Note the decrement, we want a StorageIterator pointing to
105 // the last element in grid_chunk_iterators_, not the
106 // one-beyond-last-element iterator.
107 return ChunkIterator(--grid_chunk_iterators_.end());
108 }
109 auto size() const
110 {
111 return grid_chunk_iterators_.size() - 1;
112 }
115 Chunk operator[](std::size_t i) const
116 {
117 return Chunk{grid_chunk_iterators_[i], grid_chunk_iterators_[i + 1]};
118 }
119private:
120 Storage grid_chunk_iterators_;
121};
122
123
124} // namespace Opm
125
126#endif // OPM_ELEMENT_CHUNKS_HEADER
Definition: ElementChunks.hpp:65
auto end() const
Definition: ElementChunks.hpp:102
Chunk operator[](std::size_t i) const
Definition: ElementChunks.hpp:115
auto size() const
Definition: ElementChunks.hpp:109
auto begin() const
Definition: ElementChunks.hpp:98
ElementChunks(const GridView &gv, const PartitionSet included_partition, const std::size_t num_chunks)
Definition: ElementChunks.hpp:72
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26
auto createChunkIterators(const Range &r, const std::size_t num_elem, const std::size_t num_chunks)
Definition: createThreadIterators.hpp:48
Definition: ElementChunks.hpp:89
Chunk operator*() const
Definition: ElementChunks.hpp:91
ChunkIterator(const StorageIter &itit)
Definition: ElementChunks.hpp:90
Definition: ElementChunks.hpp:81
auto end() const
Definition: ElementChunks.hpp:84
std::pair< Iter, Iter > pi_
Definition: ElementChunks.hpp:85
auto begin() const
Definition: ElementChunks.hpp:83
Chunk(const Iter &i1, const Iter &i2)
Definition: ElementChunks.hpp:82