6#ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH 
    7#define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH 
   24#include <dune/grid/io/file/gmshreader.hh> 
   25#include <dune/grid/utility/gridinfo.hh> 
   52const std::string programName = 
"headercheck";
 
   59  void usage(std::ostream &stream) {
 
   61           << 
"  " << programName << 
" [-R REFINES] GRIDFILE\n" 
   64           << 
"  -R REFINES How many global refines to do after reading\n" 
   66           << 
"  GRIDFILE Name of the .msh file to read the grid from.\n" 
   70  bool prefix_match(
const std::string &prefix, 
const std::string &str)
 
   72    return str.compare(0,prefix.size(), prefix) == 0;
 
   75  void error_argument_required(
const std::string &opt) {
 
   76    std::cerr << 
"Error: option " << opt << 
" requires argument\n";
 
   81  void error_unknown_option(
const std::string &opt) {
 
   82    std::cerr << 
"Error: unknown option: " << opt << 
"\n";
 
   87  void error_parsing_optarg(
const std::string &opt, 
const std::string &error) {
 
   88    std::cerr << 
"Error: option " << opt << 
": " << error << 
"\n";
 
   94  void parse(
const std::string &arg, T &val) {
 
   95    std::istringstream s(arg);
 
   97    bool good = !s.fail();
 
  101      good = s.fail() && s.eof();
 
  104      std::ostringstream s;
 
  106      throw std::runtime_error(s.str());
 
  110  std::size_t refines = 0;
 
  111  std::string gridFileName = 
"";
 
  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;
 
  120          for(++argv; *argv; ++argv)
 
  121            params.push_back(*argv);
 
  124        else if(prefix_match(
"-h", opt) || prefix_match(
"-?", opt) ||
 
  130        else if(opt == 
"-R" || opt == 
"--global-refines") {
 
  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()); }
 
  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()); }
 
  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()); }
 
  148          error_unknown_option(opt);
 
  151        params.push_back(arg);
 
  154    if(params.size() < 1) {
 
  155      std::cerr << 
"Need name of a .msh file to read.\n" 
  160    if(params.size() > 1) {
 
  161      std::cerr << 
"Too many arguments.\n" 
  166    gridFileName = params[0];
 
  171int main(
int argc, 
char **argv) {
 
  176    if(mpiHelper.
size() > 1) {
 
  177      if(mpiHelper.
rank() == 0)
 
  178        std::cerr << programName << 
": Sorry, this program works only in " 
  179                  << 
"serial." << std::endl;
 
  183    parseOptions(argc, argv);
 
  187    std::shared_ptr<Grid> gridp(Reader::read(gridFileName));
 
  188    gridp->globalRefine(refines);
 
  195    std::cout << gridViewInfo << std::flush;
 
  197  catch(
const std::exception &e) {
 
  200              << 
"e.what(): " << e.what() << std::endl;
 
  206              << 
"Exception message: " << e << std::endl;
 
  209  catch(
const std::string &s) {
 
  212              << 
"Exception message: " << s << std::endl;
 
  216    std::cerr << 
"Caught exception of unknown type" << std::endl;
 
Base class for Dune-Exceptions.
Definition: exceptions.hh:98
 
Read Gmsh mesh file.
Definition: gmshreader.hh:106
 
A real mpi helper.
Definition: mpihelper.hh:169
 
int size() const
return number of processes
Definition: mpihelper.hh:274
 
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:228
 
int rank() const
return rank of process
Definition: mpihelper.hh:270
 
A free function to provide the demangled class name of a given object or type as a string.
 
A few common exception classes.
 
Helpers for dealing with MPI.
 
std::string className()
Provide the demangled class name of a type T as a string.
Definition: classname.hh:47
 
void fillGridViewInfoSerial(const GV &gv, GridViewInfo< typename GV::ctype > &gridViewInfo)
fill a GridViewInfo structure from a serial grid
Definition: gridinfo.hh:214
 
structure to hold information about a certain GridView.
Definition: gridinfo.hh:100