Actual source code: schurm.c

petsc-3.15.0 2021-04-05
Report Typos and Errors
  1: #include <../src/ksp/ksp/utils/schurm/schurm.h>

  3: const char *const MatSchurComplementAinvTypes[] = {"DIAG","LUMP","BLOCKDIAG","MatSchurComplementAinvType","MAT_SCHUR_COMPLEMENT_AINV_",NULL};

  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 A, Mat *S)
477: {
478:   Mat            B, C, D, Bd, AinvBd;
479:   KSP            ksp;
480:   PetscInt       n,N,m,M;

484:   MatSchurComplementGetSubMatrices(A, NULL, NULL, &B, &C, &D);
485:   MatSchurComplementGetKSP(A, &ksp);
486:   KSPSetUp(ksp);
487:   MatConvert(B, MATDENSE, MAT_INITIAL_MATRIX, &Bd);
488:   MatDuplicate(Bd, MAT_DO_NOT_COPY_VALUES, &AinvBd);
489:   KSPMatSolve(ksp, Bd, AinvBd);
490:   MatDestroy(&Bd);
491:   MatChop(AinvBd, PETSC_SMALL);
492:   if (D) {
493:     MatGetLocalSize(D, &m, &n);
494:     MatGetSize(D, &M, &N);
495:     MatCreateDense(PetscObjectComm((PetscObject)A), m, n, M, N, NULL, S);
496:   }
497:   MatMatMult(C, AinvBd, D ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX, PETSC_DEFAULT, S);
498:   MatDestroy(&AinvBd);
499:   if (D) {
500:     MatAXPY(*S, -1.0, D, DIFFERENT_NONZERO_PATTERN);
501:   }
502:   MatConvert(*S, MATAIJ, MAT_INPLACE_MATRIX, S);
503:   MatScale(*S, -1.0);
504:   return(0);
505: }

