GEOS
Tensor.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
16 #ifndef GEOS_COMMON_TENSOR_HPP_
17 #define GEOS_COMMON_TENSOR_HPP_
18 
19 #include "GeosxMacros.hpp"
20 
21 namespace geos
22 {
23 
29 template< typename T, int SIZE_TPARAM >
30 class Tensor
31 {
32 public:
33 
34  static_assert( SIZE_TPARAM > 0, "Tensor size must be a positive value" );
35 
37  using value_type = T;
38 
40  static constexpr int SIZE = SIZE_TPARAM;
41 
49  T const & operator[]( std::ptrdiff_t const i ) const
50  {
51  return data[i];
52  }
53 
61  T & operator[]( std::ptrdiff_t const i )
62  {
63  return data[i];
64  }
65 
73  template< typename U = T >
76  std::enable_if_t< std::is_floating_point< U >::value, bool >
77  operator==( Tensor< U, SIZE > const & rhs ) const
78  {
79  for( int i = 0; i < SIZE; ++i )
80  {
81  if( (data[i] > rhs.data[i]) || (data[i] < rhs.data[i]) )
82  {
83  return false;
84  }
85  }
86  return true;
87  }
88 
96  template< typename U = T >
99  std::enable_if_t< !std::is_floating_point< U >::value, bool >
100  operator==( Tensor< U, SIZE > const & rhs ) const
101  {
102  for( int i = 0; i < SIZE; ++i )
103  {
104  if( data[i] != rhs.data[i] )
105  {
106  return false;
107  }
108  }
109  return true;
110  }
111 
119  constexpr int size( int junk ) const
120  {
121  GEOS_UNUSED_VAR( junk );
122  return SIZE;
123  }
124 
129 
132  {
133  double result = 0;
134  for( int i = 0; i < SIZE_TPARAM; ++i )
135  {
136  result += lhs.data[i] * rhs.data[i];
137  }
138  return result;
139  };
140 
142  T data[SIZE] = {};
143 
144 private:
145 
152  friend inline std::ostream & operator<<( std::ostream & os, Tensor< T, SIZE > const & t )
153  {
154  os << '{' << t.data[0];
155  for( int i = 1; i < SIZE; ++i )
156  {
157  os << ',' << t.data[i];
158  }
159  os << '}';
160  return os;
161  }
162 };
163 
164 // *****************************************************************************
165 // ****************************** END DECLARATION ******************************
166 // *****************************************************************************
167 
173 template< typename T, int SIZE_TPARAM >
175 GEOS_FORCE_INLINE Tensor< T, SIZE_TPARAM > &
177 {
178  for( int i = 0; i < SIZE_TPARAM; ++i )
179  {
180  data[i] = rhs;
181  }
182  return *this;
183 }
184 
190 template< typename T, int SIZE_TPARAM >
194 {
195  for( int i = 0; i < SIZE_TPARAM; ++i )
196  {
197  data[i] += rhs;
198  }
199  return *this;
200 }
201 
207 template< typename T, int SIZE_TPARAM >
211 {
212  for( int i = 0; i < SIZE_TPARAM; ++i )
213  {
214  data[i] += rhs.data[i];
215  }
216  return *this;
217 }
218 
219 }
220 
221 #endif //GEOS_COMMON_TENSOR_HPP_
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:49
#define GEOS_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:84
#define GEOS_FORCE_INLINE
Marks a function or lambda for inlining.
Definition: GeosxMacros.hpp:51
Lightweight wrapper around a c-array.
Definition: Tensor.hpp:31
Tensor< T, SIZE_TPARAM > & operator+=(const double &rhs)
Adds a single value to all tensor components.
Definition: Tensor.hpp:193
T value_type
Alias for type template parameter.
Definition: Tensor.hpp:37
T const & operator[](std::ptrdiff_t const i) const
Const element access.
Definition: Tensor.hpp:49
std::enable_if_t< !std::is_floating_point< U >::value, bool > operator==(Tensor< U, SIZE > const &rhs) const
Equality comparison operator.
Definition: Tensor.hpp:100
Tensor< T, SIZE_TPARAM > & operator+=(const Tensor< T, SIZE_TPARAM > &rhs)
Component-wise addition of two tensors.
Definition: Tensor.hpp:210
T & operator[](std::ptrdiff_t const i)
Non-const element access.
Definition: Tensor.hpp:61
static constexpr int SIZE
Alias for size template parameter.
Definition: Tensor.hpp:40
T data[SIZE]
Underlying array.
Definition: Tensor.hpp:142
std::enable_if_t< std::is_floating_point< U >::value, bool > operator==(Tensor< U, SIZE > const &rhs) const
Equality comparison operator.
Definition: Tensor.hpp:77
friend std::ostream & operator<<(std::ostream &os, Tensor< T, SIZE > const &t)
Stream insertion operator for Tensor.
Definition: Tensor.hpp:152
constexpr int size(int junk) const
Returns the size of the tensor.
Definition: Tensor.hpp:119
friend double operator*(const Tensor< T, SIZE_TPARAM > &lhs, const Tensor< T, SIZE_TPARAM > &rhs)
Define dot product. TODO: Check compatibility of lhs and rhs.
Definition: Tensor.hpp:131
Tensor< T, SIZE_TPARAM > & operator=(const double &rhs)
Declare assignment operators.
Definition: Tensor.hpp:176