Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_TensorProductBasisUnitTest.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 
52 #include <iterator>
53 
54 namespace TensorProductBasisUnitTest {
55 
56  // Common setup for unit tests
57  template <typename OrdinalType, typename ValueType>
58  struct UnitTestSetup {
59  ValueType rtol, atol, sparse_tol;
60  OrdinalType p,d;
61 
63  rtol = 1e-12;
64  atol = 1e-12;
65  sparse_tol = 1e-12;
66  d = 3;
67  p = 4;
68  }
69 
70  };
71 
72  typedef int ordinal_type;
73  typedef double value_type;
75 
76  TEUCHOS_UNIT_TEST( Coefficients, Isotropic ) {
77  success = true;
78 
79  // Build tensor product basis of dimension d and order p
81  for (ordinal_type i=0; i<setup.d; i++)
84 
85  // Compute expected size
86  ordinal_type sz = 1;
87  for (ordinal_type i=0; i<setup.d; ++i)
88  sz *= setup.p+1;
89 
90  // Check sizes
91  TEUCHOS_TEST_EQUALITY(sz, basis->size(), out, success);
92 
93  std::ostream_iterator<ordinal_type> out_iterator(out, " ");
94  for (ordinal_type i=0; i<sz; i++) {
95  const Stokhos::MultiIndex<ordinal_type>& term = basis->term(i);
96 
97  // Verify terms match
98  out << "term " << term << " <= " << setup.p << " : ";
99  bool is_less = true;
100  for (ordinal_type j=0; j<setup.d; j++)
101  is_less = is_less && term[j] <= setup.p;
102  if (is_less)
103  out << "passed" << std::endl;
104  else {
105  out << "failed" << std::endl;
106  success = false;
107  }
108 
109  }
110 
111  }
112 
113  TEUCHOS_UNIT_TEST( Coeficients, Anisotropic ) {
114  success = true;
115 
116  // Build anisotropic tensor product basis of dimension d
118  for (ordinal_type i=0; i<setup.d; i++)
121 
122  // Compute expected size
123  ordinal_type sz = 1;
124  for (ordinal_type i=0; i<setup.d; ++i)
125  sz *= i+2;
126 
127  // Check sizes
128  TEUCHOS_TEST_EQUALITY(sz, basis->size(), out, success);
129 
130  std::ostream_iterator<ordinal_type> out_iterator(out, " ");
131  for (ordinal_type i=0; i<sz; i++) {
132  const Stokhos::MultiIndex<ordinal_type>& term = basis->term(i);
133 
134  // Verify terms match
135  out << "term " << term << " <= " << setup.p << " : ";
136  bool is_less = true;
137  for (ordinal_type j=0; j<setup.d; j++)
138  is_less = is_less && term[j] <= setup.p;
139  if (is_less)
140  out << "passed" << std::endl;
141  else {
142  out << "failed" << std::endl;
143  success = false;
144  }
145 
146  }
147 
148  }
149 
150  TEUCHOS_UNIT_TEST( Sparse3Tensor, Anisotropic_Full ) {
151  success = true;
152 
153  // Build anisotropic tensor product basis of dimension d
155  for (ordinal_type i=0; i<setup.d; i++)
159  basis->computeTripleProductTensor();
160 
161  success = Stokhos::testSparse3Tensor(*Cijk, *basis, setup.sparse_tol,
162  setup.rtol, setup.atol, out);
163  }
164 
165  TEUCHOS_UNIT_TEST( Sparse3Tensor, Anisotropic_Linear ) {
166  success = true;
167 
168  // Build anisotropic tensor product basis of dimension d
170  for (ordinal_type i=0; i<setup.d; i++)
174  basis->computeLinearTripleProductTensor();
175 
176  success = Stokhos::testSparse3Tensor(*Cijk, *basis, setup.sparse_tol,
177  setup.rtol, setup.atol, out, true);
178  }
179 
180 
181 }
182 
183 int main( int argc, char* argv[] ) {
184  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
186  return res;
187 }
bool testSparse3Tensor(const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk, const Stokhos::ProductBasis< ordinal_type, scalar_type > &basis, const scalar_type &sparse_tol, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out, bool linear=false)
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
UnitTestSetup< ordinal_type, value_type > setup
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials...
Legendre polynomial basis.
int main(int argc, char **argv)
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)