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