Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_GramSchmidtUnitTest.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 
14 
15 #include "Stokhos.hpp"
20 
21 // Quadrature functor to be passed into quadrature expansion for mapping
22 // from Gram-Schmidt basis back to original PCE
27  pce(pce_), basis(basis_), vec(1) {}
28 
29  double operator() (const double& a) const {
30  vec[0] = a;
31  return pce.evaluate(vec);
32  }
36 };
41  pce(pce_), basis(basis_), vec(2) {}
42 
43  double operator() (const double& a, const double& b) const {
44  vec[0] = a;
45  vec[1] = b;
46  return pce.evaluate(vec);
47  }
51 };
52 
53 // Class encapsulating setup of the Gram-Schmidt basis for a given PCE
54 template <typename OrdinalType, typename ValueType>
56  ValueType rtol, atol;
57  OrdinalType sz, gs_sz;
63 
65  {
66  rtol = 1e-7;
67  atol = 1e-12;
68  const OrdinalType d = 3;
69  const OrdinalType p = 5;
70 
71  // Create product basis
73  for (OrdinalType i=0; i<d; i++)
74  bases[i] =
76  basis =
78 
79  // Create approximation
80  sz = basis->size();
82  for (OrdinalType i=0; i<d; i++)
83  x.term(i, 1) = 1.0;
84 
85  // Tensor product quadrature
88 
89  // Triple product tensor
92 
93  // Quadrature expansion
96  exp_params->set("Use Quadrature for Times", true);
98 
99  // Compute PCE via quadrature expansion
100  u.reset(basis);
101  v.reset(basis);
102  w.reset(basis);
103  exp->sin(u,x);
104  exp->exp(v,x);
105  exp->times(w,u,v);
106 
107  // Compute Stieltjes basis
109  st_bases[0] = Teuchos::rcp(new Stokhos::StieltjesPCEBasis<OrdinalType,ValueType>(p, Teuchos::rcp(&u,false), quad, true));
110  st_bases[1] = Teuchos::rcp(new Stokhos::StieltjesPCEBasis<OrdinalType,ValueType>(p, Teuchos::rcp(&v,false), quad, true));
113  Stokhos::OrthogPolyApprox<OrdinalType,ValueType> u_st(st_basis), v_st(st_basis);
114  u_st.term(0, 0) = u.mean();
115  u_st.term(0, 1) = 1.0;
116  v_st.term(0, 0) = v.mean();
117  v_st.term(1, 1) = 1.0;
118 
119  // Use Gram-Schmidt to orthogonalize Stieltjes basis of u and v
120  Teuchos::Array<ValueType> st_points_0;
121  Teuchos::Array<ValueType> st_weights_0;
123  st_bases[0]->getQuadPoints(p+1, st_points_0, st_weights_0, st_values_0);
124  Teuchos::Array<ValueType> st_points_1;
125  Teuchos::Array<ValueType> st_weights_1;
127  st_bases[1]->getQuadPoints(p+1, st_points_1, st_weights_1, st_values_1);
130  for (int i=0; i<st_points_0.size(); i++) {
131  (*st_points)[i].resize(2);
132  (*st_points)[i][0] = st_points_0[i];
133  (*st_points)[i][1] = st_points_1[i];
134  }
137  *st_weights = st_weights_0;
138 
139  gs_basis = Teuchos::rcp(new Stokhos::GramSchmidtBasis<OrdinalType,ValueType>(st_basis, *st_points, *st_weights, 1e-15));
140  gs_sz = gs_basis->size();
141 
142  // Triple product tensor
145 
146  // Create quadrature for Gram-Schmidt basis using quad points and
147  // and weights from original basis mapped to Stieljtes basis
149  st_points;
150  Teuchos::RCP< const Teuchos::Array<ValueType> > weights = st_weights;
152 
153  // Gram-Schmidt quadrature expansion
155  gs_Cijk,
156  gs_quad,
157  exp_params);
158 
162 
163  // Map expansion in Stieltjes basis to Gram-Schmidt basis
164  gs_basis->transformCoeffs(u_st.coeff(), u_gs.coeff());
165  gs_basis->transformCoeffs(v_st.coeff(), v_gs.coeff());
166 
167  // Compute w_gs = u_gs*v_gs in Gram-Schmidt basis
168  gs_exp.times(w_gs, u_gs, v_gs);
169  }
170 
171 };
172 
173 namespace GramSchmidtTest {
175 
176  // Tests mapping from Gram-Schmidt basis to original is correct
177  TEUCHOS_UNIT_TEST( Stokhos_GramSchmidtBasis, Map ) {
179  gram_schmidt_pce_binary_quad_func quad_func(setup.u_gs, *setup.gs_basis);
180  setup.exp->binary_op(quad_func, u2, setup.u, setup.v);
181  success = comparePCEs(setup.u, "u", u2, "u2", setup.rtol, setup.atol, out);
182  }
183 
184  // Tests Gram-Schmidt basis is orthogonal
185  TEUCHOS_UNIT_TEST( Stokhos_GramSchmidtBasis, Orthog ) {
186  const Teuchos::Array<double>& norms = setup.gs_basis->norm_squared();
187  const Teuchos::Array<double>& weights = setup.gs_quad->getQuadWeights();
188  const Teuchos::Array< Teuchos::Array<double> >& values =
189  setup.gs_quad->getBasisAtQuadPoints();
191  for (int i=0; i<setup.gs_sz; i++) {
192  for (int j=0; j<setup.gs_sz; j++) {
193  for (int k=0; k<weights.size(); k++)
194  mat(i,j) += weights[k]*values[k][i]*values[k][j];
195  mat(i,j) /= std::sqrt(norms[i]*norms[j]);
196  }
197  mat(i,i) -= 1.0;
198  }
199  double tol = 1e-4;
200  success = mat.normInf() < tol;
201  if (!success) {
202  out << "\n Error, mat.normInf() < tol = " << mat.normInf()
203  << " < " << tol << ": failed!\n";
204  out << "mat = " << printMat(mat) << std::endl;
205  }
206  }
207 
208  // Tests PCE computed from Gram-Schmidt basis is same as original
209  TEUCHOS_UNIT_TEST( Stokhos_GramSchmidtBasis, PCE ) {
211  gram_schmidt_pce_binary_quad_func quad_func(setup.w_gs, *setup.gs_basis);
212  setup.exp->binary_op(quad_func, w2, setup.u, setup.v);
213  success = comparePCEs(setup.w, "w", w2, "w2", setup.rtol, setup.atol, out);
214  }
215 
216  // Tests mean computed from Gram-Schmidt basis is same as original
217  TEUCHOS_UNIT_TEST( Stokhos_GramSchmidtBasis, Mean ) {
218  success = Teuchos::testRelErr("w.mean()", setup.w.mean(),
219  "w_gs.mean()", setup.w_gs.mean(),
220  "rtol", setup.rtol,
221  "rtol", setup.rtol,
222  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
223  }
224 
225  // Tests mean standard deviation from Gram-Schmidt basis is same as original
226  TEUCHOS_UNIT_TEST( Stokhos_GramSchmidtBasis, StandardDeviation ) {
227  success = Teuchos::testRelErr("w.standard_deviation()",
228  setup.w.standard_deviation(),
229  "w_gs.standard_devaition()",
230  setup.w_gs.standard_deviation(),
231  "rtol", 1e-3,
232  "rtol", 1e-3,
233  Teuchos::Ptr<std::ostream>(out.getOStream().get()));
234  }
235 
236 }
237 
238 int main( int argc, char* argv[] ) {
239  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
241 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > w_gs
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)
value_type evaluate(const Teuchos::Array< value_type > &point) const
Evaluate polynomial approximation at a point.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const =0
Compute triple product tensor.
void exp(OrthogPolyApprox< ordinal_type, value_type, node_type > &c, const OrthogPolyApprox< ordinal_type, value_type, node_type > &a)
const Stokhos::OrthogPolyApprox< int, double > & pce
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v
bool comparePCEs(const PCEType &a1, const std::string &a1_name, const Stokhos::OrthogPolyApprox< OrdinalType, ValueType > &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
GramSchmidt_PCE_Setup< int, double > setup
const Stokhos::OrthogPolyApprox< int, double > & pce
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
gram_schmidt_pce_binary_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
double operator()(const double &a) const
pointer coeff()
Return coefficient array.
const Stokhos::OrthogPolyBasis< int, double > & basis
static int runUnitTestsFromMain(int argc, char *argv[])
const Stokhos::OrthogPolyBasis< int, double > & basis
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > u_gs
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > v_gs
double operator()(const double &a, const double &b) const
TEUCHOS_UNIT_TEST(Stokhos_GramSchmidtBasis, Map)
Teuchos::RCP< const Stokhos::GramSchmidtBasis< OrdinalType, ValueType > > gs_basis
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > w
bool testRelErr(const std::string &v1_name, const T1 &v1, const std::string &v2_name, const T2 &v2, const std::string &maxRelErr_error_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename TestRelErr< T1, T2 >::magnitudeType &maxRelErr_warning, const Ptr< std::ostream > &out)
value_type mean() const
Compute mean of expansion.
gram_schmidt_pce_unary_quad_func(const Stokhos::OrthogPolyApprox< int, double > &pce_, const Stokhos::OrthogPolyBasis< int, double > &basis_)
int main(int argc, char **argv)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
ScalarTraits< ScalarType >::magnitudeType normInf() const
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > gs_quad
size_type size() const
virtual ordinal_type size() const
Return total size of basis.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
virtual ordinal_type size() const =0
Return total size of basis.
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< OrdinalType, ValueType > > exp
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.