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 "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"
38#include "Sacado_mpl_apply.hpp"
40
41// Some classes for testing mpl::is_convertible<From,To>
42struct A {};
43struct B {
44 B() {}
45 B(const A&) {}
46};
47struct C : public A {};
48
49// Size used for all Fad types
50const int global_fad_size = 10;
51
52// Test is_convertible<From,To> behaves as expected
53TEUCHOS_UNIT_TEST( Conversion, IsConvertible )
54{
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
69template <typename ad_type>
70bool 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
118TEUCHOS_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
163TEUCHOS_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
179TEUCHOS_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
195TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DFadType )
196TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SLFadType )
197TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SFadType )
198TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_DVFadType )
199TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, Fad_SimpleFadType )
200TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, Fad_VFadType )
201
202typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
203typedef Sacado::ELRFad::SLFad<double,global_fad_size> ELRFad_SLFadType;
204typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
205typedef Sacado::ELRFad::ViewFad<double,global_fad_size,1,ELRFad_DFadType> ELRFad_VFadType;
206TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_DFadType )
207TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SLFadType )
208TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRFad_SFadType )
209TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRFad_VFadType )
210
211typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
212typedef Sacado::CacheFad::SLFad<double,global_fad_size> CacheFad_SLFadType;
213typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
214typedef Sacado::CacheFad::ViewFad<double,global_fad_size,1,CacheFad_DFadType> CacheFad_VFadType;
215TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_DFadType )
216TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SLFadType )
217TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, CacheFad_SFadType )
218TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, CacheFad_VFadType )
219
220typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
221typedef Sacado::ELRCacheFad::SLFad<double,global_fad_size> ELRCacheFad_SLFadType;
222typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
223typedef Sacado::ELRCacheFad::ViewFad<double,global_fad_size,1,ELRCacheFad_DFadType> ELRCacheFad_VFadType;
224TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_DFadType )
225TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SLFadType )
226TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ADConversions, ELRCacheFad_SFadType )
227TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, ViewConversions, ELRCacheFad_VFadType )
228
229// The dx() tests in ADConversions don't make sense for these types.
230// They also need more than the default constructor, and aren't designed to be
231// nested.
232typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
233typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
234typedef Sacado::Tay::Taylor<double> TaylorType;
235typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
236typedef Sacado::Rad::ADvar<double> RadType;
237typedef Sacado::Rad2::ADvar<double> Rad2Type;
238typedef Sacado::RadVec::ADvar<double> RadVecType;
239TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, LFadType )
240TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, SFCType )
241TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, TaylorType )
242TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, CacheTaylorType )
243TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadType )
244TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, Rad2Type )
245TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Conversion, OtherConversions, RadVecType )
246
247int main( int argc, char* argv[] ) {
248 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
249
250 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
251}
Sacado::Fad::SLFad< double, global_fad_size > Fad_SLFadType
bool test_ad_conversions(Teuchos::FancyOStream &out)
Sacado::Fad::ViewFad< double, global_fad_size, 1, Fad_DFadType > Fad_VFadType
Sacado::Fad::DFad< double > Fad_DFadType
Sacado::Fad::DVFad< double > Fad_DVFadType
Sacado::Fad::SFad< double, global_fad_size > Fad_SFadType
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Conversion, ADConversions, AD)
Sacado::Fad::SimpleFad< double > Fad_SimpleFadType
const int global_fad_size
TEUCHOS_UNIT_TEST(Conversion, IsConvertible)
int main()
Definition: ad_example.cpp:191
Forward-mode AD class using dynamic memory allocation and expression templates.
Forward-mode AD class using dynamic memory allocation but no expression templates.
Forward-mode AD class using dynamic memory allocation.
Uncopyable z
const double y
B(const A &)
Determine whether a given type is a view.
F::template apply< A1, A2, A3, A4, A5 >::type type