Debian Codegraph @

packages/nox/test/epetra/DS6.5.1/Example_TrustRegion.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
22 // Trilinos Objects
23 #ifdef HAVE_MPI
24 #include "Epetra_MpiComm.h"
25 #else
26 #include "Epetra_SerialComm.h"
27 #endif
28 #include "Epetra_Map.h"
29 #include "Epetra_Vector.h"
30 #include "Epetra_RowMatrix.h"
31 #include "Epetra_CrsMatrix.h"
32 #include "Epetra_Map.h"
33 #include "Epetra_LinearProblem.h"
34 #include "AztecOO.h"
35
36 #include "Teuchos_StandardCatchMacros.hpp"
37
38 // User's application specific files
39 #include "Problem_Interface.H" // Interface file to NOX
40 #include "DennisSchnabel.H"
41
42 using namespace std;
43
44 int main(int argc, char *argv[])
45 {
46 int i;
47
48 // Initialize MPI
49 #ifdef HAVE_MPI
50 MPI_Init(&argc,&argv);
51 #endif
52
53 // Create a communicator for Epetra objects
54 #ifdef HAVE_MPI
55 Epetra_MpiComm Comm( MPI_COMM_WORLD );
56 #else
57 Epetra_SerialComm Comm;
58 #endif
59
60 bool verbose = false;
61 if (argc > 1)
62 if (argv[1][0]=='-' && argv[1][1]=='v')
63 verbose = true;
64
65 // Get the process ID and the total number of processors
66 int MyPID = Comm.MyPID();
67 int NumProc = Comm.NumProc();
68
69 int NumGlobalElements = 2; // Hardcoded for D&S Example problem
70
71 bool success = false;
72
73 try {
74 // A maximum of 2 procesors is allowed since there are only 2 equations
75 if (NumProc >= 3)
76 throw "ERROR: Maximum number of processors is 2!";
77
78 // Create the Problem class. This creates all required
79 // Epetra objects for the problem and allows calls to the
80 // function (RHS) and Jacobian evaluation routines.
81 DennisSchnabel Problem(NumGlobalElements, Comm);
82
83 // Get the vector from the Problem
84 Teuchos::RCP<Epetra_Vector> soln = Problem.getSolution();
85 NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView);
86
87 // Initialize Solution
88 if (MyPID==0) {
89 (*soln)[0]=2.0;
90 if (NumProc==1)
91 (*soln)[1]=0.5;
92 }
93 else
94 (*soln)[0]=0.5;
95
96 // Begin Nonlinear Solver ************************************
97
98 // Create the top level parameter list
99 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
100 Teuchos::rcp(new Teuchos::ParameterList);
101 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
102
103 // Set the nonlinear solver method
104 nlParams.set("Nonlinear Solver", "Inexact Trust Region Based");
105 nlParams.sublist("Trust Region").
106 set("Inner Iteration Method", "Standard Trust Region");
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
148 // Sublist for Newton direction
149 Teuchos::ParameterList& dirParams = nlParams.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", 20);
158 lsParams.set("Tolerance", 1e-4);
159 lsParams.set("Preconditioner", "None");
160 if( verbose )
161 lsParams.set("Output Frequency", 1);
162
163 // Sublist for Cauchy direction
164 Teuchos::ParameterList& cauchyDirParams = nlParams.sublist("Cauchy Direction");
165 cauchyDirParams.set("Method", "Steepest Descent");
166 Teuchos::ParameterList& sdParams = cauchyDirParams.sublist("Steepest Descent");
167 sdParams.set("Scaling Type", "Quadratic Model Min");
168
169 // Create the interface between the test problem and the nonlinear solver
170 Teuchos::RCP<Problem_Interface> interface =
171 Teuchos::rcp(new Problem_Interface(Problem));
172
173 // Create the Epetra_RowMatrix. Uncomment one or more of the following:
174 // 1. User supplied (Epetra_RowMatrix)
175 Teuchos::RCP<Epetra_RowMatrix> A = Problem.getJacobian();
176
177 // Create the callback interfaces for filling the residual and Jacbian
178 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
179 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
180
181 // Create the Linear System
182 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
183 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
184 iReq, iJac, A, noxSoln));
185
186 // Create the Group
187 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
188 Teuchos::rcp(new NOX::Epetra::Group(printParams,
189 iReq,
190 noxSoln,
191 linSys));
192
193 // Create the convergence tests
194 Teuchos::RCP<NOX::StatusTest::NormF> testNormF =
195 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-6));
196 Teuchos::RCP<NOX::StatusTest::MaxIters> testMaxIters =
197 Teuchos::rcp(new NOX::StatusTest::MaxIters(25));
198 Teuchos::RCP<NOX::StatusTest::Combo> combo =
199 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR,
200 testNormF, testMaxIters));
201
202 // Create the method
203 Teuchos::RCP<NOX::Solver::Generic> solver =
204 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
205 NOX::StatusTest::StatusType status = solver->solve();
206
207 // Get the Epetra_Vector with the final solution from the solver
208 const NOX::Epetra::Group& finalGroup =
209 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
210 const Epetra_Vector& finalSolution =
211 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
212
213 // End Nonlinear Solver **************************************
214
215 // Output the parameter list
216 if (printing.isPrintType(NOX::Utils::Parameters)) {
217 std::cout << std::endl << "Final Parameters" << std::endl
218 << "****************" << std::endl;
219 solver->getList().print(cout);
220 std::cout << std::endl;
221 }
222
223 // Print solution
224 char file_name[25];
225 FILE *ifp;
226 int NumMyElements = soln->Map().NumMyElements();
227 (void) sprintf(file_name, "output.%d",MyPID);
228 ifp = fopen(file_name, "w");
229 for (i=0; i<NumMyElements; i++)
230 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
231 fclose(ifp);
232
233 // Report results
234
235 int testStatus = 0; // Converged
236
237 // 1. Convergence
238 if (status != NOX::StatusTest::Converged) {
239 if (MyPID==0) std::cout << "Nonlinear solver failed to converge!" << std::endl;
240 testStatus = 1;
241 }
242 // 2. Nonlinear Iterations (5)
243 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations",0) != 5) {
244 testStatus = 2;
245 }
246
247 success = testStatus==0;
248
249 if (success)
250 std::cout << "Test passed!" << std::endl;
251 else
252 std::cout << "Test failed!" << std::endl;
253 }
254 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
255
256 #ifdef HAVE_MPI
257 MPI_Finalize() ;
258 #endif
259
260 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
261 } // end main