| 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 |
// ML-headers |
| 15 |
#include "ml_common.h" |
| 16 |
#include "ml_include.h" |
| 17 |
#include "ml_agg_VBMETIS.h" |
| 18 |
#include "TrilinosCouplings_config.h" |
| 19 |
|
| 20 |
#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) |
| 21 |
|
| 22 |
#include <ctime> |
| 23 |
#include <cstdlib> |
| 24 |
#include <iostream> |
| 25 |
#include <string> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#ifdef ML_MPI // FIXME: do I need this? |
| 29 |
#include <mpi.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
// this class |
| 33 |
#include "ml_nox_nonlinearlevel.H" |
| 34 |
|
| 35 |
/*----------------------------------------------------------------------* |
| 36 |
| the class defining a nonlinear coarse level m.gee 01/05| |
| 37 |
*----------------------------------------------------------------------*/ |
| 38 |
/*----------------------------------------------------------------------* |
| 39 |
| Constructor (public) m.gee 01/05| |
| 40 |
| IMPORTANT: | |
| 41 |
| No matter on which level we are here, the vector xfine is ALWAYS | |
| 42 |
| a fine grid vector here! | |
| 43 |
| this is the constructor for the ismatrixfree==false case |
| 44 |
*----------------------------------------------------------------------*/ |
| 45 |
ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel( |
| 46 |
int level, int nlevel, int printlevel, ML* ml, |
| 47 |
ML_Aggregate* ag,Epetra_CrsMatrix** P, |
| 48 |
ML_NOX::Ml_Nox_Fineinterface& interface, |
| 49 |
const Epetra_Comm& comm, const Epetra_Vector& xfine, |
| 50 |
bool ismatrixfree, bool matfreelev0, bool isnlnCG, |
| 51 |
int nitersCG, bool broyden, Epetra_CrsMatrix* Jac, |
| 52 |
string fsmoothertype, string smoothertype, |
| 53 |
string coarsesolvetype, |
| 54 |
int nsmooth_fine, int nsmooth, int nsmooth_coarse, |
| 55 |
double conv_normF, double conv_nupdate, |
| 56 |
int conv_maxiter,int numPDE, int nullspdim) |
| 57 |
: fineinterface_(interface), |
| 58 |
comm_(comm) |
| 59 |
{ |
| 60 |
level_ = level; // this level |
| 61 |
nlevel_ = nlevel; // number of total levels |
| 62 |
ml_printlevel_ = printlevel; // printlevel |
| 63 |
ml_ = ml; // the global ML object |
| 64 |
ag_ = ag; // the global ML_Aggregate object |
| 65 |
thislevel_prec_ = 0; // this level's linear preconditioner |
| 66 |
thislevel_ml_ = 0; // this level's local ML object |
| 67 |
thislevel_ag_ = 0; // this level's local ML_Aggregate object |
| 68 |
coarseinterface_ = 0; // this level's coarse interface |
| 69 |
coarseprepost_ = 0; |
| 70 |
xthis_ = 0; // this level's current solution matching this level's map!!!! |
| 71 |
thislevel_A_ = 0; // this level's NOX Matrixfree operator |
| 72 |
SmootherA_ = 0; // this level's Epetra_CrsMatrix for thislevel_prec_ |
| 73 |
ismatrixfree_ = ismatrixfree; // matrixfree flag |
| 74 |
conv_normF_ = conv_normF; // NOX convergence test stuff |
| 75 |
conv_nupdate_ = conv_nupdate; |
| 76 |
conv_maxiter_ = conv_maxiter; |
| 77 |
absresid_ = 0; |
| 78 |
nupdate_ = 0; |
| 79 |
fv_ = 0; |
| 80 |
maxiters_ = 0; |
| 81 |
combo1_ = 0; |
| 82 |
combo2_ = 0; |
| 83 |
thislevel_linSys_ = 0; // this level's NOX linear system |
| 84 |
nlParams_ = 0; // NOX parameters |
| 85 |
initialGuess_ = 0; // NOX initial guess |
| 86 |
group_ = 0; // NOX group |
| 87 |
solver_ = 0; // NOX solver |
| 88 |
isnlnCG_ = isnlnCG; |
| 89 |
azlinSys_ = 0; |
| 90 |
clone_ = 0; |
| 91 |
nitersCG_ = nitersCG; |
| 92 |
broyden_ = broyden; |
| 93 |
Broyd_ = 0; |
| 94 |
|
| 95 |
if (ismatrixfree_==true) |
| 96 |
{ |
| 97 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 98 |
<< "**ERR**: ismatrixfree_==true on level " << level_ << "\n" |
| 99 |
<< "**ERR**: in constructor for ismatrixfree_==false - case\n" |
| 100 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 101 |
} |
| 102 |
|
| 103 |
// ------------------------------------------------------------------------ |
| 104 |
// get the Jacobian of this level |
| 105 |
const Epetra_CrsGraph* graph = 0; |
| 106 |
// ------------------------------------------------------------------------ |
| 107 |
if (level_==0) |
| 108 |
{ |
| 109 |
graph = fineinterface_.getGraph(); |
| 110 |
// On fine level this is the fineinterface's Jacobian |
| 111 |
if (matfreelev0==false) |
| 112 |
SmootherA_ = fineinterface_.getJacobian(); |
| 113 |
else if (matfreelev0==true && Jac) |
| 114 |
SmootherA_ = Jac; |
| 115 |
else |
| 116 |
{ |
| 117 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 118 |
<< "**ERR**: something weired happened\n" |
| 119 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 120 |
} |
| 121 |
} |
| 122 |
// ------------------------------------------------------------------------ |
| 123 |
else |
| 124 |
{ |
| 125 |
// On coarse levels get Jacobian from hierarchy |
| 126 |
// Note: On levels>0 SmootherA_ is a real copy of the Jacobian |
| 127 |
int maxnnz=0; |
| 128 |
double cputime=0.0; |
| 129 |
ML_Operator2EpetraCrsMatrix(&(ml_->Amat[level_]), SmootherA_, maxnnz, |
| 130 |
false, cputime); |
| 131 |
SmootherA_->OptimizeStorage(); |
| 132 |
graph = &(SmootherA_->Graph()); |
| 133 |
} |
| 134 |
|
| 135 |
// just to be save |
| 136 |
if (!SmootherA_ || !graph) |
| 137 |
{ |
| 138 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 139 |
<< "**ERR**: Smoother==NULL on level " << level_ << "\n" |
| 140 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 141 |
} |
| 142 |
|
| 143 |
// ------------------------------------------------------------------------ |
| 144 |
// generate this level's coarse interface |
| 145 |
coarseinterface_ = new ML_NOX::Nox_CoarseProblem_Interface( |
| 146 |
fineinterface_,level_,ml_printlevel_, |
| 147 |
P,&(graph->RowMap()),nlevel_); |
| 148 |
|
| 149 |
// ------------------------------------------------------------------------ |
| 150 |
// generate this level's coarse prepostoperator |
| 151 |
if (level_==0) |
| 152 |
coarseprepost_ = new ML_NOX::Ml_Nox_CoarsePrePostOperator(*coarseinterface_, |
| 153 |
fineinterface_); |
| 154 |
|
| 155 |
// ------------------------------------------------------------------------ |
| 156 |
// get the current solution to this level |
| 157 |
xthis_ = coarseinterface_->restrict_fine_to_this(xfine); |
| 158 |
|
| 159 |
|
| 160 |
// ------------------------------------------------------------------------ |
| 161 |
// create this level's preconditioner |
| 162 |
// We use a 1-level ML-hierarchy for that |
| 163 |
ML_Aggregate_Create(&thislevel_ag_); |
| 164 |
ML_Create(&thislevel_ml_,1); |
| 165 |
|
| 166 |
// set the Jacobian on level 0 of the local ml |
| 167 |
EpetraMatrix2MLMatrix(thislevel_ml_,0, |
| 168 |
(dynamic_cast<Epetra_RowMatrix*>(SmootherA_))); |
| 169 |
|
| 170 |
// construct a 1-level ML-hierarchy on this level as a smoother |
| 171 |
ML_Set_PrintLevel(ml_printlevel_); |
| 172 |
ML_Aggregate_Set_CoarsenScheme_Uncoupled(thislevel_ag_); |
| 173 |
ML_Aggregate_Set_DampingFactor(thislevel_ag_, 0.0); |
| 174 |
ML_Aggregate_Set_Threshold(thislevel_ag_, 0.0); |
| 175 |
ML_Aggregate_Set_MaxCoarseSize(thislevel_ag_,1); |
| 176 |
ML_Aggregate_Set_NullSpace(thislevel_ag_,numPDE,nullspdim,NULL, |
| 177 |
SmootherA_->NumMyRows()); |
| 178 |
int thislevel_nlevel = ML_Gen_MGHierarchy_UsingAggregation(thislevel_ml_,0, |
| 179 |
ML_INCREASING,thislevel_ag_); |
| 180 |
if (thislevel_nlevel != 1) |
| 181 |
{ |
| 182 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 183 |
<< "**ERR**: ML generated a local hierarchy of " << thislevel_nlevel << " on level " << level_ << "\n" |
| 184 |
<< "**ERR**: this is supposed to be 1 Level only!\n" |
| 185 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 186 |
} |
| 187 |
|
| 188 |
// set the smoother |
| 189 |
if (level_==0) |
| 190 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,fsmoothertype,nsmooth_fine); |
| 191 |
|
| 192 |
else if (level_ != nlevel_-1) // set the smoother from the input |
| 193 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,smoothertype,nsmooth); |
| 194 |
|
| 195 |
else // set the coarse solver from the input |
| 196 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,coarsesolvetype,nsmooth_coarse); |
| 197 |
|
| 198 |
// create this level's preconditioner class |
| 199 |
ML_Epetra::MultiLevelOperator* ml_tmp = new ML_Epetra::MultiLevelOperator( |
| 200 |
thislevel_ml_,comm_, |
| 201 |
SmootherA_->OperatorDomainMap(), |
| 202 |
SmootherA_->OperatorRangeMap()); |
| 203 |
thislevel_prec_ = new ML_NOX::ML_Nox_ConstrainedMultiLevelOperator(ml_tmp,*coarseinterface_); |
| 204 |
|
| 205 |
if (!thislevel_prec_) |
| 206 |
{ |
| 207 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 208 |
<< "**ERR**: thislevel_prec_==NULL on level " << level_ << "\n" |
| 209 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 210 |
} |
| 211 |
|
| 212 |
// ------------------------------------------------------------------------ |
| 213 |
// set up NOX on this level |
| 214 |
// ------------------------------------------------------------------------ |
| 215 |
nlParams_ = new Teuchos::ParameterList(); |
| 216 |
Teuchos::ParameterList& printParams = nlParams_->sublist("Printing"); |
| 217 |
printParams.setParameter("MyPID", comm_.MyPID()); |
| 218 |
printParams.setParameter("Output Precision", 14); |
| 219 |
printParams.setParameter("Output Processor", 0); |
| 220 |
if (ml_printlevel_>9) |
| 221 |
printParams.setParameter("Output Information", |
| 222 |
NOX::Utils::OuterIteration + |
| 223 |
NOX::Utils::Warning); |
| 224 |
else if (ml_printlevel_>8) |
| 225 |
printParams.setParameter("Output Information", |
| 226 |
NOX::Utils::Warning); |
| 227 |
else |
| 228 |
printParams.setParameter("Output Information",0); |
| 229 |
|
| 230 |
if (level_==0) |
| 231 |
nlParams_->sublist("Solver Options").setParameter("User Defined Pre/Post Operator", *coarseprepost_); |
| 232 |
nlParams_->setParameter("Nonlinear Solver", "Line Search Based"); |
| 233 |
Teuchos::ParameterList& searchParams = nlParams_->sublist("Line Search"); |
| 234 |
Teuchos::ParameterList* lsParamsptr = 0; |
| 235 |
if (isnlnCG_) |
| 236 |
{ |
| 237 |
searchParams.setParameter("Method", "NonlinearCG"); |
| 238 |
Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); |
| 239 |
dirParams.setParameter("Method", "NonlinearCG"); |
| 240 |
Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG"); |
| 241 |
nlcgParams.setParameter("Restart Frequency", 10); |
| 242 |
nlcgParams.setParameter("Precondition", "On"); |
| 243 |
nlcgParams.setParameter("Orthogonalize", "Polak-Ribiere"); |
| 244 |
//nlcgParams.setParameter("Orthogonalize", "Fletcher-Reeves"); |
| 245 |
|
| 246 |
Teuchos::ParameterList& lsParams = nlcgParams.sublist("Linear Solver"); |
| 247 |
lsParams.setParameter("Aztec Solver", "CG"); |
| 248 |
lsParams.setParameter("Max Iterations", 1); |
| 249 |
lsParams.setParameter("Tolerance", 1e-11); |
| 250 |
lsParams.setParameter("Output Frequency", 0); |
| 251 |
lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); |
| 252 |
lsParams.setParameter("Preconditioner","User Defined"); |
| 253 |
} |
| 254 |
else // Newton's method using ML-preconditioned Aztec as linear solver |
| 255 |
{ |
| 256 |
searchParams.setParameter("Method", "Full Step"); |
| 257 |
// Sublist for direction |
| 258 |
Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); |
| 259 |
dirParams.setParameter("Method", "Newton"); |
| 260 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 261 |
newtonParams.setParameter("Forcing Term Method", "Constant"); |
| 262 |
//newtonParams.setParameter("Forcing Term Method", "Type 1"); |
| 263 |
//newtonParams.setParameter("Forcing Term Method", "Type 2"); |
| 264 |
newtonParams.setParameter("Forcing Term Minimum Tolerance", 1.0e-6); |
| 265 |
newtonParams.setParameter("Forcing Term Maximum Tolerance", 0.1); |
| 266 |
|
| 267 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 268 |
lsParamsptr = &lsParams; |
| 269 |
lsParams.setParameter("Size of Krylov Subspace", 100); |
| 270 |
lsParams.setParameter("Aztec Solver", "GMRES"); |
| 271 |
lsParams.setParameter("Max Iterations", nitersCG_); |
| 272 |
lsParams.setParameter("Tolerance", conv_normF_); // FIXME? is this correct? |
| 273 |
if (ml_printlevel_>8) |
| 274 |
lsParams.setParameter("Output Frequency", 50); |
| 275 |
else |
| 276 |
lsParams.setParameter("Output Frequency", 0); |
| 277 |
lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); |
| 278 |
lsParams.setParameter("Preconditioner","User Defined"); |
| 279 |
} |
| 280 |
|
| 281 |
// create the initial guess |
| 282 |
initialGuess_ = new NOX::Epetra::Vector(*xthis_, NOX::DeepCopy, true); |
| 283 |
// NOTE: do not delete xthis_, it's used and destroyed by initialGuess_ |
| 284 |
|
| 285 |
|
| 286 |
// create the necessary interfaces |
| 287 |
NOX::EpetraNew::Interface::Preconditioner* iPrec = 0; |
| 288 |
NOX::EpetraNew::Interface::Required* iReq = 0; |
| 289 |
NOX::EpetraNew::Interface::Jacobian* iJac = 0; |
| 290 |
|
| 291 |
if (isnlnCG_) |
| 292 |
{ |
| 293 |
// create the matrixfree operator used in the nlnCG |
| 294 |
thislevel_A_ = new NOX::EpetraNew::MatrixFree(*coarseinterface_,*xthis_,false); |
| 295 |
|
| 296 |
// create the necessary interfaces |
| 297 |
iPrec = 0; |
| 298 |
iReq = coarseinterface_; |
| 299 |
iJac = thislevel_A_; |
| 300 |
|
| 301 |
// create the linear system |
| 302 |
thislevel_linSys_ = new ML_NOX::Ml_Nox_LinearSystem( |
| 303 |
*iJac,*thislevel_A_,*iPrec, |
| 304 |
coarseinterface_,*thislevel_prec_, |
| 305 |
*xthis_,ismatrixfree_,level_,ml_printlevel_); |
| 306 |
// create the group |
| 307 |
group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_,*thislevel_linSys_); |
| 308 |
} |
| 309 |
else // Modified Newton's method |
| 310 |
{ |
| 311 |
if (!broyden_) |
| 312 |
{ |
| 313 |
// create the necessary interfaces |
| 314 |
iPrec = this; |
| 315 |
iReq = coarseinterface_; |
| 316 |
//iJac = this; |
| 317 |
thislevel_A_ = new NOX::EpetraNew::MatrixFree(*coarseinterface_,*xthis_,false); |
| 318 |
|
| 319 |
// create the initial guess vector |
| 320 |
//clone_ = new Epetra_Vector(*xthis_); |
| 321 |
|
| 322 |
// create the linear system |
| 323 |
//azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( |
| 324 |
// printParams,*lsParamsptr, |
| 325 |
// *iJac,*SmootherA_,*iPrec, |
| 326 |
// *thislevel_prec_,*clone_); |
| 327 |
azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( |
| 328 |
printParams,*lsParamsptr, |
| 329 |
*thislevel_A_,*thislevel_A_,*iPrec, |
| 330 |
*thislevel_prec_,*xthis_); |
| 331 |
} |
| 332 |
else // use a Broyden update for the Jacobian |
| 333 |
{ |
| 334 |
// create the initial guess vector |
| 335 |
//clone_ = new Epetra_Vector(*xthis_); |
| 336 |
|
| 337 |
// create the necessary interfaces |
| 338 |
iPrec = this; |
| 339 |
iReq = coarseinterface_; |
| 340 |
Broyd_ = new NOX::EpetraNew::BroydenOperator(*nlParams_,*xthis_, |
| 341 |
*SmootherA_,false); |
| 342 |
|
| 343 |
// create the linear system |
| 344 |
azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( |
| 345 |
printParams,*lsParamsptr, |
| 346 |
*Broyd_,*SmootherA_,*iPrec, |
| 347 |
*thislevel_prec_,*xthis_); |
| 348 |
|
| 349 |
} |
| 350 |
// create the group |
| 351 |
group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_, |
| 352 |
*azlinSys_); |
| 353 |
} |
| 354 |
|
| 355 |
// create convergence test |
| 356 |
create_Nox_Convergencetest(conv_normF_,conv_nupdate_,conv_maxiter_); |
| 357 |
|
| 358 |
// create the solver |
| 359 |
solver_ = new NOX::Solver::Manager(*group_,*combo2_,*nlParams_); |
| 360 |
|
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
/*----------------------------------------------------------------------* |
| 365 |
| Constructor (public) m.gee 01/05| |
| 366 |
| IMPORTANT: | |
| 367 |
| No matter on which level we are here, the vector xfine is ALWAYS | |
| 368 |
| a fine grid vector here! | |
| 369 |
| this is the constructor for the ismatrixfree==true case |
| 370 |
*----------------------------------------------------------------------*/ |
| 371 |
ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel( |
| 372 |
int level, int nlevel, int printlevel, ML* ml, |
| 373 |
ML_Aggregate* ag,Epetra_CrsMatrix** P, |
| 374 |
ML_NOX::Ml_Nox_Fineinterface& interface, |
| 375 |
const Epetra_Comm& comm, const Epetra_Vector& xfine, |
| 376 |
bool ismatrixfree, bool isnlnCG, int nitersCG, bool broyden, |
| 377 |
string fsmoothertype, string smoothertype, string coarsesolvetype, |
| 378 |
int nsmooth_fine, int nsmooth, int nsmooth_coarse, |
| 379 |
double conv_normF, |
| 380 |
double conv_nupdate, int conv_maxiter, |
| 381 |
int numPDE, int nullspdim, Epetra_CrsMatrix* Mat, |
| 382 |
ML_NOX::Nox_CoarseProblem_Interface* coarseinterface) |
| 383 |
: fineinterface_(interface), |
| 384 |
comm_(comm) |
| 385 |
{ |
| 386 |
level_ = level; // this level |
| 387 |
nlevel_ = nlevel; // number of total levels |
| 388 |
ml_printlevel_ = printlevel; // printlevel |
| 389 |
ml_ = ml; // the global ML object |
| 390 |
ag_ = ag; // the global ML_Aggregate object |
| 391 |
thislevel_prec_ = 0; // this level's linear preconditioner |
| 392 |
thislevel_ml_ = 0; // this level's local ML object |
| 393 |
thislevel_ag_ = 0; // this level's local ML_Aggregate object |
| 394 |
coarseinterface_ = coarseinterface; // this level's coarse interface |
| 395 |
coarseprepost_ = 0; |
| 396 |
xthis_ = 0; // this level's current solution matching this level's map!!!! |
| 397 |
thislevel_A_ = 0; // this level's NOX Matrixfree operator |
| 398 |
SmootherA_ = 0; // this level's Epetra_CrsMatrix for thislevel_prec_ |
| 399 |
ismatrixfree_ = ismatrixfree; // matrixfree flag |
| 400 |
conv_normF_ = conv_normF; // NOX convergence test stuff |
| 401 |
conv_nupdate_ = conv_nupdate; |
| 402 |
conv_maxiter_ = conv_maxiter; |
| 403 |
absresid_ = 0; |
| 404 |
nupdate_ = 0; |
| 405 |
fv_ = 0; |
| 406 |
maxiters_ = 0; |
| 407 |
combo1_ = 0; |
| 408 |
combo2_ = 0; |
| 409 |
thislevel_linSys_ = 0; // this level's NOX linear system |
| 410 |
nlParams_ = 0; // NOX parameters |
| 411 |
initialGuess_ = 0; // NOX initial guess |
| 412 |
group_ = 0; // NOX group |
| 413 |
solver_ = 0; // NOX solver |
| 414 |
SmootherA_ = Mat; |
| 415 |
isnlnCG_ = isnlnCG; |
| 416 |
azlinSys_ = 0; |
| 417 |
clone_ = 0; |
| 418 |
nitersCG_ = nitersCG; |
| 419 |
broyden_ = broyden; |
| 420 |
Broyd_ = 0; |
| 421 |
|
| 422 |
#if 0 |
| 423 |
if (isnlnCG_==false && |
| 424 |
(fsmoothertype == "Jacobi" || |
| 425 |
smoothertype == "Jacobi" || |
| 426 |
coarsesolvetype == "Jacobi" )) |
| 427 |
{ |
| 428 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 429 |
<< "**ERR**: Modified Newton's method not supported for \n" |
| 430 |
<< "**ERR**: ismatrixfree_==true && smoothertype == Jacobi-Smoother\n" |
| 431 |
<< "**ERR**: because no full Jacobian exists!\n" |
| 432 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 433 |
} |
| 434 |
#endif |
| 435 |
if (ismatrixfree_==false) |
| 436 |
{ |
| 437 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 438 |
<< "**ERR**: ismatrixfree_==false on level " << level_ << "\n" |
| 439 |
<< "**ERR**: in constructor for ismatrixfree_==true - case\n" |
| 440 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 441 |
} |
| 442 |
if (!coarseinterface_) |
| 443 |
{ |
| 444 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 445 |
<< "**ERR**: ptr to coarseinterface=NULL on level " << level_ << "\n" |
| 446 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 447 |
} |
| 448 |
if (!Mat) |
| 449 |
{ |
| 450 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 451 |
<< "**ERR**: ptr to Matrix Mat=NULL on level " << level_ << "\n" |
| 452 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 453 |
} |
| 454 |
|
| 455 |
// ------------------------------------------------------------------------ |
| 456 |
Mat->OptimizeStorage(); |
| 457 |
|
| 458 |
// ------------------------------------------------------------------------ |
| 459 |
// get the current solution to this level |
| 460 |
xthis_ = coarseinterface_->restrict_fine_to_this(xfine); |
| 461 |
|
| 462 |
// ------------------------------------------------------------------------ |
| 463 |
// create this level's preconditioner |
| 464 |
// We use a 1-level ML-hierarchy for that |
| 465 |
ML_Aggregate_Create(&thislevel_ag_); |
| 466 |
ML_Create(&thislevel_ml_,1); |
| 467 |
|
| 468 |
// ------------------------------------------------------------------------ |
| 469 |
// set the Jacobian on level 0 of the local ml |
| 470 |
EpetraMatrix2MLMatrix(thislevel_ml_,0, |
| 471 |
(dynamic_cast<Epetra_RowMatrix*>(Mat))); |
| 472 |
|
| 473 |
// ------------------------------------------------------------------------ |
| 474 |
// construct a 1-level ML-hierarchy on this level as a smoother |
| 475 |
// ------------------------------------------------------------------------ |
| 476 |
ML_Set_PrintLevel(ml_printlevel_); |
| 477 |
ML_Aggregate_Set_CoarsenScheme_Uncoupled(thislevel_ag_); |
| 478 |
ML_Aggregate_Set_DampingFactor(thislevel_ag_, 0.0); |
| 479 |
ML_Aggregate_Set_Threshold(thislevel_ag_, 0.0); |
| 480 |
ML_Aggregate_Set_MaxCoarseSize(thislevel_ag_,1); |
| 481 |
ML_Aggregate_Set_NullSpace(thislevel_ag_,numPDE,nullspdim,NULL,Mat->NumMyRows()); |
| 482 |
int thislevel_nlevel = ML_Gen_MGHierarchy_UsingAggregation(thislevel_ml_,0, |
| 483 |
ML_INCREASING,thislevel_ag_); |
| 484 |
if (thislevel_nlevel != 1) |
| 485 |
{ |
| 486 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 487 |
<< "**ERR**: ML generated a local hierarchy of " << thislevel_nlevel << " on level " << level_ << "\n" |
| 488 |
<< "**ERR**: this is supposed to be 1 Level only!\n" |
| 489 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
// set the smoother |
| 494 |
if (level_==0) |
| 495 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,fsmoothertype,nsmooth_fine); |
| 496 |
|
| 497 |
else if (level_ != nlevel_-1) // set the smoother from the input |
| 498 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,smoothertype,nsmooth); |
| 499 |
|
| 500 |
else // set the coarse solver from the input |
| 501 |
Set_Smoother(ml,ag,level_,nlevel,thislevel_ml_,thislevel_ag_,coarsesolvetype,nsmooth_coarse); |
| 502 |
|
| 503 |
// create this level's preconditioner class |
| 504 |
ML_Epetra::MultiLevelOperator* ml_tmp = new ML_Epetra::MultiLevelOperator( |
| 505 |
thislevel_ml_,comm_, |
| 506 |
Mat->OperatorDomainMap(), |
| 507 |
Mat->OperatorRangeMap()); |
| 508 |
thislevel_prec_ = new ML_NOX::ML_Nox_ConstrainedMultiLevelOperator(ml_tmp,*coarseinterface_); |
| 509 |
|
| 510 |
if (!thislevel_prec_) |
| 511 |
{ |
| 512 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::ML_Nox_NonlinearLevel:\n" |
| 513 |
<< "**ERR**: thislevel_prec_==NULL on level " << level_ << "\n" |
| 514 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 515 |
} |
| 516 |
|
| 517 |
// intensive test of this level's ML-smoother |
| 518 |
#if 0 |
| 519 |
{ |
| 520 |
cout << "Test of smoother on level " << level_ << endl; |
| 521 |
Epetra_Vector *out = new Epetra_Vector(Copy,*xthis_,0); |
| 522 |
out->PutScalar(0.0); |
| 523 |
cout << "Input\n"; |
| 524 |
xthis_->PutScalar(1.0); |
| 525 |
Mat->Multiply(false,*xthis_,*out); |
| 526 |
xthis_->PutScalar(3.0); |
| 527 |
cout << "rhs\n"; |
| 528 |
cout << *out; |
| 529 |
double norm = 0.0; |
| 530 |
out->Norm1(&norm); |
| 531 |
cout << "Norm of rhs = " << norm << endl; |
| 532 |
thislevel_prec_->ApplyInverse(*out,*xthis_); |
| 533 |
cout << "result after smoother\n"; |
| 534 |
cout << *xthis_; |
| 535 |
delete out; out = 0; |
| 536 |
} |
| 537 |
if (level_==2) exit(0); |
| 538 |
#endif |
| 539 |
|
| 540 |
// ------------------------------------------------------------------------ |
| 541 |
// generate this level's coarse prepostoperator |
| 542 |
if (level_==0) |
| 543 |
coarseprepost_ = new ML_NOX::Ml_Nox_CoarsePrePostOperator(*coarseinterface_, |
| 544 |
fineinterface_); |
| 545 |
|
| 546 |
// ------------------------------------------------------------------------ |
| 547 |
// set up NOX on this level |
| 548 |
// ------------------------------------------------------------------------ |
| 549 |
nlParams_ = new Teuchos::ParameterList(); |
| 550 |
Teuchos::ParameterList& printParams = nlParams_->sublist("Printing"); |
| 551 |
printParams.setParameter("MyPID", comm_.MyPID()); |
| 552 |
printParams.setParameter("Output Precision", 9); |
| 553 |
printParams.setParameter("Output Processor", 0); |
| 554 |
if (ml_printlevel_>9) |
| 555 |
printParams.setParameter("Output Information", |
| 556 |
NOX::Utils::OuterIteration + |
| 557 |
//NOX::Utils::OuterIterationStatusTest + |
| 558 |
//NOX::Utils::InnerIteration + |
| 559 |
//NOX::Utils::Parameters + |
| 560 |
//NOX::Utils::Details + |
| 561 |
NOX::Utils::Warning); |
| 562 |
else if (ml_printlevel_>8) |
| 563 |
printParams.setParameter("Output Information", |
| 564 |
NOX::Utils::Warning); |
| 565 |
else |
| 566 |
printParams.setParameter("Output Information",0); |
| 567 |
|
| 568 |
if (level_==0) |
| 569 |
nlParams_->sublist("Solver Options").setParameter("User Defined Pre/Post Operator", *coarseprepost_); |
| 570 |
nlParams_->setParameter("Nonlinear Solver", "Line Search Based"); |
| 571 |
Teuchos::ParameterList& searchParams = nlParams_->sublist("Line Search"); |
| 572 |
Teuchos::ParameterList* lsParamsptr = 0; |
| 573 |
if (isnlnCG_) |
| 574 |
{ |
| 575 |
searchParams.setParameter("Method", "NonlinearCG"); |
| 576 |
Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); |
| 577 |
dirParams.setParameter("Method", "NonlinearCG"); |
| 578 |
Teuchos::ParameterList& nlcgParams = dirParams.sublist("Nonlinear CG"); |
| 579 |
nlcgParams.setParameter("Restart Frequency", 10); |
| 580 |
nlcgParams.setParameter("Precondition", "On"); |
| 581 |
nlcgParams.setParameter("Orthogonalize", "Polak-Ribiere"); |
| 582 |
//nlcgParams.setParameter("Orthogonalize", "Fletcher-Reeves"); |
| 583 |
|
| 584 |
Teuchos::ParameterList& lsParams = nlcgParams.sublist("Linear Solver"); |
| 585 |
lsParams.setParameter("Aztec Solver", "CG"); |
| 586 |
lsParams.setParameter("Max Iterations", 1); |
| 587 |
lsParams.setParameter("Tolerance", 1e-11); |
| 588 |
lsParams.setParameter("Output Frequency", 0); |
| 589 |
lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); |
| 590 |
lsParams.setParameter("Preconditioner","User Defined"); |
| 591 |
} |
| 592 |
else // Newton's method using ML-preconditioned Aztec as linear solver |
| 593 |
{ |
| 594 |
searchParams.setParameter("Method", "Full Step"); |
| 595 |
// Sublist for direction |
| 596 |
Teuchos::ParameterList& dirParams = nlParams_->sublist("Direction"); |
| 597 |
dirParams.setParameter("Method", "Newton"); |
| 598 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 599 |
newtonParams.setParameter("Forcing Term Method", "Constant"); |
| 600 |
//newtonParams.setParameter("Forcing Term Method", "Type 1"); |
| 601 |
//newtonParams.setParameter("Forcing Term Method", "Type 2"); |
| 602 |
newtonParams.setParameter("Forcing Term Minimum Tolerance", 1.0e-6); |
| 603 |
newtonParams.setParameter("Forcing Term Maximum Tolerance", 0.1); |
| 604 |
|
| 605 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 606 |
lsParamsptr = &lsParams; |
| 607 |
lsParams.setParameter("Aztec Solver", "CG"); |
| 608 |
lsParams.setParameter("Max Iterations", nitersCG_); |
| 609 |
lsParams.setParameter("Tolerance", conv_normF_); // FIXME? is this correct? |
| 610 |
if (ml_printlevel_>8) |
| 611 |
lsParams.setParameter("Output Frequency", 50); |
| 612 |
else |
| 613 |
lsParams.setParameter("Output Frequency", 0); |
| 614 |
lsParams.setParameter("Preconditioning", "User Supplied Preconditioner"); |
| 615 |
lsParams.setParameter("Preconditioner","User Defined"); |
| 616 |
} |
| 617 |
|
| 618 |
// create the initial guess |
| 619 |
initialGuess_ = new NOX::Epetra::Vector(*xthis_, NOX::DeepCopy, true); |
| 620 |
// NOTE: do not delete xthis_, it's used and destroyed by initialGuess_ |
| 621 |
|
| 622 |
// create the necessary interfaces |
| 623 |
NOX::EpetraNew::Interface::Preconditioner* iPrec = 0; |
| 624 |
NOX::EpetraNew::Interface::Required* iReq = 0; |
| 625 |
NOX::EpetraNew::Interface::Jacobian* iJac = 0; |
| 626 |
|
| 627 |
if (isnlnCG_) |
| 628 |
{ |
| 629 |
// create the matrixfree operator used in the nlnCG |
| 630 |
thislevel_A_ = new NOX::EpetraNew::MatrixFree(*coarseinterface_,*xthis_,false); |
| 631 |
|
| 632 |
// create the necessary interfaces |
| 633 |
iPrec = 0; |
| 634 |
iReq = coarseinterface_; |
| 635 |
iJac = thislevel_A_; |
| 636 |
|
| 637 |
// create the linear system |
| 638 |
thislevel_linSys_ = new ML_NOX::Ml_Nox_LinearSystem( |
| 639 |
*iJac,*thislevel_A_,*iPrec, |
| 640 |
coarseinterface_,*thislevel_prec_, |
| 641 |
*xthis_,ismatrixfree_,level_,ml_printlevel_); |
| 642 |
|
| 643 |
// create the group |
| 644 |
group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_,*thislevel_linSys_); |
| 645 |
} |
| 646 |
else // Modified Newton's method |
| 647 |
{ |
| 648 |
if (!broyden_) |
| 649 |
{ |
| 650 |
// create the necessary interfaces |
| 651 |
iPrec = this; |
| 652 |
iReq = coarseinterface_; |
| 653 |
iJac = this; |
| 654 |
|
| 655 |
// create the initial guess vector |
| 656 |
clone_ = new Epetra_Vector(*xthis_); |
| 657 |
|
| 658 |
// create the linear system |
| 659 |
azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( |
| 660 |
printParams,*lsParamsptr, |
| 661 |
*iJac,*SmootherA_,*iPrec, |
| 662 |
*thislevel_prec_,*clone_); |
| 663 |
} |
| 664 |
else |
| 665 |
{ |
| 666 |
// create the initial guess vector |
| 667 |
clone_ = new Epetra_Vector(*xthis_); |
| 668 |
|
| 669 |
// create the necessary interfaces |
| 670 |
iPrec = this; |
| 671 |
iReq = coarseinterface_; |
| 672 |
Broyd_ = new NOX::EpetraNew::BroydenOperator(*nlParams_,*clone_, |
| 673 |
*SmootherA_,false); |
| 674 |
|
| 675 |
// create the linear system |
| 676 |
azlinSys_ = new NOX::EpetraNew::LinearSystemAztecOO( |
| 677 |
printParams,*lsParamsptr, |
| 678 |
*Broyd_,*SmootherA_,*iPrec, |
| 679 |
*thislevel_prec_,*clone_); |
| 680 |
} |
| 681 |
// create the group |
| 682 |
group_ = new NOX::EpetraNew::Group(printParams,*iReq,*initialGuess_,*azlinSys_); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
// create convergence test |
| 687 |
create_Nox_Convergencetest(conv_normF_,conv_nupdate_,conv_maxiter_); |
| 688 |
|
| 689 |
// create the solver |
| 690 |
solver_ = new NOX::Solver::Manager(*group_,*combo2_,*nlParams_); |
| 691 |
|
| 692 |
return; |
| 693 |
} |
| 694 |
|
| 695 |
/*----------------------------------------------------------------------* |
| 696 |
| Destructor (public) m.gee 01/05| |
| 697 |
*----------------------------------------------------------------------*/ |
| 698 |
ML_NOX::ML_Nox_NonlinearLevel::~ML_Nox_NonlinearLevel() |
| 699 |
{ |
| 700 |
|
| 701 |
// destroying this level's operator depends on ismatrixfree_ and on the level |
| 702 |
if (ismatrixfree_==false) |
| 703 |
{ |
| 704 |
if (level_ == 0) |
| 705 |
{ |
| 706 |
SmootherA_ = 0; |
| 707 |
} |
| 708 |
else if (SmootherA_) |
| 709 |
{ |
| 710 |
delete SmootherA_; |
| 711 |
SmootherA_ = 0; |
| 712 |
} |
| 713 |
else |
| 714 |
{ |
| 715 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::~ML_Nox_NonlinearLevel:\n" |
| 716 |
<< "**ERR**: something weird happened while destroying SmootherA_ on level " << level_ << "\n" |
| 717 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 718 |
} |
| 719 |
} |
| 720 |
else // ismatrixfree==true; |
| 721 |
{ |
| 722 |
// this is just a ptr to the finite difference constructed matrix |
| 723 |
// the matrixfree level is charge of destroying it |
| 724 |
SmootherA_ = 0; |
| 725 |
} |
| 726 |
|
| 727 |
// in the matrixfree case, the coarseinterface is owned by the matrixfree |
| 728 |
// class of this level and is destroyed there |
| 729 |
if (ismatrixfree_==false) |
| 730 |
{ |
| 731 |
if (coarseinterface_) |
| 732 |
delete coarseinterface_; |
| 733 |
coarseinterface_ = 0; |
| 734 |
} |
| 735 |
else |
| 736 |
coarseinterface_ = 0; |
| 737 |
|
| 738 |
if (coarseprepost_) |
| 739 |
delete coarseprepost_; |
| 740 |
coarseprepost_ = 0; |
| 741 |
|
| 742 |
if (thislevel_ag_) |
| 743 |
ML_Aggregate_Destroy(&thislevel_ag_); |
| 744 |
thislevel_ag_ = 0; |
| 745 |
|
| 746 |
if (thislevel_ml_) |
| 747 |
ML_Destroy(&thislevel_ml_); |
| 748 |
thislevel_ml_ = 0; |
| 749 |
|
| 750 |
if (thislevel_prec_) |
| 751 |
delete thislevel_prec_; |
| 752 |
thislevel_prec_ = 0; |
| 753 |
|
| 754 |
if (xthis_) |
| 755 |
delete xthis_; |
| 756 |
xthis_ = 0; |
| 757 |
|
| 758 |
if (thislevel_A_) |
| 759 |
delete thislevel_A_; |
| 760 |
thislevel_A_ = 0; |
| 761 |
|
| 762 |
if (thislevel_linSys_) |
| 763 |
delete thislevel_linSys_; |
| 764 |
thislevel_linSys_ = 0; |
| 765 |
|
| 766 |
if (azlinSys_) |
| 767 |
delete azlinSys_; |
| 768 |
azlinSys_ = 0; |
| 769 |
|
| 770 |
if (clone_) |
| 771 |
delete clone_; |
| 772 |
clone_ = 0; |
| 773 |
|
| 774 |
if (nlParams_) |
| 775 |
delete nlParams_; |
| 776 |
nlParams_ = 0; |
| 777 |
|
| 778 |
if (absresid_) |
| 779 |
delete absresid_; |
| 780 |
absresid_ = 0; |
| 781 |
|
| 782 |
if (nupdate_) |
| 783 |
delete nupdate_; |
| 784 |
nupdate_ = 0; |
| 785 |
|
| 786 |
if (fv_) |
| 787 |
delete fv_; |
| 788 |
fv_ = 0; |
| 789 |
|
| 790 |
if (maxiters_) |
| 791 |
delete maxiters_; |
| 792 |
maxiters_ = 0; |
| 793 |
|
| 794 |
if (combo1_) |
| 795 |
delete combo1_; |
| 796 |
combo1_ = 0; |
| 797 |
|
| 798 |
if (combo2_) |
| 799 |
delete combo2_; |
| 800 |
combo2_ = 0; |
| 801 |
|
| 802 |
if (group_) |
| 803 |
delete group_; |
| 804 |
group_ = 0; |
| 805 |
|
| 806 |
if (initialGuess_) |
| 807 |
delete initialGuess_; |
| 808 |
initialGuess_ = 0; |
| 809 |
|
| 810 |
if (solver_) |
| 811 |
delete solver_; |
| 812 |
solver_ = 0; |
| 813 |
|
| 814 |
if (Broyd_) |
| 815 |
delete Broyd_; |
| 816 |
Broyd_ = 0; |
| 817 |
|
| 818 |
return; |
| 819 |
} |
| 820 |
|
| 821 |
/*----------------------------------------------------------------------* |
| 822 |
| (public) m.gee 01/05| |
| 823 |
| iterate on f with initial guess x numiter times | |
| 824 |
| returns absolut value of result in x | |
| 825 |
| returns f matching x | |
| 826 |
*----------------------------------------------------------------------*/ |
| 827 |
bool ML_NOX::ML_Nox_NonlinearLevel::iterate(Epetra_Vector* f, Epetra_Vector* x, |
| 828 |
int numiter) |
| 829 |
{ |
| 830 |
double norm = conv_normF_; |
| 831 |
return iterate(f,x,numiter,&norm); |
| 832 |
} |
| 833 |
/*----------------------------------------------------------------------* |
| 834 |
| (public) m.gee 01/05| |
| 835 |
| iterate on f with initial guess x numiter times | |
| 836 |
| returns absolut value of result in x | |
| 837 |
| returns f matching x | |
| 838 |
*----------------------------------------------------------------------*/ |
| 839 |
bool ML_NOX::ML_Nox_NonlinearLevel::iterate(Epetra_Vector* f, Epetra_Vector* x, |
| 840 |
int numiter, double* norm) |
| 841 |
{ |
| 842 |
if (!group_ || !solver_ || !combo2_) |
| 843 |
{ |
| 844 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::iterate:\n" |
| 845 |
<< "**ERR**: group_ || solver_ || combo2_ or even some of them are NULL\n" |
| 846 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 847 |
} |
| 848 |
|
| 849 |
// put x into group_ |
| 850 |
NOX::Epetra::Vector X(*x,NOX::DeepCopy,true); |
| 851 |
group_->setX(X); |
| 852 |
|
| 853 |
// create the convergence test |
| 854 |
create_Nox_Convergencetest(*norm,*norm,numiter); |
| 855 |
|
| 856 |
// make a soft reset of the NOX solver (see NOX manual) |
| 857 |
solver_->reset(*group_,*combo2_); |
| 858 |
|
| 859 |
// iterate |
| 860 |
if (ml_printlevel_ > 0 && comm_.MyPID() == 0 && coarseinterface_->isFAS()==false) |
| 861 |
{ |
| 862 |
printf("ML (level %d): Entering Nonlinear Smoother, Goal: %12.8e\n",level_,*norm); fflush(stdout); |
| 863 |
//cout << "ML (level " << level_ << "): Entering Nonlinear Smoother, Goal: " << *norm << "\n"; fflush(stdout); |
| 864 |
} |
| 865 |
else if (ml_printlevel_ > 0 && comm_.MyPID() == 0 && coarseinterface_->isFAS()==true) |
| 866 |
{ |
| 867 |
printf("ML (level %d): Entering FAS-Nonlinear Smoother, Goal: %12.8e\n",level_,*norm); fflush(stdout); |
| 868 |
//cout << "ML (level " << level_ << "): Entering FAS-Nonlinear Smoother, Goal: " << *norm << "\n"; fflush(stdout); |
| 869 |
} |
| 870 |
|
| 871 |
NOX::StatusTest::StatusType status = solver_->solve(); |
| 872 |
|
| 873 |
// get number of iterations done |
| 874 |
int niter = solver_->getNumIterations(); |
| 875 |
// get the solution and the new F |
| 876 |
const NOX::EpetraNew::Group& finalGroup = |
| 877 |
dynamic_cast<const NOX::EpetraNew::Group&>(solver_->getSolutionGroup()); |
| 878 |
const Epetra_Vector& finalSolution = |
| 879 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector(); |
| 880 |
const Epetra_Vector& finalF = |
| 881 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getF())).getEpetraVector(); |
| 882 |
|
| 883 |
bool returnstatus=false; |
| 884 |
double norm2; |
| 885 |
finalF.Norm2(&norm2); |
| 886 |
*norm = norm2; |
| 887 |
|
| 888 |
if (status == NOX::StatusTest::Converged) |
| 889 |
{ |
| 890 |
returnstatus = true; |
| 891 |
if (ml_printlevel_ > 0 && comm_.MyPID() == 0) { |
| 892 |
//cout << "ML (level " << level_ << "): NOX: " |
| 893 |
// << niter << " iterations, Norm(F)=" |
| 894 |
// << norm2 << " , Converged\n"; fflush(stdout); |
| 895 |
printf("ML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Converged\n",level_,niter,norm2); |
| 896 |
fflush(stdout); |
| 897 |
} |
| 898 |
} |
| 899 |
else if (status == NOX::StatusTest::Unconverged) |
| 900 |
{ |
| 901 |
returnstatus = false; |
| 902 |
if (ml_printlevel_ > 0 && comm_.MyPID() == 0) { |
| 903 |
//cout << "ML (level " << level_ << "): NOX: " |
| 904 |
// << niter << " iterations, Norm(F)=" |
| 905 |
// << norm2 << ", Unconverged\n"; fflush(stdout); |
| 906 |
printf("ML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Unonverged\n",level_,niter,norm2); |
| 907 |
fflush(stdout); |
| 908 |
} |
| 909 |
} |
| 910 |
else if (status == NOX::StatusTest::Failed) |
| 911 |
{ |
| 912 |
returnstatus = false; |
| 913 |
if (ml_printlevel_ > 0 && comm_.MyPID() == 0) { |
| 914 |
//cout << "ML (level " << level_ << "): NOX: " |
| 915 |
// << niter << " iterations, Norm(F)=" << norm2 << ", Failed\n"; fflush(stdout); |
| 916 |
printf("ML (level %d): NOX: %d iterations, Norm(F)=%12.8e , Failed\n",level_,niter,norm2); |
| 917 |
fflush(stdout); |
| 918 |
} |
| 919 |
} |
| 920 |
else |
| 921 |
{ |
| 922 |
returnstatus = false; |
| 923 |
if (comm_.MyPID() == 0) |
| 924 |
//cout << "ML (level " << level_ << "): ***WRN*** NOX returned unknown status, Norm(F)=" |
| 925 |
// << norm2 << "\n"; fflush(stdout); |
| 926 |
printf("ML (level %d): ***WRN*** NOX: return status unknown, Norm(F)=%12.8e , Failed\n",level_,norm2); |
| 927 |
fflush(stdout); |
| 928 |
} |
| 929 |
|
| 930 |
// reset number of calls to coarseinterface->computeF |
| 931 |
coarseinterface_->resetnumcallscomputeF(); |
| 932 |
|
| 933 |
// update the solution |
| 934 |
x->Update(1.0,finalSolution,0.0); |
| 935 |
f->Update(1.0,finalF,0.0); |
| 936 |
|
| 937 |
return returnstatus; |
| 938 |
} |
| 939 |
|
| 940 |
/*----------------------------------------------------------------------* |
| 941 |
| (public) m.gee 01/05| |
| 942 |
| iterate on f with initial guess x numiter times | |
| 943 |
| returns absolut value of result in x | |
| 944 |
| returns f matching x | |
| 945 |
*----------------------------------------------------------------------*/ |
| 946 |
bool ML_NOX::ML_Nox_NonlinearLevel::applySmoother(Epetra_Vector* f, Epetra_Vector* x, |
| 947 |
int numiter) |
| 948 |
{ |
| 949 |
if (!thislevel_prec_) |
| 950 |
{ |
| 951 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::applySmoother:\n" |
| 952 |
<< "**ERR**: thislevel_prec_ is NULL\n" |
| 953 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 954 |
} |
| 955 |
|
| 956 |
int i; |
| 957 |
|
| 958 |
Epetra_Vector out(Copy,*x,0); |
| 959 |
|
| 960 |
for (i=0; i<numiter; i++) |
| 961 |
{ |
| 962 |
thislevel_prec_->ApplyInverse(*f,out); |
| 963 |
x->Update(1.0,out,1.0); |
| 964 |
computeF(*x,*f,NOX::EpetraNew::Interface::Required::Residual); |
| 965 |
} |
| 966 |
|
| 967 |
return true; |
| 968 |
} |
| 969 |
|
| 970 |
/*----------------------------------------------------------------------* |
| 971 |
| (private) m.gee 04/05| |
| 972 |
| set the smoother on this nonlinear level | |
| 973 |
*----------------------------------------------------------------------*/ |
| 974 |
bool ML_NOX::ML_Nox_NonlinearLevel::Set_Smoother(ML* ml, |
| 975 |
ML_Aggregate* ag, |
| 976 |
int level, |
| 977 |
int nlevel, |
| 978 |
ML* thislevel_ml, |
| 979 |
ML_Aggregate* thislevel_ag, |
| 980 |
string smoothertype, |
| 981 |
int nsmooth) |
| 982 |
{ |
| 983 |
if (smoothertype == "SGS") |
| 984 |
ML_Gen_Smoother_SymGaussSeidel(thislevel_ml,0,ML_BOTH,nsmooth,1.0); |
| 985 |
else if (smoothertype == "Jacobi") |
| 986 |
ML_Gen_Smoother_Jacobi(thislevel_ml,0,ML_BOTH,nsmooth,0.25); |
| 987 |
else if (smoothertype == "AmesosKLU") |
| 988 |
ML_Gen_Smoother_Amesos(thislevel_ml,0,ML_AMESOS_KLU,-1,0.0,1); |
| 989 |
else if ( (smoothertype == "MLS") || (smoothertype == "Cheby") ) |
| 990 |
ML_Gen_Smoother_Cheby(thislevel_ml,0,ML_BOTH,30.,nsmooth); |
| 991 |
else if (smoothertype == "BSGS") |
| 992 |
{ |
| 993 |
int nblocks = 0; |
| 994 |
int* blocks = NULL; |
| 995 |
int* blockpde = NULL; |
| 996 |
bool needfree = false; |
| 997 |
|
| 998 |
// try to get nodal blocks from the VBMETIS aggregation scheme |
| 999 |
ML_Aggregate_Get_Vblocks_CoarsenScheme_VBMETIS(ag,level,nlevel, |
| 1000 |
&nblocks,&blocks,&blockpde); |
| 1001 |
|
| 1002 |
if (nblocks && blocks) |
| 1003 |
needfree=true; |
| 1004 |
else |
| 1005 |
ML_Gen_Blocks_Aggregates(ag,level,&nblocks,&blocks); |
| 1006 |
|
| 1007 |
ML_Gen_Smoother_VBlockSymGaussSeidel(thislevel_ml,0,ML_BOTH,nsmooth,1., |
| 1008 |
nblocks,blocks); |
| 1009 |
if (needfree) |
| 1010 |
{ |
| 1011 |
ML_free(blocks); |
| 1012 |
ML_free(blockpde); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
else if (smoothertype == "Bcheby") |
| 1016 |
{ |
| 1017 |
int nblocks = 0; |
| 1018 |
int* blocks = NULL; |
| 1019 |
int* blockpde = NULL; |
| 1020 |
bool needfree = false; |
| 1021 |
|
| 1022 |
// try to get nodal blocks from the VBMETIS aggregation scheme |
| 1023 |
ML_Aggregate_Get_Vblocks_CoarsenScheme_VBMETIS(ag,level,nlevel, |
| 1024 |
&nblocks,&blocks,&blockpde); |
| 1025 |
|
| 1026 |
if (nblocks && blocks) |
| 1027 |
needfree=true; |
| 1028 |
else |
| 1029 |
ML_Gen_Blocks_Aggregates(ag,level,&nblocks,&blocks); |
| 1030 |
|
| 1031 |
ML_Gen_Smoother_BlockDiagScaledCheby(thislevel_ml,0,ML_BOTH,30.,nsmooth, |
| 1032 |
nblocks,blocks); |
| 1033 |
if (needfree) |
| 1034 |
{ |
| 1035 |
ML_free(blocks); |
| 1036 |
ML_free(blockpde); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
else |
| 1040 |
{ |
| 1041 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::Setsmoother:\n" |
| 1042 |
<< "**ERR**: unknown type of smoother: " << smoothertype << "\n" |
| 1043 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 1044 |
} |
| 1045 |
|
| 1046 |
return true; |
| 1047 |
} |
| 1048 |
|
| 1049 |
/*----------------------------------------------------------------------* |
| 1050 |
| (private) m.gee 01/05| |
| 1051 |
| (re)create a NOX convergence test | |
| 1052 |
| deletes combo1_ and combo2_ and recreates them | |
| 1053 |
*----------------------------------------------------------------------*/ |
| 1054 |
bool ML_NOX::ML_Nox_NonlinearLevel::create_Nox_Convergencetest(double conv_normF, |
| 1055 |
double conv_nupdate, |
| 1056 |
int conv_maxiter) |
| 1057 |
{ |
| 1058 |
if (absresid_) |
| 1059 |
delete absresid_; |
| 1060 |
if (nupdate_) |
| 1061 |
delete nupdate_; |
| 1062 |
if (fv_) |
| 1063 |
delete fv_; |
| 1064 |
if (maxiters_) |
| 1065 |
delete maxiters_; |
| 1066 |
if (combo1_) |
| 1067 |
delete combo1_; |
| 1068 |
if (combo2_) |
| 1069 |
delete combo2_; |
| 1070 |
|
| 1071 |
absresid_ = new NOX::StatusTest::NormF(conv_normF,NOX::StatusTest::NormF::Unscaled); |
| 1072 |
|
| 1073 |
nupdate_ = new NOX::StatusTest::NormUpdate(conv_nupdate,NOX::StatusTest::NormUpdate::Unscaled); |
| 1074 |
|
| 1075 |
combo1_ = new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND); |
| 1076 |
combo1_->addStatusTest(*absresid_); |
| 1077 |
combo1_->addStatusTest(*nupdate_); |
| 1078 |
|
| 1079 |
fv_ = new NOX::StatusTest::FiniteValue(); |
| 1080 |
|
| 1081 |
maxiters_ = new NOX::StatusTest::MaxIters(conv_maxiter); |
| 1082 |
|
| 1083 |
combo2_ = new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR); |
| 1084 |
combo2_->addStatusTest(*maxiters_); |
| 1085 |
combo2_->addStatusTest(*combo1_); |
| 1086 |
combo2_->addStatusTest(*fv_); |
| 1087 |
|
| 1088 |
return true; |
| 1089 |
} |
| 1090 |
|
| 1091 |
/*----------------------------------------------------------------------* |
| 1092 |
| (public,derived) m.gee 01/05| |
| 1093 |
| compute this level's prec to this level's nlnCG | |
| 1094 |
*----------------------------------------------------------------------*/ |
| 1095 |
bool ML_NOX::ML_Nox_NonlinearLevel::computePreconditioner(const Epetra_Vector& x, |
| 1096 |
Teuchos::ParameterList* precParams) |
| 1097 |
{ |
| 1098 |
#if 0 |
| 1099 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::computePreconditioner:\n" |
| 1100 |
<< "**ERR**: not impl. on level " << level_ << "\n" |
| 1101 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 1102 |
#endif |
| 1103 |
return true; |
| 1104 |
} |
| 1105 |
|
| 1106 |
/*----------------------------------------------------------------------* |
| 1107 |
| (public,derived) m.gee 03/05| |
| 1108 |
| compute this level's Jacobian | |
| 1109 |
*----------------------------------------------------------------------*/ |
| 1110 |
bool ML_NOX::ML_Nox_NonlinearLevel::computeJacobian(const Epetra_Vector& x) |
| 1111 |
{ |
| 1112 |
#if 0 |
| 1113 |
cout << "**ERR**: ML_NOX::ML_Nox_NonlinearLevel::computeJacobian:\n" |
| 1114 |
<< "**ERR**: not impl. on level " << level_ << "\n" |
| 1115 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 1116 |
#endif |
| 1117 |
return true; |
| 1118 |
} |
| 1119 |
#endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) |