Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
44#include "Teuchos_UnitTestHarness.hpp"
45#include "Teuchos_TestingHelpers.hpp"
46#include "Teuchos_UnitTestRepository.hpp"
47#include "Teuchos_GlobalMPISession.hpp"
48#include "Teuchos_ArrayView.hpp"
49
50#include "Stokhos.hpp"
52
54
55 // Common setup for unit tests
56 template <typename ordinal_type, typename value_type>
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;
72 typedef UnitTestSetup<ordinal_type,value_type> setup_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
108 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
109 for (ordinal_type i=0; i<setup.d; i++)
110 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
113 bases, coeff_index_set);
114
115 // Build isotropic total order basis of dimension d and order p
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
126 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
128 for (ordinal_type i=0; i<setup.d; i++) {
129 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
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
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
150 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
152 for (ordinal_type i=0; i<setup.d; i++) {
153 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
154 upper[i] = i+1;
155 }
157 setup.d, upper); // largest order is setup.d-1+1
159 bases, coeff_index_set);
160 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk1 =
161 smolyak_basis.computeTripleProductTensor();
162
163 // Build isotropic total order basis of dimension d and order p
165 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk2 =
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
173 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk1_lin=
174 smolyak_basis.computeLinearTripleProductTensor();
175 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk2_lin=
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
192 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
194 for (ordinal_type i=0; i<setup.d; i++) {
195 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
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);
202 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
203 smolyak_basis.computeTripleProductTensor();
204
205 success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
206 setup.sparse_tol, setup.rtol,
207 setup.atol, out);
208
209 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk_lin =
210 smolyak_basis.computeLinearTripleProductTensor();
211
212 success = success &&
213 Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
214 setup.sparse_tol, setup.rtol,
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
225 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
227 for (ordinal_type i=0; i<setup.d; i++) {
228 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true, Stokhos::MODERATE_GROWTH));
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);
235 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
236 smolyak_basis.computeTripleProductTensor();
237
238 success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
239 setup.sparse_tol, setup.rtol,
240 setup.atol, out);
241
242 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk_lin =
243 smolyak_basis.computeLinearTripleProductTensor();
244
245 success = success &&
246 Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
247 setup.sparse_tol, setup.rtol,
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
260 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
262 for (ordinal_type i=0; i<setup.d; i++) {
263 bases[i] = Teuchos::rcp(new Stokhos::ClenshawCurtisLegendreBasis<ordinal_type,value_type>(i+1, true));
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);
270 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
271 smolyak_basis.computeTripleProductTensor();
272
273 success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
274 setup.sparse_tol, setup.rtol,
275 setup.atol, out);
276
277 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk_lin =
278 smolyak_basis.computeLinearTripleProductTensor();
279
280 success = success &&
281 Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
282 setup.sparse_tol, setup.rtol,
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
293 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
295 for (ordinal_type i=0; i<setup.d; i++) {
296 bases[i] = Teuchos::rcp(new Stokhos::GaussPattersonLegendreBasis<ordinal_type,value_type>(i+1, true));
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);
303 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
304 smolyak_basis.computeTripleProductTensor();
305
306 success = Stokhos::testSparse3Tensor(*Cijk, smolyak_basis,
307 setup.sparse_tol, setup.rtol,
308 setup.atol, out);
309
310 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk_lin =
311 smolyak_basis.computeLinearTripleProductTensor();
312
313 success = success &&
314 Stokhos::testSparse3Tensor(*Cijk_lin, smolyak_basis,
315 setup.sparse_tol, setup.rtol,
316 setup.atol, out, true);
317
318 }
319
320#endif
321
322}
323
324int main( int argc, char* argv[] ) {
325 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
326 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
327}
int main(int argc, char *argv[])
An anisotropic total order index set.
Legendre polynomial basis using Clenshaw-Curtis quadrature points.
Legendre polynomial basis using Gauss-Patterson quadrature points.
Legendre polynomial basis.
A multidimensional index.
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
Multivariate orthogonal polynomial basis generated from a Smolyak sparse grid.
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
An isotropic total order index set.
UnitTestSetup< ordinal_type, value_type > setup_type
bool testCoefficients(const Stokhos::ProductBasis< ordinal_type, value_type > &basis1, const Stokhos::ProductBasis< ordinal_type, value_type > &basis2, Teuchos::FancyOStream &out)
TEUCHOS_UNIT_TEST(Coefficients, IsotropicLinear)
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)
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)