Debian Codegraph @

packages/nox/test/epetra/LOCA_Tcubed/TcubedTP_stratimikos.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
39 #include "NOX_Epetra_LinearSystem_Stratimikos.H"
40
41 // User's application specific files
42 #include "Problem_Interface.H" // Interface file to NOX
43 #include "Tcubed_FiniteElementProblem.H"
44
45 using namespace std;
46
47 int tcubed_test(int NumGlobalElements, bool verbose, Epetra_Comm& Comm,
48 bool includeUV, bool useP, const std::string& prec,
49 const std::string& prec_method)
50 {
51 int ierr = 0;
52 int MyPID = Comm.MyPID();
53
54 if (MyPID == 0) {
55 std::cout << "********** "
56 << "Testing includeUV = " << includeUV << " useP = " << useP
57 << " Preconditioner = " << prec
58 << " Preconditioner method = " << prec_method
59 << " **********" << std::endl;
60 }
61
62 try {
63
64 double nonlinear_factor = 1.0;
65 double left_bc = 0.0;
66 double right_bc = 2.07;
67
68 // Create the FiniteElementProblem class. This creates all required
69 // Epetra objects for the problem and allows calls to the
70 // function (RHS) and Jacobian evaluation routines.
71 Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm);
72
73 // Get the vector from the Problem
74 Epetra_Vector& soln = Problem.getSolution();
75
76 // Initialize Solution
77 soln.PutScalar(1.0);
78
79 // Create initial guess for the null vector of jacobian
80 // Only needed for Moore-Spence
81 Teuchos::RCP<NOX::Abstract::Vector> nullVec =
82 Teuchos::rcp(new NOX::Epetra::Vector(soln));
83 nullVec->init(1.0); // initial value 1.0
84
85 // Begin LOCA Solver ************************************
86
87 // Create parameter list
88 Teuchos::RCP<Teuchos::ParameterList> paramList =
89 Teuchos::rcp(new Teuchos::ParameterList);
90
91 // Create LOCA sublist
92 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
93
94 // Create the stepper sublist and set the stepper parameters
95 Teuchos::ParameterList& locaStepperList =
96 locaParamsList.sublist("Stepper");
97 locaStepperList.set("Continuation Method", "Arc Length");
98 locaStepperList.set("Continuation Parameter", "Nonlinear Factor");
99 locaStepperList.set("Initial Value", nonlinear_factor);
100 locaStepperList.set("Max Value", 2.0);
101 locaStepperList.set("Min Value", 0.05);
102 locaStepperList.set("Max Steps", 20);
103 locaStepperList.set("Max Nonlinear Iterations", 15);
104
105 locaStepperList.set("Bordered Solver Method", "Nested");
106 Teuchos::ParameterList& nestedList =
107 locaStepperList.sublist("Nested Bordered Solver");
108 nestedList.set("Bordered Solver Method", "Householder");
109 nestedList.set("Include UV In Preconditioner", includeUV);
110 nestedList.set("Use P For Preconditioner", useP);
111
112 // Create bifurcation sublist
113 Teuchos::ParameterList& bifurcationList =
114 locaParamsList.sublist("Bifurcation");
115 bifurcationList.set("Type", "Turning Point");
116 bifurcationList.set("Bifurcation Parameter", "Right BC");
117
118 bifurcationList.set("Formulation", "Minimally Augmented");
119 bifurcationList.set("Symmetric Jacobian", false);
120 bifurcationList.set("Update Null Vectors Every Continuation Step", true);
121 bifurcationList.set("Update Null Vectors Every Nonlinear Iteration",
122 false);
123 bifurcationList.set("Transpose Solver Method",prec);
124 bifurcationList.set("Multiply Null Vectors by Mass Matrix", true);
125
126 // The others don't seem to work with "Solve df/dp"
127 if (prec == "Explicit Transpose")
128 bifurcationList.set("Initial Null Vector Computation", "Solve df/dp");
129 else {
130 bifurcationList.set("Initial A Vector", nullVec);
131 bifurcationList.set("Initial B Vector", nullVec);
132 }
133
134 bifurcationList.set("Bordered Solver Method", "Householder");
135 bifurcationList.set("Include UV In Preconditioner", includeUV);
136 bifurcationList.set("Use P For Preconditioner", useP);
137 bifurcationList.set("Preconditioner Method", prec_method);
138
139 //bifurcationList.set("Formulation", "Moore-Spence");
140 //bifurcationList.set("Solver Method", "Phipps Bordering");
141 //bifurcationList.set("Solver Method", "Salinger Bordering");
142 //bifurcationList.set("Initial Null Vector", nullVec);
143 //bifurcationList.set("Length Normalization Vector", nullVec);
144
145 // Create predictor sublist
146 Teuchos::ParameterList& predictorList =
147 locaParamsList.sublist("Predictor");
148 predictorList.set("Method", "Secant");
149
150 // Create step size sublist
151 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
152 stepSizeList.set("Method", "Adaptive");
153 stepSizeList.set("Initial Step Size", 0.1);
154 stepSizeList.set("Min Step Size", 1.0e-3);
155 stepSizeList.set("Max Step Size", 2000.0);
156 stepSizeList.set("Aggressiveness", 0.1);
157
158 // Create the "Solver" parameters sublist to be used with NOX Solvers
159 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
160
161 // Create the NOX printing parameter list
162 Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
163 nlPrintParams.set("MyPID", MyPID);
164 nlPrintParams.set("Output Precision", 4);
165 if (verbose)
166 nlPrintParams.set("Output Information",
167 NOX::Utils::OuterIteration +
168 NOX::Utils::OuterIterationStatusTest +
169 NOX::Utils::InnerIteration +
170 NOX::Utils::Details +
171 NOX::Utils::LinearSolverDetails +
172 NOX::Utils::Warning +
173 NOX::Utils::TestDetails +
174 NOX::Utils::Error +
175 NOX::Utils::StepperIteration +
176 NOX::Utils::StepperDetails +
177 NOX::Utils::StepperParameters);
178 else
179 nlPrintParams.set("Output Information", NOX::Utils::Error);
180
181 // Create the "Linear Solver" sublist for Newton's method
182 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
183 Teuchos::ParameterList& newParams = dirParams.sublist("Newton");
184 Teuchos::ParameterList& stratLinSolParams = newParams.sublist("Stratimikos Linear Solver");
185 Teuchos::ParameterList& noxStratParams = stratLinSolParams.sublist("NOX Stratimikos Options");
186 Teuchos::ParameterList& stratParams = stratLinSolParams.sublist("Stratimikos");
187
188 noxStratParams.set("Preconditioner Reuse Policy","Rebuild");
189
190 stratParams.set("Linear Solver Type", "AztecOO");
191 stratParams.set("Preconditioner Type", "Ifpack");
192 Teuchos::ParameterList& aztecParams =
193 stratParams.sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve");
194 Teuchos::ParameterList& aztecSettingsParams =
195 aztecParams.sublist("AztecOO Settings");
196 if (verbose)
197 aztecSettingsParams.set("Output Frequency", 50);
198 else
199 stratParams.sublist("Verbose Object").set("Verbosity Level", "none");
200 aztecParams.set("Max Iterations", 200);
201 aztecParams.set("Tolerance", 1e-6);
202
203 // Create and initialize the parameter vector
204 LOCA::ParameterVector pVector;
205 pVector.addParameter("Nonlinear Factor",nonlinear_factor);
206 pVector.addParameter("Left BC", left_bc);
207 pVector.addParameter("Right BC", right_bc);
208
209 // Create the interface between the test problem and the nonlinear solver
210 // This is created by the user using inheritance of the abstract base class
211 Teuchos::RCP<Problem_Interface> interface =
212 Teuchos::rcp(new Problem_Interface(Problem));
213 Teuchos::RCP<LOCA::Epetra::Interface::TimeDependent> iReq = interface;
214 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
215
216 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
217 Teuchos::RCP<Epetra_RowMatrix> Amat =
218 Teuchos::rcp(&Problem.getJacobian(),false);
219
220 // Create scaling object
221 Teuchos::RCP<NOX::Epetra::Scaling> scaling = Teuchos::null;
222 // scaling = Teuchos::rcp(new NOX::Epetra::Scaling);
223 // Teuchos::RCP<Epetra_Vector> scalingVector =
224 // Teuchos::rcp(new Epetra_Vector(soln.Map()));
225 // //scaling->addRowSumScaling(NOX::Epetra::Scaling::Left, scalingVector);
226 // scaling->addColSumScaling(NOX::Epetra::Scaling::Right, scalingVector);
227
228 // Create transpose scaling object
229 // Teuchos::RCP<NOX::Epetra::Scaling> trans_scaling = Teuchos::null;
230 // trans_scaling = Teuchos::rcp(new NOX::Epetra::Scaling);
231 // Teuchos::RCP<Epetra_Vector> transScalingVector =
232 // Teuchos::rcp(new Epetra_Vector(soln.Map()));
233 // trans_scaling->addRowSumScaling(NOX::Epetra::Scaling::Right,
234 // transScalingVector);
235 // trans_scaling->addColSumScaling(NOX::Epetra::Scaling::Left,
236 // transScalingVector);
237 // //bifurcationList.set("Transpose Scaling", trans_scaling);
238
239 // Create the linear systems
240 Teuchos::RCP<NOX::Epetra::LinearSystemStratimikos> linsys =
241 Teuchos::rcp(new NOX::Epetra::LinearSystemStratimikos(nlPrintParams,
242 stratLinSolParams,
243 iJac, Amat,
244 soln,
245 scaling));
246
247 // Create the loca vector
248 NOX::Epetra::Vector locaSoln(soln);
249
250 // Create Epetra factory
251 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
252 Teuchos::rcp(new LOCA::Epetra::Factory);
253
254 // Create global data object
255 Teuchos::RCP<LOCA::GlobalData> globalData =
256 LOCA::createGlobalData(paramList, epetraFactory);
257
258 // Create the Group
259 Teuchos::RCP<LOCA::Epetra::Group> grp =
260 Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, iReq,
261 locaSoln, linsys, linsys,
262 pVector));
263 grp->computeF();
264
265 // Create the Solver convergence test
266 //NOX::StatusTest::NormWRMS wrms(1.0e-2, 1.0e-8);
267 Teuchos::RCP<NOX::StatusTest::NormF> wrms =
268 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-12));
269 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
270 Teuchos::rcp(new NOX::StatusTest::MaxIters(locaStepperList.get("Max Nonlinear Iterations", 10)));
271 Teuchos::RCP<NOX::StatusTest::Combo> combo =
272 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
273 combo->addStatusTest(wrms);
274 combo->addStatusTest(maxiters);
275
276 // Create the stepper
277 LOCA::Stepper stepper(globalData, grp, combo, paramList);
278 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
279
280 if (status != LOCA::Abstract::Iterator::Finished) {
281 ierr = 1;
282 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
283 globalData->locaUtils->out()
284 << "Stepper failed to converge!" << std::endl;
285 }
286
287 // Get the final solution from the stepper
288 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
289 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
290 const NOX::Epetra::Vector& finalSolution =
291 dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX());
292
293 // Output the parameter list
294 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
295 globalData->locaUtils->out()
296 << std::endl << "Final Parameters" << std::endl
297 << "****************" << std::endl;
298 stepper.getList()->print(globalData->locaUtils->out());
299 globalData->locaUtils->out() << std::endl;
300 }
301
302 // Check some statistics on the solution
303 NOX::TestCompare testCompare(globalData->locaUtils->out(),
304 *(globalData->locaUtils));
305
306 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
307 globalData->locaUtils->out()
308 << std::endl
309 << "***** Checking solution statistics *****"
310 << std::endl;
311
312 // Check number of steps
313 int numSteps = stepper.getStepNumber();
314 int numSteps_expected = 7;
315 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
316 "number of continuation steps",
317 NOX::TestCompare::Absolute);
318
319 // Check number of failed steps
320 int numFailedSteps = stepper.getNumFailedSteps();
321 int numFailedSteps_expected = 0;
322 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
323 "number of failed continuation steps",
324 NOX::TestCompare::Absolute);
325
326 // Check final value of continuation parameter
327 double factor_final = finalGroup->getParam("Nonlinear Factor");
328 double factor_expected = 2.0;
329 ierr += testCompare.testValue(factor_final, factor_expected, 1.0e-14,
330 "final value of continuation parameter",
331 NOX::TestCompare::Relative);
332
333 // Check final value of bifurcation parameter
334 double right_bc_final = finalGroup->getParam("Right BC");
335 double right_bc_expected = 1.47241293;
336 ierr += testCompare.testValue(right_bc_final, right_bc_expected, 1.0e-7,
337 "final value of bifurcation parameter",
338 NOX::TestCompare::Relative);
339
340 // Check norm of solution
341 double norm_x = finalSolution.norm();
342 double norm_x_expected = 12.038464;
343 ierr += testCompare.testValue(norm_x, norm_x_expected, 1.0e-7,
344 "norm of final solution",
345 NOX::TestCompare::Relative);
346
347 LOCA::destroyGlobalData(globalData);
348
349 }
350
351 catch (std::exception& e) {
352 std::cout << e.what() << std::endl;
353 ierr = 1;
354 }
355 catch (const char *s) {
356 std::cout << s << std::endl;
357 ierr = 1;
358 }
359 catch (...) {
360 std::cout << "Caught unknown exception!" << std::endl;
361 ierr = 1;
362 }
363
364 return ierr ;
365 }
366
367 int main(int argc, char *argv[])
368 {
369 int ierr = 0;
370
371 // Initialize MPI
372 #ifdef HAVE_MPI
373 MPI_Init(&argc,&argv);
374 #endif
375
376 // Create a communicator for Epetra objects
377 #ifdef HAVE_MPI
378 Epetra_MpiComm Comm( MPI_COMM_WORLD );
379 #else
380 Epetra_SerialComm Comm;
381 #endif
382
383 // Get the process ID and the total number of processors
384 int MyPID = Comm.MyPID();
385 int NumProc = Comm.NumProc();
386
387 // Check for verbose output
388 bool verbose = false;
389 if (argc>1)
390 if (argv[1][0]=='-' && argv[1][1]=='v')
391 verbose = true;
392
393 // Get the number of elements from the command line
394 int NumGlobalElements = 0;
395 if ((argc > 2) && (verbose))
396 NumGlobalElements = atoi(argv[2]) + 1;
397 else if ((argc > 1) && (!verbose))
398 NumGlobalElements = atoi(argv[1]) + 1;
399 else
400 NumGlobalElements = 101;
401
402 // The number of unknowns must be at least equal to the
403 // number of processors.
404 if (NumGlobalElements < NumProc) {
405 std::cout << "numGlobalBlocks = " << NumGlobalElements
406 << " cannot be < number of processors = " << NumProc << std::endl;
407 exit(1);
408 }
409
410 // Test includeUV = true, useP = true, prec = "Transpose Preconditioner"
411 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
412 true, true, "Transpose Preconditioner",
413 "Jacobian");
414
415 // Test includeUV = true, useP = false, prec = "Transpose Preconditioner"
416 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
417 true, false, "Transpose Preconditioner",
418 "Jacobian");
419
420 // Test includeUV = false, useP = true, prec = "Transpose Preconditioner"
421 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
422 true, true, "Transpose Preconditioner",
423 "SMW");
424
425 // Test includeUV = false, useP = false, prec = "Transpose Preconditioner"
426 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
427 true, false, "Transpose Preconditioner",
428 "SMW");
429
430 if (NumProc > 1) {
431 // Test includeUV = false, useP = true, prec = "Transpose Preconditioner"
432 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
433 false, true, "Transpose Preconditioner",
434 "Jacobian");
435
436 // Test includeUV = false, useP = false, prec = "Transpose Preconditioner"
437 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
438 false, false, "Transpose Preconditioner",
439 "Jacobian");
440
441 // Test includeUV = false, useP = true, prec = "Transpose Preconditioner"
442 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
443 false, true, "Transpose Preconditioner",
444 "SMW");
445
446 // Test includeUV = false, useP = false, prec = "Transpose Preconditioner"
447 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
448 false, false, "Transpose Preconditioner",
449 "SMW");
450 }
451
452 // Test includeUV = true, useP = true, prec = "Left Preconditioning"
453 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
454 true, true, "Left Preconditioning",
455 "Jacobian");
456
457 // Test includeUV = true, useP = false, prec = "Left Preconditioning"
458 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
459 true, false, "Left Preconditioning",
460 "Jacobian");
461
462 // Test includeUV = true, useP = true, prec = "Left Preconditioning"
463 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
464 true, true, "Left Preconditioning",
465 "SMW");
466
467 // Test includeUV = true, useP = false, prec = "Left Preconditioning"
468 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
469 true, false, "Left Preconditioning",
470 "SMW");
471
472 if (NumProc > 1) {
473 // Test includeUV = false, useP = true, prec = "Left Preconditioning"
474 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
475 false, true, "Left Preconditioning",
476 "Jacobian");
477
478 // Test includeUV = false, useP = false, prec = "Left Preconditioning"
479 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
480 false, false, "Left Preconditioning",
481 "Jacobian");
482
483 // Test includeUV = false, useP = true, prec = "Left Preconditioning"
484 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
485 false, true, "Left Preconditioning",
486 "SMW");
487
488 // Test includeUV = false, useP = false, prec = "Left Preconditioning"
489 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
490 false, false, "Left Preconditioning",
491 "SMW");
492 }
493
494 #ifdef HAVE_NOX_EPETRAEXT
495 // Test includeUV = true, useP = true, prec = "Explicit Transpose"
496 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
497 true, true, "Explicit Transpose",
498 "Jacobian");
499
500 // Test includeUV = true, useP = false, prec = "Explicit Transpose"
501 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
502 true, false, "Explicit Transpose",
503 "Jacobian");
504
505 if (NumProc > 1) {
506 // Test includeUV = false, useP = true, prec = "Explicit Transpose"
507 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
508 false, true, "Explicit Transpose",
509 "Jacobian");
510
511 // Test includeUV = false, useP = false, prec = "Explicit Transpose"
512 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
513 false, false, "Explicit Transpose",
514 "Jacobian");
515
516 // Test includeUV = false, useP = true, prec = "Explicit Transpose"
517 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
518 false, true, "Explicit Transpose",
519 "SMW");
520
521 // Test includeUV = false, useP = false, prec = "Explicit Transpose"
522 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
523 false, false, "Explicit Transpose",
524 "SMW");
525 }
526
527 // Test includeUV = false, useP = false, prec = "Explicit Transpose"
528 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
529 true, true, "Explicit Transpose",
530 "SMW");
531
532 // Test includeUV = false, useP = false, prec = "Explicit Transpose"
533 ierr += tcubed_test(NumGlobalElements, verbose, Comm,
534 true, false, "Explicit Transpose",
535 "SMW");
536 #endif
537
538 if (MyPID == 0) {
539 if (ierr == 0)
540 std::cout << "All tests passed!" << std::endl;
541 else
542 std::cout << ierr << " test(s) failed!" << std::endl;
543 }
544
545 #ifdef HAVE_MPI
546 MPI_Finalize() ;
547 #endif
548
549 /* end main
550 */
551 return ierr ;
552 }