Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemIfpackRecompute.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
41 #include "Teuchos_ParameterList.hpp"
42 #include "Teuchos_StandardCatchMacros.hpp"
43
44 using namespace std;
45
46 int main(int argc, char *argv[])
47 {
48 // Initialize MPI
49 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
50
51 bool success = false;
52 bool verbose = false;
53 try {
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 // Get the process ID and the total number of processors
62 int MyPID = Comm.MyPID();
63 int NumProc = Comm.NumProc();
64
65 // Check verbosity level
66 if (argc > 1)
67 if (argv[1][0]=='-' && argv[1][1]=='v')
68 verbose = true;
69
70 // Get the number of elements from the command line
71 int NumGlobalElements = 0;
72 if ((argc > 2) && (verbose))
73 NumGlobalElements = atoi(argv[2]) + 1;
74 else if ((argc > 1) && (!verbose))
75 NumGlobalElements = atoi(argv[1]) + 1;
76 else
77 NumGlobalElements = 101;
78
79 // The number of unknowns must be at least equal to the
80 // number of processors.
81 if (NumGlobalElements < NumProc) {
82 std::cout << "numGlobalBlocks = " << NumGlobalElements
83 << " cannot be < number of processors = " << NumProc << std::endl;
84 std::cout << "Test failed!" << std::endl;
85 throw std::runtime_error("NOX Error");
86 }
87
88 // Create the interface between NOX and the application
89 // This object is derived from NOX::Epetra::Interface
90 Teuchos::RCP<Interface> interface =
91 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
92
93 // Get the vector from the Problem
94 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
95 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
96 Teuchos::rcp(new NOX::Epetra::Vector(soln,
97 NOX::Epetra::Vector::CreateView));
98
99 // Set the PDE factor (for nonlinear forcing term). This could be specified
100 // via user input.
101 interface->setPDEfactor(1000.0);
102
103 // Set the initial guess
104 soln->PutScalar(1.0);
105
106 // Begin Nonlinear Solver ************************************
107
108 // Create the top level parameter list
109 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
110 Teuchos::rcp(new Teuchos::ParameterList);
111 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
112
113 // Set the nonlinear solver method
114 nlParams.set("Nonlinear Solver", "Line Search Based");
115
116 // Set the printing parameters in the "Printing" sublist
117 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
118 printParams.set("MyPID", MyPID);
119 printParams.set("Output Precision", 3);
120 printParams.set("Output Processor", 0);
121 if (verbose)
122 printParams.set("Output Information",
123 NOX::Utils::OuterIteration +
124 NOX::Utils::OuterIterationStatusTest +
125 NOX::Utils::InnerIteration +
126 NOX::Utils::LinearSolverDetails +
127 NOX::Utils::Parameters +
128 NOX::Utils::Details +
129 NOX::Utils::Warning +
130 NOX::Utils::Debug +
131 NOX::Utils::TestDetails +
132 NOX::Utils::Error);
133 else
134 printParams.set("Output Information", NOX::Utils::Error +
135 NOX::Utils::TestDetails);
136
137 // Create a print class for controlling output below
138 NOX::Utils printing(printParams);
139
140 // Sublist for line search
141 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
142 searchParams.set("Method", "Full Step");
143
144 // Sublist for direction
145 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
146 dirParams.set("Method", "Newton");
147 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
148 newtonParams.set("Forcing Term Method", "Constant");
149
150 // Sublist for linear solver for the Newton method
151 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
152 lsParams.set("Aztec Solver", "GMRES");
153 lsParams.set("Max Iterations", 800);
154 lsParams.set("Tolerance", 1e-4);
155
156 // Various Preconditioner options
157 lsParams.set("Preconditioner", "New Ifpack");
158 lsParams.set("Preconditioner Reuse Policy", "Recompute");
159
160 // Let's force all status tests to do a full check
161 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
162
163 // Create all possible Epetra_Operators.
164 // 1. User supplied (Epetra_RowMatrix)
165 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
166 // 2. Matrix-Free (Epetra_Operator)
167 Teuchos::RCP<NOX::Epetra::MatrixFree> MF =
168 Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface,
169 *noxSoln));
170 // 3. Finite Difference (Epetra_RowMatrix)
171 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD =
172 Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface,
173 *soln));
174
175 // Create the linear system
176 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
177 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF;
178 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD;
179 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
180 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
181 //interface,
182 iJac, MF,
183 iPrec, FD,
184 *soln));
185
186 // Create the Group
187 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
188 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
189 Teuchos::rcp(new NOX::Epetra::Group(printParams,
190 iReq,
191 initialGuess,
192 linSys));
193 NOX::Epetra::Group& grp = *grpPtr;
194
195 // uncomment the following for loca supergroups
196 //MF->setGroupForComputeF(*grpPtr);
197 //FD->setGroupForComputeF(*grpPtr);
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::NormF> relresid =
203 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
204 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
205 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
206 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
207 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
208 Teuchos::RCP<NOX::StatusTest::Combo> converged =
209 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
210 converged->addStatusTest(absresid);
211 converged->addStatusTest(relresid);
212 converged->addStatusTest(wrms);
213 converged->addStatusTest(update);
214 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
215 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
216 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
217 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
218 Teuchos::RCP<NOX::StatusTest::Combo> combo =
219 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
220 combo->addStatusTest(fv);
221 combo->addStatusTest(converged);
222 combo->addStatusTest(maxiters);
223
224 // Create the solver
225 Teuchos::RCP<NOX::Solver::Generic> solver =
226 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
227 NOX::StatusTest::StatusType solvStatus = solver->solve();
228
229 // End Nonlinear Solver **************************************
230
231 // Get the Epetra_Vector with the final solution from the solver
232 const NOX::Epetra::Group& finalGroup =
233 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
234 const Epetra_Vector& finalSolution =
235 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
236 getEpetraVector();
237
238 // Output the parameter list
239 if (verbose) {
240 if (printing.isPrintType(NOX::Utils::Parameters)) {
241 printing.out() << std::endl << "Final Parameters" << std::endl
242 << "****************" << std::endl;
243 solver->getList().print(printing.out());
244 printing.out() << std::endl;
245 }
246 }
247
248 // Print solution
249 char file_name[25];
250 FILE *ifp;
251 int NumMyElements = soln->Map().NumMyElements();
252 (void) sprintf(file_name, "output.%d",MyPID);
253 ifp = fopen(file_name, "w");
254 for (int i=0; i<NumMyElements; i++)
255 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
256 fclose(ifp);
257
258
259 // Tests
260 int status = 0; // Converged
261
262 // 1. Convergence
263 if (solvStatus != NOX::StatusTest::Converged) {
264 status = 1;
265 if (printing.isPrintType(NOX::Utils::Error))
266 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
267 }
268 #ifndef HAVE_MPI
269 // 2. Linear solve iterations (10) - SERIAL TEST ONLY!
270 // The number of linear iterations changes with # of procs.
271 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 10) {
272 status = 2;
273 }
274 #endif
275 // 3. Nonlinear solve iterations (10)
276 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
277 status = 3;
278
279 success = status==0;
280
281 // Summarize test results
282 if (success)
283 printing.out() << "Test passed!" << std::endl;
284 else
285 printing.out() << "Test failed!" << std::endl;
286
287 printing.out() << "Status = " << status << std::endl;
288 }
289 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
290
291 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
292 }