Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparsity_example.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 
10 #include "Stokhos_Epetra.hpp"
12 
13 #ifdef HAVE_MPI
14 #include "Epetra_MpiComm.h"
15 #else
16 #include "Epetra_SerialComm.h"
17 #endif
18 
19 #include "Ifpack_RCMReordering.h"
20 
21 #include <fstream>
22 #include <iostream>
23 
24 // sparsity_example
25 //
26 // usage:
27 // sparsity_example [options]
28 //
29 // output:
30 // prints the sparsity of the sparse 3 tensor specified by the basis,
31 // dimension, order, given by summing over the third index, to a matrix
32 // market file. This sparsity pattern yields the sparsity of the block
33 // stochastic Galerkin matrix which can be visualized, e.g., by matlab.
34 // The full/linear flag determines whether the third index ranges over
35 // the full polynomial dimension, or only over the zeroth and first order
36 // terms.
37 
38 // Basis types
40 const int num_basis_types = 6;
43 const char *basis_type_names[] = {
44  "hermite", "legendre", "clenshaw-curtis", "gauss-patterson", "rys", "jacobi" };
45 
46 // Growth policies
47 const int num_growth_types = 2;
50 const char *growth_type_names[] = { "slow", "moderate" };
51 
52 // Product Basis types
54 const int num_prod_basis_types = 4;
57 const char *prod_basis_type_names[] = {
58  "complete", "tensor", "total", "smolyak" };
59 
60 // Ordering types
62 const int num_ordering_types = 3;
65 const char *ordering_type_names[] = {
66  "total", "lexicographic", "morton-z" };
67 
68 int main(int argc, char **argv)
69 {
70  try {
71 
72  // Initialize MPI
73 #ifdef HAVE_MPI
74  MPI_Init(&argc,&argv);
75 #endif
76 
77  // Setup command line options
79  CLP.setDocString(
80  "This example generates the sparsity pattern for the block stochastic Galerkin matrix.\n");
81  int d = 3;
82  CLP.setOption("dimension", &d, "Stochastic dimension");
83  int p = 5;
84  CLP.setOption("order", &p, "Polynomial order");
85  double drop = 1.0e-12;
86  CLP.setOption("drop", &drop, "Drop tolerance");
87  std::string file = "A.mm";
88  CLP.setOption("filename", &file, "Matrix Market filename");
90  CLP.setOption("basis", &basis_type,
92  "Basis type");
94  CLP.setOption("growth", &growth_type,
96  "Growth type");
97  ProductBasisType prod_basis_type = COMPLETE;
98  CLP.setOption("product_basis", &prod_basis_type,
101  "Product basis type");
102  OrderingType ordering_type = TOTAL_ORDERING;
103  CLP.setOption("ordering", &ordering_type,
106  "Product basis ordering");
107  double alpha = 1.0;
108  CLP.setOption("alpha", &alpha, "Jacobi alpha index");
109  double beta = 1.0;
110  CLP.setOption("beta", &beta, "Jacobi beta index");
111  bool full = true;
112  CLP.setOption("full", "linear", &full, "Use full or linear expansion");
113  bool use_old = false;
114  CLP.setOption("old", "new", &use_old, "Use old or new Cijk algorithm");
115  bool print = false;
116  CLP.setOption("print", "no-print", &print, "Print Cijk to screen");
117  bool save_3tensor = false;
118  CLP.setOption("save_3tensor", "no-save_3tensor", &save_3tensor,
119  "Save full 3tensor to file");
120  std::string file_3tensor = "Cijk.dat";
121  CLP.setOption("filename_3tensor", &file_3tensor,
122  "Filename to store full 3-tensor");
123  bool unique = false;
124  CLP.setOption("unique", "no-unique", &unique,
125  "Only save the unique non-zeros");
126  bool rcm = false;
127  CLP.setOption("rcm", "no-rcm", &rcm, "Reorder using RCM");
128 
129  // Parse arguments
130  CLP.parse( argc, argv );
131 
132  // Basis
134  for (int i=0; i<d; i++) {
135  if (basis_type == HERMITE)
137  p, true, growth_type));
138  else if (basis_type == LEGENDRE)
140  p, true, growth_type));
141  else if (basis_type == CC_LEGENDRE)
142  bases[i] =
144  p, true));
145  else if (basis_type == GP_LEGENDRE)
146  bases[i] =
148  p, true));
149  else if (basis_type == RYS)
151  p, 1.0, true, growth_type));
152  else if (basis_type == JACOBI)
154  p, alpha, beta, true, growth_type));
155  }
160  if (prod_basis_type == COMPLETE)
161  basis =
163  bases, drop, use_old));
164  else if (prod_basis_type == TENSOR) {
165  if (ordering_type == TOTAL_ORDERING)
166  basis =
168  bases, drop));
169  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
170  basis =
172  bases, drop));
173  else if (ordering_type == MORTON_Z_ORDERING)
174  basis =
176  bases, drop));
177  }
178  else if (prod_basis_type == TOTAL) {
179  if (ordering_type == TOTAL_ORDERING)
180  basis =
182  bases, drop));
183  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
184  basis =
186  bases, drop));
187  else if (ordering_type == MORTON_Z_ORDERING)
188  basis =
190  bases, drop));
191  }
192  else if (prod_basis_type == SMOLYAK) {
193  Stokhos::TotalOrderIndexSet<int> index_set(d, p);
194  if (ordering_type == TOTAL_ORDERING)
195  basis =
197  bases, index_set, drop));
198  else if (ordering_type == LEXICOGRAPHIC_ORDERING)
199  basis =
201  bases, index_set, drop));
202  else if (ordering_type == MORTON_Z_ORDERING)
203  basis =
205  bases, index_set, drop));
206  }
207 
208  // Triple product tensor
211  if (full)
212  Cijk = basis->computeTripleProductTensor();
213  else
214  Cijk = basis->computeLinearTripleProductTensor();
215 
216  std::cout << "basis size = " << basis->size()
217  << " num nonzero Cijk entries = " << Cijk->num_entries()
218  << std::endl;
219 
220 #ifdef HAVE_MPI
221  Epetra_MpiComm comm(MPI_COMM_WORLD);
222 #else
223  Epetra_SerialComm comm;
224 #endif
225 
226  if (rcm) {
227  Teuchos::RCP<Cijk_type> Cijk3 = Teuchos::rcp(new Cijk_type);
228  {
229  Cijk_type::k_iterator k_begin = Cijk->k_begin();
230  Cijk_type::k_iterator k_end = Cijk->k_end();
231  for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
232  int k = index(k_it);
233  Cijk_type::kj_iterator j_begin = Cijk->j_begin(k_it);
234  Cijk_type::kj_iterator j_end = Cijk->j_end(k_it);
235  for (Cijk_type::kj_iterator j_it = j_begin; j_it != j_end; ++j_it) {
236  int j = index(j_it);
237  Cijk_type::kji_iterator i_begin = Cijk->i_begin(j_it);
238  Cijk_type::kji_iterator i_end = Cijk->i_end(j_it);
239  for (Cijk_type::kji_iterator i_it = i_begin; i_it != i_end; ++i_it) {
240  int i = index(i_it);
241  double cijk = value(i_it);
242  if (i != 0 || (i == 0 && j == 0 && k == 0))
243  Cijk3->add_term(i, j, k, cijk);
244  }
245  }
246  }
247  }
248  Cijk3->fillComplete();
249 
251  Stokhos::sparse3Tensor2CrsGraph(*basis, *Cijk3, comm);
252  Epetra_CrsMatrix mat(Copy, *graph);
253  mat.FillComplete();
254  mat.PutScalar(1.0);
255  Ifpack_RCMReordering ifpack_rcm;
256  ifpack_rcm.SetParameter("reorder: root node", basis->size()-1);
257  ifpack_rcm.Compute(mat);
258 
259  Teuchos::RCP<Cijk_type> Cijk2 = Teuchos::rcp(new Cijk_type);
260  Cijk_type::k_iterator k_begin = Cijk->k_begin();
261  Cijk_type::k_iterator k_end = Cijk->k_end();
262  for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
263  int k = ifpack_rcm.Reorder(index(k_it));
264  Cijk_type::kj_iterator j_begin = Cijk->j_begin(k_it);
265  Cijk_type::kj_iterator j_end = Cijk->j_end(k_it);
266  for (Cijk_type::kj_iterator j_it = j_begin; j_it != j_end; ++j_it) {
267  int j = ifpack_rcm.Reorder(index(j_it));
268  Cijk_type::kji_iterator i_begin = Cijk->i_begin(j_it);
269  Cijk_type::kji_iterator i_end = Cijk->i_end(j_it);
270  for (Cijk_type::kji_iterator i_it = i_begin; i_it != i_end; ++i_it) {
271  int i = ifpack_rcm.Reorder(index(i_it));
272  double cijk = value(i_it);
273  Cijk2->add_term(i, j, k, cijk);
274  }
275  }
276  }
277  Cijk2->fillComplete();
278  Cijk = Cijk2;
279  }
280 
281  if (print) {
282  std::cout << *Cijk << std::endl;
283  }
284 
285  // Print triple product sparsity to matrix market file
286  Stokhos::sparse3Tensor2MatrixMarket(*basis, *Cijk, comm, file);
287 
288  // Print full 3-tensor to file
289  if (save_3tensor) {
290  std::ofstream cijk_file(file_3tensor.c_str());
291  cijk_file.precision(14);
292  cijk_file.setf(std::ios::scientific);
293  cijk_file << "i, j, k, cijk" << std::endl;
294  Cijk_type::k_iterator k_begin = Cijk->k_begin();
295  Cijk_type::k_iterator k_end = Cijk->k_end();
296  for (Cijk_type::k_iterator k_it=k_begin; k_it!=k_end; ++k_it) {
297  int k = index(k_it);
298  Cijk_type::kj_iterator j_begin = Cijk->j_begin(k_it);
299  Cijk_type::kj_iterator j_end = Cijk->j_end(k_it);
300  for (Cijk_type::kj_iterator j_it = j_begin; j_it != j_end; ++j_it) {
301  int j = index(j_it);
302  Cijk_type::kji_iterator i_begin = Cijk->i_begin(j_it);
303  Cijk_type::kji_iterator i_end = Cijk->i_end(j_it);
304  for (Cijk_type::kji_iterator i_it = i_begin; i_it != i_end; ++i_it) {
305  int i = index(i_it);
306  double cijk = value(i_it);
307  if (!unique || ( i >= j && j >= k ))
308  cijk_file << i << ", "
309  << j << ", "
310  << k << ", "
311  << cijk << std::endl;
312  }
313  }
314  }
315  cijk_file.close();
316  }
317 
319 
320  }
321  catch (std::exception& e) {
322  std::cout << e.what() << std::endl;
323  }
324 
325  return 0;
326 }
const ProductBasisType prod_basis_type_values[]
Hermite polynomial basis.
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
Multivariate orthogonal polynomial basis generated from a total order tensor product of univariate po...
const char * basis_type_names[]
const BasisType basis_type_values[]
void sparse3Tensor2MatrixMarket(const Stokhos::OrthogPolyBasis< ordinal_type, value_type > &basis, const Stokhos::Sparse3Tensor< ordinal_type, value_type > &Cijk, const Epetra_Comm &comm, const std::string &file)
const int num_prod_basis_types
GrowthPolicy
Enumerated type for determining Smolyak growth policies.
const char * growth_type_names[]
const OrderingType ordering_type_values[]
const int num_ordering_types
A comparison functor implementing a strict weak ordering based total-order ordering, recursive on the dimension.
int FillComplete(bool OptimizeDataStorage=true)
OrderingType
int PutScalar(double ScalarConstant)
Legendre polynomial basis using Gauss-Patterson quadrature points.
ProductBasisType
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Rys polynomial basis.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
const int num_growth_types
Jacobi polynomial basis.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
BasisType
const Stokhos::GrowthPolicy growth_type_values[]
Multivariate orthogonal polynomial basis generated from a Smolyak sparse grid.
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)
Multivariate orthogonal polynomial basis generated from a tensor product of univariate polynomials...
Legendre polynomial basis.
Stokhos::Sparse3Tensor< int, double > Cijk_type
int main(int argc, char **argv)
An isotropic total order index set.
A comparison functor implementing a strict weak ordering based Morton Z-ordering. ...
Legendre polynomial basis using Clenshaw-Curtis quadrature points.
Teuchos::RCP< Epetra_CrsGraph > sparse3Tensor2CrsGraph(const Stokhos::OrthogPolyBasis< ordinal_type, value_type > &basis, const Stokhos::Sparse3Tensor< ordinal_type, value_type > &Cijk, const Epetra_Comm &comm)
Build an Epetra_CrsGraph from a sparse 3 tensor.
void setDocString(const char doc_string[])
SparseArrayIterator< index_iterator, value_iterator >::value_reference value(const SparseArrayIterator< index_iterator, value_iterator > &it)
A comparison functor implementing a strict weak ordering based lexographic ordering.
const int num_basis_types
const char * ordering_type_names[]
const char * prod_basis_type_names[]