Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nox_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 #include <iostream>
11 #include <sstream>
12 
13 // NOX
14 #include "NOX.H"
15 #include "NOX_Epetra.H"
16 
17 // Epetra communicator
18 #ifdef HAVE_MPI
19 #include "Epetra_MpiComm.h"
20 #else
21 #include "Epetra_SerialComm.h"
22 #endif
23 
24 // Stokhos Stochastic Galerkin
25 #include "Stokhos_Epetra.hpp"
26 
27 // Timing utilities
28 #include "Teuchos_TimeMonitor.hpp"
29 
30 // Our model
31 #include "SimpleME.hpp"
32 
33 int main(int argc, char *argv[]) {
34 
35 // Initialize MPI
36 #ifdef HAVE_MPI
37  MPI_Init(&argc,&argv);
38 #endif
39 
40  int MyPID;
41 
42  try {
43 
44  // Create a communicator for Epetra objects
46 #ifdef HAVE_MPI
47  globalComm = Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_WORLD));
48 #else
49  globalComm = Teuchos::rcp(new Epetra_SerialComm);
50 #endif
51  MyPID = globalComm->MyPID();
52 
53  // Create Stochastic Galerkin basis and expansion
54  int p = 5;
59  int sz = basis->size();
61  Cijk = basis->computeTripleProductTensor();
64  Cijk));
65  if (MyPID == 0)
66  std::cout << "Stochastic Galerkin expansion size = " << sz << std::endl;
67 
68  // Create stochastic parallel distribution
69  int num_spatial_procs = -1;
70  Teuchos::ParameterList parallelParams;
71  parallelParams.set("Number of Spatial Processors", num_spatial_procs);
72  Teuchos::RCP<Stokhos::ParallelData> sg_parallel_data =
73  Teuchos::rcp(new Stokhos::ParallelData(basis, Cijk, globalComm,
74  parallelParams));
76  sg_parallel_data->getMultiComm();
78  sg_parallel_data->getSpatialComm();
79 
80  // Create application model evaluator
82  Teuchos::rcp(new SimpleME(app_comm));
83 
84  // Setup stochastic Galerkin algorithmic parameters
87  sgParams->set("Jacobian Method", "Matrix Free");
88  sgParams->set("Mean Preconditioner Type", "Ifpack");
89 
90  // Create stochastic Galerkin model evaluator
93  expansion, sg_parallel_data,
94  sgParams));
95 
96  // Stochastic Galerkin initial guess
97  // Set the mean to the deterministic initial guess, higher-order terms
98  // to zero
100  sg_model->create_x_sg();
101  x_init_sg->init(0.0);
102  (*x_init_sg)[0] = *(model->get_x_init());
103  sg_model->set_x_sg_init(*x_init_sg);
104 
105  // Stochastic Galerkin parameters
106  // Linear expansion with the mean given by the deterministic initial
107  // parameter values, linear terms equal to 1, and higher order terms
108  // equal to zero.
110  sg_model->create_p_sg(0);
111  p_init_sg->init(0.0);
112  (*p_init_sg)[0] = *(model->get_p_init(0));
113  for (int i=0; i<model->get_p_map(0)->NumMyElements(); i++)
114  (*p_init_sg)[i+1][i] = 1.0;
115  sg_model->set_p_sg_init(0, *p_init_sg);
116  std::cout << "Stochatic Galerkin parameter expansion = " << std::endl
117  << *p_init_sg << std::endl;
118 
119  // Set up NOX parameters
122 
123  // Set the nonlinear solver method
124  noxParams->set("Nonlinear Solver", "Line Search Based");
125 
126  // Set the printing parameters in the "Printing" sublist
127  Teuchos::ParameterList& printParams = noxParams->sublist("Printing");
128  printParams.set("MyPID", MyPID);
129  printParams.set("Output Precision", 3);
130  printParams.set("Output Processor", 0);
131  printParams.set("Output Information",
132  NOX::Utils::OuterIteration +
133  NOX::Utils::OuterIterationStatusTest +
134  NOX::Utils::InnerIteration +
135  //NOX::Utils::Parameters +
136  //NOX::Utils::Details +
137  NOX::Utils::LinearSolverDetails +
138  NOX::Utils::Warning +
139  NOX::Utils::Error);
140 
141  // Create printing utilities
142  NOX::Utils utils(printParams);
143 
144  // Sublist for line search
145  Teuchos::ParameterList& searchParams = noxParams->sublist("Line Search");
146  searchParams.set("Method", "Full Step");
147 
148  // Sublist for direction
149  Teuchos::ParameterList& dirParams = noxParams->sublist("Direction");
150  dirParams.set("Method", "Newton");
151  Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
152  newtonParams.set("Forcing Term Method", "Constant");
153 
154  // Sublist for linear solver for the Newton method
155  Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
156  lsParams.set("Aztec Solver", "GMRES");
157  lsParams.set("Max Iterations", 100);
158  lsParams.set("Size of Krylov Subspace", 100);
159  lsParams.set("Tolerance", 1e-4);
160  lsParams.set("Output Frequency", 10);
161  lsParams.set("Preconditioner", "Ifpack");
162 
163  // Sublist for convergence tests
164  Teuchos::ParameterList& statusParams = noxParams->sublist("Status Tests");
165  statusParams.set("Test Type", "Combo");
166  statusParams.set("Number of Tests", 2);
167  statusParams.set("Combo Type", "OR");
168  Teuchos::ParameterList& normF = statusParams.sublist("Test 0");
169  normF.set("Test Type", "NormF");
170  normF.set("Tolerance", 1e-10);
171  normF.set("Scale Type", "Scaled");
172  Teuchos::ParameterList& maxIters = statusParams.sublist("Test 1");
173  maxIters.set("Test Type", "MaxIters");
174  maxIters.set("Maximum Iterations", 10);
175 
176  // Create NOX interface
178  Teuchos::rcp(new NOX::Epetra::ModelEvaluatorInterface(sg_model));
179 
180  // Create NOX linear system object
186  Teuchos::RCP<Epetra_Operator> M = sg_model->create_WPrec()->PrecOp;
188  lsParams.set("Preconditioner", "User Defined");
189  linsys =
190  Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
191  iJac, A, iPrec, M,
192  *u));
193  // linsys =
194  // Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
195  // iReq, iJac, A, *u));
196 
197  // Build NOX group
199  Teuchos::rcp(new NOX::Epetra::Group(printParams, iReq, *u, linsys));
200 
201  // Create the Solver convergence test
203  NOX::StatusTest::buildStatusTests(statusParams, utils);
204 
205  // Create the solver
207  NOX::Solver::buildSolver(grp, statusTests, noxParams);
208 
209  // Solve the system
210  NOX::StatusTest::StatusType status = solver->solve();
211 
212  // Get final solution
213  const NOX::Epetra::Group& finalGroup =
214  dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
215  const Epetra_Vector& finalSolution =
216  (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
217 
218  // Convert block Epetra_Vector to orthogonal polynomial of Epetra_Vector's
220  sg_model->create_x_sg(View, &finalSolution);
221 
222  utils.out() << "Final Solution (block vector) = " << std::endl;
223  std::cout << finalSolution << std::endl;
224  utils.out() << "Final Solution (polynomial) = " << std::endl;
225  std::cout << *x_sg << std::endl;
226 
227  if (status == NOX::StatusTest::Converged && MyPID == 0)
228  utils.out() << "Example Passed!" << std::endl;
229 
230  }
231 
232  catch (std::exception& e) {
233  std::cout << e.what() << std::endl;
234  }
235  catch (std::string& s) {
236  std::cout << s << std::endl;
237  }
238  catch (char *s) {
239  std::cout << s << std::endl;
240  }
241  catch (...) {
242  std::cout << "Caught unknown exception!" << std::endl;
243  }
244 
245 #ifdef HAVE_MPI
246  MPI_Finalize() ;
247 #endif
248 
249 }
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_x_sg(Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using x map and owned sg map.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_p_sg(int l, Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using p map.
Teuchos::RCP< const EpetraExt::MultiComm > getMultiComm() const
Get global comm.
void init(const value_type &val)
Initialize coefficients.
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual int MyPID() const =0
Nonlinear, stochastic Galerkin ModelEvaluator.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Epetra_Vector > get_x_init() const
Return initial solution.
void set_p_sg_init(int i, const Stokhos::EpetraVectorOrthogPoly &p_sg_in)
Set initial parameter polynomial.
Legendre polynomial basis.
void set_x_sg_init(const Stokhos::EpetraVectorOrthogPoly &x_sg_in)
Set initial solution polynomial.
int main(int argc, char **argv)
Teuchos::RCP< const Epetra_Comm > getSpatialComm() const
Get spatial comm.
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
Teuchos::RCP< EpetraExt::ModelEvaluator::Preconditioner > create_WPrec() const
Create preconditioner operator.