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 //
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_,
49  const ordering_type& coeff_compare) :
50  p(0),
51  d(bases_.size()),
52  sz(0),
53  bases(bases_),
54  sparse_tol(sparse_tol_),
55  max_orders(d),
56  basis_set(coeff_compare),
57  norms()
58 {
59 
60  // Compute largest order
61  for (ordinal_type i=0; i<d; i++) {
62  max_orders[i] = bases[i]->order();
63  if (max_orders[i] > p)
64  p = max_orders[i];
65  }
66 
67  // Compute basis terms
68  MultiIndex<ordinal_type> orders(d);
69  for (ordinal_type i=0; i<d; ++i)
70  orders[i] = bases[i]->order();
71  AnisotropicTotalOrderIndexSet<ordinal_type> index_set(p, orders);
72  ProductBasisUtils::buildProductBasis(index_set, basis_set, basis_map);
73  sz = basis_map.size();
74 
75  // Compute norms
76  norms.resize(sz);
77  value_type nrm;
78  for (ordinal_type k=0; k<sz; k++) {
79  nrm = value_type(1.0);
80  for (ordinal_type i=0; i<d; i++)
81  nrm = nrm * bases[i]->norm_squared(basis_map[k][i]);
82  norms[k] = nrm;
83  }
84 
85  // Create name
86  name = "Tensor product basis (";
87  for (ordinal_type i=0; i<d-1; i++)
88  name += bases[i]->getName() + ", ";
89  name += bases[d-1]->getName() + ")";
90 
91  // Allocate array for basis evaluation
92  basis_eval_tmp.resize(d);
93  for (ordinal_type j=0; j<d; j++)
94  basis_eval_tmp[j].resize(max_orders[j]+1);
95 }
96 
97 template <typename ordinal_type, typename value_type, typename ordering_type>
100 {
101 }
102 
103 template <typename ordinal_type, typename value_type, typename ordering_type>
106 order() const
107 {
108  return p;
109 }
110 
111 template <typename ordinal_type, typename value_type, typename ordering_type>
114 dimension() const
115 {
116  return d;
117 }
118 
119 template <typename ordinal_type, typename value_type, typename ordering_type>
122 size() const
123 {
124  return sz;
125 }
126 
127 template <typename ordinal_type, typename value_type, typename ordering_type>
131 {
132  return norms;
133 }
134 
135 template <typename ordinal_type, typename value_type, typename ordering_type>
136 const value_type&
139 {
140  return norms[i];
141 }
142 
143 template <typename ordinal_type, typename value_type, typename ordering_type>
147 {
148 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
149  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
150 #endif
151 
152  TotalOrderPredicate<ordinal_type> predicate(p, max_orders);
153 
154  return ProductBasisUtils::computeTripleProductTensor(
155  bases, basis_set, basis_map, predicate, predicate, sparse_tol);
156 }
157 
158 template <typename ordinal_type, typename value_type, typename ordering_type>
162 {
163 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
164  TEUCHOS_FUNC_TIME_MONITOR("Stokhos: Total Triple-Product Tensor Fill Time");
165 #endif
166 
167  TotalOrderPredicate<ordinal_type> predicate(p, max_orders);
168  TotalOrderPredicate<ordinal_type> k_predicate(1, max_orders);
169 
170  return ProductBasisUtils::computeTripleProductTensor(
171  bases, basis_set, basis_map, predicate, k_predicate, sparse_tol);
172 }
173 
174 template <typename ordinal_type, typename value_type, typename ordering_type>
178 {
179  // z = psi_{i_1}(0) * ... * psi_{i_d}(0) where i_1,...,i_d are the basis
180  // terms for coefficient i
181  value_type z = value_type(1.0);
182  for (ordinal_type j=0; j<d; j++)
183  z = z * bases[j]->evaluate(value_type(0.0), basis_map[i][j]);
184 
185  return z;
186 }
187 
188 template <typename ordinal_type, typename value_type, typename ordering_type>
189 void
192  Teuchos::Array<value_type>& basis_vals) const
193 {
194  for (ordinal_type j=0; j<d; j++)
195  bases[j]->evaluateBases(point[j], basis_eval_tmp[j]);
196 
197  // Only evaluate basis upto number of terms included in basis_pts
198  for (ordinal_type i=0; i<sz; i++) {
199  value_type t = value_type(1.0);
200  for (ordinal_type j=0; j<d; j++)
201  t *= basis_eval_tmp[j][basis_map[i][j]];
202  basis_vals[i] = t;
203  }
204 }
205 
206 template <typename ordinal_type, typename value_type, typename ordering_type>
207 void
209 print(std::ostream& os) const
210 {
211  os << "Tensor product basis of order " << p << ", dimension " << d
212  << ", and size " << sz << ". Component bases:\n";
213  for (ordinal_type i=0; i<d; i++)
214  os << *bases[i];
215  os << "Basis vector norms (squared):\n\t";
216  for (ordinal_type i=0; i<static_cast<ordinal_type>(norms.size()); i++)
217  os << norms[i] << " ";
218  os << "\n";
219 }
220 
221 template <typename ordinal_type, typename value_type, typename ordering_type>
225 {
226  return basis_map[i];
227 }
228 
229 template <typename ordinal_type, typename value_type, typename ordering_type>
232 index(const MultiIndex<ordinal_type>& term) const
233 {
234  typename coeff_set_type::const_iterator it = basis_set.find(term);
235  TEUCHOS_TEST_FOR_EXCEPTION(it == basis_set.end(), std::logic_error,
236  "Invalid term " << term);
237  return it->second;
238 }
239 
240 template <typename ordinal_type, typename value_type, typename ordering_type>
241 const std::string&
243 getName() const
244 {
245  return name;
246 }
247 
248 template <typename ordinal_type, typename value_type, typename ordering_type>
252 {
253  return bases;
254 }
255 
256 template <typename ordinal_type, typename value_type, typename ordering_type>
260 {
261  return max_orders;
262 }
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.