Debian Codegraph @

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