Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_MonomialProjGramSchmidtPCEBasis2Imp.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 
11 #include "Stokhos_BasisFactory.hpp"
15 
16 template <typename ordinal_type, typename value_type>
19  ordinal_type max_p,
22  const Teuchos::ParameterList& params_) :
23  name("Monomial Proj Gram Schmidt PCE Basis"),
24  params(params_),
25  pce_basis(pce[0].basis()),
26  pce_sz(pce_basis->size()),
27  p(max_p),
28  d(pce.size()),
29  verbose(params.get("Verbose", false)),
30  rank_threshold(params.get("Rank Threshold", 1.0e-12)),
31  orthogonalization_method(params.get("Orthogonalization Method",
32  "Householder"))
33 {
34  // Check for pce's that are constant and don't represent true random
35  // dimensions
37  for (ordinal_type i=0; i<pce.size(); i++) {
38  if (pce[i].standard_deviation() > 1.0e-15)
39  non_const_pce.push_back(pce[i]);
40  }
41  d = non_const_pce.size();
42 
43  // Build Q, Qp matrices
44  SDM A, F;
45  sz = buildQ(max_p, rank_threshold, non_const_pce, quad, terms, num_terms,
46  Qp, A, F);
47  Q.reshape(A.numRows(), sz);
48  ordinal_type ret =
50  TEUCHOS_ASSERT(ret == 0);
51 
52 //print_matlab(std::cout << "Qp = ", Qp);
53 
54  // Compute reduced quadrature rule
55  Teuchos::ParameterList quad_params = params.sublist("Reduced Quadrature");
57  quad_params);
58  SDM Q2;
59  if (quad_params.isParameter("Reduced Quadrature Method") &&
60  quad_params.get<std::string>("Reduced Quadrature Method") == "Q2") {
63  value_type rank_threshold2 = quad_params.get("Q2 Rank Threshold",
65  SDM Qp2, A2, F2;
66 
67  // Build basis, quadrature of order 2*max_p
71  if (2*max_p > pce_basis->order()) {
72 
73  // Basis
75  ordinal_type dim = prod_basis->dimension();
77  for (ordinal_type i=0; i<dim; i++)
78  bases[i] = prod_basis->getCoordinateBases()[i]->cloneWithOrder(2*max_p);
80  basis2 = cp_basis2;
81  //quad_params.sublist("Basis").set("Stochastic Galerkin Basis", basis2);
82  std::cout << "built new basis of dimension " << basis2->dimension()
83  << " and order " << basis2->order()
84  << " with total size " << basis2->size() << std::endl;
85 
86  // Quadrature
87  // quad_params.sublist("Quadrature").set("Quadrature Order", 2*max_p);
88  // quad2 =
89  // Stokhos::QuadratureFactory<ordinal_type,value_type>::create(quad_params);
90 
92  std::cout << "built new quadrature with total size " << quad2->size()
93  << std::endl;
94 
95  // Project pce to new basis
96  for (ordinal_type i=0; i<d; i++) {
97  pce2[i].reset(basis2); // this keeps lower order coeffs and sets
98  // higher order ones to 0
99  }
100  }
101 
102  // Build Q matrix of order 2*max_p
103  ordinal_type sz2 =
104  buildQ(2*max_p, rank_threshold2, pce2, quad2, terms2, num_terms2,
105  Qp2, A2, F2);
106  //print_matlab(std::cout << "Qp2 = ", Qp2);
107 
108  // Get quadrature data
109  const Teuchos::Array<value_type>& weights = quad->getQuadWeights();
111  quad->getQuadPoints();
112  ordinal_type nqp = weights.size();
113 
114  // Original basis at quadrature points -- needed to transform expansions
115  // in this basis back to original
116  ordinal_type pce_sz2 = basis2->size();
117  SDM AA(nqp, pce_sz2);
118  Teuchos::Array<value_type> basis_vals(pce_sz2);
119  for (ordinal_type i=0; i<nqp; i++) {
120  basis2->evaluateBases(points[i], basis_vals);
121  for (ordinal_type j=0; j<pce_sz2; j++)
122  AA(i,j) = basis_vals[j];
123  }
124  Q2.reshape(nqp, sz2);
125  ret = Q2.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, AA, Qp2, 0.0);
126  TEUCHOS_ASSERT(ret == 0);
127  reduced_quad = quad_factory.createReducedQuadrature(Q, Q2, F, weights);
128 
129  // // Get quadrature data
130  // const Teuchos::Array<value_type>& weights2 = quad2->getQuadWeights();
131  // ordinal_type nqp2 = weights2.size();
132  // Q2.reshape(nqp2, sz2);
133  // ret = Q2.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, A2, Qp2, 0.0);
134  // TEUCHOS_ASSERT(ret == 0);
135  // reduced_quad = quad_factory.createReducedQuadrature(Q, Q2, F2, weights2);
136  }
137  else {
138  const Teuchos::Array<value_type>& weights = quad->getQuadWeights();
139  reduced_quad = quad_factory.createReducedQuadrature(Q, Q2, F, weights);
140  }
141 
142  // Basis is orthonormal by construction
143  norms.resize(sz, 1.0);
144 }
145 
146 template <typename ordinal_type, typename value_type>
150  ordinal_type max_p,
151  value_type threshold,
155  Teuchos::Array<ordinal_type>& num_terms_,
156  SDM& Qp_, SDM& A_, SDM& F_)
157 {
159  ordinal_type pce_sz_ = pce_basis_->size();
160 
161  // Get quadrature data
162  const Teuchos::Array<value_type>& weights = quad->getQuadWeights();
164  quad->getQuadPoints();
165  const Teuchos::Array< Teuchos::Array<value_type> >& basis_values =
166  quad->getBasisAtQuadPoints();
167  ordinal_type nqp = weights.size();
168 
169  // Original basis at quadrature points -- needed to transform expansions
170  // in this basis back to original
171  A_.reshape(nqp, pce_sz_);
172  for (ordinal_type i=0; i<nqp; i++)
173  for (ordinal_type j=0; j<pce_sz_; j++)
174  A_(i,j) = basis_values[i][j];
175 
176  // Compute norms of each pce for rescaling
177  Teuchos::Array<value_type> pce_norms(d, 0.0);
178  for (ordinal_type j=0; j<d; j++) {
179  for (ordinal_type i=0; i<pce_sz_; i++)
180  pce_norms[j] += (pce[j])[i]*(pce[j])[i]*pce_basis_->norm_squared(i);
181  pce_norms[j] = std::sqrt(pce_norms[j]);
182  }
183 
184  // Compute F matrix -- PCEs evaluated at all quadrature points
185  // Since F is used in the reduced quadrature below as the quadrature points
186  // for this reduced basis, does scaling by the pce_norms mess up the points?
187  // No -- F essentially defines the random variables this basis is a function
188  // of, and thus they can be scaled in any way we want. Because we don't
189  // explicitly write the basis in terms of F, the scaling is implicit.
190  F_.reshape(nqp, d);
192  for (ordinal_type i=0; i<nqp; i++)
193  for (ordinal_type j=0; j<d; j++)
194  F_(i,j) = pce[j].evaluate(points[i], basis_values[i]);
195 
196  // Build the reduced basis
197  // Compute basis terms -- 2-D array giving powers for each linear index
198  ordinal_type max_sz;
199  CPBUtils::compute_terms(max_p, d, max_sz, terms_, num_terms_);
200 
201  // Compute B matrix -- monomials in F
202  // for i=0,...,nqp-1
203  // for j=0,...,sz-1
204  // B(i,j) = F(i,1)^terms_[j][1] * ... * F(i,d)^terms_[j][d]
205  // where sz is the total size of a basis up to order p and terms_[j]
206  // is an array of powers for each term in the total-order basis
207  SDM B(nqp, max_sz);
208  for (ordinal_type i=0; i<nqp; i++) {
209  for (ordinal_type j=0; j<max_sz; j++) {
210  B(i,j) = 1.0;
211  for (ordinal_type k=0; k<d; k++)
212  B(i,j) *= std::pow(F_(i,k), terms_[j][k]);
213  }
214  }
215 
216  // Project B into original basis -- should use SPAM for this
217  SDM Bp(pce_sz_, max_sz);
218  const Teuchos::Array<value_type>& basis_norms =
219  pce_basis_->norm_squared();
220  for (ordinal_type i=0; i<pce_sz_; i++) {
221  for (ordinal_type j=0; j<max_sz; j++) {
222  Bp(i,j) = 0.0;
223  for (ordinal_type k=0; k<nqp; k++)
224  Bp(i,j) += weights[k]*B(k,j)*A_(k,i);
225  Bp(i,j) /= basis_norms[i];
226  }
227  }
228 
229  // Rescale columns of Bp to have unit norm
230  for (ordinal_type j=0; j<max_sz; j++) {
231  value_type nrm = 0.0;
232  for (ordinal_type i=0; i<pce_sz_; i++)
233  nrm += Bp(i,j)*Bp(i,j)*basis_norms[i];
234  nrm = std::sqrt(nrm);
235  for (ordinal_type i=0; i<pce_sz_; i++)
236  Bp(i,j) /= nrm;
237  }
238 
239  // Compute our new basis -- each column of Qp is the coefficients of the
240  // new basis in the original basis. Constraint pivoting so first d+1
241  // columns and included in Qp.
242  Teuchos::Array<value_type> w(pce_sz_, 1.0);
243  SDM R;
244  Teuchos::Array<ordinal_type> piv(max_sz);
245  for (int i=0; i<d+1; i++)
246  piv[i] = 1;
248  ordinal_type sz_ = SOF::createOrthogonalBasis(
249  orthogonalization_method, threshold, verbose, Bp, w, Qp_, R, piv);
250 
251  return sz_;
252 }
253 
254 template <typename ordinal_type, typename value_type>
257 {
258 }
259 
260 template <typename ordinal_type, typename value_type>
263 order() const
264 {
265  return p;
266 }
267 
268 template <typename ordinal_type, typename value_type>
271 dimension() const
272 {
273  return d;
274 }
275 
276 template <typename ordinal_type, typename value_type>
279 size() const
280 {
281  return sz;
282 }
283 
284 template <typename ordinal_type, typename value_type>
288 {
289  return norms;
290 }
291 
292 template <typename ordinal_type, typename value_type>
293 const value_type&
296 {
297  return norms[i];
298 }
299 
300 template <typename ordinal_type, typename value_type>
304 
305 {
306  return Teuchos::null;
307 }
308 
309 template <typename ordinal_type, typename value_type>
313 
314 {
315  return Teuchos::null;
316 }
317 
318 template <typename ordinal_type, typename value_type>
322 {
323  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Not implemented!");
324 }
325 
326 template <typename ordinal_type, typename value_type>
327 void
330  Teuchos::Array<value_type>& basis_vals) const
331 {
332  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Not implemented!");
333 }
334 
335 template <typename ordinal_type, typename value_type>
336 const std::string&
338 getName() const
339 {
340  return name;
341 }
342 
343 template <typename ordinal_type, typename value_type>
344 void
346 print(std::ostream& os) const
347 {
348  os << "Gram-Schmidt basis of order " << p << ", dimension " << d
349  << ", and size " << sz << ". Matrix coefficients:\n";
350  os << printMat(Qp) << std::endl;
351  os << "Basis vector norms (squared):\n\t";
352  for (ordinal_type i=0; i<sz; i++)
353  os << norms[i] << " ";
354  os << "\n";
355 }
356 
357 template <typename ordinal_type, typename value_type>
358 void
361  ordinal_type ncol, bool transpose) const
362 {
363  if (transpose) {
364  SDM zbar(Teuchos::View, const_cast<value_type*>(in), ncol, ncol, sz);
365  SDM z(Teuchos::View, out, ncol, ncol, pce_sz);
366  ordinal_type ret =
367  z.multiply(Teuchos::NO_TRANS, Teuchos::TRANS, 1.0, zbar, Qp, 0.0);
368  TEUCHOS_ASSERT(ret == 0);
369  }
370  else {
371  SDM zbar(Teuchos::View, const_cast<value_type*>(in), sz, sz, ncol);
372  SDM z(Teuchos::View, out, pce_sz, pce_sz, ncol);
373  ordinal_type ret =
374  z.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Qp, zbar, 0.0);
375  TEUCHOS_ASSERT(ret == 0);
376  }
377 }
378 
379 template <typename ordinal_type, typename value_type>
380 void
383  ordinal_type ncol, bool transpose) const
384 {
385  if (transpose) {
386  SDM z(Teuchos::View, const_cast<value_type*>(in), ncol, ncol, pce_sz);
387  SDM zbar(Teuchos::View, out, ncol, ncol, sz);
388  ordinal_type ret =
389  zbar.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, z, Qp, 0.0);
390  TEUCHOS_ASSERT(ret == 0);
391  }
392  else {
393  SDM z(Teuchos::View, const_cast<value_type*>(in), pce_sz, pce_sz, ncol);
394  SDM zbar(Teuchos::View, out, sz, sz, ncol);
395  ordinal_type ret =
396  zbar.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, Qp, z, 0.0);
397  TEUCHOS_ASSERT(ret == 0);
398  }
399 }
400 
401 template <typename ordinal_type, typename value_type>
405 {
406  return reduced_quad;
407 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
virtual void evaluateBases(const Teuchos::ArrayView< const value_type > &point, Teuchos::Array< value_type > &basis_vals) const
Evaluate basis polynomials at given point point.
virtual Teuchos::RCP< const Stokhos::UserDefinedQuadrature< ordinal_type, value_type > > createReducedQuadrature(const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Q2, const Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F, const Teuchos::Array< value_type > &weights) const
Get reduced quadrature object.
virtual const std::string & getName() const
Return string name of basis.
Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > reduced_quad
Reduced quadrature object.
virtual Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > getReducedQuadrature() const
Get reduced quadrature object.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
MonomialProjGramSchmidtPCEBasis2(ordinal_type p, const Teuchos::Array< Stokhos::OrthogPolyApprox< ordinal_type, value_type > > &pce, const Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > &quad, const Teuchos::ParameterList &params=Teuchos::ParameterList())
Constructor.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
T & get(ParameterList &l, const std::string &name)
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ordinal_type buildQ(ordinal_type max_p, value_type threshold, const Teuchos::Array< Stokhos::OrthogPolyApprox< ordinal_type, value_type > > &pce, const Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > &quad, Teuchos::Array< Stokhos::MultiIndex< ordinal_type > > &terms_, Teuchos::Array< ordinal_type > &num_terms_, Teuchos::SerialDenseMatrix< ordinal_type, value_type > &Qp_, Teuchos::SerialDenseMatrix< ordinal_type, value_type > &A_, Teuchos::SerialDenseMatrix< ordinal_type, value_type > &F_)
Build the reduced basis, parameterized by total order max_p.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
int quad2(const Epetra_Map &map, bool verbose)
bool isParameter(const std::string &name) const
SDM Q
Values of transformed basis at quadrature points.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual value_type evaluateZero(ordinal_type i) const
Evaluate basis polynomial i at zero.
Abstract base class for quadrature methods.
virtual void transformToOriginalBasis(const value_type *in, value_type *out, ordinal_type ncol=1, bool transpose=false) const
Transform coefficients to original basis from this basis.
Abstract base class for multivariate orthogonal polynomials generated from tensor products of univari...
void resize(size_type new_size, const value_type &x=value_type())
Multivariate orthogonal polynomial basis generated from a total-order complete-polynomial tensor prod...
Generate a basis from a given set of PCE expansions that is orthogonal with respect to the product me...
void push_back(const value_type &x)
int reshape(OrdinalType numRows, OrdinalType numCols)
virtual const Teuchos::Array< value_type > & norm_squared() const
Return array storing norm-squared of each basis polynomial.
size_type size() const
ordinal_type dimension() const
Return dimension of basis.
SDM Qp
Coefficients of transformed basis in original basis.
virtual ordinal_type size() const
Return total size of basis.
Teuchos::Array< Stokhos::MultiIndex< ordinal_type > > terms
2-D array of basis terms
#define TEUCHOS_ASSERT(assertion_test)
virtual void transformFromOriginalBasis(const value_type *in, value_type *out, ordinal_type ncol=1, bool transpose=false) const
Transform coefficients from original basis to this basis.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< ordinal_type, value_type > > pce_basis
Original pce basis.
virtual void print(std::ostream &os) const
Print basis to stream os.
Teuchos::Array< ordinal_type > num_terms
Number of terms up to each order.
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...
OrdinalType numRows() const
Encapsulate various orthogonalization (ie QR) methods.