Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_StieltjesUnitTest.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"
18 
19 // Quadrature functor to be passed into quadrature expansion for mapping
20 // from Stieltjes basis back to original PCE
24  pce(pce_), basis(basis_), vec(1) {}
25 
26  double operator() (const double& a) const {
27  vec[0] = a;
28  return pce.evaluate(vec);
29  }
33 };
34 
35 // Class encapsulating setup of the Stieltjes basis for a given PCE
36 // u = Func(x), where Func is specified by a template-parameter
37 template <typename Func>
39  typedef typename Func::OrdinalType OrdinalType;
40  typedef typename Func::ValueType ValueType;
42  Func func;
51 
52  Stieltjes_PCE_Setup(bool use_pce_quad_points_) :
53  func(), use_pce_quad_points(use_pce_quad_points_)
54  {
55  rtol = 1e-8;
56  atol = 1e-12;
57  const OrdinalType d = 3;
58  const OrdinalType p = 5;
59 
60  // Create product basis
62  for (OrdinalType i=0; i<d; i++)
63  bases[i] =
65  basis =
67 
68  // Create approximation
69  sz = basis->size();
71  for (OrdinalType i=0; i<d; i++)
72  x.term(i, 1) = 1.0;
73 
74  // Tensor product quadrature
77 
78  // Triple product tensor
81 
82  // Quadrature expansion
84 
85  // Compute PCE via quadrature expansion
86  u.reset(basis);
87  v.reset(basis);
88  func.eval(*exp, x, u);
89  exp->times(v,u,u);
90 
91  // Compute Stieltjes basis
92  st_1d_basis =
94  p, Teuchos::rcp(&u,false), quad, use_pce_quad_points));
96  st_bases[0] = st_1d_basis;
97  st_basis =
99  st_sz = st_basis->size();
102  u_st[0] = u.mean();
103  u_st[1] = 1.0;
104 
105  // Tensor product quadrature
106  st_quad =
108 
109  // Triple product tensor
112 
113  // Quadrature expansion
115  st_Cijk,
116  st_quad);
117 
118  st_exp.times(v_st, u_st, u_st);
119  }
120 
121 };
122 
123 //
124 // Stieltjes tests based on expansion of u = cos(x) where x is a U([-1,1])
125 // random variable
126 //
127 namespace StieltjesCosTest {
128 
129  template <typename Ordinal_Type, typename Value_Type>
131  typedef Ordinal_Type OrdinalType;
132  typedef Value_Type ValueType;
133  static const bool is_even = true;
134  void
138  exp.cos(u,x);
139  }
140  };
142 
143  // Tests mapping from Stieltjes basis to original is correct
144  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, CosMap ) {
146  setup.st_1d_basis->transformCoeffsFromStieltjes(setup.u_st.coeff(),
147  u2.coeff());
148  success = Stokhos::comparePCEs(setup.u, "u", u2, "u2", setup.rtol, setup.atol, out);
149  }
150 
151  // Tests Stieltjes basis is orthogonal
152  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, CosOrthog ) {
153  const Teuchos::Array<double>& norms = setup.st_1d_basis->norm_squared();
154  const Teuchos::Array<double>& weights = setup.st_quad->getQuadWeights();
155  const Teuchos::Array< Teuchos::Array<double> >& values =
156  setup.st_quad->getBasisAtQuadPoints();
158  setup.st_sz);
159  for (int i=0; i<setup.st_sz; i++) {
160  for (int j=0; j<setup.st_sz; j++) {
161  for (unsigned int k=0; k<weights.size(); k++)
162  mat(i,j) += weights[k]*values[k][i]*values[k][j];
163  mat(i,j) /= std::sqrt(norms[i]*norms[j]);
164  }
165  mat(i,i) -= 1.0;
166  }
167  success = mat.normInf() < setup.atol;
168  if (!success) {
169  out << "\n Error, mat.normInf() < atol = " << mat.normInf()
170  << " < " << setup.atol << ": failed!\n";
171  out << "mat = " << printMat(mat) << std::endl;
172  }
173  }
174 
175  // Tests PCE computed from Stieltjes basis is same as original
176  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, CosPCE ) {
178  stieltjes_pce_quad_func quad_func(setup.v_st, *setup.st_basis);
179  setup.exp->unary_op(quad_func, v2, setup.u);
180  success = comparePCEs(setup.v, "v", v2, "v2", setup.rtol, setup.atol, out);
181  }
182 
183  // Tests mean computed from Stieltjes basis is same as original
184  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, CosMean ) {
185  success = Teuchos::testRelErr("v.mean()", setup.v.mean(),
186  "v_st.mean()", setup.v_st.mean(),
187  "rtol", setup.rtol,
188  "rtol", setup.rtol,
189  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
190 
191  }
192 
193  // Tests mean standard deviation from Stieltjes basis is same as original
194  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, CosStandardDeviation ) {
195  success = Teuchos::testRelErr("v.standard_deviation()",
196  setup.v.standard_deviation(),
197  "v_st.standard_devaition()",
198  setup.v_st.standard_deviation(),
199  "rtol", 1e-1,
200  "rtol", 1e-1,
201  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
202  }
203 
204 }
205 
206 //
207 // Stieltjes tests based on expansion of u = sin(x) where x is a U([-1,1])
208 // random variable
209 //
210 namespace StieltjesSinTest {
211 
212  template <typename Ordinal_Type, typename Value_Type>
214  typedef Ordinal_Type OrdinalType;
215  typedef Value_Type ValueType;
216  static const bool is_even = false;
217  void
221  exp.sin(u,x);
222  }
223  };
225 
226  // Tests mapping from Stieltjes basis to original is correct
227  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, SinMap ) {
229  setup.st_1d_basis->transformCoeffsFromStieltjes(setup.u_st.coeff(),
230  u2.coeff());
231  success = Stokhos::comparePCEs(setup.u, "u", u2, "u2", setup.rtol, setup.atol, out);
232  }
233 
234  // Tests Stieltjes basis is orthogonal
235  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, SinOrthog ) {
236  const Teuchos::Array<double>& norms = setup.st_1d_basis->norm_squared();
237  const Teuchos::Array<double>& weights = setup.st_quad->getQuadWeights();
238  const Teuchos::Array< Teuchos::Array<double> >& values =
239  setup.st_quad->getBasisAtQuadPoints();
241  setup.st_sz);
242  for (int i=0; i<setup.st_sz; i++) {
243  for (int j=0; j<setup.st_sz; j++) {
244  for (unsigned int k=0; k<weights.size(); k++)
245  mat(i,j) += weights[k]*values[k][i]*values[k][j];
246  mat(i,j) /= std::sqrt(norms[i]*norms[j]);
247  }
248  mat(i,i) -= 1.0;
249  }
250  success = mat.normInf() < setup.atol;
251  if (!success) {
252  out << "\n Error, mat.normInf() < atol = " << mat.normInf()
253  << " < " << setup.atol << ": failed!\n";
254  out << "mat = " << printMat(mat) << std::endl;
255  }
256  }
257 
258  // Tests PCE computed from Stieltjes basis is same as original
259  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, SinPCE ) {
261  stieltjes_pce_quad_func quad_func(setup.v_st, *setup.st_basis);
262  setup.exp->unary_op(quad_func, v2, setup.u);
263  success = comparePCEs(setup.v, "v", v2, "v2", setup.rtol, setup.atol, out);
264  }
265 
266  // Tests mean computed from Stieltjes basis is same as original
267  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, SinMean ) {
268  success = Teuchos::testRelErr("v.mean()", setup.v.mean(),
269  "v_st.mean()", setup.v_st.mean(),
270  "rtol", setup.rtol,
271  "rtol", setup.rtol,
272  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
273  }
274 
275  // Tests mean standard deviation from Stieltjes basis is same as original
276  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, SinStandardDeviation ) {
277  success = Teuchos::testRelErr("v.standard_deviation()",
278  setup.v.standard_deviation(),
279  "v_st.standard_devaition()",
280  setup.v_st.standard_deviation(),
281  "rtol", 1e-1,
282  "rtol", 1e-1,
283  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
284  }
285 
286 }
287 
288 //
289 // Stieltjes tests based on expansion of u = exp(x) where x is a U([-1,1])
290 // random variable. For this test we don't use the PCE quad points and
291 // instead use those generated for the Stieltjes basis
292 //
293 namespace StieltjesExpTest {
294 
295  template <typename Ordinal_Type, typename Value_Type>
297  typedef Ordinal_Type OrdinalType;
298  typedef Value_Type ValueType;
299  static const bool is_even = false;
300  void
304  exp.exp(u,x);
305  }
306  };
308 
309  // Tests mapping from Stieltjes basis to original is correct
310  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, ExpMap ) {
312  setup.st_1d_basis->transformCoeffsFromStieltjes(setup.u_st.coeff(),
313  u2.coeff());
314  success = Stokhos::comparePCEs(setup.u, "u", u2, "u2", setup.rtol, setup.atol, out);
315  }
316 
317  // Tests Stieltjes basis is orthogonal
318  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, ExpOrthog ) {
319  const Teuchos::Array<double>& norms = setup.st_1d_basis->norm_squared();
320  const Teuchos::Array<double>& weights = setup.st_quad->getQuadWeights();
321  const Teuchos::Array< Teuchos::Array<double> >& values =
322  setup.st_quad->getBasisAtQuadPoints();
324  setup.st_sz);
325  for (int i=0; i<setup.st_sz; i++) {
326  for (int j=0; j<setup.st_sz; j++) {
327  for (unsigned int k=0; k<weights.size(); k++)
328  mat(i,j) += weights[k]*values[k][i]*values[k][j];
329  mat(i,j) /= std::sqrt(norms[i]*norms[j]);
330  }
331  mat(i,i) -= 1.0;
332  }
333  success = mat.normInf() < setup.atol;
334  if (!success) {
335  out << "\n Error, mat.normInf() < atol = " << mat.normInf()
336  << " < " << setup.atol << ": failed!\n";
337  out << "mat = " << printMat(mat) << std::endl;
338  }
339  }
340 
341  // Tests PCE computed from Stieltjes basis is same as original
342  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, ExpPCE ) {
344  stieltjes_pce_quad_func quad_func(setup.v_st, *setup.st_basis);
345  setup.exp->unary_op(quad_func, v2, setup.u);
346  success = comparePCEs(setup.v, "v", v2, "v2", setup.rtol, setup.atol, out);
347  }
348 
349  // Tests mean computed from Stieltjes basis is same as original
350  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, ExpMean ) {
351  success = Teuchos::testRelErr("v.mean()", setup.v.mean(),
352  "v_st.mean()", setup.v_st.mean(),
353  "rtol", setup.rtol,
354  "rtol", setup.rtol,
355  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
356  }
357 
358  // Tests mean standard deviation from Stieltjes basis is same as original
359  TEUCHOS_UNIT_TEST( Stokhos_StieltjesPCEBasis, ExpStandardDeviation ) {
360  success = Teuchos::testRelErr("v.standard_deviation()",
361  setup.v.standard_deviation(),
362  "v_st.standard_devaition()",
363  setup.v_st.standard_deviation(),
364  "rtol", 1e-1,
365  "rtol", 1e-1,
366  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
367  }
368 
369 }
370 
371 int main( int argc, char* argv[] ) {
372  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
374 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > st_quad
void sin(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
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)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
TEUCHOS_UNIT_TEST(Stokhos_StieltjesPCEBasis, ExpMap)
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
void exp(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
Stieltjes_PCE_Setup< Stieltjes_Sin_Func< int, double > > setup(true)
void cos(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
bool comparePCEs(const PCEType &a1, const std::string &a1_name, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
pointer coeff()
Return coefficient array.
void eval(Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > &exp, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &x, Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &u)
Stieltjes_PCE_Setup< Stieltjes_Cos_Func< int, double > > setup(true)
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.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
Stieltjes_PCE_Setup(bool use_pce_quad_points_)
double operator()(const double &a) const
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u_st
TEUCHOS_UNIT_TEST(Stokhos_StieltjesPCEBasis, CosMap)
bool testRelErr(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > st_basis
value_type mean() const
Compute mean of expansion.
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u
int main(int argc, char **argv)
ScalarTraits< ScalarType >::magnitudeType normInf() const
Teuchos::Array< double > vec
Teuchos::RCP< const Stokhos::StieltjesPCEBasis< OrdinalType, ValueType > > st_1d_basis
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > > exp
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v_st
const Stokhos::OrthogPolyBasis< int, double > & basis
size_type size() const
stieltjes_pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
virtual ordinal_type size() const
Return total size of basis.
TEUCHOS_UNIT_TEST(Stokhos_StieltjesPCEBasis, SinMap)
const Stokhos::OrthogPolyApprox< int, double > & pce
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
Stieltjes_PCE_Setup< Stieltjes_Exp_Func< int, double > > setup(false)
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)
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.