Module.h 8.51 KB
Newer Older
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
////////////////////////////////////////////////////////////////////////////////
//
//  File: Module.h
//
//  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: Mesh converter module base classes.
//
////////////////////////////////////////////////////////////////////////////////

Michael Turner's avatar
Michael Turner committed
36
37
38
39
40
#ifndef NEKMESHUTILS_MODULE
#define NEKMESHUTILS_MODULE

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filtering_stream.hpp>
41

42
43
44
#include <map>
#include <vector>
#include <sstream>
45
46
#include <string>
#include <iostream>
47
48
#include <fstream>
#include <set>
49

50
#include <LibUtilities/BasicUtils/NekFactory.hpp>
51
#include <NekMeshUtils/MeshElements/Mesh.h>
's avatar
committed
52
#include <NekMeshUtils/NekMeshUtilsDeclspec.h>
53

Michael Turner's avatar
Michael Turner committed
54
55
namespace io = boost::iostreams;

56
57
namespace Nektar
{
Michael Turner's avatar
Michael Turner committed
58
59
60
namespace NekMeshUtils
{
    /**
Michael Turner's avatar
Michael Turner committed
61
62
     * Denotes different types of mesh converter modules: so far only
     * input, output and process modules are defined.
Michael Turner's avatar
Michael Turner committed
63
     */
Michael Turner's avatar
Michael Turner committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    enum ModuleType {
        eInputModule,
        eProcessModule,
        eOutputModule,
        SIZE_ModuleType
    };

    const char* const ModuleTypeMap[] =
    {
        "Input",
        "Process",
        "Output"
    };

    typedef std::map<int, std::pair<FaceSharedPtr, std::vector<int> > > PerMap;
Michael Turner's avatar
Michael Turner committed
79
80

    /**
Michael Turner's avatar
Michael Turner committed
81
     * @brief Represents a command-line configuration option.
Michael Turner's avatar
Michael Turner committed
82
     */
Michael Turner's avatar
Michael Turner committed
83
    struct ConfigOption
Michael Turner's avatar
Michael Turner committed
84
    {
Michael Turner's avatar
Michael Turner committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        /**
         * @brief Construct a new configuration option.
         *
         * @param isBool    True if the option is boolean type.
         * @param defValue  Default value of the option.
         * @param desc      Description of the option.
         */
        ConfigOption(bool isBool, std::string defValue, std::string desc) :
            isBool(isBool), beenSet(false), value(), defValue(defValue),
            desc(desc) {}
        ConfigOption() :
            isBool(false), beenSet(false), value(), defValue(), desc() {}

        /**
         * @brief Re-interpret the value stored in #value as some type using
         * boost::lexical_cast.
         */
        template<typename T>
        T as()
104
        {
Michael Turner's avatar
Michael Turner committed
105
106
107
108
109
110
111
112
113
            try
            {
                return boost::lexical_cast<T>(value);
            }
            catch(const std::exception &e)
            {
                std::cerr << e.what() << std::endl;
                abort();
            }
Michael Turner's avatar
Michael Turner committed
114
        }
Michael Turner's avatar
Michael Turner committed
115
116
117
118
119
120
121

        /**
         * @brief Interpret the value stored in #value as some type using
         * boost::lexical_cast and return true of false depending on cast
         */
        template<typename T>
        bool isType()
122
        {
Michael Turner's avatar
Michael Turner committed
123
124
125
126
127
128
129
130
131
132
133
            bool returnval = true;
            try
            {
                boost::lexical_cast<T>(value);
            }
            catch(const std::exception &e)
            {
                returnval = false;
            }

            return returnval;
Michael Turner's avatar
Michael Turner committed
134
        }
Michael Turner's avatar
Michael Turner committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150


        /// True if the configuration option is a boolean (thus does not
        /// need additional arguments).
        bool   isBool;
        /// True if the configuration option has been set at command
        /// line. If false, the default value will be put into #value.
        bool   beenSet;
        /// The value of the configuration option.
        std::string value;
        /// Default value of the configuration option.
        std::string defValue;
        /// Description of the configuration option.
        std::string desc;
    };

151

Michael Turner's avatar
Michael Turner committed
152
    /**
Michael Turner's avatar
Michael Turner committed
153
154
155
     * Abstract base class for mesh converter modules. Each subclass
     * implements the Process() function, which in some way alters the
     * mesh #m.
Michael Turner's avatar
Michael Turner committed
156
     */
Michael Turner's avatar
Michael Turner committed
157
    class Module
Michael Turner's avatar
Michael Turner committed
158
    {
Michael Turner's avatar
Michael Turner committed
159
    public:
's avatar
committed
160
161
        NEKMESHUTILS_EXPORT Module(MeshSharedPtr p_m) : m_mesh(p_m) {}
        NEKMESHUTILS_EXPORT virtual void Process() = 0;
Michael Turner's avatar
Michael Turner committed
162

's avatar
committed
163
164
165
166
        NEKMESHUTILS_EXPORT void RegisterConfig(std::string key, std::string value);
        NEKMESHUTILS_EXPORT void PrintConfig();
        NEKMESHUTILS_EXPORT void SetDefaults();
        NEKMESHUTILS_EXPORT MeshSharedPtr GetMesh()
167
        {
Michael Turner's avatar
Michael Turner committed
168
            return m_mesh;
Michael Turner's avatar
Michael Turner committed
169
        }
170

Michael Turner's avatar
Michael Turner committed
171
        /// Extract element vertices
's avatar
committed
172
        NEKMESHUTILS_EXPORT virtual void ProcessVertices();
Michael Turner's avatar
Michael Turner committed
173
        /// Extract element edges
's avatar
committed
174
        NEKMESHUTILS_EXPORT virtual void ProcessEdges(bool ReprocessEdges = true);
Michael Turner's avatar
Michael Turner committed
175
        /// Extract element faces
's avatar
committed
176
        NEKMESHUTILS_EXPORT virtual void ProcessFaces(bool ReprocessFaces = true);
Michael Turner's avatar
Michael Turner committed
177
        /// Generate element IDs
's avatar
committed
178
        NEKMESHUTILS_EXPORT virtual void ProcessElements();
Michael Turner's avatar
Michael Turner committed
179
        /// Generate composites
's avatar
committed
180
        NEKMESHUTILS_EXPORT virtual void ProcessComposites();
Michael Turner's avatar
Michael Turner committed
181

's avatar
committed
182
        NEKMESHUTILS_EXPORT virtual void ClearElementLinks();
Michael Turner's avatar
Michael Turner committed
183
184
185
186
187
188
189

    protected:
        /// Mesh object
        MeshSharedPtr m_mesh;
        /// List of configuration values.
        std::map<std::string, ConfigOption> m_config;

's avatar
committed
190
191
192
193
194
        NEKMESHUTILS_EXPORT void ReorderPrisms(PerMap &perFaces);
        NEKMESHUTILS_EXPORT void PrismLines(int                     prism,
                                            PerMap                  &perFaces,
                                            std::set<int>           &prismsDone,
                                            std::vector<ElementSharedPtr> &line);
Michael Turner's avatar
Michael Turner committed
195
196
197
198
199
200
201
202
203
204
205
206
    };

