Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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"
43#include "Teuchos_CommandLineProcessor.hpp"
44
45// The function to compute the polynomial chaos expansion of,
46// written as a template function
47template <class ScalarType>
48ScalarType simple_function(const ScalarType& u) {
49 ScalarType z = std::log(u);
50 return 1.0/(z*z + 1.0);
51}
52
53int 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;
73
74 try {
75
76 // Setup command line options
77 Teuchos::CommandLineProcessor CLP;
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
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
140 Teuchos::Array<double> pt(d);
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}
Stokhos::StandardStorage< int, double > storage_type
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Hermite polynomial basis.
Legendre polynomial basis.
Abstract base class for 1-D orthogonal polynomials.
Orthogonal polynomial expansions based on numerical quadrature.
Abstract base class for quadrature methods.
Defines quadrature for a tensor product basis by Smolyak sparse grids.
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
An isotropic total order index set.
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
int main(int argc, char **argv)
ScalarType simple_function(const ScalarType &u)