www.cemf.ir
symArrayHD.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __symArray_hpp__
22 #define __symArray_hpp__
23 
24 #include "KokkosTypes.hpp"
25 
26 #include "types.hpp"
27 #include "typeInfo.hpp"
28 #include "Vector.hpp"
29 
30 
31 namespace pFlow
32 {
33 
34 
35 template<typename Type>
37 void SWAP(Type &x, Type& y)
38 {
39  auto temp = y;
40  y = x;
41  x = temp;
42 }
43 
44 /*
45 stores the elemnt of a symetric array in the following order in a 1D vector
46 
47  0 1 2 3
48  4 5 6
49  7 8
50  9
51 
52 */
53 template<typename T, typename MemorySpace=void>
54 class symArray
55 {
56 
58 
59  using iterator = T*;
60 
61  using constIterator = const T*;
62 
63  using reference = T&;
64 
65  using constReference = const T&;
66 
67  using valueType = T;
68 
69  using pointer = T*;
70 
71  using constPointer = const T*;
72 
73  // type defs related to Kokkos
75 
76  using deviceType = typename ViewType::device_type;
77 
78  using memory_space = typename ViewType::memory_space;
79 
80  using execution_space = typename ViewType::execution_space;
81 
82 
83 private:
84 
85  uint32 n_= 0;
86 
88 
89 
90  constexpr static const char* memoerySpaceName()
91  {
92  return memory_space::name();
93  }
94 
95 public:
96 
97  // - type info
98  TypeInfoTemplateNV111("symArray", T, memoerySpaceName());
99 
102  symArray();
103 
105  explicit symArray(uint32 n)
106  :
107  symArray("symArray",n)
108  {}
109 
112  :
113  n_(n),
114  view_(name, numElem(n))
115  {}
116 
118  symArray(word name, uint32 n , const T& val)
119  :
120  symArray(name, n)
121  {
122  this->fill(val);
123  }
124 
126  symArray(word name, const Vector<T>& src)
127  :
128  view_(name)
129  {
130  if( !assign(src))
131  {
133  "error in creating symArray " << name << endl;
134  fatalExit;
135  }
136  }
137 
139  symArray(const symArray&) = default;
140 
142  symArray& operator=(const symArray&) = default;
143 
145  symArray(symArray&&) = default;
146 
148  symArray& operator=(symArray&&) = default;
149 
151  ~symArray()=default;
152 
153 
155  void fill(const T& val)
156  {
157  if(n_==0)return;
158  Kokkos::deep_copy(view_, val);
159  }
160 
163  {
164  if(i>j) SWAP(i,j);
165  return view_(j+i*n_-numElem(i));
166  }
167 
169  const T& operator()(uint32 i, uint32 j)const
170  {
171  if(i>j) SWAP(i,j);
172  return view_(j+i*n_-numElem(i));
173  }
174 
175  bool assign(const Vector<T> src)
176  {
177  uint32 nElem = src.size();
178  uint32 n;
179  if( !getN(nElem,n))
180  {
182  "size of input vector do not match a symetric array "<< nElem<<endl;
183  return false;
184  }
185 
186  if(n == 0 )
187  {
188  n_ = 0;
189  return true;
190  }
191 
192  if( n > n_ )
193  Kokkos::realloc(view_, nElem );
194 
195  n_ = n;
196  hostViewType1D<const T> temp(src.data(), nElem );
197  Kokkos::deep_copy(view_, temp);
198 
199  return true;
200  }
201 
202 
204 
205  FUNCTION_H
206  bool read(iIstream& is)
207  {
208  Vector<T> vecFromFile;
209  if( !vecFromFile.read(is) ) return false;
210 
211  this->assign(vecFromFile);
212 
213  return true;
214  }
215 
216  FUNCTION_H
217  bool write(iOstream& os)const
218  {
219 
220  uint32 s = numElem(n_);
221  Vector<T> vecToFile(view_.label(),s);
222 
223  const auto dVec = Kokkos::subview(view_, Pair<uint32,uint32>(0, s));
224  hostViewType1D<T> mirror(vecToFile.data(), vecToFile.size());
225  Kokkos::deep_copy(mirror,dVec);
226 
227  return vecToFile.write(os);
228  }
229 
230 
233  {
234  if(n==0)return 0;
235  return n*(n+1)/2;
236  }
237 
238  static bool getN(uint32 nElem, uint32& n)
239  {
240  real nr = 0.5*(sqrt(8.0*nElem+1)-1);
241  n = static_cast<uint32>(nr);
242  if( equal(nr-static_cast<real>(n) , 0.0) ) return true;
243 
244  return false;
245  }
246 
247 
248 };
249 
250 template<typename T, typename MemorySpace>
252 {
253  if( !iArr.read(is) )
254  {
255  ioErrorInFile (is.name(), is.lineNumber());
256  fatalExit;
257  }
258  return is;
259 }
260 
261 template<typename T, typename MemorySpace>
263 {
264 
265  if( !oArr.write(os) )
266  {
267  ioErrorInFile(os.name(), os.lineNumber());
268  fatalExit;
269  }
270 
271  return os;
272 }
273 
274 }
275 
276 #endif //__symArray_hpp__
pFlow::Vector::read
bool read(iIstream &is)
Definition: Vector.hpp:324
pFlow::symArray::operator()
const INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j) const
Definition: symArrayHD.hpp:169
pFlow::symArray< nonLinearProperties >::reference
nonLinearProperties & reference
Definition: symArrayHD.hpp:63
pFlow::symArray< nonLinearProperties >::iterator
nonLinearProperties * iterator
Definition: symArrayHD.hpp:59
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray()
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, const Vector< T > &src)
Definition: symArrayHD.hpp:126
pFlow::real
float real
Definition: builtinTypes.hpp:45
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n)
Definition: symArrayHD.hpp:111
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::symArray::read
FUNCTION_H bool read(iIstream &is)
Definition: symArrayHD.hpp:206
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(uint32 n)
Definition: symArrayHD.hpp:105
pFlow::sqrt
Vector< T, Allocator > sqrt(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:90
pFlow::symArray::memoerySpaceName
constexpr static const char * memoerySpaceName()
Definition: symArrayHD.hpp:90
types.hpp
pFlow::Pair
Kokkos::pair< T1, T2 > Pair
Pair of two variables.
Definition: KokkosTypes.hpp:85
pFlow::symArray< nonLinearProperties >::deviceType
typename ViewType::device_type deviceType
Definition: symArrayHD.hpp:76
pFlow::equal
INLINE_FUNCTION_HD bool equal(const box &b1, const box &b2, real tol=smallValue)
Definition: box.hpp:135
pFlow::symArray< nonLinearProperties >::execution_space
typename ViewType::execution_space execution_space
Definition: symArrayHD.hpp:80
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::symArray::n_
uint32 n_
Definition: symArrayHD.hpp:85
KokkosTypes.hpp
pFlow::symArray::~symArray
INLINE_FUNCTION_HD ~symArray()=default
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::symArray::getN
static bool getN(uint32 nElem, uint32 &n)
Definition: symArrayHD.hpp:238
pFlow
Definition: demGeometry.hpp:27
pFlow::symArray::operator()
INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j)
Definition: symArrayHD.hpp:162
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:62
pFlow::symArray< nonLinearProperties >::pointer
nonLinearProperties * pointer
Definition: symArrayHD.hpp:69
pFlow::SWAP
INLINE_FUNCTION_HD void SWAP(Type &x, Type &y)
Definition: symArrayHD.hpp:37
pFlow::symArray::assign
bool assign(const Vector< T > src)
Definition: symArrayHD.hpp:175
pFlow::symArray::fill
void fill(const T &val)
Definition: symArrayHD.hpp:155
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n, const T &val)
Definition: symArrayHD.hpp:118
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::symArray::view_
ViewType view_
Definition: symArrayHD.hpp:87
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::symArray< nonLinearProperties >::constReference
const nonLinearProperties & constReference
Definition: symArrayHD.hpp:65
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::symArray< nonLinearProperties >::memory_space
typename ViewType::memory_space memory_space
Definition: symArrayHD.hpp:78
pFlow::symArray::write
FUNCTION_H bool write(iOstream &os) const
Definition: symArrayHD.hpp:217
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::symArray< nonLinearProperties >::constPointer
const nonLinearProperties * constPointer
Definition: symArrayHD.hpp:71
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::symArray< nonLinearProperties >::constIterator
const nonLinearProperties * constIterator
Definition: symArrayHD.hpp:61
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
pFlow::hostViewType1D
Kokkos::View< T *, Kokkos::HostSpace > hostViewType1D
1D array (vector with host memeory space)
Definition: KokkosTypes.hpp:136
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::Vector::write
bool write(iOstream &os) const
Definition: Vector.hpp:328
pFlow::symArray< nonLinearProperties >::ViewType
ViewType1D< nonLinearProperties, void > ViewType
Definition: symArrayHD.hpp:74
pFlow::symArray::numElem
static INLINE_FUNCTION_HD uint32 numElem(uint32 n)
Definition: symArrayHD.hpp:232
pFlow::symArray::operator=
INLINE_FUNCTION_HD symArray & operator=(const symArray &)=default
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
typeInfo.hpp
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:55
pFlow::symArray< nonLinearProperties >::valueType
nonLinearProperties valueType
Definition: symArrayHD.hpp:67
pFlow::Vector
Definition: Vector.hpp:48
pFlow::symArray::TypeInfoTemplateNV111
TypeInfoTemplateNV111("symArray", T, memoerySpaceName())
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::symArray
Definition: symArrayHD.hpp:54
Vector.hpp