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" 49 #include "Sacado_Fad_DFad.hpp" 50 #include "Sacado_mpl_apply.hpp" 51 #include "Sacado_Random.hpp" 57 template <
typename PCEType,
typename FadType>
59 RCP<const Stokhos::CompletePolynomialBasis<int,double> >
basis;
60 RCP<Stokhos::Sparse3Tensor<int,double> >
Cijk;
68 typedef typename Sacado::mpl::apply<FadType,PCEType>::type
FadPCEType;
78 Teuchos::Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
79 for (
int i=0; i<d; i++)
87 Cijk =
basis->computeTripleProductTensor();
89 Stokhos::create_product_tensor<execution_space>(*
basis, *
Cijk);
95 rcp(
new Teuchos::ValueTypeSerializer<int,double>())));
102 template <
typename PCEType>
104 const std::string& tag,
105 Teuchos::FancyOStream& out) {
108 typedef Teuchos::SerializationTraits<Ordinal,PCEType> SerT;
112 Ordinal bytes = SerT::fromCountToIndirectBytes(count, &
x[0]);
113 char *charBuffer =
new char[bytes];
114 SerT::serialize(count, &
x[0], bytes, charBuffer);
115 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
118 bool success = (count == count2);
119 out << tag <<
" serialize/deserialize count test";
124 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 128 Teuchos::Array<PCEType> x2(count2);
129 for (
Ordinal i=0; i<count2; i++)
130 x2[i].reset(
x[i].
cijk());
131 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
133 delete [] charBuffer;
136 for (
Ordinal i=0; i<count; i++) {
137 bool success2 = Sacado::IsEqual<PCEType>::eval(
x[i], x2[i]);
138 out << tag <<
" serialize/deserialize pce test " << i;
143 out <<
": \n\tExpected: " <<
x[i] <<
", \n\tGot: " << x2[i]
145 success = success && success2;
151 template <
typename PCEType,
typename Serializer>
153 const Serializer& serializer,
154 const std::string& tag,
155 Teuchos::FancyOStream& out) {
161 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &
x[0]);
162 char *charBuffer =
new char[bytes];
163 serializer.serialize(count, &
x[0], bytes, charBuffer);
166 for (
Ordinal i=0; i<count; i++)
167 x[i].reset(serializer.getSerializerCijk());
170 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171 Teuchos::Array<PCEType> x2(count2);
172 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
174 delete [] charBuffer;
177 bool success = (count == count2);
178 out << tag <<
" serialize/deserialize count test";
183 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 187 for (
Ordinal i=0; i<count; i++) {
188 bool success2 = Sacado::IsEqual<PCEType>::eval(
x[i], x2[i]);
189 out << tag <<
" serialize/deserialize pce test " << i;
194 out <<
": \n\tExpected: " <<
x[i] <<
", \n\tGot: " << x2[i]
196 success = success && success2;
202 template <
typename PCEType,
typename Serializer>
204 const Serializer& serializer,
205 const std::string& tag,
206 Teuchos::FancyOStream& out) {
212 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &
x[0]);
213 char *charBuffer =
new char[bytes];
214 serializer.serialize(count, &
x[0], bytes, charBuffer);
217 Ordinal sz = serializer.getSerializerSize();
218 typedef typename Serializer::value_serializer_type VST;
219 RCP<const VST> vs = serializer.getValueSerializer();
220 for (
Ordinal i=0; i<count; i++) {
224 x[i].val().reset(vs->getSerializerCijk());
228 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
229 Teuchos::Array<PCEType> x2(count2);
230 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
232 delete [] charBuffer;
235 bool success = (count == count2);
236 out << tag <<
" serialize/deserialize count test";
241 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 245 for (
Ordinal i=0; i<count; i++) {
246 bool success2 = Sacado::IsEqual<PCEType>::eval(
x[i], x2[i]);
247 out << tag <<
" serialize/deserialize pce test " << i;
252 out <<
": \n\tExpected: " <<
x[i] <<
", \n\tGot: " << x2[i]
254 success = success && success2;
265 Sacado::Random<double>
rnd;
269 Teuchos::Array<PCEType>
x(n);
270 for (
int i=0; i<n; i++) {
271 x[i].reset(
setup.kokkos_cijk);
276 x, std::string(
"UQ::PCE") +
" Uniform", out);
278 x, *
setup.pce_serializer, std::string(
"UQ::PCE") +
" Uniform PTS", out);
279 success = success1 && success2;
285 Teuchos::Array<PCEType>
x(n);
286 for (
int i=0; i<n; i++) {
290 x, std::string(
"UQ::PCE") +
" Empty", out);
292 x, *
setup.pce_serializer, std::string(
"UQ::PCE") +
" Empty PTS", out);
293 success = success1 && success2;
299 int p[] = { 5, 0, 8, 8, 3, 0 };
300 Teuchos::Array<PCEType>
x(n);
301 for (
int i=0; i<n; i++) {
302 x[i].reset(
setup.kokkos_cijk, p[i]);
303 for (
int j=0;
j<p[i];
j++)
307 x, std::string(
"UQ::PCE") +
" Mixed", out);
309 x, *
setup.pce_serializer, std::string(
"UQ::PCE") +
" Mixed PTS", out);
310 success = success1 && success2;
315 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
318 Teuchos::Array<FadPCEType>
x(n);
319 for (
int i=0; i<n; i++) {
321 for (
int k=0; k<
setup.sz; k++)
322 f.fastAccessCoeff(k) =
rnd.number();
323 x[i] = FadPCEType(p,
f);
324 for (
int j=0;
j<p;
j++) {
326 for (
int k=0; k<
setup.sz; k++)
327 g.fastAccessCoeff(k) =
rnd.number();
328 x[i].fastAccessDx(
j) =
g;
333 std::string(
"UQ::PCE") +
" Nested Uniform", out);
337 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
340 Teuchos::Array<FadPCEType>
x(n);
341 for (
int i=0; i<n; i++) {
343 for (
int k=0; k<
setup.sz; k++)
344 f.fastAccessCoeff(k) =
rnd.number();
345 x[i] = FadPCEType(p,
f);
346 for (
int j=0;
j<p;
j++)
351 x, *
setup.fad_pce_serializer,
352 std::string(
"UQ::PCE") +
" Nested Empty Inner", out);
356 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
358 Teuchos::Array<FadPCEType>
x(n);
359 for (
int i=0; i<n; i++) {
361 for (
int k=0; k<
setup.sz; k++)
362 f.fastAccessCoeff(k) =
rnd.number();
363 x[i] = FadPCEType(
f);
367 x, *
setup.fad_pce_serializer,
368 std::string(
"UQ::PCE") +
" Nested Empty Outer", out);
372 typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
374 Teuchos::Array<FadPCEType>
x(n);
375 for (
int i=0; i<n; i++) {
380 x, *
setup.fad_pce_serializer,
381 std::string(
"UQ::PCE") +
" Nested Empty All", out);
385 Teuchos::GlobalMPISession mpiSession(&argc, &
argv);
387 Kokkos::initialize();
392 int res = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc,
argv);
RCP< FadPCESerializerT > fad_pce_serializer
bool testNestedSerialization(Teuchos::Array< PCEType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j) - expr2.val(j)
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
UnitTestSetup< int, double > setup
Sacado::Fad::DFad< double > FadType
int main(int argc, char *argv[])
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
Sacado::Random< double > rnd
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
TEUCHOS_UNIT_TEST(UQ_PCE_Serialization, Uniform)
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Sacado::UQ::PCE< storage_type > PCEType
PCEType::cijk_type kokkos_cijk_type
kokkos_cijk_type kokkos_cijk
bool testSerialization(const Teuchos::Array< PCEType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Kokkos::DefaultExecutionSpace execution_space
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
Stokhos::DynamicStorage< int, double, execution_space > storage_type
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT