Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemAztecSolverPrecReuse.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 #include "NOX.H"
21 #include "NOX_Epetra.H"
22
23 // Trilinos Objects
24 #ifdef HAVE_MPI
25 #include "Epetra_MpiComm.h"
26 #else
27 #include "Epetra_SerialComm.h"
28 #endif
29 #include "Epetra_Map.h"
30 #include "Epetra_Vector.h"
31 #include "Epetra_RowMatrix.h"
32 #include "Epetra_CrsMatrix.h"
33 #include "Epetra_Map.h"
34 #include "Epetra_LinearProblem.h"
35
36 // Azteco Objects
37 #include "AztecOO.h"
38 #include "Ifpack.h"
39
40 // User's application specific files
41 #include "1DfemInterface.H"
42 #include "1DfemPrePostOperator.H"
43
44 #include "Teuchos_ParameterList.hpp"
45 #include "Teuchos_StandardCatchMacros.hpp"
46
47 using namespace std;
48
49 int main(int argc, char *argv[])
50 {
51
52 // Initialize MPI
53 #ifdef HAVE_MPI
54 MPI_Init(&argc,&argv);
55 #endif
56
57 // Create a communicator for Epetra objects
58 #ifdef HAVE_MPI
59 Epetra_MpiComm Comm( MPI_COMM_WORLD );
60 #else
61 Epetra_SerialComm Comm;
62 #endif
63
64 // Check verbosity level
65 bool verbose = false;
66 bool success = false;
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 if (argc > 1)
73 if (argv[1][0]=='-' && argv[1][1]=='v')
74 verbose = true;
75
76 // Get the number of elements from the command line
77 int NumGlobalElements = 0;
78 if ((argc > 2) && (verbose))
79 NumGlobalElements = atoi(argv[2]) + 1;
80 else if ((argc > 1) && (!verbose))
81 NumGlobalElements = atoi(argv[1]) + 1;
82 else
83 NumGlobalElements = 101;
84
85 // The number of unknowns must be at least equal to the
86 // number of processors.
87 if (NumGlobalElements < NumProc) {
88 std::cout << "numGlobalBlocks = " << NumGlobalElements
89 << " cannot be < number of processors = " << NumProc << std::endl;
90 std::cout << "Test failed!" << std::endl;
91 throw std::runtime_error("NOX Error");
92 }
93
94 // Create the interface between NOX and the application
95 // This object is derived from NOX::Epetra::Interface
96 Teuchos::RCP<Interface> interface =
97 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
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 // Begin Nonlinear Solver ************************************
104
105 // Create the top level parameter list
106 Teuchos::RCP<Teuchos::ParameterList> IfpackParamsPtr =
107 Teuchos::rcp(new Teuchos::ParameterList);
108
109 // Set the printing parameters in the "Printing" sublist
110 Teuchos::ParameterList printParams;
111 printParams.set("MyPID", MyPID);
112 printParams.set("Output Precision", 3);
113 printParams.set("Output Processor", 0);
114 if (verbose)
115 printParams.set("Output Information",
116 NOX::Utils::OuterIteration +
117 NOX::Utils::OuterIterationStatusTest +
118 NOX::Utils::InnerIteration +
119 NOX::Utils::LinearSolverDetails +
120 NOX::Utils::Parameters +
121 NOX::Utils::Details +
122 NOX::Utils::Warning +
123 NOX::Utils::Debug +
124 NOX::Utils::TestDetails +
125 NOX::Utils::Error);
126 else
127 printParams.set("Output Information", NOX::Utils::Error +
128 NOX::Utils::TestDetails);
129
130 // Create a print class for controlling output below
131 NOX::Utils p(printParams);
132
133
134 // *******************************
135 // Setup Test Objects
136 // *******************************
137
138 // Create Linear Objects
139 // Get the vector from the Problem
140 if (verbose)
141 p.out() << "Creating Vectors and Matrices" << std::endl;
142 Teuchos::RCP<Epetra_Vector> solution_vec =
143 interface->getSolution();
144 Teuchos::RCP<Epetra_Vector> rhs_vec =
145 Teuchos::rcp(new Epetra_Vector(*solution_vec));
146 Teuchos::RCP<Epetra_Vector> lhs_vec =
147 Teuchos::rcp(new Epetra_Vector(*solution_vec));
148 Teuchos::RCP<Epetra_CrsMatrix> jacobian_matrix =
149 interface->getJacobian();
150
151
152 if (verbose)
153 p.out() << "Evaluating F and J" << std::endl;
154 solution_vec->PutScalar(1.0);
155 interface->computeF(*solution_vec, *rhs_vec);
156 rhs_vec->Scale(-1.0);
157 interface->computeJacobian(*solution_vec, *jacobian_matrix);
158
159 double norm =0.0;
160 rhs_vec->Norm2(&norm);
161 if (verbose)
162 p.out() << "Step 0, ||F|| = " << norm << std::endl;
163
164
165 if (verbose)
166 p.out() << "Creating Ifpack preconditioner" << std::endl;
167
168 Ifpack Factory;
169 Teuchos::RCP<Ifpack_Preconditioner> PreconditionerPtr;
170 PreconditionerPtr = Teuchos::rcp(Factory.Create("ILU",
171 jacobian_matrix.get(),0));
172 Teuchos::ParameterList teuchosParams;
173 PreconditionerPtr->SetParameters(teuchosParams);
174 PreconditionerPtr->Initialize();
175 PreconditionerPtr->Compute();
176
177
178 if (verbose)
179 p.out() << "Creating Aztec Solver" << std::endl;
180
181 Teuchos::RCP<AztecOO> aztecSolverPtr = Teuchos::rcp(new AztecOO());
182 if (verbose)
183 aztecSolverPtr->SetAztecOption(AZ_output, AZ_last);
184 else
185 aztecSolverPtr->SetAztecOption(AZ_output, AZ_none);
186
187 // *******************************
188 // Reuse Test
189 // *******************************
190
191 if (verbose) {
192 p.out() << "**********************************************" << std::endl;
193 p.out() << "Testing Newton solve with prec reuse" << std::endl;
194 p.out() << "**********************************************" << std::endl;
195 }
196
197 int step_number = 0;
198 int max_steps = 20;
199 bool converged = false;
200 int total_linear_iterations = 0;
201 while (norm > 1.0e-8 && step_number < max_steps) {
202
203 step_number++;
204
205 if (verbose)
206 p.out() << "Step " << step_number << ", ||F|| = " << norm << std::endl;
207
208 aztecSolverPtr->SetUserMatrix(jacobian_matrix.get(), false);
209 aztecSolverPtr->SetPrecOperator(PreconditionerPtr.get());
210 aztecSolverPtr->SetRHS(rhs_vec.get());
211 aztecSolverPtr->SetLHS(lhs_vec.get());
212
213 aztecSolverPtr->Iterate(400, 1.0e-4);
214
215 solution_vec->Update(1.0, *lhs_vec, 1.0);
216
217 interface->computeF(*solution_vec, *rhs_vec);
218 rhs_vec->Scale(-1.0);
219 interface->computeJacobian(*solution_vec, *jacobian_matrix);
220
221 rhs_vec->Norm2(&norm);
222
223 total_linear_iterations += aztecSolverPtr->NumIters();
224
225 if (norm < 1.0e-8)
226 converged = true;
227 }
228
229 if (verbose) {
230 p.out() << "Final Step " << step_number << ", ||F|| = " << norm << std::endl;
231 if (converged)
232 p.out() << "Converged!!" << std::endl;
233 else
234 p.out() << "Failed!!" << std::endl;
235 }
236
237 // Tests
238 int status = 0; // Converged
239
240 if (verbose)
241 p.out() << "Total Number of Linear Iterations = "
242 << total_linear_iterations << std::endl;
243
244 if (Comm.NumProc() == 1 && total_linear_iterations != 157)
245 status = 1;
246
247 if (!converged)
248 status = 2;
249
250 success = converged && status == 0;
251
252 // Summarize test results
253 if (success)
254 p.out() << "Test passed!" << std::endl;
255 else
256 p.out() << "Test failed!" << std::endl;
257
258 if (verbose)
259 p.out() << "Status = " << status << std::endl;
260 }
261 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
262
263 #ifdef HAVE_MPI
264 MPI_Finalize();
265 #endif
266
267 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
268 }