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