InputXml.cpp 7.48 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: InputXml.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: Read xml file and set up expansions
//
////////////////////////////////////////////////////////////////////////////////

36
#include <iomanip>
37 38
#include <iostream>
#include <string>
39 40
using namespace std;

41 42
#include <LibUtilities/BasicUtils/Timer.h>

43
#include "InputXml.h"
44
using namespace Nektar;
45 46 47

namespace Nektar
{
Douglas Serson's avatar
Douglas Serson committed
48
namespace FieldUtils
Chris Cantwell's avatar
Chris Cantwell committed
49 50 51 52
{

ModuleKey InputXml::m_className[5] = {
    GetModuleFactory().RegisterCreatorFunction(
53
        ModuleKey(eInputModule, "xml"), InputXml::create, "Reads Xml file."),
Chris Cantwell's avatar
Chris Cantwell committed
54
    GetModuleFactory().RegisterCreatorFunction(
55
        ModuleKey(eInputModule, "xml.gz"), InputXml::create, "Reads Xml file."),
Chris Cantwell's avatar
Chris Cantwell committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

/**
 * @brief Set up InputXml object.
 *
 */
InputXml::InputXml(FieldSharedPtr f) : InputModule(f)
{
    m_allowedFiles.insert("xml");
    m_allowedFiles.insert("xml.gz");
}

/**
 *
 */
InputXml::~InputXml()
{
}

/**
 *
 */
void InputXml::Process(po::variables_map &vm)
{
80
    LibUtilities::Timer timerpart;
81 82 83 84 85 86 87
    if (m_f->m_verbose)
    {
        if (m_f->m_comm->TreatAsRankZero())
        {
            timerpart.Start();
        }
    }
88

89 90
    // check for multiple calls to inputXml due to split xml
    // files. If so just return
91
    if (m_f->m_graph)
92
    {
93
        return;
94
    }
Chris Cantwell's avatar
Chris Cantwell committed
95

96
    string xml_ending    = "xml";
Chris Cantwell's avatar
Chris Cantwell committed
97
    string xml_gz_ending = "xml.gz";
98

Chris Cantwell's avatar
Chris Cantwell committed
99 100 101 102 103 104
    std::vector<std::string> files;
    // load .xml ending
    for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
    {
        files.push_back(m_f->m_inputfiles[xml_ending][i]);
    }
105

Chris Cantwell's avatar
Chris Cantwell committed
106
    // load any .xml.gz endings
107
    for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
Chris Cantwell's avatar
Chris Cantwell committed
108 109 110
    {
        files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
    }
111

112
    SpatialDomains::DomainRangeShPtr rng = SpatialDomains::NullDomainRangeShPtr;
113

Chris Cantwell's avatar
Chris Cantwell committed
114
    // define range to process output
115
    if (vm.count("range"))
Chris Cantwell's avatar
Chris Cantwell committed
116 117 118
    {
        vector<NekDouble> values;
        ASSERTL0(ParseUtils::GenerateUnOrderedVector(
119
                     vm["range"].as<string>().c_str(), values),
Chris Cantwell's avatar
Chris Cantwell committed
120
                 "Failed to interpret range string");
121

122
        ASSERTL0(values.size() > 1, "Do not have minimum values of xmin,xmax");
Chris Cantwell's avatar
Chris Cantwell committed
123 124
        ASSERTL0(values.size() % 2 == 0,
                 "Do not have an even number of range values");
125

126 127
        int nvalues = values.size() / 2;
        rng = MemoryManager<SpatialDomains::DomainRange>::AllocateSharedPtr();
128

129 130 131
        rng->m_doZrange   = false;
        rng->m_doYrange   = false;
        rng->m_checkShape = false;
132

133
        switch (nvalues)
Chris Cantwell's avatar
Chris Cantwell committed
134
        {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            case 3:
                rng->m_doZrange = true;
                rng->m_zmin     = values[4];
                rng->m_zmax     = values[5];
            case 2:
                rng->m_doYrange = true;
                rng->m_ymin     = values[2];
                rng->m_ymax     = values[3];
            case 1:
                rng->m_doXrange = true;
                rng->m_xmin     = values[0];
                rng->m_xmax     = values[1];
                break;
            default:
                ASSERTL0(false, "too many values specfied in range");
Chris Cantwell's avatar
Chris Cantwell committed
150 151 152 153
        }
    }

    // define range to only take a single shape.
154
    if (vm.count("onlyshape"))
Chris Cantwell's avatar
Chris Cantwell committed
155
    {
156
        if (rng == SpatialDomains::NullDomainRangeShPtr)
Chris Cantwell's avatar
Chris Cantwell committed
157
        {
158 159
            rng =
                MemoryManager<SpatialDomains::DomainRange>::AllocateSharedPtr();
Chris Cantwell's avatar
Chris Cantwell committed
160 161 162 163
            rng->m_doXrange = false;
            rng->m_doYrange = false;
            rng->m_doZrange = false;
        }
164

Chris Cantwell's avatar
Chris Cantwell committed
165
        rng->m_checkShape = true;
166

167
        string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
Chris Cantwell's avatar
Chris Cantwell committed
168
        int i;
169
        for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
Chris Cantwell's avatar
Chris Cantwell committed
170 171 172
        {
            string shapeval = LibUtilities::ShapeTypeMap[i];
            boost::to_upper(shapeval);
173
            if (shapematch.compare(shapeval) == 0)
174
            {
Chris Cantwell's avatar
Chris Cantwell committed
175 176
                rng->m_shapeType = (LibUtilities::ShapeType)i;
                break;
177
            }
Chris Cantwell's avatar
Chris Cantwell committed
178 179 180 181 182
        }
        ASSERTL0(i != LibUtilities::SIZE_ShapeType,
                 "Failed to find shape type in -onlyshape command line "
                 "argument");
    }
183

184 185 186
    // Set up command lines options that will be passed through to SessionReader
    vector<string> cmdArgs;
    cmdArgs.push_back("FieldConvert");
187

188
    if (m_f->m_verbose)
Chris Cantwell's avatar
Chris Cantwell committed
189
    {
190
        cmdArgs.push_back("--verbose");
191 192
    }

193
    if (vm.count("part-only"))
194
    {
195 196 197
        cmdArgs.push_back("--part-only");
        cmdArgs.push_back(
            boost::lexical_cast<string>(vm["part-only"].as<int>()));
198
    }
199

200
    if (vm.count("part-only-overlapping"))
201
    {
202 203 204 205 206
        cmdArgs.push_back("--part-only-overlapping");
        cmdArgs.push_back(
            boost::lexical_cast<string>(vm["part-only-overlapping"].as<int>()));
    }

207
    if (vm.count("npz"))
208 209
    {
        cmdArgs.push_back("--npz");
210
        cmdArgs.push_back(boost::lexical_cast<string>(vm["npz"].as<int>()));
211 212
    }

213 214
    int argc          = cmdArgs.size();
    const char **argv = new const char *[argc];
215 216 217
    for (int i = 0; i < argc; ++i)
    {
        argv[i] = cmdArgs[i].c_str();
218
    }
219

220 221
    m_f->m_session = LibUtilities::SessionReader::CreateInstance(
        argc, (char **)argv, files, m_f->m_comm);
222

223
    // Free up memory.
224
    delete[] argv;
225

226
    if (m_f->m_verbose)
227
    {
228
        if (m_f->m_comm->TreatAsRankZero())
229 230 231
        {
            timerpart.Stop();
            NekDouble cpuTime = timerpart.TimePerTest(1);
232

233 234 235 236 237 238
            stringstream ss;
            ss << cpuTime << "s";
            cout << "\t InputXml session reader CPU Time: " << setw(8) << left
                 << ss.str() << endl;
            timerpart.Start();
        }
Chris Cantwell's avatar
Chris Cantwell committed
239
    }
240

241
    m_f->m_graph = SpatialDomains::MeshGraph::Read(m_f->m_session, rng);
242

243
    if (m_f->m_verbose)
244
    {
245
        if (m_f->m_comm->TreatAsRankZero())
246 247 248
        {
            timerpart.Stop();
            NekDouble cpuTime = timerpart.TimePerTest(1);
249

250 251
            stringstream ss;
            ss << cpuTime << "s";
252 253
            cout << "\t InputXml mesh graph setup  CPU Time: " << setw(8)
                 << left << ss.str() << endl;
254 255 256
            timerpart.Start();
        }
    }
257
}
Chris Cantwell's avatar
Chris Cantwell committed
258
}
259
}