Nektar++
InputXml.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: InputXml.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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Read xml file and set up expansions
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <iomanip>
36#include <iostream>
37#include <string>
38using namespace std;
39
42
43#include "InputXml.h"
44using namespace Nektar;
45
46namespace Nektar::FieldUtils
47{
48
51 ModuleKey(eInputModule, "xml"), InputXml::create, "Reads Xml file."),
53 ModuleKey(eInputModule, "xml.gz"), InputXml::create, "Reads Xml file."),
54};
55
56/**
57 * @brief Set up InputXml object.
58 *
59 */
61{
62 m_allowedFiles.insert("xml");
63 m_allowedFiles.insert("xml.gz");
64}
65
66/**
67 *
68 */
70{
71}
72
73/**
74 *
75 */
76void InputXml::v_Process(po::variables_map &vm)
77{
78 LibUtilities::Timer timerpart;
79 if (m_f->m_verbose)
80 {
81 if (m_f->m_comm->TreatAsRankZero())
82 {
83 timerpart.Start();
84 }
85 }
86
87 // check for multiple calls to inputXml due to split xml
88 // files. If so just return
89 if (m_f->m_graph)
90 {
91 return;
92 }
93
94 string xml_ending = "xml";
95 string xml_gz_ending = "xml.gz";
96
97 std::vector<std::string> files;
98 // load .xml ending
99 for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
100 {
101 files.push_back(m_f->m_inputfiles[xml_ending][i]);
102 }
103
104 // load any .xml.gz endings
105 for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
106 {
107 files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
108 }
109
111
112 // define range to process output
113 if (vm.count("range"))
114 {
115 vector<NekDouble> values;
116 ASSERTL0(ParseUtils::GenerateVector(vm["range"].as<string>(), values),
117 "Failed to interpret range string");
118
119 ASSERTL0(values.size() > 1, "Do not have minimum values of xmin,xmax");
120 ASSERTL0(values.size() % 2 == 0,
121 "Do not have an even number of range values");
122
123 int nvalues = values.size() / 2;
125
126 rng->m_doZrange = false;
127 rng->m_doYrange = false;
128 rng->m_checkShape = false;
129
130 switch (nvalues)
131 {
132 case 3:
133 rng->m_doZrange = true;
134 rng->m_zmin = values[4];
135 rng->m_zmax = values[5];
136 /* Falls through. */
137 case 2:
138 rng->m_doYrange = true;
139 rng->m_ymin = values[2];
140 rng->m_ymax = values[3];
141 /* Falls through. */
142 case 1:
143 rng->m_doXrange = true;
144 rng->m_xmin = values[0];
145 rng->m_xmax = values[1];
146 break;
147 default:
149 "too many values specfied in range");
150 }
151 }
152
153 // define range to only take a single shape.
154 if (vm.count("onlyshape"))
155 {
157 {
159 rng->m_doXrange = false;
160 rng->m_doYrange = false;
161 rng->m_doZrange = false;
162 }
163
164 rng->m_checkShape = true;
165
166 string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
167 int i;
168 for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
169 {
170 string shapeval = LibUtilities::ShapeTypeMap[i];
171 boost::to_upper(shapeval);
172 if (shapematch.compare(shapeval) == 0)
173 {
174 rng->m_shapeType = (LibUtilities::ShapeType)i;
175 break;
176 }
177 }
179 "Failed to find shape type in -onlyshape command line "
180 "argument");
181 }
182
183 // Set up command lines options that will be passed through to SessionReader
184 vector<string> cmdArgs;
185 cmdArgs.push_back("FieldConvert");
186
187 if (m_f->m_verbose)
188 {
189 cmdArgs.push_back("--verbose");
190 }
191
192 if (vm.count("part-only"))
193 {
194 cmdArgs.push_back("--part-only");
195 cmdArgs.push_back(
196 boost::lexical_cast<string>(vm["part-only"].as<int>()));
197 }
198
199 if (vm.count("part-only-overlapping"))
200 {
201 cmdArgs.push_back("--part-only-overlapping");
202 cmdArgs.push_back(
203 boost::lexical_cast<string>(vm["part-only-overlapping"].as<int>()));
204 }
205
206 if (vm.count("npz"))
207 {
208 cmdArgs.push_back("--npz");
209 cmdArgs.push_back(boost::lexical_cast<string>(vm["npz"].as<int>()));
210 }
211
212 // Parallel-in-time
213 if (vm.count("npt"))
214 {
215 cmdArgs.push_back("--npt");
216 cmdArgs.push_back(boost::lexical_cast<string>(vm["npt"].as<int>()));
217 }
218
219 int argc = cmdArgs.size();
220 const char **argv = new const char *[argc];
221 for (int i = 0; i < argc; ++i)
222 {
223 argv[i] = cmdArgs[i].c_str();
224 }
225
227 argc, (char **)argv, files, m_f->m_comm);
228
229 if (vm.count("nparts"))
230 {
231 // make sure have pre-partitioned mesh for nparts option
232 ASSERTL0(boost::icontains(files[0], "_xml"),
233 "Expect the mesh to have been pre-partitioned when "
234 " using the \"--nparts\" option. Please use \"--part-only\" "
235 "option to prepartition xml file.");
236 }
237
238 // Free up memory.
239 delete[] argv;
240
241 if (m_f->m_verbose)
242 {
243 if (m_f->m_comm->TreatAsRankZero())
244 {
245 timerpart.Stop();
246 NekDouble cpuTime = timerpart.TimePerTest(1);
247
248 stringstream ss;
249 ss << cpuTime << "s";
250 cout << "\t InputXml session reader CPU Time: " << setw(8) << left
251 << ss.str() << endl;
252 timerpart.Start();
253 }
254 }
255
256 m_f->m_graph = SpatialDomains::MeshGraph::Read(m_f->m_session, rng);
257
258 if (m_f->m_verbose)
259 {
260 if (m_f->m_comm->TreatAsRankZero())
261 {
262 timerpart.Stop();
263 NekDouble cpuTime = timerpart.TimePerTest(1);
264
265 stringstream ss;
266 ss << cpuTime << "s";
267 cout << "\t InputXml mesh graph setup CPU Time: " << setw(8)
268 << left << ss.str() << endl;
269 timerpart.Start();
270 }
271 }
272}
273} // namespace Nektar::FieldUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
Abstract base class for input modules.
Definition: Module.h:287
void v_Process(po::variables_map &vm) override
Definition: InputXml.cpp:76
static ModuleKey m_className[]
ModuleKey for class.
Definition: InputXml.h:57
static ModuleSharedPtr create(FieldSharedPtr f)
Creates an instance of this class.
Definition: InputXml.h:52
InputXml(FieldSharedPtr f)
Set up InputXml object.
Definition: InputXml.cpp:60
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:274
FieldSharedPtr m_f
Field object.
Definition: Module.h:239
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
NekDouble TimePerTest(unsigned int n)
Returns amount of seconds per iteration in a test with n iterations.
Definition: Timer.cpp:65
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition: MeshGraph.cpp:115
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:990
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
const char *const ShapeTypeMap[SIZE_ShapeType]
Definition: ShapeType.hpp:75
std::shared_ptr< DomainRange > DomainRangeShPtr
Definition: DomainRange.h:64
static DomainRangeShPtr NullDomainRangeShPtr
Definition: DomainRange.h:65
double NekDouble