Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gram_schmidt_example2.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 <iostream>
11 #include <iomanip>
12 
13 #include "Stokhos.hpp"
15 
17 
19 
20 int main(int argc, char **argv)
21 {
22  try {
23 
24  // Setup command line options
26  CLP.setDocString(
27  "This example runs a Gram-Schmidt-based dimension reduction example.\n");
28 
29  int d = 2;
30  CLP.setOption("d", &d, "Stochastic dimension");
31 
32  int p = 5;
33  CLP.setOption("p", &p, "Polynomial order");
34 
35  CLP.parse( argc, argv );
36 
37  // Create product basis
39  for (int i=0; i<d; i++)
40  bases[i] = Teuchos::rcp(new basis_type(p, true));
43 
44  std::cout << "original basis size = " << basis->size() << std::endl;
45 
46  // Create approximation
47  Stokhos::OrthogPolyApprox<int,double> x(basis), u(basis), v(basis),
48  w(basis), w2(basis);
49  for (int i=0; i<d; i++) {
50  x.term(i, 1) = 1.0;
51  }
52 
53  // Tensor product quadrature
56  // Teuchos::RCP<const Stokhos::Quadrature<int,double> > quad =
57  // Teuchos::rcp(new Stokhos::SparseGridQuadrature<int,double>(basis,
58  // p+1));
59 
60  std::cout << "original quadrature size = " << quad->size() << std::endl;
61 
62  // Triple product tensor
64  basis->computeTripleProductTensor();
65 
66  // Quadrature expansion
67  Stokhos::QuadOrthogPolyExpansion<int,double> quad_exp(basis, Cijk, quad);
68 
69  // Compute PCE via quadrature expansion
70  quad_exp.sin(u,x);
71  quad_exp.exp(v,x);
72  quad_exp.times(w,v,u);
73 
74  // Create new basis from u and v
76  pces[0] = u;
77  pces[1] = v;
79  params.set("Reduced Basis Method", "Monomial Proj Gram-Schmidt");
82  factory.createReducedBasis(p, pces, quad, Cijk);
84  gs_basis->getReducedQuadrature();
85  Stokhos::OrthogPolyApprox<int,double> u_gs(gs_basis), v_gs(gs_basis),
86  w_gs(gs_basis);
87  gs_basis->transformFromOriginalBasis(u.coeff(), u_gs.coeff());
88  gs_basis->transformFromOriginalBasis(v.coeff(), v_gs.coeff());
89 
90  std::cout << "reduced basis size = " << gs_basis->size() << std::endl;
91  std::cout << "reduced quadrature size = " << gs_quad->size() << std::endl;
92 
93  Stokhos::OrthogPolyApprox<int,double> u2(basis), v2(basis);
94  gs_basis->transformToOriginalBasis(u_gs.coeff(), u2.coeff());
95  gs_basis->transformToOriginalBasis(v_gs.coeff(), v2.coeff());
96  double err_u = 0.0;
97  double err_v = 0.0;
98  for (int i=0; i<basis->size(); i++) {
99  double eu = std::abs(u[i]-u2[i]);
100  double ev = std::abs(v[i]-v2[i]);
101  if (eu > err_u) err_u = eu;
102  if (ev > err_v) err_v = ev;
103  }
104  std::cout << "error in u transformation = " << err_u << std::endl;
105  std::cout << "error in v transformation = " << err_v << std::endl;
106 
107  // Triple product tensor
110 
111  // Gram-Schmidt quadrature expansion
114  gs_exp_params->set("Use Quadrature for Times", true);
115  Stokhos::QuadOrthogPolyExpansion<int,double> gs_quad_exp(gs_basis,
116  gs_Cijk,
117  gs_quad,
118  gs_exp_params);
119 
120  // Compute w_gs = u_gs*v_gs in Gram-Schmidt basis
121  gs_quad_exp.times(w_gs, u_gs, v_gs);
122 
123  // Project w_gs back to original basis
124  gs_basis->transformToOriginalBasis(w_gs.coeff(), w2.coeff());
125 
126  std::cout.precision(12);
127  std::cout << "w = " << std::endl << w;
128  std::cout << "w2 = " << std::endl << w2;
129  std::cout << "w_gs = " << std::endl << w_gs;
130 
131  double err_w = 0.0;
132  for (int i=0; i<basis->size(); i++) {
133  double ew = std::abs(w[i]-w2[i]);
134  if (ew > err_w) err_w = ew;
135  }
136 
137  std::cout.setf(std::ios::scientific);
138  std::cout << "w.mean() = " << w.mean() << std::endl
139  << "w2.mean() = " << w2.mean() << std::endl
140  << "mean error = "
141  << std::abs(w.mean()-w2.mean()) << std::endl
142  << "w.std_dev() = " << w.standard_deviation() << std::endl
143  << "w2.std_dev() = " << w2.standard_deviation() << std::endl
144  << "std_dev error = "
145  << std::abs(w.standard_deviation()-w2.standard_deviation())
146  << std::endl
147  << "w coeff error = " << err_w << std::endl;
148 
149  }
150  catch (std::exception& e) {
151  std::cout << e.what() << std::endl;
152  }
153 }
void sin(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
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)
Generate a basis from a given set of PCE expansions that is orthogonal with respect to the product me...
void exp(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
pointer coeff()
Return coefficient array.
Stokhos::LegendreBasis< int, double > basis_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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)
virtual Teuchos::RCP< Stokhos::ReducedPCEBasis< ordinal_type, value_type > > createReducedBasis(ordinal_type p, const Teuchos::Array< Stokhos::OrthogPolyApprox< ordinal_type, value_type > > &pce, const Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > &quad, const Teuchos::RCP< const Stokhos::Sparse3Tensor< ordinal_type, value_type > > &Cijk) const
Get reduced quadrature object.
Legendre polynomial basis.
int main(int argc, char **argv)
void setDocString(const char doc_string[])
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...