LifeV
test_quadrule.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Quadrature Rule test
30 
31  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
32  @author Umberto Villa <uvilla@emory.edu>
33  @contributor
34  @maintainer Umberto Villa <uvilla@emory.edu>
35 
36  @date 02-03-2010
37 
38 The program tests the degree of exactness (DoE) and the convergence rate (CR)
39 of all the quadrature rule in 3D (Tetrahedra) or in 2D (Triangles).
40 
41 The code produce the following output:
42 
43 quadRuleTetra.txt ==> Show the Degree of Exactness of all the quadrature rules on Tetrahedral elements
44 quadRuleTetra.plt ==> Show the Convergence Rate of all the quadrature rules on Tetrahedral elements
45  using gnuplot
46  */
47 
48 
49 #include <fstream>
50 
51 #include <Epetra_SerialComm.h>
52 
53 
54 #include <lifev/core/LifeV.hpp>
55 #include <lifev/core/fem/QuadratureRule.hpp>
56 
57 #include "SetOfFun.hpp"
58 
59 namespace LifeV
60 {
61 typedef boost::numeric::ublas::vector<Real> Vector;
64 
65 // This function checks the DEGREE of Exactness (DoE) of the quadrature rules
66 template<typename Mesh>
67 bool quad_check_doe (const ReferenceFE& refFE, const GeometricMap& geoMap, const container_Type& allQuad, std::string output_file)
68 {
69  std::shared_ptr<Epetra_Comm> dummyComm ( new Epetra_SerialComm );
70  Mesh aMesh ( dummyComm );
71  UInt nEl (1);
72  regularMesh3D ( aMesh, 1, nEl, nEl, nEl);
73 
74  SetofFun fct;
75  int nquadrule = allQuad.size();
76  bool check (false);
77  output_file = output_file + ".txt";
78  std::ofstream ofile (output_file.c_str() );
79 
80 
81  for (Int nqr (0); nqr < nquadrule; ++nqr)
82  {
83 
84  CurrentFE fe (refFE, geoMap, *allQuad[nqr]);
85  ofile << "*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_" << std::endl;
86  ofile << allQuad[nqr]->name() << std::endl;
87  ofile << "Degree of Exactness: " << allQuad[nqr]->degreeOfExactness() << std::endl;
88  ofile << "*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_" << std::endl;
89 
90  ofile << "polynomial \t\t test \t quadrature error" << std::endl;
91 
92  for (UInt fun (0); fun < fct.nfun(); ++fun)
93  {
94  Real integral = 0.;
95  for (UInt i = 0; i < aMesh.numElements(); ++i)
96  {
97  fe.updateJacQuadPt (aMesh.element (i) );
98  Real s = 0., x, y, z;
99  for ( UInt ig (0); ig < fe.nbQuadPt(); ++ig )
100  {
101  fe.coorQuadPt ( x, y, z, ig );
102  s += fct.val ( fun, x, y, z) * fe.weightDet ( ig );
103  }
104  integral += s;
105  }
106  Real err;
107  err = integral - fct.ex_int (fun);
108  // Check for Quadrature Rule Errors
109  if (fct.degree (fun) <= allQuad[nqr]->degreeOfExactness() )
110  {
111  if (std::fabs (err) < 1e-9)
112  {
113  check = true;
114  ofile << fct.name (fun) << " \t passed \t (" << err << ")" << std::endl;
115  }
116  else
117  {
118  check = false;
119  ofile << fct.name (fun) << " \t FAILED \t (" << err << ")" << std::endl;
120  }
121  }
122 
123  }//end for on fun
124  ofile << std::endl << std::endl;
125  }//end for on qr
126  return check;
127 }
128 
129 // This function checks the convergence rate (CR) of the quadrature rules
130 template<typename Mesh>
131 bool quad_check_cr ( const ReferenceFE& refFE, const GeometricMap& geoMap, const container_Type& allQuad, std::string output_name)
132 {
133  std::shared_ptr<Epetra_Comm> dummyComm ( new Epetra_SerialComm );
134  SetofFun fct;
135  int fun (fct.nfun() );
136 
137  int nrefine (4);
138  Vector h (nrefine);
139 
140  for (int i = 0; i < nrefine; ++i)
141  {
142  h (i) = std::pow (.5, i);
143  }
144 
145  int nquadrule = allQuad.size();
146  boost::numeric::ublas::matrix<double> err (nrefine, nquadrule + 1);
147 
148  for (int iref = 0; iref < nrefine; ++iref)
149  {
150 
151  Mesh aMesh ( dummyComm );
152  UInt nEl ( std::pow (2.0, static_cast<double> (iref) ) );
153  regularMesh3D ( aMesh, 1, nEl, nEl, nEl);
154 
155  err (iref, 0) = h (iref);
156  for (int iqr = 0; iqr < nquadrule; ++iqr)
157  {
158 
159  // ===================================================
160  // Current FE classes for the problem under study with
161  // mapping and quadrature rules
162  // ===================================================
163 
164  CurrentFE fe (refFE, geoMap, *allQuad[iqr]);
165  Real integral = 0.;
166  for (UInt i = 0; i < aMesh.numElements(); ++i)
167  {
168  fe.updateJacQuadPt (aMesh.element (i) );
169  Real s = 0., x, y, z;
170  for ( UInt ig (0); ig < fe.nbQuadPt(); ++ig )
171  {
172  fe.coorQuadPt ( x, y, z, ig );
173  s += fct.val ( fun, x, y, z) * fe.weightDet ( ig );
174  }
175  integral += s;
176  }
177  err (iref, iqr + 1) = std::fabs (integral - fct.ex_int (fun) );
178 
179 
180  }//end for on qr
181  }//end for on iref
182 
183  std::string fname;
184  {
185  fname = output_name + ".dat";
186  std::ofstream ofile (fname.c_str() );
187  for (int i = 0; i < nrefine; ++i)
188  {
189  for (int j = 0; j < nquadrule + 1; ++j)
190  {
191  ofile << err (i, j) << " \t";
192  }
193  ofile << std::endl;
194  }
195  }
196  {
197  fname = output_name + ".plt";
198  std::ofstream ofile (fname.c_str() );
199  ofile << "set timestamp" << std::endl;
200  ofile << "set logscale" << std::endl;
201  ofile << "plot ";
202  for (int i = 0; i < nquadrule; ++i)
203  {
204  ofile << "\"" << output_name << ".dat\" using 1:" << i + 2 << " with linespoints title '" << allQuad[i]->name() << "' ,\\" << std::endl;
205  }
206  ofile << "\"" << output_name << ".dat\" using 1:($1*$1) with lines title 'order 2', \\" << std::endl;
207  ofile << "\"" << output_name << ".dat\" using 1:($1**4) with lines title 'order 4', \\" << std::endl;
208  ofile << "\"" << output_name << ".dat\" using 1:($1**6) with lines title 'order 6'";
209  }
210 
211  return true;
212 
213 }
214 
215 }//end namespace
GeometricMap - Structure for the geometrical mapping.
bool quad_check_doe(const ReferenceFE &refFE, const GeometricMap &geoMap, const container_Type &allQuad, std::string output_file)
container_Type::const_iterator constIterator_Type
The class for a reference Lagrangian finite element.
std::vector< QuadratureRule const * > container_Type