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