Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConversionTests.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 #include <type_traits>
11 
16 
17 #include "Sacado_No_Kokkos.hpp"
18 #include "Sacado_Fad_SimpleFad.hpp"
20 #include "Sacado_mpl_apply.hpp"
21 
22 // Some classes for testing std::is_convertible<From,To>
23 struct A {};
24 struct B {
25  B() {}
26  B(const A&) {}
27 };
28 struct C : public A {};
29 
30 // Size used for all Fad types
31 const int global_fad_size = 10;
32 
33 // Test is_convertible<From,To> behaves as expected
34 TEUCHOS_UNIT_TEST( Conversion, IsConvertible )
35 {
36  const bool is_b_a = std::is_convertible<B,A>::value;
37  const bool is_a_b = std::is_convertible<A,B>::value;
38  const bool is_c_a = std::is_convertible<C,A>::value;
39  const bool is_int_double = std::is_convertible<int,double>::value;
40  const bool is_double_int = std::is_convertible<double,int>::value;
41  const bool is_double_a = std::is_convertible<double,A>::value;
42  TEST_EQUALITY( is_b_a, false );
43  TEST_EQUALITY( is_a_b, true );
44  TEST_EQUALITY( is_c_a, true );
45  TEST_EQUALITY( is_int_double, true );
46  TEST_EQUALITY( is_double_int, true );
47  TEST_EQUALITY( is_double_a, false );
48 }
49 
50 template <typename ad_type>
52 {
53  bool success = true;
54  typedef typename Sacado::ValueType<ad_type>::type value_type;
55  typedef typename Sacado::ScalarType<ad_type>::type scalar_type;
56 
57  const bool is_value_ad =
59  const bool is_ad_value =
61  const bool is_scalar_ad =
63  const bool is_ad_scalar =
65  const bool is_not_view = ! Sacado::IsView<ad_type>::value;
66 
67  const bool is_int_ad =
69 
70  TEST_EQUALITY( is_value_ad, is_not_view );
71  TEST_EQUALITY_CONST( is_ad_value, false );
72  TEST_EQUALITY( is_scalar_ad, is_not_view );
73  TEST_EQUALITY_CONST( is_ad_scalar, false );
74  TEST_EQUALITY( is_int_ad, is_not_view );
75 
76  // Get the type of the result of the expression 'ad_type * ad_type'
77  // The use of declval gets around actually instantiation objects of type
78  // ad_type.
79  typedef decltype(std::declval<ad_type>()*std::declval<ad_type>()) ad_expr_type;
80  typedef decltype(std::declval<value_type>()*std::declval<value_type>()) val_expr_type;
81 
82  const bool is_ad_expr_ad =
84  const bool is_val_expr_ad =
86 
87  TEST_EQUALITY( is_ad_expr_ad, is_not_view );
88  TEST_EQUALITY( is_val_expr_ad, is_not_view );
89 
90  // typedef typename ad_expr_type::value_type ad_expr_value_type;
91  // std::cout << typeid(ad_expr_value_type).name() << std::endl;
92 
93  return success;
94 }
95 
96 // Check various AD conversions work as expected
97 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ADConversions, AD )
98 {
99  typedef AD ad_type;
100  typedef typename ad_type::value_type value_type;
101  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
102 
103  success = true;
104  success = success && test_ad_conversions<ad_type>(out);
105  success = success && test_ad_conversions<ad_ad_type>(out);
106 
107  // Check value-type expression to nested fad-fad works
108  ad_type x(global_fad_size, value_type(1.5));
109  for (int i=0; i<global_fad_size; ++i)
110  x.fastAccessDx(i) = 2.0;
111  ad_ad_type y = x + x;
112  TEST_EQUALITY_CONST( y.val().val(), 3.0 );
113  for (int i=0; i<global_fad_size; ++i) {
114  TEST_EQUALITY_CONST( y.val().dx(i), 4.0 );
115  TEST_EQUALITY_CONST( y.dx(i).val(), 0.0 );
116  for (int j=0; j<global_fad_size; ++j)
117  TEST_EQUALITY_CONST( y.dx(i).dx(j), 0.0 );
118  }
119 
120  // Check mixed value-type/Fad expression with nested fad-fad works
121  ad_ad_type z = (x + x) + y;
122  TEST_EQUALITY_CONST( z.val().val(), 6.0 );
123  for (int i=0; i<global_fad_size; ++i) {
124  TEST_EQUALITY_CONST( z.val().dx(i), 8.0 );
125  TEST_EQUALITY_CONST( z.dx(i).val(), 0.0 );
126  for (int j=0; j<global_fad_size; ++j)
127  TEST_EQUALITY_CONST( z.dx(i).dx(j), 0.0 );
128  }
129 
130  // Check mix-arithmetic with int's works
131  y += 1;
132  TEST_EQUALITY_CONST( y.val().val(), 4.0 );
133  for (int i=0; i<global_fad_size; ++i) {
134  TEST_EQUALITY_CONST( y.val().dx(i), 4.0 );
135  TEST_EQUALITY_CONST( y.dx(i).val(), 0.0 );
136  for (int j=0; j<global_fad_size; ++j)
137  TEST_EQUALITY_CONST( y.dx(i).dx(j), 0.0 );
138  }
139 }
140 
141 // Check various view conversions work as expected
142 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ViewConversions, AD )
143 {
144  typedef AD ad_type;
145  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
146 
147  success = true;
148  success = success && test_ad_conversions<ad_type>(out);
149  success = success && test_ad_conversions<ad_ad_type>(out);
150 
151  // ad_ad_type x;
152  // ad_ad_type y = x*x;
153 }
154 
155 // Check various other conversions work as expected
156 // These are for types that aren't expected to be nested, but may be nesteed
157 // inside other Fad types
158 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, OtherConversions, AD )
159 {
160  typedef AD ad_type;
161  typedef Sacado::Fad::DFad<ad_type> fad_ad_type;
162 
163  success = true;
164  success = success && test_ad_conversions<ad_type>(out);
165  success = success && test_ad_conversions<fad_ad_type>(out);
166 }
167 
174 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DFadType )
175 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SLFadType )
176 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SFadType )
177 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DVFadType )
178 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SimpleFadType )
179 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, Fad_VFadType )
180 
181 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
182 typedef Sacado::ELRFad::SLFad<double,global_fad_size> ELRFad_SLFadType;
183 typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
184 typedef Sacado::ELRFad::ViewFad<double,global_fad_size,1,ELRFad_DFadType> ELRFad_VFadType;
185 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_DFadType )
186 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SLFadType )
187 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SFadType )
188 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRFad_VFadType )
189 
190 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
191 typedef Sacado::CacheFad::SLFad<double,global_fad_size> CacheFad_SLFadType;
192 typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
193 typedef Sacado::CacheFad::ViewFad<double,global_fad_size,1,CacheFad_DFadType> CacheFad_VFadType;
194 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_DFadType )
195 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SLFadType )
196 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SFadType )
197 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, CacheFad_VFadType )
198 
199 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
200 typedef Sacado::ELRCacheFad::SLFad<double,global_fad_size> ELRCacheFad_SLFadType;
201 typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
202 typedef Sacado::ELRCacheFad::ViewFad<double,global_fad_size,1,ELRCacheFad_DFadType> ELRCacheFad_VFadType;
203 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_DFadType )
204 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SLFadType )
205 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SFadType )
206 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRCacheFad_VFadType )
207 
208 // The dx() tests in ADConversions don't make sense for these types.
209 // They also need more than the default constructor, and aren't designed to be
210 // nested.
211 typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
212 typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
213 typedef Sacado::Tay::Taylor<double> TaylorType;
214 typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
215 typedef Sacado::Rad::ADvar<double> RadType;
216 typedef Sacado::Rad2::ADvar<double> Rad2Type;
217 typedef Sacado::RadVec::ADvar<double> RadVecType;
218 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, LFadType )
219 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, SFCType )
220 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, TaylorType )
221 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, CacheTaylorType )
222 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadType )
223 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, Rad2Type )
224 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadVecType )
225 
226 int main( int argc, char* argv[] ) {
227  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
228 
230 }
Sacado::CacheFad::SLFad< double, 10 > Fad_SLFadType
Forward-mode AD class using dynamic memory allocation.
#define TEST_EQUALITY_CONST(v1, v2)
const int global_fad_size
Sacado::CacheFad::DFad< double > Fad_DFadType
GeneralFad< StaticStorage< T, Num > > SLFad
Forward-mode AD class using dynamic memory allocation but no expression templates.
F::template apply< A1, A2, A3, A4, A5 >::type type
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(ConditionalReturnType, Fad, FAD)
Sacado::Fad::DVFad< double > Fad_DVFadType
Sacado::Fad::ViewFad< double, global_fad_size, 1, Fad_DFadType > Fad_VFadType
static int runUnitTestsFromMain(int argc, char *argv[])
GeneralFad< DynamicStorage< T > > DFad
Determine whether a given type is a view.
int main()
Definition: ad_example.cpp:171
Sacado::Fad::SimpleFad< double > Fad_SimpleFadType
Forward-mode AD class using dynamic memory allocation and expression templates.
int value
Uncopyable z
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(TEST_GROUP, TEST_NAME, TYPE)
#define TEST_EQUALITY(v1, v2)
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
bool test_ad_conversions(Teuchos::FancyOStream &out)
GeneralFad< StaticFixedStorage< T, Num > > SFad
Sacado::CacheFad::SFad< double, 5 > Fad_SFadType
const double y