Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CombinePartitions.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <sstream>
4 
5 #include <tinyxml.h>
6 
8 
9 int main(int argc, char *argv[])
10 {
11  bool DeleteFiles = false;
12  if (argc < 3)
13  {
14  std::cout << "Usage: CombinePartitions [DeleteFile] nproc outfile"
15  << std::endl;
16  std::cout << " [DeleteFiles] = Delete partiion files (optional)" << std::endl;
17  std::cout << " nproc = Number of partitions" << std::endl;
18  std::cout << " outfile = Target output filename" << std::endl;
19  exit(1);
20  }
21 
22  if(argc == 4)
23  {
24  DeleteFiles = true;
25  }
26 
27  TiXmlDocument docOutput;
28  TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "utf-8", "");
29  docOutput.LinkEndChild(decl);
30 
31  TiXmlElement *master = new TiXmlElement("NEKTAR");
32  std::string basename = argv[argc-1];
33  std::string extension = argv[argc-1];
34  basename = basename.substr(0, basename.find_last_of("."));
35  extension = extension.substr(extension.find_last_of(".") + 1);
36 
37  fs::path infile(argv[argc-1]);
38  bool isdirectory = fs::is_directory(infile);
39 
40  for (int n = 0; n < atoi(argv[argc-2]); ++n)
41  {
42  std::stringstream filename;
43  if(isdirectory)
44  {
45  filename << argv[argc-1] << "/P" << n << ".fld";
46  }
47  else
48  {
49  filename << basename << "_P" << n << "." << extension;
50  }
51  TiXmlDocument docInput;
52  if (!docInput.LoadFile(filename.str()))
53  {
54  std::cerr << "Unable to open file '" << filename.str() << "'." << std::endl;
55  exit(1);
56  }
57  TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
58 
59  // load up root processor's meta data
60  if(n == 0 && nektar->FirstChildElement("Metadata"))
61  {
62  TiXmlElement *metadata = nektar->FirstChildElement("Metadata");
63  if(metadata)
64  {
65  master->LinkEndChild(new TiXmlElement(*metadata));
66  }
67  }
68 
69  // load the elements from seperate files.
70  TiXmlElement *elements = nektar->FirstChildElement("ELEMENTS");
71  while (elements)
72  {
73  master->LinkEndChild(new TiXmlElement(*elements));
74  elements = elements->NextSiblingElement();
75  }
76  }
77 
78  docOutput.LinkEndChild(master);
79 
80  if(isdirectory)
81  {
82  std::string outname = basename + "_combined" + "." + extension; ;
83  if (!docOutput.SaveFile(outname))
84  {
85  std::cerr << "Unable to write file '" << outname << "'." << std::endl;
86  }
87  }
88  else if (!docOutput.SaveFile(argv[argc-1]))
89  {
90  std::cerr << "Unable to write file '" << argv[argc-1] << "'." << std::endl;
91  }
92  else
93  {
94  if(DeleteFiles)
95  {
96 
97  for (int n = 0; n < atoi(argv[argc-2]); ++n)
98  {
99  std::string basename = argv[argc-1];
100  std::string extension = argv[argc-1];
101  basename = basename.substr(0, basename.find_last_of("."));
102  extension = extension.substr(extension.find_last_of(".") + 1);
103  std::stringstream filename;
104  filename << basename << "_P" << n << "." << extension;
105  std::string deloutput = "rm -rf ";
106  deloutput = deloutput + filename.str();
107  system(deloutput.c_str());
108  }
109  }
110  }
111 
112  exit(0);
113 }