Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
32#include "Teuchos_UnitTestHarness.hpp"
33#include "Teuchos_UnitTestRepository.hpp"
34#include "Teuchos_GlobalMPISession.hpp"
35#include "Teuchos_TestingHelpers.hpp"
36
37#include "Sacado_No_Kokkos.hpp"
40#include "Sacado_mpl_apply.hpp"
41
42// Some classes for testing std::is_convertible<From,To>
43struct A {};
44struct B {
45 B() {}
46 B(const A&) {}
47};
48struct C : public A {};
49
50// Size used for all Fad types
51const int global_fad_size = 10;
52
53// Test is_convertible<From,To> behaves as expected
54TEUCHOS_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
70template <typename ad_type>
71bool test_ad_conversions(Teuchos::FancyOStream& out)
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 =
78 std::is_convertible<value_type,ad_type>::value;
79 const bool is_ad_value =
80 std::is_convertible<ad_type,value_type>::value;
81 const bool is_scalar_ad =
82 std::is_convertible<scalar_type,ad_type>::value;
83 const bool is_ad_scalar =
84 std::is_convertible<ad_type,scalar_type>::value;
85 const bool is_not_view = ! Sacado::IsView<ad_type>::value;
86
87 const bool is_int_ad =
88 std::is_convertible<value_type,ad_type>::value;
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 =
103 std::is_convertible<ad_expr_type,ad_type>::value;
104 const bool is_val_expr_ad =
105 std::is_convertible<val_expr_type,ad_type>::value;
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
117TEUCHOS_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
162TEUCHOS_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
178TEUCHOS_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
194TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DFadType )
195TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SLFadType )
196TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SFadType )
197TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DVFadType )
198TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SimpleFadType )
199TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, Fad_VFadType )
200
201typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
202typedef Sacado::ELRFad::SLFad<double,global_fad_size> ELRFad_SLFadType;
203typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
204typedef Sacado::ELRFad::ViewFad<double,global_fad_size,1,ELRFad_DFadType> ELRFad_VFadType;
205TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_DFadType )
206TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SLFadType )
207TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SFadType )
208TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRFad_VFadType )
209
210typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
211typedef Sacado::CacheFad::SLFad<double,global_fad_size> CacheFad_SLFadType;
212typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
213typedef Sacado::CacheFad::ViewFad<double,global_fad_size,1,CacheFad_DFadType> CacheFad_VFadType;
214TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_DFadType )
215TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SLFadType )
216TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SFadType )
217TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, CacheFad_VFadType )
218
219typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
220typedef Sacado::ELRCacheFad::SLFad<double,global_fad_size> ELRCacheFad_SLFadType;
221typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
222typedef Sacado::ELRCacheFad::ViewFad<double,global_fad_size,1,ELRCacheFad_DFadType> ELRCacheFad_VFadType;
223TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_DFadType )
224TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SLFadType )
225TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SFadType )
226TEUCHOS_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.
231typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
232typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
233typedef Sacado::Tay::Taylor<double> TaylorType;
234typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
235typedef Sacado::Rad::ADvar<double> RadType;
236typedef Sacado::Rad2::ADvar<double> Rad2Type;
237typedef Sacado::RadVec::ADvar<double> RadVecType;
238TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, LFadType )
239TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, SFCType )
240TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, TaylorType )
241TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, CacheTaylorType )
242TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadType )
243TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, Rad2Type )
244TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadVecType )
245
246int main( int argc, char* argv[] ) {
247 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
248
249 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
250}
Sacado::CacheFad::SLFad< double, 10 > Fad_SLFadType
Sacado::CacheFad::SFad< double, 5 > Fad_SFadType
Sacado::CacheFad::DFad< double > Fad_DFadType
const int global_fad_size
Sacado::Tay::Taylor< double > TaylorType
bool test_ad_conversions(Teuchos::FancyOStream &out)
Sacado::ELRFad::SLFad< double, global_fad_size > ELRFad_SLFadType
Sacado::ELRCacheFad::ViewFad< double, global_fad_size, 1, ELRCacheFad_DFadType > ELRCacheFad_VFadType
Sacado::Fad::ViewFad< double, global_fad_size, 1, Fad_DFadType > Fad_VFadType
Sacado::ELRCacheFad::SLFad< double, global_fad_size > ELRCacheFad_SLFadType
Sacado::ELRFad::DFad< double > ELRFad_DFadType
Sacado::CacheFad::ViewFad< double, global_fad_size, 1, CacheFad_DFadType > CacheFad_VFadType
Sacado::CacheFad::DFad< double > CacheFad_DFadType
Sacado::ELRFad::SFad< double, global_fad_size > ELRFad_SFadType
Sacado::Tay::CacheTaylor< double > CacheTaylorType
Sacado::Fad::DVFad< double > Fad_DVFadType
Sacado::RadVec::ADvar< double > RadVecType
Sacado::CacheFad::SLFad< double, global_fad_size > CacheFad_SLFadType
Sacado::ELRCacheFad::DFad< double > ELRCacheFad_DFadType
Sacado::Rad2::ADvar< double > Rad2Type
Sacado::LFad::LogicalSparse< double, bool > LFadType
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Conversion, ADConversions, AD)
Sacado::FlopCounterPack::ScalarFlopCounter< double > SFCType
Sacado::Fad::SimpleFad< double > Fad_SimpleFadType
Sacado::ELRCacheFad::SFad< double, global_fad_size > ELRCacheFad_SFadType
Sacado::CacheFad::SFad< double, global_fad_size > CacheFad_SFadType
Sacado::ELRFad::ViewFad< double, global_fad_size, 1, ELRFad_DFadType > ELRFad_VFadType
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
Forward-mode AD class using dynamic memory allocation and expression templates.
Forward-mode AD class using dynamic memory allocation but no expression templates.
int main(int argc, char *argv[])
Sacado::Rad::ADvar< double > RadType
B(const A &)
static const bool value
F::template apply< mpl::none, mpl::none, mpl::none, mpl::none, mpl::none >::type type