Nektar++
FieldUtils/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 
49 
50 #include "Field.hpp"
51 
52 #include "FieldUtilsDeclspec.h"
53 
54 namespace po = boost::program_options;
55 
56 namespace Nektar
57 {
58 namespace FieldUtils
59 {
60 /**
61  * Denotes different types of mesh converter modules: so far only
62  * input, output and process modules are defined.
63  */
65 {
70 };
71 
72 const char *const ModuleTypeMap[] = {"Input", "Process", "Output"};
73 
75 {
88 };
89 
90 /**
91  * @brief Swap endian ordering of the input variable.
92  */
93 template <typename T>
94 void swap_endian(T &u)
95 {
96  union
97  {
98  T u;
99  unsigned char u8[sizeof(T)];
100  } source, dest;
101 
102  source.u = u;
103 
104  for (size_t k = 0; k < sizeof(T); k++)
105  {
106  dest.u8[k] = source.u8[sizeof(T) - k - 1];
107  }
108 
109  u = dest.u;
110 }
111 
112 template <typename T>
113 void swap_endian(std::vector<T> &u)
114 {
115  size_t vecSize = u.size();
116  for (int i = 0; i < vecSize; ++i)
117  {
118  swap_endian(u[i]);
119  }
120 }
121 
122 /**
123  * @brief Represents a command-line configuration option.
124  */
126 {
127  /**
128  * @brief Construct a new configuration option.
129  *
130  * @param isBool True if the option is boolean type.
131  * @param defValue Default value of the option.
132  * @param desc Description of the option.
133  */
134  ConfigOption(bool isBool, std::string defValue, std::string desc)
135  : m_isBool(isBool), m_beenSet(false), m_value(), m_defValue(defValue),
136  m_desc(desc)
137  {
138  }
140  : m_isBool(false), m_beenSet(false), m_value(), m_defValue(), m_desc()
141  {
142  }
143 
144  /**
145  * @brief Re-interpret the value stored in #value as some type using
146  * boost::lexical_cast.
147  */
148  template <typename T> T as()
149  {
150  try
151  {
152  return boost::lexical_cast<T>(m_value);
153  }
154  catch (const std::exception &e)
155  {
156  std::cerr << e.what() << std::endl;
157  abort();
158  }
159  }
160 
161  /// True if the configuration option is a boolean (thus does not
162  /// need additional arguments).
163  bool m_isBool;
164  /// True if the configuration option has been set at command
165  /// line. If false, the default value will be put into #value.
166  bool m_beenSet;
167  /// The value of the configuration option.
168  std::string m_value;
169  /// Default value of the configuration option.
170  std::string m_defValue;
171  /// Description of the configuration option.
172  std::string m_desc;
173 };
174 
175 /**
176  * Abstract base class for mesh converter modules. Each subclass
177  * implements the Process() function, which in some way alters the
178  * mesh #m.
179  */
180 class Module
181 {
182 public:
184  : m_f(p_f)
185  {
186  }
187  virtual void Process(po::variables_map &vm) = 0;
188 
189  virtual std::string GetModuleName() = 0;
190 
191  virtual std::string GetModuleDescription()
192  {
193  return " ";
194  }
195 
196  virtual ModulePriority GetModulePriority() = 0;
197 
198  FIELD_UTILS_EXPORT void RegisterConfig(std::string key,
199  std::string value = "");
200  FIELD_UTILS_EXPORT void PrintConfig();
201  FIELD_UTILS_EXPORT void SetDefaults();
202 
203  FIELD_UTILS_EXPORT void EvaluateTriFieldAtEquiSpacedPts(
205  const Array<OneD, const NekDouble> &infield,
206  Array<OneD, NekDouble> &outfield);
207 
208 protected:
209  Module(){};
210 
211  /// Field object
212  FieldSharedPtr m_f;
213  /// List of configuration values.
214  std::map<std::string, ConfigOption> m_config;
215 };
216 
217 /**
218  * @brief Abstract base class for input modules.
219  *
220  * Input modules should read the contents of #fldFile in the Process()
221  * function and populate the members of #m. Typically any given module
222  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
223  * Mesh::element, then call the protected ProcessX functions to
224  * generate edges, faces, etc.
225  */
226 class InputModule : public Module
227 {
228 public:
230  FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName);
231  FIELD_UTILS_EXPORT static std::string GuessFormat(std::string fileName);
232 
233 protected:
234  /// Print summary of elements.
235  void PrintSummary();
236  std::set<std::string> m_allowedFiles;
237 };
238 
239 typedef std::shared_ptr<InputModule> InputModuleSharedPtr;
240 
241 /**
242  * @brief Abstract base class for processing modules.
243  *
244  */
245 class ProcessModule : public Module
246 {
247 public:
250  {
251  }
252 };
253 
254 /**
255  * @brief Abstract base class for output modules.
256  *
257  * Output modules take the mesh #m and write to the file specified by
258  * the stream.
259  */
260 class OutputModule : public Module
261 {
262 public:
264  FIELD_UTILS_EXPORT void OpenStream();
265 
266 protected:
267  /// Output stream
268  std::ofstream m_fldFile;
269 };
270 
271 typedef std::pair<ModuleType, std::string> ModuleKey;
272 FIELD_UTILS_EXPORT std::ostream &operator<<(
273  std::ostream &os, const ModuleKey &rhs);
274 
275 typedef std::shared_ptr<Module> ModuleSharedPtr;
278 
280 
282 {
283 public:
284  FieldConvertComm(int argc, char *argv[], int size, int rank)
285  : CommSerial(argc, argv)
286  {
287  m_size = size;
288  m_rank = rank;
289  m_type = "FieldConvert parallel";
290  }
291  FieldConvertComm(int size, int rank) : CommSerial(0, NULL)
292  {
293  m_size = size;
294  m_rank = rank;
295  m_type = "FieldConvert parallel";
296  }
298  {
299  }
300  void v_SplitComm(int pRows, int pColumns)
301  {
302  // Compute row and column in grid.
303  m_commRow = std::shared_ptr<FieldConvertComm>(
304  new FieldConvertComm(pColumns, m_rank));
305  m_commColumn =
306  std::shared_ptr<FieldConvertComm>(new FieldConvertComm(pRows, 0));
307  }
308 
309 protected:
310  int v_GetRank(void)
311  {
312  return m_rank;
313  }
314 
315  bool v_TreatAsRankZero(void)
316  {
317  return true;
318  }
319 
320  bool v_IsSerial(void)
321  {
322  return true;
323  }
324 
326  {
327  return false;
328  }
329 
330 private:
331  int m_rank;
332 };
333 }
334 }
335 
336 #endif
void swap_endian(T &u)
Swap endian ordering of the input variable.
std::map< std::string, ConfigOption > m_config
List of configuration values.
Represents a command-line configuration option.
T as()
Re-interpret the value stored in #value as some type using boost::lexical_cast.
std::string m_desc
Description of the configuration option.
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
FieldConvertComm(int argc, char *argv[], int size, int rank)
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
std::string m_value
The value of the configuration option.
Abstract base class for input modules.
A global linear system.
Definition: CommSerial.h:54
std::pair< ModuleType, std::string > ModuleKey
const char *const ModuleTypeMap[]
std::string m_defValue
Default value of the configuration option.
virtual std::string GetModuleDescription()
std::shared_ptr< InputModule > InputModuleSharedPtr
std::ofstream m_fldFile
Output stream.
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:65
std::shared_ptr< Module > ModuleSharedPtr
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
#define FIELD_UTILS_EXPORT
std::set< std::string > m_allowedFiles
void v_SplitComm(int pRows, int pColumns)
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
bool m_beenSet
True if the configuration option has been set at command line. If false, the default value will be pu...
Abstract base class for processing modules.
bool m_isBool
True if the configuration option is a boolean (thus does not need additional arguments).
Abstract base class for output modules.
Provides a generic Factory class.
Definition: NekFactory.hpp:103
ModuleFactory & GetModuleFactory()
FIELD_UTILS_EXPORT Module(FieldSharedPtr p_f)