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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef SACADO_PCE_SCALARTRAITSIMP_HPP
43 #define SACADO_PCE_SCALARTRAITSIMP_HPP
44 
45 #include "Teuchos_ScalarTraits.hpp"
47 #include "Teuchos_Assert.hpp"
48 #include "Sacado_mpl_apply.hpp"
49 
50 #include <iterator>
51 
52 namespace Sacado {
53 
54  namespace PCE {
55 
57  template <typename PCEType>
58  struct ScalarTraitsImp {
59  typedef typename Sacado::ValueType<PCEType>::type ValueT;
60 
62  //typedef typename Teuchos::ScalarTraits<ValueT>::innerProductType innerProductType;
64  typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
65  typedef typename mpl::apply<PCEType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
66 
69  static const bool isComparable =
71  static const bool hasMachineParameters =
75  }
78  }
81  }
84  }
87  }
90  }
93  }
96  }
99  }
102  }
103  static magnitudeType magnitude(const PCEType& a) {
104 #ifdef TEUCHOS_DEBUG
105  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
106  a, "Error, the input value to magnitude(...) a = " << a <<
107  " can not be NaN!" );
108  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(a) == false, std::runtime_error,
109  "Complex magnitude is not a differentiable "
110  "function of complex inputs.");
111 #endif
112  return a.two_norm();
113  }
114  static innerProductType innerProduct(const PCEType& a, const PCEType& b) {
115 #ifdef TEUCHOS_DEBUG
116  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
117  a, "Error, the input value to innerProduct(...) a = " << a <<
118  " can not be NaN!" );
119  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
120  b, "Error, the input value to innerProduct(...) b = " << b <<
121  " can not be NaN!" );
122 #endif
123  return a.inner_product(b);
124  }
125  static ValueT zero() {
126  return ValueT(0.0);
127  }
128  static ValueT one() {
129  return ValueT(1.0);
130  }
131 
132  // Conjugate is only defined for real derivative components
133  static PCEType conjugate(const PCEType& x) {
134 #ifdef TEUCHOS_DEBUG
135  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
136  "Complex conjugate is not a differentiable "
137  "function of complex inputs.");
138 #endif
139  PCEType y = x;
140  y.copyForWrite();
141  y.val() = Teuchos::ScalarTraits<ValueT>::conjugate(x.val());
142  return y;
143  }
144 
145  // Real part is only defined for real derivative components
146  static PCEType real(const PCEType& x) {
147 #ifdef TEUCHOS_DEBUG
148  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
149  "Real component is not a differentiable "
150  "function of complex inputs.");
151 #endif
152  PCEType y = x;
153  y.copyForWrite();
154  y.val() = Teuchos::ScalarTraits<ValueT>::real(x.val());
155  return y;
156  }
157 
158  // Imaginary part is only defined for real derivative components
159  static PCEType imag(const PCEType& x) {
160 #ifdef TEUCHOS_DEBUG
161  TEUCHOS_TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
162  "Imaginary component is not a differentiable "
163  "function of complex inputs.");
164 #endif
166  }
167 
168  static ValueT nan() {
170  }
171  static bool isnaninf(const PCEType& x) {
172  for (int i=0; i<x.size(); i++)
173  if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
174  return true;
175  return false;
176  }
177  static void seedrandom(unsigned int s) {
179  }
180  static ValueT random() {
182  }
183  static std::string name() {
184  return Sacado::StringName<PCEType>::eval();
185  }
186  static PCEType squareroot(const PCEType& x) {
187 #ifdef TEUCHOS_DEBUG
188  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
189  x, "Error, the input value to squareroot(...) a = " << x <<
190  " can not be NaN!" );
191 #endif
192  return std::sqrt(x);
193  }
194  static PCEType pow(const PCEType& x, const PCEType& y) {
195  return std::pow(x,y);
196  }
197  static PCEType log(const PCEType& x) {
198  return std::log(x);
199  }
200  static PCEType log10(const PCEType& x) {
201  return std::log10(x);
202  }
203 
204  // Helper function to determine whether a complex value is real
205  static bool is_complex_real(const ValueT& x) {
206  return
208  }
209 
210  // Helper function to determine whether a Fad type is real
211  static bool is_pce_real(const PCEType& x) {
212  if (x.size() == 0)
213  return true;
215  for (int i=0; i<x.size(); i++)
216  if (!is_complex_real(x.fastAccessCoeff(i)))
217  return false;
218  }
219  return true;
220  }
221 
222  }; // class ScalarTraitsImp
223 
225  template <typename TypeTo, typename PCEType>
227  typedef typename Sacado::ValueType<PCEType>::type ValueT;
229  static TypeTo convert( const PCEType t ) {
230  return VTCT::convert(t.val());
231  }
232  static TypeTo safeConvert( const PCEType t ) {
233  return VTCT::safeConvert(t.val());
234  }
235  };
236 
237 
239  template <typename Ordinal, typename PCEType>
241  typedef typename Sacado::ValueType<PCEType>::type ValueT;
245 
246  public:
247 
249  static const bool supportsDirectSerialization = false;
250 
252 
253 
256  const PCEType buffer[]) {
257  Ordinal bytes = 0;
258  for (Ordinal i=0; i<count; i++) {
259  int sz = buffer[i].size();
261  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
263  bytes += b1+b2+b3;
264  }
265  return bytes;
266  }
267 
269  static void serialize (const Ordinal count,
270  const PCEType buffer[],
271  const Ordinal bytes,
272  char charBuffer[]) {
273  for (Ordinal i=0; i<count; i++) {
274  // First serialize size
275  int sz = buffer[i].size();
277  iSerT::serialize(1, &sz, b1, charBuffer);
278  charBuffer += b1;
279 
280  // Next serialize PCE coefficients
281  Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
283  oSerT::serialize(1, &b2, b3, charBuffer);
284  charBuffer += b3;
285  vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
286  charBuffer += b2;
287  }
288  }
289 
292  const char charBuffer[]) {
293  Ordinal count = 0;
294  Ordinal bytes_used = 0;
295  while (bytes_used < bytes) {
296 
297  // Bytes for size
299  bytes_used += b1;
300  charBuffer += b1;
301 
302  // Bytes for PCE coefficients
304  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
305  bytes_used += b3;
306  charBuffer += b3;
307  bytes_used += *b2;
308  charBuffer += *b2;
309 
310  ++count;
311  }
312  return count;
313  }
314 
316  static void deserialize (const Ordinal bytes,
317  const char charBuffer[],
318  const Ordinal count,
319  PCEType buffer[]) {
320  for (Ordinal i=0; i<count; i++) {
321 
322  // Deserialize size
324  const int *sz = iSerT::convertFromCharPtr(charBuffer);
325  charBuffer += b1;
326 
327  // Make sure PCE object is ready to receive values
328  // We assume it has already been initialized with the proper
329  // expansion object
330  if (buffer[i].size() != *sz)
331  buffer[i].reset(buffer[i].expansion(), *sz);
332  buffer[i].copyForWrite();
333 
334  // Deserialize PCE coefficients
336  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
337  charBuffer += b3;
338  vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
339  charBuffer += *b2;
340  }
341 
342  }
343 
345 
346  };
347 
348 
350  template <typename Ordinal, typename PCEType, typename ValueSerializer>
352 
353  public:
354 
356  typedef ValueSerializer value_serializer_type;
357 
359  typedef typename PCEType::expansion_type expansion_type;
360 
361 
362  protected:
363  typedef typename Sacado::ValueType<PCEType>::type ValueT;
366 
369  int sz;
370 
371  public:
372 
374  static const bool supportsDirectSerialization = false;
375 
378  expansion(expansion_), vs(vs_), sz(expansion->size()) {}
379 
382  return expansion; }
383 
386  return vs; }
387 
389 
390 
393  const PCEType buffer[]) const {
394  Ordinal bytes = 0;
395  PCEType *x = NULL;
396  const PCEType *cx;
397  for (Ordinal i=0; i<count; i++) {
398  int my_sz = buffer[i].size();
399  if (sz != my_sz) {
400  if (x == NULL)
401  x = new PCEType;
402  *x = buffer[i];
403  x->reset(expansion);
404  cx = x;
405  }
406  else
407  cx = &(buffer[i]);
409  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
411  bytes += b1+b2+b3;
412  }
413  if (x != NULL)
414  delete x;
415  return bytes;
416  }
417 
419  void serialize (const Ordinal count,
420  const PCEType buffer[],
421  const Ordinal bytes,
422  char charBuffer[]) const {
423  PCEType *x = NULL;
424  const PCEType *cx;
425  for (Ordinal i=0; i<count; i++) {
426  // First serialize size
427  int my_sz = buffer[i].size();
428  if (sz != my_sz) {
429  if (x == NULL)
430  x = new PCEType(expansion);
431  *x = buffer[i];
432  x->reset(expansion);
433  cx = x;
434  }
435  else
436  cx = &(buffer[i]);
438  iSerT::serialize(1, &sz, b1, charBuffer);
439  charBuffer += b1;
440 
441  // Next serialize PCE coefficients
442  Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
444  oSerT::serialize(1, &b2, b3, charBuffer);
445  charBuffer += b3;
446  vs->serialize(sz, cx->coeff(), b2, charBuffer);
447  charBuffer += b2;
448  }
449  if (x != NULL)
450  delete x;
451  }
452 
455  const char charBuffer[]) const {
456  Ordinal count = 0;
457  Ordinal bytes_used = 0;
458  while (bytes_used < bytes) {
459 
460  // Bytes for size
462  bytes_used += b1;
463  charBuffer += b1;
464 
465  // Bytes for PCE coefficients
467  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
468  bytes_used += b3;
469  charBuffer += b3;
470  bytes_used += *b2;
471  charBuffer += *b2;
472 
473  ++count;
474  }
475  return count;
476  }
477 
479  void deserialize (const Ordinal bytes,
480  const char charBuffer[],
481  const Ordinal count,
482  PCEType buffer[]) const {
483  for (Ordinal i=0; i<count; i++) {
484 
485  // Deserialize size
487  const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
488  charBuffer += b1;
489 
490  // Create empty PCE object of given size
491  buffer[i] = PCEType(expansion);
492 
493  // Deserialize PCE coefficients
495  const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
496  charBuffer += b3;
497  vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
498  charBuffer += *b2;
499  }
500 
501  }
502 
504 
505  };
506 
507  } // namespace PCE
508 
509 } // namespace Sacado
510 
511 #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.