Nektar++
Loading...
Searching...
No Matches
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 m_config["comprange"] = ConfigOption(false, "", "composite range values");
67}
68
69/**
70 *
71 */
75
76/**
77 *
78 */
79void InputXml::v_Process(po::variables_map &vm)
80{
81 string range = m_config["range"].as<string>();
82 string comprange = m_config["comprange"].as<string>();
83
84 LibUtilities::Timer timerpart;
85 if (m_f->m_verbose)
86 {
87 if (m_f->m_comm->TreatAsRankZero())
88 {
89 timerpart.Start();
90 }
91 }
92
93 // check for multiple calls to inputXml due to split xml
94 // files. If so just return
95 if (m_f->m_graph)
96 {
97 return;
98 }
99
100 string xml_ending = "xml";
101 string xml_gz_ending = "xml.gz";
102
103 std::vector<std::string> files;
104 // load .xml ending
105 for (int i = 0; i < m_f->m_inputfiles[xml_ending].size(); ++i)
106 {
107 files.push_back(m_f->m_inputfiles[xml_ending][i]);
108 }
109
110 // load any .xml.gz endings
111 for (int j = 0; j < m_f->m_inputfiles[xml_gz_ending].size(); ++j)
112 {
113 files.push_back(m_f->m_inputfiles[xml_gz_ending][j]);
114 }
115
117
118 // define range to process output
119 if (vm.count("range"))
120 {
122 "The command line option \"range\" (FieldConvert -r xmin,xmax,"
123 "ymin,ymax) is deprecated. Please use the InputXml "
124 "option instead, i.e. \n \t"
125 "FieldConvert myfile.xml:xml:range=\"xmin,xmax,ymin,ymax\" "
126 "out.vtu");
127 }
128
129 if (vm.count("range") || range.size())
130 {
131 vector<NekDouble> values;
132
133 if (range.size())
134 {
136 "Failed to interpret range string");
137 }
138 else
139 {
140 ASSERTL0(
141 ParseUtils::GenerateVector(vm["range"].as<string>(), values),
142 "Failed to interpret range string");
143 }
144
145 if (values.size())
146 {
147 ASSERTL0(values.size() > 1,
148 "Do not have minimum values of xmin,xmax");
149 ASSERTL0(values.size() % 2 == 0,
150 "Do not have an even number of range values");
151
152 int nvalues = values.size() / 2;
154
155 rng->m_doZrange = false;
156 rng->m_doYrange = false;
157 rng->m_checkShape = false;
158
159 switch (nvalues)
160 {
161 case 3:
162 rng->m_doZrange = true;
163 rng->m_zmin = values[4];
164 rng->m_zmax = values[5];
165 /* Falls through. */
166 case 2:
167 rng->m_doYrange = true;
168 rng->m_ymin = values[2];
169 rng->m_ymax = values[3];
170 /* Falls through. */
171 case 1:
172 rng->m_doXrange = true;
173 rng->m_xmin = values[0];
174 rng->m_xmax = values[1];
175 break;
176 default:
178 "too many values specfied in range");
179 }
180 }
181 }
182
183 if (comprange.size())
184 {
185 if ((m_f->m_comm->GetSize() > 1) && (m_f->m_comm->GetRank() == 0))
186 {
188 "Using the comprange option in parallel may cause "
189 "problems with partitioning (particuarly with Scotch) "
190 "since the elements are not contiguous");
191 }
192
193 vector<unsigned> compvalues;
194
195 ASSERTL0(ParseUtils::GenerateVector(comprange, compvalues),
196 "Failed to interpret comprange string");
197
198 if (compvalues.size())
199 {
201 {
202 rng = MemoryManager<
203 LibUtilities::DomainRange>::AllocateSharedPtr();
204 }
205 // initialise with 3D and check when graph setup
206 rng->m_compElmts = 3;
207
208 for (auto it : compvalues)
209 {
210 rng->m_comps.insert(it);
211 }
212 }
213 }
214
215 // define range to only take a single shape.
216 if (vm.count("onlyshape"))
217 {
219 {
221 rng->m_doXrange = false;
222 rng->m_doYrange = false;
223 rng->m_doZrange = false;
224 }
225
226 rng->m_checkShape = true;
227
228 string shapematch = boost::to_upper_copy(vm["onlyshape"].as<string>());
229 int i;
230 for (i = 0; i < LibUtilities::SIZE_ShapeType; ++i)
231 {
232 string shapeval = LibUtilities::ShapeTypeMap[i];
233 boost::to_upper(shapeval);
234 if (shapematch.compare(shapeval) == 0)
235 {
236 rng->m_shapeType = (LibUtilities::ShapeType)i;
237 break;
238 }
239 }
241 "Failed to find shape type in -onlyshape command line "
242 "argument");
243 }
244
245 // Set up command lines options that will be passed through to SessionReader
246 vector<string> cmdArgs;
247 cmdArgs.push_back("FieldConvert");
248
249 if (m_f->m_verbose)
250 {
251 cmdArgs.push_back("--verbose");
252 }
253
254 if (vm.count("part-only"))
255 {
256 cmdArgs.push_back("--part-only");
257 cmdArgs.push_back(std::to_string(vm["part-only"].as<int>()));
258 }
259
260 if (vm.count("part-only-overlapping"))
261 {
262 cmdArgs.push_back("--part-only-overlapping");
263 cmdArgs.push_back(
264 std::to_string(vm["part-only-overlapping"].as<int>()));
265 }
266
267 if (vm.count("npz"))
268 {
269 cmdArgs.push_back("--npz");
270 cmdArgs.push_back(std::to_string(vm["npz"].as<int>()));
271 }
272
273 // Parallel-in-time
274 if (vm.count("npt"))
275 {
276 cmdArgs.push_back("--npt");
277 cmdArgs.push_back(std::to_string(vm["npt"].as<int>()));
278 }
279
280 int argc = cmdArgs.size();
281 const char **argv = new const char *[argc];
282 for (int i = 0; i < argc; ++i)
283 {
284 argv[i] = cmdArgs[i].c_str();
285 }
286
288 argc, (char **)argv, files, m_f->m_comm);
289
290 if (vm.count("nparts"))
291 {
292 // make sure have pre-partitioned mesh for nparts option
293 ASSERTL0(boost::icontains(files[0], "_xml"),
294 "Expect the mesh to have been pre-partitioned when "
295 " using the \"--nparts\" option. Please use \"--part-only\" "
296 "option to prepartition xml file.");
297 }
298
299 // Free up memory.
300 delete[] argv;
301
302 if (m_f->m_verbose)
303 {
304 if (m_f->m_comm->TreatAsRankZero())
305 {
306 timerpart.Stop();
307 NekDouble cpuTime = timerpart.TimePerTest(1);
308
309 stringstream ss;
310 ss << cpuTime << "s";
311 cout << "\t InputXml session reader CPU Time: " << setw(8) << left
312 << ss.str() << endl;
313 timerpart.Start();
314 }
315 }
316
317 m_f->m_graph = SpatialDomains::MeshGraphIO::Read(m_f->m_session, rng);
318
319 if (m_f->m_verbose)
320 {
321 if (m_f->m_comm->TreatAsRankZero())
322 {
323 timerpart.Stop();
324 NekDouble cpuTime = timerpart.TimePerTest(1);
325
326 stringstream ss;
327 ss << cpuTime << "s";
328 cout << "\t InputXml mesh graph setup CPU Time: " << setw(8)
329 << left << ss.str() << endl;
330 timerpart.Start();
331 }
332 }
333}
334} // namespace Nektar::FieldUtils
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Abstract base class for input modules.
Definition Module.h:287
void v_Process(po::variables_map &vm) override
Definition InputXml.cpp:79
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.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
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.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
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:69
static DomainRangeShPtr NullDomainRangeShPtr
Definition DomainRange.h:70
STL namespace.
Represents a command-line configuration option.
Definition Module.h:129