Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pce_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 // pce_example
11 //
12 // usage:
13 // pce_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.1*x_1 + 0.05*x_2 + 0.01*x_3 x1,x2,x3 are zero-mean
21 // unit-variance Gaussian random variables.
22 
23 #include "Stokhos_Sacado.hpp"
24 
25 // The function to compute the polynomial chaos expansion of,
26 // written as a template function
27 template <class ScalarType>
28 ScalarType simple_function(const ScalarType& u) {
29  ScalarType z = std::log(u);
30  return 1.0/(z*z + 1.0);
31 }
32 
33 int main(int argc, char **argv)
34 {
35  // Typename of Polynomial Chaos scalar type
38 
39  // Short-hand for several classes used below
40  using Teuchos::Array;
41  using Teuchos::RCP;
42  using Teuchos::rcp;
46  using Stokhos::Quadrature;
50 
51  try {
52 
53  // Basis of dimension 3, order 4
54  const int d = 3;
55  const int p = 4;
56  Array< RCP<const OneDOrthogPolyBasis<int,double> > > bases(d);
57  for (int i=0; i<d; i++) {
58  bases[i] = rcp(new HermiteBasis<int,double>(p));
59  }
60  RCP<const CompletePolynomialBasis<int,double> > basis =
61  rcp(new CompletePolynomialBasis<int,double>(bases));
62 
63  // Quadrature method
64  RCP<const Quadrature<int,double> > quad =
65  rcp(new TensorProductQuadrature<int,double>(basis));
66 
67  // Triple product tensor
68  RCP<Sparse3Tensor<int,double> > Cijk =
69  basis->computeTripleProductTensor();
70 
71  // Expansion method
72  RCP<QuadOrthogPolyExpansion<int,double> > expn =
73  rcp(new QuadOrthogPolyExpansion<int,double>(basis, Cijk, quad));
74 
75  // Polynomial expansion of u
76  pce_type u(expn);
77  u.term(0,0) = 1.0; // zeroth order term
78  u.term(0,1) = 0.1; // first order term for dimension 0
79  u.term(1,1) = 0.05; // first order term for dimension 1
80  u.term(2,1) = 0.01; // first order term for dimension 2
81 
82  // Compute PCE expansion of function
83  pce_type v = simple_function(u);
84 
85  // Print u and v
86  std::cout << "\tu = ";
87  u.print(std::cout);
88  std::cout << "\tv = ";
89  v.print(std::cout);
90 
91  // Compute moments
92  double mean = v.mean();
93  double std_dev = v.standard_deviation();
94 
95  // Evaluate PCE and function at a point = 0.25 in each dimension
97  for (int i=0; i<d; i++)
98  pt[i] = 0.25;
99  double up = u.evaluate(pt);
100  double vp = simple_function(up);
101  double vp2 = v.evaluate(pt);
102 
103  // Print results
104  std::cout << "\tv mean = " << mean << std::endl;
105  std::cout << "\tv std. dev. = " << std_dev << std::endl;
106  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
107  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
108  }
109  catch (std::exception& e) {
110  std::cout << e.what() << std::endl;
111  }
112 }
ScalarType simple_function(const ScalarType &u)
Stokhos::StandardStorage< int, double > storage_type
Hermite polynomial basis.
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class for quadrature methods.
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
int main(int argc, char **argv)
Abstract base class for 1-D orthogonal polynomials.
Orthogonal polynomial expansions based on numerical quadrature.
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...