Actual source code: preonly.c

  1: #include <petsc/private/kspimpl.h>

  3: static PetscErrorCode KSPSetUp_PREONLY(KSP ksp)
  4: {
  5:   PetscFunctionBegin;
  6:   PetscFunctionReturn(PETSC_SUCCESS);
  7: }

  9: static PetscErrorCode KSPSolve_PREONLY(KSP ksp)
 10: {
 11:   PetscBool      diagonalscale;
 12:   PCFailedReason pcreason;

 14:   PetscFunctionBegin;
 15:   PetscCall(PCGetDiagonalScale(ksp->pc, &diagonalscale));
 16:   PetscCheck(!diagonalscale, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Krylov method %s does not support diagonal scaling", ((PetscObject)ksp)->type_name);
 17:   if (!ksp->guess_zero) {
 18:     PetscBool red;
 19:     PetscCall(PetscObjectTypeCompare((PetscObject)ksp->pc, PCREDISTRIBUTE, &red));
 20:     PetscCheck(red, PetscObjectComm((PetscObject)ksp), PETSC_ERR_USER, "KSP of type preonly (application of preconditioner only) doesn't make sense with nonzero initial guess you probably want a KSP of type Richardson");
 21:   }
 22:   ksp->its = 0;
 23:   PetscCall(KSP_PCApply(ksp, ksp->vec_rhs, ksp->vec_sol));

 25:   PetscCall(PCReduceFailedReason(ksp->pc));
 26:   PetscCall(PCGetFailedReason(ksp->pc, &pcreason));
 27:   if (pcreason) {
 28:     PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "KSPSolve has not converged with PCFailedReason %s", PCFailedReasons[pcreason]);
 29:     PetscCall(VecSetInf(ksp->vec_sol));
 30:     ksp->reason = KSP_DIVERGED_PC_FAILED;
 31:   } else {
 32:     ksp->its    = 1;
 33:     ksp->reason = KSP_CONVERGED_ITS;
 34:   }

 36:   if (ksp->numbermonitors) {
 37:     Vec       v;
 38:     PetscReal norm;
 39:     Mat       A;

 41:     PetscCall(VecNorm(ksp->vec_rhs, NORM_2, &norm));
 42:     PetscCall(KSPMonitor(ksp, 0, norm));
 43:     PetscCall(VecDuplicate(ksp->vec_rhs, &v));
 44:     PetscCall(PCGetOperators(ksp->pc, &A, NULL));
 45:     PetscCall(KSP_MatMult(ksp, A, ksp->vec_sol, v));
 46:     PetscCall(VecAYPX(v, -1.0, ksp->vec_rhs));
 47:     PetscCall(VecNorm(v, NORM_2, &norm));
 48:     PetscCall(VecDestroy(&v));
 49:     PetscCall(KSPMonitor(ksp, 1, norm));
 50:   }
 51:   PetscFunctionReturn(PETSC_SUCCESS);
 52: }

 54: static PetscErrorCode KSPMatSolve_PREONLY(KSP ksp, Mat B, Mat X)
 55: {
 56:   PetscBool      diagonalscale;
 57:   PCFailedReason pcreason;

 59:   PetscFunctionBegin;
 60:   PetscCall(PCGetDiagonalScale(ksp->pc, &diagonalscale));
 61:   PetscCheck(!diagonalscale, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Krylov method %s does not support diagonal scaling", ((PetscObject)ksp)->type_name);
 62:   PetscCheck(ksp->guess_zero, PetscObjectComm((PetscObject)ksp), PETSC_ERR_USER, "Running KSP of preonly doesn't make sense with nonzero initial guess you probably want a KSP type of Richardson");
 63:   ksp->its = 0;
 64:   PetscCall(KSP_PCMatApply(ksp, B, X));
 65:   PetscCall(PCGetFailedReason(ksp->pc, &pcreason));
 66:   /* Note: only some ranks may have this set; this may lead to problems if the caller assumes ksp->reason is set on all processes or just uses the result */
 67:   if (pcreason) {
 68:     PetscCall(MatSetInf(X));
 69:     ksp->reason = KSP_DIVERGED_PC_FAILED;
 70:   } else {
 71:     ksp->its    = 1;
 72:     ksp->reason = KSP_CONVERGED_ITS;
 73:   }
 74:   PetscFunctionReturn(PETSC_SUCCESS);
 75: }

 77: /*MC
 78:    KSPNONE - An alias for `KSPPREONLY`

 80:    Options Database Key:
 81: .   -ksp_type none - use a single application of the preconditioner only

 83:    Level: beginner

 85:    Note:
 86:    See `KSPPREONLY` for more details

 88: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSPPREONLY`, `KSP`, `KSPRICHARDSON`, `KSPCHEBYSHEV`, `KSPGetPC()`, `KSPSetInitialGuessNonzero()`,
 89:           `PCREDISTRIBUTE`, `PCRedistributeGetKSP()`, `KSPPREONLY`
 90: M*/

 92: /*MC
 93:    KSPPREONLY - This implements a method that applies ONLY the preconditioner exactly once.

 95:    This may be used in inner iterations, where it is desired to
 96:    allow multiple iterations as well as the "0-iteration" case. It is
 97:    commonly used with the direct solver preconditioners like `PCLU` and `PCCHOLESKY`.
 98:    There is an alias of this with the name `KSPNONE`.

100:    Options Database Key:
101: .   -ksp_type preonly - use a single application of the preconditioner only

103:    Level: beginner

105:    Notes:
106:    Since this does not involve an iteration the basic `KSP` parameters such as tolerances and iteration counts
107:    do not apply

109:    To apply multiple preconditioners in a simple iteration use `KSPRICHARDSON`

111:    This `KSPType` cannot be used with the flag `-ksp_initial_guess_nonzero` or the call `KSPSetInitialGuessNonzero()` since it simply applies
112:    the preconditioner to the given right-hand side during `KSPSolve()`. Except when the
113:    `PCType` is `PCREDISTRIBUTE`; in that situation pass the nonzero initial guess flag with `-ksp_initial_guess_nonzero` or `KSPSetInitialGuessNonzero()`
114:    both to the outer `KSP` (which is `KSPPREONLY`) and the inner `KSP` object obtained with `KSPGetPC()` followed by `PCRedistributedGetKSP()`
115:    followed by `KSPSetInitialGuessNonzero()` or the option  `-redistribute_ksp_initial_guess_nonzero`.

117:    Developer Note:
118:    Even though this method does not use any norms, the user is allowed to set the `KSPNormType` to any value.
119:    This is so the users does not have to change `KSPNormType` options when they switch from other `KSP` methods to this one.

121: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPRICHARDSON`, `KSPCHEBYSHEV`, `KSPGetPC()`, `KSPSetInitialGuessNonzero()`,
122:           `PCREDISTRIBUTE`, `PCRedistributeGetKSP()`, `KSPNONE`
123: M*/

125: PETSC_EXTERN PetscErrorCode KSPCreate_PREONLY(KSP ksp)
126: {
127:   PetscFunctionBegin;
128:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_LEFT, 3));
129:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 2));
130:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 2));
131:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_RIGHT, 2));
132:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_LEFT, 2));
133:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 2));
134:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NATURAL, PC_LEFT, 2));

136:   ksp->data                = NULL;
137:   ksp->ops->setup          = KSPSetUp_PREONLY;
138:   ksp->ops->solve          = KSPSolve_PREONLY;
139:   ksp->ops->matsolve       = KSPMatSolve_PREONLY;
140:   ksp->ops->destroy        = KSPDestroyDefault;
141:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
142:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
143:   ksp->ops->setfromoptions = NULL;
144:   ksp->ops->view           = NULL;
145:   ksp->guess_not_read      = PETSC_TRUE; // A KSP of preonly never needs to zero the input x since PC do not use an initial guess
146:   PetscFunctionReturn(PETSC_SUCCESS);
147: }