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 n equipspaced points along the collapsed coordinates (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  ("onlyshape", po::value<string>(),
64  "Only use element with defined shape type i.e. -onlyshape "
65  " Tetrahedron")
66  ("procid", po::value<int>(),
67  "Process as single procid of a partition of size nproc "
68  "(-nproc must be specified).")
69  ("modules-opt,p", po::value<string>(),
70  "Print options for a module.")
71  ("module,m", po::value<vector<string> >(),
72  "Specify modules which are to be used.")
73  ("useSessionVariables",
74  "Use variables defined in session for output")
75  ("verbose,v",
76  "Enable verbose mode.");
77 
78  po::options_description hidden("Hidden options");
79  hidden.add_options()
80  ("input-file", po::value<vector<string> >(), "Input filename");
81 
82  po::options_description cmdline_options;
83  cmdline_options.add(hidden).add(desc);
84 
85  po::options_description visible("Allowed options");
86  visible.add(desc);
87 
88  po::positional_options_description p;
89  p.add("input-file", -1);
90 
91  po::variables_map vm;
92 
93  try
94  {
95  po::store(po::command_line_parser(argc, argv).
96  options(cmdline_options).positional(p).run(), vm);
97  po::notify(vm);
98  }
99  catch (const std::exception& e)
100  {
101  cerr << e.what() << endl;
102  cerr << desc;
103  return 1;
104  }
105 
106  // Print available modules.
107  if (vm.count("modules-list"))
108  {
110  return 1;
111  }
112 
113  if (vm.count("modules-opt"))
114  {
115  vector<string> tmp1;
116  boost::split(tmp1, vm["modules-opt"].as<string>(),
117  boost::is_any_of(":"));
118 
119  if (tmp1.size() != 2)
120  {
121  cerr << "ERROR: To specify a module, use one of in, out or proc "
122  << "together with the filename; for example in:vtk." << endl;
123  return 1;
124  }
125 
126  if (tmp1[0] != "in" && tmp1[0] != "out" && tmp1[0] != "proc")
127  {
128  cerr << "ERROR: Invalid module type " << tmp1[0] << endl;
129  return 1;
130  }
131 
132  ModuleType t;
133 
134  if (tmp1[0] == "in")
135  {
136  t = eInputModule;
137  }
138  else if (tmp1[0] == "out")
139  {
140  t = eOutputModule;
141  }
142  else if (tmp1[0] == "proc")
143  {
144  t = eProcessModule;
145  }
146 
147  FieldSharedPtr f = boost::shared_ptr<Field>(new Field());
149  ModuleKey(t, tmp1[1]), f);
150  cerr << "Options for module " << tmp1[1] << ":" << endl;
151  mod->PrintConfig();
152  return 1;
153  }
154 
155  if (vm.count("help") || vm.count("input-file") != 1) {
156  cerr << "Usage: FieldConvert [options] inputfile.ext1 outputfile.ext2"
157  << endl;
158  cout << desc;
159  cout << endl;
160  cout << "Example Usage: \n" << endl;
161  cout << "\t FieldConvert -m vorticity file.xml file.fld file_vort.fld "
162  << endl;
163  cout << "(This will add vorticity to file file.fld and put it in a "
164  "new file file_vort.fld) " << endl;
165  cout << endl;
166  cout << "\t FieldConvert file.xml file_vort.fld file_vort.dat " << endl;
167  cout << "(process file_vort.fld and make a tecplot output "
168  "file_vort.dat) " << endl;
169 
170  return 1;
171  }
172 
173  ASSERTL0(vm.count("input-file"),
174  "Must specify input(s) and/or output file.");
175  vector<string> inout = vm["input-file"].as<vector<string> >();
176 
177 
178  /*
179  * Process list of modules. Each element of the vector of module strings can
180  * be in the following form:
181  *
182  * modname:arg1=a:arg2=b:arg3=c:arg4:arg5=asd
183  *
184  * where the only required argument is 'modname', specifing the name of the
185  * module to load.
186  */
187 
188  FieldSharedPtr f = boost::shared_ptr<Field>(new Field());
189  if (LibUtilities::GetCommFactory().ModuleExists("ParallelMPI"))
190  {
191  if(vm.count("procid"))
192  {
193  int nprocs, rank;
194 
195  ASSERTL0(vm.count("nprocs"),
196  "Must specify --nprocs when using --procid option");
197  nprocs = vm["nprocs"].as<int>();
198  rank = vm["procid"].as<int>();
199 
200  f->m_comm = boost::shared_ptr<FieldConvertComm>(
201  new FieldConvertComm(argc, argv, nprocs,rank));
202  }
203  else
204  {
206  "ParallelMPI", argc, argv);
207  }
208  }
209  else
210  {
212  "Serial", argc, argv);
213 
214  }
215 
216  vector<ModuleSharedPtr> modules;
217  vector<string> modcmds;
218 
219  if (vm.count("verbose"))
220  {
221  f->m_verbose = true;
222  }
223 
224  if (vm.count("module"))
225  {
226  modcmds = vm["module"].as<vector<string> >();
227  }
228 
229  // Add input and output modules to beginning and end of this vector.
230  modcmds.insert(modcmds.begin(), inout.begin(), inout.end()-1);
231  modcmds.push_back(*(inout.end()-1));
232  int nInput = inout.size()-1;
233 
234  InputModuleSharedPtr inputModule;
235 
236  for (int i = 0; i < modcmds.size(); ++i)
237  {
238  // First split each command by the colon separator.
239  vector<string> tmp1;
240  ModuleKey module;
241  int offset = 1;
242 
243  boost::split(tmp1, modcmds[i], boost::is_any_of(":"));
244 
245  if (i < nInput || i == modcmds.size() - 1)
246  {
247  module.first = (i < nInput ? eInputModule : eOutputModule);
248 
249  // If no colon detected, automatically detect mesh type from
250  // file extension. Otherwise override and use tmp1[1] as the
251  // module to load. This also allows us to pass options to
252  // input/output modules. So, for example, to override
253  // filename.xml to be read as vtk, you use:
254  //
255  // filename.xml:vtk:opt1=arg1:opt2=arg2
256  if (tmp1.size() == 1)
257  {
258  int dot = tmp1[0].find_last_of('.') + 1;
259  string ext = tmp1[0].substr(dot, tmp1[0].length() - dot);
260 
261  if(ext == "gz")
262  {
263  string tmp2 = tmp1[0].substr(0,dot-1);
264  dot = tmp2.find_last_of('.') + 1;
265  ext = tmp1[0].substr(dot,tmp1[0].length()-dot);
266  }
267 
268  module.second = ext;
269  tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
270  +tmp1[0]);
271  }
272  else
273  {
274  module.second = tmp1[1];
275  tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
276  +tmp1[0]);
277  offset++;
278  }
279  }
280  else
281  {
282  module.first = eProcessModule;
283  module.second = tmp1[0];
284  }
285 
286  // Create module.
287  ModuleSharedPtr mod;
288  mod = GetModuleFactory().CreateInstance(module, f);
289  modules.push_back(mod);
290 
291  if (i < nInput)
292  {
293  inputModule = boost::dynamic_pointer_cast<InputModule>(mod);
294  inputModule->AddFile(module.second, tmp1[0]);
295  }
296 
297  // Set options for this module.
298  for (int j = offset; j < tmp1.size(); ++j)
299  {
300  vector<string> tmp2;
301  boost::split(tmp2, tmp1[j], boost::is_any_of("="));
302 
303  if (tmp2.size() == 1)
304  {
305  mod->RegisterConfig(tmp2[0], "1");
306  }
307  else if (tmp2.size() == 2)
308  {
309  mod->RegisterConfig(tmp2[0], tmp2[1]);
310  }
311  else
312  {
313  cerr << "ERROR: Invalid module configuration: format is "
314  << "either :arg or :arg=val" << endl;
315  abort();
316  }
317  }
318 
319  // Ensure configuration options have been set.
320  mod->SetDefaults();
321  }
322 
323  // If any output module has to reset points then set intput modules to match
324  bool RequiresEquiSpaced = false;
325  for (int i = 0; i < modules.size(); ++i)
326  {
327  if(modules[i]->GetRequireEquiSpaced())
328  {
329  RequiresEquiSpaced = true;
330  }
331  }
332  if (RequiresEquiSpaced)
333  {
334  for (int i = 0; i < modules.size(); ++i)
335  {
336  modules[i]->SetRequireEquiSpaced(true);
337  }
338  }
339  // Run field process.
340  for (int i = 0; i < modules.size(); ++i)
341  {
342  modules[i]->Process(vm);
343  cout.flush();
344  }
345 
346  return 0;
347 }