Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
division_example.cpp
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 // hermite_example
11 //
12 // usage:
13 // hermite_example
14 //
15 // output:
16 // prints the Hermite Polynomial Chaos Expansion of the simple function
17 //
18 // v = 1/(log(u)^2+1)
19 //
20 // where u = 1 + 0.4*H_1(x) + 0.06*H_2(x) + 0.002*H_3(x), x is a zero-mean
21 // and unit-variance Gaussian random variable, and H_i(x) is the i-th
22 // Hermite polynomial.
23 
24 #include "Stokhos.hpp"
25 #include "Stokhos_Sacado.hpp"
28 
29 
30 // Typename of PC expansion type
32 
33 // Linear solvers
35  const int num_division_solver = 3;
37  const char *division_solver_names[] = { "Dense_Direct", "GMRES", "CG" };
38 
39 // Preconditioners
41  const int num_division_prec = 5;
43  const char *division_prec_names[] = { "None", "Diag", "Jacobi","GS","Schur"};
44 
45 // Option for Schur complement precond: full or diag D
46  enum Schur_option { full, diag };
47  const int num_schur_option = 2;
49  const char *schur_option_names[] = { "full", "diag"};
50 
51 // Full matrix or linear matrix (pb = dim + 1 ) used for preconditioner
53  const int num_prec_option = 2;
55  const char *prec_option_names[] = { "full", "linear"};
56 
57 
58 int main(int argc, char **argv)
59 {
60  using Teuchos::Array;
61  using Teuchos::RCP;
62  using Teuchos::rcp;
63 
64  // If applicable, set up MPI.
65  Teuchos::GlobalMPISession mpiSession (&argc, &argv);
66 
67  try {
68 
69  // Setup command line options
71  CLP.setDocString("This example tests the / operator.\n");
72  int d = 3;
73  CLP.setOption("dim", &d, "Stochastic dimension");
74  int p = 5;
75  CLP.setOption("order", &p, "Polynomial order");
76  int n = 100;
77  CLP.setOption("samples", &n, "Number of samples");
78  double shift = 5.0;
79  CLP.setOption("shift", &shift, "Shift point");
80  double tolerance = 1e-6;
81  CLP.setOption("tolerance", &tolerance, "Tolerance in Iterative Solver");
82  int prec_level = 1;
83  CLP.setOption("prec_level", &prec_level, "Level in Schur Complement Prec 0->Solve A0u0=g0 with division; 1->Form 1x1 Schur Complement");
84  int max_it_div = 50;
85  CLP.setOption("max_it_div", &max_it_div, "Maximum # of iterations for division iterative solver");
86  bool equilibrate = true;
87  CLP.setOption("equilibrate", "noequilibrate", &equilibrate,
88  "Equilibrate the linear system");
89 
90  Division_Solver solve_method = Dense_Direct;
91  CLP.setOption("solver", &solve_method,
93  "Solver Method");
94  Division_Prec prec_method = None;
95  CLP.setOption("prec", &prec_method,
97  "Preconditioner Method");
98  Schur_option schur_option = diag;
99  CLP.setOption("schur_option", &schur_option,
101  "Schur option");
102  Prec_option prec_option = whole;
103  CLP.setOption("prec_option", &prec_option,
105  "Prec option");
106  CLP.parse( argc, argv );
107 
108  // Basis
109  Array< RCP<const Stokhos::OneDOrthogPolyBasis<int,double> > > bases(d);
110  for (int i=0; i<d; i++) {
111  bases[i] = rcp(new Stokhos::LegendreBasis<int,double>(p));
112  }
113  RCP<const Stokhos::CompletePolynomialBasis<int,double> > basis =
115 
116  // Quadrature method
117  RCP<const Stokhos::Quadrature<int,double> > quad =
119 
120  // Triple product tensor
121  RCP<Stokhos::Sparse3Tensor<int,double> > Cijk =
122  basis->computeTripleProductTensor();
123 
124  // Expansion methods
127  basis, Cijk, quad));
130 
131  alg_params->set("Division Tolerance", tolerance);
132  alg_params->set("prec_iter", prec_level);
133  alg_params->set("max_it_div", max_it_div);
134  if (solve_method == Dense_Direct)
135  alg_params->set("Division Strategy", "Dense Direct");
136  else if (solve_method == GMRES)
137  alg_params->set("Division Strategy", "GMRES");
138  else if (solve_method == CG)
139  alg_params->set("Division Strategy", "CG");
140 
141 
142  if (prec_method == None)
143  alg_params->set("Prec Strategy", "None");
144  else if (prec_method == Diag)
145  alg_params->set("Prec Strategy", "Diag");
146  else if (prec_method == Jacobi)
147  alg_params->set("Prec Strategy", "Jacobi");
148  else if (prec_method == GS)
149  alg_params->set("Prec Strategy", "GS");
150  else if (prec_method == Schur)
151  alg_params->set("Prec Strategy", "Schur");
152 
153  if (schur_option == diag)
154  alg_params->set("Schur option", "diag");
155  else
156  alg_params->set("Schur option", "full");
157  if (prec_option == linear)
158  alg_params->set("Prec option", "linear");
159 
160  alg_params->set("Use Quadrature for Division", false);
161 
162  if (equilibrate)
163  alg_params->set("Equilibrate", 1);
164  else
165  alg_params->set("Equilibrate", 0);
166 
167 
168 
171  basis, Cijk, quad, alg_params));
172 
173  // Polynomial expansions
174  pce_type u_quad(quad_expn), v_quad(quad_expn);
175  u_quad.term(0,0) = 0.0;
176  for (int i=0; i<d; i++) {
177  u_quad.term(i,1) = 1.0;
178  }
179  pce_type u_alg(alg_expn), v_alg(alg_expn);
180  u_alg.term(0,0) = 0.0;
181  for (int i=0; i<d; i++) {
182  u_alg.term(i,1) = 1.0;
183  }
184 
185  // Compute expansion
186  double scale = std::exp(shift);
187  pce_type b_alg = std::exp(shift + u_alg)/scale;
188  pce_type b_quad = std::exp(shift + u_quad)/scale;
189 // v_alg = (1.0/scale) / b_alg;
190 // v_quad = (1.0/scale) / b_quad;
191  v_alg = 1.0 / std::exp(shift + u_alg);
192  v_quad = 1.0 /std::exp(shift + u_quad);
193 
194 // std::cout << b_alg.getOrthogPolyApprox() << std::endl;
195 
196  // Print u and v
197 // std::cout << "quadrature: v = 1.0 / (shift + u) = ";
198 // v_quad.print(std::cout);
199 // std::cout << "dense solve: v = 1.0 / (shift + u) = ";
200 // v_alg.print(std::cout);
201 
202  double h = 2.0 / (n-1);
203  double err_quad = 0.0;
204  double err_alg = 0.0;
205  for (int i=0; i<n; i++) {
206 
207  double x = -1.0 + h*i;
208  Array<double> pt(d);
209  for (int j=0; j<d; j++)
210  pt[j] = x;
211  double up = u_quad.evaluate(pt);
212  double vp = 1.0/(shift+up);
213  double vp_quad = v_quad.evaluate(pt);
214  double vp_alg = v_alg.evaluate(pt);
215  // std::cout << "vp = " << vp_quad << std::endl;
216  // std::cout << "vp_quad = " << vp_quad << std::endl;
217  // std::cout << "vp_alg = " << vp_alg << std::endl;
218  double point_err_quad = std::abs(vp-vp_quad);
219  double point_err_alg = std::abs(vp-vp_alg);
220  if (point_err_quad > err_quad) err_quad = point_err_quad;
221  if (point_err_alg > err_alg) err_alg = point_err_alg;
222  }
223  std::cout << "\tL_infty norm of quadrature error = " << err_quad
224  << std::endl;
225  std::cout << "\tL_infty norm of solver error = " << err_alg
226  << std::endl;
227 
228  // Check the answer
229  //if (std::abs(err) < 1e-2)
230  std::cout << "\nExample Passed!" << std::endl;
231 
233  }
234  catch (std::exception& e) {
235  std::cout << e.what() << std::endl;
236  }
237 }
const int num_division_solver
const Division_Solver Division_solver_values[]
const Prec_option Prec_option_values[]
const Division_Prec Division_prec_values[]
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
Division_Solver
const char * schur_option_names[]
const char * division_prec_names[]
const char * prec_option_names[]
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
const char * division_solver_names[]
const Schur_option Schur_option_values[]
const int num_division_prec
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Division_Prec
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
Legendre polynomial basis.
int main(int argc, char **argv)
void setDocString(const char doc_string[])
int n
Defines quadrature for a tensor product basis by tensor products of 1-D quadrature rules...