39 #include <boost/format.hpp> 45 namespace berrc = boost::system::errc;
49 namespace LibUtilities
63 :
FieldIO(pComm, sharedFilesystem)
88 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
89 std::vector<std::vector<NekDouble> > &fielddata,
93 double tm0 = 0.0, tm1 = 0.0;
94 if (
m_comm->TreatAsRankZero())
100 ASSERTL1(fielddefs.size() == fielddata.size(),
101 "Length of fielddefs and fielddata incompatible");
102 for (
int f = 0; f < fielddefs.size(); ++f)
105 "Fielddata vector must contain at least one value.");
108 fielddefs[f]->m_fields.size() *
110 "Invalid size of fielddata vector.");
116 std::string filename =
SetUpOutput(outFile,
true, backup);
121 TiXmlDeclaration *decl =
new TiXmlDeclaration(
"1.0",
"utf-8",
"");
122 doc.LinkEndChild(decl);
124 TiXmlElement *root =
new TiXmlElement(
"NEKTAR");
125 doc.LinkEndChild(root);
129 for (
int f = 0; f < fielddefs.size(); ++f)
133 TiXmlElement *elemTag =
new TiXmlElement(
"ELEMENTS");
134 root->LinkEndChild(elemTag);
137 std::string fieldsString;
139 std::stringstream fieldsStringStream;
141 for (std::vector<int>::size_type i = 0;
142 i < fielddefs[f]->m_fields.size();
147 fieldsStringStream <<
",";
149 fieldsStringStream << fielddefs[f]->m_fields[i];
152 fieldsString = fieldsStringStream.str();
154 elemTag->SetAttribute(
"FIELDS", fieldsString);
157 std::string shapeString;
159 std::stringstream shapeStringStream;
160 shapeStringStream <<
ShapeTypeMap[fielddefs[f]->m_shapeType];
161 if (fielddefs[f]->m_numHomogeneousDir == 1)
163 shapeStringStream <<
"-HomogenousExp1D";
165 else if (fielddefs[f]->m_numHomogeneousDir == 2)
167 shapeStringStream <<
"-HomogenousExp2D";
170 if (fielddefs[f]->m_homoStrips)
172 shapeStringStream <<
"-Strips";
175 shapeString = shapeStringStream.str();
177 elemTag->SetAttribute(
"SHAPE", shapeString);
180 std::string basisString;
182 std::stringstream basisStringStream;
184 for (std::vector<BasisType>::size_type i = 0;
185 i < fielddefs[f]->m_basis.size();
190 basisStringStream <<
",";
192 basisStringStream <<
BasisTypeMap[fielddefs[f]->m_basis[i]];
195 basisString = basisStringStream.str();
197 elemTag->SetAttribute(
"BASIS", basisString);
200 if (fielddefs[f]->m_numHomogeneousDir)
202 std::string homoLenString;
204 std::stringstream homoLenStringStream;
206 for (
int i = 0; i < fielddefs[f]->m_numHomogeneousDir; ++i)
209 homoLenStringStream <<
",";
211 << fielddefs[f]->m_homogeneousLengths[i];
214 homoLenString = homoLenStringStream.str();
216 elemTag->SetAttribute(
"HOMOGENEOUSLENGTHS", homoLenString);
220 if (fielddefs[f]->m_numHomogeneousDir)
222 if (fielddefs[f]->m_homogeneousYIDs.size() > 0)
224 std::string homoYIDsString;
226 std::stringstream homoYIDsStringStream;
228 for (
int i = 0; i < fielddefs[f]->m_homogeneousYIDs.size();
233 homoYIDsStringStream <<
",";
236 << fielddefs[f]->m_homogeneousYIDs[i];
239 homoYIDsString = homoYIDsStringStream.str();
241 elemTag->SetAttribute(
"HOMOGENEOUSYIDS", homoYIDsString);
244 if (fielddefs[f]->m_homogeneousZIDs.size() > 0)
246 std::string homoZIDsString;
248 std::stringstream homoZIDsStringStream;
250 for (
int i = 0; i < fielddefs[f]->m_homogeneousZIDs.size();
255 homoZIDsStringStream <<
",";
258 << fielddefs[f]->m_homogeneousZIDs[i];
261 homoZIDsString = homoZIDsStringStream.str();
263 elemTag->SetAttribute(
"HOMOGENEOUSZIDS", homoZIDsString);
266 if (fielddefs[f]->m_homogeneousSIDs.size() > 0)
268 std::string homoSIDsString;
270 std::stringstream homoSIDsStringStream;
272 for (
int i = 0; i < fielddefs[f]->m_homogeneousSIDs.size();
277 homoSIDsStringStream <<
",";
280 << fielddefs[f]->m_homogeneousSIDs[i];
283 homoSIDsString = homoSIDsStringStream.str();
285 elemTag->SetAttribute(
"HOMOGENEOUSSIDS", homoSIDsString);
290 std::string numModesString;
292 std::stringstream numModesStringStream;
294 if (fielddefs[f]->m_uniOrder)
296 numModesStringStream <<
"UNIORDER:";
299 for (std::vector<int>::size_type i = 0;
300 i < fielddefs[f]->m_basis.size();
305 numModesStringStream <<
",";
307 numModesStringStream << fielddefs[f]->m_numModes[i];
313 numModesStringStream <<
"MIXORDER:";
315 for (std::vector<int>::size_type i = 0;
316 i < fielddefs[f]->m_numModes.size();
321 numModesStringStream <<
",";
323 numModesStringStream << fielddefs[f]->m_numModes[i];
328 numModesString = numModesStringStream.str();
330 elemTag->SetAttribute(
"NUMMODESPERDIR", numModesString);
335 std::string idString;
337 std::stringstream idStringStream;
340 elemTag->SetAttribute(
"ID", idString);
341 elemTag->SetAttribute(
"COMPRESSED",
347 elemTag->SetAttribute(
"BITSIZE",
349 std::string base64string;
352 "Failed to compress field data.");
354 elemTag->LinkEndChild(
new TiXmlText(base64string));
356 doc.SaveFile(filename);
361 if (
m_comm->TreatAsRankZero())
364 std::cout <<
" (" << tm1 - tm0 <<
"s, XML)" << std::endl;
381 const std::string &outFile,
382 const std::vector<std::string> fileNames,
383 std::vector<std::vector<unsigned int> > &elementList,
387 TiXmlDeclaration *decl =
new TiXmlDeclaration(
"1.0",
"utf-8",
"");
388 doc.LinkEndChild(decl);
390 ASSERTL0(fileNames.size() == elementList.size(),
391 "Outfile names and list of elements ids does not match");
393 TiXmlElement *root =
new TiXmlElement(
"NEKTAR");
394 doc.LinkEndChild(root);
398 for (
int t = 0; t < fileNames.size(); ++t)
400 if (elementList[t].size())
402 TiXmlElement *elemIDs =
new TiXmlElement(
"Partition");
403 root->LinkEndChild(elemIDs);
405 elemIDs->SetAttribute(
"FileName", fileNames[t]);
409 elemIDs->LinkEndChild(
new TiXmlText(IDstr));
413 doc.SaveFile(outFile);
429 const std::string &inFile,
430 std::vector<std::string> &fileNames,
431 std::vector<std::vector<unsigned int> > &elementList,
434 boost::ignore_unused(fieldmetadatamap);
436 TiXmlDocument doc(inFile);
437 bool loadOkay = doc.LoadFile();
439 std::stringstream errstr;
440 errstr <<
"Unable to load file: " << inFile << std::endl;
441 errstr <<
"Reason: " << doc.ErrorDesc() << std::endl;
442 errstr <<
"Position: Line " << doc.ErrorRow() <<
", Column " 443 << doc.ErrorCol() << std::endl;
447 TiXmlHandle docHandle(&doc);
451 TiXmlElement *master = doc.FirstChildElement(
"NEKTAR");
452 ASSERTL0(master,
"Unable to find NEKTAR tag in file.");
455 std::string strPartition =
"Partition";
458 TiXmlElement *fldfileIDs = master->FirstChildElement(strPartition.c_str());
462 strPartition =
"MultipleFldFiles";
463 fldfileIDs = master->FirstChildElement(
"MultipleFldFiles");
466 "Unable to find 'Partition' or 'MultipleFldFiles' tag " 467 "within nektar tag.");
472 const char *attr = fldfileIDs->Attribute(
"FileName");
474 "'FileName' not provided as an attribute of '" + strPartition +
476 fileNames.push_back(std::string(attr));
478 const char *elementIDs = fldfileIDs->GetText();
479 ASSERTL0(elementIDs,
"Element IDs not specified.");
481 std::string elementIDsStr(elementIDs);
483 std::vector<unsigned int> idvec;
486 elementList.push_back(idvec);
488 fldfileIDs = fldfileIDs->NextSiblingElement(strPartition.c_str());
505 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
506 std::vector<std::vector<NekDouble> > &fielddata,
510 std::string infile = infilename;
512 fs::path pinfilename(infilename);
516 if (fs::is_directory(pinfilename))
518 fs::path infofile(
"Info.xml");
519 fs::path fullpath = pinfilename / infofile;
522 std::vector<std::string> filenames;
523 std::vector<std::vector<unsigned int> > elementIDs_OnPartitions;
526 infile, filenames, elementIDs_OnPartitions, fieldinfomap);
533 for (
int i = 0; i < filenames.size(); ++i)
535 fs::path pfilename(filenames[i]);
536 fullpath = pinfilename / pfilename;
549 std::map<int, std::vector<int> > FileIDs;
550 std::set<int> LoadFile;
552 for (i = 0; i < elementIDs_OnPartitions.size(); ++i)
554 for (j = 0; j < elementIDs_OnPartitions[i].size(); ++j)
556 FileIDs[elementIDs_OnPartitions[i][j]].push_back(i);
560 for (i = 0; i < ElementIDs.num_elements(); ++i)
562 auto it = FileIDs.find(ElementIDs[i]);
563 if (it != FileIDs.end())
565 for (j = 0; j < it->second.size(); ++j)
567 LoadFile.insert(it->second[j]);
572 for (
auto &iter : LoadFile)
574 fs::path pfilename(filenames[iter]);
575 fullpath = pinfilename / pfilename;
610 TiXmlElement *metadata = 0;
611 TiXmlElement *master = 0;
614 master = xml->
Get().FirstChildElement(
"NEKTAR");
615 ASSERTL0(master,
"Unable to find NEKTAR tag in file.");
616 std::string strLoop =
"NEKTAR";
620 metadata = master->FirstChildElement(
"FIELDMETADATA");
623 TiXmlElement *param = metadata->FirstChildElement(
"P");
627 TiXmlAttribute *paramAttr = param->FirstAttribute();
628 std::string attrName(paramAttr->Name());
629 std::string paramString;
631 if (attrName ==
"PARAM")
633 paramString.insert(0, paramAttr->Value());
638 "PARAM not provided as an attribute in " 639 "FIELDMETADATA section");
643 std::string paramBodyStr;
645 TiXmlNode *paramBody = param->FirstChild();
647 paramBodyStr += paramBody->ToText()->Value();
649 fieldmetadatamap[paramString] = paramBodyStr;
650 param = param->NextSiblingElement(
"P");
655 metadata = master->FirstChildElement(
"Metadata");
658 TiXmlElement *param = metadata->FirstChildElement();
662 std::string paramString = param->Value();
663 if (paramString !=
"Provenance")
666 if (param->NoChildren())
668 fieldmetadatamap[paramString] =
"";
672 TiXmlNode *paramBody = param->FirstChild();
673 std::string paramBodyStr = paramBody->ToText()->Value();
674 fieldmetadatamap[paramString] = paramBodyStr;
677 param = param->NextSiblingElement();
698 const std::string &outname,
699 const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
702 ASSERTL0(!outname.empty(),
"Empty path given to SetUpFieldMetaData()");
704 unsigned int nprocs =
m_comm->GetSize();
705 unsigned int rank =
m_comm->GetRank();
707 fs::path specPath(outname);
712 std::vector<size_t> elmtnums(nprocs, 0);
713 std::vector<unsigned int> idlist;
714 for (
size_t i = 0; i < fielddefs.size(); ++i)
716 elmtnums[rank] += fielddefs[i]->m_elementIDs.size();
717 idlist.insert(idlist.end(),
718 fielddefs[i]->m_elementIDs.begin(),
719 fielddefs[i]->m_elementIDs.end());
727 std::vector<std::vector<unsigned int> > ElementIDs(nprocs);
730 ElementIDs[0] = idlist;
731 for (
size_t i = 1; i < nprocs; ++i)
733 std::vector<unsigned int> tmp(elmtnums[i]);
739 std::vector<std::string> filenames;
740 for (
unsigned int i = 0; i < nprocs; ++i)
744 filenames.push_back(pad.str());
748 std::string infofile =
770 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
775 TiXmlElement *master =
778 master = xml->
Get().FirstChildElement(
"NEKTAR");
779 ASSERTL0(master,
"Unable to find NEKTAR tag in file.");
780 std::string strLoop =
"NEKTAR";
781 TiXmlElement *loopXml = master;
783 TiXmlElement *expansionTypes;
786 expansionTypes = master->FirstChildElement(
"EXPANSIONS");
787 ASSERTL0(expansionTypes,
"Unable to find EXPANSIONS tag in file.");
788 loopXml = expansionTypes;
789 strLoop =
"EXPANSIONS";
795 TiXmlElement *element = loopXml->FirstChildElement(
"ELEMENTS");
800 std::string idString;
801 std::string shapeString;
802 std::string basisString;
803 std::string homoLengthsString;
804 std::string homoSIDsString;
805 std::string homoZIDsString;
806 std::string homoYIDsString;
807 std::string numModesString;
808 std::string numPointsString;
809 std::string fieldsString;
810 std::string pointsString;
811 bool pointDef =
false;
812 bool numPointDef =
false;
813 TiXmlAttribute *attr = element->FirstAttribute();
816 std::string attrName(attr->Name());
817 if (attrName ==
"FIELDS")
819 fieldsString.insert(0, attr->Value());
821 else if (attrName ==
"SHAPE")
823 shapeString.insert(0, attr->Value());
825 else if (attrName ==
"BASIS")
827 basisString.insert(0, attr->Value());
829 else if (attrName ==
"HOMOGENEOUSLENGTHS")
831 homoLengthsString.insert(0, attr->Value());
833 else if (attrName ==
"HOMOGENEOUSSIDS")
835 homoSIDsString.insert(0, attr->Value());
837 else if (attrName ==
"HOMOGENEOUSZIDS")
839 homoZIDsString.insert(0, attr->Value());
841 else if (attrName ==
"HOMOGENEOUSYIDS")
843 homoYIDsString.insert(0, attr->Value());
845 else if (attrName ==
"NUMMODESPERDIR")
847 numModesString.insert(0, attr->Value());
849 else if (attrName ==
"ID")
851 idString.insert(0, attr->Value());
853 else if (attrName ==
"POINTSTYPE")
855 pointsString.insert(0, attr->Value());
858 else if (attrName ==
"NUMPOINTSPERDIR")
860 numPointsString.insert(0, attr->Value());
863 else if (attrName ==
"COMPRESSED")
867 "Compressed formats do not " 868 "match. Expected: " +
870 " but got " + std::string(attr->Value()));
872 else if (attrName ==
"BITSIZE")
880 std::string errstr(
"Unknown attribute: ");
891 if (shapeString.find(
"Strips") != std::string::npos)
901 if ((loc = shapeString.find_first_of(
"-")) != std::string::npos)
903 if (shapeString.find(
"Exp1D") != std::string::npos)
912 shapeString.erase(loc, shapeString.length());
916 std::vector<unsigned int> elementIds;
920 ASSERTL0(valid,
"Unable to correctly parse the element ids.");
937 std::string(
"Unable to correctly parse the shape type: ")
942 std::vector<std::string> basisStrings;
943 std::vector<BasisType> basis;
945 ASSERTL0(valid,
"Unable to correctly parse the basis types.");
946 for (std::vector<std::string>::size_type i = 0;
947 i < basisStrings.size();
962 std::string(
"Unable to correctly parse the basis type: ")
963 .append(basisStrings[i])
968 std::vector<NekDouble> homoLengths;
973 ASSERTL0(valid,
"Unable to correctly parse the number of " 974 "homogeneous lengths.");
978 std::vector<unsigned int> homoSIDs;
983 "Unable to correctly parse homogeneous strips IDs.");
987 std::vector<unsigned int> homoZIDs;
988 std::vector<unsigned int> homoYIDs;
995 "Unable to correctly parse homogeneous planes IDs.");
1002 ASSERTL0(valid,
"Unable to correctly parse homogeneous lines " 1003 "IDs in z-direction.");
1006 ASSERTL0(valid,
"Unable to correctly parse homogeneous lines " 1007 "IDs in y-direction.");
1011 std::vector<PointsType> points;
1015 std::vector<std::string> pointsStrings;
1017 ASSERTL0(valid,
"Unable to correctly parse the points types.");
1018 for (std::vector<std::string>::size_type i = 0;
1019 i < pointsStrings.size();
1035 "Unable to correctly parse the points type: ")
1036 .append(pointsStrings[i])
1042 std::vector<unsigned int> numModes;
1043 bool UniOrder =
false;
1045 if (strstr(numModesString.c_str(),
"UNIORDER:"))
1051 numModesString.substr(9), numModes);
1052 ASSERTL0(valid,
"Unable to correctly parse the number of modes.");
1055 std::vector<unsigned int> numPoints;
1060 "Unable to correctly parse the number of points.");
1064 std::vector<std::string> Fields;
1066 ASSERTL0(valid,
"Unable to correctly parse the number of fields.");
1086 fielddefs.push_back(fielddef);
1088 element = element->NextSiblingElement(
"ELEMENTS");
1090 loopXml = loopXml->NextSiblingElement(strLoop);
1103 const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
1104 std::vector<std::vector<NekDouble> > &fielddata)
1110 TiXmlElement *master =
1113 master = xml->
Get().FirstChildElement(
"NEKTAR");
1114 ASSERTL0(master,
"Unable to find NEKTAR tag in file.");
1119 TiXmlElement *element = master->FirstChildElement(
"ELEMENTS");
1120 ASSERTL0(element,
"Unable to find ELEMENTS tag within nektar tag.");
1124 TiXmlNode *elementChild = element->FirstChild();
1126 "Unable to extract the data from the element tag.");
1127 std::string elementStr;
1128 while (elementChild)
1130 if (elementChild->Type() == TiXmlNode::TINYXML_TEXT)
1132 elementStr += elementChild->ToText()->ValueStr();
1134 elementChild = elementChild->NextSibling();
1137 std::vector<NekDouble> elementFieldData;
1140 const char *CompressStr = element->Attribute(
"COMPRESSED");
1145 "Compressed formats do not match. " 1148 " but got " + std::string(CompressStr));
1152 elementStr, elementFieldData),
1153 "Failed to decompress field data.");
1154 fielddata.push_back(elementFieldData);
1158 fielddata[cntdumps].size() ==
1159 datasize * fielddefs[cntdumps]->m_fields.size(),
1160 "Input data is not the same length as header infoarmation");
1164 element = element->NextSiblingElement(
"ELEMENTS");
1166 master = master->NextSiblingElement(
"NEKTAR");
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
std::shared_ptr< XmlDataSource > XmlDataSourceSharedPtr
const char *const BasisTypeMap[]
const std::string kPointsTypeStr[]
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
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.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
TiXmlDocument & Get()
Return the TinyXML document of this source.
virtual 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)
Write an XML file to outFile given the field definitions fielddefs, field data fielddata and metadata...
static std::string className
Name of class.
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
std::map< std::string, std::string > FieldMetaDataMap
int CheckFieldDefinition(const FieldDefinitionsSharedPtr &fielddefs)
Check field definitions for correctness and return storage size.
const char *const ShapeTypeMap[]
std::string GetCompressString(void)
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.
std::shared_ptr< DataSource > DataSourceSharedPtr
static DataSourceSharedPtr create(const std::string &fn)
Create a new XML data source based on the filename.
#define WARNINGL0(condition, msg)
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
DataSourceSharedPtr ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import the metadata from a field file.
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
void ImportFieldData(DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
Import field data from a target file.
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.
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
static std::string GenerateSeqString(const std::vector< T > &v)
Generate a compressed comma-separated string representation of a vector of unsigned integers...
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
int ZlibEncodeToBase64Str(std::vector< T > &in, std::string &out64)
std::shared_ptr< XmlTagWriter > XmlTagWriterSharedPtr
std::string GetBitSizeStr(void)
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
std::shared_ptr< FieldDefinitions > FieldDefinitionsSharedPtr
Class for operating on Nektar++ input/output files.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
void SetUpFieldMetaData(const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
Set up field meta data map.
void ImportFieldDefs(DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
Import field definitions from the target file.
static Array< OneD, int > NullInt1DArray
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
static void AddInfoTag(TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
Add provenance information to the field metadata map.
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import field metadata from filename and return the data source which wraps filename.
int ZlibDecodeFromBase64Str(std::string &in64, std::vector< T > &out)
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector. ...