Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_TensorProductPseudoSpectralOperatorUnitTest.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 OrdinalType, typename ValueType>
57  struct UnitTestSetup {
58  ValueType rtol, atol;
59  OrdinalType p,d;
60 
62  rtol = 1e-12;
63  atol = 1e-12;
64  d = 3;
65  p = 5;
66  }
67 
68  };
69 
70  typedef int ordinal_type;
71  typedef double value_type;
73 
74  // Function for testing quadratures
75  value_type quad_func1(const Teuchos::ArrayView<const value_type>& x) {
76  value_type val = 0.0;
77  for (int i=0; i<x.size(); i++)
78  val += x[i];
79  return std::exp(val);
80  }
81 
82  // Function for testing quadratures
83  value_type quad_func2(const Teuchos::ArrayView<const value_type>& x) {
84  value_type val = 0.0;
85  for (int i=0; i<x.size(); i++)
86  val += x[i];
87  return std::sin(val);
88  }
89 
90  TEUCHOS_UNIT_TEST( TensorProduct, IsotropicPoints ) {
91  success = true;
92 
93  // Build tensor product basis of dimension d and order p
94  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
95  for (ordinal_type i=0; i<setup.d; i++)
96  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
97  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
99  *basis);
102  *basis, quad);
103 
105  tp_op, quad_op, setup.rtol, setup.atol, out);
106  }
107 
108  TEUCHOS_UNIT_TEST( TensorProduct, AnisotropicPoints ) {
109  success = true;
110 
111  // Build aniostropic tensor product basis of dimension d
112  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
113  for (ordinal_type i=0; i<setup.d; i++)
114  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
115  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
117  *basis);
120  *basis, quad);
121 
123  tp_op, quad_op, setup.rtol, setup.atol, out);
124  }
125 
126  TEUCHOS_UNIT_TEST( TensorProduct, Apply ) {
127  success = true;
128 
129  // Build anisotropic tensor product basis of dimension d
130  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
131  for (ordinal_type i=0; i<setup.d; i++)
132  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
133  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
135  *basis);
138  *basis, quad);
139 
141  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
142  }
143 
144  TEUCHOS_UNIT_TEST( TensorProduct, Apply_Trans ) {
145  success = true;
146 
147  // Build anisotropic tensor product basis of dimension d
148  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
149  for (ordinal_type i=0; i<setup.d; i++)
150  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
151  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
153  *basis);
156  *basis, quad);
157 
159  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
160  }
161 
162  TEUCHOS_UNIT_TEST( TensorProduct, Apply_PST_Lexo ) {
163  success = true;
164 
165  // Build anisotropic tensor product basis of dimension d
166  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
167  for (ordinal_type i=0; i<setup.d; i++)
168  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
170  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type,coeff_compare>(bases));
172  *basis, true);
175  *basis, quad);
176 
178  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
179  }
180 
181  TEUCHOS_UNIT_TEST( TensorProduct, Apply_PST_TotalOrder ) {
182  success = true;
183 
184  // Build anisotropic tensor product basis of dimension d
185  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
186  for (ordinal_type i=0; i<setup.d; i++)
187  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
188  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
190  *basis, true);
193  *basis, quad);
194 
196  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
197  }
198 
199  TEUCHOS_UNIT_TEST( TensorProduct, Apply_PST_Trans_Lexo ) {
200  success = true;
201 
202  // Build anisotropic tensor product basis of dimension d
203  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
204  for (ordinal_type i=0; i<setup.d; i++)
205  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
207  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type,coeff_compare>(bases));
209  *basis, true);
212  *basis, quad);
213 
215  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
216  }
217 
218  TEUCHOS_UNIT_TEST( TensorProduct, Apply_PST_Trans_TotalOrder ) {
219  success = true;
220 
221  // Build anisotropic tensor product basis of dimension d
222  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
223  for (ordinal_type i=0; i<setup.d; i++)
224  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
225  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
227  *basis, true);
230  *basis, quad);
231 
233  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
234  }
235 
236  TEUCHOS_UNIT_TEST( TensorProduct, IsotropicDiscreteOrthogonality ) {
237  success = true;
238 
239  // Build tensor product basis of dimension d and order p
240  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
241  for (ordinal_type i=0; i<setup.d; i++)
242  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
243  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
245  *basis, true);
246 
248  *basis, tp_op, setup.rtol, setup.atol, out);
249  }
250 
251  TEUCHOS_UNIT_TEST( TensorProduct, AnisotropicDiscreteOrthogonality ) {
252  success = true;
253 
254  // Build tensor product basis of dimension d and order p
255  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
256  for (ordinal_type i=0; i<setup.d; i++)
257  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
258  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TensorProductBasis<ordinal_type,value_type>(bases));
260  *basis, true);
261 
263  *basis, tp_op, setup.rtol, setup.atol, out);
264  }
265 
266  TEUCHOS_UNIT_TEST( TotalOrder, IsotropicPoints ) {
267  success = true;
268 
269  // Build total order basis of dimension d and order p
270  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
271  for (ordinal_type i=0; i<setup.d; i++)
272  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
273  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
275  *basis);
278  *basis, quad);
279 
281  tp_op, quad_op, setup.rtol, setup.atol, out);
282  }
283 
284  TEUCHOS_UNIT_TEST( TotalOrder, AnisotropicPoints ) {
285  success = true;
286 
287  // Build aniostropic tensor product basis of dimension d
288  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
289  for (ordinal_type i=0; i<setup.d; i++)
290  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
291  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
293  *basis);
296  *basis, quad);
297 
299  tp_op, quad_op, setup.rtol, setup.atol, out);
300  }
301 
302  TEUCHOS_UNIT_TEST( TotalOrder, Apply ) {
303  success = true;
304 
305  // Build anisotropic tensor product basis of dimension d
306  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
307  for (ordinal_type i=0; i<setup.d; i++)
308  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
309  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
311  *basis);
314  *basis, quad);
315 
317  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
318  }
319 
320  TEUCHOS_UNIT_TEST( TotalOrder, ApplyTrans ) {
321  success = true;
322 
323  // Build anisotropic tensor product basis of dimension d
324  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
325  for (ordinal_type i=0; i<setup.d; i++)
326  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
327  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
329  *basis);
332  *basis, quad);
333 
335  tp_op, quad_op, quad_func1, quad_func2, setup.rtol, setup.atol, out);
336  }
337 
338  TEUCHOS_UNIT_TEST( TotalOrder, IsotropicDiscreteOrthogonality ) {
339  success = true;
340 
341  // Build tensor product basis of dimension d and order p
342  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
343  for (ordinal_type i=0; i<setup.d; i++)
344  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(setup.p, true));
345  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
347  *basis);
348 
350  *basis, tp_op, setup.rtol, setup.atol, out);
351  }
352 
353  TEUCHOS_UNIT_TEST( TotalOrder, AnisotropicDiscreteOrthogonality ) {
354  success = true;
355 
356  // Build tensor product basis of dimension d and order p
357  Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(setup.d);
358  for (ordinal_type i=0; i<setup.d; i++)
359  bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(i+1, true));
360  Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > basis = Teuchos::rcp(new Stokhos::TotalOrderBasis<ordinal_type,value_type>(bases));
362  *basis);
363 
365  *basis, tp_op, setup.rtol, setup.atol, out);
366  }
367 
368 }
369 
370 int main( int argc, char* argv[] ) {
371  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
372  int res = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
373  Teuchos::TimeMonitor::summarize(std::cout);
374  return res;
375 }
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
expr val()
value_type quad_func2(const Teuchos::ArrayView< const value_type > &x)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
value_type quad_func1(const Teuchos::ArrayView< const value_type > &x)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials...
Legendre polynomial basis.
bool testPseudoSpectralDiscreteOrthogonality(const basis_type &basis, const operator_type &op, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
UnitTestSetup< ordinal_type, value_type > setup
A comparison functor implementing a strict weak ordering based lexographic ordering.
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
An operator for building pseudo-spectral coefficients using tensor-product quadrature.
int main(int argc, char *argv[])
An operator for building pseudo-spectral coefficients using an arbitrary quadrature rule...
bool testPseudoSpectralPoints(const operator_type1 &op1, const operator_type2 &op2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
bool testPseudoSpectralApply(const operator_type1 &op1, const operator_type2 &op2, const func_type1 &func1, const func_type2 &func2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
bool testPseudoSpectralApplyTrans(const operator_type1 &op1, const operator_type2 &op2, const func_type1 &func1, const func_type2 &func2, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)