Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_Sparse3TensorUnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Stokhos.hpp"
17 
18 namespace Sparse3TensorUnitTest {
19 
21 
22  // Common setup for unit tests
23  template <typename OrdinalType, typename ValueType>
24  struct UnitTestSetup {
25  ValueType rtol, atol, sparse_tol;
26  OrdinalType d, p, sz;
31 
32  UnitTestSetup(): d(3), p(4), bases(d) {
33  rtol = 1e-14;
34  atol = 1e-14;
35  sparse_tol = 1e-14;
36 
37  // Create product basis
38  for (OrdinalType i=0; i<d; i++)
39  bases[i] =
41  basis =
43  sz = basis->size();
44 
45  // Tensor product quadrature
46  quad =
48 
49 
50  // Triple product tensor
52  }
53 
54  };
55 
57 
58  TEUCHOS_UNIT_TEST( Stokhos_Sparse3Tensor, Values ) {
59  const Teuchos::Array<double>& weights = setup.quad->getQuadWeights();
60  const Teuchos::Array< Teuchos::Array<double> > & values =
61  setup.quad->getBasisAtQuadPoints();
62 
63  success = true;
64  for (Cijk_type::k_iterator k_it=setup.Cijk->k_begin();
65  k_it!=setup.Cijk->k_end(); ++k_it) {
66  int k = Stokhos::index(k_it);
67  for (Cijk_type::kj_iterator j_it = setup.Cijk->j_begin(k_it);
68  j_it != setup.Cijk->j_end(k_it); ++j_it) {
69  int j = Stokhos::index(j_it);
70  for (Cijk_type::kji_iterator i_it = setup.Cijk->i_begin(j_it);
71  i_it != setup.Cijk->i_end(j_it); ++i_it) {
72  int i = Stokhos::index(i_it);
73  double c = Stokhos::value(i_it);
74 
75  double c2 = 0.0;
76  int nqp = weights.size();
77  for (int qp=0; qp<nqp; qp++)
78  c2 += weights[qp]*values[qp][i]*values[qp][j]*values[qp][k];
79 
80  double tol = setup.atol + c*setup.rtol;
81  double err = std::abs(c-c2);
82  bool s = err < tol;
83  if (!s) {
84  out << std::endl
85  << "Check: rel_err( C(" << i << "," << j << "," << k << ") )"
86  << " = " << "rel_err( " << c << ", " << c2 << " ) = " << err
87  << " <= " << tol << " : ";
88  if (s) out << "Passed.";
89  else
90  out << "Failed!";
91  out << std::endl;
92  }
93  success = success && s;
94  }
95  }
96  }
97  }
98 
99  TEUCHOS_UNIT_TEST( Stokhos_Sparse3Tensor, Sparsity ) {
102 
103  // Create 1-D triple products
105  for (int i=0; i<setup.d; i++)
106  Cijk_1d[i] = setup.bases[i]->computeTripleProductTensor();
107 
108  for (int j=0; j<setup.sz; j++) {
109  Stokhos::MultiIndex<int> terms_j = setup.basis->term(j);
110  for (int i=0; i<setup.sz; i++) {
111  Stokhos::MultiIndex<int> terms_i = setup.basis->term(i);
112  for (int k=0; k<setup.sz; k++) {
113  Stokhos::MultiIndex<int> terms_k = setup.basis->term(k);
114  double c = 1.0;
115  for (int l=0; l<setup.d; l++)
116  c *= (*Cijk_1d[l])(terms_i[l],terms_j[l],terms_k[l]);
117  if (std::abs(c/setup.basis->norm_squared(i)) > setup.sparse_tol)
118  Cijk_quad->add_term(i,j,k,c);
119  }
120  }
121  }
122  Cijk_quad->fillComplete();
123 
124  // Check number of nonzeros
125  int nnz = setup.Cijk->num_entries();
126  int nnz_quad = Cijk_quad->num_entries();
127  success = (nnz == nnz_quad);
128  if (!success)
129  out << std::endl
130  << "Check: nnz(C) = " << nnz << " == nnz(C_quad) = " << nnz_quad
131  << ": Failed";
132  for (Cijk_type::k_iterator k_it=Cijk_quad->k_begin();
133  k_it!=Cijk_quad->k_end(); ++k_it) {
134  int k = Stokhos::index(k_it);
135  for (Cijk_type::kj_iterator j_it = Cijk_quad->j_begin(k_it);
136  j_it != Cijk_quad->j_end(k_it); ++j_it) {
137  int j = Stokhos::index(j_it);
138  for (Cijk_type::kji_iterator i_it = Cijk_quad->i_begin(j_it);
139  i_it != Cijk_quad->i_end(j_it); ++i_it) {
140  int i = Stokhos::index(i_it);
141  double c = setup.Cijk->getValue(i,j,k);
142  double c2 = Stokhos::value(i_it);
143 
144  double tol = setup.atol + c2*setup.rtol;
145  double err = std::abs(c-c2);
146  bool s = err < tol;
147  if (!s) {
148  out << std::endl
149  << "Check: rel_err( C(" << i << "," << j << "," << k << ") )"
150  << " = " << "rel_err( " << c << ", " << c2 << " ) = " << err
151  << " <= " << tol << " : ";
152  if (s) out << "Passed.";
153  else
154  out << "Failed!";
155  out << std::endl;
156  }
157  success = success && s;
158  }
159  }
160  }
161  }
162 
163  TEUCHOS_UNIT_TEST( Stokhos_Sparse3Tensor, GetValue ) {
164  success = true;
165  bool s;
166  double c, c_true;
167 
168  // Check getValue() for a few different indices
169 
170  c = setup.Cijk->getValue(0, 0, 0);
171  c_true = 1.0;
172  s = Stokhos::compareValues(c, "c", c_true, "c_true", setup.rtol, setup.atol,
173  out);
174  success = success && s;
175 
176  c = setup.Cijk->getValue(9, 25, 4);
177  c_true = 0.04;
178  s = Stokhos::compareValues(c, "c", c_true, "c_true", setup.rtol, setup.atol,
179  out);
180  success = success && s;
181 
182  c = setup.Cijk->getValue(8, 25, 4);
183  c_true = 0.0;
184  s = Stokhos::compareValues(c, "c", c_true, "c_true", setup.rtol, setup.atol,
185  out);
186  success = success && s;
187  }
188 
189 }
190 
191 int main( int argc, char* argv[] ) {
192  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
194 }
TEUCHOS_UNIT_TEST(Stokhos_Sparse3Tensor, Values)
bool compareValues(const ValueType &a1, const std::string &a1_name, const ValueType &a2, const std::string &a2_name, const ValueType &rel_tol, const ValueType &abs_tol, Teuchos::FancyOStream &out)
Bi-directional iterator for traversing a sparse array.
UnitTestSetup< int, double > setup
A multidimensional index.
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
Stokhos::Sparse3Tensor< int, double > Cijk_type
int main(int argc, char **argv)
size_type size() const
virtual ordinal_type size() const
Return total size of basis.
Teuchos::RCP< const Stokhos::CompletePolynomialBasis< OrdinalType, ValueType > > basis
Teuchos::RCP< const Stokhos::Quadrature< OrdinalType, ValueType > > quad
Teuchos::Array< Teuchos::RCP< const Stokhos::OneDOrthogPolyBasis< OrdinalType, ValueType > > > bases
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...