Actual source code: itfunc.c
1: #define PETSCKSP_DLL
3: /*
4: Interface KSP routines that the user calls.
5: */
7: #include src/ksp/ksp/kspimpl.h
11: /*@
12: KSPComputeExtremeSingularValues - Computes the extreme singular values
13: for the preconditioned operator. Called after or during KSPSolve().
15: Not Collective
17: Input Parameter:
18: . ksp - iterative context obtained from KSPCreate()
20: Output Parameters:
21: . emin, emax - extreme singular values
23: Notes:
24: One must call KSPSetComputeSingularValues() before calling KSPSetUp()
25: (or use the option -ksp_compute_eigenvalues) in order for this routine to work correctly.
27: Many users may just want to use the monitoring routine
28: KSPSingularValueMonitor() (which can be set with option -ksp_singmonitor)
29: to print the extreme singular values at each iteration of the linear solve.
31: Level: advanced
33: .keywords: KSP, compute, extreme, singular, values
35: .seealso: KSPSetComputeSingularValues(), KSPSingularValueMonitor(), KSPComputeEigenvalues()
36: @*/
37: PetscErrorCode PETSCKSP_DLLEXPORT KSPComputeExtremeSingularValues(KSP ksp,PetscReal *emax,PetscReal *emin)
38: {
45: if (!ksp->calc_sings) {
46: SETERRQ(4,"Singular values not requested before KSPSetUp()");
47: }
49: if (ksp->ops->computeextremesingularvalues) {
50: (*ksp->ops->computeextremesingularvalues)(ksp,emax,emin);
51: } else {
52: *emin = -1.0;
53: *emax = -1.0;
54: }
55: return(0);
56: }
60: /*@
61: KSPComputeEigenvalues - Computes the extreme eigenvalues for the
62: preconditioned operator. Called after or during KSPSolve().
64: Not Collective
66: Input Parameter:
67: + ksp - iterative context obtained from KSPCreate()
68: - n - size of arrays r and c. The number of eigenvalues computed (neig) will, in
69: general, be less than this.
71: Output Parameters:
72: + r - real part of computed eigenvalues
73: . c - complex part of computed eigenvalues
74: - neig - number of eigenvalues computed (will be less than or equal to n)
76: Options Database Keys:
77: + -ksp_compute_eigenvalues - Prints eigenvalues to stdout
78: - -ksp_plot_eigenvalues - Plots eigenvalues in an x-window display
80: Notes:
81: The number of eigenvalues estimated depends on the size of the Krylov space
82: generated during the KSPSolve() ; for example, with
83: CG it corresponds to the number of CG iterations, for GMRES it is the number
84: of GMRES iterations SINCE the last restart. Any extra space in r[] and c[]
85: will be ignored.
87: KSPComputeEigenvalues() does not usually provide accurate estimates; it is
88: intended only for assistance in understanding the convergence of iterative
89: methods, not for eigenanalysis.
91: One must call KSPSetComputeEigenvalues() before calling KSPSetUp()
92: in order for this routine to work correctly.
94: Many users may just want to use the monitoring routine
95: KSPSingularValueMonitor() (which can be set with option -ksp_singmonitor)
96: to print the singular values at each iteration of the linear solve.
98: Level: advanced
100: .keywords: KSP, compute, extreme, singular, values
102: .seealso: KSPSetComputeSingularValues(), KSPSingularValueMonitor(), KSPComputeExtremeSingularValues()
103: @*/
104: PetscErrorCode PETSCKSP_DLLEXPORT KSPComputeEigenvalues(KSP ksp,PetscInt n,PetscReal *r,PetscReal *c,PetscInt *neig)
105: {
113: if (!ksp->calc_sings) {
114: SETERRQ(4,"Eigenvalues not requested before KSPSetUp()");
115: }
117: if (ksp->ops->computeeigenvalues) {
118: (*ksp->ops->computeeigenvalues)(ksp,n,r,c,neig);
119: } else {
120: *neig = 0;
121: }
122: return(0);
123: }
127: /*@
128: KSPSetUpOnBlocks - Sets up the preconditioner for each block in
129: the block Jacobi, block Gauss-Seidel, and overlapping Schwarz
130: methods.
132: Collective on KSP
134: Input Parameter:
135: . ksp - the KSP context
137: Notes:
138: KSPSetUpOnBlocks() is a routine that the user can optinally call for
139: more precise profiling (via -log_summary) of the setup phase for these
140: block preconditioners. If the user does not call KSPSetUpOnBlocks(),
141: it will automatically be called from within KSPSolve().
142:
143: Calling KSPSetUpOnBlocks() is the same as calling PCSetUpOnBlocks()
144: on the PC context within the KSP context.
146: Level: advanced
148: .keywords: KSP, setup, blocks
150: .seealso: PCSetUpOnBlocks(), KSPSetUp(), PCSetUp()
151: @*/
152: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetUpOnBlocks(KSP ksp)
153: {
158: PCSetUpOnBlocks(ksp->pc);
159: return(0);
160: }
164: /*@
165: KSPSetUp - Sets up the internal data structures for the
166: later use of an iterative solver.
168: Collective on KSP
170: Input Parameter:
171: . ksp - iterative context obtained from KSPCreate()
173: Level: developer
175: .keywords: KSP, setup
177: .seealso: KSPCreate(), KSPSolve(), KSPDestroy()
178: @*/
179: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetUp(KSP ksp)
180: {
186: /* reset the convergence flag from the previous solves */
187: ksp->reason = KSP_CONVERGED_ITERATING;
189: if (!ksp->type_name){
190: KSPSetType(ksp,KSPGMRES);
191: }
193: if (ksp->setupcalled == 2) return(0);
195: PetscLogEventBegin(KSP_SetUp,ksp,ksp->vec_rhs,ksp->vec_sol,0);
197: if (!ksp->setupcalled) {
198: (*ksp->ops->setup)(ksp);
199: }
201: /* scale the matrix if requested */
202: if (ksp->dscale) {
203: Mat mat,pmat;
204: PCGetOperators(ksp->pc,&mat,&pmat,PETSC_NULL);
205: if (mat == pmat) {
206: PetscScalar *xx;
207: PetscInt i,n;
208: PetscTruth zeroflag = PETSC_FALSE;
210: if (!ksp->diagonal) { /* allocate vector to hold diagonal */
211: MatGetVecs(pmat,&ksp->diagonal,0);
212: }
213: MatGetDiagonal(mat,ksp->diagonal);
214: VecGetLocalSize(ksp->diagonal,&n);
215: VecGetArray(ksp->diagonal,&xx);
216: for (i=0; i<n; i++) {
217: if (xx[i] != 0.0) xx[i] = 1.0/sqrt(PetscAbsScalar(xx[i]));
218: else {
219: xx[i] = 1.0;
220: zeroflag = PETSC_TRUE;
221: }
222: }
223: VecRestoreArray(ksp->diagonal,&xx);
224: if (zeroflag) {
225: PetscLogInfo((ksp,"KSPSetUp:Zero detected in diagonal of matrix, using 1 at those locations\n"));
226: }
227: MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);
228: ksp->dscalefix2 = PETSC_FALSE;
229: } else {
230: SETERRQ(PETSC_ERR_SUP,"No support for diagonal scaling of linear system if preconditioner matrix not actual matrix");
231: }
232: }
233: PetscLogEventEnd(KSP_SetUp,ksp,ksp->vec_rhs,ksp->vec_sol,0);
234: PCSetUp(ksp->pc);
235: if (ksp->nullsp) {
236: PetscTruth test;
237: PetscOptionsHasName(ksp->prefix,"-ksp_test_null_space",&test);
238: if (test) {
239: Mat mat;
240: PCGetOperators(ksp->pc,&mat,PETSC_NULL,PETSC_NULL);
241: MatNullSpaceTest(ksp->nullsp,mat);
242: }
243: }
244: ksp->setupcalled = 2;
245: return(0);
246: }
250: /*@
251: KSPSolve - Solves linear system.
253: Collective on KSP
255: Parameter:
256: + ksp - iterative context obtained from KSPCreate()
257: . b - the right hand side vector
258: - x - the solution
260: Options Database Keys:
261: + -ksp_compute_eigenvalues - compute preconditioned operators eigenvalues
262: . -ksp_plot_eigenvalues - plot the computed eigenvalues in an X-window
263: . -ksp_compute_eigenvalues_explicitly - compute the eigenvalues by forming the dense operator and useing LAPACK
264: . -ksp_plot_eigenvalues_explicitly - plot the explicitly computing eigenvalues
265: . -ksp_view_binary - save matrix and right hand side that define linear system to the default binary viewer (can be
266: read later with src/ksp/examples/tutorials/ex10.c for testing solvers)
267: . -ksp_converged_reason - print reason for converged or diverged
268: . -ksp_final_residual - print 2-norm of true linear system residual at the end of the solution process
269: - -ksp_view - print the ksp data structure at the end of the system solution
271: Notes:
273: The operator is specified with PCSetOperators().
275: Call KSPGetConvergedReason() to determine if the solver converged or failed and
276: why. The number of iterations can be obtained from KSPGetIterationNumber().
277:
278: If using a direct method (e.g., via the KSP solver
279: KSPPREONLY and a preconditioner such as PCLU/PCILU),
280: then its=1. See KSPSetTolerances() and KSPDefaultConverged()
281: for more details.
283: Understanding Convergence:
284: The routines KSPSetMonitor(), KSPComputeEigenvalues(), and
285: KSPComputeEigenvaluesExplicitly() provide information on additional
286: options to monitor convergence and print eigenvalue information.
288: Level: beginner
290: .keywords: KSP, solve, linear system
292: .seealso: KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPDefaultConverged(),
293: KSPSolveTranspose(), KSPGetIterationNumber()
294: @*/
295: PetscErrorCode PETSCKSP_DLLEXPORT KSPSolve(KSP ksp,Vec b,Vec x)
296: {
298: PetscMPIInt rank;
299: PetscTruth flag1,flag2,viewed=PETSC_FALSE,flg;
300: char view[10];
301: PetscScalar zero = 0.0;
307:
308: ksp->vec_rhs = b;
309: ksp->vec_sol = x;
310: PetscOptionsHasName(ksp->prefix,"-ksp_view_binary",&flg);
311: if (flg) {
312: Mat mat;
313: PCGetOperators(ksp->pc,&mat,PETSC_NULL,PETSC_NULL);
314: MatView(mat,PETSC_VIEWER_BINARY_(ksp->comm));
315: VecView(ksp->vec_rhs,PETSC_VIEWER_BINARY_(ksp->comm));
316: }
318: PetscLogEventBegin(KSP_Solve,ksp,ksp->vec_rhs,ksp->vec_sol,0);
320: /* reset the residual history list if requested */
321: if (ksp->res_hist_reset) ksp->res_hist_len = 0;
323: PetscOptionsGetString(ksp->prefix,"-ksp_view",view,10,&flg);
324: if (flg) {
325: PetscStrcmp(view,"before",&viewed);
326: if (viewed){
327: KSPView(ksp,PETSC_VIEWER_STDOUT_(ksp->comm));
328: }
329: }
331: /* KSPSetUp() scales the matrix if needed */
332: KSPSetUp(ksp);
333: KSPSetUpOnBlocks(ksp);
335: ksp->transpose_solve = PETSC_FALSE;
337: /* diagonal scale RHS if called for */
338: if (ksp->dscale) {
339: VecPointwiseMult(ksp->vec_rhs,ksp->vec_rhs,ksp->diagonal);
340: /* second time in, but matrix was scaled back to original */
341: if (ksp->dscalefix && ksp->dscalefix2) {
342: Mat mat;
344: PCGetOperators(ksp->pc,&mat,PETSC_NULL,PETSC_NULL);
345: MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);
346: }
348: /* scale initial guess */
349: if (!ksp->guess_zero) {
350: if (!ksp->truediagonal) {
351: VecDuplicate(ksp->diagonal,&ksp->truediagonal);
352: VecCopy(ksp->diagonal,ksp->truediagonal);
353: VecReciprocal(ksp->truediagonal);
354: }
355: VecPointwiseMult(ksp->vec_sol,ksp->vec_sol,ksp->truediagonal);
356: }
357: }
358: PCPreSolve(ksp->pc,ksp);
360: if (ksp->guess_zero) { VecSet(ksp->vec_sol,zero);}
361: if (ksp->guess_knoll) {
362: PCApply(ksp->pc,ksp->vec_rhs,ksp->vec_sol);
363: KSP_RemoveNullSpace(ksp,ksp->vec_sol);
364: ksp->guess_zero = PETSC_FALSE;
365: }
366: (*ksp->ops->solve)(ksp);
367: if (!ksp->reason) {
368: SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
369: }
370: if (ksp->printreason) {
371: if (ksp->reason > 0) {
372: PetscPrintf(ksp->comm,"Linear solve converged due to %s\n",KSPConvergedReasons[ksp->reason]);
373: } else {
374: PetscPrintf(ksp->comm,"Linear solve did not converge due to %s\n",KSPConvergedReasons[ksp->reason]);
375: }
376: }
378: /* diagonal scale solution if called for */
379: PCPostSolve(ksp->pc,ksp);
380: if (ksp->dscale) {
381: VecPointwiseMult(ksp->vec_sol,ksp->vec_sol,ksp->diagonal);
382: /* unscale right hand side and matrix */
383: if (ksp->dscalefix) {
384: Mat mat;
386: VecReciprocal(ksp->diagonal);
387: VecPointwiseMult(ksp->vec_rhs,ksp->vec_rhs,ksp->diagonal);
388: PCGetOperators(ksp->pc,&mat,PETSC_NULL,PETSC_NULL);
389: MatDiagonalScale(mat,ksp->diagonal,ksp->diagonal);
390: VecReciprocal(ksp->diagonal);
391: ksp->dscalefix2 = PETSC_TRUE;
392: }
393: }
394: PetscLogEventEnd(KSP_Solve,ksp,ksp->vec_rhs,ksp->vec_sol,0);
396: MPI_Comm_rank(ksp->comm,&rank);
398: PetscOptionsHasName(ksp->prefix,"-ksp_compute_eigenvalues",&flag1);
399: PetscOptionsHasName(ksp->prefix,"-ksp_plot_eigenvalues",&flag2);
400: if (flag1 || flag2) {
401: PetscInt nits,n,i,neig;
402: PetscReal *r,*c;
403:
404: KSPGetIterationNumber(ksp,&nits);
405: n = nits+2;
407: if (!n) {
408: PetscPrintf(ksp->comm,"Zero iterations in solver, cannot approximate any eigenvalues\n");
409: } else {
410: PetscMalloc(2*n*sizeof(PetscReal),&r);
411: c = r + n;
412: KSPComputeEigenvalues(ksp,n,r,c,&neig);
413: if (flag1) {
414: PetscPrintf(ksp->comm,"Iteratively computed eigenvalues\n");
415: for (i=0; i<neig; i++) {
416: if (c[i] >= 0.0) {PetscPrintf(ksp->comm,"%g + %gi\n",r[i],c[i]);}
417: else {PetscPrintf(ksp->comm,"%g - %gi\n",r[i],-c[i]);}
418: }
419: }
420: if (flag2 && !rank) {
421: PetscViewer viewer;
422: PetscDraw draw;
423: PetscDrawSP drawsp;
425: PetscViewerDrawOpen(PETSC_COMM_SELF,0,"Iteratively Computed Eigenvalues",
426: PETSC_DECIDE,PETSC_DECIDE,300,300,&viewer);
427: PetscViewerDrawGetDraw(viewer,0,&draw);
428: PetscDrawSPCreate(draw,1,&drawsp);
429: for (i=0; i<neig; i++) {
430: PetscDrawSPAddPoint(drawsp,r+i,c+i);
431: }
432: PetscDrawSPDraw(drawsp);
433: PetscDrawSPDestroy(drawsp);
434: PetscViewerDestroy(viewer);
435: }
436: PetscFree(r);
437: }
438: }
440: PetscOptionsHasName(ksp->prefix,"-ksp_compute_eigenvalues_explicitly",&flag1);
441: PetscOptionsHasName(ksp->prefix,"-ksp_plot_eigenvalues_explicitly",&flag2);
442: if (flag1 || flag2) {
443: PetscInt n,i;
444: PetscReal *r,*c;
445: VecGetSize(ksp->vec_sol,&n);
446: PetscMalloc(2*n*sizeof(PetscReal),&r);
447: c = r + n;
448: KSPComputeEigenvaluesExplicitly(ksp,n,r,c);
449: if (flag1) {
450: PetscPrintf(ksp->comm,"Explicitly computed eigenvalues\n");
451: for (i=0; i<n; i++) {
452: if (c[i] >= 0.0) {PetscPrintf(ksp->comm,"%g + %gi\n",r[i],c[i]);}
453: else {PetscPrintf(ksp->comm,"%g - %gi\n",r[i],-c[i]);}
454: }
455: }
456: if (flag2 && !rank) {
457: PetscViewer viewer;
458: PetscDraw draw;
459: PetscDrawSP drawsp;
461: PetscViewerDrawOpen(PETSC_COMM_SELF,0,"Explicitly Computed Eigenvalues",0,320,300,300,&viewer);
462: PetscViewerDrawGetDraw(viewer,0,&draw);
463: PetscDrawSPCreate(draw,1,&drawsp);
464: for (i=0; i<n; i++) {
465: PetscDrawSPAddPoint(drawsp,r+i,c+i);
466: }
467: PetscDrawSPDraw(drawsp);
468: PetscDrawSPDestroy(drawsp);
469: PetscViewerDestroy(viewer);
470: }
471: PetscFree(r);
472: }
474: PetscOptionsHasName(ksp->prefix,"-ksp_view_operator",&flag2);
475: if (flag2) {
476: Mat A,B;
477: PCGetOperators(ksp->pc,&A,PETSC_NULL,PETSC_NULL);
478: MatComputeExplicitOperator(A,&B);
479: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(ksp->comm),PETSC_VIEWER_ASCII_MATLAB);
480: MatView(B,PETSC_VIEWER_STDOUT_(ksp->comm));
481: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(ksp->comm));
482: MatDestroy(B);
483: }
484: PetscOptionsHasName(ksp->prefix,"-ksp_view_operator_binary",&flag2);
485: if (flag2) {
486: Mat A,B;
487: PCGetOperators(ksp->pc,&A,PETSC_NULL,PETSC_NULL);
488: MatComputeExplicitOperator(A,&B);
489: MatView(B,PETSC_VIEWER_BINARY_(ksp->comm));
490: MatDestroy(B);
491: }
492: PetscOptionsHasName(ksp->prefix,"-ksp_view_preconditioned_operator_binary",&flag2);
493: if (flag2) {
494: Mat B;
495: KSPComputeExplicitOperator(ksp,&B);
496: MatView(B,PETSC_VIEWER_BINARY_(ksp->comm));
497: MatDestroy(B);
498: }
499: if (!viewed) {
500: PetscOptionsHasName(ksp->prefix,"-ksp_view",&flg);
501: if (flg) {
502: KSPView(ksp,PETSC_VIEWER_STDOUT_(ksp->comm));
503: }
504: }
505: PetscOptionsHasName(ksp->prefix,"-ksp_final_residual",&flg);
506: if (flg) {
507: Mat A;
508: Vec t;
509: PetscScalar mone = -1.0;
510: PetscReal norm;
511: if (ksp->dscale && !ksp->dscalefix) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot compute final scale with -ksp_diagonal_scale except also with -ksp_diagonal_scale_fix");
512: PCGetOperators(ksp->pc,&A,0,0);
513: VecDuplicate(ksp->vec_sol,&t);
514: KSP_MatMult(ksp,A,ksp->vec_sol,t);
515: VecWAXPY(t,mone,t,ksp->vec_rhs);
516: VecNorm(t,NORM_2,&norm);
517: VecDestroy(t);
518: PetscPrintf(ksp->comm,"KSP final norm of residual %g\n",norm);
519: }
520: return(0);
521: }
525: /*@
526: KSPSolveTranspose - Solves the transpose of a linear system. Usually
527: accessed through KSPSolveTranspose().
529: Collective on KSP
531: Input Parameter:
532: + ksp - iterative context obtained from KSPCreate()
533: . b - right hand side vector
534: - x - solution vector
536: Note:
537: Currently only supported by KSPType of KSPPREONLY. This routine is usally
538: only used internally by the BiCG solver on the subblocks in BJacobi and ASM.
540: Level: developer
542: .keywords: KSP, solve, linear system
544: .seealso: KSPCreate(), KSPSetUp(), KSPDestroy(), KSPSetTolerances(), KSPDefaultConverged(),
545: KSPSolve()
546: @*/
547: PetscErrorCode PETSCKSP_DLLEXPORT KSPSolveTranspose(KSP ksp,Vec b,Vec x)
548: {
550: PetscScalar zero = 0.0;
557: ksp->vec_rhs = b;
558: ksp->vec_sol = x;
559: KSPSetUp(ksp);
560: if (ksp->guess_zero) { VecSet(ksp->vec_sol,zero);}
561: ksp->transpose_solve = PETSC_TRUE;
562: (*ksp->ops->solve)(ksp);
563: return(0);
564: }
568: /*@C
569: KSPDestroy - Destroys KSP context.
571: Collective on KSP
573: Input Parameter:
574: . ksp - iterative context obtained from KSPCreate()
576: Level: beginner
578: .keywords: KSP, destroy
580: .seealso: KSPCreate(), KSPSetUp(), KSPSolve()
581: @*/
582: PetscErrorCode PETSCKSP_DLLEXPORT KSPDestroy(KSP ksp)
583: {
585: PetscInt i;
589: if (--ksp->refct > 0) return(0);
591: /* if memory was published with AMS then destroy it */
592: PetscObjectDepublish(ksp);
594: if (ksp->ops->destroy) {
595: (*ksp->ops->destroy)(ksp);
596: }
597: for (i=0; i<ksp->numbermonitors; i++) {
598: if (ksp->monitordestroy[i]) {
599: (*ksp->monitordestroy[i])(ksp->monitorcontext[i]);
600: }
601: }
602: PCDestroy(ksp->pc);
603: if (ksp->diagonal) {VecDestroy(ksp->diagonal);}
604: if (ksp->truediagonal) {VecDestroy(ksp->truediagonal);}
605: if (ksp->nullsp) {MatNullSpaceDestroy(ksp->nullsp);}
606: PetscHeaderDestroy(ksp);
607: return(0);
608: }
612: /*@
613: KSPSetPreconditionerSide - Sets the preconditioning side.
615: Collective on KSP
617: Input Parameter:
618: . ksp - iterative context obtained from KSPCreate()
620: Output Parameter:
621: . side - the preconditioning side, where side is one of
622: .vb
623: PC_LEFT - left preconditioning (default)
624: PC_RIGHT - right preconditioning
625: PC_SYMMETRIC - symmetric preconditioning
626: .ve
628: Options Database Keys:
629: + -ksp_left_pc - Sets left preconditioning
630: . -ksp_right_pc - Sets right preconditioning
631: - -ksp_symmetric_pc - Sets symmetric preconditioning
633: Notes:
634: Left preconditioning is used by default. Symmetric preconditioning is
635: currently available only for the KSPQCG method. Note, however, that
636: symmetric preconditioning can be emulated by using either right or left
637: preconditioning and a pre or post processing step.
639: Level: intermediate
641: .keywords: KSP, set, right, left, symmetric, side, preconditioner, flag
643: .seealso: KSPGetPreconditionerSide()
644: @*/
645: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetPreconditionerSide(KSP ksp,PCSide side)
646: {
649: ksp->pc_side = side;
650: return(0);
651: }
655: /*@C
656: KSPGetPreconditionerSide - Gets the preconditioning side.
658: Not Collective
660: Input Parameter:
661: . ksp - iterative context obtained from KSPCreate()
663: Output Parameter:
664: . side - the preconditioning side, where side is one of
665: .vb
666: PC_LEFT - left preconditioning (default)
667: PC_RIGHT - right preconditioning
668: PC_SYMMETRIC - symmetric preconditioning
669: .ve
671: Level: intermediate
673: .keywords: KSP, get, right, left, symmetric, side, preconditioner, flag
675: .seealso: KSPSetPreconditionerSide()
676: @*/
677: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetPreconditionerSide(KSP ksp,PCSide *side)
678: {
682: *side = ksp->pc_side;
683: return(0);
684: }
688: /*@
689: KSPGetTolerances - Gets the relative, absolute, divergence, and maximum
690: iteration tolerances used by the default KSP convergence tests.
692: Not Collective
694: Input Parameter:
695: . ksp - the Krylov subspace context
696:
697: Output Parameters:
698: + rtol - the relative convergence tolerance
699: . abstol - the absolute convergence tolerance
700: . dtol - the divergence tolerance
701: - maxits - maximum number of iterations
703: Notes:
704: The user can specify PETSC_NULL for any parameter that is not needed.
706: Level: intermediate
708: .keywords: KSP, get, tolerance, absolute, relative, divergence, convergence,
709: maximum, iterations
711: .seealso: KSPSetTolerances()
712: @*/
713: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetTolerances(KSP ksp,PetscReal *rtol,PetscReal *abstol,PetscReal *dtol,PetscInt *maxits)
714: {
717: if (abstol) *abstol = ksp->abstol;
718: if (rtol) *rtol = ksp->rtol;
719: if (dtol) *dtol = ksp->divtol;
720: if (maxits) *maxits = ksp->max_it;
721: return(0);
722: }
726: /*@
727: KSPSetTolerances - Sets the relative, absolute, divergence, and maximum
728: iteration tolerances used by the default KSP convergence testers.
730: Collective on KSP
732: Input Parameters:
733: + ksp - the Krylov subspace context
734: . rtol - the relative convergence tolerance
735: (relative decrease in the residual norm)
736: . abstol - the absolute convergence tolerance
737: (absolute size of the residual norm)
738: . dtol - the divergence tolerance
739: (amount residual can increase before KSPDefaultConverged()
740: concludes that the method is diverging)
741: - maxits - maximum number of iterations to use
743: Options Database Keys:
744: + -ksp_atol <abstol> - Sets abstol
745: . -ksp_rtol <rtol> - Sets rtol
746: . -ksp_divtol <dtol> - Sets dtol
747: - -ksp_max_it <maxits> - Sets maxits
749: Notes:
750: Use PETSC_DEFAULT to retain the default value of any of the tolerances.
752: See KSPDefaultConverged() for details on the use of these parameters
753: in the default convergence test. See also KSPSetConvergenceTest()
754: for setting user-defined stopping criteria.
756: Level: intermediate
758: .keywords: KSP, set, tolerance, absolute, relative, divergence,
759: convergence, maximum, iterations
761: .seealso: KSPGetTolerances(), KSPDefaultConverged(), KSPSetConvergenceTest()
762: @*/
763: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetTolerances(KSP ksp,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits)
764: {
767: if (abstol != PETSC_DEFAULT) ksp->abstol = abstol;
768: if (rtol != PETSC_DEFAULT) ksp->rtol = rtol;
769: if (dtol != PETSC_DEFAULT) ksp->divtol = dtol;
770: if (maxits != PETSC_DEFAULT) ksp->max_it = maxits;
771: return(0);
772: }
776: /*@
777: KSPSetInitialGuessNonzero - Tells the iterative solver that the
778: initial guess is nonzero; otherwise KSP assumes the initial guess
779: is to be zero (and thus zeros it out before solving).
781: Collective on KSP
783: Input Parameters:
784: + ksp - iterative context obtained from KSPCreate()
785: - flg - PETSC_TRUE indicates the guess is non-zero, PETSC_FALSE indicates the guess is zero
787: Level: beginner
789: Notes:
790: If this is not called the X vector is zeroed in the call to KSPSolve().
792: .keywords: KSP, set, initial guess, nonzero
794: .seealso: KSPGetInitialGuessNonzero(), KSPSetInitialGuessKnoll(), KSPGetInitialGuessKnoll()
795: @*/
796: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetInitialGuessNonzero(KSP ksp,PetscTruth flg)
797: {
799: ksp->guess_zero = (PetscTruth)!(int)flg;
800: return(0);
801: }
805: /*@
806: KSPGetInitialGuessNonzero - Determines whether the KSP solver is using
807: a zero initial guess.
809: Not Collective
811: Input Parameter:
812: . ksp - iterative context obtained from KSPCreate()
814: Output Parameter:
815: . flag - PETSC_TRUE if guess is nonzero, else PETSC_FALSE
817: Level: intermediate
819: .keywords: KSP, set, initial guess, nonzero
821: .seealso: KSPSetInitialGuessNonzero(), KSPSetInitialGuessKnoll(), KSPGetInitialGuessKnoll()
822: @*/
823: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetInitialGuessNonzero(KSP ksp,PetscTruth *flag)
824: {
826: if (ksp->guess_zero) *flag = PETSC_FALSE;
827: else *flag = PETSC_TRUE;
828: return(0);
829: }
833: /*@
834: KSPSetInitialGuessKnoll - Tells the iterative solver to use PCApply(pc,b,..) to compute the initial guess (The Knoll trick)
836: Collective on KSP
838: Input Parameters:
839: + ksp - iterative context obtained from KSPCreate()
840: - flg - PETSC_TRUE or PETSC_FALSE
842: Level: advanced
845: .keywords: KSP, set, initial guess, nonzero
847: .seealso: KSPGetInitialGuessKnoll(), KSPSetInitialGuessNonzero(), KSPGetInitialGuessNonzero()
848: @*/
849: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetInitialGuessKnoll(KSP ksp,PetscTruth flg)
850: {
852: ksp->guess_knoll = flg;
853: return(0);
854: }
858: /*@
859: KSPGetInitialGuessKnoll - Determines whether the KSP solver is using the Knoll trick (using PCApply(pc,b,...) to compute
860: the initial guess
862: Not Collective
864: Input Parameter:
865: . ksp - iterative context obtained from KSPCreate()
867: Output Parameter:
868: . flag - PETSC_TRUE if using Knoll trick, else PETSC_FALSE
870: Level: advanced
872: .keywords: KSP, set, initial guess, nonzero
874: .seealso: KSPSetInitialGuessKnoll(), KSPSetInitialGuessNonzero(), KSPGetInitialGuessNonzero()
875: @*/
876: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetInitialGuessKnoll(KSP ksp,PetscTruth *flag)
877: {
879: *flag = ksp->guess_knoll;
880: return(0);
881: }
885: /*@
886: KSPGetComputeSingularValues - Gets the flag indicating whether the extreme singular
887: values will be calculated via a Lanczos or Arnoldi process as the linear
888: system is solved.
890: Collective on KSP
892: Input Parameter:
893: . ksp - iterative context obtained from KSPCreate()
895: Output Parameter:
896: . flg - PETSC_TRUE or PETSC_FALSE
898: Options Database Key:
899: . -ksp_singmonitor - Activates KSPSetComputeSingularValues()
901: Notes:
902: Currently this option is not valid for all iterative methods.
904: Many users may just want to use the monitoring routine
905: KSPSingularValueMonitor() (which can be set with option -ksp_singmonitor)
906: to print the singular values at each iteration of the linear solve.
908: Level: advanced
910: .keywords: KSP, set, compute, singular values
912: .seealso: KSPComputeExtremeSingularValues(), KSPSingularValueMonitor()
913: @*/
914: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetComputeSingularValues(KSP ksp,PetscTruth *flg)
915: {
919: *flg = ksp->calc_sings;
920: return(0);
921: }
925: /*@
926: KSPSetComputeSingularValues - Sets a flag so that the extreme singular
927: values will be calculated via a Lanczos or Arnoldi process as the linear
928: system is solved.
930: Collective on KSP
932: Input Parameters:
933: + ksp - iterative context obtained from KSPCreate()
934: - flg - PETSC_TRUE or PETSC_FALSE
936: Options Database Key:
937: . -ksp_singmonitor - Activates KSPSetComputeSingularValues()
939: Notes:
940: Currently this option is not valid for all iterative methods.
942: Many users may just want to use the monitoring routine
943: KSPSingularValueMonitor() (which can be set with option -ksp_singmonitor)
944: to print the singular values at each iteration of the linear solve.
946: Level: advanced
948: .keywords: KSP, set, compute, singular values
950: .seealso: KSPComputeExtremeSingularValues(), KSPSingularValueMonitor()
951: @*/
952: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetComputeSingularValues(KSP ksp,PetscTruth flg)
953: {
956: ksp->calc_sings = flg;
957: return(0);
958: }
962: /*@
963: KSPGetComputeEigenvalues - Gets the flag indicating that the extreme eigenvalues
964: values will be calculated via a Lanczos or Arnoldi process as the linear
965: system is solved.
967: Collective on KSP
969: Input Parameter:
970: . ksp - iterative context obtained from KSPCreate()
972: Output Parameter:
973: . flg - PETSC_TRUE or PETSC_FALSE
975: Notes:
976: Currently this option is not valid for all iterative methods.
978: Level: advanced
980: .keywords: KSP, set, compute, eigenvalues
982: .seealso: KSPComputeEigenvalues(), KSPComputeEigenvaluesExplicitly()
983: @*/
984: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetComputeEigenvalues(KSP ksp,PetscTruth *flg)
985: {
989: *flg = ksp->calc_sings;
990: return(0);
991: }
995: /*@
996: KSPSetComputeEigenvalues - Sets a flag so that the extreme eigenvalues
997: values will be calculated via a Lanczos or Arnoldi process as the linear
998: system is solved.
1000: Collective on KSP
1002: Input Parameters:
1003: + ksp - iterative context obtained from KSPCreate()
1004: - flg - PETSC_TRUE or PETSC_FALSE
1006: Notes:
1007: Currently this option is not valid for all iterative methods.
1009: Level: advanced
1011: .keywords: KSP, set, compute, eigenvalues
1013: .seealso: KSPComputeEigenvalues(), KSPComputeEigenvaluesExplicitly()
1014: @*/
1015: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetComputeEigenvalues(KSP ksp,PetscTruth flg)
1016: {
1019: ksp->calc_sings = flg;
1020: return(0);
1021: }
1025: /*@C
1026: KSPGetRhs - Gets the right-hand-side vector for the linear system to
1027: be solved.
1029: Not Collective
1031: Input Parameter:
1032: . ksp - iterative context obtained from KSPCreate()
1034: Output Parameter:
1035: . r - right-hand-side vector
1037: Level: developer
1039: .keywords: KSP, get, right-hand-side, rhs
1041: .seealso: KSPGetSolution(), KSPSolve()
1042: @*/
1043: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetRhs(KSP ksp,Vec *r)
1044: {
1047: *r = ksp->vec_rhs;
1048: return(0);
1049: }
1053: /*@C
1054: KSPGetSolution - Gets the location of the solution for the
1055: linear system to be solved. Note that this may not be where the solution
1056: is stored during the iterative process; see KSPBuildSolution().
1058: Not Collective
1060: Input Parameters:
1061: . ksp - iterative context obtained from KSPCreate()
1063: Output Parameters:
1064: . v - solution vector
1066: Level: developer
1068: .keywords: KSP, get, solution
1070: .seealso: KSPGetRhs(), KSPBuildSolution(), KSPSolve()
1071: @*/
1072: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetSolution(KSP ksp,Vec *v)
1073: {
1077: *v = ksp->vec_sol;
1078: return(0);
1079: }
1083: /*@
1084: KSPSetPC - Sets the preconditioner to be used to calculate the
1085: application of the preconditioner on a vector.
1087: Collective on KSP
1089: Input Parameters:
1090: + ksp - iterative context obtained from KSPCreate()
1091: - pc - the preconditioner object
1093: Notes:
1094: Use KSPGetPC() to retrieve the preconditioner context (for example,
1095: to free it at the end of the computations).
1097: Level: developer
1099: .keywords: KSP, set, precondition, Binv
1101: .seealso: KSPGetPC()
1102: @*/
1103: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetPC(KSP ksp,PC pc)
1104: {
1111: if (ksp->pc) {PCDestroy(ksp->pc);}
1112: ksp->pc = pc;
1113: PetscObjectReference((PetscObject)ksp->pc);
1114: return(0);
1115: }
1119: /*@C
1120: KSPGetPC - Returns a pointer to the preconditioner context
1121: set with KSPSetPC().
1123: Not Collective
1125: Input Parameters:
1126: . ksp - iterative context obtained from KSPCreate()
1128: Output Parameter:
1129: . pc - preconditioner context
1131: Level: developer
1133: .keywords: KSP, get, preconditioner, Binv
1135: .seealso: KSPSetPC()
1136: @*/
1137: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetPC(KSP ksp,PC *pc)
1138: {
1142: *pc = ksp->pc;
1143: return(0);
1144: }
1148: /*@C
1149: KSPSetMonitor - Sets an ADDITIONAL function to be called at every iteration to monitor
1150: the residual/error etc.
1151:
1152: Collective on KSP
1154: Input Parameters:
1155: + ksp - iterative context obtained from KSPCreate()
1156: . monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring
1157: . mctx - [optional] context for private data for the
1158: monitor routine (use PETSC_NULL if no context is desired)
1159: - monitordestroy - [optional] routine that frees monitor context
1160: (may be PETSC_NULL)
1162: Calling Sequence of monitor:
1163: $ monitor (KSP ksp, int it, PetscReal rnorm, void *mctx)
1165: + ksp - iterative context obtained from KSPCreate()
1166: . it - iteration number
1167: . rnorm - (estimated) 2-norm of (preconditioned) residual
1168: - mctx - optional monitoring context, as set by KSPSetMonitor()
1170: Options Database Keys:
1171: + -ksp_monitor - sets KSPDefaultMonitor()
1172: . -ksp_truemonitor - sets KSPTrueMonitor()
1173: . -ksp_xmonitor - sets line graph monitor,
1174: uses KSPLGMonitorCreate()
1175: . -ksp_xtruemonitor - sets line graph monitor,
1176: uses KSPLGMonitorCreate()
1177: . -ksp_singmonitor - sets KSPSingularValueMonitor()
1178: - -ksp_cancelmonitors - cancels all monitors that have
1179: been hardwired into a code by
1180: calls to KSPSetMonitor(), but
1181: does not cancel those set via
1182: the options database.
1184: Notes:
1185: The default is to do nothing. To print the residual, or preconditioned
1186: residual if KSPSetNormType(ksp,KSP_PRECONDITIONED_NORM) was called, use
1187: KSPDefaultMonitor() as the monitoring routine, with a null monitoring
1188: context.
1190: Several different monitoring routines may be set by calling
1191: KSPSetMonitor() multiple times; all will be called in the
1192: order in which they were set.
1194: Level: beginner
1196: .keywords: KSP, set, monitor
1198: .seealso: KSPDefaultMonitor(), KSPLGMonitorCreate(), KSPClearMonitor()
1199: @*/
1200: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetMonitor(KSP ksp,PetscErrorCode (*monitor)(KSP,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
1201: {
1204: if (ksp->numbermonitors >= MAXKSPMONITORS) {
1205: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many KSP monitors set");
1206: }
1207: ksp->monitor[ksp->numbermonitors] = monitor;
1208: ksp->monitordestroy[ksp->numbermonitors] = monitordestroy;
1209: ksp->monitorcontext[ksp->numbermonitors++] = (void*)mctx;
1210: return(0);
1211: }
1215: /*@
1216: KSPClearMonitor - Clears all monitors for a KSP object.
1218: Collective on KSP
1220: Input Parameters:
1221: . ksp - iterative context obtained from KSPCreate()
1223: Options Database Key:
1224: . -ksp_cancelmonitors - Cancels all monitors that have
1225: been hardwired into a code by calls to KSPSetMonitor(),
1226: but does not cancel those set via the options database.
1228: Level: intermediate
1230: .keywords: KSP, set, monitor
1232: .seealso: KSPDefaultMonitor(), KSPLGMonitorCreate(), KSPSetMonitor()
1233: @*/
1234: PetscErrorCode PETSCKSP_DLLEXPORT KSPClearMonitor(KSP ksp)
1235: {
1238: ksp->numbermonitors = 0;
1239: return(0);
1240: }
1244: /*@C
1245: KSPGetMonitorContext - Gets the monitoring context, as set by
1246: KSPSetMonitor() for the FIRST monitor only.
1248: Not Collective
1250: Input Parameter:
1251: . ksp - iterative context obtained from KSPCreate()
1253: Output Parameter:
1254: . ctx - monitoring context
1256: Level: intermediate
1258: .keywords: KSP, get, monitor, context
1260: .seealso: KSPDefaultMonitor(), KSPLGMonitorCreate()
1261: @*/
1262: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetMonitorContext(KSP ksp,void **ctx)
1263: {
1266: *ctx = (ksp->monitorcontext[0]);
1267: return(0);
1268: }
1272: /*@
1273: KSPSetResidualHistory - Sets the array used to hold the residual history.
1274: If set, this array will contain the residual norms computed at each
1275: iteration of the solver.
1277: Not Collective
1279: Input Parameters:
1280: + ksp - iterative context obtained from KSPCreate()
1281: . a - array to hold history
1282: . na - size of a
1283: - reset - PETSC_TRUE indicates the history counter is reset to zero
1284: for each new linear solve
1286: Level: advanced
1288: Notes: The array is NOT freed by PETSc so the user needs to keep track of
1289: it and destroy once the KSP object is destroyed.
1291: If 'na' is PETSC_DECIDE or 'a' is PETSC_NULL, then a default array of
1292: length 1000 is allocated.
1294: .keywords: KSP, set, residual, history, norm
1296: .seealso: KSPGetResidualHistory()
1298: @*/
1299: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetResidualHistory(KSP ksp,PetscReal a[],PetscInt na,PetscTruth reset)
1300: {
1305: if (na != PETSC_DECIDE && a) {
1306: ksp->res_hist = a;
1307: ksp->res_hist_max = na;
1308: } else {
1309: ksp->res_hist_max = 1000;
1310: PetscMalloc(ksp->res_hist_max*sizeof(PetscReal),&ksp->res_hist);
1311: }
1312: ksp->res_hist_len = 0;
1313: ksp->res_hist_reset = reset;
1316: return(0);
1317: }
1321: /*@C
1322: KSPGetResidualHistory - Gets the array used to hold the residual history
1323: and the number of residuals it contains.
1325: Not Collective
1327: Input Parameter:
1328: . ksp - iterative context obtained from KSPCreate()
1330: Output Parameters:
1331: + a - pointer to array to hold history (or PETSC_NULL)
1332: - na - number of used entries in a (or PETSC_NULL)
1334: Level: advanced
1336: Notes:
1337: Can only be called after a KSPSetResidualHistory() otherwise a and na are set to zero
1339: The Fortran version of this routine has a calling sequence
1340: $ call KSPGetResidualHistory(KSP ksp, integer na, integer ierr)
1342: .keywords: KSP, get, residual, history, norm
1344: .seealso: KSPGetResidualHistory()
1346: @*/
1347: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetResidualHistory(KSP ksp,PetscReal *a[],PetscInt *na)
1348: {
1351: if (a) *a = ksp->res_hist;
1352: if (na) *na = ksp->res_hist_len;
1353: return(0);
1354: }
1358: /*@C
1359: KSPSetConvergenceTest - Sets the function to be used to determine
1360: convergence.
1362: Collective on KSP
1364: Input Parameters:
1365: + ksp - iterative context obtained from KSPCreate()
1366: . converge - pointer to int function
1367: - cctx - context for private data for the convergence routine (may be null)
1369: Calling sequence of converge:
1370: $ converge (KSP ksp, int it, PetscReal rnorm, KSPConvergedReason *reason,void *mctx)
1372: + ksp - iterative context obtained from KSPCreate()
1373: . it - iteration number
1374: . rnorm - (estimated) 2-norm of (preconditioned) residual
1375: . reason - the reason why it has converged or diverged
1376: - cctx - optional convergence context, as set by KSPSetConvergenceTest()
1379: Notes:
1380: Must be called after the KSP type has been set so put this after
1381: a call to KSPSetType(), or KSPSetFromOptions().
1383: The default convergence test, KSPDefaultConverged(), aborts if the
1384: residual grows to more than 10000 times the initial residual.
1386: The default is a combination of relative and absolute tolerances.
1387: The residual value that is tested may be an approximation; routines
1388: that need exact values should compute them.
1390: In the default PETSc convergence test, the precise values of reason
1391: are macros such as KSP_CONVERGED_RTOL, which are defined in petscksp.h.
1393: Level: advanced
1395: .keywords: KSP, set, convergence, test, context
1397: .seealso: KSPDefaultConverged(), KSPGetConvergenceContext()
1398: @*/
1399: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetConvergenceTest(KSP ksp,PetscErrorCode (*converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*),void *cctx)
1400: {
1403: ksp->converged = converge;
1404: ksp->cnvP = (void*)cctx;
1405: return(0);
1406: }
1410: /*@C
1411: KSPGetConvergenceContext - Gets the convergence context set with
1412: KSPSetConvergenceTest().
1414: Not Collective
1416: Input Parameter:
1417: . ksp - iterative context obtained from KSPCreate()
1419: Output Parameter:
1420: . ctx - monitoring context
1422: Level: advanced
1424: .keywords: KSP, get, convergence, test, context
1426: .seealso: KSPDefaultConverged(), KSPSetConvergenceTest()
1427: @*/
1428: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetConvergenceContext(KSP ksp,void **ctx)
1429: {
1432: *ctx = ksp->cnvP;
1433: return(0);
1434: }
1438: /*@C
1439: KSPBuildSolution - Builds the approximate solution in a vector provided.
1440: This routine is NOT commonly needed (see KSPSolve()).
1442: Collective on KSP
1444: Input Parameter:
1445: . ctx - iterative context obtained from KSPCreate()
1447: Output Parameter:
1448: Provide exactly one of
1449: + v - location to stash solution.
1450: - V - the solution is returned in this location. This vector is created
1451: internally. This vector should NOT be destroyed by the user with
1452: VecDestroy().
1454: Notes:
1455: This routine can be used in one of two ways
1456: .vb
1457: KSPBuildSolution(ksp,PETSC_NULL,&V);
1458: or
1459: KSPBuildSolution(ksp,v,PETSC_NULL);
1460: .ve
1461: In the first case an internal vector is allocated to store the solution
1462: (the user cannot destroy this vector). In the second case the solution
1463: is generated in the vector that the user provides. Note that for certain
1464: methods, such as KSPCG, the second case requires a copy of the solution,
1465: while in the first case the call is essentially free since it simply
1466: returns the vector where the solution already is stored.
1468: Level: advanced
1470: .keywords: KSP, build, solution
1472: .seealso: KSPGetSolution(), KSPBuildResidual()
1473: @*/
1474: PetscErrorCode PETSCKSP_DLLEXPORT KSPBuildSolution(KSP ksp,Vec v,Vec *V)
1475: {
1480: if (!V && !v) SETERRQ(PETSC_ERR_ARG_WRONG,"Must provide either v or V");
1481: if (!V) V = &v;
1482: (*ksp->ops->buildsolution)(ksp,v,V);
1483: return(0);
1484: }
1488: /*@C
1489: KSPBuildResidual - Builds the residual in a vector provided.
1491: Collective on KSP
1493: Input Parameter:
1494: . ksp - iterative context obtained from KSPCreate()
1496: Output Parameters:
1497: + v - optional location to stash residual. If v is not provided,
1498: then a location is generated.
1499: . t - work vector. If not provided then one is generated.
1500: - V - the residual
1502: Notes:
1503: Regardless of whether or not v is provided, the residual is
1504: returned in V.
1506: Level: advanced
1508: .keywords: KSP, build, residual
1510: .seealso: KSPBuildSolution()
1511: @*/
1512: PetscErrorCode PETSCKSP_DLLEXPORT KSPBuildResidual(KSP ksp,Vec t,Vec v,Vec *V)
1513: {
1515: PetscTruth flag = PETSC_FALSE;
1516: Vec w = v,tt = t;
1520: if (!w) {
1521: VecDuplicate(ksp->vec_rhs,&w);
1522: PetscLogObjectParent((PetscObject)ksp,w);
1523: }
1524: if (!tt) {
1525: VecDuplicate(ksp->vec_rhs,&tt); flag = PETSC_TRUE;
1526: PetscLogObjectParent((PetscObject)ksp,tt);
1527: }
1528: (*ksp->ops->buildresidual)(ksp,tt,w,V);
1529: if (flag) {VecDestroy(tt);}
1530: return(0);
1531: }
1535: /*@
1536: KSPSetDiagonalScale - Tells KSP to symmetrically diagonally scale the system
1537: before solving. This actually CHANGES the matrix (and right hand side).
1539: Collective on KSP
1541: Input Parameter:
1542: + ksp - the KSP context
1543: - scale - PETSC_TRUE or PETSC_FALSE
1545: Options Database Key:
1546: + -ksp_diagonal_scale -
1547: - -ksp_diagonal_scale_fix - scale the matrix back AFTER the solve
1550: BE CAREFUL with this routine: it actually scales the matrix and right
1551: hand side that define the system. After the system is solved the matrix
1552: and right hand side remain scaled.
1554: This routine is only used if the matrix and preconditioner matrix are
1555: the same thing.
1556:
1557: If you use this with the PCType Eisenstat preconditioner than you can
1558: use the PCEisenstatNoDiagonalScaling() option, or -pc_eisenstat_no_diagonal_scaling
1559: to save some unneeded, redundant flops.
1561: Level: intermediate
1563: .keywords: KSP, set, options, prefix, database
1565: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScaleFix()
1566: @*/
1567: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetDiagonalScale(KSP ksp,PetscTruth scale)
1568: {
1571: ksp->dscale = scale;
1572: return(0);
1573: }
1577: /*@C
1578: KSPGetDiagonalScale - Checks if KSP solver scales the matrix and
1579: right hand side
1581: Not Collective
1583: Input Parameter:
1584: . ksp - the KSP context
1586: Output Parameter:
1587: . scale - PETSC_TRUE or PETSC_FALSE
1589: Notes:
1590: BE CAREFUL with this routine: it actually scales the matrix and right
1591: hand side that define the system. After the system is solved the matrix
1592: and right hand side remain scaled.
1594: This routine is only used if the matrix and preconditioner matrix are
1595: the same thing.
1597: Level: intermediate
1599: .keywords: KSP, set, options, prefix, database
1601: .seealso: KSPSetDiagonalScale(), KSPSetDiagonalScaleFix()
1602: @*/
1603: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetDiagonalScale(KSP ksp,PetscTruth *scale)
1604: {
1608: *scale = ksp->dscale;
1609: return(0);
1610: }
1614: /*@
1615: KSPSetDiagonalScaleFix - Tells KSP to diagonally scale the system
1616: back after solving.
1618: Collective on KSP
1620: Input Parameter:
1621: + ksp - the KSP context
1622: - fix - PETSC_TRUE to scale back after the system solve, PETSC_FALSE to not
1623: rescale (default)
1625: Notes:
1626: Must be called after KSPSetDiagonalScale()
1628: Using this will slow things down, because it rescales the matrix before and
1629: after each linear solve. This is intended mainly for testing to allow one
1630: to easily get back the original system to make sure the solution computed is
1631: accurate enough.
1633: This routine is only used if the matrix and preconditioner matrix are
1634: the same thing.
1636: Level: intermediate
1638: .keywords: KSP, set, options, prefix, database
1640: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScale(), KSPGetDiagonalScaleFix()
1641: @*/
1642: PetscErrorCode PETSCKSP_DLLEXPORT KSPSetDiagonalScaleFix(KSP ksp,PetscTruth fix)
1643: {
1646: ksp->dscalefix = fix;
1647: return(0);
1648: }
1652: /*@
1653: KSPGetDiagonalScaleFix - Determines if KSP diagonally scales the system
1654: back after solving.
1656: Collective on KSP
1658: Input Parameter:
1659: . ksp - the KSP context
1661: Output Parameter:
1662: . fix - PETSC_TRUE to scale back after the system solve, PETSC_FALSE to not
1663: rescale (default)
1665: Notes:
1666: Must be called after KSPSetDiagonalScale()
1668: If PETSC_TRUE will slow things down, because it rescales the matrix before and
1669: after each linear solve. This is intended mainly for testing to allow one
1670: to easily get back the original system to make sure the solution computed is
1671: accurate enough.
1673: This routine is only used if the matrix and preconditioner matrix are
1674: the same thing.
1676: Level: intermediate
1678: .keywords: KSP, set, options, prefix, database
1680: .seealso: KSPGetDiagonalScale(), KSPSetDiagonalScale(), KSPSetDiagonalScaleFix()
1681: @*/
1682: PetscErrorCode PETSCKSP_DLLEXPORT KSPGetDiagonalScaleFix(KSP ksp,PetscTruth *fix)
1683: {
1687: *fix = ksp->dscalefix;
1688: return(0);
1689: }