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