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