Actual source code: ksponly.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2:  #include <petsc/private/snesimpl.h>

  4: static PetscErrorCode SNESSolve_KSPONLY(SNES snes)
  5: {
  6:   PetscErrorCode     ierr;
  7:   PetscInt           lits;
  8:   Vec                Y,X,F;

 11:   if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

 13:   snes->numFailures            = 0;
 14:   snes->numLinearSolveFailures = 0;
 15:   snes->reason                 = SNES_CONVERGED_ITERATING;
 16:   snes->iter                   = 0;
 17:   snes->norm                   = 0.0;

 19:   X = snes->vec_sol;
 20:   F = snes->vec_func;
 21:   Y = snes->vec_sol_update;

 23:   SNESComputeFunction(snes,X,F);
 24:   if (snes->numbermonitors) {
 25:     PetscReal fnorm;
 26:     VecNorm(F,NORM_2,&fnorm);
 27:     SNESMonitor(snes,0,fnorm);
 28:   }

 30:   /* Call general purpose update function */
 31:   if (snes->ops->update) {
 32:     (*snes->ops->update)(snes, 0);
 33:   }

 35:   /* Solve J Y = F, where J is Jacobian matrix */
 36:   SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
 37:   KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre);
 38:   KSPSolve(snes->ksp,F,Y);
 39:   snes->reason = SNES_CONVERGED_ITS;
 40:   SNESCheckKSPSolve(snes);

 42:   KSPGetIterationNumber(snes->ksp,&lits);
 43:   PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
 44:   snes->iter++;

 46:   /* Take the computed step. */
 47:   VecAXPY(X,-1.0,Y);
 48:   if (snes->numbermonitors) {
 49:     PetscReal fnorm;
 50:     SNESComputeFunction(snes,X,F);
 51:     VecNorm(F,NORM_2,&fnorm);
 52:     SNESMonitor(snes,1,fnorm);
 53:   }
 54:   return(0);
 55: }

 57: static PetscErrorCode SNESSetUp_KSPONLY(SNES snes)
 58: {

 62:   SNESSetUpMatrices(snes);
 63:   return(0);
 64: }

 66: static PetscErrorCode SNESDestroy_KSPONLY(SNES snes)
 67: {

 70:   return(0);
 71: }

 73: /* -------------------------------------------------------------------------- */
 74: /*MC
 75:       SNESKSPONLY - Nonlinear solver that only performs one Newton step and does not compute any norms.
 76:       The main purpose of this solver is to solve linear problems using the SNES interface, without
 77:       any additional overhead in the form of vector operations.

 79:    Level: beginner

 81: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR
 82: M*/
 83: PETSC_EXTERN PetscErrorCode SNESCreate_KSPONLY(SNES snes)
 84: {

 87:   snes->ops->setup          = SNESSetUp_KSPONLY;
 88:   snes->ops->solve          = SNESSolve_KSPONLY;
 89:   snes->ops->destroy        = SNESDestroy_KSPONLY;
 90:   snes->ops->setfromoptions = 0;
 91:   snes->ops->view           = 0;
 92:   snes->ops->reset          = 0;

 94:   snes->usesksp = PETSC_TRUE;
 95:   snes->usesnpc = PETSC_FALSE;

 97:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

 99:   snes->data = 0;
100:   return(0);
101: }