Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
NekMesh.cpp File Reference
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include "Module.h"
Include dependency graph for NekMesh.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 48 of file NekMesh.cpp.

References Nektar::LibUtilities::NekFactory< tKey, tBase, >::CreateInstance(), Nektar::Utilities::eInputModule, Nektar::Utilities::eOutputModule, Nektar::Utilities::eProcessModule, Nektar::Utilities::GetModuleFactory(), and Nektar::LibUtilities::NekFactory< tKey, tBase, >::PrintAvailableClasses().

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