# PhasicFlow: src/phasicFlow/streams/dataIO/dataIO.cpp 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.

dataIO.cpp
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.
-----------------------------------------------------------------------------*/
template<typename T>
bool pFlow::writeDataAsciiBinary(iOstream& os, span<T> data)
{
if( std::is_trivially_copyable_v<T> && os.isBinary() )
{
// first write the number of data
uint64 numData = data.size();
os<< numData << endl;
// write the bindary data
auto chBuffer = charSpan(data);
if(!os.write(chBuffer.data(), chBuffer.size()))
{
fatalErrorInFunction<<
"error in writing binary data to "<<os.name()<<endl;
return false;
}
}
else
{
if( !writeDataASCII(os, data) )
{
fatalErrorInFunction<<
"error in writing ASCII data to "<<os.name()<<endl;
return false;
}
}
return os.check(FUNCTION_NAME);
}
template<typename T>
bool pFlow::writeDataASCII(iOstream& os, span<T> data)
{
os<< data.size()<<endl;
os << token::BEGIN_LIST;
if(data.size()>0)
{
for(uint32 i=0; i<data.size()-1; i++)
{
os << data[i]<<token::NL;
}
os << data[data.size()-1] << token::END_LIST;
}
else
{
os<< token::END_LIST;
}
os.check(FUNCTION_NAME);
return true;
}
template<typename T>
bool pFlow::readDataAsciiBinary
(
iIstream& is,
std::vector<T>& data
)
{
if(std::is_trivially_copyable_v<T> && is.isBinary())
{
data.clear();
// read length of data
token firstToken(is);
size_t len = 0;
if( firstToken.isInt64())
{
len = firstToken.int64Token();
data.resize(len);
}
else
{
fatalErrorInFunction<<
"expected length of vector in the stream "<<is.name()<<
"but found this "<< firstToken<<endl;
return false;
}
is.read(reinterpret_cast<char*>(data.data()), len*sizeof(T));
return is.check(FUNCTION_NAME);
}
else
{
return readDataAscii(is, data);
}
}
template<typename T>
bool pFlow::readDataAscii
(
iIstream& is,
std::vector<T>& vec
)
{
is.fatalCheck(FUNCTION_NAME);
vec.clear();
token firstToken(is);
size_t len = 0;
bool lenFound = false;
if( firstToken.isInt64())
{
len = firstToken.int64Token();
lenFound = true;
vec.reserve(len);
firstToken = token(is);
}
T val{};
if( firstToken.isPunctuation() ) // start of vector
{
if(firstToken != token::BEGIN_LIST)
{
warningInFunction
<< "expected token "<< token::BEGIN_LIST
<< " but found "<< firstToken ;
return false;
}
token lastToken(is);
is.fatalCheck(FUNCTION_NAME);
while(!(lastToken.isPunctuation()
&& lastToken == token::END_LIST ))
{
is.putBack(lastToken);
is >> val;
vec.push_back(val);
is >> lastToken;
is.fatalCheck(FUNCTION_NAME);
}
} else
{
warningInFunction
<< "expected token "<< token::BEGIN_LIST
<< " but found "<< firstToken<<endl; ;
return false;
}
if(lenFound && len>0 && len != vec.size())
{
warningInFunction<<"vector length specified "<< len <<
" is different from number of elements "<< vec.size()<<endl;
return false;
}
return true;
}
template<typename T>
bool pFlow::dataIO<T>::writeData(iOstream& os, span<T> data)
{
if(!gatherData( data ) )
{
fatalErrorInFunction<<
"Error in gathering data for out stream "<< os.name()<<endl;
return false;
}
if( ioPattern_.thisProcWriteData())
{
return writeDataAsciiBinary(os, bufferSpan_);
}
else
{
return true;
}
}
template<>
inline
bool pFlow::dataIO<pFlow::word>::writeData(iOstream& os, span<word> data)
{
if( ioPattern_.isParallel() && ioPattern_.isMasterProcessorDistribute())
{
notImplementedFunction<<
"data transfer for type word is not supported in parallel mode!"<<endl;
fatalExit;
}
if(!gatherData( data ) )
{
fatalErrorInFunction<<
"Error in gathering data for out stream "<< os.name()<<endl;
return false;
}
if( ioPattern_.thisProcWriteData())
{
return writeDataASCII(os, bufferSpan_);
}
else
{
return true;
}
}
template<typename T>
bool pFlow::dataIO<T>::readData
(
iIstream& is,
std::vector<T>& data
)
{
data.clear();
if(ioPattern_.thisProcReadData())
{
return readDataAsciiBinary(is, data);
}
else
{
return true;
}
}
template<typename T>
pFlow::uniquePtr<pFlow::dataIO<T>>
pFlow::dataIO<T>::create(const IOPattern& iop)
{
word dataIOType = angleBracketsNames2(
"dataIO",
getTypeName<T>(),
pFlowProcessors().localRunTypeName());
if(IOPatternvCtorSelector_.search(dataIOType))
{
return IOPatternvCtorSelector_[dataIOType](iop);
}
else
{
printKeys
(
fatalError << "Ctor Selector "<< dataIOType << " does not exist. \n"
<<"Avaiable ones are: \n\n"
,
IOPatternvCtorSelector_
);
fatalExit;
}
return nullptr;
}
pFlow::span
Definition: span.hpp:31
notImplementedFunction
#define notImplementedFunction
Report that a function is yet not implemented.
Definition: error.hpp:84
pFlow::iOstream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
pFlow::iIstream::read
virtual iIstream & read(token &)=0
Return next token from stream.
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::writeDataAsciiBinary
bool writeDataAsciiBinary(iOstream &os, span< T > data)
Definition: dataIO.cpp:22
pFlow::dataIO::writeData
bool writeData(iOstream &os, span< T > data)
Write data to the end of file from all processors.
Definition: dataIO.cpp:193
pFlow::dataIO::create
static uniquePtr< dataIO > create(const IOPattern &iop)
Definition: dataIO.cpp:263
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::token::isPunctuation
bool isPunctuation() const
Token is PUNCTUATION.
Definition: tokenI.hpp:444
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::span::size
INLINE_FUNCTION_HD uint32 size() const
Returns the number of elements in the span.
Definition: span.hpp:101
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::printKeys
iOstream & printKeys(iOstream &os, const wordHashMap< T > &m)
pFlow::token::int64Token
int64 int64Token() const
Return int64 value.
Definition: tokenI.hpp:502
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::writeDataASCII
bool writeDataASCII(iOstream &os, span< T > data)
Definition: dataIO.cpp:57
pFlow::angleBracketsNames2
word angleBracketsNames2(const word &base, const word &w1, const word &w2)
Output base<w1,w2>
Definition: bTypesFunctions.cpp:162
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::readDataAsciiBinary
bool readDataAsciiBinary(iIstream &is, std::vector< T > &data)
Definition: dataIO.cpp:80
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::iIstream::putBack
void putBack(const token &tok)
Put back token Only a single put back is permitted.
Definition: iIstream.cpp:23
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::readDataAscii
bool readDataAscii(iIstream &is, std::vector< T > &vec)
Definition: dataIO.cpp:120
pFlow::token::isInt64
bool isInt64() const
Token is int64.
Definition: tokenI.hpp:492
fatalError
#define fatalError
Report a fatal error and exit the applicaiton.
Definition: error.hpp:70
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::charSpan
span< char > charSpan(span< T > s)
Definition: span.hpp:177
pFlow::IOstream::isBinary
bool isBinary() const
Return true if stream format is binray.
Definition: IOstream.hpp:180
pFlow::pFlowProcessors
localProcessors & pFlowProcessors()
Definition: pFlowProcessors.cpp:5
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::dataIO::readData
bool readData(iIstream &is, std::vector< T > &data)
Definition: dataIO.cpp:245