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