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.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 
46 
47 #include "Stokhos.hpp"
50 
51 typedef Kokkos::DefaultExecutionSpace execution_space;
54 
55 namespace Stokhos {
56 
57  template<class PCEType, class OrdinalType, class ValueType>
58  bool comparePCEs(const PCEType& a1,
59  const std::string& a1_name,
61  const std::string& a2_name,
62  const ValueType& rel_tol, const ValueType& abs_tol,
64  {
65  bool success = true;
66 
67  out << "Comparing " << a1_name << " == " << a2_name << " ... ";
68 
69  const OrdinalType n = a1.size();
70 
71  // Compare sizes
72  if (a2.size() != n) {
73  out << "\nError, "<<a1_name<<".size() = "<<a1.size()<<" == "
74  << a2_name<<".size() = "<<a2.size()<<" : failed!\n";
75  return false;
76  }
77 
78  // Compare elements
79  for( OrdinalType i = 0; i < n; ++i ) {
80  ValueType nrm = std::sqrt(a2.basis()->norm_squared(i));
81  ValueType err = std::abs(a1.coeff(i) - a2[i]) / nrm;
82  ValueType tol =
83  abs_tol + rel_tol*std::max(std::abs(a1.coeff(i)),std::abs(a2[i]))/nrm;
84  if (err > tol) {
85  out
86  <<"\nError, relErr("<<a1_name<<"["<<i<<"],"
87  <<a2_name<<"["<<i<<"]) = relErr("<<a1.coeff(i)<<","<<a2[i]<<") = "
88  <<err<<" <= tol = "<<tol<<": failed!\n";
89  success = false;
90  }
91  }
92  if (success) {
93  out << "passed\n";
94  }
95  else {
96  out << std::endl
97  << a1_name << " = " << a1 << std::endl
98  << a2_name << " = " << a2 << std::endl;
99  }
100 
101  return success;
102  }
103 
104 }
105 
106 namespace SacadoPCEUnitTest {
107 
108  // Common setup for unit tests
109  template <typename PCEType>
110  struct UnitTestSetup {
111 
112  typedef PCEType pce_type;
116  typedef typename pce_type::cijk_type kokkos_cijk_type;
128 
130  rtol = 1e-4;
131  atol = 1e-5;
132  crtol = 1e-12;
133  catol = 1e-12;
134  a = 3.1;
135  const ordinal_type d = 2;
136  const ordinal_type p = 7;
137 
138  // Create product basis
140  for (ordinal_type i=0; i<d; i++)
141  bases[i] =
143  basis =
145 
146  // Triple product tensor
148 
149  // Kokkos triple product tensor
150  cijk = Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
151 
152  // Algebraic expansion
154 
155  // Quad expansion for initialization
160 
161  // Create approximation
162  x_opa.reset(basis);
163  y_opa.reset(basis);
164  sin_x_opa.reset(basis);
165  cos_y_opa.reset(basis);
166  cx_opa.reset(basis,1);
167  x_opa.term(0, 0) = 1.0;
168  y_opa.term(0, 0) = 2.0;
169  cx_opa.term(0, 0) = a;
170  for (int i=0; i<d; i++) {
171  x_opa.term(i, 1) = 0.1;
172  y_opa.term(i, 1) = 0.25;
173  }
174  quad_exp->sin(sin_x_opa, x_opa);
175  quad_exp->cos(cos_y_opa, y_opa);
176 
177  // Create PCEs
178  x.reset(cijk);
179  y.reset(cijk);
180  sin_x.reset(cijk);
181  cos_y.reset(cijk);
182  cx.reset(cijk, 1);
183  x.load(x_opa.coeff());
184  y.load(y_opa.coeff());
185  sin_x.load(sin_x_opa.coeff());
186  cos_y.load(cos_y_opa.coeff());
187  cx.load(cx_opa.coeff());
188 
189  u.reset(cijk);
190  u2.reset(cijk);
191  cu.reset(cijk);
192  cu2.reset(cijk, 1);
193  sx.reset(cijk, d+1);
194  su.reset(cijk, d+1);
195  su2.reset(cijk, d+1);
196  for (ordinal_type i=0; i<d; i++) {
197  sx.fastAccessCoeff(i+1) = 0.0;
198  }
199  }
200  };
201 
203 
204  TEUCHOS_UNIT_TEST( Stokhos_PCE, UMinus) {
205  UTS setup;
206  UTS::pce_type u = -setup.sin_x;
207  UTS::opa_type u_opa(setup.basis);
208  setup.exp->unaryMinus(u_opa, setup.sin_x_opa);
209  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa",
210  setup.rtol, setup.atol, out);
211  }
212 
213 
214 #define UNARY_UNIT_TEST(OP) \
215  TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_const) { \
216  UTS setup; \
217  UTS::pce_type u = OP(setup.cx); \
218  UTS::opa_type u_opa(setup.basis); \
219  setup.exp->OP(u_opa, setup.cx_opa); \
220  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
221  setup.rtol, setup.atol, out); \
222  } \
223  TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_resize) { \
224  UTS setup; \
225  UTS::pce_type u; \
226  u = OP(setup.cx); \
227  UTS::opa_type u_opa(setup.basis); \
228  setup.exp->OP(u_opa, setup.cx_opa); \
229  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
230  setup.rtol, setup.atol, out); \
231  }
232 
247  // UNARY_UNIT_TEST(asinh)
248  // UNARY_UNIT_TEST(acosh)
249  // UNARY_UNIT_TEST(atanh)
250 
251 #define BINARY_UNIT_TEST(OP, EXPOP) \
252  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
253  UTS setup; \
254  UTS::pce_type v = setup.sin_x; \
255  UTS::pce_type w = setup.cos_y; \
256  UTS::pce_type u = OP(v,w); \
257  UTS::opa_type u_opa(setup.basis); \
258  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
259  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
260  setup.rtol, setup.atol, out); \
261  } \
262  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const) { \
263  UTS setup; \
264  UTS::pce_type w = setup.sin_x; \
265  UTS::pce_type u = OP(setup.a, w); \
266  UTS::opa_type u_opa(setup.basis); \
267  setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
268  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
269  setup.rtol, setup.atol, out); \
270  } \
271  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const) { \
272  UTS setup; \
273  UTS::pce_type v = setup.sin_x; \
274  UTS::pce_type u = OP(v, setup.a); \
275  UTS::opa_type u_opa(setup.basis); \
276  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
277  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
278  setup.rtol, setup.atol, out); \
279  } \
280  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_both_const) { \
281  UTS setup; \
282  UTS::pce_type u = OP(setup.cx, setup.cx); \
283  UTS::opa_type u_opa(setup.basis); \
284  setup.exp->EXPOP(u_opa, setup.cx_opa, setup.cx_opa); \
285  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
286  setup.rtol, setup.atol, out); \
287  } \
288  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const2) { \
289  UTS setup; \
290  UTS::pce_type w = setup.sin_x; \
291  UTS::pce_type u = OP(setup.cx, w); \
292  UTS::opa_type u_opa(setup.basis); \
293  setup.exp->EXPOP(u_opa, setup.cx_opa, setup.sin_x_opa); \
294  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
295  setup.rtol, setup.atol, out); \
296  } \
297  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const2) { \
298  UTS setup; \
299  UTS::pce_type v = setup.sin_x; \
300  UTS::pce_type u = OP(v, setup.cx); \
301  UTS::opa_type u_opa(setup.basis); \
302  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cx_opa); \
303  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
304  setup.rtol, setup.atol, out); \
305  } \
306  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
307  UTS setup; \
308  UTS::pce_type v = setup.sin_x; \
309  UTS::pce_type w = setup.cos_y; \
310  UTS::pce_type u; \
311  u = OP(v, w); \
312  UTS::opa_type u_opa(setup.basis); \
313  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
314  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
315  setup.rtol, setup.atol, out); \
316  } \
317  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const_resize) { \
318  UTS setup; \
319  UTS::pce_type w = setup.sin_x; \
320  UTS::pce_type u; \
321  u = OP(setup.a, w); \
322  UTS::opa_type u_opa(setup.basis); \
323  setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
324  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
325  setup.rtol, setup.atol, out); \
326  } \
327  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const_resize) { \
328  UTS setup; \
329  UTS::pce_type v = setup.sin_x; \
330  UTS::pce_type u; \
331  u = OP(v, setup.a); \
332  UTS::opa_type u_opa(setup.basis); \
333  setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
334  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
335  setup.rtol, setup.atol, out); \
336  }
337 
338  BINARY_UNIT_TEST(operator+, plus)
339  BINARY_UNIT_TEST(operator-, minus)
340  BINARY_UNIT_TEST(operator*, times)
341  BINARY_UNIT_TEST(operator/, divide)
342 
343 #define OPASSIGN_UNIT_TEST(OP, EXPOP) \
344  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
345  UTS setup; \
346  UTS::pce_type v = setup.sin_x; \
347  UTS::pce_type u = setup.cos_y; \
348  u OP v; \
349  UTS::opa_type u_opa = setup.cos_y_opa; \
350  setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
351  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
352  setup.rtol, setup.atol, out); \
353  } \
354  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const) { \
355  UTS setup; \
356  UTS::pce_type u = setup.sin_x; \
357  u OP setup.a; \
358  UTS::opa_type u_opa = setup.sin_x_opa; \
359  setup.exp->EXPOP(u_opa, setup.a); \
360  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
361  setup.rtol, setup.atol, out); \
362  } \
363  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const2) { \
364  UTS setup; \
365  UTS::pce_type u = setup.sin_x; \
366  u OP setup.cx; \
367  UTS::opa_type u_opa = setup.sin_x_opa; \
368  setup.exp->EXPOP(u_opa, setup.cx_opa); \
369  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
370  setup.rtol, setup.atol, out); \
371  } \
372  TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
373  UTS setup; \
374  UTS::pce_type v = setup.sin_x; \
375  UTS::pce_type u = setup.a; \
376  u OP v; \
377  UTS::opa_type u_opa = setup.cx_opa; \
378  setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
379  success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
380  setup.rtol, setup.atol, out); \
381  }
382 
383  OPASSIGN_UNIT_TEST(+=, plusEqual)
384  OPASSIGN_UNIT_TEST(-=, minusEqual)
385  OPASSIGN_UNIT_TEST(*=, timesEqual)
386  OPASSIGN_UNIT_TEST(/=, divideEqual)
387 
388  TEUCHOS_UNIT_TEST( Stokhos_PCE, initializer_list_copy ) {
389  UTS setup;
390  UTS::pce_type u = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
391  UTS::opa_type v(setup.basis, 8);
392  for (int i=0; i<8; i++)
393  v[i] = i+1;
394  success = comparePCEs(u, "u", v, "v",
395  setup.rtol, setup.atol, out);
396  }
397  TEUCHOS_UNIT_TEST( Stokhos_PCE, initializer_list_assign ) {
398  UTS setup;
399  UTS::pce_type u;
400  u = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
401  UTS::opa_type v(setup.basis, 8);
402  for (int i=0; i<8; i++)
403  v[i] = i+1;
404  success = comparePCEs(u, "u", v, "v",
405  setup.rtol, setup.atol, out);
406  }
407  TEUCHOS_UNIT_TEST( Stokhos_PCE, range_based_for ) {
408  UTS setup;
409  UTS::pce_type u;
410  u.reset(UTS::pce_type::cijk_type(), 8);
411  for (auto& z : u) { z = 3.0; }
412  UTS::opa_type v(setup.basis, 8);
413  for (int i=0; i<8; i++)
414  v[i] = 3.0;
415  success = comparePCEs(u, "u", v, "v",
416  setup.rtol, setup.atol, out);
417  }
418 }
419 
420 int main( int argc, char* argv[] ) {
421  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
422 
423  Kokkos::initialize();
424 // Kokkos::HostSpace::execution_space::initialize();
425 // if (!Kokkos::DefaultExecutionSpace::is_initialized())
426 // Kokkos::DefaultExecutionSpace::initialize();
427 
429 
430  Kokkos::finalize();
431 // Kokkos::HostSpace::execution_space::finalize();
432 // if (Kokkos::DefaultExecutionSpace::is_initialized())
433 // Kokkos::DefaultExecutionSpace::finalize();
434 
435  return res;
436 }
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
Stokhos::StandardStorage< int, double > storage_type
KOKKOS_INLINE_FUNCTION PCE< Storage > sinh(const PCE< Storage > &a)
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > Cijk
#define UNARY_UNIT_TEST(OP)
Kokkos::DefaultExecutionSpace execution_space
Stokhos::OrthogPolyApprox< int, double > opa_type
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
static int runUnitTestsFromMain(int argc, char *argv[])
Teuchos::RCP< Stokhos::AlgebraicOrthogPolyExpansion< ordinal_type, value_type > > exp
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.
#define BINARY_UNIT_TEST(OP, EXPOP)
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.
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Legendre polynomial basis.
int main(int argc, char **argv)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< ordinal_type, value_type > > basis
#define OPASSIGN_UNIT_TEST(OP, EXPOP)
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.