dune-grid  2.2beta1
gridinfo-gmsh-main.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=8 sw=2 sts=2:
00003 
00004 #ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
00005 #define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
00006 
00007 #include <cstddef>
00008 #include <cstdlib>
00009 #include <exception>
00010 #include <iostream>
00011 #include <ostream>
00012 #include <sstream>
00013 #include <stdexcept>
00014 #include <string>
00015 #include <vector>
00016 
00017 #include <dune/common/classname.hh>
00018 #include <dune/common/exceptions.hh>
00019 #include <dune/common/mpihelper.hh>
00020 #include <dune/common/shared_ptr.hh>
00021 
00022 #include <dune/grid/io/file/gmshreader.hh>
00023 #include <dune/grid/utility/gridinfo.hh>
00024 
00052 #ifdef HEADERCHECK
00053 // define so headercheck will run
00054 const std::string programName = "headercheck";
00055 #endif // HEADERCHECK
00056 
00057 #ifndef DOXYGEN
00058 namespace {
00059   // anonymous namespace so we don't freakishly conflict with another usage()
00060   // function that may be linked in from another compilation unit.
00061   void usage(std::ostream &stream) {
00062     stream << "USAGE:\n"
00063            << "  " << programName << " [-R REFINES] GRIDFILE\n"
00064            << "\n"
00065            << "PARAMTERS:\n"
00066            << "  -R REFINES How many global refines to do after reading\n"
00067            << "    (default: 0)\n"
00068            << "  GRIDFILE Name of the .msh file to read the grid from.\n"
00069            << std::flush;
00070   }
00071 
00072   bool prefix_match(const std::string &prefix, const std::string &str)
00073   { return str.compare(0,prefix.size(), prefix) == 0; }
00074 
00075   void error_argument_required(const std::string &opt) {
00076     std::cerr << "Error: option " << opt << " requires argument\n";
00077     usage(std::cerr);
00078     std::exit(1);
00079   }
00080 
00081   void error_unknown_option(const std::string &opt) {
00082     std::cerr << "Error: unknown option: " << opt << "\n";
00083     usage(std::cerr);
00084     std::exit(1);
00085   }
00086 
00087   void error_parsing_optarg(const std::string &opt, const std::string &error) {
00088     std::cerr << "Error: option " << opt << ": " << error << "\n";
00089     usage(std::cerr);
00090     std::exit(1);
00091   }
00092 
00093   template<class T>
00094   void parse(const std::string &arg, T &val) {
00095     std::istringstream s(arg);
00096     s >> val;
00097     bool good = !s.fail();
00098     if(good) {
00099       char dummy;
00100       s >> dummy;
00101       good = s.fail() && s.eof();
00102     }
00103     if(!good) {
00104       std::ostringstream s;
00105       s << "Can't parse \"" << arg << "\" as a " << Dune::className(val);
00106       throw std::runtime_error(s.str());
00107     }
00108   }
00109 
00110   std::size_t refines = 0;
00111   std::string gridFileName = "";
00112 
00113   void parseOptions(int argc, char **argv) {
00114     std::vector<std::string> params;
00115     for(++argv; *argv; ++argv) {
00116       std::string arg = *argv;
00117       if(prefix_match("-", arg)) {
00118         std::string opt = arg;
00119         if(opt == "--") {
00120           for(++argv; *argv; ++argv)
00121             params.push_back(*argv);
00122           break;
00123         }
00124         else if(prefix_match("-h", opt) || prefix_match("-?", opt) ||
00125                 opt == "--help")
00126         {
00127           usage(std::cout);
00128           std::exit(0);
00129         }
00130         else if(opt == "-R" || opt == "--global-refines") {
00131           ++argv;
00132           if(!*argv) error_argument_required(opt);
00133           try { parse(*argv, refines); }
00134           catch(const std::runtime_error &e)
00135           { error_parsing_optarg(opt, e.what()); }
00136         }
00137         else if(prefix_match("-R", opt)) {
00138           try { parse(*argv+std::strlen("-R"), refines); }
00139           catch(const std::runtime_error &e)
00140           { error_parsing_optarg(opt, e.what()); }
00141         }
00142         else if(prefix_match("--global-refines=", opt)) {
00143           try { parse(*argv+std::strlen("--global-refines="), refines); }
00144           catch(const std::runtime_error &e)
00145           { error_parsing_optarg(opt, e.what()); }
00146         }
00147         else
00148           error_unknown_option(opt);
00149       }
00150       else
00151         params.push_back(arg);
00152     }
00153     // check command line arguments
00154     if(params.size() < 1) {
00155       std::cerr << "Need name of a .msh file to read.\n"
00156                 << std::endl;
00157       usage(std::cerr);
00158       std::exit(1);
00159     }
00160     if(params.size() > 1) {
00161       std::cerr << "Too many arguments.\n"
00162                 << std::endl;
00163       usage(std::cerr);
00164       std::exit(1);
00165     }
00166     gridFileName = params[0];
00167   }
00168 }
00169 
00170 #ifndef HEADERCHECK
00171 int main(int argc, char **argv) {
00172   try {
00173     const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
00174 
00175     // check that we are not run through mpirun
00176     if(mpiHelper.size() > 1) {
00177       if(mpiHelper.rank() == 0)
00178         std::cerr << programName << ": Sorry, this program works only in "
00179                   << "serial." << std::endl;
00180       return 1;
00181     }
00182 
00183     parseOptions(argc, argv);
00184 
00185     // read grid
00186     typedef Dune::GmshReader<Grid> Reader;
00187     Dune::shared_ptr<Grid> gridp(Reader::read(gridFileName));
00188     gridp->globalRefine(refines);
00189 
00190     // collect information
00191     Dune::GridViewInfo<Grid::ctype> gridViewInfo;
00192     Dune::fillGridViewInfoSerial(gridp->leafView(), gridViewInfo);
00193 
00194     // print it
00195     std::cout << gridViewInfo << std::flush;
00196   }
00197   catch(const std::exception &e) {
00198     std::cerr << "Caught exception of type " << Dune::className(e)
00199               << std::endl
00200               << "e.what(): " << e.what() << std::endl;
00201     throw;
00202   }
00203   catch(const Dune::Exception &e) {
00204     std::cerr << "Caught exception of type " << Dune::className(e)
00205               << std::endl
00206               << "Exception message: " << e << std::endl;
00207     throw;
00208   }
00209   catch(const std::string &s) {
00210     std::cerr << "Caught exception of type " << Dune::className(s)
00211               << std::endl
00212               << "Exception message: " << s << std::endl;
00213     throw;
00214   }
00215   catch(...) {
00216     std::cerr << "Caught exception of unknown type" << std::endl;
00217     throw;
00218   }
00219 }
00220 #endif // !HEADERCHECK
00221 #endif // !DOXYGEN
00222 
00223 #endif // DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH