Debian Codegraph @

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