Actual source code: itcreate.c

petsc-master 2015-05-24
Report Typos and Errors
  2: /*
  3:      The basic KSP routines, Create, View etc. are here.
  4: */
  5: #include <petsc/private/kspimpl.h>      /*I "petscksp.h" I*/

  7: /* Logging support */
  8: PetscClassId  KSP_CLASSID;
  9: PetscClassId  DMKSP_CLASSID;
 10: PetscLogEvent KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve;

 12: /*
 13:    Contains the list of registered KSP routines
 14: */
 15: PetscFunctionList KSPList              = 0;
 16: PetscBool         KSPRegisterAllCalled = PETSC_FALSE;

 20: /*@C
 21:   KSPLoad - Loads a KSP that has been stored in binary  with KSPView().

 23:   Collective on PetscViewer

 25:   Input Parameters:
 26: + newdm - the newly loaded KSP, this needs to have been created with KSPCreate() or
 27:            some related function before a call to KSPLoad().
 28: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()

 30:    Level: intermediate

 32:   Notes:
 33:    The type is determined by the data in the file, any type set into the KSP before this call is ignored.

 35:   Notes for advanced users:
 36:   Most users should not need to know the details of the binary storage
 37:   format, since KSPLoad() and KSPView() completely hide these details.
 38:   But for anyone who's interested, the standard binary matrix storage
 39:   format is
 40: .vb
 41:      has not yet been determined
 42: .ve

 44: .seealso: PetscViewerBinaryOpen(), KSPView(), MatLoad(), VecLoad()
 45: @*/
 46: PetscErrorCode  KSPLoad(KSP newdm, PetscViewer viewer)
 47: {
 49:   PetscBool      isbinary;
 50:   PetscInt       classid;
 51:   char           type[256];
 52:   PC             pc;

 57:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
 58:   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

 60:   PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);
 61:   if (classid != KSP_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not KSP next in file");
 62:   PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);
 63:   KSPSetType(newdm, type);
 64:   if (newdm->ops->load) {
 65:     (*newdm->ops->load)(newdm,viewer);
 66:   }
 67:   KSPGetPC(newdm,&pc);
 68:   PCLoad(pc,viewer);
 69:   return(0);
 70: }

 72: #include <petscdraw.h>
 73: #if defined(PETSC_HAVE_SAWS)
 74: #include <petscviewersaws.h>
 75: #endif
 78: /*@C
 79:    KSPView - Prints the KSP data structure.

 81:    Collective on KSP

 83:    Input Parameters:
 84: +  ksp - the Krylov space context
 85: -  viewer - visualization context

 87:    Options Database Keys:
 88: .  -ksp_view - print the ksp data structure at the end of a KSPSolve call

 90:    Note:
 91:    The available visualization contexts include
 92: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
 93: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
 94:          output where only the first processor opens
 95:          the file.  All other processors send their
 96:          data to the first processor to print.

 98:    The user can open an alternative visualization context with
 99:    PetscViewerASCIIOpen() - output to a specified file.

101:    Level: beginner

103: .keywords: KSP, view

105: .seealso: PCView(), PetscViewerASCIIOpen()
106: @*/
107: PetscErrorCode  KSPView(KSP ksp,PetscViewer viewer)
108: {
110:   PetscBool      iascii,isbinary,isdraw;
111: #if defined(PETSC_HAVE_SAWS)
112:   PetscBool      issaws;
113: #endif

117:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp));

121:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
122:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
123:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
124: #if defined(PETSC_HAVE_SAWS)
125:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
126: #endif
127:   if (iascii) {
128:     PetscObjectPrintClassNamePrefixType((PetscObject)ksp,viewer);
129:     if (ksp->ops->view) {
130:       PetscViewerASCIIPushTab(viewer);
131:       (*ksp->ops->view)(ksp,viewer);
132:       PetscViewerASCIIPopTab(viewer);
133:     }
134:     if (ksp->guess_zero) {
135:       PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, initial guess is zero\n",ksp->max_it);
136:     } else {
137:       PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D\n", ksp->max_it);
138:     }
139:     if (ksp->guess_knoll) {PetscViewerASCIIPrintf(viewer,"  using preconditioner applied to right hand side for initial guess\n");}
140:     PetscViewerASCIIPrintf(viewer,"  tolerances:  relative=%g, absolute=%g, divergence=%g\n",(double)ksp->rtol,(double)ksp->abstol,(double)ksp->divtol);
141:     if (ksp->pc_side == PC_RIGHT) {
142:       PetscViewerASCIIPrintf(viewer,"  right preconditioning\n");
143:     } else if (ksp->pc_side == PC_SYMMETRIC) {
144:       PetscViewerASCIIPrintf(viewer,"  symmetric preconditioning\n");
145:     } else {
146:       PetscViewerASCIIPrintf(viewer,"  left preconditioning\n");
147:     }
148:     if (ksp->guess) {PetscViewerASCIIPrintf(viewer,"  using Fischers initial guess method %D with size %D\n",ksp->guess->method,ksp->guess->maxl);}
149:     if (ksp->dscale) {PetscViewerASCIIPrintf(viewer,"  diagonally scaled system\n");}
150:     if (!ksp->guess_zero) {PetscViewerASCIIPrintf(viewer,"  using nonzero initial guess\n");}
151:     PetscViewerASCIIPrintf(viewer,"  using %s norm type for convergence test\n",KSPNormTypes[ksp->normtype]);
152:   } else if (isbinary) {
153:     PetscInt    classid = KSP_FILE_CLASSID;
154:     MPI_Comm    comm;
155:     PetscMPIInt rank;
156:     char        type[256];

158:     PetscObjectGetComm((PetscObject)ksp,&comm);
159:     MPI_Comm_rank(comm,&rank);
160:     if (!rank) {
161:       PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
162:       PetscStrncpy(type,((PetscObject)ksp)->type_name,256);
163:       PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);
164:     }
165:     if (ksp->ops->view) {
166:       (*ksp->ops->view)(ksp,viewer);
167:     }
168:   } else if (isdraw) {
169:     PetscDraw draw;
170:     char      str[36];
171:     PetscReal x,y,bottom,h;
172:     PetscBool flg;

174:     PetscViewerDrawGetDraw(viewer,0,&draw);
175:     PetscDrawGetCurrentPoint(draw,&x,&y);
176:     PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);
177:     if (!flg) {
178:       PetscStrcpy(str,"KSP: ");
179:       PetscStrcat(str,((PetscObject)ksp)->type_name);
180:       PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
181:       bottom = y - h;
182:     } else {
183:       bottom = y;
184:     }
185:     PetscDrawPushCurrentPoint(draw,x,bottom);
186: #if defined(PETSC_HAVE_SAWS)
187:   } else if (issaws) {
188:     PetscMPIInt rank;
189:     const char  *name;

191:     PetscObjectGetName((PetscObject)ksp,&name);
192:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
193:     if (!((PetscObject)ksp)->amsmem && !rank) {
194:       char       dir[1024];

196:       PetscObjectViewSAWs((PetscObject)ksp,viewer);
197:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/its",name);
198:       PetscStackCallSAWs(SAWs_Register,(dir,&ksp->its,1,SAWs_READ,SAWs_INT));
199:       if (!ksp->res_hist) {
200:         KSPSetResidualHistory(ksp,NULL,PETSC_DECIDE,PETSC_TRUE);
201:       }
202:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/res_hist",name);
203:       PetscStackCallSAWs(SAWs_Register,(dir,ksp->res_hist,10,SAWs_READ,SAWs_DOUBLE));
204:     }
205: #endif
206:   } else if (ksp->ops->view) {
207:     (*ksp->ops->view)(ksp,viewer);
208:   }
209:   if (!ksp->skippcsetfromoptions) {
210:     if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
211:     PCView(ksp->pc,viewer);
212:   }
213:   if (isdraw) {
214:     PetscDraw draw;
215:     PetscViewerDrawGetDraw(viewer,0,&draw);
216:     PetscDrawPopCurrentPoint(draw);
217:   }
218:   return(0);
219: }


