Debian Codegraph @

packages/nox/test/epetra/DS6.5.1/Example.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 // Simple 2 equation test for quadratic and cubic line searches
11 // from Dennis & Schnabel's book, chp 6. The test problem is from
12 // Example 6.5.1
13 /*
14 * U0**2 + U1**2 - 2 = 0
15 * exp(U0-1) + U1**3 -2 = 0
16 */
17
18 // NOX Library
19 #include "NOX.H"
20 #include "NOX_Epetra.H"
21 #include "NOX_SolverStats.hpp"
22
23 // Trilinos Objects
24 #include "Teuchos_StandardCatchMacros.hpp"
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 #include "Teuchos_TestingHelpers.hpp"
38
39 // User's application specific files
40 #include "Problem_Interface.H" // Interface file to NOX
41 #include "DennisSchnabel.H"
42
43 using namespace std;
44
45 int main(int argc, char *argv[])
46 {
47 int i;
48
49 // Initialize MPI
50 #ifdef HAVE_MPI
51 (void) MPI_Init (&argc, &argv);
52 #endif
53
54 // Create a communicator for Epetra objects
55 #ifdef HAVE_MPI
56 Epetra_MpiComm Comm( MPI_COMM_WORLD );
57 #else
58 Epetra_SerialComm Comm;
59 #endif
60
61 bool verbose = false;
62 if (argc > 1)
63 if (argv[1][0]=='-' && argv[1][1]=='v')
64 verbose = true;
65
66 bool success = true;
67 try {
68 // Get the process ID and the total number of processors
69 int MyPID = Comm.MyPID();
70 int NumProc = Comm.NumProc();
71
72 int NumGlobalElements = 2; // Hardcoded for D&S Example problem
73
74 // A maximum of 2 procesors is allowed since there are only 2 equations
75 if (NumProc >= 3) {
76 std::cout << "ERROR: Maximum number of processors is 2!" << std::endl;
77 exit(1);
78 }
79
80 // Create the Problem class. This creates all required
81 // Epetra objects for the problem and allows calls to the
82 // function (RHS) and Jacobian evaluation routines.
83 DennisSchnabel Problem(NumGlobalElements, Comm);
84
85 // Get the vector from the Problem
86 Teuchos::RCP<Epetra_Vector> soln = Problem.getSolution();
87 NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView);
88
89 // Initialize Solution
90 if (MyPID==0) {
91 (*soln)[0]=2.0;
92 if (NumProc==1)
93 (*soln)[1]=0.5;
94 }
95 else
96 (*soln)[0]=0.5;
97
98 // Begin Nonlinear Solver ************************************
99
100 // Create the top level parameter list
101 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
102 Teuchos::rcp(new Teuchos::ParameterList);
103 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
104
105 // Set the nonlinear solver method
106 nlParams.set("Nonlinear Solver", "Line Search Based");
107
108 // Set the printing parameters in the "Printing" sublist
109 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
110 printParams.set("MyPID", MyPID);
111 printParams.set("Output Precision", 5);
112 printParams.set("Output Processor", 0);
113 if ( verbose )
114 printParams.set("Output Information",
115 NOX::Utils::OuterIteration +
116 NOX::Utils::OuterIterationStatusTest +
117 NOX::Utils::InnerIteration +
118 NOX::Utils::Parameters +
119 NOX::Utils::Details +
120 NOX::Utils::Warning +
121 NOX::Utils::TestDetails);
122 else
123 printParams.set("Output Information", NOX::Utils::Error +
124 NOX::Utils::TestDetails);
125
126 NOX::Utils printing(printParams);
127
128 // Identify the test problem
129 if (printing.isPrintType(NOX::Utils::TestDetails))
130 std::cout << "Starting epetra/DS6.5.1/DS_6_5_1.exe" << std::endl;
131
132 // Identify processor information
133 #ifdef HAVE_MPI
134 if (printing.isPrintType(NOX::Utils::TestDetails)) {
135 std::cout << "Parallel Run" << std::endl;
136 std::cout << "Number of processors = " << NumProc << std::endl;
137 std::cout << "Print Process = " << MyPID << std::endl;
138 }
139 Comm.Barrier();
140 if (printing.isPrintType(NOX::Utils::TestDetails))
141 std::cout << "Process " << MyPID << " is alive!" << std::endl;
142 Comm.Barrier();
143 #else
144 if (printing.isPrintType(NOX::Utils::TestDetails))
145 std::cout << "Serial Run" << std::endl;
146 #endif
147 // Sublist for line search
148 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
149 // This test is designed to exercise the following linesearch
150 searchParams.set("Method", "Polynomial");
151
152 // Sublist for direction
153 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
154 dirParams.set("Method", "Newton");
155 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
156 newtonParams.set("Forcing Term Method", "Constant");
157
158 // Sublist for linear solver for the Newton method
159 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
160 lsParams.set("Aztec Solver", "GMRES");
161 lsParams.set("Max Iterations", 20);
162 lsParams.set("Tolerance", 1e-4);
163 lsParams.set("Preconditioner", "None");
164 if( verbose )
165 lsParams.set("Output Frequency", 1);
166
167 // Debugging output
168 #ifdef HAVE_NOX_DEBUG
169 #ifdef HAVE_NOX_EPETRAEXT
170 lsParams.set("Write Linear System", true);
171 lsParams.set("Write Linear System File Prefix", "LinSys_Output_Test");
172 #endif
173 #endif
174
175 // Create the interface between the test problem and the nonlinear solver
176 Teuchos::RCP<Problem_Interface> interface =
177 Teuchos::rcp(new Problem_Interface(Problem));
178
179 // Create the Epetra_RowMatrix. Uncomment one or more of the following:
180 // 1. User supplied (Epetra_RowMatrix)
181 Teuchos::RCP<Epetra_RowMatrix> A = Problem.getJacobian();
182
183 // Create the callback interfaces for filling the residual and Jacbian
184 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
185 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
186
187 // Create the Linear System
188 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
189 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
190 iReq, iJac, A, noxSoln));
191
192 // Create the Group
193 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
194 Teuchos::rcp(new NOX::Epetra::Group(printParams,
195 iReq,
196 noxSoln,
197 linSys));
198
199 // Create the convergence tests
200 Teuchos::RCP<NOX::StatusTest::NormF> testNormF =
201 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-6));
202 Teuchos::RCP<NOX::StatusTest::MaxIters> testMaxIters =
203 Teuchos::rcp(new NOX::StatusTest::MaxIters(25));
204 Teuchos::RCP<NOX::StatusTest::Combo> combo =
205 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR,
206 testNormF, testMaxIters));
207
208 // Create the method
209 Teuchos::RCP<NOX::Solver::Generic> solver =
210 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
211 NOX::StatusTest::StatusType status = solver->solve();
212
213 // Get the Epetra_Vector with the final solution from the solver
214 const NOX::Epetra::Group& finalGroup =
215 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
216 const Epetra_Vector& finalSolution =
217 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
218
219 // End Nonlinear Solver **************************************
220
221 // Output the parameter list
222 if (printing.isPrintType(NOX::Utils::Parameters)) {
223 std::cout << std::endl << "Final Parameters" << std::endl
224 << "****************" << std::endl;
225 solver->getList().print(cout);
226 std::cout << std::endl;
227 }
228
229 // Print solution
230 char file_name[25];
231 FILE *ifp;
232 int NumMyElements = soln->Map().NumMyElements();
233 (void) sprintf(file_name, "output.%d",MyPID);
234 ifp = fopen(file_name, "w");
235 for (i=0; i<NumMyElements; i++)
236 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
237 fclose(ifp);
238
239 // Report results
240 int testStatus = 0; // Converged
241
242 // 1. Convergence
243 if (status != NOX::StatusTest::Converged) {
244 if (MyPID==0) std::cout << "Nonlinear solver failed to converge!" << std::endl;
245 testStatus = 1;
246 }
247 // 2. Nonlinear Iterations (7)
248 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations",0) != 7) {
249 testStatus = 2;
250 }
251 // 3. Linear Iterations (14)
252 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 14) {
253 testStatus = 3;
254 }
255 // 4. Number of Non-trivial Line Searches (2)
256 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Line Search").sublist("Output").get("Total Number of Non-trivial Line Searches",0) != 2) {
257 testStatus = 4;
258 }
259 // 5. Number of Line Search Inner Iterations (4)
260 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Line Search").sublist("Output").get("Total Number of Line Search Inner Iterations",0) != 4) {
261 testStatus = 5;
262 }
263 // 6. Number of Failed Line Searches (0)
264 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Line Search").sublist("Output").get("Total Number of Failed Line Searches",-1) != 0) {
265 testStatus = 6;
266 }
267
268 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearSolves,1,std::cout,success);
269 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearIterations,7,std::cout,success);
270 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numTotalNonlinearIterations,7,std::cout,success);
271
272 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->lineSearch.getNumLineSearches(),7,std::cout,success);
273 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->lineSearch.getNumNonTrivialLineSearches(),2,std::cout,success);
274 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->lineSearch.getNumIterations(),4,std::cout,success);
275 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->lineSearch.getNumFailedLineSearches(),0,std::cout,success);
276
277 TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.lastNonlinearSolve_NumLinearIterations,14,std::cout,success);
278
279 if ( (testStatus == 0) && success)
280 {
281 std::cout << "Test passed!" << std::endl;
282 }
283 else
284 {
285 std::cout << "Test failed!" << std::endl;
286 }
287 }
288 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
289
290 #ifdef HAVE_MPI
291 (void) MPI_Finalize();
292 #endif
293
294 return success ? EXIT_SUCCESS : EXIT_FAILURE;
295 } // end main