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