Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SessionReader.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File SessionReader.h
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:
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_UTILITIES_SESSIONREADER_H
37 #define NEKTAR_LIB_UTILITIES_SESSIONREADER_H
38 
39 #include <map>
40 #include <string>
41 
46 
47 #include <boost/algorithm/string.hpp>
48 #include <boost/enable_shared_from_this.hpp>
49 #include <boost/program_options/variables_map.hpp>
50 
51 class TiXmlElement;
52 class TiXmlDocument;
53 
54 namespace Nektar
55 {
56  namespace LibUtilities
57  {
58  typedef std::map<std::string, std::string> SolverInfoMap;
59  typedef std::map<std::string, NekDouble> ParameterMap;
60  typedef std::map<std::string, std::string> GeometricInfoMap;
61  typedef std::map<std::string, std::string> ExpressionMap;
62  typedef std::vector<std::string> VariableList;
63  typedef std::map<std::string, std::string> TagMap;
64  typedef std::map<std::string, std::string> FilterParams;
65  typedef std::vector<
66  std::pair<std::string, FilterParams> > FilterMap;
67 
68  struct CmdLineArg
69  {
70  std::string shortName;
71  std::string description;
72  bool isFlag;
73  };
74 
75  typedef std::map<std::string, CmdLineArg> CmdLineArgMap;
76 
77  typedef std::map<std::string, int> EnumMap;
78  typedef std::map<std::string, EnumMap> EnumMapList;
79 
80  typedef std::map<std::string, std::string> GloSysInfoMap;
81  typedef std::map<std::string, GloSysInfoMap> GloSysSolnInfoList;
82 
83  typedef std::map<std::string, std::string> GloSysInfoMap;
84  typedef std::map<std::string, GloSysInfoMap> GloSysSolnInfoList;
85 
87  {
91  };
92  const char* const FunctionTypeMap[] =
93  {
94  "No Function type",
95  "Expression",
96  "File"
97  };
98 
99  class Equation;
100  typedef boost::shared_ptr<Equation> EquationSharedPtr;
101 
102  typedef std::map<int, std::vector<unsigned int> > CompositeOrdering;
103  typedef std::map<int, std::vector<unsigned int> > BndRegionOrdering;
104 
106  {
108  std::string m_filename;
110  };
111 
112  typedef std::map<std::pair<std::string,int>, FunctionVariableDefinition>
114  typedef std::map<std::string, FunctionVariableMap >
116 
117  class SessionReader;
118  typedef boost::shared_ptr<SessionReader> SessionReaderSharedPtr;
119 
120  /// Reads and parses information from a Nektar++ XML session file.
121  class SessionReader :
122  public boost::enable_shared_from_this<SessionReader>
123  {
124  public:
125  /// Support creation through MemoryManager.
127 
128  /**
129  * @brief Creates an instance of the SessionReader class.
130  *
131  * This function should be used by an application to instantiate the
132  * session reader. It should be called at the very beginning of the
133  * application before any other processing of command-line
134  * arguments. After instantiating the class and setting up any
135  * parallel communication, it also calls the main initialisation
136  * of the object.
137  */
139  int argc, char *argv[])
140  {
142  LibUtilities::SessionReader>::AllocateSharedPtr(argc, argv);
143  p->InitSession();
144  return p;
145  }
146 
147  /**
148  * @brief Creates an instance of the SessionReader class initialised
149  * using a separate list of XML documents.
150  *
151  * This function should be used by an application to instantiate the
152  * session reader. It may be called after processing of command-line
153  * arguments. After instantiating the class and setting up any
154  * parallel communication, it also calls the main initialisation
155  * of the object.
156  */
158  int argc,
159  char *argv[],
160  std::vector<std::string> &pFilenames,
161  const CommSharedPtr &pComm = CommSharedPtr())
162  {
165  ::AllocateSharedPtr(argc, argv, pFilenames, pComm);
166  p->InitSession();
167  return p;
168  }
169 
171  int argc,
172  char *argv[],
173  const std::vector<std::string> &pFilenames,
174  const CommSharedPtr &pComm);
175 
176  /// Destructor
178 
179  /// Provides direct access to the TiXmlDocument object.
180  LIB_UTILITIES_EXPORT TiXmlDocument &GetDocument();
181  /// Provides direct access to the TiXmlElement specified.
182  LIB_UTILITIES_EXPORT TiXmlElement *GetElement(
183  const std::string& pPath);
184  /// Tests if a specified element is defined in the XML document.
186  const std::string& pPath) const;
187  /// Returns the filename of the loaded XML document.
188  LIB_UTILITIES_EXPORT const std::string &GetFilename() const;
189  /// Returns the session name of the loaded XML document.
190  LIB_UTILITIES_EXPORT const std::string &GetSessionName() const;
191  /// Returns the session name with process rank
192  LIB_UTILITIES_EXPORT const std::string GetSessionNameRank() const;
193  /// Returns the communication object.
195  /// Finalises the session.
197 
198  /* ------ PARAMETERS --------*/
199  /// Checks if a parameter is specified in the XML document.
201  const std::string &name) const;
202  /// Returns the value of the specified parameter.
204  const std::string &pName) const;
205  /// Load an integer parameter
207  const std::string &name,
208  int &var) const;
209  /// Check for and load an integer parameter.
211  const std::string &name,
212  int &var,
213  const int &def) const;
214  /// Load a double precision parameter
216  const std::string &name,
217  NekDouble &var) const;
218  /// Check for and load a double-precision parameter.
220  const std::string &name,
221  NekDouble &var,
222  const NekDouble &def) const;
223  /// Set an integer parameter
225  const std::string &name,
226  int &var);
227  /// Set a double precision parameter
229  const std::string &name,
230  NekDouble &var);
231 
232 
233  /* ------ SOLVER INFO ------ */
234  /// Checks if a solver info property is specified.
236  const std::string &name) const;
237  /// Returns the value of the specified solver info property.
238  LIB_UTILITIES_EXPORT const std::string& GetSolverInfo(
239  const std::string &pProperty) const;
240  /// Sets the value of the specified solver info property.
242  const std::string &pProperty, const std::string &pValue);
243  /// Returns the value of the specified solver info property as enum
244  template<typename T>
245  inline const T GetSolverInfoAsEnum(const std::string &pName) const;
246  /// Returns the value of the specified property and value as enum
247  template<typename T>
248  inline const T GetValueAsEnum(const std::string &pName,
249  const std::string &vValue) const;
250  /// Check for and load a solver info property.
252  const std::string &name,
253  std::string &var,
254  const std::string &def = "") const;
255  /// Check if the value of a solver info property matches.
257  const std::string &name,
258  const std::string &trueval,
259  bool &var,
260  const bool &def = false) const;
261  /// Check if the value of a solver info property matches.
263  const std::string &name,
264  const std::string &trueval) const;
265  /// Check if the value of a solver info property matches.
266  template<typename T>
267  inline bool MatchSolverInfoAsEnum(
268  const std::string &name,
269  const T &trueval) const;
270  /// Registers an enumeration value.
271  LIB_UTILITIES_EXPORT inline static std::string RegisterEnumValue(
272  std::string pEnum,
273  std::string pString,
274  int pEnumValue);
275  /// Registers the default string value of a solver info property.
276  LIB_UTILITIES_EXPORT inline static std::string
278  const std::string &pName,
279  const std::string &pValue);
280 
281  /* ----GlobalSysSolnInfo ----- */
282 
284  const std::string &variable,
285  const std::string &property) const;
286 
287  LIB_UTILITIES_EXPORT const std::string& GetGlobalSysSolnInfo(
288  const std::string &variable,
289  const std::string &property) const;
290 
291 
292  /* ------ GEOMETRIC INFO ------ */
293  /// Checks if a geometric info property is defined.
295  const std::string &name) const;
296  /// Checks for and load a geometric info string property.
298  const std::string &name,
299  std::string &var,
300  const std::string &def = "") const;
301  /// Checks for and loads a geometric info boolean property.
303  const std::string &name,
304  bool &var,
305  const bool &def = false) const;
306  /// Checks for and loads a geometric info double-precision property.
308  const std::string &name,
309  NekDouble &var,
310  const NekDouble &def = 0.0) const;
311  /// Check if the value of a geometric info string property matches.
313  const std::string &name,
314  const std::string &trueval,
315  bool &var,
316  const bool &def = false) const;
317 
318  /* ------ VARIABLES ------ */
319  /// Returns the name of the variable specified by the given index.
320  LIB_UTILITIES_EXPORT const std::string& GetVariable(
321  const unsigned int &idx) const;
323  const unsigned int &idx,
324  std::string newname);
325 
326  /// Returns the names of all variables.
327  LIB_UTILITIES_EXPORT std::vector<std::string> GetVariables() const;
328 
329  /* ------ FUNCTIONS ------*/
330  /// Checks if a specified function is defined in the XML document.
332  const std::string &name) const;
333  /// Checks if a specified function has a given variable defined.
335  const std::string &name,
336  const std::string &variable,
337  const int pDomain = 0) const;
338  /// Returns an EquationSharedPtr to a given function variable.
340  const std::string &name,
341  const std::string &variable,
342  const int pDomain = 0) const;
343  /// Returns an EquationSharedPtr to a given function variable index.
345  const std::string &name,
346  const unsigned int &var,
347  const int pDomain = 0) const;
348  /// Returns the type of a given function variable.
350  const std::string &name,
351  const std::string &variable,
352  const int pDomain = 0) const;
353  /// Returns the type of a given function variable index.
355  const std::string &pName,
356  const unsigned int &pVar,
357  const int pDomain = 0) const;
358  /// Returns the filename to be loaded for a given variable.
360  const std::string &name,
361  const std::string &variable,
362  const int pDomain = 0) const;
363  /// Returns the filename to be loaded for a given variable index.
365  const std::string &name,
366  const unsigned int &var,
367  const int pDomain = 0) const;
368 
369  /// Returns the instance of AnalyticExpressionEvaluator specific to
370  /// this session.
373 
374  /* ------ TAGS ------ */
375  /// Checks if a specified tag is defined.
377  const std::string& pName) const;
378  /// Sets a specified tag.
380  const std::string& pName,
381  const std::string& pValue);
382  /// Returns the value of a specified tag.
383  LIB_UTILITIES_EXPORT const std::string &GetTag(
384  const std::string& pName) const;
385 
386  /* ------ FILTERS ------ */
388 
389  /* ------ CMDLINE ARGUMENTS ------- */
390  /// Checks if a specified cmdline argument has been given.
392  const std::string& pName) const;
393  /// Retrieves a command-line argument value.
394  template <typename T>
396  const std::string& pName) const
397  {
398  return m_cmdLineOptions.find(pName)->second.as<T>();
399  }
400  /// Registers a command-line argument with the session reader.
401  LIB_UTILITIES_EXPORT inline static std::string
403  const std::string &pName,
404  const std::string &pShortName,
405  const std::string &pDescription);
406  /// Registers a command-line flag with the session reader.
407  LIB_UTILITIES_EXPORT inline static std::string
409  const std::string &pName,
410  const std::string &pShortName,
411  const std::string &pDescription);
412 
413  /// Substitutes expressions defined in the XML document.
414  LIB_UTILITIES_EXPORT void SubstituteExpressions(std::string &expr);
417 
419 
420  private:
421  boost::program_options::variables_map m_cmdLineOptions;
422 
423  /// Communication object.
425  /// Filenames
426  std::vector<std::string> m_filenames;
427  /// Filename of the loaded XML document.
428  std::string m_filename;
429  /// Session name of the loaded XML document (filename minus ext).
430  std::string m_sessionName;
431  /// Pointer to the loaded XML document.
432  TiXmlDocument* m_xmlDoc;
433  /// Parameters.
435  /// Solver information properties.
437  /// Geometric information properties.
439  /// Expressions.
441  /// Analytic expression evaluator instance.
443  /// Functions.
445  /// Variables.
447  /// Custom tags.
449  /// Filters map.
451  /// Be verbose
452  bool m_verbose;
453  /// Map of original composite ordering for parallel periodic bcs.
455  /// Map of original boundary region ordering for parallel periodic
456  /// bcs.
458  /// String to enumeration map for Solver Info parameters.
460  /// Default solver info options.
462  /// GlobalSysSoln Info map.
464  /// CmdLine argument map.
466 
467  /// Main constructor
469  int argc,
470  char *argv[]);
471 
473 
474  /// Returns a shared pointer to the current object.
476 
477  /// Parse the program arguments and fill #m_cmdLineOptions
478  std::vector<std::string> ParseCommandLineArguments(
479  int argc, char *argv[]);
480 
481  /// Loads an xml file into a tinyxml doc and decompresses if needed
483  const std::string &pFilename,
484  TiXmlDocument* pDoc) const;
485  /// Creates an XML document from a list of input files.
486  LIB_UTILITIES_EXPORT TiXmlDocument *MergeDoc(
487  const std::vector<std::string> &pFilenames) const;
488  /// Loads and parses the specified file.
490  /// Loads the given XML document and instantiates an appropriate
491  /// communication object.
493  int &argc,
494  char* argv[]);
495 
496  /// Partitions the mesh when running in parallel.
498  /// Partitions the comm object based on session parameters.
500 
501  /// Reads the PARAMETERS section of the XML document.
502  LIB_UTILITIES_EXPORT void ReadParameters(TiXmlElement *conditions);
503  /// Reads the SOLVERINFO section of the XML document.
504  LIB_UTILITIES_EXPORT void ReadSolverInfo(TiXmlElement *conditions);
505  /// Reads the GLOBALSYSSOLNINFO section of the XML document.
507  TiXmlElement *conditions);
508  /// Reads the GEOMETRICINFO section of the XML document.
509  LIB_UTILITIES_EXPORT void ReadGeometricInfo(TiXmlElement *geometry);
510  /// Reads the EXPRESSIONS section of the XML document.
511  LIB_UTILITIES_EXPORT void ReadExpressions(TiXmlElement *conditions);
512  /// Reads the VARIABLES section of the XML document.
513  LIB_UTILITIES_EXPORT void ReadVariables(TiXmlElement *conditions);
514  /// Reads the FUNCTIONS section of the XML document.
515  LIB_UTILITIES_EXPORT void ReadFunctions(TiXmlElement *conditions);
516  /// Reads the FILTERS section of the XML document.
517  LIB_UTILITIES_EXPORT void ReadFilters(TiXmlElement *filters);
518  /// Enforce parameters from command line arguments.
520 
521  /// Parse a string in the form lhs = rhs.
523  const std::string &line,
524  std::string &lhs,
525  std::string &rhs);
526  };
527 
528 
529  /**
530  *
531  */
532  template<typename T>
534  const std::string &name, const T &trueval) const
535  {
536  return (GetSolverInfoAsEnum<T>(name) == trueval);
537  }
538 
539 
540  /**
541  *
542  */
543  template<typename T>
545  const std::string &pName) const
546  {
547  std::string vName = boost::to_upper_copy(pName);
549  "Solver info '" + pName + "' not defined.");
550 
551  std::string vValue = GetSolverInfo(vName);
553  ASSERTL0((x = GetSolverInfoEnums().find(vName)) !=
554  GetSolverInfoEnums().end(),
555  "Enum for SolverInfo property '" + pName + "' not found.");
556 
558  ASSERTL0((y = x->second.find(vValue)) != x->second.end(),
559  "Value of SolverInfo property '" + pName +
560  "' is invalid.");
561 
562  return T(y->second);
563  }
564 
565 
566 
567  /**
568  *
569  */
570  template<typename T>
572  const std::string &pName,
573  const std::string &pValue) const
574  {
575  std::string vName = boost::to_upper_copy(pName);
576 
578  ASSERTL0((x = GetSolverInfoEnums().find(vName)) !=
579  GetSolverInfoEnums().end(),
580  "Enum for property '" + pName + "' not found.");
581 
583  ASSERTL0((y = x->second.find(pValue)) != x->second.end(),
584  "Value of property '" + pValue + "' is invalid.");
585  return T(y->second);
586  }
587 
588 
589 
590  /**
591  * A set of valid values for a given solver info property may be
592  * registered using this function. It must be called statically during
593  * the initialisation of a static variable. For example:
594  *
595  * @code
596  * std::string GlobalLinSys::lookupIds[2] = {
597  * LibUtilities::SessionReader::RegisterEnumValue(
598  * "GlobalSysSoln",
599  * "DirectFull",
600  * MultiRegions::eDirectFullMatrix),
601  * LibUtilities::SessionReader::RegisterEnumValue(
602  * "GlobalSysSoln",
603  * "DirectStaticCond",
604  * MultiRegions::eDirectStaticCond)
605  * }
606  * @endcode
607  *
608  * @param pEnum The name of the property.
609  * @param pString A valid value for the property.
610  * @param pEnumValue An enumeration value corresponding to this
611  * value.
612  *
613  * @return The value for the property provided by #pString.
614  */
616  std::string pEnum, std::string pString, int pEnumValue)
617  {
618  std::string vEnum = boost::to_upper_copy(pEnum);
620  if ((x = GetSolverInfoEnums().find(vEnum)) ==
621  GetSolverInfoEnums().end())
622  {
623  GetSolverInfoEnums()[vEnum] = EnumMap();
624  x = GetSolverInfoEnums().find(vEnum);
625  }
626  x->second[pString] = pEnumValue;
627  return pString;
628  }
629 
630 
631  /**
632  * A default value for a given solver info property may be registered
633  * using this function. The property will take this value until it is
634  * overwritten by a value specified in the XML document, or specified
635  * as a command-line argument. Usage has the form:
636  *
637  * @code
638  * std::string GlobalLinSys::def
639  * = LibUtilities::SessionReader::RegisterDefaultSolverInfo(
640  * "GlobalSysSoln","DirectMultiLevelStaticCond");
641  * @endcode
642  *
643  * @param pName The name of the property.
644  * @param pValue The default value of the property.
645  *
646  * @return The default value of the property provided by #pValue.
647  */
649  const std::string &pName,
650  const std::string &pValue)
651  {
652  std::string vName = boost::to_upper_copy(pName);
653  GetSolverInfoDefaults()[vName] = pValue;
654  return pValue;
655  }
656 
657 
658  /**
659  *
660  */
662  const std::string &pName,
663  const std::string &pShortName,
664  const std::string &pDescription)
665  {
666  ASSERTL0(!pName.empty(), "Empty name for cmdline argument.");
667  CmdLineArg x;
668  x.shortName = pShortName;
669  x.description = pDescription;
670  x.isFlag = false;
671  GetCmdLineArgMap()[pName] = x;
672  return pName;
673  }
674 
675 
676  /**
677  *
678  */
680  const std::string &pName,
681  const std::string &pShortName,
682  const std::string &pDescription)
683  {
684  ASSERTL0(!pName.empty(), "Empty name for cmdline argument.");
685  CmdLineArg x;
686  x.shortName = pShortName;
687  x.description = pDescription;
688  x.isFlag = true;
689  GetCmdLineArgMap()[pName] = x;
690  return pName;
691  }
692 
693 
694  /**
695  * This allows a member function to pass a shared pointer to itself
696  * during a call to another function.
697  */
699  {
700  return shared_from_this();
701  }
702  }
703 }
704 
705 #endif
706