Debian Codegraph @

packages/nox/test/epetra/FiniteDifferenceColoring/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 - Requires EpetraExt
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 #include "NOX_Common.H" // needed to determine HAVE_NOX_EPETRAEXT
21 #include "Teuchos_StandardCatchMacros.hpp"
22
23 // Ensure we have the required EpetraExt library built
24
25 #ifndef HAVE_NOX_EPETRAEXT
26
27 #ifdef HAVE_MPI
28 #include "Epetra_MpiComm.h"
29 #else
30 #include "Epetra_SerialComm.h"
31 #endif
32
33 int main(int argc, char *argv[])
34 {
35 // Initialize MPI
36 #ifdef HAVE_MPI
37 MPI_Init(&argc,&argv);
38 #endif
39
40 // Create a communicator for Epetra objects
41 #ifdef HAVE_MPI
42 Epetra_MpiComm Comm( MPI_COMM_WORLD );
43 #else
44 Epetra_SerialComm Comm;
45 #endif
46
47 // Summarize test results
48 if( Comm.MyPID() == 0 )
49 std::cout << "Test failed! This test requires the EpetraExt library." << std::endl;
50
51 #ifdef HAVE_MPI
52 MPI_Finalize() ;
53 #endif
54
55 int ierr = -1;
56
57 // Final return value (0 = successfull, non-zero = failure)
58 return ierr ;
59 }
60
61 #else // The real test
62
63 #include "EpetraExt_MapColoring.h"
64 #include "EpetraExt_MapColoringIndex.h"
65
66 // NOX Objects
67 #include "NOX.H"
68 #include "NOX_Epetra.H"
69
70 // Trilinos Objects
71 #ifdef HAVE_MPI
72 #include "Epetra_MpiComm.h"
73 #else
74 #include "Epetra_SerialComm.h"
75 #endif
76 #include "Epetra_Map.h"
77 #include "Epetra_MapColoring.h"
78 #include "Epetra_Vector.h"
79 #include "Epetra_IntVector.h"
80 #include "Epetra_RowMatrix.h"
81 #include "Epetra_CrsMatrix.h"
82 #include "Epetra_Map.h"
83 #include "Epetra_LinearProblem.h"
84 #include "AztecOO.h"
85
86 #include <vector>
87
88 // User's application specific files
89 #include "Problem_Interface.H" // Interface file to NOX
90 #include "FiniteElementProblem.H"
91
92 using namespace std;
93
94 int main(int argc, char *argv[])
95 {
96 int i = 0;
97
98 // Initialize MPI
99 #ifdef HAVE_MPI
100 MPI_Init(&argc,&argv);
101 #endif
102
103 // Create a communicator for Epetra objects
104 #ifdef HAVE_MPI
105 Epetra_MpiComm Comm( MPI_COMM_WORLD );
106 #else
107 Epetra_SerialComm Comm;
108 #endif
109
110 // Get the process ID and the total number of processors
111 int MyPID = Comm.MyPID();
112 int NumProc = Comm.NumProc();
113
114 // Check verbosity level
115 bool verbose = false;
116 if (argc > 1)
117 if (argv[1][0]=='-' && argv[1][1]=='v')
118 verbose = true;
119
120 // Get the number of elements from the command line
121 int NumGlobalElements = 0;
122 if ((argc > 2) && (verbose))
123 NumGlobalElements = atoi(argv[2]) + 1;
124 else if ((argc > 1) && (!verbose))
125 NumGlobalElements = atoi(argv[1]) + 1;
126 else
127 NumGlobalElements = 101;
128
129 bool success = false;
130 try {
131 // The number of unknowns must be at least equal to the
132 // number of processors.
133 if (NumGlobalElements < NumProc) {
134 std::cout << "Error: numGlobalBlocks = " << NumGlobalElements
135 << " cannot be < number of processors = " << NumProc << std::endl;
136 throw;
137 }
138
139 // Create the FiniteElementProblem class. This creates all required
140 // Epetra objects for the problem and allows calls to the
141 // function (RHS) and Jacobian evaluation routines.
142 FiniteElementProblem Problem(NumGlobalElements, Comm);
143
144 // Get the vector from the Problem
145 Teuchos::RCP<Epetra_Vector> soln = Problem.getSolution();
146 NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView);
147
148 // Initialize Solution
149 soln->PutScalar(1.0);
150
151 // Begin Nonlinear Solver ************************************
152
153 // Create the top level parameter list
154 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
155 Teuchos::rcp(new Teuchos::ParameterList);
156 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
157
158 // Set the nonlinear solver method
159 nlParams.set("Nonlinear Solver", "Line Search Based");
160 //nlParams.set("Nonlinear Solver", "Trust Region Based");
161
162 // Set the printing parameters in the "Printing" sublist
163 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
164 printParams.set("MyPID", MyPID);
165 printParams.set("Output Precision", 3);
166 printParams.set("Output Processor", 0);
167 if (verbose)
168 printParams.set("Output Information",
169 NOX::Utils::OuterIteration +
170 NOX::Utils::OuterIterationStatusTest +
171 NOX::Utils::InnerIteration +
172 NOX::Utils::Parameters +
173 NOX::Utils::Details +
174 NOX::Utils::Warning);
175 else
176 printParams.set("Output Information", NOX::Utils::Error +
177 NOX::Utils::TestDetails);
178
179 // Sublist for line search
180 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
181 searchParams.set("Method", "Full Step");
182
183 // Sublist for direction
184 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
185 dirParams.set("Method", "Newton");
186 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
187 newtonParams.set("Forcing Term Method", "Constant");
188
189 // Sublist for linear solver for the Newton method
190 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
191 lsParams.set("Aztec Solver", "GMRES");
192 lsParams.set("Max Iterations", 800);
193 lsParams.set("Tolerance", 1e-4);
194 lsParams.set("Output Frequency", 0);
195 lsParams.set("Preconditioner", "Ifpack");
196 lsParams.set("Max Age Of Prec", 5);
197
198 // Create the interface between the test problem and the nonlinear solver
199 // This is created by the user using inheritance of the abstract base class:
200 // NLS_PetraGroupInterface
201 Teuchos::RCP<Problem_Interface> interface =
202 Teuchos::rcp(new Problem_Interface(Problem));
203
204 // Create the Epetra_RowMatrix using Finite Difference with Coloring
205 bool verbose_ = false;
206 EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType =
207 EpetraExt::CrsGraph_MapColoring::GREEDY;
208
209 EpetraExt::CrsGraph_MapColoring tmpMapColoring( algType, verbose_ );
210
211 Teuchos::RCP<Epetra_MapColoring> colorMap =
212 Teuchos::rcp(&tmpMapColoring(*(Problem.getGraph())));
213
214 EpetraExt::CrsGraph_MapColoringIndex colorMapIndex(*colorMap);
215
216 Teuchos::RCP< std::vector<Epetra_IntVector> > columns =
217 Teuchos::rcp(&colorMapIndex(*(Problem.getGraph())));
218
219 // Use this constructor to create the graph numerically as a means of timing
220 // the old way of looping without colors :
221 // NOX::Epetra::FiniteDifferenceColoring A(interface, soln,
222 // *colorMap, *columns);
223 // Or use this as the standard way of using finite differencing with coloring
224 // where the application is responsible for creating the matrix graph
225 // beforehand, ie as is done in Problem.
226 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
227 Teuchos::RCP<NOX::Epetra::FiniteDifferenceColoring> A =
228 Teuchos::rcp(new NOX::Epetra::FiniteDifferenceColoring(printParams,
229 iReq, noxSoln,
230 Problem.getGraph(),
231 colorMap,
232 columns,
233 false));
234
235 // Create the linear system
236 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
237 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
238 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
239 iReq, iJac, A, noxSoln));
240 // iReq, ( Teuchos::RCP<NOX::Epetra::Interface::Jacobian>) A, A, noxSoln));
241
242 // Create the Group
243 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
244 Teuchos::rcp(new NOX::Epetra::Group(printParams,
245 iReq,
246 noxSoln,
247 linSys));
248 NOX::Epetra::Group& grp = *(grpPtr.get());
249
250 // ATOL vector if using NOX::StatusTest::WRMS
251 NOX::Epetra::Vector weights(soln);
252 weights.scale(1.0e-8);
253
254 // Create the convergence tests
255 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
256 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
257 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
258 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
259 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
260 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
261 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
262 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
263 Teuchos::RCP<NOX::StatusTest::Combo> converged =
264 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
265 converged->addStatusTest(absresid);
266 converged->addStatusTest(relresid);
267 converged->addStatusTest(wrms);
268 converged->addStatusTest(update);
269 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
270 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
271 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
272 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
273 Teuchos::RCP<NOX::StatusTest::Combo> combo =
274 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
275 combo->addStatusTest(fv);
276 combo->addStatusTest(converged);
277 combo->addStatusTest(maxiters);
278
279 // Create the method
280 Teuchos::RCP<NOX::Solver::Generic> solver =
281 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
282 NOX::StatusTest::StatusType status = solver->solve();
283
284 if (verbose) {
285 if (status != NOX::StatusTest::Converged)
286 if (MyPID==0)
287 cout << "Nonlinear solver failed to converge!" << std::endl;
288 }
289
290 // Get the Epetra_Vector with the final solution from the solver
291 const NOX::Epetra::Group& finalGroup = dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
292 const Epetra_Vector& finalSolution = (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
293
294 // End Nonlinear Solver **************************************
295
296 // Output the parameter list
297 if (verbose) {
298 NOX::Utils utils(printParams);
299 if (utils.isPrintType(NOX::Utils::Parameters)) {
300 std::cout << std::endl << "Final Parameters" << std::endl
301 << "****************" << std::endl;
302 solver->getList().print(cout);
303 std::cout << std::endl;
304 }
305 }
306
307 // Print solution
308 char file_name[25];
309 FILE *ifp;
310 int NumMyElements = soln->Map().NumMyElements();
311 (void) sprintf(file_name, "output.%d",MyPID);
312 ifp = fopen(file_name, "w");
313 for (i=0; i<NumMyElements; i++)
314 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
315 fclose(ifp);
316
317 success = (status == NOX::StatusTest::Converged);
318
319 // Summarize test results
320 if (Comm.MyPID() == 0) {
321 if (success)
322 std::cout << "Test passed!" << std::endl;
323 else
324 std::cout << "Test failed!" << std::endl;
325 }
326 }
327 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
328
329 #ifdef HAVE_MPI
330 MPI_Finalize() ;
331 #endif
332
333 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
334 }
335 #endif