Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stieltjes_example2.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"
16 
17 //typedef Stokhos::LegendreBasis<int,double> basis_type;
19 
20 struct pce_quad_func {
24  pce(pce_), basis(basis_), vec(2) {}
25 
26  double operator() (const double& a, const double& b) const {
27  vec[0] = a;
28  vec[1] = b;
29  return pce.evaluate(vec);
30  }
34 };
35 
36 struct sin_func {
38  pce(pce_) {}
39  double operator() (const Teuchos::Array<double>& x) const {
40  return std::sin(pce.evaluate(x));
41  }
43 };
44 
45 struct exp_func {
47  pce(pce_) {}
48  double operator() (const Teuchos::Array<double>& x) const {
49  return 1.0 + std::exp(pce.evaluate(x));
50  }
52 };
53 
54 double rel_err(double a, double b) {
55  return std::abs(a-b)/std::abs(b);
56 }
57 
58 int main(int argc, char **argv)
59 {
60  try {
61 
62  const unsigned int d = 2;
63  const unsigned int pmin = 1;
64  const unsigned int pmax = 10;
65  const unsigned int np = pmax-pmin+1;
66  bool use_pce_quad_points = false;
67  bool normalize = false;
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  Teuchos::Array<double> eval_pt(d, 0.5);
77  double pt_true;
78 
79  // Loop over orders
80  unsigned int n = 0;
81  for (unsigned int p=pmin; p<=pmax; p++) {
82 
83  std::cout << "p = " << p << std::endl;
84 
85  // Create product basis
86  for (unsigned int i=0; i<d; i++)
87  bases[i] = Teuchos::rcp(new basis_type(p));
90 
91  // Create approximation
92  Stokhos::OrthogPolyApprox<int,double> x(basis), u(basis), v(basis),
93  w(basis), w2(basis);
94  for (unsigned int i=0; i<d; i++) {
95  x.term(i, 1) = 1.0;
96  }
97 
98  double x_pt = x.evaluate(eval_pt);
99  pt_true = std::sin(x_pt)/(1.0 + exp(x_pt));
100 
101  // Quadrature
103 #ifdef HAVE_STOKHOS_DAKOTA
104  if (sparse_grid)
105  quad =
106  Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(basis, p));
107 #endif
108  if (!sparse_grid)
109  quad =
111 
112  // Triple product tensor
114  basis->computeTripleProductTensor();
115 
116  // Quadrature expansion
117  Stokhos::QuadOrthogPolyExpansion<int,double> quad_exp(basis, Cijk, quad);
118 
119  // Compute PCE via quadrature expansion
120  quad_exp.sin(u,x);
121  quad_exp.exp(v,x);
122  quad_exp.plusEqual(v, 1.0);
123  quad_exp.divide(w,u,v);
124 
125  // Compute Stieltjes basis
129  st_bases[0] =
131  p, sf, quad, use_pce_quad_points, normalize));
132  st_bases[1] =
134  p, ef, quad, use_pce_quad_points, normalize));
136  st_basis =
138  //std::cout << *st_basis << std::endl;
139 
140  Stokhos::OrthogPolyApprox<int,double> u_st(st_basis), v_st(st_basis),
141  w_st(st_basis);
142  u_st.term(0, 0) = u.mean();
143  u_st.term(0, 1) = 1.0;
144  v_st.term(0, 0) = v.mean();
145  v_st.term(1, 1) = 1.0;
146 
147  // Triple product tensor
149  st_basis->computeTripleProductTensor();
150 
151  // Tensor product quadrature
153  if (!use_pce_quad_points) {
154 #ifdef HAVE_STOKHOS_DAKOTA
155  if (sparse_grid)
156  st_quad = Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(st_basis, p));
157 #endif
158  if (!sparse_grid)
160  }
161  else {
162  Teuchos::Array<double> st_points_0;
163  Teuchos::Array<double> st_weights_0;
165  st_bases[0]->getQuadPoints(p+1, st_points_0, st_weights_0, st_values_0);
166  Teuchos::Array<double> st_points_1;
167  Teuchos::Array<double> st_weights_1;
169  st_bases[1]->getQuadPoints(p+1, st_points_1, st_weights_1, st_values_1);
172  for (int i=0; i<st_points_0.size(); i++) {
173  (*st_points)[i].resize(2);
174  (*st_points)[i][0] = st_points_0[i];
175  (*st_points)[i][1] = st_points_1[i];
176  }
178  Teuchos::rcp(new Teuchos::Array<double>(st_weights_0));
180  st_basis;
181  st_quad =
183  st_points,
184  st_weights));
185  }
186 
187  // Quadrature expansion
188  Stokhos::QuadOrthogPolyExpansion<int,double> st_quad_exp(st_basis,
189  st_Cijk,
190  st_quad);
191 
192  // Compute w_st = u_st*v_st in Stieltjes basis
193  st_quad_exp.divide(w_st, u_st, v_st);
194 
195  // Project w_st back to original basis
196  pce_quad_func st_func(w_st, *st_basis);
197  quad_exp.binary_op(st_func, w2, u, v);
198 
199  // std::cout.precision(12);
200  // std::cout << w;
201  // std::cout << w2;
202  // std::cout << w_st;
203  mean[n] = w.mean();
204  mean_st[n] = w2.mean();
205  std_dev[n] = w.standard_deviation();
206  std_dev_st[n] = w2.standard_deviation();
207  pt[n] = w.evaluate(eval_pt);
208  pt_st[n] = w2.evaluate(eval_pt);
209  n++;
210  }
211 
212  n = 0;
213  int wi=10;
214  std::cout << "Statistical error:" << std::endl;
215  std::cout << "p "
216  << std::setw(wi) << "mean" << " "
217  << std::setw(wi) << "mean_st" << " "
218  << std::setw(wi) << "std_dev" << " "
219  << std::setw(wi) << "std_dev_st" << " "
220  << std::setw(wi) << "point" << " "
221  << std::setw(wi) << "point_st" << std::endl;
222  for (unsigned int p=pmin; p<pmax; p++) {
223  std::cout.precision(3);
224  std::cout.setf(std::ios::scientific);
225  std::cout << p << " "
226  << std::setw(wi) << rel_err(mean[n], mean[np-1]) << " "
227  << std::setw(wi) << rel_err(mean_st[n], mean[np-1]) << " "
228  << std::setw(wi) << rel_err(std_dev[n], std_dev[np-1]) << " "
229  << std::setw(wi) << rel_err(std_dev_st[n], std_dev[np-1])
230  << " "
231  << std::setw(wi) << rel_err(pt[n], pt_true) << " "
232  << std::setw(wi) << rel_err(pt_st[n], pt_true)
233  << std::endl;
234  n++;
235  }
236 
237  }
238  catch (std::exception& e) {
239  std::cout << e.what() << std::endl;
240  }
241 }
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.
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
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_)
double operator()(const Teuchos::Array< double > &x) const
void plusEqual(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const value_type &x)
Teuchos::Array< double > vec
Stokhos::LegendreBasis< int, double > basis_type
exp_func(const Stokhos::OrthogPolyApprox< int, double > &pce_)
Generates three-term recurrence using the Discretized Stieltjes procedure applied to a functional map...
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)
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
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...