224: /*@
225:    KSPSetNormType - Sets the norm that is used for convergence testing.

227:    Logically Collective on KSP

229:    Input Parameter:
230: +  ksp - Krylov solver context
231: -  normtype - one of
232: $   KSP_NORM_NONE - skips computing the norm, this should only be used if you are using
233: $                 the Krylov method as a smoother with a fixed small number of iterations.
234: $                 Implicitly sets KSPConvergedSkip as KSP convergence test.
235: $   KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm
236: $                 of the preconditioned residual P^{-1}(b - A x)
237: $   KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual.
238: $   KSP_NORM_NATURAL - supported  by KSPCG, KSPCR, KSPCGNE, KSPCGS


241:    Options Database Key:
242: .   -ksp_norm_type <none,preconditioned,unpreconditioned,natural>

244:    Notes:
245:    Not all combinations of preconditioner side (see KSPSetPCSide()) and norm type are supported by all Krylov methods.
246:    If only one is set, PETSc tries to automatically change the other to find a compatible pair.  If no such combination
247:    is supported, PETSc will generate an error.

249:    Developer Notes:
250:    Supported combinations of norm and preconditioner side are set using KSPSetSupportedNorm().


253:    Level: advanced

255: .keywords: KSP, create, context, norms

257: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetCheckNormIteration(), KSPSetPCSide(), KSPGetPCSide()
258: @*/
259: PetscErrorCode  KSPSetNormType(KSP ksp,KSPNormType normtype)
260: {

266:   ksp->normtype = ksp->normtype_set = normtype;
267:   if (normtype == KSP_NORM_NONE) {
268:     KSPSetConvergenceTest(ksp,KSPConvergedSkip,0,0);
269:     PetscInfo(ksp,"Warning: setting KSPNormType to skip computing the norm\n\
270:  KSP convergence test is implicitly set to KSPConvergedSkip\n");
271:   }
272:   return(0);
273: }

277: /*@
278:    KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be
279:      computed and used in the convergence test.

281:    Logically Collective on KSP

283:    Input Parameter:
284: +  ksp - Krylov solver context
285: -  it  - use -1 to check at all iterations

287:    Notes:
288:    Currently only works with KSPCG, KSPBCGS and KSPIBCGS

290:    Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm

292:    On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example,
293:     -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged).
294:    Level: advanced

296: .keywords: KSP, create, context, norms

298: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType()
299: @*/
300: PetscErrorCode  KSPSetCheckNormIteration(KSP ksp,PetscInt it)
301: {
305:   ksp->chknorm = it;
306:   return(0);
307: }

311: /*@
312:    KSPSetLagNorm - Lags the residual norm calculation so that it is computed as part of the MPI_Allreduce() for
313:    computing the inner products for the next iteration.  This can reduce communication costs at the expense of doing
314:    one additional iteration.


317:    Logically Collective on KSP

319:    Input Parameter:
320: +  ksp - Krylov solver context
321: -  flg - PETSC_TRUE or PETSC_FALSE

323:    Options Database Keys:
324: .  -ksp_lag_norm - lag the calculated residual norm

326:    Notes:
327:    Currently only works with KSPIBCGS.

329:    Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm

331:    If you lag the norm and run with, for example, -ksp_monitor, the residual norm reported will be the lagged one.
332:    Level: advanced

334: .keywords: KSP, create, context, norms

336: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType(), KSPSetCheckNormIteration()
337: @*/
338: PetscErrorCode  KSPSetLagNorm(KSP ksp,PetscBool flg)
339: {
343:   ksp->lagnorm = flg;
344:   return(0);
345: }

