Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoPCESerializationTests.cpp
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 
14 
15 #include "Teuchos_Array.hpp"
16 #include "Stokhos_Sacado.hpp"
17 #include "Sacado_Fad_DFad.hpp"
18 #include "Sacado_mpl_apply.hpp"
19 #include "Sacado_Random.hpp"
20 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
23 
24 // Common setup for unit tests
25 template <typename PCEType, typename FadType>
26 struct UnitTestSetup {
30 
33 
34  typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
37  int sz;
38 
40  const int d = 2;
41  const int p = 7;
42 
43  // Create product basis
45  for (int i=0; i<d; i++)
46  bases[i] =
48  basis =
50 
51  // Triple product tensor
53 
54  // Expansion
55  exp =
57 
58  // Serializers
60  rcp(new PCESerializerT(
61  exp,
64 
65  sz = basis->size();
66  }
67 };
68 
69 template <typename PCEType>
71  const std::string& tag,
72  Teuchos::FancyOStream& out) {
73 
74  typedef int Ordinal;
76 
77  // Serialize
78  Ordinal count = x.size();
79  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
80  char *charBuffer = new char[bytes];
81  SerT::serialize(count, &x[0], bytes, charBuffer);
82  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
83 
84  // Check counts match
85  bool success = (count == count2);
86  out << tag << " serialize/deserialize count test";
87  if (success)
88  out << " passed";
89  else
90  out << " failed";
91  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
92  << std::endl;
93 
94  // Deserialize
95  Teuchos::Array<PCEType> x2(count2);
96  for (Ordinal i=0; i<count2; i++)
97  x2[i].reset(x[i].expansion());
98  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
99 
100  delete [] charBuffer;
101 
102  // Check coefficients match
103  for (Ordinal i=0; i<count; i++) {
104  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
105  out << tag << " serialize/deserialize pce test " << i;
106  if (success2)
107  out << " passed";
108  else
109  out << " failed";
110  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
111  << "." << std::endl;
112  success = success && success2;
113  }
114 
115  return success;
116 }
117 
118 template <typename PCEType, typename Serializer>
120  const Serializer& serializer,
121  const std::string& tag,
122  Teuchos::FancyOStream& out) {
123 
124  typedef int Ordinal;
125 
126  // Serialize
127  Ordinal count = x.size();
128  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
129  char *charBuffer = new char[bytes];
130  serializer.serialize(count, &x[0], bytes, charBuffer);
131 
132  // Reset x to given expansion
133  for (Ordinal i=0; i<count; i++)
134  x[i].reset(serializer.getSerializerExpansion());
135 
136  // Deserialize
137  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
138  Teuchos::Array<PCEType> x2(count2);
139  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
140 
141  delete [] charBuffer;
142 
143  // Check counts match
144  bool success = (count == count2);
145  out << tag << " serialize/deserialize count test";
146  if (success)
147  out << " passed";
148  else
149  out << " failed";
150  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
151  << std::endl;
152 
153  // Check coefficients match
154  for (Ordinal i=0; i<count; i++) {
155  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
156  out << tag << " serialize/deserialize pce test " << i;
157  if (success2)
158  out << " passed";
159  else
160  out << " failed";
161  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
162  << "." << std::endl;
163  success = success && success2;
164  }
165 
166  return success;
167 }
168 
169 template <typename PCEType, typename Serializer>
171  const Serializer& serializer,
172  const std::string& tag,
173  Teuchos::FancyOStream& out) {
174 
175  typedef int Ordinal;
176 
177  // Serialize
178  Ordinal count = x.size();
179  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
180  char *charBuffer = new char[bytes];
181  serializer.serialize(count, &x[0], bytes, charBuffer);
182 
183  // Reset x to given expansion
184  Ordinal sz = serializer.getSerializerSize();
185  typedef typename Serializer::value_serializer_type VST;
186  RCP<const VST> vs = serializer.getValueSerializer();
187  for (Ordinal i=0; i<count; i++) {
188  x[i].expand(sz);
189  for (Ordinal j=0; j<sz; j++)
190  x[i].fastAccessDx(j).reset(vs->getSerializerExpansion());
191  x[i].val().reset(vs->getSerializerExpansion());
192  }
193 
194  // Deserialize
195  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
196  Teuchos::Array<PCEType> x2(count2);
197  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
198 
199  delete [] charBuffer;
200 
201  // Check counts match
202  bool success = (count == count2);
203  out << tag << " serialize/deserialize count test";
204  if (success)
205  out << " passed";
206  else
207  out << " failed";
208  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
209  << std::endl;
210 
211  // Check coefficients match
212  for (Ordinal i=0; i<count; i++) {
213  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
214  out << tag << " serialize/deserialize pce test " << i;
215  if (success2)
216  out << " passed";
217  else
218  out << " failed";
219  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
220  << "." << std::endl;
221  success = success && success2;
222  }
223 
224  return success;
225 }
226 
227 #define PCE_SERIALIZATION_TESTS(PCEType, FadType, PCE) \
228  TEUCHOS_UNIT_TEST( PCE##_Serialization, Uniform ) { \
229  int n = 7; \
230  Teuchos::Array<PCEType> x(n); \
231  for (int i=0; i<n; i++) { \
232  x[i] = PCEType(setup.exp); \
233  for (int j=0; j<setup.sz; j++) \
234  x[i].fastAccessCoeff(j) = rnd.number(); \
235  } \
236  bool success1 = testSerialization( \
237  x, std::string(#PCE) + " Uniform", out); \
238  bool success2 = testSerialization( \
239  x, *setup.pce_serializer, std::string(#PCE) + " Uniform PTS", out); \
240  success = success1 && success2; \
241  } \
242  \
243  TEUCHOS_UNIT_TEST( PCE##_Serialization, Empty ) { \
244  int n = 7; \
245  Teuchos::Array<PCEType> x(n); \
246  for (int i=0; i<n; i++) { \
247  x[i] = rnd.number(); \
248  } \
249  bool success1 = testSerialization( \
250  x, std::string(#PCE) + " Empty", out); \
251  bool success2 = testSerialization( \
252  x, *setup.pce_serializer, std::string(#PCE) + " Empty PTS", out); \
253  success = success1 && success2; \
254  } \
255  \
256  TEUCHOS_UNIT_TEST( PCE##_Serialization, Mixed ) { \
257  int n = 6; \
258  int p[] = { 5, 0, 8, 8, 3, 0 }; \
259  Teuchos::Array<PCEType> x(n); \
260  for (int i=0; i<n; i++) { \
261  x[i] = PCEType(setup.exp, p[i]); \
262  for (int j=0; j<p[i]; j++) \
263  x[i].fastAccessCoeff(j) = rnd.number(); \
264  } \
265  bool success1 = testSerialization( \
266  x, std::string(#PCE) + " Mixed", out); \
267  bool success2 = testSerialization( \
268  x, *setup.pce_serializer, std::string(#PCE) + " Mixed PTS", out); \
269  success = success1 && success2; \
270  } \
271  \
272  TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEUniform ) { \
273  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
274  int n = 7; \
275  int p = 3; \
276  Teuchos::Array<FadPCEType> x(n); \
277  for (int i=0; i<n; i++) { \
278  PCEType f(setup.exp); \
279  for (int k=0; k<setup.sz; k++) \
280  f.fastAccessCoeff(k) = rnd.number(); \
281  x[i] = FadPCEType(p, f); \
282  for (int j=0; j<p; j++) { \
283  PCEType g(setup.exp); \
284  for (int k=0; k<setup.sz; k++) \
285  g.fastAccessCoeff(k) = rnd.number(); \
286  x[i].fastAccessDx(j) = g; \
287  } \
288  } \
289  success = \
290  testNestedSerialization(x, *setup.fad_pce_serializer, \
291  std::string(#PCE) + " Nested Uniform", out); \
292  } \
293  TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyInner ) { \
294  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
295  int n = 7; \
296  int p = 3; \
297  Teuchos::Array<FadPCEType> x(n); \
298  for (int i=0; i<n; i++) { \
299  PCEType f(setup.exp); \
300  for (int k=0; k<setup.sz; k++) \
301  f.fastAccessCoeff(k) = rnd.number(); \
302  x[i] = FadPCEType(p, f); \
303  for (int j=0; j<p; j++) \
304  x[i].fastAccessDx(j) = rnd.number(); \
305  } \
306  success = \
307  testNestedSerialization( \
308  x, *setup.fad_pce_serializer, \
309  std::string(#PCE) + " Nested Empty Inner", out); \
310  } \
311  TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyOuter ) { \
312  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
313  int n = 7; \
314  Teuchos::Array<FadPCEType> x(n); \
315  for (int i=0; i<n; i++) { \
316  PCEType f(setup.exp); \
317  for (int k=0; k<setup.sz; k++) \
318  f.fastAccessCoeff(k) = rnd.number(); \
319  x[i] = FadPCEType(f); \
320  } \
321  success = \
322  testNestedSerialization( \
323  x, *setup.fad_pce_serializer, \
324  std::string(#PCE) + " Nested Empty Outer", out); \
325  } \
326  TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyAll ) { \
327  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
328  int n = 7; \
329  Teuchos::Array<FadPCEType> x(n); \
330  for (int i=0; i<n; i++) { \
331  x[i] = rnd.number(); \
332  } \
333  success = \
334  testNestedSerialization( \
335  x, *setup.fad_pce_serializer, \
336  std::string(#PCE) + " Nested Empty All", out); \
337  }
338 
340 typedef Sacado::Fad::DFad<double> fad_type;
341 namespace PCETest {
342  Sacado::Random<double> rnd;
346 }
347 
348 namespace ETPCETest {
349  Sacado::Random<double> rnd;
353 }
354 
355 int main( int argc, char* argv[] ) {
356  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
358 }
Stokhos::StandardStorage< int, double > storage_type
Sacado::ETPCE::OrthogPoly< double, storage_type > pce_type
RCP< FadPCESerializerT > fad_pce_serializer
UnitTestSetup< pce_type, fad_type > setup
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
#define PCE_SERIALIZATION_TESTS(PCEType, FadType, PCE)
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
static int runUnitTestsFromMain(int argc, char *argv[])
Sacado::Fad::DFad< double > fad_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Sacado::Random< double > rnd
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
bool testSerialization(const Teuchos::Array< VecType > &x, const std::string &tag, Teuchos::FancyOStream &out)
UnitTestSetup< pce_type, fad_type > setup
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
Sacado::PCE::OrthogPoly< double, storage_type > pce_type
bool testNestedSerialization(Teuchos::Array< VecType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
int main(int argc, char **argv)
size_type size() const
virtual ordinal_type size() const
Return total size of basis.
Sacado::Random< double > rnd
RCP< Stokhos::AlgebraicOrthogPolyExpansion< int, double > > exp
Sacado::Fad::DFad< double > fad_type
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT