Actual source code: lcd.c

petsc-master 2020-09-19
Report Typos and Errors

  2: #include <../src/ksp/ksp/impls/lcd/lcdimpl.h>

  4: PetscErrorCode KSPSetUp_LCD(KSP ksp)
  5: {
  6:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;
  8:   PetscInt       restart = lcd->restart;

 11:   /* get work vectors needed by LCD */
 12:   KSPSetWorkVecs(ksp,2);

 14:   VecDuplicateVecs(ksp->work[0],restart+1,&lcd->P);
 15:   VecDuplicateVecs(ksp->work[0], restart + 1, &lcd->Q);
 16:   PetscLogObjectMemory((PetscObject)ksp,2*(restart+2)*sizeof(Vec));
 17:   return(0);
 18: }

 20: /*     KSPSolve_LCD - This routine actually applies the left conjugate
 21:     direction method

 23:    Input Parameter:
 24: .     ksp - the Krylov space object that was set to use LCD, by, for
 25:             example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPLCD);

 27:    Output Parameter:
 28: .     its - number of iterations used

 30: */
 31: PetscErrorCode  KSPSolve_LCD(KSP ksp)
 32: {
 34:   PetscInt       it,j,max_k;
 35:   PetscScalar    alfa, beta, num, den, mone;
 36:   PetscReal      rnorm = 0.0;
 37:   Vec            X,B,R,Z;
 38:   KSP_LCD        *lcd;
 39:   Mat            Amat,Pmat;
 40:   PetscBool      diagonalscale;

 43:   PCGetDiagonalScale(ksp->pc,&diagonalscale);
 44:   if (diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);

 46:   lcd   = (KSP_LCD*)ksp->data;
 47:   X     = ksp->vec_sol;
 48:   B     = ksp->vec_rhs;
 49:   R     = ksp->work[0];
 50:   Z     = ksp->work[1];
 51:   max_k = lcd->restart;
 52:   mone  = -1;

 54:   PCGetOperators(ksp->pc,&Amat,&Pmat);

 56:   ksp->its = 0;
 57:   if (!ksp->guess_zero) {
 58:     KSP_MatMult(ksp,Amat,X,Z);             /*   z <- b - Ax       */
 59:     VecAYPX(Z,mone,B);
 60:   } else {
 61:     VecCopy(B,Z);                         /*     z <- b (x is 0) */
 62:   }

 64:   KSP_PCApply(ksp,Z,R);                   /*     r <- M^-1z         */
 65:   if (ksp->normtype != KSP_NORM_NONE) {
 66:     VecNorm(R,NORM_2,&rnorm);
 67:     KSPCheckNorm(ksp,rnorm);
 68:   }
 69:   KSPLogResidualHistory(ksp,rnorm);
 70:   KSPMonitor(ksp,0,rnorm);
 71:   ksp->rnorm = rnorm;

 73:   /* test for convergence */
 74:   (*ksp->converged)(ksp,0,rnorm,&ksp->reason,ksp->cnvP);
 75:   if (ksp->reason) return(0);

 77:   VecCopy(R,lcd->P[0]);

 79:   while (!ksp->reason && ksp->its < ksp->max_it) {
 80:     it   = 0;
 81:     KSP_MatMult(ksp,Amat,lcd->P[it],Z);
 82:     KSP_PCApply(ksp,Z,lcd->Q[it]);

 84:     while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
 85:       ksp->its++;
 86:       VecDot(lcd->P[it],R,&num);
 87:       VecDot(lcd->P[it],lcd->Q[it], &den);
 88:       KSPCheckDot(ksp,den);
 89:       alfa = num/den;
 90:       VecAXPY(X,alfa,lcd->P[it]);
 91:       VecAXPY(R,-alfa,lcd->Q[it]);
 92:       if (ksp->normtype != KSP_NORM_NONE) {
 93:         VecNorm(R,NORM_2,&rnorm);
 94:         KSPCheckNorm(ksp,rnorm);
 95:       }

 97:       ksp->rnorm = rnorm;
 98:       KSPLogResidualHistory(ksp,rnorm);
 99:       KSPMonitor(ksp,ksp->its,rnorm);
100:       (*ksp->converged)(ksp,ksp->its,rnorm,&ksp->reason,ksp->cnvP);

102:       if (ksp->reason) break;

104:       VecCopy(R,lcd->P[it+1]);
105:       KSP_MatMult(ksp,Amat,lcd->P[it+1],Z);
106:       KSP_PCApply(ksp,Z,lcd->Q[it+1]);

108:       for (j = 0; j <= it; j++) {
109:         VecDot(lcd->P[j],lcd->Q[it+1],&num);
110:         KSPCheckDot(ksp,num);
111:         VecDot(lcd->P[j],lcd->Q[j],&den);
112:         beta = -num/den;
113:         VecAXPY(lcd->P[it+1],beta,lcd->P[j]);
114:         VecAXPY(lcd->Q[it+1],beta,lcd->Q[j]);
115:       }
116:       it++;
117:     }
118:     VecCopy(lcd->P[it],lcd->P[0]);
119:   }
120:   if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
121:   VecCopy(X,ksp->vec_sol);
122:   return(0);
123: }
124: /*
125:        KSPDestroy_LCD - Frees all memory space used by the Krylov method

127: */
128: PetscErrorCode KSPReset_LCD(KSP ksp)
129: {
130:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;

134:   if (lcd->P) { VecDestroyVecs(lcd->restart+1,&lcd->P);}
135:   if (lcd->Q) { VecDestroyVecs(lcd->restart+1,&lcd->Q);}
136:   return(0);
137: }


