Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SmolyakBasisUnitTest.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 #include "Teuchos_ArrayView.hpp"
49 
50 #include "Stokhos.hpp"
52 
53 namespace SmolyakBasisUtilsUnitTest {
54 
55  // Common setup for unit tests
56  template <typename ordinal_type, typename value_type>
57  struct UnitTestSetup {
60 
62  rtol(1e-12),
63  atol(1e-12),
64  sparse_tol(1e-12),
65  p(5),
66  d(3) {}
67 
68  };
69 
70  typedef int ordinal_type;
71  typedef double value_type;
74 
75  template <typename ordinal_type, typename value_type>
79  Teuchos::FancyOStream& out) {
80  bool success = true;
81 
82  // Check sizes
83  TEUCHOS_TEST_EQUALITY(basis1.size(), basis2.size(), out, success);
84  if (!success)
85  return false;
86 
87  ordinal_type coeff_sz = basis1.size();
88 
89  // Check coefficients
90  for (ordinal_type i=0; i<coeff_sz; ++i) {
91  out << "coeff " << basis1.term(i) << " == " << basis2.term(i) << " : ";
92  bool is_equal = basis1.term(i) == basis2.term(i);
93  if (is_equal)
94  out << "passed" << std::endl;
95  else {
96  out << "failed" << std::endl;
97  success = false;
98  }
99  }
100 
101  return success;
102  }
103 
104  TEUCHOS_UNIT_TEST( Coefficients, IsotropicLinear ) {
105  // Build isotropic Smolyak basis of dimension d and order p with
106  // linear growth -- note order of basis is determined by index set and
107  // growth rule, not coordinate bases
109  for (ordinal_type i=0; i<setup.d; i++)
113  bases, coeff_index_set);
114 
115  // Build isotropic total order basis of dimension d and order p
116  Stokhos::TotalOrderBasis<ordinal_type,value_type> total_order_basis(bases);
117 
118  // Two basis should be identical
119  success = testCoefficients(smolyak_basis, total_order_basis, out);
120  }
121 
122  TEUCHOS_UNIT_TEST( Coefficients, AnisotropicLinear ) {
123  // Build anisotropic Smolyak basis of dimension d with
124  // linear growth -- note order of basis is determined by index set and
125  // growth rule, not coordinate bases
128  for (ordinal_type i=0; i<setup.d; i++) {
130  upper[i] = i+1;
131  }
133  setup.d, upper); // largest order is setup.d-1+1
135  bases, coeff_index_set);
136 
137  // Build isotropic total order basis of dimension d and order p
138  Stokhos::TotalOrderBasis<ordinal_type,value_type> total_order_basis(bases);
139 
140  // Two basis should be identical
141  success = testCoefficients(smolyak_basis, total_order_basis, out);
142  }
143 
144  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicLinear ) {
145  success = true;
146 
147  // Build anisotropic Smolyak basis of dimension d with
148  // linear growth -- note order of basis is determined by index set and
149  // growth rule, not coordinate bases
152  for (ordinal_type i=0; i<setup.d; i++) {
154  upper[i] = i+1;
155  }
157  setup.d, upper); // largest order is setup.d-1+1
159  bases, coeff_index_set);
161  smolyak_basis.computeTripleProductTensor();
162 
163  // Build isotropic total order basis of dimension d and order p
164  Stokhos::TotalOrderBasis<ordinal_type,value_type> total_order_basis(bases);
166  total_order_basis.computeTripleProductTensor();
167 
168  // Compare Cijk tensors
169  success = Stokhos::compareSparse3Tensor(*Cijk1, "Smolyak Cijk",
170  *Cijk2, "Total Order Cijk",
171  setup.rtol, setup.atol, out);
172 
174  smolyak_basis.computeLinearTripleProductTensor();
176  total_order_basis.computeLinearTripleProductTensor();
177 
178  // Compare Cijk tensors
179  success = success &&
180  Stokhos::compareSparse3Tensor(*Cijk1_lin, "Smolyak linear Cijk",
181  *Cijk2_lin, "Total Order linear Cijk",
182  setup.rtol, setup.atol, out);
183 
184  }
185 
186  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicLinear2 ) {
187  success = true;
188 
189  // Build anisotropic Smolyak basis of dimension d with
190  // linear growth -- note order of basis is determined by index set and
191  // growth rule, not coordinate bases
194  for (ordinal_type i=0; i<setup.d; i++) {
196  upper[i] = i+1;
197  }
199  setup.d, upper); // largest order is setup.d-1+1
201  bases, coeff_index_set, setup.sparse_tol);
203  smolyak_basis.computeTripleProductTensor();
204 
205  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
207  setup.atol, out);
208 
210  smolyak_basis.computeLinearTripleProductTensor();
211 
212  success = success &&
213  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
215  setup.atol, out, true);
216 
217  }
218 
219  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicModerateLinear ) {
220  success = true;
221 
222  // Build anisotropic Smolyak basis of dimension d with
223  // moderate linear growth -- note order of basis is determined by index
224  // set and growth rule, not coordinate bases
227  for (ordinal_type i=0; i<setup.d; i++) {
229  upper[i] = i+1;
230  }
232  setup.d, upper); // largest order is setup.d-1+1
234  bases, coeff_index_set, setup.sparse_tol);
236  smolyak_basis.computeTripleProductTensor();
237 
238  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
240  setup.atol, out);
241 
243  smolyak_basis.computeLinearTripleProductTensor();
244 
245  success = success &&
246  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
248  setup.atol, out, true);
249 
250  }
251 
252 #ifdef HAVE_STOKHOS_DAKOTA
253 
254  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicClenshawCurtis ) {
255  success = true;
256 
257  // Build anisotropic Smolyak basis of dimension d with
258  // exponential growth -- note order of basis is determined by index
259  // set and growth rule, not coordinate bases
262  for (ordinal_type i=0; i<setup.d; i++) {
264  upper[i] = i+1;
265  }
267  setup.d, upper); // largest order is setup.d-1+1
269  bases, coeff_index_set, setup.sparse_tol);
271  smolyak_basis.computeTripleProductTensor();
272 
273  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
275  setup.atol, out);
276 
278  smolyak_basis.computeLinearTripleProductTensor();
279 
280  success = success &&
281  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
283  setup.atol, out, true);
284 
285  }
286 
287  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicGaussPatterson ) {
288  success = true;
289 
290  // Build anisotropic Smolyak basis of dimension d with
291  // exponential growth -- note order of basis is determined by index
292  // set and growth rule, not coordinate bases
295  for (ordinal_type i=0; i<setup.d; i++) {
297  upper[i] = i+1;
298  }
300  setup.d, upper); // largest order is setup.d-1+1
302  bases, coeff_index_set, setup.sparse_tol);
304  smolyak_basis.computeTripleProductTensor();
305 
306  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
308  setup.atol, out);
309 
311  smolyak_basis.computeLinearTripleProductTensor();
312 
313  success = success &&
314  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
316  setup.atol, out, true);
317 
318  }
319 
320 #endif
321 
322 }
323 
324 int main( int argc, char* argv[] ) {
325  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
327 }
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
bool testCoefficients(const Stokhos::ProductBasis< ordinal_type, value_type > &basis1, const Stokhos::ProductBasis< ordinal_type, value_type > &basis2, Teuchos::FancyOStream &out)
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
TEUCHOS_UNIT_TEST(Coefficients, IsotropicLinear)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
bool compareSparse3Tensor(const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk1, const std::string &cijk1_name, const Stokhos::Sparse3Tensor< ordinal_type, scalar_type > &Cijk2, const std::string &cijk2_name, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
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)
Legendre polynomial basis using Gauss-Patterson quadrature points.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
static int runUnitTestsFromMain(int argc, char *argv[])
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const =0
Get orders of each coordinate polynomial given an index i.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
An anisotropic total order index set.
Multivariate orthogonal polynomial basis generated from a Smolyak sparse grid.
Legendre polynomial basis.
int main(int argc, char **argv)
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
An isotropic total order index set.
UnitTestSetup< ordinal_type, value_type > setup_type
Legendre polynomial basis using Clenshaw-Curtis quadrature points.
virtual ordinal_type size() const =0
Return total size of basis.