    /**
     * @brief Abstract base class for input modules.
     *
     * Input modules should read the contents of #m_mshFile in the Process()
     * function and populate the members of #m. Typically any given module
     * should populate Mesh::expDim, Mesh::spaceDim, Mesh::node and
     * Mesh::element, then call the protected ProcessX functions to
     * generate edges, faces, etc.
     */
    class InputModule : public Module
Michael Turner's avatar
Michael Turner committed
207
    {
Michael Turner's avatar
Michael Turner committed
208
    public:
's avatar
committed
209
210
        NEKMESHUTILS_EXPORT InputModule(MeshSharedPtr p_m);
        NEKMESHUTILS_EXPORT void OpenStream();
Michael Turner's avatar
Michael Turner committed
211
212
213

    protected:
        /// Print summary of elements.
's avatar
committed
214
        NEKMESHUTILS_EXPORT void PrintSummary();
Michael Turner's avatar
Michael Turner committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        /// Input stream
        io::filtering_istream m_mshFile;
        /// Input stream
        std::ifstream m_mshFileStream;
    };

    /**
     * @brief Abstract base class for processing modules.
     *
     * Processing modules take a populated %Mesh object and process it in
     * some fashion; for example the %ProcessJac module calculates the
     * Jacobian of each element and prints warnings for non-positive
     * elements.
     */
    class ProcessModule : public Module
    {
    public:
's avatar
committed
232
        NEKMESHUTILS_EXPORT ProcessModule(MeshSharedPtr p_m) : Module(p_m) {}
Michael Turner's avatar
Michael Turner committed
233
234
235
236
237
238
239
240
241
242
243
    };

    /**
     * @brief Abstract base class for output modules.
     *
     * Output modules take the mesh #m and write to the file specified by
     * the stream #m_mshFile.
     */
    class OutputModule : public Module
    {
    public:
's avatar
committed
244
245
        NEKMESHUTILS_EXPORT OutputModule(MeshSharedPtr p_m);
        NEKMESHUTILS_EXPORT void OpenStream();
246

247

Michael Turner's avatar
Michael Turner committed
248
249
250
251
252
253
    protected:
        /// Output stream
        io::filtering_ostream m_mshFile;
        /// Input stream
        std::ofstream m_mshFileStream;
    };
254

Michael Turner's avatar
Michael Turner committed
255
    typedef std::pair<ModuleType,std::string> ModuleKey;
's avatar
committed
256
    NEKMESHUTILS_EXPORT std::ostream& operator<<(std::ostream& os, const ModuleKey& rhs);
257

Michael Turner's avatar
Michael Turner committed
258
259
    typedef boost::shared_ptr<Module> ModuleSharedPtr;
    typedef LibUtilities::NekFactory< ModuleKey, Module, MeshSharedPtr > ModuleFactory;
260

's avatar
committed
261
    NEKMESHUTILS_EXPORT ModuleFactory& GetModuleFactory();
Michael Turner's avatar
Michael Turner committed
262
}
263
264
265
}

#endif