Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_ConstantExpansionUnitTest.cpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Stokhos Package
7 // Copyright (2009) Sandia Corporation
8 //
9 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10 // license for use of this work by or on behalf of the U.S. Government.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
40 //
41 // ***********************************************************************
42 // @HEADER
43 
48 
49 #include "Stokhos.hpp"
51 
52 namespace ConstantExpansionUnitTest {
53 
54  // Common setup for unit tests
55  template <typename OrdinalType, typename ValueType>
56  struct UnitTestSetup {
57  ValueType rtol, atol;
58  ValueType crtol, catol;
59  OrdinalType sz;
63  ValueType a;
64 
66  rtol = 1e-4;
67  atol = 1e-5;
68  crtol = 1e-12;
69  catol = 1e-12;
70  a = 3.1;
71  const OrdinalType d = 2;
72  const OrdinalType p = 7;
73 
74  // Create product basis
76  for (OrdinalType i=0; i<d; i++)
77  bases[i] =
79  basis =
81 
82  // Constant expansion
83  exp =
85 
86  // Create approximation
87  cx.reset(basis, 1);
88  cx.term(0, 0) = a;
89  cu.reset(basis, 1);
90  cu2.reset(basis, 1);
91  }
92 
93  };
94 
96 
97  struct ASinhFunc {
98  double operator() (double a) const {
99  return std::log(a+std::sqrt(a*a+1.0));
100  }
101  };
102  struct ACoshFunc {
103  double operator() (double a) const {
104  return std::log(a+std::sqrt(a*a-1.0));
105  }
106  };
107  struct ATanhFunc {
108  double operator() (double a) const {
109  return 0.5*std::log((1.0+a)/(1.0-a));
110  }
111  };
112 
113  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, UMinus ) {
114  setup.exp->unaryMinus(setup.cu, setup.cx);
115  setup.cu2[0] = -setup.cx[0];
116  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
117  setup.rtol, setup.atol, out);
118  }
119  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ExpConst ) {
120  setup.exp->exp(setup.cu, setup.cx);
121  setup.cu2[0] = std::exp(setup.cx[0]);
122  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
123  setup.crtol, setup.catol, out);
124  }
125  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, LogConst ) {
126  setup.exp->log(setup.cu, setup.cx);
127  setup.cu2[0] = std::log(setup.cx[0]);
128  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
129  setup.crtol, setup.catol, out);
130  }
131  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Log10Const ) {
132  setup.exp->log10(setup.cu, setup.cx);
133  setup.cu2[0] = std::log10(setup.cx[0]);
134  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
135  setup.crtol, setup.catol, out);
136  }
137  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SqrtConst ) {
138  setup.exp->sqrt(setup.cu, setup.cx);
139  setup.cu2[0] = std::sqrt(setup.cx[0]);
140  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
141  setup.crtol, setup.catol, out);
142  }
143  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CbrtConst ) {
144  setup.exp->cbrt(setup.cu, setup.cx);
145  setup.cu2[0] = std::cbrt(setup.cx[0]);
146  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
147  setup.crtol, setup.catol, out);
148  }
149  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SinConst ) {
150  setup.exp->sin(setup.cu, setup.cx);
151  setup.cu2[0] = std::sin(setup.cx[0]);
152  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
153  setup.crtol, setup.catol, out);
154  }
155  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CosConst ) {
156  setup.exp->cos(setup.cu, setup.cx);
157  setup.cu2[0] = std::cos(setup.cx[0]);
158  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
159  setup.crtol, setup.catol, out);
160  }
161  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TanConst ) {
162  setup.exp->tan(setup.cu, setup.cx);
163  setup.cu2[0] = std::tan(setup.cx[0]);
164  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
165  setup.crtol, setup.catol, out);
166  }
167  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SinhConst ) {
168  setup.exp->sinh(setup.cu, setup.cx);
169  setup.cu2[0] = std::sinh(setup.cx[0]);
170  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
171  setup.crtol, setup.catol, out);
172  }
173  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CoshConst ) {
174  setup.exp->cosh(setup.cu, setup.cx);
175  setup.cu2[0] = std::cosh(setup.cx[0]);
176  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
177  setup.crtol, setup.catol, out);
178  }
179  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TanhConst ) {
180  setup.exp->tanh(setup.cu, setup.cx);
181  setup.cu2[0] = std::tanh(setup.cx[0]);
182  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
183  setup.crtol, setup.catol, out);
184  }
185  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ASinConst ) {
186  setup.exp->asin(setup.cu, setup.cx);
187  setup.cu2[0] = std::asin(setup.cx[0]);
188  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
189  setup.crtol, setup.catol, out);
190  }
191  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ACosConst ) {
192  setup.exp->acos(setup.cu, setup.cx);
193  setup.cu2[0] = std::acos(setup.cx[0]);
194  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
195  setup.crtol, setup.catol, out);
196  }
197  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ATanConst ) {
198  setup.exp->atan(setup.cu, setup.cx);
199  setup.cu2[0] = std::atan(setup.cx[0]);
200  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
201  setup.crtol, setup.catol, out);
202  }
203  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ASinhConst ) {
204  ASinhFunc f;
205  setup.exp->asinh(setup.cu, setup.cx);
206  setup.cu2[0] = f(setup.cx[0]);
207  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
208  setup.crtol, setup.catol, out);
209  }
210  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ACoshConst ) {
211  ACoshFunc f;
212  setup.exp->acosh(setup.cu, setup.cx);
213  setup.cu2[0] = f(setup.cx[0]);
214  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
215  setup.crtol, setup.catol, out);
216  }
217  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ATanhConst ) {
218  ATanhFunc f;
219  setup.exp->atanh(setup.cu, setup.cx);
220  setup.cu2[0] = f(setup.cx[0]);
221  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
222  setup.crtol, setup.catol, out);
223  }
224 
225  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Plus ) {
226  setup.exp->plus(setup.cu, setup.cx, setup.cx);
227  setup.cu2[0] = setup.cx[0] + setup.cx[0];
228  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
229  setup.rtol, setup.atol, out);
230  }
231  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusLC ) {
232  setup.exp->plus(setup.cu, setup.a, setup.cx);
233  setup.cu2[0] = setup.a + setup.cx[0];
234  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
235  setup.rtol, setup.atol, out);
236  }
237  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusRC ) {
238  setup.exp->plus(setup.cu, setup.cx, setup.a);
239  setup.cu2[0] = setup.cx[0] + setup.a;
240  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
241  setup.rtol, setup.atol, out);
242  }
243 
244  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Minus ) {
245  setup.exp->minus(setup.cu, setup.cx, setup.cx);
246  setup.cu2[0] = setup.cx[0] - setup.cx[0];
247  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
248  setup.rtol, setup.atol, out);
249  }
250  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusLC ) {
251  setup.exp->minus(setup.cu, setup.a, setup.cx);
252  setup.cu2[0] = setup.a - setup.cx[0];
253  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
254  setup.rtol, setup.atol, out);
255  }
256  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusRC ) {
257  setup.exp->minus(setup.cu, setup.cx, setup.a);
258  setup.cu2[0] = setup.cx[0] - setup.a;
259  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
260  setup.rtol, setup.atol, out);
261  }
262 
263  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Times ) {
264  setup.exp->times(setup.cu, setup.cx, setup.cx);
265  setup.cu2[0] = setup.cx[0] * setup.cx[0];
266  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
267  setup.rtol, setup.atol, out);
268  }
269  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesLC ) {
270  setup.exp->times(setup.cu, setup.a, setup.cx);
271  setup.cu2[0] = setup.a * setup.cx[0];
272  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
273  setup.rtol, setup.atol, out);
274  }
275  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesRC ) {
276  setup.exp->times(setup.cu, setup.cx, setup.a);
277  setup.cu2[0] = setup.cx[0] * setup.a;
278  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
279  setup.rtol, setup.atol, out);
280  }
281 
282  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Divide ) {
283  setup.exp->divide(setup.cu, setup.cx, setup.cx);
284  setup.cu2[0] = setup.cx[0] / setup.cx[0];
285  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
286  setup.rtol, setup.atol, out);
287  }
288  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideLC ) {
289  setup.exp->divide(setup.cu, setup.a, setup.cx);
290  setup.cu2[0] = setup.a / setup.cx[0];
291  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
292  setup.rtol, setup.atol, out);
293  }
294  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideRC ) {
295  setup.exp->divide(setup.cu, setup.cx, setup.a);
296  setup.cu2[0] = setup.cx[0] / setup.a;
297  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
298  setup.rtol, setup.atol, out);
299  }
300 
301  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PowConst ) {
302  setup.exp->pow(setup.cu, setup.cx, setup.cx);
303  setup.cu2[0] = std::pow(setup.cx[0], setup.cx[0]);
304  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
305  setup.crtol, setup.catol, out);
306  }
307 
308  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusEqualC ) {
309  setup.cu = setup.cx;
310  setup.cu2[0] = setup.cu[0] + setup.a;
311  setup.exp->plusEqual(setup.cu, setup.a);
312  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
313  setup.rtol, setup.atol, out);
314  }
315  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusEqualC2 ) {
316  setup.cu = setup.cx;
317  setup.cu2[0] = setup.cu[0] + setup.cx[0];
318  setup.exp->plusEqual(setup.cu, setup.cx);
319  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
320  setup.rtol, setup.atol, out);
321  }
322 
323  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusEqualC ) {
324  setup.cu = setup.cx;
325  setup.cu2[0] = setup.cu[0] - setup.a;
326  setup.exp->minusEqual(setup.cu, setup.a);
327  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
328  setup.rtol, setup.atol, out);
329  }
330  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusEqualC2 ) {
331  setup.cu = setup.cx;
332  setup.cu2[0] = setup.cu[0] - setup.cx[0];
333  setup.exp->minusEqual(setup.cu, setup.cx);
334  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
335  setup.rtol, setup.atol, out);
336  }
337 
338  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesEqualC ) {
339  setup.cu = setup.cx;
340  setup.cu2[0] = setup.cu[0] * setup.a;
341  setup.exp->timesEqual(setup.cu, setup.a);
342  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
343  setup.rtol, setup.atol, out);
344  }
345  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesEqualC2 ) {
346  setup.cu = setup.cx;
347  setup.cu2[0] = setup.cu[0] * setup.cx[0];
348  setup.exp->timesEqual(setup.cu, setup.cx);
349  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
350  setup.rtol, setup.atol, out);
351  }
352 
353  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideEqualC ) {
354  setup.cu = setup.cx;
355  setup.cu2[0] = setup.cu[0] / setup.a;
356  setup.exp->divideEqual(setup.cu, setup.a);
357  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
358  setup.rtol, setup.atol, out);
359  }
360  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideEqualC2 ) {
361  setup.cu = setup.cx;
362  setup.cu2[0] = setup.cu[0] / setup.cx[0];
363  setup.exp->divideEqual(setup.cu, setup.cx);
364  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
365  setup.rtol, setup.atol, out);
366  }
367 
368  // Not testing atan2(), max(), min(), abs(), fabs() since these are
369  // not smooth functions
370 
371 }
372 
373 int main( int argc, char* argv[] ) {
374  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
376 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
KOKKOS_INLINE_FUNCTION PCE< Storage > tan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > sinh(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
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_UNIT_TEST(Stokhos_ConstantExpansion, UMinus)
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)
static int runUnitTestsFromMain(int argc, char *argv[])
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 > cosh(const PCE< Storage > &a)
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > cu
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > cx
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
int main(int argc, char **argv)
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Teuchos::RCP< Stokhos::ConstantOrthogPolyExpansion< OrdinalType, ValueType > > exp
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
Stokhos::OrthogPolyApprox< OrdinalType, ValueType > cu2
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > asin(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > cos(const PCE< Storage > &a)
reference term(ordinal_type dimension, ordinal_type order)
Get coefficient term for given dimension and order.