Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stieltjes_example.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 struct pce_quad_func {
25  pce(pce_), basis(basis_), vec(2) {}
26 
27  double operator() (const double& a, const double& b) const {
28  vec[0] = a;
29  vec[1] = b;
30  return pce.evaluate(vec);
31  }
35 };
36 
37 double rel_err(double a, double b) {
38  return std::abs(a-b)/std::abs(b);
39 }
40 
41 int main(int argc, char **argv)
42 {
43  try {
44 
45  const unsigned int d = 2;
46  const unsigned int pmin = 1;
47  const unsigned int pmax = 15;
48  const unsigned int np = pmax-pmin+1;
49  bool use_pce_quad_points = false;
50  bool normalize = false;
51  bool project_integrals = false;
52  bool lanczos = false;
53  bool sparse_grid = true;
54 #ifndef HAVE_STOKHOS_DAKOTA
55  sparse_grid = false;
56 #endif
57  Teuchos::Array<double> mean(np), mean_st(np), std_dev(np), std_dev_st(np);
58  Teuchos::Array<double> pt(np), pt_st(np);
59 
61  Teuchos::Array<double> eval_pt(d, 0.5);
62  double pt_true;
63 
64  // Loop over orders
65  unsigned int n = 0;
66  for (unsigned int p=pmin; p<=pmax; p++) {
67 
68  std::cout << "p = " << p << std::endl;
69 
70  // Create product basis
71  for (unsigned int i=0; i<d; i++)
72  bases[i] = Teuchos::rcp(new basis_type(p));
75 
76  // Create approximation
77  Stokhos::OrthogPolyApprox<int,double> x(basis), u(basis), v(basis),
78  w(basis), w2(basis);
79  for (unsigned int i=0; i<d; i++) {
80  x.term(i, 1) = 1.0;
81  }
82 
83  double x_pt = x.evaluate(eval_pt);
84  pt_true = std::sin(x_pt)/(1.0 + exp(x_pt));
85 
86  // Quadrature
88 #ifdef HAVE_STOKHOS_DAKOTA
89  if (sparse_grid)
90  quad =
91  Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(basis, p));
92 #endif
93  if (!sparse_grid)
94  quad =
96 
97  // Triple product tensor
99  basis->computeTripleProductTensor();
100 
101  // Quadrature expansion
102  Stokhos::QuadOrthogPolyExpansion<int,double> quad_exp(basis, Cijk, quad);
103 
104  // Compute PCE via quadrature expansion
105  quad_exp.sin(u,x);
106  quad_exp.exp(v,x);
107  quad_exp.plusEqual(v, 1.0);
108  quad_exp.divide(w,u,v);
109 
110  // Compute Stieltjes basis
114  if (lanczos) {
115  if (project_integrals) {
116  stp_basis_u =
118  p, Teuchos::rcp(&u,false), Cijk, normalize, true));
119  stp_basis_v =
121  p, Teuchos::rcp(&v,false), Cijk, normalize, true));
122  st_bases[0] = stp_basis_u;
123  st_bases[1] = stp_basis_v;
124  }
125  else {
126  st_basis_u =
128  p, Teuchos::rcp(&u,false), quad, normalize, true));
129  st_basis_v =
131  p, Teuchos::rcp(&v,false), quad, normalize, true));
132  st_bases[0] = st_basis_u;
133  st_bases[1] = st_basis_v;
134  }
135  }
136  else {
137  st_bases[0] =
139  p, Teuchos::rcp(&u,false), quad, use_pce_quad_points,
140  normalize, project_integrals, Cijk));
141  st_bases[1] =
143  p, Teuchos::rcp(&v,false), quad, use_pce_quad_points,
144  normalize, project_integrals, Cijk));
145  }
146 
148  st_basis =
150  //std::cout << *st_basis << std::endl;
151 
152  Stokhos::OrthogPolyApprox<int,double> u_st(st_basis), v_st(st_basis),
153  w_st(st_basis);
154  if (lanczos) {
155  if (project_integrals) {
156  u_st.term(0, 0) = stp_basis_u->getNewCoeffs(0);
157  u_st.term(0, 1) = stp_basis_u->getNewCoeffs(1);
158  v_st.term(0, 0) = stp_basis_v->getNewCoeffs(0);
159  v_st.term(1, 1) = stp_basis_v->getNewCoeffs(1);
160  }
161  else {
162  u_st.term(0, 0) = st_basis_u->getNewCoeffs(0);
163  u_st.term(0, 1) = st_basis_u->getNewCoeffs(1);
164  v_st.term(0, 0) = st_basis_v->getNewCoeffs(0);
165  v_st.term(1, 1) = st_basis_v->getNewCoeffs(1);
166  }
167  }
168  else {
169  u_st.term(0, 0) = u.mean();
170  u_st.term(0, 1) = 1.0;
171  v_st.term(0, 0) = v.mean();
172  v_st.term(1, 1) = 1.0;
173  }
174 
175  // Triple product tensor
177  st_basis->computeTripleProductTensor();
178 
179  // Tensor product quadrature
181  if (!use_pce_quad_points) {
182 #ifdef HAVE_STOKHOS_DAKOTA
183  if (sparse_grid)
184  st_quad = Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(st_basis, p));
185 #endif
186  if (!sparse_grid)
188  }
189  else {
190  Teuchos::Array<double> st_points_0;
191  Teuchos::Array<double> st_weights_0;
193  st_bases[0]->getQuadPoints(p+1, st_points_0, st_weights_0, st_values_0);
194  Teuchos::Array<double> st_points_1;
195  Teuchos::Array<double> st_weights_1;
197  st_bases[1]->getQuadPoints(p+1, st_points_1, st_weights_1, st_values_1);
200  for (int i=0; i<st_points_0.size(); i++) {
201  (*st_points)[i].resize(2);
202  (*st_points)[i][0] = st_points_0[i];
203  (*st_points)[i][1] = st_points_1[i];
204  }
206  Teuchos::rcp(new Teuchos::Array<double>(st_weights_0));
208  st_basis;
209  st_quad =
211  st_points,
212  st_weights));
213  }
214 
215  // Quadrature expansion
216  Stokhos::QuadOrthogPolyExpansion<int,double> st_quad_exp(st_basis,
217  st_Cijk,
218  st_quad);
219 
220  // Compute w_st = u_st*v_st in Stieltjes basis
221  st_quad_exp.divide(w_st, u_st, v_st);
222 
223  // Project w_st back to original basis
224  pce_quad_func st_func(w_st, *st_basis);
225  quad_exp.binary_op(st_func, w2, u, v);
226 
227  // std::cout.precision(12);
228  // std::cout << w;
229  // std::cout << w2;
230  // std::cout << w_st;
231  mean[n] = w.mean();
232  mean_st[n] = w2.mean();
233  std_dev[n] = w.standard_deviation();
234  std_dev_st[n] = w2.standard_deviation();
235  pt[n] = w.evaluate(eval_pt);
236  pt_st[n] = w2.evaluate(eval_pt);
237  n++;
238  }
239 
240  n = 0;
241  int wi=10;
242  std::cout << "Statistical error:" << std::endl;
243  std::cout << "p "
244  << std::setw(wi) << "mean" << " "
245  << std::setw(wi) << "mean_st" << " "
246  << std::setw(wi) << "std_dev" << " "
247  << std::setw(wi) << "std_dev_st" << " "
248  << std::setw(wi) << "point" << " "
249  << std::setw(wi) << "point_st" << std::endl;
250  for (unsigned int p=pmin; p<pmax; p++) {
251  std::cout.precision(3);
252  std::cout.setf(std::ios::scientific);
253  std::cout << p << " "
254  << std::setw(wi) << rel_err(mean[n], mean[np-1]) << " "
255  << std::setw(wi) << rel_err(mean_st[n], mean[np-1]) << " "
256  << std::setw(wi) << rel_err(std_dev[n], std_dev[np-1]) << " "
257  << std::setw(wi) << rel_err(std_dev_st[n], std_dev[np-1])
258  << " "
259  << std::setw(wi) << rel_err(pt[n], pt_true) << " "
260  << std::setw(wi) << rel_err(pt_st[n], pt_true)
261  << std::endl;
262  n++;
263  }
264 
265  }
266  catch (std::exception& e) {
267  std::cout << e.what() << std::endl;
268  }
269 }
void binary_op(const FuncT &func, 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)
Nonlinear binary function.
Generates three-term recurrence using the Lanczos procedure applied to a polynomial chaos expansion i...
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)
pce_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
void plusEqual(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const value_type &x)
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
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)
Legendre polynomial basis.
int main(int argc, char **argv)
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...