Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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"
46#include "Teuchos_SerializationTraits.hpp"
47#include "Teuchos_Assert.hpp"
48#include "Sacado_mpl_apply.hpp"
49
50#include <iterator>
51
52namespace Sacado {
53
54 namespace PCE {
55
57 template <typename PCEType>
59 typedef typename Sacado::ValueType<PCEType>::type ValueT;
60
61 typedef typename Teuchos::ScalarTraits<ValueT>::magnitudeType magnitudeType;
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
67 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
68 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
69 static const bool isComparable =
70 Teuchos::ScalarTraits<ValueT>::isComparable;
71 static const bool hasMachineParameters =
72 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
73 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
74 return Teuchos::ScalarTraits<ValueT>::eps();
75 }
76 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
77 return Teuchos::ScalarTraits<ValueT>::sfmin();
78 }
79 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
80 return Teuchos::ScalarTraits<ValueT>::base();
81 }
82 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
83 return Teuchos::ScalarTraits<ValueT>::prec();
84 }
85 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
86 return Teuchos::ScalarTraits<ValueT>::t();
87 }
88 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rnd() {
89 return Teuchos::ScalarTraits<ValueT>::rnd();
90 }
91 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
92 return Teuchos::ScalarTraits<ValueT>::emin();
93 }
94 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
95 return Teuchos::ScalarTraits<ValueT>::rmin();
96 }
97 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
98 return Teuchos::ScalarTraits<ValueT>::emax();
99 }
100 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
101 return Teuchos::ScalarTraits<ValueT>::rmax();
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
165 return PCEType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
166 }
167
168 static ValueT nan() {
169 return Teuchos::ScalarTraits<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) {
178 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
179 }
180 static ValueT random() {
181 return Teuchos::ScalarTraits<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
207 Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
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;
214 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
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;
228 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
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;
242 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
243 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
244 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
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();
260 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
261 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
262 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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();
276 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
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());
282 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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
298 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
299 bytes_used += b1;
300 charBuffer += b1;
301
302 // Bytes for PCE coefficients
303 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
323 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
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
335 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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;
364 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
365 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
366
367 Teuchos::RCP<expansion_type> expansion;
368 Teuchos::RCP<const ValueSerializer> vs;
369 int sz;
370
371 public:
372
374 static const bool supportsDirectSerialization = false;
375
376 SerializerImp(const Teuchos::RCP<expansion_type>& expansion_,
377 const Teuchos::RCP<const ValueSerializer>& vs_) :
378 expansion(expansion_), vs(vs_), sz(expansion->size()) {}
379
381 Teuchos::RCP<expansion_type> getSerializerExpansion() const {
382 return expansion; }
383
385 Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
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]);
408 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
409 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
410 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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]);
437 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
438 iSerT::serialize(1, &sz, b1, charBuffer);
439 charBuffer += b1;
440
441 // Next serialize PCE coefficients
442 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
443 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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
461 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
462 bytes_used += b1;
463 charBuffer += b1;
464
465 // Bytes for PCE coefficients
466 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
486 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
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
494 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
Sacado::UQ::PCE< storage_type > PCEType
Implementation of Teuchos::SerializationTraits for all PCE types.
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[])
Deserialize from an indirect char[] buffer.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Teuchos::SerializationTraits< Ordinal, ValueT > vSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Sacado::ValueType< PCEType >::type ValueT
static void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[])
Return the number of bytes for count objects.
Teuchos::SerializationTraits< Ordinal, int > iSerT
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
Serializer object for all PCE types.
ValueSerializer value_serializer_type
Typename of value serializer.
Teuchos::RCP< const value_serializer_type > getValueSerializer() const
Get nested value serializer.
Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const
Return the number of objects for bytes of storage.
PCEType::expansion_type expansion_type
Typename of expansion.
Teuchos::RCP< expansion_type > expansion
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Teuchos::RCP< const ValueSerializer > vs
Sacado::ValueType< PCEType >::type ValueT
Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const
Return the number of bytes for count objects.
Teuchos::RCP< expansion_type > getSerializerExpansion() const
Return specified serializer size.
void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[]) const
Deserialize from an indirect char[] buffer.
void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[]) const
Serialize to an indirect char[] buffer.
Teuchos::SerializationTraits< Ordinal, int > iSerT
SerializerImp(const Teuchos::RCP< expansion_type > &expansion_, const Teuchos::RCP< const ValueSerializer > &vs_)
Implementation for Teuchos::ScalarTraits for all PCE types.
static PCEType log10(const PCEType &x)
static PCEType conjugate(const PCEType &x)
static PCEType squareroot(const PCEType &x)
Teuchos::ScalarTraits< ValueT >::magnitudeType magnitudeType
static Teuchos::ScalarTraits< ValueT >::magnitudeType rnd()
static Teuchos::ScalarTraits< ValueT >::magnitudeType t()
static PCEType log(const PCEType &x)
static innerProductType innerProduct(const PCEType &a, const PCEType &b)
static bool is_complex_real(const ValueT &x)
static bool isnaninf(const PCEType &x)
static magnitudeType magnitude(const PCEType &a)
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmin()
static Teuchos::ScalarTraits< ValueT >::magnitudeType emin()
Sacado::ValueType< PCEType >::type ValueT
static Teuchos::ScalarTraits< ValueT >::magnitudeType prec()
static Teuchos::ScalarTraits< ValueT >::magnitudeType eps()
static Teuchos::ScalarTraits< ValueT >::magnitudeType rmax()
static Teuchos::ScalarTraits< ValueT >::magnitudeType sfmin()
static bool is_pce_real(const PCEType &x)
mpl::apply< PCEType, typenameTeuchos::ScalarTraits< ValueT >::doublePrecision >::type doublePrecision
static void seedrandom(unsigned int s)
static PCEType real(const PCEType &x)
static Teuchos::ScalarTraits< ValueT >::magnitudeType emax()
static PCEType imag(const PCEType &x)
static Teuchos::ScalarTraits< ValueT >::magnitudeType base()
mpl::apply< PCEType, typenameTeuchos::ScalarTraits< ValueT >::halfPrecision >::type halfPrecision
static PCEType pow(const PCEType &x, const PCEType &y)
Implementation for Teuchos::ValueTypeConversionTraits for all PCE types.
Teuchos::ValueTypeConversionTraits< TypeTo, ValueT > VTCT