42#include "Teuchos_UnitTestHarness.hpp"
43#include "Teuchos_TestingHelpers.hpp"
44#include "Teuchos_UnitTestRepository.hpp"
45#include "Teuchos_GlobalMPISession.hpp"
58 template<
class PCEType,
class OrdinalType,
class ValueType>
60 const std::string& a1_name,
62 const std::string& a2_name,
63 const ValueType& rel_tol,
const ValueType& abs_tol,
64 Teuchos::FancyOStream& out)
68 out <<
"Comparing " << a1_name <<
" == " << a2_name <<
" ... ";
70 const OrdinalType n = a1.size();
74 out <<
"\nError, "<<a1_name<<
".size() = "<<a1.size()<<
" == "
75 << a2_name<<
".size() = "<<a2.size()<<
" : failed!\n";
80 for( OrdinalType i = 0; i < n; ++i ) {
81 ValueType nrm = std::sqrt(a2.basis()->norm_squared(i));
82 ValueType err = std::abs(a1.coeff(i) - a2[i]) / nrm;
84 abs_tol + rel_tol*std::max(std::abs(a1.coeff(i)),std::abs(a2[i]))/nrm;
87 <<
"\nError, relErr("<<a1_name<<
"["<<i<<
"],"
88 <<a2_name<<
"["<<i<<
"]) = relErr("<<a1.coeff(i)<<
","<<a2[i]<<
") = "
89 <<err<<
" <= tol = "<<tol<<
": failed!\n";
98 << a1_name <<
" = " << a1 << std::endl
99 << a2_name <<
" = " << a2 << std::endl;
110 template <
typename PCEType>
122 Teuchos::RCP<const Stokhos::CompletePolynomialBasis<ordinal_type,value_type> >
basis;
123 Teuchos::RCP<Stokhos::Sparse3Tensor<ordinal_type,value_type> >
Cijk;
125 Teuchos::RCP< Stokhos::AlgebraicOrthogPolyExpansion<ordinal_type,value_type> >
exp;
126 pce_type x,
y,
sin_x,
cos_y,
cx,
u,
u2,
cu,
cu2,
sx,
su,
su2;
140 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(d);
148 Cijk =
basis->computeTripleProductTensor();
151 cijk = Stokhos::create_product_tensor<execution_space>(*
basis, *
Cijk);
157 Teuchos::RCP<const Stokhos::Quadrature<int,double> >
quad =
159 Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion<int,double> > quad_exp =
171 for (
int i=0; i<d; i++) {
198 sx.fastAccessCoeff(i+1) = 0.0;
203 typedef UnitTestSetup<pce_type>
UTS;
207 UTS::pce_type u = -
setup.sin_x;
208 UTS::opa_type u_opa(
setup.basis);
209 setup.exp->unaryMinus(u_opa,
setup.sin_x_opa);
215#define UNARY_UNIT_TEST(OP) \
216 TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_const) { \
218 UTS::pce_type u = OP(setup.cx); \
219 UTS::opa_type u_opa(setup.basis); \
220 setup.exp->OP(u_opa, setup.cx_opa); \
221 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
222 setup.rtol, setup.atol, out); \
224 TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_resize) { \
228 UTS::opa_type u_opa(setup.basis); \
229 setup.exp->OP(u_opa, setup.cx_opa); \
230 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
231 setup.rtol, setup.atol, out); \
252#define BINARY_UNIT_TEST(OP, EXPOP) \
253 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
255 UTS::pce_type v = setup.sin_x; \
256 UTS::pce_type w = setup.cos_y; \
257 UTS::pce_type u = OP(v,w); \
258 UTS::opa_type u_opa(setup.basis); \
259 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
260 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
261 setup.rtol, setup.atol, out); \
263 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const) { \
265 UTS::pce_type w = setup.sin_x; \
266 UTS::pce_type u = OP(setup.a, w); \
267 UTS::opa_type u_opa(setup.basis); \
268 setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
269 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
270 setup.rtol, setup.atol, out); \
272 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const) { \
274 UTS::pce_type v = setup.sin_x; \
275 UTS::pce_type u = OP(v, setup.a); \
276 UTS::opa_type u_opa(setup.basis); \
277 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
278 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
279 setup.rtol, setup.atol, out); \
281 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_both_const) { \
283 UTS::pce_type u = OP(setup.cx, setup.cx); \
284 UTS::opa_type u_opa(setup.basis); \
285 setup.exp->EXPOP(u_opa, setup.cx_opa, setup.cx_opa); \
286 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
287 setup.rtol, setup.atol, out); \
289 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const2) { \
291 UTS::pce_type w = setup.sin_x; \
292 UTS::pce_type u = OP(setup.cx, w); \
293 UTS::opa_type u_opa(setup.basis); \
294 setup.exp->EXPOP(u_opa, setup.cx_opa, setup.sin_x_opa); \
295 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
296 setup.rtol, setup.atol, out); \
298 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const2) { \
300 UTS::pce_type v = setup.sin_x; \
301 UTS::pce_type u = OP(v, setup.cx); \
302 UTS::opa_type u_opa(setup.basis); \
303 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cx_opa); \
304 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
305 setup.rtol, setup.atol, out); \
307 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
309 UTS::pce_type v = setup.sin_x; \
310 UTS::pce_type w = setup.cos_y; \
313 UTS::opa_type u_opa(setup.basis); \
314 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
315 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
316 setup.rtol, setup.atol, out); \
318 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const_resize) { \
320 UTS::pce_type w = setup.sin_x; \
322 u = OP(setup.a, w); \
323 UTS::opa_type u_opa(setup.basis); \
324 setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
325 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
326 setup.rtol, setup.atol, out); \
328 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const_resize) { \
330 UTS::pce_type v = setup.sin_x; \
332 u = OP(v, setup.a); \
333 UTS::opa_type u_opa(setup.basis); \
334 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
335 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
336 setup.rtol, setup.atol, out); \
344#define OPASSIGN_UNIT_TEST(OP, EXPOP) \
345 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
347 UTS::pce_type v = setup.sin_x; \
348 UTS::pce_type u = setup.cos_y; \
350 UTS::opa_type u_opa = setup.cos_y_opa; \
351 setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
352 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
353 setup.rtol, setup.atol, out); \
355 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const) { \
357 UTS::pce_type u = setup.sin_x; \
359 UTS::opa_type u_opa = setup.sin_x_opa; \
360 setup.exp->EXPOP(u_opa, setup.a); \
361 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
362 setup.rtol, setup.atol, out); \
364 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const2) { \
366 UTS::pce_type u = setup.sin_x; \
368 UTS::opa_type u_opa = setup.sin_x_opa; \
369 setup.exp->EXPOP(u_opa, setup.cx_opa); \
370 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
371 setup.rtol, setup.atol, out); \
373 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
375 UTS::pce_type v = setup.sin_x; \
376 UTS::pce_type u = setup.a; \
378 UTS::opa_type u_opa = setup.cx_opa; \
379 setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
380 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
381 setup.rtol, setup.atol, out); \
391 UTS::pce_type u = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
392 UTS::opa_type v(
setup.basis, 8);
393 for (
int i=0; i<8; i++)
395 success = comparePCEs(u,
"u", v,
"v",
401 u = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
402 UTS::opa_type v(
setup.basis, 8);
403 for (
int i=0; i<8; i++)
405 success = comparePCEs(u,
"u", v,
"v",
411 u.reset(UTS::pce_type::cijk_type(), 8);
412 for (
auto& z : u) { z = 3.0; }
413 UTS::opa_type v(
setup.basis, 8);
414 for (
int i=0; i<8; i++)
416 success = comparePCEs(u,
"u", v,
"v",
#define UNARY_UNIT_TEST(VEC, SCALAR_T, OP, OPNAME, USING_OP)
#define OPASSIGN_UNIT_TEST(VEC, SCALAR_T, OP, OPNAME)
#define BINARY_UNIT_TEST(VEC, SCALAR_T, OP, OPNAME)
Sacado::UQ::PCE< storage_type > pce_type
Stokhos::DynamicStorage< int, double, execution_space > storage_type
Kokkos::DefaultHostExecutionSpace execution_space
Orthogonal polynomial expansions limited to algebraic operations.
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Legendre polynomial basis.
Class to store coefficients of a projection onto an orthogonal polynomial basis.
void reset(const Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > &new_basis, ordinal_type sz=0)
Reset to a new basis.
pointer coeff()
Return coefficient array.
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.
Orthogonal polynomial expansions based on numerical quadrature.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules.
TEUCHOS_UNIT_TEST(Stokhos_PCE, UMinus)
UnitTestSetup< pce_type > UTS
Top-level namespace for Stokhos classes and functions.
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< const Stokhos::Quadrature< int, double > > quad
Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > Cijk
pce_type::ordinal_type ordinal_type
Teuchos::RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
pce_type::execution_space execution_space
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< ordinal_type, value_type > > basis
pce_type::cijk_type kokkos_cijk_type
pce_type::value_type value_type
Stokhos::OrthogPolyApprox< ordinal_type, value_type > opa_type
Teuchos::RCP< Stokhos::AlgebraicOrthogPolyExpansion< ordinal_type, value_type > > exp