Debian Codegraph @

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