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