Debian Codegraph @

packages/stokhos/example/uq_handbook/pce_example.cpp

raw
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
36 typedef Stokhos::StandardStorage<int,double> storage_type;
37 typedef Sacado::PCE::OrthogPoly<double, storage_type> pce_type;
38
39 // Short-hand for several classes used below
40 using Teuchos::Array;
41 using Teuchos::RCP;
42 using Teuchos::rcp;
43 using Stokhos::OneDOrthogPolyBasis;
44 using Stokhos::HermiteBasis;
45 using Stokhos::CompletePolynomialBasis;
46 using Stokhos::Quadrature;
47 using Stokhos::TensorProductQuadrature;
48 using Stokhos::Sparse3Tensor;
49 using Stokhos::QuadOrthogPolyExpansion;
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
96 Teuchos::Array<double> pt(d);
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 }