Actual source code: itfunc.c
1: /*
2: Interface KSP routines that the user calls.
3: */
5: #include <petsc/private/kspimpl.h>
6: #include <petsc/private/matimpl.h>
7: #include <petscdm.h>
9: PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
10: {
13: PetscViewerPushFormat(viewer, format);
14: PetscObjectView(obj, viewer);
15: PetscViewerPopFormat(viewer);
16: return(0);
17: }
19: /*@
20: KSPComputeExtremeSingularValues - Computes the extreme singular values
21: for the preconditioned operator. Called after or during KSPSolve().
23: Not Collective
25: Input Parameter:
26: . ksp - iterative context obtained from KSPCreate()
28: Output Parameters:
29: . emin, emax - extreme singular values
31: Options Database Keys:
32: . -ksp_view_singularvalues - compute extreme singular values and print when KSPSolve completes.
34: Notes:
35: One must call KSPSetComputeSingularValues() before calling KSPSetUp()
36: (or use the option -ksp_view_eigenvalues) in order for this routine to work correctly.
38: Many users may just want to use the monitoring routine
39: KSPMonitorSingularValue() (which can be set with option -ksp_monitor_singular_value)
40: to print the extreme singular values at each iteration of the linear solve.
42: Estimates of the smallest singular value may be very inaccurate, especially if the Krylov method has not converged.
43: The largest singular value is usually accurate to within a few percent if the method has converged, but is still not
44: intended for eigenanalysis.
46: Disable restarts if using KSPGMRES, otherwise this estimate will only be using those iterations after the last
47: restart. See KSPGMRESSetRestart() for more details.
49: Level: advanced
51: .seealso: KSPSetComputeSingularValues(), KSPMonitorSingularValue(), KSPComputeEigenvalues(), KSP
52: @*/
53: PetscErrorCode KSPComputeExtremeSingularValues(KSP ksp,PetscReal *emax,PetscReal *emin)
54: {
61: if (!ksp->calc_sings) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_WRONGSTATE,"Singular values not requested before KSPSetUp()");
63: if (ksp->ops->computeextremesingularvalues) {
64: (*ksp->ops->computeextremesingularvalues)(ksp,emax,emin);
65: } else {
66: *emin = -1.0;
67: *emax = -1.0;
68: }
69: return(0);
70: }
72: /*@
73: KSPComputeEigenvalues - Computes the extreme eigenvalues for the
74: preconditioned operator. Called after or during KSPSolve().
76: Not Collective
78: Input Parameters:
79: + ksp - iterative context obtained from KSPCreate()
80: - n - size of arrays r and c. The number of eigenvalues computed (neig) will, in
81: general, be less than this.
83: Output Parameters:
84: + r - real part of computed eigenvalues, provided by user with a dimension of at least n
85: . c - complex part of computed eigenvalues, provided by user with a dimension of at least n
86: - neig - actual number of eigenvalues computed (will be less than or equal to n)
88: Options Database Keys:
89: . -ksp_view_eigenvalues - Prints eigenvalues to stdout
91: Notes:
92: The number of eigenvalues estimated depends on the size of the Krylov space
93: generated during the KSPSolve() ; for example, with
94: CG it corresponds to the number of CG iterations, for GMRES it is the number
95: of GMRES iterations SINCE the last restart. Any extra space in r[] and c[]
96: will be ignored.
98: KSPComputeEigenvalues() does not usually provide accurate estimates; it is
99: intended only for assistance in understanding the convergence of iterative
100: methods, not for eigenanalysis. For accurate computation of eigenvalues we recommend using
101: the excellent package SLEPc.
103: One must call KSPSetComputeEigenvalues() before calling KSPSetUp()
104: in order for this routine to work correctly.
106: Many users may just want to use the monitoring routine
107: KSPMonitorSingularValue() (which can be set with option -ksp_monitor_singular_value)
108: to print the singular values at each iteration of the linear solve.
110: Level: advanced
112: .seealso: KSPSetComputeSingularValues(), KSPMonitorSingularValue(), KSPComputeExtremeSingularValues(), KSP
113: @*/
114: PetscErrorCode KSPComputeEigenvalues(KSP ksp,PetscInt n,PetscReal r[],PetscReal c[],PetscInt *neig)
115: {
122: if (n<0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Requested < 0 Eigenvalues");
124: if (!ksp->calc_sings) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_WRONGSTATE,"Eigenvalues not requested before KSPSetUp()");
126: if (n && ksp->ops->computeeigenvalues) {
127: (*ksp->ops->computeeigenvalues)(ksp,n,r,c,neig);
128: } else {
129: *neig = 0;
130: }
131: return(0);
132: }
134: /*@
135: KSPComputeRitz - Computes the Ritz or harmonic Ritz pairs associated to the
136: smallest or largest in modulus, for the preconditioned operator.
137: Called after KSPSolve().
139: Not Collective
141: Input Parameters:
142: + ksp - iterative context obtained from KSPCreate()
143: . ritz - PETSC_TRUE or PETSC_FALSE for ritz pairs or harmonic Ritz pairs, respectively
144: - small - PETSC_TRUE or PETSC_FALSE for smallest or largest (harmonic) Ritz values, respectively
146: Input/Output Parameter:
147: . nrit - number of (harmonic) Ritz pairs to compute; on output,
148: actual number of computed (harmonic) Ritz pairs
150: Output Parameters:
151: + S - multidimensional vector with Ritz vectors
152: . tetar - real part of the Ritz values
153: - tetai - imaginary part of the Ritz values
155: Notes:
156: -For GMRES, the (harmonic) Ritz pairs are computed from the Hessenberg matrix obtained during
157: the last complete cycle, or obtained at the end of the solution if the method is stopped before
158: a restart. Then, the number of actual (harmonic) Ritz pairs computed is less or equal to the restart
159: parameter for GMRES if a complete cycle has been performed or less or equal to the number of GMRES
160: iterations.
161: -Moreover, for real matrices, the (harmonic) Ritz pairs are possibly complex-valued. In such a case,
162: the routine selects the complex (harmonic) Ritz value and its conjugate, and two successive columns of S
163: are equal to the real and the imaginary parts of the associated vectors.
164: -the (harmonic) Ritz pairs are given in order of increasing (harmonic) Ritz values in modulus
165: -this is currently not implemented when PETSc is built with complex numbers
167: One must call KSPSetComputeRitz() before calling KSPSetUp()
168: in order for this routine to work correctly.
170: Level: advanced
172: .seealso: KSPSetComputeRitz(), KSP
173: @*/
174: PetscErrorCode KSPComputeRitz(KSP ksp,PetscBool ritz,PetscBool small,PetscInt *nrit,Vec S[],PetscReal tetar[],PetscReal tetai[])
175: {
180: if (!ksp->calc_ritz) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_WRONGSTATE,"Ritz pairs not requested before KSPSetUp()");
181: if (ksp->ops->computeritz) {(*ksp->ops->computeritz)(ksp,ritz,small,nrit,S,tetar,tetai);}
182: return(0);
183: }
184: /*@
185: KSPSetUpOnBlocks - Sets up the preconditioner for each block in
186: the block Jacobi, block Gauss-Seidel, and overlapping Schwarz
187: methods.
189: Collective on ksp
191: Input Parameter:
192: . ksp - the KSP context
194: Notes:
195: KSPSetUpOnBlocks() is a routine that the user can optinally call for
196: more precise profiling (via -log_view) of the setup phase for these
197: block preconditioners. If the user does not call KSPSetUpOnBlocks(),
198: it will automatically be called from within KSPSolve().
200: Calling KSPSetUpOnBlocks() is the same as calling PCSetUpOnBlocks()
201: on the PC context within the KSP context.
203: Level: advanced
205: .seealso: PCSetUpOnBlocks(), KSPSetUp(), PCSetUp(), KSP
206: @*/
207: PetscErrorCode KSPSetUpOnBlocks(KSP ksp)
208: {
209: PC pc;
211: PCFailedReason pcreason;
215: KSPGetPC(ksp,&pc);
216: PCSetUpOnBlocks(pc);
217: PCGetFailedReasonRank(pc,&pcreason);
218: /* TODO: this code was wrong and is still wrong, there is no way to propagate the failure to all processes; their is no code to handle a ksp->reason on only some ranks */
219: if (pcreason) {
220: ksp->reason = KSP_DIVERGED_PC_FAILED;
221: }
222: return(0);
223: }
225: /*@
226: KSPSetReusePreconditioner - reuse the current preconditioner, do not construct a new one even if the operator changes
228: Collective on ksp
230: Input Parameters:
231: + ksp - iterative context obtained from KSPCreate()
232: - flag - PETSC_TRUE to reuse the current preconditioner
234: Level: intermediate
236: .seealso: KSPCreate(), KSPSolve(), KSPDestroy(), PCSetReusePreconditioner(), KSP
237: @*/
238: PetscErrorCode KSPSetReusePreconditioner(KSP ksp,PetscBool flag)
239: {
240: PC pc;
245: KSPGetPC(ksp,&pc);
246: PCSetReusePreconditioner(pc,flag);
247: return(0);
248: }
250: /*@
251: KSPGetReusePreconditioner - Determines if the KSP reuses the current preconditioner even if the operator in the preconditioner has changed.
253: Collective on ksp
255: Input Parameters:
256: . ksp - iterative context obtained from KSPCreate()
258: Output Parameters:
259: . flag - the boolean flag
261: Level: intermediate
263: .seealso: KSPCreate(), KSPSolve(), KSPDestroy(), KSPSetReusePreconditioner(), KSP
264: @*/
265: PetscErrorCode KSPGetReusePreconditioner(KSP ksp,PetscBool *flag)
266: {
272: *flag = PETSC_FALSE;
273: if (ksp->pc) {
274: PCGetReusePreconditioner(ksp->pc,flag);
275: }
276: return(0);
277: }
279: /*@
280: KSPSetSkipPCSetFromOptions - prevents KSPSetFromOptions() from call PCSetFromOptions(). This is used if the same PC is shared by more than one KSP so its options are not resetable for each KSP
282: Collective on ksp
284: Input Parameters:
285: + ksp - iterative context obtained from KSPCreate()
286: - flag - PETSC_TRUE to skip calling the PCSetFromOptions()
288: Level: intermediate
290: .seealso: KSPCreate(), KSPSolve(), KSPDestroy(), PCSetReusePreconditioner(), KSP
291: @*/
292: PetscErrorCode KSPSetSkipPCSetFromOptions(KSP ksp,PetscBool flag)
293: {
296: ksp->skippcsetfromoptions = flag;
297: return(0);
298: }
300: /*@
301: KSPSetUp - Sets up the internal data structures for the
302: later use of an iterative solver.
304: Collective on ksp
306: Input Parameter:
307: . ksp - iterative context obtained from KSPCreate()
309: Level: developer
311: .seealso: KSPCreate(), KSPSolve(), KSPDestroy(), KSP
312: @*/
313: PetscErrorCode KSPSetUp(KSP ksp)
314: {
316: Mat A,B;
317: Mat mat,pmat;
318: MatNullSpace nullsp;
319: PCFailedReason pcreason;
324: /* reset the convergence flag from the previous solves */
325: ksp->reason = KSP_CONVERGED_ITERATING;
327: if (!((PetscObject)ksp)->type_name) {
328: KSPSetType(ksp,KSPGMRES);
329: }
330: KSPSetUpNorms_Private(ksp,PETSC_TRUE,&ksp->normtype,&ksp->pc_side);
332: if (ksp->dmActive && !ksp->setupstage) {
333: /* first time in so build matrix and vector data structures using DM */
334: if (!ksp->vec_rhs) {DMCreateGlobalVector(ksp->dm,&ksp->vec_rhs);}
335: if (!ksp->vec_sol) {DMCreateGlobalVector(ksp->dm,&ksp->vec_sol);}
336: DMCreateMatrix(ksp->dm,&A);
337: KSPSetOperators(ksp,A,A);
338: PetscObjectDereference((PetscObject)A);
339: }
341: if (ksp->dmActive) {
342: DMKSP kdm;
343: DMGetDMKSP(ksp->dm,&kdm);
345: if (kdm->ops->computeinitialguess && ksp->setupstage != KSP_SETUP_NEWRHS) {
346: /* only computes initial guess the first time through */
347: (*kdm->ops->computeinitialguess)(ksp,ksp->vec_sol,kdm->initialguessctx);
348: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
349: }
350: if (kdm->ops->computerhs) {
351: (*kdm->ops->computerhs)(ksp,ksp->vec_rhs,kdm->rhsctx);
352: }
354: if (ksp->setupstage != KSP_SETUP_NEWRHS) {
355: if (kdm->ops->computeoperators) {
356: KSPGetOperators(ksp,&A,&B);
357: (*kdm->ops->computeoperators)(ksp,A,B,kdm->operatorsctx);
358: } else SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_WRONGSTATE,"You called KSPSetDM() but did not use DMKSPSetComputeOperators() or KSPSetDMActive(ksp,PETSC_FALSE);");
359: }
360: }
362: if (ksp->setupstage == KSP_SETUP_NEWRHS) return(0);
363: PetscLogEventBegin(KSP_SetUp,ksp,ksp->vec_rhs,ksp->vec_sol,0);
365: switch (ksp->setupstage) {
366: case KSP_SETUP_NEW:
367: (*ksp->ops->setup)(ksp);
368: break;
369: case KSP_SETUP_NEWMATRIX: { /* This should be replaced with a more general mechanism */
370: if (ksp->setupnewmatrix) {
371: (*ksp->ops->setup)(ksp);
372: }
373: } break;
374: default: break;
375: }
377: if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
378: PCGetOperators(ksp->pc,&mat,&pmat);
379: /* scale the matrix if requested */
380: if (ksp->dscale) {
381: PetscScalar *xx;
382: PetscInt i,n;
383: PetscBool zeroflag = PETSC_FALSE;
384: if (!ksp->pc) {KSPGetPC(ksp,&ksp->pc);}
385: if (!ksp->diagonal) { /* allocate vector to hold diagonal */
386: MatCreateVecs(pmat,&ksp->diagonal,NULL);
387: }
388: MatGetDiagonal(pmat,ksp->diagonal);
389: VecGetLocalSize(ksp->diagonal,&n);
390: VecGetArray(ksp->diagonal,&xx);
391: for (i=0; i<n; i++) {
392: if (xx[i] != 0.0) xx[i] = 1.0/PetscSqrtReal(PetscAbsScalar(xx[i]));
393: else {
394: xx[i] = 1.0;
395: zeroflag = PETSC_TRUE;
396: }
397: }
398: VecRestoreArray(ksp->diagonal,&xx);
399: if (zeroflag) {
400: PetscInfo(ksp,"Zero detected in diagonal of matrix, using 1 at those locations\n");
401: }
402: MatDiagonalScale(pmat,ksp->diagonal,ksp->diagonal);
403: if (mat != pmat) {MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);}
404: ksp->dscalefix2 = PETSC_FALSE;
405: }
406: PetscLogEventEnd(KSP_SetUp,ksp,ksp->vec_rhs,ksp->vec_sol,0);
407: PCSetErrorIfFailure(ksp->pc,ksp->errorifnotconverged);
408: PCSetUp(ksp->pc);
409: PCGetFailedReasonRank(ksp->pc,&pcreason);
410: /* TODO: this code was wrong and is still wrong, there is no way to propagate the failure to all processes; their is no code to handle a ksp->reason on only some ranks */
411: if (pcreason) {
412: ksp->reason = KSP_DIVERGED_PC_FAILED;
413: }
415: MatGetNullSpace(mat,&nullsp);
416: if (nullsp) {
417: PetscBool test = PETSC_FALSE;
418: PetscOptionsGetBool(((PetscObject)ksp)->options,((PetscObject)ksp)->prefix,"-ksp_test_null_space",&test,NULL);
419: if (test) {
420: MatNullSpaceTest(nullsp,mat,NULL);
421: }
422: }
423: ksp->setupstage = KSP_SETUP_NEWRHS;
424: return(0);
425: }
427: /*@C
428: KSPConvergedReasonView - Displays the reason a KSP solve converged or diverged to a viewer
430: Collective on ksp
432: Parameter:
433: + ksp - iterative context obtained from KSPCreate()
434: - viewer - the viewer to display the reason
436: Options Database Keys:
437: + -ksp_converged_reason - print reason for converged or diverged, also prints number of iterations
438: - -ksp_converged_reason ::failed - only print reason and number of iterations when diverged
440: Notes:
441: To change the format of the output call PetscViewerPushFormat(viewer,format) before this call. Use PETSC_VIEWER_DEFAULT for the default,
442: use PETSC_VIEWER_FAILED to only display a reason if it fails.
444: Level: beginner
446: .seealso: KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPConvergedDefault(),
447: KSPSolveTranspose(), KSPGetIterationNumber(), KSP, KSPGetConvergedReason(), PetscViewerPushFormat(), PetscViewerPopFormat()
448: @*/
449: PetscErrorCode KSPConvergedReasonView(KSP ksp, PetscViewer viewer)
450: {
451: PetscErrorCode ierr;
452: PetscBool isAscii;
453: PetscViewerFormat format;
456: if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp));
457: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii);
458: if (isAscii) {
459: PetscViewerGetFormat(viewer, &format);
460: PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);
461: if (ksp->reason > 0 && format != PETSC_VIEWER_FAILED) {
462: if (((PetscObject) ksp)->prefix) {
463: PetscViewerASCIIPrintf(viewer,"Linear %s solve converged due to %s iterations %D\n",((PetscObject) ksp)->prefix,KSPConvergedReasons[ksp->reason],ksp->its);
464: } else {
465: PetscViewerASCIIPrintf(viewer,"Linear solve converged due to %s iterations %D\n",KSPConvergedReasons[ksp->reason],ksp->its);
466: }
467: } else if (ksp->reason <= 0) {
468: if (((PetscObject) ksp)->prefix) {
469: PetscViewerASCIIPrintf(viewer,"Linear %s solve did not converge due to %s iterations %D\n",((PetscObject) ksp)->prefix,KSPConvergedReasons[ksp->reason],ksp->its);
470: } else {
471: PetscViewerASCIIPrintf(viewer,"Linear solve did not converge due to %s iterations %D\n",KSPConvergedReasons[ksp->reason],ksp->its);
472: }
473: if (ksp->reason == KSP_DIVERGED_PC_FAILED) {
474: PCFailedReason reason;
475: PCGetFailedReason(ksp->pc,&reason);
476: PetscViewerASCIIPrintf(viewer," PC failed due to %s \n",PCFailedReasons[reason]);
477: }
478: }
479: PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);
480: }
481: return(0);
482: }
484: /*@C
485: KSPConvergedReasonViewSet - Sets an ADDITIONAL function that is to be used at the
486: end of the linear solver to display the convergence reason of the linear solver.
488: Logically Collective on KSP
490: Input Parameters:
491: + ksp - the KSP context
492: . f - the ksp converged reason view function
493: . vctx - [optional] user-defined context for private data for the
494: ksp converged reason view routine (use NULL if no context is desired)
495: - reasonviewdestroy - [optional] routine that frees reasonview context
496: (may be NULL)
498: Options Database Keys:
499: + -ksp_converged_reason - sets a default KSPConvergedReasonView()
500: - -ksp_converged_reason_view_cancel - cancels all converged reason viewers that have
501: been hardwired into a code by
502: calls to KSPConvergedReasonViewSet(), but
503: does not cancel those set via
504: the options database.
506: Notes:
507: Several different converged reason view routines may be set by calling
508: KSPConvergedReasonViewSet() multiple times; all will be called in the
509: order in which they were set.
511: Level: intermediate
513: .seealso: KSPConvergedReasonView(), KSPConvergedReasonViewCancel()
514: @*/
515: PetscErrorCode KSPConvergedReasonViewSet(KSP ksp,PetscErrorCode (*f)(KSP,void*),void *vctx,PetscErrorCode (*reasonviewdestroy)(void**))
516: {
517: PetscInt i;
519: PetscBool identical;
523: for (i=0; i<ksp->numberreasonviews;i++) {
524: PetscMonitorCompare((PetscErrorCode (*)(void))f,vctx,reasonviewdestroy,(PetscErrorCode (*)(void))ksp->reasonview[i],ksp->reasonviewcontext[i],ksp->reasonviewdestroy[i],&identical);
525: if (identical) return(0);
526: }
527: if (ksp->numberreasonviews >= MAXKSPREASONVIEWS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many KSP reasonview set");
528: ksp->reasonview[ksp->numberreasonviews] = f;
529: ksp->reasonviewdestroy[ksp->numberreasonviews] = reasonviewdestroy;
530: ksp->reasonviewcontext[ksp->numberreasonviews++] = (void*)vctx;
531: return(0);
532: }
534: /*@
535: KSPConvergedReasonViewCancel - Clears all the reasonview functions for a KSP object.
537: Collective on KSP
539: Input Parameter:
540: . ksp - iterative context obtained from KSPCreate()
542: Level: intermediate
544: .seealso: KSPCreate(), KSPDestroy(), KSPReset()
545: @*/
546: PetscErrorCode KSPConvergedReasonViewCancel(KSP ksp)
547: {
549: PetscInt i;
553: for (i=0; i<ksp->numberreasonviews; i++) {
554: if (ksp->reasonviewdestroy[i]) {
555: (*ksp->reasonviewdestroy[i])(&ksp->reasonviewcontext[i]);
556: }
557: }
558: ksp->numberreasonviews = 0;
559: return(0);
560: }
562: /*@
563: KSPConvergedReasonViewFromOptions - Processes command line options to determine if/how a KSPReason is to be viewed.
565: Collective on ksp
567: Input Parameters:
568: . ksp - the KSP object
570: Level: intermediate
572: .seealso: KSPConvergedReasonView()
573: @*/
574: PetscErrorCode KSPConvergedReasonViewFromOptions(KSP ksp)
575: {
576: PetscViewer viewer;
577: PetscBool flg;
578: PetscViewerFormat format;
579: PetscErrorCode ierr;
580: PetscInt i;
584: /* Call all user-provided reason review routines */
585: for (i=0; i<ksp->numberreasonviews; i++) {
586: (*ksp->reasonview[i])(ksp,ksp->reasonviewcontext[i]);
587: }
589: /* Call the default PETSc routine */
590: PetscOptionsGetViewer(PetscObjectComm((PetscObject)ksp),((PetscObject)ksp)->options,((PetscObject)ksp)->prefix,"-ksp_converged_reason",&viewer,&format,&flg);
591: if (flg) {
592: PetscViewerPushFormat(viewer,format);
593: KSPConvergedReasonView(ksp, viewer);
594: PetscViewerPopFormat(viewer);
595: PetscViewerDestroy(&viewer);
596: }
597: return(0);
598: }
600: /*@C
601: KSPConvergedRateView - Displays the reason a KSP solve converged or diverged to a viewer
603: Collective on ksp
605: Input Parameters:
606: + ksp - iterative context obtained from KSPCreate()
607: - viewer - the viewer to display the reason
609: Options Database Keys:
610: . -ksp_converged_rate - print reason for convergence or divergence and the convergence rate (or 0.0 for divergence)
612: Notes:
613: To change the format of the output, call PetscViewerPushFormat(viewer,format) before this call.
615: Suppose that the residual is reduced linearly, $r_k = c^k r_0$, which means $log r_k = log r_0 + k log c$. After linear regression,
616: the slope is $\log c$. The coefficient of determination is given by $1 - \frac{\sum_i (y_i - f(x_i))^2}{\sum_i (y_i - \bar y)}$,
617: see also https://en.wikipedia.org/wiki/Coefficient_of_determination
619: Level: intermediate
621: .seealso: KSPConvergedReasonView(), KSPGetConvergedRate(), KSPSetTolerances(), KSPConvergedDefault()
622: @*/
623: PetscErrorCode KSPConvergedRateView(KSP ksp, PetscViewer viewer)
624: {
625: PetscViewerFormat format;
626: PetscBool isAscii;
627: PetscReal rrate, rRsq, erate = 0.0, eRsq = 0.0;
628: PetscInt its;
629: const char *prefix, *reason = KSPConvergedReasons[ksp->reason];
630: PetscErrorCode ierr;
633: KSPGetOptionsPrefix(ksp, &prefix);
634: KSPGetIterationNumber(ksp, &its);
635: KSPComputeConvergenceRate(ksp, &rrate, &rRsq, &erate, &eRsq);
636: if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp));
637: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii);
638: if (isAscii) {
639: PetscViewerGetFormat(viewer, &format);
640: PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);
641: if (ksp->reason > 0) {
642: if (prefix) {PetscViewerASCIIPrintf(viewer, "Linear %s solve converged due to %s iterations %D", prefix, reason, its);}
643: else {PetscViewerASCIIPrintf(viewer, "Linear solve converged due to %s iterations %D", reason, its);}
644: PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
645: if (rRsq >= 0.0) {PetscViewerASCIIPrintf(viewer, " res rate %g R^2 %g", rrate, rRsq);}
646: if (eRsq >= 0.0) {PetscViewerASCIIPrintf(viewer, " error rate %g R^2 %g", erate, eRsq);}
647: PetscViewerASCIIPrintf(viewer, "\n");
648: PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
649: } else if (ksp->reason <= 0) {
650: if (prefix) {PetscViewerASCIIPrintf(viewer, "Linear %s solve did not converge due to %s iterations %D", prefix, reason, its);}
651: else {PetscViewerASCIIPrintf(viewer, "Linear solve did not converge due to %s iterations %D", reason, its);}
652: PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
653: if (rRsq >= 0.0) {PetscViewerASCIIPrintf(viewer, " res rate %g R^2 %g", rrate, rRsq);}
654: if (eRsq >= 0.0) {PetscViewerASCIIPrintf(viewer, " error rate %g R^2 %g", erate, eRsq);}
655: PetscViewerASCIIPrintf(viewer, "\n");
656: PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
657: if (ksp->reason == KSP_DIVERGED_PC_FAILED) {
658: PCFailedReason reason;
659: PCGetFailedReason(ksp->pc,&reason);
660: PetscViewerASCIIPrintf(viewer," PC failed due to %s \n",PCFailedReasons[reason]);
661: }
662: }
663: PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);
664: }
665: return(0);
666: }
668: #include <petscdraw.h>
670: static PetscErrorCode KSPViewEigenvalues_Internal(KSP ksp, PetscBool isExplicit, PetscViewer viewer, PetscViewerFormat format)
671: {
672: PetscReal *r, *c;
673: PetscInt n, i, neig;
674: PetscBool isascii, isdraw;
675: PetscMPIInt rank;
679: MPI_Comm_rank(PetscObjectComm((PetscObject) ksp), &rank);
680: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);
681: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);
682: if (isExplicit) {
683: VecGetSize(ksp->vec_sol,&n);
684: PetscMalloc2(n, &r, n, &c);
685: KSPComputeEigenvaluesExplicitly(ksp, n, r, c);
686: neig = n;
687: } else {
688: PetscInt nits;
690: KSPGetIterationNumber(ksp, &nits);
691: n = nits+2;
692: if (!nits) {PetscViewerASCIIPrintf(viewer, "Zero iterations in solver, cannot approximate any eigenvalues\n");return(0);}
693: PetscMalloc2(n, &r, n, &c);
694: KSPComputeEigenvalues(ksp, n, r, c, &neig);
695: }
696: if (isascii) {
697: PetscViewerASCIIPrintf(viewer, "%s computed eigenvalues\n", isExplicit ? "Explicitly" : "Iteratively");
698: for (i = 0; i < neig; ++i) {
699: if (c[i] >= 0.0) {PetscViewerASCIIPrintf(viewer, "%g + %gi\n", (double) r[i], (double) c[i]);}
700: else {PetscViewerASCIIPrintf(viewer, "%g - %gi\n", (double) r[i], -(double) c[i]);}
701: }
702: } else if (isdraw && rank == 0) {
703: PetscDraw draw;
704: PetscDrawSP drawsp;
706: if (format == PETSC_VIEWER_DRAW_CONTOUR) {
707: KSPPlotEigenContours_Private(ksp,neig,r,c);
708: } else {
709: if (!ksp->eigviewer) {PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,isExplicit ? "Explicitly Computed Eigenvalues" : "Iteratively Computed Eigenvalues",PETSC_DECIDE,PETSC_DECIDE,400,400,&ksp->eigviewer);}
710: PetscViewerDrawGetDraw(ksp->eigviewer,0,&draw);
711: PetscDrawSPCreate(draw,1,&drawsp);
712: PetscDrawSPReset(drawsp);
713: for (i = 0; i < neig; ++i) {PetscDrawSPAddPoint(drawsp,r+i,c+i);}
714: PetscDrawSPDraw(drawsp,PETSC_TRUE);
715: PetscDrawSPSave(drawsp);
716: PetscDrawSPDestroy(&drawsp);
717: }
718: }
719: PetscFree2(r, c);
720: return(0);
721: }
723: static PetscErrorCode KSPViewSingularvalues_Internal(KSP ksp, PetscViewer viewer, PetscViewerFormat format)
724: {
725: PetscReal smax, smin;
726: PetscInt nits;
727: PetscBool isascii;
731: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);
732: KSPGetIterationNumber(ksp, &nits);
733: if (!nits) {PetscViewerASCIIPrintf(viewer, "Zero iterations in solver, cannot approximate any singular values\n");return(0);}
734: KSPComputeExtremeSingularValues(ksp, &smax, &smin);
735: if (isascii) {PetscViewerASCIIPrintf(viewer, "Iteratively computed extreme singular values: max %g min %g max/min %g\n",(double)smax,(double)smin,(double)(smax/smin));}
736: return(0);
737: }
739: static PetscErrorCode KSPViewFinalResidual_Internal(KSP ksp, PetscViewer viewer, PetscViewerFormat format)
740: {
741: PetscBool isascii;
745: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);
746: if (ksp->dscale && !ksp->dscalefix) SETERRQ(PetscObjectComm((PetscObject) ksp), PETSC_ERR_ARG_WRONGSTATE, "Cannot compute final scale with -ksp_diagonal_scale except also with -ksp_diagonal_scale_fix");
747: if (isascii) {
748: Mat A;
749: Vec t;
750: PetscReal norm;
752: PCGetOperators(ksp->pc, &A, NULL);
753: VecDuplicate(ksp->vec_rhs, &t);
754: KSP_MatMult(ksp, A, ksp->vec_sol, t);
755: VecAYPX(t, -1.0, ksp->vec_rhs);
756: VecNorm(t, NORM_2, &norm);
757: VecDestroy(&t);
758: PetscViewerASCIIPrintf(viewer, "KSP final norm of residual %g\n", (double) norm);
759: }
760: return(0);
761: }
763: static PetscErrorCode KSPMonitorPauseFinal_Internal(KSP ksp)
764: {
765: PetscInt i;
769: if (!ksp->pauseFinal) return(0);
770: for (i = 0; i < ksp->numbermonitors; ++i) {
771: PetscViewerAndFormat *vf = (PetscViewerAndFormat *) ksp->monitorcontext[i];
772: PetscDraw draw;
773: PetscReal lpause;
775: if (!vf) continue;
776: if (vf->lg) {
778: if (((PetscObject) vf->lg)->classid != PETSC_DRAWLG_CLASSID) continue;
779: PetscDrawLGGetDraw(vf->lg, &draw);
780: PetscDrawGetPause(draw, &lpause);
781: PetscDrawSetPause(draw, -1.0);
782: PetscDrawPause(draw);
783: PetscDrawSetPause(draw, lpause);
784: } else {
785: PetscBool isdraw;
788: if (((PetscObject) vf->viewer)->classid != PETSC_VIEWER_CLASSID) continue;
789: PetscObjectTypeCompare((PetscObject) vf->viewer, PETSCVIEWERDRAW, &isdraw);
790: if (!isdraw) continue;
791: PetscViewerDrawGetDraw(vf->viewer, 0, &draw);
792: PetscDrawGetPause(draw, &lpause);
793: PetscDrawSetPause(draw, -1.0);
794: PetscDrawPause(draw);
795: PetscDrawSetPause(draw, lpause);
796: }
797: }
798: return(0);
799: }
801: static PetscErrorCode KSPSolve_Private(KSP ksp,Vec b,Vec x)
802: {
804: PetscBool flg = PETSC_FALSE,inXisinB=PETSC_FALSE,guess_zero;
805: Mat mat,pmat;
806: MPI_Comm comm;
807: MatNullSpace nullsp;
808: Vec btmp,vec_rhs=NULL;
811: comm = PetscObjectComm((PetscObject)ksp);
812: if (x && x == b) {
813: if (!ksp->guess_zero) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"Cannot use x == b with nonzero initial guess");
814: VecDuplicate(b,&x);
815: inXisinB = PETSC_TRUE;
816: }
817: if (b) {
818: PetscObjectReference((PetscObject)b);
819: VecDestroy(&ksp->vec_rhs);
820: ksp->vec_rhs = b;
821: }
822: if (x) {
823: PetscObjectReference((PetscObject)x);
824: VecDestroy(&ksp->vec_sol);
825: ksp->vec_sol = x;
826: }
828: if (ksp->viewPre) {ObjectView((PetscObject) ksp, ksp->viewerPre, ksp->formatPre);}
830: if (ksp->presolve) {(*ksp->presolve)(ksp,ksp->vec_rhs,ksp->vec_sol,ksp->prectx);}
832: /* reset the residual history list if requested */
833: if (ksp->res_hist_reset) ksp->res_hist_len = 0;
834: if (ksp->err_hist_reset) ksp->err_hist_len = 0;
836: if (ksp->guess) {
837: PetscObjectState ostate,state;
839: KSPGuessSetUp(ksp->guess);
840: PetscObjectStateGet((PetscObject)ksp->vec_sol,&ostate);
841: KSPGuessFormGuess(ksp->guess,ksp->vec_rhs,ksp->vec_sol);
842: PetscObjectStateGet((PetscObject)ksp->vec_sol,&state);
843: if (state != ostate) {
844: ksp->guess_zero = PETSC_FALSE;
845: } else {
846: PetscInfo(ksp,"Using zero initial guess since the KSPGuess object did not change the vector\n");
847: ksp->guess_zero = PETSC_TRUE;
848: }
849: }
851: /* KSPSetUp() scales the matrix if needed */
852: KSPSetUp(ksp);
853: KSPSetUpOnBlocks(ksp);
855: VecSetErrorIfLocked(ksp->vec_sol,3);
857: PetscLogEventBegin(KSP_Solve,ksp,ksp->vec_rhs,ksp->vec_sol,0);
858: PCGetOperators(ksp->pc,&mat,&pmat);
859: /* diagonal scale RHS if called for */
860: if (ksp->dscale) {
861: VecPointwiseMult(ksp->vec_rhs,ksp->vec_rhs,ksp->diagonal);
862: /* second time in, but matrix was scaled back to original */
863: if (ksp->dscalefix && ksp->dscalefix2) {
864: Mat mat,pmat;
866: PCGetOperators(ksp->pc,&mat,&pmat);
867: MatDiagonalScale(pmat,ksp->diagonal,ksp->diagonal);
868: if (mat != pmat) {MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);}
869: }
871: /* scale initial guess */
872: if (!ksp->guess_zero) {
873: if (!ksp->truediagonal) {
874: VecDuplicate(ksp->diagonal,&ksp->truediagonal);
875: VecCopy(ksp->diagonal,ksp->truediagonal);
876: VecReciprocal(ksp->truediagonal);
877: }
878: VecPointwiseMult(ksp->vec_sol,ksp->vec_sol,ksp->truediagonal);
879: }
880: }
881: PCPreSolve(ksp->pc,ksp);
883: if (ksp->guess_zero) { VecSet(ksp->vec_sol,0.0);}
884: if (ksp->guess_knoll) { /* The Knoll trick is independent on the KSPGuess specified */
885: PCApply(ksp->pc,ksp->vec_rhs,ksp->vec_sol);
886: KSP_RemoveNullSpace(ksp,ksp->vec_sol);
887: ksp->guess_zero = PETSC_FALSE;
888: }
890: /* can we mark the initial guess as zero for this solve? */
891: guess_zero = ksp->guess_zero;
892: if (!ksp->guess_zero) {
893: PetscReal norm;
895: VecNormAvailable(ksp->vec_sol,NORM_2,&flg,&norm);
896: if (flg && !norm) ksp->guess_zero = PETSC_TRUE;
897: }
898: if (ksp->transpose_solve) {
899: MatGetNullSpace(pmat,&nullsp);
900: } else {
901: MatGetTransposeNullSpace(pmat,&nullsp);
902: }
903: if (nullsp) {
904: VecDuplicate(ksp->vec_rhs,&btmp);
905: VecCopy(ksp->vec_rhs,btmp);
906: MatNullSpaceRemove(nullsp,btmp);
907: vec_rhs = ksp->vec_rhs;
908: ksp->vec_rhs = btmp;
909: }
910: VecLockReadPush(ksp->vec_rhs);
911: if (ksp->reason == KSP_DIVERGED_PC_FAILED) {
912: VecSetInf(ksp->vec_sol);
913: }
914: (*ksp->ops->solve)(ksp);
915: KSPMonitorPauseFinal_Internal(ksp);
917: VecLockReadPop(ksp->vec_rhs);
918: if (nullsp) {
919: ksp->vec_rhs = vec_rhs;
920: VecDestroy(&btmp);
921: }
923: ksp->guess_zero = guess_zero;
925: if (!ksp->reason) SETERRQ(comm,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
926: ksp->totalits += ksp->its;
928: KSPConvergedReasonViewFromOptions(ksp);
930: if (ksp->viewRate) {
931: PetscViewerPushFormat(ksp->viewerRate,ksp->formatRate);
932: KSPConvergedRateView(ksp, ksp->viewerRate);
933: PetscViewerPopFormat(ksp->viewerRate);
934: }
935: PCPostSolve(ksp->pc,ksp);
937: /* diagonal scale solution if called for */
938: if (ksp->dscale) {
939: VecPointwiseMult(ksp->vec_sol,ksp->vec_sol,ksp->diagonal);
940: /* unscale right hand side and matrix */
941: if (ksp->dscalefix) {
942: Mat mat,pmat;
944: VecReciprocal(ksp->diagonal);
945: VecPointwiseMult(ksp->vec_rhs,ksp->vec_rhs,ksp->diagonal);
946: PCGetOperators(ksp->pc,&mat,&pmat);
947: MatDiagonalScale(pmat,ksp->diagonal,ksp->diagonal);
948: if (mat != pmat) {MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);}
949: VecReciprocal(ksp->diagonal);
950: ksp->dscalefix2 = PETSC_TRUE;
951: }
952: }
953: PetscLogEventEnd(KSP_Solve,ksp,ksp->vec_rhs,ksp->vec_sol,0);
954: if (ksp->guess) {
955: KSPGuessUpdate(ksp->guess,ksp->vec_rhs,ksp->vec_sol);
956: }
957: if (ksp->postsolve) {
958: (*ksp->postsolve)(ksp,ksp->vec_rhs,ksp->vec_sol,ksp->postctx);
959: }
961: PCGetOperators(ksp->pc,&mat,&pmat);
962: if (ksp->viewEV) {KSPViewEigenvalues_Internal(ksp, PETSC_FALSE, ksp->viewerEV, ksp->formatEV);}
963: if (ksp->viewEVExp) {KSPViewEigenvalues_Internal(ksp, PETSC_TRUE, ksp->viewerEVExp, ksp->formatEVExp);}
964: if (ksp->viewSV) {KSPViewSingularvalues_Internal(ksp, ksp->viewerSV, ksp->formatSV);}
965: if (ksp->viewFinalRes) {KSPViewFinalResidual_Internal(ksp, ksp->viewerFinalRes, ksp->formatFinalRes);}
966: if (ksp->viewMat) {ObjectView((PetscObject) mat, ksp->viewerMat, ksp->formatMat);}
967: if (ksp->viewPMat) {ObjectView((PetscObject) pmat, ksp->viewerPMat, ksp->formatPMat);}
968: if (ksp->viewRhs) {ObjectView((PetscObject) ksp->vec_rhs, ksp->viewerRhs, ksp->formatRhs);}
969: if (ksp->viewSol) {ObjectView((PetscObject) ksp->vec_sol, ksp->viewerSol, ksp->formatSol);}
970: if (ksp->view) {ObjectView((PetscObject) ksp, ksp->viewer, ksp->format);}
971: if (ksp->viewDScale) {ObjectView((PetscObject) ksp->diagonal, ksp->viewerDScale, ksp->formatDScale);}
972: if (ksp->viewMatExp) {
973: Mat A, B;
975: PCGetOperators(ksp->pc, &A, NULL);
976: if (ksp->transpose_solve) {
977: Mat AT;
979: MatCreateTranspose(A, &AT);
980: MatComputeOperator(AT, MATAIJ, &B);
981: MatDestroy(&AT);
982: } else {
983: MatComputeOperator(A, MATAIJ, &B);
984: }
985: ObjectView((PetscObject) B, ksp->viewerMatExp, ksp->formatMatExp);
986: MatDestroy(&B);
987: }
988: if (ksp->viewPOpExp) {
989: Mat B;
991: KSPComputeOperator(ksp, MATAIJ, &B);
992: ObjectView((PetscObject) B, ksp->viewerPOpExp, ksp->formatPOpExp);
993: MatDestroy(&B);
994: }
996: if (inXisinB) {
997: VecCopy(x,b);
998: VecDestroy(&x);
999: }
1000: PetscObjectSAWsBlock((PetscObject)ksp);
1001: if (ksp->errorifnotconverged && ksp->reason < 0 && ksp->reason != KSP_DIVERGED_ITS) {
1002: if (ksp->reason == KSP_DIVERGED_PC_FAILED) {
1003: PCFailedReason reason;
1004: PCGetFailedReason(ksp->pc,&reason);
1005: SETERRQ2(comm,PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged, reason %s PC failed due to %s",KSPConvergedReasons[ksp->reason],PCFailedReasons[reason]);
1006: } else SETERRQ1(comm,PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged, reason %s",KSPConvergedReasons[ksp->reason]);
1007: }
1008: return(0);
1009: }
1011: /*@
1012: KSPSolve - Solves linear system.
1014: Collective on ksp
1016: Parameters:
1017: + ksp - iterative context obtained from KSPCreate()
1018: . b - the right hand side vector
1019: - x - the solution (this may be the same vector as b, then b will be overwritten with answer)
1021: Options Database Keys:
1022: + -ksp_view_eigenvalues - compute preconditioned operators eigenvalues
1023: . -ksp_view_eigenvalues_explicit - compute the eigenvalues by forming the dense operator and using LAPACK
1024: . -ksp_view_mat binary - save matrix to the default binary viewer
1025: . -ksp_view_pmat binary - save matrix used to build preconditioner to the default binary viewer
1026: . -ksp_view_rhs binary - save right hand side vector to the default binary viewer
1027: . -ksp_view_solution binary - save computed solution vector to the default binary viewer
1028: (can be read later with src/ksp/tutorials/ex10.c for testing solvers)
1029: . -ksp_view_mat_explicit - for matrix-free operators, computes the matrix entries and views them
1030: . -ksp_view_preconditioned_operator_explicit - computes the product of the preconditioner and matrix as an explicit matrix and views it
1031: . -ksp_converged_reason - print reason for converged or diverged, also prints number of iterations
1032: . -ksp_view_final_residual - print 2-norm of true linear system residual at the end of the solution process
1033: - -ksp_view - print the ksp data structure at the end of the system solution
1035: Notes:
1037: If one uses KSPSetDM() then x or b need not be passed. Use KSPGetSolution() to access the solution in this case.
1039: The operator is specified with KSPSetOperators().
1041: Call KSPGetConvergedReason() to determine if the solver converged or failed and
1042: why. The number of iterations can be obtained from KSPGetIterationNumber().
1044: If you provide a matrix that has a MatSetNullSpace() and MatSetTransposeNullSpace() this will use that information to solve singular systems
1045: in the least squares sense with a norm minimizing solution.
1046: $
1047: $ A x = b where b = b_p + b_t where b_t is not in the range of A (and hence by the fundamental theorem of linear algebra is in the nullspace(A') see MatSetNullSpace()
1048: $
1049: $ KSP first removes b_t producing the linear system A x = b_p (which has multiple solutions) and solves this to find the ||x|| minimizing solution (and hence
1050: $ it finds the solution x orthogonal to the nullspace(A). The algorithm is simply in each iteration of the Krylov method we remove the nullspace(A) from the search
1051: $ direction thus the solution which is a linear combination of the search directions has no component in the nullspace(A).
1052: $
1053: $ We recommend always using GMRES for such singular systems.
1054: $ If nullspace(A) = nullspace(A') (note symmetric matrices always satisfy this property) then both left and right preconditioning will work
1055: $ If nullspace(A) != nullspace(A') then left preconditioning will work but right preconditioning may not work (or it may).
1057: Developer Note: The reason we cannot always solve nullspace(A) != nullspace(A') systems with right preconditioning is because we need to remove at each iteration
1058: the nullspace(AB) from the search direction. While we know the nullspace(A) the nullspace(AB) equals B^-1 times the nullspace(A) but except for trivial preconditioners
1059: such as diagonal scaling we cannot apply the inverse of the preconditioner to a vector and thus cannot compute the nullspace(AB).
1061: If using a direct method (e.g., via the KSP solver
1062: KSPPREONLY and a preconditioner such as PCLU/PCILU),
1063: then its=1. See KSPSetTolerances() and KSPConvergedDefault()
1064: for more details.
1066: Understanding Convergence:
1067: The routines KSPMonitorSet(), KSPComputeEigenvalues(), and
1068: KSPComputeEigenvaluesExplicitly() provide information on additional
1069: options to monitor convergence and print eigenvalue information.
1071: Level: beginner
1073: .seealso: KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPConvergedDefault(),
1074: KSPSolveTranspose(), KSPGetIterationNumber(), MatNullSpaceCreate(), MatSetNullSpace(), MatSetTransposeNullSpace(), KSP,
1075: KSPConvergedReasonView()
1076: @*/
1077: PetscErrorCode KSPSolve(KSP ksp,Vec b,Vec x)
1078: {
1085: ksp->transpose_solve = PETSC_FALSE;
1086: KSPSolve_Private(ksp,b,x);
1087: return(0);
1088: }
1090: /*@
1091: KSPSolveTranspose - Solves the transpose of a linear system.
1093: Collective on ksp
1095: Input Parameters:
1096: + ksp - iterative context obtained from KSPCreate()
1097: . b - right hand side vector
1098: - x - solution vector
1100: Notes:
1101: For complex numbers this solve the non-Hermitian transpose system.
1103: Developer Notes:
1104: We need to implement a KSPSolveHermitianTranspose()
1106: Level: developer
1108: .seealso: KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPConvergedDefault(),
1109: KSPSolve(), KSP
1110: @*/
1111: PetscErrorCode KSPSolveTranspose(KSP ksp,Vec b,Vec x)
1112: {
1119: if (ksp->transpose.use_explicittranspose) {
1120: Mat J,Jpre;
1121: KSPGetOperators(ksp,&J,&Jpre);
1122: if (!ksp->transpose.reuse_transpose) {
1123: MatTranspose(J,MAT_INITIAL_MATRIX,&ksp->transpose.AT);
1124: if (J != Jpre) {
1125: MatTranspose(Jpre,MAT_INITIAL_MATRIX,&ksp->transpose.BT);
1126: }
1127: ksp->transpose.reuse_transpose = PETSC_TRUE;
1128: } else {
1129: MatTranspose(J,MAT_REUSE_MATRIX,&ksp->transpose.AT);
1130: if (J != Jpre) {
1131: MatTranspose(Jpre,MAT_REUSE_MATRIX,&ksp->transpose.BT);
1132: }
1133: }
1134: if (J == Jpre && ksp->transpose.BT != ksp->transpose.AT) {
1135: PetscObjectReference((PetscObject)ksp->transpose.AT);
1136: ksp->transpose.BT = ksp->transpose.AT;
1137: }
1138: KSPSetOperators(ksp,ksp->transpose.AT,ksp->transpose.BT);
1139: } else {
1140: ksp->transpose_solve = PETSC_TRUE;
1141: }
1142: KSPSolve_Private(ksp,b,x);
1143: return(0);
1144: }
1146: static PetscErrorCode KSPViewFinalMatResidual_Internal(KSP ksp, Mat B, Mat X, PetscViewer viewer, PetscViewerFormat format, PetscInt shift)
1147: {
1148: Mat A, R;
1149: PetscReal *norms;
1150: PetscInt i, N;
1151: PetscBool flg;
1155: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg);
1156: if (flg) {
1157: PCGetOperators(ksp->pc, &A, NULL);
1158: MatMatMult(A, X, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &R);
1159: MatAYPX(R, -1.0, B, SAME_NONZERO_PATTERN);
1160: MatGetSize(R, NULL, &N);
1161: PetscMalloc1(N, &norms);
1162: MatGetColumnNorms(R, NORM_2, norms);
1163: MatDestroy(&R);
1164: for (i = 0; i < N; ++i) {
1165: PetscViewerASCIIPrintf(viewer, "%s #%D %g\n", i == 0 ? "KSP final norm of residual" : " ", shift + i, (double)norms[i]);
1166: }
1167: PetscFree(norms);
1168: }
1169: return(0);
1170: }
1172: /*@
1173: KSPMatSolve - Solves a linear system with multiple right-hand sides stored as a MATDENSE. Unlike KSPSolve(), B and X must be different matrices.
1175: Input Parameters:
1176: + ksp - iterative context
1177: - B - block of right-hand sides
1179: Output Parameter:
1180: . X - block of solutions
1182: Notes:
1183: This is a stripped-down version of KSPSolve(), which only handles -ksp_view, -ksp_converged_reason, and -ksp_view_final_residual.
1185: Level: intermediate
1187: .seealso: KSPSolve(), MatMatSolve(), MATDENSE, KSPHPDDM, PCBJACOBI, PCASM
1188: @*/
1189: PetscErrorCode KSPMatSolve(KSP ksp, Mat B, Mat X)
1190: {
1191: Mat A, P, vB, vX;
1192: Vec cb, cx;
1193: PetscInt n1, N1, n2, N2, Bbn = PETSC_DECIDE;
1194: PetscBool match;
1203: if (!B->assembled) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
1204: MatCheckPreallocated(X, 3);
1205: if (!X->assembled) {
1206: MatSetOption(X, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE);
1207: MatAssemblyBegin(X, MAT_FINAL_ASSEMBLY);
1208: MatAssemblyEnd(X, MAT_FINAL_ASSEMBLY);
1209: }
1210: if (B == X) SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_IDN, "B and X must be different matrices");
1211: KSPGetOperators(ksp, &A, &P);
1212: MatGetLocalSize(B, NULL, &n2);
1213: MatGetLocalSize(X, NULL, &n1);
1214: MatGetSize(B, NULL, &N2);
1215: MatGetSize(X, NULL, &N1);
1216: if (n1 != n2 || N1 != N2) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible number of columns between block of right-hand sides (n,N) = (%D,%D) and block of solutions (n,N) = (%D,%D)", n2, N2, n1, N1);
1217: PetscObjectBaseTypeCompareAny((PetscObject)B, &match, MATSEQDENSE, MATMPIDENSE, "");
1218: if (!match) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Provided block of right-hand sides not stored in a dense Mat");
1219: PetscObjectBaseTypeCompareAny((PetscObject)X, &match, MATSEQDENSE, MATMPIDENSE, "");
1220: if (!match) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Provided block of solutions not stored in a dense Mat");
1221: KSPSetUp(ksp);
1222: KSPSetUpOnBlocks(ksp);
1223: if (ksp->ops->matsolve) {
1224: if (ksp->guess_zero) {
1225: MatZeroEntries(X);
1226: }
1227: PetscLogEventBegin(KSP_MatSolve, ksp, B, X, 0);
1228: KSPGetMatSolveBatchSize(ksp, &Bbn);
1229: /* by default, do a single solve with all columns */
1230: if (Bbn == PETSC_DECIDE) Bbn = N2;
1231: else if (Bbn < 1) SETERRQ1(PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "KSPMatSolve() batch size %D must be positive", Bbn);
1232: PetscInfo2(ksp, "KSP type %s solving using batches of width at most %D\n", ((PetscObject)ksp)->type_name, Bbn);
1233: /* if -ksp_matsolve_batch_size is greater than the actual number of columns, do a single solve with all columns */
1234: if (Bbn >= N2) {
1235: (*ksp->ops->matsolve)(ksp, B, X);
1236: if (ksp->viewFinalRes) {
1237: KSPViewFinalMatResidual_Internal(ksp, B, X, ksp->viewerFinalRes, ksp->formatFinalRes, 0);
1238: }
1240: KSPConvergedReasonViewFromOptions(ksp);
1242: if (ksp->viewRate) {
1243: PetscViewerPushFormat(ksp->viewerRate,PETSC_VIEWER_DEFAULT);
1244: KSPConvergedRateView(ksp, ksp->viewerRate);
1245: PetscViewerPopFormat(ksp->viewerRate);
1246: }
1247: } else {
1248: for (n2 = 0; n2 < N2; n2 += Bbn) {
1249: MatDenseGetSubMatrix(B, n2, PetscMin(n2+Bbn, N2), &vB);
1250: MatDenseGetSubMatrix(X, n2, PetscMin(n2+Bbn, N2), &vX);
1251: (*ksp->ops->matsolve)(ksp, vB, vX);
1252: if (ksp->viewFinalRes) {
1253: KSPViewFinalMatResidual_Internal(ksp, vB, vX, ksp->viewerFinalRes, ksp->formatFinalRes, n2);
1254: }
1256: KSPConvergedReasonViewFromOptions(ksp);
1258: if (ksp->viewRate) {
1259: PetscViewerPushFormat(ksp->viewerRate,PETSC_VIEWER_DEFAULT);
1260: KSPConvergedRateView(ksp, ksp->viewerRate);
1261: PetscViewerPopFormat(ksp->viewerRate);
1262: }
1263: MatDenseRestoreSubMatrix(B, &vB);
1264: MatDenseRestoreSubMatrix(X, &vX);
1265: }
1266: }
1267: if (ksp->viewMat) {ObjectView((PetscObject) A, ksp->viewerMat, ksp->formatMat);}
1268: if (ksp->viewPMat) {ObjectView((PetscObject) P, ksp->viewerPMat,ksp->formatPMat);}
1269: if (ksp->viewRhs) {ObjectView((PetscObject) B, ksp->viewerRhs, ksp->formatRhs);}
1270: if (ksp->viewSol) {ObjectView((PetscObject) X, ksp->viewerSol, ksp->formatSol);}
1271: if (ksp->view) {
1272: KSPView(ksp, ksp->viewer);
1273: }
1274: PetscLogEventEnd(KSP_MatSolve, ksp, B, X, 0);
1275: } else {
1276: PetscInfo1(ksp, "KSP type %s solving column by column\n", ((PetscObject)ksp)->type_name);
1277: for (n2 = 0; n2 < N2; ++n2) {
1278: MatDenseGetColumnVecRead(B, n2, &cb);
1279: MatDenseGetColumnVecWrite(X, n2, &cx);
1280: KSPSolve(ksp, cb, cx);
1281: MatDenseRestoreColumnVecWrite(X, n2, &cx);
1282: MatDenseRestoreColumnVecRead(B, n2, &cb);
1283: }
1284: }
1285: return(0);
1286: }
1288: /*@
1289: KSPSetMatSolveBatchSize - Sets the maximum number of columns treated simultaneously in KSPMatSolve().
1291: Logically collective
1293: Input Parameters:
1294: + ksp - iterative context
1295: - bs - batch size
1297: Level: advanced
1299: .seealso: KSPMatSolve(), KSPGetMatSolveBatchSize(), -mat_mumps_icntl_27, -matmatmult_Bbn
1300: @*/
1301: PetscErrorCode KSPSetMatSolveBatchSize(KSP ksp, PetscInt bs)
1302: {
1306: ksp->nmax = bs;
1307: return(0);
1308: }
1310: /*@
1311: KSPGetMatSolveBatchSize - Gets the maximum number of columns treated simultaneously in KSPMatSolve().
1313: Input Parameter:
1314: . ksp - iterative context
1316: Output Parameter:
1317: . bs - batch size
1319: Level: advanced
1321: .seealso: KSPMatSolve(), KSPSetMatSolveBatchSize(), -mat_mumps_icntl_27, -matmatmult_Bbn
1322: @*/
1323: PetscErrorCode KSPGetMatSolveBatchSize(KSP ksp, PetscInt *bs)
1324: {
1328: *bs = ksp->nmax;
1329: return(0);
1330: }
1332: /*@
1333: KSPResetViewers - Resets all the viewers set from the options database during KSPSetFromOptions()
1335: Collective on ksp
1337: Input Parameter:
1338: . ksp - iterative context obtained from KSPCreate()
1340: Level: beginner
1342: .seealso: KSPCreate(), KSPSetUp(), KSPSolve(), KSPSetFromOptions(), KSP
1343: @*/
1344: PetscErrorCode KSPResetViewers(KSP ksp)
1345: {
1350: if (!ksp) return(0);
1351: PetscViewerDestroy(&ksp->viewer);
1352: PetscViewerDestroy(&ksp->viewerPre);
1353: PetscViewerDestroy(&ksp->viewerRate);
1354: PetscViewerDestroy(&ksp->viewerMat);
1355: PetscViewerDestroy(&ksp->viewerPMat);
1356: PetscViewerDestroy(&ksp->viewerRhs);
1357: PetscViewerDestroy(&ksp->viewerSol);
1358: PetscViewerDestroy(&ksp->viewerMatExp);
1359: PetscViewerDestroy(&ksp->viewerEV);
1360: PetscViewerDestroy(&ksp->viewerSV);
1361: PetscViewerDestroy(&ksp->viewerEVExp);
1362: PetscViewerDestroy(&ksp->viewerFinalRes);
1363: PetscViewerDestroy(&ksp->viewerPOpExp);
1364: PetscViewerDestroy(&ksp->viewerDScale);
1365: ksp->view = PETSC_FALSE;
1366: ksp->viewPre = PETSC_FALSE;
1367: ksp->viewMat = PETSC_FALSE;
1368: ksp->viewPMat = PETSC_FALSE;
1369: ksp->viewRhs = PETSC_FALSE;
1370: ksp->viewSol = PETSC_FALSE;
1371: ksp->viewMatExp = PETSC_FALSE;
1372: ksp->viewEV = PETSC_FALSE;
1373: ksp->viewSV = PETSC_FALSE;
1374: ksp->viewEVExp = PETSC_FALSE;
1375: ksp->viewFinalRes = PETSC_FALSE;
1376: ksp->viewPOpExp = PETSC_FALSE;
1377: ksp->viewDScale = PETSC_FALSE;
1378: return(0);
1379: }
1381: /*@
1382: KSPReset - Resets a KSP context to the kspsetupcalled = 0 state and removes any allocated Vecs and Mats
1384: Collective on ksp
1386: Input Parameter:
1387: . ksp - iterative context obtained from KSPCreate()
1389: Level: beginner
1391: .seealso: KSPCreate(), KSPSetUp(), KSPSolve(), KSP
1392: @*/
1393: PetscErrorCode KSPReset(KSP ksp)
1394: {
1399: if (!ksp) return(0);
1400: if (ksp->ops->reset) {
1401: (*ksp->ops->reset)(ksp);
1402: }
1403: if (ksp->pc) {PCReset(ksp->pc);}
1404: if (ksp->guess) {
1405: KSPGuess guess = ksp->guess;
1406: if (guess->ops->reset) { (*guess->ops->reset)(guess); }
1407: }
1408: VecDestroyVecs(ksp->nwork,&ksp->work);
1409: VecDestroy(&ksp->vec_rhs);
1410: VecDestroy(&ksp->vec_sol);
1411: VecDestroy(&ksp->diagonal);
1412: VecDestroy(&ksp->truediagonal);
1414: KSPResetViewers(ksp);
1416: ksp->setupstage = KSP_SETUP_NEW;
1417: ksp->nmax = PETSC_DECIDE;
1418: return(0);
1419: }
1421: /*@C
1422: KSPDestroy - Destroys KSP context.
1424: Collective on ksp
1426: Input Parameter:
1427: . ksp - iterative context obtained from KSPCreate()
1429: Level: beginner
1431: .seealso: KSPCreate(), KSPSetUp(), KSPSolve(), KSP
1432: @*/
1433: PetscErrorCode KSPDestroy(KSP *ksp)
1434: {
1436: PC pc;
1439: if (!*ksp) return(0);
1441: if (--((PetscObject)(*ksp))->refct > 0) {*ksp = NULL; return(0);}
1443: PetscObjectSAWsViewOff((PetscObject)*ksp);
1445: /*
1446: Avoid a cascading call to PCReset(ksp->pc) from the following call:
1447: PCReset() shouldn't be called from KSPDestroy() as it is unprotected by pc's
1448: refcount (and may be shared, e.g., by other ksps).
1449: */
1450: pc = (*ksp)->pc;
1451: (*ksp)->pc = NULL;
1452: KSPReset((*ksp));
1453: (*ksp)->pc = pc;
1454: if ((*ksp)->ops->destroy) {(*(*ksp)->ops->destroy)(*ksp);}
1456: if ((*ksp)->transpose.use_explicittranspose) {
1457: MatDestroy(&(*ksp)->transpose.AT);
1458: MatDestroy(&(*ksp)->transpose.BT);
1459: (*ksp)->transpose.reuse_transpose = PETSC_FALSE;
1460: }
1462: KSPGuessDestroy(&(*ksp)->guess);
1463: DMDestroy(&(*ksp)->dm);
1464: PCDestroy(&(*ksp)->pc);
1465: PetscFree((*ksp)->res_hist_alloc);
1466: PetscFree((*ksp)->err_hist_alloc);
1467: if ((*ksp)->convergeddestroy) {
1468: (*(*ksp)->convergeddestroy)((*ksp)->cnvP);
1469: }
1470: KSPMonitorCancel((*ksp));
1471: KSPConvergedReasonViewCancel((*ksp));
1472: PetscViewerDestroy(&(*ksp)->eigviewer);
1473: PetscHeaderDestroy(ksp);
1474: return(0);
1475: }
1477: /*@
1478: KSPSetPCSide - Sets the preconditioning side.
1480: Logically Collective on ksp
1482: Input Parameter:
1483: . ksp - iterative context obtained from KSPCreate()
1485: Output Parameter:
1486: . side - the preconditioning side, where side is one of
1487: .vb
1488: PC_LEFT - left preconditioning (default)
1489: PC_RIGHT - right preconditioning
1490: PC_SYMMETRIC - symmetric preconditioning
1491: .ve
1493: Options Database Keys:
1494: . -ksp_pc_side <right,left,symmetric>
1496: Notes:
1497: Left preconditioning is used by default for most Krylov methods except KSPFGMRES which only supports right preconditioning.
1499: For methods changing the side of the preconditioner changes the norm type that is used, see KSPSetNormType().
1501: Symmetric preconditioning is currently available only for the KSPQCG method. Note, however, that
1502: symmetric preconditioning can be emulated by using either right or left
1503: preconditioning and a pre or post processing step.
1505: Setting the PC side often affects the default norm type. See KSPSetNormType() for details.
1507: Level: intermediate
1509: .seealso: KSPGetPCSide(), KSPSetNormType(), KSPGetNormType(), KSP
1510: @*/
1511: PetscErrorCode KSPSetPCSide(KSP ksp,PCSide side)
1512: {
1516: ksp->pc_side = ksp->pc_side_set = side;
1517: return(0);
1518: }
1520: /*@
1521: KSPGetPCSide - Gets the preconditioning side.
1523: Not Collective
1525: Input Parameter:
1526: . ksp - iterative context obtained from KSPCreate()
1528: Output Parameter:
1529: . side - the preconditioning side, where side is one of
1530: .vb
1531: PC_LEFT - left preconditioning (default)
1532: PC_RIGHT - right preconditioning
1533: PC_SYMMETRIC - symmetric preconditioning
1534: .ve
1536: Level: intermediate
1538: .seealso: KSPSetPCSide(), KSP
1539: @*/
1540: PetscErrorCode KSPGetPCSide(KSP ksp,PCSide *side)
1541: {
1547: KSPSetUpNorms_Private(ksp,PETSC_TRUE,&ksp->normtype,&ksp->pc_side);
1548: *side = ksp->pc_side;
1549: return(0);
1550: }
1552: /*@
1553: KSPGetTolerances - Gets the relative, absolute, divergence, and maximum
1554: iteration tolerances used by the default KSP convergence tests.
1556: Not Collective
1558: Input Parameter:
1559: . ksp - the Krylov subspace context
1561: Output Parameters:
1562: + rtol - the relative convergence tolerance
1563: . abstol - the absolute convergence tolerance
1564: . dtol - the divergence tolerance
1565: - maxits - maximum number of iterations
1567: Notes:
1568: The user can specify NULL for any parameter that is not needed.
1570: Level: intermediate
1572: maximum, iterations
1574: .seealso: KSPSetTolerances(), KSP
1575: @*/
1576: PetscErrorCode KSPGetTolerances(KSP ksp,PetscReal *rtol,PetscReal *abstol,PetscReal *dtol,PetscInt *maxits)
1577: {
1580: if (abstol) *abstol = ksp->abstol;
1581: if (rtol) *rtol = ksp->rtol;
1582: if (dtol) *dtol = ksp->divtol;
1583: if (maxits) *maxits = ksp->max_it;
1584: return(0);
1585: }
1587: /*@
1588: KSPSetTolerances - Sets the relative, absolute, divergence, and maximum
1589: iteration tolerances used by the default KSP convergence testers.
1591: Logically Collective on ksp
1593: Input Parameters:
1594: + ksp - the Krylov subspace context
1595: . rtol - the relative convergence tolerance, relative decrease in the (possibly preconditioned) residual norm
1596: . abstol - the absolute convergence tolerance absolute size of the (possibly preconditioned) residual norm
1597: . dtol - the divergence tolerance, amount (possibly preconditioned) residual norm can increase before KSPConvergedDefault() concludes that the method is diverging
1598: - maxits - maximum number of iterations to use
1600: Options Database Keys:
1601: + -ksp_atol <abstol> - Sets abstol
1602: . -ksp_rtol <rtol> - Sets rtol
1603: . -ksp_divtol <dtol> - Sets dtol
1604: - -ksp_max_it <maxits> - Sets maxits
1606: Notes:
1607: Use PETSC_DEFAULT to retain the default value of any of the tolerances.
1609: See KSPConvergedDefault() for details how these parameters are used in the default convergence test. See also KSPSetConvergenceTest()
1610: for setting user-defined stopping criteria.
1612: Level: intermediate
1614: convergence, maximum, iterations
1616: .seealso: KSPGetTolerances(), KSPConvergedDefault(), KSPSetConvergenceTest(), KSP
1617: @*/
1618: PetscErrorCode KSPSetTolerances(KSP ksp,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits)
1619: {
1627: if (rtol != PETSC_DEFAULT) {
1628: if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %g must be non-negative and less than 1.0",(double)rtol);
1629: ksp->rtol = rtol;
1630: }
1631: if (abstol != PETSC_DEFAULT) {
1632: if (abstol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g must be non-negative",(double)abstol);
1633: ksp->abstol = abstol;
1634: }
1635: if (dtol != PETSC_DEFAULT) {
1636: if (dtol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Divergence tolerance %g must be larger than 1.0",(double)dtol);
1637: ksp->divtol = dtol;
1638: }
1639: if (maxits != PETSC_DEFAULT) {
1640: if (maxits < 0) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxits);
1641: ksp->max_it = maxits;
1642: }
1643: return(0);
1644: }
1646: /*@
1647: KSPSetInitialGuessNonzero - Tells the iterative solver that the
1648: initial guess is nonzero; otherwise KSP assumes the initial guess
1649: is to be zero (and thus zeros it out before solving).
1651: Logically Collective on ksp
1653: Input Parameters:
1654: + ksp - iterative context obtained from KSPCreate()
1655: - flg - PETSC_TRUE indicates the guess is non-zero, PETSC_FALSE indicates the guess is zero
1657: Options database keys:
1658: . -ksp_initial_guess_nonzero : use nonzero initial guess; this takes an optional truth value (0/1/no/yes/true/false)
1660: Level: beginner
1662: Notes:
1663: If this is not called the X vector is zeroed in the call to KSPSolve().
1665: .seealso: KSPGetInitialGuessNonzero(), KSPSetGuessType(), KSPGuessType, KSP
1666: @*/
1667: PetscErrorCode KSPSetInitialGuessNonzero(KSP ksp,PetscBool flg)
1668: {
1672: ksp->guess_zero = (PetscBool) !(int)flg;
1673: return(0);
1674: }
1676: /*@
1677: KSPGetInitialGuessNonzero - Determines whether the KSP solver is using
1678: a zero initial guess.
1680: Not Collective
1682: Input Parameter:
1683: . ksp - iterative context obtained from KSPCreate()
1685: Output Parameter:
1686: . flag - PETSC_TRUE if guess is nonzero, else PETSC_FALSE
1688: Level: intermediate
1690: .seealso: KSPSetInitialGuessNonzero(), KSP
1691: @*/
1692: PetscErrorCode KSPGetInitialGuessNonzero(KSP ksp,PetscBool *flag)
1693: {
1697: if (ksp->guess_zero) *flag = PETSC_FALSE;
1698: else *flag = PETSC_TRUE;
1699: return(0);
1700: }
1702: /*@
1703: KSPSetErrorIfNotConverged - Causes KSPSolve() to generate an error if the solver has not converged.
1705: Logically Collective on ksp
1707: Input Parameters:
1708: + ksp - iterative context obtained from KSPCreate()
1709: - flg - PETSC_TRUE indicates you want the error generated
1711: Options database keys:
1712: . -ksp_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
1714: Level: intermediate
1716: Notes:
1717: Normally PETSc continues if a linear solver fails to converge, you can call KSPGetConvergedReason() after a KSPSolve()
1718: to determine if it has converged.
1720: .seealso: KSPGetErrorIfNotConverged(), KSP
1721: @*/
1722: PetscErrorCode KSPSetErrorIfNotConverged(KSP ksp,PetscBool flg)
1723: {
1727: ksp->errorifnotconverged = flg;
1728: return(0);
1729: }
1731: /*@
1732: KSPGetErrorIfNotConverged - Will KSPSolve() generate an error if the solver does not converge?
1734: Not Collective
1736: Input Parameter:
1737: . ksp - iterative context obtained from KSPCreate()
1739: Output Parameter:
1740: . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
1742: Level: intermediate
1744: .seealso: KSPSetErrorIfNotConverged(), KSP
1745: @*/
1746: PetscErrorCode KSPGetErrorIfNotConverged(KSP ksp,PetscBool *flag)
1747: {
1751: *flag = ksp->errorifnotconverged;
1752: return(0);
1753: }
1755: /*@
1756: KSPSetInitialGuessKnoll - Tells the iterative solver to use PCApply(pc,b,..) to compute the initial guess (The Knoll trick)
1758: Logically Collective on ksp
1760: Input Parameters:
1761: + ksp - iterative context obtained from KSPCreate()
1762: - flg - PETSC_TRUE or PETSC_FALSE
1764: Level: advanced
1766: Developer Note: the Knoll trick is not currently implemented using the KSPGuess class
1768: .seealso: KSPGetInitialGuessKnoll(), KSPSetInitialGuessNonzero(), KSPGetInitialGuessNonzero(), KSP
1769: @*/
1770: PetscErrorCode KSPSetInitialGuessKnoll(KSP ksp,PetscBool flg)
1771: {
1775: ksp->guess_knoll = flg;
1776: return(0);
1777: }
1779: /*@
1780: KSPGetInitialGuessKnoll - Determines whether the KSP solver is using the Knoll trick (using PCApply(pc,b,...) to compute
1781: the initial guess
1783: Not Collective
1785: Input Parameter:
1786: . ksp - iterative context obtained from KSPCreate()
1788: Output Parameter:
1789: . flag - PETSC_TRUE if using Knoll trick, else PETSC_FALSE
1791: Level: advanced
1793: .seealso: KSPSetInitialGuessKnoll(), KSPSetInitialGuessNonzero(), KSPGetInitialGuessNonzero(), KSP
1794: @*/
1795: PetscErrorCode KSPGetInitialGuessKnoll(KSP ksp,PetscBool *flag)
1796: {
1800: *flag = ksp->guess_knoll;
1801: return(0);
1802: }
1804: /*@
1805: KSPGetComputeSingularValues - Gets the flag indicating whether the extreme singular
1806: values will be calculated via a Lanczos or Arnoldi process as the linear
1807: system is solved.
1809: Not Collective
1811: Input Parameter:
1812: . ksp - iterative context obtained from KSPCreate()
1814: Output Parameter:
1815: . flg - PETSC_TRUE or PETSC_FALSE
1817: Options Database Key:
1818: . -ksp_monitor_singular_value - Activates KSPSetComputeSingularValues()
1820: Notes:
1821: Currently this option is not valid for all iterative methods.
1823: Many users may just want to use the monitoring routine
1824: KSPMonitorSingularValue() (which can be set with option -ksp_monitor_singular_value)
1825: to print the singular values at each iteration of the linear solve.
1827: Level: advanced
1829: .seealso: KSPComputeExtremeSingularValues(), KSPMonitorSingularValue(), KSP
1830: @*/
1831: PetscErrorCode KSPGetComputeSingularValues(KSP ksp,PetscBool *flg)
1832: {
1836: *flg = ksp->calc_sings;
1837: return(0);
1838: }
1840: /*@
1841: KSPSetComputeSingularValues - Sets a flag so that the extreme singular
1842: values will be calculated via a Lanczos or Arnoldi process as the linear
1843: system is solved.
1845: Logically Collective on ksp
1847: Input Parameters:
1848: + ksp - iterative context obtained from KSPCreate()
1849: - flg - PETSC_TRUE or PETSC_FALSE
1851: Options Database Key:
1852: . -ksp_monitor_singular_value - Activates KSPSetComputeSingularValues()
1854: Notes:
1855: Currently this option is not valid for all iterative methods.
1857: Many users may just want to use the monitoring routine
1858: KSPMonitorSingularValue() (which can be set with option -ksp_monitor_singular_value)
1859: to print the singular values at each iteration of the linear solve.
1861: Level: advanced
1863: .seealso: KSPComputeExtremeSingularValues(), KSPMonitorSingularValue(), KSP
1864: @*/
1865: PetscErrorCode KSPSetComputeSingularValues(KSP ksp,PetscBool flg)
1866: {
1870: ksp->calc_sings = flg;
1871: return(0);
1872: }
1874: /*@
1875: KSPGetComputeEigenvalues - Gets the flag indicating that the extreme eigenvalues
1876: values will be calculated via a Lanczos or Arnoldi process as the linear
1877: system is solved.
1879: Not Collective
1881: Input Parameter:
1882: . ksp - iterative context obtained from KSPCreate()
1884: Output Parameter:
1885: . flg - PETSC_TRUE or PETSC_FALSE
1887: Notes:
1888: Currently this option is not valid for all iterative methods.
1890: Level: advanced
1892: .seealso: KSPComputeEigenvalues(), KSPComputeEigenvaluesExplicitly(), KSP
1893: @*/
1894: PetscErrorCode KSPGetComputeEigenvalues(KSP ksp,PetscBool *flg)
1895: {
1899: *flg = ksp->calc_sings;
1900: return(0);
1901: }
1903: /*@
1904: KSPSetComputeEigenvalues - Sets a flag so that the extreme eigenvalues
1905: values will be calculated via a Lanczos or Arnoldi process as the linear
1906: system is solved.
1908: Logically Collective on ksp
1910: Input Parameters:
1911: + ksp - iterative context obtained from KSPCreate()
1912: - flg - PETSC_TRUE or PETSC_FALSE
1914: Notes:
1915: Currently this option is not valid for all iterative methods.
1917: Level: advanced
1919: .seealso: KSPComputeEigenvalues(), KSPComputeEigenvaluesExplicitly(), KSP
1920: @*/
1921: PetscErrorCode KSPSetComputeEigenvalues(KSP ksp,PetscBool flg)
1922: {
1926: ksp->calc_sings = flg;
1927: return(0);
1928: }
1930: /*@
1931: KSPSetComputeRitz - Sets a flag so that the Ritz or harmonic Ritz pairs
1932: will be calculated via a Lanczos or Arnoldi process as the linear
1933: system is solved.
1935: Logically Collective on ksp
1937: Input Parameters:
1938: + ksp - iterative context obtained from KSPCreate()
1939: - flg - PETSC_TRUE or PETSC_FALSE
1941: Notes:
1942: Currently this option is only valid for the GMRES method.
1944: Level: advanced
1946: .seealso: KSPComputeRitz(), KSP
1947: @*/
1948: PetscErrorCode KSPSetComputeRitz(KSP ksp, PetscBool flg)
1949: {
1953: ksp->calc_ritz = flg;
1954: return(0);
1955: }
1957: /*@
1958: KSPGetRhs - Gets the right-hand-side vector for the linear system to
1959: be solved.
1961: Not Collective
1963: Input Parameter:
1964: . ksp - iterative context obtained from KSPCreate()
1966: Output Parameter:
1967: . r - right-hand-side vector
1969: Level: developer
1971: .seealso: KSPGetSolution(), KSPSolve(), KSP
1972: @*/
1973: PetscErrorCode KSPGetRhs(KSP ksp,Vec *r)
1974: {
1978: *r = ksp->vec_rhs;
1979: return(0);
1980: }
1982: /*@
1983: KSPGetSolution - Gets the location of the solution for the
1984: linear system to be solved. Note that this may not be where the solution
1985: is stored during the iterative process; see KSPBuildSolution().
1987: Not Collective
1989: Input Parameters:
1990: . ksp - iterative context obtained from KSPCreate()
1992: Output Parameters:
1993: . v - solution vector
1995: Level: developer
1997: .seealso: KSPGetRhs(), KSPBuildSolution(), KSPSolve(), KSP
1998: @*/
1999: PetscErrorCode KSPGetSolution(KSP ksp,Vec *v)
2000: {
2004: *v = ksp->vec_sol;
2005: return(0);
2006: }
2008: /*@
2009: KSPSetPC - Sets the preconditioner to be used to calculate the
2010: application of the preconditioner on a vector.
2012: Collective on ksp
2014: Input Parameters:
2015: + ksp - iterative context obtained from KSPCreate()
2016: - pc - the preconditioner object (can be NULL)
2018: Notes:
2019: Use KSPGetPC() to retrieve the preconditioner context.
2021: Level: developer
2023: .seealso: KSPGetPC(), KSP
2024: @*/
2025: PetscErrorCode KSPSetPC(KSP ksp,PC pc)
2026: {
2031: if (pc) {
2034: }
2035: PetscObjectReference((PetscObject)pc);
2036: PCDestroy(&ksp->pc);
2037: ksp->pc = pc;
2038: PetscLogObjectParent((PetscObject)ksp,(PetscObject)ksp->pc);
2039: return(0);
2040: }
2042: /*@
2043: KSPGetPC - Returns a pointer to the preconditioner context
2044: set with KSPSetPC().
2046: Not Collective
2048: Input Parameters:
2049: . ksp - iterative context obtained from KSPCreate()
2051: Output Parameter:
2052: . pc - preconditioner context
2054: Level: developer
2056: .seealso: KSPSetPC(), KSP
2057: @*/
2058: PetscErrorCode KSPGetPC(KSP ksp,PC *pc)
2059: {
2065: if (!ksp->pc) {
2066: PCCreate(PetscObjectComm((PetscObject)ksp),&ksp->pc);
2067: PetscObjectIncrementTabLevel((PetscObject)ksp->pc,(PetscObject)ksp,0);
2068: PetscLogObjectParent((PetscObject)ksp,(PetscObject)ksp->pc);
2069: PetscObjectSetOptions((PetscObject)ksp->pc,((PetscObject)ksp)->options);
2070: }
2071: *pc = ksp->pc;
2072: return(0);
2073: }
2075: /*@
2076: KSPMonitor - runs the user provided monitor routines, if they exist
2078: Collective on ksp
2080: Input Parameters:
2081: + ksp - iterative context obtained from KSPCreate()
2082: . it - iteration number
2083: - rnorm - relative norm of the residual
2085: Notes:
2086: This routine is called by the KSP implementations.
2087: It does not typically need to be called by the user.
2089: Level: developer
2091: .seealso: KSPMonitorSet()
2092: @*/
2093: PetscErrorCode KSPMonitor(KSP ksp,PetscInt it,PetscReal rnorm)
2094: {
2095: PetscInt i, n = ksp->numbermonitors;
2099: for (i=0; i<n; i++) {
2100: (*ksp->monitor[i])(ksp,it,rnorm,ksp->monitorcontext[i]);
2101: }
2102: return(0);
2103: }
2105: /*@C
2106: KSPMonitorSet - Sets an ADDITIONAL function to be called at every iteration to monitor
2107: the residual/error etc.
2109: Logically Collective on ksp
2111: Input Parameters:
2112: + ksp - iterative context obtained from KSPCreate()
2113: . monitor - pointer to function (if this is NULL, it turns off monitoring
2114: . mctx - [optional] context for private data for the
2115: monitor routine (use NULL if no context is desired)
2116: - monitordestroy - [optional] routine that frees monitor context
2117: (may be NULL)
2119: Calling Sequence of monitor:
2120: $ monitor (KSP ksp, PetscInt it, PetscReal rnorm, void *mctx)
2122: + ksp - iterative context obtained from KSPCreate()
2123: . it - iteration number
2124: . rnorm - (estimated) 2-norm of (preconditioned) residual
2125: - mctx - optional monitoring context, as set by KSPMonitorSet()
2127: Options Database Keys:
2128: + -ksp_monitor - sets KSPMonitorResidual()
2129: . -ksp_monitor draw - sets KSPMonitorResidualDraw() and plots residual
2130: . -ksp_monitor draw::draw_lg - sets KSPMonitorResidualDrawLG() and plots residual
2131: . -ksp_monitor_pause_final - Pauses any graphics when the solve finishes (only works for internal monitors)
2132: . -ksp_monitor_true_residual - sets KSPMonitorTrueResidual()
2133: . -ksp_monitor_true_residual draw::draw_lg - sets KSPMonitorTrueResidualDrawLG() and plots residual
2134: . -ksp_monitor_max - sets KSPMonitorTrueResidualMax()
2135: . -ksp_monitor_singular_value - sets KSPMonitorSingularValue()
2136: - -ksp_monitor_cancel - cancels all monitors that have
2137: been hardwired into a code by
2138: calls to KSPMonitorSet(), but
2139: does not cancel those set via
2140: the options database.
2142: Notes:
2143: The default is to do nothing. To print the residual, or preconditioned
2144: residual if KSPSetNormType(ksp,KSP_NORM_PRECONDITIONED) was called, use
2145: KSPMonitorResidual() as the monitoring routine, with a ASCII viewer as the
2146: context.
2148: Several different monitoring routines may be set by calling
2149: KSPMonitorSet() multiple times; all will be called in the
2150: order in which they were set.
2152: Fortran Notes:
2153: Only a single monitor function can be set for each KSP object
2155: Level: beginner
2157: .seealso: KSPMonitorResidual(), KSPMonitorCancel(), KSP
2158: @*/
2159: PetscErrorCode KSPMonitorSet(KSP ksp,PetscErrorCode (*monitor)(KSP,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
2160: {
2161: PetscInt i;
2163: PetscBool identical;
2167: for (i=0; i<ksp->numbermonitors;i++) {
2168: PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,monitordestroy,(PetscErrorCode (*)(void))ksp->monitor[i],ksp->monitorcontext[i],ksp->monitordestroy[i],&identical);
2169: if (identical) return(0);
2170: }
2171: if (ksp->numbermonitors >= MAXKSPMONITORS) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Too many KSP monitors set");
2172: ksp->monitor[ksp->numbermonitors] = monitor;
2173: ksp->monitordestroy[ksp->numbermonitors] = monitordestroy;
2174: ksp->monitorcontext[ksp->numbermonitors++] = (void*)mctx;
2175: return(0);
2176: }
2178: /*@
2179: KSPMonitorCancel - Clears all monitors for a KSP object.
2181: Logically Collective on ksp
2183: Input Parameters:
2184: . ksp - iterative context obtained from KSPCreate()
2186: Options Database Key:
2187: . -ksp_monitor_cancel - Cancels all monitors that have
2188: been hardwired into a code by calls to KSPMonitorSet(),
2189: but does not cancel those set via the options database.
2191: Level: intermediate
2193: .seealso: KSPMonitorResidual(), KSPMonitorSet(), KSP
2194: @*/
2195: PetscErrorCode KSPMonitorCancel(KSP ksp)
2196: {
2198: PetscInt i;
2202: for (i=0; i<ksp->numbermonitors; i++) {
2203: if (ksp->monitordestroy[i]) {
2204: (*ksp->monitordestroy[i])(&ksp->monitorcontext[i]);
2205: }
2206: }
2207: ksp->numbermonitors = 0;
2208: return(0);
2209: }
2211: /*@C
2212: KSPGetMonitorContext - Gets the monitoring context, as set by
2213: KSPMonitorSet() for the FIRST monitor only.
2215: Not Collective
2217: Input Parameter:
2218: . ksp - iterative context obtained from KSPCreate()
2220: Output Parameter:
2221: . ctx - monitoring context
2223: Level: intermediate
2225: .seealso: KSPMonitorResidual(), KSP
2226: @*/
2227: PetscErrorCode KSPGetMonitorContext(KSP ksp,void *ctx)
2228: {
2231: *(void**)ctx = ksp->monitorcontext[0];
2232: return(0);
2233: }
2235: /*@
2236: KSPSetResidualHistory - Sets the array used to hold the residual history.
2237: If set, this array will contain the residual norms computed at each
2238: iteration of the solver.
2240: Not Collective
2242: Input Parameters:
2243: + ksp - iterative context obtained from KSPCreate()
2244: . a - array to hold history
2245: . na - size of a
2246: - reset - PETSC_TRUE indicates the history counter is reset to zero
2247: for each new linear solve
2249: Level: advanced
2251: Notes:
2252: If provided, he array is NOT freed by PETSc so the user needs to keep track of it and destroy once the KSP object is destroyed.
2253: If 'a' is NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2254: default array of length 10000 is allocated.
2256: .seealso: KSPGetResidualHistory(), KSP
2258: @*/
2259: PetscErrorCode KSPSetResidualHistory(KSP ksp,PetscReal a[],PetscInt na,PetscBool reset)
2260: {
2266: PetscFree(ksp->res_hist_alloc);
2267: if (na != PETSC_DECIDE && na != PETSC_DEFAULT && a) {
2268: ksp->res_hist = a;
2269: ksp->res_hist_max = na;
2270: } else {
2271: if (na != PETSC_DECIDE && na != PETSC_DEFAULT) ksp->res_hist_max = na;
2272: else ksp->res_hist_max = 10000; /* like default ksp->max_it */
2273: PetscCalloc1(ksp->res_hist_max,&ksp->res_hist_alloc);
2275: ksp->res_hist = ksp->res_hist_alloc;
2276: }
2277: ksp->res_hist_len = 0;
2278: ksp->res_hist_reset = reset;
2279: return(0);
2280: }
2282: /*@C
2283: KSPGetResidualHistory - Gets the array used to hold the residual history
2284: and the number of residuals it contains.
2286: Not Collective
2288: Input Parameter:
2289: . ksp - iterative context obtained from KSPCreate()
2291: Output Parameters:
2292: + a - pointer to array to hold history (or NULL)
2293: - na - number of used entries in a (or NULL)
2295: Level: advanced
2297: Notes:
2298: This array is borrowed and should not be freed by the caller.
2299: Can only be called after a KSPSetResidualHistory() otherwise a and na are set to zero
2301: The Fortran version of this routine has a calling sequence
2302: $ call KSPGetResidualHistory(KSP ksp, integer na, integer ierr)
2303: note that you have passed a Fortran array into KSPSetResidualHistory() and you need
2304: to access the residual values from this Fortran array you provided. Only the na (number of
2305: residual norms currently held) is set.
2307: .seealso: KSPSetResidualHistory(), KSP
2309: @*/
2310: PetscErrorCode KSPGetResidualHistory(KSP ksp, const PetscReal *a[],PetscInt *na)
2311: {
2314: if (a) *a = ksp->res_hist;
2315: if (na) *na = ksp->res_hist_len;
2316: return(0);
2317: }
2319: /*@
2320: KSPSetErrorHistory - Sets the array used to hold the error history. If set, this array will contain the error norms computed at each iteration of the solver.
2322: Not Collective
2324: Input Parameters:
2325: + ksp - iterative context obtained from KSPCreate()
2326: . a - array to hold history
2327: . na - size of a
2328: - reset - PETSC_TRUE indicates the history counter is reset to zero for each new linear solve
2330: Level: advanced
2332: Notes:
2333: If provided, the array is NOT freed by PETSc so the user needs to keep track of it and destroy once the KSP object is destroyed.
2334: If 'a' is NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a default array of length 10000 is allocated.
2336: .seealso: KSPGetErrorHistory(), KSPSetResidualHistory(), KSP
2337: @*/
2338: PetscErrorCode KSPSetErrorHistory(KSP ksp, PetscReal a[], PetscInt na, PetscBool reset)
2339: {
2345: PetscFree(ksp->err_hist_alloc);
2346: if (na != PETSC_DECIDE && na != PETSC_DEFAULT && a) {
2347: ksp->err_hist = a;
2348: ksp->err_hist_max = na;
2349: } else {
2350: if (na != PETSC_DECIDE && na != PETSC_DEFAULT) ksp->err_hist_max = na;
2351: else ksp->err_hist_max = 10000; /* like default ksp->max_it */
2352: PetscCalloc1(ksp->err_hist_max, &ksp->err_hist_alloc);
2354: ksp->err_hist = ksp->err_hist_alloc;
2355: }
2356: ksp->err_hist_len = 0;
2357: ksp->err_hist_reset = reset;
2358: return(0);
2359: }
2361: /*@C
2362: KSPGetErrorHistory - Gets the array used to hold the error history and the number of residuals it contains.
2364: Not Collective
2366: Input Parameter:
2367: . ksp - iterative context obtained from KSPCreate()
2369: Output Parameters:
2370: + a - pointer to array to hold history (or NULL)
2371: - na - number of used entries in a (or NULL)
2373: Level: advanced
2375: Notes:
2376: This array is borrowed and should not be freed by the caller.
2377: Can only be called after a KSPSetErrorHistory() otherwise a and na are set to zero
2378: The Fortran version of this routine has a calling sequence
2379: $ call KSPGetErrorHistory(KSP ksp, integer na, integer ierr)
2380: note that you have passed a Fortran array into KSPSetErrorHistory() and you need
2381: to access the residual values from this Fortran array you provided. Only the na (number of
2382: residual norms currently held) is set.
2384: .seealso: KSPSetErrorHistory(), KSPGetResidualHistory(), KSP
2385: @*/
2386: PetscErrorCode KSPGetErrorHistory(KSP ksp, const PetscReal *a[], PetscInt *na)
2387: {
2390: if (a) *a = ksp->err_hist;
2391: if (na) *na = ksp->err_hist_len;
2392: return(0);
2393: }
2395: /*
2396: KSPComputeConvergenceRate - Compute the convergence rate for the iteration
2398: Not collective
2400: Input Parameter:
2401: . ksp - The KSP
2403: Output Parameters:
2404: + cr - The residual contraction rate
2405: . rRsq - The coefficient of determination, R^2, indicating the linearity of the data
2406: . ce - The error contraction rate
2407: - eRsq - The coefficient of determination, R^2, indicating the linearity of the data
2409: Note:
2410: Suppose that the residual is reduced linearly, $r_k = c^k r_0$, which means $log r_k = log r_0 + k log c$. After linear regression,
2411: the slope is $\log c$. The coefficient of determination is given by $1 - \frac{\sum_i (y_i - f(x_i))^2}{\sum_i (y_i - \bar y)}$,
2412: see also https://en.wikipedia.org/wiki/Coefficient_of_determination
2414: Level: advanced
2416: .seealso: KSPConvergedRateView()
2417: */
2418: PetscErrorCode KSPComputeConvergenceRate(KSP ksp, PetscReal *cr, PetscReal *rRsq, PetscReal *ce, PetscReal *eRsq)
2419: {
2420: PetscReal const *hist;
2421: PetscReal *x, *y, slope, intercept, mean = 0.0, var = 0.0, res = 0.0;
2422: PetscInt n, k;
2426: if (cr || rRsq) {
2427: KSPGetResidualHistory(ksp, &hist, &n);
2428: if (!n) {
2429: if (cr) *cr = 0.0;
2430: if (rRsq) *rRsq = -1.0;
2431: } else {
2432: PetscMalloc2(n, &x, n, &y);
2433: for (k = 0; k < n; ++k) {
2434: x[k] = k;
2435: y[k] = PetscLogReal(hist[k]);
2436: mean += y[k];
2437: }
2438: mean /= n;
2439: PetscLinearRegression(n, x, y, &slope, &intercept);
2440: for (k = 0; k < n; ++k) {
2441: res += PetscSqr(y[k] - (slope*x[k] + intercept));
2442: var += PetscSqr(y[k] - mean);
2443: }
2444: PetscFree2(x, y);
2445: if (cr) *cr = PetscExpReal(slope);
2446: if (rRsq) *rRsq = var < PETSC_MACHINE_EPSILON ? 0.0 : 1.0 - (res / var);
2447: }
2448: }
2449: if (ce || eRsq) {
2450: KSPGetErrorHistory(ksp, &hist, &n);
2451: if (!n) {
2452: if (ce) *ce = 0.0;
2453: if (eRsq) *eRsq = -1.0;
2454: } else {
2455: PetscMalloc2(n, &x, n, &y);
2456: for (k = 0; k < n; ++k) {
2457: x[k] = k;
2458: y[k] = PetscLogReal(hist[k]);
2459: mean += y[k];
2460: }
2461: mean /= n;
2462: PetscLinearRegression(n, x, y, &slope, &intercept);
2463: for (k = 0; k < n; ++k) {
2464: res += PetscSqr(y[k] - (slope*x[k] + intercept));
2465: var += PetscSqr(y[k] - mean);
2466: }
2467: PetscFree2(x, y);
2468: if (ce) *ce = PetscExpReal(slope);
2469: if (eRsq) *eRsq = var < PETSC_MACHINE_EPSILON ? 0.0 : 1.0 - (res / var);
2470: }
2471: }
2472: return(0);
2473: }
2475: /*@C
2476: KSPSetConvergenceTest - Sets the function to be used to determine
2477: convergence.
2479: Logically Collective on ksp
2481: Input Parameters:
2482: + ksp - iterative context obtained from KSPCreate()
2483: . converge - pointer to the function
2484: . cctx - context for private data for the convergence routine (may be null)
2485: - destroy - a routine for destroying the context (may be null)
2487: Calling sequence of converge:
2488: $ converge (KSP ksp, PetscInt it, PetscReal rnorm, KSPConvergedReason *reason,void *mctx)
2490: + ksp - iterative context obtained from KSPCreate()
2491: . it - iteration number
2492: . rnorm - (estimated) 2-norm of (preconditioned) residual
2493: . reason - the reason why it has converged or diverged
2494: - cctx - optional convergence context, as set by KSPSetConvergenceTest()
2496: Notes:
2497: Must be called after the KSP type has been set so put this after
2498: a call to KSPSetType(), or KSPSetFromOptions().
2500: The default convergence test, KSPConvergedDefault(), aborts if the
2501: residual grows to more than 10000 times the initial residual.
2503: The default is a combination of relative and absolute tolerances.
2504: The residual value that is tested may be an approximation; routines
2505: that need exact values should compute them.
2507: In the default PETSc convergence test, the precise values of reason
2508: are macros such as KSP_CONVERGED_RTOL, which are defined in petscksp.h.
2510: Level: advanced
2512: .seealso: KSPConvergedDefault(), KSPGetConvergenceContext(), KSPSetTolerances(), KSP, KSPGetConvergenceTest(), KSPGetAndClearConvergenceTest()
2513: @*/
2514: PetscErrorCode KSPSetConvergenceTest(KSP ksp,PetscErrorCode (*converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
2515: {
2520: if (ksp->convergeddestroy) {
2521: (*ksp->convergeddestroy)(ksp->cnvP);
2522: }
2523: ksp->converged = converge;
2524: ksp->convergeddestroy = destroy;
2525: ksp->cnvP = (void*)cctx;
2526: return(0);
2527: }
2529: /*@C
2530: KSPGetConvergenceTest - Gets the function to be used to determine
2531: convergence.
2533: Logically Collective on ksp
2535: Input Parameter:
2536: . ksp - iterative context obtained from KSPCreate()
2538: Output Parameters:
2539: + converge - pointer to convergence test function
2540: . cctx - context for private data for the convergence routine (may be null)
2541: - destroy - a routine for destroying the context (may be null)
2543: Calling sequence of converge:
2544: $ converge (KSP ksp, PetscInt it, PetscReal rnorm, KSPConvergedReason *reason,void *mctx)
2546: + ksp - iterative context obtained from KSPCreate()
2547: . it - iteration number
2548: . rnorm - (estimated) 2-norm of (preconditioned) residual
2549: . reason - the reason why it has converged or diverged
2550: - cctx - optional convergence context, as set by KSPSetConvergenceTest()
2552: Level: advanced
2554: .seealso: KSPConvergedDefault(), KSPGetConvergenceContext(), KSPSetTolerances(), KSP, KSPSetConvergenceTest(), KSPGetAndClearConvergenceTest()
2555: @*/
2556: PetscErrorCode KSPGetConvergenceTest(KSP ksp,PetscErrorCode (**converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*),void **cctx,PetscErrorCode (**destroy)(void*))
2557: {
2560: if (converge) *converge = ksp->converged;
2561: if (destroy) *destroy = ksp->convergeddestroy;
2562: if (cctx) *cctx = ksp->cnvP;
2563: return(0);
2564: }
2566: /*@C
2567: KSPGetAndClearConvergenceTest - Gets the function to be used to determine convergence. Removes the current test without calling destroy on the test context
2569: Logically Collective on ksp
2571: Input Parameter:
2572: . ksp - iterative context obtained from KSPCreate()
2574: Output Parameters:
2575: + converge - pointer to convergence test function
2576: . cctx - context for private data for the convergence routine
2577: - destroy - a routine for destroying the context
2579: Calling sequence of converge:
2580: $ converge (KSP ksp, PetscInt it, PetscReal rnorm, KSPConvergedReason *reason,void *mctx)
2582: + ksp - iterative context obtained from KSPCreate()
2583: . it - iteration number
2584: . rnorm - (estimated) 2-norm of (preconditioned) residual
2585: . reason - the reason why it has converged or diverged
2586: - cctx - optional convergence context, as set by KSPSetConvergenceTest()
2588: Level: advanced
2590: Notes: This is intended to be used to allow transferring the convergence test (and its context) to another testing object (for example another KSP) and then calling
2591: KSPSetConvergenceTest() on this original KSP. If you just called KSPGetConvergenceTest() followed by KSPSetConvergenceTest() the original context information
2592: would be destroyed and hence the transferred context would be invalid and trigger a crash on use
2594: .seealso: KSPConvergedDefault(), KSPGetConvergenceContext(), KSPSetTolerances(), KSP, KSPSetConvergenceTest(), KSPGetConvergenceTest()
2595: @*/
2596: PetscErrorCode KSPGetAndClearConvergenceTest(KSP ksp,PetscErrorCode (**converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*),void **cctx,PetscErrorCode (**destroy)(void*))
2597: {
2600: *converge = ksp->converged;
2601: *destroy = ksp->convergeddestroy;
2602: *cctx = ksp->cnvP;
2603: ksp->converged = NULL;
2604: ksp->cnvP = NULL;
2605: ksp->convergeddestroy = NULL;
2606: return(0);
2607: }
2609: /*@C
2610: KSPGetConvergenceContext - Gets the convergence context set with
2611: KSPSetConvergenceTest().
2613: Not Collective
2615: Input Parameter:
2616: . ksp - iterative context obtained from KSPCreate()
2618: Output Parameter:
2619: . ctx - monitoring context
2621: Level: advanced
2623: .seealso: KSPConvergedDefault(), KSPSetConvergenceTest(), KSP
2624: @*/
2625: PetscErrorCode KSPGetConvergenceContext(KSP ksp,void *ctx)
2626: {
2629: *(void**)ctx = ksp->cnvP;
2630: return(0);
2631: }
2633: /*@C
2634: KSPBuildSolution - Builds the approximate solution in a vector provided.
2635: This routine is NOT commonly needed (see KSPSolve()).
2637: Collective on ksp
2639: Input Parameter:
2640: . ctx - iterative context obtained from KSPCreate()
2642: Output Parameter:
2643: Provide exactly one of
2644: + v - location to stash solution.
2645: - V - the solution is returned in this location. This vector is created
2646: internally. This vector should NOT be destroyed by the user with
2647: VecDestroy().
2649: Notes:
2650: This routine can be used in one of two ways
2651: .vb
2652: KSPBuildSolution(ksp,NULL,&V);
2653: or
2654: KSPBuildSolution(ksp,v,NULL); or KSPBuildSolution(ksp,v,&v);
2655: .ve
2656: In the first case an internal vector is allocated to store the solution
2657: (the user cannot destroy this vector). In the second case the solution
2658: is generated in the vector that the user provides. Note that for certain
2659: methods, such as KSPCG, the second case requires a copy of the solution,
2660: while in the first case the call is essentially free since it simply
2661: returns the vector where the solution already is stored. For some methods
2662: like GMRES this is a reasonably expensive operation and should only be
2663: used in truly needed.
2665: Level: advanced
2667: .seealso: KSPGetSolution(), KSPBuildResidual(), KSP
2668: @*/
2669: PetscErrorCode KSPBuildSolution(KSP ksp,Vec v,Vec *V)
2670: {
2675: if (!V && !v) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_WRONG,"Must provide either v or V");
2676: if (!V) V = &v;
2677: (*ksp->ops->buildsolution)(ksp,v,V);
2678: return(0);
2679: }
2681: /*@C
2682: KSPBuildResidual - Builds the residual in a vector provided.
2684: Collective on ksp
2686: Input Parameter:
2687: . ksp - iterative context obtained from KSPCreate()
2689: Output Parameters:
2690: + v - optional location to stash residual. If v is not provided,
2691: then a location is generated.
2692: . t - work vector. If not provided then one is generated.
2693: - V - the residual
2695: Notes:
2696: Regardless of whether or not v is provided, the residual is
2697: returned in V.
2699: Level: advanced
2701: .seealso: KSPBuildSolution()
2702: @*/
2703: PetscErrorCode KSPBuildResidual(KSP ksp,Vec t,Vec v,Vec *V)
2704: {
2706: PetscBool flag = PETSC_FALSE;
2707: Vec w = v,tt = t;
2711: if (!w) {
2712: VecDuplicate(ksp->vec_rhs,&w);
2713: PetscLogObjectParent((PetscObject)ksp,(PetscObject)w);
2714: }
2715: if (!tt) {
2716: VecDuplicate(ksp->vec_sol,&tt); flag = PETSC_TRUE;
2717: PetscLogObjectParent((PetscObject)ksp,(PetscObject)tt);
2718: }
2719: (*ksp->ops->buildresidual)(ksp,tt,w,V);
2720: if (flag) {VecDestroy(&tt);}
2721: return(0);
2722: }
2724: /*@
2725: KSPSetDiagonalScale - Tells KSP to symmetrically diagonally scale the system
2726: before solving. This actually CHANGES the matrix (and right hand side).
2728: Logically Collective on ksp
2730: Input Parameters:
2731: + ksp - the KSP context
2732: - scale - PETSC_TRUE or PETSC_FALSE
2734: Options Database Key:
2735: + -ksp_diagonal_scale -
2736: - -ksp_diagonal_scale_fix - scale the matrix back AFTER the solve
2738: Notes:
2739: Scales the matrix by D^(-1/2) A D^(-1/2) [D^(1/2) x ] = D^(-1/2) b
2740: where D_{ii} is 1/abs(A_{ii}) unless A_{ii} is zero and then it is 1.
2742: BE CAREFUL with this routine: it actually scales the matrix and right
2743: hand side that define the system. After the system is solved the matrix
2744: and right hand side remain scaled unless you use KSPSetDiagonalScaleFix()
2746: This should NOT be used within the SNES solves if you are using a line
2747: search.
2749: If you use this with the PCType Eisenstat preconditioner than you can
2750: use the PCEisenstatSetNoDiagonalScaling() option, or -pc_eisenstat_no_diagonal_scaling
2751: to save some unneeded, redundant flops.
2753: Level: intermediate
2755: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScaleFix(), KSP
2756: @*/
2757: PetscErrorCode KSPSetDiagonalScale(KSP ksp,PetscBool scale)
2758: {
2762: ksp->dscale = scale;
2763: return(0);
2764: }
2766: /*@
2767: KSPGetDiagonalScale - Checks if KSP solver scales the matrix and
2768: right hand side
2770: Not Collective
2772: Input Parameter:
2773: . ksp - the KSP context
2775: Output Parameter:
2776: . scale - PETSC_TRUE or PETSC_FALSE
2778: Notes:
2779: BE CAREFUL with this routine: it actually scales the matrix and right
2780: hand side that define the system. After the system is solved the matrix
2781: and right hand side remain scaled unless you use KSPSetDiagonalScaleFix()
2783: Level: intermediate
2785: .seealso: KSPSetDiagonalScale(), KSPSetDiagonalScaleFix(), KSP
2786: @*/
2787: PetscErrorCode KSPGetDiagonalScale(KSP ksp,PetscBool *scale)
2788: {
2792: *scale = ksp->dscale;
2793: return(0);
2794: }
2796: /*@
2797: KSPSetDiagonalScaleFix - Tells KSP to diagonally scale the system
2798: back after solving.
2800: Logically Collective on ksp
2802: Input Parameters:
2803: + ksp - the KSP context
2804: - fix - PETSC_TRUE to scale back after the system solve, PETSC_FALSE to not
2805: rescale (default)
2807: Notes:
2808: Must be called after KSPSetDiagonalScale()
2810: Using this will slow things down, because it rescales the matrix before and
2811: after each linear solve. This is intended mainly for testing to allow one
2812: to easily get back the original system to make sure the solution computed is
2813: accurate enough.
2815: Level: intermediate
2817: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScale(), KSPGetDiagonalScaleFix(), KSP
2818: @*/
2819: PetscErrorCode KSPSetDiagonalScaleFix(KSP ksp,PetscBool fix)
2820: {
2824: ksp->dscalefix = fix;
2825: return(0);
2826: }
2828: /*@
2829: KSPGetDiagonalScaleFix - Determines if KSP diagonally scales the system
2830: back after solving.
2832: Not Collective
2834: Input Parameter:
2835: . ksp - the KSP context
2837: Output Parameter:
2838: . fix - PETSC_TRUE to scale back after the system solve, PETSC_FALSE to not
2839: rescale (default)
2841: Notes:
2842: Must be called after KSPSetDiagonalScale()
2844: If PETSC_TRUE will slow things down, because it rescales the matrix before and
2845: after each linear solve. This is intended mainly for testing to allow one
2846: to easily get back the original system to make sure the solution computed is
2847: accurate enough.
2849: Level: intermediate
2851: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScale(), KSPSetDiagonalScaleFix(), KSP
2852: @*/
2853: PetscErrorCode KSPGetDiagonalScaleFix(KSP ksp,PetscBool *fix)
2854: {
2858: *fix = ksp->dscalefix;
2859: return(0);
2860: }
2862: /*@C
2863: KSPSetComputeOperators - set routine to compute the linear operators
2865: Logically Collective
2867: Input Parameters:
2868: + ksp - the KSP context
2869: . func - function to compute the operators
2870: - ctx - optional context
2872: Calling sequence of func:
2873: $ func(KSP ksp,Mat A,Mat B,void *ctx)
2875: + ksp - the KSP context
2876: . A - the linear operator
2877: . B - preconditioning matrix
2878: - ctx - optional user-provided context
2880: Notes:
2881: The user provided func() will be called automatically at the very next call to KSPSolve(). It will not be called at future KSPSolve() calls
2882: unless either KSPSetComputeOperators() or KSPSetOperators() is called before that KSPSolve() is called.
2884: To reuse the same preconditioner for the next KSPSolve() and not compute a new one based on the most recently computed matrix call KSPSetReusePreconditioner()
2886: Level: beginner
2888: .seealso: KSPSetOperators(), KSPSetComputeRHS(), DMKSPSetComputeOperators(), KSPSetComputeInitialGuess()
2889: @*/
2890: PetscErrorCode KSPSetComputeOperators(KSP ksp,PetscErrorCode (*func)(KSP,Mat,Mat,void*),void *ctx)
2891: {
2893: DM dm;
2897: KSPGetDM(ksp,&dm);
2898: DMKSPSetComputeOperators(dm,func,ctx);
2899: if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX;
2900: return(0);
2901: }
2903: /*@C
2904: KSPSetComputeRHS - set routine to compute the right hand side of the linear system
2906: Logically Collective
2908: Input Parameters:
2909: + ksp - the KSP context
2910: . func - function to compute the right hand side
2911: - ctx - optional context
2913: Calling sequence of func:
2914: $ func(KSP ksp,Vec b,void *ctx)
2916: + ksp - the KSP context
2917: . b - right hand side of linear system
2918: - ctx - optional user-provided context
2920: Notes:
2921: The routine you provide will be called EACH you call KSPSolve() to prepare the new right hand side for that solve
2923: Level: beginner
2925: .seealso: KSPSolve(), DMKSPSetComputeRHS(), KSPSetComputeOperators()
2926: @*/
2927: PetscErrorCode KSPSetComputeRHS(KSP ksp,PetscErrorCode (*func)(KSP,Vec,void*),void *ctx)
2928: {
2930: DM dm;
2934: KSPGetDM(ksp,&dm);
2935: DMKSPSetComputeRHS(dm,func,ctx);
2936: return(0);
2937: }
2939: /*@C
2940: KSPSetComputeInitialGuess - set routine to compute the initial guess of the linear system
2942: Logically Collective
2944: Input Parameters:
2945: + ksp - the KSP context
2946: . func - function to compute the initial guess
2947: - ctx - optional context
2949: Calling sequence of func:
2950: $ func(KSP ksp,Vec x,void *ctx)
2952: + ksp - the KSP context
2953: . x - solution vector
2954: - ctx - optional user-provided context
2956: Notes: This should only be used in conjunction with KSPSetComputeRHS(), KSPSetComputeOperators(), otherwise
2957: call KSPSetInitialGuessNonzero() and set the initial guess values in the solution vector passed to KSPSolve().
2959: Level: beginner
2961: .seealso: KSPSolve(), KSPSetComputeRHS(), KSPSetComputeOperators(), DMKSPSetComputeInitialGuess()
2962: @*/
2963: PetscErrorCode KSPSetComputeInitialGuess(KSP ksp,PetscErrorCode (*func)(KSP,Vec,void*),void *ctx)
2964: {
2966: DM dm;
2970: KSPGetDM(ksp,&dm);
2971: DMKSPSetComputeInitialGuess(dm,func,ctx);
2972: return(0);
2973: }
2975: /*@
2976: KSPSetUseExplicitTranspose - Determines if transpose the system explicitly
2977: in KSPSolveTranspose.
2979: Logically Collective on ksp
2981: Input Parameter:
2982: . ksp - the KSP context
2984: Output Parameter:
2985: . flg - PETSC_TRUE to transpose the system in KSPSolveTranspose, PETSC_FALSE to not
2986: transpose (default)
2988: Level: advanced
2990: .seealso: KSPSolveTranspose(), KSP
2991: @*/
2992: PetscErrorCode KSPSetUseExplicitTranspose(KSP ksp,PetscBool flg)
2993: {
2997: ksp->transpose.use_explicittranspose = flg;
2998: return(0);
2999: }