Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemAndersonAcceleration.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 // Initialize MPI
50 #ifdef HAVE_MPI
51 MPI_Init(&argc,&argv);
52 #endif
53
54 int status = 0; // Converged
55
56 // Create a communicator for Epetra objects
57 #ifdef HAVE_MPI
58 Epetra_MpiComm Comm( MPI_COMM_WORLD );
59 #else
60 Epetra_SerialComm Comm;
61 #endif
62
63 // Get the process ID and the total number of processors
64 int MyPID = Comm.MyPID();
65 int NumProc = Comm.NumProc();
66
67 // Check verbosity level
68 bool verbose = false;
69 if (argc > 1)
70 if (argv[1][0]=='-' && argv[1][1]=='v')
71 verbose = true;
72
73 // Get the number of elements from the command line
74 int NumGlobalElements = 0;
75 if ((argc > 2) && (verbose))
76 NumGlobalElements = atoi(argv[2]) + 1;
77 else if ((argc > 1) && (!verbose))
78 NumGlobalElements = atoi(argv[1]) + 1;
79 else
80 NumGlobalElements = 200;
81
82 bool success = false;
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 // Get the vector from the Problem
99 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
100 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
101 Teuchos::rcp(new NOX::Epetra::Vector(soln, NOX::Epetra::Vector::CreateView));
102
103 // Set the PDE factor (for nonlinear forcing term). This could be specified
104 // via user input.
105 interface->setPDEfactor(1000.0);
106
107 // Set the initial guess
108 soln->PutScalar(1.0);
109
110 // Begin Nonlinear Solver ************************************
111
112 // Create the top level parameter list
113 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
114 Teuchos::rcp(new Teuchos::ParameterList);
115 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
116
117 // Set the nonlinear solver method
118 nlParams.set("Nonlinear Solver", "Anderson Accelerated Fixed-Point");
119 nlParams.sublist("Anderson Parameters").set("Storage Depth", 2);
120 nlParams.sublist("Anderson Parameters").set("Mixing Parameter", -1.0);
121 nlParams.sublist("Anderson Parameters").sublist("Preconditioning").set("Precondition", true);
122 nlParams.sublist("Anderson Parameters").sublist("Preconditioning").set("Recompute Jacobian", true);
123
124 //nlParams.set("Nonlinear Solver", "Line Search Based");
125
126 // Set the printing parameters in the "Printing" sublist
127 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
128 printParams.set("MyPID", MyPID);
129 printParams.set("Output Precision", 3);
130 printParams.set("Output Processor", 0);
131 if (verbose)
132 printParams.set("Output Information",
133 NOX::Utils::OuterIteration +
134 NOX::Utils::OuterIterationStatusTest +
135 NOX::Utils::InnerIteration +
136 NOX::Utils::LinearSolverDetails +
137 NOX::Utils::Parameters +
138 NOX::Utils::Details +
139 NOX::Utils::Warning +
140 NOX::Utils::Debug +
141 NOX::Utils::TestDetails +
142 NOX::Utils::Error);
143 else
144 printParams.set("Output Information", NOX::Utils::Error +
145 NOX::Utils::TestDetails);
146
147 // Create a print class for controlling output below
148 NOX::Utils printing(printParams);
149
150 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
151 dirParams.set("Method", "Newton");
152 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
153 newtonParams.set("Forcing Term Method", "Constant");
154
155 // Sublist for linear solver for the Newton method
156 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
157 lsParams.set("Aztec Solver", "GMRES");
158 lsParams.set("Max Iterations", 800);
159 lsParams.set("Tolerance", 1e-4);
160
161 // This is an ML Preconditioner test. If ML was not enabled,
162 // // just exit with success
163 #ifndef HAVE_NOX_ML_EPETRA
164 printing.out() << "NOX not compiled with ML, exiting test!" << std::endl;
165 printing.out() << "Test passed!" << std::endl;
166 #ifdef HAVE_MPI
167 MPI_Finalize();
168 #endif
169 return EXIT_SUCCESS;
170 #endif
171
172 // Various Preconditioner options
173 #ifdef HAVE_NOX_ML_EPETRA
174 // Comment out the previous Preconditioner spec and uncomment this line
175 // to turn on ML
176 lsParams.set("Preconditioner", "ML");
177 lsParams.set("Preconditioner Operator", "Use Jacobian");
178 // Create a parameter list for ML options
179 Teuchos::ParameterList MLList;
180 #endif
181
182 #ifdef HAVE_NOX_ML_EPETRA
183 if( lsParams.get("Preconditioner", "None") == "ML" ) {
184 // This Teuchos parameter list is needed for ML
185
186 // These specifications come straight from the example in
187 // Trilinos/packages/ml/example/ml_example_epetra_preconditioner.cpp
188
189 // set defaults for classic smoothed aggregation
190 /*ML_Epetra::SetDefaults("SA",MLList);
191 // maximum number of levels
192 MLList.set("max levels",5);
193 MLList.set("increasing or decreasing","decreasing");
194 // use Uncoupled scheme to create the aggregate,
195 // from level 3 use the better but more expensive MIS
196 MLList.set("aggregation: type", "Uncoupled");
197 MLList.set("aggregation: type (level 3)", "MIS");
198 // smoother is Gauss-Seidel. Example file
199 // ml_example_epetra_preconditioner_2level.cpp shows how to use
200 // AZTEC's preconditioners as smoothers
201 MLList.set("smoother: type","Gauss-Seidel");
202 // use both pre and post smoothing
203 MLList.set("smoother: pre or post", "both");
204 // solve with serial direct solver KLU
205 MLList.set("coarse: type","Jacobi");
206 // Set ML output verbosity
207 if( verbose )
208 MLList.set("output", 10);
209 else
210 MLList.set("output", 0);*/
211 MLList.set("PDE equations", 1);
212 MLList.set("coarse: max size", 2000);
213 MLList.set("coarse: type", "Amesos-KLU");
214 //MLList.set("ML output", 10);
215 lsParams.set("ML", MLList);
216 }
217 #endif
218
219 // Add a user defined pre/post operator object
220 Teuchos::RCP<NOX::Abstract::PrePostOperator> ppo =
221 Teuchos::rcp(new UserPrePostOperator(printing));
222 nlParams.sublist("Solver Options").set("User Defined Pre/Post Operator",
223 ppo);
224
225 // Let's force all status tests to do a full check
226 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
227
228 // Create all possible Epetra_Operators.
229 // 1. User supplied (Epetra_RowMatrix)
230 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
231
232 // Create the linear system
233 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
234 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
235 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = interface;
236 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
237 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
238 iReq,
239 iJac, Analytic,
240 //iPrec, Analytic,
241 *soln));
242
243 // Create the Group
244 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
245 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
246 Teuchos::rcp(new NOX::Epetra::Group(printParams,
247 iReq,
248 initialGuess,
249 linSys));
250 NOX::Epetra::Group& grp = *grpPtr;
251
252 // Create the convergence tests
253 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
254 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
255 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
256 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
257 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
258 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
259 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
260 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
261 Teuchos::RCP<NOX::StatusTest::Combo> converged =
262 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
263 converged->addStatusTest(absresid);
264 converged->addStatusTest(relresid);
265 converged->addStatusTest(wrms);
266 converged->addStatusTest(update);
267 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
268 Teuchos::rcp(new NOX::StatusTest::MaxIters(100));
269 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
270 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
271 Teuchos::RCP<NOX::StatusTest::Combo> combo =
272 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
273 combo->addStatusTest(fv);
274 combo->addStatusTest(converged);
275 combo->addStatusTest(maxiters);
276
277 // Create the solver
278 Teuchos::RCP<NOX::Solver::Generic> solver =
279 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
280 NOX::StatusTest::StatusType solvStatus = solver->solve();
281
282 // End Nonlinear Solver **************************************
283
284 // Get the Epetra_Vector with the final solution from the solver
285 const NOX::Epetra::Group& finalGroup =
286 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
287 const Epetra_Vector& finalSolution =
288 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
289 getEpetraVector();
290
291 // Output the parameter list
292 if (verbose) {
293 if (printing.isPrintType(NOX::Utils::Parameters)) {
294 printing.out() << std::endl << "Final Parameters" << std::endl
295 << "****************" << std::endl;
296 solver->getList().print(printing.out());
297 printing.out() << std::endl;
298 }
299 }
300
301 // Print solution
302 char file_name[25];
303 FILE *ifp;
304 int NumMyElements = soln->Map().NumMyElements();
305 (void) sprintf(file_name, "output.%d",MyPID);
306 ifp = fopen(file_name, "w");
307 for (int i=0; i<NumMyElements; i++)
308 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
309 fclose(ifp);
310
311
312 // Tests
313
314 // 1. Convergence
315 if (solvStatus != NOX::StatusTest::Converged) {
316 status = 1;
317 if (printing.isPrintType(NOX::Utils::Error))
318 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
319 }
320 // 2. Nonlinear solve iterations (10)
321 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 11)
322 status = 2;
323
324 success = status==0;
325
326 // Summarize test results
327 if (success)
328 printing.out() << "Test passed!" << std::endl;
329 else
330 printing.out() << "Test failed!" << std::endl;
331
332 printing.out() << "Status = " << status << std::endl;
333 }
334 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
335
336 #ifdef HAVE_MPI
337 MPI_Finalize();
338 #endif
339
340 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
341 }