Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_LegendreBasisUnitTest.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 
48 
49 #include "Stokhos.hpp"
51 #ifdef HAVE_STOKHOS_FORUQTK
52 #include "Stokhos_gaussq.h"
53 #endif
54 
55 namespace LegendreBasisUnitTest {
56 
57  // Common setup for unit tests
58  template <typename OrdinalType, typename ValueType>
59  struct UnitTestSetup {
60  ValueType rtol, atol;
61  OrdinalType p;
63 
64  UnitTestSetup() : rtol(1e-12), atol(1e-12), p(10), basis(p) {}
65 
66  };
67 
69 
70  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Order ) {
71  int order = setup.basis.order();
72  TEUCHOS_TEST_EQUALITY(order, setup.p, out, success);
73  }
74 
75  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Size ) {
76  int size = setup.basis.size();
77  TEUCHOS_TEST_EQUALITY(size, setup.p+1, out, success);
78  }
79 
80  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Norm ) {
81  const Teuchos::Array<double>& n1 = setup.basis.norm_squared();
83  for (int i=0; i<=setup.p; i++)
84  n2[i] = 1.0/(2.0*i+1.0);
85  success = Stokhos::compareArrays(n1, "n1", n2, "n2", setup.rtol, setup.atol,
86  out);
87  }
88 
89  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Norm2 ) {
92  for (int i=0; i<=setup.p; i++) {
93  n1[i] = setup.basis.norm_squared(i);
94  n2[i] = 1.0/(2.0*i+1.0);
95  }
96  success = Stokhos::compareArrays(n1, "n1", n2, "n2", setup.rtol, setup.atol,
97  out);
98  }
99 
100  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, QuadNorm ) {
101  const Teuchos::Array<double>& n1 = setup.basis.norm_squared();
105  setup.basis.getQuadPoints(2*setup.p, x, w, v);
106  int nqp = w.size();
107  for (int i=0; i<=setup.p; i++) {
108  n2[i] = 0;
109  for (int j=0; j<nqp; j++)
110  n2[i] += w[j]*v[j][i]*v[j][i];
111  }
112  success = Stokhos::compareArrays(n1, "n1", n2, "n2", setup.rtol, setup.atol,
113  out);
114  }
115 
116  // Tests basis is orthogonal
117  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, QuadOrthog ) {
118  const Teuchos::Array<double>& norms = setup.basis.norm_squared();
121  setup.basis.getQuadPoints(2*setup.p, x, w, v);
122  int nqp = w.size();
124  for (int i=0; i<=setup.p; i++) {
125  for (int j=0; j<=setup.p; j++) {
126  for (int k=0; k<nqp; k++)
127  mat(i,j) += w[k]*v[k][i]*v[k][j];
128  mat(i,j) /= std::sqrt(norms[i]*norms[j]);
129  }
130  mat(i,i) -= 1.0;
131  }
132  success = mat.normInf() < setup.atol;
133  if (!success) {
134  out << "\n Error, mat.normInf() < atol = " << mat.normInf()
135  << " < " << setup.atol << ": failed!\n";
136  out << "mat = " << mat << std::endl;
137  }
138  }
139 
140  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, TripleProduct ) {
142  setup.basis.computeTripleProductTensor();
143 
146  setup.basis.getQuadPoints(3*setup.p, x, w, v);
147 
148  success = true;
149  for (int i=0; i<=setup.p; i++) {
150  for (int j=0; j<=setup.p; j++) {
151  for (int k=0; k<=setup.p; k++) {
152  double c = 0.0;
153  int nqp = w.size();
154  for (int qp=0; qp<nqp; qp++)
155  c += w[qp]*v[qp][i]*v[qp][j]*v[qp][k];
156  std::stringstream ss;
157  ss << "Cijk(" << i << "," << j << "," << k << ")";
158  success = success &&
159  Stokhos::compareValues((*Cijk)(i,j,k), ss.str(), c, "c",
160  setup.rtol, setup.atol, out);
161  }
162  }
163  }
164  }
165 
166  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, DerivDoubleProduct ) {
168  setup.basis.computeDerivDoubleProductTensor();
169 
172  setup.basis.getQuadPoints(3*setup.p, x, w, v);
173  int nqp = w.size();
174  val.resize(nqp);
175  deriv.resize(nqp);
176  for (int i=0; i<nqp; i++) {
177  val[i].resize(setup.p+1);
178  deriv[i].resize(setup.p+1);
179  setup.basis.evaluateBasesAndDerivatives(x[i], val[i], deriv[i]);
180  }
181 
182  success = true;
183  for (int i=0; i<=setup.p; i++) {
184  for (int j=0; j<=setup.p; j++) {
185  double b = 0.0;
186  for (int qp=0; qp<nqp; qp++)
187  b += w[qp]*deriv[qp][i]*val[qp][j];
188  std::stringstream ss;
189  ss << "Bij(" << i << "," << j << ")";
190  success = success &&
191  Stokhos::compareValues((*Bij)(i,j), ss.str(), b, "b",
192  setup.rtol, setup.atol, out);
193  }
194  }
195  }
196 
197  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, DerivDoubleProduct2 ) {
199  setup.basis.computeDerivDoubleProductTensor();
200  const Teuchos::Array<double>& n = setup.basis.norm_squared();
201 
202  Teuchos::Array< Teuchos::Array<double> > deriv_coeffs(setup.p+1);
203  deriv_coeffs[0].resize(setup.p+1,0.0);
204  if (setup.p >= 1) {
205  deriv_coeffs[1].resize(setup.p+1,0.0);
206  deriv_coeffs[1][0] = 1.0;
207  }
208  if (setup.p >= 2) {
209  deriv_coeffs[2].resize(setup.p+1,0.0);
210  deriv_coeffs[2][1] = 3.0;
211  }
212  for (int k=3; k<=setup.p; k++) {
213  deriv_coeffs[k].resize(setup.p+1,0.0);
214  deriv_coeffs[k][0] = 1.0/3.0*deriv_coeffs[k-1][1];
215  for (int i=1; i<=k-3; i++)
216  deriv_coeffs[k][i] = i/(2.0*i - 1.0)*deriv_coeffs[k-1][i-1] +
217  (i+1.0)/(2.0*i + 3.0)*deriv_coeffs[k-1][i+1];
218  deriv_coeffs[k][k-2] = (k-2.0)/(2.0*k-5.0)*deriv_coeffs[k-1][k-3];
219  deriv_coeffs[k][k-1] = (k-1.0)/(2.0*k-3.0)*deriv_coeffs[k-1][k-2] + k;
220  }
221 
222  success = true;
223  for (int i=0; i<=setup.p; i++) {
224  for (int j=0; j<=setup.p; j++) {
225  double b = deriv_coeffs[i][j]*n[j];
226  std::stringstream ss;
227  ss << "Bij(" << i << "," << j << ")";
228  success = success &&
229  Stokhos::compareValues((*Bij)(i,j), ss.str(), b, "b",
230  setup.rtol, setup.atol, out);
231  }
232  }
233  }
234 
235  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, EvaluateBases ) {
236  double x = 0.1234;
237  Teuchos::Array<double> v1(setup.p+1), v2(setup.p+1);
238  setup.basis.evaluateBases(x, v1);
239 
240  // evaluate bases using formula
241  // P_0(x) = 1
242  // P_1(x) = x
243  // P_i(x) = (2*i-1)/i*x*P_{i-1}(x) - (i-1)/i*P_{i-2}(x), i=2,3,...
244  v2[0] = 1.0;
245  if (setup.p >= 1)
246  v2[1] = x;
247  for (int i=2; i<=setup.p; i++)
248  v2[i] = (2.0*i-1.0)/i*x*v2[i-1] - (i-1.0)/i*v2[i-2];
249  success = Stokhos::compareArrays(v1, "v1", v2, "v2", setup.rtol, setup.atol,
250  out);
251  }
252 
253  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, EvaluateBasesAndDerivatives ) {
254  double x = 0.1234;
255  Teuchos::Array<double> val1(setup.p+1), deriv1(setup.p+1),
256  val2(setup.p+1), deriv2(setup.p+1);
257  setup.basis.evaluateBasesAndDerivatives(x, val1, deriv1);
258 
259  // evaluate bases and derivatives using formula:
260  // P_0(x) = 1
261  // P_1(x) = x
262  // P_i(x) = (2*i-1)/i*x*P_{i-1}(x) - (i-1)/i*P_{i-2}(x), i=2,3,...
263  // P_0'(x) = 0
264  // P_1'(x) = 1
265  // P_i'(x) = i*P_{i-1}(x) + x*P_{i-1}'(x), i=2,3,...
266  val2[0] = 1.0;
267  if (setup.p >= 1)
268  val2[1] = x;
269  for (int i=2; i<=setup.p; i++)
270  val2[i] = (2.0*i-1.0)/i*x*val2[i-1] - (i-1.0)/i*val2[i-2];
271 
272  deriv2[0] = 0.0;
273  if (setup.p >= 1)
274  deriv2[1] = 1.0;
275  for (int i=2; i<=setup.p; i++)
276  deriv2[i] = i*val2[i-1] + x*deriv2[i-1];
277  success = Stokhos::compareArrays(val1, "val1", val2, "val2",
278  setup.rtol, setup.atol, out);
279  success = success &&
280  Stokhos::compareArrays(deriv1, "deriv1", deriv2, "deriv2",
281  setup.rtol, setup.atol, out);
282 
283 
284  }
285 
286  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Evaluate ) {
287  double x = 0.1234;
288  Teuchos::Array<double> v1(setup.p+1), v2(setup.p+1);
289  for (int i=0; i<=setup.p; i++)
290  v1[i] = setup.basis.evaluate(x, i);
291 
292  // evaluate bases using formula
293  // P_0(x) = 1
294  // P_1(x) = x
295  // P_i(x) = (2*i-1)/i*x*P_{i-1}(x) - (i-1)/i*P_{i-2}(x), i=2,3,...
296  v2[0] = 1.0;
297  if (setup.p >= 1)
298  v2[1] = x;
299  for (int i=2; i<=setup.p; i++)
300  v2[i] = (2.0*i-1.0)/i*x*v2[i-1] - (i-1.0)/i*v2[i-2];
301  success = Stokhos::compareArrays(v1, "v1", v2, "v2", setup.rtol, setup.atol,
302  out);
303  }
304 
305  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, Recurrence ) {
306  Teuchos::Array<double> a1(setup.p+1), b1(setup.p+1), c1(setup.p+1),
307  d1(setup.p+1);
308  Teuchos::Array<double> a2(setup.p+1), b2(setup.p+1), c2(setup.p+1),
309  d2(setup.p+1);
310  setup.basis.getRecurrenceCoefficients(a1, b1, c1, d1);
311 
312  // compute coefficients using formula
313  a2[0] = 0.0; b2[0] = 1.0; c2[0] = 1.0; d2[0] = 1.0;
314  for (int i=1; i<=setup.p; i++) {
315  a2[i] = 0.0;
316  b2[i] = i/(i+1.0);
317  c2[i] = (2.0*i+1.0)/(i+1);
318  d2[i] = 1.0;
319  }
320  success = true;
321  success = success &&
322  Stokhos::compareArrays(a1, "a1", a2, "a2", setup.rtol, setup.atol, out);
323  success = success &&
324  Stokhos::compareArrays(b1, "b1", b2, "b2", setup.rtol, setup.atol, out);
325  success = success &&
326  Stokhos::compareArrays(c1, "c1", c2, "c2", setup.rtol, setup.atol, out);
327  success = success &&
328  Stokhos::compareArrays(d1, "d1", d2, "d2", setup.rtol, setup.atol, out);
329  }
330 
331  // Tests alpha coefficients satisfy
332  // alpha_k = delta_k * (t*psi_k,psi_k)/(psi_k,psi_k)
333  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, RecurrenceAlpha ) {
334  Teuchos::Array<double> a1(setup.p+1), b1(setup.p+1), c1(setup.p+1),
335  d1(setup.p+1);
336  setup.basis.getRecurrenceCoefficients(a1, b1, c1, d1);
337 
339  const Teuchos::Array<double>& n1 = setup.basis.norm_squared();
342  setup.basis.getQuadPoints(2*setup.p, x, w, v);
343  int nqp = w.size();
344  for (int i=0; i<=setup.p; i++) {
345  a2[i] = 0;
346  for (int j=0; j<nqp; j++)
347  a2[i] += w[j]*x[j]*v[j][i]*v[j][i];
348  a2[i] = a2[i]*c1[i]/n1[i];
349  }
350  success = Stokhos::compareArrays(a1, "a1", a2, "a2", setup.rtol, setup.atol,
351  out);
352  }
353 
354  // Tests beta coefficients satisfy
355  // beta_k =
356  // gamma_k * delta_k/delta_{k-1} * (psi_k,psi_k)/(psi_{k-1},psi_{k-1})
357  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, RecurrenceBeta ) {
358  Teuchos::Array<double> a1(setup.p+1), b1(setup.p+1), c1(setup.p+1),
359  d1(setup.p+1);
360  setup.basis.getRecurrenceCoefficients(a1, b1, c1, d1);
361 
363  const Teuchos::Array<double>& n1 = setup.basis.norm_squared();
364  b2[0] = b1[0];
365  for (int i=1; i<=setup.p; i++) {
366  b2[i] = d1[i]*(c1[i]/c1[i-1])*(n1[i]/n1[i-1]);
367  }
368  success = Stokhos::compareArrays(b1, "b1", b2, "b2", setup.rtol, setup.atol,
369  out);
370  }
371 
372 #ifdef HAVE_STOKHOS_DAKOTA
373  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, QuadPointsDakota ) {
374  int n = static_cast<int>(std::ceil((setup.p+1)/2.0));
375  Teuchos::Array<double> x1, w1;
377  setup.basis.getQuadPoints(setup.p, x1, w1, v1);
378 
379  Teuchos::Array<double> x2(n), w2(n);
381  webbur::legendre_compute(n, &x2[0], &w2[0]);
382 
383  for (int i=0; i<n; i++) {
384  w2[i] *= 0.5; // measure = 1/2
385  v2[i].resize(setup.p+1);
386  setup.basis.evaluateBases(x2[i], v2[i]);
387  }
388  success = true;
389  success = success &&
390  Stokhos::compareArrays(x1, "x1", x2, "x2", setup.rtol, setup.atol, out);
391  success = success &&
392  Stokhos::compareArrays(w1, "w1", w2, "w2", setup.rtol, setup.atol, out);
393  for (int i=0; i<n; i++) {
394  std::stringstream ss1, ss2;
395  ss1 << "v1[" << i << "]";
396  ss2 << "v2[" << i << "]";
397  success = success &&
398  Stokhos::compareArrays(v1[i], ss1.str(), v2[i], ss2.str(),
399  setup.rtol, setup.atol, out);
400  }
401  }
402 #endif
403 
404 #ifdef HAVE_STOKHOS_FORUQTK
405  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, QuadPointsForUQTK ) {
406  int n = static_cast<int>(std::ceil((setup.p+1)/2.0));
407  Teuchos::Array<double> x1, w1;
409  setup.basis.getQuadPoints(setup.p, x1, w1, v1);
410 
411  Teuchos::Array<double> x2(n), w2(n);
413  int kind = 1;
414  int kpts = 0;
415  double endpts[2] = {0.0, 0.0};
417  double alpha = 0.0;
418  double beta = 0.0;
419  GAUSSQ_F77(&kind, &n, &alpha, &beta, &kpts, endpts, &b[0], &x2[0], &w2[0]);
420 
421  for (int i=0; i<n; i++) {
422  w2[i] *= 0.5; // measure = 1/2
423  v2[i].resize(setup.p+1);
424  setup.basis.evaluateBases(x2[i], v2[i]);
425  }
426  success = true;
427  success = success &&
428  Stokhos::compareArrays(x1, "x1", x2, "x2", setup.rtol, setup.atol, out);
429  success = success &&
430  Stokhos::compareArrays(w1, "w1", w2, "w2", setup.rtol, setup.atol, out);
431  for (int i=0; i<n; i++) {
432  std::stringstream ss1, ss2;
433  ss1 << "v1[" << i << "]";
434  ss2 << "v2[" << i << "]";
435  success = success &&
436  Stokhos::compareArrays(v1[i], ss1.str(), v2[i], ss2.str(),
437  setup.rtol, setup.atol, out);
438  }
439  }
440 #endif
441 
442 }
443 
444 int main( int argc, char* argv[] ) {
445  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
447 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
bool compareArrays(const Array1 &a1, const std::string &a1_name, const Array2 &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
bool compareValues(const ValueType &a1, const std::string &a1_name, const ValueType &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
static int runUnitTestsFromMain(int argc, char *argv[])
KOKKOS_INLINE_FUNCTION PCE< Storage > ceil(const PCE< Storage > &a)
Stokhos::LegendreBasis< OrdinalType, ValueType > basis
void resize(size_type new_size, const value_type &x=value_type())
TEUCHOS_UNIT_TEST(Stokhos_LegendreBasis, Order)
UnitTestSetup< int, double > setup
int main(int argc, char **argv)
ScalarTraits< ScalarType >::magnitudeType normInf() const
expr val()
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
size_type size() const
int n
void GAUSSQ_F77(int *kind, int *n, double *alpha, double *beta, int *kpts, double *endpts, double *b, double *x, double *w)