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