4#ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
5#define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
22#include <dune/grid/io/file/gmshreader.hh>
23#include <dune/grid/utility/gridinfo.hh>
54const std::string programName =
"headercheck";
61 void usage(std::ostream &stream) {
63 <<
" " << programName <<
" [-R REFINES] GRIDFILE\n"
66 <<
" -R REFINES How many global refines to do after reading\n"
68 <<
" GRIDFILE Name of the .msh file to read the grid from.\n"
72 bool prefix_match(
const std::string &prefix,
const std::string &str)
74 return str.compare(0,prefix.size(), prefix) == 0;
77 void error_argument_required(
const std::string &opt) {
78 std::cerr <<
"Error: option " << opt <<
" requires argument\n";
83 void error_unknown_option(
const std::string &opt) {
84 std::cerr <<
"Error: unknown option: " << opt <<
"\n";
89 void error_parsing_optarg(
const std::string &opt,
const std::string &error) {
90 std::cerr <<
"Error: option " << opt <<
": " << error <<
"\n";
96 void parse(
const std::string &arg, T &val) {
97 std::istringstream s(arg);
99 bool good = !s.fail();
103 good = s.fail() && s.eof();
106 std::ostringstream s;
108 throw std::runtime_error(s.str());
112 std::size_t refines = 0;
113 std::string gridFileName =
"";
115 void parseOptions(
int argc,
char **argv) {
116 std::vector<std::string> params;
117 for(++argv; *argv; ++argv) {
118 std::string arg = *argv;
119 if(prefix_match(
"-", arg)) {
120 std::string opt = arg;
122 for(++argv; *argv; ++argv)
123 params.push_back(*argv);
126 else if(prefix_match(
"-h", opt) || prefix_match(
"-?", opt) ||
132 else if(opt ==
"-R" || opt ==
"--global-refines") {
134 if(!*argv) error_argument_required(opt);
135 try { parse(*argv, refines); }
136 catch(
const std::runtime_error &e)
137 { error_parsing_optarg(opt, e.what()); }
139 else if(prefix_match(
"-R", opt)) {
140 try { parse(*argv+std::strlen(
"-R"), refines); }
141 catch(
const std::runtime_error &e)
142 { error_parsing_optarg(opt, e.what()); }
144 else if(prefix_match(
"--global-refines=", opt)) {
145 try { parse(*argv+std::strlen(
"--global-refines="), refines); }
146 catch(
const std::runtime_error &e)
147 { error_parsing_optarg(opt, e.what()); }
150 error_unknown_option(opt);
153 params.push_back(arg);
156 if(params.size() < 1) {
157 std::cerr <<
"Need name of a .msh file to read.\n"
162 if(params.size() > 1) {
163 std::cerr <<
"Too many arguments.\n"
168 gridFileName = params[0];
173int main(
int argc,
char **argv) {
178 if(mpiHelper.
size() > 1) {
179 if(mpiHelper.
rank() == 0)
180 std::cerr << programName <<
": Sorry, this program works only in "
181 <<
"serial." << std::endl;
185 parseOptions(argc, argv);
189 std::shared_ptr<Grid> gridp(Reader::read(gridFileName));
190 gridp->globalRefine(refines);
197 std::cout << gridViewInfo << std::flush;
199 catch(
const std::exception &e) {
202 <<
"e.what(): " << e.what() << std::endl;
208 <<
"Exception message: " << e << std::endl;
211 catch(
const std::string &s) {
214 <<
"Exception message: " << s << std::endl;
218 std::cerr <<
"Caught exception of unknown type" << std::endl;
Base class for Dune-Exceptions.
Definition: exceptions.hh:94
Read Gmsh mesh file.
Definition: gmshreader.hh:657
A real mpi helper.
Definition: mpihelper.hh:160
int size() const
return number of processes
Definition: mpihelper.hh:231
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:217
int rank() const
return rank of process
Definition: mpihelper.hh:227
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:23
void fillGridViewInfoSerial(const GV &gv, GridViewInfo< typename GV::ctype > &gridViewInfo)
fill a GridViewInfo structure from a serial grid
Definition: gridinfo.hh:217
structure to hold information about a certain GridView.
Definition: gridinfo.hh:98