Module.cpp 5.31 KB
Newer Older
Douglas Serson's avatar
Douglas Serson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
////////////////////////////////////////////////////////////////////////////////
//
//  File: Module.cpp
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
//  Description: Abstract input/output modules.
//
////////////////////////////////////////////////////////////////////////////////

#include <iomanip>

#include "Module.h"

using namespace std;

namespace Nektar
{
namespace FieldUtils
{
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Returns an instance of the module factory, held as a singleton.
 */
ModuleFactory &GetModuleFactory()
{
    typedef Loki::SingletonHolder<ModuleFactory, Loki::CreateUsingNew,
                                  Loki::NoDestroy, Loki::SingleThreaded>
        Type;
    return Type::Instance();
}

/**
 * Prints a given module key to a stream.
 */
std::ostream &operator<<(std::ostream &os, const ModuleKey &rhs)
{
    return os << ModuleTypeMap[rhs.first] << ": " << rhs.second;
}

InputModule::InputModule(FieldSharedPtr m) : Module(m)
{
    m_config["infile"] = ConfigOption(false, "", "Input filename.");
}

OutputModule::OutputModule(FieldSharedPtr m) : Module(m)
{
    m_config["outfile"] = ConfigOption(false, "", "Output filename.");
}
Douglas Serson's avatar
Douglas Serson committed
74

75
76
77
78
void InputModule::AddFile(string fileType, string fileName)
{
    // Check to see if this file type is allowed
    if (m_allowedFiles.count(fileType) == 0)
Douglas Serson's avatar
Douglas Serson committed
79
    {
80
81
        cerr << "File type " << fileType << " not supported for this "
             << "module." << endl;
Douglas Serson's avatar
Douglas Serson committed
82
83
    }

84
85
86
87
88
89
90
91
92
93
    m_f->m_inputfiles[fileType].push_back(fileName);
}
/**
 * @brief Open a file for output.
 */
void OutputModule::OpenStream()
{
    string fname = m_config["outfile"].as<string>();
    m_fldFile.open(fname.c_str());
    if (!m_fldFile.good())
Douglas Serson's avatar
Douglas Serson committed
94
    {
95
96
        cerr << "Error opening file: " << fname << endl;
        abort();
Douglas Serson's avatar
Douglas Serson committed
97
    }
98
}
Douglas Serson's avatar
Douglas Serson committed
99

100
101
102
103
104
105
106
/**
 * @brief Register a configuration option with a module.
 */
void Module::RegisterConfig(string key, string val)
{
    map<string, ConfigOption>::iterator it = m_config.find(key);
    if (it == m_config.end())
Douglas Serson's avatar
Douglas Serson committed
107
    {
108
109
        cerr << "WARNING: Unrecognised config option " << key
             << ", proceeding anyway." << endl;
Douglas Serson's avatar
Douglas Serson committed
110
111
    }

112
    it->second.m_beenSet = true;
Douglas Serson's avatar
Douglas Serson committed
113

114
    if (it->second.m_isBool)
Douglas Serson's avatar
Douglas Serson committed
115
    {
116
        it->second.m_value = "1";
Douglas Serson's avatar
Douglas Serson committed
117
    }
118
    else
Douglas Serson's avatar
Douglas Serson committed
119
    {
120
121
122
        it->second.m_value = val;
    }
}
Douglas Serson's avatar
Douglas Serson committed
123

124
125
126
127
128
129
/**
 * @brief Print out all configuration options for a module.
 */
void Module::PrintConfig()
{
    map<string, ConfigOption>::iterator it;
Douglas Serson's avatar
Douglas Serson committed
130

131
132
133
134
    if (m_config.size() == 0)
    {
        cerr << "No configuration options for this module." << endl;
        return;
Douglas Serson's avatar
Douglas Serson committed
135
136
    }

137
    for (it = m_config.begin(); it != m_config.end(); ++it)
Douglas Serson's avatar
Douglas Serson committed
138
    {
139
        cerr << setw(10) << it->first << ": " << it->second.m_desc << endl;
Douglas Serson's avatar
Douglas Serson committed
140
    }
141
}
Douglas Serson's avatar
Douglas Serson committed
142

143
144
145
146
147
148
149
/**
 * @brief Sets default configuration options for those which have not
 * been set.
 */
void Module::SetDefaults()
{
    map<string, ConfigOption>::iterator it;
Douglas Serson's avatar
Douglas Serson committed
150

151
152
153
    for (it = m_config.begin(); it != m_config.end(); ++it)
    {
        if (!it->second.m_beenSet)
Douglas Serson's avatar
Douglas Serson committed
154
        {
155
            it->second.m_value = it->second.m_defValue;
Douglas Serson's avatar
Douglas Serson committed
156
157
        }
    }
158
}
Douglas Serson's avatar
Douglas Serson committed
159

160
161
162
163
164
165
166
167
168
169
/**
 * @brief Tries to guess the format of the input file.
 */
string InputModule::GuessFormat(string filename)
{
    // Read first 64 bytes of data, assuming input is this long.
    ifstream inFile(filename.c_str(), ios::binary);
    vector<char> data(64, 0);
    inFile.read(&data[0], 64);

170
    string check(&data[0], 64);
171
172

    // Nek5000 format: first four characters are: #std
Dave Moxey's avatar
Dave Moxey committed
173
    if (check.compare(0, 4, "#std") == 0)
174
175
176
177
178
    {
        inFile.close();
        return "fld5000";
    }

Dave Moxey's avatar
Dave Moxey committed
179
180
    // Semtex format: first line should contain the string "Session" at
    // character 27.
Dave Moxey's avatar
Dave Moxey committed
181
    if (check.compare(26, 7, "Session") == 0)
182
183
184
185
186
187
188
189
190
191
    {
        inFile.close();
        return "fldsem";
    }

    // Otherwise don't really know -- try to guess from file extension.
    inFile.close();
    return "";
}

192
193
194
195
196
197
198
199
/**
 * @brief Print a brief summary of information.
 */
void InputModule::PrintSummary()
{
    cout << "Field size = " << m_f->m_data[0].size() * sizeof(NekDouble)
         << endl;
}
Douglas Serson's avatar
Douglas Serson committed
200
201
}
}