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 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Stokhos Package
7 // Copyright (2009) Sandia Corporation
8 //
9 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10 // license for use of this work by or on behalf of the U.S. Government.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
40 //
41 // ***********************************************************************
42 // @HEADER
43 
44 // hermite_example
45 //
46 // usage:
47 // hermite_example
48 //
49 // output:
50 // prints the Hermite Polynomial Chaos Expansion of the simple function
51 //
52 // v = 1/(log(u)^2+1)
53 //
54 // where u = 1 + 0.4*H_1(x) + 0.06*H_2(x) + 0.002*H_3(x), x is a zero-mean
55 // and unit-variance Gaussian random variable, and H_i(x) is the i-th
56 // Hermite polynomial.
57 
58 #include "Stokhos.hpp"
60 
61 int main(int argc, char **argv)
62 {
63  using Teuchos::Array;
64  using Teuchos::RCP;
65  using Teuchos::rcp;
66 
67  // If applicable, set up MPI.
68  Teuchos::GlobalMPISession mpiSession (&argc, &argv);
69 
70  try {
71 
72  // Basis of dimension 3, order 5
73  const int d = 3;
74  const int p = 5;
75  Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
76  for (int i=0; i<d; i++) {
77  bases[i] = rcp(new Stokhos::HermiteBasis<int,double>(p-i));
78  }
79  RCP<const Stokhos::CompletePolynomialBasis<int,double> > basis =
81 
82  // Quadrature method
83  RCP<const Stokhos::Quadrature<int,double> > quad =
85 
86  // Triple product tensor
87  RCP<Stokhos::Sparse3Tensor<int,double> > Cijk =
88  basis->computeTripleProductTensor();
89 
90  // Expansion method
91  Stokhos::QuadOrthogPolyExpansion<int,double> expn(basis, Cijk, quad);
92 
93  // Polynomial expansions
94  Stokhos::OrthogPolyApprox<int,double> u(basis), v(basis), w(basis);
95  u.term(0,0) = 1.0;
96  for (int i=0; i<d; i++) {
97  if (bases[i]->order() >= 1)
98  u.term(i,1) = 0.4 / d;
99  if (bases[i]->order() >= 2)
100  u.term(i,2) = 0.06 / d;
101  if (bases[i]->order() >= 3)
102  u.term(i,3) = 0.002 / d;
103  }
104 
105  // Compute expansion
106  expn.log(v,u);
107  expn.times(w,v,v);
108  expn.plusEqual(w,1.0);
109  expn.divide(v,1.0,w);
110 
111  // Print u and v
112  std::cout << "v = 1.0 / (log(u)^2 + 1):" << std::endl;
113  std::cout << "\tu = ";
114  u.print(std::cout);
115  std::cout << "\tv = ";
116  v.print(std::cout);
117 
118  // Compute moments
119  double mean = v.mean();
120  double std_dev = v.standard_deviation();
121 
122  // Evaluate PCE and function at a point = 0.25 in each dimension
123  Array<double> pt(d);
124  for (int i=0; i<d; i++)
125  pt[i] = 0.25;
126  double up = u.evaluate(pt);
127  double vp = 1.0/(std::log(up)*std::log(up)+1.0);
128  double vp2 = v.evaluate(pt);
129 
130  // Print results
131  std::cout << "\tv mean = " << mean << std::endl;
132  std::cout << "\tv std. dev. = " << std_dev << std::endl;
133  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
134  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
135 
136  // Check the answer
137  if (std::abs(vp - vp2) < 1e-2)
138  std::cout << "\nExample Passed!" << std::endl;
139 
141  }
142  catch (std::exception& e) {
143  std::cout << e.what() << std::endl;
144  }
145 }
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...