Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_BasisInteractionGraph.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 
11 
13 {}
14 
16  : vecLookup_(flm.vecLookup_), onlyUseLinear_(flm.onlyUseLinear_)
17 { }
18 
20  : onlyUseLinear_(onlyUseLinear)
21 {
22  using Teuchos::RCP;
23 
25  if(porder<0)
26  Cijk = max_basis.computeTripleProductTensor();
27  else
28  Cijk = max_basis.computeLinearTripleProductTensor();
29  initialize(max_basis,*Cijk,porder);
30 }
31 
33  const Stokhos::ProductBasis<int,double> & rowBasis,
34  const Stokhos::ProductBasis<int,double> & colBasis,bool onlyUseLinear,int porder)
35  : onlyUseLinear_(onlyUseLinear)
36 {
37  using Teuchos::RCP;
38 
40  if(porder<0)
41  Cijk = masterBasis.computeTripleProductTensor();
42  else
43  Cijk = masterBasis.computeLinearTripleProductTensor();
44  initialize(masterBasis,*Cijk,rowBasis,colBasis,porder);
45 }
46 
49  bool onlyUseLinear,int porder)
50  : onlyUseLinear_(onlyUseLinear)
51 {
52  initialize(max_basis,Cijk,porder);
53 }
54 
57  const Stokhos::ProductBasis<int,double> & rowBasis,
58  const Stokhos::ProductBasis<int,double> & colBasis,bool onlyUseLinear,int porder)
59  : onlyUseLinear_(onlyUseLinear)
60 {
61  using Teuchos::RCP;
62 
63  initialize(masterBasis,Cijk,rowBasis,colBasis,porder);
64 }
65 
68  int porder)
69 {
70  using Teuchos::RCP;
72 
73  // // max it out if defaulted
74  // if(porder<0)
75  // porder = max_basis.size();
76 
77  // RCP<Stokhos::Sparse3Tensor<int,double> > Cijk = max_basis.computeTripleProductTensor(porder);
78 
79  Cijk_type::k_iterator k_end = Cijk.k_end();
80  if (onlyUseLinear_) {
81  int dim = max_basis.dimension();
82  k_end = Cijk.find_k(dim+1);
83  }
84 
85  vecLookup_.resize(max_basis.size()); // defines number of rows
86  numCols_ = vecLookup_.size(); // set number of columns
87 
88  // Loop over Cijk entries including a non-zero in the graph at
89  // indices (i,j) if there is any k for which Cijk is non-zero
90  for(Cijk_type::k_iterator k_it=Cijk.k_begin(); k_it!=k_end; ++k_it) {
91  for(Cijk_type::kj_iterator j_it = Cijk.j_begin(k_it); j_it != Cijk.j_end(k_it); ++j_it) {
92  int j = index(j_it);
93  for(Cijk_type::kji_iterator i_it = Cijk.i_begin(j_it); i_it != Cijk.i_end(j_it); ++i_it) {
94  int i = index(i_it);
95  vecLookup_[i].push_back(j);
96  }
97  }
98  }
99 }
100 
103  const Stokhos::ProductBasis<int,double> & rowBasis,
104  const Stokhos::ProductBasis<int,double> & colBasis,int porder)
105 {
106  // for determining if their is an interaction or not
107  Stokhos::BasisInteractionGraph masterBig(masterBasis,Cijk,onlyUseLinear_,porder);
108 
109  vecLookup_.resize(rowBasis.size()); // defines number of rows
110 
111  // set number of columns
112  numCols_ = colBasis.size();
113 
114  // build row basis terms
115  std::vector<int> rowIndexToMasterIndex(rowBasis.size());
116  for(int i=0;i<rowBasis.size();i++)
117  rowIndexToMasterIndex[i] = masterBasis.index(rowBasis.term(i));
118 
119  // build column basis terms
120  std::vector<int> colIndexToMasterIndex(colBasis.size());
121  for(int i=0;i<colBasis.size();i++)
122  colIndexToMasterIndex[i] = masterBasis.index(colBasis.term(i));
123 
124  // build graph by looking up sparsity in master basis
125  for(int r=0;r<rowBasis.size();r++) {
126  int masterRow = rowIndexToMasterIndex[r];
127  for(int c=0;c<colBasis.size();c++) {
128  int masterCol = colIndexToMasterIndex[c];
129 
130  // is row and column active in master element
131  bool activeRC = masterBig(masterRow,masterCol);
132 
133  // if active add to local graph
134  if(activeRC)
135  vecLookup_[r].push_back(c);
136  }
137  }
138 }
139 
140 bool Stokhos::BasisInteractionGraph::operator()(std::size_t i,std::size_t j) const
141 {
142  const std::vector<std::size_t> & indices = activeIndices(i);
143  return indices.end() != std::find(indices.begin(),indices.end(),j);
144 }
145 
146 void Stokhos::BasisInteractionGraph::printGraph(std::ostream & os) const
147 {
148  for(std::size_t r=0;r<rowCount();r++) {
149  for(std::size_t c=0;c<colCount();c++)
150  if(operator()(r,c)) os << " * ";
151  else os << " ";
152  os << std::endl;
153  }
154 }
155 
157 {
158  std::size_t nnz = 0;
159  for(std::size_t r=0;r<rowCount();r++)
160  nnz += activeIndices(r).size();
161  return nnz;
162 }
k_iterator k_begin() const
Iterator pointing to first k entry.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const =0
Compute triple product tensor.
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
std::size_t numNonZeros() const
How many non zeros are in this graph.
virtual ordinal_type dimension() const =0
Return dimension of basis.
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
virtual ordinal_type index(const MultiIndex< ordinal_type > &term) const =0
Get index of the multivariate polynomial given orders of each coordinate.
virtual const MultiIndex< ordinal_type > & term(ordinal_type i) const =0
Get orders of each coordinate polynomial given an index i.
void initialize(const Stokhos::OrthogPolyBasis< int, double > &max_basis, const Stokhos::Sparse3Tensor< int, double > &Cijk, int porder=-1)
Setup the lookup graph.
bool operator()(std::size_t i, std::size_t j) const
Is there an entry for (i,j) in the graph.
k_iterator k_end() const
Iterator pointing to last k entry.
k_iterator find_k(ordinal_type k) const
Return k iterator for given index k.
Stokhos::Sparse3Tensor< int, double > Cijk_type
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const =0
Compute linear triple product tensor where k = 0,1.
kji_iterator i_begin(const kj_iterator &j) const
Iterator pointing to first i entry for given j and k.
kji_iterator i_end(const kj_iterator &j) const
Iterator pointing to last i entry for given j and k.
virtual ordinal_type size() const =0
Return total size of basis.