163dd3a1aSKris Buschelman #define PETSCSNES_DLL 25e76c431SBarry Smith 3724775ccSBarry Smith #include "../src/snes/impls/ls/lsimpl.h" 45e42470aSBarry Smith 58a5d9ee4SBarry Smith /* 620f52e01SBarry Smith Checks if J^T F = 0 which implies we've found a local minimum of the norm of the function, 720f52e01SBarry Smith || F(u) ||_2 but not a zero, F(u) = 0. In the case when one cannot compute J^T F we use the fact that 836669109SBarry Smith 0 = (J^T F)^T W = F^T J W iff W not in the null space of J. Thanks for Jorge More 920f52e01SBarry Smith for this trick. One assumes that the probability that W is in the null space of J is very, very small. 108a5d9ee4SBarry Smith */ 114a2ae208SSatish Balay #undef __FUNCT__ 124a2ae208SSatish Balay #define __FUNCT__ "SNESLSCheckLocalMin_Private" 13ace3abfcSBarry Smith PetscErrorCode SNESLSCheckLocalMin_Private(SNES snes,Mat A,Vec F,Vec W,PetscReal fnorm,PetscBool *ismin) 148a5d9ee4SBarry Smith { 158a5d9ee4SBarry Smith PetscReal a1; 16dfbe8321SBarry Smith PetscErrorCode ierr; 17ace3abfcSBarry Smith PetscBool hastranspose; 188a5d9ee4SBarry Smith 198a5d9ee4SBarry Smith PetscFunctionBegin; 208a5d9ee4SBarry Smith *ismin = PETSC_FALSE; 2136669109SBarry Smith ierr = MatHasOperation(A,MATOP_MULT_TRANSPOSE,&hastranspose);CHKERRQ(ierr); 2236669109SBarry Smith if (hastranspose) { 238a5d9ee4SBarry Smith /* Compute || J^T F|| */ 248a5d9ee4SBarry Smith ierr = MatMultTranspose(A,F,W);CHKERRQ(ierr); 258a5d9ee4SBarry Smith ierr = VecNorm(W,NORM_2,&a1);CHKERRQ(ierr); 261e2582c4SBarry Smith ierr = PetscInfo1(snes,"|| J^T F|| %G near zero implies found a local minimum\n",a1/fnorm);CHKERRQ(ierr); 278a5d9ee4SBarry Smith if (a1/fnorm < 1.e-4) *ismin = PETSC_TRUE; 2836669109SBarry Smith } else { 2936669109SBarry Smith Vec work; 30ea709b57SSatish Balay PetscScalar result; 3136669109SBarry Smith PetscReal wnorm; 3236669109SBarry Smith 33abb0e124SMatthew Knepley ierr = VecSetRandom(W,PETSC_NULL);CHKERRQ(ierr); 3436669109SBarry Smith ierr = VecNorm(W,NORM_2,&wnorm);CHKERRQ(ierr); 3536669109SBarry Smith ierr = VecDuplicate(W,&work);CHKERRQ(ierr); 3636669109SBarry Smith ierr = MatMult(A,W,work);CHKERRQ(ierr); 3736669109SBarry Smith ierr = VecDot(F,work,&result);CHKERRQ(ierr); 3836669109SBarry Smith ierr = VecDestroy(work);CHKERRQ(ierr); 3936669109SBarry Smith a1 = PetscAbsScalar(result)/(fnorm*wnorm); 401e2582c4SBarry Smith ierr = PetscInfo1(snes,"(F^T J random)/(|| F ||*||random|| %G near zero implies found a local minimum\n",a1);CHKERRQ(ierr); 4136669109SBarry Smith if (a1 < 1.e-4) *ismin = PETSC_TRUE; 4236669109SBarry Smith } 438a5d9ee4SBarry Smith PetscFunctionReturn(0); 448a5d9ee4SBarry Smith } 458a5d9ee4SBarry Smith 4674637425SBarry Smith /* 475ed2d596SBarry Smith Checks if J^T(F - J*X) = 0 4874637425SBarry Smith */ 494a2ae208SSatish Balay #undef __FUNCT__ 504a2ae208SSatish Balay #define __FUNCT__ "SNESLSCheckResidual_Private" 511e2582c4SBarry Smith PetscErrorCode SNESLSCheckResidual_Private(SNES snes,Mat A,Vec F,Vec X,Vec W1,Vec W2) 5274637425SBarry Smith { 5374637425SBarry Smith PetscReal a1,a2; 54dfbe8321SBarry Smith PetscErrorCode ierr; 55ace3abfcSBarry Smith PetscBool hastranspose; 5674637425SBarry Smith 5774637425SBarry Smith PetscFunctionBegin; 5874637425SBarry Smith ierr = MatHasOperation(A,MATOP_MULT_TRANSPOSE,&hastranspose);CHKERRQ(ierr); 5974637425SBarry Smith if (hastranspose) { 6074637425SBarry Smith ierr = MatMult(A,X,W1);CHKERRQ(ierr); 6179f36460SBarry Smith ierr = VecAXPY(W1,-1.0,F);CHKERRQ(ierr); 6274637425SBarry Smith 6374637425SBarry Smith /* Compute || J^T W|| */ 6474637425SBarry Smith ierr = MatMultTranspose(A,W1,W2);CHKERRQ(ierr); 6574637425SBarry Smith ierr = VecNorm(W1,NORM_2,&a1);CHKERRQ(ierr); 6674637425SBarry Smith ierr = VecNorm(W2,NORM_2,&a2);CHKERRQ(ierr); 6775567043SBarry Smith if (a1 != 0.0) { 681e2582c4SBarry Smith ierr = PetscInfo1(snes,"||J^T(F-Ax)||/||F-AX|| %G near zero implies inconsistent rhs\n",a2/a1);CHKERRQ(ierr); 6985471664SBarry Smith } 7074637425SBarry Smith } 7174637425SBarry Smith PetscFunctionReturn(0); 7274637425SBarry Smith } 7374637425SBarry Smith 7404d965bbSLois Curfman McInnes /* -------------------------------------------------------------------- 7504d965bbSLois Curfman McInnes 7604d965bbSLois Curfman McInnes This file implements a truncated Newton method with a line search, 7794b7f48cSBarry Smith for solving a system of nonlinear equations, using the KSP, Vec, 7804d965bbSLois Curfman McInnes and Mat interfaces for linear solvers, vectors, and matrices, 7904d965bbSLois Curfman McInnes respectively. 8004d965bbSLois Curfman McInnes 81fe6c6bacSLois Curfman McInnes The following basic routines are required for each nonlinear solver: 8204d965bbSLois Curfman McInnes SNESCreate_XXX() - Creates a nonlinear solver context 8304d965bbSLois Curfman McInnes SNESSetFromOptions_XXX() - Sets runtime options 84fe6c6bacSLois Curfman McInnes SNESSolve_XXX() - Solves the nonlinear system 8504d965bbSLois Curfman McInnes SNESDestroy_XXX() - Destroys the nonlinear solver context 8604d965bbSLois Curfman McInnes The suffix "_XXX" denotes a particular implementation, in this case 874b27c08aSLois Curfman McInnes we use _LS (e.g., SNESCreate_LS, SNESSolve_LS) for solving 884b27c08aSLois Curfman McInnes systems of nonlinear equations with a line search (LS) method. 8904d965bbSLois Curfman McInnes These routines are actually called via the common user interface 9004d965bbSLois Curfman McInnes routines SNESCreate(), SNESSetFromOptions(), SNESSolve(), and 9104d965bbSLois Curfman McInnes SNESDestroy(), so the application code interface remains identical 9204d965bbSLois Curfman McInnes for all nonlinear solvers. 9304d965bbSLois Curfman McInnes 9404d965bbSLois Curfman McInnes Another key routine is: 9504d965bbSLois Curfman McInnes SNESSetUp_XXX() - Prepares for the use of a nonlinear solver 9604d965bbSLois Curfman McInnes by setting data structures and options. The interface routine SNESSetUp() 9704d965bbSLois Curfman McInnes is not usually called directly by the user, but instead is called by 9804d965bbSLois Curfman McInnes SNESSolve() if necessary. 9904d965bbSLois Curfman McInnes 10004d965bbSLois Curfman McInnes Additional basic routines are: 10104d965bbSLois Curfman McInnes SNESView_XXX() - Prints details of runtime options that 10204d965bbSLois Curfman McInnes have actually been used. 10304d965bbSLois Curfman McInnes These are called by application codes via the interface routines 104186905e3SBarry Smith SNESView(). 10504d965bbSLois Curfman McInnes 10604d965bbSLois Curfman McInnes The various types of solvers (preconditioners, Krylov subspace methods, 10704d965bbSLois Curfman McInnes nonlinear solvers, timesteppers) are all organized similarly, so the 10804d965bbSLois Curfman McInnes above description applies to these categories also. 10904d965bbSLois Curfman McInnes 11004d965bbSLois Curfman McInnes -------------------------------------------------------------------- */ 1115e42470aSBarry Smith /* 1124b27c08aSLois Curfman McInnes SNESSolve_LS - Solves a nonlinear system with a truncated Newton 11304d965bbSLois Curfman McInnes method with a line search. 1145e76c431SBarry Smith 11504d965bbSLois Curfman McInnes Input Parameters: 11604d965bbSLois Curfman McInnes . snes - the SNES context 1175e76c431SBarry Smith 11804d965bbSLois Curfman McInnes Output Parameter: 119c2a78f1aSLois Curfman McInnes . outits - number of iterations until termination 12004d965bbSLois Curfman McInnes 12104d965bbSLois Curfman McInnes Application Interface Routine: SNESSolve() 1225e76c431SBarry Smith 1235e76c431SBarry Smith Notes: 1245e76c431SBarry Smith This implements essentially a truncated Newton method with a 1255e76c431SBarry Smith line search. By default a cubic backtracking line search 1265e76c431SBarry Smith is employed, as described in the text "Numerical Methods for 1275e76c431SBarry Smith Unconstrained Optimization and Nonlinear Equations" by Dennis 128393d2d9aSLois Curfman McInnes and Schnabel. 1295e76c431SBarry Smith */ 1304a2ae208SSatish Balay #undef __FUNCT__ 1314b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESSolve_LS" 132dfbe8321SBarry Smith PetscErrorCode SNESSolve_LS(SNES snes) 1335e76c431SBarry Smith { 1344b27c08aSLois Curfman McInnes SNES_LS *neP = (SNES_LS*)snes->data; 1356849ba73SBarry Smith PetscErrorCode ierr; 136a7cc72afSBarry Smith PetscInt maxits,i,lits; 137ace3abfcSBarry Smith PetscBool lssucceed; 138112a2221SBarry Smith MatStructure flg = DIFFERENT_NONZERO_PATTERN; 13985385478SLisandro Dalcin PetscReal fnorm,gnorm,xnorm=0,ynorm; 14085385478SLisandro Dalcin Vec Y,X,F,G,W; 1413d4c4710SBarry Smith KSPConvergedReason kspreason; 1425e76c431SBarry Smith 1433a40ed3dSBarry Smith PetscFunctionBegin; 1443d4c4710SBarry Smith snes->numFailures = 0; 1453d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 146184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 147184914b5SBarry Smith 1485e42470aSBarry Smith maxits = snes->max_its; /* maximum number of iterations */ 1495e42470aSBarry Smith X = snes->vec_sol; /* solution vector */ 15039e2f89bSBarry Smith F = snes->vec_func; /* residual vector */ 1515e42470aSBarry Smith Y = snes->work[0]; /* work vectors */ 1525e42470aSBarry Smith G = snes->work[1]; 1535e42470aSBarry Smith W = snes->work[2]; 1545e76c431SBarry Smith 1554c49b128SBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 1564c49b128SBarry Smith snes->iter = 0; 15775567043SBarry Smith snes->norm = 0.0; 1584c49b128SBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 15919717074SBarry Smith ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr); 1604936397dSBarry Smith if (snes->domainerror) { 1614936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 1624936397dSBarry Smith PetscFunctionReturn(0); 1634936397dSBarry Smith } 1642613ca53SBarry Smith ierr = VecNormBegin(F,NORM_2,&fnorm);CHKERRQ(ierr); /* fnorm <- ||F|| */ 1652613ca53SBarry Smith ierr = VecNormBegin(X,NORM_2,&xnorm);CHKERRQ(ierr); /* xnorm <- ||x|| */ 1662613ca53SBarry Smith ierr = VecNormEnd(F,NORM_2,&fnorm);CHKERRQ(ierr); 1672613ca53SBarry Smith ierr = VecNormEnd(X,NORM_2,&xnorm);CHKERRQ(ierr); 168e32f2f54SBarry Smith if PetscIsInfOrNanReal(fnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 1690f5bd95cSBarry Smith ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 1705e42470aSBarry Smith snes->norm = fnorm; 1710f5bd95cSBarry Smith ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 17284c569c9SBarry Smith SNESLogConvHistory(snes,fnorm,0); 17394a424c1SBarry Smith SNESMonitor(snes,0,fnorm); 1743f149594SLisandro Dalcin 1753f149594SLisandro Dalcin /* set parameter for default relative tolerance convergence test */ 1763f149594SLisandro Dalcin snes->ttol = fnorm*snes->rtol; 17785385478SLisandro Dalcin /* test convergence */ 17885385478SLisandro Dalcin ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 17906ee9f85SBarry Smith if (snes->reason) PetscFunctionReturn(0); 180d034289bSBarry Smith 1815e76c431SBarry Smith for (i=0; i<maxits; i++) { 1825e76c431SBarry Smith 183329e7e40SMatthew Knepley /* Call general purpose update function */ 184e7788613SBarry Smith if (snes->ops->update) { 185e7788613SBarry Smith ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr); 186de8cb200SMatthew Knepley } 187329e7e40SMatthew Knepley 188ea4d3ed3SLois Curfman McInnes /* Solve J Y = F, where J is Jacobian matrix */ 1895334005bSBarry Smith ierr = SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);CHKERRQ(ierr); 19094b7f48cSBarry Smith ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,flg);CHKERRQ(ierr); 19171f87433Sdalcinl ierr = SNES_KSPSolve(snes,snes->ksp,F,Y);CHKERRQ(ierr); 1923d4c4710SBarry Smith ierr = KSPGetConvergedReason(snes->ksp,&kspreason);CHKERRQ(ierr); 1933d4c4710SBarry Smith if (kspreason < 0) { 1943d4c4710SBarry Smith if (++snes->numLinearSolveFailures >= snes->maxLinearSolveFailures) { 195ef998cc9SBarry Smith ierr = PetscInfo2(snes,"iter=%D, number linear solve failures %D greater than current SNES allowed, stopping solve\n",snes->iter,snes->numLinearSolveFailures);CHKERRQ(ierr); 1963d4c4710SBarry Smith snes->reason = SNES_DIVERGED_LINEAR_SOLVE; 197ab81109fSSatish Balay break; 1983d4c4710SBarry Smith } 1993d4c4710SBarry Smith } 2003d4c4710SBarry Smith ierr = KSPGetIterationNumber(snes->ksp,&lits);CHKERRQ(ierr); 2013f149594SLisandro Dalcin snes->linear_its += lits; 2023f149594SLisandro Dalcin ierr = PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);CHKERRQ(ierr); 20374637425SBarry Smith 2049c3ca13aSBarry Smith if (neP->precheckstep) { 205ace3abfcSBarry Smith PetscBool changed_y = PETSC_FALSE; 2069c3ca13aSBarry Smith ierr = (*neP->precheckstep)(snes,X,Y,neP->precheck,&changed_y);CHKERRQ(ierr); 2079c3ca13aSBarry Smith } 2089c3ca13aSBarry Smith 209b0a32e0cSBarry Smith if (PetscLogPrintInfo){ 2101e2582c4SBarry Smith ierr = SNESLSCheckResidual_Private(snes,snes->jacobian,F,Y,G,W);CHKERRQ(ierr); 21185471664SBarry Smith } 21274637425SBarry Smith 213ea4d3ed3SLois Curfman McInnes /* Compute a (scaled) negative update in the line search routine: 214ea4d3ed3SLois Curfman McInnes Y <- X - lambda*Y 215e68848bdSBarry Smith and evaluate G = function(Y) (depends on the line search). 216ea4d3ed3SLois Curfman McInnes */ 21785385478SLisandro Dalcin ierr = VecCopy(Y,snes->vec_sol_update);CHKERRQ(ierr); 2183f149594SLisandro Dalcin ynorm = 1; gnorm = fnorm; 219dc357ad2SBarry Smith ierr = (*neP->LineSearch)(snes,neP->lsP,X,F,G,Y,W,fnorm,xnorm,&ynorm,&gnorm,&lssucceed);CHKERRQ(ierr); 220ae15b995SBarry Smith ierr = PetscInfo4(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lssucceed=%d\n",fnorm,gnorm,ynorm,(int)lssucceed);CHKERRQ(ierr); 2214a93e4ddSBarry Smith if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break; 2224936397dSBarry Smith if (snes->domainerror) { 2234936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 2244936397dSBarry Smith PetscFunctionReturn(0); 2254936397dSBarry Smith } 226a7cc72afSBarry Smith if (!lssucceed) { 22750ffb88aSMatthew Knepley if (++snes->numFailures >= snes->maxFailures) { 228ace3abfcSBarry Smith PetscBool ismin; 229647a2e1fSBarry Smith snes->reason = SNES_DIVERGED_LINE_SEARCH; 2301e2582c4SBarry Smith ierr = SNESLSCheckLocalMin_Private(snes,snes->jacobian,G,W,gnorm,&ismin);CHKERRQ(ierr); 2318a5d9ee4SBarry Smith if (ismin) snes->reason = SNES_DIVERGED_LOCAL_MIN; 2323505fcc1SBarry Smith break; 2333505fcc1SBarry Smith } 23450ffb88aSMatthew Knepley } 23585385478SLisandro Dalcin /* Update function and solution vectors */ 23685385478SLisandro Dalcin fnorm = gnorm; 23785385478SLisandro Dalcin ierr = VecCopy(G,F);CHKERRQ(ierr); 23885385478SLisandro Dalcin ierr = VecCopy(W,X);CHKERRQ(ierr); 23985385478SLisandro Dalcin /* Monitor convergence */ 24085385478SLisandro Dalcin ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 24185385478SLisandro Dalcin snes->iter = i+1; 24285385478SLisandro Dalcin snes->norm = fnorm; 24385385478SLisandro Dalcin ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 24485385478SLisandro Dalcin SNESLogConvHistory(snes,snes->norm,lits); 24585385478SLisandro Dalcin SNESMonitor(snes,snes->iter,snes->norm); 24685385478SLisandro Dalcin /* Test for convergence, xnorm = || X || */ 24785385478SLisandro Dalcin if (snes->ops->converged != SNESSkipConverged) { ierr = VecNorm(X,NORM_2,&xnorm);CHKERRQ(ierr); } 248e7788613SBarry Smith ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 2493f149594SLisandro Dalcin if (snes->reason) break; 25016c95cacSBarry Smith } 25152392280SLois Curfman McInnes if (i == maxits) { 252ae15b995SBarry Smith ierr = PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);CHKERRQ(ierr); 25385385478SLisandro Dalcin if(!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT; 25452392280SLois Curfman McInnes } 2553a40ed3dSBarry Smith PetscFunctionReturn(0); 2565e76c431SBarry Smith } 25704d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 25804d965bbSLois Curfman McInnes /* 2594b27c08aSLois Curfman McInnes SNESSetUp_LS - Sets up the internal data structures for the later use 2604b27c08aSLois Curfman McInnes of the SNESLS nonlinear solver. 26104d965bbSLois Curfman McInnes 26204d965bbSLois Curfman McInnes Input Parameter: 26304d965bbSLois Curfman McInnes . snes - the SNES context 26404d965bbSLois Curfman McInnes . x - the solution vector 26504d965bbSLois Curfman McInnes 26604d965bbSLois Curfman McInnes Application Interface Routine: SNESSetUp() 26704d965bbSLois Curfman McInnes 26804d965bbSLois Curfman McInnes Notes: 2694b27c08aSLois Curfman McInnes For basic use of the SNES solvers, the user need not explicitly call 27004d965bbSLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 27104d965bbSLois Curfman McInnes the call to SNESSolve(). 27204d965bbSLois Curfman McInnes */ 2734a2ae208SSatish Balay #undef __FUNCT__ 2744b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESSetUp_LS" 275dfbe8321SBarry Smith PetscErrorCode SNESSetUp_LS(SNES snes) 2765e76c431SBarry Smith { 277dfbe8321SBarry Smith PetscErrorCode ierr; 2783a40ed3dSBarry Smith 2793a40ed3dSBarry Smith PetscFunctionBegin; 28085385478SLisandro Dalcin if (!snes->vec_sol_update) { 28185385478SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 28285385478SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 28385385478SLisandro Dalcin } 284e74804ceSBarry Smith if (!snes->work) { 28585385478SLisandro Dalcin snes->nwork = 3; 28685385478SLisandro Dalcin ierr = VecDuplicateVecs(snes->vec_sol,snes->nwork,&snes->work);CHKERRQ(ierr); 287efee365bSSatish Balay ierr = PetscLogObjectParents(snes,snes->nwork,snes->work);CHKERRQ(ierr); 288e74804ceSBarry Smith } 2893a40ed3dSBarry Smith PetscFunctionReturn(0); 2905e76c431SBarry Smith } 29104d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 29204d965bbSLois Curfman McInnes /* 2934b27c08aSLois Curfman McInnes SNESDestroy_LS - Destroys the private SNES_LS context that was created 2944b27c08aSLois Curfman McInnes with SNESCreate_LS(). 29504d965bbSLois Curfman McInnes 29604d965bbSLois Curfman McInnes Input Parameter: 29704d965bbSLois Curfman McInnes . snes - the SNES context 29804d965bbSLois Curfman McInnes 299de49b36dSLois Curfman McInnes Application Interface Routine: SNESDestroy() 30004d965bbSLois Curfman McInnes */ 3014a2ae208SSatish Balay #undef __FUNCT__ 3024b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESDestroy_LS" 303dfbe8321SBarry Smith PetscErrorCode SNESDestroy_LS(SNES snes) 3045e76c431SBarry Smith { 30594298653SBarry Smith SNES_LS *ls = (SNES_LS*) snes->data; 306dfbe8321SBarry Smith PetscErrorCode ierr; 3073a40ed3dSBarry Smith 3083a40ed3dSBarry Smith PetscFunctionBegin; 30985385478SLisandro Dalcin if (snes->vec_sol_update) { 31085385478SLisandro Dalcin ierr = VecDestroy(snes->vec_sol_update);CHKERRQ(ierr); 31185385478SLisandro Dalcin snes->vec_sol_update = PETSC_NULL; 31285385478SLisandro Dalcin } 313*574deadeSBarry Smith if (snes->work) { 314*574deadeSBarry Smith ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr); 31521c89e3eSBarry Smith } 31694298653SBarry Smith if (ls->monitor) { 31794298653SBarry Smith ierr = PetscViewerASCIIMonitorDestroy(ls->monitor);CHKERRQ(ierr); 31894298653SBarry Smith } 3195c704376SSatish Balay ierr = PetscFree(snes->data);CHKERRQ(ierr); 320557d3f75SLisandro Dalcin 321557d3f75SLisandro Dalcin /* clear composed functions */ 322557d3f75SLisandro Dalcin ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSet_C","",PETSC_NULL);CHKERRQ(ierr); 323557d3f75SLisandro Dalcin ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPostCheck_C","",PETSC_NULL);CHKERRQ(ierr); 324557d3f75SLisandro Dalcin ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPreCheck_C","",PETSC_NULL);CHKERRQ(ierr); 325557d3f75SLisandro Dalcin 3263a40ed3dSBarry Smith PetscFunctionReturn(0); 3275e76c431SBarry Smith } 32804d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 3294a2ae208SSatish Balay #undef __FUNCT__ 33017bae607SBarry Smith #define __FUNCT__ "SNESLineSearchNo" 33182bf6240SBarry Smith 3324b828684SBarry Smith /*@C 33317bae607SBarry Smith SNESLineSearchNo - This routine is not a line search at all; 3345e76c431SBarry Smith it simply uses the full Newton step. Thus, this routine is intended 3355e76c431SBarry Smith to serve as a template and is not recommended for general use. 3365e76c431SBarry Smith 3373f9fe445SBarry Smith Logically Collective on SNES and Vec 338c7afd0dbSLois Curfman McInnes 3395e76c431SBarry Smith Input Parameters: 340c7afd0dbSLois Curfman McInnes + snes - nonlinear context 341af2d14d2SLois Curfman McInnes . lsctx - optional context for line search (not used here) 3425e76c431SBarry Smith . x - current iterate 3435e76c431SBarry Smith . f - residual evaluated at x 3443c632250SBarry Smith . y - search direction 345dc357ad2SBarry Smith . fnorm - 2-norm of f 346dc357ad2SBarry Smith - xnorm - norm of x if known, otherwise 0 3475e76c431SBarry Smith 348c4a48953SLois Curfman McInnes Output Parameters: 349c7afd0dbSLois Curfman McInnes + g - residual evaluated at new iterate y 3503c632250SBarry Smith . w - new iterate 3515e76c431SBarry Smith . gnorm - 2-norm of g 3525e76c431SBarry Smith . ynorm - 2-norm of search length 353a7cc72afSBarry Smith - flag - PETSC_TRUE on success, PETSC_FALSE on failure 354fee21e36SBarry Smith 355c4a48953SLois Curfman McInnes Options Database Key: 35617bae607SBarry Smith . -snes_ls basic - Activates SNESLineSearchNo() 357c4a48953SLois Curfman McInnes 35836851e7fSLois Curfman McInnes Level: advanced 35936851e7fSLois Curfman McInnes 36028ae5a21SLois Curfman McInnes .keywords: SNES, nonlinear, line search, cubic 36128ae5a21SLois Curfman McInnes 36217bae607SBarry Smith .seealso: SNESLineSearchCubic(), SNESLineSearchQuadratic(), 36317bae607SBarry Smith SNESLineSearchSet(), SNESLineSearchNoNorms() 3645e76c431SBarry Smith @*/ 3657087cfbeSBarry Smith PetscErrorCode SNESLineSearchNo(SNES snes,void *lsctx,Vec x,Vec f,Vec g,Vec y,Vec w,PetscReal fnorm,PetscReal xnorm,PetscReal *ynorm,PetscReal *gnorm,PetscBool *flag) 3665e76c431SBarry Smith { 367dfbe8321SBarry Smith PetscErrorCode ierr; 3684b27c08aSLois Curfman McInnes SNES_LS *neP = (SNES_LS*)snes->data; 369ace3abfcSBarry Smith PetscBool changed_w = PETSC_FALSE,changed_y = PETSC_FALSE; 3705334005bSBarry Smith 3713a40ed3dSBarry Smith PetscFunctionBegin; 372a7cc72afSBarry Smith *flag = PETSC_TRUE; 373d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 374a3b38805SLois Curfman McInnes ierr = VecNorm(y,NORM_2,ynorm);CHKERRQ(ierr); /* ynorm = || y || */ 37579f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); /* w <- x - y */ 3763c632250SBarry Smith if (neP->postcheckstep) { 3773c632250SBarry Smith ierr = (*neP->postcheckstep)(snes,x,y,w,neP->postcheck,&changed_y,&changed_w);CHKERRQ(ierr); 3788f99978dSLois Curfman McInnes } 3793c632250SBarry Smith if (changed_y) { 38079f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); /* w <- x - y */ 3813c632250SBarry Smith } 3824936397dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 3834936397dSBarry Smith if (!snes->domainerror) { 384a3b38805SLois Curfman McInnes ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); /* gnorm = || g || */ 385e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 3864936397dSBarry Smith } 387d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 3883a40ed3dSBarry Smith PetscFunctionReturn(0); 3895e76c431SBarry Smith } 39004d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 3914a2ae208SSatish Balay #undef __FUNCT__ 39217bae607SBarry Smith #define __FUNCT__ "SNESLineSearchNoNorms" 39382bf6240SBarry Smith 39429e0b56fSBarry Smith /*@C 39517bae607SBarry Smith SNESLineSearchNoNorms - This routine is not a line search at 39629e0b56fSBarry Smith all; it simply uses the full Newton step. This version does not 39729e0b56fSBarry Smith even compute the norm of the function or search direction; this 39829e0b56fSBarry Smith is intended only when you know the full step is fine and are 39929e0b56fSBarry Smith not checking for convergence of the nonlinear iteration (for 400c7afd0dbSLois Curfman McInnes example, you are running always for a fixed number of Newton steps). 401c7afd0dbSLois Curfman McInnes 4023f9fe445SBarry Smith Logically Collective on SNES and Vec 40329e0b56fSBarry Smith 40429e0b56fSBarry Smith Input Parameters: 405c7afd0dbSLois Curfman McInnes + snes - nonlinear context 406af2d14d2SLois Curfman McInnes . lsctx - optional context for line search (not used here) 40729e0b56fSBarry Smith . x - current iterate 40829e0b56fSBarry Smith . f - residual evaluated at x 4093c632250SBarry Smith . y - search direction 41029e0b56fSBarry Smith . w - work vector 411dc357ad2SBarry Smith . fnorm - 2-norm of f 412dc357ad2SBarry Smith - xnorm - norm of x if known, otherwise 0 41329e0b56fSBarry Smith 41429e0b56fSBarry Smith Output Parameters: 415c7afd0dbSLois Curfman McInnes + g - residual evaluated at new iterate y 4163c632250SBarry Smith . w - new iterate 41729e0b56fSBarry Smith . gnorm - not changed 41829e0b56fSBarry Smith . ynorm - not changed 419a7cc72afSBarry Smith - flag - set to PETSC_TRUE indicating a successful line search 420fee21e36SBarry Smith 42129e0b56fSBarry Smith Options Database Key: 42217bae607SBarry Smith . -snes_ls basicnonorms - Activates SNESLineSearchNoNorms() 42329e0b56fSBarry Smith 4248cbba510SBarry Smith Notes: 42517bae607SBarry Smith SNESLineSearchNoNorms() must be used in conjunction with 426ea56f5baSLois Curfman McInnes either the options 427ea56f5baSLois Curfman McInnes $ -snes_no_convergence_test -snes_max_it <its> 428ea56f5baSLois Curfman McInnes or alternatively a user-defined custom test set via 429329f5518SBarry Smith SNESSetConvergenceTest(); or a -snes_max_it of 1, 430329f5518SBarry Smith otherwise, the SNES solver will generate an error. 431329f5518SBarry Smith 432329f5518SBarry Smith During the final iteration this will not evaluate the function at 433329f5518SBarry Smith the solution point. This is to save a function evaluation while 434329f5518SBarry Smith using pseudo-timestepping. 4358cbba510SBarry Smith 436ea56f5baSLois Curfman McInnes The residual norms printed by monitoring routines such as 437a6570f20SBarry Smith SNESMonitorDefault() (as activated via -snes_monitor) will not be 438ea56f5baSLois Curfman McInnes correct, since they are not computed. 439ea56f5baSLois Curfman McInnes 440ea56f5baSLois Curfman McInnes Level: advanced 4418cbba510SBarry Smith 44229e0b56fSBarry Smith .keywords: SNES, nonlinear, line search, cubic 44329e0b56fSBarry Smith 44417bae607SBarry Smith .seealso: SNESLineSearchCubic(), SNESLineSearchQuadratic(), 44517bae607SBarry Smith SNESLineSearchSet(), SNESLineSearchNo() 44629e0b56fSBarry Smith @*/ 4477087cfbeSBarry Smith PetscErrorCode SNESLineSearchNoNorms(SNES snes,void *lsctx,Vec x,Vec f,Vec g,Vec y,Vec w,PetscReal fnorm,PetscReal xnorm,PetscReal *ynorm,PetscReal *gnorm,PetscBool *flag) 44829e0b56fSBarry Smith { 449dfbe8321SBarry Smith PetscErrorCode ierr; 4504b27c08aSLois Curfman McInnes SNES_LS *neP = (SNES_LS*)snes->data; 451ace3abfcSBarry Smith PetscBool changed_w = PETSC_FALSE,changed_y = PETSC_FALSE; 45229e0b56fSBarry Smith 4533a40ed3dSBarry Smith PetscFunctionBegin; 454a7cc72afSBarry Smith *flag = PETSC_TRUE; 455d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 45679f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); /* w <- x - y */ 4573c632250SBarry Smith if (neP->postcheckstep) { 4583c632250SBarry Smith ierr = (*neP->postcheckstep)(snes,x,y,w,neP->postcheck,&changed_y,&changed_w);CHKERRQ(ierr); 4593c632250SBarry Smith } 4603c632250SBarry Smith if (changed_y) { 46179f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); /* w <- x - y */ 4628f99978dSLois Curfman McInnes } 463329f5518SBarry Smith 464329f5518SBarry Smith /* don't evaluate function the last time through */ 465329f5518SBarry Smith if (snes->iter < snes->max_its-1) { 4664936397dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 467329f5518SBarry Smith } 468d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 4693a40ed3dSBarry Smith PetscFunctionReturn(0); 47029e0b56fSBarry Smith } 47104d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 4724a2ae208SSatish Balay #undef __FUNCT__ 47317bae607SBarry Smith #define __FUNCT__ "SNESLineSearchCubic" 4744b828684SBarry Smith /*@C 47517bae607SBarry Smith SNESLineSearchCubic - Performs a cubic line search (default line search method). 4765e76c431SBarry Smith 477c7afd0dbSLois Curfman McInnes Collective on SNES 478c7afd0dbSLois Curfman McInnes 4795e76c431SBarry Smith Input Parameters: 480c7afd0dbSLois Curfman McInnes + snes - nonlinear context 481af2d14d2SLois Curfman McInnes . lsctx - optional context for line search (not used here) 4825e76c431SBarry Smith . x - current iterate 4835e76c431SBarry Smith . f - residual evaluated at x 4843c632250SBarry Smith . y - search direction 4855e76c431SBarry Smith . w - work vector 486dc357ad2SBarry Smith . fnorm - 2-norm of f 487dc357ad2SBarry Smith - xnorm - norm of x if known, otherwise 0 4885e76c431SBarry Smith 489393d2d9aSLois Curfman McInnes Output Parameters: 490c7afd0dbSLois Curfman McInnes + g - residual evaluated at new iterate y 4913c632250SBarry Smith . w - new iterate 4925e76c431SBarry Smith . gnorm - 2-norm of g 4935e76c431SBarry Smith . ynorm - 2-norm of search length 494a7cc72afSBarry Smith - flag - PETSC_TRUE if line search succeeds; PETSC_FALSE on failure. 495fee21e36SBarry Smith 496c4a48953SLois Curfman McInnes Options Database Key: 497e106eecfSBarry Smith + -snes_ls cubic - Activates SNESLineSearchCubic() 498e106eecfSBarry Smith . -snes_ls_alpha <alpha> - Sets alpha 499e106eecfSBarry Smith . -snes_ls_maxstep <maxstep> - Sets the maximum stepsize the line search will use (if the 2-norm(y) > maxstep then scale y to be y = (maxstep/2-norm(y)) *y) 500e106eecfSBarry Smith - -snes_ls_minlambda <minlambda> - Sets the minimum lambda the line search will use minlambda/ max_i ( y[i]/x[i] ) 501e106eecfSBarry Smith 502c4a48953SLois Curfman McInnes 5035e76c431SBarry Smith Notes: 5045e76c431SBarry Smith This line search is taken from "Numerical Methods for Unconstrained 5055e76c431SBarry Smith Optimization and Nonlinear Equations" by Dennis and Schnabel, page 325. 5065e76c431SBarry Smith 50736851e7fSLois Curfman McInnes Level: advanced 50836851e7fSLois Curfman McInnes 50928ae5a21SLois Curfman McInnes .keywords: SNES, nonlinear, line search, cubic 51028ae5a21SLois Curfman McInnes 51117bae607SBarry Smith .seealso: SNESLineSearchQuadratic(), SNESLineSearchNo(), SNESLineSearchSet(), SNESLineSearchNoNorms() 5125e76c431SBarry Smith @*/ 5137087cfbeSBarry Smith PetscErrorCode SNESLineSearchCubic(SNES snes,void *lsctx,Vec x,Vec f,Vec g,Vec y,Vec w,PetscReal fnorm,PetscReal xnorm,PetscReal *ynorm,PetscReal *gnorm,PetscBool *flag) 5145e76c431SBarry Smith { 515406556e6SLois Curfman McInnes /* 516406556e6SLois Curfman McInnes Note that for line search purposes we work with with the related 517406556e6SLois Curfman McInnes minimization problem: 518406556e6SLois Curfman McInnes min z(x): R^n -> R, 519406556e6SLois Curfman McInnes where z(x) = .5 * fnorm*fnorm, and fnorm = || f ||_2. 520406556e6SLois Curfman McInnes */ 521406556e6SLois Curfman McInnes 522e106eecfSBarry Smith PetscReal initslope,lambdaprev,gnormprev,a,b,d,t1,t2,rellength; 523e106eecfSBarry Smith PetscReal minlambda,lambda,lambdatemp; 524aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 525f5b6597dSBarry Smith PetscScalar cinitslope; 5266b5873e3SBarry Smith #endif 527dfbe8321SBarry Smith PetscErrorCode ierr; 528a7cc72afSBarry Smith PetscInt count; 5294b27c08aSLois Curfman McInnes SNES_LS *neP = (SNES_LS*)snes->data; 530ace3abfcSBarry Smith PetscBool changed_w = PETSC_FALSE,changed_y = PETSC_FALSE; 53194298653SBarry Smith MPI_Comm comm; 5325e76c431SBarry Smith 5333a40ed3dSBarry Smith PetscFunctionBegin; 53494298653SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 535d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 536a7cc72afSBarry Smith *flag = PETSC_TRUE; 5375e76c431SBarry Smith 538cddf8d76SBarry Smith ierr = VecNorm(y,NORM_2,ynorm);CHKERRQ(ierr); 53975567043SBarry Smith if (*ynorm == 0.0) { 54094298653SBarry Smith if (neP->monitor) { 54194298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: Initial direction and size is 0\n");CHKERRQ(ierr); 54294298653SBarry Smith } 543a1c2b6eeSLois Curfman McInnes *gnorm = fnorm; 5443c632250SBarry Smith ierr = VecCopy(x,w);CHKERRQ(ierr); 545a1c2b6eeSLois Curfman McInnes ierr = VecCopy(f,g);CHKERRQ(ierr); 546a7cc72afSBarry Smith *flag = PETSC_FALSE; 547ad922ac9SBarry Smith goto theend1; 54894a9d846SBarry Smith } 549e106eecfSBarry Smith if (*ynorm > neP->maxstep) { /* Step too big, so scale back */ 55094298653SBarry Smith if (neP->monitor) { 55194298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: Scaling step by %G old ynorm %G\n",neP->maxstep/(*ynorm),*ynorm);CHKERRQ(ierr); 55294298653SBarry Smith } 553e106eecfSBarry Smith ierr = VecScale(y,neP->maxstep/(*ynorm));CHKERRQ(ierr); 554e106eecfSBarry Smith *ynorm = neP->maxstep; 5555e76c431SBarry Smith } 556ba6a83e5SMatthew Knepley ierr = VecMaxPointwiseDivide(y,x,&rellength);CHKERRQ(ierr); 557e106eecfSBarry Smith minlambda = neP->minlambda/rellength; 558a703fe33SLois Curfman McInnes ierr = MatMult(snes->jacobian,y,w);CHKERRQ(ierr); 559aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 560a703fe33SLois Curfman McInnes ierr = VecDot(f,w,&cinitslope);CHKERRQ(ierr); 561329f5518SBarry Smith initslope = PetscRealPart(cinitslope); 5625e42470aSBarry Smith #else 563a703fe33SLois Curfman McInnes ierr = VecDot(f,w,&initslope);CHKERRQ(ierr); 5645e42470aSBarry Smith #endif 5655e76c431SBarry Smith if (initslope > 0.0) initslope = -initslope; 5665e76c431SBarry Smith if (initslope == 0.0) initslope = -1.0; 5675e76c431SBarry Smith 568e68848bdSBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); 56943e71028SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 570ae15b995SBarry Smith ierr = PetscInfo(snes,"Exceeded maximum function evaluations, while checking full step length!\n");CHKERRQ(ierr); 57143e71028SBarry Smith *flag = PETSC_FALSE; 57243e71028SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 57343e71028SBarry Smith goto theend1; 57443e71028SBarry Smith } 5754936397dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 5764936397dSBarry Smith if (snes->domainerror) { 5774936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 5784936397dSBarry Smith PetscFunctionReturn(0); 57919717074SBarry Smith } 580313b5538SBarry Smith ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); 581e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 582f5b6597dSBarry Smith ierr = PetscInfo2(snes,"Initial fnorm %G gnorm %G\n",fnorm,*gnorm);CHKERRQ(ierr); 583e106eecfSBarry Smith if (.5*(*gnorm)*(*gnorm) <= .5*fnorm*fnorm + neP->alpha*initslope) { /* Sufficient reduction */ 58494298653SBarry Smith if (neP->monitor) { 58594298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: Using full step: fnorm %G gnorm %G\n",fnorm,*gnorm);CHKERRQ(ierr); 58694298653SBarry Smith } 587ad922ac9SBarry Smith goto theend1; 5885e76c431SBarry Smith } 5895e76c431SBarry Smith 5905e76c431SBarry Smith /* Fit points with quadratic */ 591313b5538SBarry Smith lambda = 1.0; 592a703fe33SLois Curfman McInnes lambdatemp = -initslope/((*gnorm)*(*gnorm) - fnorm*fnorm - 2.0*initslope); 5935e76c431SBarry Smith lambdaprev = lambda; 5945e76c431SBarry Smith gnormprev = *gnorm; 595329f5518SBarry Smith if (lambdatemp > .5*lambda) lambdatemp = .5*lambda; 596ddd12767SBarry Smith if (lambdatemp <= .1*lambda) lambda = .1*lambda; 597ddd12767SBarry Smith else lambda = lambdatemp; 598275d25c3SBarry Smith 599e68848bdSBarry Smith ierr = VecWAXPY(w,-lambda,y,x);CHKERRQ(ierr); 60043e71028SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 601ae15b995SBarry Smith ierr = PetscInfo1(snes,"Exceeded maximum function evaluations, while attempting quadratic backtracking! %D \n",snes->nfuncs);CHKERRQ(ierr); 60243e71028SBarry Smith *flag = PETSC_FALSE; 60343e71028SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 60443e71028SBarry Smith goto theend1; 60543e71028SBarry Smith } 606f5b6597dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 6074936397dSBarry Smith if (snes->domainerror) { 6084936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 6094936397dSBarry Smith PetscFunctionReturn(0); 61019717074SBarry Smith } 611cddf8d76SBarry Smith ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); 612e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 61394298653SBarry Smith if (neP->monitor) { 61494298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: gnorm after quadratic fit %G\n",*gnorm);CHKERRQ(ierr); 61594298653SBarry Smith } 616e106eecfSBarry Smith if (.5*(*gnorm)*(*gnorm) < .5*fnorm*fnorm + lambda*neP->alpha*initslope) { /* sufficient reduction */ 61794298653SBarry Smith if (neP->monitor) { 61894298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: Quadratically determined step, lambda=%18.16e\n",lambda);CHKERRQ(ierr); 61994298653SBarry Smith } 620ad922ac9SBarry Smith goto theend1; 6215e76c431SBarry Smith } 6225e76c431SBarry Smith 6235e76c431SBarry Smith /* Fit points with cubic */ 6245e76c431SBarry Smith count = 1; 625bb9195b6SBarry Smith while (PETSC_TRUE) { 626dc357ad2SBarry Smith if (lambda <= minlambda) { 62794298653SBarry Smith if (neP->monitor) { 62894298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: unable to find good step length! After %D tries \n",count);CHKERRQ(ierr); 62994298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line search: fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, minlambda=%18.16e, lambda=%18.16e, initial slope=%18.16e\n",fnorm,*gnorm,*ynorm,minlambda,lambda,initslope);CHKERRQ(ierr); 63094298653SBarry Smith } 63143e71028SBarry Smith *flag = PETSC_FALSE; 63243e71028SBarry Smith break; 6335e76c431SBarry Smith } 6345d1a10b1SBarry Smith t1 = .5*((*gnorm)*(*gnorm) - fnorm*fnorm) - lambda*initslope; 6355d1a10b1SBarry Smith t2 = .5*(gnormprev*gnormprev - fnorm*fnorm) - lambdaprev*initslope; 636ddd12767SBarry Smith a = (t1/(lambda*lambda) - t2/(lambdaprev*lambdaprev))/(lambda-lambdaprev); 6372b022350SLois Curfman McInnes b = (-lambdaprev*t1/(lambda*lambda) + lambda*t2/(lambdaprev*lambdaprev))/(lambda-lambdaprev); 6385e76c431SBarry Smith d = b*b - 3*a*initslope; 6395e76c431SBarry Smith if (d < 0.0) d = 0.0; 6405e76c431SBarry Smith if (a == 0.0) { 6415e76c431SBarry Smith lambdatemp = -initslope/(2.0*b); 6425e76c431SBarry Smith } else { 6435e76c431SBarry Smith lambdatemp = (-b + sqrt(d))/(3.0*a); 6445e76c431SBarry Smith } 6455e76c431SBarry Smith lambdaprev = lambda; 6465e76c431SBarry Smith gnormprev = *gnorm; 647329f5518SBarry Smith if (lambdatemp > .5*lambda) lambdatemp = .5*lambda; 648329f5518SBarry Smith if (lambdatemp <= .1*lambda) lambda = .1*lambda; 6495e76c431SBarry Smith else lambda = lambdatemp; 650e68848bdSBarry Smith ierr = VecWAXPY(w,-lambda,y,x);CHKERRQ(ierr); 65143e71028SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 652ae15b995SBarry Smith ierr = PetscInfo1(snes,"Exceeded maximum function evaluations, while looking for good step length! %D \n",count);CHKERRQ(ierr); 653ae15b995SBarry Smith ierr = PetscInfo5(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lambda=%18.16e, initial slope=%18.16e\n",fnorm,*gnorm,*ynorm,lambda,initslope);CHKERRQ(ierr); 654ed98166eSMatthew Knepley *flag = PETSC_FALSE; 65543e71028SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 656ed98166eSMatthew Knepley break; 657ed98166eSMatthew Knepley } 658f5b6597dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 6594936397dSBarry Smith if (snes->domainerror) { 6604936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 6614936397dSBarry Smith PetscFunctionReturn(0); 66219717074SBarry Smith } 663cddf8d76SBarry Smith ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); 664e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 665e106eecfSBarry Smith if (.5*(*gnorm)*(*gnorm) < .5*fnorm*fnorm + lambda*neP->alpha*initslope) { /* is reduction enough? */ 66694298653SBarry Smith if (neP->monitor) { 66794298653SBarry Smith ierr = PetscPrintf(comm," Line search: Cubically determined step, current gnorm %G lambda=%18.16e\n",*gnorm,lambda);CHKERRQ(ierr); 66894298653SBarry Smith } 66943e71028SBarry Smith break; 6702b022350SLois Curfman McInnes } else { 67194298653SBarry Smith if (neP->monitor) { 6728c0f1137SBarry Smith ierr = PetscPrintf(comm," Line search: Cubic step no good, shrinking lambda, current gnorm %G lambda=%18.16e\n",*gnorm,lambda);CHKERRQ(ierr); 67394298653SBarry Smith } 6745e76c431SBarry Smith } 6755e76c431SBarry Smith count++; 6765e76c431SBarry Smith } 677ad922ac9SBarry Smith theend1: 6788f99978dSLois Curfman McInnes /* Optional user-defined check for line search step validity */ 6793c632250SBarry Smith if (neP->postcheckstep && *flag) { 6803c632250SBarry Smith ierr = (*neP->postcheckstep)(snes,x,y,w,neP->postcheck,&changed_y,&changed_w);CHKERRQ(ierr); 6813c632250SBarry Smith if (changed_y) { 68279f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); 6833c632250SBarry Smith } 6843c632250SBarry Smith if (changed_y || changed_w) { /* recompute the function if the step has changed */ 685f5b6597dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 6864936397dSBarry Smith if (snes->domainerror) { 6874936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 6884936397dSBarry Smith PetscFunctionReturn(0); 68919717074SBarry Smith } 6908f99978dSLois Curfman McInnes ierr = VecNormBegin(g,NORM_2,gnorm);CHKERRQ(ierr); 691e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 692a9f58f88SMatthew Knepley ierr = VecNormBegin(y,NORM_2,ynorm);CHKERRQ(ierr); 6938f99978dSLois Curfman McInnes ierr = VecNormEnd(g,NORM_2,gnorm);CHKERRQ(ierr); 694a9f58f88SMatthew Knepley ierr = VecNormEnd(y,NORM_2,ynorm);CHKERRQ(ierr); 6958f99978dSLois Curfman McInnes } 6968f99978dSLois Curfman McInnes } 697d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 6983a40ed3dSBarry Smith PetscFunctionReturn(0); 6995e76c431SBarry Smith } 70004d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 7014a2ae208SSatish Balay #undef __FUNCT__ 70217bae607SBarry Smith #define __FUNCT__ "SNESLineSearchQuadratic" 7034b828684SBarry Smith /*@C 70417bae607SBarry Smith SNESLineSearchQuadratic - Performs a quadratic line search. 7055e76c431SBarry Smith 706c7afd0dbSLois Curfman McInnes Collective on SNES and Vec 707c7afd0dbSLois Curfman McInnes 7085e42470aSBarry Smith Input Parameters: 709c7afd0dbSLois Curfman McInnes + snes - the SNES context 710af2d14d2SLois Curfman McInnes . lsctx - optional context for line search (not used here) 7115e42470aSBarry Smith . x - current iterate 7125e42470aSBarry Smith . f - residual evaluated at x 7133c632250SBarry Smith . y - search direction 7145e42470aSBarry Smith . w - work vector 715dc357ad2SBarry Smith . fnorm - 2-norm of f 716dc357ad2SBarry Smith - xnorm - norm of x if known, otherwise 0 7175e42470aSBarry Smith 718c4a48953SLois Curfman McInnes Output Parameters: 7197f3332b4SBarry Smith + g - residual evaluated at new iterate w 720e106eecfSBarry Smith . w - new iterate (x + lambda*y) 7215e42470aSBarry Smith . gnorm - 2-norm of g 7225e42470aSBarry Smith . ynorm - 2-norm of search length 723a7cc72afSBarry Smith - flag - PETSC_TRUE if line search succeeds; PETSC_FALSE on failure. 724fee21e36SBarry Smith 725ce9499c7SBarry Smith Options Database Keys: 726ce9499c7SBarry Smith + -snes_ls quadratic - Activates SNESLineSearchQuadratic() 727ce9499c7SBarry Smith . -snes_ls_alpha <alpha> - Sets alpha 728e106eecfSBarry Smith . -snes_ls_maxstep <maxstep> - Sets the maximum stepsize the line search will use (if the 2-norm(y) > maxstep then scale y to be y = (maxstep/2-norm(y)) *y) 729e106eecfSBarry Smith - -snes_ls_minlambda <minlambda> - Sets the minimum lambda the line search will use minlambda/ max_i ( y[i]/x[i] ) 730e106eecfSBarry Smith 7315e42470aSBarry Smith Notes: 73217bae607SBarry Smith Use SNESLineSearchSet() to set this routine within the SNESLS method. 7335e42470aSBarry Smith 73436851e7fSLois Curfman McInnes Level: advanced 73536851e7fSLois Curfman McInnes 736f59ffdedSLois Curfman McInnes .keywords: SNES, nonlinear, quadratic, line search 737f59ffdedSLois Curfman McInnes 73817bae607SBarry Smith .seealso: SNESLineSearchCubic(), SNESLineSearchNo(), SNESLineSearchSet(), SNESLineSearchNoNorms() 7395e42470aSBarry Smith @*/ 7407087cfbeSBarry Smith PetscErrorCode SNESLineSearchQuadratic(SNES snes,void *lsctx,Vec x,Vec f,Vec g,Vec y,Vec w,PetscReal fnorm,PetscReal xnorm,PetscReal *ynorm,PetscReal *gnorm,PetscBool *flag) 7415e76c431SBarry Smith { 742406556e6SLois Curfman McInnes /* 743406556e6SLois Curfman McInnes Note that for line search purposes we work with with the related 744406556e6SLois Curfman McInnes minimization problem: 745406556e6SLois Curfman McInnes min z(x): R^n -> R, 746406556e6SLois Curfman McInnes where z(x) = .5 * fnorm*fnorm,and fnorm = || f ||_2. 747406556e6SLois Curfman McInnes */ 748e106eecfSBarry Smith PetscReal initslope,minlambda,lambda,lambdatemp,rellength; 749aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 750f5b6597dSBarry Smith PetscScalar cinitslope; 7516b5873e3SBarry Smith #endif 752dfbe8321SBarry Smith PetscErrorCode ierr; 753a7cc72afSBarry Smith PetscInt count; 7544b27c08aSLois Curfman McInnes SNES_LS *neP = (SNES_LS*)snes->data; 755ace3abfcSBarry Smith PetscBool changed_w = PETSC_FALSE,changed_y = PETSC_FALSE; 7565e76c431SBarry Smith 7573a40ed3dSBarry Smith PetscFunctionBegin; 758d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 759a7cc72afSBarry Smith *flag = PETSC_TRUE; 7605e76c431SBarry Smith 7613505fcc1SBarry Smith ierr = VecNorm(y,NORM_2,ynorm);CHKERRQ(ierr); 76263b9fa5eSBarry Smith if (*ynorm == 0.0) { 76394298653SBarry Smith if (neP->monitor) { 76494298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor,"Line search: Direction and size is 0\n");CHKERRQ(ierr); 76594298653SBarry Smith } 766b37302c6SLois Curfman McInnes *gnorm = fnorm; 767e68848bdSBarry Smith ierr = VecCopy(x,w);CHKERRQ(ierr); 768b37302c6SLois Curfman McInnes ierr = VecCopy(f,g);CHKERRQ(ierr); 769a7cc72afSBarry Smith *flag = PETSC_FALSE; 770ad922ac9SBarry Smith goto theend2; 77194a9d846SBarry Smith } 772e106eecfSBarry Smith if (*ynorm > neP->maxstep) { /* Step too big, so scale back */ 773e106eecfSBarry Smith ierr = VecScale(y,neP->maxstep/(*ynorm));CHKERRQ(ierr); 774e106eecfSBarry Smith *ynorm = neP->maxstep; 7755e76c431SBarry Smith } 776ba6a83e5SMatthew Knepley ierr = VecMaxPointwiseDivide(y,x,&rellength);CHKERRQ(ierr); 777e106eecfSBarry Smith minlambda = neP->minlambda/rellength; 778a703fe33SLois Curfman McInnes ierr = MatMult(snes->jacobian,y,w);CHKERRQ(ierr); 779aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 780a703fe33SLois Curfman McInnes ierr = VecDot(f,w,&cinitslope);CHKERRQ(ierr); 781329f5518SBarry Smith initslope = PetscRealPart(cinitslope); 7825e42470aSBarry Smith #else 783a703fe33SLois Curfman McInnes ierr = VecDot(f,w,&initslope);CHKERRQ(ierr); 7845e42470aSBarry Smith #endif 7855e42470aSBarry Smith if (initslope > 0.0) initslope = -initslope; 7865e42470aSBarry Smith if (initslope == 0.0) initslope = -1.0; 787f8833479SBarry Smith ierr = PetscInfo1(snes,"Initslope %G \n",initslope);CHKERRQ(ierr); 7885e42470aSBarry Smith 789e68848bdSBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); 79043e71028SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 791ae15b995SBarry Smith ierr = PetscInfo(snes,"Exceeded maximum function evaluations, while checking full step length!\n");CHKERRQ(ierr); 79243e71028SBarry Smith *flag = PETSC_FALSE; 79343e71028SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 79443e71028SBarry Smith goto theend2; 79543e71028SBarry Smith } 7964936397dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 7974936397dSBarry Smith if (snes->domainerror) { 7984936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 7994936397dSBarry Smith PetscFunctionReturn(0); 80019717074SBarry Smith } 801cddf8d76SBarry Smith ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); 802e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 803e106eecfSBarry Smith if (.5*(*gnorm)*(*gnorm) <= .5*fnorm*fnorm + neP->alpha*initslope) { /* Sufficient reduction */ 80494298653SBarry Smith if (neP->monitor) { 80594298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line Search: Using full step\n");CHKERRQ(ierr); 80694298653SBarry Smith } 807ad922ac9SBarry Smith goto theend2; 8085e42470aSBarry Smith } 8095e42470aSBarry Smith 8105e42470aSBarry Smith /* Fit points with quadratic */ 811313b5538SBarry Smith lambda = 1.0; 8125e42470aSBarry Smith count = 1; 8135ca10a37SBarry Smith while (PETSC_TRUE) { 8145e42470aSBarry Smith if (lambda <= minlambda) { /* bad luck; use full step */ 81594298653SBarry Smith if (neP->monitor) { 81694298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor,"Line search: Unable to find good step length! %D \n",count);CHKERRQ(ierr); 81794298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor,"Line search: fnorm=%G, gnorm=%G, ynorm=%G, lambda=%G, initial slope=%G\n",fnorm,*gnorm,*ynorm,lambda,initslope);CHKERRQ(ierr); 81894298653SBarry Smith } 819e68848bdSBarry Smith ierr = VecCopy(x,w);CHKERRQ(ierr); 82043e71028SBarry Smith *flag = PETSC_FALSE; 82143e71028SBarry Smith break; 8225e42470aSBarry Smith } 823a703fe33SLois Curfman McInnes lambdatemp = -initslope/((*gnorm)*(*gnorm) - fnorm*fnorm - 2.0*initslope); 824329f5518SBarry Smith if (lambdatemp > .5*lambda) lambdatemp = .5*lambda; 825329f5518SBarry Smith if (lambdatemp <= .1*lambda) lambda = .1*lambda; 826329f5518SBarry Smith else lambda = lambdatemp; 827275d25c3SBarry Smith 828e68848bdSBarry Smith ierr = VecWAXPY(w,-lambda,y,x);CHKERRQ(ierr); 82943e71028SBarry Smith if (snes->nfuncs >= snes->max_funcs) { 830ae15b995SBarry Smith ierr = PetscInfo1(snes,"Exceeded maximum function evaluations, while looking for good step length! %D \n",count);CHKERRQ(ierr); 831ae15b995SBarry Smith ierr = PetscInfo5(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lambda=%18.16e, initial slope=%18.16e\n",fnorm,*gnorm,*ynorm,lambda,initslope);CHKERRQ(ierr); 832ed98166eSMatthew Knepley *flag = PETSC_FALSE; 83343e71028SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_COUNT; 834ed98166eSMatthew Knepley break; 835ed98166eSMatthew Knepley } 836f5b6597dSBarry Smith ierr = SNESComputeFunction(snes,w,g);CHKERRQ(ierr); 8374936397dSBarry Smith if (snes->domainerror) { 8384936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 8394936397dSBarry Smith PetscFunctionReturn(0); 84019717074SBarry Smith } 841cddf8d76SBarry Smith ierr = VecNorm(g,NORM_2,gnorm);CHKERRQ(ierr); 842e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 843e106eecfSBarry Smith if (.5*(*gnorm)*(*gnorm) < .5*fnorm*fnorm + lambda*neP->alpha*initslope) { /* sufficient reduction */ 84494298653SBarry Smith if (neP->monitor) { 84594298653SBarry Smith ierr = PetscViewerASCIIMonitorPrintf(neP->monitor," Line Search: Quadratically determined step, lambda=%G\n",lambda);CHKERRQ(ierr); 84694298653SBarry Smith } 84706259719SBarry Smith break; 8485e42470aSBarry Smith } 8495e42470aSBarry Smith count++; 8505e42470aSBarry Smith } 851ad922ac9SBarry Smith theend2: 8528f99978dSLois Curfman McInnes /* Optional user-defined check for line search step validity */ 8533c632250SBarry Smith if (neP->postcheckstep) { 8543c632250SBarry Smith ierr = (*neP->postcheckstep)(snes,x,y,w,neP->postcheck,&changed_y,&changed_w);CHKERRQ(ierr); 8553c632250SBarry Smith if (changed_y) { 85679f36460SBarry Smith ierr = VecWAXPY(w,-1.0,y,x);CHKERRQ(ierr); 8573c632250SBarry Smith } 8583c632250SBarry Smith if (changed_y || changed_w) { /* recompute the function if the step has changed */ 8593c632250SBarry Smith ierr = SNESComputeFunction(snes,w,g); 8604936397dSBarry Smith if (snes->domainerror) { 8614936397dSBarry Smith ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 8624936397dSBarry Smith PetscFunctionReturn(0); 86319717074SBarry Smith } 8648f99978dSLois Curfman McInnes ierr = VecNormBegin(g,NORM_2,gnorm);CHKERRQ(ierr); 865a9f58f88SMatthew Knepley ierr = VecNormBegin(y,NORM_2,ynorm);CHKERRQ(ierr); 8668f99978dSLois Curfman McInnes ierr = VecNormEnd(g,NORM_2,gnorm);CHKERRQ(ierr); 867a9f58f88SMatthew Knepley ierr = VecNormEnd(y,NORM_2,ynorm);CHKERRQ(ierr); 868e32f2f54SBarry Smith if PetscIsInfOrNanReal(*gnorm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number"); 8698f99978dSLois Curfman McInnes } 8708f99978dSLois Curfman McInnes } 871d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_LineSearch,snes,x,f,g);CHKERRQ(ierr); 8723a40ed3dSBarry Smith PetscFunctionReturn(0); 8735e76c431SBarry Smith } 8742343ba6eSBarry Smith 87504d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 8764a2ae208SSatish Balay #undef __FUNCT__ 87717bae607SBarry Smith #define __FUNCT__ "SNESLineSearchSet" 878c9e6a524SLois Curfman McInnes /*@C 87917bae607SBarry Smith SNESLineSearchSet - Sets the line search routine to be used 8804b27c08aSLois Curfman McInnes by the method SNESLS. 8815e76c431SBarry Smith 8825e76c431SBarry Smith Input Parameters: 883c7afd0dbSLois Curfman McInnes + snes - nonlinear context obtained from SNESCreate() 884af2d14d2SLois Curfman McInnes . lsctx - optional user-defined context for use by line search 885c7afd0dbSLois Curfman McInnes - func - pointer to int function 8865e76c431SBarry Smith 8873f9fe445SBarry Smith Logically Collective on SNES 888fee21e36SBarry Smith 889c4a48953SLois Curfman McInnes Available Routines: 89017bae607SBarry Smith + SNESLineSearchCubic() - default line search 89117bae607SBarry Smith . SNESLineSearchQuadratic() - quadratic line search 89217bae607SBarry Smith . SNESLineSearchNo() - the full Newton step (actually not a line search) 89317bae607SBarry Smith - SNESLineSearchNoNorms() - the full Newton step (calculating no norms; faster in parallel) 8945e76c431SBarry Smith 895c4a48953SLois Curfman McInnes Options Database Keys: 8964b27c08aSLois Curfman McInnes + -snes_ls [cubic,quadratic,basic,basicnonorms] - Selects line search 8974b27c08aSLois Curfman McInnes . -snes_ls_alpha <alpha> - Sets alpha 898e106eecfSBarry Smith . -snes_ls_maxstep <maxstep> - Sets maximum step the line search will use (if the 2-norm(y) > maxstep then scale y to be y = (maxstep/2-norm(y)) *y) 899e106eecfSBarry Smith - -snes_ls_minlambda <minlambda> - Sets the minimum lambda the line search will use minlambda / max_i ( y[i]/x[i] ) 900c4a48953SLois Curfman McInnes 9015e76c431SBarry Smith Calling sequence of func: 902bd208895SLois Curfman McInnes .vb 903ace3abfcSBarry Smith func (SNES snes,void *lsctx,Vec x,Vec f,Vec g,Vec y,Vec w,PetscReal fnorm,PetscReal xnorm,PetscReal *ynorm,PetscReal *gnorm,PetscBool *flag) 904bd208895SLois Curfman McInnes .ve 9055e76c431SBarry Smith 9065e76c431SBarry Smith Input parameters for func: 907c7afd0dbSLois Curfman McInnes + snes - nonlinear context 908af2d14d2SLois Curfman McInnes . lsctx - optional user-defined context for line search 9095e76c431SBarry Smith . x - current iterate 9105e76c431SBarry Smith . f - residual evaluated at x 9113c632250SBarry Smith . y - search direction 912c7afd0dbSLois Curfman McInnes - fnorm - 2-norm of f 9135e76c431SBarry Smith 9145e76c431SBarry Smith Output parameters for func: 915c7afd0dbSLois Curfman McInnes + g - residual evaluated at new iterate y 9163c632250SBarry Smith . w - new iterate 9175e76c431SBarry Smith . gnorm - 2-norm of g 9185e76c431SBarry Smith . ynorm - 2-norm of search length 919a7cc72afSBarry Smith - flag - set to PETSC_TRUE if the line search succeeds; PETSC_FALSE on failure. 920f59ffdedSLois Curfman McInnes 92136851e7fSLois Curfman McInnes Level: advanced 92236851e7fSLois Curfman McInnes 923f59ffdedSLois Curfman McInnes .keywords: SNES, nonlinear, set, line search, routine 924f59ffdedSLois Curfman McInnes 92517bae607SBarry Smith .seealso: SNESLineSearchCubic(), SNESLineSearchQuadratic(), SNESLineSearchNo(), SNESLineSearchNoNorms(), 92617bae607SBarry Smith SNESLineSearchSetPostCheck(), SNESLineSearchSetParams(), SNESLineSearchGetParams(), SNESLineSearchSetPreCheck() 9275e76c431SBarry Smith @*/ 9287087cfbeSBarry Smith PetscErrorCode SNESLineSearchSet(SNES snes,PetscErrorCode (*func)(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal*,PetscReal*,PetscBool *),void *lsctx) 9295e76c431SBarry Smith { 9304ac538c5SBarry Smith PetscErrorCode ierr; 93182bf6240SBarry Smith 9323a40ed3dSBarry Smith PetscFunctionBegin; 9334ac538c5SBarry Smith ierr = PetscTryMethod(snes,"SNESLineSearchSet_C",(SNES,PetscErrorCode (*)(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal*,PetscReal*,PetscBool *),void*),(snes,func,lsctx));CHKERRQ(ierr); 9343a40ed3dSBarry Smith PetscFunctionReturn(0); 9355e76c431SBarry Smith } 9368e019c35SBarry Smith 937ace3abfcSBarry Smith typedef PetscErrorCode (*FCN2)(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal*,PetscReal*,PetscBool *); /* force argument to next function to not be extern C*/ 93804d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 939fb2e594dSBarry Smith EXTERN_C_BEGIN 9404a2ae208SSatish Balay #undef __FUNCT__ 94117bae607SBarry Smith #define __FUNCT__ "SNESLineSearchSet_LS" 9427087cfbeSBarry Smith PetscErrorCode SNESLineSearchSet_LS(SNES snes,FCN2 func,void *lsctx) 94382bf6240SBarry Smith { 94482bf6240SBarry Smith PetscFunctionBegin; 9454b27c08aSLois Curfman McInnes ((SNES_LS *)(snes->data))->LineSearch = func; 9464b27c08aSLois Curfman McInnes ((SNES_LS *)(snes->data))->lsP = lsctx; 94782bf6240SBarry Smith PetscFunctionReturn(0); 94882bf6240SBarry Smith } 949fb2e594dSBarry Smith EXTERN_C_END 95004d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 9514a2ae208SSatish Balay #undef __FUNCT__ 9523c632250SBarry Smith #define __FUNCT__ "SNESLineSearchSetPostCheck" 953c8dd0c0dSLois Curfman McInnes /*@C 9543c632250SBarry Smith SNESLineSearchSetPostCheck - Sets a routine to check the validity of new iterate computed 9554b27c08aSLois Curfman McInnes by the line search routine in the Newton-based method SNESLS. 956c8dd0c0dSLois Curfman McInnes 957c8dd0c0dSLois Curfman McInnes Input Parameters: 958c8dd0c0dSLois Curfman McInnes + snes - nonlinear context obtained from SNESCreate() 9593c632250SBarry Smith . func - pointer to function 960c8dd0c0dSLois Curfman McInnes - checkctx - optional user-defined context for use by step checking routine 961c8dd0c0dSLois Curfman McInnes 9623f9fe445SBarry Smith Logically Collective on SNES 963c8dd0c0dSLois Curfman McInnes 964c8dd0c0dSLois Curfman McInnes Calling sequence of func: 965c8dd0c0dSLois Curfman McInnes .vb 966ace3abfcSBarry Smith int func (SNES snes, Vec x,Vec y,Vec w,void *checkctx, PetscBool *changed_y,PetscBool *changed_w) 967c8dd0c0dSLois Curfman McInnes .ve 968b1ae27eaSLois Curfman McInnes where func returns an error code of 0 on success and a nonzero 969b1ae27eaSLois Curfman McInnes on failure. 970c8dd0c0dSLois Curfman McInnes 971c8dd0c0dSLois Curfman McInnes Input parameters for func: 972c8dd0c0dSLois Curfman McInnes + snes - nonlinear context 973c8dd0c0dSLois Curfman McInnes . checkctx - optional user-defined context for use by step checking routine 9743c632250SBarry Smith . x - previous iterate 9753c632250SBarry Smith . y - new search direction and length 9763c632250SBarry Smith - w - current candidate iterate 977c8dd0c0dSLois Curfman McInnes 978c8dd0c0dSLois Curfman McInnes Output parameters for func: 9793c632250SBarry Smith + y - search direction (possibly changed) 9803c632250SBarry Smith . w - current iterate (possibly modified) 9813c632250SBarry Smith . changed_y - indicates search direction was changed by this routine 9823c632250SBarry Smith - changed_w - indicates current iterate was changed by this routine 983c8dd0c0dSLois Curfman McInnes 984c8dd0c0dSLois Curfman McInnes Level: advanced 985c8dd0c0dSLois Curfman McInnes 9869e247f21SBarry Smith Notes: All line searches accept the new iterate computed by the line search checking routine. 9879e247f21SBarry Smith 9883c632250SBarry Smith Only one of changed_y and changed_w can be PETSC_TRUE 9893c632250SBarry Smith 9903c632250SBarry Smith On input w = x + y 9913c632250SBarry Smith 99217bae607SBarry Smith SNESLineSearchNo() and SNESLineSearchNoNorms() (1) compute a candidate iterate u_{i+1}, (2) pass control 993b1ae27eaSLois Curfman McInnes to the checking routine, and then (3) compute the corresponding nonlinear 994ea56f5baSLois Curfman McInnes function f(u_{i+1}) with the (possibly altered) iterate u_{i+1}. 9958f99978dSLois Curfman McInnes 99617bae607SBarry Smith SNESLineSearchQuadratic() and SNESLineSearchCubic() (1) compute a candidate iterate u_{i+1} as well as a 997ea56f5baSLois Curfman McInnes candidate nonlinear function f(u_{i+1}), (2) pass control to the checking 998ea56f5baSLois Curfman McInnes routine, and then (3) force a re-evaluation of f(u_{i+1}) if any changes 999ea56f5baSLois Curfman McInnes were made to the candidate iterate in the checking routine (as indicated 10009e247f21SBarry Smith by flag=PETSC_TRUE). The overhead of this extra function re-evaluation can be 1001b1ae27eaSLois Curfman McInnes very costly, so use this feature with caution! 10028f99978dSLois Curfman McInnes 1003c8dd0c0dSLois Curfman McInnes .keywords: SNES, nonlinear, set, line search check, step check, routine 1004c8dd0c0dSLois Curfman McInnes 100517bae607SBarry Smith .seealso: SNESLineSearchSet(), SNESLineSearchSetPreCheck() 1006c8dd0c0dSLois Curfman McInnes @*/ 10077087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetPostCheck(SNES snes,PetscErrorCode (*func)(SNES,Vec,Vec,Vec,void*,PetscBool *,PetscBool *),void *checkctx) 1008c8dd0c0dSLois Curfman McInnes { 10094ac538c5SBarry Smith PetscErrorCode ierr; 1010c8dd0c0dSLois Curfman McInnes 1011c8dd0c0dSLois Curfman McInnes PetscFunctionBegin; 10124ac538c5SBarry Smith ierr = PetscTryMethod(snes,"SNESLineSearchSetPostCheck_C",(SNES,PetscErrorCode (*)(SNES,Vec,Vec,Vec,void*,PetscBool *,PetscBool *),void*),(snes,func,checkctx));CHKERRQ(ierr); 1013c8dd0c0dSLois Curfman McInnes PetscFunctionReturn(0); 1014c8dd0c0dSLois Curfman McInnes } 101594298653SBarry Smith 10169c3ca13aSBarry Smith #undef __FUNCT__ 10179c3ca13aSBarry Smith #define __FUNCT__ "SNESLineSearchSetPreCheck" 10189c3ca13aSBarry Smith /*@C 10199c3ca13aSBarry Smith SNESLineSearchSetPreCheck - Sets a routine to check the validity of a new direction given by the linear solve 10207e4bb74cSBarry Smith before the line search is called. 10219c3ca13aSBarry Smith 10229c3ca13aSBarry Smith Input Parameters: 10239c3ca13aSBarry Smith + snes - nonlinear context obtained from SNESCreate() 10249c3ca13aSBarry Smith . func - pointer to function 10259c3ca13aSBarry Smith - checkctx - optional user-defined context for use by step checking routine 10269c3ca13aSBarry Smith 10273f9fe445SBarry Smith Logically Collective on SNES 10289c3ca13aSBarry Smith 10299c3ca13aSBarry Smith Calling sequence of func: 10309c3ca13aSBarry Smith .vb 1031ace3abfcSBarry Smith int func (SNES snes, Vec x,Vec y,void *checkctx, PetscBool *changed_y) 10329c3ca13aSBarry Smith .ve 10339c3ca13aSBarry Smith where func returns an error code of 0 on success and a nonzero 10349c3ca13aSBarry Smith on failure. 10359c3ca13aSBarry Smith 10369c3ca13aSBarry Smith Input parameters for func: 10379c3ca13aSBarry Smith + snes - nonlinear context 10389c3ca13aSBarry Smith . checkctx - optional user-defined context for use by step checking routine 10399c3ca13aSBarry Smith . x - previous iterate 10409c3ca13aSBarry Smith - y - new search direction and length 10419c3ca13aSBarry Smith 10429c3ca13aSBarry Smith Output parameters for func: 10439c3ca13aSBarry Smith + y - search direction (possibly changed) 10449c3ca13aSBarry Smith - changed_y - indicates search direction was changed by this routine 10459c3ca13aSBarry Smith 10469c3ca13aSBarry Smith Level: advanced 10479c3ca13aSBarry Smith 10489c3ca13aSBarry Smith Notes: All line searches accept the new iterate computed by the line search checking routine. 10499c3ca13aSBarry Smith 10509c3ca13aSBarry Smith .keywords: SNES, nonlinear, set, line search check, step check, routine 10519c3ca13aSBarry Smith 10527e4bb74cSBarry Smith .seealso: SNESLineSearchSet(), SNESLineSearchSetPostCheck(), SNESSetUpdate() 10539c3ca13aSBarry Smith @*/ 10547087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetPreCheck(SNES snes,PetscErrorCode (*func)(SNES,Vec,Vec,void*,PetscBool *),void *checkctx) 10559c3ca13aSBarry Smith { 10564ac538c5SBarry Smith PetscErrorCode ierr; 10579c3ca13aSBarry Smith 10589c3ca13aSBarry Smith PetscFunctionBegin; 10594ac538c5SBarry Smith ierr = PetscTryMethod(snes,"SNESLineSearchSetPreCheck_C",(SNES,PetscErrorCode (*)(SNES,Vec,Vec,void*,PetscBool *),void*),(snes,func,checkctx));CHKERRQ(ierr); 10609c3ca13aSBarry Smith PetscFunctionReturn(0); 10619c3ca13aSBarry Smith } 10629c3ca13aSBarry Smith 106394298653SBarry Smith #undef __FUNCT__ 106494298653SBarry Smith #define __FUNCT__ "SNESLineSearchSetMonitor" 106594298653SBarry Smith /*@C 106694298653SBarry Smith SNESLineSearchSetMonitor - Prints information about the progress or lack of progress of the line search 106794298653SBarry Smith 106894298653SBarry Smith Input Parameters: 106994298653SBarry Smith + snes - nonlinear context obtained from SNESCreate() 107094298653SBarry Smith - flg - PETSC_TRUE to monitor the line search 107194298653SBarry Smith 10723f9fe445SBarry Smith Logically Collective on SNES 107394298653SBarry Smith 107494298653SBarry Smith Options Database: 107594298653SBarry Smith . -snes_ls_monitor 107694298653SBarry Smith 107794298653SBarry Smith Level: intermediate 107894298653SBarry Smith 107994298653SBarry Smith 108094298653SBarry Smith .seealso: SNESLineSearchSet(), SNESLineSearchSetPostCheck(), SNESSetUpdate() 108194298653SBarry Smith @*/ 10827087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetMonitor(SNES snes,PetscBool flg) 108394298653SBarry Smith { 10844ac538c5SBarry Smith PetscErrorCode ierr; 108594298653SBarry Smith 108694298653SBarry Smith PetscFunctionBegin; 10874ac538c5SBarry Smith ierr = PetscTryMethod(snes,"SNESLineSearchSetMonitor_C",(SNES,PetscBool),(snes,flg));CHKERRQ(ierr); 108894298653SBarry Smith PetscFunctionReturn(0); 108994298653SBarry Smith } 109094298653SBarry Smith 1091c8dd0c0dSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 1092ace3abfcSBarry Smith typedef PetscErrorCode (*FCN1)(SNES,Vec,Vec,Vec,void*,PetscBool *,PetscBool *); /* force argument to next function to not be extern C*/ 1093ace3abfcSBarry Smith typedef PetscErrorCode (*FCN3)(SNES,Vec,Vec,void*,PetscBool *); /* force argument to next function to not be extern C*/ 1094c8dd0c0dSLois Curfman McInnes EXTERN_C_BEGIN 10954a2ae208SSatish Balay #undef __FUNCT__ 10963c632250SBarry Smith #define __FUNCT__ "SNESLineSearchSetPostCheck_LS" 10977087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetPostCheck_LS(SNES snes,FCN1 func,void *checkctx) 1098c8dd0c0dSLois Curfman McInnes { 1099c8dd0c0dSLois Curfman McInnes PetscFunctionBegin; 11003c632250SBarry Smith ((SNES_LS *)(snes->data))->postcheckstep = func; 11013c632250SBarry Smith ((SNES_LS *)(snes->data))->postcheck = checkctx; 1102c8dd0c0dSLois Curfman McInnes PetscFunctionReturn(0); 1103c8dd0c0dSLois Curfman McInnes } 1104c8dd0c0dSLois Curfman McInnes EXTERN_C_END 11059c3ca13aSBarry Smith 11069c3ca13aSBarry Smith EXTERN_C_BEGIN 11079c3ca13aSBarry Smith #undef __FUNCT__ 11089c3ca13aSBarry Smith #define __FUNCT__ "SNESLineSearchSetPreCheck_LS" 11097087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetPreCheck_LS(SNES snes,FCN3 func,void *checkctx) 11109c3ca13aSBarry Smith { 11119c3ca13aSBarry Smith PetscFunctionBegin; 11129c3ca13aSBarry Smith ((SNES_LS *)(snes->data))->precheckstep = func; 11139c3ca13aSBarry Smith ((SNES_LS *)(snes->data))->precheck = checkctx; 11149c3ca13aSBarry Smith PetscFunctionReturn(0); 11159c3ca13aSBarry Smith } 11169c3ca13aSBarry Smith EXTERN_C_END 1117329e7e40SMatthew Knepley 111894298653SBarry Smith EXTERN_C_BEGIN 111994298653SBarry Smith #undef __FUNCT__ 112094298653SBarry Smith #define __FUNCT__ "SNESLineSearchSetMonitor_LS" 11217087cfbeSBarry Smith PetscErrorCode SNESLineSearchSetMonitor_LS(SNES snes,PetscBool flg) 112294298653SBarry Smith { 112394298653SBarry Smith SNES_LS *ls = (SNES_LS*)snes->data; 112494298653SBarry Smith PetscErrorCode ierr; 112594298653SBarry Smith 112694298653SBarry Smith PetscFunctionBegin; 112794298653SBarry Smith if (flg && !ls->monitor) { 112894298653SBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,"stdout",((PetscObject)snes)->tablevel,&ls->monitor);CHKERRQ(ierr); 112994298653SBarry Smith } else if (!flg && ls->monitor) { 113094298653SBarry Smith ierr = PetscViewerASCIIMonitorDestroy(ls->monitor);CHKERRQ(ierr); 113194298653SBarry Smith } 113294298653SBarry Smith PetscFunctionReturn(0); 113394298653SBarry Smith } 113494298653SBarry Smith EXTERN_C_END 113594298653SBarry Smith 1136329e7e40SMatthew Knepley /* 11374b27c08aSLois Curfman McInnes SNESView_LS - Prints info from the SNESLS data structure. 113804d965bbSLois Curfman McInnes 113904d965bbSLois Curfman McInnes Input Parameters: 114004d965bbSLois Curfman McInnes . SNES - the SNES context 114104d965bbSLois Curfman McInnes . viewer - visualization context 114204d965bbSLois Curfman McInnes 114304d965bbSLois Curfman McInnes Application Interface Routine: SNESView() 114404d965bbSLois Curfman McInnes */ 11454a2ae208SSatish Balay #undef __FUNCT__ 11464b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESView_LS" 11476849ba73SBarry Smith static PetscErrorCode SNESView_LS(SNES snes,PetscViewer viewer) 1148a935fc98SLois Curfman McInnes { 11494b27c08aSLois Curfman McInnes SNES_LS *ls = (SNES_LS *)snes->data; 11502fc52814SBarry Smith const char *cstr; 1151dfbe8321SBarry Smith PetscErrorCode ierr; 1152ace3abfcSBarry Smith PetscBool iascii; 1153a935fc98SLois Curfman McInnes 11543a40ed3dSBarry Smith PetscFunctionBegin; 11552692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 115632077d6dSBarry Smith if (iascii) { 115717bae607SBarry Smith if (ls->LineSearch == SNESLineSearchNo) cstr = "SNESLineSearchNo"; 115817bae607SBarry Smith else if (ls->LineSearch == SNESLineSearchQuadratic) cstr = "SNESLineSearchQuadratic"; 115917bae607SBarry Smith else if (ls->LineSearch == SNESLineSearchCubic) cstr = "SNESLineSearchCubic"; 116019bcc07fSBarry Smith else cstr = "unknown"; 1161b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," line search variant: %s\n",cstr);CHKERRQ(ierr); 1162e106eecfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," alpha=%G, maxstep=%G, minlambda=%G\n",ls->alpha,ls->maxstep,ls->minlambda);CHKERRQ(ierr); 11635cd90555SBarry Smith } else { 1164e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for SNES EQ LS",((PetscObject)viewer)->type_name); 116519bcc07fSBarry Smith } 11663a40ed3dSBarry Smith PetscFunctionReturn(0); 1167a935fc98SLois Curfman McInnes } 116804d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 116904d965bbSLois Curfman McInnes /* 11704b27c08aSLois Curfman McInnes SNESSetFromOptions_LS - Sets various parameters for the SNESLS method. 117104d965bbSLois Curfman McInnes 117204d965bbSLois Curfman McInnes Input Parameter: 117304d965bbSLois Curfman McInnes . snes - the SNES context 117404d965bbSLois Curfman McInnes 117504d965bbSLois Curfman McInnes Application Interface Routine: SNESSetFromOptions() 117604d965bbSLois Curfman McInnes */ 11774a2ae208SSatish Balay #undef __FUNCT__ 11784b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESSetFromOptions_LS" 11796849ba73SBarry Smith static PetscErrorCode SNESSetFromOptions_LS(SNES snes) 11805e42470aSBarry Smith { 11814b27c08aSLois Curfman McInnes SNES_LS *ls = (SNES_LS *)snes->data; 1182e5999256SBarry Smith const char *lses[] = {"basic","basicnonorms","quadratic","cubic"}; 1183dfbe8321SBarry Smith PetscErrorCode ierr; 1184a7cc72afSBarry Smith PetscInt indx; 1185ace3abfcSBarry Smith PetscBool flg,set; 11865e42470aSBarry Smith 11873a40ed3dSBarry Smith PetscFunctionBegin; 1188b0a32e0cSBarry Smith ierr = PetscOptionsHead("SNES Line search options");CHKERRQ(ierr); 11894b27c08aSLois Curfman McInnes ierr = PetscOptionsReal("-snes_ls_alpha","Function norm must decrease by","None",ls->alpha,&ls->alpha,0);CHKERRQ(ierr); 11904b27c08aSLois Curfman McInnes ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",ls->maxstep,&ls->maxstep,0);CHKERRQ(ierr); 1191e106eecfSBarry Smith ierr = PetscOptionsReal("-snes_ls_minlambda","Minimum lambda allowed","None",ls->minlambda,&ls->minlambda,0);CHKERRQ(ierr); 1192acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",ls->monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 119394298653SBarry Smith if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);} 1194186905e3SBarry Smith 119517bae607SBarry Smith ierr = PetscOptionsEList("-snes_ls","Line search used","SNESLineSearchSet",lses,4,"cubic",&indx,&flg);CHKERRQ(ierr); 119625cdf11fSBarry Smith if (flg) { 119722e36657SBarry Smith switch (indx) { 1198b49fd9e1SBarry Smith case 0: 119917bae607SBarry Smith ierr = SNESLineSearchSet(snes,SNESLineSearchNo,PETSC_NULL);CHKERRQ(ierr); 1200b49fd9e1SBarry Smith break; 1201b49fd9e1SBarry Smith case 1: 120217bae607SBarry Smith ierr = SNESLineSearchSet(snes,SNESLineSearchNoNorms,PETSC_NULL);CHKERRQ(ierr); 1203b49fd9e1SBarry Smith break; 1204b49fd9e1SBarry Smith case 2: 120517bae607SBarry Smith ierr = SNESLineSearchSet(snes,SNESLineSearchQuadratic,PETSC_NULL);CHKERRQ(ierr); 1206b49fd9e1SBarry Smith break; 1207b49fd9e1SBarry Smith case 3: 120817bae607SBarry Smith ierr = SNESLineSearchSet(snes,SNESLineSearchCubic,PETSC_NULL);CHKERRQ(ierr); 1209b49fd9e1SBarry Smith break; 12105e42470aSBarry Smith } 12115e42470aSBarry Smith } 1212b0a32e0cSBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 12133a40ed3dSBarry Smith PetscFunctionReturn(0); 12145e42470aSBarry Smith } 121504d965bbSLois Curfman McInnes /* -------------------------------------------------------------------------- */ 1216ebe3b25bSBarry Smith /*MC 1217ebe3b25bSBarry Smith SNESLS - Newton based nonlinear solver that uses a line search 121804d965bbSLois Curfman McInnes 1219ebe3b25bSBarry Smith Options Database: 1220ebe3b25bSBarry Smith + -snes_ls [cubic,quadratic,basic,basicnonorms] - Selects line search 1221ebe3b25bSBarry Smith . -snes_ls_alpha <alpha> - Sets alpha 1222e106eecfSBarry Smith . -snes_ls_maxstep <maxstep> - Sets the maximum stepsize the line search will use (if the 2-norm(y) > maxstep then scale y to be y = (maxstep/2-norm(y)) *y) 1223acbee50cSBarry Smith . -snes_ls_minlambda <minlambda> - Sets the minimum lambda the line search will use minlambda / max_i ( y[i]/x[i] ) 1224acbee50cSBarry Smith - -snes_ls_monitor - print information about progress of line searches 1225acbee50cSBarry Smith 122604d965bbSLois Curfman McInnes 1227ebe3b25bSBarry Smith Notes: This is the default nonlinear solver in SNES 122804d965bbSLois Curfman McInnes 1229ee3001cbSBarry Smith Level: beginner 1230ee3001cbSBarry Smith 123117bae607SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESTR, SNESLineSearchSet(), 123217bae607SBarry Smith SNESLineSearchSetPostCheck(), SNESLineSearchNo(), SNESLineSearchCubic(), SNESLineSearchQuadratic(), 1233b3dd4ab5SBarry Smith SNESLineSearchSet(), SNESLineSearchNoNorms(), SNESLineSearchSetPreCheck(), SNESLineSearchSetParams(), SNESLineSearchGetParams() 1234ebe3b25bSBarry Smith 1235ebe3b25bSBarry Smith M*/ 1236fb2e594dSBarry Smith EXTERN_C_BEGIN 12374a2ae208SSatish Balay #undef __FUNCT__ 12384b27c08aSLois Curfman McInnes #define __FUNCT__ "SNESCreate_LS" 12397087cfbeSBarry Smith PetscErrorCode SNESCreate_LS(SNES snes) 12405e42470aSBarry Smith { 1241dfbe8321SBarry Smith PetscErrorCode ierr; 12424b27c08aSLois Curfman McInnes SNES_LS *neP; 12435e42470aSBarry Smith 12443a40ed3dSBarry Smith PetscFunctionBegin; 1245e7788613SBarry Smith snes->ops->setup = SNESSetUp_LS; 1246e7788613SBarry Smith snes->ops->solve = SNESSolve_LS; 1247e7788613SBarry Smith snes->ops->destroy = SNESDestroy_LS; 1248e7788613SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_LS; 1249e7788613SBarry Smith snes->ops->view = SNESView_LS; 12505e42470aSBarry Smith 125138f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNES_LS,&neP);CHKERRQ(ierr); 12525e42470aSBarry Smith snes->data = (void*)neP; 12535e42470aSBarry Smith neP->alpha = 1.e-4; 12545e42470aSBarry Smith neP->maxstep = 1.e8; 1255e106eecfSBarry Smith neP->minlambda = 1.e-12; 125617bae607SBarry Smith neP->LineSearch = SNESLineSearchCubic; 1257c8dd0c0dSLois Curfman McInnes neP->lsP = PETSC_NULL; 12583c632250SBarry Smith neP->postcheckstep = PETSC_NULL; 12593c632250SBarry Smith neP->postcheck = PETSC_NULL; 12603c632250SBarry Smith neP->precheckstep = PETSC_NULL; 12613c632250SBarry Smith neP->precheck = PETSC_NULL; 126282bf6240SBarry Smith 126394298653SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetMonitor_C","SNESLineSearchSetMonitor_LS",SNESLineSearchSetMonitor_LS);CHKERRQ(ierr); 126494298653SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSet_C","SNESLineSearchSet_LS",SNESLineSearchSet_LS);CHKERRQ(ierr); 126594298653SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPostCheck_C","SNESLineSearchSetPostCheck_LS",SNESLineSearchSetPostCheck_LS);CHKERRQ(ierr); 126694298653SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPreCheck_C","SNESLineSearchSetPreCheck_LS",SNESLineSearchSetPreCheck_LS);CHKERRQ(ierr); 126782bf6240SBarry Smith 12683a40ed3dSBarry Smith PetscFunctionReturn(0); 12695e42470aSBarry Smith } 1270fb2e594dSBarry Smith EXTERN_C_END 127182bf6240SBarry Smith 127282bf6240SBarry Smith 127382bf6240SBarry Smith 1274