Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NekMesh/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 // 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: Mesh converter module base classes.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef UTILITIES_NEKMESH_MODULE
37 #define UTILITIES_NEKMESH_MODULE
38 
39 #include <map>
40 #include <vector>
41 #include <sstream>
42 #include <string>
43 #include <iostream>
44 #include <fstream>
45 #include <set>
46 
49 
50 namespace Nektar
51 {
52  namespace Utilities
53  {
54  using namespace Nektar::NekMeshUtils;
55 
56  /**
57  * Denotes different types of mesh converter modules: so far only
58  * input, output and process modules are defined.
59  */
60  enum ModuleType {
65  };
66 
67  const char* const ModuleTypeMap[] =
68  {
69  "Input",
70  "Process",
71  "Output"
72  };
73 
74  typedef std::map<int, std::pair<FaceSharedPtr, std::vector<int> > > PerMap;
75 
76  /**
77  * @brief Represents a command-line configuration option.
78  */
79  struct ConfigOption
80  {
81  /**
82  * @brief Construct a new configuration option.
83  *
84  * @param isBool True if the option is boolean type.
85  * @param defValue Default value of the option.
86  * @param desc Description of the option.
87  */
88  ConfigOption(bool isBool, std::string defValue, std::string desc) :
89  isBool(isBool), beenSet(false), value(), defValue(defValue),
90  desc(desc) {}
92  isBool(false), beenSet(false), value(), defValue(), desc() {}
93 
94  /**
95  * @brief Re-interpret the value stored in #value as some type using
96  * boost::lexical_cast.
97  */
98  template<typename T>
99  T as()
100  {
101  try
102  {
103  return boost::lexical_cast<T>(value);
104  }
105  catch(const std::exception &e)
106  {
107  std::cerr << e.what() << std::endl;
108  abort();
109  }
110  }
111 
112  /**
113  * @brief Interpret the value stored in #value as some type using
114  * boost::lexical_cast and return true of false depending on cast
115  */
116  template<typename T>
117  bool isType()
118  {
119  bool returnval = true;
120  try
121  {
122  boost::lexical_cast<T>(value);
123  }
124  catch(const std::exception &e)
125  {
126  returnval = false;
127  }
128 
129  return returnval;
130  }
131 
132 
133  /// True if the configuration option is a boolean (thus does not
134  /// need additional arguments).
135  bool isBool;
136  /// True if the configuration option has been set at command
137  /// line. If false, the default value will be put into #value.
138  bool beenSet;
139  /// The value of the configuration option.
140  std::string value;
141  /// Default value of the configuration option.
142  std::string defValue;
143  /// Description of the configuration option.
144  std::string desc;
145  };
146 
147 
148  /**
149  * Abstract base class for mesh converter modules. Each subclass
150  * implements the Process() function, which in some way alters the
151  * mesh #m.
152  */
153  class Module
154  {
155  public:
156  Module(MeshSharedPtr p_m) : m_mesh(p_m) {}
157  virtual void Process() = 0;
158 
159  void RegisterConfig(std::string key, std::string value);
160  void PrintConfig();
161  void SetDefaults();
163  {
164  return m_mesh;
165  }
166 
167  /// Extract element vertices
168  virtual void ProcessVertices();
169  /// Extract element edges
170  virtual void ProcessEdges(bool ReprocessEdges = true);
171  /// Extract element faces
172  virtual void ProcessFaces(bool ReprocessFaces = true);
173  /// Generate element IDs
174  virtual void ProcessElements();
175  /// Generate composites
176  virtual void ProcessComposites();
177 
178  virtual void ClearElementLinks();
179 
180  protected:
181  /// Mesh object
183  /// List of configuration values.
184  std::map<std::string, ConfigOption> m_config;
185 
186 
187 
188  void ReorderPrisms(PerMap &perFaces);
189  void PrismLines (int prism,
190  PerMap &perFaces,
191  std::set<int> &prismsDone,
192  std::vector<ElementSharedPtr> &line);
193  };
194 
195  /**
196  * @brief Abstract base class for input modules.
197  *
198  * Input modules should read the contents of #m_mshFile in the Process()
199  * function and populate the members of #m. Typically any given module
200  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
201  * Mesh::element, then call the protected ProcessX functions to
202  * generate edges, faces, etc.
203  */
204  class InputModule : public Module
205  {
206  public:
208  void OpenStream();
209 
210  protected:
211  /// Print summary of elements.
212  void PrintSummary();
213  /// Input stream
214  std::ifstream m_mshFile;
215  };
216 
217  /**
218  * @brief Abstract base class for processing modules.
219  *
220  * Processing modules take a populated %Mesh object and process it in
221  * some fashion; for example the %ProcessJac module calculates the
222  * Jacobian of each element and prints warnings for non-positive
223  * elements.
224  */
225  class ProcessModule : public Module
226  {
227  public:
229  };
230 
231  /**
232  * @brief Abstract base class for output modules.
233  *
234  * Output modules take the mesh #m and write to the file specified by
235  * the stream #m_mshFile.
236  */
237  class OutputModule : public Module
238  {
239  public:
240  OutputModule(MeshSharedPtr p_m);
241  void OpenStream();
242 
243 
244  protected:
245  /// Output stream
246  std::ofstream m_mshFile;
247  };
248 
249  typedef std::pair<ModuleType,std::string> ModuleKey;
250  std::ostream& operator<<(std::ostream& os, const ModuleKey& rhs);
251 
252  typedef boost::shared_ptr<Module> ModuleSharedPtr;
254 
255  ModuleFactory& GetModuleFactory();
256  }
257 }
258 
259 #endif
Module(MeshSharedPtr p_m)
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
bool isType()
Interpret the value stored in value as some type using boost::lexical_cast and return true of false d...
std::ifstream m_mshFile
Input stream.
pair< ModuleType, string > ModuleKey
bool isBool
True if the configuration option is a boolean (thus does not need additional arguments).
bool beenSet
True if the configuration option has been set at command line. If false, the default value will be pu...
MeshSharedPtr m_mesh
Mesh object.
std::string defValue
Default value of the configuration option.
T as()
Re-interpret the value stored in value as some type using boost::lexical_cast.
std::string value
The value of the configuration option.
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
boost::shared_ptr< Module > ModuleSharedPtr
std::string desc
Description of the configuration option.
Represents a command-line configuration option.
std::map< int, std::pair< FaceSharedPtr, std::vector< int > > > PerMap
boost::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:137
std::ofstream m_mshFile
Output stream.
std::map< std::string, ConfigOption > m_config
List of configuration values.
ModuleFactory & GetModuleFactory()
const char *const ModuleTypeMap[]
Abstract base class for input modules.
Abstract base class for processing modules.
Provides a generic Factory class.
Definition: NekFactory.hpp:116