Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_HouseTriDiagUnitTest.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 
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
58  pce(pce_), basis(basis_), vec(1) {}
59 
60  double operator() (const double& a) const {
61  vec[0] = a;
62  return pce.evaluate(vec);
63  }
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
71 template <typename Func>
73  typedef typename Func::OrdinalType OrdinalType;
74  typedef typename Func::ValueType ValueType;
76  Func func;
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
96  for (OrdinalType i=0; i<d; i++)
97  bases[i] =
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
111 
112  // Triple product tensor
115 
116  // Quadrature expansion
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, *Cijk));
131 
132  st_basis =
134  st_sz = st_basis->size();
139 
140  // Tensor product quadrature
141  st_quad =
143 
144  // Triple product tensor
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) \
159 namespace 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->transformCoeffsFromHouse( \
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 //
232 template <typename Ordinal_Type, typename Value_Type>
234  typedef Ordinal_Type OrdinalType;
235  typedef Value_Type ValueType;
236  static const bool is_even = true;
237  void
241  exp.cos(u,x);
242  }
243 };
244 LANCZOS_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 //
251 template <typename Ordinal_Type, typename Value_Type>
253  typedef Ordinal_Type OrdinalType;
254  typedef Value_Type ValueType;
255  static const bool is_even = false;
256  void
260  exp.sin(u,x);
261  }
262 };
263 LANCZOS_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 //
271 template <typename Ordinal_Type, typename Value_Type>
273  typedef Ordinal_Type OrdinalType;
274  typedef Value_Type ValueType;
275  static const bool is_even = false;
276  void
280  exp.exp(u,x);
281  }
282 };
283 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Exp, Lanczos_Exp_Func,
284  false)
285 
286 int main( int argc, char* argv[] ) {
287  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
289 }
Teuchos_TestingHelpers.hpp
Cos
Cos
Definition: Stokhos_LanczosUnitTest.cpp:267
Lanczos_Exp_Func
Definition: Stokhos_HouseTriDiagUnitTest.cpp:272
Lanczos_PCE_Setup::u
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u
Definition: Stokhos_HouseTriDiagUnitTest.cpp:84
LANCZOS_UNIT_TESTS
#define LANCZOS_UNIT_TESTS(BASENAME, TAG, FUNC, NORMALIZE)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:158
main
int main(int argc, char **argv)
Definition: cijk_ltb_partition.cpp:57
Lanczos_PCE_Setup::atol
ValueType atol
Definition: Stokhos_HouseTriDiagUnitTest.cpp:75
Lanczos_Cos_Func::ValueType
Value_Type ValueType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:235
Lanczos_Sin_Func::OrdinalType
Ordinal_Type OrdinalType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:253
Teuchos::Array::resize
void resize(size_type new_size, const value_type &x=value_type())
Stokhos::CompletePolynomialBasis::computeTripleProductTensor
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
Definition: Stokhos_CompletePolynomialBasisImp.hpp:150
Stokhos::HouseTriDiagPCEBasis< OrdinalType, ValueType >
Stokhos::HouseTriDiagPCEBasis::getNewCoeffs
value_type getNewCoeffs(ordinal_type i) const
Get new coefficients in this new basis.
Definition: Stokhos_HouseTriDiagPCEBasisImp.hpp:208
Sacado::UQ::exp
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Definition: Sacado_UQ_PCE_Imp.hpp:827
lanczos_pce_quad_func::vec
Teuchos::Array< double > vec
Definition: Stokhos_HouseTriDiagUnitTest.cpp:66
Stokhos.hpp
Lanczos_Cos_Func::eval
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:238
lanczos_pce_quad_func::lanczos_pce_quad_func
lanczos_pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:56
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Lanczos_Sin_Func
Definition: Stokhos_HouseTriDiagUnitTest.cpp:252
Stokhos::QuadOrthogPolyExpansion::times
void times(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a, const OrthogPolyApprox< ordinal_type, value_type, node_type > &b)
Definition: Stokhos_QuadOrthogPolyExpansionImp.hpp:474
Lanczos_PCE_Setup::Lanczos_PCE_Setup
Lanczos_PCE_Setup(bool normalize)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:86
Lanczos_PCE_Setup::exp
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > > exp
Definition: Stokhos_HouseTriDiagUnitTest.cpp:79
Teuchos::GlobalMPISession
Lanczos_Sin_Func::eval
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:257
Lanczos_PCE_Setup::basis
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
Definition: Stokhos_HouseTriDiagUnitTest.cpp:78
Teuchos::RCP
lanczos_pce_quad_func::operator()
double operator()(const double &a) const
Definition: Stokhos_HouseTriDiagUnitTest.cpp:60
Lanczos_PCE_Setup::ValueType
Func::ValueType ValueType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:74
Teuchos_UnitTestHarness.hpp
Teuchos::Array< double >
Lanczos_PCE_Setup
Definition: Stokhos_HouseTriDiagUnitTest.cpp:72
Teuchos_UnitTestRepository.hpp
Stokhos::CompletePolynomialBasis::size
virtual ordinal_type size() const
Return total size of basis.
Definition: Stokhos_CompletePolynomialBasisImp.hpp:126
Stokhos::TensorProductQuadrature
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
Definition: Stokhos_TensorProductQuadrature.hpp:58
Lanczos_PCE_Setup::v
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v
Definition: Stokhos_HouseTriDiagUnitTest.cpp:84
Lanczos_Cos_Func::OrdinalType
Ordinal_Type OrdinalType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:234
Lanczos_PCE_Setup::u_st
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u_st
Definition: Stokhos_HouseTriDiagUnitTest.cpp:84
Stokhos::OrthogPolyApprox::evaluate
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
Definition: Stokhos_OrthogPolyApproxImp.hpp:248
lanczos_pce_quad_func
Definition: Stokhos_HouseTriDiagUnitTest.cpp:55
Lanczos_PCE_Setup::func
Func func
Definition: Stokhos_HouseTriDiagUnitTest.cpp:76
Stokhos::LegendreBasis< OrdinalType, ValueType >
Lanczos_Exp_Func::eval
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Definition: Stokhos_HouseTriDiagUnitTest.cpp:277
Lanczos_PCE_Setup::OrdinalType
Func::OrdinalType OrdinalType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:73
Lanczos_PCE_Setup::st_1d_proj_basis
Teuchos::RCP< const Stokhos::HouseTriDiagPCEBasis< OrdinalType, ValueType > > st_1d_proj_basis
Definition: Stokhos_HouseTriDiagUnitTest.cpp:80
Teuchos_GlobalMPISession.hpp
Stokhos::OrthogPolyApprox::reset
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
Definition: Stokhos_OrthogPolyApproxImp.hpp:138
Lanczos_PCE_Setup::st_basis
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > st_basis
Definition: Stokhos_HouseTriDiagUnitTest.cpp:82
Stokhos_UnitTestHelpers.hpp
cusp::detail::device::x
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
Lanczos_PCE_Setup::st_sz
OrdinalType st_sz
Definition: Stokhos_HouseTriDiagUnitTest.cpp:77
Lanczos_PCE_Setup::st_quad
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > st_quad
Definition: Stokhos_HouseTriDiagUnitTest.cpp:83
Lanczos_Cos_Func::is_even
static const bool is_even
Definition: Stokhos_HouseTriDiagUnitTest.cpp:236
Stokhos::CompletePolynomialBasis< OrdinalType, ValueType >
Stokhos::OrthogPolyApprox< int, double >
Stokhos_HouseTriDiagPCEBasis.hpp
Lanczos_Sin_Func::ValueType
Value_Type ValueType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:254
Lanczos_PCE_Setup::sz
OrdinalType sz
Definition: Stokhos_HouseTriDiagUnitTest.cpp:77
Teuchos::UnitTestRepository::runUnitTestsFromMain
static int runUnitTestsFromMain(int argc, char *argv[])
int
int
Definition: tpetra_mat_vec.cpp:243
Lanczos_Cos_Func
Definition: Stokhos_HouseTriDiagUnitTest.cpp:233
Lanczos_PCE_Setup::v_st
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v_st
Definition: Stokhos_HouseTriDiagUnitTest.cpp:84
Lanczos_Exp_Func::ValueType
Value_Type ValueType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:274
Lanczos_PCE_Setup::st_bases
Teuchos::Array< Teuchos::RCP< const Stokhos::OneDOrthogPolyBasis< OrdinalType, ValueType > > > st_bases
Definition: Stokhos_HouseTriDiagUnitTest.cpp:81
Lanczos_PCE_Setup::rtol
ValueType rtol
Definition: Stokhos_HouseTriDiagUnitTest.cpp:75
argv
char * argv[]
Definition: Stokhos_HouseTriDiagUnitTest.cpp:286
lanczos_pce_quad_func::basis
const Stokhos::OrthogPolyBasis< int, double > & basis
Definition: Stokhos_HouseTriDiagUnitTest.cpp:65
Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType >
Exp
Exp
Definition: Stokhos_LanczosUnitTest.cpp:318
Lanczos_Exp_Func::OrdinalType
Ordinal_Type OrdinalType
Definition: Stokhos_HouseTriDiagUnitTest.cpp:273
Stokhos::OrthogPolyBasis< int, double >
Sin
Sin
Definition: Stokhos_LanczosUnitTest.cpp:292
lanczos_pce_quad_func::pce
const Stokhos::OrthogPolyApprox< int, double > & pce
Definition: Stokhos_HouseTriDiagUnitTest.cpp:64