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