41 #include "Teuchos_UnitTestHarness.hpp" 42 #include "Teuchos_TestingHelpers.hpp" 43 #include "Teuchos_UnitTestRepository.hpp" 44 #include "Teuchos_GlobalMPISession.hpp" 46 #include "Teuchos_Array.hpp" 48 #include "Sacado_Fad_DFad.hpp" 49 #include "Sacado_mpl_apply.hpp" 50 #include "Sacado_Random.hpp" 56 template <
typename PCEType,
typename FadType>
58 RCP<const Stokhos::CompletePolynomialBasis<int,double> >
basis;
59 RCP<Stokhos::Sparse3Tensor<int,double> >
Cijk;
60 RCP< Stokhos::AlgebraicOrthogPolyExpansion<int,double> >
exp;
65 typedef typename Sacado::mpl::apply<FadType,PCEType>::type
FadPCEType;
75 Teuchos::Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
76 for (
int i=0; i<d; i++)
83 Cijk =
basis->computeTripleProductTensor();
93 rcp(
new Teuchos::ValueTypeSerializer<int,double>())));
100 template <
typename PCEType>
102 const std::string& tag,
103 Teuchos::FancyOStream& out) {
106 typedef Teuchos::SerializationTraits<Ordinal,PCEType> SerT;
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 <<
"." 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]);
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,
153 Teuchos::FancyOStream& out) {
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);
169 Teuchos::Array<PCEType> x2(count2);
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,
204 Teuchos::FancyOStream& out) {
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;
217 RCP<const VST> vs = serializer.getValueSerializer();
218 for (
Ordinal i=0; i<count; i++) {
222 x[i].val().reset(vs->getSerializerExpansion());
226 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
227 Teuchos::Array<PCEType> x2(count2);
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;
380 Sacado::Random<double>
rnd;
387 Teuchos::GlobalMPISession mpiSession(&argc, &
argv);
388 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc,
argv);
Sacado::ETPCE::OrthogPoly< double, storage_type > pce_type
RCP< FadPCESerializerT > fad_pce_serializer
UnitTestSetup< pce_type, fad_type > setup
int main(int argc, char *argv[])
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
Sacado::Fad::DFad< double > fad_type
Orthogonal polynomial expansions limited to algebraic operations.
Stokhos::StandardStorage< int, double > storage_type
#define PCE_SERIALIZATION_TESTS(PCEType, FadType, PCE)
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
Sacado::Fad::DFad< double > fad_type
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Sacado::Random< double > rnd
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
UnitTestSetup< pce_type, fad_type > setup
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
Sacado::PCE::OrthogPoly< double, storage_type > pce_type
Sacado::Random< double > rnd
bool testNestedSerialization(Teuchos::Array< PCEType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
bool testSerialization(const Teuchos::Array< PCEType > &x, const std::string &tag, Teuchos::FancyOStream &out)
RCP< Stokhos::AlgebraicOrthogPolyExpansion< int, double > > exp
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT