Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemNonlinearCG.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 NonlinearCG 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 * using nonlinear CG direction and linesearch methods.
20
21 This problem is not challenging but serves primarily to demonstrate
22 use of NonlinearCG as well as ensure it is not broken.
23
24 Preconditioning is performed using 5 gmres iterations with the
25 analytic jacobian matrix for the problem. Note that solving these
26 linear systems completely, omitting orthogonalization
27 and using a full step (1.0) is equivalent to using Newton's method.
28
29 */
30
31 // NOX Objects
32 #include "NOX.H"
33 #include "NOX_Epetra.H"
34
35 // Trilinos Objects
36 #ifdef HAVE_MPI
37 #include "Epetra_MpiComm.h"
38 #else
39 #include "Epetra_SerialComm.h"
40 #endif
41 #include "Epetra_Map.h"
42 #include "Epetra_Vector.h"
43 #include "Epetra_RowMatrix.h"
44 #include "Epetra_CrsMatrix.h"
45 #include "Epetra_Map.h"
46 #include "Epetra_LinearProblem.h"
47 #include "AztecOO.h"
48
49 // User's application specific files
50 #include "1DfemInterface.H"
51 #include "1DfemPrePostOperator.H"
52
53 #include "Teuchos_ParameterList.hpp"
54 #include "Teuchos_StandardCatchMacros.hpp"
55
56 using namespace std;
57
58 int main(int argc, char *argv[])
59 {
60
61 // Initialize MPI
62 #ifdef HAVE_MPI
63 MPI_Init(&argc,&argv);
64 #endif
65
66 // Create a communicator for Epetra objects
67 #ifdef HAVE_MPI
68 Epetra_MpiComm Comm( MPI_COMM_WORLD );
69 #else
70 Epetra_SerialComm Comm;
71 #endif
72
73 // Get the process ID and the total number of processors
74 int MyPID = Comm.MyPID();
75 int NumProc = Comm.NumProc();
76
77 // Check verbosity level
78 bool verbose = false;
79 if (argc > 1)
80 if (argv[1][0]=='-' && argv[1][1]=='v')
81 verbose = true;
82
83 // Get the number of elements from the command line
84 int NumGlobalElements = 0;
85 if ((argc > 2) && (verbose))
86 NumGlobalElements = atoi(argv[2]) + 1;
87 else if ((argc > 1) && (!verbose))
88 NumGlobalElements = atoi(argv[1]) + 1;
89 else
90 NumGlobalElements = 101;
91
92 bool success = false;
93
94 try {
95 // The number of unknowns must be at least equal to the number of
96 // processors.
97 if (NumGlobalElements < NumProc)
98 {
99 std::cout << "numGlobalBlocks = " << NumGlobalElements << " cannot be < number of processors = "
100 << NumProc << std::endl;
101 std::cout << "Test failed!" << std::endl;
102 throw std::runtime_error("NOX Error");
103 }
104
105 // Create the interface between NOX and the application
106 // This object is derived from NOX::Epetra::Interface
107 Teuchos::RCP<Interface> interface =
108 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
109
110 // Get the vector from the Problem
111 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
112 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
113 Teuchos::rcp(new NOX::Epetra::Vector(soln, NOX::Epetra::Vector::CreateView));
114
115 // Set the PDE factor (for nonlinear forcing term). This could be
116 // specified via user input.
117 interface->setPDEfactor(1000.0);
118
119 // Set the initial guess
120 soln->PutScalar(1.0);
121
122 // Begin Nonlinear Solver ************************************
123
124 // Create the top level parameter list
125 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
126 Teuchos::rcp(new Teuchos::ParameterList);
127 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
128
129 // Set the nonlinear solver method
130 nlParams.set("Nonlinear Solver", "Line Search Based");
131
132 // Set the printing parameters in the "Printing" sublist
133 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
134 printParams.set("MyPID", MyPID);
135 printParams.set("Output Precision", 3);
136 printParams.set("Output Processor", 0);
137 if (verbose)
138 printParams.set("Output Information",
139 NOX::Utils::OuterIteration +
140 NOX::Utils::OuterIterationStatusTest +
141 NOX::Utils::InnerIteration +
142 NOX::Utils::LinearSolverDetails +
143 NOX::Utils::Parameters +
144 NOX::Utils::Details +
145 NOX::Utils::Warning +
146 NOX::Utils::Debug +
147 NOX::Utils::TestDetails +
148 NOX::Utils::Error);
149 else
150 printParams.set("Output Information", NOX::Utils::Error +
151 NOX::Utils::TestDetails);
152
153 // Create a print class for controlling output below
154 NOX::Utils printing(printParams);
155
156 // Sublist for line search
157 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
158 searchParams.set("Method", "NonlinearCG"); // "Full Step" can also work well sometimes
159
160 // Sublist for direction
161 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
162 dirParams.set("Method", "NonlinearCG");
163 Teuchos::ParameterList& nonlinearcg = dirParams.sublist("Nonlinear CG");
164 nonlinearcg.set("Restart Frequency", 100);
165 nonlinearcg.set("Precondition", "On");
166 nonlinearcg.set("Orthogonalize", "Fletcher-Reeves"); // or "Polak-Ribiere"
167
168 // Sublist for linear solver for the Newton method
169 Teuchos::ParameterList& lsParams = nonlinearcg.sublist("Linear Solver");
170 lsParams.set("Aztec Solver", "GMRES");
171 //lsParams.set("Preconditioner Operator", "Use Jacobian");
172 lsParams.set("Preconditioner", "AztecOO");
173 lsParams.set("AztecOO Preconditioner Iterations", 15);
174 lsParams.set("Preconditioner Reuse Policy", "Recompute");
175
176 // Let's force all status tests to do a full check
177 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
178
179 // 1. User supplied (Epetra_RowMatrix)
180 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
181
182 // Create the linear system
183 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
184 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
185 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
186 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
187 interface,
188 iJac, Analytic,
189 *soln));
190
191 // Create the Group
192 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
193 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
194 Teuchos::rcp(new NOX::Epetra::Group(printParams,
195 iReq,
196 initialGuess,
197 linSys));
198
199 // Create the convergence tests
200 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
201 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
202 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
203 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
204 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
205 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
206 Teuchos::RCP<NOX::StatusTest::Combo> combo =
207 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
208 combo->addStatusTest(fv);
209 combo->addStatusTest(absresid);
210 combo->addStatusTest(maxiters);
211
212 // Create the solver
213 Teuchos::RCP<NOX::Solver::Generic> solver =
214 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
215 NOX::StatusTest::StatusType solvStatus = solver->solve();
216
217 // End Nonlinear Solver **************************************
218
219 // Get the Epetra_Vector with the final solution from the solver
220 const NOX::Epetra::Group& finalGroup =
221 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
222 const Epetra_Vector& finalSolution =
223 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
224 getEpetraVector();
225
226 // Output the parameter list
227 if (verbose)
228 {
229 if (printing.isPrintType(NOX::Utils::Parameters)) {
230 printing.out() << std::endl << "Final Parameters" << std::endl
231 << "****************" << std::endl;
232 solver->getList().print(printing.out());
233 printing.out() << std::endl;
234 }
235 }
236
237 // Print solution
238 char file_name[25];
239 FILE *ifp;
240 int NumMyElements = soln->Map().NumMyElements();
241 (void) sprintf(file_name, "output.%d",MyPID);
242 ifp = fopen(file_name, "w");
243 for (int i=0; i<NumMyElements; i++)
244 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
245 fclose(ifp);
246
247
248 // Tests
249 int status = 0; // Converged
250
251 // 1. Convergence
252 if (solvStatus != NOX::StatusTest::Converged)
253 {
254 status = 1;
255 if (printing.isPrintType(NOX::Utils::Error))
256 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
257 }
258 // 2. Nonlinear solve iterations (10)
259 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) > 13)
260 status = 2;
261
262 success = status==0;
263
264 // Summarize test results
265 if (success)
266 printing.out() << "Test passed!" << std::endl;
267 else
268 printing.out() << "Test failed!" << std::endl;
269
270 printing.out() << "Status = " << status << std::endl;
271 }
272 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
273
274 #ifdef HAVE_MPI
275 MPI_Finalize();
276 #endif
277
278 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
279 }