Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sacado_ensemble_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 #include "Stokhos_Sacado.hpp"
13 
14 // The function to compute the polynomial chaos expansion of,
15 // written as a template function
16 template <class ScalarType>
17 ScalarType simple_function(const ScalarType& u) {
18  ScalarType z = std::log(u);
19  return 1.0/(z*z + 1.0);
20 }
21 
22 int main(int argc, char **argv)
23 {
24  // Typename of Polynomial Chaos scalar type
25  typedef Stokhos::StandardStorage<int,double> pce_storage_type;
27 
28  // Typename of ensemble scalar type
29  const int EnsembleSize = 8;
31  typedef Sacado::MP::Vector<ensemble_storage_type> ensemble_type;
32 
33  // Short-hand for several classes used below
34  using Teuchos::Array;
35  using Teuchos::RCP;
36  using Teuchos::rcp;
41  using Stokhos::Quadrature;
47 
48  try {
49 
50  // Setup command line options
52  CLP.setDocString(
53  "This example computes the PC expansion of a simple function.\n");
54  int p = 4;
55  CLP.setOption("order", &p, "Polynomial order");
56  bool sparse = false;
57  CLP.setOption("sparse", "tensor", &sparse,
58  "Use sparse grid or tensor product quadrature");
59 
60  // Parse arguments
61  CLP.parse( argc, argv );
62 
63  // Basis of dimension 3, order given by command-line option
64  const int d = 3;
65  Array< RCP<const OneDOrthogPolyBasis<int,double> > > bases(d);
66  for (int i=0; i<d; i++) {
67  bases[i] = rcp(new HermiteBasis<int,double>(p, true));
68  }
69  RCP<const CompletePolynomialBasis<int,double> > basis =
70  rcp(new CompletePolynomialBasis<int,double>(bases));
71  const int pce_size = basis->size();
72  std::cout << "basis size = " << pce_size << std::endl;
73 
74  // Quadrature method
75  RCP<const Quadrature<int,double> > quad;
76  if (sparse) {
77  const TotalOrderIndexSet<int> index_set(d, p);
78  quad = rcp(new SmolyakSparseGridQuadrature<int,double>(basis, index_set));
79  }
80  else {
81  quad = rcp(new TensorProductQuadrature<int,double>(basis));
82  }
83  std::cout << "quadrature size = " << quad->size() << std::endl;
84 
85  // Triple product tensor
86  RCP<Sparse3Tensor<int,double> > Cijk =
87  basis->computeTripleProductTensor();
88 
89  // Expansion method
90  RCP<QuadOrthogPolyExpansion<int,double> > expn =
91  rcp(new QuadOrthogPolyExpansion<int,double>(basis, Cijk, quad));
92 
93  // Polynomial expansion of u (note: these are coefficients in the
94  // normalized basis)
95  pce_type u(expn);
96  u.term(0,0) = 1.0; // zeroth order term
97  u.term(0,1) = 0.1; // first order term for dimension 0
98  u.term(1,1) = 0.05; // first order term for dimension 1
99  u.term(2,1) = 0.01; // first order term for dimension 2
100 
101  //
102  // Compute PCE expansion of function using NISP with ensemble propagation
103  //
104 
105  // Extract quadrature data
106  const int num_quad_points = quad->size();
107  const Array<double>& quad_weights = quad->getQuadWeights();
108  const Array< Array<double> >& quad_points = quad->getQuadPoints();
109  const Array< Array<double> >& quad_values = quad->getBasisAtQuadPoints();
110 
111  // Loop over quadrature points in blocks of size EnsembleSize
112  pce_type v(expn);
113  ensemble_type u_ensemble;
114  for (int qp_block=0; qp_block<num_quad_points; qp_block+=EnsembleSize) {
115  const int qp_sz = qp_block+EnsembleSize <= num_quad_points ?
116  EnsembleSize : num_quad_points-qp_block;
117 
118  // Evaluate u at each quadrature point
119  for (int qp=0; qp<qp_sz; ++qp)
120  u_ensemble.fastAccessCoeff(qp) =
121  u.evaluate(quad_points[qp_block+qp], quad_values[qp_block+qp]);
122  for (int qp=qp_sz; qp<EnsembleSize; ++qp)
123  u_ensemble.fastAccessCoeff(qp) = u_ensemble.fastAccessCoeff(qp_sz-1);
124 
125  // Evaluate function at each quadrature point
126  ensemble_type v_ensemble = simple_function(u_ensemble);
127 
128  // Sum results into PCE integral
129  for (int pc=0; pc<pce_size; ++pc)
130  for (int qp=0; qp<qp_sz; ++qp)
131  v.fastAccessCoeff(pc) += v_ensemble.fastAccessCoeff(qp)*quad_weights[qp_block+qp]*quad_values[qp_block+qp][pc];
132  }
133 
134  /*
135  for (int qp=0; qp<num_quad_points; ++qp) {
136  double u_qp = u.evaluate(quad_points[qp]);
137  double v_qp = simple_function(u_qp);
138  double w = quad_weights[qp];
139  for (int pc=0; pc<pce_size; ++pc)
140  v.fastAccessCoeff(pc) += v_qp*w*quad_values[qp][pc];
141  }
142  */
143 
144  // Print u and v
145  std::cout << "\tu = ";
146  u.print(std::cout);
147  std::cout << "\tv = ";
148  v.print(std::cout);
149 
150  // Compute moments
151  double mean = v.mean();
152  double std_dev = v.standard_deviation();
153 
154  // Evaluate PCE and function at a point = 0.25 in each dimension
156  for (int i=0; i<d; i++)
157  pt[i] = 0.25;
158  double up = u.evaluate(pt);
159  double vp = simple_function(up);
160  double vp2 = v.evaluate(pt);
161 
162  // Print results
163  std::cout << "\tv mean = " << mean << std::endl;
164  std::cout << "\tv std. dev. = " << std_dev << std::endl;
165  std::cout << "\tv(0.25) (true) = " << vp << std::endl;
166  std::cout << "\tv(0.25) (pce) = " << vp2 << std::endl;
167 
168  // Check the answer
169  if (std::abs(vp - vp2) < 1e-2)
170  std::cout << "\nExample Passed!" << std::endl;
171  }
172  catch (std::exception& e) {
173  std::cout << e.what() << std::endl;
174  }
175 }
ScalarType simple_function(const ScalarType &u)
Hermite polynomial basis.
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
Statically allocated storage class.
Defines quadrature for a tensor product basis by Smolyak sparse grids.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class for quadrature methods.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Legendre polynomial basis.
int main(int argc, char **argv)
An isotropic total order index set.
Abstract base class for 1-D orthogonal polynomials.
void setDocString(const char doc_string[])
Orthogonal polynomial expansions based on numerical quadrature.
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...