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