Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stieltjes_example3.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 
10 #include <iostream>
11 #include <iomanip>
12 
13 #include "Stokhos.hpp"
18 
19 //typedef Stokhos::LegendreBasis<int,double> basis_type;
21 
22 struct pce_quad_func {
26  pce(pce_), basis(basis_), vec(1) {}
27 
28  double operator() (const double& a) const {
29  vec[0] = a;
30  return pce.evaluate(vec);
31  }
35 };
36 
37 struct sin_func {
39  pce(pce_) {}
40  double operator() (const Teuchos::Array<double>& x) const {
41  return std::sin(pce.evaluate(x));
42  }
44 };
45 
46 double rel_err(double a, double b) {
47  return std::abs(a-b)/std::abs(b);
48 }
49 
50 int main(int argc, char **argv)
51 {
52  try {
53 
54  const unsigned int d = 2;
55  const unsigned int pmin = 2;
56  const unsigned int pmax = 10;
57  const unsigned int np = pmax-pmin+1;
58  bool use_pce_quad_points = false;
59  bool normalize = true;
60  bool sparse_grid = true;
61 #ifndef HAVE_STOKHOS_DAKOTA
62  sparse_grid = false;
63 #endif
64  Teuchos::Array<double> mean(np), mean_st(np), std_dev(np), std_dev_st(np);
65  Teuchos::Array<double> pt(np), pt_st(np);
66 
68  Teuchos::Array<double> eval_pt(d, 0.5);
69  double pt_true;
70 
71  // Loop over orders
72  unsigned int n = 0;
73  for (unsigned int p=pmin; p<=pmax; p++) {
74 
75  std::cout << "p = " << p << std::endl;
76 
77  // Create product basis
78  for (unsigned int i=0; i<d; i++)
79  bases[i] = Teuchos::rcp(new basis_type(p));
82 
83  // Create approximation
84  Stokhos::OrthogPolyApprox<int,double> x(basis), u(basis), v(basis),
85  w(basis), w2(basis);
86  for (unsigned int i=0; i<d; i++) {
87  x.term(i, 1) = 1.0;
88  }
89 
90  double x_pt = x.evaluate(eval_pt);
91  pt_true = std::exp(std::sin(x_pt));
92 
93  // Quadrature
95 #ifdef HAVE_STOKHOS_DAKOTA
96  if (sparse_grid)
97  quad =
98  Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(basis, p));
99 #endif
100  if (!sparse_grid)
101  quad =
103 
104  // Triple product tensor
106  basis->computeTripleProductTensor();
107 
108  // Quadrature expansion
109  Stokhos::QuadOrthogPolyExpansion<int,double> quad_exp(basis, Cijk, quad);
110 
111  // Compute PCE via quadrature expansion
112  quad_exp.sin(u,x);
113  //quad_exp.times(u,u,10.0);
114  quad_exp.exp(w,u);
115 
116  // Compute Stieltjes basis
120  p, Teuchos::rcp(&u,false), Cijk, normalize));
121  st_bases[0] = st_1d_basis;
122 
124  st_basis =
126  //std::cout << *st_basis << std::endl;
127 
128  Stokhos::OrthogPolyApprox<int,double> u_st(st_basis), w_st(st_basis);
129  u_st.term(0, 0) = st_1d_basis->getNewCoeffs(0);
130  u_st.term(0, 1) = st_1d_basis->getNewCoeffs(1);
131 
132  // Triple product tensor
134  st_basis->computeTripleProductTensor();
135 
136  // Tensor product quadrature
138  if (!use_pce_quad_points) {
139 #ifdef HAVE_STOKHOS_DAKOTA
140  if (sparse_grid)
141  st_quad = Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(st_basis, p));
142 #endif
143  if (!sparse_grid)
145  }
146  else {
147  Teuchos::Array<double> st_points_0;
148  Teuchos::Array<double> st_weights_0;
150  st_bases[0]->getQuadPoints(p+1, st_points_0, st_weights_0, st_values_0);
151  Teuchos::Array<double> st_points_1;
152  Teuchos::Array<double> st_weights_1;
154  st_bases[1]->getQuadPoints(p+1, st_points_1, st_weights_1, st_values_1);
157  for (int i=0; i<st_points_0.size(); i++) {
158  (*st_points)[i].resize(2);
159  (*st_points)[i][0] = st_points_0[i];
160  (*st_points)[i][1] = st_points_1[i];
161  }
163  Teuchos::rcp(new Teuchos::Array<double>(st_weights_0));
165  st_basis;
166  st_quad =
168  st_points,
169  st_weights));
170  }
171 
172  // Quadrature expansion
173  Stokhos::QuadOrthogPolyExpansion<int,double> st_quad_exp(st_basis,
174  st_Cijk,
175  st_quad);
176 
177  // Compute w_st = u_st*v_st in Stieltjes basis
178  st_quad_exp.exp(w_st, u_st);
179 
180  // Project w_st back to original basis
181  pce_quad_func st_func(w_st, *st_basis);
182  quad_exp.unary_op(st_func, w2, u);
183 
184  // std::cout.precision(12);
185  // std::cout << w;
186  // std::cout << w2;
187  // std::cout << w_st;
188  mean[n] = w.mean();
189  mean_st[n] = w2.mean();
190  std_dev[n] = w.standard_deviation();
191  std_dev_st[n] = w2.standard_deviation();
192  pt[n] = w.evaluate(eval_pt);
193  pt_st[n] = w2.evaluate(eval_pt);
194  n++;
195  }
196 
197  n = 0;
198  int wi=10;
199  std::cout << "Statistical error:" << std::endl;
200  std::cout << "p "
201  << std::setw(wi) << "mean" << " "
202  << std::setw(wi) << "mean_st" << " "
203  << std::setw(wi) << "std_dev" << " "
204  << std::setw(wi) << "std_dev_st" << " "
205  << std::setw(wi) << "point" << " "
206  << std::setw(wi) << "point_st" << std::endl;
207  for (unsigned int p=pmin; p<pmax; p++) {
208  std::cout.precision(3);
209  std::cout.setf(std::ios::scientific);
210  std::cout << p << " "
211  << std::setw(wi) << rel_err(mean[n], mean[np-1]) << " "
212  << std::setw(wi) << rel_err(mean_st[n], mean[np-1]) << " "
213  << std::setw(wi) << rel_err(std_dev[n], std_dev[np-1]) << " "
214  << std::setw(wi) << rel_err(std_dev_st[n], std_dev[np-1])
215  << " "
216  << std::setw(wi) << rel_err(pt[n], pt_true) << " "
217  << std::setw(wi) << rel_err(pt_st[n], pt_true)
218  << std::endl;
219  n++;
220  }
221 
222  }
223  catch (std::exception& e) {
224  std::cout << e.what() << std::endl;
225  }
226 }
void sin(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
void exp(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
const Stokhos::OrthogPolyApprox< int, double > & pce
sin_func(const Stokhos::OrthogPolyApprox< int, double > &pce_)
pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
Teuchos::Array< double > vec
Stokhos::LegendreBasis< int, double > basis_type
value_type standard_deviation() const
Compute standard deviation of expansion.
double operator()(const Teuchos::Array< double > &x) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const Stokhos::OrthogPolyBasis< int, double > & basis
double operator()(const double &a, const double &b) const
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
const Stokhos::OrthogPolyApprox< int, double > & pce
value_type mean() const
Compute mean of expansion.
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
int main(int argc, char **argv)
Legendre polynomial basis using Clenshaw-Curtis quadrature points.
size_type size() const
double rel_err(double a, double b)
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
int n
Generates three-term recurrence using the Lanczos procedure applied to a polynomial chaos expansion i...
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
void unary_op(const FuncT &func, OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
Nonlinear unary function.