6#ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
7#define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
20#include <dune/common/classname.hh>
21#include <dune/common/exceptions.hh>
22#include <dune/common/parallel/mpihelper.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;
105 s <<
"Can't parse \"" << arg <<
"\" as a " << Dune::className(val);
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) {
173 const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, 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) {
198 std::cerr <<
"Caught exception of type " << Dune::className(e)
200 <<
"e.what(): " << e.what() << std::endl;
203 catch(
const Dune::Exception &e) {
204 std::cerr <<
"Caught exception of type " << Dune::className(e)
206 <<
"Exception message: " << e << std::endl;
209 catch(
const std::string &s) {
210 std::cerr <<
"Caught exception of type " << Dune::className(s)
212 <<
"Exception message: " << s << std::endl;
216 std::cerr <<
"Caught exception of unknown type" << std::endl;
int main(int argc, char **argv)
Definition recipe-integration.cc:65
void fillGridViewInfoSerial(const GV &gv, GridViewInfo< typename GV::ctype > &gridViewInfo)
fill a GridViewInfo structure from a serial grid
Definition utility/gridinfo.hh:214
Read Gmsh mesh file.
Definition gmshreader.hh:840
structure to hold information about a certain GridView.
Definition utility/gridinfo.hh:100