Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemAztecSolverPrecRebuild.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
85 try {
86 // The number of unknowns must be at least equal to the
87 // number of processors.
88 if (NumGlobalElements < NumProc) {
89 std::cout << "numGlobalBlocks = " << NumGlobalElements
90 << " cannot be < number of processors = " << NumProc << std::endl;
91 std::cout << "Test failed!" << std::endl;
92 throw std::runtime_error("NOX Error");
93 }
94
95 // Create the interface between NOX and the application
96 // This object is derived from NOX::Epetra::Interface
97 Teuchos::RCP<Interface> interface =
98 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
99
100 // Set the PDE factor (for nonlinear forcing term). This could be specified
101 // via user input.
102 interface->setPDEfactor(1000.0);
103
104 // Begin Nonlinear Solver ************************************
105
106 // Create the top level parameter list
107 Teuchos::RCP<Teuchos::ParameterList> IfpackParamsPtr =
108 Teuchos::rcp(new Teuchos::ParameterList);
109
110 // Set the printing parameters in the "Printing" sublist
111 Teuchos::ParameterList printParams;
112 printParams.set("MyPID", MyPID);
113 printParams.set("Output Precision", 3);
114 printParams.set("Output Processor", 0);
115 if (verbose)
116 printParams.set("Output Information",
117 NOX::Utils::OuterIteration +
118 NOX::Utils::OuterIterationStatusTest +
119 NOX::Utils::InnerIteration +
120 NOX::Utils::LinearSolverDetails +
121 NOX::Utils::Parameters +
122 NOX::Utils::Details +
123 NOX::Utils::Warning +
124 NOX::Utils::Debug +
125 NOX::Utils::TestDetails +
126 NOX::Utils::Error);
127 else
128 printParams.set("Output Information", NOX::Utils::Error +
129 NOX::Utils::TestDetails);
130
131 // Create a print class for controlling output below
132 NOX::Utils p(printParams);
133
134
135 // *******************************
136 // Setup Test Objects
137 // *******************************
138
139 // Create Linear Objects
140 // Get the vector from the Problem
141 if (verbose)
142 p.out() << "Creating Vectors and Matrices" << std::endl;
143 Teuchos::RCP<Epetra_Vector> solution_vec =
144 interface->getSolution();
145 Teuchos::RCP<Epetra_Vector> rhs_vec =
146 Teuchos::rcp(new Epetra_Vector(*solution_vec));
147 Teuchos::RCP<Epetra_Vector> lhs_vec =
148 Teuchos::rcp(new Epetra_Vector(*solution_vec));
149 Teuchos::RCP<Epetra_CrsMatrix> jacobian_matrix =
150 interface->getJacobian();
151
152
153 if (verbose)
154 p.out() << "Evaluating F and J" << std::endl;
155 solution_vec->PutScalar(1.0);
156 interface->computeF(*solution_vec, *rhs_vec);
157 rhs_vec->Scale(-1.0);
158 interface->computeJacobian(*solution_vec, *jacobian_matrix);
159
160 double norm =0.0;
161 rhs_vec->Norm2(&norm);
162 if (verbose)
163 p.out() << "Step 0, ||F|| = " << norm << std::endl;
164
165 if (verbose)
166 p.out() << "Creating Aztec Solver" << std::endl;
167
168 Teuchos::RCP<AztecOO> aztecSolverPtr = Teuchos::rcp(new AztecOO());
169 if (verbose)
170 aztecSolverPtr->SetAztecOption(AZ_output, AZ_last);
171 else
172 aztecSolverPtr->SetAztecOption(AZ_output, AZ_none);
173
174 // *******************************
175 // Rebuild Test
176 // *******************************
177
178 if (verbose) {
179 p.out() << "**********************************************" << std::endl;
180 p.out() << "Testing Newton solve rebuilding prec each step" << std::endl;
181 p.out() << "**********************************************" << std::endl;
182 }
183
184 int step_number = 0;
185 int max_steps = 20;
186 bool converged = false;
187 int total_linear_iterations = 0;
188 while (norm > 1.0e-8 && step_number < max_steps) {
189
190 step_number++;
191
192 if (verbose)
193 p.out() << "Step " << step_number << ", ||F|| = " << norm << std::endl;
194
195 if (verbose)
196 p.out() << "Creating Ifpack preconditioner" << std::endl;
197
198 Ifpack Factory;
199 Teuchos::RCP<Ifpack_Preconditioner> PreconditionerPtr;
200 PreconditionerPtr = Teuchos::rcp(Factory.Create("ILU",
201 jacobian_matrix.get(),0));
202 Teuchos::ParameterList teuchosParams;
203 PreconditionerPtr->SetParameters(teuchosParams);
204 PreconditionerPtr->Initialize();
205 PreconditionerPtr->Compute();
206
207 aztecSolverPtr->SetUserMatrix(jacobian_matrix.get(), false);
208 aztecSolverPtr->SetPrecOperator(PreconditionerPtr.get());
209 aztecSolverPtr->SetRHS(rhs_vec.get());
210 aztecSolverPtr->SetLHS(lhs_vec.get());
211
212 aztecSolverPtr->Iterate(400, 1.0e-4);
213
214 solution_vec->Update(1.0, *lhs_vec, 1.0);
215
216 interface->computeF(*solution_vec, *rhs_vec);
217 rhs_vec->Scale(-1.0);
218 interface->computeJacobian(*solution_vec, *jacobian_matrix);
219
220 rhs_vec->Norm2(&norm);
221
222 total_linear_iterations += aztecSolverPtr->NumIters();
223
224 if (norm < 1.0e-8)
225 converged = true;
226 }
227
228 if (verbose) {
229 p.out() << "Final Step " << step_number << ", ||F|| = " << norm << std::endl;
230 if (converged)
231 p.out() << "Converged!!" << std::endl;
232 else
233 p.out() << "Failed!!" << std::endl;
234 }
235
236 // Tests
237 int status = 0; // Converged
238
239 if (verbose)
240 p.out() << "Total Number of Linear Iterations = "
241 << total_linear_iterations << std::endl;
242
243 if (Comm.NumProc() == 1 && total_linear_iterations != 10)
244 status = 1;
245
246 if (!converged)
247 status = 2;
248
249 success = (converged && status == 0);
250
251 // Summarize test results
252 if (success)
253 p.out() << "Test passed!" << std::endl;
254 else
255 p.out() << "Test failed!" << std::endl;
256
257 if (verbose)
258 p.out() << "Status = " << status << std::endl;
259 }
260 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
261
262 #ifdef HAVE_MPI
263 MPI_Finalize();
264 #endif
265
266 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
267 }