xref: /petsc/src/snes/impls/multiblock/multiblock.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
1af0996ceSBarry Smith #include <petsc/private/snesimpl.h> /*I "petscsnes.h" I*/
2b665c654SMatthew G Knepley #include <petscdmcomposite.h>
3c7543cceSMatthew G Knepley 
4c7543cceSMatthew G Knepley typedef struct _BlockDesc *BlockDesc;
5c7543cceSMatthew G Knepley struct _BlockDesc {
6c7543cceSMatthew G Knepley   char      *name;    /* Block name */
7c7543cceSMatthew G Knepley   PetscInt   nfields; /* If block is defined on a DA, the number of DA fields */
8c7543cceSMatthew G Knepley   PetscInt  *fields;  /* If block is defined on a DA, the list of DA fields */
9c7543cceSMatthew G Knepley   IS         is;      /* Index sets defining the block */
10c7543cceSMatthew G Knepley   VecScatter sctx;    /* Scatter mapping global Vec to blockVec */
11c7543cceSMatthew G Knepley   SNES       snes;    /* Solver for this block */
12c7543cceSMatthew G Knepley   Vec        x;
13c7543cceSMatthew G Knepley   BlockDesc  next, previous;
14c7543cceSMatthew G Knepley };
15c7543cceSMatthew G Knepley 
16c7543cceSMatthew G Knepley typedef struct {
17c7543cceSMatthew G Knepley   PetscBool       issetup;       /* Flag is true after the all ISs and operators have been defined */
18c7543cceSMatthew G Knepley   PetscBool       defined;       /* Flag is true after the blocks have been defined, to prevent more blocks from being added */
19c7543cceSMatthew G Knepley   PetscBool       defaultblocks; /* Flag is true for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
20c7543cceSMatthew G Knepley   PetscInt        numBlocks;     /* Number of blocks (can be fields, domains, etc.) */
21c7543cceSMatthew G Knepley   PetscInt        bs;            /* Block size for IS, Vec and Mat structures */
22c7543cceSMatthew G Knepley   PCCompositeType type;          /* Solver combination method (additive, multiplicative, etc.) */
23c7543cceSMatthew G Knepley   BlockDesc       blocks;        /* Linked list of block descriptors */
24c7543cceSMatthew G Knepley } SNES_Multiblock;
25c7543cceSMatthew G Knepley 
269371c9d4SSatish Balay PetscErrorCode SNESReset_Multiblock(SNES snes) {
27c7543cceSMatthew G Knepley   SNES_Multiblock *mb     = (SNES_Multiblock *)snes->data;
28c7543cceSMatthew G Knepley   BlockDesc        blocks = mb->blocks, next;
29c7543cceSMatthew G Knepley 
30c7543cceSMatthew G Knepley   PetscFunctionBegin;
31c7543cceSMatthew G Knepley   while (blocks) {
329566063dSJacob Faibussowitsch     PetscCall(SNESReset(blocks->snes));
330c74a584SJed Brown #if 0
349566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&blocks->x));
350c74a584SJed Brown #endif
369566063dSJacob Faibussowitsch     PetscCall(VecScatterDestroy(&blocks->sctx));
379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&blocks->is));
38c7543cceSMatthew G Knepley     next   = blocks->next;
39c7543cceSMatthew G Knepley     blocks = next;
40c7543cceSMatthew G Knepley   }
41c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
42c7543cceSMatthew G Knepley }
43c7543cceSMatthew G Knepley 
44c7543cceSMatthew G Knepley /*
45c7543cceSMatthew G Knepley   SNESDestroy_Multiblock - Destroys the private SNES_Multiblock context that was created with SNESCreate_Multiblock().
46c7543cceSMatthew G Knepley 
47c7543cceSMatthew G Knepley   Input Parameter:
48c7543cceSMatthew G Knepley . snes - the SNES context
49c7543cceSMatthew G Knepley 
50c7543cceSMatthew G Knepley   Application Interface Routine: SNESDestroy()
51c7543cceSMatthew G Knepley */
529371c9d4SSatish Balay PetscErrorCode SNESDestroy_Multiblock(SNES snes) {
53c7543cceSMatthew G Knepley   SNES_Multiblock *mb     = (SNES_Multiblock *)snes->data;
54c7543cceSMatthew G Knepley   BlockDesc        blocks = mb->blocks, next;
55c7543cceSMatthew G Knepley 
56c7543cceSMatthew G Knepley   PetscFunctionBegin;
579566063dSJacob Faibussowitsch   PetscCall(SNESReset_Multiblock(snes));
58c7543cceSMatthew G Knepley   while (blocks) {
59c7543cceSMatthew G Knepley     next = blocks->next;
609566063dSJacob Faibussowitsch     PetscCall(SNESDestroy(&blocks->snes));
619566063dSJacob Faibussowitsch     PetscCall(PetscFree(blocks->name));
629566063dSJacob Faibussowitsch     PetscCall(PetscFree(blocks->fields));
639566063dSJacob Faibussowitsch     PetscCall(PetscFree(blocks));
64c7543cceSMatthew G Knepley     blocks = next;
65c7543cceSMatthew G Knepley   }
669566063dSJacob Faibussowitsch   PetscCall(PetscFree(snes->data));
67c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
68c7543cceSMatthew G Knepley }
69c7543cceSMatthew G Knepley 
70c7543cceSMatthew G Knepley /* Precondition: blocksize is set to a meaningful value */
719371c9d4SSatish Balay static PetscErrorCode SNESMultiblockSetFieldsRuntime_Private(SNES snes) {
72c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
73c7543cceSMatthew G Knepley   PetscInt        *ifields;
74c7543cceSMatthew G Knepley   PetscInt         i, nfields;
75c7543cceSMatthew G Knepley   PetscBool        flg = PETSC_TRUE;
76c7543cceSMatthew G Knepley   char             optionname[128], name[8];
77c7543cceSMatthew G Knepley 
78c7543cceSMatthew G Knepley   PetscFunctionBegin;
799566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(mb->bs, &ifields));
80c7543cceSMatthew G Knepley   for (i = 0;; ++i) {
8163a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, sizeof(name), "%" PetscInt_FMT, i));
8263a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(optionname, sizeof(optionname), "-snes_multiblock_%" PetscInt_FMT "_fields", i));
83c7543cceSMatthew G Knepley     nfields = mb->bs;
849566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)snes)->prefix, optionname, ifields, &nfields, &flg));
85c7543cceSMatthew G Knepley     if (!flg) break;
8628b400f6SJacob Faibussowitsch     PetscCheck(nfields, PETSC_COMM_SELF, PETSC_ERR_USER, "Cannot list zero fields");
879566063dSJacob Faibussowitsch     PetscCall(SNESMultiblockSetFields(snes, name, nfields, ifields));
88c7543cceSMatthew G Knepley   }
89c7543cceSMatthew G Knepley   if (i > 0) {
90c7543cceSMatthew G Knepley     /* Makes command-line setting of blocks take precedence over setting them in code.
91c7543cceSMatthew G Knepley        Otherwise subsequent calls to SNESMultiblockSetIS() or SNESMultiblockSetFields() would
92c7543cceSMatthew G Knepley        create new blocks, which would probably not be what the user wanted. */
93c7543cceSMatthew G Knepley     mb->defined = PETSC_TRUE;
94c7543cceSMatthew G Knepley   }
959566063dSJacob Faibussowitsch   PetscCall(PetscFree(ifields));
96c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
97c7543cceSMatthew G Knepley }
98c7543cceSMatthew G Knepley 
999371c9d4SSatish Balay static PetscErrorCode SNESMultiblockSetDefaults(SNES snes) {
100c7543cceSMatthew G Knepley   SNES_Multiblock *mb     = (SNES_Multiblock *)snes->data;
101c7543cceSMatthew G Knepley   BlockDesc        blocks = mb->blocks;
102c7543cceSMatthew G Knepley   PetscInt         i;
103c7543cceSMatthew G Knepley 
104c7543cceSMatthew G Knepley   PetscFunctionBegin;
105c7543cceSMatthew G Knepley   if (!blocks) {
106c7543cceSMatthew G Knepley     if (snes->dm) {
107c7543cceSMatthew G Knepley       PetscBool dmcomposite;
108c7543cceSMatthew G Knepley 
1099566063dSJacob Faibussowitsch       PetscCall(PetscObjectTypeCompare((PetscObject)snes->dm, DMCOMPOSITE, &dmcomposite));
110c7543cceSMatthew G Knepley       if (dmcomposite) {
111c7543cceSMatthew G Knepley         PetscInt nDM;
112c7543cceSMatthew G Knepley         IS      *fields;
113c7543cceSMatthew G Knepley 
1149566063dSJacob Faibussowitsch         PetscCall(PetscInfo(snes, "Setting up physics based multiblock solver using the embedded DM\n"));
1159566063dSJacob Faibussowitsch         PetscCall(DMCompositeGetNumberDM(snes->dm, &nDM));
1169566063dSJacob Faibussowitsch         PetscCall(DMCompositeGetGlobalISs(snes->dm, &fields));
117c7543cceSMatthew G Knepley         for (i = 0; i < nDM; ++i) {
118c7543cceSMatthew G Knepley           char name[8];
119c7543cceSMatthew G Knepley 
12063a3b9bcSJacob Faibussowitsch           PetscCall(PetscSNPrintf(name, sizeof(name), "%" PetscInt_FMT, i));
1219566063dSJacob Faibussowitsch           PetscCall(SNESMultiblockSetIS(snes, name, fields[i]));
1229566063dSJacob Faibussowitsch           PetscCall(ISDestroy(&fields[i]));
123c7543cceSMatthew G Knepley         }
1249566063dSJacob Faibussowitsch         PetscCall(PetscFree(fields));
125c7543cceSMatthew G Knepley       }
126c7543cceSMatthew G Knepley     } else {
127c7543cceSMatthew G Knepley       PetscBool flg    = PETSC_FALSE;
128c7543cceSMatthew G Knepley       PetscBool stokes = PETSC_FALSE;
129c7543cceSMatthew G Knepley 
130c7543cceSMatthew G Knepley       if (mb->bs <= 0) {
131b665c654SMatthew G Knepley         if (snes->jacobian_pre) {
1329566063dSJacob Faibussowitsch           PetscCall(MatGetBlockSize(snes->jacobian_pre, &mb->bs));
1331aa26658SKarl Rupp         } else mb->bs = 1;
134c7543cceSMatthew G Knepley       }
135c7543cceSMatthew G Knepley 
1369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetBool(NULL, ((PetscObject)snes)->prefix, "-snes_multiblock_default", &flg, NULL));
1379566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetBool(NULL, ((PetscObject)snes)->prefix, "-snes_multiblock_detect_saddle_point", &stokes, NULL));
138c7543cceSMatthew G Knepley       if (stokes) {
139c7543cceSMatthew G Knepley         IS       zerodiags, rest;
140c7543cceSMatthew G Knepley         PetscInt nmin, nmax;
141c7543cceSMatthew G Knepley 
1429566063dSJacob Faibussowitsch         PetscCall(MatGetOwnershipRange(snes->jacobian_pre, &nmin, &nmax));
1439566063dSJacob Faibussowitsch         PetscCall(MatFindZeroDiagonals(snes->jacobian_pre, &zerodiags));
1449566063dSJacob Faibussowitsch         PetscCall(ISComplement(zerodiags, nmin, nmax, &rest));
1459566063dSJacob Faibussowitsch         PetscCall(SNESMultiblockSetIS(snes, "0", rest));
1469566063dSJacob Faibussowitsch         PetscCall(SNESMultiblockSetIS(snes, "1", zerodiags));
1479566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&zerodiags));
1489566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&rest));
149c7543cceSMatthew G Knepley       } else {
150c7543cceSMatthew G Knepley         if (!flg) {
151c7543cceSMatthew G Knepley           /* Allow user to set fields from command line, if bs was known at the time of SNESSetFromOptions_Multiblock()
152c7543cceSMatthew G Knepley            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
1539566063dSJacob Faibussowitsch           PetscCall(SNESMultiblockSetFieldsRuntime_Private(snes));
1549566063dSJacob Faibussowitsch           if (mb->defined) PetscCall(PetscInfo(snes, "Blocks defined using the options database\n"));
155c7543cceSMatthew G Knepley         }
156c7543cceSMatthew G Knepley         if (flg || !mb->defined) {
1579566063dSJacob Faibussowitsch           PetscCall(PetscInfo(snes, "Using default splitting of fields\n"));
158c7543cceSMatthew G Knepley           for (i = 0; i < mb->bs; ++i) {
159c7543cceSMatthew G Knepley             char name[8];
160c7543cceSMatthew G Knepley 
16163a3b9bcSJacob Faibussowitsch             PetscCall(PetscSNPrintf(name, sizeof(name), "%" PetscInt_FMT, i));
1629566063dSJacob Faibussowitsch             PetscCall(SNESMultiblockSetFields(snes, name, 1, &i));
163c7543cceSMatthew G Knepley           }
164c7543cceSMatthew G Knepley           mb->defaultblocks = PETSC_TRUE;
165c7543cceSMatthew G Knepley         }
166c7543cceSMatthew G Knepley       }
167c7543cceSMatthew G Knepley     }
168c7543cceSMatthew G Knepley   } else if (mb->numBlocks == 1) {
169c7543cceSMatthew G Knepley     if (blocks->is) {
170c7543cceSMatthew G Knepley       IS       is2;
171c7543cceSMatthew G Knepley       PetscInt nmin, nmax;
172c7543cceSMatthew G Knepley 
1739566063dSJacob Faibussowitsch       PetscCall(MatGetOwnershipRange(snes->jacobian_pre, &nmin, &nmax));
1749566063dSJacob Faibussowitsch       PetscCall(ISComplement(blocks->is, nmin, nmax, &is2));
1759566063dSJacob Faibussowitsch       PetscCall(SNESMultiblockSetIS(snes, "1", is2));
1769566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&is2));
177f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must provide at least two sets of fields to SNES multiblock");
178c7543cceSMatthew G Knepley   }
17908401ef6SPierre Jolivet   PetscCheck(mb->numBlocks >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unhandled case, must have at least two blocks");
180c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
181c7543cceSMatthew G Knepley }
182c7543cceSMatthew G Knepley 
183c7543cceSMatthew G Knepley /*
184c7543cceSMatthew G Knepley    SNESSetUp_Multiblock - Sets up the internal data structures for the later use
185c7543cceSMatthew G Knepley    of the SNESMULTIBLOCK nonlinear solver.
186c7543cceSMatthew G Knepley 
187c7543cceSMatthew G Knepley    Input Parameters:
188c7543cceSMatthew G Knepley +  snes - the SNES context
189c7543cceSMatthew G Knepley -  x - the solution vector
190c7543cceSMatthew G Knepley 
191c7543cceSMatthew G Knepley    Application Interface Routine: SNESSetUp()
192c7543cceSMatthew G Knepley */
1939371c9d4SSatish Balay PetscErrorCode SNESSetUp_Multiblock(SNES snes) {
194c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
195c7543cceSMatthew G Knepley   BlockDesc        blocks;
196c7543cceSMatthew G Knepley   PetscInt         i, numBlocks;
197c7543cceSMatthew G Knepley 
198c7543cceSMatthew G Knepley   PetscFunctionBegin;
1999566063dSJacob Faibussowitsch   PetscCall(SNESMultiblockSetDefaults(snes));
200c7543cceSMatthew G Knepley   numBlocks = mb->numBlocks;
201b665c654SMatthew G Knepley   blocks    = mb->blocks;
202c7543cceSMatthew G Knepley 
203c7543cceSMatthew G Knepley   /* Create ISs */
204c7543cceSMatthew G Knepley   if (!mb->issetup) {
205c7543cceSMatthew G Knepley     PetscInt  ccsize, rstart, rend, nslots, bs;
206c7543cceSMatthew G Knepley     PetscBool sorted;
207c7543cceSMatthew G Knepley 
208c7543cceSMatthew G Knepley     mb->issetup = PETSC_TRUE;
209c7543cceSMatthew G Knepley     bs          = mb->bs;
2109566063dSJacob Faibussowitsch     PetscCall(MatGetOwnershipRange(snes->jacobian_pre, &rstart, &rend));
2119566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(snes->jacobian_pre, NULL, &ccsize));
212c7543cceSMatthew G Knepley     nslots = (rend - rstart) / bs;
213c7543cceSMatthew G Knepley     for (i = 0; i < numBlocks; ++i) {
214b665c654SMatthew G Knepley       if (mb->defaultblocks) {
2159566063dSJacob Faibussowitsch         PetscCall(ISCreateStride(PetscObjectComm((PetscObject)snes), nslots, rstart + i, numBlocks, &blocks->is));
216c7543cceSMatthew G Knepley       } else if (!blocks->is) {
217c7543cceSMatthew G Knepley         if (blocks->nfields > 1) {
218c7543cceSMatthew G Knepley           PetscInt *ii, j, k, nfields = blocks->nfields, *fields = blocks->fields;
219c7543cceSMatthew G Knepley 
2209566063dSJacob Faibussowitsch           PetscCall(PetscMalloc1(nfields * nslots, &ii));
221c7543cceSMatthew G Knepley           for (j = 0; j < nslots; ++j) {
222ad540459SPierre Jolivet             for (k = 0; k < nfields; ++k) ii[nfields * j + k] = rstart + bs * j + fields[k];
223c7543cceSMatthew G Knepley           }
2249566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)snes), nslots * nfields, ii, PETSC_OWN_POINTER, &blocks->is));
225c7543cceSMatthew G Knepley         } else {
2269566063dSJacob Faibussowitsch           PetscCall(ISCreateStride(PetscObjectComm((PetscObject)snes), nslots, rstart + blocks->fields[0], bs, &blocks->is));
227c7543cceSMatthew G Knepley         }
228c7543cceSMatthew G Knepley       }
2299566063dSJacob Faibussowitsch       PetscCall(ISSorted(blocks->is, &sorted));
23028b400f6SJacob Faibussowitsch       PetscCheck(sorted, PETSC_COMM_SELF, PETSC_ERR_USER, "Fields must be sorted when creating split");
231c7543cceSMatthew G Knepley       blocks = blocks->next;
232c7543cceSMatthew G Knepley     }
233c7543cceSMatthew G Knepley   }
234c7543cceSMatthew G Knepley 
235c7543cceSMatthew G Knepley #if 0
236c7543cceSMatthew G Knepley   /* Create matrices */
237c7543cceSMatthew G Knepley   ilink = jac->head;
238c7543cceSMatthew G Knepley   if (!jac->pmat) {
2399566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nsplit,&jac->pmat));
240c7543cceSMatthew G Knepley     for (i=0; i<nsplit; i++) {
2419566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is,MAT_INITIAL_MATRIX,&jac->pmat[i]));
242c7543cceSMatthew G Knepley       ilink = ilink->next;
243c7543cceSMatthew G Knepley     }
244c7543cceSMatthew G Knepley   } else {
245c7543cceSMatthew G Knepley     for (i=0; i<nsplit; i++) {
2469566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is,MAT_REUSE_MATRIX,&jac->pmat[i]));
247c7543cceSMatthew G Knepley       ilink = ilink->next;
248c7543cceSMatthew G Knepley     }
249c7543cceSMatthew G Knepley   }
250c7543cceSMatthew G Knepley   if (jac->realdiagonal) {
251c7543cceSMatthew G Knepley     ilink = jac->head;
252c7543cceSMatthew G Knepley     if (!jac->mat) {
2539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nsplit,&jac->mat));
254c7543cceSMatthew G Knepley       for (i=0; i<nsplit; i++) {
2559566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ilink->is,MAT_INITIAL_MATRIX,&jac->mat[i]));
256c7543cceSMatthew G Knepley         ilink = ilink->next;
257c7543cceSMatthew G Knepley       }
258c7543cceSMatthew G Knepley     } else {
259c7543cceSMatthew G Knepley       for (i=0; i<nsplit; i++) {
2609566063dSJacob Faibussowitsch         if (jac->mat[i]) PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ilink->is,MAT_REUSE_MATRIX,&jac->mat[i]));
261c7543cceSMatthew G Knepley         ilink = ilink->next;
262c7543cceSMatthew G Knepley       }
263c7543cceSMatthew G Knepley     }
2641aa26658SKarl Rupp   } else jac->mat = jac->pmat;
265c7543cceSMatthew G Knepley #endif
266c7543cceSMatthew G Knepley 
267c7543cceSMatthew G Knepley #if 0
268c7543cceSMatthew G Knepley   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR) {
269c7543cceSMatthew G Knepley     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
270c7543cceSMatthew G Knepley     ilink = jac->head;
271c7543cceSMatthew G Knepley     if (!jac->Afield) {
2729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nsplit,&jac->Afield));
273c7543cceSMatthew G Knepley       for (i=0; i<nsplit; i++) {
2749566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]));
275c7543cceSMatthew G Knepley         ilink = ilink->next;
276c7543cceSMatthew G Knepley       }
277c7543cceSMatthew G Knepley     } else {
278c7543cceSMatthew G Knepley       for (i=0; i<nsplit; i++) {
2799566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]));
280c7543cceSMatthew G Knepley         ilink = ilink->next;
281c7543cceSMatthew G Knepley       }
282c7543cceSMatthew G Knepley     }
283c7543cceSMatthew G Knepley   }
284c7543cceSMatthew G Knepley #endif
285c7543cceSMatthew G Knepley 
286b665c654SMatthew G Knepley   if (mb->type == PC_COMPOSITE_SCHUR) {
287c7543cceSMatthew G Knepley #if 0
288c7543cceSMatthew G Knepley     IS       ccis;
289c7543cceSMatthew G Knepley     PetscInt rstart,rend;
29008401ef6SPierre Jolivet     PetscCheck(nsplit == 2,PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");
291c7543cceSMatthew G Knepley 
292c7543cceSMatthew G Knepley     /* When extracting off-diagonal submatrices, we take complements from this range */
2939566063dSJacob Faibussowitsch     PetscCall(MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend));
294c7543cceSMatthew G Knepley 
295c7543cceSMatthew G Knepley     /* need to handle case when one is resetting up the preconditioner */
296c7543cceSMatthew G Knepley     if (jac->schur) {
297c7543cceSMatthew G Knepley       ilink = jac->head;
2989566063dSJacob Faibussowitsch       PetscCall(ISComplement(ilink->is,rstart,rend,&ccis));
2999566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B));
3009566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&ccis));
301c7543cceSMatthew G Knepley       ilink = ilink->next;
3029566063dSJacob Faibussowitsch       PetscCall(ISComplement(ilink->is,rstart,rend,&ccis));
3039566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C));
3049566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&ccis));
3059566063dSJacob Faibussowitsch       PetscCall(MatSchurComplementUpdateSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->pmat[1]));
3069566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),pc->flag));
307c7543cceSMatthew G Knepley 
308c7543cceSMatthew G Knepley     } else {
309c7543cceSMatthew G Knepley       KSP  ksp;
310c7543cceSMatthew G Knepley       char schurprefix[256];
311c7543cceSMatthew G Knepley 
312c7543cceSMatthew G Knepley       /* extract the A01 and A10 matrices */
313c7543cceSMatthew G Knepley       ilink = jac->head;
3149566063dSJacob Faibussowitsch       PetscCall(ISComplement(ilink->is,rstart,rend,&ccis));
3159566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B));
3169566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&ccis));
317c7543cceSMatthew G Knepley       ilink = ilink->next;
3189566063dSJacob Faibussowitsch       PetscCall(ISComplement(ilink->is,rstart,rend,&ccis));
3199566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C));
3209566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&ccis));
321c7543cceSMatthew G Knepley       /* Use mat[0] (diagonal block of the real matrix) preconditioned by pmat[0] */
3229566063dSJacob Faibussowitsch       PetscCall(MatCreateSchurComplement(jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1],&jac->schur));
323c7543cceSMatthew G Knepley       /* set tabbing and options prefix of KSP inside the MatSchur */
3249566063dSJacob Faibussowitsch       PetscCall(MatSchurComplementGetKSP(jac->schur,&ksp));
3259566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,2));
3269566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(schurprefix,sizeof(schurprefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",jac->head->splitname));
3279566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(ksp,schurprefix));
3289566063dSJacob Faibussowitsch       PetscCall(MatSetFromOptions(jac->schur));
329c7543cceSMatthew G Knepley 
3309566063dSJacob Faibussowitsch       PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur));
3319566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1));
3329566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac)));
333c7543cceSMatthew G Knepley       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
334c7543cceSMatthew G Knepley         PC pc;
3359566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(jac->kspschur,&pc));
3369566063dSJacob Faibussowitsch         PetscCall(PCSetType(pc,PCNONE));
337c7543cceSMatthew G Knepley         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
338c7543cceSMatthew G Knepley       }
3399566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(schurprefix,sizeof(schurprefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname));
3409566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(jac->kspschur,schurprefix));
341c7543cceSMatthew G Knepley       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
3429566063dSJacob Faibussowitsch       PetscCall(KSPSetFromOptions(jac->kspschur));
343c7543cceSMatthew G Knepley 
3449566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(2,&jac->x,2,&jac->y));
3459566063dSJacob Faibussowitsch       PetscCall(MatCreateVecs(jac->pmat[0],&jac->x[0],&jac->y[0]));
3469566063dSJacob Faibussowitsch       PetscCall(MatCreateVecs(jac->pmat[1],&jac->x[1],&jac->y[1]));
347c7543cceSMatthew G Knepley       ilink    = jac->head;
348c7543cceSMatthew G Knepley       ilink->x = jac->x[0]; ilink->y = jac->y[0];
349c7543cceSMatthew G Knepley       ilink    = ilink->next;
350c7543cceSMatthew G Knepley       ilink->x = jac->x[1]; ilink->y = jac->y[1];
351c7543cceSMatthew G Knepley     }
352c7543cceSMatthew G Knepley #endif
353c7543cceSMatthew G Knepley   } else {
354c7543cceSMatthew G Knepley     /* Set up the individual SNESs */
355c7543cceSMatthew G Knepley     blocks = mb->blocks;
356c7543cceSMatthew G Knepley     i      = 0;
357c7543cceSMatthew G Knepley     while (blocks) {
358b665c654SMatthew G Knepley       /*TODO: Set these correctly */
3599566063dSJacob Faibussowitsch       /* PetscCall(SNESSetFunction(blocks->snes, blocks->x, func)); */
3609566063dSJacob Faibussowitsch       /* PetscCall(SNESSetJacobian(blocks->snes, blocks->x, jac)); */
3619566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(blocks->snes->vec_sol, &blocks->x));
362c7543cceSMatthew G Knepley       /* really want setfromoptions called in SNESSetFromOptions_Multiblock(), but it is not ready yet */
3639566063dSJacob Faibussowitsch       PetscCall(SNESSetFromOptions(blocks->snes));
3649566063dSJacob Faibussowitsch       PetscCall(SNESSetUp(blocks->snes));
365c7543cceSMatthew G Knepley       blocks = blocks->next;
366c7543cceSMatthew G Knepley       i++;
367c7543cceSMatthew G Knepley     }
368c7543cceSMatthew G Knepley   }
369c7543cceSMatthew G Knepley 
370c7543cceSMatthew G Knepley   /* Compute scatter contexts needed by multiplicative versions and non-default splits */
371c7543cceSMatthew G Knepley   if (!mb->blocks->sctx) {
372c7543cceSMatthew G Knepley     Vec xtmp;
373c7543cceSMatthew G Knepley 
374c7543cceSMatthew G Knepley     blocks = mb->blocks;
3759566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(snes->jacobian_pre, &xtmp, NULL));
376c7543cceSMatthew G Knepley     while (blocks) {
3779566063dSJacob Faibussowitsch       PetscCall(VecScatterCreate(xtmp, blocks->is, blocks->x, NULL, &blocks->sctx));
378c7543cceSMatthew G Knepley       blocks = blocks->next;
379c7543cceSMatthew G Knepley     }
3809566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xtmp));
381c7543cceSMatthew G Knepley   }
382c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
383c7543cceSMatthew G Knepley }
384c7543cceSMatthew G Knepley 
385c7543cceSMatthew G Knepley /*
386c7543cceSMatthew G Knepley   SNESSetFromOptions_Multiblock - Sets various parameters for the SNESMULTIBLOCK method.
387c7543cceSMatthew G Knepley 
388c7543cceSMatthew G Knepley   Input Parameter:
389c7543cceSMatthew G Knepley . snes - the SNES context
390c7543cceSMatthew G Knepley 
391c7543cceSMatthew G Knepley   Application Interface Routine: SNESSetFromOptions()
392c7543cceSMatthew G Knepley */
3939371c9d4SSatish Balay static PetscErrorCode SNESSetFromOptions_Multiblock(SNES snes, PetscOptionItems *PetscOptionsObject) {
394c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
395c7543cceSMatthew G Knepley   PCCompositeType  ctype;
396c7543cceSMatthew G Knepley   PetscInt         bs;
397c7543cceSMatthew G Knepley   PetscBool        flg;
398c7543cceSMatthew G Knepley 
399c7543cceSMatthew G Knepley   PetscFunctionBegin;
400d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "SNES Multiblock options");
4019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-snes_multiblock_block_size", "Blocksize that defines number of fields", "PCFieldSplitSetBlockSize", mb->bs, &bs, &flg));
4029566063dSJacob Faibussowitsch   if (flg) PetscCall(SNESMultiblockSetBlockSize(snes, bs));
4039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-snes_multiblock_type", "Type of composition", "PCFieldSplitSetType", PCCompositeTypes, (PetscEnum)mb->type, (PetscEnum *)&ctype, &flg));
4041baa6e33SBarry Smith   if (flg) PetscCall(SNESMultiblockSetType(snes, ctype));
405c7543cceSMatthew G Knepley   /* Only setup fields once */
406b665c654SMatthew G Knepley   if ((mb->bs > 0) && (mb->numBlocks == 0)) {
407c7543cceSMatthew G Knepley     /* only allow user to set fields from command line if bs is already known, otherwise user can set them in SNESMultiblockSetDefaults() */
4089566063dSJacob Faibussowitsch     PetscCall(SNESMultiblockSetFieldsRuntime_Private(snes));
4099566063dSJacob Faibussowitsch     if (mb->defined) PetscCall(PetscInfo(snes, "Blocks defined using the options database\n"));
410c7543cceSMatthew G Knepley   }
411d0609cedSBarry Smith   PetscOptionsHeadEnd();
412c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
413c7543cceSMatthew G Knepley }
414c7543cceSMatthew G Knepley 
415c7543cceSMatthew G Knepley /*
416c7543cceSMatthew G Knepley   SNESView_Multiblock - Prints info from the SNESMULTIBLOCK data structure.
417c7543cceSMatthew G Knepley 
418c7543cceSMatthew G Knepley   Input Parameters:
419c7543cceSMatthew G Knepley + SNES - the SNES context
420c7543cceSMatthew G Knepley - viewer - visualization context
421c7543cceSMatthew G Knepley 
422c7543cceSMatthew G Knepley   Application Interface Routine: SNESView()
423c7543cceSMatthew G Knepley */
4249371c9d4SSatish Balay static PetscErrorCode SNESView_Multiblock(SNES snes, PetscViewer viewer) {
425c7543cceSMatthew G Knepley   SNES_Multiblock *mb     = (SNES_Multiblock *)snes->data;
426c7543cceSMatthew G Knepley   BlockDesc        blocks = mb->blocks;
427c7543cceSMatthew G Knepley   PetscBool        iascii;
428c7543cceSMatthew G Knepley 
429c7543cceSMatthew G Knepley   PetscFunctionBegin;
4309566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
431c7543cceSMatthew G Knepley   if (iascii) {
43263a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Multiblock with %s composition: total blocks = %" PetscInt_FMT ", blocksize = %" PetscInt_FMT "\n", PCCompositeTypes[mb->type], mb->numBlocks, mb->bs));
4339566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Solver info for each split is in the following SNES objects:\n"));
4349566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
435c7543cceSMatthew G Knepley     while (blocks) {
436c7543cceSMatthew G Knepley       if (blocks->fields) {
437c7543cceSMatthew G Knepley         PetscInt j;
438c7543cceSMatthew G Knepley 
4399566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "  Block %s Fields ", blocks->name));
4409566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
441c7543cceSMatthew G Knepley         for (j = 0; j < blocks->nfields; ++j) {
44248a46eb9SPierre Jolivet           if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
44363a3b9bcSJacob Faibussowitsch           PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, blocks->fields[j]));
444c7543cceSMatthew G Knepley         }
4459566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
4469566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
447c7543cceSMatthew G Knepley       } else {
4489566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "  Block %s Defined by IS\n", blocks->name));
449c7543cceSMatthew G Knepley       }
4509566063dSJacob Faibussowitsch       PetscCall(SNESView(blocks->snes, viewer));
451c7543cceSMatthew G Knepley       blocks = blocks->next;
452c7543cceSMatthew G Knepley     }
4539566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
454c7543cceSMatthew G Knepley   }
455c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
456c7543cceSMatthew G Knepley }
457c7543cceSMatthew G Knepley 
458c7543cceSMatthew G Knepley /*
459c7543cceSMatthew G Knepley   SNESSolve_Multiblock - Solves a nonlinear system with the Multiblock method.
460c7543cceSMatthew G Knepley 
461c7543cceSMatthew G Knepley   Input Parameters:
462c7543cceSMatthew G Knepley . snes - the SNES context
463c7543cceSMatthew G Knepley 
464c7543cceSMatthew G Knepley   Output Parameter:
465c7543cceSMatthew G Knepley . outits - number of iterations until termination
466c7543cceSMatthew G Knepley 
467c7543cceSMatthew G Knepley   Application Interface Routine: SNESSolve()
468c7543cceSMatthew G Knepley */
4699371c9d4SSatish Balay PetscErrorCode SNESSolve_Multiblock(SNES snes) {
470c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
471c7543cceSMatthew G Knepley   Vec              X, Y, F;
472c7543cceSMatthew G Knepley   PetscReal        fnorm;
473c7543cceSMatthew G Knepley   PetscInt         maxits, i;
474c7543cceSMatthew G Knepley 
475c7543cceSMatthew G Knepley   PetscFunctionBegin;
4760b121fc5SBarry Smith   PetscCheck(!snes->xl && !snes->xu && !snes->ops->computevariablebounds, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);
477c579b300SPatrick Farrell 
478c7543cceSMatthew G Knepley   snes->reason = SNES_CONVERGED_ITERATING;
479c7543cceSMatthew G Knepley 
480c7543cceSMatthew G Knepley   maxits = snes->max_its;        /* maximum number of iterations */
481c7543cceSMatthew G Knepley   X      = snes->vec_sol;        /* X^n */
482c7543cceSMatthew G Knepley   Y      = snes->vec_sol_update; /* \tilde X */
483c7543cceSMatthew G Knepley   F      = snes->vec_func;       /* residual vector */
484c7543cceSMatthew G Knepley 
4859566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(X, mb->bs));
4869566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(Y, mb->bs));
4879566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(F, mb->bs));
4889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
489c7543cceSMatthew G Knepley   snes->iter = 0;
490c7543cceSMatthew G Knepley   snes->norm = 0.;
4919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
492e4ed7901SPeter Brune 
493e4ed7901SPeter Brune   if (!snes->vec_func_init_set) {
4949566063dSJacob Faibussowitsch     PetscCall(SNESComputeFunction(snes, X, F));
4951aa26658SKarl Rupp   } else snes->vec_func_init_set = PETSC_FALSE;
4961aa26658SKarl Rupp 
4979566063dSJacob Faibussowitsch   PetscCall(VecNorm(F, NORM_2, &fnorm)); /* fnorm <- ||F||  */
498422a814eSBarry Smith   SNESCheckFunctionNorm(snes, fnorm);
4999566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
500c7543cceSMatthew G Knepley   snes->norm = fnorm;
5019566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
5029566063dSJacob Faibussowitsch   PetscCall(SNESLogConvergenceHistory(snes, fnorm, 0));
5039566063dSJacob Faibussowitsch   PetscCall(SNESMonitor(snes, 0, fnorm));
504c7543cceSMatthew G Knepley 
505c7543cceSMatthew G Knepley   /* test convergence */
506dbbe0bcdSBarry Smith   PetscUseTypeMethod(snes, converged, 0, 0.0, 0.0, fnorm, &snes->reason, snes->cnvP);
507c7543cceSMatthew G Knepley   if (snes->reason) PetscFunctionReturn(0);
508c7543cceSMatthew G Knepley 
509c7543cceSMatthew G Knepley   for (i = 0; i < maxits; i++) {
510c7543cceSMatthew G Knepley     /* Call general purpose update function */
511dbbe0bcdSBarry Smith     PetscTryTypeMethod(snes, update, snes->iter);
512c7543cceSMatthew G Knepley     /* Compute X^{new} from subsolves */
513c7543cceSMatthew G Knepley     if (mb->type == PC_COMPOSITE_ADDITIVE) {
514c7543cceSMatthew G Knepley       BlockDesc blocks = mb->blocks;
515c7543cceSMatthew G Knepley 
516b665c654SMatthew G Knepley       if (mb->defaultblocks) {
517b665c654SMatthew G Knepley         /*TODO: Make an array of Vecs for this */
5189566063dSJacob Faibussowitsch         /* PetscCall(VecStrideGatherAll(X, mb->x, INSERT_VALUES)); */
519c7543cceSMatthew G Knepley         while (blocks) {
5209566063dSJacob Faibussowitsch           PetscCall(SNESSolve(blocks->snes, NULL, blocks->x));
521c7543cceSMatthew G Knepley           blocks = blocks->next;
522c7543cceSMatthew G Knepley         }
5239566063dSJacob Faibussowitsch         /* PetscCall(VecStrideScatterAll(mb->x, X, INSERT_VALUES)); */
524c7543cceSMatthew G Knepley       } else {
525c7543cceSMatthew G Knepley         while (blocks) {
5269566063dSJacob Faibussowitsch           PetscCall(VecScatterBegin(blocks->sctx, X, blocks->x, INSERT_VALUES, SCATTER_FORWARD));
5279566063dSJacob Faibussowitsch           PetscCall(VecScatterEnd(blocks->sctx, X, blocks->x, INSERT_VALUES, SCATTER_FORWARD));
5289566063dSJacob Faibussowitsch           PetscCall(SNESSolve(blocks->snes, NULL, blocks->x));
5299566063dSJacob Faibussowitsch           PetscCall(VecScatterBegin(blocks->sctx, blocks->x, X, INSERT_VALUES, SCATTER_REVERSE));
5309566063dSJacob Faibussowitsch           PetscCall(VecScatterEnd(blocks->sctx, blocks->x, X, INSERT_VALUES, SCATTER_REVERSE));
531c7543cceSMatthew G Knepley           blocks = blocks->next;
532c7543cceSMatthew G Knepley         }
533c7543cceSMatthew G Knepley       }
53463a3b9bcSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)snes), PETSC_ERR_SUP, "Unsupported or unknown composition %d", (int)mb->type);
535c7543cceSMatthew G Knepley     /* Compute F(X^{new}) */
5369566063dSJacob Faibussowitsch     PetscCall(SNESComputeFunction(snes, X, F));
5379566063dSJacob Faibussowitsch     PetscCall(VecNorm(F, NORM_2, &fnorm));
538422a814eSBarry Smith     SNESCheckFunctionNorm(snes, fnorm);
539c7543cceSMatthew G Knepley 
540e71169deSBarry Smith     if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
541c7543cceSMatthew G Knepley       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
542c7543cceSMatthew G Knepley       break;
543c7543cceSMatthew G Knepley     }
544422a814eSBarry Smith 
545c7543cceSMatthew G Knepley     /* Monitor convergence */
5469566063dSJacob Faibussowitsch     PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
547c7543cceSMatthew G Knepley     snes->iter = i + 1;
548c7543cceSMatthew G Knepley     snes->norm = fnorm;
5499566063dSJacob Faibussowitsch     PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
5509566063dSJacob Faibussowitsch     PetscCall(SNESLogConvergenceHistory(snes, snes->norm, 0));
5519566063dSJacob Faibussowitsch     PetscCall(SNESMonitor(snes, snes->iter, snes->norm));
552c7543cceSMatthew G Knepley     /* Test for convergence */
553dbbe0bcdSBarry Smith     PetscUseTypeMethod(snes, converged, snes->iter, 0.0, 0.0, fnorm, &snes->reason, snes->cnvP);
554c7543cceSMatthew G Knepley     if (snes->reason) break;
555c7543cceSMatthew G Knepley   }
556c7543cceSMatthew G Knepley   if (i == maxits) {
55763a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(snes, "Maximum number of iterations has been reached: %" PetscInt_FMT "\n", maxits));
558c7543cceSMatthew G Knepley     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
559c7543cceSMatthew G Knepley   }
560c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
561c7543cceSMatthew G Knepley }
562c7543cceSMatthew G Knepley 
5639371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetFields_Default(SNES snes, const char name[], PetscInt n, const PetscInt fields[]) {
564c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
565c7543cceSMatthew G Knepley   BlockDesc        newblock, next = mb->blocks;
566c7543cceSMatthew G Knepley   char             prefix[128];
567c7543cceSMatthew G Knepley   PetscInt         i;
568c7543cceSMatthew G Knepley 
569c7543cceSMatthew G Knepley   PetscFunctionBegin;
570c7543cceSMatthew G Knepley   if (mb->defined) {
5719566063dSJacob Faibussowitsch     PetscCall(PetscInfo(snes, "Ignoring new block \"%s\" because the blocks have already been defined\n", name));
572c7543cceSMatthew G Knepley     PetscFunctionReturn(0);
573c7543cceSMatthew G Knepley   }
574c7543cceSMatthew G Knepley   for (i = 0; i < n; ++i) {
57563a3b9bcSJacob Faibussowitsch     PetscCheck(fields[i] < mb->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " requested but only %" PetscInt_FMT " exist", fields[i], mb->bs);
57663a3b9bcSJacob Faibussowitsch     PetscCheck(fields[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative field %" PetscInt_FMT " requested", fields[i]);
577c7543cceSMatthew G Knepley   }
5789566063dSJacob Faibussowitsch   PetscCall(PetscNew(&newblock));
579c7543cceSMatthew G Knepley   if (name) {
5809566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &newblock->name));
581c7543cceSMatthew G Knepley   } else {
582c7543cceSMatthew G Knepley     PetscInt len = floor(log10(mb->numBlocks)) + 1;
583c7543cceSMatthew G Knepley 
5849566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(len + 1, &newblock->name));
58563a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(newblock->name, len, "%" PetscInt_FMT, mb->numBlocks));
586c7543cceSMatthew G Knepley   }
587c7543cceSMatthew G Knepley   newblock->nfields = n;
5881aa26658SKarl Rupp 
5899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &newblock->fields));
5909566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(newblock->fields, fields, n));
5911aa26658SKarl Rupp 
5920298fd71SBarry Smith   newblock->next = NULL;
5931aa26658SKarl Rupp 
5949566063dSJacob Faibussowitsch   PetscCall(SNESCreate(PetscObjectComm((PetscObject)snes), &newblock->snes));
5959566063dSJacob Faibussowitsch   PetscCall(PetscObjectIncrementTabLevel((PetscObject)newblock->snes, (PetscObject)snes, 1));
5969566063dSJacob Faibussowitsch   PetscCall(SNESSetType(newblock->snes, SNESNRICHARDSON));
5979566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%smultiblock_%s_", ((PetscObject)snes)->prefix ? ((PetscObject)snes)->prefix : "", newblock->name));
5989566063dSJacob Faibussowitsch   PetscCall(SNESSetOptionsPrefix(newblock->snes, prefix));
599c7543cceSMatthew G Knepley 
600c7543cceSMatthew G Knepley   if (!next) {
601c7543cceSMatthew G Knepley     mb->blocks         = newblock;
6020298fd71SBarry Smith     newblock->previous = NULL;
603c7543cceSMatthew G Knepley   } else {
604ad540459SPierre Jolivet     while (next->next) next = next->next;
605c7543cceSMatthew G Knepley     next->next         = newblock;
606c7543cceSMatthew G Knepley     newblock->previous = next;
607c7543cceSMatthew G Knepley   }
608c7543cceSMatthew G Knepley   mb->numBlocks++;
609c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
610c7543cceSMatthew G Knepley }
611c7543cceSMatthew G Knepley 
6129371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetIS_Default(SNES snes, const char name[], IS is) {
613c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
614c7543cceSMatthew G Knepley   BlockDesc        newblock, next = mb->blocks;
615c7543cceSMatthew G Knepley   char             prefix[128];
616c7543cceSMatthew G Knepley 
617c7543cceSMatthew G Knepley   PetscFunctionBegin;
618c7543cceSMatthew G Knepley   if (mb->defined) {
6199566063dSJacob Faibussowitsch     PetscCall(PetscInfo(snes, "Ignoring new block \"%s\" because the blocks have already been defined\n", name));
620c7543cceSMatthew G Knepley     PetscFunctionReturn(0);
621c7543cceSMatthew G Knepley   }
6229566063dSJacob Faibussowitsch   PetscCall(PetscNew(&newblock));
623c7543cceSMatthew G Knepley   if (name) {
6249566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &newblock->name));
625c7543cceSMatthew G Knepley   } else {
626c7543cceSMatthew G Knepley     PetscInt len = floor(log10(mb->numBlocks)) + 1;
627c7543cceSMatthew G Knepley 
6289566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(len + 1, &newblock->name));
62963a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(newblock->name, len, "%" PetscInt_FMT, mb->numBlocks));
630c7543cceSMatthew G Knepley   }
631c7543cceSMatthew G Knepley   newblock->is = is;
6321aa26658SKarl Rupp 
6339566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)is));
6341aa26658SKarl Rupp 
6350298fd71SBarry Smith   newblock->next = NULL;
6361aa26658SKarl Rupp 
6379566063dSJacob Faibussowitsch   PetscCall(SNESCreate(PetscObjectComm((PetscObject)snes), &newblock->snes));
6389566063dSJacob Faibussowitsch   PetscCall(PetscObjectIncrementTabLevel((PetscObject)newblock->snes, (PetscObject)snes, 1));
6399566063dSJacob Faibussowitsch   PetscCall(SNESSetType(newblock->snes, SNESNRICHARDSON));
6409566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%smultiblock_%s_", ((PetscObject)snes)->prefix ? ((PetscObject)snes)->prefix : "", newblock->name));
6419566063dSJacob Faibussowitsch   PetscCall(SNESSetOptionsPrefix(newblock->snes, prefix));
642c7543cceSMatthew G Knepley 
643c7543cceSMatthew G Knepley   if (!next) {
644c7543cceSMatthew G Knepley     mb->blocks         = newblock;
6450298fd71SBarry Smith     newblock->previous = NULL;
646c7543cceSMatthew G Knepley   } else {
647ad540459SPierre Jolivet     while (next->next) next = next->next;
648c7543cceSMatthew G Knepley     next->next         = newblock;
649c7543cceSMatthew G Knepley     newblock->previous = next;
650c7543cceSMatthew G Knepley   }
651c7543cceSMatthew G Knepley   mb->numBlocks++;
652c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
653c7543cceSMatthew G Knepley }
654c7543cceSMatthew G Knepley 
6559371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetBlockSize_Default(SNES snes, PetscInt bs) {
656c7543cceSMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
657c7543cceSMatthew G Knepley 
658c7543cceSMatthew G Knepley   PetscFunctionBegin;
65963a3b9bcSJacob Faibussowitsch   PetscCheck(bs >= 1, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Blocksize must be positive, you gave %" PetscInt_FMT, bs);
6600b121fc5SBarry Smith   PetscCheck(mb->bs <= 0 || mb->bs == bs, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONGSTATE, "Cannot change blocksize from %" PetscInt_FMT " to %" PetscInt_FMT " after it has been set", mb->bs, bs);
661c7543cceSMatthew G Knepley   mb->bs = bs;
662c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
663c7543cceSMatthew G Knepley }
664c7543cceSMatthew G Knepley 
6659371c9d4SSatish Balay PetscErrorCode SNESMultiblockGetSubSNES_Default(SNES snes, PetscInt *n, SNES **subsnes) {
666c7543cceSMatthew G Knepley   SNES_Multiblock *mb     = (SNES_Multiblock *)snes->data;
667c7543cceSMatthew G Knepley   BlockDesc        blocks = mb->blocks;
668c7543cceSMatthew G Knepley   PetscInt         cnt    = 0;
669c7543cceSMatthew G Knepley 
670c7543cceSMatthew G Knepley   PetscFunctionBegin;
6719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(mb->numBlocks, subsnes));
672c7543cceSMatthew G Knepley   while (blocks) {
673c7543cceSMatthew G Knepley     (*subsnes)[cnt++] = blocks->snes;
674c7543cceSMatthew G Knepley     blocks            = blocks->next;
675c7543cceSMatthew G Knepley   }
67663a3b9bcSJacob Faibussowitsch   PetscCheck(cnt == mb->numBlocks, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupt SNESMULTIBLOCK object: number of blocks in linked list %" PetscInt_FMT " does not match number in object %" PetscInt_FMT, cnt, mb->numBlocks);
6771aa26658SKarl Rupp 
6781aa26658SKarl Rupp   if (n) *n = mb->numBlocks;
679c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
680c7543cceSMatthew G Knepley }
681c7543cceSMatthew G Knepley 
6829371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetType_Default(SNES snes, PCCompositeType type) {
68387bd6022SMatthew G Knepley   SNES_Multiblock *mb = (SNES_Multiblock *)snes->data;
68487bd6022SMatthew G Knepley 
68587bd6022SMatthew G Knepley   PetscFunctionBegin;
68687bd6022SMatthew G Knepley   mb->type = type;
68787bd6022SMatthew G Knepley   if (type == PC_COMPOSITE_SCHUR) {
68887bd6022SMatthew G Knepley #if 1
68982f516ccSBarry Smith     SETERRQ(PetscObjectComm((PetscObject)snes), PETSC_ERR_SUP, "The Schur composite type is not yet supported");
69087bd6022SMatthew G Knepley #else
69187bd6022SMatthew G Knepley     snes->ops->solve = SNESSolve_Multiblock_Schur;
69287bd6022SMatthew G Knepley     snes->ops->view  = SNESView_Multiblock_Schur;
6931aa26658SKarl Rupp 
6949566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockGetSubSNES_C", SNESMultiblockGetSubSNES_Schur));
6959566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSchurPrecondition_C", SNESMultiblockSchurPrecondition_Default));
69687bd6022SMatthew G Knepley #endif
69787bd6022SMatthew G Knepley   } else {
69887bd6022SMatthew G Knepley     snes->ops->solve = SNESSolve_Multiblock;
69987bd6022SMatthew G Knepley     snes->ops->view  = SNESView_Multiblock;
7001aa26658SKarl Rupp 
7019566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockGetSubSNES_C", SNESMultiblockGetSubSNES_Default));
7029566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSchurPrecondition_C", 0));
70387bd6022SMatthew G Knepley   }
70487bd6022SMatthew G Knepley   PetscFunctionReturn(0);
70587bd6022SMatthew G Knepley }
70687bd6022SMatthew G Knepley 
707c7543cceSMatthew G Knepley /*@
708f6dfbefdSBarry Smith   SNESMultiblockSetFields - Sets the fields for one particular block in a `SNESMULTBLOCK` solver
709c7543cceSMatthew G Knepley 
710f6dfbefdSBarry Smith   Logically Collective on snes
711c7543cceSMatthew G Knepley 
712c7543cceSMatthew G Knepley   Input Parameters:
713c7543cceSMatthew G Knepley + snes   - the solver
7140298fd71SBarry Smith . name   - name of this block, if NULL the number of the block is used
715c7543cceSMatthew G Knepley . n      - the number of fields in this block
716c7543cceSMatthew G Knepley - fields - the fields in this block
717c7543cceSMatthew G Knepley 
718c7543cceSMatthew G Knepley   Level: intermediate
719c7543cceSMatthew G Knepley 
72095452b02SPatrick Sanan   Notes:
721f6dfbefdSBarry Smith     Use `SNESMultiblockSetIS()` to set a completely general set of row indices as a block.
722c7543cceSMatthew G Knepley 
723f6dfbefdSBarry Smith   The `SNESMultiblockSetFields()` is for defining blocks as a group of strided indices, or fields.
724c7543cceSMatthew G Knepley   For example, if the vector block size is three then one can define a block as field 0, or
725c7543cceSMatthew G Knepley   1 or 2, or field 0,1 or 0,2 or 1,2 which means
726c7543cceSMatthew G Knepley     0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
727c7543cceSMatthew G Knepley   where the numbered entries indicate what is in the block.
728c7543cceSMatthew G Knepley 
729c7543cceSMatthew G Knepley   This function is called once per block (it creates a new block each time). Solve options
730c7543cceSMatthew G Knepley   for this block will be available under the prefix -multiblock_BLOCKNAME_.
731c7543cceSMatthew G Knepley 
732f6dfbefdSBarry Smith .seealso: `SNESMULTBLOCK`, `SNESMultiblockGetSubSNES()`, `SNESMULTIBLOCK`, `SNESMultiblockSetBlockSize()`, `SNESMultiblockSetIS()`
733c7543cceSMatthew G Knepley @*/
7349371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetFields(SNES snes, const char name[], PetscInt n, const PetscInt *fields) {
735c7543cceSMatthew G Knepley   PetscFunctionBegin;
736c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
737c7543cceSMatthew G Knepley   PetscValidCharPointer(name, 2);
73863a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 1, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Provided number of fields %" PetscInt_FMT " in split \"%s\" not positive", n, name);
739064a246eSJacob Faibussowitsch   PetscValidIntPointer(fields, 4);
740cac4c232SBarry Smith   PetscTryMethod(snes, "SNESMultiblockSetFields_C", (SNES, const char[], PetscInt, const PetscInt *), (snes, name, n, fields));
741c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
742c7543cceSMatthew G Knepley }
743c7543cceSMatthew G Knepley 
744c7543cceSMatthew G Knepley /*@
745f6dfbefdSBarry Smith   SNESMultiblockSetIS - Sets the global row indices for one particular block in a `SNESMULTBLOCK` solver
746c7543cceSMatthew G Knepley 
747f6dfbefdSBarry Smith   Logically Collective on snes
748c7543cceSMatthew G Knepley 
749c7543cceSMatthew G Knepley   Input Parameters:
750c7543cceSMatthew G Knepley + snes - the solver context
7510298fd71SBarry Smith . name - name of this block, if NULL the number of the block is used
752c7543cceSMatthew G Knepley - is   - the index set that defines the global row indices in this block
753c7543cceSMatthew G Knepley 
754c7543cceSMatthew G Knepley   Notes:
755f6dfbefdSBarry Smith   Use `SNESMultiblockSetFields()`, for blocks defined by strides.
756c7543cceSMatthew G Knepley 
757c7543cceSMatthew G Knepley   This function is called once per block (it creates a new block each time). Solve options
758c7543cceSMatthew G Knepley   for this block will be available under the prefix -multiblock_BLOCKNAME_.
759c7543cceSMatthew G Knepley 
760c7543cceSMatthew G Knepley   Level: intermediate
761c7543cceSMatthew G Knepley 
762f6dfbefdSBarry Smith .seealso: `SNESMULTBLOCK`, `SNESMultiblockGetSubSNES()`, `SNESMULTIBLOCK`, `SNESMultiblockSetBlockSize()`
763c7543cceSMatthew G Knepley @*/
7649371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetIS(SNES snes, const char name[], IS is) {
765c7543cceSMatthew G Knepley   PetscFunctionBegin;
766c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
767c7543cceSMatthew G Knepley   PetscValidCharPointer(name, 2);
768c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(is, IS_CLASSID, 3);
769cac4c232SBarry Smith   PetscTryMethod(snes, "SNESMultiblockSetIS_C", (SNES, const char[], IS), (snes, name, is));
770c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
771c7543cceSMatthew G Knepley }
772c7543cceSMatthew G Knepley 
773c7543cceSMatthew G Knepley /*@
774f6dfbefdSBarry Smith   SNESMultiblockSetType - Sets the type of block combination used for a `SNESMULTBLOCK` solver
775c7543cceSMatthew G Knepley 
776f6dfbefdSBarry Smith   Logically Collective on snes
777c7543cceSMatthew G Knepley 
778c7543cceSMatthew G Knepley   Input Parameters:
779c7543cceSMatthew G Knepley + snes - the solver context
780f6dfbefdSBarry Smith - type - `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE` (default), `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`
781c7543cceSMatthew G Knepley 
782c7543cceSMatthew G Knepley   Options Database Key:
783c7543cceSMatthew G Knepley . -snes_multiblock_type <type: one of multiplicative, additive, symmetric_multiplicative> - Sets block combination type
784c7543cceSMatthew G Knepley 
785f6dfbefdSBarry Smith   Level: advanced
786c7543cceSMatthew G Knepley 
787f6dfbefdSBarry Smith .seealso: `SNESMULTBLOCK`, `PCCompositeSetType()`, `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,`PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`
788c7543cceSMatthew G Knepley @*/
7899371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetType(SNES snes, PCCompositeType type) {
790c7543cceSMatthew G Knepley   PetscFunctionBegin;
791c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
792cac4c232SBarry Smith   PetscTryMethod(snes, "SNESMultiblockSetType_C", (SNES, PCCompositeType), (snes, type));
793c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
794c7543cceSMatthew G Knepley }
795c7543cceSMatthew G Knepley 
796c7543cceSMatthew G Knepley /*@
797f6dfbefdSBarry Smith   SNESMultiblockSetBlockSize - Sets the block size for structured block division in a `SNESMULTBLOCK` solver. If not set the matrix block size is used.
798c7543cceSMatthew G Knepley 
799f6dfbefdSBarry Smith   Logically Collective on snes
800c7543cceSMatthew G Knepley 
801c7543cceSMatthew G Knepley   Input Parameters:
802c7543cceSMatthew G Knepley + snes - the solver context
803c7543cceSMatthew G Knepley - bs   - the block size
804c7543cceSMatthew G Knepley 
805c7543cceSMatthew G Knepley   Level: intermediate
806c7543cceSMatthew G Knepley 
807f6dfbefdSBarry Smith .seealso: `SNESMULTBLOCK`, `SNESMultiblockGetSubSNES()`, `SNESMULTIBLOCK`, `SNESMultiblockSetFields()`
808c7543cceSMatthew G Knepley @*/
8099371c9d4SSatish Balay PetscErrorCode SNESMultiblockSetBlockSize(SNES snes, PetscInt bs) {
810c7543cceSMatthew G Knepley   PetscFunctionBegin;
811c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
812c7543cceSMatthew G Knepley   PetscValidLogicalCollectiveInt(snes, bs, 2);
813cac4c232SBarry Smith   PetscTryMethod(snes, "SNESMultiblockSetBlockSize_C", (SNES, PetscInt), (snes, bs));
814c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
815c7543cceSMatthew G Knepley }
816c7543cceSMatthew G Knepley 
817c7543cceSMatthew G Knepley /*@C
818f6dfbefdSBarry Smith   SNESMultiblockGetSubSNES - Gets the `SNES` contexts for all blocks in a `SNESMULTBLOCK` solver.
819c7543cceSMatthew G Knepley 
820f6dfbefdSBarry Smith   Not Collective but each `SNES` obtained is parallel
821c7543cceSMatthew G Knepley 
822c7543cceSMatthew G Knepley   Input Parameter:
823c7543cceSMatthew G Knepley . snes - the solver context
824c7543cceSMatthew G Knepley 
825c7543cceSMatthew G Knepley   Output Parameters:
826c7543cceSMatthew G Knepley + n       - the number of blocks
827f6dfbefdSBarry Smith - subsnes - the array of `SNES` contexts
828c7543cceSMatthew G Knepley 
829c7543cceSMatthew G Knepley   Note:
830f6dfbefdSBarry Smith   After `SNESMultiblockGetSubSNES()` the array of `SNES`s MUST be freed by the user
831f6dfbefdSBarry Smith   (not each `SNES`, just the array that contains them).
832c7543cceSMatthew G Knepley 
833f6dfbefdSBarry Smith   You must call `SNESSetUp()` before calling `SNESMultiblockGetSubSNES()`.
834c7543cceSMatthew G Knepley 
835c7543cceSMatthew G Knepley   Level: advanced
836c7543cceSMatthew G Knepley 
837f6dfbefdSBarry Smith .seealso: `SNESMULTBLOCK`, `SNESMultiblockSetIS()`, `SNESMultiblockSetFields()`
838c7543cceSMatthew G Knepley @*/
8399371c9d4SSatish Balay PetscErrorCode SNESMultiblockGetSubSNES(SNES snes, PetscInt *n, SNES *subsnes[]) {
840c7543cceSMatthew G Knepley   PetscFunctionBegin;
841c7543cceSMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
842c7543cceSMatthew G Knepley   if (n) PetscValidIntPointer(n, 2);
843cac4c232SBarry Smith   PetscUseMethod(snes, "SNESMultiblockGetSubSNES_C", (SNES, PetscInt *, SNES **), (snes, n, subsnes));
844c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
845c7543cceSMatthew G Knepley }
846c7543cceSMatthew G Knepley 
847c7543cceSMatthew G Knepley /*MC
848c7543cceSMatthew G Knepley   SNESMULTIBLOCK - Multiblock nonlinear solver that can use overlapping or nonoverlapping blocks, organized
849c7543cceSMatthew G Knepley   additively (Jacobi) or multiplicatively (Gauss-Seidel).
850c7543cceSMatthew G Knepley 
851c7543cceSMatthew G Knepley   Level: beginner
852c7543cceSMatthew G Knepley 
853f6dfbefdSBarry Smith .seealso: `SNESCreate()`, `SNES`, `SNESSetType()`, `SNESNEWTONLS`, `SNESNEWTONTR`, `SNESNRICHARDSON`, `SNESMultiblockSetType()`,
854f6dfbefdSBarry Smith           `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,`PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`
855c7543cceSMatthew G Knepley M*/
8569371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode SNESCreate_Multiblock(SNES snes) {
857c7543cceSMatthew G Knepley   SNES_Multiblock *mb;
858c7543cceSMatthew G Knepley 
859c7543cceSMatthew G Knepley   PetscFunctionBegin;
860c7543cceSMatthew G Knepley   snes->ops->destroy        = SNESDestroy_Multiblock;
861c7543cceSMatthew G Knepley   snes->ops->setup          = SNESSetUp_Multiblock;
862c7543cceSMatthew G Knepley   snes->ops->setfromoptions = SNESSetFromOptions_Multiblock;
863c7543cceSMatthew G Knepley   snes->ops->view           = SNESView_Multiblock;
864c7543cceSMatthew G Knepley   snes->ops->solve          = SNESSolve_Multiblock;
865c7543cceSMatthew G Knepley   snes->ops->reset          = SNESReset_Multiblock;
866c7543cceSMatthew G Knepley 
8672c155ee1SBarry Smith   snes->usesksp = PETSC_FALSE;
868d8d34be6SBarry Smith   snes->usesnpc = PETSC_FALSE;
8692c155ee1SBarry Smith 
8704fc747eaSLawrence Mitchell   snes->alwayscomputesfinalresidual = PETSC_TRUE;
8714fc747eaSLawrence Mitchell 
872*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mb));
873c7543cceSMatthew G Knepley   snes->data    = (void *)mb;
874c7543cceSMatthew G Knepley   mb->defined   = PETSC_FALSE;
875c7543cceSMatthew G Knepley   mb->numBlocks = 0;
876c7543cceSMatthew G Knepley   mb->bs        = -1;
877c7543cceSMatthew G Knepley   mb->type      = PC_COMPOSITE_MULTIPLICATIVE;
878c7543cceSMatthew G Knepley 
879c7543cceSMatthew G Knepley   /* We attach functions so that they can be called on another PC without crashing the program */
8809566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSetFields_C", SNESMultiblockSetFields_Default));
8819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSetIS_C", SNESMultiblockSetIS_Default));
8829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSetType_C", SNESMultiblockSetType_Default));
8839566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockSetBlockSize_C", SNESMultiblockSetBlockSize_Default));
8849566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESMultiblockGetSubSNES_C", SNESMultiblockGetSubSNES_Default));
885c7543cceSMatthew G Knepley   PetscFunctionReturn(0);
886c7543cceSMatthew G Knepley }
887