17 #include "Sacado_Fad_DFad.hpp"
18 #include "Sacado_mpl_apply.hpp"
19 #include "Sacado_Random.hpp"
25 template <
typename PCEType,
typename FadType>
34 typedef typename Sacado::mpl::apply<FadType,PCEType>::type
FadPCEType;
45 for (
int i=0; i<d; i++)
69 template <
typename PCEType>
71 const std::string& tag,
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);
85 bool success = (count == count2);
86 out << tag <<
" serialize/deserialize count test";
91 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
96 for (Ordinal i=0; i<count2; i++)
97 x2[i].reset(x[i].expansion());
98 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
100 delete [] charBuffer;
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;
110 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
112 success = success && success2;
118 template <
typename PCEType,
typename Serializer>
120 const Serializer& serializer,
121 const std::string& tag,
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);
133 for (Ordinal i=0; i<count; i++)
134 x[i].reset(serializer.getSerializerExpansion());
137 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
139 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
141 delete [] charBuffer;
144 bool success = (count == count2);
145 out << tag <<
" serialize/deserialize count test";
150 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
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;
161 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
163 success = success && success2;
169 template <
typename PCEType,
typename Serializer>
171 const Serializer& serializer,
172 const std::string& tag,
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);
184 Ordinal sz = serializer.getSerializerSize();
185 typedef typename Serializer::value_serializer_type VST;
187 for (Ordinal i=0; i<count; i++) {
189 for (Ordinal
j=0;
j<sz;
j++)
191 x[i].val().reset(vs->getSerializerExpansion());
195 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
197 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
199 delete [] charBuffer;
202 bool success = (count == count2);
203 out << tag <<
" serialize/deserialize count test";
208 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
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;
219 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
221 success = success && success2;
227 #define PCE_SERIALIZATION_TESTS(PCEType, FadType, PCE) \
228 TEUCHOS_UNIT_TEST( PCE##_Serialization, Uniform ) { \
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(); \
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; \
243 TEUCHOS_UNIT_TEST( PCE##_Serialization, Empty ) { \
245 Teuchos::Array<PCEType> x(n); \
246 for (int i=0; i<n; i++) { \
247 x[i] = rnd.number(); \
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; \
256 TEUCHOS_UNIT_TEST( PCE##_Serialization, Mixed ) { \
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(); \
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; \
272 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEUniform ) { \
273 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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; \
290 testNestedSerialization(x, *setup.fad_pce_serializer, \
291 std::string(#PCE) + " Nested Uniform", out); \
293 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyInner ) { \
294 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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(); \
307 testNestedSerialization( \
308 x, *setup.fad_pce_serializer, \
309 std::string(#PCE) + " Nested Empty Inner", out); \
311 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyOuter ) { \
312 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
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); \
322 testNestedSerialization( \
323 x, *setup.fad_pce_serializer, \
324 std::string(#PCE) + " Nested Empty Outer", out); \
326 TEUCHOS_UNIT_TEST( PCE##_Serialization, FadPCEEmptyAll ) { \
327 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
329 Teuchos::Array<FadPCEType> x(n); \
330 for (int i=0; i<n; i++) { \
331 x[i] = rnd.number(); \
334 testNestedSerialization( \
335 x, *setup.fad_pce_serializer, \
336 std::string(#PCE) + " Nested Empty All", out); \
342 Sacado::Random<double>
rnd;
348 namespace ETPCETest {
349 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