Nektar++
InputXml.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputXml.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Read xml file and set up expansions
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iomanip>
36 #include <iostream>
37 #include <string>
38 using namespace std;
39 
42 
43 #include "InputXml.h"
44 using namespace Nektar;
45 
46 namespace Nektar
47 {
48 namespace FieldUtils
49 {
50 
51 ModuleKey InputXml::m_className[5] = {
53  ModuleKey(eInputModule, "xml"), InputXml::create, "Reads Xml file."),
55  ModuleKey(eInputModule, "xml.gz"), InputXml::create, "Reads Xml file."),
56 };
57 
58 /**
59  * @brief Set up InputXml object.
60  *
61  */
62 InputXml::InputXml(FieldSharedPtr f) : InputModule(f)
63 {
64  m_allowedFiles.insert("xml");
65  m_allowedFiles.insert("xml.gz");
66 }
67 
68 /**
69  *
70  */
72 {
73 }
74 
75 /**
76  *
77  */
78 void InputXml::Process(po::variables_map &vm)
79 {
80  LibUtilities::Timer timerpart;
81  if (m_f->m_verbose)
82  {
83  if (m_f->m_comm->TreatAsRankZero())
84  {
85  timerpart.Start();
86  }
87  }
88 
89  // check for multiple calls to inputXml due to split xml
90  // files. If so just return
91  if (m_f->m_graph)
92  {
93  return;
94  }
95 
96  string xml_ending = "xml";
97  string xml_gz_ending = "xml.gz";
98 
99  std::vector<std::string> files;
100  // load .xml ending
101  for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
102  {
103  files.push_back(m_f->m_inputfiles[xml_ending][i]);
104  }
105 
106  // load any .xml.gz endings
107  for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
108  {
109  files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
110  }
111 
113 
114  // define range to process output
115  if (vm.count("range"))
116  {
117  vector<NekDouble> values;
118  ASSERTL0(ParseUtils::GenerateVector(vm["range"].as<string>(), values),
119  "Failed to interpret range string");
120 
121  ASSERTL0(values.size() > 1, "Do not have minimum values of xmin,xmax");
122  ASSERTL0(values.size() % 2 == 0,
123  "Do not have an even number of range values");
124 
125  int nvalues = values.size() / 2;
127 
128  rng->m_doZrange = false;
129  rng->m_doYrange = false;
130  rng->m_checkShape = false;
131 
132  switch (nvalues)
133  {
134  case 3:
135  rng->m_doZrange = true;
136  rng->m_zmin = values[4];
137  rng->m_zmax = values[5];
138  /* Falls through. */
139  case 2:
140  rng->m_doYrange = true;
141  rng->m_ymin = values[2];
142  rng->m_ymax = values[3];
143  /* Falls through. */
144  case 1:
145  rng->m_doXrange = true;
146  rng->m_xmin = values[0];
147  rng->m_xmax = values[1];
148  break;
149  default:
150  NEKERROR(ErrorUtil::efatal, "too many values specfied in range");
151  }
152  }
153 
154  // define range to only take a single shape.
155  if (vm.count("onlyshape"))
156  {
158  {
159  rng =
161  rng->m_doXrange = false;
162  rng->m_doYrange = false;
163  rng->m_doZrange = false;
164  }
165 
166  rng->m_checkShape = true;
167 
168  string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
169  int i;
170  for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
171  {
172  string shapeval = LibUtilities::ShapeTypeMap[i];
173  boost::to_upper(shapeval);
174  if (shapematch.compare(shapeval) == 0)
175  {
176  rng->m_shapeType = (LibUtilities::ShapeType)i;
177  break;
178  }
179  }
180  ASSERTL0(i != LibUtilities::SIZE_ShapeType,
181  "Failed to find shape type in -onlyshape command line "
182  "argument");
183  }
184 
185  // Set up command lines options that will be passed through to SessionReader
186  vector<string> cmdArgs;
187  cmdArgs.push_back("FieldConvert");
188 
189  if (m_f->m_verbose)
190  {
191  cmdArgs.push_back("--verbose");
192  }
193 
194  if (vm.count("part-only"))
195  {
196  cmdArgs.push_back("--part-only");
197  cmdArgs.push_back(
198  boost::lexical_cast<string>(vm["part-only"].as<int>()));
199  }
200 
201  if (vm.count("part-only-overlapping"))
202  {
203  cmdArgs.push_back("--part-only-overlapping");
204  cmdArgs.push_back(
205  boost::lexical_cast<string>(vm["part-only-overlapping"].as<int>()));
206  }
207 
208  if (vm.count("npz"))
209  {
210  cmdArgs.push_back("--npz");
211  cmdArgs.push_back(boost::lexical_cast<string>(vm["npz"].as<int>()));
212  }
213 
214  int argc = cmdArgs.size();
215  const char **argv = new const char *[argc];
216  for (int i = 0; i < argc; ++i)
217  {
218  argv[i] = cmdArgs[i].c_str();
219  }
220 
222  argc, (char **)argv, files, m_f->m_comm);
223 
224  if (vm.count("nparts"))
225  {
226  // make sure have pre-partitioned mesh for nparts option
227  ASSERTL0(boost::icontains(files[0],"_xml"),
228  "Expect the mesh to have been pre-partitioned when "
229  " using the\"--nparts\" option. Please use \"--part-only\" "
230  "option to prepartition xml file.");
231  }
232 
233  // Free up memory.
234  delete[] argv;
235 
236  if (m_f->m_verbose)
237  {
238  if (m_f->m_comm->TreatAsRankZero())
239  {
240  timerpart.Stop();
241  NekDouble cpuTime = timerpart.TimePerTest(1);
242 
243  stringstream ss;
244  ss << cpuTime << "s";
245  cout << "\t InputXml session reader CPU Time: " << setw(8) << left
246  << ss.str() << endl;
247  timerpart.Start();
248  }
249  }
250 
251  m_f->m_graph = SpatialDomains::MeshGraph::Read(m_f->m_session, rng);
252 
253  if (m_f->m_verbose)
254  {
255  if (m_f->m_comm->TreatAsRankZero())
256  {
257  timerpart.Stop();
258  NekDouble cpuTime = timerpart.TimePerTest(1);
259 
260  stringstream ss;
261  ss << cpuTime << "s";
262  cout << "\t InputXml mesh graph setup CPU Time: " << setw(8)
263  << left << ss.str() << endl;
264  timerpart.Start();
265  }
266  }
267 }
268 }
269 }
#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 mod...
Definition: ErrorUtil.hpp:209
NekDouble TimePerTest(unsigned int n)
Returns amount of seconds per iteration in a test with n iterations.
Definition: Timer.cpp:57
STL namespace.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
std::shared_ptr< DomainRange > DomainRangeShPtr
Definition: MeshGraph.h:128
const char *const ShapeTypeMap[]
Definition: ShapeType.hpp:67
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
Abstract base class for input modules.
std::pair< ModuleType, std::string > ModuleKey
virtual void Process(po::variables_map &vm)
Definition: InputXml.cpp:78
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
double NekDouble
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
std::set< std::string > m_allowedFiles
static DomainRangeShPtr NullDomainRangeShPtr
Definition: MeshGraph.h:129
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, DomainRangeShPtr rng=NullDomainRangeShPtr, bool fillGraph=true)
Definition: MeshGraph.cpp:113
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.