Actual source code: schurm.c
petsc-3.13.6 2020-09-29
1: #include <../src/ksp/ksp/utils/schurm/schurm.h>
3: const char *const MatSchurComplementAinvTypes[] = {"DIAG","LUMP","BLOCKDIAG","MatSchurComplementAinvType","MAT_SCHUR_COMPLEMENT_AINV_",0};
5: PetscErrorCode MatCreateVecs_SchurComplement(Mat N,Vec *right,Vec *left)
6: {
7: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
8: PetscErrorCode ierr;
11: if (Na->D) {
12: MatCreateVecs(Na->D,right,left);
13: return(0);
14: }
15: if (right) {
16: MatCreateVecs(Na->B,right,NULL);
17: }
18: if (left) {
19: MatCreateVecs(Na->C,NULL,left);
20: }
21: return(0);
22: }
24: PetscErrorCode MatView_SchurComplement(Mat N,PetscViewer viewer)
25: {
26: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
27: PetscErrorCode ierr;
30: PetscViewerASCIIPrintf(viewer,"Schur complement A11 - A10 inv(A00) A01\n");
31: if (Na->D) {
32: PetscViewerASCIIPrintf(viewer,"A11\n");
33: PetscViewerASCIIPushTab(viewer);
34: MatView(Na->D,viewer);
35: PetscViewerASCIIPopTab(viewer);
36: } else {
37: PetscViewerASCIIPrintf(viewer,"A11 = 0\n");
38: }
39: PetscViewerASCIIPrintf(viewer,"A10\n");
40: PetscViewerASCIIPushTab(viewer);
41: MatView(Na->C,viewer);
42: PetscViewerASCIIPopTab(viewer);
43: PetscViewerASCIIPrintf(viewer,"KSP of A00\n");
44: PetscViewerASCIIPushTab(viewer);
45: KSPView(Na->ksp,viewer);
46: PetscViewerASCIIPopTab(viewer);
47: PetscViewerASCIIPrintf(viewer,"A01\n");
48: PetscViewerASCIIPushTab(viewer);
49: MatView(Na->B,viewer);
50: PetscViewerASCIIPopTab(viewer);
51: return(0);
52: }
54: /*
55: A11^T - A01^T ksptrans(A00,Ap00) A10^T
56: */
57: PetscErrorCode MatMultTranspose_SchurComplement(Mat N,Vec x,Vec y)
58: {
59: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
60: PetscErrorCode ierr;
63: if (!Na->work1) {MatCreateVecs(Na->A,&Na->work1,NULL);}
64: if (!Na->work2) {MatCreateVecs(Na->A,&Na->work2,NULL);}
65: MatMultTranspose(Na->C,x,Na->work1);
66: KSPSolveTranspose(Na->ksp,Na->work1,Na->work2);
67: MatMultTranspose(Na->B,Na->work2,y);
68: VecScale(y,-1.0);
69: if (Na->D) {
70: MatMultTransposeAdd(Na->D,x,y,y);
71: }
72: return(0);
73: }
75: /*
76: A11 - A10 ksp(A00,Ap00) A01
77: */
78: PetscErrorCode MatMult_SchurComplement(Mat N,Vec x,Vec y)
79: {
80: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
81: PetscErrorCode ierr;
84: if (!Na->work1) {MatCreateVecs(Na->A,&Na->work1,NULL);}
85: if (!Na->work2) {MatCreateVecs(Na->A,&Na->work2,NULL);}
86: MatMult(Na->B,x,Na->work1);
87: KSPSolve(Na->ksp,Na->work1,Na->work2);
88: MatMult(Na->C,Na->work2,y);
89: VecScale(y,-1.0);
90: if (Na->D) {
91: MatMultAdd(Na->D,x,y,y);
92: }
93: return(0);
94: }
96: /*
97: A11 - A10 ksp(A00,Ap00) A01
98: */
99: PetscErrorCode MatMultAdd_SchurComplement(Mat N,Vec x,Vec y,Vec z)
100: {
101: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
102: PetscErrorCode ierr;
105: if (!Na->work1) {MatCreateVecs(Na->A,&Na->work1,NULL);}
106: if (!Na->work2) {MatCreateVecs(Na->A,&Na->work2,NULL);}
107: MatMult(Na->B,x,Na->work1);
108: KSPSolve(Na->ksp,Na->work1,Na->work2);
109: if (y == z) {
110: VecScale(Na->work2,-1.0);
111: MatMultAdd(Na->C,Na->work2,z,z);
112: } else {
113: MatMult(Na->C,Na->work2,z);
114: VecAYPX(z,-1.0,y);
115: }
116: if (Na->D) {
117: MatMultAdd(Na->D,x,z,z);
118: }
119: return(0);
120: }
122: PetscErrorCode MatSetFromOptions_SchurComplement(PetscOptionItems *PetscOptionsObject,Mat N)
123: {
124: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
125: PetscErrorCode ierr;
128: PetscOptionsHead(PetscOptionsObject,"MatSchurComplementOptions");
129: Na->ainvtype = MAT_SCHUR_COMPLEMENT_AINV_DIAG;
130: PetscOptionsEnum("-mat_schur_complement_ainv_type","Type of approximation for inv(A00) used when assembling Sp = A11 - A10 inv(A00) A01","MatSchurComplementSetAinvType",MatSchurComplementAinvTypes,(PetscEnum)Na->ainvtype,(PetscEnum*)&Na->ainvtype,NULL);
131: PetscOptionsTail();
132: KSPSetFromOptions(Na->ksp);
133: return(0);
134: }
136: PetscErrorCode MatDestroy_SchurComplement(Mat N)
137: {
138: Mat_SchurComplement *Na = (Mat_SchurComplement*)N->data;
139: PetscErrorCode ierr;
142: MatDestroy(&Na->A);
143: MatDestroy(&Na->Ap);
144: MatDestroy(&Na->B);
145: MatDestroy(&Na->C);
146: MatDestroy(&Na->D);
147: VecDestroy(&Na->work1);
148: VecDestroy(&Na->work2);
149: KSPDestroy(&Na->ksp);
150: PetscFree(N->data);
151: return(0);
152: }
154: /*@
155: MatCreateSchurComplement - Creates a new matrix object that behaves like the Schur complement of a matrix
157: Collective on A00
159: Input Parameters:
160: + A00,A01,A10,A11 - the four parts of the original matrix A = [A00 A01; A10 A11] (A11 is optional)
161: - Ap00 - preconditioning matrix for use in ksp(A00,Ap00) to approximate the action of A^{-1}
163: Output Parameter:
164: . S - the matrix that the Schur complement S = A11 - A10 ksp(A00,Ap00) A01
166: Level: intermediate
168: Notes:
169: The Schur complement is NOT actually formed! Rather, this
170: object performs the matrix-vector product by using formula S = A11 - A10 A^{-1} A01
171: for Schur complement S and a KSP solver to approximate the action of A^{-1}.
173: All four matrices must have the same MPI communicator.
175: A00 and A11 must be square matrices.
177: MatGetSchurComplement() takes as arguments the index sets for the submatrices and returns both the virtual Schur complement (what this returns) plus
178: a sparse approximation to the true Schur complement (useful for building a preconditioner for the Schur complement).
180: MatSchurComplementGetPmat() can be called on the output of this function to generate an explicit approximation to the Schur complement.
182: Developer Notes:
183: The API that includes MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementGetPmat() should be refactored to
184: remove redundancy and be clearer and simplier.
187: .seealso: MatCreateNormal(), MatMult(), MatCreate(), MatSchurComplementGetKSP(), MatSchurComplementUpdateSubMatrices(), MatCreateTranspose(), MatGetSchurComplement(),
188: MatSchurComplementGetPmat()
190: @*/
191: PetscErrorCode MatCreateSchurComplement(Mat A00,Mat Ap00,Mat A01,Mat A10,Mat A11,Mat *S)
192: {
196: KSPInitializePackage();
197: MatCreate(PetscObjectComm((PetscObject)A00),S);
198: MatSetType(*S,MATSCHURCOMPLEMENT);
199: MatSchurComplementSetSubMatrices(*S,A00,Ap00,A01,A10,A11);
200: return(0);
201: }
203: /*@
204: MatSchurComplementSetSubMatrices - Sets the matrices that define the Schur complement
206: Collective on S
208: Input Parameter:
209: + S - matrix obtained with MatCreateSchurComplement (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
210: . A00,A01,A10,A11 - the four parts of A = [A00 A01; A10 A11] (A11 is optional)
211: - Ap00 - preconditioning matrix for use in ksp(A00,Ap00) to approximate the action of A^{-1}.
213: Level: intermediate
215: Notes:
216: The Schur complement is NOT actually formed! Rather, this
217: object performs the matrix-vector product by using formula S = A11 - A10 A^{-1} A01
218: for Schur complement S and a KSP solver to approximate the action of A^{-1}.
220: All four matrices must have the same MPI communicator.
222: A00 and A11 must be square matrices.
224: .seealso: MatCreateNormal(), MatMult(), MatCreate(), MatSchurComplementGetKSP(), MatSchurComplementUpdateSubMatrices(), MatCreateTranspose(), MatCreateSchurComplement(), MatGetSchurComplement()
226: @*/
227: PetscErrorCode MatSchurComplementSetSubMatrices(Mat S,Mat A00,Mat Ap00,Mat A01,Mat A10,Mat A11)
228: {
229: PetscErrorCode ierr;
230: Mat_SchurComplement *Na = (Mat_SchurComplement*)S->data;
231: PetscBool isschur;
234: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
235: if (!isschur) return(0);
236: if (S->assembled) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Use MatSchurComplementUpdateSubMatrices() for already used matrix");
244: if (A00->rmap->n != A00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local columns %D",A00->rmap->n,A00->cmap->n);
245: if (A00->rmap->n != Ap00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local rows of Ap00 %D",A00->rmap->n,Ap00->rmap->n);
246: if (Ap00->rmap->n != Ap00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of Ap00 %D do not equal local columns %D",Ap00->rmap->n,Ap00->cmap->n);
247: if (A00->cmap->n != A01->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A00 %D do not equal local rows of A01 %D",A00->cmap->n,A01->rmap->n);
248: if (A10->cmap->n != A00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A10 %D do not equal local rows of A00 %D",A10->cmap->n,A00->rmap->n);
249: if (A11) {
252: if (A10->rmap->n != A11->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A10 %D do not equal local rows A11 %D",A10->rmap->n,A11->rmap->n);
253: }
255: MatSetSizes(S,A10->rmap->n,A01->cmap->n,A10->rmap->N,A01->cmap->N);
256: PetscObjectReference((PetscObject)A00);
257: PetscObjectReference((PetscObject)Ap00);
258: PetscObjectReference((PetscObject)A01);
259: PetscObjectReference((PetscObject)A10);
260: Na->A = A00;
261: Na->Ap = Ap00;
262: Na->B = A01;
263: Na->C = A10;
264: Na->D = A11;
265: if (A11) {
266: PetscObjectReference((PetscObject)A11);
267: }
268: MatSetUp(S);
269: KSPSetOperators(Na->ksp,A00,Ap00);
270: S->assembled = PETSC_TRUE;
271: return(0);
272: }
274: /*@
275: MatSchurComplementGetKSP - Gets the KSP object that is used to invert A00 in the Schur complement matrix S = A11 - A10 ksp(A00,Ap00) A01
277: Not Collective
279: Input Parameter:
280: . S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
282: Output Parameter:
283: . ksp - the linear solver object
285: Options Database:
286: . -fieldsplit_<splitname_0>_XXX sets KSP and PC options for the 0-split solver inside the Schur complement used in PCFieldSplit; default <splitname_0> is 0.
288: Level: intermediate
290: .seealso: MatSchurComplementSetKSP(), MatCreateSchurComplement(), MatCreateNormal(), MatMult(), MatCreate()
291: @*/
292: PetscErrorCode MatSchurComplementGetKSP(Mat S, KSP *ksp)
293: {
294: Mat_SchurComplement *Na;
295: PetscBool isschur;
296: PetscErrorCode ierr;
300: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
301: if (!isschur) SETERRQ1(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONG,"Not for type %s",((PetscObject)S)->type_name);
303: Na = (Mat_SchurComplement*) S->data;
304: *ksp = Na->ksp;
305: return(0);
306: }
308: /*@
309: MatSchurComplementSetKSP - Sets the KSP object that is used to invert A00 in the Schur complement matrix S = A11 - A10 ksp(A00,Ap00) A01
311: Not Collective
313: Input Parameters:
314: + S - matrix created with MatCreateSchurComplement()
315: - ksp - the linear solver object
317: Level: developer
319: Developer Notes:
320: This is used in PCFieldSplit to reuse the 0-split KSP to implement ksp(A00,Ap00) in S.
321: The KSP operators are overwritten with A00 and Ap00 currently set in S.
323: .seealso: MatSchurComplementGetKSP(), MatCreateSchurComplement(), MatCreateNormal(), MatMult(), MatCreate(), MATSCHURCOMPLEMENT
324: @*/
325: PetscErrorCode MatSchurComplementSetKSP(Mat S, KSP ksp)
326: {
327: Mat_SchurComplement *Na;
328: PetscErrorCode ierr;
329: PetscBool isschur;
333: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
334: if (!isschur) return(0);
336: Na = (Mat_SchurComplement*) S->data;
337: PetscObjectReference((PetscObject)ksp);
338: KSPDestroy(&Na->ksp);
339: Na->ksp = ksp;
340: KSPSetOperators(Na->ksp, Na->A, Na->Ap);
341: return(0);
342: }
344: /*@
345: MatSchurComplementUpdateSubMatrices - Updates the Schur complement matrix object with new submatrices
347: Collective on S
349: Input Parameters:
350: + S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
351: . A00,A01,A10,A11 - the four parts of A = [A00 A01; A10 A11] (A11 is optional)
352: - Ap00 - preconditioning matrix for use in ksp(A00,Ap00) to approximate the action of A^{-1}.
354: Level: intermediate
356: Notes:
357: All four matrices must have the same MPI communicator
359: A00 and A11 must be square matrices
361: All of the matrices provided must have the same sizes as was used with MatCreateSchurComplement() or MatSchurComplementSetSubMatrices()
362: though they need not be the same matrices.
364: .seealso: MatCreateNormal(), MatMult(), MatCreate(), MatSchurComplementGetKSP(), MatCreateSchurComplement()
366: @*/
367: PetscErrorCode MatSchurComplementUpdateSubMatrices(Mat S,Mat A00,Mat Ap00,Mat A01,Mat A10,Mat A11)
368: {
369: PetscErrorCode ierr;
370: Mat_SchurComplement *Na = (Mat_SchurComplement*)S->data;
371: PetscBool isschur;
375: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
376: if (!isschur) return(0);
377: if (!S->assembled) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Use MatSchurComplementSetSubMatrices() for a new matrix");
385: if (A00->rmap->n != A00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local columns %D",A00->rmap->n,A00->cmap->n);
386: if (A00->rmap->n != Ap00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A00 %D do not equal local rows of Ap00 %D",A00->rmap->n,Ap00->rmap->n);
387: if (Ap00->rmap->n != Ap00->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of Ap00 %D do not equal local columns %D",Ap00->rmap->n,Ap00->cmap->n);
388: if (A00->cmap->n != A01->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A00 %D do not equal local rows of A01 %D",A00->cmap->n,A01->rmap->n);
389: if (A10->cmap->n != A00->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local columns of A10 %D do not equal local rows of A00 %D",A10->cmap->n,A00->rmap->n);
390: if (A11) {
393: if (A10->rmap->n != A11->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local rows of A10 %D do not equal local rows A11 %D",A10->rmap->n,A11->rmap->n);
394: }
396: PetscObjectReference((PetscObject)A00);
397: PetscObjectReference((PetscObject)Ap00);
398: PetscObjectReference((PetscObject)A01);
399: PetscObjectReference((PetscObject)A10);
400: if (A11) {
401: PetscObjectReference((PetscObject)A11);
402: }
404: MatDestroy(&Na->A);
405: MatDestroy(&Na->Ap);
406: MatDestroy(&Na->B);
407: MatDestroy(&Na->C);
408: MatDestroy(&Na->D);
410: Na->A = A00;
411: Na->Ap = Ap00;
412: Na->B = A01;
413: Na->C = A10;
414: Na->D = A11;
416: KSPSetOperators(Na->ksp,A00,Ap00);
417: return(0);
418: }
421: /*@C
422: MatSchurComplementGetSubMatrices - Get the individual submatrices in the Schur complement
424: Collective on S
426: Input Parameter:
427: . S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
429: Output Parameters:
430: + A00,A01,A10,A11 - the four parts of the original matrix A = [A00 A01; A10 A11] (A11 is optional)
431: - Ap00 - preconditioning matrix for use in ksp(A00,Ap00) to approximate the action of A^{-1}.
433: Note: A11 is optional, and thus can be NULL. The submatrices are not increfed before they are returned and should not be modified or destroyed.
435: Level: intermediate
437: .seealso: MatCreateNormal(), MatMult(), MatCreate(), MatSchurComplementGetKSP(), MatCreateSchurComplement(), MatSchurComplementUpdateSubMatrices()
438: @*/
439: PetscErrorCode MatSchurComplementGetSubMatrices(Mat S,Mat *A00,Mat *Ap00,Mat *A01,Mat *A10,Mat *A11)
440: {
441: Mat_SchurComplement *Na = (Mat_SchurComplement*) S->data;
442: PetscErrorCode ierr;
443: PetscBool flg;
447: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&flg);
448: if (!flg) SETERRQ1(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONG,"Not for type %s",((PetscObject)S)->type_name);
449: if (A00) *A00 = Na->A;
450: if (Ap00) *Ap00 = Na->Ap;
451: if (A01) *A01 = Na->B;
452: if (A10) *A10 = Na->C;
453: if (A11) *A11 = Na->D;
454: return(0);
455: }
457: #include <petsc/private/kspimpl.h>
459: /*@
460: MatSchurComplementComputeExplicitOperator - Compute the Schur complement matrix explicitly
462: Collective on M
464: Input Parameter:
465: . M - the matrix obtained with MatCreateSchurComplement()
467: Output Parameter:
468: . S - the Schur complement matrix
470: Note: This can be expensive, so it is mainly for testing
472: Level: advanced
474: .seealso: MatCreateSchurComplement(), MatSchurComplementUpdate()
475: @*/
476: PetscErrorCode MatSchurComplementComputeExplicitOperator(Mat M, Mat *S)
477: {
478: Mat B, C, D;
479: KSP ksp;
480: PC pc;
481: PetscBool isLU, isILU;
482: PetscReal fill = 2.0;
486: MatSchurComplementGetSubMatrices(M, NULL, NULL, &B, &C, &D);
487: MatSchurComplementGetKSP(M, &ksp);
488: KSPGetPC(ksp, &pc);
489: PetscObjectTypeCompare((PetscObject) pc, PCLU, &isLU);
490: PetscObjectTypeCompare((PetscObject) pc, PCILU, &isILU);
491: if (isLU || isILU) {
492: Mat fact, Bd, AinvB, AinvBd;
493: PetscReal eps = 1.0e-10;
495: /* This can be sped up for banded LU */
496: KSPSetUp(ksp);
497: PCFactorGetMatrix(pc, &fact);
498: MatConvert(B, MATDENSE, MAT_INITIAL_MATRIX, &Bd);
499: MatDuplicate(Bd, MAT_DO_NOT_COPY_VALUES, &AinvBd);
500: MatMatSolve(fact, Bd, AinvBd);
501: MatDestroy(&Bd);
502: MatChop(AinvBd, eps);
503: MatConvert(AinvBd, MATAIJ, MAT_INITIAL_MATRIX, &AinvB);
504: MatDestroy(&AinvBd);
505: MatMatMult(C, AinvB, MAT_INITIAL_MATRIX, fill, S);
506: MatDestroy(&AinvB);
507: } else {
508: Mat Ainv;
510: PCComputeOperator(pc, MATAIJ, &Ainv);
511: #if 0
512: /* Symmetric version */
513: MatPtAP(Ainv, B, MAT_INITIAL_MATRIX, fill, S);
514: #else
515: /* Nonsymmetric version */
516: MatMatMatMult(C, Ainv, B, MAT_INITIAL_MATRIX, fill, S);
517: #endif
518: MatDestroy(&Ainv);
519: }
520: if (D) {
521: MatAXPY(*S, -1.0, D, DIFFERENT_NONZERO_PATTERN);
522: }
523: MatScale(*S,-1.0);
524: return(0);
525: }
527: /* Developer Notes:
528: This should be implemented with a MatCreate_SchurComplement() as that is the standard design for new Mat classes. */
529: PetscErrorCode MatGetSchurComplement_Basic(Mat mat,IS isrow0,IS iscol0,IS isrow1,IS iscol1,MatReuse mreuse,Mat *newmat,MatSchurComplementAinvType ainvtype, MatReuse preuse,Mat *newpmat)
530: {
532: Mat A=0,Ap=0,B=0,C=0,D=0;
533: MatReuse reuse;
545: if (mreuse == MAT_IGNORE_MATRIX && preuse == MAT_IGNORE_MATRIX) return(0);
549: if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
551: reuse = MAT_INITIAL_MATRIX;
552: if (mreuse == MAT_REUSE_MATRIX) {
553: MatSchurComplementGetSubMatrices(*newmat,&A,&Ap,&B,&C,&D);
554: if (!A || !Ap || !B || !C) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Attempting to reuse matrix but Schur complement matrices unset");
555: if (A != Ap) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Preconditioning matrix does not match operator");
556: MatDestroy(&Ap); /* get rid of extra reference */
557: reuse = MAT_REUSE_MATRIX;
558: }
559: MatCreateSubMatrix(mat,isrow0,iscol0,reuse,&A);
560: MatCreateSubMatrix(mat,isrow0,iscol1,reuse,&B);
561: MatCreateSubMatrix(mat,isrow1,iscol0,reuse,&C);
562: MatCreateSubMatrix(mat,isrow1,iscol1,reuse,&D);
563: switch (mreuse) {
564: case MAT_INITIAL_MATRIX:
565: MatCreateSchurComplement(A,A,B,C,D,newmat);
566: break;
567: case MAT_REUSE_MATRIX:
568: MatSchurComplementUpdateSubMatrices(*newmat,A,A,B,C,D);
569: break;
570: default:
571: if (mreuse != MAT_IGNORE_MATRIX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Unrecognized value of mreuse %d",(int)mreuse);
572: }
573: if (preuse != MAT_IGNORE_MATRIX) {
574: MatCreateSchurComplementPmat(A,B,C,D,ainvtype,preuse,newpmat);
575: }
576: MatDestroy(&A);
577: MatDestroy(&B);
578: MatDestroy(&C);
579: MatDestroy(&D);
580: return(0);
581: }
583: /*@
584: MatGetSchurComplement - Obtain the Schur complement from eliminating part of the matrix in another part.
586: Collective on A
588: Input Parameters:
589: + A - matrix in which the complement is to be taken
590: . isrow0 - rows to eliminate
591: . iscol0 - columns to eliminate, (isrow0,iscol0) should be square and nonsingular
592: . isrow1 - rows in which the Schur complement is formed
593: . iscol1 - columns in which the Schur complement is formed
594: . mreuse - MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX, use MAT_IGNORE_MATRIX to put nothing in S
595: . ainvtype - the type of approximation used for the inverse of the (0,0) block used in forming Sp:
596: MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG, or MAT_SCHUR_COMPLEMENT_AINV_LUMP
597: - preuse - MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX, use MAT_IGNORE_MATRIX to put nothing in Sp
599: Output Parameters:
600: + S - exact Schur complement, often of type MATSCHURCOMPLEMENT which is difficult to use for preconditioning
601: - Sp - approximate Schur complement from which a preconditioner can be built
603: Note:
604: Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
605: Section 1.5 Writing Application Codes with PETSc-specific information. The default for assembled matrices is to use the inverse of the diagonal of
606: the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
607: before forming inv(diag(A00)).
609: Sometimes users would like to provide problem-specific data in the Schur complement, usually only for special row
610: and column index sets. In that case, the user should call PetscObjectComposeFunction() on the *S matrix and pass mreuse of MAT_REUSE_MATRIX to set
611: "MatGetSchurComplement_C" to their function. If their function needs to fall back to the default implementation, it
612: should call MatGetSchurComplement_Basic().
614: MatCreateSchurComplement() takes as arguments the four submatrices and returns the virtual Schur complement (what this returns in S).
616: MatSchurComplementGetPmat() takes the virtual Schur complement and returns an explicit approximate Schur complement (what this returns in Sp).
618: In other words calling MatCreateSchurComplement() followed by MatSchurComplementGetPmat() produces the same output as this function but with slightly different
619: inputs. The actually submatrices of the original block matrix instead of index sets to the submatrices.
621: Developer Notes:
622: The API that includes MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementGetPmat() should be refactored to
623: remove redundancy and be clearer and simplier.
625: Level: advanced
627: .seealso: MatCreateSubMatrix(), PCFIELDSPLIT, MatCreateSchurComplement(), MatSchurComplementAinvType
628: @*/
629: PetscErrorCode MatGetSchurComplement(Mat A,IS isrow0,IS iscol0,IS isrow1,IS iscol1,MatReuse mreuse,Mat *S,MatSchurComplementAinvType ainvtype,MatReuse preuse,Mat *Sp)
630: {
631: PetscErrorCode ierr,(*f)(Mat,IS,IS,IS,IS,MatReuse,Mat*,MatReuse,Mat*) = NULL;
645: if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
646: f = NULL;
647: if (mreuse == MAT_REUSE_MATRIX) { /* This is the only situation, in which we can demand that the user pass a non-NULL pointer to non-garbage in S. */
648: PetscObjectQueryFunction((PetscObject)*S,"MatGetSchurComplement_C",&f);
649: }
650: if (f) {
651: (*f)(A,isrow0,iscol0,isrow1,iscol1,mreuse,S,preuse,Sp);
652: } else {
653: MatGetSchurComplement_Basic(A,isrow0,iscol0,isrow1,iscol1,mreuse,S,ainvtype,preuse,Sp);
654: }
655: return(0);
656: }
658: /*@
659: MatSchurComplementSetAinvType - set the type of approximation used for the inverse of the (0,0) block used in forming Sp in MatSchurComplementGetPmat()
661: Not collective.
663: Input Parameters:
664: + S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
665: - ainvtype - type of approximation used to form A00inv from A00 when assembling Sp = A11 - A10 A00inv A01:
666: MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_LUMP, or MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG
668: Options database:
669: -mat_schur_complement_ainv_type diag | lump | blockdiag
671: Note:
672: Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
673: Section 1.5 Writing Application Codes with PETSc-specific information. The default for assembled matrices is to use the inverse of the diagonal of
674: the (0,0) block A00 in place of A00^{-1}. This rarely produces a scalable algorithm. Optionally, A00 can be lumped
675: before forming inv(diag(A00)).
677: Level: advanced
679: .seealso: MatSchurComplementAinvType, MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementGetAinvType()
680: @*/
681: PetscErrorCode MatSchurComplementSetAinvType(Mat S,MatSchurComplementAinvType ainvtype)
682: {
683: PetscErrorCode ierr;
684: PetscBool isschur;
685: Mat_SchurComplement *schur;
689: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
690: if (!isschur) return(0);
692: schur = (Mat_SchurComplement*)S->data;
693: if (ainvtype != MAT_SCHUR_COMPLEMENT_AINV_DIAG && ainvtype != MAT_SCHUR_COMPLEMENT_AINV_LUMP && ainvtype != MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatSchurComplementAinvType: %d",(int)ainvtype);
694: schur->ainvtype = ainvtype;
695: return(0);
696: }
698: /*@
699: MatSchurComplementGetAinvType - get the type of approximation for the inverse of the (0,0) block used in forming Sp in MatSchurComplementGetPmat()
701: Not collective.
703: Input Parameter:
704: . S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
706: Output Parameter:
707: . ainvtype - type of approximation used to form A00inv from A00 when assembling Sp = A11 - A10 A00inv A01:
708: MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_LUMP, or MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG
710: Note:
711: Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
712: Section 1.5 Writing Application Codes with PETSc-specific information. The default for assembled matrices is to use the inverse of the diagonal of
713: the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
714: before forming inv(diag(A00)).
716: Level: advanced
718: .seealso: MatSchurComplementAinvType, MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementSetAinvType()
719: @*/
720: PetscErrorCode MatSchurComplementGetAinvType(Mat S,MatSchurComplementAinvType *ainvtype)
721: {
722: PetscErrorCode ierr;
723: PetscBool isschur;
724: Mat_SchurComplement *schur;
728: PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
729: if (!isschur) SETERRQ1(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONG,"Not for type %s",((PetscObject)S)->type_name);
730: schur = (Mat_SchurComplement*)S->data;
731: if (ainvtype) *ainvtype = schur->ainvtype;
732: return(0);
733: }
735: /*@
736: MatCreateSchurComplementPmat - create a preconditioning matrix for the Schur complement by assembling Sp = A11 - A10 inv(diag(A00)) A01
738: Collective on A00
740: Input Parameters:
741: + A00,A01,A10,A11 - the four parts of the original matrix A = [A00 A01; A10 A11] (A01,A10, and A11 are optional, implying zero matrices)
742: . ainvtype - type of approximation for inv(A00) used when forming Sp = A11 - A10 inv(A00) A01
743: - preuse - MAT_INITIAL_MATRIX for a new Sp, or MAT_REUSE_MATRIX to reuse an existing Sp, or MAT_IGNORE_MATRIX to put nothing in Sp
745: Output Parameter:
746: - Spmat - approximate Schur complement suitable for preconditioning S = A11 - A10 inv(diag(A00)) A01
748: Note:
749: Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
750: Section 1.5 Writing Application Codes with PETSc-specific information. The default for assembled matrices is to use the inverse of the diagonal of
751: the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
752: before forming inv(diag(A00)).
754: Level: advanced
756: .seealso: MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementAinvType
757: @*/
758: PetscErrorCode MatCreateSchurComplementPmat(Mat A00,Mat A01,Mat A10,Mat A11,MatSchurComplementAinvType ainvtype,MatReuse preuse,Mat *Spmat)
759: {
761: PetscInt N00;
764: /* Use an appropriate approximate inverse of A00 to form A11 - A10 inv(diag(A00)) A01; a NULL A01, A10 or A11 indicates a zero matrix. */
765: /* TODO: Perhaps should create an appropriately-sized zero matrix of the same type as A00? */
766: if ((!A01 || !A10) & !A11) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot assemble Spmat: A01, A10 and A11 are all NULL.");
768: if (preuse == MAT_IGNORE_MATRIX) return(0);
770: /* A zero size A00 or empty A01 or A10 imply S = A11. */
771: MatGetSize(A00,&N00,NULL);
772: if (!A01 || !A10 || !N00) {
773: if (preuse == MAT_INITIAL_MATRIX) {
774: MatDuplicate(A11,MAT_COPY_VALUES,Spmat);
775: } else { /* MAT_REUSE_MATRIX */
776: /* TODO: when can we pass SAME_NONZERO_PATTERN? */
777: MatCopy(A11,*Spmat,DIFFERENT_NONZERO_PATTERN);
778: }
779: } else {
780: Mat AdB;
781: Vec diag;
783: if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_LUMP || ainvtype == MAT_SCHUR_COMPLEMENT_AINV_DIAG) {
784: MatDuplicate(A01,MAT_COPY_VALUES,&AdB);
785: MatCreateVecs(A00,&diag,NULL);
786: if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_LUMP) {
787: MatGetRowSum(A00,diag);
788: } else {
789: MatGetDiagonal(A00,diag);
790: }
791: VecReciprocal(diag);
792: MatDiagonalScale(AdB,diag,NULL);
793: VecDestroy(&diag);
794: } else if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG) {
795: Mat A00_inv;
796: MatType type;
797: MPI_Comm comm;
799: PetscObjectGetComm((PetscObject)A00,&comm);
800: MatGetType(A00,&type);
801: MatCreate(comm,&A00_inv);
802: MatSetType(A00_inv,type);
803: MatInvertBlockDiagonalMat(A00,A00_inv);
804: MatMatMult(A00_inv,A01,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AdB);
805: MatDestroy(&A00_inv);
806: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatSchurComplementAinvType: %D", ainvtype);
807: /* Cannot really reuse Spmat in MatMatMult() because of MatAYPX() -->
808: MatAXPY() --> MatHeaderReplace() --> MatDestroy_XXX_MatMatMult() */
809: MatDestroy(Spmat);
810: MatMatMult(A10,AdB,MAT_INITIAL_MATRIX,PETSC_DEFAULT,Spmat);
811: if (!A11) {
812: MatScale(*Spmat,-1.0);
813: } else {
814: /* TODO: when can we pass SAME_NONZERO_PATTERN? */
815: MatAYPX(*Spmat,-1,A11,DIFFERENT_NONZERO_PATTERN);
816: }
817: MatDestroy(&AdB);
818: }
819: return(0);
820: }
822: PetscErrorCode MatSchurComplementGetPmat_Basic(Mat S,MatReuse preuse,Mat *Spmat)
823: {
824: Mat A,B,C,D;
825: Mat_SchurComplement *schur = (Mat_SchurComplement *)S->data;
826: PetscErrorCode ierr;
829: if (preuse == MAT_IGNORE_MATRIX) return(0);
830: MatSchurComplementGetSubMatrices(S,&A,NULL,&B,&C,&D);
831: if (!A) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Schur complement component matrices unset");
832: MatCreateSchurComplementPmat(A,B,C,D,schur->ainvtype,preuse,Spmat);
833: return(0);
834: }
836: /*@
837: MatSchurComplementGetPmat - Obtain a preconditioning matrix for the Schur complement by assembling Sp = A11 - A10 inv(diag(A00)) A01
839: Collective on S
841: Input Parameters:
842: + S - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
843: - preuse - MAT_INITIAL_MATRIX for a new Sp, or MAT_REUSE_MATRIX to reuse an existing Sp, or MAT_IGNORE_MATRIX to put nothing in Sp
845: Output Parameter:
846: - Sp - approximate Schur complement suitable for preconditioning S = A11 - A10 inv(diag(A00)) A01
848: Note:
849: Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
850: Section 1.5 Writing Application Codes with PETSc-specific information. The default for assembled matrices is to use the inverse of the diagonal of
851: the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
852: before forming inv(diag(A00)).
854: Sometimes users would like to provide problem-specific data in the Schur complement, usually only
855: for special row and column index sets. In that case, the user should call PetscObjectComposeFunction() to set
856: "MatSchurComplementGetPmat_C" to their function. If their function needs to fall back to the default implementation,
857: it should call MatSchurComplementGetPmat_Basic().
859: Developer Notes:
860: The API that includes MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementGetPmat() should be refactored to
861: remove redundancy and be clearer and simplier.
863: Level: advanced
865: .seealso: MatCreateSubMatrix(), PCFIELDSPLIT, MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementSetAinvType()
866: @*/
867: PetscErrorCode MatSchurComplementGetPmat(Mat S,MatReuse preuse,Mat *Sp)
868: {
869: PetscErrorCode ierr,(*f)(Mat,MatReuse,Mat*);
877: if (S->factortype) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
879: PetscObjectQueryFunction((PetscObject)S,"MatSchurComplementGetPmat_C",&f);
880: if (f) {
881: (*f)(S,preuse,Sp);
882: } else {
883: MatSchurComplementGetPmat_Basic(S,preuse,Sp);
884: }
885: return(0);
886: }
888: PETSC_EXTERN PetscErrorCode MatCreate_SchurComplement(Mat N)
889: {
890: PetscErrorCode ierr;
891: Mat_SchurComplement *Na;
894: PetscNewLog(N,&Na);
895: N->data = (void*) Na;
897: N->ops->destroy = MatDestroy_SchurComplement;
898: N->ops->getvecs = MatCreateVecs_SchurComplement;
899: N->ops->view = MatView_SchurComplement;
900: N->ops->mult = MatMult_SchurComplement;
901: N->ops->multtranspose = MatMultTranspose_SchurComplement;
902: N->ops->multadd = MatMultAdd_SchurComplement;
903: N->ops->setfromoptions = MatSetFromOptions_SchurComplement;
904: N->assembled = PETSC_FALSE;
905: N->preallocated = PETSC_FALSE;
907: KSPCreate(PetscObjectComm((PetscObject)N),&Na->ksp);
908: PetscObjectChangeTypeName((PetscObject)N,MATSCHURCOMPLEMENT);
909: return(0);
910: }