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::FieldUtils
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
72const std::string ModuleTypeMap[] = {"Input", "Process", "Output"};
73
75{
88};
89
90const char *const ModulePriorityMap[] = {
91 "CreateGraph", "CreateFieldData", "ModifyFieldData", "CreateExp",
92 "FillExp", "ModifyExp", "BndExtraction", "CreatePts",
93 "ConvertExpToPts", "ModifyPts", "Output"};
94
95/**
96 * @brief Swap endian ordering of the input variable.
97 */
98template <typename T> void swap_endian(T &u)
99{
100 union
101 {
102 T u;
103 unsigned char u8[sizeof(T)];
104 } source, dest;
105
106 source.u = u;
107
108 for (size_t k = 0; k < sizeof(T); k++)
109 {
110 dest.u8[k] = source.u8[sizeof(T) - k - 1];
111 }
112
113 u = dest.u;
114}
115
116template <typename T> void swap_endian(std::vector<T> &u)
117{
118 size_t vecSize = u.size();
119 for (size_t i = 0; i < vecSize; ++i)
120 {
121 swap_endian(u[i]);
122 }
123}
124
125/**
126 * @brief Represents a command-line configuration option.
127 */
129{
130 /**
131 * @brief Construct a new configuration option.
132 *
133 * @param isBool True if the option is boolean type.
134 * @param defValue Default value of the option.
135 * @param desc Description of the option.
136 */
137 ConfigOption(bool isBool, std::string defValue, std::string desc)
138 : m_isBool(isBool), m_beenSet(false), m_value(), m_defValue(defValue),
139 m_desc(desc)
140 {
141 }
143 : m_isBool(false), m_beenSet(false), m_value(), m_defValue(), m_desc()
144 {
145 }
146
147 /**
148 * @brief Re-interpret the value stored in #value as some type using
149 * boost::lexical_cast.
150 */
151 template <typename T> T as() const
152 {
153 try
154 {
155 return boost::lexical_cast<T>(m_value);
156 }
157 catch (const std::exception &e)
158 {
159 std::cerr << "We are not able to cast m_value " << m_value << " to "
160 << typeid(T).name() << std::endl
161 << e.what() << std::endl;
162 abort();
163 }
164 }
165
166 /// True if the configuration option is a boolean (thus does not
167 /// need additional arguments).
169 /// True if the configuration option has been set at command
170 /// line. If false, the default value will be put into #value.
172 /// The value of the configuration option.
173 std::string m_value;
174 /// Default value of the configuration option.
175 std::string m_defValue;
176 /// Description of the configuration option.
177 std::string m_desc;
178};
179
180typedef std::pair<ModuleType, std::string> ModuleKey;
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 std::vector<ModuleKey> GetModulePrerequisites()
223 {
225 }
226
227 FIELD_UTILS_EXPORT void RegisterConfig(std::string key,
228 std::string value = "");
231 FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName);
232
235 const Array<OneD, const NekDouble> &infield,
236 Array<OneD, NekDouble> &outfield);
237
238 /// Field object
240
241protected:
243
244 virtual void v_Process([[maybe_unused]] po::variables_map &vm)
245 {
246 NEKERROR(ErrorUtil::efatal, "v_Process not coded");
247 }
248
249 virtual std::string v_GetModuleName()
250 {
251 NEKERROR(ErrorUtil::efatal, "v_GetModuleName not coded");
252 return " ";
253 }
254
255 virtual std::string v_GetModuleDescription()
256 {
257 return " ";
258 }
259
261 {
262 NEKERROR(ErrorUtil::efatal, "v_GetModulePriority not coded");
263 return SIZE_ModulePriority;
264 }
265
266 virtual std::vector<ModuleKey> v_GetModulePrerequisites()
267 {
268 return std::vector<ModuleKey>();
269 }
270
271 /// List of configuration values.
272 std::map<std::string, ConfigOption> m_config;
273 /// List of allowed file formats.
274 std::set<std::string> m_allowedFiles;
275};
276
277/**
278 * @brief Abstract base class for input modules.
279 *
280 * Input modules should read the contents of #fldFile in the Process()
281 * function and populate the members of #m. Typically any given module
282 * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
283 * Mesh::element, then call the protected ProcessX functions to
284 * generate edges, faces, etc.
285 */
286class InputModule : public Module
287{
288public:
290 FIELD_UTILS_EXPORT static std::string GuessFormat(std::string fileName);
291 void PrintSummary();
292};
293
294typedef std::shared_ptr<InputModule> InputModuleSharedPtr;
295
296/**
297 * @brief Abstract base class for processing modules.
298 *
299 */
300class ProcessModule : public Module
301{
302public:
305 {
306 }
307};
308
309/**
310 * @brief Abstract base class for output modules.
311 *
312 * Output modules take the mesh #m and write to the file specified by
313 * the stream.
314 */
315class OutputModule : public Module
316{
317public:
320
321protected:
322 /// Output stream
323 std::ofstream m_fldFile;
324};
325
326FIELD_UTILS_EXPORT std::ostream &operator<<(std::ostream &os,
327 const ModuleKey &rhs);
328
329typedef std::shared_ptr<Module> ModuleSharedPtr;
332
334
335} // namespace Nektar::FieldUtils
336
337#endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
#define FIELD_UTILS_EXPORT
Abstract base class for input modules.
Definition: Module.h:287
void PrintSummary()
Print a brief summary of information.
Definition: Module.cpp:194
InputModule(FieldSharedPtr p_m)
Definition: Module.cpp:61
static FIELD_UTILS_EXPORT std::string GuessFormat(std::string fileName)
Tries to guess the format of the input file.
Definition: Module.cpp:162
FIELD_UTILS_EXPORT void AddFile(std::string fileType, std::string fileName)
Definition: Module.cpp:71
FIELD_UTILS_EXPORT void RegisterConfig(std::string key, std::string value="")
Register a configuration option with a module.
Definition: Module.cpp:100
FIELD_UTILS_EXPORT Module(FieldSharedPtr p_f)
Definition: Module.h:190
std::vector< ModuleKey > GetModulePrerequisites()
Definition: Module.h:222
std::string GetModuleName()
Definition: Module.h:200
virtual void v_Process(po::variables_map &vm)
Definition: Module.h:244
std::string GetModuleDescription()
Definition: Module.h:205
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:274
void Process(po::variables_map &vm)
Definition: Module.h:195
virtual std::string v_GetModuleDescription()
Definition: Module.h:255
virtual std::string v_GetModuleName()
Definition: Module.h:249
ModulePriority GetModulePriority()
Definition: Module.h:217
virtual ~Module()=default
FieldSharedPtr m_f
Field object.
Definition: Module.h:239
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:130
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:272
virtual ModulePriority v_GetModulePriority()
Definition: Module.h:260
virtual std::vector< ModuleKey > v_GetModulePrerequisites()
Definition: Module.h:266
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Definition: Module.cpp:148
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:316
OutputModule(FieldSharedPtr p_f)
Definition: Module.cpp:66
std::ofstream m_fldFile
Output stream.
Definition: Module.h:323
FIELD_UTILS_EXPORT void OpenStream()
Open a file for output.
Definition: Module.cpp:86
Abstract base class for processing modules.
Definition: Module.h:301
ProcessModule(FieldSharedPtr p_f)
Definition: Module.h:304
Provides a generic Factory class.
Definition: NekFactory.hpp:104
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
Definition: Module.h:331
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:990
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
Definition: Module.cpp:56
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
std::shared_ptr< InputModule > InputModuleSharedPtr
Definition: Module.h:294
const std::string ModuleTypeMap[]
Definition: Module.h:72
const char *const ModulePriorityMap[]
Definition: Module.h:90
void swap_endian(T &u)
Swap endian ordering of the input variable.
Definition: Module.h:98
std::shared_ptr< Module > ModuleSharedPtr
Definition: Module.h:329
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66
Represents a command-line configuration option.
Definition: Module.h:129
std::string m_value
The value of the configuration option.
Definition: Module.h:173
ConfigOption(bool isBool, std::string defValue, std::string desc)
Construct a new configuration option.
Definition: Module.h:137
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:171
std::string m_defValue
Default value of the configuration option.
Definition: Module.h:175
std::string m_desc
Description of the configuration option.
Definition: Module.h:177
bool m_isBool
True if the configuration option is a boolean (thus does not need additional arguments).
Definition: Module.h:168
T as() const
Re-interpret the value stored in #value as some type using boost::lexical_cast.
Definition: Module.h:151