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