dune-grid  2.2.0
gridinfo-gmsh-main.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
5 #define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
6 
7 #include <cstddef>
8 #include <cstdlib>
9 #include <exception>
10 #include <iostream>
11 #include <ostream>
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 #include <dune/common/classname.hh>
18 #include <dune/common/exceptions.hh>
19 #include <dune/common/mpihelper.hh>
20 #include <dune/common/shared_ptr.hh>
21 
24 
52 #ifdef HEADERCHECK
53 // define so headercheck will run
54 const std::string programName = "headercheck";
55 #endif // HEADERCHECK
56 
57 #ifndef DOXYGEN
58 namespace {
59  // anonymous namespace so we don't freakishly conflict with another usage()
60  // function that may be linked in from another compilation unit.
61  void usage(std::ostream &stream) {
62  stream << "USAGE:\n"
63  << " " << programName << " [-R REFINES] GRIDFILE\n"
64  << "\n"
65  << "PARAMTERS:\n"
66  << " -R REFINES How many global refines to do after reading\n"
67  << " (default: 0)\n"
68  << " GRIDFILE Name of the .msh file to read the grid from.\n"
69  << std::flush;
70  }
71 
72  bool prefix_match(const std::string &prefix, const std::string &str)
73  { return str.compare(0,prefix.size(), prefix) == 0; }
74 
75  void error_argument_required(const std::string &opt) {
76  std::cerr << "Error: option " << opt << " requires argument\n";
77  usage(std::cerr);
78  std::exit(1);
79  }
80 
81  void error_unknown_option(const std::string &opt) {
82  std::cerr << "Error: unknown option: " << opt << "\n";
83  usage(std::cerr);
84  std::exit(1);
85  }
86 
87  void error_parsing_optarg(const std::string &opt, const std::string &error) {
88  std::cerr << "Error: option " << opt << ": " << error << "\n";
89  usage(std::cerr);
90  std::exit(1);
91  }
92 
93  template<class T>
94  void parse(const std::string &arg, T &val) {
95  std::istringstream s(arg);
96  s >> val;
97  bool good = !s.fail();
98  if(good) {
99  char dummy;
100  s >> dummy;
101  good = s.fail() && s.eof();
102  }
103  if(!good) {
104  std::ostringstream s;
105  s << "Can't parse \"" << arg << "\" as a " << Dune::className(val);
106  throw std::runtime_error(s.str());
107  }
108  }
109 
110  std::size_t refines = 0;
111  std::string gridFileName = "";
112 
113  void parseOptions(int argc, char **argv) {
114  std::vector<std::string> params;
115  for(++argv; *argv; ++argv) {
116  std::string arg = *argv;
117  if(prefix_match("-", arg)) {
118  std::string opt = arg;
119  if(opt == "--") {
120  for(++argv; *argv; ++argv)
121  params.push_back(*argv);
122  break;
123  }
124  else if(prefix_match("-h", opt) || prefix_match("-?", opt) ||
125  opt == "--help")
126  {
127  usage(std::cout);
128  std::exit(0);
129  }
130  else if(opt == "-R" || opt == "--global-refines") {
131  ++argv;
132  if(!*argv) error_argument_required(opt);
133  try { parse(*argv, refines); }
134  catch(const std::runtime_error &e)
135  { error_parsing_optarg(opt, e.what()); }
136  }
137  else if(prefix_match("-R", opt)) {
138  try { parse(*argv+std::strlen("-R"), refines); }
139  catch(const std::runtime_error &e)
140  { error_parsing_optarg(opt, e.what()); }
141  }
142  else if(prefix_match("--global-refines=", opt)) {
143  try { parse(*argv+std::strlen("--global-refines="), refines); }
144  catch(const std::runtime_error &e)
145  { error_parsing_optarg(opt, e.what()); }
146  }
147  else
148  error_unknown_option(opt);
149  }
150  else
151  params.push_back(arg);
152  }
153  // check command line arguments
154  if(params.size() < 1) {
155  std::cerr << "Need name of a .msh file to read.\n"
156  << std::endl;
157  usage(std::cerr);
158  std::exit(1);
159  }
160  if(params.size() > 1) {
161  std::cerr << "Too many arguments.\n"
162  << std::endl;
163  usage(std::cerr);
164  std::exit(1);
165  }
166  gridFileName = params[0];
167  }
168 }
169 
170 #ifndef HEADERCHECK
171 int main(int argc, char **argv) {
172  try {
173  const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
174 
175  // check that we are not run through mpirun
176  if(mpiHelper.size() > 1) {
177  if(mpiHelper.rank() == 0)
178  std::cerr << programName << ": Sorry, this program works only in "
179  << "serial." << std::endl;
180  return 1;
181  }
182 
183  parseOptions(argc, argv);
184 
185  // read grid
186  typedef Dune::GmshReader<Grid> Reader;
187  Dune::shared_ptr<Grid> gridp(Reader::read(gridFileName));
188  gridp->globalRefine(refines);
189 
190  // collect information
191  Dune::GridViewInfo<Grid::ctype> gridViewInfo;
192  Dune::fillGridViewInfoSerial(gridp->leafView(), gridViewInfo);
193 
194  // print it
195  std::cout << gridViewInfo << std::flush;
196  }
197  catch(const std::exception &e) {
198  std::cerr << "Caught exception of type " << Dune::className(e)
199  << std::endl
200  << "e.what(): " << e.what() << std::endl;
201  throw;
202  }
203  catch(const Dune::Exception &e) {
204  std::cerr << "Caught exception of type " << Dune::className(e)
205  << std::endl
206  << "Exception message: " << e << std::endl;
207  throw;
208  }
209  catch(const std::string &s) {
210  std::cerr << "Caught exception of type " << Dune::className(s)
211  << std::endl
212  << "Exception message: " << s << std::endl;
213  throw;
214  }
215  catch(...) {
216  std::cerr << "Caught exception of unknown type" << std::endl;
217  throw;
218  }
219 }
220 #endif // !HEADERCHECK
221 #endif // !DOXYGEN
222 
223 #endif // DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH