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