Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemAztecSolverPrecRecompute.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 // Get the process ID and the total number of processors
65 int MyPID = Comm.MyPID();
66 int NumProc = Comm.NumProc();
67
68 // Check verbosity level
69 bool verbose = false;
70 if (argc > 1)
71 if (argv[1][0]=='-' && argv[1][1]=='v')
72 verbose = true;
73
74 // Get the number of elements from the command line
75 int NumGlobalElements = 0;
76 if ((argc > 2) && (verbose))
77 NumGlobalElements = atoi(argv[2]) + 1;
78 else if ((argc > 1) && (!verbose))
79 NumGlobalElements = atoi(argv[1]) + 1;
80 else
81 NumGlobalElements = 101;
82
83 bool success = false;
84 try {
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 // Recompute 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 if (step_number != 1) {
209 if (verbose)
210 p.out() << "Recomputing Preconditioner (reusing graph)" << std::endl;
211
212 PreconditionerPtr->Compute();
213
214 }
215
216 aztecSolverPtr->SetUserMatrix(jacobian_matrix.get(), false);
217 aztecSolverPtr->SetPrecOperator(PreconditionerPtr.get());
218 aztecSolverPtr->SetRHS(rhs_vec.get());
219 aztecSolverPtr->SetLHS(lhs_vec.get());
220
221 aztecSolverPtr->Iterate(400, 1.0e-4);
222
223 solution_vec->Update(1.0, *lhs_vec, 1.0);
224
225 interface->computeF(*solution_vec, *rhs_vec);
226 rhs_vec->Scale(-1.0);
227 interface->computeJacobian(*solution_vec, *jacobian_matrix);
228
229 rhs_vec->Norm2(&norm);
230
231 total_linear_iterations += aztecSolverPtr->NumIters();
232
233 if (norm < 1.0e-8)
234 converged = true;
235 }
236
237 if (verbose) {
238 p.out() << "Final Step " << step_number << ", ||F|| = " << norm << std::endl;
239 if (converged)
240 p.out() << "Converged!!" << std::endl;
241 else
242 p.out() << "Failed!!" << std::endl;
243 }
244
245 // Tests
246 int status = 0; // Converged
247
248 if (verbose)
249 p.out() << "Total Number of Linear Iterations = "
250 << total_linear_iterations << std::endl;
251
252 if (Comm.NumProc() == 1 && total_linear_iterations != 10)
253 status = 1;
254
255 if (!converged)
256 status = 2;
257
258 success = status==0;
259
260 // Summarize test results
261 if (converged && status == 0)
262 p.out() << "Test passed!" << std::endl;
263 else
264 p.out() << "Test failed!" << std::endl;
265
266 if (verbose)
267 p.out() << "Status = " << status << std::endl;
268 }
269 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
270
271 #ifdef HAVE_MPI
272 MPI_Finalize();
273 #endif
274
275 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
276 }