Debian Codegraph @

packages/nox/test/epetra/1Dfem/1Dfem.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 #include "1DfemPrePostOperator.H"
41
42 #include "Teuchos_ParameterList.hpp"
43 #include "Teuchos_StandardCatchMacros.hpp"
44
45 using namespace std;
46
47 int main(int argc, char *argv[])
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 // Get the process ID and the total number of processors
62 int MyPID = Comm.MyPID();
63 int NumProc = Comm.NumProc();
64
65 // Check verbosity level
66 bool verbose = false;
67 if (argc > 1)
68 if (argv[1][0]=='-' && argv[1][1]=='v')
69 verbose = true;
70
71 // Get the number of elements from the command line
72 int NumGlobalElements = 0;
73 if ((argc > 2) && (verbose))
74 NumGlobalElements = atoi(argv[2]) + 1;
75 else if ((argc > 1) && (!verbose))
76 NumGlobalElements = atoi(argv[1]) + 1;
77 else
78 NumGlobalElements = 101;
79
80 bool success = false;
81 try {
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", "None");
161 //lsParams.set("Preconditioner", "AztecOO");
162 lsParams.set("Preconditioner", "New Ifpack");
163 lsParams.set("Preconditioner Reuse Policy", "Reuse");
164 //lsParams.set("Preconditioner Reuse Policy", "Recompute");
165 //lsParams.set("Preconditioner Reuse Policy", "Rebuild");
166 lsParams.set("Max Age Of Prec", 5);
167
168 // Add a user defined pre/post operator object
169 Teuchos::RCP<NOX::Abstract::PrePostOperator> ppo =
170 Teuchos::rcp(new UserPrePostOperator(printing));
171 nlParams.sublist("Solver Options").set("User Defined Pre/Post Operator",
172 ppo);
173
174 // Let's force all status tests to do a full check
175 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
176
177 // Create all possible Epetra_Operators.
178 // 1. User supplied (Epetra_RowMatrix)
179 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
180 // 2. Matrix-Free (Epetra_Operator)
181 Teuchos::RCP<NOX::Epetra::MatrixFree> MF =
182 Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface,
183 *noxSoln));
184 // 3. Finite Difference (Epetra_RowMatrix)
185 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD =
186 Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface,
187 *soln));
188
189 // Create the linear system
190 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
191 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF;
192 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD;
193 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
194 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
195 //interface,
196 iJac, MF,
197 iPrec, FD,
198 *soln));
199
200 // Create the Group
201 NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView);
202 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
203 Teuchos::rcp(new NOX::Epetra::Group(printParams,
204 iReq,
205 initialGuess,
206 linSys));
207 NOX::Epetra::Group& grp = *grpPtr;
208
209 // uncomment the following for loca supergroups
210 //MF->setGroupForComputeF(*grpPtr);
211 //FD->setGroupForComputeF(*grpPtr);
212
213 // Create the convergence tests
214 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
215 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
216 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
217 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
218 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
219 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
220 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
221 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
222 Teuchos::RCP<NOX::StatusTest::Combo> converged =
223 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
224 converged->addStatusTest(absresid);
225 converged->addStatusTest(relresid);
226 converged->addStatusTest(wrms);
227 converged->addStatusTest(update);
228 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
229 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
230 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
231 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
232 Teuchos::RCP<NOX::StatusTest::Combo> combo =
233 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
234 combo->addStatusTest(fv);
235 combo->addStatusTest(converged);
236 combo->addStatusTest(maxiters);
237
238 // Create the solver
239 Teuchos::RCP<NOX::Solver::Generic> solver =
240 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
241 NOX::StatusTest::StatusType solvStatus = solver->solve();
242
243 // End Nonlinear Solver **************************************
244
245 // Get the Epetra_Vector with the final solution from the solver
246 const NOX::Epetra::Group& finalGroup =
247 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
248 const Epetra_Vector& finalSolution =
249 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
250 getEpetraVector();
251
252 // Output the parameter list
253 if (verbose) {
254 if (printing.isPrintType(NOX::Utils::Parameters)) {
255 printing.out() << std::endl << "Final Parameters" << std::endl
256 << "****************" << std::endl;
257 solver->getList().print(printing.out());
258 printing.out() << std::endl;
259 }
260 }
261
262 // Print solution
263 char file_name[25];
264 FILE *ifp;
265 int NumMyElements = soln->Map().NumMyElements();
266 (void) sprintf(file_name, "output.%d",MyPID);
267 ifp = fopen(file_name, "w");
268 for (int i=0; i<NumMyElements; i++)
269 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
270 fclose(ifp);
271
272
273 // Tests
274 int status = 0; // Converged
275
276 // 1. Convergence
277 if (solvStatus != NOX::StatusTest::Converged) {
278 status = 1;
279 if (printing.isPrintType(NOX::Utils::Error))
280 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
281 }
282 #ifndef HAVE_MPI
283 // 2. Linear solve iterations (53) - SERIAL TEST ONLY!
284 // The number of linear iterations changes with # of procs.
285 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 53) {
286 status = 2;
287 }
288 #endif
289 // 3. Nonlinear solve iterations (10)
290 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
291 status = 3;
292 // 4. Test the pre/post iterate options
293 {
294 UserPrePostOperator & ppo2 =
295 dynamic_cast<UserPrePostOperator&>(*ppo.get());
296 if (ppo2.getNumRunPreIterate() != 10)
297 status = 4;
298 if (ppo2.getNumRunPostIterate() != 10)
299 status = 4;
300 if (ppo2.getNumRunPreSolve() != 1)
301 status = 4;
302 if (ppo2.getNumRunPostSolve() != 1)
303 status = 4;
304 if (ppo2.getNumRunPreLineSearch() != 10)
305 status = 4;
306 if (ppo2.getNumRunPostLineSearch() != 10)
307 status = 4;
308 }
309
310 success = status==0;
311
312 // Summarize test results
313 if (success)
314 printing.out() << "Test passed!" << std::endl;
315 else
316 printing.out() << "Test failed!" << std::endl;
317
318 printing.out() << "Status = " << status << std::endl;
319 }
320 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
321
322 #ifdef HAVE_MPI
323 MPI_Finalize();
324 #endif
325
326 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
327 }