48 #include "Sacado_Fad_DFad.hpp"
49 #include "Sacado_mpl_apply.hpp"
50 #include "Sacado_Random.hpp"
56 template <
typename PCEType,
typename FadType>
65 typedef typename Sacado::mpl::apply<FadType,PCEType>::type
FadPCEType;
76 for (
int i=0; i<d; i++)
100 template <
typename PCEType>
102 const std::string& tag,
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);
116 bool success = (count == count2);
117 out << tag <<
" serialize/deserialize count test";
122 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
127 for (Ordinal i=0; i<count2; i++)
128 x2[i].reset(x[i].expansion());
129 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
131 delete [] charBuffer;
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;
141 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
143 success = success && success2;
149 template <
typename PCEType,
typename Serializer>
151 const Serializer& serializer,
152 const std::string& tag,
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);
164 for (Ordinal i=0; i<count; i++)
165 x[i].reset(serializer.getSerializerExpansion());
168 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
170 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
172 delete [] charBuffer;
175 bool success = (count == count2);
176 out << tag <<
" serialize/deserialize count test";
181 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
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;
192 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
194 success = success && success2;
200 template <
typename PCEType,
typename Serializer>
202 const Serializer& serializer,
203 const std::string& tag,
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);
215 Ordinal sz = serializer.getSerializerSize();
216 typedef typename Serializer::value_serializer_type VST;
218 for (Ordinal i=0; i<count; i++) {
220 for (Ordinal
j=0;
j<sz;
j++)
222 x[i].val().reset(vs->getSerializerExpansion());
226 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
228 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
230 delete [] charBuffer;
233 bool success = (count == count2);
234 out << tag <<
" serialize/deserialize count test";
239 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
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;
250 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
252 success = success && success2;
258 #define PCE_SERIALIZATION_TESTS(PCEType, FadType, PCE) \
259 TEUCHOS_UNIT_TEST( PCE##_Serialization, Uniform ) { \
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(); \
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; \
274 TEUCHOS_UNIT_TEST( PCE##_Serialization, Empty ) { \
276 Teuchos::Array<PCEType> x(n); \
277 for (int i=0; i<n; i++) { \
278 x[i] = rnd.number(); \
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; \
287 TEUCHOS_UNIT_TEST( PCE##_Serialization, Mixed ) { \
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(); \
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; \
303 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEUniform ) { \
304 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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; \
321 testNestedSerialization(x, *setup.fad_pce_serializer, \
322 std::string(#PCE) + " Nested Uniform", out); \
324 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyInner ) { \
325 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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(); \
338 testNestedSerialization( \
339 x, *setup.fad_pce_serializer, \
340 std::string(#PCE) + " Nested Empty Inner", out); \
342 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyOuter ) { \
343 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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); \
353 testNestedSerialization( \
354 x, *setup.fad_pce_serializer, \
355 std::string(#PCE) + " Nested Empty Outer", out); \
357 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyAll ) { \
358 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
360 Teuchos::Array<FadPCEType> x(n); \
361 for (int i=0; i<n; i++) { \
362 x[i] = rnd.number(); \
365 testNestedSerialization( \
366 x, *setup.fad_pce_serializer, \
367 std::string(#PCE) + " Nested Empty All", out); \
373 Sacado::Random<double>
rnd;
379 namespace ETPCETest {
380 Sacado::Random<double>
rnd;
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)
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