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 // @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 #include "Teuchos_ArrayView.hpp"
15 
16 #include "Stokhos.hpp"
18 
19 namespace SmolyakBasisUtilsUnitTest {
20 
21  // Common setup for unit tests
22  template <typename ordinal_type, typename value_type>
23  struct UnitTestSetup {
26 
28  rtol(1e-12),
29  atol(1e-12),
30  sparse_tol(1e-12),
31  p(5),
32  d(3) {}
33 
34  };
35 
36  typedef int ordinal_type;
37  typedef double value_type;
40 
41  template <typename ordinal_type, typename value_type>
45  Teuchos::FancyOStream& out) {
46  bool success = true;
47 
48  // Check sizes
49  TEUCHOS_TEST_EQUALITY(basis1.size(), basis2.size(), out, success);
50  if (!success)
51  return false;
52 
53  ordinal_type coeff_sz = basis1.size();
54 
55  // Check coefficients
56  for (ordinal_type i=0; i<coeff_sz; ++i) {
57  out << "coeff " << basis1.term(i) << " == " << basis2.term(i) << " : ";
58  bool is_equal = basis1.term(i) == basis2.term(i);
59  if (is_equal)
60  out << "passed" << std::endl;
61  else {
62  out << "failed" << std::endl;
63  success = false;
64  }
65  }
66 
67  return success;
68  }
69 
70  TEUCHOS_UNIT_TEST( Coefficients, IsotropicLinear ) {
71  // Build isotropic Smolyak basis of dimension d and order p with
72  // linear growth -- note order of basis is determined by index set and
73  // growth rule, not coordinate bases
75  for (ordinal_type i=0; i<setup.d; i++)
79  bases, coeff_index_set);
80 
81  // Build isotropic total order basis of dimension d and order p
83 
84  // Two basis should be identical
85  success = testCoefficients(smolyak_basis, total_order_basis, out);
86  }
87 
88  TEUCHOS_UNIT_TEST( Coefficients, AnisotropicLinear ) {
89  // Build anisotropic Smolyak basis of dimension d with
90  // linear growth -- note order of basis is determined by index set and
91  // growth rule, not coordinate bases
94  for (ordinal_type i=0; i<setup.d; i++) {
96  upper[i] = i+1;
97  }
99  setup.d, upper); // largest order is setup.d-1+1
101  bases, coeff_index_set);
102 
103  // Build isotropic total order basis of dimension d and order p
104  Stokhos::TotalOrderBasis<ordinal_type,value_type> total_order_basis(bases);
105 
106  // Two basis should be identical
107  success = testCoefficients(smolyak_basis, total_order_basis, out);
108  }
109 
110  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicLinear ) {
111  success = true;
112 
113  // Build anisotropic Smolyak basis of dimension d with
114  // linear growth -- note order of basis is determined by index set and
115  // growth rule, not coordinate bases
118  for (ordinal_type i=0; i<setup.d; i++) {
120  upper[i] = i+1;
121  }
123  setup.d, upper); // largest order is setup.d-1+1
125  bases, coeff_index_set);
127  smolyak_basis.computeTripleProductTensor();
128 
129  // Build isotropic total order basis of dimension d and order p
130  Stokhos::TotalOrderBasis<ordinal_type,value_type> total_order_basis(bases);
132  total_order_basis.computeTripleProductTensor();
133 
134  // Compare Cijk tensors
135  success = Stokhos::compareSparse3Tensor(*Cijk1, "Smolyak Cijk",
136  *Cijk2, "Total Order Cijk",
137  setup.rtol, setup.atol, out);
138 
140  smolyak_basis.computeLinearTripleProductTensor();
142  total_order_basis.computeLinearTripleProductTensor();
143 
144  // Compare Cijk tensors
145  success = success &&
146  Stokhos::compareSparse3Tensor(*Cijk1_lin, "Smolyak linear Cijk",
147  *Cijk2_lin, "Total Order linear Cijk",
148  setup.rtol, setup.atol, out);
149 
150  }
151 
152  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicLinear2 ) {
153  success = true;
154 
155  // Build anisotropic Smolyak basis of dimension d with
156  // linear growth -- note order of basis is determined by index set and
157  // growth rule, not coordinate bases
160  for (ordinal_type i=0; i<setup.d; i++) {
162  upper[i] = i+1;
163  }
165  setup.d, upper); // largest order is setup.d-1+1
167  bases, coeff_index_set, setup.sparse_tol);
169  smolyak_basis.computeTripleProductTensor();
170 
171  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
173  setup.atol, out);
174 
176  smolyak_basis.computeLinearTripleProductTensor();
177 
178  success = success &&
179  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
181  setup.atol, out, true);
182 
183  }
184 
185  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicModerateLinear ) {
186  success = true;
187 
188  // Build anisotropic Smolyak basis of dimension d with
189  // moderate linear growth -- note order of basis is determined by index
190  // set and growth rule, not coordinate bases
193  for (ordinal_type i=0; i<setup.d; i++) {
195  upper[i] = i+1;
196  }
198  setup.d, upper); // largest order is setup.d-1+1
200  bases, coeff_index_set, setup.sparse_tol);
202  smolyak_basis.computeTripleProductTensor();
203 
204  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
206  setup.atol, out);
207 
209  smolyak_basis.computeLinearTripleProductTensor();
210 
211  success = success &&
212  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
214  setup.atol, out, true);
215 
216  }
217 
218 #ifdef HAVE_STOKHOS_DAKOTA
219 
220  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicClenshawCurtis ) {
221  success = true;
222 
223  // Build anisotropic Smolyak basis of dimension d with
224  // exponential growth -- note order of basis is determined by index
225  // set and growth rule, not coordinate bases
228  for (ordinal_type i=0; i<setup.d; i++) {
230  upper[i] = i+1;
231  }
233  setup.d, upper); // largest order is setup.d-1+1
235  bases, coeff_index_set, setup.sparse_tol);
237  smolyak_basis.computeTripleProductTensor();
238 
239  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
241  setup.atol, out);
242 
244  smolyak_basis.computeLinearTripleProductTensor();
245 
246  success = success &&
247  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
249  setup.atol, out, true);
250 
251  }
252 
253  TEUCHOS_UNIT_TEST( Sparse3Tensor, AnisotropicGaussPatterson ) {
254  success = true;
255 
256  // Build anisotropic Smolyak basis of dimension d with
257  // exponential growth -- note order of basis is determined by index
258  // set and growth rule, not coordinate bases
261  for (ordinal_type i=0; i<setup.d; i++) {
263  upper[i] = i+1;
264  }
266  setup.d, upper); // largest order is setup.d-1+1
268  bases, coeff_index_set, setup.sparse_tol);
270  smolyak_basis.computeTripleProductTensor();
271 
272  success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
274  setup.atol, out);
275 
277  smolyak_basis.computeLinearTripleProductTensor();
278 
279  success = success &&
280  Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
282  setup.atol, out, true);
283 
284  }
285 
286 #endif
287 
288 }
289 
290 int main( int argc, char* argv[] ) {
291  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
293 }
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.