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

#include <PtsIO.h>

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

Public Member Functions

 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

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 59 of file PtsIO.h.

Constructor & Destructor Documentation

◆ PtsIO()

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

Definition at line 55 of file PtsIO.cpp.

56 : FieldIOXml(pComm, sharedFilesystem)
57{
58}
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
Definition: FieldIOXml.cpp:58

◆ ~PtsIO()

Nektar::LibUtilities::PtsIO::~PtsIO ( )
inlineoverride

Definition at line 65 of file PtsIO.h.

66 {
67 }

Member Function Documentation

◆ Import()

void Nektar::LibUtilities::PtsIO::Import ( const std::string &  inFile,
PtsFieldSharedPtr ptsField,
FieldMetaDataMap fieldmetadatamap = NullFieldMetaDataMap,
DomainRangeShPtr Range = NullDomainRangeShPtr 
)

Import a pts field from file.

Parameters
inFilefilename of the file to read
ptsFieldthe resulting pts field.

Definition at line 66 of file PtsIO.cpp.

68{
69 std::string infile = inFile;
70
71 fs::path pinfilename(infile);
72
73 // check to see that infile is a directory
74 if (fs::is_directory(pinfilename))
75 {
76 fs::path infofile("Info.xml");
77 fs::path fullpath = pinfilename / infofile;
78 infile = PortablePath(fullpath);
79
80 std::vector<std::string> filenames;
81 std::vector<std::vector<unsigned int>> elementIDs_OnPartitions;
82
83 ImportMultiFldFileIDs(infile, filenames, elementIDs_OnPartitions,
84 fieldmetadatamap);
85
86 // Load metadata
87 ImportFieldMetaData(infile, fieldmetadatamap);
88
89 if (filenames.size() == m_comm->GetSpaceComm()->GetSize())
90 {
91 // only load the file that matches this rank
92 filenames.clear();
93 boost::format pad("P%1$07d.%2$s");
94 pad % m_comm->GetSpaceComm()->GetRank() % GetFileEnding();
95 filenames.push_back(pad.str());
96 }
97
98 for (int i = 0; i < filenames.size(); ++i)
99 {
100 fs::path pfilename(filenames[i]);
101 fullpath = pinfilename / pfilename;
102 string fname = PortablePath(fullpath);
103
104 if (i == 0)
105 {
106 v_ImportPtsFieldData(fname, ptsField, Range);
107 }
108 else
109 {
111 v_ImportPtsFieldData(fname, newPtsField, Range);
112 Array<OneD, Array<OneD, NekDouble>> pts;
113 newPtsField->GetPts(pts);
114 ptsField->AddPoints(pts);
115 }
116 }
117 }
118 else
119 {
120 v_ImportPtsFieldData(infile, ptsField, Range);
121 }
122}
DataSourceSharedPtr ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import the metadata from a field file.
Definition: FieldIO.h:371
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:280
std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: FieldIO.h:273
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.
Definition: FieldIOXml.cpp:419
virtual void v_ImportPtsFieldData(const std::string inFile, PtsFieldSharedPtr &ptsField, DomainRangeShPtr &Range=NullDomainRangeShPtr)
Definition: PtsIO.cpp:173
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:56
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:184

References CellMLToNektar.pycml::format, Nektar::LibUtilities::FieldIO::GetFileEnding(), Nektar::LibUtilities::FieldIO::ImportFieldMetaData(), Nektar::LibUtilities::FieldIOXml::ImportMultiFldFileIDs(), Nektar::LibUtilities::FieldIO::m_comm, Nektar::LibUtilities::PortablePath(), and v_ImportPtsFieldData().

Referenced by Nektar::SolverUtils::SessionFunction::EvaluatePts(), and Nektar::FieldUtils::ProcessPointDataToFld::v_Process().

◆ SetUpFieldMetaData()

void Nektar::LibUtilities::PtsIO::SetUpFieldMetaData ( const std::string  outname)
protected

Definition at line 268 of file PtsIO.cpp.

269{
270 ASSERTL0(!outname.empty(), "Empty path given to SetUpFieldMetaData()");
271
272 int nprocs = m_comm->GetSpaceComm()->GetSize();
273 int rank = m_comm->GetSpaceComm()->GetRank();
274
275 fs::path specPath(outname);
276
277 // Collate per-process element lists on root process to generate
278 // the info file.
279 if (rank == 0)
280 {
281 // Set up output names
282 std::vector<std::string> filenames;
283 std::vector<std::vector<unsigned int>> ElementIDs;
284 for (int i = 0; i < nprocs; ++i)
285 {
286 boost::format pad("P%1$07d.%2$s");
287 pad % i % GetFileEnding();
288 filenames.push_back(pad.str());
289
290 std::vector<unsigned int> tmp;
291 tmp.push_back(0);
292 ElementIDs.push_back(tmp);
293 }
294
295 // Write the Info.xml file
296 string infofile =
297 LibUtilities::PortablePath(specPath / fs::path("Info.xml"));
298
299 cout << "Writing: " << specPath << endl;
300
301 const FieldMetaDataMap fieldmetadatamap;
302 WriteMultiFldFileIDs(infofile, filenames, ElementIDs, fieldmetadatamap);
303 }
304}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
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.
Definition: FieldIOXml.cpp:372
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:50

References ASSERTL0, CellMLToNektar.pycml::format, Nektar::LibUtilities::FieldIO::GetFileEnding(), Nektar::LibUtilities::FieldIO::m_comm, Nektar::LibUtilities::PortablePath(), and Nektar::LibUtilities::FieldIOXml::WriteMultiFldFileIDs().

Referenced by Nektar::LibUtilities::CsvIO::Write(), and Write().

◆ v_GetFileEnding()

