DUNE-FEM (unstable)

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