Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PostProcessing/FieldConvert/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 <map>
41 #include <iostream>
42 #include <fstream>
43 #include <vector>
44 #include <set>
45 
48 
49 #include "Field.hpp"
50 
51 namespace po = boost::program_options;
52 
53 namespace Nektar
54 {
55  namespace Utilities
56  {
57  using namespace std;
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  /**
78  * @brief Represents a command-line configuration option.
79  */
80  struct ConfigOption
81  {
82  /**
83  * @brief Construct a new configuration option.
84  *
85  * @param isBool True if the option is boolean type.
86  * @param defValue Default value of the option.
87  * @param desc Description of the option.
88  */
89  ConfigOption(bool isBool, string defValue, string desc) :
90  m_isBool(isBool), m_beenSet(false), m_value(),
91  m_defValue(defValue), m_desc(desc) {}
93  m_isBool(false), m_beenSet(false), m_value(),
94  m_defValue(), m_desc() {}
95 
96  /**
97  * @brief Re-interpret the value stored in #value as some type using
98  * boost::lexical_cast.
99  */
100  template<typename T>
101  T as()
102  {
103  try
104  {
105  return boost::lexical_cast<T>(m_value);
106  }
107  catch(const exception &e)
108  {
109  cerr << e.what() << endl;
110  abort();
111  }
112  }
113 
114  /// True if the configuration option is a boolean (thus does not
115  /// need additional arguments).
116  bool m_isBool;
117  /// True if the configuration option has been set at command
118  /// line. If false, the default value will be put into #value.
119  bool m_beenSet;
120  /// The value of the configuration option.
121  string m_value;
122  /// Default value of the configuration option.
123  string m_defValue;
124  /// Description of the configuration option.
125  string m_desc;
126  };
127 
128  /**
129  * Abstract base class for mesh converter modules. Each subclass
130  * implements the Process() function, which in some way alters the
131  * mesh #m.
132  */
133  class Module
134  {
135  public:
136  Module(FieldSharedPtr p_f) : m_f(p_f), m_requireEquiSpaced(false) {}
137  virtual void Process(po::variables_map &vm) = 0;
138 
139  void RegisterConfig(string key, string value);
140  void PrintConfig();
141  void SetDefaults();
142 
143  bool GetRequireEquiSpaced(void)
144  {
145  return m_requireEquiSpaced;
146  }
147 
148  void SetRequireEquiSpaced(bool pVal)
149  {
150  m_requireEquiSpaced = pVal;
151  }
152 
153  protected:
154  /// Field object
156  /// List of configuration values.
157  map<string, ConfigOption> m_config;
159 
160  };
161 
162  /**
163  * @brief Abstract base class for input modules.
164  *
165  * Input modules should read the contents of #fldFile in the Process()
166  * function and populate the members of #m. Typically any given module
167  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
168  * Mesh::element, then call the protected ProcessX functions to
169  * generate edges, faces, etc.
170  */
171  class InputModule : public Module
172  {
173  public:
175  void AddFile(string fileType, string fileName);
176 
177  protected:
178  /// Print summary of elements.
179  void PrintSummary();
180  set<string> m_allowedFiles;
181 
182  };
183 
184  typedef boost::shared_ptr<InputModule> InputModuleSharedPtr;
185 
186  /**
187  * @brief Abstract base class for processing modules.
188  *
189  */
190  class ProcessModule : public Module
191  {
192  public:
194 
195  };
196 
197  /**
198  * @brief Abstract base class for output modules.
199  *
200  * Output modules take the mesh #m and write to the file specified by
201  * the stream.
202  */
203  class OutputModule : public Module
204  {
205  public:
207  void OpenStream();
208 
209  protected:
210  /// Output stream
211  ofstream m_fldFile;
212 
213  };
214 
215  typedef pair<ModuleType,string> ModuleKey;
216  ostream& operator<<(ostream& os, const ModuleKey& rhs);
217 
218  typedef boost::shared_ptr<Module> ModuleSharedPtr;
221 
223 
225  {
226  public:
227  FieldConvertComm(int argc, char* argv[], int size, int rank) : CommSerial(argc, argv)
228  {
229  m_size = size;
230  m_rank = rank;
231  m_type = "FieldConvert parallel";
232  }
233  FieldConvertComm(int size, int rank) : CommSerial(0, NULL)
234  {
235  m_size = size;
236  m_rank = rank;
237  m_type = "FieldConvert parallel";
238  }
239  virtual ~FieldConvertComm() {}
240  void v_SplitComm(int pRows, int pColumns)
241  {
242  // Compute row and column in grid.
243  m_commRow = boost::shared_ptr<FieldConvertComm>(new FieldConvertComm(pColumns,m_rank));
244  m_commColumn = boost::shared_ptr<FieldConvertComm>(new FieldConvertComm(pRows,0));
245  }
246 
247  protected:
248  int v_GetRank(void)
249  {
250  return m_rank;
251  }
252 
253  bool v_TreatAsRankZero(void)
254  {
255  return true;
256  }
257 
258  bool v_RemoveExistingFiles(void)
259  {
260  return false;
261  }
262  private:
263  int m_rank;
264  };
265  }
266 }
267 
268 #endif