Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pecos_hermite_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 // pecos_hermite_example
11 //
12 // usage:
13 // pecos_hermite_example
14 //
15 // output:
16 // prints the Hermite Polynomial Chaos Expansion of the simple function
17 //
18 // v = 1/(log(u)^2+1)
19 //
20 // where u = 1 + 0.4*H_1(x) + 0.06*H_2(x) + 0.002*H_3(x), x is a zero-mean
21 // and unit-variance Gaussian random variable, and H_i(x) is the i-th
22 // Hermite polynomial.
23 //
24 // Same as hermite_example, except uses Pecos to define the Hermite basis.
25 
26 #include "Stokhos.hpp"
27 #include "HermiteOrthogPolynomial.hpp" // from Pecos
28 
29 int main(int argc, char **argv)
30 {
31  try {
32 
33  // Basis of dimension 3, order 5
34  const int d = 3;
35  const int p = 5;
37  for (int i=0; i<d; i++) {
38  bases[i] = Teuchos::rcp(new Stokhos::PecosOneDOrthogPolyBasis<int,double>(Teuchos::rcp(new Pecos::HermiteOrthogPolynomial), "Hermite", p));
39  }
42 
43  // Quadrature method
46 
47  // Triple product tensor
49  basis->computeTripleProductTensor();
50 
51  // Expansion method
52  Stokhos::QuadOrthogPolyExpansion<int,double> expn(basis, Cijk, quad);
53 
54  // Polynomial expansions
55  Stokhos::OrthogPolyApprox<int,double> u(basis), v(basis), w(basis);
56  u.term(0,0) = 1.0;
57  for (int i=0; i<d; i++) {
58  u.term(i,1) = 0.4 / d;
59  u.term(i,2) = 0.06 / d;
60  u.term(i,3) = 0.002 / d;
61  }
62 
63  // Compute expansion
64  expn.log(v,u);
65  expn.times(w,v,v);
66  expn.plusEqual(w,1.0);
67  expn.divide(v,1.0,w);
68  //expn.times(v,u,u);
69 
70  // Print u and v
71  std::cout << "v = 1.0 / (log(u)^2 + 1):" << std::endl;
72  std::cout << "\tu = ";
73  u.print(std::cout);
74  std::cout << "\tv = ";
75  v.print(std::cout);
76 
77  // Compute moments
78  double mean = v.mean();
79  double std_dev = v.standard_deviation();
80 
81  // Evaluate PCE and function at a point = 0.25 in each dimension
83  for (int i=0; i<d; i++)
84  pt[i] = 0.25;
85  double up = u.evaluate(pt);
86  double vp = 1.0/(std::log(up)*std::log(up)+1.0);
87  double vp2 = v.evaluate(pt);
88 
89  // Print results
90  std::cout << "\tv mean = " << mean << std::endl;
91  std::cout << "\tv std. dev. = " << std_dev << std::endl;
92  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
93  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
94 
95  // Check the answer
96  if (std::abs(vp - vp2) < 1e-2)
97  std::cout << "\nExample Passed!" << std::endl;
98  }
99  catch (std::exception& e) {
100  std::cout << e.what() << std::endl;
101  }
102 }
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)
void plusEqual(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const value_type &x)
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)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
void log(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
int main(int argc, char **argv)
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...