Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_TensorProductBasisImp.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_,
19  const ordering_type& coeff_compare) :
20  p(0),
21  d(bases_.size()),
22  sz(0),
23  bases(bases_),
24  sparse_tol(sparse_tol_),
25  max_orders(d),
26  basis_set(coeff_compare),
27  norms()
28 {
29  // Resize bases for given index if necessary
30  if (index.dimension() > 0) {
31  for (ordinal_type i=0; i<d; i++) {
32  if (index[i] != bases[i]->order())
33  bases[i] = bases[i]->cloneWithOrder(index[i]);
34  }
35  }
36 
37  // Compute largest order
38  for (ordinal_type i=0; i<d; i++) {
39  max_orders[i] = bases[i]->order();
40  if (max_orders[i] > p)
41  p = max_orders[i];
42  }
43 
44  // Compute basis terms
45  MultiIndex<ordinal_type> orders(d);
46  for (ordinal_type i=0; i<d; ++i)
47  orders[i] = bases[i]->order();
48  TensorProductIndexSet<ordinal_type> index_set(orders);
50  sz = basis_map.size();
51 
52  // Compute norms
53  norms.resize(sz);
54  value_type nrm;
55  for (ordinal_type k=0; k<sz; k++) {
56  nrm = value_type(1.0);
57  for (ordinal_type i=0; i<d; i++)
58  nrm = nrm * bases[i]->norm_squared(basis_map[k][i]);
59  norms[k] = nrm;
60  }
61 
62  // Create name
63  name = "Tensor product basis (";
64  for (ordinal_type i=0; i<d-1; i++)
65  name += bases[i]->getName() + ", ";
66  name += bases[d-1]->getName() + ")";
67 
68  // Allocate array for basis evaluation
69  basis_eval_tmp.resize(d);
70  for (ordinal_type j=0; j<d; j++)
71  basis_eval_tmp[j].resize(max_orders[j]+1);
72 }
73 
74 template <typename ordinal_type, typename value_type, typename ordering_type>
77 {
78 }
79 
80 template <typename ordinal_type, typename value_type, typename ordering_type>
83 order() const
84 {
85  return p;
86 }
87 
88 template <typename ordinal_type, typename value_type, typename ordering_type>
91 dimension() const
92 {
93  return d;
94 }
95 
96 template <typename ordinal_type, typename value_type, typename ordering_type>
99 size() const
100 {
101  return sz;
102 }
103 
104 template <typename ordinal_type, typename value_type, typename ordering_type>
108 {
109  return norms;
110 }
111 
112 template <typename ordinal_type, typename value_type, typename ordering_type>
113 const value_type&
116 {
117  return norms[i];
118 }
119 
120 template <typename ordinal_type, typename value_type, typename ordering_type>
124 {
125 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
126  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
127 #endif
128 
129  TensorProductPredicate<ordinal_type> predicate(max_orders);
130 
132  bases, basis_set, basis_map, predicate, predicate, sparse_tol);
133 }
134 
135 template <typename ordinal_type, typename value_type, typename ordering_type>
139 {
140 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
141  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
142 #endif
143 
144  TensorProductPredicate<ordinal_type> predicate(max_orders);
145  TotalOrderPredicate<ordinal_type> k_predicate(1, max_orders);
146 
148  bases, basis_set, basis_map, predicate, k_predicate, sparse_tol);
149 }
150 
151 template <typename ordinal_type, typename value_type, typename ordering_type>
155 {
156  // z = psi_{i_1}(0) * ... * psi_{i_d}(0) where i_1,...,i_d are the basis
157  // terms for coefficient i
158  value_type z = value_type(1.0);
159  for (ordinal_type j=0; j<d; j++)
160  z = z * bases[j]->evaluate(value_type(0.0), basis_map[i][j]);
161 
162  return z;
163 }
164 
165 template <typename ordinal_type, typename value_type, typename ordering_type>
166 void
169  Teuchos::Array<value_type>& basis_vals) const
170 {
171  for (ordinal_type j=0; j<d; j++)
172  bases[j]->evaluateBases(point[j], basis_eval_tmp[j]);
173 
174  // Only evaluate basis upto number of terms included in basis_pts
175  for (ordinal_type i=0; i<sz; i++) {
176  value_type t = value_type(1.0);
177  for (ordinal_type j=0; j<d; j++)
178  t *= basis_eval_tmp[j][basis_map[i][j]];
179  basis_vals[i] = t;
180  }
181 }
182 
183 template <typename ordinal_type, typename value_type, typename ordering_type>
184 void
186 print(std::ostream& os) const
187 {
188  os << "Tensor product basis of order " << p << ", dimension " << d
189  << ", and size " << sz << ". Component bases:\n";
190  for (ordinal_type i=0; i<d; i++)
191  os << *bases[i];
192  os << "Basis vector norms (squared):\n\t";
193  for (ordinal_type i=0; i<static_cast<ordinal_type>(norms.size()); i++)
194  os << norms[i] << " ";
195  os << "\n";
196 }
197 
198 template <typename ordinal_type, typename value_type, typename ordering_type>
202 {
203  return basis_map[i];
204 }
205 
206 template <typename ordinal_type, typename value_type, typename ordering_type>
209 index(const MultiIndex<ordinal_type>& term) const
210 {
211  typename coeff_set_type::const_iterator it = basis_set.find(term);
212  TEUCHOS_TEST_FOR_EXCEPTION(it == basis_set.end(), std::logic_error,
213  "Invalid term " << term);
214  return it->second;
215 }
216 
217 template <typename ordinal_type, typename value_type, typename ordering_type>
218 const std::string&
220 getName() const
221 {
222  return name;
223 }
224 
225 template <typename ordinal_type, typename value_type, typename ordering_type>
229 {
230  return bases;
231 }
232 
233 template <typename ordinal_type, typename value_type, typename ordering_type>
237 {
238  return max_orders;
239 }
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeLinearTripleProductTensor() const
Compute linear triple product tensor where k = 0,1,..,d.
ordinal_type order() const
Return order of basis.
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
coeff_type max_orders
Maximum orders for each dimension.
virtual value_type evaluateZero(ordinal_type i) const
Evaluate basis polynomial i at zero.
Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > getCoordinateBases() const
Return coordinate bases.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::Array< Teuchos::Array< value_type > > basis_eval_tmp
Temporary array used in basis evaluation.
ordinal_type dimension() const
Dimension.
static Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor(const Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > &bases, const basis_set_type &basis_set, const basis_map_type &basis_map, const coeff_predicate_type &coeff_pred, const k_coeff_predicate_type &k_coeff_pred, const value_type sparse_tol=1.0e-12)
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 const MultiIndex< ordinal_type > & term(ordinal_type i) const
Get orders of each coordinate polynomial given an index i.
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
virtual const std::string & getName() const
Return string name of basis.
ordinal_type sz
Total size of basis.
Teuchos::Array< value_type > norms
Norms.
virtual const Teuchos::Array< value_type > & norm_squared() const
Return array storing norm-squared of each basis polynomial.
void resize(size_type new_size, const value_type &x=value_type())
virtual void evaluateBases(const Teuchos::ArrayView< const value_type > &point, Teuchos::Array< value_type > &basis_vals) const
Evaluate basis polynomials at given point point.
Predicate functor for building sparse triple products based on total order.
ordinal_type dimension() const
Return dimension of basis.
Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > bases
Array of bases.
ordinal_type d
Total dimension of basis.
virtual ordinal_type size() const
Return total size of basis.
size_type size() const
ordinal_type p
Total order of basis.
TensorProductBasis(const Teuchos::Array< Teuchos::RCP< const OneDOrthogPolyBasis< ordinal_type, value_type > > > &bases, const value_type &sparse_tol=1.0e-12, const MultiIndex< ordinal_type > &index=MultiIndex< ordinal_type >(), const coeff_compare_type &coeff_compare=coeff_compare_type())
Constructor.
virtual void print(std::ostream &os) const
Print basis to stream os.
Predicate functor for building sparse triple products.
virtual ordinal_type index(const MultiIndex< ordinal_type > &term) const
Get index of the multivariate polynomial given orders of each coordinate.
virtual MultiIndex< ordinal_type > getMaxOrders() const
Return maximum order allowable for each coordinate basis.