Debian Codegraph @

packages/nox/test/epetra/LOCA_Brusselator/brussHopfCont.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 pi = 4.0*atan(1.0);
71 double D1 = 1.0/40.0;
72 double D2 = 1.0/40.0;
73 double alpha = 0.25;
74 double beta = 2.0*pi*pi*D1 + 1.0 + alpha*alpha;
75 int maxNewtonIters = 10;
76
77 try {
78
79 // Initialize MPI
80 #ifdef HAVE_MPI
81 MPI_Init(&argc,&argv);
82 #endif
83
84 // Create a communicator for Epetra objects
85 #ifdef HAVE_MPI
86 Epetra_MpiComm Comm( MPI_COMM_WORLD );
87 #else
88 Epetra_SerialComm Comm;
89 #endif
90
91 // Get the process ID and the total number of processors
92 MyPID = Comm.MyPID();
93 int NumProc = Comm.NumProc();
94
95 // Check for verbose output
96 bool verbose = false;
97 if (argc>1)
98 if (argv[1][0]=='-' && argv[1][1]=='v')
99 verbose = true;
100
101 // Get the number of elements from the command line
102 int NumGlobalNodes = 0;
103 if ((argc > 2) && (verbose))
104 NumGlobalNodes = atoi(argv[2]) + 1;
105 else if ((argc > 1) && (!verbose))
106 NumGlobalNodes = atoi(argv[1]) + 1;
107 else
108 NumGlobalNodes = 101;
109
110 // The number of unknowns must be at least equal to the
111 // number of processors.
112 if (NumGlobalNodes < NumProc) {
113 std::cout << "numGlobalNodes = " << NumGlobalNodes
114 << " cannot be < number of processors = " << NumProc
115 << std::endl;
116 exit(1);
117 }
118
119 // Create the Brusselator problem class. This creates all required
120 // Epetra objects for the problem and allows calls to the
121 // function (F) and Jacobian evaluation routines.
122 Brusselator Problem(NumGlobalNodes, Comm);
123 Problem.setParameters(alpha, beta, D1, D2);
124 Problem.initializeSoln();
125
126 // Get the vector from the Problem
127 Epetra_Vector& soln = Problem.getSolution();
128 NOX::Epetra::Vector initialGuess(Teuchos::rcp(&soln,false),
129 NOX::Epetra::Vector::CreateView,
130 NOX::DeepCopy);
131
132 // Create initial eigenvectors
133 Epetra_Vector& x = Problem.getMesh();
134 NOX::Epetra::Vector y(initialGuess);
135 NOX::Epetra::Vector z(initialGuess);
136 double lambda_real = (beta - 1.0 - alpha*alpha)/2.0;
137 double lambda_imag = std::sqrt(alpha*alpha - lambda_real*lambda_real);
138 double v1_real = -alpha*alpha;
139 double v1_imag = 0.0;
140 double v2_real = beta - 1.0 - lambda_real;
141 double v2_imag = -lambda_imag;
142 for (int i=0; i<x.MyLength(); i++) {
143 y.getEpetraVector()[2*i] = v1_real*sin(1.0*pi*x[i]);
144 y.getEpetraVector()[2*i+1] = v2_real*sin(1.0*pi*x[i]);
145 z.getEpetraVector()[2*i] = v1_imag*sin(1.0*pi*x[i]);
146 z.getEpetraVector()[2*i+1] = v2_imag*sin(1.0*pi*x[i]);
147 }
148
149 // Initial guess for frequency (valid for |alpha| > (pi^2)*|D1|)
150 double w = lambda_imag;
151
152 // Create length scaling vector (phi)
153 NOX::Epetra::Vector phi(initialGuess);
154 phi.init(1.0);
155
156 Teuchos::RCP<NOX::Abstract::Vector> y_vec =
157 Teuchos::rcp(&y,false);
158 Teuchos::RCP<NOX::Abstract::Vector> z_vec =
159 Teuchos::rcp(&z,false);
160 Teuchos::RCP<NOX::Abstract::Vector> phi_vec =
161 Teuchos::rcp(&phi,false);
162
163 // Create initial values for a and b for minimally augmented method
164 Teuchos::RCP<NOX::Abstract::Vector> a_vec_real =
165 Teuchos::rcp(new NOX::Epetra::Vector(initialGuess));
166 Teuchos::RCP<NOX::Abstract::Vector> a_vec_imag =
167 Teuchos::rcp(new NOX::Epetra::Vector(initialGuess));
168 Teuchos::RCP<NOX::Abstract::Vector> b_vec_real =
169 Teuchos::rcp(new NOX::Epetra::Vector(initialGuess));
170 Teuchos::RCP<NOX::Abstract::Vector> b_vec_imag =
171 Teuchos::rcp(new NOX::Epetra::Vector(initialGuess));
172 *a_vec_real = *y_vec;
173 *a_vec_imag = *z_vec;
174 *b_vec_real = *y_vec;
175 *b_vec_imag = *z_vec;
176
177 // Begin LOCA Solver ************************************
178
179 // Create the top level parameter list
180
181 Teuchos::RCP<Teuchos::ParameterList> paramList =
182 Teuchos::rcp(new Teuchos::ParameterList);
183
184 // Create LOCA sublist
185 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
186
187 // Create the stepper sublist and set the stepper parameters
188 Teuchos::ParameterList& stepperList = locaParamsList.sublist("Stepper");
189 stepperList.set("Continuation Parameter", "alpha");
190 stepperList.set("Initial Value", alpha);
191 stepperList.set("Max Value", 1.0);
192 stepperList.set("Min Value", 0.24);
193 stepperList.set("Max Steps", 100);
194 stepperList.set("Max Nonlinear Iterations", maxNewtonIters);
195
196 // Create bifurcation sublist
197 Teuchos::ParameterList& bifurcationList =
198 locaParamsList.sublist("Bifurcation");
199 bifurcationList.set("Type", "Hopf");
200 bifurcationList.set("Bifurcation Parameter", "beta");
201 bifurcationList.set("Initial Frequency", w);
202
203 // // For Moore-Spence Formulation
204 // bifurcationList.set("Formulation", "Moore-Spence");
205 // bifurcationList.set("Solver Method", "Salinger Bordering");
206 // bifurcationList.set("Length Normalization Vector", phi_vec);
207 // bifurcationList.set("Initial Real Eigenvector", y_vec);
208 // bifurcationList.set("Initial Imaginary Eigenvector", z_vec);
209
210 // For minimally augmented formulation
211 bifurcationList.set("Formulation", "Minimally Augmented");
212 bifurcationList.set("Initial Real A Vector", a_vec_real); // Must set
213 bifurcationList.set("Initial Imaginary A Vector", a_vec_imag); // Must set
214 bifurcationList.set("Initial Real B Vector", b_vec_real); // Must set
215 bifurcationList.set("Initial Imaginary B Vector", b_vec_imag); // Must set
216 bifurcationList.set("Update Null Vectors Every Continuation Step", true);
217
218 // For minimally augmented method, should set these for good performance
219 // Direct solve of bordered equations
220 bifurcationList.set("Bordered Solver Method", "Householder");
221 bifurcationList.set("Include UV In Preconditioner", true);
222
223 // Combine arc-length and turning point bordered rows & columns
224 stepperList.set("Bordered Solver Method", "Nested");
225 Teuchos::ParameterList& nestedList =
226 stepperList.sublist("Nested Bordered Solver");
227 // Direct solve of combined bordered system
228 nestedList.set("Bordered Solver Method", "Householder");
229 nestedList.set("Include UV In Preconditioner", true);
230
231 // Create predictor sublist
232 Teuchos::ParameterList& predictorList =
233 locaParamsList.sublist("Predictor");
234 predictorList.set("Method", "Secant");
235
236 // Teuchos::ParameterList& firstStepPredictor =
237 // predictorList.sublist("First Step Predictor");
238 // firstStepPredictor.set("Method", "Random");
239 // firstStepPredictor.set("Epsilon", 1.0e-3);
240 // Teuchos::ParameterList& lastStepPredictor =
241 // predictorList.sublist("Last Step Predictor");
242 // lastStepPredictor.set("Method", "Random");
243 // lastStepPredictor.set("Epsilon", 1.0e-3);
244
245 // Create step size sublist
246 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
247 stepSizeList.set("Initial Step Size", 0.02);
248 stepSizeList.set("Min Step Size", 1.0e-3);
249 stepSizeList.set("Max Step Size", 1.0);
250 stepSizeList.set("Aggressiveness", 0.5);
251
252 // Create the "Solver" parameters sublist to be used with NOX Solvers
253 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
254
255 // Set the printing parameters in the "Printing" sublist
256 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
257 printParams.set("MyPID", MyPID);
258 printParams.set("Output Precision", 3);
259 printParams.set("Output Processor", 0);
260 if (verbose)
261 printParams.set("Output Information",
262 NOX::Utils::OuterIteration +
263 NOX::Utils::OuterIterationStatusTest +
264 NOX::Utils::InnerIteration +
265 NOX::Utils::Details +
266 NOX::Utils::LinearSolverDetails +
267 NOX::Utils::Warning +
268 NOX::Utils::TestDetails +
269 NOX::Utils::Error +
270 NOX::Utils::StepperIteration +
271 NOX::Utils::StepperDetails +
272 NOX::Utils::StepperParameters);
273 else
274 printParams.set("Output Information", NOX::Utils::Error);
275
276 // Sublist for "Linear Solver"
277 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
278 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
279 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
280 lsParams.set("Aztec Solver", "GMRES");
281 lsParams.set("Max Iterations", 800);
282 lsParams.set("Tolerance", 1e-6);
283 lsParams.set("Output Frequency", 50);
284 lsParams.set("Preconditioner", "Ifpack");
285
286 // Create the interface between the test problem and the nonlinear solver
287 Teuchos::RCP<Problem_Interface> interface =
288 Teuchos::rcp(new Problem_Interface(Problem));
289
290 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
291 Teuchos::RCP<Epetra_RowMatrix> A =
292 Teuchos::rcp(&Problem.getJacobian(),false);
293
294 // Use an Epetra Scaling object if desired
295 Teuchos::RCP<Epetra_Vector> scaleVec =
296 Teuchos::rcp(new Epetra_Vector(soln));
297 NOX::Epetra::Scaling scaling;
298 scaling.addRowSumScaling(NOX::Epetra::Scaling::Left, scaleVec);
299
300 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface;
301
302 // Create the Linear System
303 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface;
304 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
305 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
306 iReq, iJac, A, soln));
307 //&scaling);
308
309 // Create shifted Linear System
310 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> shiftedLinSys =
311 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
312 iReq, iJac, A, soln));
313 //&scaling);
314
315 // Create and initialize the parameter vector
316 LOCA::ParameterVector pVector;
317 pVector.addParameter("alpha",alpha);
318 pVector.addParameter("beta",beta);
319 pVector.addParameter("D1",D1);
320 pVector.addParameter("D2",D2);
321
322 // Create Epetra factory
323 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
324 Teuchos::rcp(new LOCA::Epetra::Factory);
325
326 // Create global data object
327 Teuchos::RCP<LOCA::GlobalData> globalData =
328 LOCA::createGlobalData(paramList, epetraFactory);
329
330 // Create the Group
331 Teuchos::RCP<LOCA::Epetra::Interface::TimeDependent> iTime =
332 interface;
333 Teuchos::RCP<LOCA::Epetra::Group> grp =
334 Teuchos::rcp(new LOCA::Epetra::Group(globalData, printParams,
335 iTime, initialGuess, linSys,
336 shiftedLinSys,
337 pVector));
338 grp->computeF();
339
340 // Create the convergence tests
341 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
342 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8,
343 NOX::StatusTest::NormF::Unscaled));
344 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
345 Teuchos::rcp(new NOX::StatusTest::MaxIters(maxNewtonIters));
346 Teuchos::RCP<NOX::StatusTest::Combo> combo =
347 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
348 combo->addStatusTest(absresid);
349 combo->addStatusTest(maxiters);
350
351 // Create stepper
352 LOCA::Stepper stepper(globalData, grp, combo, paramList);
353
354 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
355
356 if (status != LOCA::Abstract::Iterator::Finished) {
357 ierr = 1;
358 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
359 globalData->locaUtils->out()
360 << "Stepper failed to converge!" << std::endl;
361 }
362
363 // Get the final solution from the stepper
364 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
365 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
366
367 // Output the parameter list
368 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
369 globalData->locaUtils->out()
370 << std::endl << "Final Parameters" << std::endl
371 << "****************" << std::endl;
372 stepper.getList()->print(globalData->locaUtils->out());
373 globalData->locaUtils->out() << std::endl;
374 }
375
376 // Check some statistics on the solution
377 NOX::TestCompare testCompare(globalData->locaUtils->out(),
378 *(globalData->locaUtils));
379
380 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
381 globalData->locaUtils->out()
382 << std::endl
383 << "***** Checking solution statistics *****"
384 << std::endl;
385
386 // Check number of continuation steps
387 int numSteps = stepper.getStepNumber();
388 int numSteps_expected = 12;
389 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
390 "number of continuation steps",
391 NOX::TestCompare::Absolute);
392
393 // Check number of failed steps
394 int numFailedSteps = stepper.getNumFailedSteps();
395 int numFailedSteps_expected = 0;
396 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
397 "number of failed continuation steps",
398 NOX::TestCompare::Absolute);
399
400 // Check final value of continuation parameter
401 double alpha_final = finalGroup->getParam("alpha");
402 double alpha_expected = 1.0;
403 ierr += testCompare.testValue(alpha_final, alpha_expected, 1.0e-14,
404 "final value of continuation parameter",
405 NOX::TestCompare::Relative);
406
407 // Check final value of bifurcation parameter
408 double beta_final = finalGroup->getParam("beta");
409 double beta_expected = 5.279;
410 ierr += testCompare.testValue(beta_final, beta_expected, 1.0e-3,
411 "final value of bifurcation parameter",
412 NOX::TestCompare::Relative);
413
414 LOCA::destroyGlobalData(globalData);
415 }
416 catch (std::exception& e) {
417 std::cout << e.what() << std::endl;
418 ierr = 1;
419 }
420 catch (const char *s) {
421 std::cout << s << std::endl;
422 ierr = 1;
423 }
424 catch (...) {
425 std::cout << "Caught unknown exception!" << std::endl;
426 ierr = 1;
427 }
428
429 if (MyPID == 0) {
430 if (ierr == 0)
431 std::cout << "All tests passed!" << std::endl;
432 else
433 std::cout << ierr << " test(s) failed!" << std::endl;
434 }
435
436 #ifdef HAVE_MPI
437 MPI_Finalize() ;
438 #endif
439
440 return ierr;
441 }