Dune Core Modules (2.8.0)

gridinfo-gmsh-main.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
5#define DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
6
7#include <cstddef>
8#include <cstdlib>
9#include <exception>
10#include <iostream>
11#include <memory>
12#include <ostream>
13#include <sstream>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
21
22#include <dune/grid/io/file/gmshreader.hh>
23#include <dune/grid/utility/gridinfo.hh>
24
52#ifdef HEADERCHECK
53// define so headercheck will run
54const std::string programName = "headercheck";
55#endif // HEADERCHECK
56
57#ifndef DOXYGEN
58namespace {
59 // anonymous namespace so we don't freakishly conflict with another usage()
60 // function that may be linked in from another compilation unit.
61 void usage(std::ostream &stream) {
62 stream << "USAGE:\n"
63 << " " << programName << " [-R REFINES] GRIDFILE\n"
64 << "\n"
65 << "PARAMETERS:\n"
66 << " -R REFINES How many global refines to do after reading\n"
67 << " (default: 0)\n"
68 << " GRIDFILE Name of the .msh file to read the grid from.\n"
69 << std::flush;
70 }
71
72 bool prefix_match(const std::string &prefix, const std::string &str)
73 {
74 return str.compare(0,prefix.size(), prefix) == 0;
75 }
76
77 void error_argument_required(const std::string &opt) {
78 std::cerr << "Error: option " << opt << " requires argument\n";
79 usage(std::cerr);
80 std::exit(1);
81 }
82
83 void error_unknown_option(const std::string &opt) {
84 std::cerr << "Error: unknown option: " << opt << "\n";
85 usage(std::cerr);
86 std::exit(1);
87 }
88
89 void error_parsing_optarg(const std::string &opt, const std::string &error) {
90 std::cerr << "Error: option " << opt << ": " << error << "\n";
91 usage(std::cerr);
92 std::exit(1);
93 }
94
95 template<class T>
96 void parse(const std::string &arg, T &val) {
97 std::istringstream s(arg);
98 s >> val;
99 bool good = !s.fail();
100 if(good) {
101 char dummy;
102 s >> dummy;
103 good = s.fail() && s.eof();
104 }
105 if(!good) {
106 std::ostringstream s;
107 s << "Can't parse \"" << arg << "\" as a " << Dune::className(val);
108 throw std::runtime_error(s.str());
109 }
110 }
111
112 std::size_t refines = 0;
113 std::string gridFileName = "";
114
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;
121 if(opt == "--") {
122 for(++argv; *argv; ++argv)
123 params.push_back(*argv);
124 break;
125 }
126 else if(prefix_match("-h", opt) || prefix_match("-?", opt) ||
127 opt == "--help")
128 {
129 usage(std::cout);
130 std::exit(0);
131 }
132 else if(opt == "-R" || opt == "--global-refines") {
133 ++argv;
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()); }
138 }
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()); }
143 }
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()); }
148 }
149 else
150 error_unknown_option(opt);
151 }
152 else
153 params.push_back(arg);
154 }
155 // check command line arguments
156 if(params.size() < 1) {
157 std::cerr << "Need name of a .msh file to read.\n"
158 << std::endl;
159 usage(std::cerr);
160 std::exit(1);
161 }
162 if(params.size() > 1) {
163 std::cerr << "Too many arguments.\n"
164 << std::endl;
165 usage(std::cerr);
166 std::exit(1);
167 }
168 gridFileName = params[0];
169 }
170}
171
172#ifndef HEADERCHECK
173int main(int argc, char **argv) {
174 try {
175 const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
176
177 // check that we are not run through mpirun
178 if(mpiHelper.size() > 1) {
179 if(mpiHelper.rank() == 0)
180 std::cerr << programName << ": Sorry, this program works only in "
181 << "serial." << std::endl;
182 return 1;
183 }
184
185 parseOptions(argc, argv);
186
187 // read grid
188 typedef Dune::GmshReader<Grid> Reader;
189 std::shared_ptr<Grid> gridp(Reader::read(gridFileName));
190 gridp->globalRefine(refines);
191
192 // collect information
194 Dune::fillGridViewInfoSerial(gridp->leafGridView(), gridViewInfo);
195
196 // print it
197 std::cout << gridViewInfo << std::flush;
198 }
199 catch(const std::exception &e) {
200 std::cerr << "Caught exception of type " << Dune::className(e)
201 << std::endl
202 << "e.what(): " << e.what() << std::endl;
203 throw;
204 }
205 catch(const Dune::Exception &e) {
206 std::cerr << "Caught exception of type " << Dune::className(e)
207 << std::endl
208 << "Exception message: " << e << std::endl;
209 throw;
210 }
211 catch(const std::string &s) {
212 std::cerr << "Caught exception of type " << Dune::className(s)
213 << std::endl
214 << "Exception message: " << s << std::endl;
215 throw;
216 }
217 catch(...) {
218 std::cerr << "Caught exception of unknown type" << std::endl;
219 throw;
220 }
221}
222#endif // !HEADERCHECK
223#endif // !DOXYGEN
224
225#endif // DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
Base class for Dune-Exceptions.
Definition: exceptions.hh:94
Read Gmsh mesh file.
Definition: gmshreader.hh:772
A real mpi helper.
Definition: mpihelper.hh:175
int size() const
return number of processes
Definition: mpihelper.hh:266
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:240
int rank() const
return rank of process
Definition: mpihelper.hh:262
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:45
void fillGridViewInfoSerial(const GV &gv, GridViewInfo< typename GV::ctype > &gridViewInfo)
fill a GridViewInfo structure from a serial grid
Definition: gridinfo.hh:212
structure to hold information about a certain GridView.
Definition: gridinfo.hh:98
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)