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
43
44#include "InputXml.h"
45using namespace Nektar;
46
47namespace Nektar::FieldUtils
48{
49
52 ModuleKey(eInputModule, "xml"), InputXml::create, "Reads Xml file."),
54 ModuleKey(eInputModule, "xml.gz"), InputXml::create, "Reads Xml file."),
55};
56
57/**
58 * @brief Set up InputXml object.
59 *
60 */
62{
63 m_allowedFiles.insert("xml");
64 m_allowedFiles.insert("xml.gz");
65 m_config["range"] = ConfigOption(false, "", "range values");
66}
67
68/**
69 *
70 */
72{
73}
74
75/**
76 *
77 */
78void InputXml::v_Process(po::variables_map &vm)
79{
80 string range = m_config["range"].as<string>();
81
82 LibUtilities::Timer timerpart;
83 if (m_f->m_verbose)
84 {
85 if (m_f->m_comm->TreatAsRankZero())
86 {
87 timerpart.Start();
88 }
89 }
90
91 // check for multiple calls to inputXml due to split xml
92 // files. If so just return
93 if (m_f->m_graph)
94 {
95 return;
96 }
97
98 string xml_ending = "xml";
99 string xml_gz_ending = "xml.gz";
100
101 std::vector<std::string> files;
102 // load .xml ending
103 for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
104 {
105 files.push_back(m_f->m_inputfiles[xml_ending][i]);
106 }
107
108 // load any .xml.gz endings
109 for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
110 {
111 files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
112 }
113
115
116 // define range to process output
117 if (vm.count("range"))
118 {
120 "The command line option \"range\" (FieldConvert -r xmin,xmax,"
121 "ymin,ymax) is deprecated. Please use the InputXml "
122 "option instead, i.e. \n \t"
123 "FieldConvert myfile.xml:xml:range=\"xmin,xmax,ymin,ymax\" "
124 "out.vtu");
125 }
126
127 if (vm.count("range") || range.size())
128 {
129 vector<NekDouble> values;
130
131 if (range.size())
132 {
134 "Failed to interpret range string");
135 }
136 else
137 {
138 ASSERTL0(
139 ParseUtils::GenerateVector(vm["range"].as<string>(), values),
140 "Failed to interpret range string");
141 }
142
143 ASSERTL0(values.size() > 1, "Do not have minimum values of xmin,xmax");
144 ASSERTL0(values.size() % 2 == 0,
145 "Do not have an even number of range values");
146
147 int nvalues = values.size() / 2;
149
150 rng->m_doZrange = false;
151 rng->m_doYrange = false;
152 rng->m_checkShape = false;
153
154 switch (nvalues)
155 {
156 case 3:
157 rng->m_doZrange = true;
158 rng->m_zmin = values[4];
159 rng->m_zmax = values[5];
160 /* Falls through. */
161 case 2:
162 rng->m_doYrange = true;
163 rng->m_ymin = values[2];
164 rng->m_ymax = values[3];
165 /* Falls through. */
166 case 1:
167 rng->m_doXrange = true;
168 rng->m_xmin = values[0];
169 rng->m_xmax = values[1];
170 break;
171 default:
173 "too many values specfied in range");
174 }
175 }
176
177 // define range to only take a single shape.
178 if (vm.count("onlyshape"))
179 {
181 {
183 rng->m_doXrange = false;
184 rng->m_doYrange = false;
185 rng->m_doZrange = false;
186 }
187
188 rng->m_checkShape = true;
189
190 string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
191 int i;
192 for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
193 {
194 string shapeval = LibUtilities::ShapeTypeMap[i];
195 boost::to_upper(shapeval);
196 if (shapematch.compare(shapeval) == 0)
197 {
198 rng->m_shapeType = (LibUtilities::ShapeType)i;
199 break;
200 }
201 }
203 "Failed to find shape type in -onlyshape command line "
204 "argument");
205 }
206
207 // Set up command lines options that will be passed through to SessionReader
208 vector<string> cmdArgs;
209 cmdArgs.push_back("FieldConvert");
210
211 if (m_f->m_verbose)
212 {
213 cmdArgs.push_back("--verbose");
214 }
215
216 if (vm.count("part-only"))
217 {
218 cmdArgs.push_back("--part-only");
219 cmdArgs.push_back(
220 boost::lexical_cast<string>(vm["part-only"].as<int>()));
221 }
222
223 if (vm.count("part-only-overlapping"))
224 {
225 cmdArgs.push_back("--part-only-overlapping");
226 cmdArgs.push_back(
227 boost::lexical_cast<string>(vm["part-only-overlapping"].as<int>()));
228 }
229
230 if (vm.count("npz"))
231 {
232 cmdArgs.push_back("--npz");
233 cmdArgs.push_back(boost::lexical_cast<string>(vm["npz"].as<int>()));
234 }
235
236 // Parallel-in-time
237 if (vm.count("npt"))
238 {
239 cmdArgs.push_back("--npt");
240 cmdArgs.push_back(boost::lexical_cast<string>(vm["npt"].as<int>()));
241 }
242
243 int argc = cmdArgs.size();
244 const char **argv = new const char *[argc];
245 for (int i = 0; i < argc; ++i)
246 {
247 argv[i] = cmdArgs[i].c_str();
248 }
249
251 argc, (char **)argv, files, m_f->m_comm);
252
253 if (vm.count("nparts"))
254 {
255 // make sure have pre-partitioned mesh for nparts option
256 ASSERTL0(boost::icontains(files[0], "_xml"),
257 "Expect the mesh to have been pre-partitioned when "
258 " using the \"--nparts\" option. Please use \"--part-only\" "
259 "option to prepartition xml file.");
260 }
261
262 // Free up memory.
263 delete[] argv;
264
265 if (m_f->m_verbose)
266 {
267 if (m_f->m_comm->TreatAsRankZero())
268 {
269 timerpart.Stop();
270 NekDouble cpuTime = timerpart.TimePerTest(1);
271
272 stringstream ss;
273 ss << cpuTime << "s";
274 cout << "\t InputXml session reader CPU Time: " << setw(8) << left
275 << ss.str() << endl;
276 timerpart.Start();
277 }
278 }
279
280 m_f->m_graph = SpatialDomains::MeshGraphIO::Read(m_f->m_session, rng);
281
282 if (m_f->m_verbose)
283 {
284 if (m_f->m_comm->TreatAsRankZero())
285 {
286 timerpart.Stop();
287 NekDouble cpuTime = timerpart.TimePerTest(1);
288
289 stringstream ss;
290 ss << cpuTime << "s";
291 cout << "\t InputXml mesh graph setup CPU Time: " << setw(8)
292 << left << ss.str() << endl;
293 timerpart.Start();
294 }
295 }
296}
297} // 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:78
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:61
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:274
FieldSharedPtr m_f
Field object.
Definition: Module.h:239
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:272
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
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: MeshGraphIO.cpp:53
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:1026
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
STL namespace.
Represents a command-line configuration option.
Definition: Module.h:129