Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_KL_ExponentialRandomFieldImp.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 <cmath>
11 #include <algorithm>
12 #include "Teuchos_Array.hpp"
13 
15 
16 template <typename value_type, typename execution_space>
19 {
20  // Get required parameters
21  num_KL = solverParams.get<int>("Number of KL Terms");
22  mean = solverParams.get<double>("Mean");
23  std_dev = solverParams.get<double>("Standard Deviation");
24 
25  Teuchos::Array<double> domain_upper_bound_double;
26  Teuchos::Array<double> domain_lower_bound_double;
27  Teuchos::Array<double> correlation_length_double;
28 
29  // Get Domain Upper Bounds
30  if (solverParams.isType<std::string>("Domain Upper Bounds"))
31  domain_upper_bound_double =
32  Teuchos::getArrayFromStringParameter<double>(
33  solverParams, "Domain Upper Bounds");
34  else
35  domain_upper_bound_double =
36  solverParams.get< Teuchos::Array<double> >("Domain Upper Bounds");
37 
38  // (Convert each element from double to value_type)
39  Teuchos::Array<value_type> domain_upper_bound(domain_upper_bound_double.size());
40  for (int i=0; i<domain_upper_bound.size(); i++)
41  domain_upper_bound[i]=domain_upper_bound_double[i];
42 
43  // Get Domain Lower Bounds
44  if (solverParams.isType<std::string>("Domain Lower Bounds"))
45  domain_lower_bound_double =
46  Teuchos::getArrayFromStringParameter<double>(
47  solverParams, "Domain Lower Bounds");
48  else
49  domain_lower_bound_double =
50  solverParams.get< Teuchos::Array<double> >("Domain Lower Bounds");
51 
52  // (Convert each element from double to value_type)
53  Teuchos::Array<value_type> domain_lower_bound(domain_lower_bound_double.size());
54  for (int i=0; i<domain_lower_bound.size(); i++)
55  domain_lower_bound[i]=domain_lower_bound_double[i];
56 
57  // Get Correlation Lengths
58  if (solverParams.isType<std::string>("Correlation Lengths"))
59  correlation_length_double =
60  Teuchos::getArrayFromStringParameter<double>(
61  solverParams, "Correlation Lengths");
62  else
63  correlation_length_double =
64  solverParams.get< Teuchos::Array<double> >("Correlation Lengths");
65 
66  // (Convert each element from double to value_type)
67  Teuchos::Array<value_type> correlation_length(correlation_length_double.size());
68  for (int i=0; i<correlation_length.size(); i++)
69  correlation_length[i]=correlation_length_double[i];
70 
71  // Compute 1-D eigenfunctions for each dimension
72  dim = domain_upper_bound.size();
74  for (int i=0; i<dim; i++) {
75  eig_pairs[i].resize(num_KL);
77  num_KL, domain_lower_bound[i], domain_upper_bound[i],
78  correlation_length[i], i, solverParams);
79  eig_pairs[i] = cov_func.getEigenPairs();
80  }
81 
82  // Compute all possible tensor product combinations of 1-D eigenfunctions
83  int num_prod = static_cast<int>(std::pow(static_cast<double>(num_KL),
84  static_cast<double>(dim)));
85  Teuchos::Array<product_eigen_pair_type> product_eig_pairs(num_prod);
86  Teuchos::Array<int> index(dim, 0);
87  int cnt = 0;
90  while (cnt < num_prod) {
91  for (int i=0; i<dim; i++) {
92  eigs[i] = eig_pairs[i][index[i]];
93  }
94  product_eig_pairs[cnt].set(eigs);
95  ++index[0];
96  int j = 0;
97  while (j < dim-1 && index[j] == num_KL) {
98  index[j] = 0;
99  ++j;
100  ++index[j];
101  }
102  ++cnt;
103  }
104 
105  // Sort product eigenfunctions based on product eigenvalue
106  std::sort(product_eig_pairs.begin(), product_eig_pairs.end(),
108 
109  // Copy eigenpairs into view
110  product_eigen_funcs =
111  eigen_func_array_type("product eigen functions", num_prod, dim);
112  product_eigen_values =
113  eigen_value_array_type("product eigen vvalues", num_prod);
114  typename eigen_func_array_type::HostMirror host_product_eigen_funcs =
115  Kokkos::create_mirror_view(product_eigen_funcs);
116  typename eigen_value_array_type::HostMirror host_product_eigen_values =
117  Kokkos::create_mirror_view(product_eigen_values);
118  for (int i=0; i<num_prod; ++i) {
119  host_product_eigen_values(i) = 1.0;
120  for (int j=0; j<dim; ++j) {
121  host_product_eigen_values(i) *= product_eig_pairs[i].eig_pairs[j].eig_val;
122  host_product_eigen_funcs(i,j) =
123  product_eig_pairs[i].eig_pairs[j].eig_func;
124  }
125  }
126  Kokkos::deep_copy(product_eigen_funcs, host_product_eigen_funcs);
127  Kokkos::deep_copy(product_eigen_values, host_product_eigen_values);
128 }
129 
130 template <typename value_type, typename execution_space>
131 template <typename point_type, typename rv_type>
132 KOKKOS_INLINE_FUNCTION
135 evaluate(const point_type& point, const rv_type& random_variables) const
136 {
138  result_type result = mean;
139  for (int i=0; i<num_KL; ++i) {
140  result_type t = std_dev*std::sqrt(product_eigen_values(i));
141  for (int j=0; j<dim; ++j)
142  t *= product_eigen_funcs(i,j).evaluate(point[j]);
143  result += random_variables[i]*t;
144  }
145  return result;
146 }
147 
148 template <typename value_type, typename execution_space>
149 template <typename point_type>
150 KOKKOS_INLINE_FUNCTION
153 evaluate_standard_deviation(const point_type& point) const
154 {
156  result_type result = 0.0;
157  for (int i=0; i<num_KL; i++) {
158  result_type t = 1.0;
159  for (int j=0; j<dim; ++j)
160  t *= product_eigen_funcs(i,j).evaluate(point[j]);
161  result += product_eigen_values(i).eig_val*t*t;
162  }
163  return std::sqrt(result);
164 }
165 
166 template <typename value_type, typename execution_space>
167 template <typename point_type>
168 KOKKOS_INLINE_FUNCTION
171 evaluate_eigenfunction(const point_type& point, int i) const
172 {
174  result_type t = std_dev*std::sqrt(product_eigen_values(i));
175  for (int j=0; j<dim; ++j)
176  t *= product_eigen_funcs(i,j).evaluate(point[j]);
177  return t;
178 }
179 
180 template <typename value_type, typename execution_space>
181 void
183 print(std::ostream& os) const
184 {
185  os << "KL expansion using " << num_KL << " terms in " << dim
186  << " dimensions:" << std::endl;
187  os << "\tMean = " << mean << ", standard deviation = " << std_dev
188  << std::endl;
189  os << "\tEigenvalues, Eigenfunctions:" << std::endl;
190  for (int i=0; i<num_KL; i++) {
191  os << "\t\t" << product_eigen_values(i) << ", ";
192  for (int j=0; j<dim-1; ++j) {
193  os << "(";
194  product_eigen_funcs(i,j).print(os);
195  os << ") * ";
196  }
197  os << "(";
198  product_eigen_funcs(i,dim-1).print(os);
199  os << ")" << std::endl;
200  }
201 }
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION Teuchos::PromotionTraits< typename rv_type::value_type, value_type >::promote evaluate(const point_type &point, const rv_type &random_variables) const
Evaluate random field at a point.
SparseArrayIterator< index_iterator, value_iterator >::value_type index(const SparseArrayIterator< index_iterator, value_iterator > &it)
Kokkos::View< one_d_eigen_func_type **, execution_space > eigen_func_array_type
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
T & get(ParameterList &l, const std::string &name)
Kokkos::View< value_type *, execution_space > eigen_value_array_type
void print(std::ostream &os) const
Print KL expansion.
KOKKOS_INLINE_FUNCTION Teuchos::PromotionTraits< typename point_type::value_type, value_type >::promote evaluate_eigenfunction(const point_type &point, int i) const
Evaluate given eigenfunction at a point.
KOKKOS_INLINE_FUNCTION Teuchos::PromotionTraits< typename point_type::value_type, value_type >::promote evaluate_standard_deviation(const point_type &point) const
Evaluate standard deviation of random field at a point.
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
void resize(size_type new_size, const value_type &x=value_type())
Class representing an exponential covariance function and its KL eigevalues/eigenfunctions.
iterator end()
Predicate class for sorting product eigenfunctions based on eigenvalue.
size_type size() const
bool isType(const std::string &name) const
const Teuchos::Array< eigen_pair_type > & getEigenPairs() const
Get eigenpairs.
iterator begin()
Stokhos::CrsMatrix< ValueType, Device, Layout >::HostMirror create_mirror_view(const Stokhos::CrsMatrix< ValueType, Device, Layout > &A)