// @HEADER // ***************************************************************************** // Stokhos Package // // Copyright 2009 NTESS and the Stokhos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP #define SACADO_PCE_SCALARTRAITSIMP_HPP #include "Teuchos_ScalarTraits.hpp" #include "Teuchos_SerializationTraits.hpp" #include "Teuchos_Assert.hpp" #include "Sacado_mpl_apply.hpp" #include namespace Sacado { namespace PCE { //! Implementation for Teuchos::ScalarTraits for all PCE types template struct ScalarTraitsImp { typedef typename Sacado::ValueType::type ValueT; typedef typename Teuchos::ScalarTraits::magnitudeType magnitudeType; //typedef typename Teuchos::ScalarTraits::innerProductType innerProductType; typedef ValueT innerProductType; typedef typename mpl::apply::halfPrecision>::type halfPrecision; typedef typename mpl::apply::doublePrecision>::type doublePrecision; static const bool isComplex = Teuchos::ScalarTraits::isComplex; static const bool isOrdinal = Teuchos::ScalarTraits::isOrdinal; static const bool isComparable = Teuchos::ScalarTraits::isComparable; static const bool hasMachineParameters = Teuchos::ScalarTraits::hasMachineParameters; static typename Teuchos::ScalarTraits::magnitudeType eps() { return Teuchos::ScalarTraits::eps(); } static typename Teuchos::ScalarTraits::magnitudeType sfmin() { return Teuchos::ScalarTraits::sfmin(); } static typename Teuchos::ScalarTraits::magnitudeType base() { return Teuchos::ScalarTraits::base(); } static typename Teuchos::ScalarTraits::magnitudeType prec() { return Teuchos::ScalarTraits::prec(); } static typename Teuchos::ScalarTraits::magnitudeType t() { return Teuchos::ScalarTraits::t(); } static typename Teuchos::ScalarTraits::magnitudeType rnd() { return Teuchos::ScalarTraits::rnd(); } static typename Teuchos::ScalarTraits::magnitudeType emin() { return Teuchos::ScalarTraits::emin(); } static typename Teuchos::ScalarTraits::magnitudeType rmin() { return Teuchos::ScalarTraits::rmin(); } static typename Teuchos::ScalarTraits::magnitudeType emax() { return Teuchos::ScalarTraits::emax(); } static typename Teuchos::ScalarTraits::magnitudeType rmax() { return Teuchos::ScalarTraits::rmax(); } static magnitudeType magnitude(const PCEType& a) { #ifdef TEUCHOS_DEBUG TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" ); TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(a) == false, std::runtime_error, "Complex magnitude is not a differentiable " "function of complex inputs."); #endif return a.two_norm(); } static innerProductType innerProduct(const PCEType& a, const PCEType& b) { #ifdef TEUCHOS_DEBUG TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( a, "Error, the input value to innerProduct(...) a = " << a << " can not be NaN!" ); TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( b, "Error, the input value to innerProduct(...) b = " << b << " can not be NaN!" ); #endif return a.inner_product(b); } static ValueT zero() { return ValueT(0.0); } static ValueT one() { return ValueT(1.0); } // Conjugate is only defined for real derivative components static PCEType conjugate(const PCEType& x) { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error, "Complex conjugate is not a differentiable " "function of complex inputs."); #endif PCEType y = x; y.copyForWrite(); y.val() = Teuchos::ScalarTraits::conjugate(x.val()); return y; } // Real part is only defined for real derivative components static PCEType real(const PCEType& x) { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error, "Real component is not a differentiable " "function of complex inputs."); #endif PCEType y = x; y.copyForWrite(); y.val() = Teuchos::ScalarTraits::real(x.val()); return y; } // Imaginary part is only defined for real derivative components static PCEType imag(const PCEType& x) { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error, "Imaginary component is not a differentiable " "function of complex inputs."); #endif return PCEType(Teuchos::ScalarTraits::imag(x.val())); } static ValueT nan() { return Teuchos::ScalarTraits::nan(); } static bool isnaninf(const PCEType& x) { for (int i=0; i::isnaninf(x.fastAccessCoeff(i))) return true; return false; } static void seedrandom(unsigned int s) { Teuchos::ScalarTraits::seedrandom(s); } static ValueT random() { return Teuchos::ScalarTraits::random(); } static std::string name() { return Sacado::StringName::eval(); } static PCEType squareroot(const PCEType& x) { #ifdef TEUCHOS_DEBUG TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( x, "Error, the input value to squareroot(...) a = " << x << " can not be NaN!" ); #endif return std::sqrt(x); } static PCEType pow(const PCEType& x, const PCEType& y) { return std::pow(x,y); } static PCEType log(const PCEType& x) { return std::log(x); } static PCEType log10(const PCEType& x) { return std::log10(x); } // Helper function to determine whether a complex value is real static bool is_complex_real(const ValueT& x) { return Teuchos::ScalarTraits::magnitude(x-Teuchos::ScalarTraits::real(x)) == 0; } // Helper function to determine whether a Fad type is real static bool is_pce_real(const PCEType& x) { if (x.size() == 0) return true; if (Teuchos::ScalarTraits::isComplex) { for (int i=0; i struct ValueTypeConversionTraitsImp { typedef typename Sacado::ValueType::type ValueT; typedef Teuchos::ValueTypeConversionTraits VTCT; static TypeTo convert( const PCEType t ) { return VTCT::convert(t.val()); } static TypeTo safeConvert( const PCEType t ) { return VTCT::safeConvert(t.val()); } }; //! Implementation of Teuchos::SerializationTraits for all PCE types template class SerializationTraitsImp { typedef typename Sacado::ValueType::type ValueT; typedef Teuchos::SerializationTraits vSerT; typedef Teuchos::SerializationTraits iSerT; typedef Teuchos::SerializationTraits oSerT; public: /// \brief Whether the type T supports direct serialization. static const bool supportsDirectSerialization = false; //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) { Ordinal bytes = 0; for (Ordinal i=0; ichar[] buffer. */ static void serialize (const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) { for (Ordinal i=0; ibytes of storage. */ static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) { Ordinal count = 0; Ordinal bytes_used = 0; while (bytes_used < bytes) { // Bytes for size Ordinal b1 = iSerT::fromCountToDirectBytes(1); bytes_used += b1; charBuffer += b1; // Bytes for PCE coefficients Ordinal b3 = oSerT::fromCountToDirectBytes(1); const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer); bytes_used += b3; charBuffer += b3; bytes_used += *b2; charBuffer += *b2; ++count; } return count; } /** \brief Deserialize from an indirect char[] buffer. */ static void deserialize (const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) { for (Ordinal i=0; i class SerializerImp { public: //! Typename of value serializer typedef ValueSerializer value_serializer_type; //! Typename of expansion typedef typename PCEType::expansion_type expansion_type; protected: typedef typename Sacado::ValueType::type ValueT; typedef Teuchos::SerializationTraits iSerT; typedef Teuchos::SerializationTraits oSerT; Teuchos::RCP expansion; Teuchos::RCP vs; int sz; public: /// \brief Whether the type T supports direct serialization. static const bool supportsDirectSerialization = false; SerializerImp(const Teuchos::RCP& expansion_, const Teuchos::RCP& vs_) : expansion(expansion_), vs(vs_), sz(expansion->size()) {} //! Return specified serializer size Teuchos::RCP getSerializerExpansion() const { return expansion; } //! Get nested value serializer Teuchos::RCP getValueSerializer() const { return vs; } //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const { Ordinal bytes = 0; PCEType *x = NULL; const PCEType *cx; for (Ordinal i=0; ireset(expansion); cx = x; } else cx = &(buffer[i]); Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff()); Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); bytes += b1+b2+b3; } if (x != NULL) delete x; return bytes; } /** \brief Serialize to an indirect char[] buffer. */ void serialize (const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) const { PCEType *x = NULL; const PCEType *cx; for (Ordinal i=0; ireset(expansion); cx = x; } else cx = &(buffer[i]); Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz); iSerT::serialize(1, &sz, b1, charBuffer); charBuffer += b1; // Next serialize PCE coefficients Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff()); Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); oSerT::serialize(1, &b2, b3, charBuffer); charBuffer += b3; vs->serialize(sz, cx->coeff(), b2, charBuffer); charBuffer += b2; } if (x != NULL) delete x; } /** \brief Return the number of objects for bytes of storage. */ Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const { Ordinal count = 0; Ordinal bytes_used = 0; while (bytes_used < bytes) { // Bytes for size Ordinal b1 = iSerT::fromCountToDirectBytes(1); bytes_used += b1; charBuffer += b1; // Bytes for PCE coefficients Ordinal b3 = oSerT::fromCountToDirectBytes(1); const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer); bytes_used += b3; charBuffer += b3; bytes_used += *b2; charBuffer += *b2; ++count; } return count; } /** \brief Deserialize from an indirect char[] buffer. */ void deserialize (const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) const { for (Ordinal i=0; ideserialize(*b2, charBuffer, *my_sz, buffer[i].coeff()); charBuffer += *b2; } } //@} }; } // namespace PCE } // namespace Sacado #endif // SACADO_FAD_SCALARTRAITSIMP_HPP