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 // @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 
14 
15 #include "Stokhos.hpp"
17 
18 namespace ConstantExpansionUnitTest {
19 
20  // Common setup for unit tests
21  template <typename OrdinalType, typename ValueType>
22  struct UnitTestSetup {
23  ValueType rtol, atol;
24  ValueType crtol, catol;
25  OrdinalType sz;
29  ValueType a;
30 
32  rtol = 1e-4;
33  atol = 1e-5;
34  crtol = 1e-12;
35  catol = 1e-12;
36  a = 3.1;
37  const OrdinalType d = 2;
38  const OrdinalType p = 7;
39 
40  // Create product basis
42  for (OrdinalType i=0; i<d; i++)
43  bases[i] =
45  basis =
47 
48  // Constant expansion
49  exp =
51 
52  // Create approximation
53  cx.reset(basis, 1);
54  cx.term(0, 0) = a;
55  cu.reset(basis, 1);
56  cu2.reset(basis, 1);
57  }
58 
59  };
60 
62 
63  struct ASinhFunc {
64  double operator() (double a) const {
65  return std::log(a+std::sqrt(a*a+1.0));
66  }
67  };
68  struct ACoshFunc {
69  double operator() (double a) const {
70  return std::log(a+std::sqrt(a*a-1.0));
71  }
72  };
73  struct ATanhFunc {
74  double operator() (double a) const {
75  return 0.5*std::log((1.0+a)/(1.0-a));
76  }
77  };
78 
79  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, UMinus ) {
80  setup.exp->unaryMinus(setup.cu, setup.cx);
81  setup.cu2[0] = -setup.cx[0];
82  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
83  setup.rtol, setup.atol, out);
84  }
85  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ExpConst ) {
86  setup.exp->exp(setup.cu, setup.cx);
87  setup.cu2[0] = std::exp(setup.cx[0]);
88  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
89  setup.crtol, setup.catol, out);
90  }
91  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, LogConst ) {
92  setup.exp->log(setup.cu, setup.cx);
93  setup.cu2[0] = std::log(setup.cx[0]);
94  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
95  setup.crtol, setup.catol, out);
96  }
97  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Log10Const ) {
98  setup.exp->log10(setup.cu, setup.cx);
99  setup.cu2[0] = std::log10(setup.cx[0]);
100  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
101  setup.crtol, setup.catol, out);
102  }
103  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SqrtConst ) {
104  setup.exp->sqrt(setup.cu, setup.cx);
105  setup.cu2[0] = std::sqrt(setup.cx[0]);
106  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
107  setup.crtol, setup.catol, out);
108  }
109  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CbrtConst ) {
110  setup.exp->cbrt(setup.cu, setup.cx);
111  setup.cu2[0] = std::cbrt(setup.cx[0]);
112  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
113  setup.crtol, setup.catol, out);
114  }
115  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SinConst ) {
116  setup.exp->sin(setup.cu, setup.cx);
117  setup.cu2[0] = std::sin(setup.cx[0]);
118  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
119  setup.crtol, setup.catol, out);
120  }
121  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CosConst ) {
122  setup.exp->cos(setup.cu, setup.cx);
123  setup.cu2[0] = std::cos(setup.cx[0]);
124  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
125  setup.crtol, setup.catol, out);
126  }
127  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TanConst ) {
128  setup.exp->tan(setup.cu, setup.cx);
129  setup.cu2[0] = std::tan(setup.cx[0]);
130  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
131  setup.crtol, setup.catol, out);
132  }
133  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, SinhConst ) {
134  setup.exp->sinh(setup.cu, setup.cx);
135  setup.cu2[0] = std::sinh(setup.cx[0]);
136  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
137  setup.crtol, setup.catol, out);
138  }
139  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, CoshConst ) {
140  setup.exp->cosh(setup.cu, setup.cx);
141  setup.cu2[0] = std::cosh(setup.cx[0]);
142  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
143  setup.crtol, setup.catol, out);
144  }
145  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TanhConst ) {
146  setup.exp->tanh(setup.cu, setup.cx);
147  setup.cu2[0] = std::tanh(setup.cx[0]);
148  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
149  setup.crtol, setup.catol, out);
150  }
151  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ASinConst ) {
152  setup.exp->asin(setup.cu, setup.cx);
153  setup.cu2[0] = std::asin(setup.cx[0]);
154  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
155  setup.crtol, setup.catol, out);
156  }
157  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ACosConst ) {
158  setup.exp->acos(setup.cu, setup.cx);
159  setup.cu2[0] = std::acos(setup.cx[0]);
160  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
161  setup.crtol, setup.catol, out);
162  }
163  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ATanConst ) {
164  setup.exp->atan(setup.cu, setup.cx);
165  setup.cu2[0] = std::atan(setup.cx[0]);
166  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
167  setup.crtol, setup.catol, out);
168  }
169  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ASinhConst ) {
170  ASinhFunc f;
171  setup.exp->asinh(setup.cu, setup.cx);
172  setup.cu2[0] = f(setup.cx[0]);
173  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
174  setup.crtol, setup.catol, out);
175  }
176  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ACoshConst ) {
177  ACoshFunc f;
178  setup.exp->acosh(setup.cu, setup.cx);
179  setup.cu2[0] = f(setup.cx[0]);
180  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
181  setup.crtol, setup.catol, out);
182  }
183  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, ATanhConst ) {
184  ATanhFunc f;
185  setup.exp->atanh(setup.cu, setup.cx);
186  setup.cu2[0] = f(setup.cx[0]);
187  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
188  setup.crtol, setup.catol, out);
189  }
190 
191  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Plus ) {
192  setup.exp->plus(setup.cu, setup.cx, setup.cx);
193  setup.cu2[0] = setup.cx[0] + setup.cx[0];
194  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
195  setup.rtol, setup.atol, out);
196  }
197  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusLC ) {
198  setup.exp->plus(setup.cu, setup.a, setup.cx);
199  setup.cu2[0] = setup.a + setup.cx[0];
200  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
201  setup.rtol, setup.atol, out);
202  }
203  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusRC ) {
204  setup.exp->plus(setup.cu, setup.cx, setup.a);
205  setup.cu2[0] = setup.cx[0] + setup.a;
206  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
207  setup.rtol, setup.atol, out);
208  }
209 
210  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Minus ) {
211  setup.exp->minus(setup.cu, setup.cx, setup.cx);
212  setup.cu2[0] = setup.cx[0] - setup.cx[0];
213  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
214  setup.rtol, setup.atol, out);
215  }
216  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusLC ) {
217  setup.exp->minus(setup.cu, setup.a, setup.cx);
218  setup.cu2[0] = setup.a - setup.cx[0];
219  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
220  setup.rtol, setup.atol, out);
221  }
222  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusRC ) {
223  setup.exp->minus(setup.cu, setup.cx, setup.a);
224  setup.cu2[0] = setup.cx[0] - setup.a;
225  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
226  setup.rtol, setup.atol, out);
227  }
228 
229  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Times ) {
230  setup.exp->times(setup.cu, setup.cx, setup.cx);
231  setup.cu2[0] = setup.cx[0] * setup.cx[0];
232  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
233  setup.rtol, setup.atol, out);
234  }
235  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesLC ) {
236  setup.exp->times(setup.cu, setup.a, setup.cx);
237  setup.cu2[0] = setup.a * setup.cx[0];
238  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
239  setup.rtol, setup.atol, out);
240  }
241  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesRC ) {
242  setup.exp->times(setup.cu, setup.cx, setup.a);
243  setup.cu2[0] = setup.cx[0] * setup.a;
244  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
245  setup.rtol, setup.atol, out);
246  }
247 
248  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, Divide ) {
249  setup.exp->divide(setup.cu, setup.cx, setup.cx);
250  setup.cu2[0] = setup.cx[0] / setup.cx[0];
251  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
252  setup.rtol, setup.atol, out);
253  }
254  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideLC ) {
255  setup.exp->divide(setup.cu, setup.a, setup.cx);
256  setup.cu2[0] = setup.a / setup.cx[0];
257  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
258  setup.rtol, setup.atol, out);
259  }
260  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideRC ) {
261  setup.exp->divide(setup.cu, setup.cx, setup.a);
262  setup.cu2[0] = setup.cx[0] / setup.a;
263  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
264  setup.rtol, setup.atol, out);
265  }
266 
267  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PowConst ) {
268  setup.exp->pow(setup.cu, setup.cx, setup.cx);
269  setup.cu2[0] = std::pow(setup.cx[0], setup.cx[0]);
270  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
271  setup.crtol, setup.catol, out);
272  }
273 
274  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusEqualC ) {
275  setup.cu = setup.cx;
276  setup.cu2[0] = setup.cu[0] + setup.a;
277  setup.exp->plusEqual(setup.cu, setup.a);
278  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
279  setup.rtol, setup.atol, out);
280  }
281  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, PlusEqualC2 ) {
282  setup.cu = setup.cx;
283  setup.cu2[0] = setup.cu[0] + setup.cx[0];
284  setup.exp->plusEqual(setup.cu, setup.cx);
285  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
286  setup.rtol, setup.atol, out);
287  }
288 
289  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusEqualC ) {
290  setup.cu = setup.cx;
291  setup.cu2[0] = setup.cu[0] - setup.a;
292  setup.exp->minusEqual(setup.cu, setup.a);
293  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
294  setup.rtol, setup.atol, out);
295  }
296  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, MinusEqualC2 ) {
297  setup.cu = setup.cx;
298  setup.cu2[0] = setup.cu[0] - setup.cx[0];
299  setup.exp->minusEqual(setup.cu, setup.cx);
300  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
301  setup.rtol, setup.atol, out);
302  }
303 
304  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesEqualC ) {
305  setup.cu = setup.cx;
306  setup.cu2[0] = setup.cu[0] * setup.a;
307  setup.exp->timesEqual(setup.cu, setup.a);
308  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
309  setup.rtol, setup.atol, out);
310  }
311  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, TimesEqualC2 ) {
312  setup.cu = setup.cx;
313  setup.cu2[0] = setup.cu[0] * setup.cx[0];
314  setup.exp->timesEqual(setup.cu, setup.cx);
315  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
316  setup.rtol, setup.atol, out);
317  }
318 
319  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideEqualC ) {
320  setup.cu = setup.cx;
321  setup.cu2[0] = setup.cu[0] / setup.a;
322  setup.exp->divideEqual(setup.cu, setup.a);
323  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
324  setup.rtol, setup.atol, out);
325  }
326  TEUCHOS_UNIT_TEST( Stokhos_ConstantExpansion, DivideEqualC2 ) {
327  setup.cu = setup.cx;
328  setup.cu2[0] = setup.cu[0] / setup.cx[0];
329  setup.exp->divideEqual(setup.cu, setup.cx);
330  success = Stokhos::comparePCEs(setup.cu, "cu", setup.cu2, "cu2",
331  setup.rtol, setup.atol, out);
332  }
333 
334  // Not testing atan2(), max(), min(), abs(), fabs() since these are
335  // not smooth functions
336 
337 }
338 
339 int main( int argc, char* argv[] ) {
340  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
342 }
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.