Debian Codegraph @

packages/nox/test/epetra/LOCA_UnitTests/HouseholderTransposeBorderedSolve.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 #include "LOCA.H"
11 #include "LOCA_Epetra.H"
12
13 #include "LOCA_BorderedSolver_AbstractStrategy.H"
14 #include "LOCA_BorderedSolver_JacobianOperator.H"
15 #include "LOCA_Parameter_SublistParser.H"
16 #include "NOX_TestCompare.H"
17
18 // Trilinos Objects
19 #ifdef HAVE_MPI
20 #include "Epetra_MpiComm.h"
21 #else
22 #include "Epetra_SerialComm.h"
23 #endif
24 #include "Epetra_Map.h"
25 #include "Epetra_RowMatrix.h"
26 #include "Epetra_CrsMatrix.h"
27
28 // User's application specific files
29 #include "Problem_Interface.H"
30 #include "Tcubed_FiniteElementProblem.H"
31 #include "LOCALinearConstraint.H"
32
33 // Global variables used in main() and testSolve()
34 Teuchos::RCP<LOCA::MultiContinuation::AbstractGroup> grp;
35 Teuchos::RCP<LOCA::BorderedSolver::JacobianOperator> op;
36 Teuchos::RCP<LinearConstraint> constraints;
37 Teuchos::RCP<LOCA::Parameter::SublistParser> parsedParams;
38 Teuchos::RCP<LOCA::BorderedSolver::AbstractStrategy> bordering;
39 Teuchos::RCP<LOCA::BorderedSolver::AbstractStrategy> householder;
40 Teuchos::RCP<LOCA::GlobalData> globalData;
41 Teuchos::RCP<NOX::TestCompare> testCompare;
42 Teuchos::RCP<NOX::Abstract::MultiVector> A;
43 Teuchos::RCP<NOX::Abstract::MultiVector> B;
44 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> C;
45 Teuchos::RCP<NOX::Abstract::MultiVector> F;
46 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> G;
47 Teuchos::RCP<NOX::Abstract::MultiVector> X_bordering;
48 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> Y_bordering;
49 Teuchos::RCP<NOX::Abstract::MultiVector> X_householder;
50 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> Y_householder;
51
52 int
53 testSolve(bool flagA, bool flagB, bool flagC, bool flagF, bool flagG,
54 double reltol, double abstol,
55 const std::string& testName) {
56 int ierr = 0;
57
58 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
59 globalData->locaUtils->out()
60 << std::endl << "***** " << testName << " *****" << std::endl;
61
62 Teuchos::RCP<NOX::Abstract::MultiVector> a =
63 Teuchos::null;
64 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> c =
65 Teuchos::null;
66 Teuchos::RCP<NOX::Abstract::MultiVector> f =
67 Teuchos::null;
68 Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> g =
69 Teuchos::null;
70
71 if (!flagA)
72 a = A;
73 if (!flagC)
74 c = C;
75 if (!flagF)
76 f = F;
77 if (!flagG)
78 g = G;
79 constraints->setIsZeroDX(flagB);
80
81 // Set up bordered problem
82 bordering->setMatrixBlocks(op, a, constraints, c);
83 bordering->initForTransposeSolve();
84
85 householder->setMatrixBlocks(op, a, constraints, c);
86 householder->initForTransposeSolve();
87
88 X_bordering->init(0.0);
89 Y_bordering->putScalar(0.0);
90 X_householder->init(0.0);
91 Y_householder->putScalar(0.0);
92
93 // Solve using bordering
94 NOX::Abstract::Group::ReturnType borderingStatus =
95 bordering->applyInverseTranspose(*(parsedParams->getSublist("Linear Solver")),
96 f.get(), g.get(), *X_bordering, *Y_bordering);
97 if (borderingStatus == NOX::Abstract::Group::Failed)
98 ++ierr;
99
100 // Solve using householder
101 NOX::Abstract::Group::ReturnType householderStatus =
102 householder->applyInverseTranspose(*(parsedParams->getSublist("Linear Solver")),
103 f.get(), g.get(), *X_householder,
104 *Y_householder);
105 if (householderStatus == NOX::Abstract::Group::Failed)
106 ++ierr;
107
108 ierr += testCompare->testMultiVector(*X_householder, *X_bordering,
109 reltol, abstol, "Solution Component");
110
111 ierr += testCompare->testMatrix(*Y_householder, *Y_bordering,
112 reltol, abstol, "Parameter Component");
113
114 return ierr;
115 }
116
117
118 int main(int argc, char *argv[])
119 {
120 int nConstraints = 10;
121 int nRHS = 7;
122
123 double left_bc = 0.0;
124 double right_bc = 1.0;
125 double nonlinear_factor = 1.0;
126 int ierr = 0;
127 double reltol = 1.0e-8;
128 double abstol = 1.0e-8;
129 double lstol = 1.0e-11;
130
131 int MyPID = 0;
132
133 try {
134
135 // Initialize MPI
136 #ifdef HAVE_MPI
137 MPI_Init(&argc,&argv);
138 #endif
139
140 // Create a communicator for Epetra objects
141 #ifdef HAVE_MPI
142 Epetra_MpiComm Comm( MPI_COMM_WORLD );
143 #else
144 Epetra_SerialComm Comm;
145 #endif
146
147 // Get the total number of processors
148 MyPID = Comm.MyPID();
149 int NumProc = Comm.NumProc();
150
151 bool verbose = false;
152 // Check for verbose output
153 if (argc>1)
154 if (argv[1][0]=='-' && argv[1][1]=='v')
155 verbose = true;
156
157 // Get the number of elements from the command line
158 int NumGlobalElements = 0;
159 if ((argc > 2) && (verbose))
160 NumGlobalElements = atoi(argv[2]) + 1;
161 else if ((argc > 1) && (!verbose))
162 NumGlobalElements = atoi(argv[1]) + 1;
163 else
164 NumGlobalElements = 101;
165
166 // The number of unknowns must be at least equal to the
167 // number of processors.
168 if (NumGlobalElements < NumProc) {
169 std::cout << "numGlobalBlocks = " << NumGlobalElements
170 << " cannot be < number of processors = " << NumProc << std::endl;
171 exit(1);
172 }
173
174 // Seed the random number generator in Teuchos. We create random
175 // bordering matrices and it is possible different processors might generate
176 // different matrices. By setting the seed, this shouldn't happen.
177 Teuchos::ScalarTraits<double>::seedrandom(12345);
178
179 // Create parameter list
180 Teuchos::RCP<Teuchos::ParameterList> paramList =
181 Teuchos::rcp(new Teuchos::ParameterList);
182
183 // Create LOCA sublist
184 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
185
186 // Create the constraints list
187 Teuchos::ParameterList& constraintsList =
188 locaParamsList.sublist("Constraints");
189 constraintsList.set("Bordered Solver Method", "Bordering");
190
191 // Create the "Solver" parameters sublist to be used with NOX Solvers
192 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
193
194 Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
195 nlPrintParams.set("MyPID", MyPID);
196 if (verbose)
197 nlPrintParams.set("Output Information",
198 NOX::Utils::Error +
199 NOX::Utils::Details +
200 NOX::Utils::LinearSolverDetails +
201 NOX::Utils::OuterIteration +
202 NOX::Utils::InnerIteration +
203 NOX::Utils::Warning +
204 NOX::Utils::TestDetails +
205 NOX::Utils::StepperIteration +
206 NOX::Utils::StepperDetails);
207 else
208 nlPrintParams.set("Output Information", NOX::Utils::Error);
209
210 // Create the "Direction" sublist for the "Line Search Based" solver
211 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
212 Teuchos::ParameterList& newParams = dirParams.sublist("Newton");
213 Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver");
214 lsParams.set("Aztec Solver", "GMRES");
215 lsParams.set("Max Iterations", 100);
216 lsParams.set("Tolerance", lstol);
217 if (verbose)
218 lsParams.set("Output Frequency", 1);
219 else
220 lsParams.set("Output Frequency", 0);
221 lsParams.set("Scaling", "None");
222 lsParams.set("Preconditioner", "Ifpack");
223 //lsParams.set("Preconditioner", "AztecOO");
224 //lsParams.set("Jacobian Operator", "Matrix-Free");
225 //lsParams.set("Preconditioner Operator", "Finite Difference");
226 lsParams.set("Aztec Preconditioner", "ilut");
227 //lsParams.set("Overlap", 2);
228 //lsParams.set("Fill Factor", 2.0);
229 //lsParams.set("Drop Tolerance", 1.0e-12);
230 lsParams.set("Max Age Of Prec", -2);
231
232 // Create the FiniteElementProblem class. This creates all required
233 // Epetra objects for the problem and allows calls to the
234 // function (RHS) and Jacobian evaluation routines.
235 Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm);
236
237 // Get the vector from the Problem
238 Epetra_Vector& soln = Problem.getSolution();
239
240 // Initialize Solution
241 soln.PutScalar(0.0);
242
243 // Create and initialize the parameter vector
244 LOCA::ParameterVector pVector;
245 pVector.addParameter("Nonlinear Factor",nonlinear_factor);
246 pVector.addParameter("Left BC", left_bc);
247 pVector.addParameter("Right BC", right_bc);
248
249 // Create the interface between the test problem and the nonlinear solver
250 // This is created by the user using inheritance of the abstract base
251 // class:
252 Teuchos::RCP<Problem_Interface> interface =
253 Teuchos::rcp(new Problem_Interface(Problem));
254 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface;
255 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
256
257 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
258 Teuchos::RCP<Epetra_RowMatrix> Amat =
259 Teuchos::rcp(&Problem.getJacobian(),false);
260
261 // Create the linear systems
262 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys =
263 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams,
264 lsParams, iReq, iJac,
265 Amat, soln));
266
267 // Create the loca vector
268 NOX::Epetra::Vector locaSoln(soln);
269
270 // Create Epetra factory
271 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
272 Teuchos::rcp(new LOCA::Epetra::Factory);
273
274 // Create global data object
275 globalData = LOCA::createGlobalData(paramList, epetraFactory);
276
277 // Create parsed parameter list
278 parsedParams =
279 Teuchos::rcp(new LOCA::Parameter::SublistParser(globalData));
280 parsedParams->parseSublists(paramList);
281
282 // Create the Group
283 grp = Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams,
284 iReq, locaSoln,
285 linsys, pVector));
286
287 // Create Jacobian operator
288 op = Teuchos::rcp(new LOCA::BorderedSolver::JacobianOperator(grp));
289
290 // Change initial guess to a random vector
291 Teuchos::RCP<NOX::Abstract::Vector> xnew =
292 grp->getX().clone();
293 xnew->random();
294 grp->setX(*xnew);
295
296 // Create the constraints object & constraint param IDs list
297 constraints =
298 Teuchos::rcp(new LinearConstraint(nConstraints, LOCA::ParameterVector(),
299 locaSoln));
300
301 // Create bordering solver
302 bordering
303 = globalData->locaFactory->createBorderedSolverStrategy(
304 parsedParams,
305 parsedParams->getSublist("Constraints"));
306
307 // Change strategy to Householder
308 constraintsList.set("Bordered Solver Method",
309 "Householder");
310
311 // Create householder solver
312 householder
313 = globalData->locaFactory->createBorderedSolverStrategy(
314 parsedParams,
315 parsedParams->getSublist("Constraints"));
316
317 // Check some statistics on the solution
318 testCompare = Teuchos::rcp(new NOX::TestCompare(
319 globalData->locaUtils->out(),
320 *(globalData->locaUtils)));
321
322 // Evaluate blocks
323 grp->computeF();
324 grp->computeJacobian();
325
326 // A
327 A = grp->getX().createMultiVector(nConstraints);
328 A->random();
329
330 // B
331 constraints->setX(grp->getX());
332 B = grp->getX().createMultiVector(nConstraints);
333 B->random();
334 constraints->setDgDx(*B);
335 constraints->computeConstraints();
336 constraints->computeDX();
337
338 // C
339 C = Teuchos::rcp(new NOX::Abstract::MultiVector::DenseMatrix(nConstraints,
340 nConstraints));
341 C->random();
342
343 // Set up left- and right-hand sides
344 F = grp->getX().createMultiVector(nRHS);
345 F->random();
346 G = Teuchos::rcp(new NOX::Abstract::MultiVector::DenseMatrix(nConstraints,
347 nRHS));
348 G->random();
349 X_bordering = F->clone(NOX::ShapeCopy);
350 Y_bordering =
351 Teuchos::rcp(new NOX::Abstract::MultiVector::DenseMatrix(nConstraints,
352 nRHS));
353 X_householder = F->clone(NOX::ShapeCopy);
354 Y_householder =
355 Teuchos::rcp(new NOX::Abstract::MultiVector::DenseMatrix(nConstraints,
356 nRHS));
357
358 std::string testName;
359
360 // Test all nonzero
361 testName = "Testing all nonzero";
362 ierr += testSolve(false, false, false, false, false,
363 reltol, abstol, testName);
364
365 // Test A = 0
366 testName = "Testing A=0";
367 ierr += testSolve(true, false, false, false, false,
368 reltol, abstol, testName);
369
370 // Test B = 0
371 testName = "Testing B=0";
372 ierr += testSolve(false, true, false, false, false,
373 reltol, abstol, testName);
374
375 // Test C = 0
376 testName = "Testing C=0";
377 ierr += testSolve(false, false, true, false, false,
378 reltol, abstol, testName);
379
380 // Test F = 0
381 testName = "Testing F=0";
382 ierr += testSolve(false, false, false, true, false,
383 reltol, abstol, testName);
384
385 // Test G = 0
386 testName = "Testing G=0";
387 ierr += testSolve(false, false, false, false, true,
388 reltol, abstol, testName);
389
390 // Test A,B = 0
391 testName = "Testing A,B=0";
392 ierr += testSolve(true, true, false, false, false,
393 reltol, abstol, testName);
394
395 // Test A,F = 0
396 testName = "Testing A,F=0";
397 ierr += testSolve(true, false, false, true, false,
398 reltol, abstol, testName);
399
400 // Test A,G = 0
401 testName = "Testing A,G=0";
402 ierr += testSolve(true, false, false, false, true,
403 reltol, abstol, testName);
404
405 // Test B,F = 0
406 testName = "Testing B,F=0";
407 ierr += testSolve(false, true, false, true, false,
408 reltol, abstol, testName);
409
410 // Test B,G = 0
411 testName = "Testing B,G=0";
412 ierr += testSolve(false, true, false, false, true,
413 reltol, abstol, testName);
414
415 // Test C,F = 0
416 testName = "Testing C,F=0";
417 ierr += testSolve(false, false, true, true, false,
418 reltol, abstol, testName);
419
420 // Test C,G = 0
421 testName = "Testing C,G=0";
422 ierr += testSolve(false, false, true, false, true,
423 reltol, abstol, testName);
424
425 // Test F,G = 0
426 testName = "Testing F,G=0";
427 ierr += testSolve(false, false, false, true, true,
428 reltol, abstol, testName);
429
430 // Test A,B,F = 0
431 testName = "Testing A,B,F=0";
432 ierr += testSolve(true, true, false, true, false,
433 reltol, abstol, testName);
434
435 // Test A,B,G = 0
436 testName = "Testing A,B,G=0";
437 ierr += testSolve(true, true, false, false, true,
438 reltol, abstol, testName);
439
440 // Test A,F,G = 0
441 testName = "Testing A,F,G=0";
442 ierr += testSolve(true, false, false, true, true,
443 reltol, abstol, testName);
444
445 // Test B,F,G = 0
446 testName = "Testing B,F,G=0";
447 ierr += testSolve(false, true, false, true, true,
448 reltol, abstol, testName);
449
450 // Test C,F,G = 0
451 testName = "Testing C,F,G=0";
452 ierr += testSolve(false, false, true, true, true,
453 reltol, abstol, testName);
454
455 // Test A,B,F,G = 0
456 testName = "Testing A,B,F,G=0";
457 ierr += testSolve(true, true, false, true, true,
458 reltol, abstol, testName);
459
460 LOCA::destroyGlobalData(globalData);
461
462 }
463
464 catch (std::exception& e) {
465 std::cout << e.what() << std::endl;
466 ierr = 1;
467 }
468 catch (const char *s) {
469 std::cout << s << std::endl;
470 ierr = 1;
471 }
472 catch (...) {
473 std::cout << "Caught unknown exception!" << std::endl;
474 ierr = 1;
475 }
476
477 if (MyPID == 0) {
478 if (ierr == 0)
479 std::cout << "All tests passed!" << std::endl;
480 else
481 std::cout << ierr << " test(s) failed!" << std::endl;
482 }
483
484 #ifdef HAVE_MPI
485 MPI_Finalize() ;
486 #endif
487
488 return ierr;
489 }