Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Stokhos_MonoProjUnitTest.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"
52
53// Quadrature functor to be passed into quadrature expansion for mapping
54// from Lanczos basis back to original PCE
59
60 double operator() (const double& a) const {
61 vec[0] = a;
62 return pce.evaluate(vec);
63 }
66 mutable Teuchos::Array<double> vec;
67};
68
69// Class encapsulating setup of the Lanczos basis for a given PCE
70// u = Func(x), where Func is specified by a template-parameter
71template <typename Func>
72struct Lanczos_PCE_Setup {
73 typedef typename Func::OrdinalType OrdinalType;
74 typedef typename Func::ValueType ValueType;
76 Func func;
78 Teuchos::RCP<const Stokhos::CompletePolynomialBasis<OrdinalType,ValueType> > basis;
79 Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion<OrdinalType,ValueType> > exp;
80 Teuchos::RCP<const Stokhos::MonoProjPCEBasis<OrdinalType,ValueType> > st_1d_proj_basis;
81 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<OrdinalType,ValueType> > > st_bases;
82 Teuchos::RCP<const Stokhos::CompletePolynomialBasis<OrdinalType,ValueType> > st_basis;
83 Teuchos::RCP<const Stokhos::Quadrature<OrdinalType,ValueType> > st_quad;
85
86 Lanczos_PCE_Setup(bool normalize) :
87 func()
88 {
89 rtol = 1e-8;
90 atol = 1e-12;
91 const OrdinalType d = 3;
92 const OrdinalType p = 5;
93
94 // Create product basis
95 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<OrdinalType,ValueType> > > bases(d);
96 for (OrdinalType i=0; i<d; i++)
97 bases[i] =
98 Teuchos::rcp(new Stokhos::LegendreBasis<OrdinalType,ValueType>(p, true));
99 basis =
101
102 // Create approximation
103 sz = basis->size();
105 for (OrdinalType i=0; i<d; i++)
106 x.term(i, 1) = 1.0;
107
108 // Tensor product quadrature
109 Teuchos::RCP<const Stokhos::Quadrature<OrdinalType,ValueType> > quad =
111
112 // Triple product tensor
113 Teuchos::RCP<Stokhos::Sparse3Tensor<int,double> > Cijk =
114 basis->computeTripleProductTensor(basis->size());
115
116 // Quadrature expansion
117 exp = Teuchos::rcp(new Stokhos::QuadOrthogPolyExpansion<OrdinalType,ValueType>(basis, Cijk, quad));
118
119 // Compute PCE via quadrature expansion
120 u.reset(basis);
121 v.reset(basis);
122 func.eval(*exp, x, u);
123 exp->times(v,u,u);
124
125 // Compute Lanczos basis
126 st_bases.resize(1);
129 p, u, *quad, *Cijk));
131
132 st_basis =
134 st_sz = st_basis->size();
137 u_st[0] = st_1d_proj_basis->getNewCoeffs(0);
138 u_st[1] = st_1d_proj_basis->getNewCoeffs(1);
139
140 // Tensor product quadrature
141 st_quad =
143
144 // Triple product tensor
145 Teuchos::RCP<Stokhos::Sparse3Tensor<int,double> > st_Cijk =
146 st_basis->computeTripleProductTensor(st_basis->size());
147
148 // Quadrature expansion
150 st_Cijk,
151 st_quad);
152
153 st_exp.times(v_st, u_st, u_st);
154 }
155
156};
157
158#define LANCZOS_UNIT_TESTS(BASENAME, TAG, FUNC, NORMALIZE) \
159namespace BASENAME ## TAG { \
160 \
161 Lanczos_PCE_Setup< FUNC<int,double> > setup(NORMALIZE); \
162 \
163 TEUCHOS_UNIT_TEST( BASENAME, TAG ## Map ) { \
164 Stokhos::OrthogPolyApprox<int,double> u2(setup.basis); \
165 setup.st_1d_proj_basis->transformCoeffs( \
166 setup.u_st.coeff(), \
167 u2.coeff()); \
168 success = Stokhos::comparePCEs(setup.u, "u", u2, "u2", \
169 setup.rtol, setup.atol, out); \
170 } \
171 \
172 TEUCHOS_UNIT_TEST( BASENAME, TAG ## Orthog ) { \
173 const Teuchos::Array<double>& norms = \
174 setup.st_bases[0]->norm_squared(); \
175 const Teuchos::Array<double>& weights = \
176 setup.st_quad->getQuadWeights(); \
177 const Teuchos::Array< Teuchos::Array<double> >& values = \
178 setup.st_quad->getBasisAtQuadPoints(); \
179 Teuchos::SerialDenseMatrix<int,double> mat(setup.st_sz, \
180 setup.st_sz); \
181 for (int i=0; i<setup.st_sz; i++) { \
182 for (int j=0; j<setup.st_sz; j++) { \
183 for (unsigned int k=0; k<weights.size(); k++) \
184 mat(i,j) += weights[k]*values[k][i]*values[k][j]; \
185 mat(i,j) /= std::sqrt(norms[i]*norms[j]); \
186 } \
187 mat(i,i) -= 1.0; \
188 } \
189 success = mat.normInf() < setup.atol; \
190 if (!success) { \
191 out << "\n Error, mat.normInf() < atol = " << mat.normInf() \
192 << " < " << setup.atol << ": failed!\n"; \
193 out << "mat = " << mat << std::endl; \
194 } \
195 } \
196 \
197 TEUCHOS_UNIT_TEST( BASENAME, TAG ## PCE ) { \
198 Stokhos::OrthogPolyApprox<int,double> v2(setup.basis); \
199 lanczos_pce_quad_func quad_func(setup.v_st, *setup.st_basis); \
200 setup.exp->unary_op(quad_func, v2, setup.u); \
201 success = comparePCEs(setup.v, "v", v2, "v2", setup.rtol, \
202 setup.atol, out); \
203 } \
204 \
205 TEUCHOS_UNIT_TEST( BASENAME, TAG ## Mean ) { \
206 success = Teuchos::testRelErr( \
207 "v.mean()", setup.v.mean(), \
208 "v_st.mean()", setup.v_st.mean(), \
209 "rtol", setup.rtol, \
210 "rtol", setup.rtol, \
211 Teuchos::Ptr<std::ostream>(out.getOStream().get())); \
212 \
213 } \
214 \
215 TEUCHOS_UNIT_TEST( BASENAME, TAG ## StandardDeviation ) { \
216 success = Teuchos::testRelErr( \
217 "v.standard_deviation()", \
218 setup.v.standard_deviation(), \
219 "v_st.standard_devaition()", \
220 setup.v_st.standard_deviation(), \
221 "rtol", 1e-1, \
222 "rtol", 1e-1, \
223 Teuchos::Ptr<std::ostream>(out.getOStream().get())); \
224 } \
225 \
226}
227
228//
229// Lanczos tests based on expansion of u = cos(x) where x is a U([-1,1])
230// random variable
231//
232template <typename Ordinal_Type, typename Value_Type>
233struct Lanczos_Cos_Func {
234 typedef Ordinal_Type OrdinalType;
235 typedef Value_Type ValueType;
236 static const bool is_even = true;
237 void
243};
244LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Cos, Lanczos_Cos_Func,
245 false)
246
247//
248// Lanczos tests based on expansion of u = sin(x) where x is a U([-1,1])
249// random variable
250//
251template <typename Ordinal_Type, typename Value_Type>
252struct Lanczos_Sin_Func {
253 typedef Ordinal_Type OrdinalType;
254 typedef Value_Type ValueType;
255 static const bool is_even = false;
256 void
262};
263LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Sin, Lanczos_Sin_Func,
264 false)
265
266//
267// Lanczos tests based on expansion of u = exp(x) where x is a U([-1,1])
268// random variable. For this test we don't use the PCE quad points and
269// instead use those generated for the Lanczos basis
270//
271template <typename Ordinal_Type, typename Value_Type>
272struct Lanczos_Exp_Func {
273 typedef Ordinal_Type OrdinalType;
274 typedef Value_Type ValueType;
275 static const bool is_even = false;
276 void
282};
283LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Exp, Lanczos_Exp_Func,
284 false)
285
286int main( int argc, char* argv[] ) {
287 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
288 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
289}
#define LANCZOS_UNIT_TESTS(BASENAME, TAG, FUNC, NORMALIZE)
char * argv[]
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Legendre polynomial basis.
Generates three-term recurrence using the Lanczos procedure applied to a polynomial chaos expansion i...
Class to store coefficients of a projection onto an orthogonal polynomial basis.
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
Abstract base class for multivariate orthogonal polynomials.
Orthogonal polynomial expansions based on numerical quadrature.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
Teuchos::RCP< const Stokhos::MonoProjPCEBasis< OrdinalType, ValueType > > st_1d_proj_basis
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > st_quad
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v
Teuchos::RCP< const Stokhos::HouseTriDiagPCEBasis< OrdinalType, ValueType > > st_1d_proj_basis
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > st_basis
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v_st
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > > exp
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u_st
Teuchos::Array< Teuchos::RCP< const Stokhos::OneDOrthogPolyBasis< OrdinalType, ValueType > > > st_bases
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
double operator()(const double &a) const
const Stokhos::OrthogPolyBasis< int, double > & basis
lanczos_pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
const Stokhos::OrthogPolyApprox< int, double > & pce