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_OneDExponentialCovarianceFunctionImp.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_Assert.hpp"
11 
12 template <typename value_type>
15  const value_type& a,
16  const value_type& b,
17  const value_type& L_,
18  const int dim_name,
19  Teuchos::ParameterList& solverParams) :
20  L(L_),
21  eig_pair(M)
22 {
23  // Get parameters with default values
24  magnitude_type eps = solverParams.get("Bound Perturbation Size", 1e-6);
25  magnitude_type tol = solverParams.get("Nonlinear Solver Tolerance", 1e-10);
26  int max_it = solverParams.get("Maximum Nonlinear Solver Iterations", 100);
27 
28  value_type aa, alpha, omega, lambda;
29  int i=0;
30  double pi = 4.0*std::atan(1.0);
31  int idx = 0;
32 
33  aa = (b-a)/2.0;
34  while (i < M-1) {
35  alpha = aa/L;
36  omega = bisection(EigFuncCos(alpha), idx*pi, idx*pi+pi/2.0-eps,
37  tol, max_it) / aa;
38  lambda = 2.0*L/(L*L*omega*omega + 1.0);
39  eig_pair[i].eig_val = lambda;
41  ExponentialOneDEigenFunction<value_type>::COS, a, b, omega, dim_name);
42  i++;
43 
44  omega = bisection(EigFuncSin(alpha), idx*pi+pi/2.0+eps, (idx+1)*pi,
45  tol, max_it) / aa;
46  lambda = 2.0*L/(L*L*omega*omega + 1.0);
47  eig_pair[i].eig_val = lambda;
49  ExponentialOneDEigenFunction<value_type>::SIN, a, b, omega, dim_name);
50  i++;
51 
52  idx++;
53  }
54  if (i < M) {
55  omega = bisection(EigFuncCos(alpha), idx*pi, idx*pi+pi/2.0-eps,
56  tol, max_it) / aa;
57  lambda = 2.0*L/(L*L*omega*omega + 1.0);
58  eig_pair[i].eig_val = lambda;
60  ExponentialOneDEigenFunction<value_type>::COS, a, b, omega, dim_name);
61  }
62 }
63 
64 template <typename value_type>
65 template <class Func>
68 newton(const Func& func, const value_type& a, const value_type& b,
69  magnitude_type tol, int max_num_its)
70 {
71  value_type u = (a+b)/2.0;
72  value_type f = func.eval(u);
73  int nit = 0;
75  nit < max_num_its) {
76  std::cout << "u = " << u << " f = " << f << std::endl;
77  value_type dfdu = func.deriv(u);
78  u -= f / dfdu;
79  f = func.eval(u);
80  ++nit;
81  }
82  TEUCHOS_TEST_FOR_EXCEPTION(nit >= max_num_its, std::logic_error,
83  "Nonlinear solver did not converge!" << std::endl);
84 
85  return u;
86 }
87 
88 template <typename value_type>
89 template <class Func>
92 bisection(const Func& func, const value_type& a, const value_type& b,
93  magnitude_type tol, int max_num_its)
94 {
95  value_type low, hi;
96  value_type fa = func.eval(a);
97  value_type fb = func.eval(b);
98  TEUCHOS_TEST_FOR_EXCEPTION(fa*fb > value_type(0.0), std::logic_error,
99  "Bounds [" << a << "," << b << "] must bracket the root!" << std::endl <<
100  "f(a) = " << fa << ", f(b) = " << fb << std::endl)
101 
102  if (fa <= 0.0) {
103  low = a;
104  hi = b;
105  }
106  else {
107  low = b;
108  hi = a;
109  }
110 
111  int nit = 0;
112  value_type u = low + (hi - low)/2.0;
113  value_type f = func.eval(u);
114  while ((Teuchos::ScalarTraits<value_type>::magnitude(hi - low) > 2.0*tol ||
116  nit < max_num_its) {
117  //std::cout << "u = " << u << " f = " << f << std::endl;
118  if (f <= 0.0)
119  low = u;
120  else
121  hi = u;
122  u = low + (hi - low)/2.0;
123  f = func.eval(u);
124  ++nit;
125  }
126  TEUCHOS_TEST_FOR_EXCEPTION(nit >= max_num_its, std::logic_error,
127  "Nonlinear solver did not converge!" << std::endl);
128 
129  return u;
130 }
value_type bisection(const Func &func, const value_type &a, const value_type &b, magnitude_type tol, int max_num_its)
A basic root finder based on bisection.
T & get(ParameterList &l, const std::string &name)
value_type newton(const Func &func, const value_type &a, const value_type &b, magnitude_type tol, int max_num_its)
A basic root finder based on Newton&#39;s method.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
OneDExponentialCovarianceFunction(int M, const value_type &a, const value_type &b, const value_type &L, const int dim_name, Teuchos::ParameterList &solverParams)
Constructor.
Nonlinear function whose roots define eigenvalues for cos() eigenfunction.
Nonlinear function whose roots define eigenvalues for sin() eigenfunction.
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
Teuchos::ScalarTraits< value_type >::magnitudeType magnitude_type
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
One-dimensional eigenfunction for exponential covariance function.