Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LogicalSparseUnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // Sacado includes
11 #include "Sacado_No_Kokkos.hpp"
12 #include "Sacado_Random.hpp"
13 
16 
17 // gtest includes
18 #include <gtest/gtest.h>
19 
20 // A class for testing each DFad operation
22 protected:
23 
24  // DFad variables
26 
27  // Logical sparse variables
29 
30  // Random number generator
32 
33  // Number of derivative components
34  int n;
35 
36  // Tolerances to which fad objects should be the same
37  double tol_a, tol_r;
38 
40  urand(0.0, 1.0), n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
41 
42  void SetUp() override {
43  double val;
44 
45  val = urand.number();
46  a_dfad = DFadType(n,val);
47  a_ls = LSType(n,val);
48 
49  val = urand.number();
50  b_dfad = DFadType(n,val);
51  b_ls = LSType(n,val);
52 
53  val = urand.number();
54  c_dfad = val;
55  c_ls = val;
56 
57  for (int i=0; i<n; i++) {
58  val = urand.number();
59  a_dfad.fastAccessDx(i) = val;
60  a_ls.fastAccessDx(i) = 1;
61 
62  val = urand.number();
63  b_dfad.fastAccessDx(i) = val;
64  b_ls.fastAccessDx(i) = 1;
65  }
66  }
67 
68  void TearDown() override {}
69 
70  // Assert to Fad objects are the same
71  void compareFads(const DFadType& x_dfad, const LSType& x_ls) {
72  // Compare sizes
73  ASSERT_TRUE(x_dfad.size() == x_ls.size());
74 
75  // Compare hasFastAccess
76  ASSERT_TRUE(x_dfad.hasFastAccess() == x_ls.hasFastAccess());
77 
78  // Compare values
79  compareDoubles(x_dfad.val(), x_ls.val());
80 
81  for (int i=0; i<x_ls.size(); i++) {
82 
83  // Compare dx
84  compareDx(x_dfad.dx(i), x_ls.dx(i));
85 
86  // Compare fastAccessDx
87  compareDx(x_dfad.fastAccessDx(i), x_ls.fastAccessDx(i));
88  }
89  }
90 
91  // Assert two doubles are the same to relative precision
92  void compareDoubles(double a, double b) {
93  ASSERT_TRUE( fabs(a-b) < tol_a + tol_r*fabs(a) );
94  }
95 
96  // Assert two bools are the same
97  void compareBools(bool a, bool b) {
98  ASSERT_TRUE( a == b );
99  }
100 
101  // Assert a double and bool are same (logically)
102  void compareDx(double a, bool b) {
103  ASSERT_TRUE( (a && b) || !(a || b) );
104  }
105 
106  template <typename ScalarT>
107  ScalarT composite1(const ScalarT& a, const ScalarT& b) {
108  ScalarT t1 = 3. * a + sin(b) / log(fabs(a - b * 7.));
109  ScalarT t2 = 1.0e3;
110  ScalarT t3 = 5.7e4;
111  ScalarT t4 = 3.2e5;
112  t1 *= cos(a + exp(t1)) / 6. - tan(t1*sqrt(abs(a * log10(abs(b)))));
113  t1 -= acos((6.+asin(pow(fabs(a),b)/t2))/t3) * asin(pow(fabs(b),2.)*1.0/t4) * atan((b*pow(2.,log(abs(a))))/(t3*t4));
114  t1 /= cosh(b - 0.7) + 7.*sinh(t1 + 0.8)*tanh(9./a) - 9.;
115  t1 += pow(abs(a*4.),b-8.)/cos(a*b*a);
116 
117  return t1;
118  }
119 
120 }; // class LogicalSparseOpsUnitTest
121 
122 #define BINARY_OP_TEST(TESTNAME,OP) \
123  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
124  c_dfad = a_dfad OP b_dfad; \
125  c_ls = a_ls OP b_ls; \
126  compareFads(c_dfad, c_ls); \
127  \
128  double val = urand.number(); \
129  c_dfad = a_dfad OP val; \
130  c_ls = a_ls OP val; \
131  compareFads(c_dfad, c_ls); \
132  \
133  c_dfad = val OP b_dfad; \
134  c_ls = val OP b_ls; \
135  compareFads(c_dfad, c_ls); \
136  }
137 
138 #define RELOP_TEST(TESTNAME,OP) \
139  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
140  bool r1 = a_dfad OP b_dfad; \
141  bool r2 = a_ls OP b_ls; \
142  ASSERT_TRUE(r1 == r2); \
143  \
144  double val = urand.number(); \
145  r1 = a_dfad OP val; \
146  r2 = a_ls OP val; \
147  ASSERT_TRUE(r1 == r2); \
148  \
149  r1 = val OP b_dfad; \
150  r2 = val OP b_ls; \
151  ASSERT_TRUE(r1 == r2); \
152  }
153 
154 #define BINARY_FUNC_TEST(TESTNAME,FUNC) \
155  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
156  c_dfad = FUNC (a_dfad,b_dfad); \
157  c_ls = FUNC (a_ls,b_ls); \
158  compareFads(c_dfad, c_ls); \
159  \
160  double val = urand.number(); \
161  c_dfad = FUNC (a_dfad,val); \
162  c_ls = FUNC (a_ls,val); \
163  compareFads(c_dfad, c_ls); \
164  \
165  c_dfad = FUNC (val,b_dfad); \
166  c_ls = FUNC (val,b_ls); \
167  compareFads(c_dfad, c_ls); \
168  }
169 
170 #define UNARY_OP_TEST(TESTNAME,OP) \
171  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
172  c_dfad = OP a_dfad; \
173  c_ls = OP a_ls; \
174  compareFads(c_dfad, c_ls); \
175  }
176 
177 #define UNARY_FUNC_TEST(TESTNAME,FUNC) \
178  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
179  c_dfad = FUNC (a_dfad); \
180  c_ls = FUNC (a_ls); \
181  compareFads(c_dfad, c_ls); \
182  }
183 
184 #define UNARY_ASSIGNOP_TEST(TESTNAME,OP) \
185  TEST_F(LogicalSparseOpsUnitTest, TESTNAME) { \
186  c_dfad OP a_dfad; \
187  c_ls OP a_ls; \
188  compareFads(c_dfad, c_ls); \
189  \
190  double val = urand.number(); \
191  c_dfad OP val; \
192  c_ls OP val; \
193  compareFads(c_dfad, c_ls); \
194  }
195 
196 BINARY_OP_TEST(testAddition, +)
197 BINARY_OP_TEST(testSubtraction, -)
198 BINARY_OP_TEST(testMultiplication, *)
199 BINARY_OP_TEST(testDivision, /)
200 
201 RELOP_TEST(testEquals, ==)
202 RELOP_TEST(testNotEquals, !=)
203 RELOP_TEST(testLessThanOrEquals, <=)
204 RELOP_TEST(testGreaterThanOrEquals, >=)
205 RELOP_TEST(testLessThan, <)
206 RELOP_TEST(testGreaterThan, >)
207 
208 BINARY_FUNC_TEST(testPow, pow)
209 
210 UNARY_OP_TEST(testUnaryPlus, +)
211 UNARY_OP_TEST(testUnaryMinus, -)
212 
213 UNARY_FUNC_TEST(testExp, exp)
214 UNARY_FUNC_TEST(testLog, log)
215 UNARY_FUNC_TEST(testLog10, log10)
216 UNARY_FUNC_TEST(testSqrt, sqrt)
217 UNARY_FUNC_TEST(testCos, cos)
218 UNARY_FUNC_TEST(testSin, sin)
219 UNARY_FUNC_TEST(testTan, tan)
220 UNARY_FUNC_TEST(testACos, acos)
221 UNARY_FUNC_TEST(testASin, asin)
222 UNARY_FUNC_TEST(testATan, atan)
223 UNARY_FUNC_TEST(testCosh, cosh)
224 UNARY_FUNC_TEST(testSinh, sinh)
225 UNARY_FUNC_TEST(testTanh, tanh)
226 UNARY_FUNC_TEST(testAbs, abs)
227 UNARY_FUNC_TEST(testFAbs, fabs)
228 
229 UNARY_ASSIGNOP_TEST(testPlusEquals, +=)
230 UNARY_ASSIGNOP_TEST(testMinusEquals, -=)
231 UNARY_ASSIGNOP_TEST(testTimesEquals, *=)
232 UNARY_ASSIGNOP_TEST(testDivideEquals, /=)
233 
235  c_dfad = composite1(a_dfad, b_dfad);
236  c_ls = composite1(a_ls, b_ls);
237  compareFads(c_dfad, c_ls);
238 }
239 
241  DFadType aa_dfad = a_dfad;
242  LSType aa_ls = a_ls;
243  aa_dfad = 1.0;
244  aa_ls = 1.0;
245  aa_dfad = aa_dfad + b_dfad;
246  aa_ls = aa_ls + b_ls;
247  compareFads(aa_dfad, aa_ls);
248 }
249 
251  DFadType aa_dfad = a_dfad;
252  LSType aa_ls = a_ls;
253  aa_dfad = 1.0;
254  aa_ls = 1.0;
255  aa_dfad = aa_dfad - b_dfad;
256  aa_ls = aa_ls - b_ls;
257  compareFads(aa_dfad, aa_ls);
258 }
259 
261  DFadType aa_dfad = a_dfad;
262  LSType aa_ls = a_ls;
263  aa_dfad = 2.0;
264  aa_ls = 2.0;
265  aa_dfad = aa_dfad * b_dfad;
266  aa_ls = aa_ls * b_ls;
267  compareFads(aa_dfad, aa_ls);
268 }
269 
271  DFadType aa_dfad = a_dfad;
272  LSType aa_ls = a_ls;
273  aa_dfad = 2.0;
274  aa_ls = 2.0;
275  aa_dfad = aa_dfad / b_dfad;
276  aa_ls = aa_ls / b_ls;
277  compareFads(aa_dfad, aa_ls);
278 }
279 
281  double val;
282 
283  // LFAd, LFad
284  LSType aa_ls = a_ls + 1.0;
285  c_ls = max(aa_ls, a_ls);
286  compareDoubles(c_ls.val(), aa_ls.val());
287  for (int i=0; i<n; i++) {
288  compareBools(c_ls.dx(i), aa_ls.dx(i));
289  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
290  }
291  c_ls = max(a_ls, aa_ls);
292  compareDoubles(c_ls.val(), aa_ls.val());
293  for (int i=0; i<n; i++) {
294  compareBools(c_ls.dx(i), aa_ls.dx(i));
295  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
296  }
297 
298  // Expr, LFad
299  c_ls = max(a_ls+1.0, a_ls);
300  compareDoubles(c_ls.val(), aa_ls.val());
301  for (int i=0; i<n; i++) {
302  compareBools(c_ls.dx(i), aa_ls.dx(i));
303  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
304  }
305  c_ls = max(a_ls, a_ls+1.0);
306  compareDoubles(c_ls.val(), aa_ls.val());
307  for (int i=0; i<n; i++) {
308  compareBools(c_ls.dx(i), aa_ls.dx(i));
309  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
310  }
311 
312  // Expr, Expr (same)
313  c_ls = max(a_ls+1.0, a_ls+1.0);
314  compareDoubles(c_ls.val(), aa_ls.val());
315  for (int i=0; i<n; i++) {
316  compareBools(c_ls.dx(i), aa_ls.dx(i));
317  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
318  }
319 
320  // Expr, Expr (different)
321  c_ls = max(a_ls+1.0, a_ls-1.0);
322  compareDoubles(c_ls.val(), aa_ls.val());
323  for (int i=0; i<n; i++) {
324  compareBools(c_ls.dx(i), aa_ls.dx(i));
325  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
326  }
327  c_ls = max(a_ls-1.0, a_ls+1.0);
328  compareDoubles(c_ls.val(), aa_ls.val());
329  for (int i=0; i<n; i++) {
330  compareBools(c_ls.dx(i), aa_ls.dx(i));
331  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
332  }
333 
334  // LFad, const
335  val = a_ls.val() + 1;
336  c_ls = max(a_ls, val);
337  compareDoubles(c_ls.val(), val);
338  for (int i=0; i<n; i++)
339  compareBools(c_ls.dx(i), 0);
340  val = a_ls.val() - 1;
341  c_ls = max(a_ls, val);
342  compareDoubles(c_ls.val(), a_ls.val());
343  for (int i=0; i<n; i++) {
344  compareBools(c_ls.dx(i), a_ls.dx(i));
345  compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
346  }
347  val = b_ls.val() + 1;
348  c_ls = max(val, b_ls);
349  compareDoubles(c_ls.val(), val);
350  for (int i=0; i<n; i++)
351  compareBools(c_ls.dx(i), 0);
352  val = b_ls.val() - 1;
353  c_ls = max(val, b_ls);
354  compareDoubles(c_ls.val(), b_ls.val());
355  for (int i=0; i<n; i++) {
356  compareBools(c_ls.dx(i), b_ls.dx(i));
357  compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
358  }
359 
360  // Expr, const
361  val = a_ls.val();
362  c_ls = max(a_ls+1.0, val);
363  compareDoubles(c_ls.val(), aa_ls.val());
364  for (int i=0; i<n; i++) {
365  compareBools(c_ls.dx(i), aa_ls.dx(i));
366  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
367  }
368  c_ls = max(val, a_ls+1.0);
369  compareDoubles(c_ls.val(), aa_ls.val());
370  for (int i=0; i<n; i++) {
371  compareBools(c_ls.dx(i), aa_ls.dx(i));
372  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
373  }
374 }
375 
377  double val;
378 
379  // LFad, LFad
380  LSType aa_ls = a_ls - 1.0;
381  c_ls = min(aa_ls, a_ls);
382  compareDoubles(c_ls.val(), aa_ls.val());
383  for (int i=0; i<n; i++) {
384  compareBools(c_ls.dx(i), aa_ls.dx(i));
385  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
386  }
387  c_ls = min(a_ls, aa_ls);
388  compareDoubles(c_ls.val(), aa_ls.val());
389  for (int i=0; i<n; i++) {
390  compareBools(c_ls.dx(i), aa_ls.dx(i));
391  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
392  }
393 
394  // Expr, LFad
395  c_ls = min(a_ls-1.0, a_ls);
396  compareDoubles(c_ls.val(), aa_ls.val());
397  for (int i=0; i<n; i++) {
398  compareBools(c_ls.dx(i), aa_ls.dx(i));
399  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
400  }
401  c_ls = min(a_ls, a_ls-1.0);
402  compareDoubles(c_ls.val(), aa_ls.val());
403  for (int i=0; i<n; i++) {
404  compareBools(c_ls.dx(i), aa_ls.dx(i));
405  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
406  }
407 
408  // Expr, Expr (same)
409  c_ls = min(a_ls-1.0, a_ls-1.0);
410  compareDoubles(c_ls.val(), aa_ls.val());
411  for (int i=0; i<n; i++) {
412  compareBools(c_ls.dx(i), aa_ls.dx(i));
413  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
414  }
415 
416  // Expr, Expr (different)
417  c_ls = min(a_ls+1.0, a_ls-1.0);
418  compareDoubles(c_ls.val(), aa_ls.val());
419  for (int i=0; i<n; i++) {
420  compareBools(c_ls.dx(i), aa_ls.dx(i));
421  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
422  }
423  c_ls = min(a_ls-1.0, a_ls+1.0);
424  compareDoubles(c_ls.val(), aa_ls.val());
425  for (int i=0; i<n; i++) {
426  compareBools(c_ls.dx(i), aa_ls.dx(i));
427  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
428  }
429 
430  // LFad, const
431  val = a_ls.val() - 1;
432  c_ls = min(a_ls, val);
433  compareDoubles(c_ls.val(), val);
434  for (int i=0; i<n; i++)
435  compareBools(c_ls.dx(i), 0);
436  val = a_ls.val() + 1;
437  c_ls = min(a_ls, val);
438  compareDoubles(c_ls.val(), a_ls.val());
439  for (int i=0; i<n; i++) {
440  compareBools(c_ls.dx(i), a_ls.dx(i));
441  compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
442  }
443  val = b_ls.val() - 1;
444  c_ls = min(val, b_ls);
445  compareDoubles(c_ls.val(), val);
446  for (int i=0; i<n; i++)
447  compareBools(c_ls.dx(i), 0);
448  val = b_ls.val() + 1;
449  c_ls = min(val, b_ls);
450  compareDoubles(c_ls.val(), b_ls.val());
451  for (int i=0; i<n; i++) {
452  compareBools(c_ls.dx(i), b_ls.dx(i));
453  compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
454  }
455 
456  // Expr, const
457  val = a_ls.val();
458  c_ls = min(a_ls-1.0, val);
459  compareDoubles(c_ls.val(), aa_ls.val());
460  for (int i=0; i<n; i++) {
461  compareBools(c_ls.dx(i), aa_ls.dx(i));
462  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
463  }
464  c_ls = min(val, a_ls-1.0);
465  compareDoubles(c_ls.val(), aa_ls.val());
466  for (int i=0; i<n; i++) {
467  compareBools(c_ls.dx(i), aa_ls.dx(i));
468  compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
469  }
470 }
void compareDoubles(double a, double b)
void compareDx(double a, bool b)
asin(expr.val())
cosh(expr.val())
#define UNARY_OP_TEST(TESTNAME, OP)
abs(expr.val())
#define BINARY_FUNC_TEST(TESTNAME, FUNC)
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2379
Sacado::LFad::LogicalSparse< double, bool > LSType
void compareBools(bool a, bool b)
atan(expr.val())
#define UNARY_FUNC_TEST(TESTNAME, FUNC)
ScalarT number()
Get random number.
expr val()
ScalarT composite1(const ScalarT &a, const ScalarT &b)
tanh(expr.val())
#define ASSERT_TRUE(condition)
Definition: gtest.h:1985
#define RELOP_TEST(TESTNAME, OP)
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
#define BINARY_OP_TEST(TESTNAME, OP)
sqrt(expr.val())
sinh(expr.val())
tan(expr.val())
Sacado::Fad::DFad< double > DFadType
sin(expr.val())
log(expr.val())
acos(expr.val())
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
#define UNARY_ASSIGNOP_TEST(TESTNAME, OP)
exp(expr.val())
Sacado::Random< double > urand
fabs(expr.val())
int n
void compareFads(const DFadType &x_dfad, const LSType &x_ls)
log10(expr.val())
cos(expr.val())