349: /*@
350:    KSPSetSupportedNorm - Sets a norm and preconditioner side supported by a KSP

352:    Logically Collective

354:    Input Arguments:
355: +  ksp - Krylov method
356: .  normtype - supported norm type
357: .  pcside - preconditioner side that can be used with this norm
358: -  preference - integer preference for this combination, larger values have higher priority

360:    Level: developer

362:    Notes:
363:    This function should be called from the implementation files KSPCreate_XXX() to declare
364:    which norms and preconditioner sides are supported. Users should not need to call this
365:    function.

367:    KSP_NORM_NONE is supported by default with all KSP methods and any PC side at priority 1.  If a KSP explicitly does
368:    not support KSP_NORM_NONE, it should set this by setting priority=0.  Since defaulting to KSP_NORM_NONE is usually
369:    undesirable, more desirable norms should usually have priority 2 or higher.

371: .seealso: KSPSetNormType(), KSPSetPCSide()
372: @*/
373: PetscErrorCode KSPSetSupportedNorm(KSP ksp,KSPNormType normtype,PCSide pcside,PetscInt priority)
374: {

378:   ksp->normsupporttable[normtype][pcside] = priority;
379:   return(0);
380: }

384: PetscErrorCode KSPNormSupportTableReset_Private(KSP ksp)
385: {

389:   PetscMemzero(ksp->normsupporttable,sizeof(ksp->normsupporttable));
390:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);
391:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);
392:   ksp->pc_side  = ksp->pc_side_set;
393:   ksp->normtype = ksp->normtype_set;
394:   return(0);
395: }

399: PetscErrorCode KSPSetUpNorms_Private(KSP ksp,KSPNormType *normtype,PCSide *pcside)
400: {
401:   PetscInt i,j,best,ibest = 0,jbest = 0;

404:   best = 0;
405:   for (i=0; i<KSP_NORM_MAX; i++) {
406:     for (j=0; j<PC_SIDE_MAX; j++) {
407:       if ((ksp->normtype == KSP_NORM_DEFAULT || ksp->normtype == i)
408:           && (ksp->pc_side == PC_SIDE_DEFAULT || ksp->pc_side == j)
409:           && (ksp->normsupporttable[i][j] > best)) {
410:         best  = ksp->normsupporttable[i][j];
411:         ibest = i;
412:         jbest = j;
413:       }
414:     }
415:   }
416:   if (best < 1) {
417:     if (ksp->normtype == KSP_NORM_DEFAULT && ksp->pc_side == PC_SIDE_DEFAULT) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"The %s KSP implementation did not call KSPSetSupportedNorm()",((PetscObject)ksp)->type_name);
418:     if (ksp->normtype == KSP_NORM_DEFAULT) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s",((PetscObject)ksp)->type_name,PCSides[ksp->pc_side]);
419:     if (ksp->pc_side == PC_SIDE_DEFAULT) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s",((PetscObject)ksp)->type_name,KSPNormTypes[ksp->normtype]);
420:     SETERRQ3(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s with %s",((PetscObject)ksp)->type_name,KSPNormTypes[ksp->normtype],PCSides[ksp->pc_side]);
421:   }
422:   *normtype = (KSPNormType)ibest;
423:   *pcside   = (PCSide)jbest;
424:   return(0);
425: }

429: /*@
430:    KSPGetNormType - Gets the norm that is used for convergence testing.

432:    Not Collective

434:    Input Parameter:
435: .  ksp - Krylov solver context

437:    Output Parameter:
438: .  normtype - norm that is used for convergence testing

440:    Level: advanced

442: .keywords: KSP, create, context, norms

444: .seealso: KSPNormType, KSPSetNormType(), KSPConvergedSkip()
445: @*/
446: PetscErrorCode  KSPGetNormType(KSP ksp, KSPNormType *normtype)
447: {

453:   KSPSetUpNorms_Private(ksp,&ksp->normtype,&ksp->pc_side);
454:   *normtype = ksp->normtype;
455:   return(0);
456: }

458: #if defined(PETSC_HAVE_SAWS)
459: #include <petscviewersaws.h>
460: #endif

