Actual source code: fieldsplit.c
1: #include <petsc/private/pcimpl.h>
2: #include <petsc/private/kspimpl.h>
3: #include <petsc/private/matimpl.h>
4: #include <petscdm.h>
5: #include <petscdevice.h>
6: #if PetscDefined(HAVE_CUDA)
7: #include <petscdevice_cuda.h>
8: #endif
9: #if PetscDefined(HAVE_HIP)
10: #include <petscdevice_hip.h>
11: #endif
13: const char *const PCFieldSplitSchurPreTypes[] = {"SELF", "SELFP", "A11", "USER", "FULL", "PCFieldSplitSchurPreType", "PC_FIELDSPLIT_SCHUR_PRE_", NULL};
14: const char *const PCFieldSplitSchurFactTypes[] = {"DIAG", "LOWER", "UPPER", "FULL", "PCFieldSplitSchurFactType", "PC_FIELDSPLIT_SCHUR_FACT_", NULL};
16: PetscLogEvent KSP_Solve_FS_0, KSP_Solve_FS_1, KSP_Solve_FS_S, KSP_Solve_FS_U, KSP_Solve_FS_L, KSP_Solve_FS_2, KSP_Solve_FS_3, KSP_Solve_FS_4;
18: typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
19: struct _PC_FieldSplitLink {
20: KSP ksp;
21: Vec x, y, z;
22: Mat X, Y, Z;
23: char *splitname;
24: PetscInt nfields;
25: PetscInt *fields, *fields_col;
26: VecScatter sctx;
27: IS is, is_col;
28: PC_FieldSplitLink next, previous;
29: PetscLogEvent event;
31: /* Used only when setting coordinates with PCSetCoordinates */
32: PetscInt dim;
33: PetscInt ndofs;
34: PetscReal *coords;
35: };
37: typedef struct {
38: PCCompositeType type;
39: PetscBool defaultsplit; /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
40: PetscBool splitdefined; /* Flag is set after the splits have been defined, to prevent more splits from being added */
41: PetscInt bs; /* Block size for IS and Mat structures */
42: PetscInt nsplits; /* Number of field divisions defined */
43: Vec *x, *y, w1, w2;
44: Mat *mat; /* The diagonal block for each split */
45: Mat *pmat; /* The preconditioning diagonal block for each split */
46: Mat *Afield; /* The rows of the matrix associated with each split */
47: PetscBool issetup;
49: /* Only used when Schur complement preconditioning is used */
50: Mat B; /* The (0,1) block */
51: Mat C; /* The (1,0) block */
52: Mat schur; /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
53: Mat schurp; /* Assembled approximation to S built by MatSchurComplement to be used as a matrix for constructing the preconditioner when solving with S */
54: Mat schur_user; /* User-provided matrix for constructing the preconditioner for the Schur complement */
55: PCFieldSplitSchurPreType schurpre; /* Determines which matrix is used for the Schur complement */
56: PCFieldSplitSchurFactType schurfactorization;
57: KSP kspschur; /* The solver for S */
58: KSP kspupper; /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
59: PetscScalar schurscale; /* Scaling factor for the Schur complement solution with DIAG factorization */
61: /* Only used when Golub-Kahan bidiagonalization preconditioning is used */
62: Mat H; /* The modified matrix H = A00 + nu*A01*A01' */
63: PetscReal gkbtol; /* Stopping tolerance for lower bound estimate */
64: PetscInt gkbdelay; /* The delay window for the stopping criterion */
65: PetscReal gkbnu; /* Parameter for augmented Lagrangian H = A + nu*A01*A01' */
66: PetscInt gkbmaxit; /* Maximum number of iterations for outer loop */
67: PetscBool gkbmonitor; /* Monitor for gkb iterations and the lower bound error */
68: PetscViewer gkbviewer; /* Viewer context for gkbmonitor */
69: Vec u, v, d, Hu; /* Work vectors for the GKB algorithm */
70: PetscScalar *vecz; /* Contains intermediate values, eg for lower bound */
72: PC_FieldSplitLink head;
73: PetscBool isrestrict; /* indicates PCFieldSplitRestrictIS() has been last called on this object, hack */
74: PetscBool suboptionsset; /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
75: PetscBool dm_splits; /* Whether to use DMCreateFieldDecomposition() whenever possible */
76: PetscBool diag_use_amat; /* Whether to extract diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
77: PetscBool offdiag_use_amat; /* Whether to extract off-diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
78: PetscBool detect; /* Whether to form 2-way split by finding zero diagonal entries */
79: PetscBool coordinates_set; /* Whether PCSetCoordinates has been called */
80: } PC_FieldSplit;
82: /*
83: Note:
84: there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
85: inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
86: PC you could change this.
87: */
89: /* This helper is so that setting a user-provided matrix is orthogonal to choosing to use it. This way the
90: * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
91: static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
92: {
93: switch (jac->schurpre) {
94: case PC_FIELDSPLIT_SCHUR_PRE_SELF:
95: return jac->schur;
96: case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
97: return jac->schurp;
98: case PC_FIELDSPLIT_SCHUR_PRE_A11:
99: return jac->pmat[1];
100: case PC_FIELDSPLIT_SCHUR_PRE_FULL: /* We calculate this and store it in schur_user */
101: case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
102: default:
103: return jac->schur_user ? jac->schur_user : jac->pmat[1];
104: }
105: }
107: #include <petscdraw.h>
108: static PetscErrorCode PCView_FieldSplit(PC pc, PetscViewer viewer)
109: {
110: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
111: PetscBool isascii, isdraw;
112: PetscInt i, j;
113: PC_FieldSplitLink ilink = jac->head;
115: PetscFunctionBegin;
116: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
117: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
118: if (isascii) {
119: if (jac->bs > 0) {
120: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with %s composition: total splits = %" PetscInt_FMT ", blocksize = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits, jac->bs));
121: } else {
122: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with %s composition: total splits = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits));
123: }
124: if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for blocks\n"));
125: if (jac->diag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for diagonal blocks\n"));
126: if (jac->offdiag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for off-diagonal blocks\n"));
127: PetscCall(PetscViewerASCIIPrintf(viewer, " Solver info for each split is in the following KSP objects:\n"));
128: for (i = 0; i < jac->nsplits; i++) {
129: if (ilink->fields) {
130: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Fields ", i));
131: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
132: for (j = 0; j < ilink->nfields; j++) {
133: if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
134: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
135: }
136: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
137: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
138: } else {
139: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Defined by IS\n", i));
140: }
141: PetscCall(KSPView(ilink->ksp, viewer));
142: ilink = ilink->next;
143: }
144: }
146: if (isdraw) {
147: PetscDraw draw;
148: PetscReal x, y, w, wd;
150: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
151: PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
152: w = 2 * PetscMin(1.0 - x, x);
153: wd = w / (jac->nsplits + 1);
154: x = x - wd * (jac->nsplits - 1) / 2.0;
155: for (i = 0; i < jac->nsplits; i++) {
156: PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
157: PetscCall(KSPView(ilink->ksp, viewer));
158: PetscCall(PetscDrawPopCurrentPoint(draw));
159: x += wd;
160: ilink = ilink->next;
161: }
162: }
163: PetscFunctionReturn(PETSC_SUCCESS);
164: }
166: static PetscErrorCode PCView_FieldSplit_Schur(PC pc, PetscViewer viewer)
167: {
168: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
169: PetscBool isascii, isdraw;
170: PetscInt i, j;
171: PC_FieldSplitLink ilink = jac->head;
172: MatSchurComplementAinvType atype;
174: PetscFunctionBegin;
175: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
176: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
177: if (isascii) {
178: if (jac->bs > 0) {
179: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with Schur preconditioner, blocksize = %" PetscInt_FMT ", factorization %s\n", jac->bs, PCFieldSplitSchurFactTypes[jac->schurfactorization]));
180: } else {
181: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with Schur preconditioner, factorization %s\n", PCFieldSplitSchurFactTypes[jac->schurfactorization]));
182: }
183: if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for blocks\n"));
184: switch (jac->schurpre) {
185: case PC_FIELDSPLIT_SCHUR_PRE_SELF:
186: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from S itself\n"));
187: break;
188: case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
189: if (jac->schur) {
190: PetscCall(MatSchurComplementGetAinvType(jac->schur, &atype));
191: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from Sp, an assembled approximation to S, which uses A00's %sinverse\n", atype == MAT_SCHUR_COMPLEMENT_AINV_DIAG ? "diagonal's " : (atype == MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG ? "block diagonal's " : (atype == MAT_SCHUR_COMPLEMENT_AINV_FULL ? "full " : "lumped diagonal's "))));
192: }
193: break;
194: case PC_FIELDSPLIT_SCHUR_PRE_A11:
195: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from A11\n"));
196: break;
197: case PC_FIELDSPLIT_SCHUR_PRE_FULL:
198: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from the exact Schur complement\n"));
199: break;
200: case PC_FIELDSPLIT_SCHUR_PRE_USER:
201: if (jac->schur_user) {
202: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from user provided matrix\n"));
203: } else {
204: PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioner for the Schur complement formed from A11\n"));
205: }
206: break;
207: default:
208: SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
209: }
210: PetscCall(PetscViewerASCIIPrintf(viewer, " Split info:\n"));
211: PetscCall(PetscViewerASCIIPushTab(viewer));
212: for (i = 0; i < jac->nsplits; i++) {
213: if (ilink->fields) {
214: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Fields ", i));
215: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
216: for (j = 0; j < ilink->nfields; j++) {
217: if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
218: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
219: }
220: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
221: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
222: } else {
223: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Defined by IS\n", i));
224: }
225: ilink = ilink->next;
226: }
227: PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for A00 block\n"));
228: PetscCall(PetscViewerASCIIPushTab(viewer));
229: if (jac->head) PetscCall(KSPView(jac->head->ksp, viewer));
230: else PetscCall(PetscViewerASCIIPrintf(viewer, " not yet available\n"));
231: PetscCall(PetscViewerASCIIPopTab(viewer));
232: if (jac->head && jac->kspupper != jac->head->ksp) {
233: PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for upper A00 in upper triangular factor\n"));
234: PetscCall(PetscViewerASCIIPushTab(viewer));
235: if (jac->kspupper) PetscCall(KSPView(jac->kspupper, viewer));
236: else PetscCall(PetscViewerASCIIPrintf(viewer, " not yet available\n"));
237: PetscCall(PetscViewerASCIIPopTab(viewer));
238: }
239: PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for S = A11 - A10 inv(A00) A01\n"));
240: PetscCall(PetscViewerASCIIPushTab(viewer));
241: if (jac->kspschur) {
242: PetscCall(KSPView(jac->kspschur, viewer));
243: } else {
244: PetscCall(PetscViewerASCIIPrintf(viewer, " not yet available\n"));
245: }
246: PetscCall(PetscViewerASCIIPopTab(viewer));
247: PetscCall(PetscViewerASCIIPopTab(viewer));
248: } else if (isdraw && jac->head) {
249: PetscDraw draw;
250: PetscReal x, y, w, wd, h;
251: PetscInt cnt = 2;
252: char str[32];
254: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
255: PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
256: if (jac->kspupper != jac->head->ksp) cnt++;
257: w = 2 * PetscMin(1.0 - x, x);
258: wd = w / (cnt + 1);
260: PetscCall(PetscSNPrintf(str, 32, "Schur fact. %s", PCFieldSplitSchurFactTypes[jac->schurfactorization]));
261: PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
262: y -= h;
263: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER && !jac->schur_user) {
264: PetscCall(PetscSNPrintf(str, 32, "Prec. for Schur from %s", PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]));
265: } else {
266: PetscCall(PetscSNPrintf(str, 32, "Prec. for Schur from %s", PCFieldSplitSchurPreTypes[jac->schurpre]));
267: }
268: PetscCall(PetscDrawStringBoxed(draw, x + wd * (cnt - 1) / 2.0, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
269: y -= h;
270: x = x - wd * (cnt - 1) / 2.0;
272: PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
273: PetscCall(KSPView(jac->head->ksp, viewer));
274: PetscCall(PetscDrawPopCurrentPoint(draw));
275: if (jac->kspupper != jac->head->ksp) {
276: x += wd;
277: PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
278: PetscCall(KSPView(jac->kspupper, viewer));
279: PetscCall(PetscDrawPopCurrentPoint(draw));
280: }
281: x += wd;
282: PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
283: PetscCall(KSPView(jac->kspschur, viewer));
284: PetscCall(PetscDrawPopCurrentPoint(draw));
285: }
286: PetscFunctionReturn(PETSC_SUCCESS);
287: }
289: static PetscErrorCode PCView_FieldSplit_GKB(PC pc, PetscViewer viewer)
290: {
291: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
292: PetscBool isascii, isdraw;
293: PetscInt i, j;
294: PC_FieldSplitLink ilink = jac->head;
296: PetscFunctionBegin;
297: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
298: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
299: if (isascii) {
300: if (jac->bs > 0) {
301: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with %s composition: total splits = %" PetscInt_FMT ", blocksize = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits, jac->bs));
302: } else {
303: PetscCall(PetscViewerASCIIPrintf(viewer, " FieldSplit with %s composition: total splits = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits));
304: }
305: if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for blocks\n"));
306: if (jac->diag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for diagonal blocks\n"));
307: if (jac->offdiag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat (not Pmat) as operator for off-diagonal blocks\n"));
309: PetscCall(PetscViewerASCIIPrintf(viewer, " Stopping tolerance=%.1e, delay in error estimate=%" PetscInt_FMT ", maximum iterations=%" PetscInt_FMT "\n", (double)jac->gkbtol, jac->gkbdelay, jac->gkbmaxit));
310: PetscCall(PetscViewerASCIIPrintf(viewer, " Solver info for H = A00 + nu*A01*A01' matrix:\n"));
311: PetscCall(PetscViewerASCIIPushTab(viewer));
313: if (ilink->fields) {
314: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number 0 Fields "));
315: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
316: for (j = 0; j < ilink->nfields; j++) {
317: if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
318: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
319: }
320: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
321: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
322: } else {
323: PetscCall(PetscViewerASCIIPrintf(viewer, "Split number 0 Defined by IS\n"));
324: }
325: PetscCall(KSPView(ilink->ksp, viewer));
327: PetscCall(PetscViewerASCIIPopTab(viewer));
328: }
330: if (isdraw) {
331: PetscDraw draw;
332: PetscReal x, y, w, wd;
334: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
335: PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
336: w = 2 * PetscMin(1.0 - x, x);
337: wd = w / (jac->nsplits + 1);
338: x = x - wd * (jac->nsplits - 1) / 2.0;
339: for (i = 0; i < jac->nsplits; i++) {
340: PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
341: PetscCall(KSPView(ilink->ksp, viewer));
342: PetscCall(PetscDrawPopCurrentPoint(draw));
343: x += wd;
344: ilink = ilink->next;
345: }
346: }
347: PetscFunctionReturn(PETSC_SUCCESS);
348: }
350: /* Precondition: jac->bs is set to a meaningful value or MATNEST */
351: static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
352: {
353: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
354: PetscInt bs, i, nfields, *ifields, nfields_col, *ifields_col;
355: PetscBool flg, flg_col, mnest;
356: char optionname[128], splitname[8], optionname_col[128];
358: PetscFunctionBegin;
359: PetscCall(PetscObjectTypeCompare((PetscObject)pc->mat, MATNEST, &mnest));
360: if (mnest) PetscCall(MatNestGetSize(pc->pmat, &bs, NULL));
361: else bs = jac->bs;
362: PetscCall(PetscMalloc2(bs, &ifields, bs, &ifields_col));
363: for (i = 0, flg = PETSC_TRUE;; i++) {
364: PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
365: PetscCall(PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%" PetscInt_FMT "_fields", i));
366: PetscCall(PetscSNPrintf(optionname_col, sizeof(optionname_col), "-pc_fieldsplit_%" PetscInt_FMT "_fields_col", i));
367: nfields = bs;
368: nfields_col = bs;
369: PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname, ifields, &nfields, &flg));
370: PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname_col, ifields_col, &nfields_col, &flg_col));
371: if (!flg) break;
372: else if (flg && !flg_col) {
373: PetscCheck(nfields, PETSC_COMM_SELF, PETSC_ERR_USER, "Cannot list zero fields");
374: PetscCall(PCFieldSplitSetFields(pc, splitname, nfields, ifields, ifields));
375: } else {
376: PetscCheck(nfields && nfields_col, PETSC_COMM_SELF, PETSC_ERR_USER, "Cannot list zero fields");
377: PetscCheck(nfields == nfields_col, PETSC_COMM_SELF, PETSC_ERR_USER, "Number of row and column fields must match");
378: PetscCall(PCFieldSplitSetFields(pc, splitname, nfields, ifields, ifields_col));
379: }
380: }
381: if (i > 0) {
382: /* Makes command-line setting of splits take precedence over setting them in code.
383: Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
384: create new splits, which would probably not be what the user wanted. */
385: jac->splitdefined = PETSC_TRUE;
386: }
387: PetscCall(PetscFree2(ifields, ifields_col));
388: PetscFunctionReturn(PETSC_SUCCESS);
389: }
391: static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
392: {
393: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
394: PC_FieldSplitLink ilink = jac->head;
395: PetscBool fieldsplit_default = PETSC_FALSE, coupling = PETSC_FALSE;
396: PetscInt i;
398: PetscFunctionBegin;
399: /*
400: Kinda messy, but at least this now uses DMCreateFieldDecomposition().
401: Should probably be rewritten.
402: */
403: if (!ilink) {
404: PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_detect_coupling", &coupling, NULL));
405: if (pc->dm && jac->dm_splits && !jac->detect && !coupling) {
406: PetscInt numFields, f, i, j;
407: char **fieldNames;
408: IS *fields;
409: DM *dms;
410: DM subdm[128];
411: PetscBool flg;
413: PetscCall(DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms));
414: /* Allow the user to prescribe the splits */
415: for (i = 0, flg = PETSC_TRUE;; i++) {
416: PetscInt ifields[128];
417: IS compField;
418: char optionname[128], splitname[8];
419: PetscInt nfields = numFields;
421: PetscCall(PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%" PetscInt_FMT "_fields", i));
422: PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname, ifields, &nfields, &flg));
423: if (!flg) break;
424: PetscCheck(numFields <= 128, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot currently support %" PetscInt_FMT " > 128 fields", numFields);
425: PetscCall(DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]));
426: if (nfields == 1) PetscCall(PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField));
427: else {
428: PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
429: PetscCall(PCFieldSplitSetIS(pc, splitname, compField));
430: }
431: PetscCall(ISDestroy(&compField));
432: for (j = 0; j < nfields; ++j) {
433: f = ifields[j];
434: PetscCall(PetscFree(fieldNames[f]));
435: PetscCall(ISDestroy(&fields[f]));
436: }
437: }
438: if (i == 0) {
439: for (f = 0; f < numFields; ++f) {
440: PetscCall(PCFieldSplitSetIS(pc, fieldNames[f], fields[f]));
441: PetscCall(PetscFree(fieldNames[f]));
442: PetscCall(ISDestroy(&fields[f]));
443: }
444: } else {
445: for (j = 0; j < numFields; j++) PetscCall(DMDestroy(dms + j));
446: PetscCall(PetscFree(dms));
447: PetscCall(PetscMalloc1(i, &dms));
448: for (j = 0; j < i; ++j) dms[j] = subdm[j];
449: }
450: PetscCall(PetscFree(fieldNames));
451: PetscCall(PetscFree(fields));
452: if (dms) {
453: PetscCall(PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n"));
454: for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
455: const char *prefix;
456: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ilink->ksp, &prefix));
457: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dms[i], prefix));
458: PetscCall(KSPSetDM(ilink->ksp, dms[i]));
459: PetscCall(KSPSetDMActive(ilink->ksp, KSP_DMACTIVE_ALL, PETSC_FALSE));
460: PetscCall(PetscObjectIncrementTabLevel((PetscObject)dms[i], (PetscObject)ilink->ksp, 0));
461: PetscCall(DMDestroy(&dms[i]));
462: }
463: PetscCall(PetscFree(dms));
464: }
465: } else {
466: if (jac->bs <= 0) {
467: if (pc->pmat) PetscCall(MatGetBlockSize(pc->pmat, &jac->bs));
468: else jac->bs = 1;
469: }
471: if (jac->detect) {
472: IS zerodiags, rest;
473: PetscInt nmin, nmax;
475: PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
476: if (jac->diag_use_amat) {
477: PetscCall(MatFindZeroDiagonals(pc->mat, &zerodiags));
478: } else {
479: PetscCall(MatFindZeroDiagonals(pc->pmat, &zerodiags));
480: }
481: PetscCall(ISComplement(zerodiags, nmin, nmax, &rest));
482: PetscCall(PCFieldSplitSetIS(pc, "0", rest));
483: PetscCall(PCFieldSplitSetIS(pc, "1", zerodiags));
484: PetscCall(ISDestroy(&zerodiags));
485: PetscCall(ISDestroy(&rest));
486: } else if (coupling) {
487: IS coupling, rest;
488: PetscInt nmin, nmax;
490: PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
491: if (jac->offdiag_use_amat) {
492: PetscCall(MatFindOffBlockDiagonalEntries(pc->mat, &coupling));
493: } else {
494: PetscCall(MatFindOffBlockDiagonalEntries(pc->pmat, &coupling));
495: }
496: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc->mat), nmax - nmin, nmin, 1, &rest));
497: PetscCall(ISSetIdentity(rest));
498: PetscCall(PCFieldSplitSetIS(pc, "0", rest));
499: PetscCall(PCFieldSplitSetIS(pc, "1", coupling));
500: PetscCall(ISDestroy(&coupling));
501: PetscCall(ISDestroy(&rest));
502: } else {
503: PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_default", &fieldsplit_default, NULL));
504: if (!fieldsplit_default) {
505: /* Allow user to set fields from command line, if bs was known at the time of PCSetFromOptions_FieldSplit()
506: then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
507: PetscCall(PCFieldSplitSetRuntimeSplits_Private(pc));
508: if (jac->splitdefined) PetscCall(PetscInfo(pc, "Splits defined using the options database\n"));
509: }
510: if ((fieldsplit_default || !jac->splitdefined) && !jac->isrestrict) {
511: Mat M = pc->pmat;
512: PetscBool isnest;
513: PetscInt nf;
515: PetscCall(PetscInfo(pc, "Using default splitting of fields\n"));
516: PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &isnest));
517: if (!isnest) {
518: M = pc->mat;
519: PetscCall(PetscObjectTypeCompare((PetscObject)pc->mat, MATNEST, &isnest));
520: }
521: if (!isnest) nf = jac->bs;
522: else PetscCall(MatNestGetSize(M, &nf, NULL));
523: for (i = 0; i < nf; i++) {
524: char splitname[8];
526: PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
527: PetscCall(PCFieldSplitSetFields(pc, splitname, 1, &i, &i));
528: }
529: jac->defaultsplit = PETSC_TRUE;
530: }
531: }
532: }
533: } else if (jac->nsplits == 1) {
534: IS is2;
535: PetscInt nmin, nmax;
537: PetscCheck(ilink->is, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Must provide at least two sets of fields to PCFieldSplit()");
538: PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
539: PetscCall(ISComplement(ilink->is, nmin, nmax, &is2));
540: PetscCall(PCFieldSplitSetIS(pc, "1", is2));
541: PetscCall(ISDestroy(&is2));
542: }
544: PetscCheck(jac->nsplits >= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "Unhandled case, must have at least two fields, not %" PetscInt_FMT, jac->nsplits);
545: PetscFunctionReturn(PETSC_SUCCESS);
546: }
548: static PetscErrorCode MatGolubKahanComputeExplicitOperator(Mat A, Mat B, Mat C, Mat *H, PetscReal gkbnu)
549: {
550: Mat BT, T;
551: PetscReal nrmT, nrmB;
553: PetscFunctionBegin;
554: PetscCall(MatHermitianTranspose(C, MAT_INITIAL_MATRIX, &T)); /* Test if augmented matrix is symmetric */
555: PetscCall(MatAXPY(T, -1.0, B, DIFFERENT_NONZERO_PATTERN));
556: PetscCall(MatNorm(T, NORM_1, &nrmT));
557: PetscCall(MatNorm(B, NORM_1, &nrmB));
558: PetscCheck(nrmB <= 0 || nrmT / nrmB < PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Matrix is not symmetric/Hermitian, GKB is not applicable.");
560: /* Compute augmented Lagrangian matrix H = A00 + nu*A01*A01'. This corresponds to */
561: /* setting N := 1/nu*I in [Ar13]. */
562: PetscCall(MatHermitianTranspose(B, MAT_INITIAL_MATRIX, &BT));
563: PetscCall(MatMatMult(B, BT, MAT_INITIAL_MATRIX, PETSC_CURRENT, H)); /* H = A01*A01' */
564: PetscCall(MatAYPX(*H, gkbnu, A, DIFFERENT_NONZERO_PATTERN)); /* H = A00 + nu*A01*A01' */
566: PetscCall(MatDestroy(&BT));
567: PetscCall(MatDestroy(&T));
568: PetscFunctionReturn(PETSC_SUCCESS);
569: }
571: PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
573: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
574: {
575: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
576: PC_FieldSplitLink ilink;
577: PetscInt i, nsplit;
578: PetscBool matnest = PETSC_FALSE;
580: PetscFunctionBegin;
581: pc->failedreason = PC_NOERROR;
582: PetscCall(PCFieldSplitSetDefaults(pc));
583: nsplit = jac->nsplits;
584: ilink = jac->head;
585: if (pc->pmat) PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));
587: /* get the matrices for each split */
588: if (!jac->issetup) {
589: PetscInt rstart, rend, nslots, bs;
591: jac->issetup = PETSC_TRUE;
593: /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
594: if (jac->defaultsplit || !ilink->is) {
595: if (jac->bs <= 0) jac->bs = nsplit;
596: }
598: /* MatCreateSubMatrix() for [S]BAIJ matrices can only work if the indices include entire blocks of the matrix */
599: PetscCall(MatGetBlockSize(pc->pmat, &bs));
600: if (bs > 1 && (jac->bs <= bs || jac->bs % bs)) {
601: PetscBool blk;
603: PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &blk, MATBAIJ, MATSBAIJ, MATSEQBAIJ, MATSEQSBAIJ, MATMPIBAIJ, MATMPISBAIJ, NULL));
604: PetscCheck(!blk, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Cannot use MATBAIJ with PCFIELDSPLIT and currently set matrix and PC blocksizes");
605: }
607: if (!matnest) { /* use the matrix blocksize and stride IS to determine the index sets that define the submatrices */
608: bs = jac->bs;
609: PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
610: nslots = (rend - rstart) / bs;
611: for (i = 0; i < nsplit; i++) {
612: if (jac->defaultsplit) {
613: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + i, nsplit, &ilink->is));
614: PetscCall(PetscObjectReference((PetscObject)ilink->is));
615: ilink->is_col = ilink->is;
616: } else if (!ilink->is) {
617: PetscBool same_fields = PETSC_TRUE;
619: for (PetscInt k = 0; k < ilink->nfields; k++) {
620: if (ilink->fields[k] != ilink->fields_col[k]) same_fields = PETSC_FALSE;
621: }
623: if (ilink->nfields > 1) {
624: PetscInt *ii, *jj, j, k, nfields = ilink->nfields, *fields = ilink->fields, *fields_col = ilink->fields_col;
626: PetscCall(PetscMalloc1(ilink->nfields * nslots, &ii));
627: if (!same_fields) PetscCall(PetscMalloc1(ilink->nfields * nslots, &jj));
628: for (j = 0; j < nslots; j++) {
629: for (k = 0; k < nfields; k++) {
630: ii[nfields * j + k] = rstart + bs * j + fields[k];
631: if (!same_fields) jj[nfields * j + k] = rstart + bs * j + fields_col[k];
632: }
633: }
634: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), nslots * nfields, ii, PETSC_OWN_POINTER, &ilink->is));
635: if (!same_fields) PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), nslots * nfields, jj, PETSC_OWN_POINTER, &ilink->is_col));
636: else {
637: PetscCall(PetscObjectReference((PetscObject)ilink->is));
638: ilink->is_col = ilink->is;
639: }
640: PetscCall(ISSetBlockSize(ilink->is, nfields));
641: PetscCall(ISSetBlockSize(ilink->is_col, nfields));
642: } else {
643: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + ilink->fields[0], bs, &ilink->is));
644: if (!same_fields) PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + ilink->fields_col[0], bs, &ilink->is_col));
645: else {
646: PetscCall(PetscObjectReference((PetscObject)ilink->is));
647: ilink->is_col = ilink->is;
648: }
649: }
650: }
651: ilink = ilink->next;
652: }
653: } else { /* use the IS that define the MATNEST to determine the index sets that define the submatrices */
654: IS *rowis, *colis, *ises = NULL;
655: PetscInt mis, nis;
657: PetscCall(MatNestGetSize(pc->pmat, &mis, &nis));
658: PetscCall(PetscMalloc2(mis, &rowis, nis, &colis));
659: PetscCall(MatNestGetISs(pc->pmat, rowis, colis));
660: if (!jac->defaultsplit) PetscCall(PetscMalloc1(mis, &ises));
662: for (i = 0; i < nsplit; i++) {
663: if (jac->defaultsplit) {
664: PetscCall(ISDuplicate(rowis[i], &ilink->is));
665: PetscCall(PetscObjectReference((PetscObject)ilink->is));
666: ilink->is_col = ilink->is;
667: } else if (!ilink->is) {
668: if (ilink->nfields > 1) {
669: for (PetscInt j = 0; j < ilink->nfields; j++) ises[j] = rowis[ilink->fields[j]];
670: PetscCall(ISConcatenate(PetscObjectComm((PetscObject)pc), ilink->nfields, ises, &ilink->is));
671: } else {
672: PetscCall(ISDuplicate(rowis[ilink->fields[0]], &ilink->is));
673: }
674: PetscCall(PetscObjectReference((PetscObject)ilink->is));
675: ilink->is_col = ilink->is;
676: }
677: ilink = ilink->next;
678: }
679: PetscCall(PetscFree2(rowis, colis));
680: PetscCall(PetscFree(ises));
681: }
682: }
684: ilink = jac->head;
685: if (!jac->pmat) {
686: Vec xtmp;
688: PetscCall(MatCreateVecs(pc->pmat, &xtmp, NULL));
689: PetscCall(PetscMalloc1(nsplit, &jac->pmat));
690: PetscCall(PetscMalloc2(nsplit, &jac->x, nsplit, &jac->y));
691: for (i = 0; i < nsplit; i++) {
692: MatNullSpace sp;
694: /* Check for matrix attached to IS */
695: PetscCall(PetscObjectQuery((PetscObject)ilink->is, "pmat", (PetscObject *)&jac->pmat[i]));
696: if (jac->pmat[i]) {
697: PetscCall(PetscObjectReference((PetscObject)jac->pmat[i]));
698: if (jac->type == PC_COMPOSITE_SCHUR) {
699: jac->schur_user = jac->pmat[i];
701: PetscCall(PetscObjectReference((PetscObject)jac->schur_user));
702: }
703: } else {
704: const char *prefix;
705: PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, ilink->is_col, MAT_INITIAL_MATRIX, &jac->pmat[i]));
706: PetscCall(MatGetOptionsPrefix(jac->pmat[i], &prefix));
707: if (!prefix) {
708: PetscCall(KSPGetOptionsPrefix(ilink->ksp, &prefix));
709: PetscCall(MatSetOptionsPrefix(jac->pmat[i], prefix));
710: }
711: PetscCall(MatSetFromOptions(jac->pmat[i]));
712: PetscCall(MatViewFromOptions(jac->pmat[i], NULL, "-mat_view"));
713: }
714: /* create work vectors for each split */
715: PetscCall(MatCreateVecs(jac->pmat[i], &jac->x[i], &jac->y[i]));
716: ilink->x = jac->x[i];
717: ilink->y = jac->y[i];
718: ilink->z = NULL;
719: /* compute scatter contexts needed by multiplicative versions and non-default splits */
720: PetscCall(VecScatterCreate(xtmp, ilink->is, jac->x[i], NULL, &ilink->sctx));
721: PetscCall(PetscObjectQuery((PetscObject)ilink->is, "nearnullspace", (PetscObject *)&sp));
722: if (sp) PetscCall(MatSetNearNullSpace(jac->pmat[i], sp));
723: ilink = ilink->next;
724: }
725: PetscCall(VecDestroy(&xtmp));
726: } else {
727: MatReuse scall;
728: MatNullSpace *nullsp = NULL;
730: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
731: PetscCall(MatGetNullSpaces(nsplit, jac->pmat, &nullsp));
732: for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->pmat[i]));
733: scall = MAT_INITIAL_MATRIX;
734: } else scall = MAT_REUSE_MATRIX;
736: for (i = 0; i < nsplit; i++) {
737: Mat pmat;
739: /* Check for matrix attached to IS */
740: PetscCall(PetscObjectQuery((PetscObject)ilink->is, "pmat", (PetscObject *)&pmat));
741: if (!pmat) PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, ilink->is_col, scall, &jac->pmat[i]));
742: ilink = ilink->next;
743: }
744: if (nullsp) PetscCall(MatRestoreNullSpaces(nsplit, jac->pmat, &nullsp));
745: }
746: if (jac->diag_use_amat) {
747: ilink = jac->head;
748: if (!jac->mat) {
749: PetscCall(PetscMalloc1(nsplit, &jac->mat));
750: for (i = 0; i < nsplit; i++) {
751: PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, ilink->is_col, MAT_INITIAL_MATRIX, &jac->mat[i]));
752: ilink = ilink->next;
753: }
754: } else {
755: MatReuse scall;
756: MatNullSpace *nullsp = NULL;
758: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
759: PetscCall(MatGetNullSpaces(nsplit, jac->mat, &nullsp));
760: for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->mat[i]));
761: scall = MAT_INITIAL_MATRIX;
762: } else scall = MAT_REUSE_MATRIX;
764: for (i = 0; i < nsplit; i++) {
765: PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, ilink->is_col, scall, &jac->mat[i]));
766: ilink = ilink->next;
767: }
768: if (nullsp) PetscCall(MatRestoreNullSpaces(nsplit, jac->mat, &nullsp));
769: }
770: } else {
771: jac->mat = jac->pmat;
772: }
774: /* Check for null space attached to IS */
775: ilink = jac->head;
776: for (i = 0; i < nsplit; i++) {
777: MatNullSpace sp;
779: PetscCall(PetscObjectQuery((PetscObject)ilink->is, "nullspace", (PetscObject *)&sp));
780: if (sp) PetscCall(MatSetNullSpace(jac->mat[i], sp));
781: ilink = ilink->next;
782: }
784: if (jac->type != PC_COMPOSITE_ADDITIVE && jac->type != PC_COMPOSITE_SCHUR && jac->type != PC_COMPOSITE_GKB) {
785: /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
786: /* FIXME: Can/should we reuse jac->mat whenever (jac->diag_use_amat) is true? */
787: ilink = jac->head;
788: if (nsplit == 2 && jac->type == PC_COMPOSITE_MULTIPLICATIVE) {
789: /* special case need where Afield[0] is not needed and only certain columns of Afield[1] are needed since update is only on those rows of the solution */
790: if (!jac->Afield) {
791: PetscCall(PetscCalloc1(nsplit, &jac->Afield));
792: if (jac->offdiag_use_amat) {
793: PetscCall(MatCreateSubMatrix(pc->mat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->Afield[1]));
794: } else {
795: PetscCall(MatCreateSubMatrix(pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->Afield[1]));
796: }
797: } else {
798: MatReuse scall;
800: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
801: PetscCall(MatDestroy(&jac->Afield[1]));
802: scall = MAT_INITIAL_MATRIX;
803: } else scall = MAT_REUSE_MATRIX;
805: if (jac->offdiag_use_amat) {
806: PetscCall(MatCreateSubMatrix(pc->mat, ilink->next->is, ilink->is, scall, &jac->Afield[1]));
807: } else {
808: PetscCall(MatCreateSubMatrix(pc->pmat, ilink->next->is, ilink->is, scall, &jac->Afield[1]));
809: }
810: }
811: } else {
812: if (!jac->Afield) {
813: PetscCall(PetscMalloc1(nsplit, &jac->Afield));
814: for (i = 0; i < nsplit; i++) {
815: if (jac->offdiag_use_amat) {
816: PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, NULL, MAT_INITIAL_MATRIX, &jac->Afield[i]));
817: } else {
818: PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, NULL, MAT_INITIAL_MATRIX, &jac->Afield[i]));
819: }
820: ilink = ilink->next;
821: }
822: } else {
823: MatReuse scall;
824: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
825: for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->Afield[i]));
826: scall = MAT_INITIAL_MATRIX;
827: } else scall = MAT_REUSE_MATRIX;
829: for (i = 0; i < nsplit; i++) {
830: if (jac->offdiag_use_amat) {
831: PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, NULL, scall, &jac->Afield[i]));
832: } else {
833: PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, NULL, scall, &jac->Afield[i]));
834: }
835: ilink = ilink->next;
836: }
837: }
838: }
839: }
841: if (jac->type == PC_COMPOSITE_SCHUR) {
842: PetscBool isset, isspd = PETSC_FALSE, issym = PETSC_FALSE, flg;
843: char lscname[256];
844: PetscObject LSC_L;
846: PetscCheck(nsplit == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "To use Schur complement preconditioner you must have exactly 2 fields");
848: /* If pc->mat is SPD, don't scale by -1 the Schur complement */
849: PetscCall(MatIsSPDKnown(pc->pmat, &isset, &isspd));
850: if (jac->schurscale == (PetscScalar)-1.0) jac->schurscale = (isset && isspd) ? 1.0 : -1.0;
851: PetscCall(MatIsSymmetricKnown(pc->pmat, &isset, &issym));
853: PetscCall(PetscObjectTypeCompareAny(jac->offdiag_use_amat ? (PetscObject)pc->mat : (PetscObject)pc->pmat, &flg, MATSEQSBAIJ, MATMPISBAIJ, ""));
855: if (jac->schur) {
856: KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
857: MatReuse scall;
859: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
860: scall = MAT_INITIAL_MATRIX;
861: PetscCall(MatDestroy(&jac->B));
862: PetscCall(MatDestroy(&jac->C));
863: } else scall = MAT_REUSE_MATRIX;
865: PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
866: ilink = jac->head;
867: PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, scall, &jac->B));
868: if (!flg) PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, scall, &jac->C));
869: else {
870: PetscCall(MatIsHermitianKnown(jac->offdiag_use_amat ? pc->mat : pc->pmat, &isset, &flg));
871: if (isset && flg) PetscCall(MatCreateHermitianTranspose(jac->B, &jac->C));
872: else PetscCall(MatCreateTranspose(jac->B, &jac->C));
873: }
874: ilink = ilink->next;
875: PetscCall(MatSchurComplementUpdateSubMatrices(jac->schur, jac->mat[0], jac->pmat[0], jac->B, jac->C, jac->mat[1]));
876: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
877: PetscCall(MatDestroy(&jac->schurp));
878: PetscCall(MatSchurComplementGetPmat(jac->schur, MAT_INITIAL_MATRIX, &jac->schurp));
879: } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL && jac->kspupper != jac->head->ksp) {
880: PetscCall(MatDestroy(&jac->schur_user));
881: PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
882: }
883: if (kspA != kspInner) PetscCall(KSPSetOperators(kspA, jac->mat[0], jac->pmat[0]));
884: if (kspUpper != kspA) PetscCall(KSPSetOperators(kspUpper, jac->mat[0], jac->pmat[0]));
885: PetscCall(KSPSetOperators(jac->kspschur, jac->schur, FieldSplitSchurPre(jac)));
886: } else {
887: const char *Dprefix;
888: char schurprefix[256], schurmatprefix[256];
889: char schurtestoption[256];
890: MatNullSpace sp;
891: KSP kspt;
893: /* extract the A01 and A10 matrices */
894: ilink = jac->head;
895: PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, MAT_INITIAL_MATRIX, &jac->B));
896: if (!flg) PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->C));
897: else {
898: PetscCall(MatIsHermitianKnown(jac->offdiag_use_amat ? pc->mat : pc->pmat, &isset, &flg));
899: if (isset && flg) PetscCall(MatCreateHermitianTranspose(jac->B, &jac->C));
900: else PetscCall(MatCreateTranspose(jac->B, &jac->C));
901: }
902: ilink = ilink->next;
903: /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
904: PetscCall(MatCreate(((PetscObject)jac->mat[0])->comm, &jac->schur));
905: PetscCall(MatSetType(jac->schur, MATSCHURCOMPLEMENT));
906: PetscCall(MatSchurComplementSetSubMatrices(jac->schur, jac->mat[0], jac->pmat[0], jac->B, jac->C, jac->mat[1]));
907: PetscCall(PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
908: PetscCall(MatSetOptionsPrefix(jac->schur, schurmatprefix));
909: PetscCall(MatSchurComplementGetKSP(jac->schur, &kspt));
910: PetscCall(KSPSetOptionsPrefix(kspt, schurmatprefix));
912: /* Note: this is not true in general */
913: PetscCall(MatGetNullSpace(jac->mat[1], &sp));
914: if (sp) PetscCall(MatSetNullSpace(jac->schur, sp));
916: PetscCall(PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname));
917: PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, schurtestoption, NULL, NULL, &flg));
918: if (flg) {
919: DM dmInner;
920: KSP kspInner;
921: PC pcInner;
923: PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
924: PetscCall(KSPReset(kspInner));
925: PetscCall(KSPSetOperators(kspInner, jac->mat[0], jac->pmat[0]));
926: PetscCall(PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
927: /* Indent this deeper to emphasize the "inner" nature of this solver. */
928: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject)pc, 2));
929: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspInner->pc, (PetscObject)pc, 2));
930: PetscCall(KSPSetOptionsPrefix(kspInner, schurprefix));
932: /* Set DM for new solver */
933: PetscCall(KSPGetDM(jac->head->ksp, &dmInner));
934: PetscCall(KSPSetDM(kspInner, dmInner));
935: PetscCall(KSPSetDMActive(kspInner, KSP_DMACTIVE_ALL, PETSC_FALSE));
937: /* Defaults to PCKSP as preconditioner */
938: PetscCall(KSPGetPC(kspInner, &pcInner));
939: PetscCall(PCSetType(pcInner, PCKSP));
940: PetscCall(PCKSPSetKSP(pcInner, jac->head->ksp));
941: } else {
942: /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
943: * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
944: * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
945: * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
946: * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
947: * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
948: PetscCall(KSPSetType(jac->head->ksp, KSPGMRES));
949: PetscCall(MatSchurComplementSetKSP(jac->schur, jac->head->ksp));
950: }
951: PetscCall(KSPSetOperators(jac->head->ksp, jac->mat[0], jac->pmat[0]));
952: PetscCall(KSPSetFromOptions(jac->head->ksp));
953: PetscCall(MatSetFromOptions(jac->schur));
955: PetscCall(PetscObjectTypeCompare((PetscObject)jac->schur, MATSCHURCOMPLEMENT, &flg));
956: if (flg) { /* Need to do this otherwise PCSetUp_KSP will overwrite the amat of jac->head->ksp */
957: KSP kspInner;
958: PC pcInner;
960: PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
961: PetscCall(KSPGetPC(kspInner, &pcInner));
962: PetscCall(PetscObjectTypeCompare((PetscObject)pcInner, PCKSP, &flg));
963: if (flg) {
964: KSP ksp;
966: PetscCall(PCKSPGetKSP(pcInner, &ksp));
967: if (ksp == jac->head->ksp) PetscCall(PCSetUseAmat(pcInner, PETSC_TRUE));
968: }
969: }
970: PetscCall(PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname));
971: PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, schurtestoption, NULL, NULL, &flg));
972: if (flg) {
973: DM dmInner;
975: PetscCall(PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
976: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper));
977: PetscCall(KSPSetNestLevel(jac->kspupper, pc->kspnestlevel));
978: PetscCall(KSPSetErrorIfNotConverged(jac->kspupper, pc->erroriffailure));
979: PetscCall(KSPSetOptionsPrefix(jac->kspupper, schurprefix));
980: PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspupper, (PetscObject)pc, 1));
981: PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspupper->pc, (PetscObject)pc, 1));
982: PetscCall(KSPGetDM(jac->head->ksp, &dmInner));
983: PetscCall(KSPSetDM(jac->kspupper, dmInner));
984: PetscCall(KSPSetDMActive(jac->kspupper, KSP_DMACTIVE_ALL, PETSC_FALSE));
985: PetscCall(KSPSetFromOptions(jac->kspupper));
986: PetscCall(KSPSetOperators(jac->kspupper, jac->mat[0], jac->pmat[0]));
987: PetscCall(VecDuplicate(jac->head->x, &jac->head->z));
988: } else {
989: jac->kspupper = jac->head->ksp;
990: PetscCall(PetscObjectReference((PetscObject)jac->head->ksp));
991: }
993: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) PetscCall(MatSchurComplementGetPmat(jac->schur, MAT_INITIAL_MATRIX, &jac->schurp));
994: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspschur));
995: PetscCall(KSPSetNestLevel(jac->kspschur, pc->kspnestlevel));
996: PetscCall(KSPSetErrorIfNotConverged(jac->kspschur, pc->erroriffailure));
997: PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspschur, (PetscObject)pc, 1));
998: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
999: PC pcschur;
1000: PetscCall(KSPGetPC(jac->kspschur, &pcschur));
1001: PetscCall(PCSetType(pcschur, PCNONE));
1002: /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
1003: } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
1004: if (jac->schurfactorization != PC_FIELDSPLIT_SCHUR_FACT_FULL || jac->kspupper != jac->head->ksp) PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1005: }
1006: PetscCall(KSPSetOperators(jac->kspschur, jac->schur, FieldSplitSchurPre(jac)));
1007: PetscCall(KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix));
1008: PetscCall(KSPSetOptionsPrefix(jac->kspschur, Dprefix));
1009: /* propagate DM */
1010: {
1011: DM sdm;
1012: PetscCall(KSPGetDM(jac->head->next->ksp, &sdm));
1013: if (sdm) {
1014: PetscCall(KSPSetDM(jac->kspschur, sdm));
1015: PetscCall(KSPSetDMActive(jac->kspschur, KSP_DMACTIVE_ALL, PETSC_FALSE));
1016: }
1017: }
1018: /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1019: /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
1020: PetscCall(KSPSetFromOptions(jac->kspschur));
1021: }
1022: PetscCall(MatAssemblyBegin(jac->schur, MAT_FINAL_ASSEMBLY));
1023: PetscCall(MatAssemblyEnd(jac->schur, MAT_FINAL_ASSEMBLY));
1024: if (issym) PetscCall(MatSetOption(jac->schur, MAT_SYMMETRIC, PETSC_TRUE));
1025: if (isspd) PetscCall(MatSetOption(jac->schur, MAT_SPD, PETSC_TRUE));
1027: /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
1028: PetscCall(PetscSNPrintf(lscname, sizeof(lscname), "%s_LSC_L", ilink->splitname));
1029: PetscCall(PetscObjectQuery((PetscObject)pc->mat, lscname, &LSC_L));
1030: if (!LSC_L) PetscCall(PetscObjectQuery((PetscObject)pc->pmat, lscname, &LSC_L));
1031: if (LSC_L) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "LSC_L", LSC_L));
1032: PetscCall(PetscSNPrintf(lscname, sizeof(lscname), "%s_LSC_Lp", ilink->splitname));
1033: PetscCall(PetscObjectQuery((PetscObject)pc->pmat, lscname, &LSC_L));
1034: if (!LSC_L) PetscCall(PetscObjectQuery((PetscObject)pc->mat, lscname, &LSC_L));
1035: if (LSC_L) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "LSC_Lp", LSC_L));
1036: } else if (jac->type == PC_COMPOSITE_GKB) {
1037: PetscCheck(nsplit == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "To use GKB preconditioner you must have exactly 2 fields");
1038: ilink = jac->head;
1039: PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, MAT_INITIAL_MATRIX, &jac->B));
1040: /* Create work vectors for GKB algorithm */
1041: PetscCall(VecDuplicate(ilink->x, &jac->u));
1042: PetscCall(VecDuplicate(ilink->x, &jac->Hu));
1043: PetscCall(VecDuplicate(ilink->x, &jac->w2));
1044: PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->C));
1045: ilink = ilink->next;
1046: /* Create work vectors for GKB algorithm */
1047: PetscCall(VecDuplicate(ilink->x, &jac->v));
1048: PetscCall(VecDuplicate(ilink->x, &jac->d));
1049: PetscCall(VecDuplicate(ilink->x, &jac->w1));
1050: PetscCall(MatGolubKahanComputeExplicitOperator(jac->mat[0], jac->B, jac->C, &jac->H, jac->gkbnu));
1051: PetscCall(PetscCalloc1(jac->gkbdelay, &jac->vecz));
1053: ilink = jac->head;
1054: PetscCall(KSPSetOperators(ilink->ksp, jac->H, jac->H));
1055: if (!jac->suboptionsset) PetscCall(KSPSetFromOptions(ilink->ksp));
1056: /* Create gkb_monitor context */
1057: if (jac->gkbmonitor) {
1058: PetscInt tablevel;
1059: PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &jac->gkbviewer));
1060: PetscCall(PetscViewerSetType(jac->gkbviewer, PETSCVIEWERASCII));
1061: PetscCall(PetscObjectGetTabLevel((PetscObject)ilink->ksp, &tablevel));
1062: PetscCall(PetscViewerASCIISetTab(jac->gkbviewer, tablevel));
1063: PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)ilink->ksp, 1));
1064: }
1065: } else {
1066: /* set up the individual splits' PCs */
1067: i = 0;
1068: ilink = jac->head;
1069: while (ilink) {
1070: PetscCall(KSPSetOperators(ilink->ksp, jac->mat[i], jac->pmat[i]));
1071: /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1072: if (!jac->suboptionsset) PetscCall(KSPSetFromOptions(ilink->ksp));
1073: i++;
1074: ilink = ilink->next;
1075: }
1076: }
1078: /* Set coordinates to the sub PC objects whenever these are set */
1079: if (jac->coordinates_set) {
1080: PC pc_coords;
1081: if (jac->type == PC_COMPOSITE_SCHUR) {
1082: // Head is first block.
1083: PetscCall(KSPGetPC(jac->head->ksp, &pc_coords));
1084: PetscCall(PCSetCoordinates(pc_coords, jac->head->dim, jac->head->ndofs, jac->head->coords));
1085: // Second one is Schur block, but its KSP object is in kspschur.
1086: PetscCall(KSPGetPC(jac->kspschur, &pc_coords));
1087: PetscCall(PCSetCoordinates(pc_coords, jac->head->next->dim, jac->head->next->ndofs, jac->head->next->coords));
1088: } else if (jac->type == PC_COMPOSITE_GKB) {
1089: PetscCall(PetscInfo(pc, "Warning: Setting coordinates does nothing for the GKB Fieldpslit preconditioner\n"));
1090: } else {
1091: ilink = jac->head;
1092: while (ilink) {
1093: PetscCall(KSPGetPC(ilink->ksp, &pc_coords));
1094: PetscCall(PCSetCoordinates(pc_coords, ilink->dim, ilink->ndofs, ilink->coords));
1095: ilink = ilink->next;
1096: }
1097: }
1098: }
1100: jac->suboptionsset = PETSC_TRUE;
1101: PetscFunctionReturn(PETSC_SUCCESS);
1102: }
1104: static PetscErrorCode PCSetUpOnBlocks_FieldSplit_Schur(PC pc)
1105: {
1106: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1107: PC_FieldSplitLink ilinkA = jac->head;
1108: KSP kspA = ilinkA->ksp, kspUpper = jac->kspupper;
1110: PetscFunctionBegin;
1111: if (jac->schurfactorization == PC_FIELDSPLIT_SCHUR_FACT_FULL && kspUpper != kspA) {
1112: PetscCall(KSPSetUp(kspUpper));
1113: PetscCall(KSPSetUpOnBlocks(kspUpper));
1114: }
1115: PetscCall(KSPSetUp(kspA));
1116: PetscCall(KSPSetUpOnBlocks(kspA));
1117: if (jac->schurpre != PC_FIELDSPLIT_SCHUR_PRE_FULL) {
1118: PetscCall(KSPSetUp(jac->kspschur));
1119: PetscCall(KSPSetUpOnBlocks(jac->kspschur));
1120: } else if (kspUpper == kspA && jac->schurfactorization == PC_FIELDSPLIT_SCHUR_FACT_FULL) {
1121: Mat A;
1122: PetscInt m, M, N;
1123: VecType vtype;
1124: PetscMemType mtype;
1125: PetscScalar *array;
1127: PetscCall(MatGetSize(jac->B, &M, &N));
1128: PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1129: PetscCall(MatGetVecType(jac->B, &vtype));
1130: PetscCall(VecGetArrayAndMemType(ilinkA->x, &array, &mtype));
1131: PetscCall(VecRestoreArrayAndMemType(ilinkA->x, &array));
1132: PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&A));
1133: if (A) {
1134: PetscInt P;
1136: PetscCall(MatGetSize(A, NULL, &P));
1137: if (P < N + 1) { // need to recreate AinvB, otherwise, the Schur complement won't be updated
1138: PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", NULL));
1139: A = NULL;
1140: }
1141: }
1142: if (!A) {
1143: if (PetscMemTypeHost(mtype) || (!PetscDefined(HAVE_CUDA) && !PetscDefined(HAVE_HIP))) PetscCall(PetscMalloc1(m * (N + 1), &array));
1144: #if PetscDefined(HAVE_CUDA)
1145: else if (PetscMemTypeCUDA(mtype)) PetscCallCUDA(cudaMalloc((void **)&array, sizeof(PetscScalar) * m * (N + 1)));
1146: #endif
1147: #if PetscDefined(HAVE_HIP)
1148: else if (PetscMemTypeHIP(mtype)) PetscCallHIP(hipMalloc((void **)&array, sizeof(PetscScalar) * m * (N + 1)));
1149: #endif
1150: PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)jac->schur), vtype, m, PETSC_DECIDE, M, N + 1, PETSC_DECIDE, array, &A)); // number of columns of the Schur complement plus one
1151: PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", (PetscObject)A));
1152: PetscCall(MatDestroy(&A));
1153: }
1154: }
1155: PetscFunctionReturn(PETSC_SUCCESS);
1156: }
1158: static PetscErrorCode PCSetUpOnBlocks_FieldSplit(PC pc)
1159: {
1160: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1161: PC_FieldSplitLink ilink = jac->head;
1163: PetscFunctionBegin;
1164: while (ilink) {
1165: PetscCall(KSPSetUp(ilink->ksp));
1166: PetscCall(KSPSetUpOnBlocks(ilink->ksp));
1167: ilink = ilink->next;
1168: }
1169: PetscFunctionReturn(PETSC_SUCCESS);
1170: }
1172: static PetscErrorCode PCSetUpOnBlocks_FieldSplit_GKB(PC pc)
1173: {
1174: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1175: PC_FieldSplitLink ilinkA = jac->head;
1176: KSP ksp = ilinkA->ksp;
1178: PetscFunctionBegin;
1179: PetscCall(KSPSetUp(ksp));
1180: PetscCall(KSPSetUpOnBlocks(ksp));
1181: PetscFunctionReturn(PETSC_SUCCESS);
1182: }
1184: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc, Vec x, Vec y)
1185: {
1186: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1187: PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1188: KSP kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
1189: Mat AinvB = NULL;
1190: PetscInt N, P;
1192: PetscFunctionBegin;
1193: switch (jac->schurfactorization) {
1194: case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1195: /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1196: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1197: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1198: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1199: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1200: PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1201: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1202: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1203: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1204: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1205: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1206: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1207: PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1208: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1209: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1210: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1211: PetscCall(VecScale(ilinkD->y, jac->schurscale));
1212: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1213: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1214: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1215: break;
1216: case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1217: /* [A00 0; A10 S], suitable for left preconditioning */
1218: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1219: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1220: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1221: PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1222: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1223: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1224: PetscCall(MatMult(jac->C, ilinkA->y, ilinkD->x));
1225: PetscCall(VecScale(ilinkD->x, -1.));
1226: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1227: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1228: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1229: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1230: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1231: PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1232: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1233: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1234: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1235: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1236: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1237: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1238: break;
1239: case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1240: /* [A00 A01; 0 S], suitable for right preconditioning */
1241: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1242: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1243: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1244: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1245: PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1246: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1247: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1248: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1249: PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->x));
1250: PetscCall(VecScale(ilinkA->x, -1.));
1251: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1252: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1253: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1254: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1255: PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1256: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1257: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1258: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1259: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1260: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1261: break;
1262: case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1263: /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1264: PetscCall(MatGetSize(jac->B, NULL, &P));
1265: N = P;
1266: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1267: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1268: PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->y, NULL));
1269: if (kspUpper == kspA) {
1270: PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&AinvB));
1271: if (AinvB) {
1272: PetscCall(MatGetSize(AinvB, NULL, &N));
1273: if (N > P) { // first time PCApply_FieldSplit_Schur() is called
1274: PetscMemType mtype;
1275: Vec c = NULL;
1276: PetscScalar *array;
1277: PetscInt m, M;
1279: PetscCall(MatGetSize(jac->B, &M, NULL));
1280: PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1281: PetscCall(MatDenseGetArrayAndMemType(AinvB, &array, &mtype));
1282: PetscCall(VecCreateMPIWithArrayAndMemType(PetscObjectComm((PetscObject)jac->schur), mtype, 1, m, M, array + m * P, &c));
1283: PetscCall(MatDenseRestoreArrayAndMemType(AinvB, &array));
1284: PetscCall(VecCopy(ilinkA->x, c));
1285: PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1286: PetscCall(KSPSetOperators(jac->kspschur, jac->schur, jac->schur_user));
1287: PetscCall(VecCopy(c, ilinkA->y)); // retrieve the solution as the last column of the composed Mat
1288: PetscCall(VecDestroy(&c));
1289: }
1290: }
1291: }
1292: if (N == P) PetscCall(KSPSolve(kspLower, ilinkA->x, ilinkA->y));
1293: PetscCall(KSPCheckSolve(kspLower, pc, ilinkA->y));
1294: PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->y, NULL));
1295: PetscCall(MatMult(jac->C, ilinkA->y, ilinkD->x));
1296: PetscCall(VecScale(ilinkD->x, -1.0));
1297: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1298: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1300: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1301: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1302: PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1303: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1304: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1305: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1306: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1308: if (kspUpper == kspA) {
1309: if (!AinvB) {
1310: PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->y));
1311: PetscCall(VecAXPY(ilinkA->x, -1.0, ilinkA->y));
1312: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1313: PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1314: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1315: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1316: } else PetscCall(MatMultAdd(AinvB, ilinkD->y, ilinkA->y, ilinkA->y));
1317: } else {
1318: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1319: PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1320: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1321: PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->x));
1322: PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->z, NULL));
1323: PetscCall(KSPSolve(kspUpper, ilinkA->x, ilinkA->z));
1324: PetscCall(KSPCheckSolve(kspUpper, pc, ilinkA->z));
1325: PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->z, NULL));
1326: PetscCall(VecAXPY(ilinkA->y, -1.0, ilinkA->z));
1327: }
1328: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1329: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1330: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1331: }
1332: PetscFunctionReturn(PETSC_SUCCESS);
1333: }
1335: /*
1336: PCFieldSplitCreateWorkMats_Private - Allocate per-field dense work matrices for multi-RHS
1338: Input Parameters:
1339: + pc - the PC context
1340: - X - matrix to copy column-layout from
1342: Notes:
1343: If matrices already exist with correct column count, they are reused.
1344: If column count changed, old matrices are destroyed and new ones created.
1345: */
1346: static PetscErrorCode PCFieldSplitCreateWorkMats_Private(PC pc, Mat X)
1347: {
1348: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1349: PC_FieldSplitLink ilink = jac->head;
1350: PetscInt mx, Mx, my, My, N;
1352: PetscFunctionBegin;
1353: while (ilink) {
1354: /* check if reallocation needed (previous allocation with wrong column count) */
1355: if (ilink->X) {
1356: PetscCall(MatGetSize(ilink->X, NULL, &N));
1357: if (N != X->cmap->N) {
1358: PetscCall(MatDestroy(&ilink->X));
1359: PetscCall(MatDestroy(&ilink->Y));
1360: PetscCall(MatDestroy(&ilink->Z));
1361: }
1362: }
1363: /* create if needed */
1364: if (!ilink->X) {
1365: VecType xtype, ytype;
1367: PetscCall(VecGetType(ilink->x, &xtype));
1368: PetscCall(VecGetType(ilink->y, &ytype));
1369: PetscCall(VecGetLocalSize(ilink->x, &mx));
1370: PetscCall(VecGetSize(ilink->x, &Mx));
1371: PetscCall(VecGetLocalSize(ilink->y, &my));
1372: PetscCall(VecGetSize(ilink->y, &My));
1373: /* use default lda */
1374: PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)pc), xtype, mx, X->cmap->n, Mx, X->cmap->N, PETSC_DECIDE, NULL, &ilink->X));
1375: PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)pc), ytype, my, X->cmap->n, My, X->cmap->N, PETSC_DECIDE, NULL, &ilink->Y));
1376: }
1377: ilink = ilink->next;
1378: }
1379: PetscFunctionReturn(PETSC_SUCCESS);
1380: }
1382: static PetscErrorCode PCMatApply_FieldSplit_Schur(PC pc, Mat X, Mat Y)
1383: {
1384: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1385: PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1386: KSP kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
1387: Mat AinvB = NULL;
1388: PetscInt N, P;
1390: PetscFunctionBegin;
1391: /* create working matrices with the correct number of columns */
1392: PetscCall(PCFieldSplitCreateWorkMats_Private(pc, X));
1393: switch (jac->schurfactorization) {
1394: case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1395: /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1396: PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1397: PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, INSERT_VALUES, SCATTER_FORWARD));
1398: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1399: PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1400: PetscCall(KSPCheckMatSolve(kspA, pc, ilinkA->Y));
1401: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1402: PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1403: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1404: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1405: PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1406: PetscCall(KSPCheckMatSolve(jac->kspschur, pc, ilinkD->Y));
1407: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1408: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1409: PetscCall(MatScale(ilinkD->Y, jac->schurscale));
1410: PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1411: break;
1412: case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1413: /* [A00 0; A10 S], suitable for left preconditioning */
1414: PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1415: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1416: PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1417: PetscCall(KSPCheckMatSolve(kspA, pc, ilinkA->Y));
1418: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1419: PetscCall(MatMatMult(jac->C, ilinkA->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkD->X));
1420: PetscCall(MatScale(ilinkD->X, -1.0));
1421: PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, ADD_VALUES, SCATTER_FORWARD));
1422: PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1423: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1424: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1425: PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1426: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1427: PetscCall(KSPCheckMatSolve(jac->kspschur, pc, ilinkD->Y));
1428: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1429: PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1430: break;
1431: case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1432: /* [A00 A01; 0 S], suitable for right preconditioning */
1433: PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, INSERT_VALUES, SCATTER_FORWARD));
1434: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1435: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1436: PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1437: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1438: PetscCall(KSPCheckMatSolve(jac->kspschur, pc, ilinkD->Y));
1439: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1440: PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1441: PetscCall(MatScale(ilinkA->X, -1.0));
1442: PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, ADD_VALUES, SCATTER_FORWARD));
1443: PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1444: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1445: PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1446: PetscCall(KSPCheckMatSolve(kspA, pc, ilinkA->Y));
1447: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1448: PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1449: break;
1450: case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1451: /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1452: PetscCall(MatGetSize(jac->B, NULL, &P));
1453: N = P;
1454: PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1455: PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->X, ilinkA->Y, NULL));
1456: if (kspUpper == kspA) {
1457: PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&AinvB));
1458: if (AinvB) {
1459: PetscCall(MatGetSize(AinvB, NULL, &N));
1460: if (N > P) { // first time PCApply_FieldSplit_Schur() is called
1461: PetscMemType mtype;
1462: Mat C = NULL;
1463: PetscScalar *array;
1464: PetscInt m, M, q, Q, p;
1466: PetscCall(MatGetSize(jac->B, &M, NULL));
1467: PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1468: PetscCall(MatGetSize(X, NULL, &Q));
1469: PetscCall(MatGetLocalSize(X, NULL, &q));
1470: PetscCall(MatDenseGetArrayAndMemType(AinvB, &array, &mtype));
1471: if (N != P + Q) {
1472: Mat replace;
1474: PetscCall(MatGetLocalSize(jac->B, NULL, &p));
1475: if (PetscMemTypeCUDA(mtype)) {
1476: #if PetscDefined(HAVE_CUDA)
1477: PetscCallCUDA(cudaFree(array));
1478: PetscCallCUDA(cudaMalloc((void **)&array, sizeof(PetscScalar) * m * (P + Q)));
1479: #endif
1480: } else if (PetscMemTypeHIP(mtype)) {
1481: #if PetscDefined(HAVE_HIP)
1482: PetscCallHIP(hipFree(array));
1483: PetscCallHIP(hipMalloc((void **)&array, sizeof(PetscScalar) * m * (P + Q)));
1484: #endif
1485: } else {
1486: PetscCheck(PetscMemTypeHost(mtype), PetscObjectComm((PetscObject)jac->schur), PETSC_ERR_SUP, "PetscMemType should be either PETSC_MEMTYPE_HOST, PETSC_MEMTYPE_CUDA, or PETSC_MEMTYPE_HIP");
1487: PetscCall(PetscFree(array));
1488: PetscCall(PetscMalloc1(m * (P + Q), &array));
1489: }
1490: PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)jac->schur), mtype, m, PETSC_DECIDE, M, P + Q, PETSC_DECIDE, array, &replace));
1491: PetscCall(MatHeaderReplace(AinvB, &replace));
1492: }
1493: PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)jac->schur), mtype, m, q, M, Q, PETSC_DECIDE, array + m * P, &C));
1494: PetscCall(MatDenseRestoreArrayAndMemType(AinvB, &array));
1495: PetscCall(MatCopy(ilinkA->X, C, SAME_NONZERO_PATTERN));
1496: PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1497: PetscCall(KSPSetOperators(jac->kspschur, jac->schur, jac->schur_user));
1498: PetscCall(MatCopy(C, ilinkA->Y, SAME_NONZERO_PATTERN)); // retrieve solutions as last columns of the composed Mat
1499: PetscCall(MatDestroy(&C));
1500: }
1501: }
1502: }
1503: if (N == P) PetscCall(KSPMatSolve(kspLower, ilinkA->X, ilinkA->Y));
1504: PetscCall(KSPCheckMatSolve(kspLower, pc, ilinkA->Y));
1505: PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->X, ilinkA->Y, NULL));
1506: PetscCall(MatMatMult(jac->C, ilinkA->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkD->X));
1507: PetscCall(MatScale(ilinkD->X, -1.0));
1508: PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, ADD_VALUES, SCATTER_FORWARD));
1510: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1511: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1512: PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1513: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1514: PetscCall(KSPCheckMatSolve(jac->kspschur, pc, ilinkD->Y));
1515: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1516: PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1518: if (kspUpper == kspA) {
1519: if (!AinvB) {
1520: PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->Y));
1521: PetscCall(MatAXPY(ilinkA->X, -1.0, ilinkA->Y, SAME_NONZERO_PATTERN));
1522: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1523: PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1524: PetscCall(KSPCheckMatSolve(kspA, pc, ilinkA->Y));
1525: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1526: } else {
1527: PetscCall(MatMatMult(AinvB, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1528: PetscCall(MatAXPY(ilinkA->Y, 1.0, ilinkA->X, SAME_NONZERO_PATTERN));
1529: }
1530: } else {
1531: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1532: PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1533: PetscCall(KSPCheckMatSolve(kspA, pc, ilinkA->Y));
1534: PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1535: if (!ilinkA->Z) PetscCall(MatDuplicate(ilinkA->X, MAT_DO_NOT_COPY_VALUES, &ilinkA->Z));
1536: PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->X, ilinkA->Z, NULL));
1537: PetscCall(KSPMatSolve(kspUpper, ilinkA->X, ilinkA->Z));
1538: PetscCall(KSPCheckMatSolve(kspUpper, pc, ilinkA->Z));
1539: PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->X, ilinkA->Z, NULL));
1540: PetscCall(MatAXPY(ilinkA->Y, -1.0, ilinkA->Z, SAME_NONZERO_PATTERN));
1541: }
1542: PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1543: }
1544: PetscFunctionReturn(PETSC_SUCCESS);
1545: }
1547: static PetscErrorCode PCApplyTranspose_FieldSplit_Schur(PC pc, Vec x, Vec y)
1548: {
1549: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1550: PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1551: KSP kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
1553: PetscFunctionBegin;
1554: switch (jac->schurfactorization) {
1555: case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1556: /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1557: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1558: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1559: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1560: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1561: PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1562: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1563: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1564: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1565: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1566: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1567: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1568: PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1569: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1570: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1571: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1572: PetscCall(VecScale(ilinkD->y, jac->schurscale));
1573: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1574: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1575: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1576: break;
1577: case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1578: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1579: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1580: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1581: PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1582: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1583: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1584: PetscCall(MatMultTranspose(jac->B, ilinkA->y, ilinkD->x));
1585: PetscCall(VecScale(ilinkD->x, -1.));
1586: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1587: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1588: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1589: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1590: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1591: PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1592: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1593: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1594: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1595: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1596: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1597: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1598: break;
1599: case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1600: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1601: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1602: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1603: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1604: PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1605: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1606: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1607: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1608: PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->x));
1609: PetscCall(VecScale(ilinkA->x, -1.));
1610: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1611: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1612: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1613: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1614: PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1615: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1616: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1617: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1618: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1619: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1620: break;
1621: case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1622: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1623: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1624: PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->y, NULL));
1625: PetscCall(KSPSolveTranspose(kspUpper, ilinkA->x, ilinkA->y));
1626: PetscCall(KSPCheckSolve(kspUpper, pc, ilinkA->y));
1627: PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->y, NULL));
1628: PetscCall(MatMultTranspose(jac->B, ilinkA->y, ilinkD->x));
1629: PetscCall(VecScale(ilinkD->x, -1.0));
1630: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1631: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1633: PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1634: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1635: PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1636: PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1637: PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1638: PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1639: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1641: if (kspLower == kspA) {
1642: PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->y));
1643: PetscCall(VecAXPY(ilinkA->x, -1.0, ilinkA->y));
1644: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1645: PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1646: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1647: PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1648: } else {
1649: PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1650: PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1651: PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1652: PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->x));
1653: PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->z, NULL));
1654: PetscCall(KSPSolveTranspose(kspLower, ilinkA->x, ilinkA->z));
1655: PetscCall(KSPCheckSolve(kspLower, pc, ilinkA->z));
1656: PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->z, NULL));
1657: PetscCall(VecAXPY(ilinkA->y, -1.0, ilinkA->z));
1658: }
1659: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1660: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1661: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1662: }
1663: PetscFunctionReturn(PETSC_SUCCESS);
1664: }
1666: #define FieldSplitSplitSolveAdd(ilink, xx, yy) \
1667: ((PetscErrorCode)(VecScatterBegin((ilink)->sctx, xx, (ilink)->x, INSERT_VALUES, SCATTER_FORWARD) || VecScatterEnd((ilink)->sctx, xx, (ilink)->x, INSERT_VALUES, SCATTER_FORWARD) || PetscLogEventBegin((ilink)->event, (ilink)->ksp, (ilink)->x, (ilink)->y, NULL) || \
1668: KSPSolve((ilink)->ksp, (ilink)->x, (ilink)->y) || KSPCheckSolve((ilink)->ksp, pc, (ilink)->y) || PetscLogEventEnd((ilink)->event, (ilink)->ksp, (ilink)->x, (ilink)->y, NULL) || VecScatterBegin((ilink)->sctx, (ilink)->y, yy, ADD_VALUES, SCATTER_REVERSE) || \
1669: VecScatterEnd((ilink)->sctx, (ilink)->y, yy, ADD_VALUES, SCATTER_REVERSE)))
1671: static PetscErrorCode PCApply_FieldSplit(PC pc, Vec x, Vec y)
1672: {
1673: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1674: PC_FieldSplitLink ilink = jac->head;
1675: PetscInt cnt, bs;
1677: PetscFunctionBegin;
1678: if (jac->type == PC_COMPOSITE_ADDITIVE) {
1679: PetscBool matnest;
1681: PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));
1682: if (jac->defaultsplit && !matnest) {
1683: PetscCall(VecGetBlockSize(x, &bs));
1684: PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of x vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1685: PetscCall(VecGetBlockSize(y, &bs));
1686: PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of y vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1687: PetscCall(VecStrideGatherAll(x, jac->x, INSERT_VALUES));
1688: while (ilink) {
1689: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1690: PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1691: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1692: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1693: ilink = ilink->next;
1694: }
1695: PetscCall(VecStrideScatterAll(jac->y, y, INSERT_VALUES));
1696: } else {
1697: PetscCall(VecSet(y, 0.0));
1698: while (ilink) {
1699: PetscCall(FieldSplitSplitSolveAdd(ilink, x, y));
1700: ilink = ilink->next;
1701: }
1702: }
1703: } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1704: PetscCall(VecSet(y, 0.0));
1705: /* solve on first block for first block variables */
1706: PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, INSERT_VALUES, SCATTER_FORWARD));
1707: PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, INSERT_VALUES, SCATTER_FORWARD));
1708: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1709: PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1710: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1711: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1712: PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1713: PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1715: /* compute the residual only onto second block variables using first block variables */
1716: PetscCall(MatMult(jac->Afield[1], ilink->y, ilink->next->x));
1717: ilink = ilink->next;
1718: PetscCall(VecScale(ilink->x, -1.0));
1719: PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1720: PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1722: /* solve on second block variables */
1723: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1724: PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1725: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1726: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1727: PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1728: PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1729: } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1730: if (!jac->w1) {
1731: PetscCall(VecDuplicate(x, &jac->w1));
1732: PetscCall(VecDuplicate(x, &jac->w2));
1733: }
1734: PetscCall(VecSet(y, 0.0));
1735: PetscCall(FieldSplitSplitSolveAdd(ilink, x, y));
1736: cnt = 1;
1737: while (ilink->next) {
1738: ilink = ilink->next;
1739: /* compute the residual only over the part of the vector needed */
1740: PetscCall(MatMult(jac->Afield[cnt++], y, ilink->x));
1741: PetscCall(VecScale(ilink->x, -1.0));
1742: PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1743: PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1744: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1745: PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1746: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1747: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1748: PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1749: PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1750: }
1751: if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1752: cnt -= 2;
1753: while (ilink->previous) {
1754: ilink = ilink->previous;
1755: /* compute the residual only over the part of the vector needed */
1756: PetscCall(MatMult(jac->Afield[cnt--], y, ilink->x));
1757: PetscCall(VecScale(ilink->x, -1.0));
1758: PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1759: PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1760: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1761: PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1762: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1763: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1764: PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1765: PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1766: }
1767: }
1768: } else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Unsupported or unknown composition %d", (int)jac->type);
1769: PetscFunctionReturn(PETSC_SUCCESS);
1770: }
1772: static PetscErrorCode PCMatApply_FieldSplit(PC pc, Mat X, Mat Y)
1773: {
1774: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1775: PC_FieldSplitLink ilink = jac->head;
1776: PetscInt cnt;
1778: PetscFunctionBegin;
1779: /* create working matrices with the correct number of columns */
1780: PetscCall(PCFieldSplitCreateWorkMats_Private(pc, X));
1781: if (jac->type == PC_COMPOSITE_ADDITIVE) {
1782: PetscCall(MatZeroEntries(Y));
1783: while (ilink) {
1784: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1785: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1786: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1787: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1788: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1789: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1790: ilink = ilink->next;
1791: }
1792: } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1793: PetscCall(MatZeroEntries(Y));
1794: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1795: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1796: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1797: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1798: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1799: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1801: /* compute the residual only onto second block variables using first block variables */
1802: PetscCall(MatMatMult(jac->Afield[1], ilink->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->next->X));
1803: ilink = ilink->next;
1804: PetscCall(MatScale(ilink->X, -1.0));
1805: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));
1807: /* solve on second block variables */
1808: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1809: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1810: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1811: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1812: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1813: } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1814: /* general multiplicative with any number of splits */
1815: PetscCall(MatZeroEntries(Y));
1816: /* first split */
1817: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1818: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1819: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1820: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1821: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1822: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1823: cnt = 1;
1824: /* forward sweep */
1825: while (ilink->next) {
1826: ilink = ilink->next;
1827: /* compute the residual only over the part of the vector needed */
1828: PetscCall(MatMatMult(jac->Afield[cnt++], Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->X));
1829: PetscCall(MatScale(ilink->X, -1.0));
1830: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));
1831: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1832: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1833: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1834: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1835: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1836: }
1837: /* backward sweep for symmetric multiplicative */
1838: if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1839: cnt -= 2;
1840: while (ilink->previous) {
1841: ilink = ilink->previous;
1842: /* compute the residual only over the part of the vector needed */
1843: PetscCall(MatMatMult(jac->Afield[cnt--], Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->X));
1844: PetscCall(MatScale(ilink->X, -1.0));
1845: PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));
1846: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1847: PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1848: PetscCall(KSPCheckMatSolve(ilink->ksp, pc, ilink->Y));
1849: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1850: PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1851: }
1852: }
1853: } else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PCMatApply() not implemented for this fieldsplit type");
1854: PetscFunctionReturn(PETSC_SUCCESS);
1855: }
1857: static PetscErrorCode PCApply_FieldSplit_GKB(PC pc, Vec x, Vec y)
1858: {
1859: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1860: PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1861: KSP ksp = ilinkA->ksp;
1862: Vec u, v, Hu, d, work1, work2;
1863: PetscScalar alpha, z, nrmz2, *vecz;
1864: PetscReal lowbnd, nu, beta;
1865: PetscInt iterGKB;
1867: PetscFunctionBegin;
1868: PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1869: PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1870: PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1871: PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1873: u = jac->u;
1874: v = jac->v;
1875: Hu = jac->Hu;
1876: d = jac->d;
1877: work1 = jac->w1;
1878: work2 = jac->w2;
1879: vecz = jac->vecz;
1881: /* Change RHS to comply with matrix regularization H = A + nu*B*B' */
1882: /* Add q = q + nu*B*b */
1883: if (jac->gkbnu) {
1884: nu = jac->gkbnu;
1885: PetscCall(VecScale(ilinkD->x, jac->gkbnu));
1886: PetscCall(MatMultAdd(jac->B, ilinkD->x, ilinkA->x, ilinkA->x)); /* q = q + nu*B*b */
1887: } else {
1888: /* Situation when no augmented Lagrangian is used. Then we set inner */
1889: /* matrix N = I in [Ar13], and thus nu = 1. */
1890: nu = 1;
1891: }
1893: /* Transform rhs from [q,tilde{b}] to [0,b] */
1894: PetscCall(PetscLogEventBegin(ilinkA->event, ksp, ilinkA->x, ilinkA->y, NULL));
1895: PetscCall(KSPSolve(ksp, ilinkA->x, ilinkA->y));
1896: PetscCall(KSPCheckSolve(ksp, pc, ilinkA->y));
1897: PetscCall(PetscLogEventEnd(ilinkA->event, ksp, ilinkA->x, ilinkA->y, NULL));
1898: PetscCall(MatMultHermitianTranspose(jac->B, ilinkA->y, work1));
1899: PetscCall(VecAXPBY(work1, 1.0 / nu, -1.0, ilinkD->x)); /* c = b - B'*x */
1901: /* First step of algorithm */
1902: PetscCall(VecNorm(work1, NORM_2, &beta)); /* beta = sqrt(nu*c'*c)*/
1903: KSPCheckDot(ksp, beta);
1904: beta = PetscSqrtReal(nu) * beta;
1905: PetscCall(VecAXPBY(v, nu / beta, 0.0, work1)); /* v = nu/beta *c */
1906: PetscCall(MatMult(jac->B, v, work2)); /* u = H^{-1}*B*v */
1907: PetscCall(PetscLogEventBegin(ilinkA->event, ksp, work2, u, NULL));
1908: PetscCall(KSPSolve(ksp, work2, u));
1909: PetscCall(KSPCheckSolve(ksp, pc, u));
1910: PetscCall(PetscLogEventEnd(ilinkA->event, ksp, work2, u, NULL));
1911: PetscCall(MatMult(jac->H, u, Hu)); /* alpha = u'*H*u */
1912: PetscCall(VecDot(Hu, u, &alpha));
1913: KSPCheckDot(ksp, alpha);
1914: PetscCheck(PetscRealPart(alpha) > 0.0, PETSC_COMM_SELF, PETSC_ERR_NOT_CONVERGED, "GKB preconditioner diverged, H is not positive definite");
1915: alpha = PetscSqrtReal(PetscAbsScalar(alpha));
1916: PetscCall(VecScale(u, 1.0 / alpha));
1917: PetscCall(VecAXPBY(d, 1.0 / alpha, 0.0, v)); /* v = nu/beta *c */
1919: z = beta / alpha;
1920: vecz[1] = z;
1922: /* Computation of first iterate x(1) and p(1) */
1923: PetscCall(VecAXPY(ilinkA->y, z, u));
1924: PetscCall(VecCopy(d, ilinkD->y));
1925: PetscCall(VecScale(ilinkD->y, -z));
1927: iterGKB = 1;
1928: lowbnd = 2 * jac->gkbtol;
1929: if (jac->gkbmonitor) PetscCall(PetscViewerASCIIPrintf(jac->gkbviewer, "%3" PetscInt_FMT " GKB Lower bound estimate %14.12e\n", iterGKB, (double)lowbnd));
1931: while (iterGKB < jac->gkbmaxit && lowbnd > jac->gkbtol) {
1932: iterGKB += 1;
1933: PetscCall(MatMultHermitianTranspose(jac->B, u, work1)); /* v <- nu*(B'*u-alpha/nu*v) */
1934: PetscCall(VecAXPBY(v, nu, -alpha, work1));
1935: PetscCall(VecNorm(v, NORM_2, &beta)); /* beta = sqrt(nu)*v'*v */
1936: beta = beta / PetscSqrtReal(nu);
1937: PetscCall(VecScale(v, 1.0 / beta));
1938: PetscCall(MatMult(jac->B, v, work2)); /* u <- H^{-1}*(B*v-beta*H*u) */
1939: PetscCall(MatMult(jac->H, u, Hu));
1940: PetscCall(VecAXPY(work2, -beta, Hu));
1941: PetscCall(PetscLogEventBegin(ilinkA->event, ksp, work2, u, NULL));
1942: PetscCall(KSPSolve(ksp, work2, u));
1943: PetscCall(KSPCheckSolve(ksp, pc, u));
1944: PetscCall(PetscLogEventEnd(ilinkA->event, ksp, work2, u, NULL));
1945: PetscCall(MatMult(jac->H, u, Hu)); /* alpha = u'*H*u */
1946: PetscCall(VecDot(Hu, u, &alpha));
1947: KSPCheckDot(ksp, alpha);
1948: PetscCheck(PetscRealPart(alpha) > 0.0, PETSC_COMM_SELF, PETSC_ERR_NOT_CONVERGED, "GKB preconditioner diverged, H is not positive definite");
1949: alpha = PetscSqrtReal(PetscAbsScalar(alpha));
1950: PetscCall(VecScale(u, 1.0 / alpha));
1952: z = -beta / alpha * z; /* z <- beta/alpha*z */
1953: vecz[0] = z;
1955: /* Computation of new iterate x(i+1) and p(i+1) */
1956: PetscCall(VecAXPBY(d, 1.0 / alpha, -beta / alpha, v)); /* d = (v-beta*d)/alpha */
1957: PetscCall(VecAXPY(ilinkA->y, z, u)); /* r = r + z*u */
1958: PetscCall(VecAXPY(ilinkD->y, -z, d)); /* p = p - z*d */
1959: PetscCall(MatMult(jac->H, ilinkA->y, Hu)); /* ||u||_H = u'*H*u */
1960: PetscCall(VecDot(Hu, ilinkA->y, &nrmz2));
1962: /* Compute Lower Bound estimate */
1963: if (iterGKB > jac->gkbdelay) {
1964: lowbnd = 0.0;
1965: for (PetscInt j = 0; j < jac->gkbdelay; j++) lowbnd += PetscAbsScalar(vecz[j] * vecz[j]);
1966: lowbnd = PetscSqrtReal(lowbnd / PetscAbsScalar(nrmz2));
1967: }
1969: for (PetscInt j = 0; j < jac->gkbdelay - 1; j++) vecz[jac->gkbdelay - j - 1] = vecz[jac->gkbdelay - j - 2];
1970: if (jac->gkbmonitor) PetscCall(PetscViewerASCIIPrintf(jac->gkbviewer, "%3" PetscInt_FMT " GKB Lower bound estimate %14.12e\n", iterGKB, (double)lowbnd));
1971: }
1973: PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1974: PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1975: PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1976: PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1977: PetscFunctionReturn(PETSC_SUCCESS);
1978: }
1980: #define FieldSplitSplitSolveAddTranspose(ilink, xx, yy) \
1981: ((PetscErrorCode)(VecScatterBegin((ilink)->sctx, xx, (ilink)->y, INSERT_VALUES, SCATTER_FORWARD) || VecScatterEnd((ilink)->sctx, xx, (ilink)->y, INSERT_VALUES, SCATTER_FORWARD) || PetscLogEventBegin((ilink)->event, (ilink)->ksp, (ilink)->y, (ilink)->x, NULL) || \
1982: KSPSolveTranspose((ilink)->ksp, (ilink)->y, (ilink)->x) || KSPCheckSolve((ilink)->ksp, pc, (ilink)->x) || PetscLogEventEnd((ilink)->event, (ilink)->ksp, (ilink)->y, (ilink)->x, NULL) || VecScatterBegin((ilink)->sctx, (ilink)->x, yy, ADD_VALUES, SCATTER_REVERSE) || \
1983: VecScatterEnd((ilink)->sctx, (ilink)->x, yy, ADD_VALUES, SCATTER_REVERSE)))
1985: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc, Vec x, Vec y)
1986: {
1987: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
1988: PC_FieldSplitLink ilink = jac->head;
1989: PetscInt bs;
1991: PetscFunctionBegin;
1992: if (jac->type == PC_COMPOSITE_ADDITIVE) {
1993: PetscBool matnest;
1995: PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));
1996: if (jac->defaultsplit && !matnest) {
1997: PetscCall(VecGetBlockSize(x, &bs));
1998: PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of x vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1999: PetscCall(VecGetBlockSize(y, &bs));
2000: PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of y vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
2001: PetscCall(VecStrideGatherAll(x, jac->x, INSERT_VALUES));
2002: while (ilink) {
2003: PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
2004: PetscCall(KSPSolveTranspose(ilink->ksp, ilink->x, ilink->y));
2005: PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
2006: PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
2007: ilink = ilink->next;
2008: }
2009: PetscCall(VecStrideScatterAll(jac->y, y, INSERT_VALUES));
2010: } else {
2011: PetscCall(VecSet(y, 0.0));
2012: while (ilink) {
2013: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
2014: ilink = ilink->next;
2015: }
2016: }
2017: } else {
2018: if (!jac->w1) {
2019: PetscCall(VecDuplicate(x, &jac->w1));
2020: PetscCall(VecDuplicate(x, &jac->w2));
2021: }
2022: PetscCall(VecSet(y, 0.0));
2023: if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
2024: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
2025: while (ilink->next) {
2026: ilink = ilink->next;
2027: PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2028: PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2029: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2030: }
2031: while (ilink->previous) {
2032: ilink = ilink->previous;
2033: PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2034: PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2035: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2036: }
2037: } else {
2038: while (ilink->next) { /* get to last entry in linked list */
2039: ilink = ilink->next;
2040: }
2041: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
2042: while (ilink->previous) {
2043: ilink = ilink->previous;
2044: PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2045: PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2046: PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2047: }
2048: }
2049: }
2050: PetscFunctionReturn(PETSC_SUCCESS);
2051: }
2053: static PetscErrorCode PCReset_FieldSplit(PC pc)
2054: {
2055: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2056: PC_FieldSplitLink ilink = jac->head, next;
2058: PetscFunctionBegin;
2059: while (ilink) {
2060: PetscCall(KSPDestroy(&ilink->ksp));
2061: PetscCall(VecDestroy(&ilink->x));
2062: PetscCall(VecDestroy(&ilink->y));
2063: PetscCall(VecDestroy(&ilink->z));
2064: PetscCall(MatDestroy(&ilink->X));
2065: PetscCall(MatDestroy(&ilink->Y));
2066: PetscCall(MatDestroy(&ilink->Z));
2067: PetscCall(VecScatterDestroy(&ilink->sctx));
2068: PetscCall(ISDestroy(&ilink->is));
2069: PetscCall(ISDestroy(&ilink->is_col));
2070: PetscCall(PetscFree(ilink->splitname));
2071: PetscCall(PetscFree(ilink->fields));
2072: PetscCall(PetscFree(ilink->fields_col));
2073: next = ilink->next;
2074: PetscCall(PetscFree(ilink));
2075: ilink = next;
2076: }
2077: jac->head = NULL;
2078: PetscCall(PetscFree2(jac->x, jac->y));
2079: if (jac->mat && jac->mat != jac->pmat) {
2080: PetscCall(MatDestroyMatrices(jac->nsplits, &jac->mat));
2081: } else if (jac->mat) {
2082: jac->mat = NULL;
2083: }
2084: if (jac->pmat) PetscCall(MatDestroyMatrices(jac->nsplits, &jac->pmat));
2085: if (jac->Afield) PetscCall(MatDestroyMatrices(jac->nsplits, &jac->Afield));
2086: jac->nsplits = 0;
2087: PetscCall(VecDestroy(&jac->w1));
2088: PetscCall(VecDestroy(&jac->w2));
2089: if (jac->schur) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", NULL));
2090: PetscCall(MatDestroy(&jac->schur));
2091: PetscCall(MatDestroy(&jac->schurp));
2092: PetscCall(MatDestroy(&jac->schur_user));
2093: PetscCall(KSPDestroy(&jac->kspschur));
2094: PetscCall(KSPDestroy(&jac->kspupper));
2095: PetscCall(MatDestroy(&jac->B));
2096: PetscCall(MatDestroy(&jac->C));
2097: PetscCall(MatDestroy(&jac->H));
2098: PetscCall(VecDestroy(&jac->u));
2099: PetscCall(VecDestroy(&jac->v));
2100: PetscCall(VecDestroy(&jac->Hu));
2101: PetscCall(VecDestroy(&jac->d));
2102: PetscCall(PetscFree(jac->vecz));
2103: PetscCall(PetscViewerDestroy(&jac->gkbviewer));
2104: jac->isrestrict = PETSC_FALSE;
2105: PetscFunctionReturn(PETSC_SUCCESS);
2106: }
2108: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
2109: {
2110: PetscFunctionBegin;
2111: PetscCall(PCReset_FieldSplit(pc));
2112: PetscCall(PetscFree(pc->data));
2113: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
2114: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetFields_C", NULL));
2115: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetIS_C", NULL));
2116: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetType_C", NULL));
2117: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetBlockSize_C", NULL));
2118: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitRestrictIS_C", NULL));
2119: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSchurGetSubKSP_C", NULL));
2120: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", NULL));
2121: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", NULL));
2122: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", NULL));
2123: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", NULL));
2124: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", NULL));
2125: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", NULL));
2126: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", NULL));
2127: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", NULL));
2128: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", NULL));
2129: PetscFunctionReturn(PETSC_SUCCESS);
2130: }
2132: static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc, PetscOptionItems PetscOptionsObject)
2133: {
2134: PetscInt bs;
2135: PetscBool flg;
2136: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2137: PCCompositeType ctype;
2139: PetscFunctionBegin;
2140: PetscOptionsHeadBegin(PetscOptionsObject, "FieldSplit options");
2141: PetscCall(PetscOptionsBool("-pc_fieldsplit_dm_splits", "Whether to use DMCreateFieldDecomposition() for splits", "PCFieldSplitSetDMSplits", jac->dm_splits, &jac->dm_splits, NULL));
2142: PetscCall(PetscOptionsInt("-pc_fieldsplit_block_size", "Blocksize that defines number of fields", "PCFieldSplitSetBlockSize", jac->bs, &bs, &flg));
2143: if (flg) PetscCall(PCFieldSplitSetBlockSize(pc, bs));
2144: jac->diag_use_amat = pc->useAmat;
2145: PetscCall(PetscOptionsBool("-pc_fieldsplit_diag_use_amat", "Use Amat (not Pmat) to extract diagonal fieldsplit blocks", "PCFieldSplitSetDiagUseAmat", jac->diag_use_amat, &jac->diag_use_amat, NULL));
2146: jac->offdiag_use_amat = pc->useAmat;
2147: PetscCall(PetscOptionsBool("-pc_fieldsplit_off_diag_use_amat", "Use Amat (not Pmat) to extract off-diagonal fieldsplit blocks", "PCFieldSplitSetOffDiagUseAmat", jac->offdiag_use_amat, &jac->offdiag_use_amat, NULL));
2148: PetscCall(PetscOptionsBool("-pc_fieldsplit_detect_saddle_point", "Form 2-way split by detecting zero diagonal entries", "PCFieldSplitSetDetectSaddlePoint", jac->detect, &jac->detect, NULL));
2149: PetscCall(PCFieldSplitSetDetectSaddlePoint(pc, jac->detect)); /* Sets split type and Schur PC type */
2150: PetscCall(PetscOptionsEnum("-pc_fieldsplit_type", "Type of composition", "PCFieldSplitSetType", PCCompositeTypes, (PetscEnum)jac->type, (PetscEnum *)&ctype, &flg));
2151: if (flg) PetscCall(PCFieldSplitSetType(pc, ctype));
2152: /* Only setup fields once */
2153: if (jac->bs > 0 && jac->nsplits == 0) {
2154: /* only allow user to set fields from command line.
2155: otherwise user can set them in PCFieldSplitSetDefaults() */
2156: PetscCall(PCFieldSplitSetRuntimeSplits_Private(pc));
2157: if (jac->splitdefined) PetscCall(PetscInfo(pc, "Splits defined using the options database\n"));
2158: }
2159: if (jac->type == PC_COMPOSITE_SCHUR) {
2160: PetscCall(PetscOptionsGetEnum(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_schur_factorization_type", PCFieldSplitSchurFactTypes, (PetscEnum *)&jac->schurfactorization, &flg));
2161: if (flg) PetscCall(PetscInfo(pc, "Deprecated use of -pc_fieldsplit_schur_factorization_type\n"));
2162: PetscCall(PetscOptionsEnum("-pc_fieldsplit_schur_fact_type", "Which off-diagonal parts of the block factorization to use", "PCFieldSplitSetSchurFactType", PCFieldSplitSchurFactTypes, (PetscEnum)jac->schurfactorization, (PetscEnum *)&jac->schurfactorization, NULL));
2163: PetscCall(PetscOptionsEnum("-pc_fieldsplit_schur_precondition", "How to build preconditioner for Schur complement", "PCFieldSplitSetSchurPre", PCFieldSplitSchurPreTypes, (PetscEnum)jac->schurpre, (PetscEnum *)&jac->schurpre, NULL));
2164: PetscCall(PetscOptionsScalar("-pc_fieldsplit_schur_scale", "Scale Schur complement", "PCFieldSplitSetSchurScale", jac->schurscale, &jac->schurscale, NULL));
2165: } else if (jac->type == PC_COMPOSITE_GKB) {
2166: PetscCall(PetscOptionsReal("-pc_fieldsplit_gkb_tol", "The tolerance for the lower bound stopping criterion", "PCFieldSplitSetGKBTol", jac->gkbtol, &jac->gkbtol, NULL));
2167: PetscCall(PetscOptionsInt("-pc_fieldsplit_gkb_delay", "The delay value for lower bound criterion", "PCFieldSplitSetGKBDelay", jac->gkbdelay, &jac->gkbdelay, NULL));
2168: PetscCall(PetscOptionsBoundedReal("-pc_fieldsplit_gkb_nu", "Parameter in augmented Lagrangian approach", "PCFieldSplitSetGKBNu", jac->gkbnu, &jac->gkbnu, NULL, 0.0));
2169: PetscCall(PetscOptionsInt("-pc_fieldsplit_gkb_maxit", "Maximum allowed number of iterations", "PCFieldSplitSetGKBMaxit", jac->gkbmaxit, &jac->gkbmaxit, NULL));
2170: PetscCall(PetscOptionsBool("-pc_fieldsplit_gkb_monitor", "Prints number of GKB iterations and error", "PCFieldSplitGKB", jac->gkbmonitor, &jac->gkbmonitor, NULL));
2171: }
2172: /*
2173: In the initial call to this routine the sub-solver data structures do not exist so we cannot call KSPSetFromOptions() on them yet.
2174: But after the initial setup of ALL the layers of sub-solvers is completed we do want to call KSPSetFromOptions() on the sub-solvers every time it
2175: is called on the outer solver in case changes were made in the options database
2177: But even after PCSetUp_FieldSplit() is called all the options inside the inner levels of sub-solvers may still not have been set thus we only call the KSPSetFromOptions()
2178: if we know that the entire stack of sub-solvers below this have been complete instantiated, we check this by seeing if any solver iterations are complete.
2179: Without this extra check test p2p1fetidp_olof_full and others fail with incorrect matrix types.
2181: There could be a negative side effect of calling the KSPSetFromOptions() below.
2183: If one captured the PetscObjectState of the options database one could skip these calls if the database has not changed from the previous call
2184: */
2185: if (jac->issetup) {
2186: PC_FieldSplitLink ilink = jac->head;
2187: if (jac->type == PC_COMPOSITE_SCHUR) {
2188: if (jac->kspupper && jac->kspupper->totalits > 0) PetscCall(KSPSetFromOptions(jac->kspupper));
2189: if (jac->kspschur && jac->kspschur->totalits > 0) PetscCall(KSPSetFromOptions(jac->kspschur));
2190: }
2191: while (ilink) {
2192: if (ilink->ksp->totalits > 0) PetscCall(KSPSetFromOptions(ilink->ksp));
2193: ilink = ilink->next;
2194: }
2195: }
2196: PetscOptionsHeadEnd();
2197: PetscFunctionReturn(PETSC_SUCCESS);
2198: }
2200: static PetscErrorCode PCFieldSplitSetFields_FieldSplit(PC pc, const char splitname[], PetscInt n, const PetscInt *fields, const PetscInt *fields_col)
2201: {
2202: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2203: PC_FieldSplitLink ilink, next = jac->head;
2204: char prefix[128];
2205: PetscInt i;
2206: PetscLogEvent nse;
2208: PetscFunctionBegin;
2209: if (jac->splitdefined) {
2210: PetscCall(PetscInfo(pc, "Ignoring new split \"%s\" because the splits have already been defined\n", splitname));
2211: PetscFunctionReturn(PETSC_SUCCESS);
2212: }
2213: for (i = 0; i < n; i++) PetscCheck(fields[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative field %" PetscInt_FMT " requested", fields[i]);
2214: PetscCall(PetscNew(&ilink));
2215: if (splitname) {
2216: PetscCall(PetscStrallocpy(splitname, &ilink->splitname));
2217: } else {
2218: PetscCall(PetscMalloc1(3, &ilink->splitname));
2219: PetscCall(PetscSNPrintf(ilink->splitname, 2, "%" PetscInt_FMT, jac->nsplits));
2220: }
2221: PetscCall(PetscMPIIntCast(jac->nsplits, &nse));
2222: ilink->event = jac->nsplits < 5 ? KSP_Solve_FS_0 + nse : KSP_Solve_FS_0 + 4; /* Splits greater than 4 logged in 4th split */
2223: PetscCall(PetscMalloc1(n, &ilink->fields));
2224: PetscCall(PetscArraycpy(ilink->fields, fields, n));
2225: PetscCall(PetscMalloc1(n, &ilink->fields_col));
2226: PetscCall(PetscArraycpy(ilink->fields_col, fields_col, n));
2228: ilink->nfields = n;
2229: ilink->next = NULL;
2230: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &ilink->ksp));
2231: PetscCall(KSPSetNestLevel(ilink->ksp, pc->kspnestlevel));
2232: PetscCall(KSPSetErrorIfNotConverged(ilink->ksp, pc->erroriffailure));
2233: PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)pc, 1));
2234: PetscCall(KSPSetType(ilink->ksp, KSPPREONLY));
2236: PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
2237: PetscCall(KSPSetOptionsPrefix(ilink->ksp, prefix));
2239: if (!next) {
2240: jac->head = ilink;
2241: ilink->previous = NULL;
2242: } else {
2243: while (next->next) next = next->next;
2244: next->next = ilink;
2245: ilink->previous = next;
2246: }
2247: jac->nsplits++;
2248: PetscFunctionReturn(PETSC_SUCCESS);
2249: }
2251: static PetscErrorCode PCFieldSplitSchurGetSubKSP_FieldSplit(PC pc, PetscInt *n, KSP **subksp)
2252: {
2253: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2255: PetscFunctionBegin;
2256: *subksp = NULL;
2257: if (n) *n = 0;
2258: if (jac->type == PC_COMPOSITE_SCHUR) {
2259: PetscInt nn;
2261: PetscCheck(jac->schur, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitSchurGetSubKSP()");
2262: PetscCheck(jac->nsplits == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "Unexpected number of splits %" PetscInt_FMT " != 2", jac->nsplits);
2263: nn = jac->nsplits + (jac->kspupper != jac->head->ksp ? 1 : 0);
2264: PetscCall(PetscMalloc1(nn, subksp));
2265: (*subksp)[0] = jac->head->ksp;
2266: (*subksp)[1] = jac->kspschur;
2267: if (jac->kspupper != jac->head->ksp) (*subksp)[2] = jac->kspupper;
2268: if (n) *n = nn;
2269: }
2270: PetscFunctionReturn(PETSC_SUCCESS);
2271: }
2273: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc, PetscInt *n, KSP **subksp)
2274: {
2275: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2277: PetscFunctionBegin;
2278: PetscCheck(jac->schur, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitGetSubKSP()");
2279: PetscCall(PetscMalloc1(jac->nsplits, subksp));
2280: PetscCall(MatSchurComplementGetKSP(jac->schur, *subksp));
2282: (*subksp)[1] = jac->kspschur;
2283: if (n) *n = jac->nsplits;
2284: PetscFunctionReturn(PETSC_SUCCESS);
2285: }
2287: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit(PC pc, PetscInt *n, KSP **subksp)
2288: {
2289: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2290: PetscInt cnt = 0;
2291: PC_FieldSplitLink ilink = jac->head;
2293: PetscFunctionBegin;
2294: PetscCall(PetscMalloc1(jac->nsplits, subksp));
2295: while (ilink) {
2296: (*subksp)[cnt++] = ilink->ksp;
2297: ilink = ilink->next;
2298: }
2299: PetscCheck(cnt == jac->nsplits, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupt PCFIELDSPLIT object: number of splits in linked list %" PetscInt_FMT " does not match number in object %" PetscInt_FMT, cnt, jac->nsplits);
2300: if (n) *n = jac->nsplits;
2301: PetscFunctionReturn(PETSC_SUCCESS);
2302: }
2304: /*@
2305: PCFieldSplitRestrictIS - Restricts the fieldsplit `IS`s to be within a given `IS`.
2307: Input Parameters:
2308: + pc - the preconditioner context
2309: - isy - the index set that defines the indices to which the fieldsplit is to be restricted
2311: Level: advanced
2313: Developer Notes:
2314: It seems the resulting `IS`s will not cover the entire space, so
2315: how can they define a convergent preconditioner? Needs explaining.
2317: .seealso: [](sec_block_matrices), `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
2318: @*/
2319: PetscErrorCode PCFieldSplitRestrictIS(PC pc, IS isy)
2320: {
2321: PetscFunctionBegin;
2324: PetscTryMethod(pc, "PCFieldSplitRestrictIS_C", (PC, IS), (pc, isy));
2325: PetscFunctionReturn(PETSC_SUCCESS);
2326: }
2328: static PetscErrorCode PCFieldSplitRestrictIS_FieldSplit(PC pc, IS isy)
2329: {
2330: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2331: PC_FieldSplitLink ilink = jac->head, next;
2332: PetscInt localsize, size, sizez, i;
2333: const PetscInt *ind, *indz;
2334: PetscInt *indc, *indcz;
2335: PetscBool flg;
2337: PetscFunctionBegin;
2338: PetscCall(ISGetLocalSize(isy, &localsize));
2339: PetscCallMPI(MPI_Scan(&localsize, &size, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)isy)));
2340: size -= localsize;
2341: while (ilink) {
2342: IS isrl, isr;
2343: PC subpc;
2344: PetscCall(ISEmbed(ilink->is, isy, PETSC_TRUE, &isrl));
2345: PetscCall(ISGetLocalSize(isrl, &localsize));
2346: PetscCall(PetscMalloc1(localsize, &indc));
2347: PetscCall(ISGetIndices(isrl, &ind));
2348: PetscCall(PetscArraycpy(indc, ind, localsize));
2349: PetscCall(ISRestoreIndices(isrl, &ind));
2350: PetscCall(ISDestroy(&isrl));
2351: for (i = 0; i < localsize; i++) *(indc + i) += size;
2352: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)isy), localsize, indc, PETSC_OWN_POINTER, &isr));
2353: PetscCall(PetscObjectReference((PetscObject)isr));
2354: PetscCall(ISDestroy(&ilink->is));
2355: ilink->is = isr;
2356: PetscCall(PetscObjectReference((PetscObject)isr));
2357: PetscCall(ISDestroy(&ilink->is_col));
2358: ilink->is_col = isr;
2359: PetscCall(ISDestroy(&isr));
2360: PetscCall(KSPGetPC(ilink->ksp, &subpc));
2361: PetscCall(PetscObjectTypeCompare((PetscObject)subpc, PCFIELDSPLIT, &flg));
2362: if (flg) {
2363: IS iszl, isz;
2364: MPI_Comm comm;
2365: PetscCall(ISGetLocalSize(ilink->is, &localsize));
2366: comm = PetscObjectComm((PetscObject)ilink->is);
2367: PetscCall(ISEmbed(isy, ilink->is, PETSC_TRUE, &iszl));
2368: PetscCallMPI(MPI_Scan(&localsize, &sizez, 1, MPIU_INT, MPI_SUM, comm));
2369: sizez -= localsize;
2370: PetscCall(ISGetLocalSize(iszl, &localsize));
2371: PetscCall(PetscMalloc1(localsize, &indcz));
2372: PetscCall(ISGetIndices(iszl, &indz));
2373: PetscCall(PetscArraycpy(indcz, indz, localsize));
2374: PetscCall(ISRestoreIndices(iszl, &indz));
2375: PetscCall(ISDestroy(&iszl));
2376: for (i = 0; i < localsize; i++) *(indcz + i) += sizez;
2377: PetscCall(ISCreateGeneral(comm, localsize, indcz, PETSC_OWN_POINTER, &isz));
2378: PetscCall(PCFieldSplitRestrictIS(subpc, isz));
2379: PetscCall(ISDestroy(&isz));
2380: }
2381: next = ilink->next;
2382: ilink = next;
2383: }
2384: jac->isrestrict = PETSC_TRUE;
2385: PetscFunctionReturn(PETSC_SUCCESS);
2386: }
2388: static PetscErrorCode PCFieldSplitSetIS_FieldSplit(PC pc, const char splitname[], IS is)
2389: {
2390: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2391: PC_FieldSplitLink ilink, next = jac->head;
2392: char prefix[128];
2393: PetscLogEvent nse;
2395: PetscFunctionBegin;
2396: if (jac->splitdefined) {
2397: PetscCall(PetscInfo(pc, "Ignoring new split \"%s\" because the splits have already been defined\n", splitname));
2398: PetscFunctionReturn(PETSC_SUCCESS);
2399: }
2400: PetscCall(PetscNew(&ilink));
2401: if (splitname) {
2402: PetscCall(PetscStrallocpy(splitname, &ilink->splitname));
2403: } else {
2404: PetscCall(PetscMalloc1(8, &ilink->splitname));
2405: PetscCall(PetscSNPrintf(ilink->splitname, 7, "%" PetscInt_FMT, jac->nsplits));
2406: }
2407: PetscCall(PetscMPIIntCast(jac->nsplits, &nse));
2408: ilink->event = jac->nsplits < 5 ? KSP_Solve_FS_0 + nse : KSP_Solve_FS_0 + 4; /* Splits greater than 4 logged in 4th split */
2409: PetscCall(PetscObjectReference((PetscObject)is));
2410: PetscCall(ISDestroy(&ilink->is));
2411: ilink->is = is;
2412: PetscCall(PetscObjectReference((PetscObject)is));
2413: PetscCall(ISDestroy(&ilink->is_col));
2414: ilink->is_col = is;
2415: ilink->next = NULL;
2416: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &ilink->ksp));
2417: PetscCall(KSPSetNestLevel(ilink->ksp, pc->kspnestlevel));
2418: PetscCall(KSPSetErrorIfNotConverged(ilink->ksp, pc->erroriffailure));
2419: PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)pc, 1));
2420: PetscCall(KSPSetType(ilink->ksp, KSPPREONLY));
2422: PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
2423: PetscCall(KSPSetOptionsPrefix(ilink->ksp, prefix));
2425: if (!next) {
2426: jac->head = ilink;
2427: ilink->previous = NULL;
2428: } else {
2429: while (next->next) next = next->next;
2430: next->next = ilink;
2431: ilink->previous = next;
2432: }
2433: jac->nsplits++;
2434: PetscFunctionReturn(PETSC_SUCCESS);
2435: }
2437: /*@
2438: PCFieldSplitSetFields - Sets the fields that define one particular split in `PCFIELDSPLIT`
2440: Logically Collective
2442: Input Parameters:
2443: + pc - the preconditioner context
2444: . splitname - name of this split, if `NULL` the number of the split is used
2445: . n - the number of fields in this split
2446: . fields - the fields in this split
2447: - fields_col - generally the same as `fields`, if it does not match `fields` then the submatrix that is solved for this set of fields comes from an off-diagonal block
2448: of the matrix and `fields_col` provides the column indices for that block
2450: Options Database Key:
2451: . -pc_fieldsplit_%d_fields a,b,... - indicates the fields to be used in the `%d`'th split
2453: Level: intermediate
2455: Notes:
2456: Use `PCFieldSplitSetIS()` to set a general set of indices as a split.
2458: If the matrix used to construct the preconditioner is `MATNEST` then field i refers to the `is_row[i]` `IS` passed to `MatCreateNest()`.
2460: If the matrix used to construct the preconditioner is not `MATNEST` then
2461: `PCFieldSplitSetFields()` is for defining fields as strided blocks (based on the block size provided to the matrix with `MatSetBlockSize()` or
2462: to the `PC` with `PCFieldSplitSetBlockSize()`). For example, if the block
2463: size is three then one can define a split as 0, or 1 or 2 or 0,1 or 0,2 or 1,2 which mean
2464: 0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x23x56x8.. x12x45x78x....
2465: where the numbered entries indicate what is in the split.
2467: This function is called once per split (it creates a new split each time). Solve options
2468: for this split will be available under the prefix `-fieldsplit_SPLITNAME_`.
2470: `PCFieldSplitSetIS()` does not support having a `fields_col` different from `fields`
2472: Developer Notes:
2473: This routine does not actually create the `IS` representing the split, that is delayed
2474: until `PCSetUp_FieldSplit()`, because information about the vector/matrix layouts may not be
2475: available when this routine is called.
2477: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetBlockSize()`, `PCFieldSplitSetIS()`, `PCFieldSplitRestrictIS()`,
2478: `MatSetBlockSize()`, `MatCreateNest()`
2479: @*/
2480: PetscErrorCode PCFieldSplitSetFields(PC pc, const char splitname[], PetscInt n, const PetscInt fields[], const PetscInt fields_col[])
2481: {
2482: PetscFunctionBegin;
2484: PetscAssertPointer(splitname, 2);
2485: PetscCheck(n >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Provided number of fields %" PetscInt_FMT " in split \"%s\" not positive", n, splitname);
2486: PetscAssertPointer(fields, 4);
2487: PetscTryMethod(pc, "PCFieldSplitSetFields_C", (PC, const char[], PetscInt, const PetscInt *, const PetscInt *), (pc, splitname, n, fields, fields_col));
2488: PetscFunctionReturn(PETSC_SUCCESS);
2489: }
2491: /*@
2492: PCFieldSplitSetDiagUseAmat - set flag indicating whether to extract diagonal blocks from Amat (rather than Pmat) to build
2493: the sub-matrices associated with each split. Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.
2495: Logically Collective
2497: Input Parameters:
2498: + pc - the preconditioner object
2499: - flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from
2501: Options Database Key:
2502: . -pc_fieldsplit_diag_use_amat - use the Amat to provide the diagonal blocks
2504: Level: intermediate
2506: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitGetDiagUseAmat()`, `PCFieldSplitSetOffDiagUseAmat()`, `PCFIELDSPLIT`
2507: @*/
2508: PetscErrorCode PCFieldSplitSetDiagUseAmat(PC pc, PetscBool flg)
2509: {
2510: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2511: PetscBool isfs;
2513: PetscFunctionBegin;
2515: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2516: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2517: jac->diag_use_amat = flg;
2518: PetscFunctionReturn(PETSC_SUCCESS);
2519: }
2521: /*@
2522: PCFieldSplitGetDiagUseAmat - get the flag indicating whether to extract diagonal blocks from Amat (rather than Pmat) to build
2523: the sub-matrices associated with each split. Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.
2525: Logically Collective
2527: Input Parameter:
2528: . pc - the preconditioner object
2530: Output Parameter:
2531: . flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from
2533: Level: intermediate
2535: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitSetDiagUseAmat()`, `PCFieldSplitGetOffDiagUseAmat()`, `PCFIELDSPLIT`
2536: @*/
2537: PetscErrorCode PCFieldSplitGetDiagUseAmat(PC pc, PetscBool *flg)
2538: {
2539: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2540: PetscBool isfs;
2542: PetscFunctionBegin;
2544: PetscAssertPointer(flg, 2);
2545: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2546: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2547: *flg = jac->diag_use_amat;
2548: PetscFunctionReturn(PETSC_SUCCESS);
2549: }
2551: /*@
2552: PCFieldSplitSetOffDiagUseAmat - set flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat) to build
2553: the sub-matrices associated with each split. Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.
2555: Logically Collective
2557: Input Parameters:
2558: + pc - the preconditioner object
2559: - flg - boolean flag indicating whether or not to use Amat to extract the off-diagonal blocks from
2561: Options Database Key:
2562: . -pc_fieldsplit_off_diag_use_amat (true|false) - use the Amat to extract the off-diagonal blocks
2564: Level: intermediate
2566: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitGetOffDiagUseAmat()`, `PCFieldSplitSetDiagUseAmat()`, `PCFIELDSPLIT`
2567: @*/
2568: PetscErrorCode PCFieldSplitSetOffDiagUseAmat(PC pc, PetscBool flg)
2569: {
2570: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2571: PetscBool isfs;
2573: PetscFunctionBegin;
2575: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2576: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2577: jac->offdiag_use_amat = flg;
2578: PetscFunctionReturn(PETSC_SUCCESS);
2579: }
2581: /*@
2582: PCFieldSplitGetOffDiagUseAmat - get the flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat) to build
2583: the sub-matrices associated with each split. Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.
2585: Logically Collective
2587: Input Parameter:
2588: . pc - the preconditioner object
2590: Output Parameter:
2591: . flg - boolean flag indicating whether or not to use Amat to extract the off-diagonal blocks from
2593: Level: intermediate
2595: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitSetOffDiagUseAmat()`, `PCFieldSplitGetDiagUseAmat()`, `PCFIELDSPLIT`
2596: @*/
2597: PetscErrorCode PCFieldSplitGetOffDiagUseAmat(PC pc, PetscBool *flg)
2598: {
2599: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2600: PetscBool isfs;
2602: PetscFunctionBegin;
2604: PetscAssertPointer(flg, 2);
2605: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2606: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2607: *flg = jac->offdiag_use_amat;
2608: PetscFunctionReturn(PETSC_SUCCESS);
2609: }
2611: /*@
2612: PCFieldSplitSetIS - Sets the exact elements for a split in a `PCFIELDSPLIT`
2614: Logically Collective
2616: Input Parameters:
2617: + pc - the preconditioner context
2618: . splitname - name of this split, if `NULL` the number of the split is used
2619: - is - the index set that defines the elements in this split
2621: Level: intermediate
2623: Notes:
2624: Use `PCFieldSplitSetFields()`, for splits defined by strided `IS` based on the matrix block size or the `is_rows[]` passed into `MATNEST`
2626: This function is called once per split (it creates a new split each time). Solve options
2627: for this split will be available under the prefix -fieldsplit_SPLITNAME_.
2629: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetBlockSize()`, `PCFieldSplitSetFields()`
2630: @*/
2631: PetscErrorCode PCFieldSplitSetIS(PC pc, const char splitname[], IS is)
2632: {
2633: PetscFunctionBegin;
2635: if (splitname) PetscAssertPointer(splitname, 2);
2637: PetscTryMethod(pc, "PCFieldSplitSetIS_C", (PC, const char[], IS), (pc, splitname, is));
2638: PetscFunctionReturn(PETSC_SUCCESS);
2639: }
2641: /*@
2642: PCFieldSplitGetIS - Retrieves the elements for a split as an `IS`
2644: Logically Collective
2646: Input Parameters:
2647: + pc - the preconditioner context
2648: - splitname - name of this split
2650: Output Parameter:
2651: . is - the index set that defines the elements in this split, or `NULL` if the split is not found
2653: Level: intermediate
2655: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetIS()`, `PCFieldSplitGetISByIndex()`
2656: @*/
2657: PetscErrorCode PCFieldSplitGetIS(PC pc, const char splitname[], IS *is)
2658: {
2659: PetscFunctionBegin;
2661: PetscAssertPointer(splitname, 2);
2662: PetscAssertPointer(is, 3);
2663: {
2664: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2665: PC_FieldSplitLink ilink = jac->head;
2666: PetscBool found;
2668: *is = NULL;
2669: while (ilink) {
2670: PetscCall(PetscStrcmp(ilink->splitname, splitname, &found));
2671: if (found) {
2672: *is = ilink->is;
2673: break;
2674: }
2675: ilink = ilink->next;
2676: }
2677: }
2678: PetscFunctionReturn(PETSC_SUCCESS);
2679: }
2681: /*@
2682: PCFieldSplitGetISByIndex - Retrieves the elements for a given split as an `IS`
2684: Logically Collective
2686: Input Parameters:
2687: + pc - the preconditioner context
2688: - index - index of this split
2690: Output Parameter:
2691: . is - the index set that defines the elements in this split
2693: Level: intermediate
2695: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitGetIS()`, `PCFieldSplitSetIS()`
2696: @*/
2697: PetscErrorCode PCFieldSplitGetISByIndex(PC pc, PetscInt index, IS *is)
2698: {
2699: PetscFunctionBegin;
2700: PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative field %" PetscInt_FMT " requested", index);
2702: PetscAssertPointer(is, 3);
2703: {
2704: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2705: PC_FieldSplitLink ilink = jac->head;
2706: PetscInt i = 0;
2707: PetscCheck(index < jac->nsplits, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " requested but only %" PetscInt_FMT " exist", index, jac->nsplits);
2709: while (i < index) {
2710: ilink = ilink->next;
2711: ++i;
2712: }
2713: PetscCall(PCFieldSplitGetIS(pc, ilink->splitname, is));
2714: }
2715: PetscFunctionReturn(PETSC_SUCCESS);
2716: }
2718: /*@
2719: PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
2720: fieldsplit preconditioner when calling `PCFieldSplitSetFields()`. If not set the matrix block size is used.
2722: Logically Collective
2724: Input Parameters:
2725: + pc - the preconditioner context
2726: - bs - the block size
2728: Level: intermediate
2730: Note:
2731: If the matrix is a `MATNEST` then the `is_rows[]` passed to `MatCreateNest()` determines the fields.
2733: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
2734: @*/
2735: PetscErrorCode PCFieldSplitSetBlockSize(PC pc, PetscInt bs)
2736: {
2737: PetscFunctionBegin;
2740: PetscTryMethod(pc, "PCFieldSplitSetBlockSize_C", (PC, PetscInt), (pc, bs));
2741: PetscFunctionReturn(PETSC_SUCCESS);
2742: }
2744: /*@
2745: PCFieldSplitGetSubKSP - Gets the `KSP` contexts for all splits
2747: Collective
2749: Input Parameter:
2750: . pc - the preconditioner context
2752: Output Parameters:
2753: + n - the number of splits
2754: - subksp - the array of `KSP` contexts
2756: Level: advanced
2758: Notes:
2759: After `PCFieldSplitGetSubKSP()` the array of `KSP`s is to be freed by the user with `PetscFree()`
2760: (not the `KSP`, just the array that contains them).
2762: You must call `PCSetUp()` before calling `PCFieldSplitGetSubKSP()`.
2764: If the fieldsplit is of type `PC_COMPOSITE_SCHUR`, it returns the `KSP` object used inside the
2765: Schur complement and the `KSP` object used to iterate over the Schur complement.
2766: To access all the `KSP` objects used in `PC_COMPOSITE_SCHUR`, use `PCFieldSplitSchurGetSubKSP()`.
2768: If the fieldsplit is of type `PC_COMPOSITE_GKB`, it returns the `KSP` object used to solve the
2769: inner linear system defined by the matrix H in each loop.
2771: Fortran Note:
2772: Call `PCFieldSplitRestoreSubKSP()` when the array of `KSP` is no longer needed
2774: Developer Notes:
2775: There should be a `PCFieldSplitRestoreSubKSP()` instead of requiring the user to call `PetscFree()`
2777: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`, `PCFieldSplitSchurGetSubKSP()`
2778: @*/
2779: PetscErrorCode PCFieldSplitGetSubKSP(PC pc, PetscInt *n, KSP *subksp[])
2780: {
2781: PetscFunctionBegin;
2783: if (n) PetscAssertPointer(n, 2);
2784: PetscUseMethod(pc, "PCFieldSplitGetSubKSP_C", (PC, PetscInt *, KSP **), (pc, n, subksp));
2785: PetscFunctionReturn(PETSC_SUCCESS);
2786: }
2788: /*@
2789: PCFieldSplitSchurGetSubKSP - Gets the `KSP` contexts used inside the Schur complement based `PCFIELDSPLIT`
2791: Collective
2793: Input Parameter:
2794: . pc - the preconditioner context
2796: Output Parameters:
2797: + n - the number of splits
2798: - subksp - the array of `KSP` contexts
2800: Level: advanced
2802: Notes:
2803: After `PCFieldSplitSchurGetSubKSP()` the array of `KSP`s is to be freed by the user with `PetscFree()`
2804: (not the `KSP` just the array that contains them).
2806: You must call `PCSetUp()` before calling `PCFieldSplitSchurGetSubKSP()`.
2808: If the fieldsplit type is of type `PC_COMPOSITE_SCHUR`, it returns (in order)
2809: + 1 - the `KSP` used for the (1,1) block
2810: . 2 - the `KSP` used for the Schur complement (not the one used for the interior Schur solver)
2811: - 3 - the `KSP` used for the (1,1) block in the upper triangular factor (if different from that of the (1,1) block).
2813: It returns a null array if the fieldsplit is not of type `PC_COMPOSITE_SCHUR`; in this case, you should use `PCFieldSplitGetSubKSP()`.
2815: Fortran Note:
2816: Call `PCFieldSplitSchurRestoreSubKSP()` when the array of `KSP` is no longer needed
2818: Developer Notes:
2819: There should be a `PCFieldSplitRestoreSubKSP()` instead of requiring the user to call `PetscFree()`
2821: Should the functionality of `PCFieldSplitSchurGetSubKSP()` and `PCFieldSplitGetSubKSP()` be merged?
2823: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`, `PCFieldSplitGetSubKSP()`
2824: @*/
2825: PetscErrorCode PCFieldSplitSchurGetSubKSP(PC pc, PetscInt *n, KSP *subksp[])
2826: {
2827: PetscFunctionBegin;
2829: if (n) PetscAssertPointer(n, 2);
2830: PetscUseMethod(pc, "PCFieldSplitSchurGetSubKSP_C", (PC, PetscInt *, KSP **), (pc, n, subksp));
2831: PetscFunctionReturn(PETSC_SUCCESS);
2832: }
2834: /*@
2835: PCFieldSplitSetSchurPre - Indicates from what operator the preconditioner is constructed for the Schur complement.
2836: The default is the A11 matrix.
2838: Collective
2840: Input Parameters:
2841: + pc - the preconditioner context
2842: . ptype - which matrix to use for preconditioning the Schur complement: `PC_FIELDSPLIT_SCHUR_PRE_A11` (default),
2843: `PC_FIELDSPLIT_SCHUR_PRE_SELF`, `PC_FIELDSPLIT_SCHUR_PRE_USER`,
2844: `PC_FIELDSPLIT_SCHUR_PRE_SELFP`, and `PC_FIELDSPLIT_SCHUR_PRE_FULL`
2845: - pre - matrix to use for preconditioning, or `NULL`
2847: Options Database Keys:
2848: + -pc_fieldsplit_schur_precondition (self|selfp|user|a11|full) - default is `a11`. See notes for meaning of various arguments
2849: - -fieldsplit_1_pc_type pctype - the preconditioner algorithm that is used to construct the preconditioner from the operator
2851: Level: intermediate
2853: Notes:
2854: If ptype is
2855: + a11 - the preconditioner for the Schur complement is generated from the block diagonal part of the preconditioner
2856: matrix associated with the Schur complement (i.e. A11), not the Schur complement matrix
2857: . self - the preconditioner for the Schur complement is generated from the symbolic representation of the Schur complement matrix:
2858: The only preconditioners that currently work with this symbolic representation matrix object are `PCLSC` and `PCHPDDM`
2859: . user - the preconditioner for the Schur complement is generated from the user provided matrix (pre argument
2860: to this function).
2861: . selfp - the preconditioning for the Schur complement is generated from an explicitly-assembled approximation $ Sp = A11 - A10 inv(diag(A00)) A01 $
2862: This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
2863: lumped before extracting the diagonal using the additional option `-fieldsplit_1_mat_schur_complement_ainv_type lump`
2864: - full - the preconditioner for the Schur complement is generated from the exact Schur complement matrix representation
2865: computed internally by `PCFIELDSPLIT` (this is expensive)
2866: useful mostly as a test that the Schur complement approach can work for your problem
2868: When solving a saddle point problem, where the A11 block is identically zero, using `a11` as the ptype only makes sense
2869: with the additional option `-fieldsplit_1_pc_type none`. Usually for saddle point problems one would use a `ptype` of `self` and
2870: `-fieldsplit_1_pc_type lsc` which uses the least squares commutator to compute a preconditioner for the Schur complement.
2872: Developer Note:
2873: The name of this function and the option `-pc_fieldsplit_schur_precondition` are inconsistent; precondition should be used everywhere.
2875: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSchurPre()`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`,
2876: `MatSchurComplementSetAinvType()`, `PCLSC`, `PCFieldSplitSetSchurFactType()`
2877: @*/
2878: PetscErrorCode PCFieldSplitSetSchurPre(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2879: {
2880: PetscFunctionBegin;
2882: PetscTryMethod(pc, "PCFieldSplitSetSchurPre_C", (PC, PCFieldSplitSchurPreType, Mat), (pc, ptype, pre));
2883: PetscFunctionReturn(PETSC_SUCCESS);
2884: }
2886: PetscErrorCode PCFieldSplitSchurPrecondition(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2887: {
2888: return PCFieldSplitSetSchurPre(pc, ptype, pre);
2889: } /* Deprecated name */
2891: /*@
2892: PCFieldSplitGetSchurPre - For Schur complement fieldsplit, determine how the Schur complement will be
2893: preconditioned. See `PCFieldSplitSetSchurPre()` for details.
2895: Logically Collective
2897: Input Parameter:
2898: . pc - the preconditioner context
2900: Output Parameters:
2901: + ptype - which matrix to use for preconditioning the Schur complement: `PC_FIELDSPLIT_SCHUR_PRE_A11`, `PC_FIELDSPLIT_SCHUR_PRE_SELF`, `PC_FIELDSPLIT_SCHUR_PRE_USER`
2902: - pre - matrix to use for preconditioning (with `PC_FIELDSPLIT_SCHUR_PRE_USER`), or `NULL`
2904: Level: intermediate
2906: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitSetSchurPre()`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`, `PCLSC`
2907: @*/
2908: PetscErrorCode PCFieldSplitGetSchurPre(PC pc, PCFieldSplitSchurPreType *ptype, Mat *pre)
2909: {
2910: PetscFunctionBegin;
2912: PetscUseMethod(pc, "PCFieldSplitGetSchurPre_C", (PC, PCFieldSplitSchurPreType *, Mat *), (pc, ptype, pre));
2913: PetscFunctionReturn(PETSC_SUCCESS);
2914: }
2916: /*@
2917: PCFieldSplitSchurGetS - extract the `MATSCHURCOMPLEMENT` object used by this `PCFIELDSPLIT` in case it needs to be configured separately
2919: Not Collective
2921: Input Parameter:
2922: . pc - the preconditioner context
2924: Output Parameter:
2925: . S - the Schur complement matrix
2927: Level: advanced
2929: Note:
2930: This matrix should not be destroyed using `MatDestroy()`; rather, use `PCFieldSplitSchurRestoreS()`.
2932: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurPre()`, `MATSCHURCOMPLEMENT`, `PCFieldSplitSchurRestoreS()`,
2933: `MatCreateSchurComplement()`, `MatSchurComplementGetKSP()`, `MatSchurComplementComputeExplicitOperator()`, `MatGetSchurComplement()`
2934: @*/
2935: PetscErrorCode PCFieldSplitSchurGetS(PC pc, Mat *S)
2936: {
2937: const char *t;
2938: PetscBool isfs;
2939: PC_FieldSplit *jac;
2941: PetscFunctionBegin;
2943: PetscCall(PetscObjectGetType((PetscObject)pc, &t));
2944: PetscCall(PetscStrcmp(t, PCFIELDSPLIT, &isfs));
2945: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PC of type PCFIELDSPLIT, got %s instead", t);
2946: jac = (PC_FieldSplit *)pc->data;
2947: PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PCFIELDSPLIT of type SCHUR, got %d instead", jac->type);
2948: if (S) *S = jac->schur;
2949: PetscFunctionReturn(PETSC_SUCCESS);
2950: }
2952: /*@
2953: PCFieldSplitSchurRestoreS - returns the `MATSCHURCOMPLEMENT` matrix used by this `PC`
2955: Not Collective
2957: Input Parameters:
2958: + pc - the preconditioner context
2959: - S - the Schur complement matrix
2961: Level: advanced
2963: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurPre()`, `MatSchurComplement`, `PCFieldSplitSchurGetS()`
2964: @*/
2965: PetscErrorCode PCFieldSplitSchurRestoreS(PC pc, Mat *S)
2966: {
2967: const char *t;
2968: PetscBool isfs;
2969: PC_FieldSplit *jac;
2971: PetscFunctionBegin;
2973: PetscCall(PetscObjectGetType((PetscObject)pc, &t));
2974: PetscCall(PetscStrcmp(t, PCFIELDSPLIT, &isfs));
2975: PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PC of type PCFIELDSPLIT, got %s instead", t);
2976: jac = (PC_FieldSplit *)pc->data;
2977: PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PCFIELDSPLIT of type SCHUR, got %d instead", jac->type);
2978: PetscCheck(S && (*S == jac->schur), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatSchurComplement restored is not the same as gotten");
2979: PetscFunctionReturn(PETSC_SUCCESS);
2980: }
2982: static PetscErrorCode PCFieldSplitSetSchurPre_FieldSplit(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2983: {
2984: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2986: PetscFunctionBegin;
2987: jac->schurpre = ptype;
2988: if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
2989: PetscCall(MatDestroy(&jac->schur_user));
2990: jac->schur_user = pre;
2991: PetscCall(PetscObjectReference((PetscObject)jac->schur_user));
2992: }
2993: PetscFunctionReturn(PETSC_SUCCESS);
2994: }
2996: static PetscErrorCode PCFieldSplitGetSchurPre_FieldSplit(PC pc, PCFieldSplitSchurPreType *ptype, Mat *pre)
2997: {
2998: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3000: PetscFunctionBegin;
3001: if (ptype) *ptype = jac->schurpre;
3002: if (pre) *pre = jac->schur_user;
3003: PetscFunctionReturn(PETSC_SUCCESS);
3004: }
3006: /*@
3007: PCFieldSplitSetSchurFactType - sets which blocks of the approximate block factorization to retain in the preconditioner {cite}`murphy2000note` and {cite}`ipsen2001note`
3009: Collective
3011: Input Parameters:
3012: + pc - the preconditioner context
3013: - ftype - which blocks of factorization to retain, `PC_FIELDSPLIT_SCHUR_FACT_FULL` is default
3015: Options Database Key:
3016: . -pc_fieldsplit_schur_fact_type (diag|lower|upper|full) - default is `full`
3018: Level: intermediate
3020: Notes:
3021: The `full` factorization is
3023: ```{math}
3024: \left(\begin{array}{cc} A & B \\
3025: C & E \\
3026: \end{array}\right) =
3027: \left(\begin{array}{cc} I & 0 \\
3028: C A^{-1} & I \\
3029: \end{array}\right)
3030: \left(\begin{array}{cc} A & 0 \\
3031: 0 & S \\
3032: \end{array}\right)
3033: \left(\begin{array}{cc} I & A^{-1}B \\
3034: 0 & I \\
3035: \end{array}\right) = L D U,
3036: ```
3038: where $ S = E - C A^{-1} B $. In practice, the full factorization is applied via block triangular solves with the grouping $L(DU)$. `upper` uses $DU$, `lower` uses $LD$,
3039: and `diag` is the diagonal part with the sign of $S$ flipped (because this makes the preconditioner positive definite for many formulations,
3040: thus allowing the use of `KSPMINRES)`. Sign flipping of $S$ can be turned off with `PCFieldSplitSetSchurScale()`.
3042: If $A$ and $S$ are solved exactly
3043: + 1 - `full` factorization is a direct solver.
3044: . 2 - The preconditioned operator with `lower` or `upper` has all eigenvalues equal to 1 and minimal polynomial of degree 2, so `KSPGMRES` converges in 2 iterations.
3045: - 3 - With `diag`, the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so `KSPGMRES` converges in at most 4 iterations.
3047: If the iteration count is very low, consider using `KSPFGMRES` or `KSPGCR` which can use one less preconditioner
3048: application in this case. Note that the preconditioned operator may be highly non-normal, so such fast convergence may not be observed in practice.
3050: For symmetric problems in which $A$ is positive definite and $S$ is negative definite, `diag` can be used with `KSPMINRES`.
3052: A flexible method like `KSPFGMRES` or `KSPGCR`, [](sec_flexibleksp), must be used if the fieldsplit preconditioner is nonlinear (e.g., a few iterations of a Krylov method is used to solve with $A$ or $S$).
3054: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurScale()`,
3055: [](sec_flexibleksp), `PCFieldSplitSetSchurPre()`
3056: @*/
3057: PetscErrorCode PCFieldSplitSetSchurFactType(PC pc, PCFieldSplitSchurFactType ftype)
3058: {
3059: PetscFunctionBegin;
3061: PetscTryMethod(pc, "PCFieldSplitSetSchurFactType_C", (PC, PCFieldSplitSchurFactType), (pc, ftype));
3062: PetscFunctionReturn(PETSC_SUCCESS);
3063: }
3065: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc, PCFieldSplitSchurFactType ftype)
3066: {
3067: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3069: PetscFunctionBegin;
3070: jac->schurfactorization = ftype;
3071: PetscFunctionReturn(PETSC_SUCCESS);
3072: }
3074: /*@
3075: PCFieldSplitSetSchurScale - Controls the sign flip of S for `PC_FIELDSPLIT_SCHUR_FACT_DIAG`.
3077: Collective
3079: Input Parameters:
3080: + pc - the preconditioner context
3081: - scale - scaling factor for the Schur complement
3083: Options Database Key:
3084: . -pc_fieldsplit_schur_scale scale - default is -1.0
3086: Level: intermediate
3088: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurFactType`, `PCFieldSplitSetSchurFactType()`
3089: @*/
3090: PetscErrorCode PCFieldSplitSetSchurScale(PC pc, PetscScalar scale)
3091: {
3092: PetscFunctionBegin;
3095: PetscTryMethod(pc, "PCFieldSplitSetSchurScale_C", (PC, PetscScalar), (pc, scale));
3096: PetscFunctionReturn(PETSC_SUCCESS);
3097: }
3099: static PetscErrorCode PCFieldSplitSetSchurScale_FieldSplit(PC pc, PetscScalar scale)
3100: {
3101: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3103: PetscFunctionBegin;
3104: jac->schurscale = scale;
3105: PetscFunctionReturn(PETSC_SUCCESS);
3106: }
3108: /*@
3109: PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement
3111: Collective
3113: Input Parameter:
3114: . pc - the preconditioner context
3116: Output Parameters:
3117: + A00 - the (0,0) block
3118: . A01 - the (0,1) block
3119: . A10 - the (1,0) block
3120: - A11 - the (1,1) block
3122: Level: advanced
3124: Note:
3125: Use `NULL` for any unneeded output arguments
3127: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `MatSchurComplementGetSubMatrices()`, `MatSchurComplementSetSubMatrices()`
3128: @*/
3129: PetscErrorCode PCFieldSplitGetSchurBlocks(PC pc, Mat *A00, Mat *A01, Mat *A10, Mat *A11)
3130: {
3131: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3133: PetscFunctionBegin;
3135: PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
3136: if (A00) *A00 = jac->pmat[0];
3137: if (A01) *A01 = jac->B;
3138: if (A10) *A10 = jac->C;
3139: if (A11) *A11 = jac->pmat[1];
3140: PetscFunctionReturn(PETSC_SUCCESS);
3141: }
3143: /*@
3144: PCFieldSplitSetGKBTol - Sets the solver tolerance for the generalized Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`
3146: Collective
3148: Input Parameters:
3149: + pc - the preconditioner context
3150: - tolerance - the solver tolerance
3152: Options Database Key:
3153: . -pc_fieldsplit_gkb_tol tolerance - default is 1e-5
3155: Level: intermediate
3157: Note:
3158: The generalized GKB algorithm {cite}`arioli2013` uses a lower bound estimate of the error in energy norm as stopping criterion.
3159: It stops once the lower bound estimate undershoots the required solver tolerance. Although the actual error might be bigger than
3160: this estimate, the stopping criterion is satisfactory in practical cases.
3162: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBNu()`, `PCFieldSplitSetGKBMaxit()`
3163: @*/
3164: PetscErrorCode PCFieldSplitSetGKBTol(PC pc, PetscReal tolerance)
3165: {
3166: PetscFunctionBegin;
3169: PetscTryMethod(pc, "PCFieldSplitSetGKBTol_C", (PC, PetscReal), (pc, tolerance));
3170: PetscFunctionReturn(PETSC_SUCCESS);
3171: }
3173: static PetscErrorCode PCFieldSplitSetGKBTol_FieldSplit(PC pc, PetscReal tolerance)
3174: {
3175: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3177: PetscFunctionBegin;
3178: jac->gkbtol = tolerance;
3179: PetscFunctionReturn(PETSC_SUCCESS);
3180: }
3182: /*@
3183: PCFieldSplitSetGKBMaxit - Sets the maximum number of iterations for the generalized Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`
3185: Collective
3187: Input Parameters:
3188: + pc - the preconditioner context
3189: - maxit - the maximum number of iterations
3191: Options Database Key:
3192: . -pc_fieldsplit_gkb_maxit maxit - default is 100
3194: Level: intermediate
3196: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBNu()`
3197: @*/
3198: PetscErrorCode PCFieldSplitSetGKBMaxit(PC pc, PetscInt maxit)
3199: {
3200: PetscFunctionBegin;
3203: PetscTryMethod(pc, "PCFieldSplitSetGKBMaxit_C", (PC, PetscInt), (pc, maxit));
3204: PetscFunctionReturn(PETSC_SUCCESS);
3205: }
3207: static PetscErrorCode PCFieldSplitSetGKBMaxit_FieldSplit(PC pc, PetscInt maxit)
3208: {
3209: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3211: PetscFunctionBegin;
3212: jac->gkbmaxit = maxit;
3213: PetscFunctionReturn(PETSC_SUCCESS);
3214: }
3216: /*@
3217: PCFieldSplitSetGKBDelay - Sets the delay in the lower bound error estimate in the generalized Golub-Kahan bidiagonalization {cite}`arioli2013` in `PCFIELDSPLIT`
3218: preconditioner.
3220: Collective
3222: Input Parameters:
3223: + pc - the preconditioner context
3224: - delay - the delay window in the lower bound estimate
3226: Options Database Key:
3227: . -pc_fieldsplit_gkb_delay delay - default is 5
3229: Level: intermediate
3231: Notes:
3232: The algorithm uses a lower bound estimate of the error in energy norm as stopping criterion. The lower bound of the error $ ||u-u^k||_H $
3233: is expressed as a truncated sum. The error at iteration k can only be measured at iteration (k + `delay`), and thus the algorithm needs
3234: at least (`delay` + 1) iterations to stop.
3236: For more details on the generalized Golub-Kahan bidiagonalization method and its lower bound stopping criterion, please refer to {cite}`arioli2013`
3238: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBNu()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBMaxit()`
3239: @*/
3240: PetscErrorCode PCFieldSplitSetGKBDelay(PC pc, PetscInt delay)
3241: {
3242: PetscFunctionBegin;
3245: PetscTryMethod(pc, "PCFieldSplitSetGKBDelay_C", (PC, PetscInt), (pc, delay));
3246: PetscFunctionReturn(PETSC_SUCCESS);
3247: }
3249: static PetscErrorCode PCFieldSplitSetGKBDelay_FieldSplit(PC pc, PetscInt delay)
3250: {
3251: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3253: PetscFunctionBegin;
3254: jac->gkbdelay = delay;
3255: PetscFunctionReturn(PETSC_SUCCESS);
3256: }
3258: /*@
3259: PCFieldSplitSetGKBNu - Sets the scalar value nu >= 0 in the transformation H = A00 + nu*A01*A01' of the (1,1) block in the
3260: Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`
3262: Collective
3264: Input Parameters:
3265: + pc - the preconditioner context
3266: - nu - the shift parameter
3268: Options Database Key:
3269: . -pc_fieldsplit_gkb_nu nu - default is 1
3271: Level: intermediate
3273: Notes:
3274: This shift is in general done to obtain better convergence properties for the outer loop of the algorithm. This is often achieved by choosing `nu` sufficiently large. However,
3275: if `nu` is chosen too large, the matrix H might be badly conditioned and the solution of the linear system $Hx = b$ in the inner loop becomes difficult. It is therefore
3276: necessary to find a good balance in between the convergence of the inner and outer loop.
3278: For `nu` = 0, no shift is done. In this case A00 has to be positive definite. The matrix N in {cite}`arioli2013` is then chosen as identity.
3280: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBMaxit()`
3281: @*/
3282: PetscErrorCode PCFieldSplitSetGKBNu(PC pc, PetscReal nu)
3283: {
3284: PetscFunctionBegin;
3287: PetscTryMethod(pc, "PCFieldSplitSetGKBNu_C", (PC, PetscReal), (pc, nu));
3288: PetscFunctionReturn(PETSC_SUCCESS);
3289: }
3291: static PetscErrorCode PCFieldSplitSetGKBNu_FieldSplit(PC pc, PetscReal nu)
3292: {
3293: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3295: PetscFunctionBegin;
3296: jac->gkbnu = nu;
3297: PetscFunctionReturn(PETSC_SUCCESS);
3298: }
3300: static PetscErrorCode PCFieldSplitSetType_FieldSplit(PC pc, PCCompositeType type)
3301: {
3302: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3304: PetscFunctionBegin;
3305: jac->type = type;
3306: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", NULL));
3307: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", NULL));
3308: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", NULL));
3309: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", NULL));
3310: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", NULL));
3311: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", NULL));
3312: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", NULL));
3313: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", NULL));
3314: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", NULL));
3316: if (type == PC_COMPOSITE_SCHUR) {
3317: pc->ops->apply = PCApply_FieldSplit_Schur;
3318: pc->ops->applytranspose = PCApplyTranspose_FieldSplit_Schur;
3319: pc->ops->matapply = PCMatApply_FieldSplit_Schur;
3320: pc->ops->view = PCView_FieldSplit_Schur;
3321: pc->ops->setuponblocks = PCSetUpOnBlocks_FieldSplit_Schur;
3323: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit_Schur));
3324: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", PCFieldSplitSetSchurPre_FieldSplit));
3325: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", PCFieldSplitGetSchurPre_FieldSplit));
3326: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", PCFieldSplitSetSchurFactType_FieldSplit));
3327: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", PCFieldSplitSetSchurScale_FieldSplit));
3328: } else if (type == PC_COMPOSITE_GKB) {
3329: pc->ops->apply = PCApply_FieldSplit_GKB;
3330: pc->ops->applytranspose = NULL;
3331: pc->ops->matapply = NULL;
3332: pc->ops->view = PCView_FieldSplit_GKB;
3333: pc->ops->setuponblocks = PCSetUpOnBlocks_FieldSplit_GKB;
3335: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit));
3336: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", PCFieldSplitSetGKBTol_FieldSplit));
3337: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", PCFieldSplitSetGKBMaxit_FieldSplit));
3338: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", PCFieldSplitSetGKBNu_FieldSplit));
3339: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", PCFieldSplitSetGKBDelay_FieldSplit));
3340: } else {
3341: pc->ops->apply = PCApply_FieldSplit;
3342: pc->ops->applytranspose = PCApplyTranspose_FieldSplit;
3343: pc->ops->matapply = PCMatApply_FieldSplit;
3344: pc->ops->view = PCView_FieldSplit;
3345: pc->ops->setuponblocks = PCSetUpOnBlocks_FieldSplit;
3347: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit));
3348: }
3349: PetscFunctionReturn(PETSC_SUCCESS);
3350: }
3352: static PetscErrorCode PCFieldSplitSetBlockSize_FieldSplit(PC pc, PetscInt bs)
3353: {
3354: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3356: PetscFunctionBegin;
3357: PetscCheck(bs >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Blocksize must be positive, you gave %" PetscInt_FMT, bs);
3358: PetscCheck(jac->bs <= 0 || jac->bs == bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Cannot change fieldsplit blocksize from %" PetscInt_FMT " to %" PetscInt_FMT " after it has been set", jac->bs, bs);
3359: jac->bs = bs;
3360: PetscFunctionReturn(PETSC_SUCCESS);
3361: }
3363: static PetscErrorCode PCSetCoordinates_FieldSplit(PC pc, PetscInt dim, PetscInt nloc, PetscReal coords[])
3364: {
3365: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3366: PC_FieldSplitLink ilink_current = jac->head;
3367: IS is_owned;
3369: PetscFunctionBegin;
3370: jac->coordinates_set = PETSC_TRUE; // Internal flag
3371: PetscCall(MatGetOwnershipIS(pc->mat, &is_owned, NULL));
3373: while (ilink_current) {
3374: // For each IS, embed it to get local coords indces
3375: IS is_coords;
3376: PetscInt ndofs_block;
3377: const PetscInt *block_dofs_enumeration; // Numbering of the dofs relevant to the current block
3379: // Setting drop to true for safety. It should make no difference.
3380: PetscCall(ISEmbed(ilink_current->is, is_owned, PETSC_TRUE, &is_coords));
3381: PetscCall(ISGetLocalSize(is_coords, &ndofs_block));
3382: PetscCall(ISGetIndices(is_coords, &block_dofs_enumeration));
3384: // Allocate coordinates vector and set it directly
3385: PetscCall(PetscMalloc1(ndofs_block * dim, &ilink_current->coords));
3386: for (PetscInt dof = 0; dof < ndofs_block; ++dof) {
3387: for (PetscInt d = 0; d < dim; ++d) ilink_current->coords[dim * dof + d] = coords[dim * block_dofs_enumeration[dof] + d];
3388: }
3389: ilink_current->dim = dim;
3390: ilink_current->ndofs = ndofs_block;
3391: PetscCall(ISRestoreIndices(is_coords, &block_dofs_enumeration));
3392: PetscCall(ISDestroy(&is_coords));
3393: ilink_current = ilink_current->next;
3394: }
3395: PetscCall(ISDestroy(&is_owned));
3396: PetscFunctionReturn(PETSC_SUCCESS);
3397: }
3399: /*@
3400: PCFieldSplitSetType - Sets the type, `PCCompositeType`, of a `PCFIELDSPLIT`
3402: Collective
3404: Input Parameters:
3405: + pc - the preconditioner context
3406: - type - `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE` (default), `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`,
3407: `PC_COMPOSITE_GKB`
3409: Options Database Key:
3410: . -pc_fieldsplit_type (multiplicative|additive|symmetric_multiplicative|special|schur) - Sets fieldsplit preconditioner type
3412: Level: intermediate
3414: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCCompositeType`, `PCCompositeGetType()`, `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,
3415: `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`, `PCFieldSplitSetSchurFactType()`
3416: @*/
3417: PetscErrorCode PCFieldSplitSetType(PC pc, PCCompositeType type)
3418: {
3419: PetscFunctionBegin;
3421: PetscTryMethod(pc, "PCFieldSplitSetType_C", (PC, PCCompositeType), (pc, type));
3422: PetscFunctionReturn(PETSC_SUCCESS);
3423: }
3425: /*@
3426: PCFieldSplitGetType - Gets the type, `PCCompositeType`, of a `PCFIELDSPLIT`
3428: Not collective
3430: Input Parameter:
3431: . pc - the preconditioner context
3433: Output Parameter:
3434: . type - `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE` (default), `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`
3436: Level: intermediate
3438: .seealso: [](sec_block_matrices), `PC`, `PCCompositeSetType()`, `PCFIELDSPLIT`, `PCCompositeType`, `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,
3439: `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`
3440: @*/
3441: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
3442: {
3443: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3445: PetscFunctionBegin;
3447: PetscAssertPointer(type, 2);
3448: *type = jac->type;
3449: PetscFunctionReturn(PETSC_SUCCESS);
3450: }
3452: /*@
3453: PCFieldSplitSetDMSplits - Flags whether `DMCreateFieldDecomposition()` should be used to define the splits in a `PCFIELDSPLIT`, whenever possible.
3455: Logically Collective
3457: Input Parameters:
3458: + pc - the preconditioner context
3459: - flg - boolean indicating whether to use field splits defined by the `DM`
3461: Options Database Key:
3462: . -pc_fieldsplit_dm_splits (true|false) - use the field splits defined by the `DM`
3464: Level: intermediate
3466: Developer Note:
3467: The name should be `PCFieldSplitSetUseDMSplits()`, similar change to options database
3469: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitGetDMSplits()`, `DMCreateFieldDecomposition()`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
3470: @*/
3471: PetscErrorCode PCFieldSplitSetDMSplits(PC pc, PetscBool flg)
3472: {
3473: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3474: PetscBool isfs;
3476: PetscFunctionBegin;
3479: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
3480: if (isfs) jac->dm_splits = flg;
3481: PetscFunctionReturn(PETSC_SUCCESS);
3482: }
3484: /*@
3485: PCFieldSplitGetDMSplits - Returns flag indicating whether `DMCreateFieldDecomposition()` should be used to define the splits in a `PCFIELDSPLIT`, whenever possible.
3487: Logically Collective
3489: Input Parameter:
3490: . pc - the preconditioner context
3492: Output Parameter:
3493: . flg - boolean indicating whether to use field splits defined by the `DM`
3495: Level: intermediate
3497: Developer Note:
3498: The name should be `PCFieldSplitGetUseDMSplits()`
3500: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetDMSplits()`, `DMCreateFieldDecomposition()`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
3501: @*/
3502: PetscErrorCode PCFieldSplitGetDMSplits(PC pc, PetscBool *flg)
3503: {
3504: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3505: PetscBool isfs;
3507: PetscFunctionBegin;
3509: PetscAssertPointer(flg, 2);
3510: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
3511: if (isfs) {
3512: if (flg) *flg = jac->dm_splits;
3513: }
3514: PetscFunctionReturn(PETSC_SUCCESS);
3515: }
3517: /*@
3518: PCFieldSplitGetDetectSaddlePoint - Returns flag indicating whether `PCFIELDSPLIT` will attempt to automatically determine fields based on zero diagonal entries.
3520: Logically Collective
3522: Input Parameter:
3523: . pc - the preconditioner context
3525: Output Parameter:
3526: . flg - boolean indicating whether to detect fields or not
3528: Level: intermediate
3530: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetDetectSaddlePoint()`
3531: @*/
3532: PetscErrorCode PCFieldSplitGetDetectSaddlePoint(PC pc, PetscBool *flg)
3533: {
3534: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3536: PetscFunctionBegin;
3537: *flg = jac->detect;
3538: PetscFunctionReturn(PETSC_SUCCESS);
3539: }
3541: /*@
3542: PCFieldSplitSetDetectSaddlePoint - Sets flag indicating whether `PCFIELDSPLIT` will attempt to automatically determine fields based on zero diagonal entries.
3544: Logically Collective
3546: Input Parameter:
3547: . pc - the preconditioner context
3549: Output Parameter:
3550: . flg - boolean indicating whether to detect fields or not
3552: Options Database Key:
3553: . -pc_fieldsplit_detect_saddle_point (true|false) - detect and use the saddle point
3555: Level: intermediate
3557: Note:
3558: Also sets the split type to `PC_COMPOSITE_SCHUR` (see `PCFieldSplitSetType()`) and the Schur preconditioner type to `PC_FIELDSPLIT_SCHUR_PRE_SELF` (see `PCFieldSplitSetSchurPre()`).
3560: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitGetDetectSaddlePoint()`, `PCFieldSplitSetType()`, `PCFieldSplitSetSchurPre()`, `PC_FIELDSPLIT_SCHUR_PRE_SELF`
3561: @*/
3562: PetscErrorCode PCFieldSplitSetDetectSaddlePoint(PC pc, PetscBool flg)
3563: {
3564: PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3566: PetscFunctionBegin;
3567: jac->detect = flg;
3568: if (jac->detect) {
3569: PetscCall(PCFieldSplitSetType(pc, PC_COMPOSITE_SCHUR));
3570: PetscCall(PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_SELF, NULL));
3571: }
3572: PetscFunctionReturn(PETSC_SUCCESS);
3573: }
3575: /*MC
3576: PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
3577: collections of variables (that may overlap) called fields or splits. Each field often represents a different continuum variable
3578: represented on a grid, such as velocity, pressure, or temperature.
3579: In the literature these are sometimes called block preconditioners; but should not be confused with `PCBJACOBI`.
3580: See [the users manual section on "Solving Block Matrices"](sec_block_matrices) for more details.
3582: Options Database Keys:
3583: + -pc_fieldsplit_%d_fields a,b,... - indicates the fields to be used in the `%d`'th split
3584: . -pc_fieldsplit_default - automatically add any fields to additional splits that have not
3585: been supplied explicitly by `-pc_fieldsplit_%d_fields`
3586: . -pc_fieldsplit_block_size bs - size of block that defines fields (i.e. there are bs fields)
3587: when the matrix is not of `MatType` `MATNEST`
3588: . -pc_fieldsplit_type (additive|multiplicative|symmetric_multiplicative|schur|gkb) - type of relaxation or factorization splitting
3589: . -pc_fieldsplit_schur_precondition (self|selfp|user|a11|full) - default is `a11`; see `PCFieldSplitSetSchurPre()`
3590: . -pc_fieldsplit_schur_fact_type (diag|lower|upper|full) - set factorization type when using `-pc_fieldsplit_type schur`;
3591: see `PCFieldSplitSetSchurFactType()`
3592: . -pc_fieldsplit_dm_splits (true|false) (default is true) - Whether to use `DMCreateFieldDecomposition()` for splits
3593: - -pc_fieldsplit_detect_saddle_point (true|false) - automatically finds rows with zero diagonal and uses Schur complement with no preconditioner as the solver
3595: Options prefixes for inner solvers when using the Schur complement preconditioner are `-fieldsplit_0_` and `-fieldsplit_1_` .
3596: The options prefix for the inner solver when using the Golub-Kahan biadiagonalization preconditioner is `-fieldsplit_0_`
3597: For all other solvers they are `-fieldsplit_%d_` for the `%d`'th field; use `-fieldsplit_` for all fields.
3599: To set options on the solvers for all blocks, prepend `-fieldsplit_` to all the `PC`
3600: options database keys. For example, `-fieldsplit_pc_type ilu` `-fieldsplit_pc_factor_levels 1`.
3602: To set the options on the solvers separate for each block call `PCFieldSplitGetSubKSP()`
3603: and set the options directly on the resulting `KSP` object
3605: Level: intermediate
3607: Notes:
3608: Use `PCFieldSplitSetFields()` to set splits defined by "strided" entries or with a `MATNEST` and `PCFieldSplitSetIS()`
3609: to define a split by an arbitrary collection of entries.
3611: If no splits are set, the default is used. If a `DM` is associated with the `PC` and it supports
3612: `DMCreateFieldDecomposition()`, then that is used for the default. Otherwise if the matrix is not `MATNEST`, the splits are defined by entries strided by bs,
3613: beginning at 0 then 1, etc to bs-1. The block size can be set with `PCFieldSplitSetBlockSize()`,
3614: if this is not called the block size defaults to the blocksize of the second matrix passed
3615: to `KSPSetOperators()`/`PCSetOperators()`.
3617: For the Schur complement preconditioner if
3618: ```{math}
3619: J = \left[\begin{array}{cc} A_{00} & A_{01} \\ A_{10} & A_{11} \end{array}\right]
3620: ```
3622: the preconditioner using `full` factorization is logically
3623: ```{math}
3624: \left[\begin{array}{cc} I & -\text{ksp}(A_{00}) A_{01} \\ 0 & I \end{array}\right] \left[\begin{array}{cc} \text{ksp}(A_{00}) & 0 \\ 0 & \text{ksp}(S) \end{array}\right] \left[\begin{array}{cc} I & 0 \\ -A_{10} \text{ksp}(A_{00}) & I \end{array}\right]
3625: ```
3626: where the action of $\text{ksp}(A_{00})$ is applied using the `KSP` solver with prefix `-fieldsplit_0_`. $S$ is the Schur complement
3627: ```{math}
3628: S = A_{11} - A_{10} \text{ksp}(A_{00}) A_{01}
3629: ```
3630: which is usually dense and not stored explicitly. The action of $\text{ksp}(S)$ is computed using the `KSP` solver with prefix `-fieldsplit_splitname_` (where `splitname`
3631: was given in providing the SECOND split or 1 if not given). Accordingly, if using `PCFieldSplitGetSubKSP()`, the array of sub-`KSP` contexts will hold two `KSP`s: at its
3632: 0th index, the `KSP` associated with `-fieldsplit_0_`, and at its 1st index, the `KSP` corresponding to `-fieldsplit_1_`.
3633: By default, $A_{11}$ is used to construct a preconditioner for $S$, use `PCFieldSplitSetSchurPre()` for all the possible ways to construct the preconditioner for $S$.
3635: The factorization type is set using `-pc_fieldsplit_schur_fact_type <diag, lower, upper, full>`. `full` is shown above,
3636: `diag` gives
3637: ```{math}
3638: \left[\begin{array}{cc} \text{ksp}(A_{00}) & 0 \\ 0 & -\text{ksp}(S) \end{array}\right]
3639: ```
3640: Note that, slightly counter intuitively, there is a negative in front of the $\text{ksp}(S)$ so that the preconditioner is positive definite. For SPD matrices $J$, the sign flip
3641: can be turned off with `PCFieldSplitSetSchurScale()` or by command line `-pc_fieldsplit_schur_scale 1.0`. The `lower` factorization is the inverse of
3642: ```{math}
3643: \left[\begin{array}{cc} A_{00} & 0 \\ A_{10} & S \end{array}\right]
3644: ```
3645: where the inverses of $A_{00}$ and $S$ are applied using `KSP`s. The upper factorization is the inverse of
3646: ```{math}
3647: \left[\begin{array}{cc} A_{00} & A_{01} \\ 0 & S \end{array}\right]
3648: ```
3649: where again the inverses of $A_{00}$ and $S$ are applied using `KSP`s.
3651: If only one set of indices (one `IS`) is provided with `PCFieldSplitSetIS()` then the complement of that `IS`
3652: is used automatically for a second submatrix.
3654: The fieldsplit preconditioner cannot currently be used with the `MATBAIJ` or `MATSBAIJ` data formats if the blocksize is larger than 1.
3655: Generally it should be used with the `MATAIJ` or `MATNEST` `MatType`
3657: The forms of these preconditioners are closely related, if not identical, to forms derived as "Distributive Iterations", see,
3658: for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling {cite}`wesseling2009`.
3659: One can also use `PCFIELDSPLIT` inside a smoother resulting in "Distributive Smoothers".
3661: See "A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations" {cite}`elman2008tcp`.
3663: The Constrained Pressure Preconditioner (CPR) can be implemented using `PCCOMPOSITE` with `PCGALERKIN`. CPR first solves an $R A P$ subsystem, updates the
3664: residual on all variables (`PCCompositeSetType(pc,PC_COMPOSITE_MULTIPLICATIVE)`), and then applies a simple ILU like preconditioner on all the variables.
3666: The generalized Golub-Kahan bidiagonalization preconditioner (GKB) can be applied to symmetric $2 \times 2$ block matrices of the shape
3667: ```{math}
3668: \left[\begin{array}{cc} A_{00} & A_{01} \\ A_{01}' & 0 \end{array}\right]
3669: ```
3670: with $A_{00}$ positive semi-definite. The implementation follows {cite}`arioli2013`. Therein, we choose $N := 1/\nu * I$ and the $(1,1)$-block of the matrix is modified to $H = _{A00} + \nu*A_{01}*A_{01}'$.
3671: A linear system $Hx = b$ has to be solved in each iteration of the GKB algorithm. This solver is chosen with the option prefix `-fieldsplit_0_`.
3673: Some `PCFIELDSPLIT` variants are called physics-based preconditioners, since the preconditioner takes into account the underlying physics of the
3674: problem. But this nomenclature is not well-defined.
3676: Developer Note:
3677: The Schur complement functionality of `PCFIELDSPLIT` should likely be factored into its own `PC` thus simplifying the implementation of the preconditioners and their
3678: user API.
3680: .seealso: [](sec_block_matrices), `PC`, `PCCreate()`, `PCSetType()`, `PCType`, `PCLSC`,
3681: `PCFieldSplitGetSubKSP()`, `PCFieldSplitSchurGetSubKSP()`, `PCFieldSplitSetFields()`,
3682: `PCFieldSplitSetType()`, `PCFieldSplitSetIS()`, `PCFieldSplitSetSchurPre()`, `PCFieldSplitSetSchurFactType()`,
3683: `MatSchurComplementSetAinvType()`, `PCFieldSplitSetSchurScale()`, `PCFieldSplitSetDetectSaddlePoint()`
3684: M*/
3686: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
3687: {
3688: PC_FieldSplit *jac;
3690: PetscFunctionBegin;
3691: PetscCall(PetscNew(&jac));
3693: jac->bs = -1;
3694: jac->type = PC_COMPOSITE_MULTIPLICATIVE;
3695: jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
3696: jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
3697: jac->schurscale = -1.0;
3698: jac->dm_splits = PETSC_TRUE;
3699: jac->gkbtol = 1e-5;
3700: jac->gkbdelay = 5;
3701: jac->gkbnu = 1;
3702: jac->gkbmaxit = 100;
3704: pc->data = (void *)jac;
3706: pc->ops->setup = PCSetUp_FieldSplit;
3707: pc->ops->reset = PCReset_FieldSplit;
3708: pc->ops->destroy = PCDestroy_FieldSplit;
3709: pc->ops->setfromoptions = PCSetFromOptions_FieldSplit;
3710: pc->ops->applyrichardson = NULL;
3712: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSchurGetSubKSP_C", PCFieldSplitSchurGetSubKSP_FieldSplit));
3713: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetFields_C", PCFieldSplitSetFields_FieldSplit));
3714: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetIS_C", PCFieldSplitSetIS_FieldSplit));
3715: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetType_C", PCFieldSplitSetType_FieldSplit));
3716: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetBlockSize_C", PCFieldSplitSetBlockSize_FieldSplit));
3717: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitRestrictIS_C", PCFieldSplitRestrictIS_FieldSplit));
3718: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_FieldSplit));
3720: /* Initialize function pointers */
3721: PetscCall(PCFieldSplitSetType(pc, jac->type));
3722: PetscFunctionReturn(PETSC_SUCCESS);
3723: }