xref: /petsc/src/ksp/ksp/interface/itcreate.c (revision f0523c5fc43eab0988c01ff3e0933081fbffb47d)
1 #define PETSCKSP_DLL
2 
3 /*
4      The basic KSP routines, Create, View etc. are here.
5 */
6 #include "include/private/kspimpl.h"      /*I "petscksp.h" I*/
7 #include "petscsys.h"
8 
9 /* Logging support */
10 PetscCookie PETSCKSP_DLLEXPORT KSP_COOKIE;
11 PetscLogEvent  KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve;
12 
13 
14 PetscTruth KSPRegisterAllCalled = PETSC_FALSE;
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "KSPView"
18 /*@C
19    KSPView - Prints the KSP data structure.
20 
21    Collective on KSP
22 
23    Input Parameters:
24 +  ksp - the Krylov space context
25 -  viewer - visualization context
26 
27    Options Database Keys:
28 .  -ksp_view - print the ksp data structure at the end of a KSPSolve call
29 
30    Note:
31    The available visualization contexts include
32 +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
33 -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
34          output where only the first processor opens
35          the file.  All other processors send their
36          data to the first processor to print.
37 
38    The user can open an alternative visualization context with
39    PetscViewerASCIIOpen() - output to a specified file.
40 
41    Level: beginner
42 
43 .keywords: KSP, view
44 
45 .seealso: PCView(), PetscViewerASCIIOpen()
46 @*/
47 PetscErrorCode PETSCKSP_DLLEXPORT KSPView(KSP ksp,PetscViewer viewer)
48 {
49   const KSPType  type;
50   PetscErrorCode ierr;
51   PetscTruth     iascii;
52 
53   PetscFunctionBegin;
54   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
55   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)ksp)->comm);
56   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
57   PetscCheckSameComm(ksp,1,viewer,2);
58 
59   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
60   if (iascii) {
61     ierr = KSPGetType(ksp,&type);CHKERRQ(ierr);
62     if (((PetscObject)ksp)->prefix) {
63       ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:(%s)\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr);
64     } else {
65       ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:\n");CHKERRQ(ierr);
66     }
67     if (type) {
68       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
69     } else {
70       ierr = PetscViewerASCIIPrintf(viewer,"  type: not yet set\n");CHKERRQ(ierr);
71     }
72     if (ksp->ops->view) {
73       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
74       ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);
75       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
76     }
77     if (ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, initial guess is zero\n",ksp->max_it);CHKERRQ(ierr);}
78     else                 {ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D\n", ksp->max_it);CHKERRQ(ierr);}
79     if (ksp->guess_knoll) {ierr = PetscViewerASCIIPrintf(viewer,"  using preconditioner applied to right hand side for initial guess\n");CHKERRQ(ierr);}
80     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances:  relative=%G, absolute=%G, divergence=%G\n",ksp->rtol,ksp->abstol,ksp->divtol);CHKERRQ(ierr);
81     if (ksp->pc_side == PC_RIGHT)          {ierr = PetscViewerASCIIPrintf(viewer,"  right preconditioning\n");CHKERRQ(ierr);}
82     else if (ksp->pc_side == PC_SYMMETRIC) {ierr = PetscViewerASCIIPrintf(viewer,"  symmetric preconditioning\n");CHKERRQ(ierr);}
83     else                                   {ierr = PetscViewerASCIIPrintf(viewer,"  left preconditioning\n");CHKERRQ(ierr);}
84     if (ksp->guess) {ierr = PetscViewerASCIIPrintf(viewer,"  using Fischers initial guess method %D with size %D\n",ksp->guess->method,ksp->guess->maxl);CHKERRQ(ierr);}
85     if (ksp->dscale) {ierr = PetscViewerASCIIPrintf(viewer,"  diagonally scaled system\n");CHKERRQ(ierr);}
86     if (ksp->nullsp) {ierr = PetscViewerASCIIPrintf(viewer,"  has attached null space\n");CHKERRQ(ierr);}
87   } else {
88     if (ksp->ops->view) {
89       ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr);
90     }
91   }
92   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
93   ierr = PCView(ksp->pc,viewer);CHKERRQ(ierr);
94   PetscFunctionReturn(0);
95 }
96 
97 /*
98    Contains the list of registered KSP routines
99 */
100 PetscFList KSPList = 0;
101 
102 #undef __FUNCT__
103 #define __FUNCT__ "KSPSetNormType"
104 /*@
105    KSPSetNormType - Sets the norm that is used for convergence testing.
106 
107    Collective on KSP
108 
109    Input Parameter:
110 +  ksp - Krylov solver context
111 -  normtype - one of
112 $   KSP_NORM_NO - skips computing the norm, this should only be used if you are using
113 $                 the Krylov method as a smoother with a fixed small number of iterations.
114 $                 Implicitly sets KSPSkipConverged as KSP convergence test.
115 $                 Supported only by CG, Richardson, Bi-CG-stab, CR, and CGS methods.
116 $   KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm
117 $                 of the preconditioned residual
118 $   KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual, supported only by
119 $                 CG, CHEBYCHEV, and RICHARDSON, automatically true for right (see KSPSetPreconditioningSide())
120 $                 preconditioning..
121 $   KSP_NORM_NATURAL - supported  by cg, cr, and cgs
122 
123 
124    Options Database Key:
125 .   -ksp_norm_type <none,preconditioned,unpreconditioned,natural>
126 
127    Notes:
128    Currently only works with the CG, Richardson, Bi-CG-stab, CR, and CGS methods.
129 
130    Level: advanced
131 
132 .keywords: KSP, create, context, norms
133 
134 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged()
135 @*/
136 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNormType(KSP ksp,KSPNormType normtype)
137 {
138   PetscErrorCode ierr;
139 
140   PetscFunctionBegin;
141   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
142   ksp->normtype = normtype;
143   if (normtype == KSP_NORM_NO) {
144     ierr = KSPSetConvergenceTest(ksp,KSPSkipConverged,0,0);CHKERRQ(ierr);
145     ierr = PetscInfo(ksp,"Warning: setting KSPNormType to skip computing the norm\n\
146  KSP convergence test is implicitly set to KSPSkipConverged\n");CHKERRQ(ierr);
147   }
148   PetscFunctionReturn(0);
149 }
150 
151 #undef __FUNCT__
152 #define __FUNCT__ "KSPSetCheckNormIteration"
153 /*@
154    KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be
155      computed and used in the convergence test.
156 
157    Collective on KSP
158 
159    Input Parameter:
160 +  ksp - Krylov solver context
161 -  it  - use -1 to check at all iterations
162 
163    Notes:
164    Currently only works with KSPCG, KSPBCGS and KSPIBCGS
165 
166    Use KSPSetNormType(ksp,KSP_NORM_NO) to never check the norm
167 
168    On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example,
169     -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged).
170    Level: advanced
171 
172 .keywords: KSP, create, context, norms
173 
174 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType()
175 @*/
176 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetCheckNormIteration(KSP ksp,PetscInt it)
177 {
178   PetscFunctionBegin;
179   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
180   ksp->chknorm = it;
181   PetscFunctionReturn(0);
182 }
183 
184 #undef __FUNCT__
185 #define __FUNCT__ "KSPGetNormType"
186 /*@
187    KSPGetNormType - Sets the norm that is used for convergence testing.
188 
189    Not Collective
190 
191    Input Parameter:
192 .  ksp - Krylov solver context
193 
194    Output Parameter:
195 .  normtype - norm that is used for convergence testing
196 
197    Level: advanced
198 
199 .keywords: KSP, create, context, norms
200 
201 .seealso: KSPNormType, KSPSetNormType(), KSPSkipConverged()
202 @*/
203 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNormType(KSP ksp, KSPNormType *normtype) {
204   PetscFunctionBegin;
205   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
206   PetscValidPointer(normtype, 2);
207   *normtype = ksp->normtype;
208   PetscFunctionReturn(0);
209 }
210 
211 #if 0
212 #undef __FUNCT__
213 #define __FUNCT__ "KSPPublish_Petsc"
214 static PetscErrorCode KSPPublish_Petsc(PetscObject obj)
215 {
216   PetscFunctionBegin;
217   PetscFunctionReturn(0);
218 }
219 #endif
220 
221 #undef __FUNCT__
222 #define __FUNCT__ "KSPSetOperators"
223 /*@
224    KSPSetOperators - Sets the matrix associated with the linear system
225    and a (possibly) different one associated with the preconditioner.
226 
227    Collective on KSP and Mat
228 
229    Input Parameters:
230 +  ksp - the KSP context
231 .  Amat - the matrix associated with the linear system
232 .  Pmat - the matrix to be used in constructing the preconditioner, usually the
233           same as Amat.
234 -  flag - flag indicating information about the preconditioner matrix structure
235    during successive linear solves.  This flag is ignored the first time a
236    linear system is solved, and thus is irrelevant when solving just one linear
237    system.
238 
239    Notes:
240    The flag can be used to eliminate unnecessary work in the preconditioner
241    during the repeated solution of linear systems of the same size.  The
242    available options are
243 $    SAME_PRECONDITIONER -
244 $      Pmat is identical during successive linear solves.
245 $      This option is intended for folks who are using
246 $      different Amat and Pmat matrices and want to reuse the
247 $      same preconditioner matrix.  For example, this option
248 $      saves work by not recomputing incomplete factorization
249 $      for ILU/ICC preconditioners.
250 $    SAME_NONZERO_PATTERN -
251 $      Pmat has the same nonzero structure during
252 $      successive linear solves.
253 $    DIFFERENT_NONZERO_PATTERN -
254 $      Pmat does not have the same nonzero structure.
255 
256     Passing a PETSC_NULL for Amat or Pmat removes the matrix that is currently used.
257 
258     If you wish to replace either Amat or Pmat but leave the other one untouched then
259     first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference()
260     on it and then pass it back in in your call to KSPSetOperators().
261 
262     Caution:
263     If you specify SAME_NONZERO_PATTERN, PETSc believes your assertion
264     and does not check the structure of the matrix.  If you erroneously
265     claim that the structure is the same when it actually is not, the new
266     preconditioner will not function correctly.  Thus, use this optimization
267     feature carefully!
268 
269     If in doubt about whether your preconditioner matrix has changed
270     structure or not, use the flag DIFFERENT_NONZERO_PATTERN.
271 
272     Level: beginner
273 
274    Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators
275       are created in PC and returned to the user. In this case, if both operators
276       mat and pmat are requested, two DIFFERENT operators will be returned. If
277       only one is requested both operators in the PC will be the same (i.e. as
278       if one had called KSP/PCSetOperators() with the same argument for both Mats).
279       The user must set the sizes of the returned matrices and their type etc just
280       as if the user created them with MatCreate(). For example,
281 
282 $         KSP/PCGetOperators(ksp/pc,&mat,PETSC_NULL,PETSC_NULL); is equivalent to
283 $           set size, type, etc of mat
284 
285 $         MatCreate(comm,&mat);
286 $         KSP/PCSetOperators(ksp/pc,mat,mat,SAME_NONZERO_PATTERN);
287 $         PetscObjectDereference((PetscObject)mat);
288 $           set size, type, etc of mat
289 
290      and
291 
292 $         KSP/PCGetOperators(ksp/pc,&mat,&pmat,PETSC_NULL); is equivalent to
293 $           set size, type, etc of mat and pmat
294 
295 $         MatCreate(comm,&mat);
296 $         MatCreate(comm,&pmat);
297 $         KSP/PCSetOperators(ksp/pc,mat,pmat,SAME_NONZERO_PATTERN);
298 $         PetscObjectDereference((PetscObject)mat);
299 $         PetscObjectDereference((PetscObject)pmat);
300 $           set size, type, etc of mat and pmat
301 
302     The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy
303     of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely
304     managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look
305     at this is when you create a SNES you do not NEED to create a KSP and attach it to
306     the SNES object (the SNES object manages it for you). Similarly when you create a KSP
307     you do not need to attach a PC to it (the KSP object manages the PC object for you).
308     Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when
309     it can be created for you?
310 
311 .keywords: KSP, set, operators, matrix, preconditioner, linear system
312 
313 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators()
314 @*/
315 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat,MatStructure flag)
316 {
317   PetscErrorCode ierr;
318 
319   PetscFunctionBegin;
320   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
321   if (Amat) PetscValidHeaderSpecific(Amat,MAT_COOKIE,2);
322   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_COOKIE,3);
323   if (Amat) PetscCheckSameComm(ksp,1,Amat,2);
324   if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3);
325   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
326   ierr = PCSetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr);
327   if (ksp->setupcalled > 1) ksp->setupcalled = 1;  /* so that next solve call will call setup */
328   if (ksp->guess) {
329     ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr);
330   }
331   PetscFunctionReturn(0);
332 }
333 
334 #undef __FUNCT__
335 #define __FUNCT__ "KSPGetOperators"
336 /*@
337    KSPGetOperators - Gets the matrix associated with the linear system
338    and a (possibly) different one associated with the preconditioner.
339 
340    Collective on KSP and Mat
341 
342    Input Parameter:
343 .  ksp - the KSP context
344 
345    Output Parameters:
346 +  Amat - the matrix associated with the linear system
347 .  Pmat - the matrix to be used in constructing the preconditioner, usually the
348           same as Amat.
349 -  flag - flag indicating information about the preconditioner matrix structure
350    during successive linear solves.  This flag is ignored the first time a
351    linear system is solved, and thus is irrelevant when solving just one linear
352    system.
353 
354     Level: intermediate
355 
356 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system
357 
358 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet()
359 @*/
360 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag)
361 {
362   PetscErrorCode ierr;
363 
364   PetscFunctionBegin;
365   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
366   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
367   ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr);
368   PetscFunctionReturn(0);
369 }
370 
371 #undef __FUNCT__
372 #define __FUNCT__ "KSPGetOperatorsSet"
373 /*@C
374    KSPGetOperatorsSet - Determines if the matrix associated with the linear system and
375    possibly a different one associated with the preconditioner have been set in the KSP.
376 
377    Not collective, though the results on all processes should be the same
378 
379    Input Parameter:
380 .  pc - the preconditioner context
381 
382    Output Parameters:
383 +  mat - the matrix associated with the linear system was set
384 -  pmat - matrix associated with the preconditioner was set, usually the same
385 
386    Level: intermediate
387 
388 .keywords: KSP, get, operators, matrix, linear system
389 
390 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet()
391 @*/
392 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperatorsSet(KSP ksp,PetscTruth *mat,PetscTruth *pmat)
393 {
394   PetscErrorCode ierr;
395 
396   PetscFunctionBegin;
397   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
398   if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);}
399   ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr);
400   PetscFunctionReturn(0);
401 }
402 
403 #undef __FUNCT__
404 #define __FUNCT__ "KSPCreate"
405 /*@
406    KSPCreate - Creates the default KSP context.
407 
408    Collective on MPI_Comm
409 
410    Input Parameter:
411 .  comm - MPI communicator
412 
413    Output Parameter:
414 .  ksp - location to put the KSP context
415 
416    Notes:
417    The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt
418    orthogonalization.
419 
420    Level: beginner
421 
422 .keywords: KSP, create, context
423 
424 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP
425 @*/
426 PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate(MPI_Comm comm,KSP *inksp)
427 {
428   KSP            ksp;
429   PetscErrorCode ierr;
430   void           *ctx;
431 
432   PetscFunctionBegin;
433   PetscValidPointer(inksp,2);
434   *inksp = 0;
435 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
436   ierr = KSPInitializePackage(PETSC_NULL);CHKERRQ(ierr);
437 #endif
438 
439   ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_COOKIE,-1,"KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr);
440 
441   ksp->max_it        = 10000;
442   ksp->pc_side       = PC_LEFT;
443   ksp->rtol          = 1.e-5;
444   ksp->abstol        = 1.e-50;
445   ksp->divtol        = 1.e4;
446 
447   ksp->chknorm             = -1;
448   ksp->normtype            = KSP_NORM_PRECONDITIONED;
449   ksp->rnorm               = 0.0;
450   ksp->its                 = 0;
451   ksp->guess_zero          = PETSC_TRUE;
452   ksp->calc_sings          = PETSC_FALSE;
453   ksp->res_hist            = PETSC_NULL;
454   ksp->res_hist_alloc      = PETSC_NULL;
455   ksp->res_hist_len        = 0;
456   ksp->res_hist_max        = 0;
457   ksp->res_hist_reset      = PETSC_TRUE;
458   ksp->numbermonitors      = 0;
459 
460   ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr);
461   ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr);
462   ksp->ops->buildsolution  = KSPDefaultBuildSolution;
463   ksp->ops->buildresidual  = KSPDefaultBuildResidual;
464 
465   ksp->vec_sol         = 0;
466   ksp->vec_rhs         = 0;
467   ksp->pc              = 0;
468   ksp->data            = 0;
469   ksp->nwork           = 0;
470   ksp->work            = 0;
471   ksp->reason          = KSP_CONVERGED_ITERATING;
472   ksp->setupcalled     = 0;
473 
474   ierr = PetscPublishAll(ksp);CHKERRQ(ierr);
475   *inksp = ksp;
476   PetscFunctionReturn(0);
477 }
478 
479 #undef __FUNCT__
480 #define __FUNCT__ "KSPSetType"
481 /*@C
482    KSPSetType - Builds KSP for a particular solver.
483 
484    Collective on KSP
485 
486    Input Parameters:
487 +  ksp      - the Krylov space context
488 -  type - a known method
489 
490    Options Database Key:
491 .  -ksp_type  <method> - Sets the method; use -help for a list
492     of available methods (for instance, cg or gmres)
493 
494    Notes:
495    See "petsc/include/petscksp.h" for available methods (for instance,
496    KSPCG or KSPGMRES).
497 
498   Normally, it is best to use the KSPSetFromOptions() command and
499   then set the KSP type from the options database rather than by using
500   this routine.  Using the options database provides the user with
501   maximum flexibility in evaluating the many different Krylov methods.
502   The KSPSetType() routine is provided for those situations where it
503   is necessary to set the iterative solver independently of the command
504   line or options database.  This might be the case, for example, when
505   the choice of iterative solver changes during the execution of the
506   program, and the user's application is taking responsibility for
507   choosing the appropriate method.  In other words, this routine is
508   not for beginners.
509 
510   Level: intermediate
511 
512 .keywords: KSP, set, method
513 
514 .seealso: PCSetType(), KSPType
515 
516 @*/
517 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetType(KSP ksp, const KSPType type)
518 {
519   PetscErrorCode ierr,(*r)(KSP);
520   PetscTruth     match;
521 
522   PetscFunctionBegin;
523   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
524   PetscValidCharPointer(type,2);
525 
526   ierr = PetscTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr);
527   if (match) PetscFunctionReturn(0);
528 
529   ierr =  PetscFListFind(KSPList,((PetscObject)ksp)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
530   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type);
531   /* Destroy the previous private KSP context */
532   if (ksp->ops->destroy) { ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); }
533   /* Reinitialize function pointers in KSPOps structure */
534   ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr);
535   ksp->ops->buildsolution = KSPDefaultBuildSolution;
536   ksp->ops->buildresidual = KSPDefaultBuildResidual;
537   /* Call the KSPCreate_XXX routine for this particular Krylov solver */
538   ksp->setupcalled = 0;
539   ierr = (*r)(ksp);CHKERRQ(ierr);
540   ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr);
541   PetscFunctionReturn(0);
542 }
543 
544 #undef __FUNCT__
545 #define __FUNCT__ "KSPRegisterDestroy"
546 /*@
547    KSPRegisterDestroy - Frees the list of KSP methods that were
548    registered by KSPRegisterDynamic().
549 
550    Not Collective
551 
552    Level: advanced
553 
554 .keywords: KSP, register, destroy
555 
556 .seealso: KSPRegisterDynamic(), KSPRegisterAll()
557 @*/
558 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegisterDestroy(void)
559 {
560   PetscErrorCode ierr;
561 
562   PetscFunctionBegin;
563   ierr = PetscFListDestroy(&KSPList);CHKERRQ(ierr);
564   KSPRegisterAllCalled = PETSC_FALSE;
565   PetscFunctionReturn(0);
566 }
567 
568 #undef __FUNCT__
569 #define __FUNCT__ "KSPGetType"
570 /*@C
571    KSPGetType - Gets the KSP type as a string from the KSP object.
572 
573    Not Collective
574 
575    Input Parameter:
576 .  ksp - Krylov context
577 
578    Output Parameter:
579 .  name - name of KSP method
580 
581    Level: intermediate
582 
583 .keywords: KSP, get, method, name
584 
585 .seealso: KSPSetType()
586 @*/
587 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetType(KSP ksp,const KSPType *type)
588 {
589   PetscFunctionBegin;
590   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
591   PetscValidPointer(type,2);
592   *type = ((PetscObject)ksp)->type_name;
593   PetscFunctionReturn(0);
594 }
595 
596 #undef __FUNCT__
597 #define __FUNCT__ "KSPRegister"
598 /*@C
599   KSPRegister - See KSPRegisterDynamic()
600 
601   Level: advanced
602 @*/
603 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP))
604 {
605   PetscErrorCode ierr;
606   char           fullname[PETSC_MAX_PATH_LEN];
607 
608   PetscFunctionBegin;
609   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
610   ierr = PetscFListAdd(&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
611   PetscFunctionReturn(0);
612 }
613 
614 #undef __FUNCT__
615 #define __FUNCT__ "KSPSetNullSpace"
616 /*@
617   KSPSetNullSpace - Sets the null space of the operator
618 
619   Collective on KSP
620 
621   Input Parameters:
622 +  ksp - the Krylov space object
623 -  nullsp - the null space of the operator
624 
625   Level: advanced
626 
627 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace()
628 @*/
629 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNullSpace(KSP ksp,MatNullSpace nullsp)
630 {
631   PetscErrorCode ierr;
632 
633   PetscFunctionBegin;
634   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
635   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE,2);
636   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
637   if (ksp->nullsp) { ierr = MatNullSpaceDestroy(ksp->nullsp);CHKERRQ(ierr); }
638   ksp->nullsp = nullsp;
639   PetscFunctionReturn(0);
640 }
641 
642 #undef __FUNCT__
643 #define __FUNCT__ "KSPGetNullSpace"
644 /*@
645   KSPGetNullSpace - Gets the null space of the operator
646 
647   Collective on KSP
648 
649   Input Parameters:
650 +  ksp - the Krylov space object
651 -  nullsp - the null space of the operator
652 
653   Level: advanced
654 
655 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace()
656 @*/
657 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp)
658 {
659   PetscFunctionBegin;
660   PetscValidHeaderSpecific(ksp,KSP_COOKIE,1);
661   PetscValidPointer(nullsp,2);
662   *nullsp = ksp->nullsp;
663   PetscFunctionReturn(0);
664 }
665 
666