464: /*@
465:    KSPSetOperators - Sets the matrix associated with the linear system
466:    and a (possibly) different one associated with the preconditioner.

468:    Collective on KSP and Mat

470:    Input Parameters:
471: +  ksp - the KSP context
472: .  Amat - the matrix that defines the linear system
473: -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.

475:    Notes:

477:     All future calls to KSPSetOperators() must use the same size matrices!

479:     Passing a NULL for Amat or Pmat removes the matrix that is currently used.

481:     If you wish to replace either Amat or Pmat but leave the other one untouched then
482:     first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference()
483:     on it and then pass it back in in your call to KSPSetOperators().

485:     Level: beginner

487:    Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators
488:       are created in PC and returned to the user. In this case, if both operators
489:       mat and pmat are requested, two DIFFERENT operators will be returned. If
490:       only one is requested both operators in the PC will be the same (i.e. as
491:       if one had called KSP/PCSetOperators() with the same argument for both Mats).
492:       The user must set the sizes of the returned matrices and their type etc just
493:       as if the user created them with MatCreate(). For example,

495: $         KSP/PCGetOperators(ksp/pc,&mat,NULL); is equivalent to
496: $           set size, type, etc of mat

498: $         MatCreate(comm,&mat);
499: $         KSP/PCSetOperators(ksp/pc,mat,mat);
500: $         PetscObjectDereference((PetscObject)mat);
501: $           set size, type, etc of mat

503:      and

505: $         KSP/PCGetOperators(ksp/pc,&mat,&pmat); is equivalent to
506: $           set size, type, etc of mat and pmat

508: $         MatCreate(comm,&mat);
509: $         MatCreate(comm,&pmat);
510: $         KSP/PCSetOperators(ksp/pc,mat,pmat);
511: $         PetscObjectDereference((PetscObject)mat);
512: $         PetscObjectDereference((PetscObject)pmat);
513: $           set size, type, etc of mat and pmat

515:     The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy
516:     of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely
517:     managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look
518:     at this is when you create a SNES you do not NEED to create a KSP and attach it to
519:     the SNES object (the SNES object manages it for you). Similarly when you create a KSP
520:     you do not need to attach a PC to it (the KSP object manages the PC object for you).
521:     Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when
522:     it can be created for you?

524: .keywords: KSP, set, operators, matrix, preconditioner, linear system

526: .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators(), KSPSetComputeOperators(), KSPSetComputeInitialGuess(), KSPSetComputeRHS()
527: @*/
528: PetscErrorCode  KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat)
529: {

538:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
539:   PCSetOperators(ksp->pc,Amat,Pmat);
540:   if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;  /* so that next solve call will call PCSetUp() on new matrix */
541:   if (ksp->guess) {
542:     KSPFischerGuessReset(ksp->guess);
543:   }
544:   return(0);
545: }

549: /*@
550:    KSPGetOperators - Gets the matrix associated with the linear system
551:    and a (possibly) different one associated with the preconditioner.

553:    Collective on KSP and Mat

555:    Input Parameter:
556: .  ksp - the KSP context

558:    Output Parameters:
559: +  Amat - the matrix that defines the linear system
560: -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.

562:     Level: intermediate

564:    Notes: DOES NOT increase the reference counts of the matrix, so you should NOT destroy them.

566: .keywords: KSP, set, get, operators, matrix, preconditioner, linear system

568: .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet()
569: @*/
570: PetscErrorCode  KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat)
571: {

576:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
577:   PCGetOperators(ksp->pc,Amat,Pmat);
578:   return(0);
579: }

583: /*@C
584:    KSPGetOperatorsSet - Determines if the matrix associated with the linear system and
585:    possibly a different one associated with the preconditioner have been set in the KSP.

587:    Not collective, though the results on all processes should be the same

589:    Input Parameter:
590: .  pc - the KSP context

592:    Output Parameters:
593: +  mat - the matrix associated with the linear system was set
594: -  pmat - matrix associated with the preconditioner was set, usually the same

596:    Level: intermediate

598: .keywords: KSP, get, operators, matrix, linear system

600: .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet()
601: @*/
602: PetscErrorCode  KSPGetOperatorsSet(KSP ksp,PetscBool  *mat,PetscBool  *pmat)
603: {

608:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
609:   PCGetOperatorsSet(ksp->pc,mat,pmat);
610:   return(0);
611: }

