Actual source code: itcreate.c

petsc-master 2017-04-26
Report Typos and Errors

  2: /*
  3:      The basic KSP routines, Create, View etc. are here.
  4: */
  5:  #include <petsc/private/kspimpl.h>

  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;

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

 21:   Collective on PetscViewer

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

 28:    Level: intermediate

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

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

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

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

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

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

 77:    Collective on KSP

 79:    Input Parameters:
 80: +  ksp - the Krylov space context
 81: -  viewer - visualization context

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

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

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

 97:    Level: beginner

 99: .keywords: KSP, view

101: .seealso: PCView(), PetscViewerASCIIOpen()
102: @*/
103: PetscErrorCode  KSPView(KSP ksp,PetscViewer viewer)
104: {
106:   PetscBool      iascii,isbinary,isdraw;
107: #if defined(PETSC_HAVE_SAWS)
108:   PetscBool      issaws;
109: #endif

113:   if (!viewer) {
114:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);
115:   }

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

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

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

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

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


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

223:    Logically Collective on KSP

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


237:    Options Database Key:
238: .   -ksp_norm_type <none,preconditioned,unpreconditioned,natural>

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

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

248:    Level: advanced

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

252: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetCheckNormIteration(), KSPSetPCSide(), KSPGetPCSide(), KSPNormType
253: @*/
254: PetscErrorCode  KSPSetNormType(KSP ksp,KSPNormType normtype)
255: {
259:   ksp->normtype = ksp->normtype_set = normtype;
260:   return(0);
261: }

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

267:    Logically Collective on KSP

269:    Input Parameter:
270: +  ksp - Krylov solver context
271: -  it  - use -1 to check at all iterations

273:    Notes:
274:    Currently only works with KSPCG, KSPBCGS and KSPIBCGS

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

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

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

284: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType()
285: @*/
286: PetscErrorCode  KSPSetCheckNormIteration(KSP ksp,PetscInt it)
287: {
291:   ksp->chknorm = it;
292:   return(0);
293: }

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


301:    Logically Collective on KSP

303:    Input Parameter:
304: +  ksp - Krylov solver context
305: -  flg - PETSC_TRUE or PETSC_FALSE

307:    Options Database Keys:
308: .  -ksp_lag_norm - lag the calculated residual norm

310:    Notes:
311:    Currently only works with KSPIBCGS.

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

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

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

320: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType(), KSPSetCheckNormIteration()
321: @*/
322: PetscErrorCode  KSPSetLagNorm(KSP ksp,PetscBool flg)
323: {
327:   ksp->lagnorm = flg;
328:   return(0);
329: }

331: /*@
332:    KSPSetSupportedNorm - Sets a norm and preconditioner side supported by a KSP

334:    Logically Collective

336:    Input Arguments:
337: +  ksp - Krylov method
338: .  normtype - supported norm type
339: .  pcside - preconditioner side that can be used with this norm
340: -  preference - integer preference for this combination, larger values have higher priority

342:    Level: developer

344:    Notes:
345:    This function should be called from the implementation files KSPCreate_XXX() to declare
346:    which norms and preconditioner sides are supported. Users should not need to call this
347:    function.

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

353: .seealso: KSPSetNormType(), KSPSetPCSide()
354: @*/
355: PetscErrorCode KSPSetSupportedNorm(KSP ksp,KSPNormType normtype,PCSide pcside,PetscInt priority)
356: {

360:   ksp->normsupporttable[normtype][pcside] = priority;
361:   return(0);
362: }

364: PetscErrorCode KSPNormSupportTableReset_Private(KSP ksp)
365: {

369:   PetscMemzero(ksp->normsupporttable,sizeof(ksp->normsupporttable));
370:   ksp->pc_side  = ksp->pc_side_set;
371:   ksp->normtype = ksp->normtype_set;
372:   return(0);
373: }

375: PetscErrorCode KSPSetUpNorms_Private(KSP ksp,PetscBool errorifnotsupported,KSPNormType *normtype,PCSide *pcside)
376: {
377:   PetscInt i,j,best,ibest = 0,jbest = 0;

380:   best = 0;
381:   for (i=0; i<KSP_NORM_MAX; i++) {
382:     for (j=0; j<PC_SIDE_MAX; j++) {
383:       if ((ksp->normtype == KSP_NORM_DEFAULT || ksp->normtype == i) && (ksp->pc_side == PC_SIDE_DEFAULT || ksp->pc_side == j) && (ksp->normsupporttable[i][j] > best)) {
384:         best  = ksp->normsupporttable[i][j];
385:         ibest = i;
386:         jbest = j;
387:       }
388:     }
389:   }
390:   if (best < 1 && errorifnotsupported) {
391:     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);
392:     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]);
393:     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]);
394:     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]);
395:   }
396:   if (normtype) *normtype = (KSPNormType)ibest;
397:   if (pcside)   *pcside   = (PCSide)jbest;
398:   return(0);
399: }

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

404:    Not Collective

406:    Input Parameter:
407: .  ksp - Krylov solver context

409:    Output Parameter:
410: .  normtype - norm that is used for convergence testing

