Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
44#include "Teuchos_UnitTestHarness.hpp"
45#include "Teuchos_TestingHelpers.hpp"
46#include "Teuchos_UnitTestRepository.hpp"
47#include "Teuchos_GlobalMPISession.hpp"
48
49#include "Stokhos.hpp"
51
52#include <iterator>
53
55
56 // Common setup for unit tests
57 template <typename OrdinalType, typename ValueType>
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;
74 UnitTestSetup<ordinal_type,value_type> setup;
75
76 TEUCHOS_UNIT_TEST( Coefficients, Isotropic ) {
77 success = true;
78
79 // Build tensor product basis of dimension d and order p
80 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
81 for (ordinal_type i=0; i<setup.d; i++)
82 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
83 Teuchos::RCP<const Stokhos::TensorProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
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
117 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
118 for (ordinal_type i=0; i<setup.d; i++)
119 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
120 Teuchos::RCP<const Stokhos::TensorProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
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
154 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
155 for (ordinal_type i=0; i<setup.d; i++)
156 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
157 Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases, setup.sparse_tol));
158 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
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
169 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
170 for (ordinal_type i=0; i<setup.d; i++)
171 bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
172 Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases, setup.sparse_tol));
173 Teuchos::RCP< Stokhos::Sparse3Tensor<ordinal_type, value_type> > Cijk =
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
183int main( int argc, char* argv[] ) {
184 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
185 int res = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
186 return res;
187}
int main(int argc, char *argv[])
Legendre polynomial basis.
A multidimensional index.
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials.
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)
UnitTestSetup< ordinal_type, value_type > setup