Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_LanczosProjPCEBasisImp.hpp
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 "Teuchos_Assert.hpp"
11 #include "Teuchos_BLAS.hpp"
12 #include "Teuchos_TimeMonitor.hpp"
13 
14 template <typename ordinal_type, typename value_type>
17  ordinal_type p,
20  bool normalize,
21  bool limit_integration_order_) :
22  RecurrenceBasis<ordinal_type, value_type>("Lanczos-proj PCE", p, normalize),
23  pce(pce_),
24  limit_integration_order(limit_integration_order_),
25  pce_sz(pce->basis()->size()),
26  Cijk_matrix(pce_sz,pce_sz),
27  weights(Teuchos::Copy,
28  const_cast<value_type*>(pce->basis()->norm_squared().getRawPtr()),
29  pce_sz),
30  u0(pce_sz),
31  lanczos_vecs(pce_sz, p+1),
32  new_pce(p+1)
33 {
34  u0[0] = value_type(1);
35 
36  pce_norms = pce->basis()->norm_squared();
37  for (ordinal_type i=0; i<pce_sz; i++) {
38  pce_norms[i] = std::sqrt(pce_norms[i]);
39  weights[i] = value_type(1);
40  }
41 
42  // Compute matrix -- For the matrix to be symmetric, the original basis
43  // must be normalized. However we don't want to require this, so we
44  // rescale the pce coefficients for a normalized basis
46  for (typename Cijk_type::k_iterator k_it = Cijk->k_begin();
47  k_it != Cijk->k_end(); ++k_it) {
48  ordinal_type k = index(k_it);
49  for (typename Cijk_type::kj_iterator j_it = Cijk->j_begin(k_it);
50  j_it != Cijk->j_end(k_it); ++j_it) {
51  ordinal_type j = index(j_it);
52  value_type val = 0;
53  for (typename Cijk_type::kji_iterator i_it = Cijk->i_begin(j_it);
54  i_it != Cijk->i_end(j_it); ++i_it) {
55  ordinal_type i = index(i_it);
56  value_type c = value(i_it);
57  val += (*pce)[i]*c / (pce_norms[j]*pce_norms[k]);
58  }
59  Cijk_matrix(k,j) = val;
60  }
61  }
62 
63  // Setup of rest of recurrence basis
64  this->setup();
65 
66 
67 }
68 
69 template <typename ordinal_type, typename value_type>
72 {
73 }
74 
75 template <typename ordinal_type, typename value_type>
76 void
79  Teuchos::Array<value_type>& quad_points,
80  Teuchos::Array<value_type>& quad_weights,
81  Teuchos::Array< Teuchos::Array<value_type> >& quad_values) const
82 {
83 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
84  TEUCHOS_FUNC_TIME_MONITOR("Stokhos::LanczosPCEBasis -- compute Gauss points");
85 #endif
86 
87  // Call base class
88  ordinal_type num_points =
89  static_cast<ordinal_type>(std::ceil((quad_order+1)/2.0));
90 
91  // We can't always reliably generate quadrature points of order > 2*p
92  // when using sparse grids for the underlying quadrature
93  if (limit_integration_order && quad_order > 2*this->p)
94  quad_order = 2*this->p;
96  quad_points,
97  quad_weights,
98  quad_values);
99 
100  // Fill in the rest of the points with zero weight
101  if (quad_weights.size() < num_points) {
102  ordinal_type old_size = quad_weights.size();
103  quad_weights.resize(num_points);
104  quad_points.resize(num_points);
105  quad_values.resize(num_points);
106  for (ordinal_type i=old_size; i<num_points; i++) {
107  quad_weights[i] = value_type(0);
108  quad_points[i] = quad_points[0];
109  quad_values[i].resize(this->p+1);
110  this->evaluateBases(quad_points[i], quad_values[i]);
111  }
112  }
113 }
114 
115 template <typename ordinal_type, typename value_type>
119 {
120  return
122  p,*this));
123 }
124 
125 template <typename ordinal_type, typename value_type>
129 {
130  return new_pce[i];
131 }
132 
133 template <typename ordinal_type, typename value_type>
134 void
137 {
138  // Transform coefficients to normalized basis
140  blas.GEMV(Teuchos::NO_TRANS, pce_sz, this->p+1,
141  value_type(1.0), lanczos_vecs.values(), pce_sz,
142  in, ordinal_type(1), value_type(0.0), out, ordinal_type(1));
143 
144  // Transform from normalized to original
145  for (ordinal_type i=0; i<pce_sz; i++)
146  out[i] /= pce_norms[i];
147 }
148 
149 template <typename ordinal_type, typename value_type>
150 bool
156  Teuchos::Array<value_type>& gamma) const
157 {
159  vectorspace_type vs(weights);
160  operator_type A(Cijk_matrix);
161 
162  // Create space to store lanczos vectors -- use lanczos_vecs if
163  // we are requesting p+1 vectors
165  if (n == this->p+1)
166  lv = Teuchos::rcp(&lanczos_vecs, false);
167  else
168  lv = Teuchos::rcp(new matrix_type(pce_sz,n));
169 
170  if (this->normalize)
171  lanczos_type::computeNormalized(n, vs, A, u0, *lv, alpha, beta, nrm);
172  else
173  lanczos_type::compute(n, vs, A, u0, *lv, alpha, beta, nrm);
174 
175  for (ordinal_type i=0; i<n; i++) {
176  delta[i] = value_type(1.0);
177  }
178  if (this->normalize)
179  gamma = beta;
180  else
181  for (ordinal_type i=0; i<n; i++)
182  gamma[i] = value_type(1.0);
183 
184  /*
185  matrix_type slv(pce_sz, n);
186  for (ordinal_type j=0; j<n; j++)
187  for (ordinal_type i=0; i<pce_sz; i++)
188  slv(i,j) = (*lv)(i,j) * weights[i];
189  matrix_type prod(n,n);
190  prod.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, *lv, slv, 0.0);
191  for (ordinal_type j=0; j<n; j++) {
192  for (ordinal_type i=0; i<n; i++)
193  prod(i,j) /= std::sqrt(nrm[i]*nrm[j]);
194  prod(j,j) -= 1.0;
195  }
196  std::cout << "orthogonalization error = " << prod.normInf() << std::endl;
197  */
198 
199  return this->normalize;
200 }
201 
202 template <typename ordinal_type, typename value_type>
203 void
206 {
208 
209  // Project original PCE into the new basis
210  vector_type u(pce_sz);
211  for (ordinal_type i=0; i<pce_sz; i++)
212  u[i] = (*pce)[i]*pce_norms[i];
213  new_pce.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, lanczos_vecs, u,
214  0.0);
215  for (ordinal_type i=0; i<=this->p; i++)
216  new_pce[i] /= this->norms[i];
217 }
218 
219 template <typename ordinal_type, typename value_type>
222  RecurrenceBasis<ordinal_type, value_type>("Lanczos-proj PCE", p, false),
223  pce(basis.pce),
224  limit_integration_order(basis.limit_integration_order),
225  pce_sz(basis.pce_sz),
226  pce_norms(basis.pce_norms),
227  Cijk_matrix(basis.Cijk_matrix),
228  weights(basis.weights),
229  u0(basis.u0),
230  lanczos_vecs(pce_sz, p+1),
231  new_pce()
232 {
233  this->setup();
234 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
value_type getNewCoeffs(ordinal_type i) const
Get new coefficients in this new basis.
matrix_type Cijk_matrix
Triple-product matrix used in generating lanczos vectors.
virtual Teuchos::RCP< OneDOrthogPolyBasis< ordinal_type, value_type > > cloneWithOrder(ordinal_type p) const
Clone this object with the option of building a higher order basis.
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
virtual void setup()
Setup basis after computing recurrence coefficients.
Implementation of OneDOrthogPolyBasis based on the general three-term recurrence relationship: for ...
Data structure storing a sparse 3-tensor C(i,j,k) in a a compressed format.
void GEMV(ETransp trans, const OrdinalType &m, const OrdinalType &n, const alpha_type alpha, const A_type *A, const OrdinalType &lda, const x_type *x, const OrdinalType &incx, const beta_type beta, ScalarType *y, const OrdinalType &incy) const
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
virtual void getQuadPoints(ordinal_type quad_order, Teuchos::Array< value_type > &points, Teuchos::Array< value_type > &weights, Teuchos::Array< Teuchos::Array< value_type > > &values) const
Compute quadrature points, weights, and values of basis polynomials at given set of points points...
RawPointerConversionTraits< Container >::Ptr_t getRawPtr(const Container &c)
Teuchos::RCP< const Stokhos::OrthogPolyApprox< ordinal_type, value_type > > pce
PCE Lanczos procedure is based on.
virtual bool computeRecurrenceCoefficients(ordinal_type n, Teuchos::Array< value_type > &alpha, Teuchos::Array< value_type > &beta, Teuchos::Array< value_type > &delta, Teuchos::Array< value_type > &gamma) const
Compute recurrence coefficients.
Teuchos::Array< value_type > pce_norms
Basis norms.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void transformCoeffsFromLanczos(const value_type *in, value_type *out) const
Map expansion coefficients from this basis to original.
vector_type u0
Initial Lanczos vector.
KOKKOS_INLINE_FUNCTION PCE< Storage > ceil(const PCE< Storage > &a)
vector_type weights
Weighting vector used in inner-products.
LanczosProjPCEBasis(ordinal_type p, const Teuchos::RCP< const Stokhos::OrthogPolyApprox< ordinal_type, value_type > > &pce, const Teuchos::RCP< const Stokhos::Sparse3Tensor< ordinal_type, value_type > > &Cijk, bool normalize, bool limit_integration_order=false)
Constructor.
virtual void getQuadPoints(ordinal_type quad_order, Teuchos::Array< value_type > &points, Teuchos::Array< value_type > &weights, Teuchos::Array< Teuchos::Array< value_type > > &values) const
Get Gauss quadrature points, weights, and values of basis at points.
void resize(size_type new_size, const value_type &x=value_type())
Stokhos::Sparse3Tensor< int, double > Cijk_type
virtual void setup()
Setup basis after computing recurrence coefficients.
expr val()
size_type size() const
ordinal_type pce_sz
Size of PC expansion.
Generates three-term recurrence using the Lanczos procedure applied to a polynomial chaos expansion i...