412:    Level: advanced

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

416: .seealso: KSPNormType, KSPSetNormType(), KSPConvergedSkip()
417: @*/
418: PetscErrorCode  KSPGetNormType(KSP ksp, KSPNormType *normtype)
419: {

425:   KSPSetUpNorms_Private(ksp,PETSC_TRUE,&ksp->normtype,&ksp->pc_side);
426:   *normtype = ksp->normtype;
427:   return(0);
428: }

430: #if defined(PETSC_HAVE_SAWS)
431:  #include <petscviewersaws.h>
432: #endif

434: /*@
435:    KSPSetOperators - Sets the matrix associated with the linear system
436:    and a (possibly) different one associated with the preconditioner.

438:    Collective on KSP and Mat

440:    Input Parameters:
441: +  ksp - the KSP context
442: .  Amat - the matrix that defines the linear system
443: -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.

445:    Notes:

447:     If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
448:     space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.

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

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

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

458:     Level: beginner

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

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

471: $         MatCreate(comm,&mat);
472: $         KSP/PCSetOperators(ksp/pc,mat,mat);
473: $         PetscObjectDereference((PetscObject)mat);
474: $           set size, type, etc of mat

476:      and

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

481: $         MatCreate(comm,&mat);
482: $         MatCreate(comm,&pmat);
483: $         KSP/PCSetOperators(ksp/pc,mat,pmat);
484: $         PetscObjectDereference((PetscObject)mat);
485: $         PetscObjectDereference((PetscObject)pmat);
486: $           set size, type, etc of mat and pmat

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

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

499: .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators(), KSPSetComputeOperators(), KSPSetComputeInitialGuess(), KSPSetComputeRHS()
500: @*/
501: PetscErrorCode  KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat)
502: {

511:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
512:   PCSetOperators(ksp->pc,Amat,Pmat);
513:   if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;  /* so that next solve call will call PCSetUp() on new matrix */
514:   if (ksp->guess) {
515:     KSPFischerGuessReset(ksp->guess);
516:   }
517:   return(0);
518: }

520: /*@
521:    KSPGetOperators - Gets the matrix associated with the linear system
522:    and a (possibly) different one associated with the preconditioner.

524:    Collective on KSP and Mat

526:    Input Parameter:
527: .  ksp - the KSP context

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

533:     Level: intermediate

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

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

539: .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet()
540: @*/
541: PetscErrorCode  KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat)
542: {

547:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
548:   PCGetOperators(ksp->pc,Amat,Pmat);
549:   return(0);
550: }

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

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

558:    Input Parameter:
559: .  pc - the KSP context

561:    Output Parameters:
562: +  mat - the matrix associated with the linear system was set
563: -  pmat - matrix associated with the preconditioner was set, usually the same

565:    Level: intermediate

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

569: .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet()
570: @*/
571: PetscErrorCode  KSPGetOperatorsSet(KSP ksp,PetscBool  *mat,PetscBool  *pmat)
572: {

577:   if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
578:   PCGetOperatorsSet(ksp->pc,mat,pmat);
579:   return(0);
580: }

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

585:    Logically Collective on KSP

587:    Input Parameters:
588: +   ksp - the solver object
589: .   presolve - the function to call before the solve
590: -   prectx - any context needed by the function

592:    Level: developer

594: .keywords: KSP, create, context

596: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPostSolve()
597: @*/
598: PetscErrorCode  KSPSetPreSolve(KSP ksp,PetscErrorCode (*presolve)(KSP,Vec,Vec,void*),void *prectx)
599: {
602:   ksp->presolve = presolve;
603:   ksp->prectx   = prectx;
604:   return(0);
605: }

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

610:    Logically Collective on KSP

612:    Input Parameters:
613: +   ksp - the solver object
614: .   postsolve - the function to call after the solve
615: -   postctx - any context needed by the function

617:    Level: developer

619: .keywords: KSP, create, context

621: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPreSolve()
622: @*/
623: PetscErrorCode  KSPSetPostSolve(KSP ksp,PetscErrorCode (*postsolve)(KSP,Vec,Vec,void*),void *postctx)
624: {
627:   ksp->postsolve = postsolve;
628:   ksp->postctx   = postctx;
629:   return(0);
630: }

