Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/nlnml_nonlinearlevel.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 /* ******************************************************************** */
11 /* See the file COPYRIGHT for a complete copyright notice, contact */
12 /* person and disclaimer. */
13 /* ******************************************************************** */
14 /*!
15 * \file nlnml_preconditioner1.cpp
16 *
17 * \class NLNML_NonlinearLevel
18 *
19 * \brief a nonlinear coarse grid class
20 *
21 * \date Last update do Doxygen: 31-Mar-05
22 *
23 */
24 #include "ml_common.h"
25 #include "TrilinosCouplings_config.h"
26
27 #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)
28
29 // ---------- Includes ----------
30 #include <ctime>
31 #include <cstdlib>
32 #include <iostream>
33 #include <string>
34 #include <vector>
35
36 // this class
37 #include "nlnml_nonlinearlevel.H"
38
39 using namespace std;
40
41 /*----------------------------------------------------------------------*
42 | ctor (public) m.gee 3/06|
43 *----------------------------------------------------------------------*/
44 NLNML::NLNML_NonlinearLevel::NLNML_NonlinearLevel(
45 int level, RefCountPtr<Teuchos::ParameterList> params,
46 ML* ml, ML_Aggregate* ag,
47 RefCountPtr< vector< RefCountPtr<Epetra_CrsMatrix> > > P,
48 RefCountPtr<NLNML::NLNML_FineLevelNoxInterface> finterface,
49 const Epetra_Comm& comm,
50 RefCountPtr<Epetra_CrsMatrix> fineJac,
51 const Epetra_Vector& xfine,
52 bool isnlncg, int niterscg,
53 int numpde, int dimns) :
54 level_(level),
55 isnlncg_(isnlncg),
56 ml_(ml),
57 ag_(ag),
58 thislevel_ml_(NULL),
59 thislevel_ag_(NULL),
60 params_(params),
61 P_(P),
62 fineinterface_(finterface),
63 comm_(comm),
64 fineJac_(fineJac)
65 {
66 // get Jacobian of this level
67 const Epetra_CrsGraph* graph = 0;
68 if (!Level()) // fine grid
69 {
70 graph = &(fineJac->Graph());
71 SmootherA_ = fineJac;
72 }
73 else
74 {
75 // on coarse grids get Jacobian from hierarchy
76 int maxnnz = 0;
77 double cputime = 0.0;
78 Epetra_CrsMatrix* tmp;
79 ML_Operator2EpetraCrsMatrix(&(ml_->Amat[level_]),tmp,maxnnz,
80 false, cputime);
81 SmootherA_ = rcp(tmp);
82 SmootherA_->OptimizeStorage();
83 graph = &(SmootherA_->Graph());
84 }
85
86 // generate the coarse level interface
87 int outlevel = getParameter("nlnML output",0);
88 int maxlevel = getParameter("nlnML max levels",2);
89 coarseinterface_ =
90 rcp(new NLNML::NLNML_CoarseLevelNoxInterface(fineinterface_,Level(),outlevel,
91 P_,graph->RowMap(),maxlevel));
92
93 // on the fine grid, generate a pre/postoperator to apply constraints
94 bool applyconstraints = false;
95 if (!Level() && getParameter("nlnML apply constraints",false)==true)
96 applyconstraints = true;
97
98 if (!Level() && applyconstraints)
99 prepost_ = rcp(new NLNML::NLNML_PrePostOperator(coarseinterface_,
100 fineinterface_));
101
102 // get the current solution to this level
103 xthis_ = rcp(coarseinterface_->restrict_fine_to_this(xfine));
104
105 // create this level's preconditioner
106 ML_Aggregate_Create(&thislevel_ag_);
107 ML_Create(&thislevel_ml_,1);
108
109 // set the Jacobian on level 0 of the local ml
110 EpetraMatrix2MLMatrix(thislevel_ml_,0,SmootherA_.get());
111 ML_Set_PrintLevel(OutLevel());
112 ML_Aggregate_Set_CoarsenScheme_Uncoupled(thislevel_ag_);
113 ML_Aggregate_Set_DampingFactor(thislevel_ag_, 0.0);
114 ML_Aggregate_Set_Threshold(thislevel_ag_, 0.0);
115 ML_Aggregate_Set_MaxCoarseSize(thislevel_ag_,1);
116 ML_Aggregate_Set_NullSpace(thislevel_ag_,numpde,dimns,NULL,
117 SmootherA_->NumMyRows());
118 int thislevel_nlevel = ML_Gen_MGHierarchy_UsingAggregation(thislevel_ml_,0,
119 ML_INCREASING,thislevel_ag_);
120 if (thislevel_nlevel != 1)
121 {
122 cout << "**ERR**: NLNML::NLNML_NonlinearLevel::NLNML_NonlinearLevel:\n"
123 << "**ERR**: ML generated a local hierarchy of " << thislevel_nlevel << " on level " << level_ << "\n"
124 << "**ERR**: this is supposed to be 1 Level only!\n"
125 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
126 }
127
128 // set the smoother
129 string smoothertype;
130 int nsmooth;
131 if (!Level())
132 {
133 smoothertype = getParameter("nlnML linear smoother type fine level",(string)"SGS");
134 nsmooth = getParameter("nlnML linear smoother sweeps fine level",2);
135 }
136 else if (Level() != maxlevel-1)
137 {
138 smoothertype = getParameter("nlnML linear smoother type coarsest level",(string)"SGS");
139 nsmooth = getParameter("nlnML linear smoother sweeps coarsest level",2);
140 }
141 else
142 {
143 smoothertype = getParameter("nlnML linear smoother type medium level",(string)"SGS");
144 nsmooth = getParameter("nlnML linear smoother sweeps medium level",2);
145 }
146 Set_Smoother(ml_,ag_,Level(),maxlevel,thislevel_ml_,thislevel_ag_,smoothertype,nsmooth);
147
148 // create this level's preconditioner class
149 ML_Epetra::MultiLevelOperator* ml_tmp = new ML_Epetra::MultiLevelOperator(
150 thislevel_ml_,comm_,
151 SmootherA_->OperatorDomainMap(),
152 SmootherA_->OperatorRangeMap());
153 RefCountPtr<ML_Epetra::MultiLevelOperator> rcpml_tmp = rcp(ml_tmp);
154
155 thislevel_prec_ = rcp(new NLNML::NLNML_ConstrainedMultiLevelOperator(
156 rcpml_tmp,
157 coarseinterface_,
158 applyconstraints));
159
160 // ------------------------------------------------------------------------
161 // set up NOX on this level
162 // ------------------------------------------------------------------------
163 nlparams_ = rcp(new Teuchos::ParameterList());
164 Teuchos::ParameterList& printParams = nlparams_->sublist("Printing");
165 printParams.set("MyPID", comm_.MyPID());
166 printParams.set("Output Precision", 14);
167 printParams.set("Output Processor", 0);
168 if (OutLevel()>9)
169 printParams.set("Output Information",
170 NOX::Utils::OuterIteration +
171 NOX::Utils::Warning);
172 else if (OutLevel()>8)
173 printParams.set("Output Information",
174 NOX::Utils::Warning);
175 else
176 printParams.set("Output Information",0);
177
178 if (!Level() && applyconstraints)
179 nlparams_->sublist("Solver Options").set("User Defined Pre/Post Operator",prepost_);
180
181 nlparams_->set("Nonlinear Solver", "Line Search Based");
182 Teuchos::ParameterList& searchParams = nlparams_->sublist("Line Search");
183 Teuchos::ParameterList* lsParamsptr = 0;
184 if (isnlncg)
185 {
186 searchParams.set("Method", "NonlinearCG");
187 Teuchos::ParameterList& dirParams = nlparams_->sublist("Direction");
188 dirParams.set("Method", "NonlinearCG");
189 Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG");
190 nlcgParams.set("Restart Frequency", 10);
191 nlcgParams.set("Precondition", "On");
192 nlcgParams.set("Orthogonalize", "Polak-Ribiere");
193 //nlcgParams.set("Orthogonalize", "Fletcher-Reeves");
194
195 Teuchos::ParameterList& lsParams = nlcgParams.sublist("Linear Solver");
196 lsParams.set("Aztec Solver", "CG");
197 lsParams.set("Max Iterations", 1);
198 lsParams.set("Tolerance", 1e-11);
199 lsParams.set("Output Frequency", 0);
200 lsParams.set("Preconditioning", "User Supplied Preconditioner");
201 lsParams.set("Preconditioner","User Defined");
202 }
203 else
204 {
205 searchParams.set("Method", "Full Step");
206 Teuchos::ParameterList& dirParams = nlparams_->sublist("Direction");
207 dirParams.set("Method", "Newton");
208 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
209 newtonParams.set("Forcing Term Method", "Constant");
210 //newtonParams.set("Forcing Term Method", "Type 1");
211 //newtonParams.set("Forcing Term Method", "Type 2");
212 newtonParams.set("Forcing Term Minimum Tolerance", 1.0e-6);
213 newtonParams.set("Forcing Term Maximum Tolerance", 0.1);
214
215 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
216 lsParamsptr = &lsParams;
217 lsParams.set("Size of Krylov Subspace", 100);
218 lsParams.set("Aztec Solver", "GMRES");
219 lsParams.set("Max Iterations", niterscg);
220 double norm = getParameter("nlnML absolute residual tolerance",1.0e-06);
221 lsParams.set("Tolerance",norm);
222 if (OutLevel()>9)
223 lsParams.set("Output Frequency",10);
224 else
225 lsParams.set("Output Frequency",0);
226 lsParams.set("Preconditioning", "User Supplied Preconditioner");
227 lsParams.set("Preconditioner","User Defined");
228 }
229
230 // create the initial guess
231 NOX::Epetra::Vector initialguess(xthis_);
232
233 // create interfaces
234 thislevel_A_ = rcp(new NOX::Epetra::MatrixFree(printParams,coarseinterface_,*xthis_,false));
235 if (isnlncg)
236 {
237 thislevel_linSys_ =
238 rcp( new NLNML::NLNML_LinearSystem(thislevel_A_,thislevel_A_,null,
239 coarseinterface_,thislevel_prec_,
240 true,Level(),OutLevel()));
241 group_ = rcp(new NOX::Epetra::Group(printParams,coarseinterface_,initialguess,
242 thislevel_linSys_));
243 }
244 else // Newton's method
245 {
246 RefCountPtr<NOX::Epetra::Interface::Preconditioner> iPrec = rcp(this);
247 iPrec.release();
248 azlinSys_ =
249 rcp(new NOX::Epetra::LinearSystemAztecOO(printParams,*lsParamsptr,
250 thislevel_A_,thislevel_A_,
251 iPrec,thislevel_prec_,
252 initialguess));
253 group_ = rcp(new NOX::Epetra::Group(printParams,coarseinterface_,initialguess,
254 azlinSys_));
255 }
256
257 // create some convergence test
258 double norm = getParameter("nlnML absolute residual tolerance",1.0e-06);
259 create_Nox_Convergencetest(norm,norm,1);
260
261 // create the solver
262 //JJH
263 //solver_ = rcp(new NOX::Solver::Manager(group_,combo2_,nlparams_));
264 solver_ = NOX::Solver::buildSolver(group_,combo2_,nlparams_);
265
266
267 return;
268 }
269
270
271 /*----------------------------------------------------------------------*
272 | dtor (public) m.gee 3/06|
273 *----------------------------------------------------------------------*/
274 NLNML::NLNML_NonlinearLevel::~NLNML_NonlinearLevel()
275 {
276 if (thislevel_ag_) {
277 ML_Aggregate_Destroy(&thislevel_ag_);
278 thislevel_ag_ = NULL;
279 }
280 if (thislevel_ml_) {
281 ML_Destroy(&thislevel_ml_);
282 thislevel_ml_ = NULL;
283 }
284 return;
285 }
286
287
288 /*----------------------------------------------------------------------*
289 | compute this preconditioner (public, derived) m.gee 3/06|
290 *----------------------------------------------------------------------*/
291 bool NLNML::NLNML_NonlinearLevel::computePreconditioner(
292 const Epetra_Vector& x,
293 Epetra_Operator& M,
294 Teuchos::ParameterList* precParams)
295 {
296 return true;
297 }
298
299
300 /*----------------------------------------------------------------------*
301 | compute this Jacobian (public, derived) m.gee 3/06|
302 *----------------------------------------------------------------------*/
303 bool NLNML::NLNML_NonlinearLevel::computeJacobian(const Epetra_Vector& x,
304 Epetra_Operator& Jac)
305 {
306 return true;
307 }
308
309 /*----------------------------------------------------------------------*
310 | (private) m.gee 3/06|
311 *----------------------------------------------------------------------*/
312 bool NLNML::NLNML_NonlinearLevel::Set_Smoother(ML* ml, ML_Aggregate* ag,
313 int level, int nlevel,
314 ML* thislevel_ml,
315 ML_Aggregate* thislevel_ag,
316 string smoothertype,
317 int nsmooth)
318 {
319 if (smoothertype=="SGS")
320 ML_Gen_Smoother_SymGaussSeidel(thislevel_ml,0,ML_BOTH,nsmooth,0.67);
321 else if (smoothertype == "Jacobi")
322 ML_Gen_Smoother_Jacobi(thislevel_ml,0,ML_BOTH,nsmooth,0.2);
323 else if (smoothertype == "AmesosKLU")
324 ML_Gen_Smoother_Amesos(thislevel_ml,0,ML_AMESOS_KLU,-1,0.0,1);
325 else if ((smoothertype == "MLS")||(smoothertype == "Cheby"))
326 ML_Gen_Smoother_Cheby(thislevel_ml,0,ML_BOTH,30.,nsmooth);
327 else if (smoothertype == "BSGS")
328 {
329 int nblocks = 0;
330 int* blocks = NULL;
331 int* blockpde = NULL;
332
333 // try to get nodal blocks from the VBMETIS aggregation scheme
334 ML_Aggregate_Get_Vblocks_CoarsenScheme_VBMETIS(ag,level,nlevel,
335 &nblocks,&blocks,&blockpde);
336
337 if (nblocks && blocks);
338 else
339 ML_Gen_Blocks_Aggregates(ag,level,&nblocks,&blocks);
340
341 ML_Gen_Smoother_VBlockSymGaussSeidel(thislevel_ml,0,ML_BOTH,nsmooth,1.,
342 nblocks,blocks);
343 if (nblocks && blocks)
344 {
345 ML_free(blocks);
346 ML_free(blockpde);
347 }
348 }
349 else if (smoothertype == "Bcheby")
350 {
351 int nblocks = 0;
352 int* blocks = NULL;
353 int* blockpde = NULL;
354
355 // try to get nodal blocks from the VBMETIS aggregation scheme
356 ML_Aggregate_Get_Vblocks_CoarsenScheme_VBMETIS(ag,level,nlevel,
357 &nblocks,&blocks,&blockpde);
358
359 if (nblocks && blocks);
360 else
361 ML_Gen_Blocks_Aggregates(ag,level,&nblocks,&blocks);
362
363 ML_Gen_Smoother_BlockDiagScaledCheby(thislevel_ml,0,ML_BOTH,30.,nsmooth,
364 nblocks,blocks);
365 if (nblocks && blocks)
366 {
367 ML_free(blocks);
368 ML_free(blockpde);
369 }
370 }
371 else
372 {
373 cout << "**ERR**: NLNML::NLNML_NonlinearLevel::Set_Smoother:\n"
374 << "**ERR**: unknown type of smoother: " << smoothertype << " on level " << Level() << "\n"
375 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
376 }
377 return true;
378 }
379
380
381
382 /*----------------------------------------------------------------------*
383 | (private) m.gee 3/06|
384 *----------------------------------------------------------------------*/
385 void NLNML::NLNML_NonlinearLevel::create_Nox_Convergencetest(double normf,
386 double norm_update,
387 int maxiter)
388 {
389 absresid_ = rcp(new NOX::StatusTest::NormF(normf,NOX::StatusTest::NormF::Unscaled));
390 nupdate_ = rcp(new NOX::StatusTest::NormUpdate(norm_update,NOX::StatusTest::NormUpdate::Unscaled));
391 combo1_ = rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
392 combo1_->addStatusTest(absresid_);
393 combo1_->addStatusTest(nupdate_);
394
395 fv_ = rcp(new NOX::StatusTest::FiniteValue());
396 maxiters_ = rcp(new NOX::StatusTest::MaxIters(maxiter));
397 combo2_ = rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
398 combo2_->addStatusTest(fv_);
399 combo2_->addStatusTest(maxiters_);
400 combo2_->addStatusTest(combo1_);
401 return;
402 }
403
404 /*----------------------------------------------------------------------*
405 | (private) m.gee 3/06|
406 *----------------------------------------------------------------------*/
407 bool NLNML::NLNML_NonlinearLevel::Iterate(Epetra_Vector* f,
408 Epetra_Vector* x,
409 int numiter,
410 double* norm)
411 {
412 if (solver_==null || group_==null || combo2_==null)
413 {
414 cout << "**ERR**: NLNML::NLNML_NonlinearLevel::Iterate:\n"
415 << "**ERR**: group_ || solver_ || combo2_ or even some of them are NULL\n"
416 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
417 }
418
419 // put x into group_
420 NOX::Epetra::Vector X(*x,NOX::DeepCopy);
421 group_->setX(X);
422
423 // recreate the convergence test
424 create_Nox_Convergencetest(*norm,*norm,numiter);
425
426 // make a soft reset of the NOX solver
427 //JJH
428 //solver_->reset(group_,combo2_);
429 solver_->reset(group_->getX(),combo2_);
430
431 if (OutLevel() && !Comm().MyPID() && !coarseinterface_->isFAS())
432 {
433 printf("nlnML (level %d): Entering Nonlinear Smoother, Goal: %12.8e\n",level_,*norm);
434 fflush(stdout);
435 }
436 else if (OutLevel() && !Comm().MyPID() && coarseinterface_->isFAS())
437 {
438 printf("nlnML (level %d): Entering FAS-Nonlinear Smoother, Goal: %12.8e\n",level_,*norm);
439 fflush(stdout);
440 }
441
442 // iterate
443 NOX::StatusTest::StatusType status = solver_->solve();
444
445 // get # iterations
446 int niter = solver_->getNumIterations();
447
448 // get solution and F
449 const NOX::Epetra::Group& finalGroup =
450 dynamic_cast<const NOX::Epetra::Group&>(solver_->getSolutionGroup());
451 const Epetra_Vector& finalSolution =
452 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
453 const Epetra_Vector& finalF =
454 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getF())).getEpetraVector();
455
456 bool returnstatus=false;
457 double norm2;
458 finalF.Norm2(&norm2);
459 *norm = norm2;
460
461 if (status == NOX::StatusTest::Converged)
462 {
463 returnstatus = true;
464 if (OutLevel() > 0 && Comm().MyPID() == 0) {
465 //cout << "ML (level " << level_ << "): NOX: "
466 // << niter << " iterations, Norm(F)="
467 // << norm2 << " , Converged\n"; fflush(stdout);
468 printf("nlnML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Converged\n",level_,niter,norm2);
469 fflush(stdout);
470 }
471 }
472 else if (status == NOX::StatusTest::Unconverged)
473 {
474 returnstatus = false;
475 if (OutLevel() > 0 && Comm().MyPID() == 0) {
476 //cout << "ML (level " << level_ << "): NOX: "
477 // << niter << " iterations, Norm(F)="
478 // << norm2 << ", Unconverged\n"; fflush(stdout);
479 printf("nlnML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Unonverged\n",level_,niter,norm2);
480 fflush(stdout);
481 }
482 }
483 else if (status == NOX::StatusTest::Failed)
484 {
485 returnstatus = false;
486 if (OutLevel() > 0 && Comm().MyPID() == 0) {
487 //cout << "ML (level " << level_ << "): NOX: "
488 // << niter << " iterations, Norm(F)=" << norm2 << ", Failed\n"; fflush(stdout);
489 printf("nlnML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Failed\n",level_,niter,norm2);
490 fflush(stdout);
491 }
492 }
493 else
494 {
495 returnstatus = false;
496 if (Comm().MyPID() == 0) {
497 //cout << "ML (level " << level_ << "): ***WRN*** NOX returned unknown status, Norm(F)="
498 // << norm2 << "\n"; fflush(stdout);
499 printf("nlnML (level %d): ***WRN*** NOX: return status unknown, Norm(F)=%12.8e , Failed\n",level_,norm2);
500 fflush(stdout);
501 }
502 }
503
504 // reset number of calls to coarseinterface->computeF
505 coarseinterface_->resetnumcallscomputeF();
506
507 // update the solution
508 x->Update(1.0,finalSolution,0.0);
509 f->Update(1.0,finalF,0.0);
510
511 return returnstatus;
512 }
513 #endif