Debian Codegraph @

packages/nox/test/epetra/LOCA_Brusselator_xyzt/brussXYZT_bdsdt.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 #include "Epetra_Map.h"
43 #include "Epetra_MapColoring.h"
44 #include "Epetra_Vector.h"
45 #include "Epetra_RowMatrix.h"
46 #include "Epetra_CrsMatrix.h"
47 #include "Epetra_Map.h"
48 #include "Epetra_LinearProblem.h"
49 #include "AztecOO.h"
50 #include "Teuchos_GlobalMPISession.hpp"
51
52 // Added to allow timings
53 #include "Epetra_Time.h"
54
55 // Headers needed for FD coloring
56 #include <vector>
57 #include "EpetraExt_MapColoring.h"
58 #include "EpetraExt_MapColoringIndex.h"
59
60 // User's application specific files
61 #include "Problem_Interface.H" // Interface file to NOX
62 #include "Brusselator.H"
63
64 // XYZT-specific headers
65 #ifdef HAVE_MPI
66 #include "EpetraExt_MultiMpiComm.h"
67 #else
68 #include "EpetraExt_MultiSerialComm.h"
69 #endif
70 #include "LOCA_Epetra_Interface_xyzt.H"
71
72 using namespace std;
73
74 #define MAX_NEWTON_ITERS 200
75 #define BETA_INIT 1.8
76
77 // Forward declarations
78 void getParamList(Teuchos::ParameterList *p, int MyPID);
79 void getPrecLSParams(Teuchos::ParameterList *p, int MyPID);
80 void getPrecPrintParams(Teuchos::ParameterList *p, int MyPID);
81
82 int main(int argc, char *argv[])
83 {
84
85 // Initialize MPI
86 Teuchos::GlobalMPISession mpiSession(&argc,&argv);
87
88 int ierr = 0;
89 int MyPID = 0;
90 double alpha = 1.00; // stable steady state
91 double beta = BETA_INIT;
92 double D1 = 1.0/40.0;
93 double D2 = 1.0/40.0;
94 int NumGlobalNodes = 10; // default
95 int spatialProcs = 1; // default
96 int numTimeSteps = 100; // default
97
98 try {
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 if ((argc > 2) && (verbose))
108 NumGlobalNodes = atoi(argv[2]) + 1;
109 else if ((argc > 1) && (!verbose))
110 NumGlobalNodes = atoi(argv[1]) + 1;
111
112 // Get the number of processors to use for the spatial problem
113 if ((argc > 3) && (verbose))
114 spatialProcs = atoi(argv[3]);
115 else if ((argc > 2) && (!verbose))
116 spatialProcs = atoi(argv[2]);
117
118 // Get the number of processors to use for the spatial problem
119 if ((argc > 4) && (verbose))
120 numTimeSteps = atoi(argv[4]);
121 else if ((argc > 3) && (!verbose))
122 numTimeSteps = atoi(argv[3]);
123
124 // MPI MANIPULATION FOR XYZT PROBLEMS
125 #ifdef HAVE_MPI
126 Teuchos::RCP<EpetraExt::MultiMpiComm> globalComm =
127 Teuchos::rcp(new EpetraExt::MultiMpiComm(MPI_COMM_WORLD,
128 spatialProcs, numTimeSteps));
129 #else
130 Teuchos::RCP<EpetraExt::MultiSerialComm> globalComm =
131 Teuchos::rcp(new EpetraExt::MultiSerialComm(numTimeSteps));
132 #endif
133 Epetra_Comm& Comm = globalComm->SubDomainComm();
134 MyPID = globalComm->MyPID();
135
136 // Get the total number of processors
137 int NumProc = Comm.NumProc();
138
139 // The number of unknowns must be at least equal to the
140 // number of processors.
141 if (NumGlobalNodes < NumProc) {
142 std::cout << "numGlobalNodes = " << NumGlobalNodes
143 << " cannot be < number of processors = " << NumProc
144 << std::endl;
145 exit(1);
146 }
147
148 // Create the Brusselator problem class. This creates all required
149 // Epetra objects for the problem and allows calls to the
150 // function (F) and Jacobian evaluation routines.
151 Brusselator::OverlapType OType = Brusselator::ELEMENTS;
152 Brusselator Problem(NumGlobalNodes, Comm, OType);
153
154 // Get the vector from the Problem
155 Epetra_Vector& soln = Problem.getSolution();
156
157 // Begin Nonlinear Solver ************************************
158
159 // Create the top level parameter list
160 Teuchos::RCP<Teuchos::ParameterList> paramList =
161 Teuchos::rcp(new Teuchos::ParameterList);
162 getParamList(&(*paramList), MyPID);
163
164 // Enable backtracking
165 paramList->sublist("NOX").sublist("Line Search").set("Method","Polynomial");
166
167 // Sublist for "Linear Solver"
168 Teuchos::ParameterList& lsParams =
169 paramList->sublist("NOX").sublist("Direction").sublist("Newton").sublist("Linear Solver");
170
171 // Sublist for "Printing"
172 Teuchos::ParameterList& printParams =
173 paramList->sublist("NOX").sublist("Printing");
174
175 // Change NOX priting settings if "-verbose" not requested
176 if (!verbose)
177 printParams.set("Output Information", NOX::Utils::Error);
178
179 // Create the interface between the test problem and the nonlinear solver
180 Teuchos::RCP<Problem_Interface> interface =
181 Teuchos::rcp(new Problem_Interface(Problem));
182
183 // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner
184 Teuchos::RCP<Epetra_RowMatrix> A =
185 Teuchos::rcp(&Problem.getJacobian(),false);
186
187 // Create initial guess
188 Epetra_MultiVector initGuess(soln.Map(),
189 globalComm->NumTimeStepsOnDomain());
190 for (int i=0; i<globalComm->NumTimeStepsOnDomain(); i++)
191 *(initGuess(i)) = soln;
192
193 // Get XYZT preconditioner linear solver parameters
194 Teuchos::RCP<Teuchos::ParameterList> precLSParams =
195 Teuchos::rcp(new Teuchos::ParameterList);
196 getPrecLSParams(&(*precLSParams), MyPID);
197
198 // Get XYZT preconditioner print parameters
199 Teuchos::RCP<Teuchos::ParameterList> precPrintParams =
200 Teuchos::rcp(new Teuchos::ParameterList);
201 getPrecPrintParams(&(*precPrintParams), MyPID);
202
203 // Change xyztPrec priting settings if "-verbose" not requested
204 if (!verbose)
205 precPrintParams->set("Output Information", NOX::Utils::Error);
206
207 // Create the XYZT object
208 Teuchos::RCP<LOCA::Epetra::Interface::xyzt> ixyzt =
209 Teuchos::rcp(new LOCA::Epetra::Interface::xyzt(interface,
210 initGuess, A,
211 globalComm,
212 soln, 0.5,
213 precPrintParams.get(),
214 precLSParams.get()));
215
216 // Create the XYZT operator, solution, and preconditioner
217 Teuchos::RCP<Epetra_RowMatrix> Axyzt =
218 Teuchos::rcp(&(ixyzt->getJacobian()),false);
219 Epetra_Vector& solnxyzt = ixyzt->getSolution();
220 Teuchos::RCP<Epetra_Operator> Mxyzt =
221 Teuchos::rcp(&(ixyzt->getPreconditioner()),false);
222
223 // Create the Linear System
224 Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = ixyzt;
225 Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = ixyzt;
226 Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec =
227 Teuchos::rcp(&(ixyzt->getPreconditioner()),false);
228 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys =
229 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams,
230 iJac, Axyzt,
231 iPrec, Mxyzt,
232 solnxyzt));
233
234 // Create full xyzt initial guess
235 NOX::Epetra::Vector initialGuess(solnxyzt);
236
237 // Create and initialize the parameter vector
238 LOCA::ParameterVector pVector;
239 pVector.addParameter("alpha",alpha);
240 pVector.addParameter("beta",beta);
241 pVector.addParameter("D1",D1);
242 pVector.addParameter("D2",D2);
243
244 // Create Epetra factory
245 Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory =
246 Teuchos::rcp(new LOCA::Epetra::Factory);
247
248 // Create global data object
249 Teuchos::RCP<LOCA::GlobalData> globalData =
250 LOCA::createGlobalData(paramList, epetraFactory);
251
252 Teuchos::RCP<LOCA::Epetra::Group> grp =
253 Teuchos::rcp(new LOCA::Epetra::Group(globalData, printParams,
254 iReq, initialGuess, linSys,
255 pVector));
256
257 grp->computeF();
258
259 // Create the convergence tests
260 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
261 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8,
262 NOX::StatusTest::NormF::Unscaled));
263 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
264 Teuchos::rcp(new NOX::StatusTest::MaxIters(MAX_NEWTON_ITERS));
265 Teuchos::RCP<NOX::StatusTest::Combo> combo =
266 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
267 combo->addStatusTest(absresid);
268 combo->addStatusTest(maxiters);
269
270 // Create stepper
271 LOCA::Stepper stepper(globalData, grp, combo, paramList);
272
273 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
274
275 if (status != LOCA::Abstract::Iterator::Finished) {
276 ierr =1;
277 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
278 globalData->locaUtils->out()
279 << "Stepper failed to converge!" << std::endl;
280 }
281
282 // Get the final solution from the stepper
283 Teuchos::RCP<const LOCA::Epetra::Group> finalGroup =
284 Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup());
285
286 // Output the parameter list
287 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
288 globalData->locaUtils->out()
289 << std::endl << "Final Parameters" << std::endl
290 << "****************" << std::endl;
291 stepper.getList()->print(globalData->locaUtils->out());
292 globalData->locaUtils->out() << std::endl;
293 }
294
295 // Check some statistics on the solution
296 NOX::TestCompare testCompare(globalData->locaUtils->out(),
297 *(globalData->locaUtils));
298
299 if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails))
300 globalData->locaUtils->out()
301 << std::endl
302 << "***** Checking solution statistics *****"
303 << std::endl;
304
305 // Check number of continuation steps
306 int numSteps = stepper.getStepNumber();
307 int numSteps_expected = 3;
308 ierr += testCompare.testValue(numSteps, numSteps_expected, 0.0,
309 "number of continuation steps",
310 NOX::TestCompare::Absolute);
311
312 // Check number of failed steps
313 int numFailedSteps = stepper.getNumFailedSteps();
314 int numFailedSteps_expected = 0;
315 ierr += testCompare.testValue(numFailedSteps, numFailedSteps_expected, 0.0,
316 "number of failed continuation steps",
317 NOX::TestCompare::Absolute);
318
319 // Check final value of continuation parameter
320 double beta_final = finalGroup->getParam("beta");
321 double beta_expected = 2.0;
322 ierr += testCompare.testValue(beta_final, beta_expected, 1.0e-14,
323 "final value of continuation parameter",
324 NOX::TestCompare::Relative);
325
326 // Check final value of ||F||
327 double Fnorm_final = (const_cast<Teuchos::ParameterList&>(*stepper.getList()).sublist("NOX").sublist("Output").get("2-Norm of Residual",1.0e+10));
328 double Fnorm_expected = 0.0;
329 ierr += testCompare.testValue(Fnorm_final, Fnorm_expected, 1.0e-8,
330 "final value of ||F||",
331 NOX::TestCompare::Absolute);
332
333 // Check number of nonlinear iterations on last continuation step
334 int nonlin_final = (const_cast<Teuchos::ParameterList&>(*stepper.getList()).sublist("NOX").
335 sublist("Output").get("Nonlinear Iterations",MAX_NEWTON_ITERS));
336 int nonlin_expected = 4;
337 ierr += testCompare.testValue(nonlin_final, nonlin_expected, 0.0,
338 "number of nonlinear iterations on last continuation step",
339 NOX::TestCompare::Absolute);
340
341
342 // initialize solution comparison norm on all procs
343 double solution_diff_norm = 0.0;
344
345 // Compute norm of difference of computed solution - expected solution
346 if (globalComm->MyPID() == (globalComm->NumProc()-1)) {
347 // Get solution at last time step
348 ixyzt->getSolution().ExtractBlockValues(soln,
349 (globalComm->NumTimeStepsOnDomain() +
350 globalComm->FirstTimeStepOnDomain() - 1));
351
352 // Check final solution at final time step
353 NOX::Epetra::Vector final_solution(soln);
354 NOX::Epetra::Vector final_x_expected(final_solution);
355 for (int i=0; i<NumGlobalNodes; i++) {
356 final_x_expected.getEpetraVector()[2*i] = alpha;
357 final_x_expected.getEpetraVector()[2*i+1] = beta_final/alpha;
358 }
359 solution_diff_norm = testCompare.computeVectorNorm(final_solution,
360 final_x_expected,
361 1.0e-4,
362 1.0e-4);
363
364 }
365
366 // Check final solution at final time step on all procs
367 globalComm->Broadcast(&solution_diff_norm, 1, globalComm->NumProc()-1);
368 double solution_diff_norm_expected = 0.0;
369 ierr += testCompare.testValue(solution_diff_norm, solution_diff_norm_expected, 1.0e-4,
370 "inf norm of (solution_final - solution_expected) at last time step",
371 NOX::TestCompare::Absolute);
372
373 LOCA::destroyGlobalData(globalData);
374
375 }
376 catch (std::exception& e) {
377 std::cout << e.what() << std::endl;
378 ierr = 1;
379 }
380 catch (const char *s) {
381 std::cout << s << std::endl;
382 ierr = 1;
383 }
384 catch (...) {
385 std::cout << "Caught unknown exception!" << std::endl;
386 ierr = 1;
387 }
388
389 if (MyPID == 0) {
390 if (ierr == 0)
391 std::cout << "All tests passed!" << std::endl;
392 else
393 std::cout << ierr << " test(s) failed!" << std::endl;
394 }
395
396 return ierr;
397 }
398
399 void getParamList(Teuchos::ParameterList *p, int MyPID) {
400 // Create LOCA sublist
401 Teuchos::ParameterList& locaParamsList = p->sublist("LOCA");
402
403 // Create the stepper sublist and set the stepper parameters
404 Teuchos::ParameterList& stepperList = locaParamsList.sublist("Stepper");
405 stepperList.set("Bordered Solver Method", "Householder");
406 stepperList.set("Continuation Parameter", "beta");
407 stepperList.set("Initial Value", BETA_INIT);
408 stepperList.set("Max Value", 2.0);
409 stepperList.set("Min Value", 0.0);
410 stepperList.set("Max Steps", 100);
411 stepperList.set("Max Nonlinear Iterations", MAX_NEWTON_ITERS);
412 stepperList.set("Compute Eigenvalues",false);
413
414 // Create predictor sublist
415 Teuchos::ParameterList& predictorList =
416 locaParamsList.sublist("Predictor");
417 predictorList.set("Method", "Constant");
418
419 // Create step size sublist
420 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
421 stepSizeList.set("Initial Step Size", 0.1);
422 stepSizeList.set("Min Step Size", 1.0e-3);
423 stepSizeList.set("Max Step Size", 10.0);
424 stepSizeList.set("Aggressiveness", 0.1);
425
426 // Create the "Solver" parameters sublist to be used with NOX Solvers
427 Teuchos::ParameterList& nlParams = p->sublist("NOX");
428
429 // Set the printing parameters in the "Printing" sublist
430 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
431 printParams.set("MyPID", MyPID);
432 printParams.set("Output Precision", 3);
433 printParams.set("Output Processor", 0);
434 printParams.set("Output Information",
435 NOX::Utils::OuterIteration +
436 NOX::Utils::OuterIterationStatusTest +
437 NOX::Utils::InnerIteration +
438 NOX::Utils::Details +
439 NOX::Utils::Warning +
440 NOX::Utils::TestDetails +
441 NOX::Utils::Error +
442 NOX::Utils::StepperIteration +
443 NOX::Utils::StepperDetails +
444 NOX::Utils::StepperParameters);
445
446 // Sublist for "Linear Solver"
447 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
448 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
449 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
450 lsParams.set("Aztec Solver", "GMRES");
451 lsParams.set("Max Iterations", 800);
452 lsParams.set("Tolerance", 1e-6);
453 lsParams.set("Output Frequency", AZ_all);
454 lsParams.set("Output Solver Details", true);
455 lsParams.set("Preconditioner", "User Defined");
456 }
457
458 void getPrecLSParams(Teuchos::ParameterList *p, int MyPID) {
459 p->set("Aztec Solver", "GMRES");
460 p->set("Preconditioner", "Ifpack");
461 p->set("Max Iterations", 800);
462 p->set("Tolerance", 1e-6);
463 p->set("Output Frequency", 50);
464 p->set("XYZTPreconditioner", "BDSDT");
465 p->set("Use Preconditioner as Solver", false);
466 p->set("Outer Use Preconditioner as Solver", false);
467 p->set("Periodic", false);
468 }
469
470 void getPrecPrintParams(Teuchos::ParameterList *p, int MyPID) {
471 p->set("MyPID", MyPID);
472 p->set("Output Precision", 3);
473 p->set("Output Processor", 0);
474 p->set("Output Information",
475 NOX::Utils::OuterIteration +
476 NOX::Utils::OuterIterationStatusTest +
477 NOX::Utils::InnerIteration +
478 NOX::Utils::Details +
479 NOX::Utils::Warning +
480 NOX::Utils::TestDetails +
481 NOX::Utils::Error +
482 NOX::Utils::StepperIteration +
483 NOX::Utils::StepperDetails +
484 NOX::Utils::StepperParameters);
485 }