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)
 
 ~CsvIO () override
 
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)
 
 ~PtsIO () override
 
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...
 
- Public Member Functions inherited from Nektar::LibUtilities::FieldIOXml
 FieldIOXml (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Default constructor. More...
 
 ~FieldIOXml () override
 
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...
 
- 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...
 
const std::string & GetClassName () const
 
std::string GetFileEnding () const
 Helper function that determines default file extension. More...
 

Protected Member Functions

void v_ImportPtsFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField, DomainRangeShPtr &Range) override
 
std::string v_GetFileEnding () const override
 
- Protected Member Functions inherited from Nektar::LibUtilities::PtsIO
virtual void v_ImportPtsFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField, DomainRangeShPtr &Range=NullDomainRangeShPtr)
 
std::string v_GetFileEnding () const override
 
void SetUpFieldMetaData (const std::string outname)
 
- Protected Member Functions inherited from Nektar::LibUtilities::FieldIOXml
void v_Write (const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const bool backup=false) override
 Write an XML file to outFile given the field definitions fielddefs, field data fielddata and metadata fieldmetadatamap. 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) override
 Import an XML format file. More...
 
DataSourceSharedPtr v_ImportFieldMetaData (const std::string &filename, FieldMetaDataMap &fieldmetadatamap) override
 Import field metadata from filename and return the data source which wraps filename. More...
 
const std::string & v_GetClassName () const override
 Returns the class name. More...
 
- Protected Member Functions inherited from Nektar::LibUtilities::FieldIO
int CheckFieldDefinition (const FieldDefinitionsSharedPtr &fielddefs)
 Check field definitions for correctness and return storage size. More...
 
std::vector< unsigned int > GetNumberOfCoeffsPerElement (const FieldDefinitionsSharedPtr &fielddefs)
 Compute number of data points needed to store expansion inside each element. More...
 
std::string SetUpOutput (const std::string outname, bool perRank, bool backup=false)
 Set up the filesystem ready for output. More...
 
virtual void v_Write (const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup=false)=0
 Write out the field information to the file outFile. More...
 
virtual 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)=0
 Read field information from the file infilename. More...
 
virtual DataSourceSharedPtr v_ImportFieldMetaData (const std::string &filename, FieldMetaDataMap &fieldmetadatamap)=0
 Import the metadata from a field file. More...
 
virtual std::string v_GetFileEnding () const
 
virtual const std::string & v_GetClassName () const =0
 

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 52 of file CsvIO.h.

Constructor & Destructor Documentation

◆ CsvIO()

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

Definition at line 57 of file CsvIO.cpp.

58 : PtsIO(pComm, sharedFilesystem)
59{
60}
PtsIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
Definition: PtsIO.cpp:55

◆ ~CsvIO()

Nektar::LibUtilities::CsvIO::~CsvIO ( )
inlineoverride

Definition at line 58 of file CsvIO.h.

59 {
60 }

Member Function Documentation

◆ v_GetFileEnding()

std::string Nektar::LibUtilities::CsvIO::v_GetFileEnding ( ) const
overrideprotectedvirtual

Reimplemented from Nektar::LibUtilities::FieldIO.

Definition at line 278 of file CsvIO.cpp.

279{
280 return "csv";
281}

◆ v_ImportPtsFieldData()

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

Reimplemented from Nektar::LibUtilities::PtsIO.

Definition at line 111 of file CsvIO.cpp.

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

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

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

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