Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoUQPCEUnitTest.hpp
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.hpp"
11 
12 namespace Stokhos {
13 
14  template<class PCEType, class OrdinalType, class ValueType>
15  bool comparePCEs(const PCEType& a1,
16  const std::string& a1_name,
18  const std::string& a2_name,
19  const ValueType& rel_tol, const ValueType& abs_tol,
21  {
22  bool success = true;
23 
24  out << "Comparing " << a1_name << " == " << a2_name << " ... ";
25 
26  const OrdinalType n = a1.size();
27 
28  // Compare sizes
29  if (a2.size() != n) {
30  out << "\nError, "<<a1_name<<".size() = "<<a1.size()<<" == "
31  << a2_name<<".size() = "<<a2.size()<<" : failed!\n";
32  return false;
33  }
34 
35  // Compare elements
36  for( OrdinalType i = 0; i < n; ++i ) {
37  ValueType nrm = std::sqrt(a2.basis()->norm_squared(i));
38  ValueType err = std::abs(a1.coeff(i) - a2[i]) / nrm;
39  ValueType tol =
40  abs_tol + rel_tol*std::max(std::abs(a1.coeff(i)),std::abs(a2[i]))/nrm;
41  if (err > tol) {
42  out
43  <<"\nError, relErr("<<a1_name<<"["<<i<<"],"
44  <<a2_name<<"["<<i<<"]) = relErr("<<a1.coeff(i)<<","<<a2[i]<<") = "
45  <<err<<" <= tol = "<<tol<<": failed!\n";
46  success = false;
47  }
48  }
49  if (success) {
50  out << "passed\n";
51  }
52  else {
53  out << std::endl
54  << a1_name << " = " << a1 << std::endl
55  << a2_name << " = " << a2 << std::endl;
56  }
57 
58  return success;
59  }
60 
61 }
62 
63 namespace SacadoPCEUnitTest {
64 
65  // Common setup for unit tests
66  template <typename PCEType>
67  struct UnitTestSetup {
68 
69  typedef PCEType pce_type;
71  typedef typename pce_type::value_type value_type;
73  typedef typename pce_type::cijk_type kokkos_cijk_type;
82  pce_type x, y, sin_x, cos_y, cx, u, u2, cu, cu2, sx, su, su2;
84  value_type a;
85 
87  rtol = 1e-4;
88  atol = 1e-5;
89  crtol = 1e-12;
90  catol = 1e-12;
91  a = 3.1;
92  const ordinal_type d = 2;
93  const ordinal_type p = 7;
94 
95  // Create product basis
97  for (ordinal_type i=0; i<d; i++)
98  bases[i] =
100  basis =
102 
103  // Triple product tensor
105 
106  // Kokkos triple product tensor
107  cijk = Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
108 
109  // Algebraic expansion
111 
112  // Quad expansion for initialization
117 
118  // Create approximation
119  x_opa.reset(basis);
120  y_opa.reset(basis);
121  sin_x_opa.reset(basis);
122  cos_y_opa.reset(basis);
123  cx_opa.reset(basis,1);
124  x_opa.term(0, 0) = 1.0;
125  y_opa.term(0, 0) = 2.0;
126  cx_opa.term(0, 0) = a;
127  for (int i=0; i<d; i++) {
128  x_opa.term(i, 1) = 0.1;
129  y_opa.term(i, 1) = 0.25;
130  }
131  quad_exp->sin(sin_x_opa, x_opa);
132  quad_exp->cos(cos_y_opa, y_opa);
133 
134  // Create PCEs
135  x.reset(cijk);
136  y.reset(cijk);
137  sin_x.reset(cijk);
138  cos_y.reset(cijk);
139  cx.reset(cijk, 1);
140  x.load(x_opa.coeff());
141  y.load(y_opa.coeff());
142  sin_x.load(sin_x_opa.coeff());
143  cos_y.load(cos_y_opa.coeff());
144  cx.load(cx_opa.coeff());
145 
146  u.reset(cijk);
147  u2.reset(cijk);
148  cu.reset(cijk);
149  cu2.reset(cijk, 1);
150  sx.reset(cijk, d+1);
151  su.reset(cijk, d+1);
152  su2.reset(cijk, d+1);
153  for (ordinal_type i=0; i<d; i++) {
154  sx.fastAccessCoeff(i+1) = 0.0;
155  }
156  }
157  };
158 
160 
161  TEUCHOS_UNIT_TEST( Stokhos_PCE, UMinus) {
162  UTS setup;
163  UTS::pce_type u = -setup.sin_x;
164  UTS::opa_type u_opa(setup.basis);
165  setup.exp->unaryMinus(u_opa, setup.sin_x_opa);
166  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa",
167  setup.rtol, setup.atol, out);
168  }
169 
170 
171 #define UNARY_UNIT_TEST(OP) \
172  TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_const) { \
173  UTS setup; \
174  UTS::pce_type u = OP(setup.cx); \
175  UTS::opa_type u_opa(setup.basis); \
176  setup.exp->OP(u_opa, setup.cx_opa); \
177  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
178  setup.rtol, setup.atol, out); \
179  } \
180  TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_resize) { \
181  UTS setup; \
182  UTS::pce_type u; \
183  u = OP(setup.cx); \
184  UTS::opa_type u_opa(setup.basis); \
185  setup.exp->OP(u_opa, setup.cx_opa); \
186  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
187  setup.rtol, setup.atol, out); \
188  }
189 
204  // UNARY_UNIT_TEST(asinh)
205  // UNARY_UNIT_TEST(acosh)
206  // UNARY_UNIT_TEST(atanh)
207 
208 #define BINARY_UNIT_TEST(OP, EXPOP) \
209  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
210  UTS setup; \
211  UTS::pce_type v = setup.sin_x; \
212  UTS::pce_type w = setup.cos_y; \
213  UTS::pce_type u = OP(v,w); \
214  UTS::opa_type u_opa(setup.basis); \
215  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
216  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
217  setup.rtol, setup.atol, out); \
218  } \
219  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const) { \
220  UTS setup; \
221  UTS::pce_type w = setup.sin_x; \
222  UTS::pce_type u = OP(setup.a, w); \
223  UTS::opa_type u_opa(setup.basis); \
224  setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
225  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
226  setup.rtol, setup.atol, out); \
227  } \
228  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const) { \
229  UTS setup; \
230  UTS::pce_type v = setup.sin_x; \
231  UTS::pce_type u = OP(v, setup.a); \
232  UTS::opa_type u_opa(setup.basis); \
233  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
234  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
235  setup.rtol, setup.atol, out); \
236  } \
237  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_both_const) { \
238  UTS setup; \
239  UTS::pce_type u = OP(setup.cx, setup.cx); \
240  UTS::opa_type u_opa(setup.basis); \
241  setup.exp->EXPOP(u_opa, setup.cx_opa, setup.cx_opa); \
242  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
243  setup.rtol, setup.atol, out); \
244  } \
245  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const2) { \
246  UTS setup; \
247  UTS::pce_type w = setup.sin_x; \
248  UTS::pce_type u = OP(setup.cx, w); \
249  UTS::opa_type u_opa(setup.basis); \
250  setup.exp->EXPOP(u_opa, setup.cx_opa, setup.sin_x_opa); \
251  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
252  setup.rtol, setup.atol, out); \
253  } \
254  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const2) { \
255  UTS setup; \
256  UTS::pce_type v = setup.sin_x; \
257  UTS::pce_type u = OP(v, setup.cx); \
258  UTS::opa_type u_opa(setup.basis); \
259  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cx_opa); \
260  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
261  setup.rtol, setup.atol, out); \
262  } \
263  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
264  UTS setup; \
265  UTS::pce_type v = setup.sin_x; \
266  UTS::pce_type w = setup.cos_y; \
267  UTS::pce_type u; \
268  u = OP(v, w); \
269  UTS::opa_type u_opa(setup.basis); \
270  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
271  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
272  setup.rtol, setup.atol, out); \
273  } \
274  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const_resize) { \
275  UTS setup; \
276  UTS::pce_type w = setup.sin_x; \
277  UTS::pce_type u; \
278  u = OP(setup.a, w); \
279  UTS::opa_type u_opa(setup.basis); \
280  setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
281  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
282  setup.rtol, setup.atol, out); \
283  } \
284  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const_resize) { \
285  UTS setup; \
286  UTS::pce_type v = setup.sin_x; \
287  UTS::pce_type u; \
288  u = OP(v, setup.a); \
289  UTS::opa_type u_opa(setup.basis); \
290  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
291  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
292  setup.rtol, setup.atol, out); \
293  }
294 
295  BINARY_UNIT_TEST(operator+, plus)
296  BINARY_UNIT_TEST(operator-, minus)
297  BINARY_UNIT_TEST(operator*, times)
298  BINARY_UNIT_TEST(operator/, divide)
299 
300 #define OPASSIGN_UNIT_TEST(OP, EXPOP) \
301  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
302  UTS setup; \
303  UTS::pce_type v = setup.sin_x; \
304  UTS::pce_type u = setup.cos_y; \
305  u OP v; \
306  UTS::opa_type u_opa = setup.cos_y_opa; \
307  setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
308  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
309  setup.rtol, setup.atol, out); \
310  } \
311  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const) { \
312  UTS setup; \
313  UTS::pce_type u = setup.sin_x; \
314  u OP setup.a; \
315  UTS::opa_type u_opa = setup.sin_x_opa; \
316  setup.exp->EXPOP(u_opa, setup.a); \
317  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
318  setup.rtol, setup.atol, out); \
319  } \
320  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const2) { \
321  UTS setup; \
322  UTS::pce_type u = setup.sin_x; \
323  u OP setup.cx; \
324  UTS::opa_type u_opa = setup.sin_x_opa; \
325  setup.exp->EXPOP(u_opa, setup.cx_opa); \
326  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
327  setup.rtol, setup.atol, out); \
328  } \
329  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
330  UTS setup; \
331  UTS::pce_type v = setup.sin_x; \
332  UTS::pce_type u = setup.a; \
333  u OP v; \
334  UTS::opa_type u_opa = setup.cx_opa; \
335  setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
336  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
337  setup.rtol, setup.atol, out); \
338  }
339 
340  OPASSIGN_UNIT_TEST(+=, plusEqual)
341  OPASSIGN_UNIT_TEST(-=, minusEqual)
342  OPASSIGN_UNIT_TEST(*=, timesEqual)
343  OPASSIGN_UNIT_TEST(/=, divideEqual)
344 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > tan(const PCE< Storage > &a)
Stokhos::OrthogPolyApprox< ordinal_type, value_type > opa_type
KOKKOS_INLINE_FUNCTION PCE< Storage > sinh(const PCE< Storage > &a)
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
Kokkos::DefaultExecutionSpace execution_space
Stokhos::OrthogPolyApprox< int, double > opa_type
#define OPASSIGN_UNIT_TEST(OP, EXPOP)
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)
Teuchos::RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Orthogonal polynomial expansions limited to algebraic operations.
pointer coeff()
Return coefficient array.
KOKKOS_INLINE_FUNCTION PCE< Storage > tanh(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > cbrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > acos(const PCE< Storage > &a)
UnitTestSetup< pce_type > UTS
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
KOKKOS_INLINE_FUNCTION PCE< Storage > max(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
TEUCHOS_UNIT_TEST(Stokhos_PCE, UMinus)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
KOKKOS_INLINE_FUNCTION PCE< Storage > cosh(const PCE< Storage > &a)
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > basis() const
Return basis.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
virtual const Teuchos::Array< value_type > & norm_squared() const =0
Return array storing norm-squared of each basis polynomial.
#define BINARY_UNIT_TEST(OP, EXPOP)
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Legendre polynomial basis.
#define UNARY_UNIT_TEST(OP)
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
ordinal_type size() const
Return size.
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< int, double > > exp
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
int n
KOKKOS_INLINE_FUNCTION PCE< Storage > asin(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > cos(const PCE< Storage > &a)
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.