142f4f86dSBarry Smith #include <../src/snes/impls/richardson/snesrichardsonimpl.h> 2028b6e76SBarry Smith 3bf7f4e0aSPeter Brune 4028b6e76SBarry Smith #undef __FUNCT__ 5d5c3842bSBarry Smith #define __FUNCT__ "SNESReset_NRichardson" 6d5c3842bSBarry Smith PetscErrorCode SNESReset_NRichardson(SNES snes) 7028b6e76SBarry Smith { 8028b6e76SBarry Smith PetscFunctionBegin; 9028b6e76SBarry Smith PetscFunctionReturn(0); 10028b6e76SBarry Smith } 11028b6e76SBarry Smith 12028b6e76SBarry Smith /* 13d5c3842bSBarry Smith SNESDestroy_NRichardson - Destroys the private SNES_NRichardson context that was created with SNESCreate_NRichardson(). 14028b6e76SBarry Smith 15028b6e76SBarry Smith Input Parameter: 16028b6e76SBarry Smith . snes - the SNES context 17028b6e76SBarry Smith 18028b6e76SBarry Smith Application Interface Routine: SNESDestroy() 19028b6e76SBarry Smith */ 20028b6e76SBarry Smith #undef __FUNCT__ 21d5c3842bSBarry Smith #define __FUNCT__ "SNESDestroy_NRichardson" 22d5c3842bSBarry Smith PetscErrorCode SNESDestroy_NRichardson(SNES snes) 23028b6e76SBarry Smith { 24028b6e76SBarry Smith PetscErrorCode ierr; 25028b6e76SBarry Smith 26028b6e76SBarry Smith PetscFunctionBegin; 27d5c3842bSBarry Smith ierr = SNESReset_NRichardson(snes);CHKERRQ(ierr); 28028b6e76SBarry Smith ierr = PetscFree(snes->data);CHKERRQ(ierr); 29028b6e76SBarry Smith PetscFunctionReturn(0); 30028b6e76SBarry Smith } 31028b6e76SBarry Smith 32028b6e76SBarry Smith /* 33d5c3842bSBarry Smith SNESSetUp_NRichardson - Sets up the internal data structures for the later use 34d5c3842bSBarry Smith of the SNESNRICHARDSON nonlinear solver. 35028b6e76SBarry Smith 36028b6e76SBarry Smith Input Parameters: 37028b6e76SBarry Smith + snes - the SNES context 38028b6e76SBarry Smith - x - the solution vector 39028b6e76SBarry Smith 40028b6e76SBarry Smith Application Interface Routine: SNESSetUp() 41028b6e76SBarry Smith */ 42028b6e76SBarry Smith #undef __FUNCT__ 43d5c3842bSBarry Smith #define __FUNCT__ "SNESSetUp_NRichardson" 44d5c3842bSBarry Smith PetscErrorCode SNESSetUp_NRichardson(SNES snes) 45028b6e76SBarry Smith { 46028b6e76SBarry Smith PetscFunctionBegin; 47028b6e76SBarry Smith PetscFunctionReturn(0); 48028b6e76SBarry Smith } 49028b6e76SBarry Smith 50028b6e76SBarry Smith /* 5104d7464bSBarry Smith SNESSetFromOptions_NRichardson - Sets various parameters for the SNESNEWTONLS method. 52028b6e76SBarry Smith 53028b6e76SBarry Smith Input Parameter: 54028b6e76SBarry Smith . snes - the SNES context 55028b6e76SBarry Smith 56028b6e76SBarry Smith Application Interface Routine: SNESSetFromOptions() 57028b6e76SBarry Smith */ 58028b6e76SBarry Smith #undef __FUNCT__ 59d5c3842bSBarry Smith #define __FUNCT__ "SNESSetFromOptions_NRichardson" 60d5c3842bSBarry Smith static PetscErrorCode SNESSetFromOptions_NRichardson(SNES snes) 61028b6e76SBarry Smith { 62028b6e76SBarry Smith PetscErrorCode ierr; 63f1c6b773SPeter Brune SNESLineSearch linesearch; 645fd66863SKarl Rupp 65028b6e76SBarry Smith PetscFunctionBegin; 6642f4f86dSBarry Smith ierr = PetscOptionsHead("SNES Richardson options");CHKERRQ(ierr); 67028b6e76SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 689e764e56SPeter Brune if (!snes->linesearch) { 69f1c6b773SPeter Brune ierr = SNESGetSNESLineSearch(snes, &linesearch);CHKERRQ(ierr); 701a4f838cSPeter Brune ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);CHKERRQ(ierr); 719e764e56SPeter Brune } 72028b6e76SBarry Smith PetscFunctionReturn(0); 73028b6e76SBarry Smith } 74028b6e76SBarry Smith 75028b6e76SBarry Smith /* 76d5c3842bSBarry Smith SNESView_NRichardson - Prints info from the SNESRichardson data structure. 77028b6e76SBarry Smith 78028b6e76SBarry Smith Input Parameters: 79028b6e76SBarry Smith + SNES - the SNES context 80028b6e76SBarry Smith - viewer - visualization context 81028b6e76SBarry Smith 82028b6e76SBarry Smith Application Interface Routine: SNESView() 83028b6e76SBarry Smith */ 84028b6e76SBarry Smith #undef __FUNCT__ 85d5c3842bSBarry Smith #define __FUNCT__ "SNESView_NRichardson" 86d5c3842bSBarry Smith static PetscErrorCode SNESView_NRichardson(SNES snes, PetscViewer viewer) 87028b6e76SBarry Smith { 88028b6e76SBarry Smith PetscBool iascii; 89028b6e76SBarry Smith PetscErrorCode ierr; 90028b6e76SBarry Smith 91028b6e76SBarry Smith PetscFunctionBegin; 92251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 93028b6e76SBarry Smith if (iascii) { 94028b6e76SBarry Smith } 95028b6e76SBarry Smith PetscFunctionReturn(0); 96028b6e76SBarry Smith } 97028b6e76SBarry Smith 98028b6e76SBarry Smith /* 99d5c3842bSBarry Smith SNESSolve_NRichardson - Solves a nonlinear system with the Richardson method. 100028b6e76SBarry Smith 101028b6e76SBarry Smith Input Parameters: 102028b6e76SBarry Smith . snes - the SNES context 103028b6e76SBarry Smith 104028b6e76SBarry Smith Output Parameter: 105028b6e76SBarry Smith . outits - number of iterations until termination 106028b6e76SBarry Smith 107028b6e76SBarry Smith Application Interface Routine: SNESSolve() 108028b6e76SBarry Smith */ 109028b6e76SBarry Smith #undef __FUNCT__ 110d5c3842bSBarry Smith #define __FUNCT__ "SNESSolve_NRichardson" 111d5c3842bSBarry Smith PetscErrorCode SNESSolve_NRichardson(SNES snes) 112028b6e76SBarry Smith { 113bf7f4e0aSPeter Brune Vec X, Y, F; 114e7058c64SPeter Brune PetscReal xnorm, fnorm, ynorm; 115028b6e76SBarry Smith PetscInt maxits, i; 116028b6e76SBarry Smith PetscErrorCode ierr; 117bf7f4e0aSPeter Brune PetscBool lsSuccess; 118028b6e76SBarry Smith SNESConvergedReason reason; 119028b6e76SBarry Smith 120028b6e76SBarry Smith PetscFunctionBegin; 121028b6e76SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 122028b6e76SBarry Smith 123028b6e76SBarry Smith maxits = snes->max_its; /* maximum number of iterations */ 124028b6e76SBarry Smith X = snes->vec_sol; /* X^n */ 125028b6e76SBarry Smith Y = snes->vec_sol_update; /* \tilde X */ 126028b6e76SBarry Smith F = snes->vec_func; /* residual vector */ 127028b6e76SBarry Smith 128028b6e76SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 129028b6e76SBarry Smith snes->iter = 0; 130028b6e76SBarry Smith snes->norm = 0.; 131028b6e76SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 132e4ed7901SPeter Brune if (!snes->vec_func_init_set) { 133028b6e76SBarry Smith ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr); 134028b6e76SBarry Smith if (snes->domainerror) { 135028b6e76SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 136028b6e76SBarry Smith PetscFunctionReturn(0); 137028b6e76SBarry Smith } 1381aa26658SKarl Rupp } else snes->vec_func_init_set = PETSC_FALSE; 1391aa26658SKarl Rupp 140e4ed7901SPeter Brune if (!snes->norm_init_set) { 141028b6e76SBarry Smith ierr = VecNorm(F,NORM_2,&fnorm);CHKERRQ(ierr); /* fnorm <- ||F|| */ 142189a9710SBarry Smith if (PetscIsInfOrNanReal(fnorm)) { 143189a9710SBarry Smith snes->reason = SNES_DIVERGED_FNORM_NAN; 144189a9710SBarry Smith PetscFunctionReturn(0); 145189a9710SBarry Smith } 146e4ed7901SPeter Brune } else { 147e4ed7901SPeter Brune fnorm = snes->norm_init; 148e4ed7901SPeter Brune snes->norm_init_set = PETSC_FALSE; 149e4ed7901SPeter Brune } 150e4ed7901SPeter Brune 151028b6e76SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 152028b6e76SBarry Smith snes->norm = fnorm; 153028b6e76SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 154a71f0d7dSBarry Smith ierr = SNESLogConvergenceHistory(snes,fnorm,0);CHKERRQ(ierr); 155028b6e76SBarry Smith ierr = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr); 156028b6e76SBarry Smith 157028b6e76SBarry Smith /* set parameter for default relative tolerance convergence test */ 158028b6e76SBarry Smith snes->ttol = fnorm*snes->rtol; 159028b6e76SBarry Smith /* test convergence */ 160028b6e76SBarry Smith ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 161028b6e76SBarry Smith if (snes->reason) PetscFunctionReturn(0); 162028b6e76SBarry Smith 163028b6e76SBarry Smith for (i = 0; i < maxits; i++) { 164bf7f4e0aSPeter Brune lsSuccess = PETSC_TRUE; 165028b6e76SBarry Smith /* Call general purpose update function */ 166028b6e76SBarry Smith if (snes->ops->update) { 167028b6e76SBarry Smith ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr); 168028b6e76SBarry Smith } 169c40d0f55SPeter Brune if (snes->pc && snes->pcside == PC_RIGHT) { 170028b6e76SBarry Smith ierr = VecCopy(X,Y);CHKERRQ(ierr); 171217b9c2eSPeter Brune ierr = SNESSetInitialFunction(snes->pc, F);CHKERRQ(ierr); 172217b9c2eSPeter Brune ierr = SNESSetInitialFunctionNorm(snes->pc, fnorm);CHKERRQ(ierr); 17390cf3547SPeter Brune ierr = PetscLogEventBegin(SNES_NPCSolve,snes->pc,Y,snes->vec_rhs,0);CHKERRQ(ierr); 174c90fad12SPeter Brune ierr = SNESSolve(snes->pc, snes->vec_rhs, Y);CHKERRQ(ierr); 17590cf3547SPeter Brune ierr = PetscLogEventEnd(SNES_NPCSolve,snes->pc,Y,snes->vec_rhs,0);CHKERRQ(ierr); 176028b6e76SBarry Smith ierr = SNESGetConvergedReason(snes->pc,&reason);CHKERRQ(ierr); 17734b225ddSBarry Smith if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) { 178028b6e76SBarry Smith snes->reason = SNES_DIVERGED_INNER; 179028b6e76SBarry Smith PetscFunctionReturn(0); 180028b6e76SBarry Smith } 181e5583369SPeter Brune ierr = VecAYPX(Y,-1.0,X);CHKERRQ(ierr); 182a3685310SPeter Brune } else { 183a3685310SPeter Brune ierr = VecCopy(F,Y);CHKERRQ(ierr); 184028b6e76SBarry Smith } 185f1c6b773SPeter Brune ierr = SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Y);CHKERRQ(ierr); 186f1c6b773SPeter Brune ierr = SNESLineSearchGetNorms(snes->linesearch, &xnorm, &fnorm, &ynorm);CHKERRQ(ierr); 187f1c6b773SPeter Brune ierr = SNESLineSearchGetSuccess(snes->linesearch, &lsSuccess);CHKERRQ(ierr); 188028b6e76SBarry Smith if (!lsSuccess) { 189028b6e76SBarry Smith if (++snes->numFailures >= snes->maxFailures) { 190028b6e76SBarry Smith snes->reason = SNES_DIVERGED_LINE_SEARCH; 191028b6e76SBarry Smith break; 192028b6e76SBarry Smith } 193028b6e76SBarry Smith } 194028b6e76SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 195028b6e76SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 196028b6e76SBarry Smith break; 197028b6e76SBarry Smith } 198028b6e76SBarry Smith if (snes->domainerror) { 199028b6e76SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 200028b6e76SBarry Smith PetscFunctionReturn(0); 201028b6e76SBarry Smith } 202028b6e76SBarry Smith /* Monitor convergence */ 203028b6e76SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 204028b6e76SBarry Smith snes->iter = i+1; 205028b6e76SBarry Smith snes->norm = fnorm; 206028b6e76SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 207a71f0d7dSBarry Smith ierr = SNESLogConvergenceHistory(snes,snes->norm,0);CHKERRQ(ierr); 208028b6e76SBarry Smith ierr = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr); 209028b6e76SBarry Smith /* Test for convergence */ 210e7058c64SPeter Brune ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 211028b6e76SBarry Smith if (snes->reason) break; 212028b6e76SBarry Smith } 213028b6e76SBarry Smith if (i == maxits) { 214028b6e76SBarry Smith ierr = PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);CHKERRQ(ierr); 215028b6e76SBarry Smith if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT; 216028b6e76SBarry Smith } 217028b6e76SBarry Smith PetscFunctionReturn(0); 218028b6e76SBarry Smith } 219028b6e76SBarry Smith 220028b6e76SBarry Smith /*MC 221d5c3842bSBarry Smith SNESNRICHARDSON - Richardson nonlinear solver that uses successive substitutions, also sometimes known as Picard iteration. 222028b6e76SBarry Smith 223028b6e76SBarry Smith Level: beginner 224028b6e76SBarry Smith 225028b6e76SBarry Smith Options Database: 22672835e02SPeter Brune + -snes_linesearch_type <l2,cp,basic> Line search type. 22772835e02SPeter Brune - -snes_linesearch_damping<1.0> Damping for the line search. 228028b6e76SBarry Smith 229af60355fSPeter Brune Notes: If no inner nonlinear preconditioner is provided then solves F(x) - b = 0 using x^{n+1} = x^{n} - lambda 230af60355fSPeter Brune (F(x^n) - b) where lambda is obtained either SNESLineSearchSetDamping(), -snes_damping or a line search. If 231af60355fSPeter Brune an inner nonlinear preconditioner is provided (either with -npc_snes_type or SNESSetPC()) then the inner 232af60355fSPeter Brune solver is called an initial solution x^n and the nonlinear Richardson uses x^{n+1} = x^{n} + lambda d^{n} 233af60355fSPeter Brune where d^{n} = \hat{x}^{n} - x^{n} where \hat{x}^{n} is the solution returned from the inner solver. 234028b6e76SBarry Smith 23572835e02SPeter Brune The update, especially without inner nonlinear preconditioner, may be ill-scaled. If using the basic 23672835e02SPeter Brune linesearch, one may have to scale the update with -snes_linesearch_damping 23772835e02SPeter Brune 238028b6e76SBarry Smith This uses no derivative information thus will be much slower then Newton's method obtained with -snes_type ls 239028b6e76SBarry Smith 24004d7464bSBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESQN, SNESNCG 241028b6e76SBarry Smith M*/ 242028b6e76SBarry Smith #undef __FUNCT__ 243d5c3842bSBarry Smith #define __FUNCT__ "SNESCreate_NRichardson" 244*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_NRichardson(SNES snes) 245028b6e76SBarry Smith { 24692c02d66SPeter Brune PetscErrorCode ierr; 247bf7f4e0aSPeter Brune SNES_NRichardson *neP; 2485fd66863SKarl Rupp 249028b6e76SBarry Smith PetscFunctionBegin; 250d5c3842bSBarry Smith snes->ops->destroy = SNESDestroy_NRichardson; 251d5c3842bSBarry Smith snes->ops->setup = SNESSetUp_NRichardson; 252d5c3842bSBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_NRichardson; 253d5c3842bSBarry Smith snes->ops->view = SNESView_NRichardson; 254d5c3842bSBarry Smith snes->ops->solve = SNESSolve_NRichardson; 255d5c3842bSBarry Smith snes->ops->reset = SNESReset_NRichardson; 256028b6e76SBarry Smith 257028b6e76SBarry Smith snes->usesksp = PETSC_FALSE; 25842f4f86dSBarry Smith snes->usespc = PETSC_TRUE; 259028b6e76SBarry Smith 260bf7f4e0aSPeter Brune ierr = PetscNewLog(snes, SNES_NRichardson, &neP);CHKERRQ(ierr); 261bf7f4e0aSPeter Brune snes->data = (void*) neP; 262bf7f4e0aSPeter Brune 26388976e71SPeter Brune if (!snes->tolerancesset) { 2640e444f03SPeter Brune snes->max_funcs = 30000; 2650e444f03SPeter Brune snes->max_its = 10000; 266c522fa08SPeter Brune snes->stol = 1e-20; 26788976e71SPeter Brune } 268028b6e76SBarry Smith PetscFunctionReturn(0); 269028b6e76SBarry Smith } 270