Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
sacado_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Stokhos_Sacado.hpp"
44 
45 // The function to compute the polynomial chaos expansion of,
46 // written as a template function
47 template <class ScalarType>
48 ScalarType simple_function(const ScalarType& u) {
49  ScalarType z = std::log(u);
50  return 1.0/(z*z + 1.0);
51 }
52 
53 int main(int argc, char **argv)
54 {
55  // Typename of Polynomial Chaos scalar type
58 
59  // Short-hand for several classes used below
60  using Teuchos::Array;
61  using Teuchos::RCP;
62  using Teuchos::rcp;
67  using Stokhos::Quadrature;
73 
74  try {
75 
76  // Setup command line options
78  CLP.setDocString(
79  "This example computes the PC expansion of a simple function.\n");
80  int p = 4;
81  CLP.setOption("order", &p, "Polynomial order");
82  bool sparse = false;
83  CLP.setOption("sparse", "tensor", &sparse,
84  "Use sparse grid or tensor product quadrature");
85 
86  // Parse arguments
87  CLP.parse( argc, argv );
88 
89  // Basis of dimension 3, order given by command-line option
90  const int d = 3;
91  Array< RCP<const OneDOrthogPolyBasis<int,double> > > bases(d);
92  for (int i=0; i<d; i++) {
93  bases[i] = rcp(new HermiteBasis<int,double>(p, true));
94  }
95  RCP<const CompletePolynomialBasis<int,double> > basis =
96  rcp(new CompletePolynomialBasis<int,double>(bases));
97  std::cout << "basis size = " << basis->size() << std::endl;
98 
99  // Quadrature method
100  RCP<const Quadrature<int,double> > quad;
101  if (sparse) {
102  const TotalOrderIndexSet<int> index_set(d, p);
103  quad = rcp(new SmolyakSparseGridQuadrature<int,double>(basis, index_set));
104  }
105  else {
106  quad = rcp(new TensorProductQuadrature<int,double>(basis));
107  }
108  std::cout << "quadrature size = " << quad->size() << std::endl;
109 
110  // Triple product tensor
111  RCP<Sparse3Tensor<int,double> > Cijk =
112  basis->computeTripleProductTensor();
113 
114  // Expansion method
115  RCP<QuadOrthogPolyExpansion<int,double> > expn =
116  rcp(new QuadOrthogPolyExpansion<int,double>(basis, Cijk, quad));
117 
118  // Polynomial expansion of u (note: these are coefficients in the
119  // normalized basis)
120  pce_type u(expn);
121  u.term(0,0) = 1.0; // zeroth order term
122  u.term(0,1) = 0.1; // first order term for dimension 0
123  u.term(1,1) = 0.05; // first order term for dimension 1
124  u.term(2,1) = 0.01; // first order term for dimension 2
125 
126  // Compute PCE expansion of function
127  pce_type v = simple_function(u);
128 
129  // Print u and v
130  std::cout << "\tu = ";
131  u.print(std::cout);
132  std::cout << "\tv = ";
133  v.print(std::cout);
134 
135  // Compute moments
136  double mean = v.mean();
137  double std_dev = v.standard_deviation();
138 
139  // Evaluate PCE and function at a point = 0.25 in each dimension
141  for (int i=0; i<d; i++)
142  pt[i] = 0.25;
143  double up = u.evaluate(pt);
144  double vp = simple_function(up);
145  double vp2 = v.evaluate(pt);
146 
147  // Print results
148  std::cout << "\tv mean = " << mean << std::endl;
149  std::cout << "\tv std. dev. = " << std_dev << std::endl;
150  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
151  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
152 
153  // Check the answer
154  if (std::abs(vp - vp2) < 1e-2)
155  std::cout << "\nExample Passed!" << std::endl;
156  }
157  catch (std::exception& e) {
158  std::cout << e.what() << std::endl;
159  }
160 }
Sacado::UQ::abs
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Definition: Sacado_UQ_PCE_Imp.hpp:1181
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Stokhos::Quadrature
Abstract base class for quadrature methods.
Definition: Stokhos_Quadrature.hpp:54
CLP
Definition: gram_schmidt_example3.cpp:87
Stokhos::Sparse3Tensor
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
Definition: Stokhos_Sparse3Tensor.hpp:56
Stokhos_Sacado.hpp
Teuchos::RCP
Stokhos::TotalOrderIndexSet
An isotropic total order index set.
Definition: Stokhos_ProductBasisUtils.hpp:215
Teuchos::Array
Stokhos::OneDOrthogPolyBasis
Abstract base class for 1-D orthogonal polynomials.
Definition: Stokhos_OneDOrthogPolyBasis.hpp:81
Sacado::UQ::log
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
Definition: Sacado_UQ_PCE_Imp.hpp:844
pce_type
Sacado::PCE::OrthogPoly< double, Storage > pce_type
Definition: ifpack2_pce_instantiations.cpp:45
Stokhos::TensorProductQuadrature
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
Definition: Stokhos_TensorProductQuadrature.hpp:58
Stokhos::StandardStorage< int, double >
Stokhos::LegendreBasis
Legendre polynomial basis.
Definition: Stokhos_LegendreBasis.hpp:67
Stokhos::HermiteBasis
Hermite polynomial basis.
Definition: Stokhos_HermiteBasis.hpp:67
Sacado::ETPCE::OrthogPoly
Definition: Sacado_ETPCE_OrthogPolyTraits.hpp:50
Teuchos_CommandLineProcessor.hpp
main
int main(int argc, char **argv)
Definition: sacado_example.cpp:53
storage_type
Stokhos::StandardStorage< int, double > storage_type
Definition: Stokhos_SacadoETPCEUnitTest.cpp:50
Stokhos::CompletePolynomialBasis
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Definition: Stokhos_CompletePolynomialBasis.hpp:72
Teuchos::CommandLineProcessor
Stokhos::SmolyakSparseGridQuadrature
Defines quadrature for a tensor product basis by Smolyak sparse grids.
Definition: Stokhos_SmolyakSparseGridQuadrature.hpp:63
argv
char * argv[]
Definition: Stokhos_HouseTriDiagUnitTest.cpp:286
Stokhos::QuadOrthogPolyExpansion
Orthogonal polynomial expansions based on numerical quadrature.
Definition: Stokhos_QuadOrthogPolyExpansion.hpp:62
Sacado::PCE::OrthogPoly< double, Storage >
simple_function
ScalarType simple_function(const ScalarType &u)
Definition: sacado_example.cpp:48