507: /* Developer Notes:
508:     This should be implemented with a MatCreate_SchurComplement() as that is the standard design for new Mat classes. */
509: PetscErrorCode MatGetSchurComplement_Basic(Mat mat,IS isrow0,IS iscol0,IS isrow1,IS iscol1,MatReuse mreuse,Mat *newmat,MatSchurComplementAinvType ainvtype, MatReuse preuse,Mat *newpmat)
510: {
512:   Mat            A=NULL,Ap=NULL,B=NULL,C=NULL,D=NULL;
513:   MatReuse       reuse;

525:   if (mreuse == MAT_IGNORE_MATRIX && preuse == MAT_IGNORE_MATRIX) return(0);

529:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

531:   reuse = MAT_INITIAL_MATRIX;
532:   if (mreuse == MAT_REUSE_MATRIX) {
533:     MatSchurComplementGetSubMatrices(*newmat,&A,&Ap,&B,&C,&D);
534:     if (!A || !Ap || !B || !C) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Attempting to reuse matrix but Schur complement matrices unset");
535:     if (A != Ap) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Preconditioning matrix does not match operator");
536:     MatDestroy(&Ap); /* get rid of extra reference */
537:     reuse = MAT_REUSE_MATRIX;
538:   }
539:   MatCreateSubMatrix(mat,isrow0,iscol0,reuse,&A);
540:   MatCreateSubMatrix(mat,isrow0,iscol1,reuse,&B);
541:   MatCreateSubMatrix(mat,isrow1,iscol0,reuse,&C);
542:   MatCreateSubMatrix(mat,isrow1,iscol1,reuse,&D);
543:   switch (mreuse) {
544:   case MAT_INITIAL_MATRIX:
545:     MatCreateSchurComplement(A,A,B,C,D,newmat);
546:     break;
547:   case MAT_REUSE_MATRIX:
548:     MatSchurComplementUpdateSubMatrices(*newmat,A,A,B,C,D);
549:     break;
550:   default:
551:     if (mreuse != MAT_IGNORE_MATRIX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Unrecognized value of mreuse %d",(int)mreuse);
552:   }
553:   if (preuse != MAT_IGNORE_MATRIX) {
554:     MatCreateSchurComplementPmat(A,B,C,D,ainvtype,preuse,newpmat);
555:   }
556:   MatDestroy(&A);
557:   MatDestroy(&B);
558:   MatDestroy(&C);
559:   MatDestroy(&D);
560:   return(0);
561: }

563: /*@
564:     MatGetSchurComplement - Obtain the Schur complement from eliminating part of the matrix in another part.

566:     Collective on A

568:     Input Parameters:
569: +   A      - matrix in which the complement is to be taken
570: .   isrow0 - rows to eliminate
571: .   iscol0 - columns to eliminate, (isrow0,iscol0) should be square and nonsingular
572: .   isrow1 - rows in which the Schur complement is formed
573: .   iscol1 - columns in which the Schur complement is formed
574: .   mreuse - MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX, use MAT_IGNORE_MATRIX to put nothing in S
575: .   ainvtype - the type of approximation used for the inverse of the (0,0) block used in forming Sp:
576:                        MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG, or MAT_SCHUR_COMPLEMENT_AINV_LUMP
577: -   preuse - MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX, use MAT_IGNORE_MATRIX to put nothing in Sp

579:     Output Parameters:
580: +   S      - exact Schur complement, often of type MATSCHURCOMPLEMENT which is difficult to use for preconditioning
581: -   Sp     - approximate Schur complement from which a preconditioner can be built

583:     Note:
584:     Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
585:     application-specific information.  The default for assembled matrices is to use the inverse of the diagonal of
586:     the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
587:     before forming inv(diag(A00)).

589:     Sometimes users would like to provide problem-specific data in the Schur complement, usually only for special row
590:     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
591:     "MatGetSchurComplement_C" to their function.  If their function needs to fall back to the default implementation, it
592:     should call MatGetSchurComplement_Basic().

594:     MatCreateSchurComplement() takes as arguments the four submatrices and returns the virtual Schur complement (what this returns in S).

596:     MatSchurComplementGetPmat() takes the virtual Schur complement and returns an explicit approximate Schur complement (what this returns in Sp).

598:     In other words calling MatCreateSchurComplement() followed by MatSchurComplementGetPmat() produces the same output as this function but with slightly different
599:     inputs. The actually submatrices of the original block matrix instead of index sets to the submatrices.

601:     Developer Notes:
602:     The API that includes MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementGetPmat() should be refactored to
603:     remove redundancy and be clearer and simplier.

605:     Level: advanced

607: .seealso: MatCreateSubMatrix(), PCFIELDSPLIT, MatCreateSchurComplement(), MatSchurComplementAinvType
608: @*/
609: PetscErrorCode  MatGetSchurComplement(Mat A,IS isrow0,IS iscol0,IS isrow1,IS iscol1,MatReuse mreuse,Mat *S,MatSchurComplementAinvType ainvtype,MatReuse preuse,Mat *Sp)
610: {
611:   PetscErrorCode ierr,(*f)(Mat,IS,IS,IS,IS,MatReuse,Mat*,MatReuse,Mat*) = NULL;

625:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
626:   f = NULL;
627:   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. */
628:     PetscObjectQueryFunction((PetscObject)*S,"MatGetSchurComplement_C",&f);
629:   }
630:   if (f) {
631:     (*f)(A,isrow0,iscol0,isrow1,iscol1,mreuse,S,preuse,Sp);
632:   } else {
633:     MatGetSchurComplement_Basic(A,isrow0,iscol0,isrow1,iscol1,mreuse,S,ainvtype,preuse,Sp);
634:   }
635:   return(0);
636: }

638: /*@
639:     MatSchurComplementSetAinvType - set the type of approximation used for the inverse of the (0,0) block used in forming Sp in MatSchurComplementGetPmat()

641:     Not collective.

643:     Input Parameters:
644: +   S        - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
645: -   ainvtype - type of approximation used to form A00inv from A00 when assembling Sp = A11 - A10 A00inv A01:
646:                       MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_LUMP, or MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG

648:     Options database:
649:     -mat_schur_complement_ainv_type diag | lump | blockdiag

651:     Note:
652:     Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
653:     application-specific information.  The default for assembled matrices is to use the inverse of the diagonal of
654:     the (0,0) block A00 in place of A00^{-1}. This rarely produces a scalable algorithm. Optionally, A00 can be lumped
655:     before forming inv(diag(A00)).

657:     Level: advanced

659: .seealso: MatSchurComplementAinvType, MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementGetAinvType()
660: @*/
661: PetscErrorCode  MatSchurComplementSetAinvType(Mat S,MatSchurComplementAinvType ainvtype)
662: {
663:   PetscErrorCode      ierr;
664:   PetscBool           isschur;
665:   Mat_SchurComplement *schur;

669:   PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
670:   if (!isschur) return(0);
672:   schur = (Mat_SchurComplement*)S->data;
673:   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);
674:   schur->ainvtype = ainvtype;
675:   return(0);
676: }

