Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
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
55 struct lanczos_pce_quad_func {
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>
72 struct Lanczos_PCE_Setup {
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, *quad, *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->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 //
232 template <typename Ordinal_Type, typename Value_Type>
233 struct Lanczos_Cos_Func {
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>
252 struct Lanczos_Sin_Func {
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>
272 struct Lanczos_Exp_Func {
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 }
void sin(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
const Stokhos::OrthogPolyApprox< int, double > & pce
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)
lanczos_pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
Teuchos::RCP< const Stokhos::HouseTriDiagPCEBasis< OrdinalType, ValueType > > st_1d_proj_basis
void exp(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
void cos(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
value_type getNewCoeffs(ordinal_type i) const
Get new coefficients in this new basis.
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v_st
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u
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 > > st_basis
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > st_quad
const Stokhos::OrthogPolyBasis< int, double > & basis
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
Teuchos::RCP< const Stokhos::MonoProjPCEBasis< OrdinalType, ValueType > > st_1d_proj_basis
void resize(size_type new_size, const value_type &x=value_type())
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u_st
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
int main(int argc, char **argv)
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > > exp
virtual ordinal_type size() const
Return total size of basis.
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Teuchos::Array< Teuchos::RCP< const Stokhos::OneDOrthogPolyBasis< OrdinalType, ValueType > > > st_bases
Func::OrdinalType OrdinalType
Lanczos_PCE_Setup(bool normalize)
double operator()(const double &a) const
#define LANCZOS_UNIT_TESTS(BASENAME, TAG, FUNC, NORMALIZE)
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.