Debian Codegraph @

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