Debian Codegraph @

packages/nox/examples/epetra/NOX_1DFEMNonlinear/Example.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 #include "Teuchos_StandardCatchMacros.hpp"
38
39 // User's application specific files
40 #include "Problem_Interface.H" // Interface file to NOX
41 #include "FiniteElementProblem.H"
42
43 // Required for reading and writing parameter lists from xml format
44 // Configure Trilinos with --enable-teuchos-extended
45 #ifdef HAVE_TEUCHOS_EXTENDED
46 #include "Teuchos_XMLParameterListHelpers.hpp"
47 #include <sstream>
48 #endif
49
50 using namespace std;
51
52 int main(int argc, char *argv[])
53 {
54 // Initialize MPI
55 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
56
57 bool success = false;
58 bool verbose = false;
59 try {
60 // Create a communicator for Epetra objects
61 #ifdef HAVE_MPI
62 Epetra_MpiComm Comm( MPI_COMM_WORLD );
63 #else
64 Epetra_SerialComm Comm;
65 #endif
66
67 int ierr = 0, i;
68
69 // Get the process ID and the total number of processors
70 int MyPID = Comm.MyPID();
71 int NumProc = Comm.NumProc();
72
73 // Get the number of elements from the command line
74 int NumGlobalElements = 100 + 1;
75
76 // The number of unknowns must be at least equal to the
77 // number of processors.
78 if (NumGlobalElements < NumProc) {
79 std::cout << "numGlobalBlocks = " << NumGlobalElements
80 << " cannot be < number of processors = " << NumProc << std::endl;
81 exit(1);
82 }
83
84 // Create the FiniteElementProblem class. This creates all required
85 // Epetra objects for the problem and allows calls to the
86 // function (RHS) and Jacobian evaluation routines.
87 FiniteElementProblem Problem(NumGlobalElements, Comm);
88
89 // Get the vector from the Problem
90 Teuchos::RCP<Epetra_Vector> soln = Problem.getSolution();
91 NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView);
92
93 // Initialize Solution
94 soln->PutScalar(1.0);
95
96 // Begin Nonlinear Solver ************************************
97
98 // Create the top level parameter list
99 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
100 Teuchos::rcp(new Teuchos::ParameterList);
101 Teuchos::ParameterList& nlParams = *nlParamsPtr.get();
102
103 // Set the nonlinear solver method
104 nlParams.set("Nonlinear Solver", "Line Search Based");
105 //nlParams.set("Nonlinear Solver", "Trust Region Based");
106
107 // Set the printing parameters in the "Printing" sublist
108 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
109 printParams.set("MyPID", MyPID);
110 printParams.set("Output Precision", 3);
111 printParams.set("Output Processor", 0);
112 printParams.set("Output Information",
113 NOX::Utils::OuterIteration +
114 NOX::Utils::OuterIterationStatusTest +
115 NOX::Utils::InnerIteration +
116 NOX::Utils::Parameters +
117 NOX::Utils::Details +
118 NOX::Utils::Warning);
119
120 // Create printing utilities
121 NOX::Utils utils(printParams);
122
123 // Sublist for line search
124 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
125 searchParams.set("Method", "Full Step");
126 //searchParams.set("Method", "Interval Halving");
127 //searchParams.set("Method", "Polynomial");
128 //searchParams.set("Method", "NonlinearCG");
129 //searchParams.set("Method", "Quadratic");
130 //searchParams.set("Method", "More'-Thuente");
131
132 // Sublist for direction
133 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
134 // dirParams.set("Method", "Modified-Newton");
135 // Teuchos::ParameterList& newtonParams = dirParams.sublist("Modified-Newton");
136 // newtonParams.set("Max Age of Jacobian", 2);
137 dirParams.set("Method", "Newton");
138 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
139 newtonParams.set("Forcing Term Method", "Constant");
140 //newtonParams.set("Forcing Term Method", "Type 1");
141 //newtonParams.set("Forcing Term Method", "Type 2");
142 //newtonParams.set("Forcing Term Minimum Tolerance", 1.0e-4);
143 //newtonParams.set("Forcing Term Maximum Tolerance", 0.1);
144 //dirParams.set("Method", "Steepest Descent");
145 //Teuchos::ParameterList& sdParams = dirParams.sublist("Steepest Descent");
146 //sdParams.set("Scaling Type", "None");
147 //sdParams.set("Scaling Type", "2-Norm");
148 //sdParams.set("Scaling Type", "Quadratic Model Min");
149 //dirParams.set("Method", "NonlinearCG");
150 //Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG");
151 //nlcgParams.set("Restart Frequency", 2000);
152 //nlcgParams.set("Precondition", "On");
153 //nlcgParams.set("Orthogonalize", "Polak-Ribiere");
154 //nlcgParams.set("Orthogonalize", "Fletcher-Reeves");
155
156 // Sublist for linear solver for the Newton method
157 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
158 lsParams.set("Aztec Solver", "GMRES");
159 lsParams.set("Max Iterations", 800);
160 lsParams.set("Tolerance", 1e-4);
161 lsParams.set("Preconditioner", "None");
162 //lsParams.set("Preconditioner", "Ifpack");
163 lsParams.set("Max Age Of Prec", 5);
164
165 // Create the interface between the test problem and the nonlinear solver
166 // This is created by the user using inheritance of the abstract base class:
167 // NOX_Epetra_Interface
168 Teuchos::RCP<Problem_Interface> interface =
169 Teuchos::rcp(new Problem_Interface(Problem));
170
171 // Create the Epetra_RowMatrix. Uncomment one or more of the following:
172 // 1. User supplied (Epetra_RowMatrix)
173 Teuchos::RCP<Epetra_RowMatrix> Analytic = Problem.getJacobian();
174 // 2. Matrix-Free (Epetra_Operator)
175 Teuchos::RCP<NOX::Epetra::MatrixFree> MF =
176 Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface, noxSoln));
177 // 3. Finite Difference (Epetra_RowMatrix)
178 Teuchos::RCP<NOX::Epetra::FiniteDifference> FD =
179 Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface, noxSoln));
180
181 // Create the linear system
182 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
183 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF;
184 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec =
185 interface;
186 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
187 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
188 iReq, iJac, MF,
189 noxSoln));
190
191 // Create the Group
192 Teuchos::RCP<NOX::Epetra::Group> grp =
193 Teuchos::rcp(new NOX::Epetra::Group(printParams, iReq, noxSoln,
194 linSys));
195
196 // Create the convergence tests
197 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
198 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
199 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
200 Teuchos::rcp(new NOX::StatusTest::NormF(*grp.get(), 1.0e-2));
201 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
202 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
203 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
204 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
205 Teuchos::RCP<NOX::StatusTest::Combo> converged =
206 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
207 converged->addStatusTest(absresid);
208 converged->addStatusTest(relresid);
209 converged->addStatusTest(wrms);
210 converged->addStatusTest(update);
211 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
212 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
213 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
214 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
215 Teuchos::RCP<NOX::StatusTest::Combo> combo =
216 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
217 combo->addStatusTest(fv);
218 combo->addStatusTest(converged);
219 combo->addStatusTest(maxiters);
220
221 #ifdef HAVE_TEUCHOS_EXTENDED
222 std::stringstream input_file_name;
223 input_file_name << "input_" << MyPID << ".xml";
224
225 // Write the parameter list to a file
226 std::cout << "Writing parameter list to "<< input_file_name.str() << std::endl;
227 Teuchos::writeParameterListToXmlFile(*nlParamsPtr, input_file_name.str());
228
229 // Read in the parameter list from a file
230 std::cout << "Reading parameter list from " << input_file_name.str() << std::endl;
231 Teuchos::RCP<Teuchos::ParameterList> finalParamsPtr =
232 Teuchos::rcp(new Teuchos::ParameterList);
233 Teuchos::updateParametersFromXmlFile(input_file_name.str(),
234 outArg(*finalParamsPtr));
235 #else
236 Teuchos::RCP<Teuchos::ParameterList> finalParamsPtr = nlParamsPtr;
237 #endif
238
239 // Create the method
240 Teuchos::RCP<NOX::Solver::Generic> solver =
241 NOX::Solver::buildSolver(grp, combo, finalParamsPtr);
242 NOX::StatusTest::StatusType status = solver->solve();
243
244 if (status == NOX::StatusTest::Converged)
245 utils.out() << "Test Passed!" << std::endl;
246 else {
247 if (MyPID==0)
248 utils.out() << "Nonlinear solver failed to converge!" << std::endl;
249 }
250
251 // Get the Epetra_Vector with the final solution from the solver
252 const NOX::Epetra::Group& finalGroup = dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
253 const Epetra_Vector& finalSolution = (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
254
255 // End Nonlinear Solver **************************************
256
257 // Output the parameter list
258 if (utils.isPrintType(NOX::Utils::Parameters)) {
259 utils.out() << std::endl << "Final Parameters" << std::endl
260 << "****************" << std::endl;
261 solver->getList().print(utils.out());
262 utils.out() << std::endl;
263 }
264
265 // Print solution
266 char file_name[25];
267 FILE *ifp;
268 int NumMyElements = soln->Map().NumMyElements();
269 (void) sprintf(file_name, "output.%d",MyPID);
270 ifp = fopen(file_name, "w");
271 for (i=0; i<NumMyElements; i++)
272 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
273 fclose(ifp);
274
275 success = ierr==0;
276 }
277 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
278
279 /* end main
280 */
281 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
282 }