Sacado Package Browser (Single Doxygen Collection)  Version of the Day
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 "Teuchos_UnitTestHarness.hpp"
31 #include "Teuchos_UnitTestRepository.hpp"
32 #include "Teuchos_GlobalMPISession.hpp"
33 #include "Teuchos_TestingHelpers.hpp"
34 
35 #include "Sacado_No_Kokkos.hpp"
36 #include "Sacado_Fad_SimpleFad.hpp"
38 #include "Sacado_mpl_apply.hpp"
40 
41 // Some classes for testing mpl::is_convertible<From,To>
42 struct A {};
43 struct B {
44  B() {}
45  B(const A&) {}
46 };
47 struct C : public A {};
48 
49 // Size used for all Fad types
50 const int global_fad_size = 10;
51 
52 // Test is_convertible<From,To> behaves as expected
53 TEUCHOS_UNIT_TEST( Conversion, IsConvertible )
54 {
55  const bool is_b_a = Sacado::mpl::is_convertible<B,A>::value;
56  const bool is_a_b = Sacado::mpl::is_convertible<A,B>::value;
57  const bool is_c_a = Sacado::mpl::is_convertible<C,A>::value;
58  const bool is_int_double = Sacado::mpl::is_convertible<int,double>::value;
59  const bool is_double_int = Sacado::mpl::is_convertible<double,int>::value;
60  const bool is_double_a = Sacado::mpl::is_convertible<double,A>::value;
61  TEST_EQUALITY( is_b_a, false );
62  TEST_EQUALITY( is_a_b, true );
63  TEST_EQUALITY( is_c_a, true );
64  TEST_EQUALITY( is_int_double, true );
65  TEST_EQUALITY( is_double_int, true );
66  TEST_EQUALITY( is_double_a, false );
67 }
68 
69 template <typename ad_type>
70 bool test_ad_conversions(Teuchos::FancyOStream& out)
71 {
72  bool success = true;
73  typedef typename Sacado::ValueType<ad_type>::type value_type;
74  typedef typename Sacado::ScalarType<ad_type>::type scalar_type;
75 
76  const bool is_value_ad =
78  const bool is_ad_value =
80  const bool is_scalar_ad =
82  const bool is_ad_scalar =
84  const bool is_not_view = ! Sacado::IsView<ad_type>::value;
85 
86  const bool is_int_ad =
88 
89  TEST_EQUALITY( is_value_ad, is_not_view );
90  TEST_EQUALITY_CONST( is_ad_value, false );
91  TEST_EQUALITY( is_scalar_ad, is_not_view );
92  TEST_EQUALITY_CONST( is_ad_scalar, false );
93  TEST_EQUALITY( is_int_ad, is_not_view );
94 
95 #ifdef HAVE_SACADO_CXX11
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 #endif
113 
114  return success;
115 }
116 
117 // Check various AD conversions work as expected
118 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ADConversions, AD )
119 {
120  typedef AD ad_type;
121  typedef typename ad_type::value_type value_type;
122  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
123 
124  success = true;
125  success = success && test_ad_conversions<ad_type>(out);
126  success = success && test_ad_conversions<ad_ad_type>(out);
127 
128  // Check value-type expression to nested fad-fad works
129  ad_type x(global_fad_size, value_type(1.5));
130  for (int i=0; i<global_fad_size; ++i)
131  x.fastAccessDx(i) = 2.0;
132  ad_ad_type y = x + x;
133  TEST_EQUALITY_CONST( y.val().val(), 3.0 );
134  for (int i=0; i<global_fad_size; ++i) {
135  TEST_EQUALITY_CONST( y.val().dx(i), 4.0 );
136  TEST_EQUALITY_CONST( y.dx(i).val(), 0.0 );
137  for (int j=0; j<global_fad_size; ++j)
138  TEST_EQUALITY_CONST( y.dx(i).dx(j), 0.0 );
139  }
140 
141  // Check mixed value-type/Fad expression with nested fad-fad works
142  ad_ad_type z = (x + x) + y;
143  TEST_EQUALITY_CONST( z.val().val(), 6.0 );
144  for (int i=0; i<global_fad_size; ++i) {
145  TEST_EQUALITY_CONST( z.val().dx(i), 8.0 );
146  TEST_EQUALITY_CONST( z.dx(i).val(), 0.0 );
147  for (int j=0; j<global_fad_size; ++j)
148  TEST_EQUALITY_CONST( z.dx(i).dx(j), 0.0 );
149  }
150 
151  // Check mix-arithmetic with int's works
152  y += 1;
153  TEST_EQUALITY_CONST( y.val().val(), 4.0 );
154  for (int i=0; i<global_fad_size; ++i) {
155  TEST_EQUALITY_CONST( y.val().dx(i), 4.0 );
156  TEST_EQUALITY_CONST( y.dx(i).val(), 0.0 );
157  for (int j=0; j<global_fad_size; ++j)
158  TEST_EQUALITY_CONST( y.dx(i).dx(j), 0.0 );
159  }
160 }
161 
162 // Check various view conversions work as expected
163 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, ViewConversions, AD )
164 {
165  typedef AD ad_type;
166  typedef typename Sacado::mpl::apply< ad_type, ad_type >::type ad_ad_type;
167 
168  success = true;
169  success = success && test_ad_conversions<ad_type>(out);
170  success = success && test_ad_conversions<ad_ad_type>(out);
171 
172  // ad_ad_type x;
173  // ad_ad_type y = x*x;
174 }
175 
176 // Check various other conversions work as expected
177 // These are for types that aren't expected to be nested, but may be nesteed
178 // inside other Fad types
179 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Conversion, OtherConversions, AD )
180 {
181  typedef AD ad_type;
182  typedef Sacado::Fad::DFad<ad_type> fad_ad_type;
183 
184  success = true;
185  success = success && test_ad_conversions<ad_type>(out);
186  success = success && test_ad_conversions<fad_ad_type>(out);
187 }
188 
196 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DFadType )
197 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SLFadType )
198 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SFadType )
199 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DMFadType )
200 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DVFadType )
201 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SimpleFadType )
202 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, Fad_VFadType )
203 
204 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
205 typedef Sacado::ELRFad::SLFad<double,global_fad_size> ELRFad_SLFadType;
206 typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
208 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_DFadType )
209 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SLFadType )
210 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SFadType )
211 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRFad_VFadType )
212 
213 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
214 typedef Sacado::CacheFad::SLFad<double,global_fad_size> CacheFad_SLFadType;
215 typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
217 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_DFadType )
218 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SLFadType )
219 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SFadType )
220 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, CacheFad_VFadType )
221 
222 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
223 typedef Sacado::ELRCacheFad::SLFad<double,global_fad_size> ELRCacheFad_SLFadType;
224 typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
226 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_DFadType )
227 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SLFadType )
228 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SFadType )
229 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRCacheFad_VFadType )
230 
231 // The dx() tests in ADConversions don't make sense for these types.
232 // They also need more than the default constructor, and aren't designed to be
233 // nested.
234 typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
235 typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
236 typedef Sacado::Tay::Taylor<double> TaylorType;
237 typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
238 typedef Sacado::Rad::ADvar<double> RadType;
239 typedef Sacado::Rad2::ADvar<double> Rad2Type;
240 typedef Sacado::RadVec::ADvar<double> RadVecType;
241 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, LFadType )
242 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, SFCType )
243 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, TaylorType )
244 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, CacheTaylorType )
245 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadType )
246 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, Rad2Type )
247 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadVecType )
248 
249 template <>
250 Sacado::Fad::MemPool* Sacado::Fad::MemPoolStorage<double>::defaultPool_ = NULL;
251 template <>
252 Sacado::Fad::MemPool* Sacado::Fad::MemPoolStorage< Sacado::Fad::DMFad<double> >::defaultPool_ = NULL;
253 
254 int main( int argc, char* argv[] ) {
255  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
256 
257  Sacado::Fad::MemPoolManager<double> poolManager(100);
260 
262  Sacado::Fad::MemPool *pool2 = poolManager2.getMemoryPool(global_fad_size);
263  Sacado::Fad::DMFad< Sacado::Fad::DMFad<double> >::setDefaultPool(pool2);
264 
265  return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
266 }
MemPool * getMemoryPool(unsigned int dim)
Get memory pool for supplied dimension dim.
int main(int argc, char *argv[])
Sacado::Fad::DMFad< double > Fad_DMFadType
Forward-mode AD class using dynamic memory allocation.
GeneralFad< StaticStorage< T, Num > > SLFad
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Conversion, ADConversions, AD)
Forward-mode AD class using dynamic memory allocation but no expression templates.
lambda< F >::type ::template apply< A1, A2, A3, A4, A5 >::type type
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
Sacado::Fad::DVFad< double > Fad_DVFadType
const int global_fad_size
Sacado::Fad::ViewFad< double, global_fad_size, 1, Fad_DFadType > Fad_VFadType
GeneralFad< DynamicStorage< T > > DFad
Determine whether a given type is a view.
Sacado::Fad::SFad< double, global_fad_size > Fad_SFadType
Derivative array storage class using dynamic memory allocation.
Sacado::Fad::SimpleFad< double > Fad_SimpleFadType
Sacado::Fad::SLFad< double, global_fad_size > Fad_SLFadType
Forward-mode AD class using dynamic memory allocation and expression templates.
Sacado::Fad::DFad< double > Fad_DFadType
B(const A &)
Forward-mode AD class using dynamic memory allocation and expression templates.
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
bool test_ad_conversions(Teuchos::FancyOStream &out)
GeneralFad< StaticFixedStorage< T, Num > > SFad