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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 #include "Teuchos_TimeMonitor.hpp"
43 
44 template <typename ordinal_type, typename value_type, typename ordering_type>
47  const Teuchos::Array< Teuchos::RCP<const OneDOrthogPolyBasis<ordinal_type, value_type> > >& bases_,
48  const value_type& sparse_tol_,
50  const ordering_type& coeff_compare) :
51  p(0),
52  d(bases_.size()),
53  sz(0),
54  bases(bases_),
55  sparse_tol(sparse_tol_),
56  max_orders(d),
57  basis_set(coeff_compare),
58  norms()
59 {
60  // Resize bases for given index if necessary
61  if (index.dimension() > 0) {
62  for (ordinal_type i=0; i<d; i++) {
63  if (index[i] != bases[i]->order())
64  bases[i] = bases[i]->cloneWithOrder(index[i]);
65  }
66  }
67 
68  // Compute largest order
69  for (ordinal_type i=0; i<d; i++) {
70  max_orders[i] = bases[i]->order();
71  if (max_orders[i] > p)
72  p = max_orders[i];
73  }
74 
75  // Compute basis terms
76  MultiIndex<ordinal_type> orders(d);
77  for (ordinal_type i=0; i<d; ++i)
78  orders[i] = bases[i]->order();
79  TensorProductIndexSet<ordinal_type> index_set(orders);
81  sz = basis_map.size();
82 
83  // Compute norms
84  norms.resize(sz);
85  value_type nrm;
86  for (ordinal_type k=0; k<sz; k++) {
87  nrm = value_type(1.0);
88  for (ordinal_type i=0; i<d; i++)
89  nrm = nrm * bases[i]->norm_squared(basis_map[k][i]);
90  norms[k] = nrm;
91  }
92 
93  // Create name
94  name = "Tensor product basis (";
95  for (ordinal_type i=0; i<d-1; i++)
96  name += bases[i]->getName() + ", ";
97  name += bases[d-1]->getName() + ")";
98 
99  // Allocate array for basis evaluation
100  basis_eval_tmp.resize(d);
101  for (ordinal_type j=0; j<d; j++)
102  basis_eval_tmp[j].resize(max_orders[j]+1);
103 }
104 
105 template <typename ordinal_type, typename value_type, typename ordering_type>
108 {
109 }
110 
111 template <typename ordinal_type, typename value_type, typename ordering_type>
114 order() const
115 {
116  return p;
117 }
118 
119 template <typename ordinal_type, typename value_type, typename ordering_type>
122 dimension() const
123 {
124  return d;
125 }
126 
127 template <typename ordinal_type, typename value_type, typename ordering_type>
130 size() const
131 {
132  return sz;
133 }
134 
135 template <typename ordinal_type, typename value_type, typename ordering_type>
139 {
140  return norms;
141 }
142 
143 template <typename ordinal_type, typename value_type, typename ordering_type>
144 const value_type&
147 {
148  return norms[i];
149 }
150 
151 template <typename ordinal_type, typename value_type, typename ordering_type>
155 {
156 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
157  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
158 #endif
159 
160  TensorProductPredicate<ordinal_type> predicate(max_orders);
161 
163  bases, basis_set, basis_map, predicate, predicate, sparse_tol);
164 }
165 
166 template <typename ordinal_type, typename value_type, typename ordering_type>
170 {
171 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
172  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
173 #endif
174 
175  TensorProductPredicate<ordinal_type> predicate(max_orders);
176  TotalOrderPredicate<ordinal_type> k_predicate(1, max_orders);
177 
179  bases, basis_set, basis_map, predicate, k_predicate, sparse_tol);
180 }
181 
182 template <typename ordinal_type, typename value_type, typename ordering_type>
186 {
187  // z = psi_{i_1}(0) * ... * psi_{i_d}(0) where i_1,...,i_d are the basis
188  // terms for coefficient i
189  value_type z = value_type(1.0);
190  for (ordinal_type j=0; j<d; j++)
191  z = z * bases[j]->evaluate(value_type(0.0), basis_map[i][j]);
192 
193  return z;
194 }
195 
196 template <typename ordinal_type, typename value_type, typename ordering_type>
197 void
200  Teuchos::Array<value_type>& basis_vals) const
201 {
202  for (ordinal_type j=0; j<d; j++)
203  bases[j]->evaluateBases(point[j], basis_eval_tmp[j]);
204 
205  // Only evaluate basis upto number of terms included in basis_pts
206  for (ordinal_type i=0; i<sz; i++) {
207  value_type t = value_type(1.0);
208  for (ordinal_type j=0; j<d; j++)
209  t *= basis_eval_tmp[j][basis_map[i][j]];
210  basis_vals[i] = t;
211  }
212 }
213 
214 template <typename ordinal_type, typename value_type, typename ordering_type>
215 void
217 print(std::ostream& os) const
218 {
219  os << "Tensor product basis of order " << p << ", dimension " << d
220  << ", and size " << sz << ". Component bases:\n";
221  for (ordinal_type i=0; i<d; i++)
222  os << *bases[i];
223  os << "Basis vector norms (squared):\n\t";
224  for (ordinal_type i=0; i<static_cast<ordinal_type>(norms.size()); i++)
225  os << norms[i] << " ";
226  os << "\n";
227 }
228 
229 template <typename ordinal_type, typename value_type, typename ordering_type>
233 {
234  return basis_map[i];
235 }
236 
237 template <typename ordinal_type, typename value_type, typename ordering_type>
240 index(const MultiIndex<ordinal_type>& term) const
241 {
242  typename coeff_set_type::const_iterator it = basis_set.find(term);
243  TEUCHOS_TEST_FOR_EXCEPTION(it == basis_set.end(), std::logic_error,
244  "Invalid term " << term);
245  return it->second;
246 }
247 
248 template <typename ordinal_type, typename value_type, typename ordering_type>
249 const std::string&
251 getName() const
252 {
253  return name;
254 }
255 
256 template <typename ordinal_type, typename value_type, typename ordering_type>
260 {
261  return bases;
262 }
263 
264 template <typename ordinal_type, typename value_type, typename ordering_type>
268 {
269  return max_orders;
270 }
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.