632: /*@
633:    KSPCreate - Creates the default KSP context.

635:    Collective on MPI_Comm

637:    Input Parameter:
638: .  comm - MPI communicator

640:    Output Parameter:
641: .  ksp - location to put the KSP context

643:    Notes:
644:    The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt
645:    orthogonalization.

647:    Level: beginner

649: .keywords: KSP, create, context

651: .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP
652: @*/
653: PetscErrorCode  KSPCreate(MPI_Comm comm,KSP *inksp)
654: {
655:   KSP            ksp;
657:   void           *ctx;

661:   *inksp = 0;
662:   KSPInitializePackage();

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

666:   ksp->max_it  = 10000;
667:   ksp->pc_side = ksp->pc_side_set = PC_SIDE_DEFAULT;
668:   ksp->rtol    = 1.e-5;
669: #if defined(PETSC_USE_REAL_SINGLE)
670:   ksp->abstol  = 1.e-25;
671: #else
672:   ksp->abstol  = 1.e-50;
673: #endif
674:   ksp->divtol  = 1.e4;

676:   ksp->chknorm        = -1;
677:   ksp->normtype       = ksp->normtype_set = KSP_NORM_DEFAULT;
678:   ksp->rnorm          = 0.0;
679:   ksp->its            = 0;
680:   ksp->guess_zero     = PETSC_TRUE;
681:   ksp->calc_sings     = PETSC_FALSE;
682:   ksp->res_hist       = NULL;
683:   ksp->res_hist_alloc = NULL;
684:   ksp->res_hist_len   = 0;
685:   ksp->res_hist_max   = 0;
686:   ksp->res_hist_reset = PETSC_TRUE;
687:   ksp->numbermonitors = 0;

689:   KSPConvergedDefaultCreate(&ctx);
690:   KSPSetConvergenceTest(ksp,KSPConvergedDefault,ctx,KSPConvergedDefaultDestroy);
691:   ksp->ops->buildsolution = KSPBuildSolutionDefault;
692:   ksp->ops->buildresidual = KSPBuildResidualDefault;

694:   ksp->vec_sol    = 0;
695:   ksp->vec_rhs    = 0;
696:   ksp->pc         = 0;
697:   ksp->data       = 0;
698:   ksp->nwork      = 0;
699:   ksp->work       = 0;
700:   ksp->reason     = KSP_CONVERGED_ITERATING;
701:   ksp->setupstage = KSP_SETUP_NEW;

703:   KSPNormSupportTableReset_Private(ksp);

705:   *inksp = ksp;
706:   return(0);
707: }

709: /*@C
710:    KSPSetType - Builds KSP for a particular solver.

712:    Logically Collective on KSP

714:    Input Parameters:
715: +  ksp      - the Krylov space context
716: -  type - a known method

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

722:    Notes:
723:    See "petsc/include/petscksp.h" for available methods (for instance,
724:    KSPCG or KSPGMRES).

726:   Normally, it is best to use the KSPSetFromOptions() command and
727:   then set the KSP type from the options database rather than by using
728:   this routine.  Using the options database provides the user with
729:   maximum flexibility in evaluating the many different Krylov methods.
730:   The KSPSetType() routine is provided for those situations where it
731:   is necessary to set the iterative solver independently of the command
732:   line or options database.  This might be the case, for example, when
733:   the choice of iterative solver changes during the execution of the
734:   program, and the user's application is taking responsibility for
735:   choosing the appropriate method.  In other words, this routine is
736:   not for beginners.

738:   Level: intermediate

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

743: .keywords: KSP, set, method

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

747: @*/
748: PetscErrorCode  KSPSetType(KSP ksp, KSPType type)
749: {
750:   PetscErrorCode ierr,(*r)(KSP);
751:   PetscBool      match;


757:   PetscObjectTypeCompare((PetscObject)ksp,type,&match);
758:   if (match) return(0);

760:    PetscFunctionListFind(KSPList,type,&r);
761:   if (!r) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type);
762:   /* Destroy the previous private KSP context */
763:   if (ksp->ops->destroy) {
764:     (*ksp->ops->destroy)(ksp);
765:     ksp->ops->destroy = NULL;
766:   }
767:   /* Reinitialize function pointers in KSPOps structure */
768:   PetscMemzero(ksp->ops,sizeof(struct _KSPOps));
769:   ksp->ops->buildsolution = KSPBuildSolutionDefault;
770:   ksp->ops->buildresidual = KSPBuildResidualDefault;
771:   KSPNormSupportTableReset_Private(ksp);
772:   /* Call the KSPCreate_XXX routine for this particular Krylov solver */
773:   ksp->setupstage = KSP_SETUP_NEW;
774:   PetscObjectChangeTypeName((PetscObject)ksp,type);
775:   (*r)(ksp);
776:   return(0);
777: }

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

782:    Not Collective

784:    Input Parameter:
785: .  ksp - Krylov context

787:    Output Parameter:
788: .  name - name of KSP method

790:    Level: intermediate

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

794: .seealso: KSPSetType()
795: @*/
796: PetscErrorCode  KSPGetType(KSP ksp,KSPType *type)
797: {
801:   *type = ((PetscObject)ksp)->type_name;
802:   return(0);
803: }

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

808:    Not Collective

810:    Input Parameters:
811: +  name_solver - name of a new user-defined solver
812: -  routine_create - routine to create method context

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

817:    Sample usage:
818: .vb
819:    KSPRegister("my_solver",MySolverCreate);
820: .ve

822:    Then, your solver can be chosen with the procedural interface via
823: $     KSPSetType(ksp,"my_solver")
824:    or at runtime via the option
825: $     -ksp_type my_solver

827:    Level: advanced

829: .keywords: KSP, register

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

833: @*/
834: PetscErrorCode  KSPRegister(const char sname[],PetscErrorCode (*function)(KSP))
835: {

839:   PetscFunctionListAdd(&KSPList,sname,function);
840:   return(0);
841: }