Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_PCE_ScalarTraitsImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP
11 #define SACADO_PCE_SCALARTRAITSIMP_HPP
12 
13 #include "Teuchos_ScalarTraits.hpp"
15 #include "Teuchos_Assert.hpp"
16 #include "Sacado_mpl_apply.hpp"
17 
18 #include <iterator>
19 
20 namespace Sacado {
21 
22  namespace PCE {
23 
25  template <typename PCEType>
26  struct ScalarTraitsImp {
27  typedef typename Sacado::ValueType<PCEType>::type ValueT;
28 
30  //typedef typename Teuchos::ScalarTraits<ValueT>::innerProductType innerProductType;
32  typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
33  typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
34 
37  static const bool isComparable =
39  static const bool hasMachineParameters =
43  }
46  }
49  }
52  }
55  }
58  }
61  }
64  }
67  }
70  }
71  static magnitudeType magnitude(const PCEType& a) {
72 #ifdef TEUCHOS_DEBUG
73  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
74  a, "Error, the input value to magnitude(...) a = " << a <<
75  " can not be NaN!" );
76  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(a) == false, std::runtime_error,
77  "Complex magnitude is not a differentiable "
78  "function of complex inputs.");
79 #endif
80  return a.two_norm();
81  }
82  static innerProductType innerProduct(const PCEType& a, const PCEType& b) {
83 #ifdef TEUCHOS_DEBUG
84  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
85  a, "Error, the input value to innerProduct(...) a = " << a <<
86  " can not be NaN!" );
87  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
88  b, "Error, the input value to innerProduct(...) b = " << b <<
89  " can not be NaN!" );
90 #endif
91  return a.inner_product(b);
92  }
93  static ValueT zero() {
94  return ValueT(0.0);
95  }
96  static ValueT one() {
97  return ValueT(1.0);
98  }
99 
100  // Conjugate is only defined for real derivative components
101  static PCEType conjugate(const PCEType& x) {
102 #ifdef TEUCHOS_DEBUG
103  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
104  "Complex conjugate is not a differentiable "
105  "function of complex inputs.");
106 #endif
107  PCEType y = x;
108  y.copyForWrite();
109  y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
110  return y;
111  }
112 
113  // Real part is only defined for real derivative components
114  static PCEType real(const PCEType& x) {
115 #ifdef TEUCHOS_DEBUG
116  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
117  "Real component is not a differentiable "
118  "function of complex inputs.");
119 #endif
120  PCEType y = x;
121  y.copyForWrite();
122  y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
123  return y;
124  }
125 
126  // Imaginary part is only defined for real derivative components
127  static PCEType imag(const PCEType& x) {
128 #ifdef TEUCHOS_DEBUG
129  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
130  "Imaginary component is not a differentiable "
131  "function of complex inputs.");
132 #endif
134  }
135 
136  static ValueT nan() {
138  }
139  static bool isnaninf(const PCEType& x) {
140  for (int i=0; i<x.size(); i++)
141  if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
142  return true;
143  return false;
144  }
145  static void seedrandom(unsigned int s) {
147  }
148  static ValueT random() {
150  }
151  static std::string name() {
152  return Sacado::StringName<PCEType>::eval();
153  }
154  static PCEType squareroot(const PCEType& x) {
155 #ifdef TEUCHOS_DEBUG
156  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
157  x, "Error, the input value to squareroot(...) a = " << x <<
158  " can not be NaN!" );
159 #endif
160  return std::sqrt(x);
161  }
162  static PCEType pow(const PCEType& x, const PCEType& y) {
163  return std::pow(x,y);
164  }
165  static PCEType log(const PCEType& x) {
166  return std::log(x);
167  }
168  static PCEType log10(const PCEType& x) {
169  return std::log10(x);
170  }
171 
172  // Helper function to determine whether a complex value is real
173  static bool is_complex_real(const ValueT& x) {
174  return
176  }
177 
178  // Helper function to determine whether a Fad type is real
179  static bool is_pce_real(const PCEType& x) {
180  if (x.size() == 0)
181  return true;
183  for (int i=0; i<x.size(); i++)
184  if (!is_complex_real(x.fastAccessCoeff(i)))
185  return false;
186  }
187  return true;
188  }
189 
190  }; // class ScalarTraitsImp
191 
193  template <typename TypeTo, typename PCEType>
195  typedef typename Sacado::ValueType<PCEType>::type ValueT;
197  static TypeTo convert( const PCEType t ) {
198  return VTCT::convert(t.val());
199  }
200  static TypeTo safeConvert( const PCEType t ) {
201  return VTCT::safeConvert(t.val());
202  }
203  };
204 
205 
207  template <typename Ordinal, typename PCEType>
209  typedef typename Sacado::ValueType<PCEType>::type ValueT;
213 
214  public:
215 
217  static const bool supportsDirectSerialization = false;
218 
220 
221 
224  const PCEType buffer[]) {
225  Ordinal bytes = 0;
226  for (Ordinal i=0; i<count; i++) {
227  int sz = buffer[i].size();
229  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
231  bytes += b1+b2+b3;
232  }
233  return bytes;
234  }
235 
237  static void serialize (const Ordinal count,
238  const PCEType buffer[],
239  const Ordinal bytes,
240  char charBuffer[]) {
241  for (Ordinal i=0; i<count; i++) {
242  // First serialize size
243  int sz = buffer[i].size();
245  iSerT::serialize(1, &sz, b1, charBuffer);
246  charBuffer += b1;
247 
248  // Next serialize PCE coefficients
249  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
251  oSerT::serialize(1, &b2, b3, charBuffer);
252  charBuffer += b3;
253  vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
254  charBuffer += b2;
255  }
256  }
257 
260  const char charBuffer[]) {
261  Ordinal count = 0;
262  Ordinal bytes_used = 0;
263  while (bytes_used < bytes) {
264 
265  // Bytes for size
267  bytes_used += b1;
268  charBuffer += b1;
269 
270  // Bytes for PCE coefficients
272  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
273  bytes_used += b3;
274  charBuffer += b3;
275  bytes_used += *b2;
276  charBuffer += *b2;
277 
278  ++count;
279  }
280  return count;
281  }
282 
284  static void deserialize (const Ordinal bytes,
285  const char charBuffer[],
286  const Ordinal count,
287  PCEType buffer[]) {
288  for (Ordinal i=0; i<count; i++) {
289 
290  // Deserialize size
292  const int *sz = iSerT::convertFromCharPtr(charBuffer);
293  charBuffer += b1;
294 
295  // Make sure PCE object is ready to receive values
296  // We assume it has already been initialized with the proper
297  // expansion object
298  if (buffer[i].size() != *sz)
299  buffer[i].reset(buffer[i].expansion(), *sz);
300  buffer[i].copyForWrite();
301 
302  // Deserialize PCE coefficients
304  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
305  charBuffer += b3;
306  vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
307  charBuffer += *b2;
308  }
309 
310  }
311 
313 
314  };
315 
316 
318  template <typename Ordinal, typename PCEType, typename ValueSerializer>
320 
321  public:
322 
324  typedef ValueSerializer value_serializer_type;
325 
327  typedef typename PCEType::expansion_type expansion_type;
328 
329 
330  protected:
331  typedef typename Sacado::ValueType<PCEType>::type ValueT;
334 
337  int sz;
338 
339  public:
340 
342  static const bool supportsDirectSerialization = false;
343 
346  expansion(expansion_), vs(vs_), sz(expansion->size()) {}
347 
350  return expansion; }
351 
354  return vs; }
355 
357 
358 
361  const PCEType buffer[]) const {
362  Ordinal bytes = 0;
363  PCEType *x = NULL;
364  const PCEType *cx;
365  for (Ordinal i=0; i<count; i++) {
366  int my_sz = buffer[i].size();
367  if (sz != my_sz) {
368  if (x == NULL)
369  x = new PCEType;
370  *x = buffer[i];
371  x->reset(expansion);
372  cx = x;
373  }
374  else
375  cx = &(buffer[i]);
377  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
379  bytes += b1+b2+b3;
380  }
381  if (x != NULL)
382  delete x;
383  return bytes;
384  }
385 
387  void serialize (const Ordinal count,
388  const PCEType buffer[],
389  const Ordinal bytes,
390  char charBuffer[]) const {
391  PCEType *x = NULL;
392  const PCEType *cx;
393  for (Ordinal i=0; i<count; i++) {
394  // First serialize size
395  int my_sz = buffer[i].size();
396  if (sz != my_sz) {
397  if (x == NULL)
398  x = new PCEType(expansion);
399  *x = buffer[i];
400  x->reset(expansion);
401  cx = x;
402  }
403  else
404  cx = &(buffer[i]);
406  iSerT::serialize(1, &sz, b1, charBuffer);
407  charBuffer += b1;
408 
409  // Next serialize PCE coefficients
410  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
412  oSerT::serialize(1, &b2, b3, charBuffer);
413  charBuffer += b3;
414  vs->serialize(sz, cx->coeff(), b2, charBuffer);
415  charBuffer += b2;
416  }
417  if (x != NULL)
418  delete x;
419  }
420 
423  const char charBuffer[]) const {
424  Ordinal count = 0;
425  Ordinal bytes_used = 0;
426  while (bytes_used < bytes) {
427 
428  // Bytes for size
430  bytes_used += b1;
431  charBuffer += b1;
432 
433  // Bytes for PCE coefficients
435  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
436  bytes_used += b3;
437  charBuffer += b3;
438  bytes_used += *b2;
439  charBuffer += *b2;
440 
441  ++count;
442  }
443  return count;
444  }
445 
447  void deserialize (const Ordinal bytes,
448  const char charBuffer[],
449  const Ordinal count,
450  PCEType buffer[]) const {
451  for (Ordinal i=0; i<count; i++) {
452 
453  // Deserialize size
455  const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
456  charBuffer += b1;
457 
458  // Create empty PCE object of given size
459  buffer[i] = PCEType(expansion);
460 
461  // Deserialize PCE coefficients
463  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
464  charBuffer += b3;
465  vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
466  charBuffer += *b2;
467  }
468 
469  }
470 
472 
473  };
474 
475  } // namespace PCE
476 
477 } // namespace Sacado
478 
479 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[])
Deserialize from an indirect char[] buffer.
static void serialize(const Ordinal count, const intbuffer[], const Ordinal bytes, char charBuffer[])
static Ordinal fromCountToIndirectBytes(const Ordinal count, const intbuffer[])
static Ordinal fromCountToDirectBytes(const Ordinal count)
static Teuchos::ScalarTraits< ValueT >::magnitudeType eps()
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmin()
SerializerImp(const Teuchos::RCP< expansion_type > &expansion_, const Teuchos::RCP< const ValueSerializer > &vs_)
Teuchos::RCP< expansion_type > getSerializerExpansion() const
Return specified serializer size.
Implementation for Teuchos::ScalarTraits for all PCE types.
Teuchos::RCP< const ValueSerializer > vs
PCEType::expansion_type expansion_type
Typename of expansion.
static magnitudeType eps()
Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const
Return the number of bytes for count objects.
static Teuchos::ScalarTraits< ValueT >::magnitudeType emax()
Serializer object for all PCE types.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
static TypeTo safeConvert(const TypeFrom t)
static PCEType log(const PCEType &x)
static Ordinal fromCountToIndirectBytes(const Ordinal count, const T buffer[])
static Teuchos::ScalarTraits< ValueT >::magnitudeType sfmin()
Teuchos::RCP< const value_serializer_type > getValueSerializer() const
Get nested value serializer.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static TypeTo convert(const TypeFrom t)
static magnitudeType real(T a)
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmax()
static PCEType log10(const PCEType &x)
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[])
Sacado::ValueType< PCEType >::type ValueT
Teuchos::RCP< expansion_type > expansion
Teuchos::SerializationTraits< Ordinal, ValueT > vSerT
static bool isnaninf(const PCEType &x)
Sacado::ValueType< PCEType >::type ValueT
static magnitudeType emax()
static magnitudeType base()
ValueSerializer value_serializer_type
Typename of value serializer.
static magnitudeType rmax()
static magnitudeType sfmin()
Teuchos::ScalarTraits< ValueT >::magnitudeType magnitudeType
static Teuchos::ScalarTraits< ValueT >::magnitudeType rnd()
static Teuchos::ScalarTraits< ValueT >::magnitudeType base()
static PCEType squareroot(const PCEType &x)
static T conjugate(T a)
static PCEType real(const PCEType &x)
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const
Return the number of objects for bytes of storage.
static magnitudeType prec()
Sacado::ValueType< PCEType >::type ValueT
static bool is_complex_real(const ValueT &x)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
static magnitudeType t()
static T * convertFromCharPtr(char *ptr)
static bool is_pce_real(const PCEType &x)
Teuchos::ValueTypeConversionTraits< TypeTo, ValueT > VTCT
static void seedrandom(unsigned int s)
static PCEType imag(const PCEType &x)
static magnitudeType rmin()
Implementation for Teuchos::ValueTypeConversionTraits for all PCE types.
static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[])
Return the number of bytes for count objects.
static magnitudeType magnitude(const PCEType &a)
Implementation of Teuchos::SerializationTraits for all PCE types.
static void seedrandom(unsigned int s)
static Teuchos::ScalarTraits< ValueT >::magnitudeType prec()
static innerProductType innerProduct(const PCEType &a, const PCEType &b)
static magnitudeType magnitude(T a)
static void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
Sacado::UQ::PCE< storage_type > PCEType
void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) const
Deserialize from an indirect char[] buffer.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
static PCEType pow(const PCEType &x, const PCEType &y)
Teuchos::SerializationTraits< Ordinal, int > iSerT
static Teuchos::ScalarTraits< ValueT >::magnitudeType emin()
void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) const
Serialize to an indirect char[] buffer.
static PCEType conjugate(const PCEType &x)
mpl::apply< PCEType, typename Teuchos::ScalarTraits< ValueT >::halfPrecision >::type halfPrecision
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
static magnitudeType emin()
static magnitudeType rnd()
static void serialize(const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[])
static Ordinal fromCountToDirectBytes(const Ordinal count)
static Teuchos::ScalarTraits< ValueT >::magnitudeType t()
mpl::apply< PCEType, typename Teuchos::ScalarTraits< ValueT >::doublePrecision >::type doublePrecision
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.