Dune Core Modules (2.9.0)

gridinfo-gmsh-main.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 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
54#ifdef HEADERCHECK
55// define so headercheck will run
56const std::string programName = "headercheck";
57#endif // HEADERCHECK
58
59#ifndef DOXYGEN
60namespace {
61 // anonymous namespace so we don't freakishly conflict with another usage()
62 // function that may be linked in from another compilation unit.
63 void usage(std::ostream &stream) {
64 stream << "USAGE:\n"
65 << " " << programName << " [-R REFINES] GRIDFILE\n"
66 << "\n"
67 << "PARAMETERS:\n"
68 << " -R REFINES How many global refines to do after reading\n"
69 << " (default: 0)\n"
70 << " GRIDFILE Name of the .msh file to read the grid from.\n"
71 << std::flush;
72 }
73
74 bool prefix_match(const std::string &prefix, const std::string &str)
75 {
76 return str.compare(0,prefix.size(), prefix) == 0;
77 }
78
79 void error_argument_required(const std::string &opt) {
80 std::cerr << "Error: option " << opt << " requires argument\n";
81 usage(std::cerr);
82 std::exit(1);
83 }
84
85 void error_unknown_option(const std::string &opt) {
86 std::cerr << "Error: unknown option: " << opt << "\n";
87 usage(std::cerr);
88 std::exit(1);
89 }
90
91 void error_parsing_optarg(const std::string &opt, const std::string &error) {
92 std::cerr << "Error: option " << opt << ": " << error << "\n";
93 usage(std::cerr);
94 std::exit(1);
95 }
96
97 template<class T>
98 void parse(const std::string &arg, T &val) {
99 std::istringstream s(arg);
100 s >> val;
101 bool good = !s.fail();
102 if(good) {
103 char dummy;
104 s >> dummy;
105 good = s.fail() && s.eof();
106 }
107 if(!good) {
108 std::ostringstream s;
109 s << "Can't parse \"" << arg << "\" as a " << Dune::className(val);
110 throw std::runtime_error(s.str());
111 }
112 }
113
114 std::size_t refines = 0;
115 std::string gridFileName = "";
116
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;
123 if(opt == "--") {
124 for(++argv; *argv; ++argv)
125 params.push_back(*argv);
126 break;
127 }
128 else if(prefix_match("-h", opt) || prefix_match("-?", opt) ||
129 opt == "--help")
130 {
131 usage(std::cout);
132 std::exit(0);
133 }
134 else if(opt == "-R" || opt == "--global-refines") {
135 ++argv;
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()); }
140 }
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()); }
145 }
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()); }
150 }
151 else
152 error_unknown_option(opt);
153 }
154 else
155 params.push_back(arg);
156 }
157 // check command line arguments
158 if(params.size() < 1) {
159 std::cerr << "Need name of a .msh file to read.\n"
160 << std::endl;
161 usage(std::cerr);
162 std::exit(1);
163 }
164 if(params.size() > 1) {
165 std::cerr << "Too many arguments.\n"
166 << std::endl;
167 usage(std::cerr);
168 std::exit(1);
169 }
170 gridFileName = params[0];
171 }
172}
173
174#ifndef HEADERCHECK
175int main(int argc, char **argv) {
176 try {
177 const Dune::MPIHelper &mpiHelper = Dune::MPIHelper::instance(argc, argv);
178
179 // check that we are not run through mpirun
180 if(mpiHelper.size() > 1) {
181 if(mpiHelper.rank() == 0)
182 std::cerr << programName << ": Sorry, this program works only in "
183 << "serial." << std::endl;
184 return 1;
185 }
186
187 parseOptions(argc, argv);
188
189 // read grid
190 typedef Dune::GmshReader<Grid> Reader;
191 std::shared_ptr<Grid> gridp(Reader::read(gridFileName));
192 gridp->globalRefine(refines);
193
194 // collect information
196 Dune::fillGridViewInfoSerial(gridp->leafGridView(), gridViewInfo);
197
198 // print it
199 std::cout << gridViewInfo << std::flush;
200 }
201 catch(const std::exception &e) {
202 std::cerr << "Caught exception of type " << Dune::className(e)
203 << std::endl
204 << "e.what(): " << e.what() << std::endl;
205 throw;
206 }
207 catch(const Dune::Exception &e) {
208 std::cerr << "Caught exception of type " << Dune::className(e)
209 << std::endl
210 << "Exception message: " << e << std::endl;
211 throw;
212 }
213 catch(const std::string &s) {
214 std::cerr << "Caught exception of type " << Dune::className(s)
215 << std::endl
216 << "Exception message: " << s << std::endl;
217 throw;
218 }
219 catch(...) {
220 std::cerr << "Caught exception of unknown type" << std::endl;
221 throw;
222 }
223}
224#endif // !HEADERCHECK
225#endif // !DOXYGEN
226
227#endif // DUNE_GRID_UTILITY_GRIDINFO_GMSH_MAIN_HH
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Read Gmsh mesh file.
Definition: gmshreader.hh:805
A real mpi helper.
Definition: mpihelper.hh:179
int size() const
return number of processes
Definition: mpihelper.hh:272
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:246
int rank() const
return rank of process
Definition: mpihelper.hh:268
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 (Dec 21, 23:30, 2024)