Debian Codegraph @

packages/stokhos/src/sacado/Sacado_PCE_ScalarTraitsImp.hpp

raw
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"
14 #include "Teuchos_SerializationTraits.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
24 //! Implementation for Teuchos::ScalarTraits for all PCE types
25 template <typename PCEType>
26 struct ScalarTraitsImp {
27 typedef typename Sacado::ValueType<PCEType>::type ValueT;
28
29 typedef typename Teuchos::ScalarTraits<ValueT>::magnitudeType magnitudeType;
30 //typedef typename Teuchos::ScalarTraits<ValueT>::innerProductType innerProductType;
31 typedef ValueT 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
35 static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
36 static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
37 static const bool isComparable =
38 Teuchos::ScalarTraits<ValueT>::isComparable;
39 static const bool hasMachineParameters =
40 Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
41 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
42 return Teuchos::ScalarTraits<ValueT>::eps();
43 }
44 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
45 return Teuchos::ScalarTraits<ValueT>::sfmin();
46 }
47 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base() {
48 return Teuchos::ScalarTraits<ValueT>::base();
49 }
50 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec() {
51 return Teuchos::ScalarTraits<ValueT>::prec();
52 }
53 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t() {
54 return Teuchos::ScalarTraits<ValueT>::t();
55 }
56 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rnd() {
57 return Teuchos::ScalarTraits<ValueT>::rnd();
58 }
59 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin() {
60 return Teuchos::ScalarTraits<ValueT>::emin();
61 }
62 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin() {
63 return Teuchos::ScalarTraits<ValueT>::rmin();
64 }
65 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax() {
66 return Teuchos::ScalarTraits<ValueT>::emax();
67 }
68 static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax() {
69 return Teuchos::ScalarTraits<ValueT>::rmax();
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
133 return PCEType(Teuchos::ScalarTraits<ValueT>::imag(x.val()));
134 }
135
136 static ValueT nan() {
137 return Teuchos::ScalarTraits<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) {
146 Teuchos::ScalarTraits<ValueT>::seedrandom(s);
147 }
148 static ValueT random() {
149 return Teuchos::ScalarTraits<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
175 Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
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;
182 if (Teuchos::ScalarTraits<ValueT>::isComplex) {
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
192 //! Implementation for Teuchos::ValueTypeConversionTraits for all PCE types
193 template <typename TypeTo, typename PCEType>
194 struct ValueTypeConversionTraitsImp {
195 typedef typename Sacado::ValueType<PCEType>::type ValueT;
196 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
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
206 //! Implementation of Teuchos::SerializationTraits for all PCE types
207 template <typename Ordinal, typename PCEType>
208 class SerializationTraitsImp {
209 typedef typename Sacado::ValueType<PCEType>::type ValueT;
210 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
211 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
212 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
213
214 public:
215
216 /// \brief Whether the type T supports direct serialization.
217 static const bool supportsDirectSerialization = false;
218
219 //! @name Indirect serialization functions (always defined and supported)
220 //@{
221
222 /** \brief Return the number of bytes for <tt>count</tt> objects. */
223 static Ordinal fromCountToIndirectBytes(const Ordinal count,
224 const PCEType buffer[]) {
225 Ordinal bytes = 0;
226 for (Ordinal i=0; i<count; i++) {
227 int sz = buffer[i].size();
228 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
229 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
230 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
231 bytes += b1+b2+b3;
232 }
233 return bytes;
234 }
235
236 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
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();
244 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
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());
250 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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
258 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
259 static Ordinal fromIndirectBytesToCount(const Ordinal bytes,
260 const char charBuffer[]) {
261 Ordinal count = 0;
262 Ordinal bytes_used = 0;
263 while (bytes_used < bytes) {
264
265 // Bytes for size
266 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
267 bytes_used += b1;
268 charBuffer += b1;
269
270 // Bytes for PCE coefficients
271 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
283 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
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
291 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
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
303 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
312 //@}
313
314 };
315
316
317 //! Serializer object for all PCE types
318 template <typename Ordinal, typename PCEType, typename ValueSerializer>
319 class SerializerImp {
320
321 public:
322
323 //! Typename of value serializer
324 typedef ValueSerializer value_serializer_type;
325
326 //! Typename of expansion
327 typedef typename PCEType::expansion_type expansion_type;
328
329
330 protected:
331 typedef typename Sacado::ValueType<PCEType>::type ValueT;
332 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
333 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
334
335 Teuchos::RCP<expansion_type> expansion;
336 Teuchos::RCP<const ValueSerializer> vs;
337 int sz;
338
339 public:
340
341 /// \brief Whether the type T supports direct serialization.
342 static const bool supportsDirectSerialization = false;
343
344 SerializerImp(const Teuchos::RCP<expansion_type>& expansion_,
345 const Teuchos::RCP<const ValueSerializer>& vs_) :
346 expansion(expansion_), vs(vs_), sz(expansion->size()) {}
347
348 //! Return specified serializer size
349 Teuchos::RCP<expansion_type> getSerializerExpansion() const {
350 return expansion; }
351
352 //! Get nested value serializer
353 Teuchos::RCP<const value_serializer_type> getValueSerializer() const {
354 return vs; }
355
356 //! @name Indirect serialization functions (always defined and supported)
357 //@{
358
359 /** \brief Return the number of bytes for <tt>count</tt> objects. */
360 Ordinal fromCountToIndirectBytes(const Ordinal count,
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]);
376 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
377 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
378 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
379 bytes += b1+b2+b3;
380 }
381 if (x != NULL)
382 delete x;
383 return bytes;
384 }
385
386 /** \brief Serialize to an indirect <tt>char[]</tt> buffer. */
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]);
405 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
406 iSerT::serialize(1, &sz, b1, charBuffer);
407 charBuffer += b1;
408
409 // Next serialize PCE coefficients
410 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
411 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
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
421 /** \brief Return the number of objects for <tt>bytes</tt> of storage. */
422 Ordinal fromIndirectBytesToCount(const Ordinal bytes,
423 const char charBuffer[]) const {
424 Ordinal count = 0;
425 Ordinal bytes_used = 0;
426 while (bytes_used < bytes) {
427
428 // Bytes for size
429 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
430 bytes_used += b1;
431 charBuffer += b1;
432
433 // Bytes for PCE coefficients
434 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
446 /** \brief Deserialize from an indirect <tt>char[]</tt> buffer. */
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
454 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
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
462 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
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
471 //@}
472
473 };
474
475 } // namespace PCE
476
477 } // namespace Sacado
478
479 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP