Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stieltjes_example4.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 
20 
21 template <int Num>
22 struct s_quad_func {
23  static const int N = Num;
24  double a;
25 
26  s_quad_func(double a_) : a(a_) {}
27 
28  double operator() (double x[Num]) const {
29  double prod = 1;
30  for (int i=0; i<Num; i++)
31  prod *= x[i];
32  return 1.0 / (prod - a);
33  }
34 };
35 
36 struct pce_quad_func {
40  pce(pce_), basis(basis_), vec(1) {}
41 
42  double operator() (const double& a) const {
43  vec[0] = a;
44  return pce.evaluate(vec);
45  }
49 };
50 
51 double rel_err(double a, double b) {
52  return std::abs(a-b)/std::abs(b);
53 }
54 
55 int main(int argc, char **argv)
56 {
57  try {
58 
59  const int d = 5;
60  const int pmin = 1;
61  const int pmax = 10;
62  const int np = pmax-pmin+1;
63  const double a = 1.5;
64  bool use_pce_quad_points = false;
65  bool normalize = false;
66  bool project_integrals = false;
67  bool lanczos = true;
68  bool sparse_grid = true;
69 #ifndef HAVE_STOKHOS_DAKOTA
70  sparse_grid = false;
71 #endif
72  Teuchos::Array<double> mean(np), mean_st(np), std_dev(np), std_dev_st(np);
73  Teuchos::Array<double> pt(np), pt_st(np);
74 
76 
77  Teuchos::Array<double> eval_pt(d, 0.56789);
78  double pt_true;
79 
80  // Loop over orders
81  int n = 0;
82  for (int p=pmin; p<=pmax; p++) {
83 
84  std::cout << "p = " << p << std::endl;
85 
86  // Create product basis
87  for (int i=0; i<d; i++)
88  bases[i] = Teuchos::rcp(new basis_type(p));
91 
92  // Create approximation
93  Stokhos::OrthogPolyApprox<int,double> s(basis), t1(basis), t2(basis);
95  for (int i=0; i<d; i++) {
96  xi[i] = new Stokhos::OrthogPolyApprox<int,double>(basis);
97  xi[i]->term(i, 1) = 1.0;
98  }
99 
100  // Quadrature
102 #ifdef HAVE_STOKHOS_DAKOTA
103  if (sparse_grid)
104  quad =
105  Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(basis, p));
106 #endif
107  if (!sparse_grid)
108  quad =
110 
111  // Triple product tensor
113  basis->computeTripleProductTensor();
114 
115  // Quadrature expansion
116  Stokhos::QuadOrthogPolyExpansion<int,double> quad_exp(basis, Cijk, quad);
117 
118  // Compute PCE via quadrature expansion
119  s_quad_func<d> s_func(a);
122  for (int i=0; i<d; i++)
123  xip[i] = xi[i];
124  quad_exp.nary_op(s_func,s,xip);
125  quad_exp.divide(t1,1.0,s);
126  delete [] xip;
127 
128  // compute true point
130  for (int i=0; i<d; i++)
131  xx[i] = xi[i]->evaluate(eval_pt);
132  pt_true = s_func(&(xx[0]));
133  pt_true = 1.0/pt_true;
134 
135  // Compute Stieltjes basis
139  if (lanczos) {
140  if (project_integrals) {
141  stp_basis_s =
143  p, Teuchos::rcp(&s,false), Cijk, normalize, true));
144  st_bases[0] = stp_basis_s;
145  }
146  else {
147  st_basis_s =
149  p, Teuchos::rcp(&s,false), quad, normalize, true));
150  st_bases[0] = st_basis_s;
151  }
152  }
153  else {
154  st_bases[0] =
156  p, Teuchos::rcp(&s,false), quad, use_pce_quad_points,
157  normalize, project_integrals, Cijk));
158  }
159 
161  st_basis =
163  //std::cout << *st_basis << std::endl;
164 
165  Stokhos::OrthogPolyApprox<int,double> s_st(st_basis), t_st(st_basis);
166  if (lanczos) {
167  if (project_integrals) {
168  s_st.term(0, 0) = stp_basis_s->getNewCoeffs(0);
169  s_st.term(0, 1) = stp_basis_s->getNewCoeffs(1);
170  }
171  else {
172  s_st.term(0, 0) = st_basis_s->getNewCoeffs(0);
173  s_st.term(0, 1) = st_basis_s->getNewCoeffs(1);
174  }
175  }
176  else {
177  s_st.term(0, 0) = s.mean();
178  s_st.term(0, 1) = 1.0;
179  }
180 
181  // Triple product tensor
183  st_basis->computeTripleProductTensor();
184 
185  // Tensor product quadrature
187 #ifdef HAVE_STOKHOS_DAKOTA
188  if (sparse_grid)
189  st_quad = Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(st_basis, p));
190 #endif
191  if (!sparse_grid)
193 
194  // Quadrature expansion
195  Stokhos::QuadOrthogPolyExpansion<int,double> st_quad_exp(st_basis,
196  st_Cijk,
197  st_quad);
198 
199  // Compute t_st = 1/s_st in Stieltjes basis
200  st_quad_exp.divide(t_st, 1.0, s_st);
201 
202  // Project t_st back to original basis
203  pce_quad_func st_func(t_st, *st_basis);
204  quad_exp.unary_op(st_func, t2, s);
205 
206  // std::cout.precision(12);
207  // std::cout << w;
208  // std::cout << w2;
209  // std::cout << w_st;
210  mean[n] = t1.mean();
211  mean_st[n] = t2.mean();
212  std_dev[n] = t1.standard_deviation();
213  std_dev_st[n] = t2.standard_deviation();
214  pt[n] = t1.evaluate(eval_pt);
215  pt_st[n] = t2.evaluate(eval_pt);
216  n++;
217 
218  for (int i=0; i<d; i++)
219  delete xi[i];
220  }
221 
222  n = 0;
223  int wi=10;
224  std::cout << "Statistical error:" << std::endl;
225  std::cout << "p "
226  << std::setw(wi) << "mean" << " "
227  << std::setw(wi) << "mean_st" << " "
228  << std::setw(wi) << "std_dev" << " "
229  << std::setw(wi) << "std_dev_st" << " "
230  << std::setw(wi) << "point" << " "
231  << std::setw(wi) << "point_st" << std::endl;
232  for (int p=pmin; p<pmax; p++) {
233  std::cout.precision(3);
234  std::cout.setf(std::ios::scientific);
235  std::cout << p << " "
236  << std::setw(wi) << rel_err(mean[n], mean[np-1]) << " "
237  << std::setw(wi) << rel_err(mean_st[n], mean[np-1]) << " "
238  << std::setw(wi) << rel_err(std_dev[n], std_dev[np-1]) << " "
239  << std::setw(wi) << rel_err(std_dev_st[n], std_dev[np-1])
240  << " "
241  << std::setw(wi) << rel_err(pt[n], pt_true) << " "
242  << std::setw(wi) << rel_err(pt_st[n], pt_true)
243  << std::endl;
244  n++;
245  }
246 
247  }
248  catch (std::exception& e) {
249  std::cout << e.what() << std::endl;
250  }
251 }
Generates three-term recurrence using the Lanczos procedure applied to a polynomial chaos expansion i...
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
static const int N
s_quad_func(double a_)
pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
double operator()(double x[Num]) const
Teuchos::Array< double > vec
Stokhos::LegendreBasis< int, double > basis_type
void divide(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)
Generates three-term recurrence using the Discretized Stieltjes procedure applied to a polynomial cha...
value_type standard_deviation() const
Compute standard deviation of expansion.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const Stokhos::OrthogPolyBasis< int, double > & basis
void nary_op(const FuncT &func, OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > **a)
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.
Legendre polynomial basis.
int main(int argc, char **argv)
double rel_err(double a, double b)
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.