Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_LanczosUnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Stokhos.hpp"
19 
20 // Quadrature functor to be passed into quadrature expansion for mapping
21 // from Lanczos basis back to original PCE
22 struct lanczos_pce_quad_func {
25  pce(pce_), basis(basis_), vec(1) {}
26 
27  double operator() (const double& a) const {
28  vec[0] = a;
29  return pce.evaluate(vec);
30  }
34 };
35 
36 // Class encapsulating setup of the Lanczos basis for a given PCE
37 // u = Func(x), where Func is specified by a template-parameter
38 template <typename Func>
39 struct Lanczos_PCE_Setup {
40  typedef typename Func::OrdinalType OrdinalType;
41  typedef typename Func::ValueType ValueType;
43  Func func;
53 
54  Lanczos_PCE_Setup(bool normalize, bool project) :
55  func()
56  {
57  rtol = 1e-8;
58  atol = 1e-10;
59  const OrdinalType d = 3;
60  const OrdinalType p = 5;
61 
62  // Create product basis
64  for (OrdinalType i=0; i<d; i++)
65  bases[i] =
67  basis =
69 
70  // Create approximation
71  sz = basis->size();
73  for (OrdinalType i=0; i<d; i++)
74  x.term(i, 1) = 1.0;
75 
76  // Tensor product quadrature
79 
80  // Triple product tensor
83 
84  // Quadrature expansion
86 
87  // Compute PCE via quadrature expansion
88  u.reset(basis);
89  v.reset(basis);
90  func.eval(*exp, x, u);
91  exp->times(v,u,u);
92 
93  // Compute Lanczos basis
94  st_bases.resize(1);
95  if (project) {
98  p, Teuchos::rcp(&u,false), Cijk, normalize));
100  }
101  else {
102  st_1d_basis =
104  p, Teuchos::rcp(&u,false), quad, normalize, false));
105  st_bases[0] = st_1d_basis;
106  }
107 
108  st_basis =
110  st_sz = st_basis->size();
113  if (project) {
116  }
117  else {
118  u_st[0] = st_1d_basis->getNewCoeffs(0);
119  u_st[1] = st_1d_basis->getNewCoeffs(1);
120  }
121 
122  // Tensor product quadrature
123  st_quad =
125 
126  // Triple product tensor
129 
130  // Quadrature expansion
132  st_Cijk,
133  st_quad);
134 
135  st_exp.times(v_st, u_st, u_st);
136  }
137 
138 };
139 
140 #define LANCZOS_UNIT_TESTS(BASENAME, TAG, FUNC, NORMALIZE, PROJECT) \
141 namespace BASENAME ## TAG { \
142  \
143  Lanczos_PCE_Setup< FUNC<int,double> > setup(NORMALIZE, PROJECT); \
144  \
145  TEUCHOS_UNIT_TEST( BASENAME, TAG ## Map ) { \
146  Stokhos::OrthogPolyApprox<int,double> u2(setup.basis); \
147  if (PROJECT) \
148  setup.st_1d_proj_basis->transformCoeffsFromLanczos( \
149  setup.u_st.coeff(), \
150  u2.coeff()); \
151  else \
152  setup.st_1d_basis->transformCoeffsFromLanczos( \
153  setup.u_st.coeff(), \
154  u2.coeff()); \
155  success = Stokhos::comparePCEs(setup.u, "u", u2, "u2", \
156  setup.rtol, setup.atol, out); \
157  } \
158  \
159  TEUCHOS_UNIT_TEST( BASENAME, TAG ## Orthog ) { \
160  const Teuchos::Array<double>& norms = \
161  setup.st_bases[0]->norm_squared(); \
162  const Teuchos::Array<double>& weights = \
163  setup.st_quad->getQuadWeights(); \
164  const Teuchos::Array< Teuchos::Array<double> >& values = \
165  setup.st_quad->getBasisAtQuadPoints(); \
166  Teuchos::SerialDenseMatrix<int,double> mat(setup.st_sz, \
167  setup.st_sz); \
168  for (int i=0; i<setup.st_sz; i++) { \
169  for (int j=0; j<setup.st_sz; j++) { \
170  for (unsigned int k=0; k<weights.size(); k++) \
171  mat(i,j) += weights[k]*values[k][i]*values[k][j]; \
172  mat(i,j) /= std::sqrt(norms[i]*norms[j]); \
173  } \
174  mat(i,i) -= 1.0; \
175  } \
176  success = mat.normInf() < setup.atol; \
177  if (!success) { \
178  out << "\n Error, mat.normInf() < atol = " << mat.normInf() \
179  << " < " << setup.atol << ": failed!\n"; \
180  out << "mat = " << printMat(mat) << std::endl; \
181  } \
182  } \
183  \
184  TEUCHOS_UNIT_TEST( BASENAME, TAG ## PCE ) { \
185  Stokhos::OrthogPolyApprox<int,double> v2(setup.basis); \
186  lanczos_pce_quad_func quad_func(setup.v_st, *setup.st_basis); \
187  setup.exp->unary_op(quad_func, v2, setup.u); \
188  success = comparePCEs(setup.v, "v", v2, "v2", setup.rtol, \
189  setup.atol, out); \
190  } \
191  \
192  TEUCHOS_UNIT_TEST( BASENAME, TAG ## Mean ) { \
193  success = Teuchos::testRelErr( \
194  "v.mean()", setup.v.mean(), \
195  "v_st.mean()", setup.v_st.mean(), \
196  "rtol", setup.rtol, \
197  "rtol", setup.rtol, \
198  Teuchos::Ptr<std::ostream>(out.getOStream().get())); \
199  \
200  } \
201  \
202  TEUCHOS_UNIT_TEST( BASENAME, TAG ## StandardDeviation ) { \
203  success = Teuchos::testRelErr( \
204  "v.standard_deviation()", \
205  setup.v.standard_deviation(), \
206  "v_st.standard_devaition()", \
207  setup.v_st.standard_deviation(), \
208  "rtol", 1e-1, \
209  "rtol", 1e-1, \
210  Teuchos::Ptr<std::ostream>(out.getOStream().get())); \
211  } \
212  \
213 }
214 
215 //
216 // Lanczos tests based on expansion of u = cos(x) where x is a U([-1,1])
217 // random variable
218 //
219 template <typename Ordinal_Type, typename Value_Type>
220 struct Lanczos_Cos_Func {
221  typedef Ordinal_Type OrdinalType;
222  typedef Value_Type ValueType;
223  static const bool is_even = true;
224  void
228  exp.cos(u,x);
229  }
230 };
231 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Cos, Lanczos_Cos_Func,
232  false, true)
233 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_ProjNorm, Cos, Lanczos_Cos_Func,
234  true, true)
235 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis, Cos, Lanczos_Cos_Func,
236  false, false)
237 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Norm, Cos, Lanczos_Cos_Func,
238  true, false)
239 
240 //
241 // Lanczos tests based on expansion of u = sin(x) where x is a U([-1,1])
242 // random variable
243 //
244 template <typename Ordinal_Type, typename Value_Type>
245 struct Lanczos_Sin_Func {
246  typedef Ordinal_Type OrdinalType;
247  typedef Value_Type ValueType;
248  static const bool is_even = false;
249  void
253  exp.sin(u,x);
254  }
255 };
256 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Sin, Lanczos_Sin_Func,
257  false, true)
258 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_ProjNorm, Sin, Lanczos_Sin_Func,
259  true, true)
260 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis, Sin, Lanczos_Sin_Func,
261  false, false)
262 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Norm, Sin, Lanczos_Sin_Func,
263  true, false)
264 
265 //
266 // Lanczos tests based on expansion of u = exp(x) where x is a U([-1,1])
267 // random variable. For this test we don't use the PCE quad points and
268 // instead use those generated for the Lanczos basis
269 //
270 template <typename Ordinal_Type, typename Value_Type>
271 struct Lanczos_Exp_Func {
272  typedef Ordinal_Type OrdinalType;
273  typedef Value_Type ValueType;
274  static const bool is_even = false;
275  void
279  exp.exp(u,x);
280  }
281 };
282 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Proj, Exp, Lanczos_Exp_Func,
283  false, true)
284 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_ProjNorm, Exp, Lanczos_Exp_Func,
285  true, true)
286 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis, Exp, Lanczos_Exp_Func,
287  false, false)
288 LANCZOS_UNIT_TESTS(Stokhos_LanczosPCEBasis_Norm, Exp, Lanczos_Exp_Func,
289  true, false)
290 
291 int main( int argc, char* argv[] ) {
292  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
294 }
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)
value_type getNewCoeffs(ordinal_type i) const
Get new coefficients in this new basis.
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.
Lanczos_PCE_Setup(bool normalize, bool project)
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > st_quad
Teuchos::RCP< const Stokhos::LanczosPCEBasis< OrdinalType, ValueType > > st_1d_basis
const Stokhos::OrthogPolyBasis< int, double > & basis
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
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
Teuchos::RCP< const Stokhos::LanczosProjPCEBasis< OrdinalType, ValueType > > st_1d_proj_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
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.