Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FieldConvert/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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Abstract input/output modules.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <iomanip>
37 
38 #include "Module.h"
39 
40 using namespace std;
41 
42 namespace Nektar
43 {
44  namespace Utilities
45  {
46  /**
47  * Returns an instance of the module factory, held as a singleton.
48  */
50  {
51  typedef Loki::SingletonHolder<ModuleFactory,
52  Loki::CreateUsingNew,
53  Loki::NoDestroy,
54  Loki::SingleThreaded> Type;
55  return Type::Instance();
56  }
57 
58  /**
59  * Prints a given module key to a stream.
60  */
61  std::ostream& operator<<(std::ostream& os, const ModuleKey& rhs)
62  {
63  return os << ModuleTypeMap[rhs.first] << ": " << rhs.second;
64  }
65 
66  InputModule::InputModule(FieldSharedPtr m) : Module(m)
67  {
68  m_config["infile"] = ConfigOption(false, "", "Input filename.");
69  }
70 
72  {
73  m_config["outfile"] = ConfigOption(false, "", "Output filename.");
74  }
75 
76  void InputModule::AddFile(string fileType, string fileName)
77  {
78  // Check to see if this file type is allowed
79  if (m_allowedFiles.count(fileType) == 0)
80  {
81  cerr << "File type " << fileType << " not supported for this "
82  << "module." << endl;
83  }
84 
85  m_f->m_inputfiles[fileType].push_back(fileName);
86  }
87  /**
88  * @brief Open a file for output.
89  */
91  {
92  string fname = m_config["outfile"].as<string>();
93  m_fldFile.open(fname.c_str());
94  if (!m_fldFile.good())
95  {
96  cerr << "Error opening file: " << fname << endl;
97  abort();
98  }
99  }
100 
101  /**
102  * @brief Register a configuration option with a module.
103  */
104  void Module::RegisterConfig(string key, string val)
105  {
107  if (it == m_config.end())
108  {
109  cerr << "WARNING: Unrecognised config option " << key
110  << ", proceeding anyway." << endl;
111  }
112 
113  it->second.m_beenSet = true;
114 
115  if (it->second.m_isBool)
116  {
117  it->second.m_value = "1";
118  }
119  else
120  {
121  it->second.m_value = val;
122  }
123  }
124 
125  /**
126  * @brief Print out all configuration options for a module.
127  */
129  {
131 
132  if (m_config.size() == 0)
133  {
134  cerr << "No configuration options for this module." << endl;
135  return;
136  }
137 
138  for (it = m_config.begin(); it != m_config.end(); ++it)
139  {
140  cerr << setw(10) << it->first << ": " << it->second.m_desc
141  << endl;
142  }
143  }
144 
145  /**
146  * @brief Sets default configuration options for those which have not
147  * been set.
148  */
150  {
152 
153  for (it = m_config.begin(); it != m_config.end(); ++it)
154  {
155  if (!it->second.m_beenSet)
156  {
157  it->second.m_value = it->second.m_defValue;
158  }
159  }
160  }
161 
162  /**
163  * @brief Print a brief summary of information.
164  */
166  {
167  cout << "Field size = " <<
168  m_f->m_data[0].size() * sizeof(NekDouble) << endl;
169  }
170  }
171 }
ofstream m_fldFile
Output stream.
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
pair< ModuleType, string > ModuleKey
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
void RegisterConfig(string key, string value)
Register a configuration option with a module.
FieldSharedPtr m_f
Field object.
double NekDouble
void AddFile(string fileType, string fileName)
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:698
void PrintSummary()
Print summary of elements.
void PrintConfig()
Print out all configuration options for a module.
Represents a command-line configuration option.
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
void OpenStream()
Open a file for output.
void SetDefaults()
Sets default configuration options for those which have not been set.
ModuleFactory & GetModuleFactory()
const char *const ModuleTypeMap[]
Provides a generic Factory class.
Definition: NekFactory.hpp:116