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