opm-simulators
MiniVector.hpp
1 /*
2  Copyright 2025 Equinor ASA
3  This file is part of the Open Porous Media project (OPM).
4  OPM is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8  OPM is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with OPM. If not, see <http://www.gnu.org/licenses/>.
14 */
15 
16 #ifndef OPM_GPUISTL_MINIVECTOR_HPP
17 #define OPM_GPUISTL_MINIVECTOR_HPP
18 
19 #include <algorithm>
20 #include <array>
21 #include <cstddef>
22 #include <initializer_list>
23 #include <stdexcept>
24 #include <type_traits>
25 
26 #include <opm/common/ErrorMacros.hpp>
27 #include <opm/common/utility/gpuDecorators.hpp>
28 
29 #include <dune/common/fvector.hh>
30 
41 namespace Opm::gpuistl
42 {
43 
48 template <class T, int Dimension>
50 {
51  static_assert(Dimension > 0, "Dimension must be positive");
52 
53 public:
55  using value_type = T;
57  using size_type = std::size_t;
61  using const_reference = const value_type&;
63  using iterator = typename std::array<T, Dimension>::iterator;
65  using const_iterator = typename std::array<T, Dimension>::const_iterator;
66 
70  OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible<value_type>::value) = default;
71 
76  OPM_HOST_DEVICE constexpr explicit MiniVector(const value_type& value)
77  {
78  fill(value);
79  }
80 
85  explicit MiniVector(const Dune::FieldVector<T, Dimension>& fv)
86  {
87  for (size_type i = 0; i < Dimension; ++i) {
88  data_[i] = fv[i];
89  }
90  }
91 
104  OPM_HOST_DEVICE MiniVector(std::initializer_list<value_type> init)
105  {
106  if (init.size() != Dimension) {
107  OPM_THROW(std::runtime_error, "Opm::MiniVector – initializer‑list size mismatch");
108  }
109  std::copy_n(init.begin(), Dimension, data_.begin());
110  }
111 
115  OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
116  {
117  return data_[idx];
118  }
119 
121  OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
122  {
123  return data_[idx];
124  }
125 
130  OPM_HOST_DEVICE reference at(size_type idx)
131  {
132  if (idx >= Dimension) {
133  OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
134  }
135  return data_[idx];
136  }
138  OPM_HOST_DEVICE const_reference at(size_type idx) const
139  {
140  if (idx >= Dimension) {
141  OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
142  }
143  return data_[idx];
144  }
145 
147  OPM_HOST_DEVICE constexpr iterator begin() noexcept
148  {
149  return data_.begin();
150  }
152  OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
153  {
154  return data_.begin();
155  }
157  OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
158  {
159  return data_.cbegin();
160  }
161 
163  OPM_HOST_DEVICE constexpr iterator end() noexcept
164  {
165  return data_.end();
166  }
168  OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
169  {
170  return data_.end();
171  }
173  OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
174  {
175  return data_.cend();
176  }
177 
179  OPM_HOST_DEVICE static constexpr size_type size() noexcept
180  {
181  return Dimension;
182  }
183 
184 
189  OPM_HOST_DEVICE constexpr void fill(const value_type& value)
190  {
191  for (auto& x : data_) {
192  x = value;
193  }
194  }
195 
197  OPM_HOST_DEVICE bool operator==(const MiniVector& other) const noexcept
198  {
199  for (size_type i = 0; i < Dimension; ++i) {
200  if (data_[i] != other.data_[i]) {
201  return false;
202  }
203  }
204  return true;
205  }
206 
208  OPM_HOST_DEVICE bool operator!=(const MiniVector& other) const noexcept
209  {
210  return !(*this == other);
211  }
212 
214  OPM_HOST_DEVICE MiniVector& operator=(const value_type& value)
215  {
216  fill(value);
217  return *this;
218  }
219 
220  OPM_HOST_DEVICE MiniVector operator+(const value_type& value) const
221  {
222  MiniVector result = *this;
223  for (auto& x : result.data_) {
224  x += value;
225  }
226  return result;
227  }
228 
229  OPM_HOST_DEVICE MiniVector& operator+=(const MiniVector& other)
230  {
231  for (size_type i = 0; i < Dimension; ++i) {
232  data_[i] += other.data_[i];
233  }
234  return *this;
235  }
236 
237  OPM_HOST_DEVICE MiniVector operator+(const MiniVector& other) const
238  {
239  MiniVector result = *this;
240  result += other;
241  return result;
242  }
243 
244  OPM_HOST_DEVICE MiniVector operator-(const MiniVector& other) const
245  {
246  MiniVector result = *this;
247  for (size_type i = 0; i < Dimension; ++i) {
248  result.data_[i] -= other.data_[i];
249  }
250  return result;
251  }
252 
253  OPM_HOST_DEVICE MiniVector& operator-=(const MiniVector& other)
254  {
255  for (size_type i = 0; i < Dimension; ++i) {
256  data_[i] -= other.data_[i];
257  }
258  return *this;
259  }
260 
262  OPM_HOST_DEVICE MiniVector& operator*=(const value_type& value)
263  {
264  for (auto& x : data_) {
265  x *= value;
266  }
267  return *this;
268  }
269 
270 private:
272  std::array<value_type, Dimension> data_ {};
273 };
274 
275 } // namespace Opm::gpuistl
276 
277 #endif // OPM_GPUISTL_MINIVECTOR_HPP
OPM_HOST_DEVICE constexpr iterator end() noexcept
Definition: MiniVector.hpp:163
OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
Definition: MiniVector.hpp:152
OPM_HOST_DEVICE constexpr iterator begin() noexcept
Definition: MiniVector.hpp:147
static OPM_HOST_DEVICE constexpr size_type size() noexcept
Definition: MiniVector.hpp:179
OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
Definition: MiniVector.hpp:115
OPM_HOST_DEVICE reference at(size_type idx)
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:130
OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
Definition: MiniVector.hpp:121
OPM_HOST_DEVICE bool operator==(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:197
OPM_HOST_DEVICE const_reference at(size_type idx) const
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:138
OPM_HOST_DEVICE MiniVector & operator=(const value_type &value)
Definition: MiniVector.hpp:214
OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible< value_type >::value)=default
Default‑constructs the MiniVector; elements are value‑initialized.
OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
Definition: MiniVector.hpp:157
OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
Definition: MiniVector.hpp:168
T value_type
Element type.
Definition: MiniVector.hpp:55
value_type & reference
Mutable element reference.
Definition: MiniVector.hpp:59
OPM_HOST_DEVICE MiniVector & operator*=(const value_type &value)
Definition: MiniVector.hpp:262
typename std::array< T, Dimension >::const_iterator const_iterator
Immutable iterator.
Definition: MiniVector.hpp:65
const value_type & const_reference
Immutable element reference.
Definition: MiniVector.hpp:61
OPM_HOST_DEVICE constexpr void fill(const value_type &value)
Fill every component with the supplied value.
Definition: MiniVector.hpp:189
MiniVector(const Dune::FieldVector< T, Dimension > &fv)
Conversion constructor from Dune::FieldVector.
Definition: MiniVector.hpp:85
OPM_HOST_DEVICE bool operator!=(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:208
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: AmgxInterface.hpp:37
OPM_HOST_DEVICE MiniVector(std::initializer_list< value_type > init)
Initializer‑list constructor.
Definition: MiniVector.hpp:104
std::size_t size_type
Index/size type.
Definition: MiniVector.hpp:57
OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
Definition: MiniVector.hpp:173
typename std::array< T, Dimension >::iterator iterator
Mutable iterator.
Definition: MiniVector.hpp:63
Definition: MiniVector.hpp:49