v0.14.0
Tensor1_pointer.hpp
Go to the documentation of this file.
1 /* A version for pointers, useful for previously
2  constructed arrays. */
3 
4 #pragma once
5 
6 namespace FTensor
7 {
8 
9 template <class T, int Tensor_Dim> class Tensor1<T *, Tensor_Dim> {
10 protected:
11 
12  const int inc;
13 
14  /* Note that the T *'s are mutable, so the pointer can change,
15  allowing iterating over a array. */
16 
17  mutable T *restrict data[Tensor_Dim];
18 
19 public:
20  /* Initializations for varying numbers of elements, with each one
21  defined for a particular Tensor_Dim. To initialize a different
22  dimension, just add the appropriate constructor and call to
23  the Tensor1_constructor constructor. */
24  Tensor1(T *d0, const int i = 1) : inc(i) {
26  }
27  Tensor1(T *d0, T *d1, const int i = 1) : inc(i) {
29  }
30  Tensor1(T *d0, T *d1, T *d2, const int i = 1) : inc(i) {
32  }
33  Tensor1(T *d0, T *d1, T *d2, T *d3, const int i = 1) : inc(i) {
35  }
36  Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, const int i = 1) : inc(i) {
37  Tensor1_constructor<T *restrict, Tensor_Dim>(data, d0, d1, d2, d3, d4, d5);
38  }
39  Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, T *d6, T *d7,
40  const int i = 1)
41  : inc(i) {
42  Tensor1_constructor<T *restrict, Tensor_Dim>(data, d0, d1, d2, d3, d4, d5,
43  d6, d7);
44  }
45  Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, T *d6, T *d7, T *d8,
46  const int i = 1)
47  : inc(i) {
48  Tensor1_constructor<T *restrict, Tensor_Dim>(data, d0, d1, d2, d3, d4, d5,
49  d6, d7, d8);
50  }
51  /* Initializations for varying numbers of elements. */
52  template <class... U> Tensor1(U *... d) : data(d...), inc(1) {}
53 
54  /* Initialization from array */
55  template <class U>
56  Tensor1(std::array<U *, Tensor_Dim> &a, const int i = 1) : inc(i) {
57  std::copy(a.begin(), a.end(), data);
58  }
59 
60  Tensor1(const int i = 1) : inc(i) {}
61 
64  (*this)(a) /= l2();
65  return *this;
66  }
67 
68  T l2() const { return sqrt(l2_squared(Number<Tensor_Dim>())); }
69 
70  template <int Current_Dim> T l2_squared(const Number<Current_Dim> &) const {
71  return (*data[Current_Dim - 1]) * (*data[Current_Dim - 1]) +
72  l2_squared(Number<Current_Dim - 1>());
73  }
74  T l2_squared(const Number<1> &) const { return (*data[0]) * (*data[0]); }
75 
76  /* There are two operator(int)'s, one for non-consts that lets you
77  change the value, and one for consts that doesn't. */
78 
79  T &operator()(const int N) {
80 #ifdef FTENSOR_DEBUG
81  if(N >= Tensor_Dim || N < 0)
82  {
83  std::stringstream s;
84  s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.operator(" << N
85  << ")" << std::endl;
86  throw std::out_of_range(s.str());
87  }
88 #endif
89  return *data[N];
90  }
91  T operator()(const int N) const
92  {
93 #ifdef FTENSOR_DEBUG
94  if(N >= Tensor_Dim || N < 0)
95  {
96  std::stringstream s;
97  s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.operator(" << N
98  << ") const" << std::endl;
99  throw std::out_of_range(s.str());
100  }
101 #endif
102  return *data[N];
103  }
104  T *ptr(const int N) const
105  {
106 #ifdef FTENSOR_DEBUG
107  if(N >= Tensor_Dim || N < 0)
108  {
109  std::stringstream s;
110  s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.ptr(" << N << ")"
111  << std::endl;
112  throw std::out_of_range(s.str());
113  }
114 #endif
115  return data[N];
116  }
117 
118  T *&ptr(const int N) {
119 #ifdef FTENSOR_DEBUG
120  if (N >= Tensor_Dim || N < 0) {
121  std::stringstream s;
122  s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.ptr(" << N << ")"
123  << std::endl;
124  throw std::out_of_range(s.str());
125  }
126 #endif
127  return data[N];
128  }
129 
130  /* These operator()'s are the first part in constructing template
131  expressions. They can be used to slice off lower dimensional
132  parts. They are not entirely safe, since you can accidentaly use a
133  higher dimension than what is really allowed (like Dim=5). */
134 
135  template <char i, int Dim>
137  operator()(const Index<i, Dim> &index)
138  {
139  return Tensor1_Expr<Tensor1<T *, Tensor_Dim>, T, Dim, i>(*this);
140  }
141 
142  template <char i, int Dim>
144  operator()(const Index<i, Dim> &index) const
145  {
146  return Tensor1_Expr<const Tensor1<T *, Tensor_Dim>, T, Dim, i>(*this);
147  }
148 
149  /* The ++ operator increments the pointer, not the number that the
150  pointer points to. This allows iterating over a grid. */
151 
152  const Tensor1 &operator++() const
153  {
154  for(int i = 0; i < Tensor_Dim; ++i)
155  data[i] += inc;
156  return *this;
157  }
158 
159  private:
160 
161  /**
162  * @brief Preventing casting on derived class
163  *
164  * That can be source of errors
165  *
166  */
167  template <int I>
168  Tensor1(const Tensor1<PackPtr<T *, I>, Tensor_Dim> &) = delete;
169  };
170 
171  template <class T, int Tensor_Dim, int I>
172  class Tensor1<PackPtr<T *, I>, Tensor_Dim> : public Tensor1<T *, Tensor_Dim> {
173 
174  public:
175 
176  /* Initializations for varying numbers of elements. */
177  template <class... U> Tensor1(U *... d) : Tensor1<T *, Tensor_Dim>(d...) {}
178 
179  template <class U>
180  Tensor1(std::array<U *, Tensor_Dim> &a) : Tensor1<T *, Tensor_Dim>(a) {}
181 
182  Tensor1(): Tensor1<T *, Tensor_Dim>() {}
183 
184  /* The ++ operator increments the pointer, not the number that the
185  pointer points to. This allows iterating over a grid. */
186 
187  const Tensor1 &operator++() const
188  {
189  for(int i = 0; i < Tensor_Dim; ++i)
191  return *this;
192  }
193  };
194 
195 
196 }
FTensor
JSON compatible output.
Definition: Christof_constructor.hpp:6
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, const int i=1)
Definition: Tensor1_pointer.hpp:36
FTensor::Tensor1
Definition: Tensor1_value.hpp:8
d3
static constexpr std::array< double, 3 > d3
Definition: prism_elements_from_surface.cpp:20
FTensor::d
const Tensor1_Expr< const dTensor0< T, Dim, i >, typename promote< T, double >::V, Dim, i > d(const Tensor0< T * > &a, const Index< i, Dim > index, const Tensor1< int, Dim > &d_ijk, const Tensor1< double, Dim > &d_xyz)
Definition: dTensor0.hpp:27
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, const int i=1)
Definition: Tensor1_pointer.hpp:24
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, T *d6, T *d7, const int i=1)
Definition: Tensor1_pointer.hpp:39
FTensor::Tensor1< T *, Tensor_Dim >::l2_squared
T l2_squared(const Number< 1 > &) const
Definition: Tensor1_pointer.hpp:74
FTensor::Tensor1< T *, Tensor_Dim >
Definition: Tensor1_pointer.hpp:9
FTensor::Tensor1_constructor
Definition: Tensor1_constructor.hpp:8
I
constexpr IntegrationType I
Definition: operators_tests.cpp:31
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(std::array< U *, Tensor_Dim > &a, const int i=1)
Definition: Tensor1_pointer.hpp:56
FTensor::Tensor1< PackPtr< T *, I >, Tensor_Dim >::Tensor1
Tensor1()
Definition: Tensor1_pointer.hpp:182
Tensor1::Tensor1
Tensor1(double d0, double d1, double d2)
Definition: single.cpp:46
FTensor::Tensor1< T *, Tensor_Dim >::operator()
Tensor1_Expr< const Tensor1< T *, Tensor_Dim >, T, Dim, i > operator()(const Index< i, Dim > &index) const
Definition: Tensor1_pointer.hpp:144
FTensor::Tensor1< T *, Tensor_Dim >::inc
const int inc
Definition: Tensor1_pointer.hpp:12
FTensor::Tensor1< T *, Tensor_Dim >::l2_squared
T l2_squared(const Number< Current_Dim > &) const
Definition: Tensor1_pointer.hpp:70
FTensor::Number
Definition: Number.hpp:11
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, T *d2, T *d3, const int i=1)
Definition: Tensor1_pointer.hpp:33
FTensor::Tensor1_Expr
Definition: Tensor1_Expr.hpp:27
a
constexpr double a
Definition: approx_sphere.cpp:30
FTensor::Tensor1< T *, Tensor_Dim >::normalize
Tensor1< T, Tensor_Dim > normalize()
Definition: Tensor1_pointer.hpp:62
FTensor::Tensor1< T *, Tensor_Dim >::operator++
const Tensor1 & operator++() const
Definition: Tensor1_pointer.hpp:152
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, T *d2, const int i=1)
Definition: Tensor1_pointer.hpp:30
FTensor::PackPtr
Definition: FTensor.hpp:54
i
FTensor::Index< 'i', SPACE_DIM > i
Definition: hcurl_divergence_operator_2d.cpp:27
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(U *... d)
Definition: Tensor1_pointer.hpp:52
FTensor::Index
Definition: Index.hpp:23
N
const int N
Definition: speed_test.cpp:3
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(const int i=1)
Definition: Tensor1_pointer.hpp:60
FTensor::Tensor1< PackPtr< T *, I >, Tensor_Dim >::Tensor1
Tensor1(std::array< U *, Tensor_Dim > &a)
Definition: Tensor1_pointer.hpp:180
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, const int i=1)
Definition: Tensor1_pointer.hpp:27
FTensor::Tensor1< T *, Tensor_Dim >::operator()
T operator()(const int N) const
Definition: Tensor1_pointer.hpp:91
FTensor::Tensor1< PackPtr< T *, I >, Tensor_Dim >::operator++
const Tensor1 & operator++() const
Definition: Tensor1_pointer.hpp:187
FTensor::Tensor1< T *, Tensor_Dim >::operator()
T & operator()(const int N)
Definition: Tensor1_pointer.hpp:79
d4
static constexpr std::array< double, 3 > d4
Definition: prism_elements_from_surface.cpp:21
FTensor::Tensor1< T *, Tensor_Dim >::l2
T l2() const
Definition: Tensor1_pointer.hpp:68
FTensor::Tensor1< T *, Tensor_Dim >::ptr
T *& ptr(const int N)
Definition: Tensor1_pointer.hpp:118
FTensor::Tensor1< T *, Tensor_Dim >::operator()
Tensor1_Expr< Tensor1< T *, Tensor_Dim >, T, Dim, i > operator()(const Index< i, Dim > &index)
Definition: Tensor1_pointer.hpp:137
FTensor::Tensor1< T *, Tensor_Dim >::Tensor1
Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, T *d6, T *d7, T *d8, const int i=1)
Definition: Tensor1_pointer.hpp:45
FTensor::Tensor1< T *, Tensor_Dim >::ptr
T * ptr(const int N) const
Definition: Tensor1_pointer.hpp:104
FTensor::Tensor1< PackPtr< T *, I >, Tensor_Dim >::Tensor1
Tensor1(U *... d)
Definition: Tensor1_pointer.hpp:177
EshelbianPlasticity::U
@ U
Definition: EshelbianContact.cpp:193