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