Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // hermite_example
11 //
12 // usage:
13 // 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 #include "Stokhos.hpp"
26 
27 int main(int argc, char **argv)
28 {
29  using Teuchos::Array;
30  using Teuchos::RCP;
31  using Teuchos::rcp;
32 
33  // If applicable, set up MPI.
34  Teuchos::GlobalMPISession mpiSession (&argc, &argv);
35 
36  try {
37 
38  // Basis of dimension 3, order 5
39  const int d = 3;
40  const int p = 5;
41  Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
42  for (int i=0; i<d; i++) {
43  bases[i] = rcp(new Stokhos::HermiteBasis<int,double>(p-i));
44  }
45  RCP<const Stokhos::CompletePolynomialBasis<int,double> > basis =
47 
48  // Quadrature method
49  RCP<const Stokhos::Quadrature<int,double> > quad =
51 
52  // Triple product tensor
53  RCP<Stokhos::Sparse3Tensor<int,double> > Cijk =
54  basis->computeTripleProductTensor();
55 
56  // Expansion method
57  Stokhos::QuadOrthogPolyExpansion<int,double> expn(basis, Cijk, quad);
58 
59  // Polynomial expansions
60  Stokhos::OrthogPolyApprox<int,double> u(basis), v(basis), w(basis);
61  u.term(0,0) = 1.0;
62  for (int i=0; i<d; i++) {
63  if (bases[i]->order() >= 1)
64  u.term(i,1) = 0.4 / d;
65  if (bases[i]->order() >= 2)
66  u.term(i,2) = 0.06 / d;
67  if (bases[i]->order() >= 3)
68  u.term(i,3) = 0.002 / d;
69  }
70 
71  // Compute expansion
72  expn.log(v,u);
73  expn.times(w,v,v);
74  expn.plusEqual(w,1.0);
75  expn.divide(v,1.0,w);
76 
77  // Print u and v
78  std::cout << "v = 1.0 / (log(u)^2 + 1):" << std::endl;
79  std::cout << "\tu = ";
80  u.print(std::cout);
81  std::cout << "\tv = ";
82  v.print(std::cout);
83 
84  // Compute moments
85  double mean = v.mean();
86  double std_dev = v.standard_deviation();
87 
88  // Evaluate PCE and function at a point = 0.25 in each dimension
89  Array<double> pt(d);
90  for (int i=0; i<d; i++)
91  pt[i] = 0.25;
92  double up = u.evaluate(pt);
93  double vp = 1.0/(std::log(up)*std::log(up)+1.0);
94  double vp2 = v.evaluate(pt);
95 
96  // Print results
97  std::cout << "\tv mean = " << mean << std::endl;
98  std::cout << "\tv std. dev. = " << std_dev << std::endl;
99  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
100  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
101 
102  // Check the answer
103  if (std::abs(vp - vp2) < 1e-2)
104  std::cout << "\nExample Passed!" << std::endl;
105 
107  }
108  catch (std::exception& e) {
109  std::cout << e.what() << std::endl;
110  }
111 }
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)
Hermite polynomial basis.
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)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
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...