Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Read xml file and set up expansions
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <iomanip>
37 #include <iostream>
38 #include <string>
39 using namespace std;
40 
42 
43 #include "InputXml.h"
44 using namespace Nektar;
45 
46 static std::string npts = LibUtilities::SessionReader::RegisterCmdLineArgument(
47  "NumberOfPoints", "n", "Define number of points to dump output");
48 
49 namespace Nektar
50 {
51 namespace FieldUtils
52 {
53 
54 ModuleKey InputXml::m_className[5] = {
56  ModuleKey(eInputModule, "xml"), InputXml::create, "Reads Xml file."),
58  ModuleKey(eInputModule, "xml.gz"), InputXml::create, "Reads Xml file."),
59 };
60 
61 /**
62  * @brief Set up InputXml object.
63  *
64  */
65 InputXml::InputXml(FieldSharedPtr f) : InputModule(f)
66 {
67  m_allowedFiles.insert("xml");
68  m_allowedFiles.insert("xml.gz");
69  m_allowedFiles.insert("fld"); // these files could be allowed with xml files
70  m_allowedFiles.insert("chk");
71  m_allowedFiles.insert("rst");
72 }
73 
74 /**
75  *
76  */
78 {
79 }
80 
81 /**
82  *
83  */
84 void InputXml::Process(po::variables_map &vm)
85 {
86  Timer timerpart;
87 
88  // check for multiple calls to inputXml due to split xml
89  // files. If so just return
90  int expsize = m_f->m_exp.size();
91  m_f->m_comm->AllReduce(expsize, LibUtilities::ReduceMax);
92 
93  if (expsize != 0)
94  {
95  return;
96  }
97 
98  if (m_f->m_verbose)
99  {
100  if (m_f->m_comm->TreatAsRankZero())
101  {
102  cout << "Processing input xml file" << endl;
103  timerpart.Start();
104  }
105  }
106 
107  // check to see if fld file defined so can use in
108  // expansion defintion if required
109  string fldending;
110  bool fldfilegiven = true;
111 
112  // Determine appropriate field input
113  if (m_f->m_inputfiles.count("fld") != 0)
114  {
115  fldending = "fld";
116  }
117  else if (m_f->m_inputfiles.count("chk") != 0)
118  {
119  fldending = "chk";
120  }
121  else if (m_f->m_inputfiles.count("rst") != 0)
122  {
123  fldending = "rst";
124  }
125  else
126  {
127  fldfilegiven = false;
128  }
129 
130  string xml_ending = "xml";
131  string xml_gz_ending = "xml.gz";
132 
133  std::vector<std::string> files;
134  // load .xml ending
135  for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
136  {
137  files.push_back(m_f->m_inputfiles[xml_ending][i]);
138  }
139 
140  // load any .xml.gz endings
141  for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
142  {
143  files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
144  }
145 
147 
148  // define range to process output
149  if (vm.count("range"))
150  {
151  vector<NekDouble> values;
153  vm["range"].as<string>().c_str(), values),
154  "Failed to interpret range string");
155 
156  ASSERTL0(values.size() > 1, "Do not have minimum values of xmin,xmax");
157  ASSERTL0(values.size() % 2 == 0,
158  "Do not have an even number of range values");
159 
160  int nvalues = values.size() / 2;
162 
163  rng->m_doZrange = false;
164  rng->m_doYrange = false;
165  rng->m_checkShape = false;
166 
167  switch (nvalues)
168  {
169  case 3:
170  rng->m_doZrange = true;
171  rng->m_zmin = values[4];
172  rng->m_zmax = values[5];
173  case 2:
174  rng->m_doYrange = true;
175  rng->m_ymin = values[2];
176  rng->m_ymax = values[3];
177  case 1:
178  rng->m_doXrange = true;
179  rng->m_xmin = values[0];
180  rng->m_xmax = values[1];
181  break;
182  default:
183  ASSERTL0(false, "too many values specfied in range");
184  }
185  }
186 
187  // define range to only take a single shape.
188  if (vm.count("onlyshape"))
189  {
191  {
192  rng =
194  rng->m_doXrange = false;
195  rng->m_doYrange = false;
196  rng->m_doZrange = false;
197  }
198 
199  rng->m_checkShape = true;
200 
201  string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
202  int i;
203  for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
204  {
205  string shapeval = LibUtilities::ShapeTypeMap[i];
206  boost::to_upper(shapeval);
207  if (shapematch.compare(shapeval) == 0)
208  {
209  rng->m_shapeType = (LibUtilities::ShapeType)i;
210  break;
211  }
212  }
213  ASSERTL0(i != LibUtilities::SIZE_ShapeType,
214  "Failed to find shape type in -onlyshape command line "
215  "argument");
216  }
217 
218  // Set up command lines options that will be passed through to SessionReader
219  vector<string> cmdArgs;
220  cmdArgs.push_back("FieldConvert");
221 
222  if (m_f->m_verbose)
223  {
224  cmdArgs.push_back("--verbose");
225  }
226 
227  if (vm.count("part-only"))
228  {
229  cmdArgs.push_back("--part-only");
230  cmdArgs.push_back(
231  boost::lexical_cast<string>(vm["part-only"].as<int>()));
232  }
233 
234  if (vm.count("part-only-overlapping"))
235  {
236  cmdArgs.push_back("--part-only-overlapping");
237  cmdArgs.push_back(
238  boost::lexical_cast<string>(vm["part-only-overlapping"].as<int>()));
239  }
240 
241  if (vm.count("npz"))
242  {
243  cmdArgs.push_back("--npz");
244  cmdArgs.push_back(boost::lexical_cast<string>(vm["npz"].as<int>()));
245  }
246 
247  int argc = cmdArgs.size();
248  const char **argv = new const char *[argc];
249  for (int i = 0; i < argc; ++i)
250  {
251  argv[i] = cmdArgs[i].c_str();
252  }
253 
255  argc, (char **)argv, files, m_f->m_comm);
256 
257  // Free up memory.
258  delete[] argv;
259 
260  if (m_f->m_verbose)
261  {
262  if (m_f->m_comm->TreatAsRankZero())
263  {
264  timerpart.Stop();
265  NekDouble cpuTime = timerpart.TimePerTest(1);
266 
267  stringstream ss;
268  ss << cpuTime << "s";
269  cout << "\t InputXml session reader CPU Time: " << setw(8) << left
270  << ss.str() << endl;
271  timerpart.Start();
272  }
273  }
274 
275  m_f->m_graph = SpatialDomains::MeshGraph::Read(m_f->m_session, rng);
276 
277  if (m_f->m_verbose)
278  {
279  if (m_f->m_comm->TreatAsRankZero())
280  {
281  timerpart.Stop();
282  NekDouble cpuTime = timerpart.TimePerTest(1);
283 
284  stringstream ss;
285  ss << cpuTime << "s";
286  cout << "\t InputXml mesh graph setup CPU Time: " << setw(8)
287  << left << ss.str() << endl;
288  timerpart.Start();
289  }
290  }
291 
292  // currently load all field (possibly could read data from
293  // expansion list but it is re-arranged in expansion)
294  const SpatialDomains::ExpansionMap &expansions =
295  m_f->m_graph->GetExpansions();
296 
297  // if Range has been speficied it is possible to have a
298  // partition which is empty so ccheck this and return if
299  // no elements present.
300  if (!expansions.size())
301  {
302  return;
303  }
304 
305  m_f->m_exp.resize(1);
306 
307  // load fielddef header if fld file is defined. This gives
308  // precedence to Homogeneous definition in fld file
309  int NumHomogeneousDir = 0;
310  if (fldfilegiven)
311  {
312  // use original expansion to identify which elements are in
313  // this partition/subrange
314 
315  Array<OneD, int> ElementGIDs(expansions.size());
316  SpatialDomains::ExpansionMap::const_iterator expIt;
317 
318  int i = 0;
319  for (expIt = expansions.begin(); expIt != expansions.end(); ++expIt)
320  {
321  ElementGIDs[i++] = expIt->second->m_geomShPtr->GetGlobalID();
322  }
323 
324  m_f->m_fielddef.clear();
325  m_f->m_data.clear();
326 
327  m_f->FieldIOForFile(m_f->m_inputfiles[fldending][0])->Import(
328  m_f->m_inputfiles[fldending][0], m_f->m_fielddef, m_f->m_data,
329  m_f->m_fieldMetaDataMap, ElementGIDs);
330  NumHomogeneousDir = m_f->m_fielddef[0]->m_numHomogeneousDir;
331 
332  //----------------------------------------------
333  // Set up Expansion information to use mode order from field
334  m_f->m_graph->SetExpansions(m_f->m_fielddef);
335  }
336  else
337  {
338  if (m_f->m_session->DefinesSolverInfo("HOMOGENEOUS"))
339  {
340  std::string HomoStr = m_f->m_session->GetSolverInfo("HOMOGENEOUS");
341 
342  if ((HomoStr == "HOMOGENEOUS1D") || (HomoStr == "Homogeneous1D") ||
343  (HomoStr == "1D") || (HomoStr == "Homo1D"))
344  {
345  NumHomogeneousDir = 1;
346  }
347  if ((HomoStr == "HOMOGENEOUS2D") || (HomoStr == "Homogeneous2D") ||
348  (HomoStr == "2D") || (HomoStr == "Homo2D"))
349  {
350  NumHomogeneousDir = 2;
351  }
352  }
353  }
354 
355  // reset expansion defintion to use equispaced points if required.
357  {
358  int nPointsNew = 0;
359 
360  if (vm.count("output-points"))
361  {
362  nPointsNew = vm["output-points"].as<int>();
363  }
364 
365  m_f->m_graph->SetExpansionsToEvenlySpacedPoints(nPointsNew);
366  }
367  else
368  {
369  if (vm.count("output-points"))
370  {
371  int nPointsNew = vm["output-points"].as<int>();
372  m_f->m_graph->SetExpansionsToPointOrder(nPointsNew);
373  }
374  }
375 
376  if (m_f->m_verbose)
377  {
378  if (m_f->m_comm->TreatAsRankZero())
379  {
380  timerpart.Stop();
381  NekDouble cpuTime = timerpart.TimePerTest(1);
382 
383  stringstream ss;
384  ss << cpuTime << "s";
385  cout << "\t InputXml setexpansion CPU Time: " << setw(8) << left
386  << ss.str() << endl;
387  timerpart.Start();
388  }
389  }
390 
391  // Override number of planes with value from cmd line
392  if (NumHomogeneousDir == 1 && vm.count("output-points-hom-z"))
393  {
394  int expdim = m_f->m_graph->GetMeshDimension();
395  m_f->m_fielddef[0]->m_numModes[expdim] =
396  vm["output-points-hom-z"].as<int>();
397  }
398 
399  m_f->m_exp[0] = m_f->SetUpFirstExpList(NumHomogeneousDir, fldfilegiven);
400 
401  if (m_f->m_verbose)
402  {
403  if (m_f->m_comm->TreatAsRankZero())
404  {
405  timerpart.Stop();
406  NekDouble cpuTime = timerpart.TimePerTest(1);
407 
408  stringstream ss1;
409 
410  ss1 << cpuTime << "s";
411  cout << "\t InputXml set first exp CPU Time: " << setw(8) << left
412  << ss1.str() << endl;
413  }
414  }
415 }
416 }
417 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
static boost::shared_ptr< MeshGraph > Read(const LibUtilities::SessionReaderSharedPtr &pSession, DomainRangeShPtr &rng=NullDomainRangeShPtr)
Definition: MeshGraph.cpp:124
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
STL namespace.
pair< ModuleType, string > ModuleKey
const char *const ShapeTypeMap[]
Definition: ShapeType.hpp:66
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
Abstract base class for input modules.
void Stop()
Definition: Timer.cpp:62
static std::string npts
Definition: InputXml.cpp:46
virtual void Process(po::variables_map &vm)
Definition: InputXml.cpp:84
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:767
boost::shared_ptr< DomainRange > DomainRangeShPtr
Definition: MeshGraph.h:157
double NekDouble
static DomainRangeShPtr NullDomainRangeShPtr
Definition: MeshGraph.h:158
void Start()
Definition: Timer.cpp:51
static bool GenerateUnOrderedVector(const char *const str, std::vector< NekDouble > &vec)
Definition: ParseUtils.hpp:128
NekDouble TimePerTest(unsigned int n)
Returns amount of seconds per iteration in a test with n iterations.
Definition: Timer.cpp:108
std::map< int, ExpansionShPtr > ExpansionMap
Definition: MeshGraph.h:174
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215