140: PetscErrorCode KSPDestroy_LCD(KSP ksp)
141: {

145:   KSPReset_LCD(ksp);
146:   PetscFree(ksp->data);
147:   return(0);
148: }

150: /*
151:      KSPView_LCD - Prints information about the current Krylov method being used

153:       Currently this only prints information to a file (or stdout) about the
154:       symmetry of the problem. If your Krylov method has special options or
155:       flags that information should be printed here.

157: */
158: PetscErrorCode KSPView_LCD(KSP ksp,PetscViewer viewer)
159: {

161:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;
163:   PetscBool      iascii;

166:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
167:   if (iascii) {
168:     PetscViewerASCIIPrintf(viewer,"  restart=%d\n",lcd->restart);
169:     PetscViewerASCIIPrintf(viewer,"  happy breakdown tolerance %g\n",lcd->haptol);
170:   }
171:   return(0);
172: }

174: /*
175:     KSPSetFromOptions_LCD - Checks the options database for options related to the
176:                             LCD method.
177: */
178: PetscErrorCode KSPSetFromOptions_LCD(PetscOptionItems *PetscOptionsObject,KSP ksp)
179: {
181:   PetscBool      flg;
182:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;

185:   PetscOptionsHead(PetscOptionsObject,"KSP LCD options");
186:   PetscOptionsInt("-ksp_lcd_restart","Number of vectors conjugate","KSPLCDSetRestart",lcd->restart,&lcd->restart,&flg);
187:   if (flg && lcd->restart < 1) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
188:   PetscOptionsReal("-ksp_lcd_haptol","Tolerance for exact convergence (happy ending)","KSPLCDSetHapTol",lcd->haptol,&lcd->haptol,&flg);
189:   if (flg && lcd->haptol < 0.0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
190:   return(0);
191: }

193: /*MC
194:      KSPLCD -  Implements the LCD (left conjugate direction) method in PETSc.

196:    Options Database Keys:
197: +   -ksp_lcd_restart - number of vectors conjudate
198: -   -ksp_lcd_haptol - tolerance for exact convergence (happing ending)

200:    Level: beginner

202:     Notes:
203:     Support only for left preconditioning

205:     References:
206: +    1. - J.Y. Yuan, G.H.Golub, R.J. Plemmons, and W.A.G. Cecilio. Semiconjugate
207:      direction methods for real positive definite system. BIT Numerical
208:      Mathematics, 44(1),2004.
209: .    2. - Y. Dai and J.Y. Yuan. Study on semiconjugate direction methods for
210:      nonsymmetric systems. International Journal for Numerical Methods in
211:      Engineering, 60, 2004.
212: .    3. - L. Catabriga, A.L.G.A. Coutinho, and L.P.Franca. Evaluating the LCD
213:      algorithm for solving linear systems of equations arising from implicit
214:      SUPG formulation of compressible flows. International Journal for
215:      Numerical Methods in Engineering, 60, 2004
216: -    4. - L. Catabriga, A. M. P. Valli, B. Z. Melotti, L. M. Pessoa,
217:      A. L. G. A. Coutinho, Performance of LCD iterative method in the finite
218:      element and finite difference solution of convection diffusion
219:      equations,  Communications in Numerical Methods in Engineering, (Early
220:      View).

222:   Contributed by: Lucia Catabriga <luciac@ices.utexas.edu>


225: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
226:            KSPCGSetType(), KSPLCDSetRestart(), KSPLCDSetHapTol()

228: M*/

230: PETSC_EXTERN PetscErrorCode KSPCreate_LCD(KSP ksp)
231: {
233:   KSP_LCD        *lcd;

236:   PetscNewLog(ksp,&lcd);
237:   ksp->data    = (void*)lcd;
238:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);
239:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
240:   lcd->restart = 30;
241:   lcd->haptol  = 1.0e-30;

243:   /*
244:        Sets the functions that are associated with this data structure
245:        (in C++ this is the same as defining virtual functions)
246:   */
247:   ksp->ops->setup          = KSPSetUp_LCD;
248:   ksp->ops->solve          = KSPSolve_LCD;
249:   ksp->ops->reset          = KSPReset_LCD;
250:   ksp->ops->destroy        = KSPDestroy_LCD;
251:   ksp->ops->view           = KSPView_LCD;
252:   ksp->ops->setfromoptions = KSPSetFromOptions_LCD;
253:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
254:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
255:   return(0);
256: }