Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Stokhos_JacobiBasisUnitTest.cpp
Go to the documentation of this file.
1// $Id$
2// $Source$
3// @HEADER
4// ***********************************************************************
5//
6// Stokhos Package
7// Copyright (2009) Sandia Corporation
8//
9// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10// license for use of this work by or on behalf of the U.S. Government.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38//
39// Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
40//
41// ***********************************************************************
42// @HEADER
43
44#include "Teuchos_UnitTestHarness.hpp"
45#include "Teuchos_TestingHelpers.hpp"
46#include "Teuchos_UnitTestRepository.hpp"
47#include "Teuchos_GlobalMPISession.hpp"
48
49#include "Stokhos.hpp"
51#include <iomanip>
52#ifdef HAVE_STOKHOS_FORUQTK
53#include "Stokhos_gaussq.h"
54#endif
55
56using std::cout;
57using std::setw;
58using std::endl;
59
60
61
62namespace Stokhos {
63using namespace Teuchos;
64
66{
67public:
69 JacobiTester(int quadOrder)
70 : quad_()
71 {
72 /* We'll set up a Gauss-Legendre quadrature rule for doing the
73 * integrations. We don't want to use Gauss-Jacobi quadrature here,
74 * because errors in the Jacobi basis will cause errors in the
75 * G-J quadrature as well. */
76 Array< RCP<const OneDOrthogPolyBasis<int,double> > > qBases(1);
77 qBases[0] = rcp(new LegendreBasis<int,double>(quadOrder));
78
79 RCP<const CompletePolynomialBasis<int,double> > qBasis =
81
82 quad_ = rcp(new TensorProductQuadrature<int, double>(qBasis, quadOrder));
83 }
84
95 bool testInnerProduct(double alpha, double beta, int nMax) const
96 {
97 JacobiBasis<int, double> basis(nMax, alpha, beta, false);
98
99 Array<Array<double> > qp = quad_->getQuadPoints();
100 Array<double> qw = quad_->getQuadWeights();
101 bool pass = true;
102
103 for (int n=0; n<=nMax; n++)
104 {
105 double nFact = tgamma(n+1.0);
106 cout << "n=" << n << endl;
107 for (double x=-1.0; x<=1.0; x+=0.25)
108 {
109 cout << setw(20) << x << setw(20) << basis.evaluate(x, n)
110 << endl;
111 }
112 for (int m=0; m<=nMax; m++)
113 {
114 double sum = 0.0;
115 for (int q=0; q<qw.size(); q++)
116 {
117 double x = qp[q][0];
118 double w = qw[q] * pow(1-x,alpha)*pow(1+x,beta);
119 double Pn = basis.evaluate(x, n);
120 double Pm = basis.evaluate(x, m);
121 sum += 2.0*w*Pn*Pm;
122 }
123 double exact = 0.0;
124 if (n==m)
125 exact = pow(2.0, alpha+beta+1.0)/(2.0*n+alpha+beta+1.0)
126 * tgamma(n+alpha+1.0)*tgamma(n+beta+1.0)
127 /tgamma(n+alpha+beta+1.0)/nFact;
128 double err = fabs(exact - sum);
129 cout << setw(4) << n << setw(4) << m
130 << setw(20) << exact << setw(20) << sum << setw(20) << err
131 << endl;
132 /* Use a fairly loose tolerance because the Gauss-Legendre
133 * quadrature won't be exact when either alpha or beta is not
134 * an integer */
135 if (err > 1.0e-6)
136 {
137 pass = false;
138 cout << "***** FAIL ******" << endl;
139 }
140 }
141 }
142 return pass;
143 }
144private:
145 RCP<Quadrature<int, double> > quad_;
146};
147
148
149
150}
151
152int main( int argc, char* argv[] )
153{
154 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
155
156 Stokhos::JacobiTester tester(400);
157
158 bool allOK = true;
159 for (double alpha = 0.75; alpha <= 2.0; alpha += 0.25)
160 {
161 for (double beta = 0.75; beta <= alpha; beta += 0.25)
162 {
163 cout << "alpha=" << setw(20) << alpha
164 << " beta=" << setw(20) << beta << endl;
165 bool ok = tester.testInnerProduct(alpha, beta, 8);
166 allOK = allOK && ok;
167 }
168 }
169
170 if (allOK==true)
171 {
172 cout << "Jacobi tests PASSED!" << endl;
173 cout << "End Result: TEST PASSED" << endl;
174 return 0;
175 }
176 else
177 {
178 cout << "Jacobi tests FAILED ***" << endl;
179 return 1;
180 }
181}
int main(int argc, char *argv[])
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Jacobi polynomial basis.
RCP< Quadrature< int, double > > quad_
bool testInnerProduct(double alpha, double beta, int nMax) const
Legendre polynomial basis.
virtual value_type evaluate(const value_type &point, ordinal_type order) const
Evaluate basis polynomial given by order order at given point point.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
Top-level namespace for Stokhos classes and functions.