Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MeshConvert.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: MeshConvert.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 limitations 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: Mesh 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 namespace po = boost::program_options;
46 
47 int main(int argc, char* argv[])
48 {
49  po::options_description desc("Available options");
50  desc.add_options()
51  ("help,h", "Produce this help message.")
52  ("modules-list,l", "Print the list of available modules.")
53  ("modules-opt,p", po::value<string>(),
54  "Print options for a module.")
55  ("module,m", po::value<vector<string> >(),
56  "Specify modules which are to be used.")
57  ("verbose,v", "Enable verbose mode.");
58 
59  po::options_description hidden("Hidden options");
60  hidden.add_options()
61  ("input-file", po::value<vector<string> >(), "Input filename");
62 
63  po::options_description cmdline_options;
64  cmdline_options.add(hidden).add(desc);
65 
66  po::options_description visible("Allowed options");
67  visible.add(desc);
68 
69  po::positional_options_description p;
70  p.add("input-file", -1);
71 
72  po::variables_map vm;
73 
74  try
75  {
76  po::store(po::command_line_parser(argc, argv).
77  options(cmdline_options).positional(p).run(), vm);
78  po::notify(vm);
79  }
80  catch (const std::exception& e)
81  {
82  cerr << e.what() << endl;
83  cerr << desc;
84  return 1;
85  }
86 
87  // Print available modules.
88  if (vm.count("modules-list"))
89  {
91  return 1;
92  }
93 
94  if (vm.count("modules-opt"))
95  {
96  vector<string> tmp1;
97  boost::split(tmp1, vm["modules-opt"].as<string>(), boost::is_any_of(":"));
98 
99  if (tmp1.size() != 2)
100  {
101  cerr << "ERROR: To specify a module, use one of in, out or proc "
102  << "together with the filename; for example in:vtk." << endl;
103  return 1;
104  }
105 
106  if (tmp1[0] != "in" && tmp1[0] != "out" && tmp1[0] != "proc")
107  {
108  cerr << "ERROR: Invalid module type " << tmp1[0] << endl;
109  return 1;
110  }
111 
112  ModuleType t;
113 
114  if (tmp1[0] == "in")
115  {
116  t = eInputModule;
117  }
118  else if (tmp1[0] == "out")
119  {
120  t = eOutputModule;
121  }
122  else if (tmp1[0] == "proc")
123  {
124  t = eProcessModule;
125  }
126 
127  MeshSharedPtr m = boost::shared_ptr<Mesh>(new Mesh());
129  ModuleKey(t, tmp1[1]), m);
130  cerr << "Options for module " << tmp1[1] << ":" << endl;
131  mod->PrintConfig();
132  return 1;
133  }
134 
135  if (vm.count("help") || vm.count("input-file") != 1) {
136  cerr << "Usage: MeshConvert [options] inputfile.ext1 outputfile.ext2"
137  << endl;
138  cout << desc;
139  return 1;
140  }
141 
142  vector<string> inout = vm["input-file"].as<vector<string> >();
143 
144  if (inout.size() < 2)
145  {
146  cerr << "ERROR: You must specify an input and output file." << endl;
147  return 1;
148  }
149 
150  /*
151  * Process list of modules. Each element of the vector of module strings can
152  * be in the following form:
153  *
154  * modname:arg1=a:arg2=b:arg3=c:arg4:arg5=asd
155  *
156  * where the only required argument is 'modname', specifing the name of the
157  * module to load.
158  */
159 
160  MeshSharedPtr mesh = boost::shared_ptr<Mesh>(new Mesh());
161  vector<ModuleSharedPtr> modules;
162  vector<string> modcmds;
163 
164  if (vm.count("verbose"))
165  {
166  mesh->m_verbose = true;
167  }
168 
169  if (vm.count("module"))
170  {
171  modcmds = vm["module"].as<vector<string> >();
172  }
173 
174  // Add input and output modules to beginning and end of this vector.
175  modcmds.insert (modcmds.begin(), inout[0]);
176  modcmds.push_back(inout[1]);
177 
178  for (int i = 0; i < modcmds.size(); ++i)
179  {
180  // First split each command by the colon separator.
181  vector<string> tmp1;
182  ModuleKey module;
183  int offset = 1;
184 
185  boost::split(tmp1, modcmds[i], boost::is_any_of(":"));
186 
187  if (i == 0 || i == modcmds.size() - 1)
188  {
189  module.first = (i == 0 ? eInputModule : eOutputModule);
190 
191  // If no colon detected, automatically detect mesh type from
192  // file extension. Otherwise override and use tmp1[1] as the
193  // module to load. This also allows us to pass options to
194  // input/output modules. So, for example, to override
195  // filename.xml to be read as vtk, you use:
196  //
197  // filename.xml:vtk:opt1=arg1:opt2=arg2
198  if (tmp1.size() == 1)
199  {
200  int dot = tmp1[0].find_last_of('.') + 1;
201  string ext = tmp1[0].substr(dot, tmp1[0].length() - dot);
202  module.second = ext;
203  tmp1.push_back(string(i == 0 ? "infile=" : "outfile=")+tmp1[0]);
204  }
205  else
206  {
207  module.second = tmp1[1];
208  tmp1.push_back(string(i == 0 ? "infile=" : "outfile=")+tmp1[0]);
209  offset++;
210  }
211  }
212  else
213  {
214  module.first = eProcessModule;
215  module.second = tmp1[0];
216  }
217 
218  // Create module.
219  ModuleSharedPtr mod = GetModuleFactory().CreateInstance(module,mesh);
220  modules.push_back(mod);
221 
222  // Set options for this module.
223  for (int j = offset; j < tmp1.size(); ++j)
224  {
225  vector<string> tmp2;
226  boost::split(tmp2, tmp1[j], boost::is_any_of("="));
227 
228  if (tmp2.size() == 1)
229  {
230  mod->RegisterConfig(tmp2[0], "1");
231  }
232  else if (tmp2.size() == 2)
233  {
234  mod->RegisterConfig(tmp2[0], tmp2[1]);
235  }
236  else
237  {
238  cerr << "ERROR: Invalid module configuration: format is "
239  << "either :arg or :arg=val" << endl;
240  abort();
241  }
242  }
243 
244  // Ensure configuration options have been set.
245  mod->SetDefaults();
246  }
247 
248  // Run mesh process.
249  for (int i = 0; i < modules.size(); ++i)
250  {
251  modules[i]->Process();
252  }
253 
254  return 0;
255 }