Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemLeanMatrixFree.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 try {
83 // The number of unknowns must be at least equal to the
84 // number of processors.
85 if (NumGlobalElements < NumProc) {
86 std::cout << "numGlobalBlocks = " << NumGlobalElements
87 << " cannot be < number of processors = " << NumProc << std::endl;
88 std::cout << "Test failed!" << std::endl;
89 throw std::runtime_error("NOX Error");
90 }
91
92 // Create the interface between NOX and the application
93 // This object is derived from NOX::Epetra::Interface
94 Teuchos::RCP<Interface> interface =
95 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
96
97 // Get the vector from the Problem
98 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
99 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
100 Teuchos::rcp(new NOX::Epetra::Vector(soln,
101 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", "Line Search Based");
119
120 // Set the printing parameters in the "Printing" sublist
121 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
122 printParams.set("MyPID", MyPID);
123 printParams.set("Output Precision", 3);
124 printParams.set("Output Processor", 0);
125 if (verbose)
126 printParams.set("Output Information",
127 NOX::Utils::OuterIteration +
128 NOX::Utils::OuterIterationStatusTest +
129 NOX::Utils::InnerIteration +
130 NOX::Utils::LinearSolverDetails +
131 NOX::Utils::Parameters +
132 NOX::Utils::Details +
133 NOX::Utils::Warning +
134 NOX::Utils::Debug +
135 NOX::Utils::TestDetails +
136 NOX::Utils::Error);
137 else
138 printParams.set("Output Information", NOX::Utils::Error +
139 NOX::Utils::TestDetails);
140
141 // Create a print class for controlling output below
142 NOX::Utils printing(printParams);
143
144 // Sublist for line search
145 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
146 searchParams.set("Method", "Full Step");
147
148 // Sublist for 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 //newtonParams.set("Forcing Term Method", "Type 1");
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 // Various Preconditioner options
162 lsParams.set("Preconditioner", "None");
163 //lsParams.set("Preconditioner", "AztecOO");
164 //lsParams.set("Preconditioner", "New Ifpack");
165 lsParams.set("Preconditioner Reuse Policy", "Reuse");
166 //lsParams.set("Preconditioner Reuse Policy", "Recompute");
167 //lsParams.set("Preconditioner Reuse Policy", "Rebuild");
168 lsParams.set("Max Age Of Prec", 5);
169
170 // Add a user defined pre/post operator object
171 Teuchos::RCP<NOX::Abstract::PrePostOperator> ppo =
172 Teuchos::rcp(new UserPrePostOperator(printing));
173 nlParams.sublist("Solver Options").set("User Defined Pre/Post Operator",
174 ppo);
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 // Create all possible Epetra_Operators.
180 // 1. User supplied (Epetra_RowMatrix)
181 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
182 // 2. Matrix-Free (Epetra_Operator)
183 Teuchos::RCP<NOX::Epetra::MatrixFree> MF =
184 Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface,
185 *noxSoln));
186 // 3. Finite Difference (Epetra_RowMatrix)
187 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD =
188 Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface,
189 *soln));
190
191 // Create the linear system
192 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
193 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF;
194 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD;
195 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
196 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
197 //interface,
198 iJac, MF,
199 iPrec, FD,
200 *soln));
201
202 // Create the Group
203 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
204 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
205 Teuchos::rcp(new NOX::Epetra::Group(printParams,
206 iReq,
207 initialGuess,
208 linSys));
209 NOX::Epetra::Group& grp = *grpPtr;
210
211 // For LeanMatrixFree, disable linear resid checking
212 grp.disableLinearResidualComputation(true);
213
214 // uncomment the following for loca supergroups
215 //MF->setGroupForComputeF(*grpPtr);
216 //FD->setGroupForComputeF(*grpPtr);
217
218 // Create the convergence tests
219 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
220 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
221 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
222 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
223 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
224 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
225 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
226 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
227 Teuchos::RCP<NOX::StatusTest::Combo> converged =
228 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
229 converged->addStatusTest(absresid);
230 converged->addStatusTest(relresid);
231 converged->addStatusTest(wrms);
232 converged->addStatusTest(update);
233 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
234 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
235 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
236 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
237 Teuchos::RCP<NOX::StatusTest::Combo> combo =
238 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
239 combo->addStatusTest(fv);
240 combo->addStatusTest(converged);
241 combo->addStatusTest(maxiters);
242
243 // Create the solver
244 Teuchos::RCP<NOX::Solver::Generic> solver =
245 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
246
247 // For LeanMatrixFree, get unperturbed F from solver
248 MF->setSolverForComputeJacobian(solver);
249
250 NOX::StatusTest::StatusType solvStatus = solver->solve();
251
252 // End Nonlinear Solver **************************************
253
254 // Get the Epetra_Vector with the final solution from the solver
255 const NOX::Epetra::Group& finalGroup =
256 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
257 const Epetra_Vector& finalSolution =
258 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
259 getEpetraVector();
260
261 // Output the parameter list
262 if (verbose) {
263 if (printing.isPrintType(NOX::Utils::Parameters)) {
264 printing.out() << std::endl << "Final Parameters" << std::endl
265 << "****************" << std::endl;
266 solver->getList().print(printing.out());
267 printing.out() << std::endl;
268 }
269 }
270
271 // Print solution
272 char file_name[25];
273 FILE *ifp;
274 int NumMyElements = soln->Map().NumMyElements();
275 (void) sprintf(file_name, "output.%d",MyPID);
276 ifp = fopen(file_name, "w");
277 for (int i=0; i<NumMyElements; i++)
278 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
279 fclose(ifp);
280
281
282 // Tests
283 int status = 0; // Converged
284
285 // 1. Convergence
286 if (solvStatus != NOX::StatusTest::Converged) {
287 status = 1;
288 if (printing.isPrintType(NOX::Utils::Error))
289 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
290 }
291 #ifndef HAVE_MPI
292 // 2. Linear solve iterations (53) - SERIAL TEST ONLY!
293 // The number of linear iterations changes with # of procs.
294 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 659) {
295 status = 2;
296 }
297 #endif
298 // 3. Nonlinear solve iterations (10)
299 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
300 status = 3;
301 // 4. Test the pre/post iterate options
302 {
303 UserPrePostOperator & ppo2 =
304 dynamic_cast<UserPrePostOperator&>(*ppo.get());
305 if (ppo2.getNumRunPreIterate() != 10)
306 status = 4;
307 if (ppo2.getNumRunPostIterate() != 10)
308 status = 4;
309 if (ppo2.getNumRunPreSolve() != 1)
310 status = 4;
311 if (ppo2.getNumRunPostSolve() != 1)
312 status = 4;
313 }
314
315 success = status==0;
316 // Summarize test results
317 if (success)
318 printing.out() << "Test passed!" << std::endl;
319 else
320 printing.out() << "Test failed!" << std::endl;
321
322 printing.out() << "Status = " << status << std::endl;
323 }
324 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
325
326 #ifdef HAVE_MPI
327 MPI_Finalize();
328 #endif
329
330 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
331 }