615: /*@C
616:    KSPSetPreSolve - Sets a function that is called before every KSPSolve() is started

618:    Logically Collective on KSP

620:    Input Parameters:
621: +   ksp - the solver object
622: .   presolve - the function to call before the solve
623: -   prectx - any context needed by the function

625:    Level: developer

627: .keywords: KSP, create, context

629: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPostSolve()
630: @*/
631: PetscErrorCode  KSPSetPreSolve(KSP ksp,PetscErrorCode (*presolve)(KSP,Vec,Vec,void*),void *prectx)
632: {
635:   ksp->presolve = presolve;
636:   ksp->prectx   = prectx;
637:   return(0);
638: }

642: /*@C
643:    KSPSetPostSolve - Sets a function that is called after every KSPSolve() completes (whether it converges or not)

645:    Logically Collective on KSP

647:    Input Parameters:
648: +   ksp - the solver object
649: .   postsolve - the function to call after the solve
650: -   postctx - any context needed by the function

652:    Level: developer

654: .keywords: KSP, create, context

656: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPreSolve()
657: @*/
658: PetscErrorCode  KSPSetPostSolve(KSP ksp,PetscErrorCode (*postsolve)(KSP,Vec,Vec,void*),void *postctx)
659: {
662:   ksp->postsolve = postsolve;
663:   ksp->postctx   = postctx;
664:   return(0);
665: }

669: /*@
670:    KSPCreate - Creates the default KSP context.

672:    Collective on MPI_Comm

674:    Input Parameter:
675: .  comm - MPI communicator

677:    Output Parameter:
678: .  ksp - location to put the KSP context

680:    Notes:
681:    The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt
682:    orthogonalization.

684:    Level: beginner

686: .keywords: KSP, create, context

688: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP
689: @*/
690: PetscErrorCode  KSPCreate(MPI_Comm comm,KSP *inksp)
691: {
692:   KSP            ksp;
694:   void           *ctx;

698:   *inksp = 0;
699:   KSPInitializePackage();

701:   PetscHeaderCreate(ksp,KSP_CLASSID,"KSP","Krylov Method","KSP",comm,KSPDestroy,KSPView);

703:   ksp->max_it  = 10000;
704:   ksp->pc_side = ksp->pc_side_set = PC_SIDE_DEFAULT;
705:   ksp->rtol    = 1.e-5;
706: #if defined(PETSC_USE_REAL_SINGLE)
707:   ksp->abstol  = 1.e-25;
708: #else
709:   ksp->abstol  = 1.e-50;
710: #endif
711:   ksp->divtol  = 1.e4;

713:   ksp->chknorm        = -1;
714:   ksp->normtype       = ksp->normtype_set = KSP_NORM_DEFAULT;
715:   ksp->rnorm          = 0.0;
716:   ksp->its            = 0;
717:   ksp->guess_zero     = PETSC_TRUE;
718:   ksp->calc_sings     = PETSC_FALSE;
719:   ksp->res_hist       = NULL;
720:   ksp->res_hist_alloc = NULL;
721:   ksp->res_hist_len   = 0;
722:   ksp->res_hist_max   = 0;
723:   ksp->res_hist_reset = PETSC_TRUE;
724:   ksp->numbermonitors = 0;

726:   KSPConvergedDefaultCreate(&ctx);
727:   KSPSetConvergenceTest(ksp,KSPConvergedDefault,ctx,KSPConvergedDefaultDestroy);
728:   ksp->ops->buildsolution = KSPBuildSolutionDefault;
729:   ksp->ops->buildresidual = KSPBuildResidualDefault;

731:   ksp->vec_sol    = 0;
732:   ksp->vec_rhs    = 0;
733:   ksp->pc         = 0;
734:   ksp->data       = 0;
735:   ksp->nwork      = 0;
736:   ksp->work       = 0;
737:   ksp->reason     = KSP_CONVERGED_ITERATING;
738:   ksp->setupstage = KSP_SETUP_NEW;

740:   KSPNormSupportTableReset_Private(ksp);

742:   *inksp = ksp;
743:   return(0);
744: }

