# PhasicFlow: src/phasicFlow/containers/symArrayHD/symArrayHD.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

symArrayHD.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __symArray_hpp__
#define __symArray_hpp__
#include "KokkosTypes.hpp"
#include "types.hpp"
#include "typeInfo.hpp"
#include "Vector.hpp"
namespace pFlow
{
template<typename Type>
INLINE_FUNCTION_HD
void SWAP(Type &x, Type& y)
{
auto temp = y;
y = x;
x = temp;
}
/*
stores the elemnt of a symetric array in the following order in a 1D vector
0 1 2 3
4 5 6
7 8
9
*/
template<typename T, typename MemorySpace=void>
class symArray
{
using SymArrayType = symArray<T, MemorySpace>;
using iterator = T*;
using constIterator = const T*;
using reference = T&;
using constReference = const T&;
using valueType = T;
using pointer = T*;
using constPointer = const T*;
// type defs related to Kokkos
using ViewType = ViewType1D<T,MemorySpace>;
using deviceType = typename ViewType::device_type;
using memory_space = typename ViewType::memory_space;
using execution_space = typename ViewType::execution_space;
private:
uint32 n_= 0;
ViewType view_;
constexpr static const char* memoerySpaceName()
{
return memory_space::name();
}
public:
// - type info
TypeInfoTemplateNV111("symArray", T, memoerySpaceName());
INLINE_FUNCTION_H
symArray();
INLINE_FUNCTION_H
explicit symArray(uint32 n)
:
symArray("symArray",n)
{}
INLINE_FUNCTION_H
symArray(word name, uint32 n)
:
n_(n),
view_(name, numElem(n))
{}
INLINE_FUNCTION_H
symArray(word name, uint32 n , const T& val)
:
symArray(name, n)
{
this->fill(val);
}
INLINE_FUNCTION_H
symArray(word name, const Vector<T>& src)
:
view_(name)
{
if( !assign(src))
{
fatalErrorInFunction<<
"error in creating symArray " << name << endl;
fatalExit;
}
}
INLINE_FUNCTION_HD
symArray(const symArray&) = default;
INLINE_FUNCTION_HD
symArray& operator=(const symArray&) = default;
INLINE_FUNCTION_HD
symArray(symArray&&) = default;
INLINE_FUNCTION_HD
symArray& operator=(symArray&&) = default;
INLINE_FUNCTION_HD
~symArray()=default;
void fill(const T& val)
{
if(n_==0)return;
Kokkos::deep_copy(view_, val);
}
INLINE_FUNCTION_HD
T& operator()(uint32 i, uint32 j)
{
if(i>j) SWAP(i,j);
return view_(j+i*n_-numElem(i));
}
INLINE_FUNCTION_HD
const T& operator()(uint32 i, uint32 j)const
{
if(i>j) SWAP(i,j);
return view_(j+i*n_-numElem(i));
}
bool assign(const Vector<T> src)
{
uint32 nElem = src.size();
uint32 n;
if( !getN(nElem,n))
{
fatalErrorInFunction<<
"size of input vector do not match a symetric array "<< nElem<<endl;
return false;
}
if(n == 0 )
{
n_ = 0;
return true;
}
if( n > n_ )
Kokkos::realloc(view_, nElem );
n_ = n;
hostViewType1D<const T> temp(src.data(), nElem );
Kokkos::deep_copy(view_, temp);
return true;
}
FUNCTION_H
bool read(iIstream& is)
{
Vector<T> vecFromFile;
if( !vecFromFile.read(is) ) return false;
this->assign(vecFromFile);
return true;
}
FUNCTION_H
bool write(iOstream& os)const
{
uint32 s = numElem(n_);
Vector<T> vecToFile(view_.label(),s);
const auto dVec = Kokkos::subview(view_, Pair<uint32,uint32>(0, s));
hostViewType1D<T> mirror(vecToFile.data(), vecToFile.size());
Kokkos::deep_copy(mirror,dVec);
return vecToFile.write(os);
}
INLINE_FUNCTION_HD
static uint32 numElem(uint32 n)
{
if(n==0)return 0;
return n*(n+1)/2;
}
static bool getN(uint32 nElem, uint32& n)
{
real nr = 0.5*(sqrt(8.0*nElem+1)-1);
n = static_cast< uint32 >(nr);
if( equal(nr-static_cast< real >(n) , 0.0) ) return true;
return false;
}
};
template<typename T, typename MemorySpace>
inline iIstream& operator >> (iIstream & is, symArray<T, MemorySpace> & iArr )
{
if( !iArr.read(is) )
{
ioErrorInFile (is.name(), is.lineNumber());
fatalExit;
}
return is;
}
template<typename T, typename MemorySpace>
inline iOstream& operator << (iOstream& os, const symArray<T, MemorySpace>& oArr )
{
if( !oArr.write(os) )
{
ioErrorInFile(os.name(), os.lineNumber());
fatalExit;
}
return os;
}
}
#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