678: /*@
679:     MatSchurComplementGetAinvType - get the type of approximation for the inverse of the (0,0) block used in forming Sp in MatSchurComplementGetPmat()

681:     Not collective.

683:     Input Parameter:
684: .   S      - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01

686:     Output Parameter:
687: .   ainvtype - type of approximation used to form A00inv from A00 when assembling Sp = A11 - A10 A00inv A01:
688:                       MAT_SCHUR_COMPLEMENT_AINV_DIAG, MAT_SCHUR_COMPLEMENT_AINV_LUMP, or MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG

690:     Note:
691:     Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
692:     application-specific information.  The default for assembled matrices is to use the inverse of the diagonal of
693:     the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
694:     before forming inv(diag(A00)).

696:     Level: advanced

698: .seealso: MatSchurComplementAinvType, MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementSetAinvType()
699: @*/
700: PetscErrorCode  MatSchurComplementGetAinvType(Mat S,MatSchurComplementAinvType *ainvtype)
701: {
702:   PetscErrorCode      ierr;
703:   PetscBool           isschur;
704:   Mat_SchurComplement *schur;

708:   PetscObjectTypeCompare((PetscObject)S,MATSCHURCOMPLEMENT,&isschur);
709:   if (!isschur) SETERRQ1(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONG,"Not for type %s",((PetscObject)S)->type_name);
710:   schur = (Mat_SchurComplement*)S->data;
711:   if (ainvtype) *ainvtype = schur->ainvtype;
712:   return(0);
713: }

715: /*@
716:     MatCreateSchurComplementPmat - create a preconditioning matrix for the Schur complement by assembling Sp = A11 - A10 inv(diag(A00)) A01

718:     Collective on A00

720:     Input Parameters:
721: +   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)
722: .   ainvtype             - type of approximation for inv(A00) used when forming Sp = A11 - A10 inv(A00) A01
723: -   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

725:     Output Parameter:
726: -   Spmat                - approximate Schur complement suitable for preconditioning S = A11 - A10 inv(diag(A00)) A01

728:     Note:
729:     Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
730:     application-specific information.  The default for assembled matrices is to use the inverse of the diagonal of
731:     the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
732:     before forming inv(diag(A00)).

734:     Level: advanced

736: .seealso: MatCreateSchurComplement(), MatGetSchurComplement(), MatSchurComplementGetPmat(), MatSchurComplementAinvType
737: @*/
738: PetscErrorCode  MatCreateSchurComplementPmat(Mat A00,Mat A01,Mat A10,Mat A11,MatSchurComplementAinvType ainvtype,MatReuse preuse,Mat *Spmat)
739: {
741:   PetscInt       N00;

744:   /* 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. */
745:   /* TODO: Perhaps should create an appropriately-sized zero matrix of the same type as A00? */
746:   if ((!A01 || !A10) & !A11) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot assemble Spmat: A01, A10 and A11 are all NULL.");
748:   if (preuse == MAT_IGNORE_MATRIX) return(0);

750:   /* A zero size A00 or empty A01 or A10 imply S = A11. */
751:   MatGetSize(A00,&N00,NULL);
752:   if (!A01 || !A10 || !N00) {
753:     if (preuse == MAT_INITIAL_MATRIX) {
754:       MatDuplicate(A11,MAT_COPY_VALUES,Spmat);
755:     } else { /* MAT_REUSE_MATRIX */
756:       /* TODO: when can we pass SAME_NONZERO_PATTERN? */
757:       MatCopy(A11,*Spmat,DIFFERENT_NONZERO_PATTERN);
758:     }
759:   } else {
760:     Mat AdB;
761:     Vec diag;

763:     if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_LUMP || ainvtype == MAT_SCHUR_COMPLEMENT_AINV_DIAG) {
764:       MatDuplicate(A01,MAT_COPY_VALUES,&AdB);
765:       MatCreateVecs(A00,&diag,NULL);
766:       if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_LUMP) {
767:         MatGetRowSum(A00,diag);
768:       } else {
769:         MatGetDiagonal(A00,diag);
770:       }
771:       VecReciprocal(diag);
772:       MatDiagonalScale(AdB,diag,NULL);
773:       VecDestroy(&diag);
774:     } else if (ainvtype == MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG) {
775:       Mat      A00_inv;
776:       MatType  type;
777:       MPI_Comm comm;

779:       PetscObjectGetComm((PetscObject)A00,&comm);
780:       MatGetType(A00,&type);
781:       MatCreate(comm,&A00_inv);
782:       MatSetType(A00_inv,type);
783:       MatInvertBlockDiagonalMat(A00,A00_inv);
784:       MatMatMult(A00_inv,A01,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AdB);
785:       MatDestroy(&A00_inv);
786:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatSchurComplementAinvType: %D", ainvtype);
787:     /* Cannot really reuse Spmat in MatMatMult() because of MatAYPX() -->
788:          MatAXPY() --> MatHeaderReplace() --> MatDestroy_XXX_MatMatMult()  */
789:     MatDestroy(Spmat);
790:     MatMatMult(A10,AdB,MAT_INITIAL_MATRIX,PETSC_DEFAULT,Spmat);
791:     if (!A11) {
792:       MatScale(*Spmat,-1.0);
793:     } else {
794:       /* TODO: when can we pass SAME_NONZERO_PATTERN? */
795:       MatAYPX(*Spmat,-1,A11,DIFFERENT_NONZERO_PATTERN);
796:     }
797:     MatDestroy(&AdB);
798:   }
799:   return(0);
800: }

