Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_StieltjesBasisImp.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_TimeMonitor.hpp"
12 
13 template <typename ordinal_type, typename value_type, typename func_type>
16  ordinal_type p,
17  const Teuchos::RCP<const func_type>& func_,
19  bool use_pce_quad_points_,
20  bool normalize) :
21  RecurrenceBasis<ordinal_type, value_type>("Stieltjes PCE", p, normalize),
22  func(func_),
23  quad(quad_),
24  pce_weights(quad->getQuadWeights()),
25  basis_values(quad->getBasisAtQuadPoints()),
26  func_vals(),
27  phi_vals(),
28  use_pce_quad_points(use_pce_quad_points_)
29 {
30  // Setup rest of recurrence basis
31  this->setup();
32 }
33 
34 template <typename ordinal_type, typename value_type, typename func_type>
37 {
38 }
39 
40 template <typename ordinal_type, typename value_type, typename func_type>
41 void
44  Teuchos::Array<value_type>& quad_points,
45  Teuchos::Array<value_type>& quad_weights,
46  Teuchos::Array< Teuchos::Array<value_type> >& quad_values) const
47 {
48 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
49  TEUCHOS_FUNC_TIME_MONITOR("Stokhos::StieltjesBasis -- compute Gauss points");
50 #endif
51 
52  // Use underlying pce's quad points, weights, values
53  if (use_pce_quad_points) {
54  quad_points = func_vals;
55  quad_weights = pce_weights;
56  quad_values = phi_vals;
57  return;
58  }
59 
60  // Call base class
61  ordinal_type num_points =
62  static_cast<ordinal_type>(std::ceil((quad_order+1)/2.0));
63 
64  // We can't reliably generate quadrature points of order > 2*p
65  if (quad_order > 2*this->p)
66  quad_order = 2*this->p;
68  quad_points,
69  quad_weights,
70  quad_values);
71 
72  // Fill in the rest of the points with zero weight
73  if (quad_weights.size() < num_points) {
74  ordinal_type old_size = quad_weights.size();
75  quad_weights.resize(num_points);
76  quad_points.resize(num_points);
77  quad_values.resize(num_points);
78  for (ordinal_type i=old_size; i<num_points; i++) {
79  quad_weights[i] = value_type(0);
80  quad_points[i] = quad_points[0];
81  quad_values[i].resize(this->p+1);
82  this->evaluateBases(quad_points[i], quad_values[i]);
83  }
84  }
85 }
86 
87 template <typename ordinal_type, typename value_type, typename func_type>
88 bool
94  Teuchos::Array<value_type>& gamma) const
95 {
96  ordinal_type nqp = phi_vals.size();
99  for (ordinal_type i=0; i<nqp; i++)
100  vals[i].resize(n);
101  stieltjes(0, n, pce_weights, func_vals, alpha, beta, nrm, vals);
102  for (ordinal_type i=0; i<n; i++) {
103  delta[i] = value_type(1.0);
104  gamma[i] = value_type(1.0);
105  }
106 
107  // Save basis functions at quad point values
108  if (n == this->p+1)
109  phi_vals = vals;
110 
111  return false;
112 }
113 
114 template <typename ordinal_type, typename value_type, typename func_type>
115 void
118 {
119  // Evaluate func at quad points
120  const Teuchos::Array< Teuchos::Array<value_type> >& quad_points =
121  quad->getQuadPoints();
122  ordinal_type nqp = pce_weights.size();
123  func_vals.resize(nqp);
124  phi_vals.resize(nqp);
125  for (ordinal_type i=0; i<nqp; i++) {
126  func_vals[i] = (*func)(quad_points[i]);
127  phi_vals[i].resize(this->p+1);
128  }
129 
131 }
132 
133 template <typename ordinal_type, typename value_type, typename func_type>
134 void
137  ordinal_type nfinish,
138  const Teuchos::Array<value_type>& weights,
139  const Teuchos::Array<value_type>& points,
143  Teuchos::Array< Teuchos::Array<value_type> >& phi_vals) const
144 {
145 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
146  TEUCHOS_FUNC_TIME_MONITOR("Stokhos::StieltjesBasis -- Discretized Stieltjes Procedure");
147 #endif
148 
149  value_type val1, val2;
150  ordinal_type start = nstart;
151  if (nstart == 0) {
152  integrateBasisSquared(0, a, b, weights, points, phi_vals, val1, val2);
153  nrm[0] = val1;
154  a[0] = val2/val1;
155  b[0] = value_type(1);
156  start = 1;
157  }
158  for (ordinal_type i=start; i<nfinish; i++) {
159  integrateBasisSquared(i, a, b, weights, points, phi_vals, val1, val2);
160  // std::cout << "i = " << i << " val1 = " << val1 << " val2 = " << val2
161  // << std::endl;
162  TEUCHOS_TEST_FOR_EXCEPTION(val1 < 0.0, std::logic_error,
163  "Stokhos::StieltjesBasis::stieltjes(): "
164  << " Polynomial " << i << " out of " << nfinish
165  << " has norm " << val1
166  << "! Try increasing number of quadrature points");
167  nrm[i] = val1;
168  a[i] = val2/val1;
169  b[i] = nrm[i]/nrm[i-1];
170  }
171 }
172 
173 template <typename ordinal_type, typename value_type, typename func_type>
174 void
179  const Teuchos::Array<value_type>& weights,
180  const Teuchos::Array<value_type>& points,
182  value_type& val1, value_type& val2) const
183 {
184  evaluateRecurrence(k, a, b, points, phi_vals);
185  ordinal_type nqp = weights.size();
186  val1 = value_type(0);
187  val2 = value_type(0);
188  for (ordinal_type i=0; i<nqp; i++) {
189  val1 += weights[i]*phi_vals[i][k]*phi_vals[i][k];
190  val2 += weights[i]*phi_vals[i][k]*phi_vals[i][k]*points[i];
191  }
192 }
193 
194 template <typename ordinal_type, typename value_type, typename func_type>
195 void
200  const Teuchos::Array<value_type>& points,
202 {
203  ordinal_type np = points.size();
204  if (k == 0)
205  for (ordinal_type i=0; i<np; i++)
206  values[i][k] = 1.0;
207  else if (k == 1)
208  for (ordinal_type i=0; i<np; i++)
209  values[i][k] = points[i] - a[k-1];
210  else
211  for (ordinal_type i=0; i<np; i++)
212  values[i][k] =
213  (points[i] - a[k-1])*values[i][k-1] - b[k-1]*values[i][k-2];
214 }
215 
216 template <typename ordinal_type, typename value_type, typename func_type>
220 {
222 }
223 
224 template <typename ordinal_type, typename value_type, typename func_type>
228  func(basis.func),
229  quad(basis.quad),
230  pce_weights(quad->getQuadWeights()),
231  basis_values(quad->getBasisAtQuadPoints()),
232  func_vals(),
233  phi_vals(),
234  use_pce_quad_points(basis.use_pce_quad_points)
235 {
236  this->setup();
237 }
StieltjesBasis(ordinal_type p, const Teuchos::RCP< const func_type > &func, const Teuchos::RCP< const Stokhos::Quadrature< ordinal_type, value_type > > &quad, bool use_pce_quad_points, bool normalize=false)
Constructor.
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
Implementation of OneDOrthogPolyBasis based on the general three-term recurrence relationship: for ...
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.
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...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void stieltjes(ordinal_type nstart, ordinal_type nfinish, const Teuchos::Array< value_type > &weights, const Teuchos::Array< value_type > &points, Teuchos::Array< value_type > &a, Teuchos::Array< value_type > &b, Teuchos::Array< value_type > &nrm, Teuchos::Array< Teuchos::Array< value_type > > &phi_vals) const
Compute 3-term recurrence using Stieljtes procedure.
Generates three-term recurrence using the Discretized Stieltjes procedure applied to a functional map...
virtual void setup()
Setup basis after computing recurrence coefficients.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class for quadrature methods.
KOKKOS_INLINE_FUNCTION PCE< Storage > ceil(const PCE< Storage > &a)
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.
void resize(size_type new_size, const value_type &x=value_type())
void integrateBasisSquared(ordinal_type k, const Teuchos::Array< value_type > &a, const Teuchos::Array< value_type > &b, const Teuchos::Array< value_type > &weights, const Teuchos::Array< value_type > &points, Teuchos::Array< Teuchos::Array< value_type > > &phi_vals, value_type &val1, value_type &val2) const
Compute and .
virtual void setup()
Setup basis after computing recurrence coefficients.
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.
size_type size() const
void evaluateRecurrence(ordinal_type k, const Teuchos::Array< value_type > &a, const Teuchos::Array< value_type > &b, const Teuchos::Array< value_type > &points, Teuchos::Array< Teuchos::Array< value_type > > &values) const
Evaluate polynomials via 3-term recurrence.