Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_GSReducedPCEBasisBaseImp.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 
12 template <typename ordinal_type, typename value_type>
15  ordinal_type max_p,
18  const Teuchos::ParameterList& params_) :
19  params(params_),
20  pce_basis(pce[0].basis()),
21  pce_sz(pce_basis->size()),
22  p(max_p),
23  d(pce.size()),
24  verbose(params.get("Verbose", false)),
25  rank_threshold(params.get("Rank Threshold", 1.0e-12)),
26  orthogonalization_method(params.get("Orthogonalization Method",
27  "Householder"))
28 {
29 }
30 
31 template <typename ordinal_type, typename value_type>
32 void
35  ordinal_type max_p,
38 {
39  // Check for pce's that are constant and don't represent true random
40  // dimensions
42  for (ordinal_type i=0; i<pce.size(); i++) {
43  if (pce[i].standard_deviation() > 1.0e-15)
44  pce2.push_back(&pce[i]);
45  }
46  d = pce2.size();
47 
48  // Get quadrature data
49  const Teuchos::Array<value_type>& weights = quad->getQuadWeights();
51  quad->getQuadPoints();
52  const Teuchos::Array< Teuchos::Array<value_type> >& basis_values =
53  quad->getBasisAtQuadPoints();
54  ordinal_type nqp = weights.size();
55 
56  // Original basis at quadrature points -- needed to transform expansions
57  // in this basis back to original
58  SDM A(nqp, pce_sz);
59  for (ordinal_type i=0; i<nqp; i++)
60  for (ordinal_type j=0; j<pce_sz; j++)
61  A(i,j) = basis_values[i][j];
62 
63  // Compute norms of each pce for rescaling
64  Teuchos::Array<value_type> pce_norms(d, 0.0);
65  for (ordinal_type j=0; j<d; j++) {
66  for (ordinal_type i=0; i<pce_sz; i++)
67  pce_norms[j] += (*pce2[j])[i]*(*pce2[j])[i]*pce_basis->norm_squared(i);
68  pce_norms[j] = std::sqrt(pce_norms[j]);
69  }
70 
71  // Compute F matrix -- PCEs evaluated at all quadrature points
72  // Since F is used in the reduced quadrature below as the quadrature points
73  // for this reduced basis, does scaling by the pce_norms mess up the points?
74  // No -- F essentially defines the random variables this basis is a function
75  // of, and thus they can be scaled in any way we want. Because we don't
76  // explicitly write the basis in terms of F, the scaling is implicit.
77  SDM F(nqp, d);
79  for (ordinal_type i=0; i<nqp; i++)
80  for (ordinal_type j=0; j<d; j++)
81  F(i,j) = pce2[j]->evaluate(points[i], basis_values[i]);
82 
83  // Build the reduced basis
84  sz = buildReducedBasis(max_p, rank_threshold, A, F, weights, terms, num_terms,
85  Qp, Q);
86 
87  // Compute reduced quadrature rule
88  Teuchos::ParameterList quad_params = params.sublist("Reduced Quadrature");
90  quad_params);
91  SDM Q2;
92  if (quad_params.isParameter("Reduced Quadrature Method") &&
93  quad_params.get<std::string>("Reduced Quadrature Method") == "Q2") {
96  value_type rank_threshold2 = quad_params.get("Q2 Rank Threshold",
97  rank_threshold);
98  SDM Qp2;
99  //ordinal_type sz2 =
100  buildReducedBasis(2*max_p, rank_threshold2, A, F, weights, terms2,
101  num_terms2, Qp2, Q2);
102  }
103  reduced_quad = quad_factory.createReducedQuadrature(Q, Q2, F, weights);
104 
105  // Basis is orthonormal by construction
106  norms.resize(sz, 1.0);
107 }
108 
109 template <typename ordinal_type, typename value_type>
112 {
113 }
114 
115 template <typename ordinal_type, typename value_type>
118 order() const
119 {
120  return p;
121 }
122 
123 template <typename ordinal_type, typename value_type>
126 dimension() const
127 {
128  return d;
129 }
130 
131 template <typename ordinal_type, typename value_type>
134 size() const
135 {
136  return sz;
137 }
138 
139 template <typename ordinal_type, typename value_type>
143 {
144  return norms;
145 }
146 
147 template <typename ordinal_type, typename value_type>
148 const value_type&
151 {
152  return norms[i];
153 }
154 
155 template <typename ordinal_type, typename value_type>
159 
160 {
161  return Teuchos::null;
162 }
163 
164 template <typename ordinal_type, typename value_type>
168 
169 {
170  return Teuchos::null;
171 }
172 
173 template <typename ordinal_type, typename value_type>
177 {
178  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Not implemented!");
179 }
180 
181 template <typename ordinal_type, typename value_type>
182 void
185  Teuchos::Array<value_type>& basis_vals) const
186 {
187  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Not implemented!");
188 }
189 
190 template <typename ordinal_type, typename value_type>
191 void
193 print(std::ostream& os) const
194 {
195  os << "Gram-Schmidt basis of order " << p << ", dimension " << d
196  << ", and size " << sz << ". Matrix coefficients:\n";
197  os << printMat(Qp) << std::endl;
198  os << "Basis vector norms (squared):\n\t";
199  for (ordinal_type i=0; i<sz; i++)
200  os << norms[i] << " ";
201  os << "\n";
202 }
203 
204 template <typename ordinal_type, typename value_type>
205 void
208  ordinal_type ncol, bool transpose) const
209 {
210  if (transpose) {
211  SDM zbar(Teuchos::View, const_cast<value_type*>(in), ncol, ncol, sz);
212  SDM z(Teuchos::View, out, ncol, ncol, pce_sz);
213  ordinal_type ret =
214  z.multiply(Teuchos::NO_TRANS, Teuchos::TRANS, 1.0, zbar, Qp, 0.0);
215  TEUCHOS_ASSERT(ret == 0);
216  }
217  else {
218  SDM zbar(Teuchos::View, const_cast<value_type*>(in), sz, sz, ncol);
219  SDM z(Teuchos::View, out, pce_sz, pce_sz, ncol);
220  ordinal_type ret =
221  z.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Qp, zbar, 0.0);
222  TEUCHOS_ASSERT(ret == 0);
223  }
224 }
225 
226 template <typename ordinal_type, typename value_type>
227 void
230  ordinal_type ncol, bool transpose) const
231 {
232  if (transpose) {
233  SDM z(Teuchos::View, const_cast<value_type*>(in), ncol, ncol, pce_sz);
234  SDM zbar(Teuchos::View, out, ncol, ncol, sz);
235  ordinal_type ret =
236  zbar.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, z, Qp, 0.0);
237  TEUCHOS_ASSERT(ret == 0);
238  }
239  else {
240  SDM z(Teuchos::View, const_cast<value_type*>(in), pce_sz, pce_sz, ncol);
241  SDM zbar(Teuchos::View, out, sz, sz, ncol);
242  ordinal_type ret =
243  zbar.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, Qp, z, 0.0);
244  TEUCHOS_ASSERT(ret == 0);
245  }
246 }
247 
248 template <typename ordinal_type, typename value_type>
252 {
253  return reduced_quad;
254 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
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 Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
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="")
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 const Teuchos::Array< value_type > & norm_squared() const
Return array storing norm-squared of each basis polynomial.
bool isParameter(const std::string &name) const
void setup(ordinal_type p, const Teuchos::Array< Stokhos::OrthogPolyApprox< ordinal_type, value_type > > &pce, const Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > &quad)
virtual void print(std::ostream &os) const
Print basis to stream os.
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.
GSReducedPCEBasisBase(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.
virtual ordinal_type size() const
Return total size of basis.
virtual value_type evaluateZero(ordinal_type i) const
Evaluate basis polynomial i at zero.
Generate a basis from a given set of PCE expansions that is orthogonal with respect to the product me...
ordinal_type order() const
Return order of basis.
void push_back(const value_type &x)
size_type size() const
ordinal_type dimension() const
Return dimension of basis.
#define TEUCHOS_ASSERT(assertion_test)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
virtual Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > getReducedQuadrature() const
Get reduced quadrature object.
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.