Nektar++
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 // 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: Mesh converter module base classes.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKMESHUTILS_MODULE
36 #define NEKMESHUTILS_MODULE
37 
38 #include <boost/iostreams/device/file_descriptor.hpp>
39 #include <boost/iostreams/filtering_stream.hpp>
40 
41 #include <map>
42 #include <vector>
43 #include <sstream>
44 #include <string>
45 #include <iostream>
46 #include <fstream>
47 #include <set>
48 
52 
53 namespace io = boost::iostreams;
54 
55 namespace Nektar
56 {
57 namespace NekMeshUtils
58 {
59  /**
60  * Denotes different types of mesh converter modules: so far only
61  * input, output and process modules are defined.
62  */
63  enum ModuleType {
68  };
69 
70  const char* const ModuleTypeMap[] =
71  {
72  "Input",
73  "Process",
74  "Output"
75  };
76 
77  typedef std::map<int, std::pair<FaceSharedPtr, std::vector<int> > > PerMap;
78 
79  /**
80  * @brief Represents a command-line configuration option.
81  */
82  struct ConfigOption
83  {
84  /**
85  * @brief Construct a new configuration option.
86  *
87  * @param isBool True if the option is boolean type.
88  * @param defValue Default value of the option.
89  * @param desc Description of the option.
90  */
91  ConfigOption(bool isBool, std::string defValue, std::string desc) :
93  desc(desc) {}
95  isBool(false), beenSet(false), value(), defValue(), desc() {}
96 
97  /**
98  * @brief Re-interpret the value stored in #value as some type using
99  * boost::lexical_cast.
100  */
101  template<typename T>
102  T as()
103  {
104  try
105  {
106  return boost::lexical_cast<T>(value);
107  }
108  catch(const std::exception &e)
109  {
110  std::cerr << e.what() << std::endl;
111  abort();
112  }
113  }
114 
115  /**
116  * @brief Interpret the value stored in #value as some type using
117  * boost::lexical_cast and return true of false depending on cast
118  */
119  template<typename T>
120  bool isType()
121  {
122  bool returnval = true;
123  try
124  {
125  boost::lexical_cast<T>(value);
126  }
127  catch(const std::exception &e)
128  {
129  returnval = false;
130  }
131 
132  return returnval;
133  }
134 
135 
136  /// True if the configuration option is a boolean (thus does not
137  /// need additional arguments).
138  bool isBool;
139  /// True if the configuration option has been set at command
140  /// line. If false, the default value will be put into #value.
141  bool beenSet;
142  /// The value of the configuration option.
143  std::string value;
144  /// Default value of the configuration option.
145  std::string defValue;
146  /// Description of the configuration option.
147  std::string desc;
148  };
149 
150 
151  /**
152  * Abstract base class for mesh converter modules. Each subclass
153  * implements the Process() function, which in some way alters the
154  * mesh #m.
155  */
156  class Module
157  {
158  public:
160  NEKMESHUTILS_EXPORT virtual void Process() = 0;
161 
162  NEKMESHUTILS_EXPORT void RegisterConfig(std::string key,
163  std::string value = std::string());
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 std::shared_ptr<Module> ModuleSharedPtr;
260 
262 }
263 }
264 
265 #endif
#define NEKMESHUTILS_EXPORT
Provides a generic Factory class.
Definition: NekFactory.hpp:104
Abstract base class for input modules.
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for input.
NEKMESHUTILS_EXPORT void PrintSummary()
Print summary of elements.
NEKMESHUTILS_EXPORT InputModule(MeshSharedPtr p_m)
io::filtering_istream m_mshFile
Input stream.
NEKMESHUTILS_EXPORT Module(MeshSharedPtr p_m)
virtual NEKMESHUTILS_EXPORT void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
virtual NEKMESHUTILS_EXPORT void ClearElementLinks()
virtual NEKMESHUTILS_EXPORT void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
NEKMESHUTILS_EXPORT void ReorderPrisms(PerMap &perFaces)
Reorder node IDs so that prisms and tetrahedra are aligned correctly.
NEKMESHUTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
NEKMESHUTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
NEKMESHUTILS_EXPORT MeshSharedPtr GetMesh()
virtual NEKMESHUTILS_EXPORT void Process()=0
virtual NEKMESHUTILS_EXPORT void ProcessElements()
Generate element IDs.
virtual NEKMESHUTILS_EXPORT void ProcessVertices()
Extract element vertices.
std::map< std::string, ConfigOption > m_config
List of configuration values.
virtual NEKMESHUTILS_EXPORT void ProcessComposites()
Generate composites.
NEKMESHUTILS_EXPORT void RegisterConfig(std::string key, std::string value=std::string())
Register a configuration option with a module.
NEKMESHUTILS_EXPORT void PrismLines(int prism, PerMap &perFaces, std::set< int > &prismsDone, std::vector< ElementSharedPtr > &line)
Abstract base class for output modules.
NEKMESHUTILS_EXPORT OutputModule(MeshSharedPtr p_m)
io::filtering_ostream m_mshFile
Output stream.
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for output.
Abstract base class for processing modules.
NEKMESHUTILS_EXPORT ProcessModule(MeshSharedPtr p_m)
std::pair< ModuleType, std::string > ModuleKey
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
const char *const ModuleTypeMap[]
std::map< int, std::pair< FaceSharedPtr, std::vector< int > > > PerMap
LibUtilities::NekFactory< ModuleKey, Module, MeshSharedPtr > ModuleFactory
std::ostream & operator<<(std::ostream &os, const NodeSharedPtr &n)
Print description of node to given ostream.
std::shared_ptr< Module > ModuleSharedPtr
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
Represents a command-line configuration option.
T as()
Re-interpret the value stored in value as some type using boost::lexical_cast.
bool isBool
True if the configuration option is a boolean (thus does not need additional arguments).
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
std::string desc
Description of the configuration option.
bool isType()
Interpret the value stored in value as some type using boost::lexical_cast and return true of false d...
std::string defValue
Default value 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...
std::string value
The value of the configuration option.