Nektar++
Module.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Module.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 // 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: Field converter module base classes.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef FIELDUTILS_MODULE
36 #define FIELDUTILS_MODULE
37 
38 #include <fstream>
39 #include <iomanip>
40 #include <iostream>
41 #include <map>
42 #include <set>
43 #include <string>
44 #include <typeinfo>
45 #include <vector>
46 
51 
52 #include "Field.hpp"
53 
54 #include "FieldUtilsDeclspec.h"
55 
56 namespace po = boost::program_options;
57 
58 namespace Nektar
59 {
60 namespace FieldUtils
61 {
62 /**
63  * Denotes different types of mesh converter modules: so far only
64  * input, output and process modules are defined.
65  */
67 {
72 };
73 
74 const std::string ModuleTypeMap[] = {"Input", "Process", "Output"};
75 
77 {
90 };
91 
92 const char *const ModulePriorityMap[] = {
93  "CreateGraph", "CreateFieldData", "ModifyFieldData", "CreateExp",
94  "FillExp", "ModifyExp", "BndExtraction", "CreatePts",
95  "ConvertExpToPts", "ModifyPts", "Output"};
96 
97 /**
98  * @brief Swap endian ordering of the input variable.
99  */
100 template <typename T> void swap_endian(T &u)
101 {
102  union
103  {
104  T u;
105  unsigned char u8[sizeof(T)];
106  } source, dest;
107 
108  source.u = u;
109 
110  for (size_t k = 0; k < sizeof(T); k++)
111  {
112  dest.u8[k] = source.u8[sizeof(T) - k - 1];
113  }
114 
115  u = dest.u;
116 }
117 
118 template <typename T> void swap_endian(std::vector<T> &u)
119 {
120  size_t vecSize = u.size();
121  for (int i = 0; i < vecSize; ++i)
122  {
123  swap_endian(u[i]);
124  }
125 }
126 
127 /**
128  * @brief Represents a command-line configuration option.
129  */
131 {
132  /**
133  * @brief Construct a new configuration option.
134  *
135  * @param isBool True if the option is boolean type.
136  * @param defValue Default value of the option.
137  * @param desc Description of the option.
138  */
139  ConfigOption(bool isBool, std::string defValue, std::string desc)
140  : m_isBool(isBool), m_beenSet(false), m_value(), m_defValue(defValue),
141  m_desc(desc)
142  {
143  }
145  : m_isBool(false), m_beenSet(false), m_value(), m_defValue(), m_desc()
146  {
147  }
148 
149  /**
150  * @brief Re-interpret the value stored in #value as some type using
151  * boost::lexical_cast.
152  */
153  template <typename T> T as() const
154  {
155  try
156  {
157  return boost::lexical_cast<T>(m_value);
158  }
159  catch (const std::exception &e)
160  {
161  std::cerr << "We are not able to cast m_value " << m_value << " to "
162  << typeid(T).name() << std::endl
163  << e.what() << std::endl;
164  abort();
165  }
166  }
167 
168  /// True if the configuration option is a boolean (thus does not
169  /// need additional arguments).
170  bool m_isBool;
171  /// True if the configuration option has been set at command
172  /// line. If false, the default value will be put into #value.
173  bool m_beenSet;
174  /// The value of the configuration option.
175  std::string m_value;
176  /// Default value of the configuration option.
177  std::string m_defValue;
178  /// Description of the configuration option.
179  std::string m_desc;
180 };
181 
182 /**
183  * Abstract base class for mesh converter modules. Each subclass
184  * implements the Process() function, which in some way alters the
185  * mesh #m.
186  */
187 class Module
188 {
189 public:
191  {
192  }
193  virtual void Process(po::variables_map &vm) = 0;
194 
195  virtual ~Module() = default;
196 
197  virtual std::string GetModuleName() = 0;
198 
199  virtual std::string GetModuleDescription()
200  {
201  return " ";
202  }
203 
204  const ConfigOption &GetConfigOption(const std::string &key) const
205  {
206  auto it = m_config.find(key);
207  ASSERTL0(it != m_config.end(), "Configuration key not found!");
208  return it->second;
209  }
210 
212 
213  FIELD_UTILS_EXPORT void RegisterConfig(std::string key,
214  std::string value = "");
217  FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName);
218 
221  const Array<OneD, const NekDouble> &infield,
222  Array<OneD, NekDouble> &outfield);
223 
224  /// Field object
226 
227 protected:
228  Module(){};
229 
230  /// List of configuration values.
231  std::map<std::string, ConfigOption> m_config;
232  /// List of allowed file formats.
233  std::set<std::string> m_allowedFiles;
234 };
235 
236 /**
237  * @brief Abstract base class for input modules.
238  *
239  * Input modules should read the contents of #fldFile in the Process()
240  * function and populate the members of #m. Typically any given module
241  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
242  * Mesh::element, then call the protected ProcessX functions to
243  * generate edges, faces, etc.
244  */
245 class InputModule : public Module
246 {
247 public:
249  FIELD_UTILS_EXPORT static std::string GuessFormat(std::string fileName);
250  void PrintSummary();
251 };
252 
253 typedef std::shared_ptr<InputModule> InputModuleSharedPtr;
254 
255 /**
256  * @brief Abstract base class for processing modules.
257  *
258  */
259 class ProcessModule : public Module
260 {
261 public:
264  {
265  }
266 };
267 
268 /**
269  * @brief Abstract base class for output modules.
270  *
271  * Output modules take the mesh #m and write to the file specified by
272  * the stream.
273  */
274 class OutputModule : public Module
275 {
276 public:
279 
280 protected:
281  /// Output stream
282  std::ofstream m_fldFile;
283 };
284 
285 typedef std::pair<ModuleType, std::string> ModuleKey;
286 FIELD_UTILS_EXPORT std::ostream &operator<<(std::ostream &os,
287  const ModuleKey &rhs);
288 
289 typedef std::shared_ptr<Module> ModuleSharedPtr;
292 
294 
295 } // namespace FieldUtils
296 } // namespace Nektar
297 
298 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define FIELD_UTILS_EXPORT
Abstract base class for input modules.
Definition: Module.h:246
void PrintSummary()
Print a brief summary of information.
Definition: Module.cpp:196
InputModule(FieldSharedPtr p_m)
Definition: Module.cpp:63
static FIELD_UTILS_EXPORT std::string GuessFormat(std::string fileName)
Tries to guess the format of the input file.
Definition: Module.cpp:164
virtual void Process(po::variables_map &vm)=0
FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName)
Definition: Module.cpp:73
FIELD_UTILS_EXPORT void RegisterConfig(std::string key, std::string value="")
Register a configuration option with a module.
Definition: Module.cpp:102
FIELD_UTILS_EXPORT Module(FieldSharedPtr p_f)
Definition: Module.h:190
virtual std::string GetModuleName()=0
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:233
virtual ~Module()=default
FieldSharedPtr m_f
Field object.
Definition: Module.h:225
virtual ModulePriority GetModulePriority()=0
FIELD_UTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
Definition: Module.cpp:132
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:228
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Definition: Module.cpp:150
const ConfigOption & GetConfigOption(const std::string &key) const
Definition: Module.h:204
virtual std::string GetModuleDescription()
Definition: Module.h:199
FIELD_UTILS_EXPORT void EvaluateTriFieldAtEquiSpacedPts(LocalRegions::ExpansionSharedPtr &exp, const Array< OneD, const NekDouble > &infield, Array< OneD, NekDouble > &outfield)
Abstract base class for output modules.
Definition: Module.h:275
OutputModule(FieldSharedPtr p_f)
Definition: Module.cpp:68
std::ofstream m_fldFile
Output stream.
Definition: Module.h:282
FIELD_UTILS_EXPORT void OpenStream()
Open a file for output.
Definition: Module.cpp:88
Abstract base class for processing modules.
Definition: Module.h:260
ProcessModule(FieldSharedPtr p_f)
Definition: Module.h:263
Provides a generic Factory class.
Definition: NekFactory.hpp:105
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
Definition: Module.h:291
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:989
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
Definition: Module.cpp:58
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:285
std::shared_ptr< InputModule > InputModuleSharedPtr
Definition: Module.h:253
const std::string ModuleTypeMap[]
Definition: Module.h:74
const char *const ModulePriorityMap[]
Definition: Module.h:92
void swap_endian(T &u)
Swap endian ordering of the input variable.
Definition: Module.h:100
std::shared_ptr< Module > ModuleSharedPtr
Definition: Module.h:289
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:68
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
Represents a command-line configuration option.
Definition: Module.h:131
std::string m_value
The value of the configuration option.
Definition: Module.h:175
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
Definition: Module.h:139
bool m_beenSet
True if the configuration option has been set at command line. If false, the default value will be pu...
Definition: Module.h:173
std::string m_defValue
Default value of the configuration option.
Definition: Module.h:177
std::string m_desc
Description of the configuration option.
Definition: Module.h:179
bool m_isBool
True if the configuration option is a boolean (thus does not need additional arguments).
Definition: Module.h:170
T as() const
Re-interpret the value stored in #value as some type using boost::lexical_cast.
Definition: Module.h:153