Debian Codegraph @

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