Debian Codegraph @

packages/nox/test/epetra/LOCA_Brusselator/brussCont_ShiftInvert2Matrix.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 Brusselator Test Problem
11
12 /* Solves the nonlinear equation:
13 *
14 * dT d2T
15 * --- - D1 --- - alpha + (beta+1)*T - C*T**2 = 0
16 * dt dx2
17 *
18 * T(t,0) = T(t,1) = alpha = 0.6
19 * T(0,x) = alpha + sinusoidal perturbation
20 *
21 *
22 * dC d2C
23 * --- - D2 --- - beta*T + C*T**2 = 0
24 * dt dx2
25 *
26 * C(t,0) = C(t,1) = beta / alpha = 2.0 / 0.6
27 * C(0,x) = beta / alpha + sinusoidal perturbation
28 *
29 * and
30 * D1 = D2 = 0.025
31 *
32 * with d representing partial differentiation.
33 */
34
35 /* This version tests the "Shift-Invert 2 Matrix" eigensolver, which
36 * requires a second matrix A2 passed to the shiftedLinSys and the
37 * declareSeparateMatricMemory() call to the LOCA::Epetra::Group.
38 *
39 */
40
41 // NOX Objects
42 #include "LOCA.H"
43 #include "LOCA_Epetra.H"
44 #include "NOX_Common.H"
45 #include "NOX_TestCompare.H"
46
47 // Trilinos Objects
48 #ifdef HAVE_MPI
49 #include "Epetra_MpiComm.h"
50 #else
51 #include "Epetra_SerialComm.h"
52 #endif
53 #include "Epetra_Map.h"
54 #include "Epetra_MapColoring.h"
55 #include "Epetra_Vector.h"
56 #include "Epetra_RowMatrix.h"
57 #include "Epetra_CrsMatrix.h"
58 #include "Epetra_Map.h"
59 #include "Epetra_LinearProblem.h"
60 #include "AztecOO.h"
61
62 // Added to allow timings
63 #include "Epetra_Time.h"
64
65 // Headers needed for FD coloring
66 #include <vector>
67
68 // User's application specific files
69 #include "Problem_Interface.H" // Interface file to NOX
70 #include "Brusselator.H"
71
72 int main(int argc, char *argv[])
73 {
74 int ierr = 0;
75 int MyPID = 0;
76 double alpha = 0.25;
77 double beta = 1.5;
78 double D1 = 1.0/40.0;
79 double D2 = 1.0/40.0;
80 int maxNewtonIters = 10;
81
82 try {
83
84 // Initialize MPI
85 #ifdef HAVE_MPI
86 MPI_Init(&argc,&argv);
87 #endif
88
89 // Create a communicator for Epetra objects
90 #ifdef HAVE_MPI
91 Epetra_MpiComm Comm( MPI_COMM_WORLD );
92 #else
93 Epetra_SerialComm Comm;
94 #endif
95
96 // Get the process ID and the total number of processors
97 MyPID = Comm.MyPID();
98 int NumProc = Comm.NumProc();
99
100 // Check for verbose output
101 bool verbose = false;
102 if (argc>1)
103 if (argv[1][0]=='-' && argv[1][1]=='v')
104 verbose = true;
105
106 // Get the number of elements from the command line
107 int NumGlobalNodes = 0;
108 if ((argc > 2) && (verbose))
109 NumGlobalNodes = atoi(argv[2]) + 1;
110 else if ((argc > 1) && (!verbose))
111 NumGlobalNodes = atoi(argv[1]) + 1;
112 else
113 NumGlobalNodes = 101;
114
115 // The number of unknowns must be at least equal to the
116 // number of processors.
117 if (NumGlobalNodes < NumProc) {
118 std::cout << "numGlobalNodes = " << NumGlobalNodes
119 << " cannot be < number of processors = " << NumProc
120 << std::endl;
121 exit(1);
122 }
123
124 // Create the Brusselator problem class. This creates all required
125 // Epetra objects for the problem and allows calls to the
126 // function (F) and Jacobian evaluation routines.
127 Brusselator Problem(NumGlobalNodes, Comm);
128
129 // Get the vector from the Problem
130 Epetra_Vector& soln = Problem.getSolution();
131
132 // Begin LOCA Solver ************************************
133
134 // Create the top level parameter list
135
136 Teuchos::RCP<Teuchos::ParameterList> paramList =
137 Teuchos::rcp(new Teuchos::ParameterList);
138
139 // Create LOCA sublist
140 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
141
142 // Create the stepper sublist and set the stepper parameters
143 Teuchos::ParameterList& stepperList = locaParamsList.sublist("Stepper");
144 stepperList.set("Bordered Solver Method", "Householder");
145 stepperList.set("Continuation Parameter", "beta");
146 stepperList.set("Initial Value", beta);
147 stepperList.set("Max Value", 1.6);
148 stepperList.set("Min Value", 0.0);
149 stepperList.set("Max Steps", 100);
150 stepperList.set("Max Nonlinear Iterations", maxNewtonIters);
151
152 #ifdef HAVE_LOCA_ANASAZI
153 // Create Anasazi Eigensolver sublist (needs --with-loca-anasazi)
154 stepperList.set("Compute Eigenvalues",true);
155 Teuchos::ParameterList& aList = stepperList.sublist("Eigensolver");
156 aList.set("Method", "Anasazi");
157 if (!verbose)
158 aList.set("Verbosity", Anasazi::Errors);
159 aList.set("Block Size", 1); // Size of blocks
160 aList.set("Num Blocks", 50); // Size of Arnoldi factorization
161 aList.set("Num Eigenvalues", 3); // Number of eigenvalues
162 aList.set("Convergence Tolerance", 1.0e-7); // Tolerance
163 aList.set("Step Size", 1); // How often to check convergence
164 aList.set("Maximum Restarts",1); // Maximum number of restarts
165 aList.set("Operator", "Shift-Invert 2 Matrix");
166 aList.set("Shift", 0.1);
167 #else
168 stepperList.set("Compute Eigenvalues",false);
169 #endif
170
171 // Create predictor sublist
172 Teuchos::ParameterList& predictorList =
173 locaParamsList.sublist("Predictor");
174 predictorList.set("Method", "Constant");
175
176 // Create step size sublist
177 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
178 stepSizeList.set("Initial Step Size", 0.01);
179 stepSizeList.set("Min Step Size", 1.0e-3);
180 stepSizeList.set("Max Step Size", 0.01);
181 stepSizeList.set("Aggressiveness", 0.1);
182
183 // Create the "Solver" parameters sublist to be used with NOX Solvers
184 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
185
186 // Set the printing parameters in the "Printing" sublist
187 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
188 printParams.set("MyPID", MyPID);
189 printParams.set("Output Precision", 3);
190 printParams.set("Output Processor", 0);
191 if (verbose)
192 printParams.set("Output Information",
193 NOX::Utils::OuterIteration +
194 NOX::Utils::OuterIterationStatusTest +
195 NOX::Utils::InnerIteration +
196 NOX::Utils::Details +
197 NOX::Utils::Warning +
198 NOX::Utils::TestDetails +
199 NOX::Utils::Error +
200 NOX::Utils::StepperIteration +
201 NOX::Utils::StepperDetails +
202 NOX::Utils::StepperParameters);
203 else
204 printParams.set("Output Information", NOX::Utils::Error);
205
206 // Sublist for "Linear Solver"
207 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
208 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
209 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
210 lsParams.set("Aztec Solver", "GMRES");
211 lsParams.set("Max Iterations", 800);
212 lsParams.set("Tolerance", 1e-6);
213 lsParams.set("Output Frequency", 50);
214 lsParams.set("Preconditioner", "Ifpack");
215
216 // Create the interface between the test problem and the nonlinear solver
217 Teuchos::RCP<Problem_Interface> interface =
218 Teuchos::rcp(new Problem_Interface(Problem));
219
220 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
221 Teuchos::RCP<Epetra_RowMatrix> A =
222 Teuchos::rcp(&Problem.getJacobian(),false);
223 // Clone matrix for separate memory -- used in 2 Matrix eigensolver
224 Teuchos::RCP<Epetra_RowMatrix> A2 =
225 Teuchos::rcp(new Epetra_CrsMatrix(Problem.getJacobian()));
226
227 // Use an Epetra Scaling object if desired
228 Teuchos::RCP<Epetra_Vector> scaleVec =
229 Teuchos::rcp(new Epetra_Vector(soln));
230 NOX::Epetra::Scaling scaling;
231 scaling.addRowSumScaling(NOX::Epetra::Scaling::Left, scaleVec);
232
233 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface;
234
235 // Create the Linear System
236 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
237 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
238 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
239 iReq, iJac, A, soln));
240 //&scaling);
241 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> shiftedLinSys =
242 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
243 iReq, iJac, A2, soln));
244
245 // Create initial guess
246 NOX::Epetra::Vector initialGuess(Teuchos::rcp(&soln,false),
247 NOX::Epetra::Vector::CreateView,
248 NOX::DeepCopy);
249
250 // Create and initialize the parameter vector
251 LOCA::ParameterVector pVector;
252 pVector.addParameter("alpha",alpha);
253 pVector.addParameter("beta",beta);
254 pVector.addParameter("D1",D1);
255 pVector.addParameter("D2",D2);
256
257 // Create Epetra factory
258 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
259 Teuchos::rcp(new LOCA::Epetra::Factory);
260
261 // Create global data object
262 Teuchos::RCP<LOCA::GlobalData> globalData =
263 LOCA::createGlobalData(paramList, epetraFactory);
264
265 // Create the Group
266 Teuchos::RCP<LOCA::Epetra::Interface::TimeDependent> iTime = interface;
267 Teuchos::RCP<LOCA::Epetra::Group> grp =
268 Teuchos::rcp(new LOCA::Epetra::Group(globalData, printParams,
269 iTime, initialGuess, linSys,
270 shiftedLinSys, pVector));
271
272 // Set flag in group to indicate that linSys and shiftedLinSys in Group constructor
273 // (line above) use separate memory for the matrix. This allows algorithms to
274 // compute and store 2 different matrices. Currently used by Shift-Invert 2 Matrix
275 // eigensolver.
276 grp->declareSeparateMatrixMemory();
277
278 grp->computeF();
279
280 // Create the convergence tests
281 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
282 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8,
283 NOX::StatusTest::NormF::Unscaled));
284 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
285 Teuchos::rcp(new NOX::StatusTest::MaxIters(maxNewtonIters));
286 Teuchos::RCP<NOX::StatusTest::Combo> combo =
287 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
288 combo->addStatusTest(absresid);
289 combo->addStatusTest(maxiters);
290
291 // Create stepper
292 LOCA::Stepper stepper(globalData, grp, combo, paramList);
293
294 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
295
296 if (status != LOCA::Abstract::Iterator::Finished) {
297 ierr = 1;
298 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
299 globalData->locaUtils->out()
300 << "Stepper failed to converge!" << std::endl;
301 }
302
303 // Get the final solution from the stepper
304 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
305 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
306 const NOX::Epetra::Vector& finalSolution =
307 dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX());
308
309 // Output the parameter list
310 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
311 globalData->locaUtils->out()
312 << std::endl << "Final Parameters" << std::endl
313 << "****************" << std::endl;
314 stepper.getList()->print(globalData->locaUtils->out());
315 globalData->locaUtils->out() << std::endl;
316 }
317
318 // Check some statistics on the solution
319 NOX::TestCompare testCompare(globalData->locaUtils->out(),
320 *(globalData->locaUtils));
321
322 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
323 globalData->locaUtils->out()
324 << std::endl
325 << "***** Checking solution statistics *****"
326 << std::endl;
327
328 // Check number of continuation steps
329 int numSteps = stepper.getStepNumber();
330 int numSteps_expected = 11;
331 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
332 "number of continuation steps",
333 NOX::TestCompare::Absolute);
334
335 // Check number of failed steps
336 int numFailedSteps = stepper.getNumFailedSteps();
337 int numFailedSteps_expected = 0;
338 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
339 "number of failed continuation steps",
340 NOX::TestCompare::Absolute);
341
342 // Check final value of continuation parameter
343 double beta_final = finalGroup->getParam("beta");
344 double beta_expected = 1.6;
345 ierr += testCompare.testValue(beta_final, beta_expected, 1.0e-14,
346 "final value of continuation parameter",
347 NOX::TestCompare::Relative);
348
349 // Check final of solution
350 NOX::Epetra::Vector final_x_expected(finalSolution);
351 int n = final_x_expected.getEpetraVector().MyLength()/2;
352 for (int i=0; i<n; i++) {
353 final_x_expected.getEpetraVector()[2*i] = alpha;
354 final_x_expected.getEpetraVector()[2*i+1] = beta_final/alpha;
355 }
356 ierr += testCompare.testVector(finalSolution, final_x_expected,
357 1.0e-6, 1.0e-6,
358 "value of final solution");
359
360 LOCA::destroyGlobalData(globalData);
361 }
362 catch (std::exception& e) {
363 std::cout << e.what() << std::endl;
364 ierr = 1;
365 }
366 catch (const char *s) {
367 std::cout << s << std::endl;
368 ierr = 1;
369 }
370 catch (...) {
371 std::cout << "Caught unknown exception!" << std::endl;
372 ierr = 1;
373 }
374
375 if (MyPID == 0) {
376 if (ierr == 0)
377 std::cout << "All tests passed!" << std::endl;
378 else
379 std::cout << ierr << " test(s) failed!" << std::endl;
380 }
381
382 #ifdef HAVE_MPI
383 MPI_Finalize() ;
384 #endif
385
386 return ierr;
387 }