Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemMLPreconditioner.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // 1D Finite Element Test Problem
11 /* Solves the nonlinear equation:
12 *
13 * d2u
14 * --- - k * u**2 = 0
15 * dx2
16 *
17 * subject to @ x=0, u=1
18 */
19
20 // NOX Objects
21 #include "NOX.H"
22 #include "NOX_Epetra.H"
23
24 // Trilinos Objects
25 #ifdef HAVE_MPI
26 #include "Epetra_MpiComm.h"
27 #else
28 #include "Epetra_SerialComm.h"
29 #endif
30 #include "Epetra_Map.h"
31 #include "Epetra_Vector.h"
32 #include "Epetra_RowMatrix.h"
33 #include "Epetra_CrsMatrix.h"
34 #include "Epetra_Map.h"
35 #include "Epetra_LinearProblem.h"
36 #include "AztecOO.h"
37
38 // User's application specific files
39 #include "1DfemInterface.H"
40 #include "1DfemPrePostOperator.H"
41
42 #include "Teuchos_ParameterList.hpp"
43 #include "Teuchos_StandardCatchMacros.hpp"
44
45 using namespace std;
46
47 int main(int argc, char *argv[])
48 {
49
50 // Initialize MPI
51 #ifdef HAVE_MPI
52 MPI_Init(&argc,&argv);
53 #endif
54
55 // Create a communicator for Epetra objects
56 #ifdef HAVE_MPI
57 Epetra_MpiComm Comm( MPI_COMM_WORLD );
58 #else
59 Epetra_SerialComm Comm;
60 #endif
61
62 // Get the process ID and the total number of processors
63 int MyPID = Comm.MyPID();
64 int NumProc = Comm.NumProc();
65
66 // Check verbosity level
67 bool verbose = false;
68 if (argc > 1)
69 if (argv[1][0]=='-' && argv[1][1]=='v')
70 verbose = true;
71
72 // Get the number of elements from the command line
73 int NumGlobalElements = 0;
74 if ((argc > 2) && (verbose))
75 NumGlobalElements = atoi(argv[2]) + 1;
76 else if ((argc > 1) && (!verbose))
77 NumGlobalElements = atoi(argv[1]) + 1;
78 else
79 NumGlobalElements = 101;
80
81 bool success = false;
82
83 try {
84 // The number of unknowns must be at least equal to the
85 // number of processors.
86 if (NumGlobalElements < NumProc) {
87 std::cout << "numGlobalBlocks = " << NumGlobalElements
88 << " cannot be < number of processors = " << NumProc << std::endl;
89 std::cout << "Test failed!" << std::endl;
90 throw std::runtime_error("NOX Error");
91 }
92
93 // Create the interface between NOX and the application
94 // This object is derived from NOX::Epetra::Interface
95 Teuchos::RCP<Interface> interface =
96 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
97
98 // Set the PDE factor (for nonlinear forcing term). This could be specified
99 // via user input.
100 interface->setPDEfactor(1000.0);
101
102 // Get the vector from the Problem
103 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
104 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
105 Teuchos::rcp(new NOX::Epetra::Vector(soln,
106 NOX::Epetra::Vector::CreateView));
107
108 // Begin Nonlinear Solver ************************************
109
110 // Create the top level parameter list
111 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
112 Teuchos::rcp(new Teuchos::ParameterList);
113 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
114
115 // Set the nonlinear solver method
116 nlParams.set("Nonlinear Solver", "Line Search Based");
117
118 // Set the printing parameters in the "Printing" sublist
119 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
120 printParams.set("MyPID", MyPID);
121 printParams.set("Output Precision", 3);
122 printParams.set("Output Processor", 0);
123 if (verbose)
124 printParams.set("Output Information",
125 NOX::Utils::OuterIteration +
126 NOX::Utils::OuterIterationStatusTest +
127 NOX::Utils::InnerIteration +
128 NOX::Utils::LinearSolverDetails +
129 NOX::Utils::Parameters +
130 NOX::Utils::Details +
131 NOX::Utils::Warning +
132 NOX::Utils::Debug +
133 NOX::Utils::TestDetails +
134 NOX::Utils::Error);
135 else
136 printParams.set("Output Information", NOX::Utils::Error +
137 NOX::Utils::TestDetails);
138
139 // Create a print class for controlling output below
140 NOX::Utils printing(printParams);
141
142 // Sublist for line search
143 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
144 searchParams.set("Method", "Full Step");
145
146 // Sublist for direction
147 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
148 dirParams.set("Method", "Newton");
149 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
150 newtonParams.set("Forcing Term Method", "Constant");
151
152 // Sublist for linear solver for the Newton method
153 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
154 lsParams.set("Aztec Solver", "GMRES");
155 lsParams.set("Max Iterations", 800);
156 lsParams.set("Tolerance", 1e-4);
157
158 // This is an ML Preconditioner test. If ML was not enabled,
159 // just exit with success
160 #ifndef HAVE_NOX_ML_EPETRA
161 printing.out() << "NOX not compiled with ML, exiting test!" << std::endl;
162 printing.out() << "Test passed!" << std::endl;
163 #ifdef HAVE_MPI
164 MPI_Finalize();
165 #endif
166 return 0;
167 #endif
168
169 // Various Preconditioner options
170 #ifdef HAVE_NOX_ML_EPETRA
171 // Comment out the previous Preconditioner spec and uncomment this line
172 // to turn on ML
173 lsParams.set("Preconditioner", "ML");
174 // Create a parameter list for ML options
175 Teuchos::ParameterList MLList;
176 #endif
177
178 #ifdef HAVE_NOX_ML_EPETRA
179 if( lsParams.get("Preconditioner", "None") == "ML" ) {
180 // This Teuchos parameter list is needed for ML
181
182 // These specifications come straight from the example in
183 // Trilinos/packages/ml/example/ml_example_epetra_preconditioner.cpp
184
185 // set defaults for classic smoothed aggregation
186 ML_Epetra::SetDefaults("SA",MLList);
187 // maximum number of levels
188 MLList.set("max levels",5);
189 MLList.set("increasing or decreasing","decreasing");
190 // use Uncoupled scheme to create the aggregate,
191 // from level 3 use the better but more expensive MIS
192 MLList.set("aggregation: type", "Uncoupled");
193 MLList.set("aggregation: type (level 3)", "MIS");
194 // smoother is Gauss-Seidel. Example file
195 // ml_example_epetra_preconditioner_2level.cpp shows how to use
196 // AZTEC's preconditioners as smoothers
197 MLList.set("smoother: type","Gauss-Seidel");
198 // use both pre and post smoothing
199 MLList.set("smoother: pre or post", "both");
200 // solve with serial direct solver KLU
201 MLList.set("coarse: type","Jacobi");
202
203 // Set ML output verbosity
204 if( verbose )
205 MLList.set("output", 10);
206 else
207 MLList.set("output", 0);
208
209 lsParams.set("ML", MLList);
210
211 }
212 #endif
213
214 // Create all possible Epetra_Operators.
215 // 1. User supplied (Epetra_RowMatrix)
216 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
217
218 // Create the linear system
219 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
220 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
221 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
222 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
223 iReq,
224 iJac, Analytic,
225 *soln));
226
227 // Create the Group
228 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
229 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
230 Teuchos::rcp(new NOX::Epetra::Group(printParams,
231 iReq,
232 initialGuess,
233 linSys));
234
235 NOX::Epetra::Group& grp = *grpPtr;
236
237 // Create the convergence tests
238 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
239 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
240 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
241 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
242 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
243 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
244 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
245 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
246 Teuchos::RCP<NOX::StatusTest::Combo> converged =
247 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
248 converged->addStatusTest(absresid);
249 converged->addStatusTest(relresid);
250 converged->addStatusTest(wrms);
251 converged->addStatusTest(update);
252 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
253 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
254 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
255 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
256 Teuchos::RCP<NOX::StatusTest::Combo> combo =
257 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
258 combo->addStatusTest(fv);
259 combo->addStatusTest(converged);
260 combo->addStatusTest(maxiters);
261
262 // Create the solver
263 Teuchos::RCP<NOX::Solver::Generic> solver =
264 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
265 NOX::StatusTest::StatusType solvStatus = solver->solve();
266
267 // End Nonlinear Solver **************************************
268
269 // Get the Epetra_Vector with the final solution from the solver
270 const NOX::Epetra::Group& finalGroup =
271 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
272 const Epetra_Vector& finalSolution =
273 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
274 getEpetraVector();
275
276 // Output the parameter list
277 if (verbose) {
278 if (printing.isPrintType(NOX::Utils::Parameters)) {
279 printing.out() << std::endl << "Final Parameters" << std::endl
280 << "****************" << std::endl;
281 solver->getList().print(printing.out());
282 printing.out() << std::endl;
283 }
284 }
285
286 // Print solution
287 char file_name[25];
288 FILE *ifp;
289 int NumMyElements = soln->Map().NumMyElements();
290 (void) sprintf(file_name, "output.%d",MyPID);
291 ifp = fopen(file_name, "w");
292 for (int i=0; i<NumMyElements; i++)
293 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
294 fclose(ifp);
295
296
297 // Tests
298 int status = 0; // Converged
299
300 // 1. Convergence
301 if (solvStatus != NOX::StatusTest::Converged) {
302 status = 1;
303 if (printing.isPrintType(NOX::Utils::Error))
304 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
305 }
306 #ifndef HAVE_MPI
307 // 2. Linear solve iterations (552 with ML) - SERIAL TEST ONLY!
308 // The number of linear iterations changes with # of procs.
309 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 552) {
310 status = 2;
311 }
312 #endif
313 // 3. Nonlinear solve iterations (10)
314 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
315 status = 3;
316
317 success = status==0;
318
319 // Summarize test results
320 if (success)
321 printing.out() << "Test passed!" << std::endl;
322 else
323 printing.out() << "Test failed!" << std::endl;
324 }
325 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
326
327 #ifdef HAVE_MPI
328 MPI_Finalize();
329 #endif
330
331 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
332 }