Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PreProcessing/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  * Abstract base class for mesh converter modules. Each subclass
142  * implements the Process() function, which in some way alters the
143  * mesh #m.
144  */
145  class Module
146  {
147  public:
148  Module(MeshSharedPtr p_m) : m_mesh(p_m) {}
149  virtual void Process() = 0;
150 
151  void RegisterConfig(string key, string value);
152  void PrintConfig();
153  void SetDefaults();
155  {
156  return m_mesh;
157  }
158 
159  /// Extract element vertices
160  virtual void ProcessVertices();
161 
162  protected:
163  /// Mesh object
165  /// List of configuration values.
166  map<string, ConfigOption> m_config;
167 
168  /// Extract element edges
169  virtual void ProcessEdges(bool ReprocessEdges = true);
170  /// Extract element faces
171  virtual void ProcessFaces(bool ReprocessFaces = true);
172  /// Generate element IDs
173  virtual void ProcessElements();
174  /// Generate composites
175  virtual void ProcessComposites();
176 
177  void ReorderPrisms(PerMap &perFaces);
178  void PrismLines (int prism,
179  PerMap &perFaces,
180  set<int> &prismsDone,
181  vector<ElementSharedPtr> &line);
182  };
183 
184  /**
185  * @brief Abstract base class for input modules.
186  *
187  * Input modules should read the contents of #m_mshFile in the Process()
188  * function and populate the members of #m. Typically any given module
189  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
190  * Mesh::element, then call the protected ProcessX functions to
191  * generate edges, faces, etc.
192  */
193  class InputModule : public Module
194  {
195  public:
197  void OpenStream();
198 
199  protected:
200  /// Print summary of elements.
201  void PrintSummary();
202  /// Input stream
203  std::ifstream m_mshFile;
204  };
205 
206  /**
207  * @brief Abstract base class for processing modules.
208  *
209  * Processing modules take a populated %Mesh object and process it in
210  * some fashion; for example the %ProcessJac module calculates the
211  * Jacobian of each element and prints warnings for non-positive
212  * elements.
213  */
214  class ProcessModule : public Module
215  {
216  public:
218  };
219 
220  /**
221  * @brief Abstract base class for output modules.
222  *
223  * Output modules take the mesh #m and write to the file specified by
224  * the stream #m_mshFile.
225  */
226  class OutputModule : public Module
227  {
228  public:
230  void OpenStream();
231 
232  protected:
233  /// Output stream
234  std::ofstream m_mshFile;
235  };
236 
237  typedef std::pair<ModuleType,std::string> ModuleKey;
238  std::ostream& operator<<(std::ostream& os, const ModuleKey& rhs);
239 
240  typedef boost::shared_ptr<Module> ModuleSharedPtr;
242 
244  }
245 }
246 
247 #endif