Nektar++
Public Member Functions | Protected Member Functions | List of all members
Nektar::LibUtilities::CsvIO Class Reference

#include <CsvIO.h>

Inheritance diagram for Nektar::LibUtilities::CsvIO:
[legend]

Public Member Functions

 CsvIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
 
virtual ~CsvIO ()
 
void Write (const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
 Save a pts field to a file. More...
 
- Public Member Functions inherited from Nektar::LibUtilities::PtsIO
 PtsIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
 
virtual ~PtsIO ()
 
void Import (const std::string &inFile, PtsFieldSharedPtr &ptsField, FieldMetaDataMap &fieldmetadatamap=NullFieldMetaDataMap, DomainRangeShPtr &Range=NullDomainRangeShPtr)
 Import a pts field from file. More...
 
void Write (const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
 Save a pts field to a file. More...
 
void ImportFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField, DomainRangeShPtr &Range=NullDomainRangeShPtr)
 
- Public Member Functions inherited from Nektar::LibUtilities::FieldIOXml
 FieldIOXml (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Default constructor. More...
 
virtual ~FieldIOXml ()
 
void ImportFieldDefs (DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
 Import field definitions from the target file. More...
 
void ImportFieldData (DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
 Import field data from a target file. More...
 
void WriteMultiFldFileIDs (const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int > > &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
 Write out a file containing element ID to partition mapping. More...
 
void SetUpFieldMetaData (const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
 Set up field meta data map. More...
 
void ImportMultiFldFileIDs (const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int > > &elementList, FieldMetaDataMap &fieldmetadatamap)
 Read file containing element ID to partition mapping. More...
 
void v_Import (const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata=NullVectorNekDoubleVector, FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const Array< OneD, int > &ElementIDs=NullInt1DArray)
 Import an XML format file. More...
 
virtual const std::string & GetClassName () const
 Returns the class name. More...
 
- Public Member Functions inherited from Nektar::LibUtilities::FieldIO
 FieldIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Constructor for FieldIO base class. More...
 
virtual ~FieldIO ()
 
void Write (const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const bool backup=false)
 Write out the field information to the file outFile. More...
 
void Import (const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata=NullVectorNekDoubleVector, FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const Array< OneD, int > &ElementIDs=NullInt1DArray)
 Read field information from the file infilename. More...
 
DataSourceSharedPtr ImportFieldMetaData (const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
 Import the metadata from a field file. More...
 

Protected Member Functions

virtual void v_ImportFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField, DomainRangeShPtr &Range)
 
virtual std::string GetFileEnding () const
 Helper function that determines default file extension. More...
 
- Protected Member Functions inherited from Nektar::LibUtilities::PtsIO
void SetUpFieldMetaData (const std::string outname)
 
- Protected Member Functions inherited from Nektar::LibUtilities::FieldIO
int CheckFieldDefinition (const FieldDefinitionsSharedPtr &fielddefs)
 Check field definitions for correctness and return storage size. More...
 
std::string SetUpOutput (const std::string outname, bool perRank, bool backup=false)
 Set up the filesystem ready for output. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Nektar::LibUtilities::FieldIOXml
static FieldIOSharedPtr create (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Creates an instance of this class. More...
 
- Static Public Member Functions inherited from Nektar::LibUtilities::FieldIO
static const std::string GetFileType (const std::string &filename, CommSharedPtr comm)
 Determine file type of given input file. More...
 
static std::shared_ptr< FieldIOCreateDefault (const LibUtilities::SessionReaderSharedPtr session)
 Returns an object for the default FieldIO method. More...
 
static std::shared_ptr< FieldIOCreateForFile (const LibUtilities::SessionReaderSharedPtr session, const std::string &filename)
 Construct a FieldIO object for a given input filename. More...
 
static void AddInfoTag (TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
 Add provenance information to the field metadata map. More...
 
- Static Public Attributes inherited from Nektar::LibUtilities::FieldIOXml
static std::string className
 Name of class. More...
 
- Protected Attributes inherited from Nektar::LibUtilities::FieldIO
LibUtilities::CommSharedPtr m_comm
 Communicator to use when writing parallel format. More...
 
bool m_sharedFilesystem
 Boolean dictating whether we are on a shared filesystem. More...
 

Detailed Description

Definition at line 54 of file CsvIO.h.

Constructor & Destructor Documentation

◆ CsvIO()

Nektar::LibUtilities::CsvIO::CsvIO ( LibUtilities::CommSharedPtr  pComm,
bool  sharedFilesystem = false 
)

Definition at line 60 of file CsvIO.cpp.

61  : PtsIO(pComm, sharedFilesystem)
62 {
63 }
PtsIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
Definition: PtsIO.cpp:60

◆ ~CsvIO()

virtual Nektar::LibUtilities::CsvIO::~CsvIO ( )
inlinevirtual

Definition at line 60 of file CsvIO.h.

61  {
62  }

Member Function Documentation

◆ GetFileEnding()

virtual std::string Nektar::LibUtilities::CsvIO::GetFileEnding ( ) const
inlineprotectedvirtual

Helper function that determines default file extension.

Reimplemented from Nektar::LibUtilities::PtsIO.

Definition at line 81 of file CsvIO.h.

82  {
83  return "csv";
84  };

◆ v_ImportFieldData()

void Nektar::LibUtilities::CsvIO::v_ImportFieldData ( const std::string  inFile,
PtsFieldSharedPtr ptsField,
DomainRangeShPtr Range 
)
protectedvirtual

Reimplemented from Nektar::LibUtilities::PtsIO.

Definition at line 112 of file CsvIO.cpp.

113 {
114  std::stringstream errstr;
115  errstr << "Unable to load file: " << inFile << std::endl;
116  std::ifstream in(inFile.c_str());
117  ASSERTL0(in.is_open(), errstr.str());
118 
119  std::string line;
120  std::getline(in, line);
121  boost::erase_first(line, "#");
122 
123  std::vector<std::string> fieldNames;
124  bool valid = ParseUtils::GenerateVector(line, fieldNames);
125  ASSERTL0(valid, "Unable to process list of fields from line: " + line);
126 
127  int dim = 0;
128  for (auto &it : fieldNames)
129  {
130  if (it == "x" || it == "y" || it == "z")
131  {
132  dim++;
133  }
134  }
135 
136  ASSERTL0(dim,"Failed to find a paramater labelled \"x\",\"y\" or \"z\" "
137  "in file" + inFile + ". Is the coordinated labelled something else?");
138 
139  size_t totvars = fieldNames.size();
140  std::vector<std::string> dimNames = {"x", "y", "z"};
141  Array<OneD, int> loc_coord(dim);
142  for (int i = 0; i < dim; ++i)
143  {
144  auto p = std::find(fieldNames.begin(), fieldNames.end(), dimNames[i]);
145  if (p != fieldNames.end())
146  {
147  auto j = std::distance(fieldNames.begin(), p);
148 
149  loc_coord[i] = j;
150  }
151  }
152 
153  std::vector<NekDouble> ptsSerial;
154  typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
155  Tokenizer tok(line);
156  while (getline(in, line))
157  {
158  tok.assign(line);
159 
160  ASSERTL0(std::distance(tok.begin(), tok.end()) ==
161  std::iterator_traits<Tokenizer::iterator>::difference_type(totvars),
162  "wrong number of columns in line: " + line);
163 
164  bool ReadValue = true;
165 
166  if(Range != NullDomainRangeShPtr)
167  {
168  int cnt = 0;
169  for (auto &it : tok)
170  {
171  for(int j = 0; j < dim; ++j)
172  {
173  if(cnt == loc_coord[j])
174  {
175  NekDouble CoordVal = boost::lexical_cast<NekDouble>(
176  boost::trim_copy(std::string(it)));
177  switch(j)
178  {
179  case 0:
180  {
181  if((CoordVal < Range->m_xmin) || (CoordVal > Range->m_xmax))
182  {
183  ReadValue = false;
184  }
185  }
186  break;
187  case 1:
188  {
189  if((CoordVal < Range->m_ymin) || (CoordVal > Range->m_ymax))
190  {
191  ReadValue = false;
192  }
193  }
194  break;
195  case 2:
196  {
197  if((CoordVal < Range->m_zmin) || (CoordVal > Range->m_zmax))
198  {
199  ReadValue = false;
200  }
201  }
202  }
203  }
204  }
205  cnt++;
206  }
207  }
208  if(ReadValue)
209  {
210  for (auto &it : tok)
211  {
212  try
213  {
214  ptsSerial.push_back(
215  boost::lexical_cast<NekDouble>(
216  boost::trim_copy(std::string(it))));
217  }
218  catch(const boost::bad_lexical_cast &)
219  {
220  NEKERROR(ErrorUtil::efatal, "could not convert line: " + line);
221  }
222  }
223  }
224  }
225 
226  size_t npts = ptsSerial.size() / totvars;
227 
228  Array<OneD, Array<OneD, NekDouble> > pts(totvars);
229  for (size_t i = 0; i < totvars; ++i)
230  {
231  pts[i] = Array<OneD, NekDouble>(npts);
232  }
233 
234  for (size_t i = 0; i < npts; ++i)
235  {
236  for (size_t j = 0; j < totvars; ++j)
237  {
238  pts[j][i] = ptsSerial[i * totvars + j];
239  }
240  }
241 
242  // reorder pts to make x,y,z the first columns
243  for (int i = 0; i < dim; ++i)
244  {
245  auto p = std::find(fieldNames.begin(), fieldNames.end(), dimNames[i]);
246  if (p != fieldNames.end())
247  {
248  auto j = std::distance(fieldNames.begin(), p);
249 
250  if (i == j)
251  {
252  continue;
253  }
254 
255  Array<OneD, NekDouble> tmp = pts[i];
256  pts[i] = pts[j];
257  pts[j] = tmp;
258 
259  std::string tmp2 = fieldNames[i];
260  fieldNames[i] = fieldNames[j];
261  fieldNames[j] = tmp2;
262  }
263  }
264  fieldNames.erase(fieldNames.begin(), fieldNames.begin() + dim);
265 
266  ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts);
267 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:135
static DomainRangeShPtr NullDomainRangeShPtr
Definition: DomainRange.h:62
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
double NekDouble

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), ASSERTL0, Nektar::ErrorUtil::efatal, Nektar::StdRegions::find(), Nektar::ParseUtils::GenerateVector(), NEKERROR, Nektar::LibUtilities::NullDomainRangeShPtr, and CellMLToNektar.cellml_metadata::p.

◆ Write()

void Nektar::LibUtilities::CsvIO::Write ( const std::string &  outFile,
const PtsFieldSharedPtr ptsField,
const bool  backup = false 
)

Save a pts field to a file.

Parameters
outFilefilename of the file
ptsFieldthe pts field

Definition at line 71 of file CsvIO.cpp.

74 {
75  size_t nTotvars = ptsField->GetNFields() + ptsField->GetDim();
76  size_t np = ptsField->GetNpoints();
77 
78  std::string filename = SetUpOutput(outFile, true, backup);
79  SetUpFieldMetaData(outFile);
80 
81  std::ofstream ptsFile;
82  ptsFile.open(filename.c_str());
83 
84  std::vector<std::string> xyz;
85  xyz.push_back("x");
86  xyz.push_back("y");
87  xyz.push_back("z");
88  xyz.resize(ptsField->GetDim());
89 
90  std::string fn = boost::algorithm::join(xyz, ",");
91  ptsFile << "# " << fn << ",";
92  fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
93  ptsFile << fn;
94  ptsFile << std::endl;
95 
96  Array<OneD, Array<OneD, NekDouble> > pts;
97  ptsField->GetPts(pts);
98  for (size_t i = 0; i < np; ++i)
99  {
100  ptsFile << pts[0][i];
101  for (size_t j = 1; j < nTotvars; ++j)
102  {
103  ptsFile << "," << pts[j][i];
104  }
105  ptsFile << std::endl;
106  }
107 
108  ptsFile.close();
109 }
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
Definition: FieldIO.cpp:410
void SetUpFieldMetaData(const std::string outname)
Definition: PtsIO.cpp:311

References Nektar::LibUtilities::PtsIO::SetUpFieldMetaData(), and Nektar::LibUtilities::FieldIO::SetUpOutput().

Referenced by Nektar::SolverUtils::CouplingCwipi::DumpRawFields(), and Nektar::FieldUtils::OutputPts::OutputFromPts().