Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions
NekMesh.cpp File Reference
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <NekMeshUtils/Module/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 47 of file NekMesh.cpp.

References Nektar::LibUtilities::NekFactory< tKey, tBase, >::CreateInstance(), Nektar::FieldUtils::eInputModule, Nektar::FieldUtils::eOutputModule, Nektar::FieldUtils::eProcessModule, Nektar::FieldUtils::GetModuleFactory(), CG_Iterations::Mesh, CellMLToNektar.cellml_metadata::p, Nektar::LibUtilities::NekFactory< tKey, tBase, >::PrintAvailableClasses(), and CellMLToNektar.translators::run().

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: NekMesh [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 
203  if (ext == "gz")
204  {
205  string tmp = tmp1[0].substr(0, tmp1[0].length() - 3);
206  dot = tmp.find_last_of('.') + 1;
207  ext = tmp.substr(dot, tmp.length() - dot);
208  }
209 
210  module.second = ext;
211  tmp1.push_back(string(i == 0 ? "infile=" : "outfile=")+tmp1[0]);
212  }
213  else
214  {
215  module.second = tmp1[1];
216  tmp1.push_back(string(i == 0 ? "infile=" : "outfile=")+tmp1[0]);
217  offset++;
218  }
219  }
220  else
221  {
222  module.first = eProcessModule;
223  module.second = tmp1[0];
224  }
225 
226  // Create module.
227  ModuleSharedPtr mod = GetModuleFactory().CreateInstance(module,mesh);
228  modules.push_back(mod);
229 
230  // Set options for this module.
231  for (int j = offset; j < tmp1.size(); ++j)
232  {
233  vector<string> tmp2;
234  boost::split(tmp2, tmp1[j], boost::is_any_of("="));
235 
236  if (tmp2.size() == 1)
237  {
238  mod->RegisterConfig(tmp2[0], "1");
239  }
240  else if (tmp2.size() == 2)
241  {
242  mod->RegisterConfig(tmp2[0], tmp2[1]);
243  }
244  else
245  {
246  cerr << "ERROR: Invalid module configuration: format is "
247  << "either :arg or :arg=val" << endl;
248  abort();
249  }
250  }
251 
252  // Ensure configuration options have been set.
253  mod->SetDefaults();
254  }
255 
256  // Run mesh process.
257  for (int i = 0; i < modules.size(); ++i)
258  {
259  modules[i]->Process();
260  }
261 
262  return 0;
263 }
void PrintAvailableClasses(std::ostream &pOut=std::cout)
Prints the available classes to stdout.
Definition: NekFactory.hpp:247
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
pair< ModuleType, string > ModuleKey
boost::shared_ptr< Module > ModuleSharedPtr
boost::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:147
std::pair< ModuleType, std::string > ModuleKey
ModuleFactory & GetModuleFactory()