Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparsity_slice_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 #include <sstream>
14 
15 #ifdef HAVE_MPI
16 #include "Epetra_MpiComm.h"
17 #else
18 #include "Epetra_SerialComm.h"
19 #endif
20 #include "Epetra_LocalMap.h"
21 #include "Epetra_CrsMatrix.h"
22 #include "EpetraExt_RowMatrixOut.h"
23 
24 // sparsity_example
25 //
26 // usage:
27 // sparsity_slice_example [options]
28 //
29 // output:
30 // prints the sparsity of the sparse 3 tensor specified by the basis,
31 // dimension, and order, printing a matrix-market file for each k-slice.
32 // The full/linear flag determines whether the third index ranges over
33 // the full polynomial dimension, or only over the zeroth and first order
34 // terms.
35 
36 // Basis types
38 const int num_basis_types = 3;
40 const char *basis_type_names[] = { "hermite", "legendre", "rys" };
41 
42 int main(int argc, char **argv)
43 {
44  int num_k = 0;
45 
46  try {
47 
48  // Initialize MPI
49 #ifdef HAVE_MPI
50  MPI_Init(&argc,&argv);
51 #endif
52 
53  // Setup command line options
55  CLP.setDocString(
56  "This example generates the sparsity pattern for the block stochastic Galerkin matrix.\n");
57  int d = 3;
58  CLP.setOption("dimension", &d, "Stochastic dimension");
59  int p = 5;
60  CLP.setOption("order", &p, "Polynomial order");
61  double drop = 1.0e-15;
62  CLP.setOption("drop", &drop, "Drop tolerance");
63  std::string file_base = "A";
64  CLP.setOption("base filename", &file_base, "Base filename for matrix market files");
66  CLP.setOption("basis", &basis_type,
68  "Basis type");
69  bool full = true;
70  CLP.setOption("full", "linear", &full, "Use full or linear expansion");
71  bool use_old = false;
72  CLP.setOption("old", "new", &use_old, "Use old or new Cijk algorithm");
73 
74  // Parse arguments
75  CLP.parse( argc, argv );
76 
77  // Basis
79  for (int i=0; i<d; i++) {
80  if (basis_type == HERMITE)
82  else if (basis_type == LEGENDRE)
84  else if (basis_type == RYS)
85  bases[i] = Teuchos::rcp(new Stokhos::RysBasis<int,double>(p, 1.0,
86  false));
87  }
90  drop,
91  use_old));
92 
93  // Triple product tensor
95  if (full) {
96  num_k = basis->size();
97  Cijk = basis->computeTripleProductTensor();
98  }
99  else {
100  num_k = basis->dimension()+1;
101  Cijk = basis->computeLinearTripleProductTensor();
102  }
103 
104  std::cout << "basis size = " << basis->size()
105  << " num nonzero Cijk entries = " << Cijk->num_entries()
106  << std::endl;
107 
108 #ifdef HAVE_MPI
109  Epetra_MpiComm comm(MPI_COMM_WORLD);
110 #else
111  Epetra_SerialComm comm;
112 #endif
113 
114  // Number of stochastic rows
115  int num_rows = basis->size();
116 
117  // Replicated local map
118  Epetra_LocalMap map(num_rows, 0, comm);
119 
120  // Loop over Cijk entries including a non-zero in the graph at
121  // indices (i,j) if Cijk is non-zero for each k
123  double one = 1.0;
124  for (Cijk_type::k_iterator k_it=Cijk->k_begin();
125  k_it!=Cijk->k_end(); ++k_it) {
126  int k = index(k_it);
127  Epetra_CrsMatrix mat(Copy, map, 1);
128  for (Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
129  j_it != Cijk->j_end(k_it); ++j_it) {
130  int j = index(j_it);
131  for (Cijk_type::kji_iterator i_it = Cijk->i_begin(j_it);
132  i_it != Cijk->i_end(j_it); ++i_it) {
133  int i = index(i_it);
134  mat.InsertGlobalValues(i, 1, &one, &j);
135  }
136  }
137  mat.FillComplete();
138 
139  // Construct file name
140  std::stringstream ss;
141  ss << file_base << "_" << k << ".mm";
142  std::string file = ss.str();
143 
144  // Save matrix to file
145  EpetraExt::RowMatrixToMatrixMarketFile(file.c_str(), mat);
146  }
147 
148  }
149  catch (std::exception& e) {
150  std::cout << e.what() << std::endl;
151  }
152 
153  return num_k;
154 }
Hermite polynomial basis.
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
const char * basis_type_names[]
const BasisType basis_type_values[]
virtual int InsertGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
int FillComplete(bool OptimizeDataStorage=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Rys polynomial basis.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
BasisType
Legendre polynomial basis.
Stokhos::Sparse3Tensor< int, double > Cijk_type
int main(int argc, char **argv)
void setDocString(const char doc_string[])
const int num_basis_types