GenericInput.tpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 
25 
26 namespace mimmo{
27 
28 namespace inputCSVStream{
29 
36 template <typename T>
37 std::fstream&
38 ifstreamcsv(std::fstream &in, T &x){
39 
40  T dummy{};
41  char delim;
42  if ((in.good())) {
43  if (in >> dummy && in >> delim && !in.bad()) { x = dummy;}
44  }
45  return(in);
46 }
47 
54 template <typename T>
55 std::fstream& ifstreamcsvend(std::fstream &in, T &x){
56 
57  T dummy{};
58  if ((in.good())) {
59  if (in >> dummy && !in.bad()) { x = dummy;}
60  }
61  return(in);
62 }
63 
70 template <typename T>
71 std::fstream& ifstreamcsv(std::fstream &in, std::vector< T > &x){
72 
73  T dummy;
74 
75  while (in.good()) {
76  if (ifstreamcsv(in,dummy) && !in.bad()) { x.push_back(dummy); }
77  }
78  return(in);
79 }
80 
87 template <typename T>
88 std::fstream& ifstreamcsvend(std::fstream &in, std::vector< T > &x){
89  return ifstreamcsv(in, x);
90 }
91 
98 template <typename T, size_t d>
99 std::fstream& ifstreamcsv(std::fstream &in, std::array< T, d> &x){
100  T dummy{};
101  size_t i;
102 
103  i = 0;
104  while ((in.good()) && (i < d-1)) {
105  if (ifstreamcsv(in,dummy) && !in.bad()) {
106  x[i] = dummy;
107  }
108  i++;
109  } //next i
110  if (ifstreamcsvend(in,dummy) && !in.bad()) {
111  x[i] = dummy;
112  }
113  return(in);
114 };
115 
122 template <typename T, size_t d>
123 std::fstream& ifstreamcsvend(std::fstream &in, std::array< T, d> &x){
124  return ifstreamcsv(in,x);
125 };
126 
133 template <typename T>
134 std::fstream& ifstreamcsv(std::fstream &in, MimmoPiercedVector< T > &x){
135 
136  T dummy;
137  long id;
138  std::string name;
139  int location;
140  long sizeData = 0, readSizeData;
141 
142  if(ifstreamcsvend(in, name) && !in.bad()){
143  x.setName(name);
144  }
145  if(ifstreamcsvend(in, location) && !in.bad()){
146  if(x.intIsValidLocation(location)) x.setDataLocation(static_cast<MPVLocation>(location));
147  }
148  if(ifstreamcsvend(in, readSizeData) && !in.bad()){
149  sizeData = long(readSizeData);
150  }
151  x.reserve(sizeData);
152  for(long count = 0; count<sizeData; ++count){
153  ifstreamcsv(in,id) ;
154  ifstreamcsvend(in,dummy);
155  x.insert(id,dummy);
156  }
157  return(in);
158 }
159 
166 template<typename T, size_t d >
167 std::fstream& ifstreamcsv(std::fstream &in, MimmoPiercedVector< std::array< T,d > > &x){
168 
169  std::array<T,d> dummy;
170  long id;
171  std::string name;
172  int location;
173  long sizeData = 0, readSizeData;
174 
175  if(ifstreamcsvend(in, name) && !in.bad()){
176  x.setName(name);
177  }
178  if(ifstreamcsvend(in, location) && !in.bad()){
179  if(x.intIsValidLocation(location)) x.setDataLocation(static_cast<MPVLocation>(location));
180  }
181  if(ifstreamcsvend(in, readSizeData) && !in.bad()){
182  if (readSizeData < std::numeric_limits<long>::max())
183  sizeData = long(readSizeData);
184  else
185  sizeData = 0;
186  }
187  x.reserve(sizeData);
188  for(long count = 0; count<sizeData; ++count){
189  ifstreamcsv(in,id) ;
190  ifstreamcsvend(in,dummy);
191 
192  x.insert(id,dummy);
193  }
194  return(in);
195 }
196 
197 
198 }
199 
201 
207 template<typename T>
208 void
210  _setInput(data);
211  _setResult(data);
212 }
213 
219 template<typename T>
220 void
222  _setInput(data);
223  _setResult(data);
224 }
225 
230 template<typename T>
231 T*
233  return(static_cast<IODataT<T>*>(m_input.get())->getData());
234 }
235 
240 template<typename T>
241 void
243  m_result = std::move(std::unique_ptr<IOData>(new IODataT<T>(*data)));
244 }
245 
249 template<typename T>
250 void
252  m_result = std::move(std::unique_ptr<IOData>(new IODataT<T>(data)));
253 }
254 
259 template<typename T>
260 void
261 GenericInput::_setInput(T* data){
262  m_input = std::move(std::unique_ptr<IOData>(new IODataT<T>(*data)));
263 }
264 
269 template<typename T>
270 void
271 GenericInput::_setInput(T& data){
272  m_input = std::move(std::unique_ptr<IOData>(new IODataT<T>(data)));
273 }
274 
279 template<typename T>
280 T*
281 GenericInput::_getInput(){
282  return(static_cast<IODataT<T>*>(m_input.get())->getData());
283 }
284 
289 template<typename T>
290 void
291 GenericInput::_setResult(T* data){
292  m_result = std::move(std::unique_ptr<IOData> (new IODataT<T>(*data)));
293 }
294 
299 template<typename T>
300 void
301 GenericInput::_setResult(T& data){
302  m_result = std::move(std::unique_ptr<IOData> (new IODataT<T>(data)));
303 }
304 
309 template<typename T>
310 T*
311 GenericInput::_getResult(){
312  return(static_cast<IODataT<T>*>(m_result.get())->getData());
313 }
314 
321 template<typename T>
322 T
324  if (m_readFromFile){
325  T data;
326 #if MIMMO_ENABLE_MPI
327  if(getRank() == 0)
328 # endif
329  {
330  std::fstream file;
331  file.open(m_dir+"/"+m_filename, std::fstream::in);
332  if (file.is_open()){
333  if (m_csv){
334  inputCSVStream::ifstreamcsv(file, data);
335  }
336  else{
337  file >> data;
338  }
339  file.close();
340  }else{
341  (*m_log) << "Error GenericInput: file not open --> exit" << std::endl;
342  throw std::runtime_error (m_name + " : cannot open " + m_filename + " requested");
343  }
344 
345  }
346 
347 #if MIMMO_ENABLE_MPI
348  // 0 read something, let's share with all its friends.
349  if(m_nprocs > 1) sendReadDataToAllProcs(data);
350 #endif
351  //finally store it in result slot.
352  _setResult(data);
353  }
354 
355  //ready to return the data.
356  return *static_cast<IODataT<T>*>(m_result.get())->getData();
357 }
358 
359 #if MIMMO_ENABLE_MPI
360 
365 template<typename T>
366 void
367 GenericInput::sendReadDataToAllProcs(T & dataTC){
368 
369  if(getRank() == 0){
370 
371  //create char output data buffer and reverse data into it.
372  mimmo::OBinaryStream dataBuffer;
373  dataBuffer << dataTC;
374  long dataBufferSize = dataBuffer.getSize();
375  //Send data to all other procs
376  for (int sendRank=1; sendRank<m_nprocs; sendRank++){
377  MPI_Send(&dataBufferSize, 1, MPI_LONG, sendRank, 100, m_communicator);
378  MPI_Send(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, sendRank, 110, m_communicator);
379  }
380  //hey 0, your job is done.
381  }else{
382 
383  long dataBufferSize;
384  MPI_Recv(&dataBufferSize, 1, MPI_LONG, 0, 100, m_communicator, MPI_STATUS_IGNORE);
385  mimmo::IBinaryStream dataBuffer(dataBufferSize);
386  MPI_Recv(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, 0, 110, m_communicator, MPI_STATUS_IGNORE);
387 
388  dataBuffer >> dataTC;
389  }
390 }
391 
392 #endif
393 
394 
395 //GENERICINPUTMPVDATA/////////////////////////////////////////////////////
396 
397 
404 template < typename T>
405 MimmoPiercedVector< T > *
407  return _getResult< T >() ;
408 }
409 
414 template<typename T>
415 void
416 GenericInputMPVData::_setResult(MimmoPiercedVector< T > * data){
417  m_result = std::move(std::unique_ptr<IOData> (new IODataT<MimmoPiercedVector< T > >(*data)));
418 }
419 
424 template<typename T>
425 void
426 GenericInputMPVData::_setResult(MimmoPiercedVector< T > & data){
427  m_result = std::move(std::unique_ptr<IOData> (new IODataT<MimmoPiercedVector < T > >(data)));
428 }
429 
436 template<typename T>
437 MimmoPiercedVector< T >*
438 GenericInputMPVData::_getResult(){
439 
440  MimmoPiercedVector< T > data;
441  MimmoSharedPointer<MimmoObject> refgeo = getGeometry();
442 
443 #if MIMMO_ENABLE_MPI
444  if(getRank() == 0)
445 #endif
446  {
447  std::string name;
448  int n_loc;
449  long nSize = 0, readNSize;
450  long id;
451  T data_T;
452 
453  if(m_csv) m_binary = false;
454 
455  std::fstream file;
456  file.open(m_dir+"/"+m_filename, std::fstream::in);
457  if (file.is_open()){
458  if (m_binary){
459  std::size_t length;
460  bitpit::genericIO::absorbBINARY(file, length);
461  std::vector<char> namevector(length);
462  for (std::size_t i=0; i<length; i++){
463  bitpit::genericIO::absorbBINARY(file, namevector[i]);
464  }
465  name = std::string(name.begin(), name.end());
466  bitpit::genericIO::absorbBINARY(file, n_loc);
467  bitpit::genericIO::absorbBINARY(file, nSize);
468  data.reserve(nSize);
469  for(int i=0; i<nSize; ++i){
470  bitpit::genericIO::absorbBINARY(file, id);
471  bitpit::genericIO::absorbBINARY(file, data_T);
472  data.insert(id,data_T);
473  }
474  data.setDataLocation(n_loc);
475  data.setName(name);
476  }
477  else if (m_csv){
478  inputCSVStream::ifstreamcsv(file, data);
479  }else{
480  bitpit::genericIO::absorbASCII(file, name);
481  bitpit::genericIO::absorbASCII(file, n_loc);
482  bitpit::genericIO::absorbASCII(file, readNSize);
483  if(file.good()) nSize = long(readNSize);
484  data.reserve(nSize);
485  for(long i=0; i<nSize; ++i){
486  bitpit::genericIO::absorbASCII(file, id);
487  bitpit::genericIO::absorbASCII(file, data_T);
488  data.insert(id,data_T);
489  }
490  data.setDataLocation(n_loc);
491  data.setName(name);
492  }
493  file.close();
494  }else{
495  (*m_log) << "file not open --> exit" << std::endl;
496  throw std::runtime_error (m_name + " : cannot open " + m_filename + " requested");
497  }
498  }
499 
500 #if MIMMO_ENABLE_MPI
501  //if there are any other procs send data to them.
502  if(m_nprocs > 1) sendReadDataToAllProcs(data);
503 #endif
504 
505  if(refgeo == nullptr){
506  _setResult(data);
507  }else{
508  data.setGeometry(refgeo);
509  MimmoPiercedVector<T> data2 = data.resizeToCoherentDataIds();
510  _setResult(data2);
511  }
512 
513  return static_cast<IODataT<MimmoPiercedVector< T > >*>(m_result.get())->getData();
514 }
515 
516 
517 #if MIMMO_ENABLE_MPI
518 
523 template<typename T>
524 void
525 GenericInputMPVData::sendReadDataToAllProcs(MimmoPiercedVector<T> & dataTC){
526 
527  if(getRank() == 0){
528 
529  mimmo::OBinaryStream dataBuffer;
530  //create char output data buffer and reverse data into it.
531  dataBuffer << dataTC;
532  long dataBufferSize = dataBuffer.getSize();
533 
534  //Send data to all other procs
535  for (int sendRank=1; sendRank<m_nprocs; sendRank++){
536  MPI_Send(&dataBufferSize, 1, MPI_LONG, sendRank, 100, m_communicator);
537  MPI_Send(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, sendRank, 110, m_communicator);
538  }
539  //hey 0, your job is done.
540  }else{
541 
542  long dataBufferSize;
543  MPI_Recv(&dataBufferSize, 1, MPI_LONG, 0, 100, m_communicator, MPI_STATUS_IGNORE);
544 
545  mimmo::IBinaryStream dataBuffer(dataBufferSize);
546  MPI_Recv(dataBuffer.data(), dataBuffer.getSize(), MPI_CHAR, 0, 110, m_communicator, MPI_STATUS_IGNORE);
547 
548  dataBuffer >> dataTC;
549  }
550 }
551 
552 #endif
553 
554 }
std::fstream & ifstreamcsv(std::fstream &in, T &x)
MPVLocation
Define data location for the MimmoPiercedVector field.
void setName(std::string name)
mimmo custom derivation of bitpit OBinaryStream (see relative doc)
void setInput(T *data)
GENERICINPUT////////////////////////////////////////////////////////////////////////////.
T * getData()
Definition: IOData.hpp:115
MimmoPiercedVector is the basic data container for mimmo library.
std::fstream & ifstreamcsvend(std::fstream &in, T &x)
MimmoPiercedVector< T > * getResult()
void setDataLocation(MPVLocation loc)
mimmo custom derivation of bitpit IBinaryStream (see relative doc)
void setResult(T *data)
IODataT is the templated class of generic data derived from IOData base class.
Definition: IOData.hpp:79
MimmoSharedPointer< MimmoObject > getGeometry()