Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemScaled.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
50 // Initialize MPI
51 #ifdef HAVE_MPI
52 MPI_Init(&argc,&argv);
53 #endif
54
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 bool verbose = false;
68 if (argc > 1)
69 if (argv[1][0]=='-' && argv[1][1]=='v')
70 verbose = true;
71
72 // Get the number of elements from the command line
73 int NumGlobalElements = 0;
74 if ((argc > 2) && (verbose))
75 NumGlobalElements = atoi(argv[2]) + 1;
76 else if ((argc > 1) && (!verbose))
77 NumGlobalElements = atoi(argv[1]) + 1;
78 else
79 NumGlobalElements = 101;
80
81 bool success = false;
82 try {
83 // The number of unknowns must be at least equal to the
84 // number of processors.
85 if (NumGlobalElements < NumProc) {
86 std::cout << "numGlobalBlocks = " << NumGlobalElements
87 << " cannot be < number of processors = " << NumProc << std::endl;
88 std::cout << "Test failed!" << std::endl;
89 throw std::runtime_error("NOX Error");
90 }
91
92 // Create the interface between NOX and the application
93 // This object is derived from NOX::Epetra::Interface
94 Teuchos::RCP<Interface> interface =
95 Teuchos::rcp(new Interface(NumGlobalElements, Comm));
96
97 // Set the PDE factor (for nonlinear forcing term). This could be specified
98 // via user input.
99 interface->setPDEfactor(1000.0);
100
101 // Use a scaled vector space. The scaling must also be registered
102 // with the linear solver so the linear system is consistent!
103 Teuchos::RCP<Epetra_Vector> scaleVec =
104 Teuchos::rcp(new Epetra_Vector( *(interface->getSolution())));
105 scaleVec->PutScalar(2.0);
106 Teuchos::RCP<NOX::Epetra::Scaling> scaling =
107 Teuchos::rcp(new NOX::Epetra::Scaling);
108 scaling->addUserScaling(NOX::Epetra::Scaling::Left, scaleVec);
109
110 // Use a weighted vector space for scaling all norms
111 Teuchos::RCP<NOX::Epetra::VectorSpace> weightedVectorSpace =
112 Teuchos::rcp(new NOX::Epetra::VectorSpaceScaledL2(scaling));
113
114 // Get the vector from the Problem
115 Teuchos::RCP<Epetra_Vector> soln = interface->getSolution();
116 Teuchos::RCP<NOX::Epetra::Vector> noxSoln =
117 Teuchos::rcp(new NOX::Epetra::Vector(soln,
118 NOX::Epetra::Vector::CreateCopy,
119 NOX::DeepCopy,
120 weightedVectorSpace));
121
122 // Begin Nonlinear Solver ************************************
123
124 // Create the top level parameter list
125 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
126 Teuchos::rcp(new Teuchos::ParameterList);
127 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
128
129 // Set the nonlinear solver method
130 nlParams.set("Nonlinear Solver", "Line Search Based");
131
132 // Set the printing parameters in the "Printing" sublist
133 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
134 printParams.set("MyPID", MyPID);
135 printParams.set("Output Precision", 3);
136 printParams.set("Output Processor", 0);
137 if (verbose)
138 printParams.set("Output Information",
139 NOX::Utils::OuterIteration +
140 NOX::Utils::OuterIterationStatusTest +
141 NOX::Utils::InnerIteration +
142 NOX::Utils::LinearSolverDetails +
143 NOX::Utils::Parameters +
144 NOX::Utils::Details +
145 NOX::Utils::Warning +
146 NOX::Utils::Debug +
147 NOX::Utils::TestDetails +
148 NOX::Utils::Error);
149 else
150 printParams.set("Output Information", NOX::Utils::Error +
151 NOX::Utils::TestDetails);
152
153 // Create a print class for controlling output below
154 NOX::Utils printing(printParams);
155
156 // Sublist for line search
157 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
158 searchParams.set("Method", "Full Step");
159
160 // Sublist for direction
161 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
162 dirParams.set("Method", "Newton");
163 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
164 newtonParams.set("Forcing Term Method", "Constant");
165
166 // Sublist for linear solver for the Newton method
167 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
168 lsParams.set("Aztec Solver", "GMRES");
169 lsParams.set("Max Iterations", 800);
170 lsParams.set("Tolerance", 1e-4);
171
172 // Various Preconditioner options
173 //lsParams.set("Preconditioner", "AztecOO");
174 lsParams.set("Preconditioner", "Ifpack");
175 lsParams.set("Preconditioner Reuse Policy", "Reuse");
176 lsParams.set("Max Age Of Prec", 5);
177
178 // Add a user defined pre/post operator object
179 Teuchos::RCP<NOX::Abstract::PrePostOperator> ppo =
180 Teuchos::rcp(new UserPrePostOperator(printing));
181 nlParams.sublist("Solver Options").set("User Defined Pre/Post Operator",
182 ppo);
183
184 // Let's force all status tests to do a full check
185 nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete");
186
187 // User supplied (Epetra_RowMatrix)
188 Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian();
189
190 // Create the linear system
191 Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface;
192 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
193 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
194 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
195 iReq,
196 iJac, Analytic,
197 *noxSoln,
198 scaling));
199
200 // Create the Group
201 Teuchos::RCP<NOX::Epetra::Group> grpPtr =
202 Teuchos::rcp(new NOX::Epetra::Group(printParams,
203 iReq,
204 *noxSoln,
205 linSys));
206 NOX::Epetra::Group& grp = *grpPtr;
207
208 // uncomment the following for loca supergroups
209 //MF->setGroupForComputeF(*grpPtr);
210 //FD->setGroupForComputeF(*grpPtr);
211
212 // Create the convergence tests
213 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
214 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
215 Teuchos::RCP<NOX::StatusTest::NormF> relresid =
216 Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2));
217 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
218 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
219 Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms =
220 Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8));
221 Teuchos::RCP<NOX::StatusTest::Combo> converged =
222 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
223 converged->addStatusTest(absresid);
224 converged->addStatusTest(relresid);
225 converged->addStatusTest(wrms);
226 converged->addStatusTest(update);
227 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
228 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
229 Teuchos::RCP<NOX::StatusTest::FiniteValue> fv =
230 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
231 Teuchos::RCP<NOX::StatusTest::Combo> combo =
232 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
233 combo->addStatusTest(fv);
234 combo->addStatusTest(converged);
235 combo->addStatusTest(maxiters);
236
237 // Create the solver
238 Teuchos::RCP<NOX::Solver::Generic> solver =
239 NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr);
240 NOX::StatusTest::StatusType solvStatus = solver->solve();
241
242 // End Nonlinear Solver **************************************
243
244 // Get the Epetra_Vector with the final solution from the solver
245 const NOX::Epetra::Group& finalGroup =
246 dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup());
247 const Epetra_Vector& finalSolution =
248 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).
249 getEpetraVector();
250
251 // Output the parameter list
252 if (verbose) {
253 if (printing.isPrintType(NOX::Utils::Parameters)) {
254 printing.out() << std::endl << "Final Parameters" << std::endl
255 << "****************" << std::endl;
256 solver->getList().print(printing.out());
257 printing.out() << std::endl;
258 }
259 }
260
261 // Print solution
262 char file_name[25];
263 FILE *ifp;
264 int NumMyElements = soln->Map().NumMyElements();
265 (void) sprintf(file_name, "output.%d",MyPID);
266 ifp = fopen(file_name, "w");
267 for (int i=0; i<NumMyElements; i++)
268 fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]);
269 fclose(ifp);
270
271
272 // Tests
273 int status = 0; // Converged
274
275 // 1. Convergence
276 if (solvStatus != NOX::StatusTest::Converged) {
277 status = 1;
278 if (printing.isPrintType(NOX::Utils::Error))
279 printing.out() << "Nonlinear solver failed to converge!" << std::endl;
280 }
281 #ifndef HAVE_MPI
282 // 2. Linear solve iterations (53) - SERIAL TEST ONLY!
283 // The number of linear iterations changes with # of procs.
284 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 53) {
285 status = 2;
286 }
287 #endif
288 // 3. Nonlinear solve iterations (10)
289 if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10)
290 status = 3;
291 // 4. Test the pre/post iterate options
292 {
293 UserPrePostOperator & ppo2 =
294 dynamic_cast<UserPrePostOperator&>(*ppo.get());
295 if (ppo2.getNumRunPreIterate() != 10)
296 status = 4;
297 if (ppo2.getNumRunPostIterate() != 10)
298 status = 4;
299 if (ppo2.getNumRunPreSolve() != 1)
300 status = 4;
301 if (ppo2.getNumRunPostSolve() != 1)
302 status = 4;
303 }
304
305 success = status==0;
306
307 // Summarize test results
308 if (success)
309 printing.out() << "Test passed!" << std::endl;
310 else
311 printing.out() << "Test failed!" << std::endl;
312 }
313 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
314
315 #ifdef HAVE_MPI
316 MPI_Finalize();
317 #endif
318
319 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
320 }