Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/ml_nox_preconditioner2.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 ml_nox_preconditioner2.cpp
16 *
17 * \class ML_Nox_Preconditioner
18 *
19 * \brief ML nonlinear preconditioner and solver
20 *
21 * \date Last update do Doxygen: 31-Mar-05
22 *
23 */
24 // ML-headers
25 #include "ml_common.h"
26 #include "TrilinosCouplings_config.h"
27
28 #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)
29
30 #include <ctime>
31 #include <cstdlib>
32 #include <iostream>
33 #include <string>
34 #include <vector>
35
36 #ifdef ML_MPI // FIXME: Do we need this here?
37 #include <mpi.h>
38 #endif
39
40 // this class
41 #include "ml_nox_preconditioner.H"
42
43 /*----------------------------------------------------------------------*
44 | compute the nonlin multilevel preconditioner (private) m.gee 01/05|
45 | with Jacobian supplied |
46 *----------------------------------------------------------------------*/
47 bool ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Jacobian_Nonlinearpreconditioner(
48 const Epetra_Vector& xfine)
49 {
50 int i;
51
52 // extract the prolongators from the hierarchy
53 Epetra_CrsMatrix** P = new Epetra_CrsMatrix*[ml_nlevel_];
54 for (i=0; i<ml_nlevel_; i++)
55 P[i] = 0;
56 for (i=1; i<ml_nlevel_; i++) // there is no Pmat on level 0
57 {
58 double t1 = GetClock();
59 int maxnnz = 0;
60 double cputime;
61 ML_Operator2EpetraCrsMatrix(&(ml_->Pmat[i]), P[i], maxnnz, false, cputime);
62 P[i]->OptimizeStorage();
63 double t2 = GetClock() - t1;
64 if (ml_printlevel_>5 && 0 == comm_.MyPID())
65 cout << "ML (level " << i << "): extraction of P in " << t2 << " sec\n";
66 }
67
68 // create the vector of nonlinear levels if it does not exist yet
69 n_nlnlevel_ = ml_nlevel_;
70 if (!nlnLevel_)
71 {
72 nlnLevel_ = new ML_NOX::ML_Nox_NonlinearLevel*[n_nlnlevel_];
73 for (i=0; i<n_nlnlevel_; i++)
74 nlnLevel_[i] = 0;
75 }
76 else // the vector of nonlinear levels already exists
77 {
78 nlnLevel_[0]->destroyP();
79 for (i=0; i<n_nlnlevel_; i++)
80 {
81 nlnLevel_[i]->setP(NULL);
82 delete nlnLevel_[i];
83 nlnLevel_[i] = 0;
84 }
85 }
86
87 // loop all levels and create the nonlinear level class
88 for (i=0; i<n_nlnlevel_; i++)
89 {
90 // choose a blocks size
91 int nullspdim = ml_dim_nullsp_;
92 int numPDE = 0;
93 if (i==0)
94 numPDE = ml_numPDE_;
95 else if (ml_spatialDimension_==1)
96 numPDE = 1;
97 else if (ml_spatialDimension_==2)
98 numPDE = 3;
99 else if (ml_spatialDimension_==3)
100 numPDE = 6;
101 else
102 {
103 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Jacobian_Nonlinearpreconditioner:\n"
104 << "**ERR**: spatialDimension=" << ml_spatialDimension_ << " unknown\n"
105 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
106 }
107
108 // choose the nonlinear solver
109 bool isnlnCG = true;
110 int nitersCG = 100;
111 if (i==0)
112 {
113 isnlnCG = usenlnCG_fine_;
114 nitersCG = nitersCG_fine_;
115 }
116 else if (i == n_nlnlevel_-1)
117 {
118 isnlnCG = usenlnCG_coarse_;
119 nitersCG = nitersCG_coarse_;
120 }
121 else
122 {
123 isnlnCG = usenlnCG_;
124 nitersCG = nitersCG_;
125 }
126
127 // create new nonlinear level
128 if (!nlnLevel_[i])
129 nlnLevel_[i] = new ML_NOX::ML_Nox_NonlinearLevel(
130 i,ml_nlevel_,ml_printlevel_,ml_,ag_,P,
131 interface_,comm_,xfine,ismatrixfree_,
132 matfreelev0_,isnlnCG,nitersCG,
133 useBroyden_,fineJac_,ml_fsmoothertype_,
134 ml_smoothertype_,ml_coarsesolve_,
135 nsmooth_fine_,nsmooth_,nsmooth_coarse_,
136 FAS_normF_,FAS_nupdate_,
137 3000,numPDE,nullspdim);
138 else
139 {
140 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Jacobian_Nonlinearpreconditioner:\n"
141 << "**ERR**: nlnLevel_[i] != 0 ????????\n"
142 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
143 }
144
145 }
146
147 // in the nonlinear preconditioner, we don't need the ml hierarchy anymore
148 if (ag_)
149 {
150 ML_Aggregate_Destroy(&ag_);
151 ag_ = 0;
152 }
153 if (ml_)
154 {
155 ML_Destroy(&ml_);
156 ml_ = 0;
157 }
158
159 return(true);
160 }
161
162 /*----------------------------------------------------------------------*
163 | compute the nonlin multilevel preconditioner (private) m.gee 02/05|
164 | without Jacobian supplied |
165 *----------------------------------------------------------------------*/
166 bool ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Matrixfree_Nonlinearpreconditioner(
167 const Epetra_Vector& xfine)
168 {
169 int i;
170
171 // extract the prolongators from the hierarchy
172 Epetra_CrsMatrix** P = new Epetra_CrsMatrix*[ml_nlevel_];
173 for (i=0; i<ml_nlevel_; i++)
174 P[i] = 0;
175 for (i=1; i<ml_nlevel_; i++) // there is no Pmat on level 0
176 {
177 double t1 = GetClock();
178 int maxnnz = 0;
179 double cputime;
180 ML_Operator2EpetraCrsMatrix(&(ml_->Pmat[i]), P[i], maxnnz, false, cputime);
181 P[i]->OptimizeStorage();
182 double t2 = GetClock() - t1;
183 if (ml_printlevel_>5 && 0 == comm_.MyPID())
184 cout << "ML (level " << i << "): extraction of P in " << t2 << " sec\n";
185 }
186
187 // construct the vector of coarse level matrixfree problems
188 // the coarse level problems also create the coarse level interface to the user code
189 if (ml_matfreelevel_) // the matfreelevels already exist, clean them for reuse
190 {
191 ml_matfreelevel_[0]->destroyP();
192 for (i=0; i<nmatfreelevel_; i++)
193 ml_matfreelevel_[i]->setP(NULL);
194 }
195 else // create new vector of matfreelevels
196 {
197 nmatfreelevel_ = ml_nlevel_;
198 ml_matfreelevel_ = new ML_NOX::ML_Nox_MatrixfreeLevel*[nmatfreelevel_];
199 for (i=0; i<nmatfreelevel_; i++)
200 ml_matfreelevel_[i] = 0;
201 }
202
203 // create the vector of nonlinear levels if it does not exist yet
204 n_nlnlevel_ = ml_nlevel_;
205 if (!nlnLevel_)
206 {
207 nlnLevel_ = new ML_NOX::ML_Nox_NonlinearLevel*[n_nlnlevel_];
208 for (i=0; i<n_nlnlevel_; i++)
209 nlnLevel_[i] = 0;
210 }
211 else // the vector of nonlinear levels already exists
212 {
213 // NOTE: the matfree level and the nonlinear level share the
214 // coarseinterface, so they also share the vector of P's
215 // -> no need to destroyP again here
216 for (i=0; i<n_nlnlevel_; i++)
217 {
218 nlnLevel_[i]->setP(NULL);
219 delete nlnLevel_[i];
220 nlnLevel_[i] = 0;
221 }
222 delete [] nlnLevel_;
223 nlnLevel_ = new ML_NOX::ML_Nox_NonlinearLevel*[n_nlnlevel_];
224 for (i=0; i<n_nlnlevel_; i++)
225 nlnLevel_[i] = 0;
226 }
227
228 // the number of matrixfree levels has to be equal to the number of nonlinear levels
229 if (nmatfreelevel_ != n_nlnlevel_)
230 {
231 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Matrixfree_Nonlinearpreconditioner:\n"
232 << "**ERR**: nmatfreelevel_=" << nmatfreelevel_ << " != n_nlnlevel_=" << n_nlnlevel_ << endl
233 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
234 }
235
236 // loop all levels, create the matrixfree and then the nonlinear level class
237 for (i=0; i<n_nlnlevel_; i++)
238 {
239 if (comm_.MyPID()==0 && ml_printlevel_ > 5 )
240 cout << "ML (level " << i << "): Entering FD-coarselevel (re)construction\n"; fflush(stdout);
241 bool isJacobismoother=false;
242
243 // for Jacobi smoothing, compute main diagonal only
244 if (i==0 && ml_fsmoothertype_ == "Jacobi")
245 isJacobismoother=true;
246 else if (i<ml_coarsestlev_ && ml_smoothertype_ == "Jacobi")
247 isJacobismoother=true;
248 else
249 isJacobismoother=false;
250 if (i==ml_coarsestlev_ && ml_coarsesolve_ == "Jacobi")
251 isJacobismoother=true;
252
253 // when using Newton's method, compute complete Jacobian
254 if (i==0 && usenlnCG_fine_==false)
255 isJacobismoother=false;
256 if (i==n_nlnlevel_-1 && usenlnCG_coarse_==false)
257 isJacobismoother=false;
258 if (i!=0 && i!=n_nlnlevel_-1 && usenlnCG_==false)
259 isJacobismoother=false;
260
261 int bsize;
262 if (i==0) bsize = ml_numPDE_;
263 else bsize = ml_dim_nullsp_;
264
265 if (!(ml_matfreelevel_[i])) // create a new matrixfree level
266 ml_matfreelevel_[i] = new ML_NOX::ML_Nox_MatrixfreeLevel(
267 i,ml_nlevel_,ml_printlevel_,ml_,
268 ag_,P,interface_,comm_,xfine,
269 fd_alpha_,fd_beta_,fd_centered_,
270 isJacobismoother,bsize);
271 else // redo an existing level
272 ml_matfreelevel_[i]->recreateLevel(i,ml_nlevel_,ml_printlevel_,ml_,
273 ag_,P,interface_,comm_,xfine);
274
275 // get this level's operator
276 Epetra_CrsMatrix* tmpMat = ml_matfreelevel_[i]->getunderlyingMatrix();
277 if (!tmpMat)
278 {
279 cout << "**ERR**: ML_Nox_Preconditioner::ML_Nox_compute_Matrixfree_Linearpreconditioner:\n"
280 << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::getunderlyingMatrix() on level " << i << "\n"
281 << "**ERR**: returned NULL-ptr\n"
282 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
283 }
284
285 // check the matrix for zero rows and fix the main diagonal
286 if (fixdiagonal_)
287 fix_MainDiagonal(&tmpMat,i);
288 tmpMat->OptimizeStorage();
289
290 // get the coarse interface from the matfreelevel
291 ML_NOX::Nox_CoarseProblem_Interface* coarseinterface =
292 ml_matfreelevel_[i]->getCoarseinterface();
293 if (!coarseinterface)
294 {
295 cout << "**ERR**: ML_Nox_Preconditioner::ML_Nox_compute_Matrixfree_Linearpreconditioner:\n"
296 << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::getCoarseinterface() on level " << i << "\n"
297 << "**ERR**: returned NULL-ptr\n"
298 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
299 }
300
301 // choose a blocks size
302 int nullspdim = ml_dim_nullsp_;
303 int numPDE = 0;
304 if (i==0)
305 numPDE = ml_numPDE_;
306 else if (ml_spatialDimension_==1)
307 numPDE = 1;
308 else if (ml_spatialDimension_==2)
309 numPDE = 3;
310 else if (ml_spatialDimension_==3)
311 numPDE = 6;
312 else
313 {
314 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Jacobian_Nonlinearpreconditioner:\n"
315 << "**ERR**: spatialDimension=" << ml_spatialDimension_ << " unknown\n"
316 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
317 }
318
319 // choose the nonlinear solver
320 bool isnlnCG = true;
321 int nitersCG = 100;
322 if (i==0)
323 {
324 isnlnCG = usenlnCG_fine_;
325 nitersCG = nitersCG_fine_;
326 }
327 else if (i == n_nlnlevel_-1)
328 {
329 isnlnCG = usenlnCG_coarse_;
330 nitersCG = nitersCG_coarse_;
331 }
332 else
333 {
334 isnlnCG = usenlnCG_;
335 nitersCG = nitersCG_;
336 }
337
338 // create new nonlinear level
339 if (!nlnLevel_[i]) // create new nonlinear level
340 nlnLevel_[i] = new ML_NOX::ML_Nox_NonlinearLevel(
341 i,ml_nlevel_,ml_printlevel_,ml_,ag_,P,
342 interface_,comm_,xfine,ismatrixfree_,
343 isnlnCG,nitersCG,useBroyden_,
344 ml_fsmoothertype_,ml_smoothertype_,
345 ml_coarsesolve_,
346 nsmooth_fine_,nsmooth_,nsmooth_coarse_,
347 FAS_normF_,
348 FAS_nupdate_,3000,numPDE,nullspdim,tmpMat,
349 coarseinterface);
350 else
351 {
352 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_compute_Jacobian_Nonlinearpreconditioner:\n"
353 << "**ERR**: nlnLevel_[i] != 0 ????????\n"
354 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
355 }
356 } // loop over all levels
357
358 // in the nonlinear preocnditioner, we don't need the ml hierarchy anymore
359 if (ag_)
360 {
361 ML_Aggregate_Destroy(&ag_);
362 ag_ = 0;
363 }
364 if (ml_)
365 {
366 ML_Destroy(&ml_);
367 ml_ = 0;
368 }
369
370 return(true);
371 }
372
373 /*----------------------------------------------------------------------*
374 | apply preconditioner (private) m.gee 02/05|
375 | nonlinear, |
376 *----------------------------------------------------------------------*/
377 int ML_NOX::ML_Nox_Preconditioner::ML_Nox_ApplyInverse_NonLinear(
378 const Epetra_MultiVector& X,
379 Epetra_MultiVector& Y) const
380 {
381 if (!noxsolver_)
382 {
383 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::ML_Nox_ApplyInverse_NonLinear:\n"
384 << "**ERR**: noxsolver not registered, use set_nox_solver(solver)!\n"
385 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
386 }
387
388 // get the solution group from the nox solver
389 const NOX::EpetraNew::Group& finalGroup =
390 dynamic_cast<const NOX::EpetraNew::Group&>(noxsolver_->getSolutionGroup());
391 // get current guess and F
392 const Epetra_Vector& currentSolution =
393 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector();
394 const Epetra_Vector& currentF =
395 (dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getF())).getEpetraVector();
396 double norm2;
397 currentF.Norm2(&norm2);
398
399 // make a copy of currentSolution and currentF
400 Epetra_Vector* f = new Epetra_Vector(View,X,0);
401 Epetra_Vector* x = new Epetra_Vector(Copy,currentSolution,0);
402
403 // call the cycle
404 if (ml_printlevel_>0 && comm_.MyPID()==0)
405 cout << "\n\nML :============Entering Nonlinear V-cycle============\n";
406 bool converged = false;
407 double t3 = GetClock();
408 ML_Nox_FAS_cycle(f,x,0,&converged,&norm2);
409 double t4 = GetClock();
410 if (ml_printlevel_>0 && comm_.MyPID()==0)
411 cout << "ML :============V-cycle time is : " << (t4-t3) << " sec\n";
412 if (converged && ml_printlevel_>0 && comm_.MyPID()==0)
413 cout << "ML :============Nonlinear preconditioner converged====\n";
414
415 // copy correction to Y
416 Y.Scale(1.0,*x);
417
418 // tidy up
419 if (f) delete f;
420 if (x) delete x;
421
422 return(0);
423 }
424
425 /*----------------------------------------------------------------------*
426 | (public) m.gee 01/05|
427 | run a nonlinear FAS cycle solver with nlnCG on each level |
428 *----------------------------------------------------------------------*/
429 int ML_NOX::ML_Nox_Preconditioner::solve()
430 {
431 int i;
432 bool status=false;
433
434 // get the initial solution from the interface
435 const Epetra_Vector* xfine = interface_.getSolution();
436
437 // compute the preconditioner
438 double t5 = GetClock();
439 status = computePreconditioner(*xfine);
440 double t6 = GetClock();
441 if (ml_printlevel_>0 && comm_.MyPID()==0)
442 cout << "ML :============setup time : " << (t6-t5) << " sec\n\n\n";
443
444
445
446 // make sure everything is sane
447 if (isinit() != true)
448 {
449 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::solve:\n"
450 << "**ERR**: initflag is false\n"
451 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
452 }
453 if (islinearPrec_ != false)
454 {
455 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::solve:\n"
456 << "**ERR**: 'this' has not been initialized as nonlinear multigrid\n"
457 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
458 }
459 // make sure that nlnLevel_[level] exists for all levels we are going to use
460 if (!nlnLevel_)
461 {
462 cout << "**ERR**: ML_NOX::ML_Nox_Preconditioner::solve:\n"
463 << "**ERR**: nlnLevel_ is NULL\n"
464 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
465 }
466
467 // get the residual matching this solution
468 Epetra_Vector* f = new Epetra_Vector(xfine->Map(),false);
469 Epetra_Vector* x = new Epetra_Vector(Copy,*xfine,0);
470
471 nlnLevel_[0]->setModifiedSystem(false, NULL, NULL);
472 nlnLevel_[0]->computeF(*x,*f,NOX::EpetraNew::Interface::Required::Residual);
473
474 double t1 = GetClock();
475 {
476 for (i=1; i<=FAS_maxcycle_; i++)
477 {
478 if (ml_printlevel_>0 && comm_.MyPID()==0)
479 cout << "\n\n\nML :============Entering FAS-V-cycle # " << i << "============\n";
480 bool converged = false;
481 double t3 = GetClock();
482 status = ML_Nox_FAS_cycle1(f,x,0,&converged);
483 double t4 = GetClock();
484 if (ml_printlevel_>0 && comm_.MyPID()==0)
485 cout << "ML :============V-cycle time is : " << (t4-t3) << " sec\n";
486 if (converged)
487 {
488 double t2 = GetClock();
489 if (ml_printlevel_>0 && comm_.MyPID()==0)
490 cout << "ML :============FAS converged after " << i << " V-cycles============\n"
491 << "ML :============solution time : " << (t2-t1) << " sec\n\n\n";
492 if (f) delete f;
493 if (x) delete x;
494 return(0);
495 }
496 computePreconditioner(*x);
497 }
498 if (f) delete f;
499 if (x) delete x;
500 return(-1);
501 }
502 }
503
504
505 /*----------------------------------------------------------------------*
506 | (private) m.gee 02/05|
507 | the FAS cycle as a preconditioner |
508 | |
509 | f : on input the residual |
510 | x : on input the solution vector |
511 | both vectors match the level level |
512 | |
513 | f : on output nothing at the moment |
514 | x : on output a correction to the solution |
515 | on output the correction vector (level==0) |
516 *----------------------------------------------------------------------*/
517 bool ML_NOX::ML_Nox_Preconditioner::ML_Nox_FAS_cycle(Epetra_Vector* f, Epetra_Vector* x,
518 int level, bool* converged, double* finenorm) const
519 {
520 // we want to converge this level some digits better than the finer one
521 double thisnorm = *finenorm/10000;
522 if (thisnorm<FAS_normF_) thisnorm = FAS_normF_;
523
524 Epetra_Vector* fbar = 0;
525 Epetra_Vector* xbar = 0;
526 Epetra_Vector* fxbar = 0;
527
528 //======reached coarsest level========================================
529 if (level==ml_coarsestlev_)
530 {
531 // create the FAS-problem
532 fbar = new Epetra_Vector(Copy,*f,0);
533 xbar = new Epetra_Vector(Copy,*x,0);
534 fxbar = new Epetra_Vector(xbar->Map(),false);
535 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
536 nlnLevel_[level]->computeF(*xbar,*fxbar,NOX::EpetraNew::Interface::Required::Residual);
537 nlnLevel_[level]->setModifiedSystem(true,fbar,fxbar);
538 // iterate on the FAS-problem
539 if (FAS_coarsesmooth_>0)
540 *converged = nlnLevel_[level]->iterate(f,x,FAS_coarsesmooth_,&thisnorm);
541 // calculate the correction
542 x->Update(-1.0,*xbar,1.0);
543 // reset the system
544 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
545 // tidy up
546 delete fbar; fbar = 0;
547 delete xbar; xbar = 0;
548 delete fxbar; fxbar = 0;
549 return true;
550 }
551
552 //======set up FAS on this level=================
553 // copy input residual f for use in FAS-postsmoothing
554 fbar = new Epetra_Vector(Copy,*f,0);
555 xbar = new Epetra_Vector(Copy,*x,0);
556 fxbar = new Epetra_Vector(xbar->Map(),false);
557 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
558 nlnLevel_[level]->computeF(*xbar,*fxbar,NOX::EpetraNew::Interface::Required::Residual);
559 nlnLevel_[level]->setModifiedSystem(true,fbar,fxbar);
560
561 //======presmoothing on the FAS system===========================
562 double prenorm = thisnorm;
563 if (level > 0 && FAS_presmooth_>0)
564 *converged = nlnLevel_[level]->iterate(f,x,FAS_presmooth_,&prenorm);
565 else if (level==0 && FAS_prefinesmooth_>0)
566 *converged = nlnLevel_[level]->iterate(f,x,FAS_prefinesmooth_,&prenorm);
567 // a converged level is enough to be happy, don't go any coarser
568 if (*converged)
569 {
570 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
571 x->Update(-1.0,*xbar,1.0);
572 if (fbar) delete fbar; fbar = 0;
573 if (xbar) delete xbar; xbar = 0;
574 if (fxbar) delete fxbar; fxbar = 0;
575 return true;
576 }
577
578 //======restrict to next coarse level=================================
579 Epetra_Vector* xcoarse = nlnLevel_[level]->restrict_to_next_coarser_level(x,level,level+1);
580 Epetra_Vector* fcoarse = nlnLevel_[level]->restrict_to_next_coarser_level(f,level,level+1);
581
582 //======call this cycle on next coarser level=========================
583 bool coarseconverged=false;
584 ML_Nox_FAS_cycle(fcoarse,xcoarse,level+1,&coarseconverged,&prenorm);
585 delete fcoarse; fcoarse = 0;
586
587 //======prolong correction to this level==============================
588 Epetra_Vector *xcorrect = nlnLevel_[level]->prolong_to_this_level(xcoarse,level,level+1);
589 delete xcoarse; xcoarse = 0;
590
591 //===== apply constraints (this may or may not be in here, it converges better without,
592 // but is more stable with)
593 // nlnLevel_[level]->ApplyAllConstraints(*xcorrect);
594
595 //======update this level's solution==================================
596 x->Update(1.0,*xcorrect,1.0);
597 delete xcorrect; xcorrect = 0;
598
599 //======do this level's FAS-iteration or postsmoothing================
600 double postnorm = thisnorm;
601 // iterate
602 if (level>0 && FAS_postsmooth_>0)
603 *converged = nlnLevel_[level]->iterate(f,x,FAS_postsmooth_,&postnorm);
604 else if (level==0 && FAS_postfinesmooth_>0)
605 *converged = nlnLevel_[level]->iterate(f,x,FAS_postfinesmooth_,&postnorm);
606 // reset the system
607 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
608 // for FAS calulate the correction and tidy up
609 x->Update(-1.0,*xbar,1.0);
610 if (fbar) delete fbar; fbar = 0;
611 if (xbar) delete xbar; xbar = 0;
612 if (fxbar) delete fxbar; fxbar = 0;
613
614 return true;
615 }
616
617 /*----------------------------------------------------------------------*
618 | (private) m.gee 01/05|
619 | the FAS cycle as a solver |
620 | |
621 | f : on input the residual |
622 | x : on input the solution vector |
623 | both vectors match the level level |
624 | |
625 | f : on output nothing at the moment |
626 | x : on output a correction to the solution (level>0) |
627 | on output the solution vector (level==0) |
628 *----------------------------------------------------------------------*/
629 bool ML_NOX::ML_Nox_Preconditioner::ML_Nox_FAS_cycle1(Epetra_Vector* f, Epetra_Vector* x,
630 int level, bool* converged)
631 {
632 Epetra_Vector* fbar = 0;
633 Epetra_Vector* xbar = 0;
634 Epetra_Vector* fxbar = 0;
635
636 //======reached coarsest level========================================
637 if (level==ml_coarsestlev_)
638 {
639 // create the FAS-problem
640 fbar = new Epetra_Vector(Copy,*f,0);
641 xbar = new Epetra_Vector(Copy,*x,0);
642 fxbar = new Epetra_Vector(xbar->Map(),false);
643 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
644 nlnLevel_[level]->computeF(*xbar,*fxbar,NOX::EpetraNew::Interface::Required::Residual);
645 nlnLevel_[level]->setModifiedSystem(true,fbar,fxbar);
646 // iterate on the FAS-problem
647 if (FAS_coarsesmooth_>0)
648 *converged = nlnLevel_[level]->iterate(f,x,FAS_coarsesmooth_);
649 // calculate the correction
650 x->Update(-1.0,*xbar,1.0);
651 // reset the system
652 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
653 // tidy up
654 delete fbar; fbar = 0;
655 delete xbar; xbar = 0;
656 delete fxbar; fxbar = 0;
657 return true;
658 }
659
660 //======if not finest level, set up FAS on this level=================
661 if (level>0)
662 {
663 // copy input residual f for use in FAS-postsmoothing
664 fbar = new Epetra_Vector(Copy,*f,0);
665 xbar = new Epetra_Vector(Copy,*x,0);
666 fxbar = new Epetra_Vector(xbar->Map(),false);
667 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
668 nlnLevel_[level]->computeF(*xbar,*fxbar,NOX::EpetraNew::Interface::Required::Residual);
669 nlnLevel_[level]->setModifiedSystem(true,fbar,fxbar);
670 }
671
672 //======presmoothing on the original system===========================
673 if (level > 0 && FAS_presmooth_>0)
674 {
675 *converged = nlnLevel_[level]->iterate(f,x,FAS_presmooth_);
676 if (*converged)
677 {
678 // for FAS calulate the correction and tidy up
679 x->Update(-1.0,*xbar,1.0);
680 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
681 delete fbar; fbar = 0;
682 delete xbar; xbar = 0;
683 delete fxbar; fxbar = 0;
684 return true;
685 }
686 }
687 else if (level==0 && FAS_prefinesmooth_>0)
688 {
689 *converged = nlnLevel_[level]->iterate(f,x,FAS_prefinesmooth_);
690 // a converged fine level is enough to be happy....
691 if (*converged)
692 return true;
693 }
694
695 //======restrict to next coarse level=================================
696 Epetra_Vector* xcoarse = nlnLevel_[level]->restrict_to_next_coarser_level(x,level,level+1);
697 Epetra_Vector* fcoarse = nlnLevel_[level]->restrict_to_next_coarser_level(f,level,level+1);
698
699 //======call this cycle on next coarser level=========================
700 bool coarseconverged=false;
701 ML_Nox_FAS_cycle1(fcoarse,xcoarse,level+1,&coarseconverged);
702 delete fcoarse; fcoarse = 0;
703
704 //======prolong correction to this level==============================
705 Epetra_Vector *xcorrect = nlnLevel_[level]->prolong_to_this_level(xcoarse,level,level+1);
706 delete xcoarse; xcoarse = 0;
707
708 //======update this level's solution==================================
709 x->Update(1.0,*xcorrect,1.0);
710 delete xcorrect; xcorrect = 0;
711
712 //======do this level's FAS-iteration or postsmoothing================
713 if (level>0 && FAS_postsmooth_>0)
714 {
715 // iterate
716 *converged = nlnLevel_[level]->iterate(f,x,FAS_postsmooth_);
717 // reset the system
718 nlnLevel_[level]->setModifiedSystem(false,NULL,NULL);
719 // for FAS calulate the correction and tidy up
720 x->Update(-1.0,*xbar,1.0);
721 delete fbar; fbar = 0;
722 delete xbar; xbar = 0;
723 delete fxbar; fxbar = 0;
724 }
725 else if (FAS_postfinesmooth_>0) // level==0
726 {
727 // iterate
728 *converged = nlnLevel_[level]->iterate(f,x,FAS_postfinesmooth_);
729 }
730 return true;
731 }
732
733 #endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA)