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 <vector>
45 #include <typeinfo>
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 /**
99  * @brief Swap endian ordering of the input variable.
100  */
101 template <typename T>
102 void swap_endian(T &u)
103 {
104  union
105  {
106  T u;
107  unsigned char u8[sizeof(T)];
108  } source, dest;
109 
110  source.u = u;
111 
112  for (size_t k = 0; k < sizeof(T); k++)
113  {
114  dest.u8[k] = source.u8[sizeof(T) - k - 1];
115  }
116 
117  u = dest.u;
118 }
119 
120 template <typename T>
121 void swap_endian(std::vector<T> &u)
122 {
123  size_t vecSize = u.size();
124  for (int i = 0; i < vecSize; ++i)
125  {
126  swap_endian(u[i]);
127  }
128 }
129 
130 /**
131  * @brief Represents a command-line configuration option.
132  */
134 {
135  /**
136  * @brief Construct a new configuration option.
137  *
138  * @param isBool True if the option is boolean type.
139  * @param defValue Default value of the option.
140  * @param desc Description of the option.
141  */
142  ConfigOption(bool isBool, std::string defValue, std::string desc)
143  : m_isBool(isBool), m_beenSet(false), m_value(), m_defValue(defValue),
144  m_desc(desc)
145  {
146  }
148  : m_isBool(false), m_beenSet(false), m_value(), m_defValue(), m_desc()
149  {
150  }
151 
152  /**
153  * @brief Re-interpret the value stored in #value as some type using
154  * boost::lexical_cast.
155  */
156  template <typename T>
157  T as() const
158  {
159  try
160  {
161  return boost::lexical_cast<T>(m_value);
162  }
163  catch (const std::exception &e)
164  {
165  std::cerr << "We are not able to cast m_value " << m_value
166  << " to " << typeid(T).name() << std::endl
167  << e.what() << std::endl;
168  abort();
169  }
170  }
171 
172  /// True if the configuration option is a boolean (thus does not
173  /// need additional arguments).
174  bool m_isBool;
175  /// True if the configuration option has been set at command
176  /// line. If false, the default value will be put into #value.
177  bool m_beenSet;
178  /// The value of the configuration option.
179  std::string m_value;
180  /// Default value of the configuration option.
181  std::string m_defValue;
182  /// Description of the configuration option.
183  std::string m_desc;
184 };
185 
186 /**
187  * Abstract base class for mesh converter modules. Each subclass
188  * implements the Process() function, which in some way alters the
189  * mesh #m.
190  */
191 class Module
192 {
193 public:
195  : m_f(p_f)
196  {
197  }
198  virtual void Process(po::variables_map &vm) = 0;
199 
200  virtual ~Module() = default;
201 
202  virtual std::string GetModuleName() = 0;
203 
204  virtual std::string GetModuleDescription()
205  {
206  return " ";
207  }
208 
209  const ConfigOption& GetConfigOption(const std::string& key) const
210  {
211  auto it = m_config.find(key);
212  ASSERTL0(it != m_config.end(), "Configuration key not found!");
213  return it->second;
214  }
215 
217 
218  FIELD_UTILS_EXPORT void RegisterConfig(std::string key,
219  std::string value = "");
222  FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName);
223 
226  const Array<OneD, const NekDouble> &infield,
227  Array<OneD, NekDouble> &outfield);
228 
229  /// Field object
231 
232 protected:
233  Module(){};
234 
235  /// List of configuration values.
236  std::map<std::string, ConfigOption> m_config;
237  /// List of allowed file formats.
238  std::set<std::string> m_allowedFiles;
239 };
240 
241 /**
242  * @brief Abstract base class for input modules.
243  *
244  * Input modules should read the contents of #fldFile in the Process()
245  * function and populate the members of #m. Typically any given module
246  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
247  * Mesh::element, then call the protected ProcessX functions to
248  * generate edges, faces, etc.
249  */
250 class InputModule : public Module
251 {
252 public:
254  FIELD_UTILS_EXPORT static std::string GuessFormat(std::string fileName);
255  void PrintSummary();
256 };
257 
258 typedef std::shared_ptr<InputModule> InputModuleSharedPtr;
259 
260 /**
261  * @brief Abstract base class for processing modules.
262  *
263  */
264 class ProcessModule : public Module
265 {
266 public:
269  {
270  }
271 };
272 
273 /**
274  * @brief Abstract base class for output modules.
275  *
276  * Output modules take the mesh #m and write to the file specified by
277  * the stream.
278  */
279 class OutputModule : public Module
280 {
281 public:
284 
285 protected:
286  /// Output stream
287  std::ofstream m_fldFile;
288 };
289 
290 typedef std::pair<ModuleType, std::string> ModuleKey;
291 FIELD_UTILS_EXPORT std::ostream &operator<<(
292  std::ostream &os, const ModuleKey &rhs);
293 
294 typedef std::shared_ptr<Module> ModuleSharedPtr;
297 
299 
300 }
301 }
302 
303 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define FIELD_UTILS_EXPORT
Abstract base class for input modules.
Definition: Module.h:251
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:194
virtual std::string GetModuleName()=0
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:238
virtual ~Module()=default
FieldSharedPtr m_f
Field object.
Definition: Module.h:230
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:233
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:209
virtual std::string GetModuleDescription()
Definition: Module.h:204
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:280
OutputModule(FieldSharedPtr p_f)
Definition: Module.cpp:68
std::ofstream m_fldFile
Output stream.
Definition: Module.h:287
FIELD_UTILS_EXPORT void OpenStream()
Open a file for output.
Definition: Module.cpp:88
Abstract base class for processing modules.
Definition: Module.h:265
ProcessModule(FieldSharedPtr p_f)
Definition: Module.h:268
Provides a generic Factory class.
Definition: NekFactory.hpp:105
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
Definition: Module.h:296
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:290
std::shared_ptr< InputModule > InputModuleSharedPtr
Definition: Module.h:258
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:102
std::shared_ptr< Module > ModuleSharedPtr
Definition: Module.h:294
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:134
std::string m_value
The value of the configuration option.
Definition: Module.h:179
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
Definition: Module.h:142
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:177
std::string m_defValue
Default value of the configuration option.
Definition: Module.h:181
std::string m_desc
Description of the configuration option.
Definition: Module.h:183
bool m_isBool
True if the configuration option is a boolean (thus does not need additional arguments).
Definition: Module.h:174
T as() const
Re-interpret the value stored in #value as some type using boost::lexical_cast.
Definition: Module.h:157