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