Debian Codegraph @

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