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>
56const std::string programName =
"headercheck";
63 void usage(std::ostream &stream) {
65 <<
" " << programName <<
" [-R REFINES] GRIDFILE\n"
68 <<
" -R REFINES How many global refines to do after reading\n"
70 <<
" GRIDFILE Name of the .msh file to read the grid from.\n"
74 bool prefix_match(
const std::string &prefix,
const std::string &str)
76 return str.compare(0,prefix.size(), prefix) == 0;
79 void error_argument_required(
const std::string &opt) {
80 std::cerr <<
"Error: option " << opt <<
" requires argument\n";
85 void error_unknown_option(
const std::string &opt) {
86 std::cerr <<
"Error: unknown option: " << opt <<
"\n";
91 void error_parsing_optarg(
const std::string &opt,
const std::string &error) {
92 std::cerr <<
"Error: option " << opt <<
": " << error <<
"\n";
98 void parse(
const std::string &arg, T &val) {
99 std::istringstream s(arg);
101 bool good = !s.fail();
105 good = s.fail() && s.eof();
108 std::ostringstream s;
109 s <<
"Can't parse \"" << arg <<
"\" as a " << Dune::className(val);
110 throw std::runtime_error(s.str());
114 std::size_t refines = 0;
115 std::string gridFileName =
"";
117 void parseOptions(
int argc,
char **argv) {
118 std::vector<std::string> params;
119 for(++argv; *argv; ++argv) {
120 std::string arg = *argv;
121 if(prefix_match(
"-", arg)) {
122 std::string opt = arg;
124 for(++argv; *argv; ++argv)
125 params.push_back(*argv);
128 else if(prefix_match(
"-h", opt) || prefix_match(
"-?", opt) ||
134 else if(opt ==
"-R" || opt ==
"--global-refines") {
136 if(!*argv) error_argument_required(opt);
137 try { parse(*argv, refines); }
138 catch(
const std::runtime_error &e)
139 { error_parsing_optarg(opt, e.what()); }
141 else if(prefix_match(
"-R", opt)) {
142 try { parse(*argv+std::strlen(
"-R"), refines); }
143 catch(
const std::runtime_error &e)
144 { error_parsing_optarg(opt, e.what()); }
146 else if(prefix_match(
"--global-refines=", opt)) {
147 try { parse(*argv+std::strlen(
"--global-refines="), refines); }
148 catch(
const std::runtime_error &e)
149 { error_parsing_optarg(opt, e.what()); }
152 error_unknown_option(opt);
155 params.push_back(arg);
158 if(params.size() < 1) {
159 std::cerr <<
"Need name of a .msh file to read.\n"
164 if(params.size() > 1) {
165 std::cerr <<
"Too many arguments.\n"
170 gridFileName = params[0];
175int main(
int argc,
char **argv) {
177 const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
180 if(mpiHelper.size() > 1) {
181 if(mpiHelper.rank() == 0)
182 std::cerr << programName <<
": Sorry, this program works only in "
183 <<
"serial." << std::endl;
187 parseOptions(argc, argv);
191 std::shared_ptr<Grid> gridp(Reader::read(gridFileName));
192 gridp->globalRefine(refines);
199 std::cout << gridViewInfo << std::flush;
201 catch(
const std::exception &e) {
202 std::cerr <<
"Caught exception of type " << Dune::className(e)
204 <<
"e.what(): " << e.what() << std::endl;
207 catch(
const Dune::Exception &e) {
208 std::cerr <<
"Caught exception of type " << Dune::className(e)
210 <<
"Exception message: " << e << std::endl;
213 catch(
const std::string &s) {
214 std::cerr <<
"Caught exception of type " << Dune::className(s)
216 <<
"Exception message: " << s << std::endl;
220 std::cerr <<
"Caught exception of unknown type" << std::endl;
int main(int argc, char **argv)
Definition: recipe-integration.cc:70
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:805
structure to hold information about a certain GridView.
Definition: utility/gridinfo.hh:100