Nektar++
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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Field converter module base classes.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#ifndef FIELDUTILS_MODULE
36#define FIELDUTILS_MODULE
37
38#include <fstream>
39#include <iomanip>
40#include <iostream>
41#include <map>
42#include <set>
43#include <string>
44#include <typeinfo>
45#include <vector>
46
51
52#include "Field.hpp"
53
54#include "FieldUtilsDeclspec.h"
55
56namespace po = boost::program_options;
57
58namespace Nektar
59{
60namespace FieldUtils
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
74const std::string ModuleTypeMap[] = {"Input", "Process", "Output"};
75
77{
90};
91
92const char *const ModulePriorityMap[] = {
93 "CreateGraph", "CreateFieldData", "ModifyFieldData", "CreateExp",
94 "FillExp", "ModifyExp", "BndExtraction", "CreatePts",
95 "ConvertExpToPts", "ModifyPts", "Output"};
96
97/**
98 * @brief Swap endian ordering of the input variable.
99 */
100template <typename T> void swap_endian(T &u)
101{
102 union
103 {
104 T u;
105 unsigned char u8[sizeof(T)];
106 } source, dest;
107
108 source.u = u;
109
110 for (size_t k = 0; k < sizeof(T); k++)
111 {
112 dest.u8[k] = source.u8[sizeof(T) - k - 1];
113 }
114
115 u = dest.u;
116}
117
118template <typename T> void swap_endian(std::vector<T> &u)
119{
120 size_t vecSize = u.size();
121 for (size_t i = 0; i < vecSize; ++i)
122 {
123 swap_endian(u[i]);
124 }
125}
126
127/**
128 * @brief Represents a command-line configuration option.
129 */
131{
132 /**
133 * @brief Construct a new configuration option.
134 *
135 * @param isBool True if the option is boolean type.
136 * @param defValue Default value of the option.
137 * @param desc Description of the option.
138 */
139 ConfigOption(bool isBool, std::string defValue, std::string desc)
140 : m_isBool(isBool), m_beenSet(false), m_value(), m_defValue(defValue),
141 m_desc(desc)
142 {
143 }
145 : m_isBool(false), m_beenSet(false), m_value(), m_defValue(), m_desc()
146 {
147 }
148
149 /**
150 * @brief Re-interpret the value stored in #value as some type using
151 * boost::lexical_cast.
152 */
153 template <typename T> T as() const
154 {
155 try
156 {
157 return boost::lexical_cast<T>(m_value);
158 }
159 catch (const std::exception &e)
160 {
161 std::cerr << "We are not able to cast m_value " << m_value << " to "
162 << typeid(T).name() << std::endl
163 << e.what() << std::endl;
164 abort();
165 }
166 }
167
168 /// True if the configuration option is a boolean (thus does not
169 /// need additional arguments).
171 /// True if the configuration option has been set at command
172 /// line. If false, the default value will be put into #value.
174 /// The value of the configuration option.
175 std::string m_value;
176 /// Default value of the configuration option.
177 std::string m_defValue;
178 /// Description of the configuration option.
179 std::string m_desc;
180};
181
182/**
183 * Abstract base class for mesh converter modules. Each subclass
184 * implements the Process() function, which in some way alters the
185 * mesh #m.
186 */
188{
189public:
191 {
192 }
193 virtual ~Module() = default;
194
195 void Process(po::variables_map &vm)
196 {
197 v_Process(vm);
198 }
199
200 std::string GetModuleName()
201 {
202 return v_GetModuleName();
203 }
204
206 {
207 return v_GetModuleDescription();
208 }
209
210 const ConfigOption &GetConfigOption(const std::string &key) const
211 {
212 auto it = m_config.find(key);
213 ASSERTL0(it != m_config.end(), "Configuration key not found!");
214 return it->second;
215 }
216
218 {
219 return v_GetModulePriority();
220 }
221
222 FIELD_UTILS_EXPORT void RegisterConfig(std::string key,
223 std::string value = "");
226 FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName);
227
230 const Array<OneD, const NekDouble> &infield,
231 Array<OneD, NekDouble> &outfield);
232
233 /// Field object
235
236protected:
238
239 virtual void v_Process(po::variables_map &vm)
240 {
241 boost::ignore_unused(vm);
242 NEKERROR(ErrorUtil::efatal, "v_Process not coded");
243 }
244
245 virtual std::string v_GetModuleName()
246 {
247 NEKERROR(ErrorUtil::efatal, "v_GetModuleName not coded");
248 return " ";
249 }
250
251 virtual std::string v_GetModuleDescription()
252 {
253 return " ";
254 }
255
257 {
258 NEKERROR(ErrorUtil::efatal, "v_GetModulePriority not coded");
259 return SIZE_ModulePriority;
260 }
261
262 /// List of configuration values.
263 std::map<std::string, ConfigOption> m_config;
264 /// List of allowed file formats.
265 std::set<std::string> m_allowedFiles;
266};
267
268/**
269 * @brief Abstract base class for input modules.
270 *
271 * Input modules should read the contents of #fldFile in the Process()
272 * function and populate the members of #m. Typically any given module
273 * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
274 * Mesh::element, then call the protected ProcessX functions to
275 * generate edges, faces, etc.
276 */
277class InputModule : public Module
278{
279public:
281 FIELD_UTILS_EXPORT static std::string GuessFormat(std::string fileName);
282 void PrintSummary();
283};
284
285typedef std::shared_ptr<InputModule> InputModuleSharedPtr;
286
287/**
288 * @brief Abstract base class for processing modules.
289 *
290 */
291class ProcessModule : public Module
292{
293public:
296 {
297 }
298};
299
300/**
301 * @brief Abstract base class for output modules.
302 *
303 * Output modules take the mesh #m and write to the file specified by
304 * the stream.
305 */
306class OutputModule : public Module
307{
308public:
311
312protected:
313 /// Output stream
314 std::ofstream m_fldFile;
315};
316
317typedef std::pair<ModuleType, std::string> ModuleKey;
318FIELD_UTILS_EXPORT std::ostream &operator<<(std::ostream &os,
319 const ModuleKey &rhs);
320
321typedef std::shared_ptr<Module> ModuleSharedPtr;
324
326
327} // namespace FieldUtils
328} // namespace Nektar
329
330#endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define FIELD_UTILS_EXPORT
Abstract base class for input modules.
Definition: Module.h:278
void PrintSummary()
Print a brief summary of information.
Definition: Module.cpp:196
InputModule(FieldSharedPtr p_m)
Definition: Module.cpp:63
static FIELD_UTILS_EXPORT std::string GuessFormat(std::string fileName)
Tries to guess the format of the input file.
Definition: Module.cpp:164
FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName)
Definition: Module.cpp:73
FIELD_UTILS_EXPORT void RegisterConfig(std::string key, std::string value="")
Register a configuration option with a module.
Definition: Module.cpp:102
FIELD_UTILS_EXPORT Module(FieldSharedPtr p_f)
Definition: Module.h:190
std::string GetModuleName()
Definition: Module.h:200
virtual void v_Process(po::variables_map &vm)
Definition: Module.h:239
std::string GetModuleDescription()
Definition: Module.h:205
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:265
void Process(po::variables_map &vm)
Definition: Module.h:195
virtual std::string v_GetModuleDescription()
Definition: Module.h:251
virtual std::string v_GetModuleName()
Definition: Module.h:245
ModulePriority GetModulePriority()
Definition: Module.h:217
virtual ~Module()=default
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
const ConfigOption & GetConfigOption(const std::string &key) const
Definition: Module.h:210
FIELD_UTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
Definition: Module.cpp:132
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:263
virtual ModulePriority v_GetModulePriority()
Definition: Module.h:256
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Definition: Module.cpp:150
FIELD_UTILS_EXPORT void EvaluateTriFieldAtEquiSpacedPts(LocalRegions::ExpansionSharedPtr &exp, const Array< OneD, const NekDouble > &infield, Array< OneD, NekDouble > &outfield)
Abstract base class for output modules.
Definition: Module.h:307
OutputModule(FieldSharedPtr p_f)
Definition: Module.cpp:68
std::ofstream m_fldFile
Output stream.
Definition: Module.h:314
FIELD_UTILS_EXPORT void OpenStream()
Open a file for output.
Definition: Module.cpp:88
Abstract base class for processing modules.
Definition: Module.h:292
ProcessModule(FieldSharedPtr p_f)
Definition: Module.h:295
Provides a generic Factory class.
Definition: NekFactory.hpp:105
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
Definition: Module.h:323
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:991
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
Definition: Module.cpp:58
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:317
std::shared_ptr< InputModule > InputModuleSharedPtr
Definition: Module.h:285
const std::string ModuleTypeMap[]
Definition: Module.h:74
const char *const ModulePriorityMap[]
Definition: Module.h:92
void swap_endian(T &u)
Swap endian ordering of the input variable.
Definition: Module.h:100
std::shared_ptr< Module > ModuleSharedPtr
Definition: Module.h:321
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:68
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
Represents a command-line configuration option.
Definition: Module.h:131
std::string m_value
The value of the configuration option.
Definition: Module.h:175
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
Definition: Module.h:139
bool m_beenSet
True if the configuration option has been set at command line. If false, the default value will be pu...
Definition: Module.h:173
std::string m_defValue
Default value of the configuration option.
Definition: Module.h:177
std::string m_desc
Description of the configuration option.
Definition: Module.h:179
bool m_isBool
True if the configuration option is a boolean (thus does not need additional arguments).
Definition: Module.h:170
T as() const
Re-interpret the value stored in #value as some type using boost::lexical_cast.
Definition: Module.h:153