Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_TotalOrderBasisImp.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_TimeMonitor.hpp"
12 
13 template <typename ordinal_type, typename value_type, typename ordering_type>
16  const Teuchos::Array< Teuchos::RCP<const OneDOrthogPolyBasis<ordinal_type, value_type> > >& bases_,
17  const value_type& sparse_tol_,
18  const ordering_type& coeff_compare) :
19  p(0),
20  d(bases_.size()),
21  sz(0),
22  bases(bases_),
23  sparse_tol(sparse_tol_),
24  max_orders(d),
25  basis_set(coeff_compare),
26  norms()
27 {
28 
29  // Compute largest order
30  for (ordinal_type i=0; i<d; i++) {
31  max_orders[i] = bases[i]->order();
32  if (max_orders[i] > p)
33  p = max_orders[i];
34  }
35 
36  // Compute basis terms
37  MultiIndex<ordinal_type> orders(d);
38  for (ordinal_type i=0; i<d; ++i)
39  orders[i] = bases[i]->order();
40  AnisotropicTotalOrderIndexSet<ordinal_type> index_set(p, orders);
41  ProductBasisUtils::buildProductBasis(index_set, basis_set, basis_map);
42  sz = basis_map.size();
43 
44  // Compute norms
45  norms.resize(sz);
46  value_type nrm;
47  for (ordinal_type k=0; k<sz; k++) {
48  nrm = value_type(1.0);
49  for (ordinal_type i=0; i<d; i++)
50  nrm = nrm * bases[i]->norm_squared(basis_map[k][i]);
51  norms[k] = nrm;
52  }
53 
54  // Create name
55  name = "Tensor product basis (";
56  for (ordinal_type i=0; i<d-1; i++)
57  name += bases[i]->getName() + ", ";
58  name += bases[d-1]->getName() + ")";
59 
60  // Allocate array for basis evaluation
61  basis_eval_tmp.resize(d);
62  for (ordinal_type j=0; j<d; j++)
63  basis_eval_tmp[j].resize(max_orders[j]+1);
64 }
65 
66 template <typename ordinal_type, typename value_type, typename ordering_type>
69 {
70 }
71 
72 template <typename ordinal_type, typename value_type, typename ordering_type>
75 order() const
76 {
77  return p;
78 }
79 
80 template <typename ordinal_type, typename value_type, typename ordering_type>
83 dimension() const
84 {
85  return d;
86 }
87 
88 template <typename ordinal_type, typename value_type, typename ordering_type>
91 size() const
92 {
93  return sz;
94 }
95 
96 template <typename ordinal_type, typename value_type, typename ordering_type>
99 norm_squared() const
100 {
101  return norms;
102 }
103 
104 template <typename ordinal_type, typename value_type, typename ordering_type>
105 const value_type&
108 {
109  return norms[i];
110 }
111 
112 template <typename ordinal_type, typename value_type, typename ordering_type>
116 {
117 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
118  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
119 #endif
120 
121  TotalOrderPredicate<ordinal_type> predicate(p, max_orders);
122 
123  return ProductBasisUtils::computeTripleProductTensor(
124  bases, basis_set, basis_map, predicate, predicate, sparse_tol);
125 }
126 
127 template <typename ordinal_type, typename value_type, typename ordering_type>
131 {
132 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
133  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
134 #endif
135 
136  TotalOrderPredicate<ordinal_type> predicate(p, max_orders);
137  TotalOrderPredicate<ordinal_type> k_predicate(1, max_orders);
138 
139  return ProductBasisUtils::computeTripleProductTensor(
140  bases, basis_set, basis_map, predicate, k_predicate, sparse_tol);
141 }
142 
143 template <typename ordinal_type, typename value_type, typename ordering_type>
147 {
148  // z = psi_{i_1}(0) * ... * psi_{i_d}(0) where i_1,...,i_d are the basis
149  // terms for coefficient i
150  value_type z = value_type(1.0);
151  for (ordinal_type j=0; j<d; j++)
152  z = z * bases[j]->evaluate(value_type(0.0), basis_map[i][j]);
153 
154  return z;
155 }
156 
157 template <typename ordinal_type, typename value_type, typename ordering_type>
158 void
161  Teuchos::Array<value_type>& basis_vals) const
162 {
163  for (ordinal_type j=0; j<d; j++)
164  bases[j]->evaluateBases(point[j], basis_eval_tmp[j]);
165 
166  // Only evaluate basis upto number of terms included in basis_pts
167  for (ordinal_type i=0; i<sz; i++) {
168  value_type t = value_type(1.0);
169  for (ordinal_type j=0; j<d; j++)
170  t *= basis_eval_tmp[j][basis_map[i][j]];
171  basis_vals[i] = t;
172  }
173 }
174 
175 template <typename ordinal_type, typename value_type, typename ordering_type>
176 void
178 print(std::ostream& os) const
179 {
180  os << "Tensor product basis of order " << p << ", dimension " << d
181  << ", and size " << sz << ". Component bases:\n";
182  for (ordinal_type i=0; i<d; i++)
183  os << *bases[i];
184  os << "Basis vector norms (squared):\n\t";
185  for (ordinal_type i=0; i<static_cast<ordinal_type>(norms.size()); i++)
186  os << norms[i] << " ";
187  os << "\n";
188 }
189 
190 template <typename ordinal_type, typename value_type, typename ordering_type>
194 {
195  return basis_map[i];
196 }
197 
198 template <typename ordinal_type, typename value_type, typename ordering_type>
201 index(const MultiIndex<ordinal_type>& term) const
202 {
203  typename coeff_set_type::const_iterator it = basis_set.find(term);
204  TEUCHOS_TEST_FOR_EXCEPTION(it == basis_set.end(), std::logic_error,
205  "Invalid term " << term);
206  return it->second;
207 }
208 
209 template <typename ordinal_type, typename value_type, typename ordering_type>
210 const std::string&
212 getName() const
213 {
214  return name;
215 }
216 
217 template <typename ordinal_type, typename value_type, typename ordering_type>
221 {
222  return bases;
223 }
224 
225 template <typename ordinal_type, typename value_type, typename ordering_type>
229 {
230  return max_orders;
231 }
virtual ordinal_type size() const
Return total size of basis.
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
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 MultiIndex< ordinal_type > & term(ordinal_type i) const
Get orders of each coordinate polynomial given an index i.
ordinal_type dimension() const
Return dimension of basis.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
static void buildProductBasis(const index_set_type &index_set, const growth_rule_type &growth_rule, basis_set_type &basis_set, basis_map_type &basis_map)
Generate a product basis from an index set.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
TotalOrderBasis(const Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > &bases, const value_type &sparse_tol=1.0e-12, const coeff_compare_type &coeff_compare=coeff_compare_type())
Constructor.
virtual value_type evaluateZero(ordinal_type i) const
Evaluate basis polynomial i at zero.
virtual MultiIndex< ordinal_type > getMaxOrders() const
Return maximum order allowable for each coordinate basis.
Predicate functor for building sparse triple products based on total order.
virtual ordinal_type index(const MultiIndex< ordinal_type > &term) const
Get index of the multivariate polynomial given orders of each coordinate.
ordinal_type order() const
Return order of basis.
virtual void print(std::ostream &os) const
Print basis to stream os.
virtual ~TotalOrderBasis()
Destructor.
virtual const Teuchos::Array< value_type > & norm_squared() const
Return array storing norm-squared of each basis polynomial.
Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > getCoordinateBases() const
Return coordinate bases.
virtual const std::string & getName() const
Return string name of basis.