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