748: /*@C
749:    KSPSetType - Builds KSP for a particular solver.

751:    Logically Collective on KSP

753:    Input Parameters:
754: +  ksp      - the Krylov space context
755: -  type - a known method

757:    Options Database Key:
758: .  -ksp_type  <method> - Sets the method; use -help for a list
759:     of available methods (for instance, cg or gmres)

761:    Notes:
762:    See "petsc/include/petscksp.h" for available methods (for instance,
763:    KSPCG or KSPGMRES).

765:   Normally, it is best to use the KSPSetFromOptions() command and
766:   then set the KSP type from the options database rather than by using
767:   this routine.  Using the options database provides the user with
768:   maximum flexibility in evaluating the many different Krylov methods.
769:   The KSPSetType() routine is provided for those situations where it
770:   is necessary to set the iterative solver independently of the command
771:   line or options database.  This might be the case, for example, when
772:   the choice of iterative solver changes during the execution of the
773:   program, and the user's application is taking responsibility for
774:   choosing the appropriate method.  In other words, this routine is
775:   not for beginners.

777:   Level: intermediate

779:   Developer Note: KSPRegister() is used to add Krylov types to KSPList from which they
780:   are accessed by KSPSetType().

782: .keywords: KSP, set, method

784: .seealso: PCSetType(), KSPType, KSPRegister(), KSPCreate()

786: @*/
787: PetscErrorCode  KSPSetType(KSP ksp, KSPType type)
788: {
789:   PetscErrorCode ierr,(*r)(KSP);
790:   PetscBool      match;


796:   PetscObjectTypeCompare((PetscObject)ksp,type,&match);
797:   if (match) return(0);

799:    PetscFunctionListFind(KSPList,type,&r);
800:   if (!r) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type);
801:   /* Destroy the previous private KSP context */
802:   if (ksp->ops->destroy) {
803:     (*ksp->ops->destroy)(ksp);
804:     ksp->ops->destroy = NULL;
805:   }
806:   /* Reinitialize function pointers in KSPOps structure */
807:   PetscMemzero(ksp->ops,sizeof(struct _KSPOps));
808:   ksp->ops->buildsolution = KSPBuildSolutionDefault;
809:   ksp->ops->buildresidual = KSPBuildResidualDefault;
810:   KSPNormSupportTableReset_Private(ksp);
811:   /* Call the KSPCreate_XXX routine for this particular Krylov solver */
812:   ksp->setupstage = KSP_SETUP_NEW;
813:   PetscObjectChangeTypeName((PetscObject)ksp,type);
814:   (*r)(ksp);
815:   return(0);
816: }

820: /*@C
821:    KSPGetType - Gets the KSP type as a string from the KSP object.

823:    Not Collective

825:    Input Parameter:
826: .  ksp - Krylov context

828:    Output Parameter:
829: .  name - name of KSP method

831:    Level: intermediate

833: .keywords: KSP, get, method, name

835: .seealso: KSPSetType()
836: @*/
837: PetscErrorCode  KSPGetType(KSP ksp,KSPType *type)
838: {
842:   *type = ((PetscObject)ksp)->type_name;
843:   return(0);
844: }

848: /*@C
849:   KSPRegister -  Adds a method to the Krylov subspace solver package.

851:    Not Collective

853:    Input Parameters:
854: +  name_solver - name of a new user-defined solver
855: -  routine_create - routine to create method context

857:    Notes:
858:    KSPRegister() may be called multiple times to add several user-defined solvers.

860:    Sample usage:
861: .vb
862:    KSPRegister("my_solver",MySolverCreate);
863: .ve

865:    Then, your solver can be chosen with the procedural interface via
866: $     KSPSetType(ksp,"my_solver")
867:    or at runtime via the option
868: $     -ksp_type my_solver

870:    Level: advanced

872: .keywords: KSP, register

874: .seealso: KSPRegisterAll(), KSPRegisterDestroy()

876: @*/
877: PetscErrorCode  KSPRegister(const char sname[],PetscErrorCode (*function)(KSP))
878: {

882:   PetscFunctionListAdd(&KSPList,sname,function);
883:   return(0);
884: }