Nektar++
Module.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: Module.cpp
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: Abstract input/output modules.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <boost/algorithm/string.hpp>
36#include <iomanip>
37
38#include "Module.h"
39
40using namespace std;
41
42namespace Nektar::FieldUtils
43{
44/**
45 * Returns an instance of the module factory, held as a singleton.
46 */
48{
49 static ModuleFactory instance;
50 return instance;
51}
52
53/**
54 * Prints a given module key to a stream.
55 */
56std::ostream &operator<<(std::ostream &os, const ModuleKey &rhs)
57{
58 return os << ModuleTypeMap[rhs.first] << ": " << rhs.second;
59}
60
62{
63 m_config["infile"] = ConfigOption(false, "", "Input filename.");
64}
65
67{
68 m_config["outfile"] = ConfigOption(false, "", "Output filename.");
69}
70
71void Module::AddFile(string fileType, string fileName)
72{
73 // Check to see if this file type is allowed
74 if (m_allowedFiles.count(fileType) == 0)
75 {
76 cerr << "File type " << fileType << " not supported for this "
77 << "module." << endl;
78 }
79
80 m_f->m_inputfiles[fileType].push_back(fileName);
81}
82
83/**
84 * @brief Open a file for output.
85 */
87{
88 string fname = m_config["outfile"].as<string>();
89 m_fldFile.open(fname.c_str());
90 if (!m_fldFile.good())
91 {
92 cerr << "Error opening file: " << fname << endl;
93 abort();
94 }
95}
96
97/**
98 * @brief Register a configuration option with a module.
99 */
100void Module::RegisterConfig(string key, string val)
101{
102 auto it = m_config.find(key);
103 if (it == m_config.end())
104 {
105 cerr << "WARNING: Unrecognised config option " << key
106 << ", proceeding anyway." << endl;
107 ConfigOption conf(false, "", "");
108 conf.m_beenSet = true;
109 conf.m_value = val;
110 m_config[key] = conf;
111 }
112 else
113 {
114 it->second.m_beenSet = true;
115
116 if (it->second.m_isBool && val == "")
117 {
118 it->second.m_value = "1";
119 }
120 else
121 {
122 it->second.m_value = val;
123 }
124 }
125}
126
127/**
128 * @brief Print out all configuration options for a module.
129 */
131{
132 if (m_config.size() == 0)
133 {
134 cerr << "No configuration options for this module." << endl;
135 return;
136 }
137
138 for (auto &it : m_config)
139 {
140 cerr << setw(10) << it.first << ": " << it.second.m_desc << endl;
141 }
142}
143
144/**
145 * @brief Sets default configuration options for those which have not
146 * been set.
147 */
149{
150 for (auto &it : m_config)
151 {
152 if (!it.second.m_beenSet)
153 {
154 it.second.m_value = it.second.m_defValue;
155 }
156 }
157}
158
159/**
160 * @brief Tries to guess the format of the input file.
161 */
162string InputModule::GuessFormat(string filename)
163{
164 // Read first 64 bytes of data, assuming input is this long.
165 ifstream inFile(filename.c_str(), ios::binary);
166 vector<char> data(64, 0);
167 inFile.read(&data[0], 64);
168
169 string check(&data[0], 64);
170
171 // Nek5000 format: first four characters are: #std
172 if (check.compare(0, 4, "#std") == 0)
173 {
174 inFile.close();
175 return "fld5000";
176 }
177
178 // Semtex format: first line should contain the string "Session" at
179 // character 27.
180 if (check.compare(26, 7, "Session") == 0)
181 {
182 inFile.close();
183 return "fldsem";
184 }
185
186 // Otherwise don't really know -- try to guess from file extension.
187 inFile.close();
188 return "";
189}
190
191/**
192 * @brief Print a brief summary of information.
193 */
195{
196 cout << "Field size = " << m_f->m_data[0].size() * sizeof(NekDouble)
197 << endl;
198}
199} // namespace Nektar::FieldUtils
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
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:274
FieldSharedPtr m_f
Field object.
Definition: Module.h:239
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
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
Definition: Module.cpp:148
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
Provides a generic Factory class.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:1026
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
Definition: Module.cpp:56
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
const std::string ModuleTypeMap[]
Definition: Module.h:72
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
double NekDouble
STL namespace.
Represents a command-line configuration option.
Definition: Module.h:129
std::string m_value
The value of the configuration option.
Definition: Module.h:173
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