www.cemf.ir
dataIO.cpp
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 template<typename T>
23 {
24 
25  if( std::is_trivially_copyable_v<T> && os.isBinary() )
26  {
27  // first write the number of data
28  uint64 numData = data.size();
29  os<< numData << endl;
30 
31  // write the bindary data
32  auto chBuffer = charSpan(data);
33 
34  if(!os.write(chBuffer.data(), chBuffer.size()))
35  {
37  "error in writing binary data to "<<os.name()<<endl;
38  return false;
39  }
40 
41  }
42  else
43  {
44  if( !writeDataASCII(os, data) )
45  {
47  "error in writing ASCII data to "<<os.name()<<endl;
48  return false;
49  }
50  }
51 
52  return os.check(FUNCTION_NAME);
53 
54 }
55 
56 template<typename T>
58 {
59  os<< data.size()<<endl;
60  os << token::BEGIN_LIST;
61  if(data.size()>0)
62  {
63  for(uint32 i=0; i<data.size()-1; i++)
64  {
65  os << data[i]<<token::NL;
66  }
67  os << data[data.size()-1] << token::END_LIST;
68  }
69  else
70  {
71  os<< token::END_LIST;
72  }
73 
74  os.check(FUNCTION_NAME);
75  return true;
76 }
77 
78 template<typename T>
80 (
81  iIstream& is,
82  std::vector<T>& data
83 )
84 {
85 
86  if(std::is_trivially_copyable_v<T> && is.isBinary())
87  {
88  data.clear();
89  // read length of data
90  token firstToken(is);
91 
92  size_t len = 0;
93  if( firstToken.isInt64())
94  {
95  len = firstToken.int64Token();
96  data.resize(len);
97  }
98  else
99  {
101  "expected length of vector in the stream "<<is.name()<<
102  "but found this "<< firstToken<<endl;
103  return false;
104  }
105 
106  is.read(reinterpret_cast<char*>(data.data()), len*sizeof(T));
107 
108  return is.check(FUNCTION_NAME);
109  }
110  else
111  {
112 
113  return readDataAscii(is, data);
114  }
115 
116 }
117 
118 template<typename T>
120 (
121  iIstream& is,
122  std::vector<T>& vec
123 )
124 {
125 
127 
128  vec.clear();
129 
130  token firstToken(is);
131 
132  size_t len = 0;
133  bool lenFound = false;
134  if( firstToken.isInt64())
135  {
136  len = firstToken.int64Token();
137  lenFound = true;
138  vec.reserve(len);
139  firstToken = token(is);
140  }
141 
142 
143  T val{};
144  if( firstToken.isPunctuation() ) // start of vector
145  {
146  if(firstToken != token::BEGIN_LIST)
147  {
149  << "expected token "<< token::BEGIN_LIST
150  << " but found "<< firstToken ;
151  return false;
152 
153  }
154 
155  token lastToken(is);
156 
158 
159  while(!(lastToken.isPunctuation()
160  && lastToken == token::END_LIST ))
161  {
162 
163  is.putBack(lastToken);
164 
165  is >> val;
166  vec.push_back(val);
167 
168  is >> lastToken;
170 
171  }
172 
173  } else
174  {
176  << "expected token "<< token::BEGIN_LIST
177  << " but found "<< firstToken<<endl; ;
178  return false;
179  }
180 
181  if(lenFound && len>0 && len != vec.size())
182  {
183  warningInFunction<<"vector length specified "<< len <<
184  " is different from number of elements "<< vec.size()<<endl;
185  return false;
186  }
187 
188  return true;
189 }
190 
191 
192 template<typename T>
194 {
196  if(!gatherData( data ) )
197  {
199  "Error in gathering data for out stream "<< os.name()<<endl;
200  return false;
201  }
202 
203  if( ioPattern_.thisProcWriteData())
204  {
205  return writeDataAsciiBinary(os, bufferSpan_);
206  }
207  else
208  {
209  return true;
210  }
211 }
212 
213 template<>
214 inline
216 {
217 
218  if( ioPattern_.isParallel() && ioPattern_.isMasterProcessorDistribute())
219  {
221  "data transfer for type word is not supported in parallel mode!"<<endl;
222  fatalExit;
223  }
224 
226  if(!gatherData( data ) )
227  {
229  "Error in gathering data for out stream "<< os.name()<<endl;
230  return false;
231  }
232 
233  if( ioPattern_.thisProcWriteData())
234  {
235  return writeDataASCII(os, bufferSpan_);
236  }
237  else
238  {
239  return true;
240  }
241 }
242 
243 template<typename T>
245 (
246  iIstream& is,
247  std::vector<T>& data
248 )
249 {
250  data.clear();
251  if(ioPattern_.thisProcReadData())
252  {
253  return readDataAsciiBinary(is, data);
254  }
255  else
256  {
257  return true;
258  }
259 }
260 
261 template<typename T>
264 {
265 
266  word dataIOType = angleBracketsNames2(
267  "dataIO",
268  getTypeName<T>(),
269  pFlowProcessors().localRunTypeName());
270 
271  if(IOPatternvCtorSelector_.search(dataIOType))
272  {
273  return IOPatternvCtorSelector_[dataIOType](iop);
274  }
275  else
276  {
277  printKeys
278  (
279  fatalError << "Ctor Selector "<< dataIOType << " does not exist. \n"
280  <<"Avaiable ones are: \n\n"
281  ,
282  IOPatternvCtorSelector_
283  );
284  fatalExit;
285  }
286 
287  return nullptr;
288 }
289 
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