Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FieldConvert.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: FieldConvert.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 // License for the specific language governing rights and laimitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Field conversion utility.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <string>
37 #include <boost/algorithm/string.hpp>
38 #include <boost/program_options.hpp>
39 
40 #include "Module.h"
41 
42 using namespace std;
43 using namespace Nektar::Utilities;
44 
45 int main(int argc, char* argv[])
46 {
47  po::options_description desc("Available options");
48  desc.add_options()
49  ("help,h",
50  "Produce this help message.")
51  ("modules-list,l",
52  "Print the list of available modules.")
53  ("output-points,n", po::value<int>(),
54  "Output at p equipspaced points (for .dat, .vtk).")
55  ("error,e",
56  "Write error of fields for regression checking")
57  ("range,r", po::value<string>(),
58  "Define output range i.e. (-r xmin,xmax,ymin,ymax,zmin,zmax) "
59  "in which any vertex is contained.")
60  ("nprocs", po::value<int>(),
61  "Used to define nprocs if running serial problem to mimic "
62  "parallel run.")
63  ("procid", po::value<int>(),
64  "Process as single procid of a partition of size nproc "
65  "(-nproc must be specified).")
66  ("modules-opt,p", po::value<string>(),
67  "Print options for a module.")
68  ("module,m", po::value<vector<string> >(),
69  "Specify modules which are to be used.")
70  ("useSessionVariables",
71  "Use variables defined in session for output")
72  ("verbose,v",
73  "Enable verbose mode.");
74 
75  po::options_description hidden("Hidden options");
76  hidden.add_options()
77  ("input-file", po::value<vector<string> >(), "Input filename");
78 
79  po::options_description cmdline_options;
80  cmdline_options.add(hidden).add(desc);
81 
82  po::options_description visible("Allowed options");
83  visible.add(desc);
84 
85  po::positional_options_description p;
86  p.add("input-file", -1);
87 
88  po::variables_map vm;
89 
90  try
91  {
92  po::store(po::command_line_parser(argc, argv).
93  options(cmdline_options).positional(p).run(), vm);
94  po::notify(vm);
95  }
96  catch (const std::exception& e)
97  {
98  cerr << e.what() << endl;
99  cerr << desc;
100  return 1;
101  }
102 
103  // Print available modules.
104  if (vm.count("modules-list"))
105  {
107  return 1;
108  }
109 
110  if (vm.count("modules-opt"))
111  {
112  vector<string> tmp1;
113  boost::split(tmp1, vm["modules-opt"].as<string>(),
114  boost::is_any_of(":"));
115 
116  if (tmp1.size() != 2)
117  {
118  cerr << "ERROR: To specify a module, use one of in, out or proc "
119  << "together with the filename; for example in:vtk." << endl;
120  return 1;
121  }
122 
123  if (tmp1[0] != "in" && tmp1[0] != "out" && tmp1[0] != "proc")
124  {
125  cerr << "ERROR: Invalid module type " << tmp1[0] << endl;
126  return 1;
127  }
128 
129  ModuleType t;
130 
131  if (tmp1[0] == "in")
132  {
133  t = eInputModule;
134  }
135  else if (tmp1[0] == "out")
136  {
137  t = eOutputModule;
138  }
139  else if (tmp1[0] == "proc")
140  {
141  t = eProcessModule;
142  }
143 
144  FieldSharedPtr f = boost::shared_ptr<Field>(new Field());
146  ModuleKey(t, tmp1[1]), f);
147  cerr << "Options for module " << tmp1[1] << ":" << endl;
148  mod->PrintConfig();
149  return 1;
150  }
151 
152  if (vm.count("help") || vm.count("input-file") != 1) {
153  cerr << "Usage: FieldConvert [options] inputfile.ext1 outputfile.ext2"
154  << endl;
155  cout << desc;
156  cout << endl;
157  cout << "Example Usage: \n" << endl;
158  cout << "\t FieldConvert -m vorticity file.xml file.fld file_vort.fld "
159  << endl;
160  cout << "(This will add vorticity to file file.fld and put it in a "
161  "new file file_vort.fld) " << endl;
162  cout << endl;
163  cout << "\t FieldConvert file.xml file_vort.fld file_vort.dat " << endl;
164  cout << "(process file_vort.fld and make a tecplot output "
165  "file_vort.dat) " << endl;
166 
167  return 1;
168  }
169 
170  ASSERTL0(vm.count("input-file"),
171  "Must specify input(s) and/or output file.");
172  vector<string> inout = vm["input-file"].as<vector<string> >();
173 
174 
175  /*
176  * Process list of modules. Each element of the vector of module strings can
177  * be in the following form:
178  *
179  * modname:arg1=a:arg2=b:arg3=c:arg4:arg5=asd
180  *
181  * where the only required argument is 'modname', specifing the name of the
182  * module to load.
183  */
184 
185  FieldSharedPtr f = boost::shared_ptr<Field>(new Field());
186  if (LibUtilities::GetCommFactory().ModuleExists("ParallelMPI"))
187  {
188  if(vm.count("procid"))
189  {
190  int nprocs, rank;
191 
192  ASSERTL0(vm.count("nprocs"),
193  "Must specify --nprocs when using --procid option");
194  nprocs = vm["nprocs"].as<int>();
195  rank = vm["procid"].as<int>();
196 
197  f->m_comm = boost::shared_ptr<FieldConvertComm>(
198  new FieldConvertComm(argc, argv, nprocs,rank));
199  }
200  else
201  {
203  "ParallelMPI", argc, argv);
204  }
205  }
206  else
207  {
209  "Serial", argc, argv);
210 
211  }
212 
213  vector<ModuleSharedPtr> modules;
214  vector<string> modcmds;
215 
216  if (vm.count("verbose"))
217  {
218  f->m_verbose = true;
219  }
220 
221  if (vm.count("module"))
222  {
223  modcmds = vm["module"].as<vector<string> >();
224  }
225 
226  // Add input and output modules to beginning and end of this vector.
227  modcmds.insert(modcmds.begin(), inout.begin(), inout.end()-1);
228  modcmds.push_back(*(inout.end()-1));
229  int nInput = inout.size()-1;
230 
231  InputModuleSharedPtr inputModule;
232 
233  for (int i = 0; i < modcmds.size(); ++i)
234  {
235  // First split each command by the colon separator.
236  vector<string> tmp1;
237  ModuleKey module;
238  int offset = 1;
239 
240  boost::split(tmp1, modcmds[i], boost::is_any_of(":"));
241 
242  if (i < nInput || i == modcmds.size() - 1)
243  {
244  module.first = (i < nInput ? eInputModule : eOutputModule);
245 
246  // If no colon detected, automatically detect mesh type from
247  // file extension. Otherwise override and use tmp1[1] as the
248  // module to load. This also allows us to pass options to
249  // input/output modules. So, for example, to override
250  // filename.xml to be read as vtk, you use:
251  //
252  // filename.xml:vtk:opt1=arg1:opt2=arg2
253  if (tmp1.size() == 1)
254  {
255  int dot = tmp1[0].find_last_of('.') + 1;
256  string ext = tmp1[0].substr(dot, tmp1[0].length() - dot);
257 
258  if(ext == "gz")
259  {
260  string tmp2 = tmp1[0].substr(0,dot-1);
261  dot = tmp2.find_last_of('.') + 1;
262  ext = tmp1[0].substr(dot,tmp1[0].length()-dot);
263  }
264 
265  module.second = ext;
266  tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
267  +tmp1[0]);
268  }
269  else
270  {
271  module.second = tmp1[1];
272  tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
273  +tmp1[0]);
274  offset++;
275  }
276  }
277  else
278  {
279  module.first = eProcessModule;
280  module.second = tmp1[0];
281  }
282 
283  // Create module.
284  ModuleSharedPtr mod;
285  mod = GetModuleFactory().CreateInstance(module, f);
286  modules.push_back(mod);
287 
288  if (i < nInput)
289  {
290  inputModule = boost::dynamic_pointer_cast<InputModule>(mod);
291  inputModule->AddFile(module.second, tmp1[0]);
292  }
293 
294  // Set options for this module.
295  for (int j = offset; j < tmp1.size(); ++j)
296  {
297  vector<string> tmp2;
298  boost::split(tmp2, tmp1[j], boost::is_any_of("="));
299 
300  if (tmp2.size() == 1)
301  {
302  mod->RegisterConfig(tmp2[0], "1");
303  }
304  else if (tmp2.size() == 2)
305  {
306  mod->RegisterConfig(tmp2[0], tmp2[1]);
307  }
308  else
309  {
310  cerr << "ERROR: Invalid module configuration: format is "
311  << "either :arg or :arg=val" << endl;
312  abort();
313  }
314  }
315 
316  // Ensure configuration options have been set.
317  mod->SetDefaults();
318  }
319 
320  // If any output module has to reset points then set intput modules to match
321  bool RequiresEquiSpaced = false;
322  for (int i = 0; i < modules.size(); ++i)
323  {
324  if(modules[i]->GetRequireEquiSpaced())
325  {
326  RequiresEquiSpaced = true;
327  }
328  }
329  if (RequiresEquiSpaced)
330  {
331  for (int i = 0; i < modules.size(); ++i)
332  {
333  modules[i]->SetRequireEquiSpaced(true);
334  }
335  }
336  // Run field process.
337  for (int i = 0; i < modules.size(); ++i)
338  {
339  modules[i]->Process(vm);
340  cout.flush();
341  }
342 
343  return 0;
344 }