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 
185  OPM_HOST_DEVICE static constexpr bool empty() noexcept
186  {
187  return Dimension == 0;
188  }
189 
190 
195  OPM_HOST_DEVICE constexpr void fill(const value_type& value)
196  {
197  for (auto& x : data_) {
198  x = value;
199  }
200  }
201 
203  OPM_HOST_DEVICE bool operator==(const MiniVector& other) const noexcept
204  {
205  for (size_type i = 0; i < Dimension; ++i) {
206  if (data_[i] != other.data_[i]) {
207  return false;
208  }
209  }
210  return true;
211  }
212 
214  OPM_HOST_DEVICE bool operator!=(const MiniVector& other) const noexcept
215  {
216  return !(*this == other);
217  }
218 
220  OPM_HOST_DEVICE MiniVector& operator=(const value_type& value)
221  {
222  fill(value);
223  return *this;
224  }
225 
226  OPM_HOST_DEVICE MiniVector operator+(const value_type& value) const
227  {
228  MiniVector result = *this;
229  for (auto& x : result.data_) {
230  x += value;
231  }
232  return result;
233  }
234 
235  OPM_HOST_DEVICE MiniVector& operator+=(const MiniVector& other)
236  {
237  for (size_type i = 0; i < Dimension; ++i) {
238  data_[i] += other.data_[i];
239  }
240  return *this;
241  }
242 
243  OPM_HOST_DEVICE MiniVector operator+(const MiniVector& other) const
244  {
245  MiniVector result = *this;
246  result += other;
247  return result;
248  }
249 
250  OPM_HOST_DEVICE MiniVector operator-(const MiniVector& other) const
251  {
252  MiniVector result = *this;
253  for (size_type i = 0; i < Dimension; ++i) {
254  result.data_[i] -= other.data_[i];
255  }
256  return result;
257  }
258 
259  OPM_HOST_DEVICE MiniVector& operator-=(const MiniVector& other)
260  {
261  for (size_type i = 0; i < Dimension; ++i) {
262  data_[i] -= other.data_[i];
263  }
264  return *this;
265  }
266 
268  OPM_HOST_DEVICE MiniVector& operator*=(const value_type& value)
269  {
270  for (auto& x : data_) {
271  x *= value;
272  }
273  return *this;
274  }
275 
276 private:
278  std::array<value_type, Dimension> data_ {};
279 };
280 
281 } // namespace Opm::gpuistl
282 
283 #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:203
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:220
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:268
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:195
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:214
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: ThermalGasWaterFlowProblem.hpp:94
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
static OPM_HOST_DEVICE constexpr bool empty() noexcept
Definition: MiniVector.hpp:185
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