Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PromoteTests.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 // This test requires C++11 (for static_assert), so why not use the
11 // standard type traits
12 #include <type_traits>
13 #include <utility>
18 
19 #include "Sacado_No_Kokkos.hpp"
20 #include "Sacado_Fad_SimpleFad.hpp"
22 #include "Sacado_mpl_apply.hpp"
23 
24 template <typename ad_type>
25 bool testADPromote() {
26  using Sacado::Promote;
27  using std::is_same;
28 
29  typedef typename Sacado::ValueType<ad_type>::type value_type;
30  typedef typename Sacado::ScalarType<ad_type>::type scalar_type;
31 
32  // Get the type of the result of the expression '- ad_type'
33  // The use of declval gets around actually instantiation objects of type
34  // ad_type.
35  // We use a unary expression to catch special-case problems with Promote
36  // since the AD type may be convertible to the expression type
37  typedef decltype(-std::declval<ad_type>()) expr_type;
38 
39  static_assert(
40  is_same<typename Promote<ad_type,ad_type>::type, ad_type >::value,
41  "Promote<ad_type,ad_type>::type != ad_type");
42 
43  static_assert(
44  is_same<typename Promote<ad_type,value_type>::type, ad_type >::value,
45  "Promote<ad_type,value_type>::type != ad_type");
46 
47  static_assert(
48  is_same<typename Promote<value_type,ad_type>::type, ad_type >::value,
49  "Promote<value_type,ad_type>::type != ad_type");
50 
51  static_assert(
52  is_same<typename Promote<ad_type,scalar_type>::type, ad_type >::value,
53  "Promote<ad_type,scalar_type>::type != ad_type");
54 
55  static_assert(
56  is_same<typename Promote<scalar_type,ad_type>::type, ad_type >::value,
57  "Promote<scalar_type,ad_type>::type != ad_type");
58 
59  static_assert(
60  is_same<typename Promote<ad_type,expr_type>::type, ad_type >::value,
61  "Promote<ad_type,expr_type>::type != ad_type");
62 
63  static_assert(
64  is_same<typename Promote<expr_type,ad_type>::type, ad_type >::value,
65  "Promote<expr_type,ad_type>::type != ad_type");
66 
67  static_assert(
68  is_same<typename Promote<expr_type,value_type>::type, ad_type >::value,
69  "Promote<expr_type,value_type>::type != ad_type");
70 
71  static_assert(
72  is_same<typename Promote<value_type,expr_type>::type, ad_type >::value,
73  "Promote<value_type,expr_type>::type != ad_type");
74 
75  static_assert(
76  is_same<typename Promote<expr_type,scalar_type>::type, ad_type >::value,
77  "Promote<expr_type,scalar_type>::type != ad_type");
78 
79  static_assert(
80  is_same<typename Promote<scalar_type,expr_type>::type, ad_type >::value,
81  "Promote<scalar_type,expr_type>::type != ad_type");
82 
83  // These tests are all compile-time tests, so if the test compiles,
84  // it passes...
85  return true;
86 }
87 
88 template <typename view_type>
90  using Sacado::Promote;
91  using std::is_same;
92 
93  typedef typename Sacado::ValueType<view_type>::type value_type;
94  typedef typename Sacado::ScalarType<view_type>::type scalar_type;
95  typedef typename view_type::base_fad_type base_fad_type;
96 
97  // Get the type of the result of the expression '- view_type'
98  // The use of declval gets around actually instantiation objects of type
99  // view_type.
100  // We use a unary expression to catch special-case problems with Promote
101  // since the AD type may be convertible to the expression type
102  typedef decltype(-std::declval<view_type>()) expr_type;
103 
104  static_assert(
105  is_same<typename Promote<view_type,view_type>::type, base_fad_type >::value,
106  "Promote<view_type,view_type>::type != base_fad_type");
107 
108  static_assert(
109  is_same<typename Promote<view_type,value_type>::type, base_fad_type >::value,
110  "Promote<view_type,value_type>::type != base_fad_type");
111 
112  static_assert(
113  is_same<typename Promote<value_type,view_type>::type, base_fad_type >::value,
114  "Promote<value_type,view_type>::type != base_fad_type");
115 
116  static_assert(
117  is_same<typename Promote<view_type,scalar_type>::type, base_fad_type >::value,
118  "Promote<view_type,scalar_type>::type != base_fad_type");
119 
120  static_assert(
121  is_same<typename Promote<scalar_type,view_type>::type, base_fad_type >::value,
122  "Promote<scalar_type,view_type>::type != base_fad_type");
123 
124  static_assert(
125  is_same<typename Promote<view_type,expr_type>::type, base_fad_type >::value,
126  "Promote<view_type,expr_type>::type != base_fad_type");
127 
128  static_assert(
129  is_same<typename Promote<expr_type,view_type>::type, base_fad_type >::value,
130  "Promote<expr_type,view_type>::type != base_fad_type");
131 
132  static_assert(
133  is_same<typename Promote<expr_type,value_type>::type, base_fad_type >::value,
134  "Promote<expr_type,value_type>::type != base_fad_type");
135 
136  static_assert(
137  is_same<typename Promote<value_type,expr_type>::type, base_fad_type >::value,
138  "Promote<value_type,expr_type>::type != base_fad_type");
139 
140  static_assert(
141  is_same<typename Promote<expr_type,scalar_type>::type, base_fad_type >::value,
142  "Promote<expr_type,scalar_type>::type != base_fad_type");
143 
144  static_assert(
145  is_same<typename Promote<scalar_type,expr_type>::type, base_fad_type >::value,
146  "Promote<scalar_type,expr_type>::type != base_fad_type");
147 
148  // These tests are all compile-time tests, so if the test compiles,
149  // it passes...
150  return true;
151 }
152 
153 template <typename fad_type>
155  using Sacado::Promote;
156  using std::is_same;
157 
158  typedef typename Sacado::ViewFadType<fad_type,0,1>::type view_fad_type;
159 
160  typedef typename Sacado::mpl::apply< fad_type, fad_type >::type fad_fad_type;
161  typedef typename Sacado::mpl::apply< view_fad_type, fad_type >::type view_fad_fad_type;
162  typedef typename Sacado::mpl::apply< view_fad_type, view_fad_type >::type view_view_fad_type;
163 
164  typedef typename view_fad_type::base_fad_type base_fad_type;
165  typedef typename view_fad_fad_type::base_fad_type base_fad_fad_type;
166 
167  testADPromote<fad_type>();
168  testADPromote<fad_fad_type>();
169  testViewPromote<view_fad_type>();
170  testViewPromote<view_fad_fad_type>();
171  testViewPromote<view_view_fad_type>();
172 
173  static_assert(
174  is_same<typename Promote<view_fad_type,fad_type>::type, fad_type >::value,
175  "Promote<view_fad_type,fad_type>::type != fad_type");
176 
177  static_assert(
178  is_same<typename Promote<fad_type,view_fad_type>::type, fad_type >::value,
179  "Promote<fad_type,view_fad_type>::type != fad_type");
180 
181  static_assert(
182  is_same<typename Promote<view_fad_fad_type,fad_fad_type>::type, fad_fad_type >::value,
183  "Promote<view_fad_fad_type,fad_fad_type>::type != fad_fad_type");
184 
185  static_assert(
186  is_same<typename Promote<fad_fad_type,view_fad_fad_type>::type, fad_fad_type >::value,
187  "Promote<fad_fad_type,view_fad_fad_type>::type != fad_fad_type");
188 
189  typedef decltype(std::declval<fad_type>()*std::declval<fad_type>()) fad_expr_type;
190  typedef decltype(std::declval<view_fad_type>()*std::declval<view_fad_type>()) view_fad_expr_type;
191 
192  static_assert(
193  is_same<typename Promote<view_fad_type,fad_expr_type>::type, base_fad_type >::value,
194  "Promote<view_fad_type,fad_expr_type>::type != base_fad_type");
195 
196  static_assert(
197  is_same<typename Promote<fad_expr_type,view_fad_type>::type, base_fad_type >::value,
198  "Promote<fad_expr_type,view_fad_type>::type != base_fad_type");
199 
200  static_assert(
201  is_same<typename Promote<fad_type,view_fad_expr_type>::type, fad_type >::value,
202  "Promote<fad_type,view_fad_expr_type>::type != fad_type");
203 
204  static_assert(
205  is_same<typename Promote<view_fad_expr_type,fad_type>::type, fad_type >::value,
206  "Promote<view_fad_expr_type,fad_type>::type != fad_type");
207 
208  typedef decltype(-std::declval<fad_fad_type>()) fad_fad_expr_type;
209  typedef decltype(-std::declval<view_fad_fad_type>()) view_fad_fad_expr_type;
210  typedef decltype(-std::declval<view_view_fad_type>()) view_view_fad_expr_type;
211 
212  static_assert(
213  is_same<typename Promote<view_fad_type,fad_fad_expr_type>::type, fad_fad_type >::value,
214  "Promote<view_fad_type,fad_fad_expr_type>::type != fad_fad_type");
215 
216  static_assert(
217  is_same<typename Promote<fad_fad_expr_type,view_fad_type>::type, fad_fad_type >::value,
218  "Promote<fad_fad_expr_type,view_fad_type>::type != fad_fad_type");
219 
220  static_assert(
221  is_same<typename Promote<view_fad_type,view_fad_fad_expr_type>::type, base_fad_fad_type >::value,
222  "Promote<view_fad_type,view_fad_fad_expr_type>::type != base_fad_fad_type");
223 
224  static_assert(
225  is_same<typename Promote<view_fad_fad_expr_type,view_fad_type>::type, base_fad_fad_type >::value,
226  "Promote<view_fad_fad_expr_type,view_fad_type>::type != base_fad_fad_type");
227 
228  static_assert(
229  is_same<typename Promote<fad_type,view_view_fad_expr_type>::type, base_fad_fad_type >::value,
230  "Promote<fad_type,view_fad_fad_expr_type>::type != base_fad_fad_type");
231 
232  static_assert(
233  is_same<typename Promote<view_view_fad_expr_type,fad_type>::type, base_fad_fad_type >::value,
234  "Promote<view_fad_fad_expr_type,fad_type>::type != base_fad_fad_type");
235 
236  static_assert(
237  is_same<typename Promote<fad_expr_type,fad_fad_expr_type>::type, fad_fad_type >::value,
238  "Promote<fad_expr_type,fad_fad_expr_type>::type != fad_fad_type");
239 
240  static_assert(
241  is_same<typename Promote<fad_fad_expr_type,fad_expr_type>::type, fad_fad_type >::value,
242  "Promote<fad_fad_expr_type,fad_expr_type>::type != fad_fad_type");
243 
244  static_assert(
245  is_same<typename Promote<view_fad_expr_type,fad_fad_expr_type>::type, fad_fad_type >::value,
246  "Promote<view_fad_expr_type,fad_fad_expr_type>::type != fad_fad_type");
247 
248  static_assert(
249  is_same<typename Promote<fad_fad_expr_type,view_fad_expr_type>::type, fad_fad_type >::value,
250  "Promote<fad_fad_expr_type,view_fad_expr_type>::type != fad_fad_type");
251 
252  static_assert(
253  is_same<typename Promote<fad_expr_type,view_fad_fad_expr_type>::type, base_fad_fad_type >::value,
254  "Promote<fad_expr_type,view_fad_fad_expr_type>::type != base_fad_fad_type");
255 
256  static_assert(
257  is_same<typename Promote<view_fad_fad_expr_type,fad_expr_type>::type, base_fad_fad_type >::value,
258  "Promote<view_fad_fad_expr_type,fad_expr_type>::type != base_fad_fad_type");
259 
260  static_assert(
261  is_same<typename Promote<view_fad_expr_type,view_fad_fad_expr_type>::type, base_fad_fad_type >::value,
262  "Promote<view_fad_expr_type,view_fad_fad_expr_type>::type != base_fad_fad_type");
263 
264  static_assert(
265  is_same<typename Promote<view_fad_fad_expr_type,view_fad_expr_type>::type, base_fad_fad_type >::value,
266  "Promote<view_fad_fad_expr_type,view_fad_expr_type>::type != base_fad_fad_type");
267 
268  static_assert(
269  is_same<typename Promote<fad_expr_type,view_view_fad_expr_type>::type, base_fad_fad_type >::value,
270  "Promote<fad_expr_type,view_view_fad_expr_type>::type != base_fad_fad_type");
271 
272  static_assert(
273  is_same<typename Promote<view_view_fad_expr_type,fad_expr_type>::type, base_fad_fad_type >::value,
274  "Promote<view_view_fad_expr_type,fad_expr_type>::type != base_fad_fad_type");
275 
276  static_assert(
277  is_same<typename Promote<view_fad_expr_type,view_view_fad_expr_type>::type, base_fad_fad_type >::value,
278  "Promote<view_fad_expr_type,view_view_fad_expr_type>::type != base_fad_fad_type");
279 
280  static_assert(
281  is_same<typename Promote<view_view_fad_expr_type,view_fad_expr_type>::type, base_fad_fad_type >::value,
282  "Promote<view_view_fad_expr_type,view_fad_expr_type>::type != base_fad_fad_type");
283 
284  // These tests are all compile-time tests, so if the test compiles,
285  // it passes...
286  return true;
287 }
288 
289 template <typename scalar_type>
290 bool testPromote() {
291  typedef Sacado::Fad::DFad<scalar_type> fad_scalar_type;
292 
293  testADPromote<scalar_type>();
294  testADPromote<fad_scalar_type>();
295 
296  // These tests are all compile-time tests, so if the test compiles,
297  // it passes...
298  return true;
299 }
300 
302 {
303  success = testFadPromote<FAD>();
304 }
305 
307 {
308  success = testPromote<AD>();
309 }
310 
311 const int global_fad_size = 10;
312 
318 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, Fad_SFadType )
319 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, Fad_SLFadType )
320 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, Fad_DVFadType )
321 
322 typedef Sacado::ELRFad::DFad<double> ELRFad_DFadType;
323 typedef Sacado::ELRFad::SLFad<double,2*global_fad_size> ELRFad_SLFadType;
324 typedef Sacado::ELRFad::SFad<double,global_fad_size> ELRFad_SFadType;
325 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRFad_DFadType )
326 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRFad_SFadType )
327 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRFad_SLFadType )
328 
329 typedef Sacado::CacheFad::DFad<double> CacheFad_DFadType;
330 typedef Sacado::CacheFad::SLFad<double,2*global_fad_size> CacheFad_SLFadType;
331 typedef Sacado::CacheFad::SFad<double,global_fad_size> CacheFad_SFadType;
332 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, CacheFad_DFadType )
333 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, CacheFad_SFadType )
334 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, CacheFad_SLFadType )
335 
336 typedef Sacado::ELRCacheFad::DFad<double> ELRCacheFad_DFadType;
337 typedef Sacado::ELRCacheFad::SLFad<double,2*global_fad_size> ELRCacheFad_SLFadType;
338 typedef Sacado::ELRCacheFad::SFad<double,global_fad_size> ELRCacheFad_SFadType;
339 typedef Sacado::ELRCacheFad::ViewFad<double,global_fad_size,1,ELRCacheFad_DFadType> ELRCacheFad_VFadType;
340 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRCacheFad_DFadType )
341 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRCacheFad_SFadType )
342 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Fad, ELRCacheFad_SLFadType )
343 
344 typedef Sacado::Fad::SimpleFad<double> SimpleFadType;
345 typedef Sacado::LFad::LogicalSparse<double,bool> LFadType;
346 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, SimpleFadType )
347 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, LFadType )
348 
349 typedef Sacado::FlopCounterPack::ScalarFlopCounter<double> SFCType;
350 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, SFCType )
351 
352 typedef Sacado::Tay::Taylor<double> TaylorType;
353 typedef Sacado::Tay::CacheTaylor<double> CacheTaylorType;
354 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, TaylorType )
355 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, CacheTaylorType )
356 
357 typedef Sacado::Rad::ADvar<double> RadType;
358 typedef Sacado::Rad2::ADvar<double> Rad2Type;
359 typedef Sacado::RadVec::ADvar<double> RadVecType;
360 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, RadType )
361 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, Rad2Type )
362 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Promote, Other, RadVecType )
363 
364 int main( int argc, char* argv[] ) {
365  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
367 }
Sacado::CacheFad::SLFad< double, 10 > Fad_SLFadType
bool testViewPromote()
Forward-mode AD class using dynamic memory allocation.
const int global_fad_size
Sacado::CacheFad::DFad< double > Fad_DFadType
GeneralFad< StaticStorage< T, Num > > SLFad
bool testFadPromote()
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_TEMPLATE_1_DECL(ConditionalReturnType, Fad, FAD)
Sacado::Fad::DVFad< double > Fad_DVFadType
static int runUnitTestsFromMain(int argc, char *argv[])
GeneralFad< DynamicStorage< T > > DFad
int main()
Definition: ad_example.cpp:171
bool testADPromote()
Forward-mode AD class using dynamic memory allocation and expression templates.
int value
bool testPromote()
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(TEST_GROUP, TEST_NAME, TYPE)
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
GeneralFad< StaticFixedStorage< T, Num > > SFad
Base template specification for Promote.
Sacado::CacheFad::SFad< double, 5 > Fad_SFadType
Get view type for any Fad type.