Debian Codegraph @

packages/trilinoscouplings/examples/ml/NonlinML/ml_nox_1Delasticity_example.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // 1D Finite Element Test Problem
11 /* Solves the nonlinear equation:
12 *
13 * d2u
14 * --- - k * u**2 = 0
15 * dx2
16 *
17 * subject to @ x=0, u=1
18 */
19 // see documentation for the ml-class ML_NOX::ML_Nox_Preconditioner.
20 // compiling and running this example requires a bunch of packages:
21 // --enable-nox --enable-prerelease
22 // --enable-nox-epetra
23 // --enable-epetra
24 // --enable-epetraext
25 // --enable-teuchos
26 // --enable-ml --with-ml_nox
27 // --enable-aztecoo
28 // --enable-amesos
29 // due to cross-dependencies with nox, it is necessary to
30 // configure Trilinos and make install WITHOUT the --with-ml_nox option, then
31 // configure Trilinos and make install WITH the --with-ml_nox option again.
32 // The example should now work, if not, contact Michael Gee [email protected].
33 //
34 // usage:
35 // ml_nox_1Delasticity_example.exe <number_of_elements>
36 // Try a large number (otherwise You don't get several levels), let's say
37 // at least 50000
38
39 // ml objects
40 #include "ml_common.h"
41 #include "TrilinosCouplings_config.h"
42
43 #if defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT)
44
45 // ml objects
46 #include "nlnml_preconditioner.H"
47 #include "nlnml_linearsystem.H"
48
49 // NOX Objects
50 #include "NOX.H"
51 #include "NOX_Epetra.H"
52
53 // Trilinos Objects
54 #ifdef ML_MPI
55 #include "Epetra_MpiComm.h"
56 #else
57 #include "Epetra_SerialComm.h"
58 #endif
59 #include "Epetra_Map.h"
60 #include "Epetra_Vector.h"
61 #include "Epetra_RowMatrix.h"
62 #include "Epetra_CrsMatrix.h"
63 #include "Epetra_Map.h"
64 #include "Epetra_LinearProblem.h"
65 #include "AztecOO.h"
66
67 // User's application specific files
68 #include "Problem_Interface.H" // Interface to application
69 #include "FiniteElementProblem.H" // the application
70
71 using namespace std;
72 using namespace Teuchos;
73
74 int main(int argc, char *argv[])
75 {
76 // Initialize MPI
77 #ifdef HAVE_MPI
78 MPI_Init(&argc,&argv);
79 #endif
80
81 // Create a communicator for Epetra objects
82 #ifdef HAVE_MPI
83 Epetra_MpiComm Comm( MPI_COMM_WORLD );
84 #else
85 Epetra_SerialComm Comm;
86 #endif
87
88 // Get the process ID and the total number of processors
89 int MyPID = Comm.MyPID();
90 int NumProc = Comm.NumProc();
91
92 // Get the number of elements from the command line
93 int NumGlobalElements;
94 switch(argc) {
95 case 2:
96 NumGlobalElements = atoi(argv[1]) + 1;
97 break;
98 case 1:
99 NumGlobalElements = 5001;
100 break;
101 default:
102 cout << "Usage: " << argv[0] << " <number_of_elements>" << endl;
103 # ifdef HAVE_MPI
104 MPI_Finalize() ;
105 # endif
106 exit(EXIT_FAILURE);
107 break;
108 }
109 cout << NumGlobalElements << endl;
110
111 // The number of unknowns must be at least equal to the
112 // number of processors.
113 if (NumGlobalElements < NumProc) {
114 cout << "numGlobalBlocks = " << NumGlobalElements
115 << " cannot be < number of processors = " << NumProc << endl;
116 # ifdef HAVE_MPI
117 MPI_Finalize() ;
118 # endif
119 exit(EXIT_FAILURE);
120 }
121
122 // Create the FiniteElementProblem class. This creates all required
123 // Epetra objects for the problem and allows calls to the
124 // function (RHS) and Jacobian evaluation routines.
125 FiniteElementProblem nlnproblem(NumGlobalElements, Comm);
126
127 // Get the vector from the Problem
128 Epetra_Vector& soln = nlnproblem.getSolution();
129
130 // Initialize Solution
131 soln.PutScalar(1.0);
132
133 // evaluate the nonlinear function once to be safe
134 {
135 Epetra_Vector* rhs = new Epetra_Vector(Copy,soln,0);
136 nlnproblem.evaluate(ALL,&soln,rhs,NULL);
137 delete rhs; rhs = 0;
138 }
139
140
141 // Create the interface between the test problem and the nonlinear solver
142 // This is created by the user using inheritance of the abstract base class:
143 // Ml_Nox_Fineinterface (which inherits from NOX::EpetraNew:: - interfaces)
144 Teuchos::RefCountPtr<Problem_Interface> fineinterface =
145 rcp(new Problem_Interface(nlnproblem));
146
147 // Begin Nonlinear Solver ************************************
148 // Create the top level nox parameter list
149 Teuchos::ParameterList nlParams;
150 RefCountPtr<Teuchos::ParameterList> rcpparams = rcp(&nlParams);
151 rcpparams.release();
152
153 // Set the printing parameters in the "Printing" sublist
154 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
155 printParams.set("MyPID", MyPID);
156 printParams.set("Output Precision", 9);
157 printParams.set("Output Processor", 0);
158 printParams.set("Output Information",
159 NOX::Utils::OuterIteration +
160 NOX::Utils::Warning
161 );
162
163 //----------------- Newton------------------------------
164 Teuchos::ParameterList* lsparams = 0;
165 bool isnlnCG = true;
166 if (!isnlnCG)
167 {
168 // Set the nonlinear solver method
169 nlParams.set("Nonlinear Solver", "Line Search Based");
170
171 // Sublist for line search
172 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
173 searchParams.set("Method", "Full Step");
174
175 // Sublist for direction
176 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
177 dirParams.set("Method", "Newton");
178 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
179 newtonParams.set("Forcing Term Method", "Constant");
180 //newtonParams.set("Forcing Term Method", "Type 1");
181 //newtonParams.set("Forcing Term Method", "Type 2");
182 newtonParams.set("Forcing Term Minimum Tolerance", 1.0e-6);
183 newtonParams.set("Forcing Term Maximum Tolerance", 0.1);
184
185 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
186 lsParams.set("Aztec Solver", "CG");
187 lsParams.set("Max Iterations", 100);
188 lsParams.set("Tolerance", 1e-7);
189 lsParams.set("Output Frequency", 50);
190 lsparams = &lsParams;
191
192 //lsParams.set("Preconditioning", "AztecOO: Jacobian Matrix");
193 lsParams.set("Preconditioning", "User Supplied Preconditioner");
194 lsParams.set("Preconditioner","User Defined");
195
196 lsParams.set("Aztec Preconditioner", "ilu");
197 lsParams.set("Graph Fill", 2);
198 lsParams.set("Fill Factor", 1);
199 //-----------------------------------------------------------
200 }
201 else
202 {
203 //-----------------nonlinearCG------------------------------
204 // Set the nonlinear solver method as line search
205 nlParams.set("Nonlinear Solver", "Line Search Based");
206
207 // get sublist for type of linesearch
208 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
209
210 // set the nonlinearCG method
211 searchParams.set("Method", "NonlinearCG");
212
213 // Sublist for direction
214 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
215 dirParams.set("Method", "NonlinearCG");
216
217 // sublist for nlnCG params
218 Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG");
219 nlcgParams.set("Restart Frequency", 500);
220 //nlcgParams.set("Precondition", "Off");
221 nlcgParams.set("Precondition", "On");
222 nlcgParams.set("Orthogonalize", "Polak-Ribiere");
223 //nlcgParams.set("Orthogonalize", "Fletcher-Reeves");
224 nlcgParams.set("Restart Frequency", 25);
225
226 Teuchos::ParameterList& lsParams = nlcgParams.sublist("Linear Solver");
227 lsParams.set("Aztec Solver", "CG");
228 lsParams.set("Max Iterations", 1);
229 lsParams.set("Tolerance", 1e-11);
230 lsParams.set("Output Frequency", 50);
231 //lsParams.set("Preconditioning", "None");
232 lsParams.set("Preconditioning", "User Supplied Preconditioner");
233
234 // EpetraNew takes this as user supplied preconditioner
235 lsParams.set("Preconditioner","User Defined");
236
237 //lsParams.set("Preconditioning", "AztecOO: Jacobian Matrix");
238 lsParams.set("Aztec Preconditioner", "ilu");
239 lsParams.set("Graph Fill", 0);
240 lsParams.set("Fill Factor", 1);
241 }
242 // End Nonlinear Solver ************************************
243
244
245 // Begin Preconditioner ************************************
246 ParameterList mlparams;
247 mlparams.set("nlnML output", 6 ); // ML-output-level (0-10)
248 mlparams.set("nlnML max levels", 10 ); // max. # levels (minimum = 2 !)
249 mlparams.set("nlnML coarse: max size", 5000 ); // the size ML stops generating coarser levels
250 mlparams.set("nlnML is linear preconditioner", false );
251 mlparams.set("nlnML apply constraints", false );
252 mlparams.set("nlnML is matrixfree", false );
253 mlparams.set("nlnML finite difference fine level", false );
254 mlparams.set("nlnML finite difference alpha", 1.0e-08 );
255 mlparams.set("nlnML finite difference beta", 1.0e-07 );
256 mlparams.set("nlnML finite difference centered", false );
257 mlparams.set("nlnML Jacobian fix diagonal", false );
258
259 mlparams.set("nlnML absolute residual tolerance", 1.0e-06 );
260 mlparams.set("nlnML max cycles", 500 );
261 mlparams.set("nlnML adaptive recompute", 0.0 ); // recompute if residual is larger then this value
262 mlparams.set("nlnML offset recompute", 0 ); // every offset this preconditioner is recomputed
263 mlparams.set("nlnML additional adaptive nullspace", 0 ); // compute adaptive nullspace (additional kernel vectors)
264 mlparams.set("nlnML PDE equations", 1 ); // dof per node
265 mlparams.set("nlnML null space: dimension", 1 ); // dimension of nullspace
266 mlparams.set("nlnML spatial dimension", 1 );
267 mlparams.set("nlnML coarse: type", "Uncoupled"); // Uncoupled METIS VBMETIS
268 mlparams.set("nlnML nodes per aggregate", 3 ); // # nodes per agg for coarsening METIS and VBMETIS
269
270 mlparams.set("nlnML use nlncg on fine level", true); // use nlnCG or mod. Newton's method
271 mlparams.set("nlnML use nlncg on medium level", true);
272 mlparams.set("nlnML use nlncg on coarsest level", false);
273
274 mlparams.set("nlnML max iterations newton-krylov fine level", 0); // # iterations of lin. CG in mod. Newton's method
275 mlparams.set("nlnML max iterations newton-krylov medium level" , 0);
276 mlparams.set("nlnML max iterations newton-krylov coarsest level", 15);
277
278 mlparams.set("nlnML linear smoother type fine level", "MLS"); // SGS BSGS Jacobi MLS Bcheby AmesosKLU
279 mlparams.set("nlnML linear smoother type medium level", "MLS");
280 mlparams.set("nlnML linear smoother type coarsest level", "AmesosKLU");
281 mlparams.set("nlnML linear smoother sweeps fine level", 24);
282 mlparams.set("nlnML linear smoother sweeps medium level", 24);
283 mlparams.set("nlnML linear smoother sweeps coarsest level", 1);
284
285 mlparams.set("nlnML nonlinear presmoothing sweeps fine level", 1);
286 mlparams.set("nlnML nonlinear presmoothing sweeps medium level", 0);
287 mlparams.set("nlnML nonlinear smoothing sweeps coarse level", 2);
288 mlparams.set("nlnML nonlinear postsmoothing sweeps medium level", 2);
289 mlparams.set("nlnML nonlinear postsmoothing sweeps fine level", 3);
290
291 RefCountPtr<NLNML::NLNML_Preconditioner> Prec =
292 rcp(new NLNML::NLNML_Preconditioner(fineinterface,mlparams,Comm));
293
294 // End Preconditioner **************************************
295
296
297 // run the preconditioner as a solver **********************
298 #if 0
299 // the preconditioner can also act as a multigrid solver (without outer Krylov method)
300 if (mlparams.get("nlnML is linear preconditioner",true)==false)
301 {
302 int printlevel = mlparams.get("nlnML output",6);
303 int notconverged = Prec->solve();
304 double appltime = fineinterface->getsumtime();
305 if (printlevel>0 && Comm.MyPID()==0)
306 {
307 cout << "NOX/ML :===========of which time in ccarat : " << appltime << " sec\n";
308 cout << "NOX/ML :======number calls to computeF in this solve : " << fineinterface->getnumcallscomputeF() << "\n\n\n";
309 }
310 fineinterface->resetsumtime();
311 fineinterface->setnumcallscomputeF(0);
312 return notconverged;
313 }
314 #endif
315 // End run the preconditioner as a solver *******************
316
317 // creat initial guess
318 NOX::Epetra::Vector initialGuess(soln);
319
320
321 // for nlnCG:
322 NLNML::NLNML_LinearSystem* linSys = 0;
323 NOX::Epetra::LinearSystemAztecOO* azlinSys = 0;
324
325 if (isnlnCG)
326 {
327 RefCountPtr<NOX::Epetra::MatrixFree> B =
328 rcp(new NOX::Epetra::MatrixFree(printParams,fineinterface,soln,false));
329 // iPrec = &Prec;
330 // iJac = B;
331 // iReq = fineinterface.get();
332 bool matrixfree = mlparams.get("nlnML is matrixfree",false);
333 int ml_printlevel = mlparams.get("nlnML output",6);
334 linSys = new NLNML::NLNML_LinearSystem(B,B,Prec,null,Prec,matrixfree,0,ml_printlevel);
335 }
336 else
337 {
338 // for Newton:
339 RefCountPtr<Epetra_CrsMatrix> A = rcp(fineinterface->getJacobian());
340 A.release();
341 azlinSys = new NOX::Epetra::LinearSystemAztecOO(printParams,*lsparams,
342 fineinterface,A,Prec,
343 Prec,initialGuess);
344 }
345
346
347 RefCountPtr<NLNML::NLNML_LinearSystem> rcplinsys = rcp(linSys);
348 RefCountPtr<NOX::Epetra::LinearSystemAztecOO> rcpazlinsys = rcp(azlinSys);
349
350 // Create the Group
351 NOX::Epetra::Group* grp = 0;
352 if (isnlnCG)
353 grp = new NOX::Epetra::Group(printParams,fineinterface,initialGuess,rcplinsys);
354 else
355 grp = new NOX::Epetra::Group(printParams,fineinterface,initialGuess,rcpazlinsys);
356
357 RefCountPtr<NOX::Epetra::Group> rcpgrp = rcp(grp);
358
359 // Create the convergence tests
360 double FAS_normF = mlparams.get("nlnML absolute residual tolerance",1.0e-06);
361 RefCountPtr<NOX::StatusTest::NormF> absresid
362 = rcp(new NOX::StatusTest::NormF(FAS_normF));
363 RefCountPtr<NOX::StatusTest::NormUpdate> nupdate
364 = rcp(new NOX::StatusTest::NormUpdate(FAS_normF));
365 RefCountPtr<NOX::StatusTest::Combo> converged
366 = rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
367 converged->addStatusTest(absresid);
368 converged->addStatusTest(nupdate);
369
370 RefCountPtr<NOX::StatusTest::FiniteValue> fv =
371 rcp(new NOX::StatusTest::FiniteValue());
372 RefCountPtr<NOX::StatusTest::MaxIters> maxiters =
373 rcp(new NOX::StatusTest::MaxIters(250));
374 RefCountPtr<NOX::StatusTest::Combo> combo =
375 rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
376 combo->addStatusTest(maxiters);
377 combo->addStatusTest(converged);
378 combo->addStatusTest(fv);
379
380 // Create the method
381 RCP<NOX::Solver::Generic> rcpsolver = NOX::Solver::buildSolver(rcpgrp,combo,rcpparams);
382 #if 0
383 NOX::Solver::Manager solver(rcpgrp, combo, rcpparams);
384 RefCountPtr<NOX::Solver::Manager> rcpsolver =
385 rcp(&solver);
386 rcpsolver.release();
387 #endif
388 // register the solver class in the preconditioner in case of nonlinear preconditioning
389 Prec->SetNoxSolver(rcpsolver);
390
391
392 // solve
393 double t0 = GetClock();
394 NOX::StatusTest::StatusType status = rcpsolver->solve();
395 double t1 = GetClock();
396 int ml_printlevel = mlparams.get("nlnML output",6);
397 if (ml_printlevel>0 && Comm.MyPID()==0)
398 cout << "NOX/ML :============solve time incl. setup : " << (t1-t0) << " sec\n";
399 double appltime = fineinterface->getsumtime();
400 if (ml_printlevel>0 && Comm.MyPID()==0)
401 {
402 cout << "NOX/ML :===========of which time in application : " << appltime << " sec\n";
403 cout << "NOX/ML :======number calls to computeF in this solve : "
404 << fineinterface->getnumcallscomputeF() << "\n\n\n";
405 }
406 fineinterface->resetsumtime();
407 fineinterface->setnumcallscomputeF(0);
408
409 if (status != NOX::StatusTest::Converged)
410 {
411 if (Comm.MyPID()==0)
412 cout << "***WRN***: ML/NOX not converged!";
413 # ifdef HAVE_MPI
414 MPI_Finalize() ;
415 # endif
416 exit(EXIT_FAILURE);
417 }
418
419 #ifdef HAVE_MPI
420 MPI_Finalize();
421 #endif
422 exit(EXIT_SUCCESS);
423
424 } /* end main */
425
426
427
428 #else
429 // ml objects
430 #include <iostream>
431 #include "ml_common.h"
432 int main(int argc, char *argv[])
433 {
434 std::cout << "running ml_nox_1Delasticity_example.exe needs: \n"
435 << "defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT)\n"
436 << "see documentation for the ml-class ML_NOX::ML_Nox_Preconditioner\n";
437 std::cout.flush();
438 return 0;
439 }
440 #endif