Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_JacobiBasisUnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Stokhos.hpp"
17 #include <iomanip>
18 #ifdef HAVE_STOKHOS_FORUQTK
19 #include "Stokhos_gaussq.h"
20 #endif
21 
22 using std::cout;
23 using std::setw;
24 using std::endl;
25 
26 
27 
28 namespace Stokhos {
29 using namespace Teuchos;
30 
32 {
33 public:
35  JacobiTester(int quadOrder)
36  : quad_()
37  {
38  /* We'll set up a Gauss-Legendre quadrature rule for doing the
39  * integrations. We don't want to use Gauss-Jacobi quadrature here,
40  * because errors in the Jacobi basis will cause errors in the
41  * G-J quadrature as well. */
43  qBases[0] = rcp(new LegendreBasis<int,double>(quadOrder));
44 
47 
48  quad_ = rcp(new TensorProductQuadrature<int, double>(qBasis, quadOrder));
49  }
50 
61  bool testInnerProduct(double alpha, double beta, int nMax) const
62  {
63  JacobiBasis<int, double> basis(nMax, alpha, beta, false);
64 
65  Array<Array<double> > qp = quad_->getQuadPoints();
66  Array<double> qw = quad_->getQuadWeights();
67  bool pass = true;
68 
69  for (int n=0; n<=nMax; n++)
70  {
71  double nFact = tgamma(n+1.0);
72  cout << "n=" << n << endl;
73  for (double x=-1.0; x<=1.0; x+=0.25)
74  {
75  cout << setw(20) << x << setw(20) << basis.evaluate(x, n)
76  << endl;
77  }
78  for (int m=0; m<=nMax; m++)
79  {
80  double sum = 0.0;
81  for (int q=0; q<qw.size(); q++)
82  {
83  double x = qp[q][0];
84  double w = qw[q] * pow(1-x,alpha)*pow(1+x,beta);
85  double Pn = basis.evaluate(x, n);
86  double Pm = basis.evaluate(x, m);
87  sum += 2.0*w*Pn*Pm;
88  }
89  double exact = 0.0;
90  if (n==m)
91  exact = pow(2.0, alpha+beta+1.0)/(2.0*n+alpha+beta+1.0)
92  * tgamma(n+alpha+1.0)*tgamma(n+beta+1.0)
93  /tgamma(n+alpha+beta+1.0)/nFact;
94  double err = fabs(exact - sum);
95  cout << setw(4) << n << setw(4) << m
96  << setw(20) << exact << setw(20) << sum << setw(20) << err
97  << endl;
98  /* Use a fairly loose tolerance because the Gauss-Legendre
99  * quadrature won't be exact when either alpha or beta is not
100  * an integer */
101  if (err > 1.0e-6)
102  {
103  pass = false;
104  cout << "***** FAIL ******" << endl;
105  }
106  }
107  }
108  return pass;
109  }
110 private:
112 };
113 
114 
115 
116 }
117 
118 int main( int argc, char* argv[] )
119 {
120  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
121 
122  Stokhos::JacobiTester tester(400);
123 
124  bool allOK = true;
125  for (double alpha = 0.75; alpha <= 2.0; alpha += 0.25)
126  {
127  for (double beta = 0.75; beta <= alpha; beta += 0.25)
128  {
129  cout << "alpha=" << setw(20) << alpha
130  << " beta=" << setw(20) << beta << endl;
131  bool ok = tester.testInnerProduct(alpha, beta, 8);
132  allOK = allOK && ok;
133  }
134  }
135 
136  if (allOK==true)
137  {
138  cout << "Jacobi tests PASSED!" << endl;
139  cout << "End Result: TEST PASSED" << endl;
140  return 0;
141  }
142  else
143  {
144  cout << "Jacobi tests FAILED ***" << endl;
145  return 1;
146  }
147 }
KOKKOS_INLINE_FUNCTION PCE< Storage > fabs(const PCE< Storage > &a)
RCP< Quadrature< int, double > > quad_
virtual value_type evaluate(const value_type &point, ordinal_type order) const
Evaluate basis polynomial given by order order at given point point.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Jacobi polynomial basis.
Legendre polynomial basis.
int main(int argc, char **argv)
bool testInnerProduct(double alpha, double beta, int nMax) const
size_type size() const
int n
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type sum(const Kokkos::View< RD, RP...> &r, const Kokkos::View< XD, XP...> &x)