COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-mesh-gausslegendre.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Tests mesh interpolation"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/assign/list_of.hpp>
12 #include <boost/assign/std/vector.hpp>
13 
14 #include "common/Log.hpp"
15 #include "common/Core.hpp"
16 
17 #include "mesh/Quadrature.hpp"
22 #include "mesh/ElementType.hpp"
23 #include "mesh/ShapeFunction.hpp"
24 
25 using namespace boost;
26 using namespace boost::assign;
27 using namespace cf3;
28 using namespace cf3::mesh;
29 using namespace cf3::common;
30 
32 
33 struct Fixture
34 {
37  {
38  int* argc = &boost::unit_test::framework::master_test_suite().argc;
39  char*** argv = &boost::unit_test::framework::master_test_suite().argv;
40  }
41 
44  {
45  }
46 
48  template <Uint P>
49  double integrate(double (*f)(double), double a, double b)
50  {
51  double c1 = (b - a) / 2, c2 = (b + a) / 2, sum = 0;
52  for (int i = 0; i < P; i++)
54  return c1 * sum;
55  }
56 
58  int m_argc;
59  char** m_argv;
60 
61 };
62 
64 
65 BOOST_FIXTURE_TEST_SUITE( TestSuite, Fixture )
66 
67 
69 BOOST_AUTO_TEST_CASE( Line_static )
70 {
71  CFinfo << 1 << " roots " << gausslegendre::Line<1>::local_coordinates().transpose() << CFendl;
72  CFinfo << 1 << " weights " << gausslegendre::Line<1>::weights() << CFendl;
73 
74  CFinfo << 2 << " roots " << gausslegendre::Line<2>::local_coordinates().transpose() << CFendl;
75  CFinfo << 2 << " weights " << gausslegendre::Line<2>::weights() << CFendl;
76 
77  CFinfo << 3 << " roots " << gausslegendre::Line<3>::local_coordinates().transpose() << CFendl;
78  CFinfo << 3 << " weights " << gausslegendre::Line<3>::weights() << CFendl;
79 
80  CFinfo << 4 << " roots " << gausslegendre::Line<4>::local_coordinates().transpose() << CFendl;
81  CFinfo << 4 << " weights " << gausslegendre::Line<4>::weights() << CFendl;
82 
83  CFinfo << 5 << " roots " << gausslegendre::Line<5>::local_coordinates().transpose() << CFendl;
84  CFinfo << 5 << " weights " << gausslegendre::Line<5>::weights() << CFendl;
85 
86  CFinfo << 8 << " roots " << gausslegendre::Line<8>::local_coordinates().transpose() << CFendl;
87  CFinfo << 8 << " weights " << gausslegendre::Line<8>::weights() << CFendl;
88 
89  CFinfo << 10 << " roots " << gausslegendre::Line<10>::local_coordinates().transpose() << CFendl;
90  CFinfo << 10 << " weights " << gausslegendre::Line<10>::weights() << CFendl;
91 
92  CFinfo << integrate<1 >(std::exp, 0.,1.) << CFendl;
93  CFinfo << integrate<2 >(std::exp, 0.,1.) << CFendl;
94  CFinfo << integrate<3 >(std::exp, 0.,1.) << CFendl;
95  CFinfo << integrate<4 >(std::exp, 0.,1.) << CFendl;
96  CFinfo << integrate<5 >(std::exp, 0.,1.) << CFendl;
97  CFinfo << integrate<8 >(std::exp, 0.,1.) << CFendl;
98  CFinfo << integrate<10>(std::exp, 0.,1.) << CFendl;
99 }
100 
101 
102 BOOST_AUTO_TEST_CASE( Line_dynamic )
103 {
104  Handle<mesh::Quadrature> lineP1 = Core::instance().root().create_component<mesh::Quadrature>("lineP1","cf3.mesh.gausslegendre.LineP1");
105  CFinfo << 1 << " roots " << lineP1->local_coordinates().transpose() << CFendl;
106  CFinfo << 1 << " weights " << lineP1->weights() << CFendl;
107 
108  Handle<mesh::Quadrature> lineP2 = Core::instance().root().create_component<mesh::Quadrature>("lineP2","cf3.mesh.gausslegendre.LineP2");
109  CFinfo << 2 << " roots " << lineP2->local_coordinates().transpose() << CFendl;
110  CFinfo << 2 << " weights " << lineP2->weights() << CFendl;
111 
112  Handle<mesh::Quadrature> lineP3 = Core::instance().root().create_component<mesh::Quadrature>("lineP3","cf3.mesh.gausslegendre.LineP3");
113  CFinfo << 3 << " roots " << lineP3->local_coordinates().transpose() << CFendl;
114  CFinfo << 3 << " weights " << lineP3->weights() << CFendl;
115 
116  Handle<mesh::Quadrature> lineP4 = Core::instance().root().create_component<mesh::Quadrature>("lineP4","cf3.mesh.gausslegendre.LineP4");
117  CFinfo << 4 << " roots " << lineP4->local_coordinates().transpose() << CFendl;
118  CFinfo << 4 << " weights " << lineP4->weights() << CFendl;
119 
120  Handle<mesh::Quadrature> lineP5 = Core::instance().root().create_component<mesh::Quadrature>("lineP5","cf3.mesh.gausslegendre.LineP5");
121  CFinfo << 5 << " roots " << lineP5->local_coordinates().transpose() << CFendl;
122  CFinfo << 5 << " weights " << lineP5->weights() << CFendl;
123 }
124 
125 BOOST_AUTO_TEST_CASE( LagrangeP2_Line_integral_static )
126 {
127  // A third order polynomial function will be integrated exactly by a gauss legendre quadrature of order 2.
128  // Exact integral for polynomial order 2*p-1
129  typedef LagrangeP2::Line1D ETYPE;
130  typedef gausslegendre::Line<2> QDR;
131  Real a=0, b=4;
132  ETYPE::NodesT elem_coords; elem_coords << a, b, 0.5*(a+b) ;
133  Eigen::Matrix<Real, ETYPE::nb_nodes, 1> func;
134 
135  // Set function to x^2
136  for (Uint n=0; n<ETYPE::nb_nodes; ++n)
137  {
138  Real x = elem_coords[n];
139  func[n] = std::pow(x,2.);
140  }
141 
142  Real integral(0.);
143  for( Uint n=0; n<QDR::nb_nodes; ++n)
144  {
145  ETYPE::SF::ValueT interpolate = ETYPE::SF::value( QDR::local_coordinates().row(n) );
146  Real Jdet = ETYPE::jacobian_determinant( QDR::local_coordinates().row(n), elem_coords );
147  integral += Jdet * QDR::weights()[n] * interpolate * func;
148  }
149  BOOST_CHECK_CLOSE(integral, std::pow(b,3.)/3.,1e-10); // integral(x^2) = x^3/3
150 }
151 
152 BOOST_AUTO_TEST_CASE( LagrangeP2_Line_integral_dynamic )
153 {
154  // A third order polynomial function will be integrated exactly by a gauss legendre quadrature of order 2.
155  // Exact integral for polynomial order 2*p-1
156  Handle<mesh::ElementType> etype = Core::instance().root().create_component<mesh::ElementType>("etype","cf3.mesh.LagrangeP2.Line2D");
157  Handle<mesh::Quadrature > qdr = Core::instance().root().create_component<mesh::Quadrature >("qdr", "cf3.mesh.gausslegendre.LineP2");
158 
159  Real a=0, b=4;
160  Real ax=a*std::sqrt(2.)/2., ay=a*std::sqrt(2.)/2.;
161  Real bx=b*std::sqrt(2.)/2., by=b*std::sqrt(2.)/2.;
162  RealMatrix elem_coords(etype->nb_nodes(),etype->dimension());
163  RealVector func(etype->nb_nodes());
164 
165  elem_coords <<
166  ax, ay,
167  bx, by,
168  0.5*(ax+bx), 0.5*(ay+by) ;
169 
170  // Set function to x^2
171  for (Uint n=0; n<etype->nb_nodes(); ++n)
172  {
173  Real x = elem_coords(n,XX);
174  Real y = elem_coords(n,YY);
175  Real r = std::sqrt(x*x+y*y);
176  func[n] = std::pow(r,2.);
177  }
178 
179  Real integral(0.);
180  for( Uint n=0; n<qdr->nb_nodes(); ++n)
181  {
182  RealRowVector interpolate = etype->shape_function().value( qdr->local_coordinates().row(n) );
183  Real Jdet = etype->jacobian_determinant( qdr->local_coordinates().row(n), elem_coords );
184  integral += Jdet * qdr->weights()[n] * interpolate * func;
185  }
186  BOOST_CHECK_CLOSE(integral, std::pow(b,3.)/3.,1e-10); // integral(x^2) = x^3/3
187 }
188 
190 
191 BOOST_AUTO_TEST_SUITE_END()
192 
193 
#define CFinfo
these are always defined
Definition: Log.hpp:104
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
external boost library namespace
const RealRowVector & weights() const
Definition: Quadrature.hpp:71
virtual Real jacobian_determinant(const RealVector &mapped_coord, const RealMatrix &nodes) const =0
static Real jacobian_determinant(const MappedCoordsT &mapped_coord, const NodesT &nodes)
Definition: Hexa3D.cpp:169
~Fixture()
common tear-down for each test case
#define CFendl
Definition: Log.hpp:109
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Lagrange P2 line (1D) Element type This class provides the lagrangian shape function describing the r...
Definition: Line1D.hpp:36
Definition: Defs.hpp:17
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealMatrix
Dynamic sized matrix of Real scalars.
Definition: MatrixTypes.hpp:22
Eigen::Matrix< Real, nb_nodes, Hexa3D_traits::dimension > NodesT
Eigen::Matrix< Real, 1, Eigen::Dynamic > RealRowVector
Dynamic sized row vector.
Definition: MatrixTypes.hpp:31
Basic Classes for Mesh applications used by COOLFluiD.
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealVector
Dynamic sized column vector.
Definition: MatrixTypes.hpp:25
BOOST_AUTO_TEST_CASE(Line_static)
Top-level namespace for coolfluid.
Definition: Action.cpp:18
int m_argc
common values accessed by all tests goes here
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
double integrate(double(*f)(double), double a, double b)
possibly common functions used on the tests below
virtual RealRowVector value(const RealVector &local_coordinate) const =0
Compute the shape function values in the given local coordinate.
Uint nb_nodes() const
Definition: ElementType.hpp:73
#define P(a, b, c, d, k, s, t)
Fixture()
common setup for each test case
Definition: Defs.hpp:17
const RealMatrix & local_coordinates() const
Definition: Quadrature.hpp:68
Most basic kernel library.
Definition: Action.cpp:19
Uint dimension() const
Definition: ElementType.hpp:82
boost::proto::result_of::make_expr< boost::proto::tag::function, pow_fun< Exp >, Arg const & >::type const pow(Arg const &arg)
Definition: Functions.hpp:44
boost::proto::result_of::make_expr< boost::proto::tag::function, IntegralTag< boost::mpl::int_< Order > >, ExprT const & >::type const integral(ExprT const &expr)
virtual const ShapeFunction & shape_function() const =0
Send comments to:
COOLFluiD Web Admin