30 #ifndef SACADO_FAD_SCALARTRAITSIMP_HPP 31 #define SACADO_FAD_SCALARTRAITSIMP_HPP 33 #ifdef HAVE_SACADO_TEUCHOS 35 #include "Teuchos_ScalarTraits.hpp" 36 #include "Teuchos_SerializationTraits.hpp" 37 #include "Teuchos_SerializationTraitsHelpers.hpp" 38 #include "Teuchos_Assert.hpp" 39 #include "Teuchos_RCP.hpp" 40 #include "Teuchos_Array.hpp" 50 template <
typename FadType>
51 struct ScalarTraitsImp {
54 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
55 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
56 typedef typename mpl::apply<FadType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
58 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
59 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
60 static const bool isComparable =
61 Teuchos::ScalarTraits<ValueT>::isComparable;
62 static const bool hasMachineParameters =
63 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
64 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
65 return Teuchos::ScalarTraits<ValueT>::eps();
67 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
68 return Teuchos::ScalarTraits<ValueT>::sfmin();
70 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
71 return Teuchos::ScalarTraits<ValueT>::base();
73 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
74 return Teuchos::ScalarTraits<ValueT>::prec();
76 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
77 return Teuchos::ScalarTraits<ValueT>::t();
79 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType
rnd() {
82 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
83 return Teuchos::ScalarTraits<ValueT>::emin();
85 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
86 return Teuchos::ScalarTraits<ValueT>::rmin();
88 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
89 return Teuchos::ScalarTraits<ValueT>::emax();
91 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
92 return Teuchos::ScalarTraits<ValueT>::rmax();
94 static magnitudeType magnitude(
const FadType&
a) {
96 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
97 a,
"Error, the input value to magnitude(...) a = " <<
a <<
99 TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(
a) ==
false, std::runtime_error,
100 "Complex magnitude is not a differentiable " 101 "function of complex inputs.");
104 magnitudeType b(
a.size(),
105 Teuchos::ScalarTraits<ValueT>::magnitude(
a.val()));
106 if (Teuchos::ScalarTraits<ValueT>::real(
a.val()) >= 0)
107 for (
int i=0; i<
a.size(); i++)
109 Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessDx(i));
111 for (
int i=0; i<
a.size(); i++)
113 -Teuchos::ScalarTraits<ValueT>::magnitude(
a.fastAccessDx(i));
116 static ValueT zero() {
119 static ValueT one() {
126 TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) ==
false, std::runtime_error,
127 "Complex conjugate is not a differentiable " 128 "function of complex inputs.");
131 y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
138 TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) ==
false, std::runtime_error,
139 "Real component is not a differentiable " 140 "function of complex inputs.");
143 y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
150 TEUCHOS_TEST_FOR_EXCEPTION(is_fad_real(x) ==
false, std::runtime_error,
151 "Imaginary component is not a differentiable " 152 "function of complex inputs.");
154 return FadType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
157 static ValueT nan() {
158 return Teuchos::ScalarTraits<ValueT>::nan();
160 static bool isnaninf(
const FadType& x) {
161 if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.val()))
163 for (
int i=0; i<x.size(); i++)
164 if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.dx(i)))
168 static void seedrandom(
unsigned int s) {
169 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
171 static ValueT random() {
172 return Teuchos::ScalarTraits<ValueT>::random();
174 static std::string name() {
179 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
180 x,
"Error, the input value to squareroot(...) a = " << x <<
181 " can not be NaN!" );
190 static bool is_complex_real(
const ValueT& x) {
192 Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
196 static bool is_fad_real(
const FadType& x) {
199 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
200 if (!is_complex_real(x.val()))
202 for (
int i=0; i<x.size(); i++)
203 if (!is_complex_real(x.fastAccessDx(i)))
212 template <
typename Ordinal,
typename FadType,
typename Serializer>
213 struct SerializationImp {
218 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
221 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
229 static const bool supportsDirectSerialization =
false;
235 static Ordinal fromCountToIndirectBytes(
const Serializer& vs,
242 for (
Ordinal i=0; i<count; i++) {
243 int my_sz = buffer[i].size();
245 if (sz == 0) tot_sz = my_sz;
246 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
247 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].
val()));
248 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
250 if (tot_sz != my_sz) {
259 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
260 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
261 bytes += b1+b2+b3+b4+b5;
269 static void serialize (
const Serializer& vs,
277 for (
Ordinal i=0; i<count; i++) {
279 int my_sz = buffer[i].size();
281 if (sz == 0) tot_sz = my_sz;
282 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
283 iSerT::serialize(1, &tot_sz, b1, charBuffer);
287 Ordinal b2 = vs.fromCountToIndirectBytes(1, &(buffer[i].
val()));
288 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
289 oSerT::serialize(1, &b2, b3, charBuffer);
291 vs.serialize(1, &(buffer[i].
val()), b2, charBuffer);
296 if (tot_sz != my_sz) {
305 b4 = vs.fromCountToIndirectBytes(tot_sz, cx->dx());
306 Ordinal b5 = oSerT::fromCountToIndirectBytes(1, &b4);
307 oSerT::serialize(1, &b4, b5, charBuffer);
309 vs.serialize(tot_sz, cx->dx(), b4, charBuffer);
317 static Ordinal fromIndirectBytesToCount(
const Serializer& vs,
319 const char charBuffer[],
323 while (bytes_used < bytes) {
326 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
331 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
332 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
339 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
340 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
352 static void deserialize (
const Serializer& vs,
354 const char charBuffer[],
358 for (
Ordinal i=0; i<count; i++) {
361 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
362 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
367 if (sz == 0) tot_sz = *my_sz;
368 buffer[i] =
FadType(tot_sz, 0.0);
371 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
372 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
374 vs.deserialize(*b2, charBuffer, 1, &(buffer[i].
val()));
378 Ordinal b5 = oSerT::fromCountToDirectBytes(1);
379 const Ordinal *b4 = oSerT::convertFromCharPtr(charBuffer);
381 vs.deserialize(*b4, charBuffer, *my_sz,
393 template <
typename Ordinal,
typename FadType>
394 struct SerializationTraitsImp {
402 typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
405 typedef typename DS::DefaultSerializerType ValueSerializer;
408 typedef SerializationImp<Ordinal,FadType,ValueSerializer> Imp;
413 static const bool supportsDirectSerialization =
414 Imp::supportsDirectSerialization;
422 return Imp::fromCountToIndirectBytes(
423 DS::getDefaultSerializer(), count, buffer);
427 static void serialize (
const Ordinal count,
432 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
437 const char charBuffer[]) {
438 return Imp::fromIndirectBytesToCount(
439 DS::getDefaultSerializer(), bytes, charBuffer);
443 static void deserialize (
const Ordinal bytes,
444 const char charBuffer[],
448 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
456 template <
typename Ordinal,
typename FadType>
457 struct StaticSerializationTraitsImp {
459 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
460 typedef Teuchos::DirectSerializationTraits<Ordinal,FadType> DSerT;
461 typedef Sacado::Fad::SerializationTraitsImp<Ordinal,FadType> STI;
464 static const bool supportsDirectSerialization =
465 vSerT::supportsDirectSerialization;
472 return DSerT::fromCountToDirectBytes(count);
476 static char* convertToCharPtr(
FadType* ptr ) {
477 return DSerT::convertToCharPtr(ptr);
481 static const char* convertToCharPtr(
const FadType* ptr ) {
482 return DSerT::convertToCharPtr(ptr);
487 return DSerT::fromDirectBytesToCount(bytes);
491 static FadType* convertFromCharPtr(
char* ptr ) {
492 return DSerT::convertFromCharPtr(ptr);
496 static const FadType* convertFromCharPtr(
const char* ptr ) {
497 return DSerT::convertFromCharPtr(ptr);
508 if (supportsDirectSerialization)
509 return DSerT::fromCountToIndirectBytes(count, buffer);
511 return STI::fromCountToIndirectBytes(count, buffer);
515 static void serialize (
const Ordinal count,
519 if (supportsDirectSerialization)
520 return DSerT::serialize(count, buffer, bytes, charBuffer);
522 return STI::serialize(count, buffer, bytes, charBuffer);
527 const char charBuffer[]) {
528 if (supportsDirectSerialization)
529 return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
531 return STI::fromIndirectBytesToCount(bytes, charBuffer);
535 static void deserialize (
const Ordinal bytes,
536 const char charBuffer[],
539 if (supportsDirectSerialization)
540 return DSerT::deserialize(bytes, charBuffer, count, buffer);
542 return STI::deserialize(bytes, charBuffer, count, buffer);
550 template <
typename Ordinal,
typename FadType,
typename ValueSerializer>
551 class SerializerImp {
556 typedef SerializationImp<Ordinal,FadType,ValueSerializer> Imp;
559 Teuchos::RCP<const ValueSerializer> vs;
567 typedef ValueSerializer value_serializer_type;
570 static const bool supportsDirectSerialization =
571 Imp::supportsDirectSerialization;
574 SerializerImp(
const Teuchos::RCP<const ValueSerializer>& vs_,
579 Ordinal getSerializerSize()
const {
return sz; }
582 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
590 const FadType buffer[])
const {
591 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
595 void serialize (
const Ordinal count,
598 char charBuffer[])
const {
599 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
604 const char charBuffer[])
const {
605 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
609 void deserialize (
const Ordinal bytes,
610 const char charBuffer[],
613 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
625 #if defined(HAVE_SACADO_KOKKOSCORE) && defined(HAVE_SACADO_TEUCHOSKOKKOSCOMM) && defined(HAVE_SACADO_VIEW_SPEC) && !defined(SACADO_DISABLE_FAD_VIEW_SPEC) 631 #endif // HAVE_SACADO_TEUCHOS 633 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP static std::string eval()
Sacado::Fad::DFad< double > FadType
pow(expr1.val(), expr2.val())
SimpleFad< ValueT > pow(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
Sacado::Random< double > rnd