xref: /petsc/src/ksp/ksp/interface/itcreate.c (revision 6f3d89d0b697a3d79aa40878724d2c791c95edac)
1 /*
2      The basic KSP routines, Create, View etc. are here.
3 */
4 #include <petsc/private/kspimpl.h>      /*I "petscksp.h" I*/
5 
6 /* Logging support */
7 PetscClassId  KSP_CLASSID;
8 PetscClassId  DMKSP_CLASSID;
9 PetscClassId  KSPGUESS_CLASSID;
10 PetscLogEvent KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve, KSP_SolveTranspose, KSP_MatSolve;
11 
12 /*
13    Contains the list of registered KSP routines
14 */
15 PetscFunctionList KSPList              = NULL;
16 PetscBool         KSPRegisterAllCalled = PETSC_FALSE;
17 
18 /*@C
19   KSPLoad - Loads a KSP that has been stored in binary  with KSPView().
20 
21   Collective on viewer
22 
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()
27 
28    Level: intermediate
29 
30   Notes:
31    The type is determined by the data in the file, any type set into the KSP before this call is ignored.
32 
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
41 
42 .seealso: PetscViewerBinaryOpen(), KSPView(), MatLoad(), VecLoad()
43 @*/
44 PetscErrorCode  KSPLoad(KSP newdm, PetscViewer viewer)
45 {
46   PetscErrorCode ierr;
47   PetscBool      isbinary;
48   PetscInt       classid;
49   char           type[256];
50   PC             pc;
51 
52   PetscFunctionBegin;
53   PetscValidHeaderSpecific(newdm,KSP_CLASSID,1);
54   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
55   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
56   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
57 
58   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
59   if (classid != KSP_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not KSP next in file");
60   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
61   ierr = KSPSetType(newdm, type);CHKERRQ(ierr);
62   if (newdm->ops->load) {
63     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
64   }
65   ierr = KSPGetPC(newdm,&pc);CHKERRQ(ierr);
66   ierr = PCLoad(pc,viewer);CHKERRQ(ierr);
67   PetscFunctionReturn(0);
68 }
69 
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.
76 
77    Collective on ksp
78 
79    Input Parameters:
80 +  ksp - the Krylov space context
81 -  viewer - visualization context
82 
83    Options Database Keys:
84 .  -ksp_view - print the ksp data structure at the end of a KSPSolve call
85 
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.
93 
94    The user can open an alternative visualization context with
95    PetscViewerASCIIOpen() - output to a specified file.
96 
97   In the debugger you can do "call KSPView(ksp,0)" to display the KSP. (The same holds for any PETSc object viewer).
98 
99    Level: beginner
100 
101 .seealso: PCView(), PetscViewerASCIIOpen()
102 @*/
103 PetscErrorCode  KSPView(KSP ksp,PetscViewer viewer)
104 {
105   PetscErrorCode ierr;
106   PetscBool      iascii,isbinary,isdraw,isstring;
107 #if defined(PETSC_HAVE_SAWS)
108   PetscBool      issaws;
109 #endif
110 
111   PetscFunctionBegin;
112   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
113   if (!viewer) {
114     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);CHKERRQ(ierr);
115   }
116   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
117   PetscCheckSameComm(ksp,1,viewer,2);
118 
119   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
120   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
121   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
122   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
123 #if defined(PETSC_HAVE_SAWS)
124   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
125 #endif
126   if (iascii) {
127     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ksp,viewer);CHKERRQ(ierr);
128     if (ksp->ops->view) {
129       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
130       ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);
131       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
132     }
133     if (ksp->guess_zero) {
134       ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, initial guess is zero\n",ksp->max_it);CHKERRQ(ierr);
135     } else {
136       ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, nonzero initial guess\n", ksp->max_it);CHKERRQ(ierr);
137     }
138     if (ksp->guess_knoll) {ierr = PetscViewerASCIIPrintf(viewer,"  using preconditioner applied to right hand side for initial guess\n");CHKERRQ(ierr);}
139     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances:  relative=%g, absolute=%g, divergence=%g\n",(double)ksp->rtol,(double)ksp->abstol,(double)ksp->divtol);CHKERRQ(ierr);
140     if (ksp->pc_side == PC_RIGHT) {
141       ierr = PetscViewerASCIIPrintf(viewer,"  right preconditioning\n");CHKERRQ(ierr);
142     } else if (ksp->pc_side == PC_SYMMETRIC) {
143       ierr = PetscViewerASCIIPrintf(viewer,"  symmetric preconditioning\n");CHKERRQ(ierr);
144     } else {
145       ierr = PetscViewerASCIIPrintf(viewer,"  left preconditioning\n");CHKERRQ(ierr);
146     }
147     if (ksp->guess) {
148       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
149       ierr = KSPGuessView(ksp->guess,viewer);CHKERRQ(ierr);
150       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
151     }
152     if (ksp->dscale) {ierr = PetscViewerASCIIPrintf(viewer,"  diagonally scaled system\n");CHKERRQ(ierr);}
153     ierr = PetscViewerASCIIPrintf(viewer,"  using %s norm type for convergence test\n",KSPNormTypes[ksp->normtype]);CHKERRQ(ierr);
154   } else if (isbinary) {
155     PetscInt    classid = KSP_FILE_CLASSID;
156     MPI_Comm    comm;
157     PetscMPIInt rank;
158     char        type[256];
159 
160     ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
161     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
162     if (!rank) {
163       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
164       ierr = PetscStrncpy(type,((PetscObject)ksp)->type_name,256);CHKERRQ(ierr);
165       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
166     }
167     if (ksp->ops->view) {
168       ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);
169     }
170   } else if (isstring) {
171     const char *type;
172     ierr = KSPGetType(ksp,&type);CHKERRQ(ierr);
173     ierr = PetscViewerStringSPrintf(viewer," KSPType: %-7.7s",type);CHKERRQ(ierr);
174     if (ksp->ops->view) {ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);}
175   } else if (isdraw) {
176     PetscDraw draw;
177     char      str[36];
178     PetscReal x,y,bottom,h;
179     PetscBool flg;
180 
181     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
182     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
183     ierr = PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);CHKERRQ(ierr);
184     if (!flg) {
185       ierr   = PetscStrncpy(str,"KSP: ",sizeof(str));CHKERRQ(ierr);
186       ierr   = PetscStrlcat(str,((PetscObject)ksp)->type_name,sizeof(str));CHKERRQ(ierr);
187       ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
188       bottom = y - h;
189     } else {
190       bottom = y;
191     }
192     ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
193 #if defined(PETSC_HAVE_SAWS)
194   } else if (issaws) {
195     PetscMPIInt rank;
196     const char  *name;
197 
198     ierr = PetscObjectGetName((PetscObject)ksp,&name);CHKERRQ(ierr);
199     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
200     if (!((PetscObject)ksp)->amsmem && !rank) {
201       char       dir[1024];
202 
203       ierr = PetscObjectViewSAWs((PetscObject)ksp,viewer);CHKERRQ(ierr);
204       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/its",name);CHKERRQ(ierr);
205       PetscStackCallSAWs(SAWs_Register,(dir,&ksp->its,1,SAWs_READ,SAWs_INT));
206       if (!ksp->res_hist) {
207         ierr = KSPSetResidualHistory(ksp,NULL,PETSC_DECIDE,PETSC_TRUE);CHKERRQ(ierr);
208       }
209       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/res_hist",name);CHKERRQ(ierr);
210       PetscStackCallSAWs(SAWs_Register,(dir,ksp->res_hist,10,SAWs_READ,SAWs_DOUBLE));
211     }
212 #endif
213   } else if (ksp->ops->view) {
214     ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);
215   }
216   if (ksp->pc) {
217     ierr = PCView(ksp->pc,viewer);CHKERRQ(ierr);
218   }
219   if (isdraw) {
220     PetscDraw draw;
221     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
222     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
223   }
224   PetscFunctionReturn(0);
225 }
226 
227 /*@C
228    KSPViewFromOptions - View from Options
229 
230    Collective on KSP
231 
232    Input Parameters:
233 +  A - Krylov solver context
234 .  obj - Optional object
235 -  name - command line option
236 
237    Level: intermediate
238 .seealso:  KSP, KSPView, PetscObjectViewFromOptions(), KSPCreate()
239 @*/
240 PetscErrorCode  KSPViewFromOptions(KSP A,PetscObject obj,const char name[])
241 {
242   PetscErrorCode ierr;
243 
244   PetscFunctionBegin;
245   PetscValidHeaderSpecific(A,KSP_CLASSID,1);
246   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
247   PetscFunctionReturn(0);
248 }
249 
250 /*@
251    KSPSetNormType - Sets the norm that is used for convergence testing.
252 
253    Logically Collective on ksp
254 
255    Input Parameter:
256 +  ksp - Krylov solver context
257 -  normtype - one of
258 $   KSP_NORM_NONE - skips computing the norm, this should generally only be used if you are using
259 $                 the Krylov method as a smoother with a fixed small number of iterations.
260 $                 Implicitly sets KSPConvergedSkip() as KSP convergence test.
261 $                 Note that certain algorithms such as KSPGMRES ALWAYS require the norm calculation,
262 $                 for these methods the norms are still computed, they are just not used in
263 $                 the convergence test.
264 $   KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm
265 $                 of the preconditioned residual P^{-1}(b - A x)
266 $   KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual.
267 $   KSP_NORM_NATURAL - supported  by KSPCG, KSPCR, KSPCGNE, KSPCGS
268 
269 
270    Options Database Key:
271 .   -ksp_norm_type <none,preconditioned,unpreconditioned,natural>
272 
273    Notes:
274    Not all combinations of preconditioner side (see KSPSetPCSide()) and norm type are supported by all Krylov methods.
275    If only one is set, PETSc tries to automatically change the other to find a compatible pair.  If no such combination
276    is supported, PETSc will generate an error.
277 
278    Developer Notes:
279    Supported combinations of norm and preconditioner side are set using KSPSetSupportedNorm().
280 
281    Level: advanced
282 
283 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetCheckNormIteration(), KSPSetPCSide(), KSPGetPCSide(), KSPNormType
284 @*/
285 PetscErrorCode  KSPSetNormType(KSP ksp,KSPNormType normtype)
286 {
287   PetscFunctionBegin;
288   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
289   PetscValidLogicalCollectiveEnum(ksp,normtype,2);
290   ksp->normtype = ksp->normtype_set = normtype;
291   PetscFunctionReturn(0);
292 }
293 
294 /*@
295    KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be
296      computed and used in the convergence test.
297 
298    Logically Collective on ksp
299 
300    Input Parameter:
301 +  ksp - Krylov solver context
302 -  it  - use -1 to check at all iterations
303 
304    Notes:
305    Currently only works with KSPCG, KSPBCGS and KSPIBCGS
306 
307    Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm
308 
309    On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example,
310     -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged).
311    Level: advanced
312 
313 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType()
314 @*/
315 PetscErrorCode  KSPSetCheckNormIteration(KSP ksp,PetscInt it)
316 {
317   PetscFunctionBegin;
318   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
319   PetscValidLogicalCollectiveInt(ksp,it,2);
320   ksp->chknorm = it;
321   PetscFunctionReturn(0);
322 }
323 
324 /*@
325    KSPSetLagNorm - Lags the residual norm calculation so that it is computed as part of the MPI_Allreduce() for
326    computing the inner products for the next iteration.  This can reduce communication costs at the expense of doing
327    one additional iteration.
328 
329 
330    Logically Collective on ksp
331 
332    Input Parameter:
333 +  ksp - Krylov solver context
334 -  flg - PETSC_TRUE or PETSC_FALSE
335 
336    Options Database Keys:
337 .  -ksp_lag_norm - lag the calculated residual norm
338 
339    Notes:
340    Currently only works with KSPIBCGS.
341 
342    Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm
343 
344    If you lag the norm and run with, for example, -ksp_monitor, the residual norm reported will be the lagged one.
345    Level: advanced
346 
347 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPConvergedSkip(), KSPSetNormType(), KSPSetCheckNormIteration()
348 @*/
349 PetscErrorCode  KSPSetLagNorm(KSP ksp,PetscBool flg)
350 {
351   PetscFunctionBegin;
352   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
353   PetscValidLogicalCollectiveBool(ksp,flg,2);
354   ksp->lagnorm = flg;
355   PetscFunctionReturn(0);
356 }
357 
358 /*@
359    KSPSetSupportedNorm - Sets a norm and preconditioner side supported by a KSP
360 
361    Logically Collective
362 
363    Input Arguments:
364 +  ksp - Krylov method
365 .  normtype - supported norm type
366 .  pcside - preconditioner side that can be used with this norm
367 -  priority - positive integer preference for this combination; larger values have higher priority
368 
369    Level: developer
370 
371    Notes:
372    This function should be called from the implementation files KSPCreate_XXX() to declare
373    which norms and preconditioner sides are supported. Users should not need to call this
374    function.
375 
376 .seealso: KSPSetNormType(), KSPSetPCSide()
377 @*/
378 PetscErrorCode KSPSetSupportedNorm(KSP ksp,KSPNormType normtype,PCSide pcside,PetscInt priority)
379 {
380 
381   PetscFunctionBegin;
382   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
383   ksp->normsupporttable[normtype][pcside] = priority;
384   PetscFunctionReturn(0);
385 }
386 
387 PetscErrorCode KSPNormSupportTableReset_Private(KSP ksp)
388 {
389   PetscErrorCode ierr;
390 
391   PetscFunctionBegin;
392   ierr = PetscMemzero(ksp->normsupporttable,sizeof(ksp->normsupporttable));CHKERRQ(ierr);
393   ksp->pc_side  = ksp->pc_side_set;
394   ksp->normtype = ksp->normtype_set;
395   PetscFunctionReturn(0);
396 }
397 
398 PetscErrorCode KSPSetUpNorms_Private(KSP ksp,PetscBool errorifnotsupported,KSPNormType *normtype,PCSide *pcside)
399 {
400   PetscInt i,j,best,ibest = 0,jbest = 0;
401 
402   PetscFunctionBegin;
403   best = 0;
404   for (i=0; i<KSP_NORM_MAX; i++) {
405     for (j=0; j<PC_SIDE_MAX; j++) {
406       if ((ksp->normtype == KSP_NORM_DEFAULT || ksp->normtype == i) && (ksp->pc_side == PC_SIDE_DEFAULT || ksp->pc_side == j) && (ksp->normsupporttable[i][j] > best)) {
407         best  = ksp->normsupporttable[i][j];
408         ibest = i;
409         jbest = j;
410       }
411     }
412   }
413   if (best < 1 && errorifnotsupported) {
414     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);
415     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]);
416     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]);
417     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]);
418   }
419   if (normtype) *normtype = (KSPNormType)ibest;
420   if (pcside)   *pcside   = (PCSide)jbest;
421   PetscFunctionReturn(0);
422 }
423 
424 /*@
425    KSPGetNormType - Gets the norm that is used for convergence testing.
426 
427    Not Collective
428 
429    Input Parameter:
430 .  ksp - Krylov solver context
431 
432    Output Parameter:
433 .  normtype - norm that is used for convergence testing
434 
435    Level: advanced
436 
437 .seealso: KSPNormType, KSPSetNormType(), KSPConvergedSkip()
438 @*/
439 PetscErrorCode  KSPGetNormType(KSP ksp, KSPNormType *normtype)
440 {
441   PetscErrorCode ierr;
442 
443   PetscFunctionBegin;
444   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
445   PetscValidPointer(normtype,2);
446   ierr      = KSPSetUpNorms_Private(ksp,PETSC_TRUE,&ksp->normtype,&ksp->pc_side);CHKERRQ(ierr);
447   *normtype = ksp->normtype;
448   PetscFunctionReturn(0);
449 }
450 
451 #if defined(PETSC_HAVE_SAWS)
452 #include <petscviewersaws.h>
453 #endif
454 
455 /*@
456    KSPSetOperators - Sets the matrix associated with the linear system
457    and a (possibly) different one associated with the preconditioner.
458 
459    Collective on ksp
460 
461    Input Parameters:
462 +  ksp - the KSP context
463 .  Amat - the matrix that defines the linear system
464 -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.
465 
466    Notes:
467 
468     If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
469     space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
470 
471     All future calls to KSPSetOperators() must use the same size matrices!
472 
473     Passing a NULL for Amat or Pmat removes the matrix that is currently used.
474 
475     If you wish to replace either Amat or Pmat but leave the other one untouched then
476     first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference()
477     on it and then pass it back in in your call to KSPSetOperators().
478 
479     Level: beginner
480 
481    Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators
482       are created in PC and returned to the user. In this case, if both operators
483       mat and pmat are requested, two DIFFERENT operators will be returned. If
484       only one is requested both operators in the PC will be the same (i.e. as
485       if one had called KSP/PCSetOperators() with the same argument for both Mats).
486       The user must set the sizes of the returned matrices and their type etc just
487       as if the user created them with MatCreate(). For example,
488 
489 $         KSP/PCGetOperators(ksp/pc,&mat,NULL); is equivalent to
490 $           set size, type, etc of mat
491 
492 $         MatCreate(comm,&mat);
493 $         KSP/PCSetOperators(ksp/pc,mat,mat);
494 $         PetscObjectDereference((PetscObject)mat);
495 $           set size, type, etc of mat
496 
497      and
498 
499 $         KSP/PCGetOperators(ksp/pc,&mat,&pmat); is equivalent to
500 $           set size, type, etc of mat and pmat
501 
502 $         MatCreate(comm,&mat);
503 $         MatCreate(comm,&pmat);
504 $         KSP/PCSetOperators(ksp/pc,mat,pmat);
505 $         PetscObjectDereference((PetscObject)mat);
506 $         PetscObjectDereference((PetscObject)pmat);
507 $           set size, type, etc of mat and pmat
508 
509     The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy
510     of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely
511     managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look
512     at this is when you create a SNES you do not NEED to create a KSP and attach it to
513     the SNES object (the SNES object manages it for you). Similarly when you create a KSP
514     you do not need to attach a PC to it (the KSP object manages the PC object for you).
515     Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when
516     it can be created for you?
517 
518 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators(), KSPSetComputeOperators(), KSPSetComputeInitialGuess(), KSPSetComputeRHS()
519 @*/
520 PetscErrorCode  KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat)
521 {
522   PetscErrorCode ierr;
523 
524   PetscFunctionBegin;
525   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
526   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
527   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
528   if (Amat) PetscCheckSameComm(ksp,1,Amat,2);
529   if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3);
530   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
531   ierr = PCSetOperators(ksp->pc,Amat,Pmat);CHKERRQ(ierr);
532   if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;  /* so that next solve call will call PCSetUp() on new matrix */
533   PetscFunctionReturn(0);
534 }
535 
536 /*@
537    KSPGetOperators - Gets the matrix associated with the linear system
538    and a (possibly) different one associated with the preconditioner.
539 
540    Collective on ksp
541 
542    Input Parameter:
543 .  ksp - the KSP context
544 
545    Output Parameters:
546 +  Amat - the matrix that defines the linear system
547 -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.
548 
549     Level: intermediate
550 
551    Notes:
552     DOES NOT increase the reference counts of the matrix, so you should NOT destroy them.
553 
554 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet()
555 @*/
556 PetscErrorCode  KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat)
557 {
558   PetscErrorCode ierr;
559 
560   PetscFunctionBegin;
561   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
562   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
563   ierr = PCGetOperators(ksp->pc,Amat,Pmat);CHKERRQ(ierr);
564   PetscFunctionReturn(0);
565 }
566 
567 /*@C
568    KSPGetOperatorsSet - Determines if the matrix associated with the linear system and
569    possibly a different one associated with the preconditioner have been set in the KSP.
570 
571    Not collective, though the results on all processes should be the same
572 
573    Input Parameter:
574 .  pc - the KSP context
575 
576    Output Parameters:
577 +  mat - the matrix associated with the linear system was set
578 -  pmat - matrix associated with the preconditioner was set, usually the same
579 
580    Level: intermediate
581 
582 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet()
583 @*/
584 PetscErrorCode  KSPGetOperatorsSet(KSP ksp,PetscBool  *mat,PetscBool  *pmat)
585 {
586   PetscErrorCode ierr;
587 
588   PetscFunctionBegin;
589   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
590   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
591   ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr);
592   PetscFunctionReturn(0);
593 }
594 
595 /*@C
596    KSPSetPreSolve - Sets a function that is called before every KSPSolve() is started
597 
598    Logically Collective on ksp
599 
600    Input Parameters:
601 +   ksp - the solver object
602 .   presolve - the function to call before the solve
603 -   prectx - any context needed by the function
604 
605    Calling sequence of presolve:
606 $  func(KSP ksp,Vec rhs,Vec x,void *ctx)
607 
608 +  ksp - the KSP context
609 .  rhs - the right-hand side vector
610 .  x - the solution vector
611 -  ctx - optional user-provided context
612 
613    Level: developer
614 
615 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPostSolve()
616 @*/
617 PetscErrorCode  KSPSetPreSolve(KSP ksp,PetscErrorCode (*presolve)(KSP,Vec,Vec,void*),void *prectx)
618 {
619   PetscFunctionBegin;
620   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
621   ksp->presolve = presolve;
622   ksp->prectx   = prectx;
623   PetscFunctionReturn(0);
624 }
625 
626 /*@C
627    KSPSetPostSolve - Sets a function that is called after every KSPSolve() completes (whether it converges or not)
628 
629    Logically Collective on ksp
630 
631    Input Parameters:
632 +   ksp - the solver object
633 .   postsolve - the function to call after the solve
634 -   postctx - any context needed by the function
635 
636    Level: developer
637 
638    Calling sequence of postsolve:
639 $  func(KSP ksp,Vec rhs,Vec x,void *ctx)
640 
641 +  ksp - the KSP context
642 .  rhs - the right-hand side vector
643 .  x - the solution vector
644 -  ctx - optional user-provided context
645 
646 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPreSolve()
647 @*/
648 PetscErrorCode  KSPSetPostSolve(KSP ksp,PetscErrorCode (*postsolve)(KSP,Vec,Vec,void*),void *postctx)
649 {
650   PetscFunctionBegin;
651   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
652   ksp->postsolve = postsolve;
653   ksp->postctx   = postctx;
654   PetscFunctionReturn(0);
655 }
656 
657 /*@
658    KSPCreate - Creates the default KSP context.
659 
660    Collective
661 
662    Input Parameter:
663 .  comm - MPI communicator
664 
665    Output Parameter:
666 .  ksp - location to put the KSP context
667 
668    Notes:
669    The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt
670    orthogonalization.
671 
672    Level: beginner
673 
674 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP
675 @*/
676 PetscErrorCode  KSPCreate(MPI_Comm comm,KSP *inksp)
677 {
678   KSP            ksp;
679   PetscErrorCode ierr;
680   void           *ctx;
681 
682   PetscFunctionBegin;
683   PetscValidPointer(inksp,2);
684   *inksp = NULL;
685   ierr = KSPInitializePackage();CHKERRQ(ierr);
686 
687   ierr = PetscHeaderCreate(ksp,KSP_CLASSID,"KSP","Krylov Method","KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr);
688 
689   ksp->max_it  = 10000;
690   ksp->pc_side = ksp->pc_side_set = PC_SIDE_DEFAULT;
691   ksp->rtol    = 1.e-5;
692 #if defined(PETSC_USE_REAL_SINGLE)
693   ksp->abstol  = 1.e-25;
694 #else
695   ksp->abstol  = 1.e-50;
696 #endif
697   ksp->divtol  = 1.e4;
698 
699   ksp->chknorm        = -1;
700   ksp->normtype       = ksp->normtype_set = KSP_NORM_DEFAULT;
701   ksp->rnorm          = 0.0;
702   ksp->its            = 0;
703   ksp->guess_zero     = PETSC_TRUE;
704   ksp->calc_sings     = PETSC_FALSE;
705   ksp->res_hist       = NULL;
706   ksp->res_hist_alloc = NULL;
707   ksp->res_hist_len   = 0;
708   ksp->res_hist_max   = 0;
709   ksp->res_hist_reset = PETSC_TRUE;
710   ksp->err_hist       = NULL;
711   ksp->err_hist_alloc = NULL;
712   ksp->err_hist_len   = 0;
713   ksp->err_hist_max   = 0;
714   ksp->err_hist_reset = PETSC_TRUE;
715   ksp->numbermonitors = 0;
716   ksp->setfromoptionscalled = 0;
717 
718   ierr                    = KSPConvergedDefaultCreate(&ctx);CHKERRQ(ierr);
719   ierr                    = KSPSetConvergenceTest(ksp,KSPConvergedDefault,ctx,KSPConvergedDefaultDestroy);CHKERRQ(ierr);
720   ksp->ops->buildsolution = KSPBuildSolutionDefault;
721   ksp->ops->buildresidual = KSPBuildResidualDefault;
722 
723   ksp->vec_sol    = NULL;
724   ksp->vec_rhs    = NULL;
725   ksp->pc         = NULL;
726   ksp->data       = NULL;
727   ksp->nwork      = 0;
728   ksp->work       = NULL;
729   ksp->reason     = KSP_CONVERGED_ITERATING;
730   ksp->setupstage = KSP_SETUP_NEW;
731 
732   ierr = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr);
733 
734   *inksp = ksp;
735   PetscFunctionReturn(0);
736 }
737 
738 /*@C
739    KSPSetType - Builds KSP for a particular solver.
740 
741    Logically Collective on ksp
742 
743    Input Parameters:
744 +  ksp      - the Krylov space context
745 -  type - a known method
746 
747    Options Database Key:
748 .  -ksp_type  <method> - Sets the method; use -help for a list
749     of available methods (for instance, cg or gmres)
750 
751    Notes:
752    See "petsc/include/petscksp.h" for available methods (for instance,
753    KSPCG or KSPGMRES).
754 
755   Normally, it is best to use the KSPSetFromOptions() command and
756   then set the KSP type from the options database rather than by using
757   this routine.  Using the options database provides the user with
758   maximum flexibility in evaluating the many different Krylov methods.
759   The KSPSetType() routine is provided for those situations where it
760   is necessary to set the iterative solver independently of the command
761   line or options database.  This might be the case, for example, when
762   the choice of iterative solver changes during the execution of the
763   program, and the user's application is taking responsibility for
764   choosing the appropriate method.  In other words, this routine is
765   not for beginners.
766 
767   Level: intermediate
768 
769   Developer Note: KSPRegister() is used to add Krylov types to KSPList from which they
770   are accessed by KSPSetType().
771 
772 .seealso: PCSetType(), KSPType, KSPRegister(), KSPCreate()
773 
774 @*/
775 PetscErrorCode  KSPSetType(KSP ksp, KSPType type)
776 {
777   PetscErrorCode ierr,(*r)(KSP);
778   PetscBool      match;
779 
780   PetscFunctionBegin;
781   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
782   PetscValidCharPointer(type,2);
783 
784   ierr = PetscObjectTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr);
785   if (match) PetscFunctionReturn(0);
786 
787   ierr = PetscFunctionListFind(KSPList,type,&r);CHKERRQ(ierr);
788   if (!r) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type);
789   /* Destroy the previous private KSP context */
790   if (ksp->ops->destroy) {
791     ierr              = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr);
792     ksp->ops->destroy = NULL;
793   }
794   /* Reinitialize function pointers in KSPOps structure */
795   ierr                    = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr);
796   ksp->ops->buildsolution = KSPBuildSolutionDefault;
797   ksp->ops->buildresidual = KSPBuildResidualDefault;
798   ierr                    = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr);
799   ksp->setupnewmatrix     = PETSC_FALSE; // restore default (setup not called in case of new matrix)
800   /* Call the KSPCreate_XXX routine for this particular Krylov solver */
801   ksp->setupstage = KSP_SETUP_NEW;
802   ierr            = (*r)(ksp);CHKERRQ(ierr);
803   ierr            = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr);
804   PetscFunctionReturn(0);
805 }
806 
807 /*@C
808    KSPGetType - Gets the KSP type as a string from the KSP object.
809 
810    Not Collective
811 
812    Input Parameter:
813 .  ksp - Krylov context
814 
815    Output Parameter:
816 .  name - name of KSP method
817 
818    Level: intermediate
819 
820 .seealso: KSPSetType()
821 @*/
822 PetscErrorCode  KSPGetType(KSP ksp,KSPType *type)
823 {
824   PetscFunctionBegin;
825   PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
826   PetscValidPointer(type,2);
827   *type = ((PetscObject)ksp)->type_name;
828   PetscFunctionReturn(0);
829 }
830 
831 /*@C
832   KSPRegister -  Adds a method to the Krylov subspace solver package.
833 
834    Not Collective
835 
836    Input Parameters:
837 +  name_solver - name of a new user-defined solver
838 -  routine_create - routine to create method context
839 
840    Notes:
841    KSPRegister() may be called multiple times to add several user-defined solvers.
842 
843    Sample usage:
844 .vb
845    KSPRegister("my_solver",MySolverCreate);
846 .ve
847 
848    Then, your solver can be chosen with the procedural interface via
849 $     KSPSetType(ksp,"my_solver")
850    or at runtime via the option
851 $     -ksp_type my_solver
852 
853    Level: advanced
854 
855 .seealso: KSPRegisterAll()
856 @*/
857 PetscErrorCode  KSPRegister(const char sname[],PetscErrorCode (*function)(KSP))
858 {
859   PetscErrorCode ierr;
860 
861   PetscFunctionBegin;
862   ierr = KSPInitializePackage();CHKERRQ(ierr);
863   ierr = PetscFunctionListAdd(&KSPList,sname,function);CHKERRQ(ierr);
864   PetscFunctionReturn(0);
865 }
866