Debian Codegraph @

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