std::string Nektar::LibUtilities::PtsIO::v_GetFileEnding ( ) const
inlineoverrideprotectedvirtual

Reimplemented from Nektar::LibUtilities::FieldIO.

Definition at line 83 of file PtsIO.h.

84 {
85 return "pts";
86 };

◆ v_ImportPtsFieldData()

void Nektar::LibUtilities::PtsIO::v_ImportPtsFieldData ( const std::string  inFile,
PtsFieldSharedPtr ptsField,
DomainRangeShPtr Range = NullDomainRangeShPtr 
)
protectedvirtual

Reimplemented in Nektar::LibUtilities::CsvIO.

Definition at line 173 of file PtsIO.cpp.

176{
177 TiXmlDocument docInput(inFile);
178 bool loadOkay1 = docInput.LoadFile();
179
180 std::stringstream errstr;
181 errstr << "Unable to load file: " << inFile << std::endl;
182 errstr << "Reason: " << docInput.ErrorDesc() << std::endl;
183 errstr << "Position: Line " << docInput.ErrorRow() << ", Column "
184 << docInput.ErrorCol() << std::endl;
185 ASSERTL0(loadOkay1, errstr.str());
186
187 TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
188 TiXmlElement *points = nektar->FirstChildElement("POINTS");
189 int dim;
190 int err = points->QueryIntAttribute("DIM", &dim);
191
192 ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute DIM.");
193
194 std::string fields = points->Attribute("FIELDS");
195
196 vector<string> fieldNames;
197 if (!fields.empty())
198 {
199 bool valid = ParseUtils::GenerateVector(fields, fieldNames);
200 ASSERTL0(
201 valid,
202 "Unable to process list of field variable in FIELDS attribute: " +
203 fields);
204 }
205
206 map<PtsInfo, int> ptsInfo = NullPtsInfoMap;
207
208 const char *ptinfo = points->Attribute("PTSINFO");
209 if (ptinfo && boost::iequals(ptinfo, "EquiSpaced"))
210 {
211 ptsInfo[eIsEquiSpacedData] = 1;
212 }
213
214 int np;
215 err = points->QueryIntAttribute("PTSPERELMTEDGE", &np);
216 if (err == TIXML_SUCCESS)
217 {
218 ptsInfo[ePtsPerElmtEdge] = np;
219 }
220
221 size_t nfields = fieldNames.size();
222 size_t totvars = dim + nfields;
223
224 TiXmlNode *pointsBody = points->FirstChild();
225
226 std::istringstream pointsDataStrm(pointsBody->ToText()->Value());
227
228 vector<NekDouble> ptsSerial;
229 Array<OneD, Array<OneD, NekDouble>> pts(totvars);
230
231 try
232 {
233 NekDouble ptsStream;
234 while (!pointsDataStrm.fail())
235 {
236 pointsDataStrm >> ptsStream;
237
238 ptsSerial.push_back(ptsStream);
239 }
240 }
241 catch (...)
242 {
243 NEKERROR(ErrorUtil::efatal, "Unable to read Points data.");
244 }
245
246 size_t npts = ptsSerial.size() / totvars;
247
248 for (size_t i = 0; i < totvars; ++i)
249 {
250 pts[i] = Array<OneD, NekDouble>(npts);
251 }
252
253 for (size_t i = 0; i < npts; ++i)
254 {
255 for (size_t j = 0; j < totvars; ++j)
256 {
257 pts[j][i] = ptsSerial[i * totvars + j];
258 }
259 }
260
261 ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts,
262 ptsInfo);
263}
#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 std::map< PtsInfo, int > NullPtsInfoMap
Definition: PtsField.h:66
double NekDouble

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), ASSERTL0, Nektar::ErrorUtil::efatal, Nektar::LibUtilities::eIsEquiSpacedData, Nektar::LibUtilities::ePtsPerElmtEdge, Nektar::ParseUtils::GenerateVector(), NEKERROR, and Nektar::LibUtilities::NullPtsInfoMap.

Referenced by Import().

◆ Write()

void Nektar::LibUtilities::PtsIO::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 130 of file PtsIO.cpp.

133{
134 size_t nTotvars = ptsField->GetNFields() + ptsField->GetDim();
135 size_t np = ptsField->GetNpoints();
136
137 std::string filename = SetUpOutput(outFile, true, backup);
138 SetUpFieldMetaData(outFile);
139
140 // until tinyxml gains support for line break, write the xml manually
141 std::ofstream ptsFile;
142 ptsFile.open(filename.c_str());
143
144 ptsFile << R"(<?xml version="1.0" encoding="utf-8" ?>)" << endl;
145 ptsFile << "<NEKTAR>" << endl;
146 ptsFile << " <POINTS ";
147 ptsFile << "DIM=\"" << ptsField->GetDim() << "\" ";
148 string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
149 ptsFile << "FIELDS=\"" << fn << "\" ";
150 ptsFile << ">" << endl;
151
152 Array<OneD, Array<OneD, NekDouble>> pts;
153 ptsField->GetPts(pts);
154 for (size_t i = 0; i < np; ++i)
155 {
156 ptsFile << " ";
157 ptsFile << pts[0][i];
158 for (size_t j = 1; j < nTotvars; ++j)
159 {
160 ptsFile << " " << pts[j][i];
161 }
162 ptsFile << endl;
163 }
164 ptsFile << " </POINTS>" << endl;
165 ptsFile << "</NEKTAR>" << endl;
166
167 ptsFile.close();
168}
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 SetUpFieldMetaData(), and Nektar::LibUtilities::FieldIO::SetUpOutput().

Referenced by Nektar::FieldUtils::OutputPts::v_OutputFromPts(), and Nektar::SolverUtils::CouplingFile::v_Send().