Debian Codegraph @

packages/nox/test/epetra/LOCA_Pitchfork/Pitchfork.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // LOCA: Library of Continuation Algorithms Package
4 //
5 // Copyright 2001-2005 NTESS and the LOCA contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // 1D Finite Element Test Problem
11 /* Solves continuation problem (Parameter c="Right BC")
12 *
13 * d2u
14 * --- + lambda * u - alpha * u**2 + beta * u**3 = 0
15 * dx2
16 *
17 * subject to the boundar conditions u(-1) = u(1) = beta.
18 */
19
20 // LOCA Objects
21 #include "LOCA.H"
22 #include "LOCA_Epetra.H"
23 #include "NOX_TestCompare.H"
24
25 // Trilinos Objects
26 #ifdef HAVE_MPI
27 #include "Epetra_MpiComm.h"
28 #else
29 #include "Epetra_SerialComm.h"
30 #endif
31 #include "Epetra_Map.h"
32 #include "Epetra_Vector.h"
33 #include "Epetra_RowMatrix.h"
34 #include "Epetra_CrsMatrix.h"
35 #include "Epetra_Map.h"
36 #include "Epetra_LinearProblem.h"
37 #include "AztecOO.h"
38
39 // User's application specific files
40 #include "Problem_Interface.H" // Interface file to NOX
41 #include "Pitchfork_FiniteElementProblem.H"
42
43 using namespace std;
44
45 int main(int argc, char *argv[])
46 {
47 int ierr = 0;
48 int MyPID = 0;
49
50 try {
51
52 // Initialize MPI
53 #ifdef HAVE_MPI
54 MPI_Init(&argc,&argv);
55 #endif
56
57 // Create a communicator for Epetra objects
58 #ifdef HAVE_MPI
59 Epetra_MpiComm Comm( MPI_COMM_WORLD );
60 #else
61 Epetra_SerialComm Comm;
62 #endif
63
64 // Get the process ID and the total number of processors
65 MyPID = Comm.MyPID();
66 int NumProc = Comm.NumProc();
67
68 // Check for verbose output
69 bool verbose = false;
70 if (argc>1)
71 if (argv[1][0]=='-' && argv[1][1]=='v')
72 verbose = true;
73
74 // Get the number of elements from the command line
75 int NumGlobalElements = 0;
76 if ((argc > 2) && (verbose))
77 NumGlobalElements = atoi(argv[2]) + 1;
78 else if ((argc > 1) && (!verbose))
79 NumGlobalElements = atoi(argv[1]) + 1;
80 else
81 NumGlobalElements = 101;
82
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 exit(1);
89 }
90
91 int maxNewtonIters = 20;
92 double alpha = 1.0;
93 double beta = 0.0;
94 double lambda = -2.0;
95
96 // Create the FiniteElementProblem class. This creates all required
97 // Epetra objects for the problem and allows calls to the
98 // function (RHS) and Jacobian evaluation routines.
99 Pitchfork_FiniteElementProblem Problem(NumGlobalElements, Comm);
100
101 // Get the vector from the Problem
102 Epetra_Vector& soln = Problem.getSolution();
103
104 // Initialize Solution
105 soln.PutScalar(0.0);
106
107 // Begin LOCA Solver ************************************
108
109 // Create parameter list
110 Teuchos::RCP<Teuchos::ParameterList> paramList =
111 Teuchos::rcp(new Teuchos::ParameterList);
112
113 // Create LOCA sublist
114 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
115
116 // Create the stepper sublist and set the stepper parameters
117 Teuchos::ParameterList& locaStepperList =
118 locaParamsList.sublist("Stepper");
119 locaStepperList.set("Continuation Parameter", "lambda");
120 locaStepperList.set("Initial Value", lambda);
121 locaStepperList.set("Max Value", 4.0);
122 locaStepperList.set("Min Value", -4.0);
123 locaStepperList.set("Max Steps", 50);
124 locaStepperList.set("Max Nonlinear Iterations", maxNewtonIters);
125
126 #ifdef HAVE_LOCA_ANASAZI
127 // Create Anasazi Eigensolver sublist (needs --with-loca-anasazi)
128 locaStepperList.set("Compute Eigenvalues",true);
129 Teuchos::ParameterList& aList = locaStepperList.sublist("Eigensolver");
130 aList.set("Method", "Anasazi");
131 if (!verbose)
132 aList.set("Verbosity", Anasazi::Errors);
133 #else
134 locaStepperList.set("Compute Eigenvalues",false);
135 #endif
136
137 // Create step size sublist
138 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
139 stepSizeList.set("Initial Step Size", -0.1);
140 stepSizeList.set("Min Step Size", 1.0e-3);
141 stepSizeList.set("Max Step Size", 10.0);
142 stepSizeList.set("Aggressiveness", 0.1);
143
144 // Create the "Solver" parameters sublist to be used with NOX Solvers
145 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
146
147 // Create the NOX printing parameter list
148 Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
149 nlPrintParams.set("MyPID", MyPID);
150 if (verbose)
151 nlPrintParams.set("Output Information",
152 NOX::Utils::OuterIteration +
153 NOX::Utils::OuterIterationStatusTest +
154 NOX::Utils::InnerIteration +
155 NOX::Utils::Details +
156 NOX::Utils::Warning +
157 NOX::Utils::TestDetails +
158 NOX::Utils::Error +
159 NOX::Utils::StepperIteration +
160 NOX::Utils::StepperDetails +
161 NOX::Utils::StepperParameters);
162 else
163 nlPrintParams.set("Output Information", NOX::Utils::Error);
164
165 // Create the "Linear Solver" sublist
166 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
167 Teuchos::ParameterList& newParams = dirParams.sublist("Newton");
168 Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver");
169 lsParams.set("Aztec Solver", "GMRES");
170 lsParams.set("Max Iterations", 100);
171 lsParams.set("Tolerance", 1e-4);
172 if (verbose)
173 lsParams.set("Output Frequency", 50);
174 else
175 lsParams.set("Output Frequency", 0);
176 lsParams.set("Scaling", "None");
177 lsParams.set("Preconditioner", "Ifpack");
178
179 // Create and initialize the parameter vector
180 LOCA::ParameterVector pVector;
181 pVector.addParameter("lambda",lambda);
182 pVector.addParameter("alpha", alpha);
183 pVector.addParameter("beta", beta);
184
185 // Create the interface between the test problem and the nonlinear solver
186 // This is created by the user using inheritance of the abstract base
187 // class:
188 Teuchos::RCP<Problem_Interface> interface =
189 Teuchos::rcp(new Problem_Interface(Problem));
190 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface;
191 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
192
193 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
194 Teuchos::RCP<Epetra_RowMatrix> Amat =
195 Teuchos::rcp(&Problem.getJacobian(),false);
196
197 // Create the linear systems
198 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys =
199 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams,
200 lsParams, iReq, iJac,
201 Amat, soln));
202
203 // Create the loca vector
204 NOX::Epetra::Vector locaSoln(soln);
205
206 // Create Epetra factory
207 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
208 Teuchos::rcp(new LOCA::Epetra::Factory);
209
210 // Create global data object
211 Teuchos::RCP<LOCA::GlobalData> globalData =
212 LOCA::createGlobalData(paramList, epetraFactory);
213
214 // Create the Group
215 Teuchos::RCP<LOCA::Epetra::Group> grp =
216 Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams,
217 iReq, locaSoln,
218 linsys, pVector));
219 grp->computeF();
220
221 // Create the Solver convergence test
222 Teuchos::RCP<NOX::StatusTest::NormF> wrms =
223 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
224 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
225 Teuchos::rcp(new NOX::StatusTest::MaxIters(maxNewtonIters));
226 Teuchos::RCP<NOX::StatusTest::Combo> combo =
227 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
228 combo->addStatusTest(wrms);
229 combo->addStatusTest(maxiters);
230
231 // Create the stepper
232 LOCA::Stepper stepper(globalData, grp, combo, paramList);
233 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
234
235 if (status != LOCA::Abstract::Iterator::Finished) {
236 ierr = 1;
237 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
238 globalData->locaUtils->out()
239 << "Stepper failed to converge!" << std::endl;
240 }
241
242 // Get the final solution from the stepper
243 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
244 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
245 const NOX::Epetra::Vector& finalSolution =
246 dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX());
247
248 // Output the parameter list
249 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
250 globalData->locaUtils->out()
251 << std::endl << "Final Parameters" << std::endl
252 << "****************" << std::endl;
253 stepper.getList()->print(globalData->locaUtils->out());
254 globalData->locaUtils->out() << std::endl;
255 }
256
257 // Check some statistics on the solution
258 NOX::TestCompare testCompare(globalData->locaUtils->out(),
259 *(globalData->locaUtils));
260
261 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
262 globalData->locaUtils->out()
263 << std::endl
264 << "***** Checking solution statistics *****"
265 << std::endl;
266
267 // Check number of steps
268 int numSteps = stepper.getStepNumber();
269 int numSteps_expected = 13;
270 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
271 "number of continuation steps",
272 NOX::TestCompare::Absolute);
273
274 // Check number of failed steps
275 int numFailedSteps = stepper.getNumFailedSteps();
276 int numFailedSteps_expected = 0;
277 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
278 "number of failed continuation steps",
279 NOX::TestCompare::Absolute);
280
281 // Check final value of continuation parameter
282 double alpha_final = finalGroup->getParam("lambda");
283 double alpha_expected = -4.0;
284 ierr += testCompare.testValue(alpha_final, alpha_expected, 1.0e-14,
285 "final value of continuation parameter",
286 NOX::TestCompare::Relative);
287
288 // Check norm of solution
289 double norm_x = finalSolution.norm();
290 double norm_x_expected = 0.0;
291 ierr += testCompare.testValue(norm_x, norm_x_expected, 1.0e-7,
292 "norm of final solution",
293 NOX::TestCompare::Relative);
294
295 LOCA::destroyGlobalData(globalData);
296 }
297
298 catch (std::exception& e) {
299 std::cout << e.what() << std::endl;
300 ierr = 1;
301 }
302 catch (const char *s) {
303 std::cout << s << std::endl;
304 ierr = 1;
305 }
306 catch (...) {
307 std::cout << "Caught unknown exception!" << std::endl;
308 ierr = 1;
309 }
310
311 if (MyPID == 0) {
312 if (ierr == 0)
313 std::cout << "All tests passed!" << std::endl;
314 else
315 std::cout << ierr << " test(s) failed!" << std::endl;
316 }
317
318 #ifdef HAVE_MPI
319 MPI_Finalize() ;
320 #endif
321
322 /* end main
323 */
324 return ierr;
325 }