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  void RegisterConfig(string key, string value);
144  void PrintConfig();
145  void SetDefaults();
146 
148  {
149  return m_requireEquiSpaced;
150  }
151 
152  void SetRequireEquiSpaced(bool pVal)
153  {
154  m_requireEquiSpaced = pVal;
155  }
156 
157  void EvaluateTriFieldAtEquiSpacedPts(
159  const Array<OneD, const NekDouble> &infield,
160  Array<OneD, NekDouble> &outfield);
161 
162  protected:
163  Module(){};
164 
165  /// Field object
166  FieldSharedPtr m_f;
167  /// List of configuration values.
168  map<string, ConfigOption> m_config;
170 
171  };
172 
173  /**
174  * @brief Abstract base class for input modules.
175  *
176  * Input modules should read the contents of #fldFile in the Process()
177  * function and populate the members of #m. Typically any given module
178  * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
179  * Mesh::element, then call the protected ProcessX functions to
180  * generate edges, faces, etc.
181  */
182  class InputModule : public Module
183  {
184  public:
186  void AddFile(string fileType, string fileName);
187 
188  protected:
189  /// Print summary of elements.
190  void PrintSummary();
191  set<string> m_allowedFiles;
192 
193  };
194 
195  typedef boost::shared_ptr<InputModule> InputModuleSharedPtr;
196 
197  /**
198  * @brief Abstract base class for processing modules.
199  *
200  */
201  class ProcessModule : public Module
202  {
203  public:
206  };
207 
208  /**
209  * @brief Abstract base class for output modules.
210  *
211  * Output modules take the mesh #m and write to the file specified by
212  * the stream.
213  */
214  class OutputModule : public Module
215  {
216  public:
218  void OpenStream();
219 
220  protected:
221  /// Output stream
222  ofstream m_fldFile;
223  };
224 
225  typedef pair<ModuleType,string> ModuleKey;
226  ostream& operator<<(ostream& os, const ModuleKey& rhs);
227 
228  typedef boost::shared_ptr<Module> ModuleSharedPtr;
231 
233 
235  {
236  public:
237  FieldConvertComm(int argc, char* argv[], int size, int rank) : CommSerial(argc, argv)
238  {
239  m_size = size;
240  m_rank = rank;
241  m_type = "FieldConvert parallel";
242  }
243  FieldConvertComm(int size, int rank) : CommSerial(0, NULL)
244  {
245  m_size = size;
246  m_rank = rank;
247  m_type = "FieldConvert parallel";
248  }
249  virtual ~FieldConvertComm() {}
250  void v_SplitComm(int pRows, int pColumns)
251  {
252  // Compute row and column in grid.
253  m_commRow = boost::shared_ptr<FieldConvertComm>(
254  new FieldConvertComm(pColumns,m_rank));
255  m_commColumn = boost::shared_ptr<FieldConvertComm>(
256  new FieldConvertComm(pRows,0));
257  }
258 
259  protected:
260  int v_GetRank(void)
261  {
262  return m_rank;
263  }
264 
265  bool v_TreatAsRankZero(void)
266  {
267  return true;
268  }
269 
271  {
272  return false;
273  }
274  private:
275  int m_rank;
276  };
277  }
278 }
279 
280 #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