Date: 2025-03-29T05:58:26+00:00

Actual source code: bcgs.c

      1: #include <../src/ksp/ksp/impls/bcgs/bcgsimpl.h>

      3: PetscErrorCode KSPSetFromOptions_BCGS(KSP ksp, PetscOptionItems PetscOptionsObject)
      4: {
      5:   PetscFunctionBegin;
      6:   PetscOptionsHeadBegin(PetscOptionsObject, "KSP BCGS Options");
      7:   PetscOptionsHeadEnd();
      8:   PetscFunctionReturn(PETSC_SUCCESS);
      9: }

     11: PetscErrorCode KSPSetUp_BCGS(KSP ksp)
     12: {
     13:   PetscFunctionBegin;
     14:   PetscCall(KSPSetWorkVecs(ksp, 6));
     15:   PetscFunctionReturn(PETSC_SUCCESS);
     16: }

     18: PetscErrorCode KSPSolve_BCGS(KSP ksp)
     19: {
     20:   PetscInt    i;
     21:   PetscScalar rho, rhoold, alpha, beta, omega, omegaold, d1;
     22:   Vec         X, B, V, P, R, RP, T, S;
     23:   PetscReal   dp   = 0.0, d2;
     24:   KSP_BCGS   *bcgs = (KSP_BCGS *)ksp->data;

     26:   PetscFunctionBegin;
     27:   X  = ksp->vec_sol;
     28:   B  = ksp->vec_rhs;
     29:   R  = ksp->work[0];
     30:   RP = ksp->work[1];
     31:   V  = ksp->work[2];
     32:   T  = ksp->work[3];
     33:   S  = ksp->work[4];
     34:   P  = ksp->work[5];

     36:   /* Compute initial preconditioned residual */
     37:   PetscCall(KSPInitialResidual(ksp, X, V, T, R, B));

     39:   /* with right preconditioning need to save initial guess to add to final solution */
     40:   if (ksp->pc_side == PC_RIGHT && !ksp->guess_zero) {
     41:     if (!bcgs->guess) PetscCall(VecDuplicate(X, &bcgs->guess));
     42:     PetscCall(VecCopy(X, bcgs->guess));
     43:     PetscCall(VecSet(X, 0.0));
     44:   }

     46:   /* Test for nothing to do */
     47:   if (ksp->normtype != KSP_NORM_NONE) {
     48:     PetscCall(VecNorm(R, NORM_2, &dp));
     49:     KSPCheckNorm(ksp, dp);
     50:   }
     51:   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
     52:   ksp->its   = 0;
     53:   ksp->rnorm = dp;
     54:   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
     55:   PetscCall(KSPLogResidualHistory(ksp, dp));
     56:   PetscCall(KSPMonitor(ksp, 0, dp));
     57:   PetscCall((*ksp->converged)(ksp, 0, dp, &ksp->reason, ksp->cnvP));
     58:   if (ksp->reason) {
     59:     if (bcgs->guess) PetscCall(VecAXPY(X, 1.0, bcgs->guess));
     60:     PetscFunctionReturn(PETSC_SUCCESS);
     61:   }

     63:   /* Make the initial Rp == R */
     64:   PetscCall(VecCopy(R, RP));

     66:   rhoold   = 1.0;
     67:   alpha    = 1.0;
     68:   omegaold = 1.0;
     69:   PetscCall(VecSet(P, 0.0));
     70:   PetscCall(VecSet(V, 0.0));

     72:   i = 0;
     73:   do {
     74:     PetscCall(VecDot(R, RP, &rho)); /*   rho <- (r,rp)      */
     75:     beta = (rho / rhoold) * (alpha / omegaold);
     76:     PetscCall(VecAXPBYPCZ(P, 1.0, -omegaold * beta, beta, R, V)); /* p <- r - omega * beta* v + beta * p */
     77:     PetscCall(KSP_PCApplyBAorAB(ksp, P, V, T));                   /*   v <- K p           */
     78:     PetscCall(VecDot(V, RP, &d1));
     79:     KSPCheckDot(ksp, d1);
     80:     if (d1 == 0.0) {
     81:       PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "KSPSolve breakdown due to zero inner product");
     82:       ksp->reason = KSP_DIVERGED_BREAKDOWN;
     83:       PetscCall(PetscInfo(ksp, "Breakdown due to zero inner product\n"));
     84:       break;
     85:     }
     86:     alpha = rho / d1;                           /*   a <- rho / (v,rp)  */
     87:     PetscCall(VecWAXPY(S, -alpha, V, R));       /*   s <- r - a v       */
     88:     PetscCall(KSP_PCApplyBAorAB(ksp, S, T, R)); /*   t <- K s    */
     89:     PetscCall(VecDotNorm2(S, T, &d1, &d2));
     90:     if (d2 == 0.0) {
     91:       /* t is 0.  if s is 0, then alpha v == r, and hence alpha p
     92:          may be our solution.  Give it a try? */
     93:       PetscCall(VecDot(S, S, &d1));
     94:       if (d1 != 0.0) {
     95:         PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "KSPSolve has failed due to singular preconditioned operator");
     96:         ksp->reason = KSP_DIVERGED_BREAKDOWN;
     97:         PetscCall(PetscInfo(ksp, "Failed due to singular preconditioned operator\n"));
     98:         break;
     99:       }
    100:       PetscCall(VecAXPY(X, alpha, P)); /*   x <- x + a p       */
    101:       PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
    102:       ksp->its++;
    103:       ksp->rnorm  = 0.0;
    104:       ksp->reason = KSP_CONVERGED_RTOL;
    105:       PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
    106:       PetscCall(KSPLogResidualHistory(ksp, dp));
    107:       PetscCall(KSPMonitor(ksp, i + 1, 0.0));
    108:       break;
    109:     }
    110:     omega = d1 / d2;                                    /*   w <- (t's) / (t't) */
    111:     PetscCall(VecAXPBYPCZ(X, alpha, omega, 1.0, P, S)); /* x <- alpha * p + omega * s + x */
    112:     PetscCall(VecWAXPY(R, -omega, T, S));               /*   r <- s - w t       */
    113:     if (ksp->normtype != KSP_NORM_NONE && ksp->chknorm < i + 2) {
    114:       PetscCall(VecNorm(R, NORM_2, &dp));
    115:       KSPCheckNorm(ksp, dp);
    116:     }

    118:     rhoold   = rho;
    119:     omegaold = omega;

    121:     PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
    122:     ksp->its++;
    123:     ksp->rnorm = dp;
    124:     PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
    125:     PetscCall(KSPLogResidualHistory(ksp, dp));
    126:     PetscCall(KSPMonitor(ksp, i + 1, dp));
    127:     PetscCall((*ksp->converged)(ksp, i + 1, dp, &ksp->reason, ksp->cnvP));
    128:     if (ksp->reason) break;
    129:     if (rho == 0.0) {
    130:       PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "KSPSolve breakdown due to zero inner product");
    131:       ksp->reason = KSP_DIVERGED_BREAKDOWN;
    132:       PetscCall(PetscInfo(ksp, "Breakdown due to zero rho inner product\n"));
    133:       break;
    134:     }
    135:     i++;
    136:   } while (i < ksp->max_it);

    138:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;

    140:   PetscCall(KSPUnwindPreconditioner(ksp, X, T));
    141:   if (bcgs->guess) PetscCall(VecAXPY(X, 1.0, bcgs->guess));
    142:   PetscFunctionReturn(PETSC_SUCCESS);
    143: }

    145: PetscErrorCode KSPBuildSolution_BCGS(KSP ksp, Vec v, Vec *V)
    146: {
    147:   KSP_BCGS *bcgs = (KSP_BCGS *)ksp->data;

    149:   PetscFunctionBegin;
    150:   if (ksp->pc_side == PC_RIGHT) {
    151:     if (v) {
    152:       PetscCall(KSP_PCApply(ksp, ksp->vec_sol, v));
    153:       if (bcgs->guess) PetscCall(VecAXPY(v, 1.0, bcgs->guess));
    154:       *V = v;
    155:     } else SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Not working with right preconditioner");
    156:   } else {
    157:     if (v) {
    158:       PetscCall(VecCopy(ksp->vec_sol, v));
    159:       *V = v;
    160:     } else *V = ksp->vec_sol;
    161:   }
    162:   PetscFunctionReturn(PETSC_SUCCESS);
    163: }

    165: PetscErrorCode KSPReset_BCGS(KSP ksp)
    166: {
    167:   KSP_BCGS *cg = (KSP_BCGS *)ksp->data;

    169:   PetscFunctionBegin;
    170:   PetscCall(VecDestroy(&cg->guess));
    171:   PetscFunctionReturn(PETSC_SUCCESS);
    172: }

    174: PetscErrorCode KSPDestroy_BCGS(KSP ksp)
    175: {
    176:   PetscFunctionBegin;
    177:   PetscCall(KSPReset_BCGS(ksp));
    178:   PetscCall(KSPDestroyDefault(ksp));
    179:   PetscFunctionReturn(PETSC_SUCCESS);
    180: }

    182: /*MC
    183:    KSPBCGS - Implements the BiCGStab (Stabilized version of Biconjugate Gradient) method {cite}`vorst92`

    185:    Level: beginner

    187:    Notes:
    188:    Supports left and right preconditioning but not symmetric

    190:    See `KSPBCGSL` for additional stabilization

    192:    See `KSPFBCGS`, `KSPFBCGSR`, and `KSPPIPEBCGS` for flexible and pipelined versions of the algorithm

    194:    This method can be a good alternative to `KSPGMRES` in that it does not require explicitly storing the Krylov space as `KSPGMRES` requires
    195:    and there is no acceptable restart value that can be set with `KSPGMRESSetRestart()` that balances cost per iteration and convergence rates with `KSPGMRES`.

    197: .seealso: [](ch_ksp), `KSPFBCGS`, `KSPFBCGSR`, `KSPPIPEBCGS`, `KSPBCGSL`, `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPBICG`, `KSPBCGSL`, `KSPFBICG`, `KSPQMRCGS`, `KSPSetPCSide()`
    198: M*/
    199: PETSC_EXTERN PetscErrorCode KSPCreate_BCGS(KSP ksp)
    200: {
    201:   KSP_BCGS *bcgs;

    203:   PetscFunctionBegin;
    204:   PetscCall(PetscNew(&bcgs));

    206:   ksp->data                = bcgs;
    207:   ksp->ops->setup          = KSPSetUp_BCGS;
    208:   ksp->ops->solve          = KSPSolve_BCGS;
    209:   ksp->ops->destroy        = KSPDestroy_BCGS;
    210:   ksp->ops->reset          = KSPReset_BCGS;
    211:   ksp->ops->buildsolution  = KSPBuildSolution_BCGS;
    212:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
    213:   ksp->ops->setfromoptions = KSPSetFromOptions_BCGS;

    215:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 3));
    216:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 2));
    217:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_LEFT, 1));
    218:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1));
    219:   PetscFunctionReturn(PETSC_SUCCESS);
    220: }