802: PetscErrorCode  MatSchurComplementGetPmat_Basic(Mat S,MatReuse preuse,Mat *Spmat)
803: {
804:   Mat A,B,C,D;
805:   Mat_SchurComplement *schur = (Mat_SchurComplement *)S->data;
806:   PetscErrorCode      ierr;

809:   if (preuse == MAT_IGNORE_MATRIX) return(0);
810:   MatSchurComplementGetSubMatrices(S,&A,NULL,&B,&C,&D);
811:   if (!A) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Schur complement component matrices unset");
812:   MatCreateSchurComplementPmat(A,B,C,D,schur->ainvtype,preuse,Spmat);
813:   return(0);
814: }

816: /*@
817:     MatSchurComplementGetPmat - Obtain a preconditioning matrix for the Schur complement by assembling Sp = A11 - A10 inv(diag(A00)) A01

819:     Collective on S

821:     Input Parameters:
822: +   S      - matrix obtained with MatCreateSchurComplement() (or equivalent) and implementing the action of A11 - A10 ksp(A00,Ap00) A01
823: -   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

825:     Output Parameter:
826: -   Sp     - approximate Schur complement suitable for preconditioning S = A11 - A10 inv(diag(A00)) A01

828:     Note:
829:     Since the real Schur complement is usually dense, providing a good approximation to newpmat usually requires
830:     application-specific information.  The default for assembled matrices is to use the inverse of the diagonal of
831:     the (0,0) block A00 in place of A00^{-1}. This rarely produce a scalable algorithm. Optionally, A00 can be lumped
832:     before forming inv(diag(A00)).

834:     Sometimes users would like to provide problem-specific data in the Schur complement, usually only
835:     for special row and column index sets.  In that case, the user should call PetscObjectComposeFunction() to set
836:     "MatSchurComplementGetPmat_C" to their function.  If their function needs to fall back to the default implementation,
837:     it should call MatSchurComplementGetPmat_Basic().

839:     Developer Notes:
840:     The API that includes MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementGetPmat() should be refactored to
841:     remove redundancy and be clearer and simplier.

843:     Level: advanced

845: .seealso: MatCreateSubMatrix(), PCFIELDSPLIT, MatGetSchurComplement(), MatCreateSchurComplement(), MatSchurComplementSetAinvType()
846: @*/
847: PetscErrorCode  MatSchurComplementGetPmat(Mat S,MatReuse preuse,Mat *Sp)
848: {
849:   PetscErrorCode ierr,(*f)(Mat,MatReuse,Mat*);

857:   if (S->factortype) SETERRQ(PetscObjectComm((PetscObject)S),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

859:   PetscObjectQueryFunction((PetscObject)S,"MatSchurComplementGetPmat_C",&f);
860:   if (f) {
861:     (*f)(S,preuse,Sp);
862:   } else {
863:     MatSchurComplementGetPmat_Basic(S,preuse,Sp);
864:   }
865:   return(0);
866: }

868: PETSC_EXTERN PetscErrorCode MatCreate_SchurComplement(Mat N)
869: {
870:   PetscErrorCode      ierr;
871:   Mat_SchurComplement *Na;

874:   PetscNewLog(N,&Na);
875:   N->data = (void*) Na;

877:   N->ops->destroy        = MatDestroy_SchurComplement;
878:   N->ops->getvecs        = MatCreateVecs_SchurComplement;
879:   N->ops->view           = MatView_SchurComplement;
880:   N->ops->mult           = MatMult_SchurComplement;
881:   N->ops->multtranspose  = MatMultTranspose_SchurComplement;
882:   N->ops->multadd        = MatMultAdd_SchurComplement;
883:   N->ops->setfromoptions = MatSetFromOptions_SchurComplement;
884:   N->assembled           = PETSC_FALSE;
885:   N->preallocated        = PETSC_FALSE;

887:   KSPCreate(PetscObjectComm((PetscObject)N),&Na->ksp);
888:   PetscObjectChangeTypeName((PetscObject)N,MATSCHURCOMPLEMENT);
889:   return(0);
890: }