Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SparseGridQuadratureImp.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 "TriKota_ConfigDefs.hpp"
11 #include "sandia_sgmg.hpp"
12 #include "sandia_rules.hpp"
13 #include "Teuchos_TimeMonitor.hpp"
14 
15 template <typename ordinal_type, typename value_type>
16 Stokhos::SparseGridQuadrature<ordinal_type, value_type>::
17 SparseGridQuadrature(
18  const Teuchos::RCP<const ProductBasis<ordinal_type,value_type> >& product_basis,
19  ordinal_type sparse_grid_level,
20  value_type duplicate_tol,
21  ordinal_type growth_rate) :
22  coordinate_bases(product_basis->getCoordinateBases())
23 {
24 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
25  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Sparse Grid Generation");
26 #endif
27 
28  ordinal_type d = product_basis->dimension();
29  ordinal_type p = product_basis->order();
30  ordinal_type sz = product_basis->size();
31  ordinal_type level = sparse_grid_level;
32 
33  // Make level = order the default, which is correct for Gaussian abscissas
34  // slow, linear growth, and total-order basis
35  if (level == 0) {
36  level = p;
37  }
38 
39  //std::cout << "Sparse grid level = " << level << std::endl;
40 
41  // Compute quad points, weights, values
43 
46  for (ordinal_type i=0; i<d; i++) {
47  compute1DPoints[i] = &(getMyPoints);
48  compute1DWeights[i] = &(getMyWeights);
49  growth_rules[i] = coordinate_bases[i]->getSparseGridGrowthRule();
50  }
51 
52  // Set the static sparse grid quadrature pointer to this
53  // (this will cause a conflict if another sparse grid quadrature object
54  // is trying to access the VPISparseGrid library, but that's all we can
55  // do at this point).
56  sgq = this;
57 
58  int num_total_pts =
59  webbur::sgmg_size_total(d, level, growth_rate, &growth_rules[0]);
60  ordinal_type ntot =
61  webbur::sgmg_size(d, level, &compute1DPoints[0], duplicate_tol,
62  growth_rate, &growth_rules[0]);
63  Teuchos::Array<int> sparse_order(ntot*d);
64  Teuchos::Array<int> sparse_index(ntot*d);
65  Teuchos::Array<int> sparse_unique_index(num_total_pts);
66  quad_points.resize(ntot);
67  quad_weights.resize(ntot);
68  quad_values.resize(ntot);
69  Teuchos::Array<value_type> gp(ntot*d);
70 
71  webbur::sgmg_unique_index(d, level, &compute1DPoints[0],
72  duplicate_tol, ntot, num_total_pts,
73  growth_rate, &growth_rules[0],
74  &sparse_unique_index[0]);
75  webbur::sgmg_index(d, level, ntot, num_total_pts,
76  &sparse_unique_index[0], growth_rate,
77  &growth_rules[0],
78  &sparse_order[0], &sparse_index[0]);
79  webbur::sgmg_weight(d, level, &compute1DWeights[0],
80  ntot, num_total_pts,
81  &sparse_unique_index[0], growth_rate,
82  &growth_rules[0],
83  &quad_weights[0]);
84  webbur::sgmg_point(d, level, &compute1DPoints[0],
85  ntot, &sparse_order[0],
86  &sparse_index[0], growth_rate,
87  &growth_rules[0],
88  &gp[0]);
89 
90  for (ordinal_type i=0; i<ntot; i++) {
91  quad_values[i].resize(sz);
92  quad_points[i].resize(d);
93  for (ordinal_type j=0; j<d; j++)
94  quad_points[i][j] = gp[i*d+j];
95  product_basis->evaluateBases(quad_points[i], quad_values[i]);
96  }
97 
98  //std::cout << "Number of quadrature points = " << ntot << std::endl;
99 }
100 
101 template <typename ordinal_type, typename value_type>
103 Stokhos::SparseGridQuadrature<ordinal_type, value_type>::
104 getQuadPoints() const
105 {
106  return quad_points;
107 }
108 
109 template <typename ordinal_type, typename value_type>
111 Stokhos::SparseGridQuadrature<ordinal_type, value_type>::
112 getQuadWeights() const
113 {
114  return quad_weights;
115 }
116 
117 template <typename ordinal_type, typename value_type>
119 Stokhos::SparseGridQuadrature<ordinal_type, value_type>::
120 getBasisAtQuadPoints() const
121 {
122  return quad_values;
123 }
124 
125 template <typename ordinal_type, typename value_type>
126 void
127 Stokhos::SparseGridQuadrature<ordinal_type,value_type>::
128 getMyPoints( int order, int dim, double x[] )
129 {
130  Teuchos::Array<double> quad_points;
131  Teuchos::Array<double> quad_weights;
133  ordinal_type p = sgq->coordinate_bases[dim]->quadDegreeOfExactness(order);
134  sgq->coordinate_bases[dim]->getQuadPoints(p, quad_points,
135  quad_weights, quad_values);
136  TEUCHOS_ASSERT(quad_points.size() == order);
137  for (int i = 0; i<quad_points.size(); i++) {
138  x[i] = quad_points[i];
139  }
140 }
141 
142 template <typename ordinal_type, typename value_type>
143 void
144 Stokhos::SparseGridQuadrature<ordinal_type,value_type>::
145 getMyWeights( int order, int dim, double w[] )
146 {
147  Teuchos::Array<double> quad_points;
148  Teuchos::Array<double> quad_weights;
150  ordinal_type p = sgq->coordinate_bases[dim]->quadDegreeOfExactness(order);
151  sgq->coordinate_bases[dim]->getQuadPoints(p, quad_points,
152  quad_weights, quad_values);
153  TEUCHOS_ASSERT(quad_points.size() == order);
154  for (int i = 0; i<quad_points.size(); i++) {
155  w[i] = quad_weights[i];
156  }
157 }
158 
159 template <typename ordinal_type, typename value_type>
160 std::ostream&
161 Stokhos::SparseGridQuadrature<ordinal_type,value_type>::
162 print(std::ostream& os) const
163 {
164  ordinal_type nqp = quad_weights.size();
165  os << "Sparse Grid Quadrature with " << nqp << " points:"
166  << std::endl << "Weight : Points" << std::endl;
167  for (ordinal_type i=0; i<nqp; i++) {
168  os << i << ": " << quad_weights[i] << " : ";
169  for (ordinal_type j=0; j<static_cast<ordinal_type>(quad_points[i].size());
170  j++)
171  os << quad_points[i][j] << " ";
172  os << std::endl;
173  }
174  os << "Basis values at quadrature points:" << std::endl;
175  for (ordinal_type i=0; i<nqp; i++) {
176  os << i << " " << ": ";
177  for (ordinal_type j=0; j<static_cast<ordinal_type>(quad_values[i].size());
178  j++)
179  os << quad_values[i][j] << " ";
180  os << std::endl;
181  }
182 
183  return os;
184 }
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
size_type size() const
#define TEUCHOS_ASSERT(assertion_test)