Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_UQ_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_UQ_PCE_SCALARTRAITSIMP_HPP
43#define SACADO_UQ_PCE_SCALARTRAITSIMP_HPP
44
45#include "Teuchos_ScalarTraits.hpp"
46#include "Teuchos_SerializationTraits.hpp"
47#include "Teuchos_RCP.hpp"
48#include "Teuchos_Assert.hpp"
49#include "Sacado_mpl_apply.hpp"
50#include "Teuchos_as.hpp"
51
52#include <iterator>
53
54namespace Sacado {
55
56 namespace UQ {
57
59 template <typename PCEType>
61 typedef typename PCEType::storage_type storage_type;
62 typedef typename storage_type::value_type value_type;
63 typedef typename storage_type::ordinal_type ordinal_type;
64 typedef Teuchos::ScalarTraits<value_type> TVT;
65
66 typedef typename TVT::magnitudeType value_mag_type;
67 typedef typename TVT::halfPrecision value_half_type;
68 typedef typename TVT::doublePrecision value_double_type;
69
70 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_mag_type>::type storage_mag_type;
71 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_half_type>::type storage_half_type;
72 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_double_type>::type storage_double_type;
73
75 typedef typename Sacado::mpl::apply<PCEType, storage_half_type>::type halfPrecision;
76 typedef typename Sacado::mpl::apply<PCEType, storage_double_type>::type doublePrecision;
77 typedef typename TVT::coordinateType coordinateType;
78
80
81 static const bool isComplex = TVT::isComplex;
82 static const bool isOrdinal = TVT::isOrdinal;
83 static const bool isComparable = TVT::isComparable;
84 static const bool hasMachineParameters = TVT::hasMachineParameters;
85
86 static value_mag_type eps() { return TVT::eps(); }
87
88 static value_mag_type sfmin() { return TVT::sfmin(); }
89
90 static value_mag_type base() { return TVT::base(); }
91
92 static value_mag_type prec() { return TVT::prec(); }
93
94 static value_mag_type t() { return TVT::t(); }
95
96 static value_mag_type rnd() { return TVT::rnd(); }
97
98 static value_mag_type emin() { return TVT::emin(); }
99
100 static value_mag_type rmin() { return TVT::rmin(); }
101
102 static value_mag_type emax() { return TVT::emax(); }
103
104 static value_mag_type rmax() { return TVT::rmax(); }
105
106 static magnitudeType magnitude(const PCEType& a) {
107 return a.two_norm();
108 }
109
110 static innerProductType innerProduct(const PCEType& a, const PCEType& b) {
111 return a.inner_product(b);
112 }
113
114 static PCEType zero() { return PCEType(0.0); }
115
116 static PCEType one() { return PCEType(1.0); }
117
118 static PCEType conjugate(const PCEType& x) {
119 PCEType y = x;
120 y.copyForWrite();
121 y.val() = TVT::conjugate(x.val());
122 return y;
123 }
124
125 static magnitudeType real(const PCEType& x) {
127 const ordinal_type sz = x.size();
128 for (ordinal_type i=0; i<sz; ++i) {
129 value_mag_type t = TVT::real(x.fastAccessCoeff(i));
130 m +=t*t;
131 }
132 return std::sqrt(m);
133 }
134
135 static magnitudeType imag(const PCEType& x) {
137 const ordinal_type sz = x.size();
138 for (ordinal_type i=0; i<sz; ++i) {
139 value_mag_type t = TVT::imag(x.fastAccessCoeff(i));
140 m +=t*t;
141 }
142 return std::sqrt(m);
143 }
144
145
146 static value_type nan() { return TVT::nan(); }
147
148 static bool isnaninf(const PCEType& x) {
149 for (int i=0; i<x.size(); i++)
150 if (TVT::isnaninf(x.fastAccessCoeff(i)))
151 return true;
152 return false;
153 }
154
155 static void seedrandom(unsigned int s) { TVT::seedrandom(s); }
156
157 static PCEType random() { return PCEType(TVT::random()); }
158
159 static const char * name() { return "Sacado::UQ::PCE<>"; }
160
161 static PCEType squareroot(const PCEType& x) { return std::sqrt(x); }
162
163 static PCEType pow(const PCEType& x, const PCEType& y) {
164 return std::pow(x,y);
165 }
166
167 static PCEType log(const PCEType& x) { return std::log(x); }
168
169 static PCEType log10(const PCEType& x) { return std::log10(x); }
170
171 }; // class PCEScalarTraitsImp
172
174 template <typename TypeTo, typename PCEType>
176 typedef typename Sacado::ValueType<PCEType>::type ValueT;
177 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
178 static TypeTo convert( const PCEType t ) {
179 return VTCT::convert(t.val());
180 }
181 static TypeTo safeConvert( const PCEType t ) {
182 return VTCT::safeConvert(t.val());
183 }
184 };
185
187 template <typename Ordinal, typename PCEType>
189 typedef typename Sacado::ValueType<PCEType>::type ValueT;
190 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
191 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
192 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
193
194 public:
195
197 static const bool supportsDirectSerialization = false;
198
200
201
204 const PCEType buffer[]) {
205 Ordinal bytes = 0;
206 for (Ordinal i=0; i<count; i++) {
207 int sz = buffer[i].size();
208 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
209 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
210 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
211 bytes += b1+b2+b3;
212 }
213 return bytes;
214 }
215
217 static void serialize (const Ordinal count,
218 const PCEType buffer[],
219 const Ordinal bytes,
220 char charBuffer[]) {
221 for (Ordinal i=0; i<count; i++) {
222 // First serialize size
223 int sz = buffer[i].size();
224 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
225 iSerT::serialize(1, &sz, b1, charBuffer);
226 charBuffer += b1;
227
228 // Next serialize PCE coefficients
229 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
230 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
231 oSerT::serialize(1, &b2, b3, charBuffer);
232 charBuffer += b3;
233 vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
234 charBuffer += b2;
235 }
236 }
237
240 const char charBuffer[]) {
241 Ordinal count = 0;
242 Ordinal bytes_used = 0;
243 while (bytes_used < bytes) {
244
245 // Bytes for size
246 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
247 bytes_used += b1;
248 charBuffer += b1;
249
250 // Bytes for PCE coefficients
251 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
252 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
253 bytes_used += b3;
254 charBuffer += b3;
255 bytes_used += *b2;
256 charBuffer += *b2;
257
258 ++count;
259 }
260 return count;
261 }
262
264 static void deserialize (const Ordinal bytes,
265 const char charBuffer[],
266 const Ordinal count,
267 PCEType buffer[]) {
268 for (Ordinal i=0; i<count; i++) {
269
270 // Deserialize size
271 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
272 const int *sz = iSerT::convertFromCharPtr(charBuffer);
273 charBuffer += b1;
274
275 // Make sure PCE object is ready to receive values
276 // We assume it has already been initialized with the proper
277 // cijk object
278 if (buffer[i].size() != *sz)
279 buffer[i].reset(buffer[i].cijk(), *sz);
280 buffer[i].copyForWrite();
281
282 // Deserialize PCE coefficients
283 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
284 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
285 charBuffer += b3;
286 vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
287 charBuffer += *b2;
288 }
289
290 }
291
293
294 };
295
296
298 template <typename Ordinal, typename PCEType, typename ValueSerializer>
300
301 public:
302
304 typedef ValueSerializer value_serializer_type;
305
307 typedef typename PCEType::cijk_type cijk_type;
308
309
310 protected:
311 typedef typename Sacado::ValueType<PCEType>::type ValueT;
312 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
313 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
314
316 Teuchos::RCP<const ValueSerializer> vs;
317 int sz;
318
319 public:
320
322 static const bool supportsDirectSerialization = false;
323
325 const Teuchos::RCP<const ValueSerializer>& vs_) :
326 cijk(cijk_), vs(vs_), sz(cijk.dimension()) {}
327
329 cijk_type getSerializerCijk() const { return cijk; }
330
332 Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
333 return vs; }
334
336
337
340 const PCEType buffer[]) const {
341 Ordinal bytes = 0;
342 PCEType *x = NULL;
343 const PCEType *cx;
344 for (Ordinal i=0; i<count; i++) {
345 int my_sz = buffer[i].size();
346 if (sz != my_sz) {
347 if (x == NULL)
348 x = new PCEType;
349 *x = buffer[i];
350 x->reset(cijk);
351 cx = x;
352 }
353 else
354 cx = &(buffer[i]);
355 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
356 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
357 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
358 bytes += b1+b2+b3;
359 }
360 if (x != NULL)
361 delete x;
362 return bytes;
363 }
364
366 void serialize (const Ordinal count,
367 const PCEType buffer[],
368 const Ordinal bytes,
369 char charBuffer[]) const {
370 PCEType *x = NULL;
371 const PCEType *cx;
372 for (Ordinal i=0; i<count; i++) {
373 // First serialize size
374 int my_sz = buffer[i].size();
375 if (sz != my_sz) {
376 if (x == NULL)
377 x = new PCEType(cijk);
378 *x = buffer[i];
379 x->reset(cijk);
380 cx = x;
381 }
382 else
383 cx = &(buffer[i]);
384 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
385 iSerT::serialize(1, &sz, b1, charBuffer);
386 charBuffer += b1;
387
388 // Next serialize PCE coefficients
389 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
390 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
391 oSerT::serialize(1, &b2, b3, charBuffer);
392 charBuffer += b3;
393 vs->serialize(sz, cx->coeff(), b2, charBuffer);
394 charBuffer += b2;
395 }
396 if (x != NULL)
397 delete x;
398 }
399
402 const char charBuffer[]) const {
403 Ordinal count = 0;
404 Ordinal bytes_used = 0;
405 while (bytes_used < bytes) {
406
407 // Bytes for size
408 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
409 bytes_used += b1;
410 charBuffer += b1;
411
412 // Bytes for PCE coefficients
413 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
414 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
415 bytes_used += b3;
416 charBuffer += b3;
417 bytes_used += *b2;
418 charBuffer += *b2;
419
420 ++count;
421 }
422 return count;
423 }
424
426 void deserialize (const Ordinal bytes,
427 const char charBuffer[],
428 const Ordinal count,
429 PCEType buffer[]) const {
430 for (Ordinal i=0; i<count; i++) {
431
432 // Deserialize size
433 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
434 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
435 charBuffer += b1;
436
437 // Create empty PCE object of given size
438 buffer[i].reset(cijk);
439
440 // Deserialize PCE coefficients
441 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
442 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
443 charBuffer += b3;
444 vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
445 charBuffer += *b2;
446 }
447
448 }
449
451
452 };
453
454 } // namespace UQ
455
456} // namespace Sacado
457
458#endif // SACADO_FAD_SCALARTRAITSIMP_HPP
Sacado::UQ::PCE< storage_type > PCEType
Implementation of Teuchos::SerializationTraits for all PCE types.
Teuchos::SerializationTraits< Ordinal, ValueT > vSerT
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
static void deserialize(const Ordinal bytes, const char charBuffer[], const Ordinal count, PCEType buffer[])
Deserialize from an indirect char[] buffer.
static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[])
Return the number of objects for bytes of storage.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Teuchos::SerializationTraits< Ordinal, int > iSerT
static Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[])
Return the number of bytes for count objects.
static void serialize(const Ordinal count, const PCEType buffer[], const Ordinal bytes, char charBuffer[])
Serialize to an indirect char[] buffer.
Serializer object for all PCE types.
Teuchos::SerializationTraits< Ordinal, Ordinal > oSerT
Ordinal fromCountToIndirectBytes(const Ordinal count, const PCEType buffer[]) const
Return the number of bytes for count objects.
PCEType::cijk_type cijk_type
Typename of cijk.
Sacado::ValueType< PCEType >::type ValueT
Teuchos::RCP< const ValueSerializer > vs
PCESerializerImp(const cijk_type &cijk_, const Teuchos::RCP< const ValueSerializer > &vs_)
static const bool supportsDirectSerialization
Whether the type T supports direct serialization.
Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const
Return the number of objects for bytes of storage.
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
ValueSerializer value_serializer_type
Typename of value serializer.
Teuchos::RCP< const value_serializer_type > getValueSerializer() const
Get nested value serializer.
cijk_type getSerializerCijk() const
Return specified serializer size.
Implementation for Teuchos::ScalarTraits for all PCE types.
static PCEType log10(const PCEType &x)
Teuchos::ScalarTraits< value_type > TVT
Sacado::mpl::apply< PCEType, storage_double_type >::type doublePrecision
Sacado::mpl::apply< storage_type, ordinal_type, value_half_type >::type storage_half_type
Sacado::mpl::apply< storage_type, ordinal_type, value_double_type >::type storage_double_type
static PCEType conjugate(const PCEType &x)
Sacado::mpl::apply< storage_type, ordinal_type, value_mag_type >::type storage_mag_type
static PCEType squareroot(const PCEType &x)
static PCEType pow(const PCEType &x, const PCEType &y)
static magnitudeType imag(const PCEType &x)
static magnitudeType magnitude(const PCEType &a)
Sacado::mpl::apply< PCEType, storage_half_type >::type halfPrecision
static innerProductType innerProduct(const PCEType &a, const PCEType &b)
static magnitudeType real(const PCEType &x)
static PCEType log(const PCEType &x)
Implementation for Teuchos::ValueTypeConversionTraits for all PCE types.
Teuchos::ValueTypeConversionTraits< TypeTo, ValueT > VTCT