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