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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #include <type_traits>
31 
36 
37 #include "Sacado_No_Kokkos.hpp"
38 #include "Sacado_Fad_SimpleFad.hpp"
40 #include "Sacado_mpl_apply.hpp"
41 
42 // Some classes for testing std::is_convertible<From,To>
43 struct A {};
44 struct B {
45  B() {}
46  B(const A&) {}
47 };
48 struct C : public A {};
49 
50 // Size used for all Fad types
51 const int global_fad_size = 10;
52 
53 // Test is_convertible<From,To> behaves as expected
54 TEUCHOS_UNIT_TEST( Conversion, IsConvertible )
55 {
56  const bool is_b_a = std::is_convertible<B,A>::value;
57  const bool is_a_b = std::is_convertible<A,B>::value;
58  const bool is_c_a = std::is_convertible<C,A>::value;
59  const bool is_int_double = std::is_convertible<int,double>::value;
60  const bool is_double_int = std::is_convertible<double,int>::value;
61  const bool is_double_a = std::is_convertible<double,A>::value;
62  TEST_EQUALITY( is_b_a, false );
63  TEST_EQUALITY( is_a_b, true );
64  TEST_EQUALITY( is_c_a, true );
65  TEST_EQUALITY( is_int_double, true );
66  TEST_EQUALITY( is_double_int, true );
67  TEST_EQUALITY( is_double_a, false );
68 }
69 
70 template <typename ad_type>
72 {
73  bool success = true;
74  typedef typename Sacado::ValueType<ad_type>::type value_type;
75  typedef typename Sacado::ScalarType<ad_type>::type scalar_type;
76 
77  const bool is_value_ad =
79  const bool is_ad_value =
81  const bool is_scalar_ad =
83  const bool is_ad_scalar =
85  const bool is_not_view = ! Sacado::IsView<ad_type>::value;
86 
87  const bool is_int_ad =
89 
90  TEST_EQUALITY( is_value_ad, is_not_view );
91  TEST_EQUALITY_CONST( is_ad_value, false );
92  TEST_EQUALITY( is_scalar_ad, is_not_view );
93  TEST_EQUALITY_CONST( is_ad_scalar, false );
94  TEST_EQUALITY( is_int_ad, is_not_view );
95 
96  // Get the type of the result of the expression 'ad_type * ad_type'
97  // The use of declval gets around actually instantiation objects of type
98  // ad_type.
99  typedef decltype(std::declval<ad_type>()*std::declval<ad_type>()) ad_expr_type;
100  typedef decltype(std::declval<value_type>()*std::declval<value_type>()) val_expr_type;
101 
102  const bool is_ad_expr_ad =
104  const bool is_val_expr_ad =
106 
107  TEST_EQUALITY( is_ad_expr_ad, is_not_view );
108  TEST_EQUALITY( is_val_expr_ad, is_not_view );
109 
110  // typedef typename ad_expr_type::value_type ad_expr_value_type;
111  // std::cout << typeid(ad_expr_value_type).name() << std::endl;
112 
113  return success;
114 }
115 
116 // Check various AD conversions work as expected
117 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ADConversions, AD )
118 {
119  typedef AD ad_type;
120  typedef typename ad_type::value_type value_type;
121  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
122 
123  success = true;
124  success = success && test_ad_conversions<ad_type>(out);
125  success = success && test_ad_conversions<ad_ad_type>(out);
126 
127  // Check value-type expression to nested fad-fad works
128  ad_type x(global_fad_size, value_type(1.5));
129  for (int i=0; i<global_fad_size; ++i)
130  x.fastAccessDx(i) = 2.0;
131  ad_ad_type y = x + x;
132  TEST_EQUALITY_CONST( y.val().val(), 3.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  // Check mixed value-type/Fad expression with nested fad-fad works
141  ad_ad_type z = (x + x) + y;
142  TEST_EQUALITY_CONST( z.val().val(), 6.0 );
143  for (int i=0; i<global_fad_size; ++i) {
144  TEST_EQUALITY_CONST( z.val().dx(i), 8.0 );
145  TEST_EQUALITY_CONST( z.dx(i).val(), 0.0 );
146  for (int j=0; j<global_fad_size; ++j)
147  TEST_EQUALITY_CONST( z.dx(i).dx(j), 0.0 );
148  }
149 
150  // Check mix-arithmetic with int's works
151  y += 1;
152  TEST_EQUALITY_CONST( y.val().val(), 4.0 );
153  for (int i=0; i<global_fad_size; ++i) {
154  TEST_EQUALITY_CONST( y.val().dx(i), 4.0 );
155  TEST_EQUALITY_CONST( y.dx(i).val(), 0.0 );
156  for (int j=0; j<global_fad_size; ++j)
157  TEST_EQUALITY_CONST( y.dx(i).dx(j), 0.0 );
158  }
159 }
160 
161 // Check various view conversions work as expected
162 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ViewConversions, AD )
163 {
164  typedef AD ad_type;
165  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
166 
167  success = true;
168  success = success && test_ad_conversions<ad_type>(out);
169  success = success && test_ad_conversions<ad_ad_type>(out);
170 
171  // ad_ad_type x;
172  // ad_ad_type y = x*x;
173 }
174 
175 // Check various other conversions work as expected
176 // These are for types that aren't expected to be nested, but may be nesteed
177 // inside other Fad types
178 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, OtherConversions, AD )
179 {
180  typedef AD ad_type;
181  typedef Sacado::Fad::DFad<ad_type> fad_ad_type;
182 
183  success = true;
184  success = success && test_ad_conversions<ad_type>(out);
185  success = success && test_ad_conversions<fad_ad_type>(out);
186 }
187 
194 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DFadType )
195 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SLFadType )
196 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SFadType )
197 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DVFadType )
198 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SimpleFadType )
199 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, Fad_VFadType )
200 
201 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
202 typedef Sacado::ELRFad::SLFad<double,global_fad_size> ELRFad_SLFadType;
203 typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
204 typedef Sacado::ELRFad::ViewFad<double,global_fad_size,1,ELRFad_DFadType> ELRFad_VFadType;
205 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_DFadType )
206 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SLFadType )
207 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SFadType )
208 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRFad_VFadType )
209 
210 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
211 typedef Sacado::CacheFad::SLFad<double,global_fad_size> CacheFad_SLFadType;
212 typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
213 typedef Sacado::CacheFad::ViewFad<double,global_fad_size,1,CacheFad_DFadType> CacheFad_VFadType;
214 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_DFadType )
215 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SLFadType )
216 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SFadType )
217 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, CacheFad_VFadType )
218 
219 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
220 typedef Sacado::ELRCacheFad::SLFad<double,global_fad_size> ELRCacheFad_SLFadType;
221 typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
222 typedef Sacado::ELRCacheFad::ViewFad<double,global_fad_size,1,ELRCacheFad_DFadType> ELRCacheFad_VFadType;
223 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_DFadType )
224 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SLFadType )
225 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SFadType )
226 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRCacheFad_VFadType )
227 
228 // The dx() tests in ADConversions don't make sense for these types.
229 // They also need more than the default constructor, and aren't designed to be
230 // nested.
231 typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
232 typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
233 typedef Sacado::Tay::Taylor<double> TaylorType;
234 typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
235 typedef Sacado::Rad::ADvar<double> RadType;
236 typedef Sacado::Rad2::ADvar<double> Rad2Type;
237 typedef Sacado::RadVec::ADvar<double> RadVecType;
238 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, LFadType )
239 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, SFCType )
240 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, TaylorType )
241 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, CacheTaylorType )
242 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadType )
243 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, Rad2Type )
244 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadVecType )
245 
246 int main( int argc, char* argv[] ) {
247  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
248 
250 }
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:191
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