Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Stokhos_SacadoUQPCEUnitTest.hpp
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
42#include "Stokhos.hpp"
43
44namespace Stokhos {
45
46 template<class PCEType, class OrdinalType, class ValueType>
47 bool comparePCEs(const PCEType& a1,
48 const std::string& a1_name,
50 const std::string& a2_name,
51 const ValueType& rel_tol, const ValueType& abs_tol,
52 Teuchos::FancyOStream& out)
53 {
54 bool success = true;
55
56 out << "Comparing " << a1_name << " == " << a2_name << " ... ";
57
58 const OrdinalType n = a1.size();
59
60 // Compare sizes
61 if (a2.size() != n) {
62 out << "\nError, "<<a1_name<<".size() = "<<a1.size()<<" == "
63 << a2_name<<".size() = "<<a2.size()<<" : failed!\n";
64 return false;
65 }
66
67 // Compare elements
68 for( OrdinalType i = 0; i < n; ++i ) {
69 ValueType nrm = std::sqrt(a2.basis()->norm_squared(i));
70 ValueType err = std::abs(a1.coeff(i) - a2[i]) / nrm;
71 ValueType tol =
72 abs_tol + rel_tol*std::max(std::abs(a1.coeff(i)),std::abs(a2[i]))/nrm;
73 if (err > tol) {
74 out
75 <<"\nError, relErr("<<a1_name<<"["<<i<<"],"
76 <<a2_name<<"["<<i<<"]) = relErr("<<a1.coeff(i)<<","<<a2[i]<<") = "
77 <<err<<" <= tol = "<<tol<<": failed!\n";
78 success = false;
79 }
80 }
81 if (success) {
82 out << "passed\n";
83 }
84 else {
85 out << std::endl
86 << a1_name << " = " << a1 << std::endl
87 << a2_name << " = " << a2 << std::endl;
88 }
89
90 return success;
91 }
92
93}
94
95namespace SacadoPCEUnitTest {
96
97 // Common setup for unit tests
98 template <typename PCEType>
99 struct UnitTestSetup {
100
102 typedef typename pce_type::ordinal_type ordinal_type;
103 typedef typename pce_type::value_type value_type;
104 typedef typename pce_type::execution_space execution_space;
105 typedef typename pce_type::cijk_type kokkos_cijk_type;
110 Teuchos::RCP<const Stokhos::CompletePolynomialBasis<ordinal_type,value_type> > basis;
111 Teuchos::RCP<Stokhos::Sparse3Tensor<ordinal_type,value_type> > Cijk;
113 Teuchos::RCP< Stokhos::AlgebraicOrthogPolyExpansion<ordinal_type,value_type> > exp;
114 pce_type x, y, sin_x, cos_y, cx, u, u2, cu, cu2, sx, su, su2;
117
119 rtol = 1e-4;
120 atol = 1e-5;
121 crtol = 1e-12;
122 catol = 1e-12;
123 a = 3.1;
124 const ordinal_type d = 2;
125 const ordinal_type p = 7;
126
127 // Create product basis
128 Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<ordinal_type,value_type> > > bases(d);
129 for (ordinal_type i=0; i<d; i++)
130 bases[i] =
131 Teuchos::rcp(new Stokhos::LegendreBasis<ordinal_type,value_type>(p, true));
132 basis =
134
135 // Triple product tensor
136 Cijk = basis->computeTripleProductTensor();
137
138 // Kokkos triple product tensor
139 cijk = Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
140
141 // Algebraic expansion
143
144 // Quad expansion for initialization
145 Teuchos::RCP<const Stokhos::Quadrature<int,double> > quad =
147 Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion<int,double> > quad_exp =
149
150 // Create approximation
155 cx_opa.reset(basis,1);
156 x_opa.term(0, 0) = 1.0;
157 y_opa.term(0, 0) = 2.0;
158 cx_opa.term(0, 0) = a;
159 for (int i=0; i<d; i++) {
160 x_opa.term(i, 1) = 0.1;
161 y_opa.term(i, 1) = 0.25;
162 }
163 quad_exp->sin(sin_x_opa, x_opa);
164 quad_exp->cos(cos_y_opa, y_opa);
165
166 // Create PCEs
167 x.reset(cijk);
168 y.reset(cijk);
169 sin_x.reset(cijk);
170 cos_y.reset(cijk);
171 cx.reset(cijk, 1);
172 x.load(x_opa.coeff());
173 y.load(y_opa.coeff());
174 sin_x.load(sin_x_opa.coeff());
175 cos_y.load(cos_y_opa.coeff());
176 cx.load(cx_opa.coeff());
177
178 u.reset(cijk);
179 u2.reset(cijk);
180 cu.reset(cijk);
181 cu2.reset(cijk, 1);
182 sx.reset(cijk, d+1);
183 su.reset(cijk, d+1);
184 su2.reset(cijk, d+1);
185 for (ordinal_type i=0; i<d; i++) {
186 sx.fastAccessCoeff(i+1) = 0.0;
187 }
188 }
189 };
190
192
193 TEUCHOS_UNIT_TEST( Stokhos_PCE, UMinus) {
194 UTS setup;
197 setup.exp->unaryMinus(u_opa, setup.sin_x_opa);
198 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa",
199 setup.rtol, setup.atol, out);
200 }
201
202
203#define UNARY_UNIT_TEST(OP) \
204 TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_const) { \
205 UTS setup; \
206 UTS::pce_type u = OP(setup.cx); \
207 UTS::opa_type u_opa(setup.basis); \
208 setup.exp->OP(u_opa, setup.cx_opa); \
209 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
210 setup.rtol, setup.atol, out); \
211 } \
212 TEUCHOS_UNIT_TEST( Stokhos_PCE, OP##_resize) { \
213 UTS setup; \
214 UTS::pce_type u; \
215 u = OP(setup.cx); \
216 UTS::opa_type u_opa(setup.basis); \
217 setup.exp->OP(u_opa, setup.cx_opa); \
218 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
219 setup.rtol, setup.atol, out); \
220 }
221
222 UNARY_UNIT_TEST(exp)
223 UNARY_UNIT_TEST(log)
224 UNARY_UNIT_TEST(log10)
225 UNARY_UNIT_TEST(sqrt)
226 UNARY_UNIT_TEST(cbrt)
227 UNARY_UNIT_TEST(sin)
228 UNARY_UNIT_TEST(cos)
229 UNARY_UNIT_TEST(tan)
230 UNARY_UNIT_TEST(sinh)
231 UNARY_UNIT_TEST(cosh)
232 UNARY_UNIT_TEST(tanh)
233 UNARY_UNIT_TEST(asin)
234 UNARY_UNIT_TEST(acos)
235 UNARY_UNIT_TEST(atan)
236 // UNARY_UNIT_TEST(asinh)
237 // UNARY_UNIT_TEST(acosh)
238 // UNARY_UNIT_TEST(atanh)
239
240#define BINARY_UNIT_TEST(OP, EXPOP) \
241 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
242 UTS setup; \
243 UTS::pce_type v = setup.sin_x; \
244 UTS::pce_type w = setup.cos_y; \
245 UTS::pce_type u = OP(v,w); \
246 UTS::opa_type u_opa(setup.basis); \
247 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_opa); \
248 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
249 setup.rtol, setup.atol, out); \
250 } \
251 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const) { \
252 UTS setup; \
253 UTS::pce_type w = setup.sin_x; \
254 UTS::pce_type u = OP(setup.a, w); \
255 UTS::opa_type u_opa(setup.basis); \
256 setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
257 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
258 setup.rtol, setup.atol, out); \
259 } \
260 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const) { \
261 UTS setup; \
262 UTS::pce_type v = setup.sin_x; \
263 UTS::pce_type u = OP(v, setup.a); \
264 UTS::opa_type u_opa(setup.basis); \
265 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
266 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
267 setup.rtol, setup.atol, out); \
268 } \
269 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_both_const) { \
270 UTS setup; \
271 UTS::pce_type u = OP(setup.cx, setup.cx); \
272 UTS::opa_type u_opa(setup.basis); \
273 setup.exp->EXPOP(u_opa, setup.cx_opa, setup.cx_opa); \
274 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
275 setup.rtol, setup.atol, out); \
276 } \
277 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_left_const2) { \
278 UTS setup; \
279 UTS::pce_type w = setup.sin_x; \
280 UTS::pce_type u = OP(setup.cx, w); \
281 UTS::opa_type u_opa(setup.basis); \
282 setup.exp->EXPOP(u_opa, setup.cx_opa, setup.sin_x_opa); \
283 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
284 setup.rtol, setup.atol, out); \
285 } \
286 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const2) { \
287 UTS setup; \
288 UTS::pce_type v = setup.sin_x; \
289 UTS::pce_type u = OP(v, setup.cx); \
290 UTS::opa_type u_opa(setup.basis); \
291 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cx_opa); \
292 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
293 setup.rtol, setup.atol, out); \
294 } \
295 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
296 UTS setup; \
297 UTS::pce_type v = setup.sin_x; \
298 UTS::pce_type w = setup.cos_y; \
299 UTS::pce_type u; \
300 u = OP(v, w); \
301 UTS::opa_type u_opa(setup.basis); \
302 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.cos_y_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##_left_const_resize) { \
307 UTS setup; \
308 UTS::pce_type w = setup.sin_x; \
309 UTS::pce_type u; \
310 u = OP(setup.a, w); \
311 UTS::opa_type u_opa(setup.basis); \
312 setup.exp->EXPOP(u_opa, setup.a, setup.sin_x_opa); \
313 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
314 setup.rtol, setup.atol, out); \
315 } \
316 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_right_const_resize) { \
317 UTS setup; \
318 UTS::pce_type v = setup.sin_x; \
319 UTS::pce_type u; \
320 u = OP(v, setup.a); \
321 UTS::opa_type u_opa(setup.basis); \
322 setup.exp->EXPOP(u_opa, setup.sin_x_opa, setup.a); \
323 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
324 setup.rtol, setup.atol, out); \
325 }
326
327 BINARY_UNIT_TEST(operator+, plus)
328 BINARY_UNIT_TEST(operator-, minus)
329 BINARY_UNIT_TEST(operator*, times)
330 BINARY_UNIT_TEST(operator/, divide)
331
332#define OPASSIGN_UNIT_TEST(OP, EXPOP) \
333 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP) { \
334 UTS setup; \
335 UTS::pce_type v = setup.sin_x; \
336 UTS::pce_type u = setup.cos_y; \
337 u OP v; \
338 UTS::opa_type u_opa = setup.cos_y_opa; \
339 setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
340 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
341 setup.rtol, setup.atol, out); \
342 } \
343 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const) { \
344 UTS setup; \
345 UTS::pce_type u = setup.sin_x; \
346 u OP setup.a; \
347 UTS::opa_type u_opa = setup.sin_x_opa; \
348 setup.exp->EXPOP(u_opa, setup.a); \
349 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
350 setup.rtol, setup.atol, out); \
351 } \
352 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_const2) { \
353 UTS setup; \
354 UTS::pce_type u = setup.sin_x; \
355 u OP setup.cx; \
356 UTS::opa_type u_opa = setup.sin_x_opa; \
357 setup.exp->EXPOP(u_opa, setup.cx_opa); \
358 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
359 setup.rtol, setup.atol, out); \
360 } \
361 TEUCHOS_UNIT_TEST( Stokhos_PCE, EXPOP##_resize) { \
362 UTS setup; \
363 UTS::pce_type v = setup.sin_x; \
364 UTS::pce_type u = setup.a; \
365 u OP v; \
366 UTS::opa_type u_opa = setup.cx_opa; \
367 setup.exp->EXPOP(u_opa, setup.sin_x_opa); \
368 success = Stokhos::comparePCEs(u, "u", u_opa, "u_opa", \
369 setup.rtol, setup.atol, out); \
370 }
371
372 OPASSIGN_UNIT_TEST(+=, plusEqual)
373 OPASSIGN_UNIT_TEST(-=, minusEqual)
374 OPASSIGN_UNIT_TEST(*=, timesEqual)
375 OPASSIGN_UNIT_TEST(/=, divideEqual)
376}
#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)
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.
ordinal_type size() const
Return size.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > basis() const
Return basis.
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)
Stokhos::OrthogPolyApprox< int, double > opa_type
Teuchos::RCP< const Stokhos::Quadrature< int, double > > quad
Teuchos::RCP< Stokhos::QuadOrthogPolyExpansion< int, double > > exp
Teuchos::RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
Stokhos::OrthogPolyApprox< ordinal_type, value_type > opa_type