libMesh
compare.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #include "libmesh/libmesh_config.h"
20 
21 // C++ includes
22 #include <iostream>
23 #include <vector>
24 #include <string>
25 #ifdef LIBMESH_HAVE_GETOPT_H
26 // GCC 2.95.3 (and maybe others) do not include
27 // getopt.h in unistd.h... However IBM xlC has no
28 // getopt.h! This works around that.
29 #include <getopt.h>
30 #endif
31 #include <stdio.h>
32 #include <fstream>
33 
34 // Local Includes
35 #include "libmesh/libmesh.h"
36 #include "libmesh/equation_systems.h"
37 #include "libmesh/mesh.h"
38 #include "libmesh/perfmon.h"
39 #include "libmesh/enum_xdr_mode.h"
40 
41 
42 using namespace libMesh;
43 
44 
48 void usage(const std::string & progName)
49 {
50  std::ostringstream helpList;
51  helpList << "usage:\n"
52  << " "
53  << progName
54  << " [options] ...\n"
55  << "\n"
56  << "options:\n"
57  << " -d <dim> <dim>-dimensional mesh\n"
58  << " -m <string> Mesh file name\n"
59  << " -l <string> Left Equation Systems file name\n"
60  << " -r <string> Right Equation Systems file name\n"
61  << " -t <float> threshold\n"
62  << " -a ASCII format (default)\n"
63  << " -b binary format\n"
64  << " -v Verbose\n"
65  << " -q really quiet\n"
66  << " -h Print help menu\n"
67  << "\n"
68  << "\n"
69  << " This program is used to compare equation systems to a user-specified\n"
70  << " threshold. Equation systems are imported in the libMesh format\n"
71  << " provided through the read and write methods in class EquationSystems.\n"
72  << " \n"
73  << " ./compare -d 3 -m grid.xda -l leftfile.dat -r rightfile.dat -b -t 1.e-8\n"
74  << "\n"
75  << " will read in the mesh grid.xda, the equation systems leftfile.dat and\n"
76  << " rightfile.dat in binary format and compare systems, and especially the\n"
77  << " floats stored in vectors. The comparison is said to be passed when the\n"
78  << " floating point values agree up to the given threshold. When no threshold\n"
79  << " is set the default libMesh tolerance is used. If neither -a or -b are set,\n"
80  << " ASCII format is assumed.\n"
81  << "\n";
82 
83  libmesh_error_msg(helpList.str());
84 }
85 
86 
87 
88 void process_cmd_line(int argc,
89  char ** argv,
90  std::vector<std::string> & names,
91  unsigned char & dim,
92  double & threshold,
93  XdrMODE & format,
94  bool & verbose,
95  bool & quiet)
96 {
97  char optionStr[] =
98  "d:m:l:r:t:abvq?h";
99 
100  int opt;
101 
102  bool format_set = false;
103  bool left_name_set = false;
104 
105  if (argc < 3)
106  usage(std::string(argv[0]));
107 
108 
109  while ((opt = getopt(argc, argv, optionStr)) != -1)
110  {
111  switch (opt)
112  {
113 
117  case 'm':
118  {
119  if (names.empty())
120  names.push_back(optarg);
121  else
122  libmesh_error_msg("ERROR: Mesh file name must precede left file name!");
123  break;
124  }
125 
129  case 'd':
130  {
131  dim = cast_int<unsigned char>(atoi(optarg));
132  break;
133  }
134 
138  case 'l':
139  {
140  if (!left_name_set)
141  {
142  names.push_back(optarg);
143  left_name_set = true;
144  }
145  else
146  libmesh_error_msg("ERROR: Mesh file name must precede right file name!");
147  break;
148  }
149 
153  case 'r':
154  {
155  if ((!names.empty()) && (left_name_set))
156  names.push_back(optarg);
157  else
158  libmesh_error_msg("ERROR: Mesh file name and left file name must precede right file name!");
159  break;
160  }
161 
165  case 't':
166  {
167  threshold = atof(optarg);
168  break;
169  }
170 
174  case 'a':
175  {
176  if (format_set)
177  libmesh_error_msg("ERROR: Equation system file format already set!");
178  else
179  {
180  format = READ;
181  format_set = true;
182  }
183  break;
184  }
185 
189  case 'b':
190  {
191  if (format_set)
192  libmesh_error_msg("ERROR: Equation system file format already set!");
193  else
194  {
195  format = DECODE;
196  format_set = true;
197  }
198  break;
199  }
200 
201 
205  case 'v':
206  {
207  verbose = true;
208  break;
209  }
210 
214  case 'q':
215  {
216  quiet = true;
217  break;
218  }
219 
220  case 'h':
221  case '?':
222  usage(argv[0]);
223 
224  default:
225  return;
226  }
227  }
228 
229 }
230 
231 
232 
233 
234 
241  EquationSystems & res,
242  double threshold,
243  bool verbose)
244 {
245 
246  if (verbose)
247  {
248  libMesh::out << "********* LEFT SYSTEM *********" << std::endl;
249  les.print_info ();
250  libMesh::out << "********* RIGHT SYSTEM *********" << std::endl;
251  res.print_info ();
252  libMesh::out << "********* COMPARISON PHASE *********" << std::endl
253  << std::endl;
254  }
255 
259  bool result = les.compare(res, threshold, verbose);
260  if (verbose)
261  {
262  libMesh::out << "********* FINISHED *********" << std::endl;
263  }
264  return result;
265 }
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 int main (int argc, char ** argv)
277 {
278  LibMeshInit init(argc, argv);
279 
280  // these should not be contained in the following braces
281  bool quiet = false;
282  bool are_equal;
283 
284  PerfMon perfmon(argv[0]);
285 
286  // default values
287  std::vector<std::string> names;
288  unsigned char dim = static_cast<unsigned char>(-1);
289  double threshold = TOLERANCE;
290  XdrMODE format = READ;
291  bool verbose = false;
292 
293  // get commands
294  process_cmd_line(argc, argv,
295  names,
296  dim,
297  threshold,
298  format,
299  verbose,
300  quiet);
301 
302 
303  if (dim == static_cast<unsigned char>(-1))
304  libmesh_error_msg("ERROR: you must specify the dimension on " \
305  << "the command line!\n\n" \
306  << argv[0] \
307  << " -d 3 ... for example");
308 
309  if (quiet)
310  verbose = false;
311 
312  if (verbose)
313  {
314  libMesh::out << "Settings:" << std::endl
315  << " dimensionality = " << +dim << std::endl
316  << " mesh = " << names[0] << std::endl
317  << " left system = " << names[1] << std::endl
318  << " right system = " << names[2] << std::endl
319  << " threshold = " << threshold << std::endl
320  << " read format = " << format << std::endl
321  << std::endl;
322  }
323 
324 
328  Mesh left_mesh (init.comm(), dim);
329  Mesh right_mesh (init.comm(), dim);
330 
331 
332  if (!names.empty())
333  {
334  left_mesh.read (names[0]);
335  right_mesh.read (names[0]);
336 
337  if (verbose)
338  left_mesh.print_info();
339  }
340  else
341  {
342  libMesh::out << "No input specified." << std::endl;
343  return 1;
344  }
345 
346 
350  EquationSystems left_system (left_mesh);
351  EquationSystems right_system (right_mesh);
352 
353  if (names.size() == 3)
354  {
355  left_system.read (names[1], format);
356  right_system.read (names[2], format);
357  }
358  else
359  libmesh_error_msg("Bad input specified.");
360 
361  are_equal = do_compare (left_system, right_system, threshold, verbose);
362 
363 
367  unsigned int our_result;
368 
369  if (are_equal)
370  {
371  if (!quiet)
372  libMesh::out << std::endl
373  << " Congrat's, up to the defined threshold, the two"
374  << std::endl
375  << " are identical."
376  << std::endl;
377  our_result=0;
378  }
379  else
380  {
381  if (!quiet)
382  libMesh::out << std::endl
383  << " Oops, differences occurred!"
384  << std::endl
385  << " Use -v to obtain more information where differences occurred."
386  << std::endl;
387  our_result=1;
388  }
389 
390  // return libMesh::close();
391  return our_result;
392 }
This is the EquationSystems class.
virtual void read(const std::string &name, void *mesh_data=libmesh_nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false) libmesh_override
Reads the file specified by name.
unsigned int dim
static const Real TOLERANCE
void read(const std::string &name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:62
The libMesh namespace provides an interface to certain functionality in the library.
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:32
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
bool do_compare(EquationSystems &les, EquationSystems &res, double threshold, bool verbose)
everything that is identical for the systems, and should not go into EquationSystems::compare(), can go in this do_compare().
Definition: compare.C:240
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
void usage(const std::string &progName)
how to use this, and command line processor
Definition: compare.C:48
const Parallel::Communicator & comm() const
Definition: libmesh.h:81
PAPI stands for Performance Application Programming Interface.
Definition: perfmon.h:52
int main(int argc, char **argv)
Definition: compare.C:276
OStreamProxy out
void process_cmd_line(int argc, char **argv, std::vector< std::string > &names, unsigned char &dim, double &threshold, XdrMODE &format, bool &verbose, bool &quiet)
Definition: compare.C:88
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50