Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemStratimikosReuse.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 #include "NOX_Epetra_LinearSystem_Stratimikos.H"
39
40 // User's application specific files
41 #include "1DfemInterface.H"
42
43 #include "Teuchos_ParameterList.hpp"
44 #include "Teuchos_StandardCatchMacros.hpp"
45
46 using namespace std;
47
48 int main(int argc, char *argv[])
49 {
50
51 // Initialize MPI
52 #ifdef HAVE_MPI
53 MPI_Init(&argc,&argv);
54 #endif
55
56 // Create a communicator for Epetra objects
57 #ifdef HAVE_MPI
58 Epetra_MpiComm Comm( MPI_COMM_WORLD );
59 #else
60 Epetra_SerialComm Comm;
61 #endif
62
63 // Get the process ID and the total number of processors
64 int MyPID = Comm.MyPID();
65 int NumProc = Comm.NumProc();
66
67 // Check verbosity level
68 bool verbose = false;
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
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 // Get the vector from the Problem
100 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
101 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
102 Teuchos::rcp(new NOX::Epetra::Vector(soln,
103 NOX::Epetra::Vector::CreateView));
104
105 // Set the PDE factor (for nonlinear forcing term). This could be specified
106 // via user input.
107 interface->setPDEfactor(1000.0);
108
109 // Set the initial guess
110 soln->PutScalar(1.0);
111
112 // Begin Nonlinear Solver ************************************
113
114 // Create the top level parameter list
115 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
116 Teuchos::rcp(new Teuchos::ParameterList);
117 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
118
119 // Set the nonlinear solver method
120 nlParams.set("Nonlinear Solver", "Line Search Based");
121
122 // Set the printing parameters in the "Printing" sublist
123 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
124 printParams.set("MyPID", MyPID);
125 printParams.set("Output Precision", 3);
126 printParams.set("Output Processor", 0);
127 if (verbose)
128 printParams.set("Output Information",
129 NOX::Utils::OuterIteration +
130 NOX::Utils::OuterIterationStatusTest +
131 NOX::Utils::InnerIteration +
132 NOX::Utils::LinearSolverDetails +
133 NOX::Utils::Parameters +
134 NOX::Utils::Details +
135 NOX::Utils::Warning +
136 NOX::Utils::Debug +
137 NOX::Utils::TestDetails +
138 NOX::Utils::Error);
139 else
140 printParams.set("Output Information", NOX::Utils::Error +
141 NOX::Utils::TestDetails);
142
143 // Create a print class for controlling output below
144 NOX::Utils printing(printParams);
145
146 // Sublist for line search
147 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
148 searchParams.set("Method", "Full Step");
149
150 // Sublist for direction
151 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
152 dirParams.set("Method", "Newton");
153 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
154 newtonParams.set("Forcing Term Method", "Constant");
155
156 // Alternative linear solver list for Stratimikos
157 Teuchos::ParameterList& stratLinSolParams = newtonParams.sublist("Stratimikos Linear Solver");
158 Teuchos::ParameterList& noxStratParams = stratLinSolParams.sublist("NOX Stratimikos Options");
159 Teuchos::ParameterList& stratParams = stratLinSolParams.sublist("Stratimikos");
160
161 noxStratParams.set("Preconditioner Reuse Policy","Reuse");
162 noxStratParams.set("Max Age Of Prec", 2);
163
164 stratParams.set("Linear Solver Type", "Belos");
165 stratParams.set("Preconditioner Type", "Ifpack");
166 Teuchos::ParameterList& belosParams = stratParams
167 .sublist("Linear Solver Types").sublist("Belos");
168 belosParams.set("Solver Type","Block GMRES");
169 Teuchos::ParameterList& belosgmresParams = belosParams.sublist("Solver Types").sublist("Block GMRES");
170 belosgmresParams.set("Convergence Tolerance", 1.0e-6);
171 belosgmresParams.set("Maximum Iterations", 80);
172 if (verbose) {
173 belosgmresParams.set("Output Frequency",1);
174 belosgmresParams.set("Output Style",1);
175 belosgmresParams.set("Verbosity",33);
176 }
177 else {
178 belosParams.sublist("VerboseObject").set("Verbosity Level","none");
179 belosgmresParams.set("Verbosity",0);
180 }
181
182 // Let's force all status tests to do a full check
183 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
184
185 // Create all possible Epetra_Operators.
186 // 1. User supplied (Epetra_RowMatrix)
187 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
188 // 2. Matrix-Free (Epetra_Operator)
189 Teuchos::RCP<NOX::Epetra::MatrixFree> MF =
190 Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface,
191 *noxSoln));
192 // 3. Finite Difference (Epetra_RowMatrix)
193 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD =
194 Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface,
195 *soln));
196
197 // Create the linear system
198 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
199 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
200 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD;
201 Teuchos::RCP<NOX::Epetra::LinearSystem> linSys =
202 Teuchos::rcp(new NOX::Epetra::LinearSystemStratimikos(printParams, stratLinSolParams,
203 iJac, Analytic,
204 //iPrec, FD,
205 *soln));
206
207 // Create the Group
208 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
209 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
210 Teuchos::rcp(new NOX::Epetra::Group(printParams,
211 iReq,
212 initialGuess,
213 linSys));
214 NOX::Epetra::Group& grp = *grpPtr;
215
216 // uncomment the following for loca supergroups
217 //MF->setGroupForComputeF(*grpPtr);
218 //FD->setGroupForComputeF(*grpPtr);
219
220 // Create the convergence tests
221 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
222 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
223 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
224 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
225 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
226 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
227 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
228 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
229 Teuchos::RCP<NOX::StatusTest::Combo> converged =
230 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
231 converged->addStatusTest(absresid);
232 converged->addStatusTest(relresid);
233 converged->addStatusTest(wrms);
234 converged->addStatusTest(update);
235 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
236 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
237 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
238 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
239 Teuchos::RCP<NOX::StatusTest::Combo> combo =
240 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
241 combo->addStatusTest(fv);
242 combo->addStatusTest(converged);
243 combo->addStatusTest(maxiters);
244
245 // Create the solver
246 Teuchos::RCP<NOX::Solver::Generic> solver =
247 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
248 NOX::StatusTest::StatusType solvStatus = solver->solve();
249
250 // End Nonlinear Solver **************************************
251
252 // Get the Epetra_Vector with the final solution from the solver
253 const NOX::Epetra::Group& finalGroup =
254 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
255 const Epetra_Vector& finalSolution =
256 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
257 getEpetraVector();
258
259 // Output the parameter list
260 if (verbose) {
261 if (printing.isPrintType(NOX::Utils::Parameters)) {
262 printing.out() << std::endl << "Final Parameters" << std::endl
263 << "****************" << std::endl;
264 solver->getList().print(printing.out());
265 printing.out() << std::endl;
266 }
267 }
268
269 // Print solution
270 char file_name[25];
271 FILE *ifp;
272 int NumMyElements = soln->Map().NumMyElements();
273 (void) sprintf(file_name, "output.%d",MyPID);
274 ifp = fopen(file_name, "w");
275 for (int i=0; i<NumMyElements; i++)
276 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
277 fclose(ifp);
278
279
280 // Tests
281 int status = 0; // Converged
282
283 // 1. Convergence
284 if (solvStatus != NOX::StatusTest::Converged) {
285 status = 1;
286 if (printing.isPrintType(NOX::Utils::Error))
287 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
288 }
289
290 // 3. Nonlinear solve iterations (10)
291 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
292 status = 3;
293
294 success = status==0;
295
296 // Summarize test results
297 if (success)
298 printing.out() << "Test passed!" << std::endl;
299 else
300 printing.out() << "Test failed!" << std::endl;
301
302 printing.out() << "Status = " << status << std::endl;
303 }
304 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
305
306 #ifdef HAVE_MPI
307 MPI_Finalize();
308 #endif
309
310 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
311 }