# PhasicFlow: src/phasicFlow/containers/symArrayHD/symArrayHD.hpp Source File

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"
/*
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
*/
namespace pFlow
{
template<typename Type>
INLINE_FUNCTION_HD
void SWAP(Type &x, Type& y)
{
auto temp = y;
y = x;
x = temp;
}
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;
protected:
uint32 n_=0;
ViewType view_;
constexpr static inline const char* memoerySpaceName()
{
return memory_space::name();
}
public:
// - type info
TypeInfoTemplateNV2("symArray", T, memoerySpaceName());
INLINE_FUNCTION_H
symArray();
INLINE_FUNCTION_H
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, Vector<T> src)
:
view_(name)
{
if( !assign(src))
{
fatalErrorInFunction<<
"error in creating symArray " << name << endl;
fatalExit;
}
}
INLINE_FUNCTION_H
symArray(const symArray&) = default;
INLINE_FUNCTION_H
symArray& operator=(const symArray&) = default;
INLINE_FUNCTION_H
symArray(symArray&&) = delete;
INLINE_FUNCTION_H
symArray& operator=(symArray&&) = delete;
INLINE_FUNCTION_H
~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
{
int32 s = numElem(n_);
Vector<T, noConstructAllocator<T>> vecToFile(s);
const auto dVec = Kokkos::subview(view_, kPair<int32,int32>(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:378
pFlow::symArray::operator()
const INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j) const
Definition: symArrayHD.hpp:171
pFlow::symArray< nonLinearProperties >::reference
nonLinearProperties & reference
Definition: symArrayHD.hpp:65
pFlow::symArray< nonLinearProperties >::iterator
nonLinearProperties * iterator
Definition: symArrayHD.hpp:61
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray()
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n)
Definition: symArrayHD.hpp:113
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::symArray::operator=
INLINE_FUNCTION_H symArray & operator=(const symArray &)=default
pFlow::symArray::read
FUNCTION_H bool read(iIstream &is)
Definition: symArrayHD.hpp:208
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(uint32 n)
Definition: symArrayHD.hpp:107
pFlow::symArray::memoerySpaceName
constexpr static const char * memoerySpaceName()
Definition: symArrayHD.hpp:92
types.hpp
pFlow::symArray< nonLinearProperties >::deviceType
typename ViewType::device_type deviceType
Definition: symArrayHD.hpp:78
pFlow::symArray< nonLinearProperties >::execution_space
typename ViewType::execution_space execution_space
Definition: symArrayHD.hpp:82
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::symArray::n_
uint32 n_
Definition: symArrayHD.hpp:87
KokkosTypes.hpp
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::symArray::getN
static bool getN(uint32 nElem, uint32 &n)
Definition: symArrayHD.hpp:240
pFlow
Definition: demComponent.hpp:28
pFlow::symArray::operator()
INLINE_FUNCTION_HD T & operator()(uint32 i, uint32 j)
Definition: symArrayHD.hpp:164
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:58
pFlow::symArray< nonLinearProperties >::pointer
nonLinearProperties * pointer
Definition: symArrayHD.hpp:71
pFlow::SWAP
INLINE_FUNCTION_HD void SWAP(Type &x, Type &y)
Definition: symArrayHD.hpp:48
pFlow::symArray::assign
bool assign(const Vector< T > src)
Definition: symArrayHD.hpp:177
pFlow::symArray::fill
void fill(const T &val)
Definition: symArrayHD.hpp:157
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, Vector< T > src)
Definition: symArrayHD.hpp:128
pFlow::symArray::symArray
INLINE_FUNCTION_H symArray(word name, uint32 n, const T &val)
Definition: symArrayHD.hpp:120
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::symArray::view_
ViewType view_
Definition: symArrayHD.hpp:89
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::realloc
INLINE_FUNCTION_H void realloc(ViewType1D< Type, Properties... > &view, int32 len)
Definition: KokkosUtilities.hpp:51
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::symArray< nonLinearProperties >::constReference
const nonLinearProperties & constReference
Definition: symArrayHD.hpp:67
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:80
pFlow::symArray::write
FUNCTION_H bool write(iOstream &os) const
Definition: symArrayHD.hpp:219
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
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:73
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::symArray< nonLinearProperties >::constIterator
const nonLinearProperties * constIterator
Definition: symArrayHD.hpp:63
pFlow::symArray::TypeInfoTemplateNV2
TypeInfoTemplateNV2("symArray", T, memoerySpaceName())
pFlow::hostViewType1D
Kokkos::View< T *, Kokkos::HostSpace > hostViewType1D
Definition: KokkosTypes.hpp:104
pFlow::Vector::write
bool write(iOstream &os) const
Definition: Vector.hpp:383
pFlow::symArray< nonLinearProperties >::ViewType
ViewType1D< nonLinearProperties, void > ViewType
Definition: symArrayHD.hpp:76
pFlow::symArray::numElem
static INLINE_FUNCTION_HD uint32 numElem(uint32 n)
Definition: symArrayHD.hpp:234
pFlow::symArray::~symArray
INLINE_FUNCTION_H ~symArray()=default
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
typeInfo.hpp
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::sqrt
INLINE_FUNCTION_HD real sqrt(real x)
Definition: math.hpp:148
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::symArray< nonLinearProperties >::valueType
nonLinearProperties valueType
Definition: symArrayHD.hpp:69
pFlow::Vector
Definition: Vector.hpp:46
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::symArray
Definition: symArrayHD.hpp:56
pFlow::equal
INLINE_FUNCTION_HD bool equal(const real &s1, const real &s2)
Definition: bTypesFunctions.hpp:188
Vector.hpp
pFlow::kPair
Kokkos::pair< T1, T2 > kPair
Definition: KokkosTypes.hpp:52