Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // 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 FieldUtils
45 {
46 /**
47  * Returns an instance of the module factory, held as a singleton.
48  */
50 {
51  typedef Loki::SingletonHolder<ModuleFactory, Loki::CreateUsingNew,
52  Loki::NoDestroy, Loki::SingleThreaded>
53  Type;
54  return Type::Instance();
55 }
56 
57 /**
58  * Prints a given module key to a stream.
59  */
60 std::ostream &operator<<(std::ostream &os, const ModuleKey &rhs)
61 {
62  return os << ModuleTypeMap[rhs.first] << ": " << rhs.second;
63 }
64 
65 InputModule::InputModule(FieldSharedPtr m) : Module(m)
66 {
67  m_config["infile"] = ConfigOption(false, "", "Input filename.");
68 }
69 
71 {
72  m_config["outfile"] = ConfigOption(false, "", "Output filename.");
73 }
74 
75 void InputModule::AddFile(string fileType, string fileName)
76 {
77  // Check to see if this file type is allowed
78  if (m_allowedFiles.count(fileType) == 0)
79  {
80  cerr << "File type " << fileType << " not supported for this "
81  << "module." << endl;
82  }
83 
84  m_f->m_inputfiles[fileType].push_back(fileName);
85 }
86 /**
87  * @brief Open a file for output.
88  */
90 {
91  string fname = m_config["outfile"].as<string>();
92  m_fldFile.open(fname.c_str());
93  if (!m_fldFile.good())
94  {
95  cerr << "Error opening file: " << fname << endl;
96  abort();
97  }
98 }
99 
100 /**
101  * @brief Register a configuration option with a module.
102  */
103 void Module::RegisterConfig(string key, string val)
104 {
106  if (it == m_config.end())
107  {
108  cerr << "WARNING: Unrecognised config option " << key
109  << ", proceeding anyway." << endl;
110  }
111 
112  it->second.m_beenSet = true;
113 
114  if (it->second.m_isBool)
115  {
116  it->second.m_value = "1";
117  }
118  else
119  {
120  it->second.m_value = val;
121  }
122 }
123 
124 /**
125  * @brief Print out all configuration options for a module.
126  */
128 {
130 
131  if (m_config.size() == 0)
132  {
133  cerr << "No configuration options for this module." << endl;
134  return;
135  }
136 
137  for (it = m_config.begin(); it != m_config.end(); ++it)
138  {
139  cerr << setw(10) << it->first << ": " << it->second.m_desc << endl;
140  }
141 }
142 
143 /**
144  * @brief Sets default configuration options for those which have not
145  * been set.
146  */
148 {
150 
151  for (it = m_config.begin(); it != m_config.end(); ++it)
152  {
153  if (!it->second.m_beenSet)
154  {
155  it->second.m_value = it->second.m_defValue;
156  }
157  }
158 }
159 
160 /**
161  * @brief Print a brief summary of information.
162  */
164 {
165  cout << "Field size = " << m_f->m_data[0].size() * sizeof(NekDouble)
166  << endl;
167 }
168 }
169 }
map< string, ConfigOption > m_config
List of configuration values.
Represents a command-line configuration option.
std::ostream & operator<<(std::ostream &os, const ModuleKey &rhs)
LibUtilities::NekFactory< ModuleKey, Module, FieldSharedPtr > ModuleFactory
STL namespace.
pair< ModuleType, string > ModuleKey
FIELD_UTILS_EXPORT void SetDefaults()
Sets default configuration options for those which have not been set.
void PrintSummary()
Print summary of elements.
const char *const ModuleTypeMap[]
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:740
double NekDouble
FIELD_UTILS_EXPORT void AddFile(string fileType, string fileName)
FIELD_UTILS_EXPORT void RegisterConfig(string key, string value)
Register a configuration option with a module.
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
ofstream m_fldFile
Output stream.
FIELD_UTILS_EXPORT void OpenStream()
Open a file for output.
FIELD_UTILS_EXPORT void PrintConfig()
Print out all configuration options for a module.
Provides a generic Factory class.
Definition: NekFactory.hpp:116
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.