xref: /petsc/src/dm/impls/composite/pack.c (revision 0f21e855378ab50d5841671960a70849a76e210e)
147c6ae99SBarry Smith 
2ccd284c7SBarry Smith #include <../src/dm/impls/composite/packimpl.h>       /*I  "petscdmcomposite.h"  I*/
3*0f21e855SMatthew G. Knepley #include <petscproblem.h>
447c6ae99SBarry Smith 
547c6ae99SBarry Smith #undef __FUNCT__
647c6ae99SBarry Smith #define __FUNCT__ "DMCompositeSetCoupling"
747c6ae99SBarry Smith /*@C
847c6ae99SBarry Smith     DMCompositeSetCoupling - Sets user provided routines that compute the coupling between the
99ae5db72SJed Brown       seperate components (DMs) in a DMto build the correct matrix nonzero structure.
1047c6ae99SBarry Smith 
1147c6ae99SBarry Smith 
1247c6ae99SBarry Smith     Logically Collective on MPI_Comm
1347c6ae99SBarry Smith 
1447c6ae99SBarry Smith     Input Parameter:
1547c6ae99SBarry Smith +   dm - the composite object
1647c6ae99SBarry Smith -   formcouplelocations - routine to set the nonzero locations in the matrix
1747c6ae99SBarry Smith 
1847c6ae99SBarry Smith     Level: advanced
1947c6ae99SBarry Smith 
201b2093e4SBarry Smith     Notes: See DMSetApplicationContext() and DMGetApplicationContext() for how to get user information into
2147c6ae99SBarry Smith         this routine
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith @*/
247087cfbeSBarry Smith PetscErrorCode  DMCompositeSetCoupling(DM dm,PetscErrorCode (*FormCoupleLocations)(DM,Mat,PetscInt*,PetscInt*,PetscInt,PetscInt,PetscInt,PetscInt))
2547c6ae99SBarry Smith {
2647c6ae99SBarry Smith   DM_Composite *com = (DM_Composite*)dm->data;
2747c6ae99SBarry Smith 
2847c6ae99SBarry Smith   PetscFunctionBegin;
2947c6ae99SBarry Smith   com->FormCoupleLocations = FormCoupleLocations;
3047c6ae99SBarry Smith   PetscFunctionReturn(0);
3147c6ae99SBarry Smith }
3247c6ae99SBarry Smith 
3347c6ae99SBarry Smith #undef __FUNCT__
340c010503SBarry Smith #define __FUNCT__ "DMDestroy_Composite"
356bf464f9SBarry Smith PetscErrorCode  DMDestroy_Composite(DM dm)
3647c6ae99SBarry Smith {
3747c6ae99SBarry Smith   PetscErrorCode         ierr;
3847c6ae99SBarry Smith   struct DMCompositeLink *next, *prev;
3947c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
4047c6ae99SBarry Smith 
4147c6ae99SBarry Smith   PetscFunctionBegin;
4247c6ae99SBarry Smith   next = com->next;
4347c6ae99SBarry Smith   while (next) {
4447c6ae99SBarry Smith     prev = next;
4547c6ae99SBarry Smith     next = next->next;
46fcfd50ebSBarry Smith     ierr = DMDestroy(&prev->dm);CHKERRQ(ierr);
4747c6ae99SBarry Smith     ierr = PetscFree(prev->grstarts);CHKERRQ(ierr);
4847c6ae99SBarry Smith     ierr = PetscFree(prev);CHKERRQ(ierr);
4947c6ae99SBarry Smith   }
50435a35e8SMatthew G Knepley   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
51435a35e8SMatthew G Knepley   ierr = PetscFree(com);CHKERRQ(ierr);
5247c6ae99SBarry Smith   PetscFunctionReturn(0);
5347c6ae99SBarry Smith }
5447c6ae99SBarry Smith 
5547c6ae99SBarry Smith #undef __FUNCT__
560c010503SBarry Smith #define __FUNCT__ "DMView_Composite"
577087cfbeSBarry Smith PetscErrorCode  DMView_Composite(DM dm,PetscViewer v)
5847c6ae99SBarry Smith {
5947c6ae99SBarry Smith   PetscErrorCode ierr;
6047c6ae99SBarry Smith   PetscBool      iascii;
6147c6ae99SBarry Smith   DM_Composite   *com = (DM_Composite*)dm->data;
6247c6ae99SBarry Smith 
6347c6ae99SBarry Smith   PetscFunctionBegin;
64251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6547c6ae99SBarry Smith   if (iascii) {
6647c6ae99SBarry Smith     struct DMCompositeLink *lnk = com->next;
6747c6ae99SBarry Smith     PetscInt               i;
6847c6ae99SBarry Smith 
6947c6ae99SBarry Smith     ierr = PetscViewerASCIIPrintf(v,"DM (%s)\n",((PetscObject)dm)->prefix ? ((PetscObject)dm)->prefix : "no prefix");CHKERRQ(ierr);
709ae5db72SJed Brown     ierr = PetscViewerASCIIPrintf(v,"  contains %D DMs\n",com->nDM);CHKERRQ(ierr);
7147c6ae99SBarry Smith     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
7247c6ae99SBarry Smith     for (i=0; lnk; lnk=lnk->next,i++) {
739ae5db72SJed Brown       ierr = PetscViewerASCIIPrintf(v,"Link %D: DM of type %s\n",i,((PetscObject)lnk->dm)->type_name);CHKERRQ(ierr);
7447c6ae99SBarry Smith       ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
7547c6ae99SBarry Smith       ierr = DMView(lnk->dm,v);CHKERRQ(ierr);
7647c6ae99SBarry Smith       ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
7747c6ae99SBarry Smith     }
7847c6ae99SBarry Smith     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
7947c6ae99SBarry Smith   }
8047c6ae99SBarry Smith   PetscFunctionReturn(0);
8147c6ae99SBarry Smith }
8247c6ae99SBarry Smith 
8347c6ae99SBarry Smith /* --------------------------------------------------------------------------------------*/
8447c6ae99SBarry Smith #undef __FUNCT__
85d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp_Composite"
867087cfbeSBarry Smith PetscErrorCode  DMSetUp_Composite(DM dm)
8747c6ae99SBarry Smith {
8847c6ae99SBarry Smith   PetscErrorCode         ierr;
8947c6ae99SBarry Smith   PetscInt               nprev = 0;
9047c6ae99SBarry Smith   PetscMPIInt            rank,size;
9147c6ae99SBarry Smith   DM_Composite           *com  = (DM_Composite*)dm->data;
9247c6ae99SBarry Smith   struct DMCompositeLink *next = com->next;
9347c6ae99SBarry Smith   PetscLayout            map;
9447c6ae99SBarry Smith 
9547c6ae99SBarry Smith   PetscFunctionBegin;
96ce94432eSBarry Smith   if (com->setup) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Packer has already been setup");
97ce94432eSBarry Smith   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm),&map);CHKERRQ(ierr);
9847c6ae99SBarry Smith   ierr = PetscLayoutSetLocalSize(map,com->n);CHKERRQ(ierr);
9947c6ae99SBarry Smith   ierr = PetscLayoutSetSize(map,PETSC_DETERMINE);CHKERRQ(ierr);
10047c6ae99SBarry Smith   ierr = PetscLayoutSetBlockSize(map,1);CHKERRQ(ierr);
10147c6ae99SBarry Smith   ierr = PetscLayoutSetUp(map);CHKERRQ(ierr);
10247c6ae99SBarry Smith   ierr = PetscLayoutGetSize(map,&com->N);CHKERRQ(ierr);
1030298fd71SBarry Smith   ierr = PetscLayoutGetRange(map,&com->rstart,NULL);CHKERRQ(ierr);
104fcfd50ebSBarry Smith   ierr = PetscLayoutDestroy(&map);CHKERRQ(ierr);
10547c6ae99SBarry Smith 
1069ae5db72SJed Brown   /* now set the rstart for each linked vector */
107ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
108ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
10947c6ae99SBarry Smith   while (next) {
11047c6ae99SBarry Smith     next->rstart  = nprev;
11106ebdd98SJed Brown     nprev        += next->n;
11247c6ae99SBarry Smith     next->grstart = com->rstart + next->rstart;
113785e854fSJed Brown     ierr          = PetscMalloc1(size,&next->grstarts);CHKERRQ(ierr);
114ce94432eSBarry Smith     ierr          = MPI_Allgather(&next->grstart,1,MPIU_INT,next->grstarts,1,MPIU_INT,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
11547c6ae99SBarry Smith     next          = next->next;
11647c6ae99SBarry Smith   }
11747c6ae99SBarry Smith   com->setup = PETSC_TRUE;
11847c6ae99SBarry Smith   PetscFunctionReturn(0);
11947c6ae99SBarry Smith }
12047c6ae99SBarry Smith 
12147c6ae99SBarry Smith /* ----------------------------------------------------------------------------------*/
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith #undef __FUNCT__
12447c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetNumberDM"
12573e31fe2SJed Brown /*@
12647c6ae99SBarry Smith     DMCompositeGetNumberDM - Get's the number of DM objects in the DMComposite
12747c6ae99SBarry Smith        representation.
12847c6ae99SBarry Smith 
12947c6ae99SBarry Smith     Not Collective
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith     Input Parameter:
13247c6ae99SBarry Smith .    dm - the packer object
13347c6ae99SBarry Smith 
13447c6ae99SBarry Smith     Output Parameter:
13547c6ae99SBarry Smith .     nDM - the number of DMs
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith     Level: beginner
13847c6ae99SBarry Smith 
13947c6ae99SBarry Smith @*/
1407087cfbeSBarry Smith PetscErrorCode  DMCompositeGetNumberDM(DM dm,PetscInt *nDM)
14147c6ae99SBarry Smith {
14247c6ae99SBarry Smith   DM_Composite *com = (DM_Composite*)dm->data;
1435fd66863SKarl Rupp 
14447c6ae99SBarry Smith   PetscFunctionBegin;
14547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14647c6ae99SBarry Smith   *nDM = com->nDM;
14747c6ae99SBarry Smith   PetscFunctionReturn(0);
14847c6ae99SBarry Smith }
14947c6ae99SBarry Smith 
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith #undef __FUNCT__
15247c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetAccess"
15347c6ae99SBarry Smith /*@C
15447c6ae99SBarry Smith     DMCompositeGetAccess - Allows one to access the individual packed vectors in their global
15547c6ae99SBarry Smith        representation.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     Collective on DMComposite
15847c6ae99SBarry Smith 
1599ae5db72SJed Brown     Input Parameters:
16047c6ae99SBarry Smith +    dm - the packer object
1619ae5db72SJed Brown -    gvec - the global vector
1629ae5db72SJed Brown 
1639ae5db72SJed Brown     Output Parameters:
1640298fd71SBarry Smith .    Vec* ... - the packed parallel vectors, NULL for those that are not needed
16547c6ae99SBarry Smith 
16647c6ae99SBarry Smith     Notes: Use DMCompositeRestoreAccess() to return the vectors when you no longer need them
16747c6ae99SBarry Smith 
168f73e5cebSJed Brown     Fortran Notes:
169f73e5cebSJed Brown 
170f73e5cebSJed Brown     Fortran callers must use numbered versions of this routine, e.g., DMCompositeGetAccess4(dm,gvec,vec1,vec2,vec3,vec4)
171f73e5cebSJed Brown     or use the alternative interface DMCompositeGetAccessArray().
172f73e5cebSJed Brown 
17347c6ae99SBarry Smith     Level: advanced
17447c6ae99SBarry Smith 
175f73e5cebSJed Brown .seealso: DMCompositeGetEntries(), DMCompositeScatter()
17647c6ae99SBarry Smith @*/
1777087cfbeSBarry Smith PetscErrorCode  DMCompositeGetAccess(DM dm,Vec gvec,...)
17847c6ae99SBarry Smith {
17947c6ae99SBarry Smith   va_list                Argp;
18047c6ae99SBarry Smith   PetscErrorCode         ierr;
18147c6ae99SBarry Smith   struct DMCompositeLink *next;
18247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
18347c6ae99SBarry Smith 
18447c6ae99SBarry Smith   PetscFunctionBegin;
18547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18647c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
18747c6ae99SBarry Smith   next = com->next;
18847c6ae99SBarry Smith   if (!com->setup) {
189d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
19047c6ae99SBarry Smith   }
19147c6ae99SBarry Smith 
19247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
19347c6ae99SBarry Smith   va_start(Argp,gvec);
19447c6ae99SBarry Smith   while (next) {
19547c6ae99SBarry Smith     Vec *vec;
19647c6ae99SBarry Smith     vec = va_arg(Argp, Vec*);
1979ae5db72SJed Brown     if (vec) {
1989ae5db72SJed Brown       PetscScalar *array;
1999ae5db72SJed Brown       ierr = DMGetGlobalVector(next->dm,vec);CHKERRQ(ierr);
2009ae5db72SJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
2019ae5db72SJed Brown       ierr = VecPlaceArray(*vec,array+next->rstart);CHKERRQ(ierr);
2029ae5db72SJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
20347c6ae99SBarry Smith     }
20447c6ae99SBarry Smith     next = next->next;
20547c6ae99SBarry Smith   }
20647c6ae99SBarry Smith   va_end(Argp);
20747c6ae99SBarry Smith   PetscFunctionReturn(0);
20847c6ae99SBarry Smith }
20947c6ae99SBarry Smith 
21047c6ae99SBarry Smith #undef __FUNCT__
211f73e5cebSJed Brown #define __FUNCT__ "DMCompositeGetAccessArray"
212f73e5cebSJed Brown /*@C
213f73e5cebSJed Brown     DMCompositeGetAccessArray - Allows one to access the individual packed vectors in their global
214f73e5cebSJed Brown        representation.
215f73e5cebSJed Brown 
216f73e5cebSJed Brown     Collective on DMComposite
217f73e5cebSJed Brown 
218f73e5cebSJed Brown     Input Parameters:
219f73e5cebSJed Brown +    dm - the packer object
220f73e5cebSJed Brown .    pvec - packed vector
221f73e5cebSJed Brown .    nwanted - number of vectors wanted
2220298fd71SBarry Smith -    wanted - sorted array of vectors wanted, or NULL to get all vectors
223f73e5cebSJed Brown 
224f73e5cebSJed Brown     Output Parameters:
225f73e5cebSJed Brown .    vecs - array of requested global vectors (must be allocated)
226f73e5cebSJed Brown 
227f73e5cebSJed Brown     Notes: Use DMCompositeRestoreAccessArray() to return the vectors when you no longer need them
228f73e5cebSJed Brown 
229f73e5cebSJed Brown     Level: advanced
230f73e5cebSJed Brown 
231f73e5cebSJed Brown .seealso: DMCompositeGetAccess(), DMCompositeGetEntries(), DMCompositeScatter(), DMCompositeGather()
232f73e5cebSJed Brown @*/
233f73e5cebSJed Brown PetscErrorCode  DMCompositeGetAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs)
234f73e5cebSJed Brown {
235f73e5cebSJed Brown   PetscErrorCode         ierr;
236f73e5cebSJed Brown   struct DMCompositeLink *link;
237f73e5cebSJed Brown   PetscInt               i,wnum;
238f73e5cebSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
239f73e5cebSJed Brown 
240f73e5cebSJed Brown   PetscFunctionBegin;
241f73e5cebSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
242f73e5cebSJed Brown   PetscValidHeaderSpecific(pvec,VEC_CLASSID,2);
243f73e5cebSJed Brown   if (!com->setup) {
244f73e5cebSJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr);
245f73e5cebSJed Brown   }
246f73e5cebSJed Brown 
247f73e5cebSJed Brown   for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) {
248f73e5cebSJed Brown     if (!wanted || i == wanted[wnum]) {
249f73e5cebSJed Brown       PetscScalar *array;
250f73e5cebSJed Brown       Vec v;
251f73e5cebSJed Brown       ierr = DMGetGlobalVector(link->dm,&v);CHKERRQ(ierr);
252f73e5cebSJed Brown       ierr = VecGetArray(pvec,&array);CHKERRQ(ierr);
253f73e5cebSJed Brown       ierr = VecPlaceArray(v,array+link->rstart);CHKERRQ(ierr);
254f73e5cebSJed Brown       ierr = VecRestoreArray(pvec,&array);CHKERRQ(ierr);
255f73e5cebSJed Brown       vecs[wnum++] = v;
256f73e5cebSJed Brown     }
257f73e5cebSJed Brown   }
258f73e5cebSJed Brown   PetscFunctionReturn(0);
259f73e5cebSJed Brown }
260f73e5cebSJed Brown 
261f73e5cebSJed Brown #undef __FUNCT__
26247c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreAccess"
26347c6ae99SBarry Smith /*@C
264aa219208SBarry Smith     DMCompositeRestoreAccess - Returns the vectors obtained with DMCompositeGetAccess()
26547c6ae99SBarry Smith        representation.
26647c6ae99SBarry Smith 
26747c6ae99SBarry Smith     Collective on DMComposite
26847c6ae99SBarry Smith 
2699ae5db72SJed Brown     Input Parameters:
27047c6ae99SBarry Smith +    dm - the packer object
27147c6ae99SBarry Smith .    gvec - the global vector
2720298fd71SBarry Smith -    Vec* ... - the individual parallel vectors, NULL for those that are not needed
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith     Level: advanced
27547c6ae99SBarry Smith 
2769ae5db72SJed Brown .seealso  DMCompositeAddDM(), DMCreateGlobalVector(),
2776eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeScatter(),
278aa219208SBarry Smith          DMCompositeRestoreAccess(), DMCompositeGetAccess()
27947c6ae99SBarry Smith 
28047c6ae99SBarry Smith @*/
2817087cfbeSBarry Smith PetscErrorCode  DMCompositeRestoreAccess(DM dm,Vec gvec,...)
28247c6ae99SBarry Smith {
28347c6ae99SBarry Smith   va_list                Argp;
28447c6ae99SBarry Smith   PetscErrorCode         ierr;
28547c6ae99SBarry Smith   struct DMCompositeLink *next;
28647c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
28747c6ae99SBarry Smith 
28847c6ae99SBarry Smith   PetscFunctionBegin;
28947c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29047c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
29147c6ae99SBarry Smith   next = com->next;
29247c6ae99SBarry Smith   if (!com->setup) {
293d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
29447c6ae99SBarry Smith   }
29547c6ae99SBarry Smith 
29647c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
29747c6ae99SBarry Smith   va_start(Argp,gvec);
29847c6ae99SBarry Smith   while (next) {
29947c6ae99SBarry Smith     Vec *vec;
30047c6ae99SBarry Smith     vec = va_arg(Argp, Vec*);
3019ae5db72SJed Brown     if (vec) {
3029ae5db72SJed Brown       ierr = VecResetArray(*vec);CHKERRQ(ierr);
3039ae5db72SJed Brown       ierr = DMRestoreGlobalVector(next->dm,vec);CHKERRQ(ierr);
30447c6ae99SBarry Smith     }
30547c6ae99SBarry Smith     next = next->next;
30647c6ae99SBarry Smith   }
30747c6ae99SBarry Smith   va_end(Argp);
30847c6ae99SBarry Smith   PetscFunctionReturn(0);
30947c6ae99SBarry Smith }
31047c6ae99SBarry Smith 
31147c6ae99SBarry Smith #undef __FUNCT__
312f73e5cebSJed Brown #define __FUNCT__ "DMCompositeRestoreAccessArray"
313f73e5cebSJed Brown /*@C
314f73e5cebSJed Brown     DMCompositeRestoreAccessArray - Returns the vectors obtained with DMCompositeGetAccessArray()
315f73e5cebSJed Brown 
316f73e5cebSJed Brown     Collective on DMComposite
317f73e5cebSJed Brown 
318f73e5cebSJed Brown     Input Parameters:
319f73e5cebSJed Brown +    dm - the packer object
320f73e5cebSJed Brown .    pvec - packed vector
321f73e5cebSJed Brown .    nwanted - number of vectors wanted
3220298fd71SBarry Smith .    wanted - sorted array of vectors wanted, or NULL to get all vectors
323f73e5cebSJed Brown -    vecs - array of global vectors to return
324f73e5cebSJed Brown 
325f73e5cebSJed Brown     Level: advanced
326f73e5cebSJed Brown 
327f73e5cebSJed Brown .seealso: DMCompositeRestoreAccess(), DMCompositeRestoreEntries(), DMCompositeScatter(), DMCompositeGather()
328f73e5cebSJed Brown @*/
329f73e5cebSJed Brown PetscErrorCode  DMCompositeRestoreAccessArray(DM dm,Vec pvec,PetscInt nwanted,const PetscInt *wanted,Vec *vecs)
330f73e5cebSJed Brown {
331f73e5cebSJed Brown   PetscErrorCode         ierr;
332f73e5cebSJed Brown   struct DMCompositeLink *link;
333f73e5cebSJed Brown   PetscInt               i,wnum;
334f73e5cebSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
335f73e5cebSJed Brown 
336f73e5cebSJed Brown   PetscFunctionBegin;
337f73e5cebSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338f73e5cebSJed Brown   PetscValidHeaderSpecific(pvec,VEC_CLASSID,2);
339f73e5cebSJed Brown   if (!com->setup) {
340f73e5cebSJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr);
341f73e5cebSJed Brown   }
342f73e5cebSJed Brown 
343f73e5cebSJed Brown   for (i=0,wnum=0,link=com->next; link && wnum<nwanted; i++,link=link->next) {
344f73e5cebSJed Brown     if (!wanted || i == wanted[wnum]) {
345f73e5cebSJed Brown       ierr = VecResetArray(vecs[wnum]);CHKERRQ(ierr);
346f73e5cebSJed Brown       ierr = DMRestoreGlobalVector(link->dm,&vecs[wnum]);CHKERRQ(ierr);
347f73e5cebSJed Brown       wnum++;
348f73e5cebSJed Brown     }
349f73e5cebSJed Brown   }
350f73e5cebSJed Brown   PetscFunctionReturn(0);
351f73e5cebSJed Brown }
352f73e5cebSJed Brown 
353f73e5cebSJed Brown #undef __FUNCT__
35447c6ae99SBarry Smith #define __FUNCT__ "DMCompositeScatter"
35547c6ae99SBarry Smith /*@C
35647c6ae99SBarry Smith     DMCompositeScatter - Scatters from a global packed vector into its individual local vectors
35747c6ae99SBarry Smith 
35847c6ae99SBarry Smith     Collective on DMComposite
35947c6ae99SBarry Smith 
3609ae5db72SJed Brown     Input Parameters:
36147c6ae99SBarry Smith +    dm - the packer object
36247c6ae99SBarry Smith .    gvec - the global vector
3630298fd71SBarry Smith -    Vec ... - the individual sequential vectors, NULL for those that are not needed
36447c6ae99SBarry Smith 
36547c6ae99SBarry Smith     Level: advanced
36647c6ae99SBarry Smith 
3676f3c3dcfSJed Brown     Notes:
3686f3c3dcfSJed Brown     DMCompositeScatterArray() is a non-variadic alternative that is often more convenient for library callers and is
3696f3c3dcfSJed Brown     accessible from Fortran.
3706f3c3dcfSJed Brown 
3719ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
3726eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
37347c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
3746f3c3dcfSJed Brown          DMCompositeScatterArray()
37547c6ae99SBarry Smith 
37647c6ae99SBarry Smith @*/
3777087cfbeSBarry Smith PetscErrorCode  DMCompositeScatter(DM dm,Vec gvec,...)
37847c6ae99SBarry Smith {
37947c6ae99SBarry Smith   va_list                Argp;
38047c6ae99SBarry Smith   PetscErrorCode         ierr;
38147c6ae99SBarry Smith   struct DMCompositeLink *next;
3828fd8f222SJed Brown   PetscInt               cnt;
38347c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
38447c6ae99SBarry Smith 
38547c6ae99SBarry Smith   PetscFunctionBegin;
38647c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38747c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
38847c6ae99SBarry Smith   if (!com->setup) {
389d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
39047c6ae99SBarry Smith   }
39147c6ae99SBarry Smith 
39247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
39347c6ae99SBarry Smith   va_start(Argp,gvec);
3948fd8f222SJed Brown   for (cnt=3,next=com->next; next; cnt++,next=next->next) {
3959ae5db72SJed Brown     Vec local;
3969ae5db72SJed Brown     local = va_arg(Argp, Vec);
3979ae5db72SJed Brown     if (local) {
3989ae5db72SJed Brown       Vec         global;
39947c6ae99SBarry Smith       PetscScalar *array;
4009ae5db72SJed Brown       PetscValidHeaderSpecific(local,VEC_CLASSID,cnt);
4019ae5db72SJed Brown       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
4029ae5db72SJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
4039ae5db72SJed Brown       ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr);
4049ae5db72SJed Brown       ierr = DMGlobalToLocalBegin(next->dm,global,INSERT_VALUES,local);CHKERRQ(ierr);
4059ae5db72SJed Brown       ierr = DMGlobalToLocalEnd(next->dm,global,INSERT_VALUES,local);CHKERRQ(ierr);
4069ae5db72SJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
4079ae5db72SJed Brown       ierr = VecResetArray(global);CHKERRQ(ierr);
4089ae5db72SJed Brown       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
40947c6ae99SBarry Smith     }
41047c6ae99SBarry Smith   }
41147c6ae99SBarry Smith   va_end(Argp);
41247c6ae99SBarry Smith   PetscFunctionReturn(0);
41347c6ae99SBarry Smith }
41447c6ae99SBarry Smith 
41547c6ae99SBarry Smith #undef __FUNCT__
4166f3c3dcfSJed Brown #define __FUNCT__ "DMCompositeScatterArray"
4176f3c3dcfSJed Brown /*@
4186f3c3dcfSJed Brown     DMCompositeScatterArray - Scatters from a global packed vector into its individual local vectors
4196f3c3dcfSJed Brown 
4206f3c3dcfSJed Brown     Collective on DMComposite
4216f3c3dcfSJed Brown 
4226f3c3dcfSJed Brown     Input Parameters:
4236f3c3dcfSJed Brown +    dm - the packer object
4246f3c3dcfSJed Brown .    gvec - the global vector
4256f3c3dcfSJed Brown .    lvecs - array of local vectors, NULL for any that are not needed
4266f3c3dcfSJed Brown 
4276f3c3dcfSJed Brown     Level: advanced
4286f3c3dcfSJed Brown 
4296f3c3dcfSJed Brown     Note:
4306f3c3dcfSJed Brown     This is a non-variadic alternative to DMCompositeScatterArray()
4316f3c3dcfSJed Brown 
4326f3c3dcfSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector()
4336f3c3dcfSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
4346f3c3dcfSJed Brown          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
4356f3c3dcfSJed Brown 
4366f3c3dcfSJed Brown @*/
4376f3c3dcfSJed Brown PetscErrorCode  DMCompositeScatterArray(DM dm,Vec gvec,Vec *lvecs)
4386f3c3dcfSJed Brown {
4396f3c3dcfSJed Brown   PetscErrorCode         ierr;
4406f3c3dcfSJed Brown   struct DMCompositeLink *next;
4416f3c3dcfSJed Brown   PetscInt               i;
4426f3c3dcfSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
4436f3c3dcfSJed Brown 
4446f3c3dcfSJed Brown   PetscFunctionBegin;
4456f3c3dcfSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4466f3c3dcfSJed Brown   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
4476f3c3dcfSJed Brown   if (!com->setup) {
4486f3c3dcfSJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr);
4496f3c3dcfSJed Brown   }
4506f3c3dcfSJed Brown 
4516f3c3dcfSJed Brown   /* loop over packed objects, handling one at at time */
4526f3c3dcfSJed Brown   for (i=0,next=com->next; next; next=next->next,i++) {
4536f3c3dcfSJed Brown     if (lvecs[i]) {
4546f3c3dcfSJed Brown       Vec         global;
4556f3c3dcfSJed Brown       PetscScalar *array;
4566f3c3dcfSJed Brown       PetscValidHeaderSpecific(lvecs[i],VEC_CLASSID,3);
4576f3c3dcfSJed Brown       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
4586f3c3dcfSJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
4596f3c3dcfSJed Brown       ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr);
4606f3c3dcfSJed Brown       ierr = DMGlobalToLocalBegin(next->dm,global,INSERT_VALUES,lvecs[i]);CHKERRQ(ierr);
4616f3c3dcfSJed Brown       ierr = DMGlobalToLocalEnd(next->dm,global,INSERT_VALUES,lvecs[i]);CHKERRQ(ierr);
4626f3c3dcfSJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
4636f3c3dcfSJed Brown       ierr = VecResetArray(global);CHKERRQ(ierr);
4646f3c3dcfSJed Brown       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
4656f3c3dcfSJed Brown     }
4666f3c3dcfSJed Brown   }
4676f3c3dcfSJed Brown   PetscFunctionReturn(0);
4686f3c3dcfSJed Brown }
4696f3c3dcfSJed Brown 
4706f3c3dcfSJed Brown #undef __FUNCT__
47147c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGather"
47247c6ae99SBarry Smith /*@C
47347c6ae99SBarry Smith     DMCompositeGather - Gathers into a global packed vector from its individual local vectors
47447c6ae99SBarry Smith 
47547c6ae99SBarry Smith     Collective on DMComposite
47647c6ae99SBarry Smith 
47747c6ae99SBarry Smith     Input Parameter:
47847c6ae99SBarry Smith +    dm - the packer object
47947c6ae99SBarry Smith .    gvec - the global vector
4800298fd71SBarry Smith -    Vec ... - the individual sequential vectors, NULL for any that are not needed
48147c6ae99SBarry Smith 
48247c6ae99SBarry Smith     Level: advanced
48347c6ae99SBarry Smith 
4849ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
4856eb61c8cSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
48647c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
48747c6ae99SBarry Smith 
48847c6ae99SBarry Smith @*/
4897087cfbeSBarry Smith PetscErrorCode  DMCompositeGather(DM dm,Vec gvec,InsertMode imode,...)
49047c6ae99SBarry Smith {
49147c6ae99SBarry Smith   va_list                Argp;
49247c6ae99SBarry Smith   PetscErrorCode         ierr;
49347c6ae99SBarry Smith   struct DMCompositeLink *next;
49447c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
4958fd8f222SJed Brown   PetscInt               cnt;
49647c6ae99SBarry Smith 
49747c6ae99SBarry Smith   PetscFunctionBegin;
49847c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49947c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
50047c6ae99SBarry Smith   if (!com->setup) {
501d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
50247c6ae99SBarry Smith   }
50347c6ae99SBarry Smith 
50447c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
505df0c820aSJed Brown   va_start(Argp,imode);
5068fd8f222SJed Brown   for (cnt=3,next=com->next; next; cnt++,next=next->next) {
5079ae5db72SJed Brown     Vec local;
5089ae5db72SJed Brown     local = va_arg(Argp, Vec);
5099ae5db72SJed Brown     if (local) {
51047c6ae99SBarry Smith       PetscScalar *array;
5119ae5db72SJed Brown       Vec         global;
5129ae5db72SJed Brown       PetscValidHeaderSpecific(local,VEC_CLASSID,cnt);
5139ae5db72SJed Brown       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
5149ae5db72SJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
5159ae5db72SJed Brown       ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr);
5169ae5db72SJed Brown       ierr = DMLocalToGlobalBegin(next->dm,local,imode,global);CHKERRQ(ierr);
5179ae5db72SJed Brown       ierr = DMLocalToGlobalEnd(next->dm,local,imode,global);CHKERRQ(ierr);
5189ae5db72SJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
5199ae5db72SJed Brown       ierr = VecResetArray(global);CHKERRQ(ierr);
5209ae5db72SJed Brown       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
52147c6ae99SBarry Smith     }
52247c6ae99SBarry Smith   }
52347c6ae99SBarry Smith   va_end(Argp);
52447c6ae99SBarry Smith   PetscFunctionReturn(0);
52547c6ae99SBarry Smith }
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith #undef __FUNCT__
5286f3c3dcfSJed Brown #define __FUNCT__ "DMCompositeGatherArray"
5296f3c3dcfSJed Brown /*@
5306f3c3dcfSJed Brown     DMCompositeGatherArray - Gathers into a global packed vector from its individual local vectors
5316f3c3dcfSJed Brown 
5326f3c3dcfSJed Brown     Collective on DMComposite
5336f3c3dcfSJed Brown 
5346f3c3dcfSJed Brown     Input Parameter:
5356f3c3dcfSJed Brown +    dm - the packer object
5366f3c3dcfSJed Brown .    gvec - the global vector
5376f3c3dcfSJed Brown -    lvecs - the individual sequential vectors, NULL for any that are not needed
5386f3c3dcfSJed Brown 
5396f3c3dcfSJed Brown     Level: advanced
5406f3c3dcfSJed Brown 
5416f3c3dcfSJed Brown     Notes:
5426f3c3dcfSJed Brown     This is a non-variadic alternative to DMCompositeGather().
5436f3c3dcfSJed Brown 
5446f3c3dcfSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
5456f3c3dcfSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
5466f3c3dcfSJed Brown          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries(),
5476f3c3dcfSJed Brown @*/
5486f3c3dcfSJed Brown PetscErrorCode  DMCompositeGatherArray(DM dm,Vec gvec,InsertMode imode,Vec *lvecs)
5496f3c3dcfSJed Brown {
5506f3c3dcfSJed Brown   PetscErrorCode         ierr;
5516f3c3dcfSJed Brown   struct DMCompositeLink *next;
5526f3c3dcfSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
5536f3c3dcfSJed Brown   PetscInt               i;
5546f3c3dcfSJed Brown 
5556f3c3dcfSJed Brown   PetscFunctionBegin;
5566f3c3dcfSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5576f3c3dcfSJed Brown   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
5586f3c3dcfSJed Brown   if (!com->setup) {
5596f3c3dcfSJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr);
5606f3c3dcfSJed Brown   }
5616f3c3dcfSJed Brown 
5626f3c3dcfSJed Brown   /* loop over packed objects, handling one at at time */
5636f3c3dcfSJed Brown   for (next=com->next,i=0; next; next=next->next,i++) {
5646f3c3dcfSJed Brown     if (lvecs[i]) {
5656f3c3dcfSJed Brown       PetscScalar *array;
5666f3c3dcfSJed Brown       Vec         global;
5676f3c3dcfSJed Brown       PetscValidHeaderSpecific(lvecs[i],VEC_CLASSID,3);
5686f3c3dcfSJed Brown       ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
5696f3c3dcfSJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
5706f3c3dcfSJed Brown       ierr = VecPlaceArray(global,array+next->rstart);CHKERRQ(ierr);
5716f3c3dcfSJed Brown       ierr = DMLocalToGlobalBegin(next->dm,lvecs[i],imode,global);CHKERRQ(ierr);
5726f3c3dcfSJed Brown       ierr = DMLocalToGlobalEnd(next->dm,lvecs[i],imode,global);CHKERRQ(ierr);
5736f3c3dcfSJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
5746f3c3dcfSJed Brown       ierr = VecResetArray(global);CHKERRQ(ierr);
5756f3c3dcfSJed Brown       ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
5766f3c3dcfSJed Brown     }
5776f3c3dcfSJed Brown   }
5786f3c3dcfSJed Brown   PetscFunctionReturn(0);
5796f3c3dcfSJed Brown }
5806f3c3dcfSJed Brown 
5816f3c3dcfSJed Brown #undef __FUNCT__
58247c6ae99SBarry Smith #define __FUNCT__ "DMCompositeAddDM"
58347c6ae99SBarry Smith /*@C
584aa219208SBarry Smith     DMCompositeAddDM - adds a DM  vector to a DMComposite
58547c6ae99SBarry Smith 
58647c6ae99SBarry Smith     Collective on DMComposite
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith     Input Parameter:
58947c6ae99SBarry Smith +    dm - the packer object
59047c6ae99SBarry Smith -    dm - the DM object, if the DM is a da you will need to caste it with a (DM)
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Level: advanced
59347c6ae99SBarry Smith 
5940c010503SBarry Smith .seealso DMDestroy(), DMCompositeGather(), DMCompositeAddDM(), DMCreateGlobalVector(),
5956eb61c8cSJed Brown          DMCompositeScatter(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
59647c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith @*/
5997087cfbeSBarry Smith PetscErrorCode  DMCompositeAddDM(DM dmc,DM dm)
60047c6ae99SBarry Smith {
60147c6ae99SBarry Smith   PetscErrorCode         ierr;
60206ebdd98SJed Brown   PetscInt               n,nlocal;
60347c6ae99SBarry Smith   struct DMCompositeLink *mine,*next;
60406ebdd98SJed Brown   Vec                    global,local;
60547c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dmc->data;
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith   PetscFunctionBegin;
60847c6ae99SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
60947c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
61047c6ae99SBarry Smith   next = com->next;
611ce94432eSBarry Smith   if (com->setup) SETERRQ(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONGSTATE,"Cannot add a DM once you have used the DMComposite");
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith   /* create new link */
614b00a9115SJed Brown   ierr = PetscNew(&mine);CHKERRQ(ierr);
61547c6ae99SBarry Smith   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
61647c6ae99SBarry Smith   ierr = DMGetGlobalVector(dm,&global);CHKERRQ(ierr);
61747c6ae99SBarry Smith   ierr = VecGetLocalSize(global,&n);CHKERRQ(ierr);
61847c6ae99SBarry Smith   ierr = DMRestoreGlobalVector(dm,&global);CHKERRQ(ierr);
61906ebdd98SJed Brown   ierr = DMGetLocalVector(dm,&local);CHKERRQ(ierr);
62006ebdd98SJed Brown   ierr = VecGetSize(local,&nlocal);CHKERRQ(ierr);
62106ebdd98SJed Brown   ierr = DMRestoreLocalVector(dm,&local);CHKERRQ(ierr);
6228865f1eaSKarl Rupp 
62347c6ae99SBarry Smith   mine->n      = n;
62406ebdd98SJed Brown   mine->nlocal = nlocal;
62547c6ae99SBarry Smith   mine->dm     = dm;
6260298fd71SBarry Smith   mine->next   = NULL;
62747c6ae99SBarry Smith   com->n      += n;
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith   /* add to end of list */
6308865f1eaSKarl Rupp   if (!next) com->next = mine;
6318865f1eaSKarl Rupp   else {
63247c6ae99SBarry Smith     while (next->next) next = next->next;
63347c6ae99SBarry Smith     next->next = mine;
63447c6ae99SBarry Smith   }
63547c6ae99SBarry Smith   com->nDM++;
63647c6ae99SBarry Smith   com->nmine++;
63747c6ae99SBarry Smith   PetscFunctionReturn(0);
63847c6ae99SBarry Smith }
63947c6ae99SBarry Smith 
6409804daf3SBarry Smith #include <petscdraw.h>
64126887b52SJed Brown PETSC_EXTERN PetscErrorCode  VecView_MPI(Vec,PetscViewer);
64247c6ae99SBarry Smith #undef __FUNCT__
64347c6ae99SBarry Smith #define __FUNCT__ "VecView_DMComposite"
6447087cfbeSBarry Smith PetscErrorCode  VecView_DMComposite(Vec gvec,PetscViewer viewer)
64547c6ae99SBarry Smith {
64647c6ae99SBarry Smith   DM                     dm;
64747c6ae99SBarry Smith   PetscErrorCode         ierr;
64847c6ae99SBarry Smith   struct DMCompositeLink *next;
64947c6ae99SBarry Smith   PetscBool              isdraw;
650cef07954SSatish Balay   DM_Composite           *com;
65147c6ae99SBarry Smith 
65247c6ae99SBarry Smith   PetscFunctionBegin;
653c688c046SMatthew G Knepley   ierr = VecGetDM(gvec, &dm);CHKERRQ(ierr);
654ce94432eSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)gvec),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMComposite");
65547c6ae99SBarry Smith   com  = (DM_Composite*)dm->data;
65647c6ae99SBarry Smith   next = com->next;
65747c6ae99SBarry Smith 
658251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
65947c6ae99SBarry Smith   if (!isdraw) {
66047c6ae99SBarry Smith     /* do I really want to call this? */
66147c6ae99SBarry Smith     ierr = VecView_MPI(gvec,viewer);CHKERRQ(ierr);
66247c6ae99SBarry Smith   } else {
66347c6ae99SBarry Smith     PetscInt cnt = 0;
66447c6ae99SBarry Smith 
66547c6ae99SBarry Smith     /* loop over packed objects, handling one at at time */
66647c6ae99SBarry Smith     while (next) {
66747c6ae99SBarry Smith       Vec         vec;
6689ae5db72SJed Brown       PetscScalar *array;
66947c6ae99SBarry Smith       PetscInt    bs;
67047c6ae99SBarry Smith 
6719ae5db72SJed Brown       /* Should use VecGetSubVector() eventually, but would need to forward the DM for that to work */
6729ae5db72SJed Brown       ierr = DMGetGlobalVector(next->dm,&vec);CHKERRQ(ierr);
6739ae5db72SJed Brown       ierr = VecGetArray(gvec,&array);CHKERRQ(ierr);
6749ae5db72SJed Brown       ierr = VecPlaceArray(vec,array+next->rstart);CHKERRQ(ierr);
6759ae5db72SJed Brown       ierr = VecRestoreArray(gvec,&array);CHKERRQ(ierr);
67647c6ae99SBarry Smith       ierr = VecView(vec,viewer);CHKERRQ(ierr);
67747c6ae99SBarry Smith       ierr = VecGetBlockSize(vec,&bs);CHKERRQ(ierr);
6789ae5db72SJed Brown       ierr = VecResetArray(vec);CHKERRQ(ierr);
6799ae5db72SJed Brown       ierr = DMRestoreGlobalVector(next->dm,&vec);CHKERRQ(ierr);
68047c6ae99SBarry Smith       ierr = PetscViewerDrawBaseAdd(viewer,bs);CHKERRQ(ierr);
68147c6ae99SBarry Smith       cnt += bs;
68247c6ae99SBarry Smith       next = next->next;
68347c6ae99SBarry Smith     }
68447c6ae99SBarry Smith     ierr = PetscViewerDrawBaseAdd(viewer,-cnt);CHKERRQ(ierr);
68547c6ae99SBarry Smith   }
68647c6ae99SBarry Smith   PetscFunctionReturn(0);
68747c6ae99SBarry Smith }
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith #undef __FUNCT__
6900c010503SBarry Smith #define __FUNCT__ "DMCreateGlobalVector_Composite"
6917087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector_Composite(DM dm,Vec *gvec)
69247c6ae99SBarry Smith {
69347c6ae99SBarry Smith   PetscErrorCode ierr;
69447c6ae99SBarry Smith   DM_Composite   *com = (DM_Composite*)dm->data;
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith   PetscFunctionBegin;
69747c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
698d7bf68aeSBarry Smith   ierr = DMSetUp(dm);CHKERRQ(ierr);
699ce94432eSBarry Smith   ierr = VecCreateMPI(PetscObjectComm((PetscObject)dm),com->n,com->N,gvec);CHKERRQ(ierr);
700c688c046SMatthew G Knepley   ierr = VecSetDM(*gvec, dm);CHKERRQ(ierr);
70147c6ae99SBarry Smith   ierr = VecSetOperation(*gvec,VECOP_VIEW,(void (*)(void))VecView_DMComposite);CHKERRQ(ierr);
70247c6ae99SBarry Smith   PetscFunctionReturn(0);
70347c6ae99SBarry Smith }
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith #undef __FUNCT__
7060c010503SBarry Smith #define __FUNCT__ "DMCreateLocalVector_Composite"
7077087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector_Composite(DM dm,Vec *lvec)
70847c6ae99SBarry Smith {
70947c6ae99SBarry Smith   PetscErrorCode ierr;
71047c6ae99SBarry Smith   DM_Composite   *com = (DM_Composite*)dm->data;
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith   PetscFunctionBegin;
71347c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71447c6ae99SBarry Smith   if (!com->setup) {
715d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
71647c6ae99SBarry Smith   }
717ce94432eSBarry Smith   ierr = VecCreateSeq(PetscObjectComm((PetscObject)dm),com->nghost,lvec);CHKERRQ(ierr);
718c688c046SMatthew G Knepley   ierr = VecSetDM(*lvec, dm);CHKERRQ(ierr);
71947c6ae99SBarry Smith   PetscFunctionReturn(0);
72047c6ae99SBarry Smith }
72147c6ae99SBarry Smith 
72247c6ae99SBarry Smith #undef __FUNCT__
7236eb61c8cSJed Brown #define __FUNCT__ "DMCompositeGetISLocalToGlobalMappings"
72447c6ae99SBarry Smith /*@C
7259ae5db72SJed Brown     DMCompositeGetISLocalToGlobalMappings - gets an ISLocalToGlobalMapping for each DM in the DMComposite, maps to the composite global space
72647c6ae99SBarry Smith 
72706ebdd98SJed Brown     Collective on DM
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith     Input Parameter:
73047c6ae99SBarry Smith .    dm - the packer object
73147c6ae99SBarry Smith 
73247c6ae99SBarry Smith     Output Parameters:
7339ae5db72SJed Brown .    ltogs - the individual mappings for each packed vector. Note that this includes
7349ae5db72SJed Brown            all the ghost points that individual ghosted DMDA's may have.
73547c6ae99SBarry Smith 
73647c6ae99SBarry Smith     Level: advanced
73747c6ae99SBarry Smith 
73847c6ae99SBarry Smith     Notes:
7396eb61c8cSJed Brown        Each entry of ltogs should be destroyed with ISLocalToGlobalMappingDestroy(), the ltogs array should be freed with PetscFree().
74047c6ae99SBarry Smith 
7419ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
74247c6ae99SBarry Smith          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(),
74347c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries()
74447c6ae99SBarry Smith 
74547c6ae99SBarry Smith @*/
7467087cfbeSBarry Smith PetscErrorCode  DMCompositeGetISLocalToGlobalMappings(DM dm,ISLocalToGlobalMapping **ltogs)
74747c6ae99SBarry Smith {
74847c6ae99SBarry Smith   PetscErrorCode         ierr;
74947c6ae99SBarry Smith   PetscInt               i,*idx,n,cnt;
75047c6ae99SBarry Smith   struct DMCompositeLink *next;
75147c6ae99SBarry Smith   PetscMPIInt            rank;
75247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
75347c6ae99SBarry Smith 
75447c6ae99SBarry Smith   PetscFunctionBegin;
75547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
756728e99d6SJed Brown   ierr = DMSetUp(dm);CHKERRQ(ierr);
757785e854fSJed Brown   ierr = PetscMalloc1((com->nDM),ltogs);CHKERRQ(ierr);
75847c6ae99SBarry Smith   next = com->next;
759ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
76047c6ae99SBarry Smith 
76147c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
76247c6ae99SBarry Smith   cnt = 0;
76347c6ae99SBarry Smith   while (next) {
7646eb61c8cSJed Brown     ISLocalToGlobalMapping ltog;
7656eb61c8cSJed Brown     PetscMPIInt            size;
76686994e45SJed Brown     const PetscInt         *suboff,*indices;
7676eb61c8cSJed Brown     Vec                    global;
76847c6ae99SBarry Smith 
7696eb61c8cSJed Brown     /* Get sub-DM global indices for each local dof */
7701411c6eeSJed Brown     ierr = DMGetLocalToGlobalMapping(next->dm,&ltog);CHKERRQ(ierr);
7716eb61c8cSJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltog,&n);CHKERRQ(ierr);
77286994e45SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltog,&indices);CHKERRQ(ierr);
773785e854fSJed Brown     ierr = PetscMalloc1(n,&idx);CHKERRQ(ierr);
77447c6ae99SBarry Smith 
7756eb61c8cSJed Brown     /* Get the offsets for the sub-DM global vector */
7766eb61c8cSJed Brown     ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
7776eb61c8cSJed Brown     ierr = VecGetOwnershipRanges(global,&suboff);CHKERRQ(ierr);
778ce94432eSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)global),&size);CHKERRQ(ierr);
7796eb61c8cSJed Brown 
7806eb61c8cSJed Brown     /* Shift the sub-DM definition of the global space to the composite global space */
7816eb61c8cSJed Brown     for (i=0; i<n; i++) {
78286994e45SJed Brown       PetscInt subi = indices[i],lo = 0,hi = size,t;
7836eb61c8cSJed Brown       /* Binary search to find which rank owns subi */
7846eb61c8cSJed Brown       while (hi-lo > 1) {
7856eb61c8cSJed Brown         t = lo + (hi-lo)/2;
7866eb61c8cSJed Brown         if (suboff[t] > subi) hi = t;
7876eb61c8cSJed Brown         else                  lo = t;
7886eb61c8cSJed Brown       }
7896eb61c8cSJed Brown       idx[i] = subi - suboff[lo] + next->grstarts[lo];
7906eb61c8cSJed Brown     }
79186994e45SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltog,&indices);CHKERRQ(ierr);
792f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm),1,n,idx,PETSC_OWN_POINTER,&(*ltogs)[cnt]);CHKERRQ(ierr);
7936eb61c8cSJed Brown     ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
79447c6ae99SBarry Smith     next = next->next;
79547c6ae99SBarry Smith     cnt++;
79647c6ae99SBarry Smith   }
79747c6ae99SBarry Smith   PetscFunctionReturn(0);
79847c6ae99SBarry Smith }
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith #undef __FUNCT__
80187c85e80SJed Brown #define __FUNCT__ "DMCompositeGetLocalISs"
80287c85e80SJed Brown /*@C
8039ae5db72SJed Brown    DMCompositeGetLocalISs - Gets index sets for each component of a composite local vector
80487c85e80SJed Brown 
80587c85e80SJed Brown    Not Collective
80687c85e80SJed Brown 
80787c85e80SJed Brown    Input Arguments:
80887c85e80SJed Brown . dm - composite DM
80987c85e80SJed Brown 
81087c85e80SJed Brown    Output Arguments:
81187c85e80SJed Brown . is - array of serial index sets for each each component of the DMComposite
81287c85e80SJed Brown 
81387c85e80SJed Brown    Level: intermediate
81487c85e80SJed Brown 
81587c85e80SJed Brown    Notes:
81687c85e80SJed Brown    At present, a composite local vector does not normally exist.  This function is used to provide index sets for
81787c85e80SJed Brown    MatGetLocalSubMatrix().  In the future, the scatters for each entry in the DMComposite may be be merged into a single
8189ae5db72SJed Brown    scatter to a composite local vector.  The user should not typically need to know which is being done.
81987c85e80SJed Brown 
82087c85e80SJed Brown    To get the composite global indices at all local points (including ghosts), use DMCompositeGetISLocalToGlobalMappings().
82187c85e80SJed Brown 
82287c85e80SJed Brown    To get index sets for pieces of the composite global vector, use DMCompositeGetGlobalISs().
82387c85e80SJed Brown 
82487c85e80SJed Brown    Each returned IS should be destroyed with ISDestroy(), the array should be freed with PetscFree().
82587c85e80SJed Brown 
82687c85e80SJed Brown .seealso: DMCompositeGetGlobalISs(), DMCompositeGetISLocalToGlobalMappings(), MatGetLocalSubMatrix(), MatCreateLocalRef()
82787c85e80SJed Brown @*/
8287087cfbeSBarry Smith PetscErrorCode  DMCompositeGetLocalISs(DM dm,IS **is)
82987c85e80SJed Brown {
83087c85e80SJed Brown   PetscErrorCode         ierr;
83187c85e80SJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
83287c85e80SJed Brown   struct DMCompositeLink *link;
83387c85e80SJed Brown   PetscInt               cnt,start;
83487c85e80SJed Brown 
83587c85e80SJed Brown   PetscFunctionBegin;
83687c85e80SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83787c85e80SJed Brown   PetscValidPointer(is,2);
838785e854fSJed Brown   ierr = PetscMalloc1(com->nmine,is);CHKERRQ(ierr);
83906ebdd98SJed Brown   for (cnt=0,start=0,link=com->next; link; start+=link->nlocal,cnt++,link=link->next) {
840520db06cSJed Brown     PetscInt bs;
8419ae5db72SJed Brown     ierr = ISCreateStride(PETSC_COMM_SELF,link->nlocal,start,1,&(*is)[cnt]);CHKERRQ(ierr);
8421411c6eeSJed Brown     ierr = DMGetBlockSize(link->dm,&bs);CHKERRQ(ierr);
843520db06cSJed Brown     ierr = ISSetBlockSize((*is)[cnt],bs);CHKERRQ(ierr);
844520db06cSJed Brown   }
84587c85e80SJed Brown   PetscFunctionReturn(0);
84687c85e80SJed Brown }
84787c85e80SJed Brown 
84887c85e80SJed Brown #undef __FUNCT__
84947c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetGlobalISs"
85047c6ae99SBarry Smith /*@C
85147c6ae99SBarry Smith     DMCompositeGetGlobalISs - Gets the index sets for each composed object
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Collective on DMComposite
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Input Parameter:
85647c6ae99SBarry Smith .    dm - the packer object
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith     Output Parameters:
85947c6ae99SBarry Smith .    is - the array of index sets
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Level: advanced
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Notes:
86447c6ae99SBarry Smith        The is entries should be destroyed with ISDestroy(), the is array should be freed with PetscFree()
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith        These could be used to extract a subset of vector entries for a "multi-physics" preconditioner
86747c6ae99SBarry Smith 
8686eb61c8cSJed Brown        Use DMCompositeGetLocalISs() for index sets in the packed local numbering, and
8696eb61c8cSJed Brown        DMCompositeGetISLocalToGlobalMappings() for to map local sub-DM (including ghost) indices to packed global
8706eb61c8cSJed Brown        indices.
87147c6ae99SBarry Smith 
8729ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
87347c6ae99SBarry Smith          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetAccess(), DMCompositeScatter(),
87447c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(),DMCompositeGetEntries()
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith @*/
8776eb61c8cSJed Brown 
8787087cfbeSBarry Smith PetscErrorCode  DMCompositeGetGlobalISs(DM dm,IS *is[])
87947c6ae99SBarry Smith {
88047c6ae99SBarry Smith   PetscErrorCode         ierr;
88166bb578eSMark Adams   PetscInt               cnt = 0;
88247c6ae99SBarry Smith   struct DMCompositeLink *next;
88347c6ae99SBarry Smith   PetscMPIInt            rank;
88447c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith   PetscFunctionBegin;
88747c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
888785e854fSJed Brown   ierr = PetscMalloc1((com->nDM),is);CHKERRQ(ierr);
88947c6ae99SBarry Smith   next = com->next;
890ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
89347c6ae99SBarry Smith   while (next) {
89466bb578eSMark Adams     ierr = ISCreateStride(PetscObjectComm((PetscObject)dm),next->n,next->grstart,1,&(*is)[cnt]);CHKERRQ(ierr);
895*0f21e855SMatthew G. Knepley     if (dm->prob) {
89665c226d8SMatthew G. Knepley       MatNullSpace space;
89765c226d8SMatthew G. Knepley       Mat          pmat;
898*0f21e855SMatthew G. Knepley       PetscObject  disc;
899*0f21e855SMatthew G. Knepley       PetscInt     Nf;
90065c226d8SMatthew G. Knepley 
901*0f21e855SMatthew G. Knepley       ierr = PetscProblemGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
902*0f21e855SMatthew G. Knepley       if (cnt >= Nf) continue;
903*0f21e855SMatthew G. Knepley       ierr = PetscProblemGetDiscretization(dm->prob, cnt, &disc);CHKERRQ(ierr);
904*0f21e855SMatthew G. Knepley       ierr = PetscObjectQuery(disc, "nullspace", (PetscObject*) &space);CHKERRQ(ierr);
905aac2dd2dSMatthew G. Knepley       if (space) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "nullspace", (PetscObject) space);CHKERRQ(ierr);}
906*0f21e855SMatthew G. Knepley       ierr = PetscObjectQuery(disc, "nearnullspace", (PetscObject*) &space);CHKERRQ(ierr);
907aac2dd2dSMatthew G. Knepley       if (space) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "nearnullspace", (PetscObject) space);CHKERRQ(ierr);}
908*0f21e855SMatthew G. Knepley       ierr = PetscObjectQuery(disc, "pmat", (PetscObject*) &pmat);CHKERRQ(ierr);
909aac2dd2dSMatthew G. Knepley       if (pmat) {ierr = PetscObjectCompose((PetscObject) (*is)[cnt], "pmat", (PetscObject) pmat);CHKERRQ(ierr);}
91065c226d8SMatthew G. Knepley     }
91147c6ae99SBarry Smith     cnt++;
91247c6ae99SBarry Smith     next = next->next;
91347c6ae99SBarry Smith   }
91447c6ae99SBarry Smith   PetscFunctionReturn(0);
91547c6ae99SBarry Smith }
91647c6ae99SBarry Smith 
9174d343eeaSMatthew G Knepley #undef __FUNCT__
9184d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS_Composite"
91921c9b008SJed Brown PetscErrorCode DMCreateFieldIS_Composite(DM dm, PetscInt *numFields,char ***fieldNames, IS **fields)
9204d343eeaSMatthew G Knepley {
9214d343eeaSMatthew G Knepley   PetscInt       nDM;
9224d343eeaSMatthew G Knepley   DM             *dms;
9234d343eeaSMatthew G Knepley   PetscInt       i;
9244d343eeaSMatthew G Knepley   PetscErrorCode ierr;
9254d343eeaSMatthew G Knepley 
9264d343eeaSMatthew G Knepley   PetscFunctionBegin;
9274d343eeaSMatthew G Knepley   ierr = DMCompositeGetNumberDM(dm, &nDM);CHKERRQ(ierr);
9288865f1eaSKarl Rupp   if (numFields) *numFields = nDM;
9294d343eeaSMatthew G Knepley   ierr = DMCompositeGetGlobalISs(dm, fields);CHKERRQ(ierr);
9304d343eeaSMatthew G Knepley   if (fieldNames) {
931785e854fSJed Brown     ierr = PetscMalloc1(nDM, &dms);CHKERRQ(ierr);
932785e854fSJed Brown     ierr = PetscMalloc1(nDM, fieldNames);CHKERRQ(ierr);
9334d343eeaSMatthew G Knepley     ierr = DMCompositeGetEntriesArray(dm, dms);CHKERRQ(ierr);
9344d343eeaSMatthew G Knepley     for (i=0; i<nDM; i++) {
9354d343eeaSMatthew G Knepley       char       buf[256];
9364d343eeaSMatthew G Knepley       const char *splitname;
9374d343eeaSMatthew G Knepley 
9384d343eeaSMatthew G Knepley       /* Split naming precedence: object name, prefix, number */
9394d343eeaSMatthew G Knepley       splitname = ((PetscObject) dm)->name;
9404d343eeaSMatthew G Knepley       if (!splitname) {
9414d343eeaSMatthew G Knepley         ierr = PetscObjectGetOptionsPrefix((PetscObject)dms[i],&splitname);CHKERRQ(ierr);
9424d343eeaSMatthew G Knepley         if (splitname) {
9434d343eeaSMatthew G Knepley           size_t len;
9448caf3d72SBarry Smith           ierr                 = PetscStrncpy(buf,splitname,sizeof(buf));CHKERRQ(ierr);
9458caf3d72SBarry Smith           buf[sizeof(buf) - 1] = 0;
9464d343eeaSMatthew G Knepley           ierr                 = PetscStrlen(buf,&len);CHKERRQ(ierr);
9474d343eeaSMatthew G Knepley           if (buf[len-1] == '_') buf[len-1] = 0; /* Remove trailing underscore if it was used */
9484d343eeaSMatthew G Knepley           splitname = buf;
9494d343eeaSMatthew G Knepley         }
9504d343eeaSMatthew G Knepley       }
9514d343eeaSMatthew G Knepley       if (!splitname) {
9528caf3d72SBarry Smith         ierr      = PetscSNPrintf(buf,sizeof(buf),"%D",i);CHKERRQ(ierr);
9534d343eeaSMatthew G Knepley         splitname = buf;
9544d343eeaSMatthew G Knepley       }
95521c9b008SJed Brown       ierr = PetscStrallocpy(splitname,&(*fieldNames)[i]);CHKERRQ(ierr);
9564d343eeaSMatthew G Knepley     }
9574d343eeaSMatthew G Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
9584d343eeaSMatthew G Knepley   }
9594d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9604d343eeaSMatthew G Knepley }
9614d343eeaSMatthew G Knepley 
962e7c4fc90SDmitry Karpeev /*
963e7c4fc90SDmitry Karpeev  This could take over from DMCreateFieldIS(), as it is more general,
9640298fd71SBarry Smith  making DMCreateFieldIS() a special case -- calling with dmlist == NULL;
965e7c4fc90SDmitry Karpeev  At this point it's probably best to be less intrusive, however.
966e7c4fc90SDmitry Karpeev  */
967e7c4fc90SDmitry Karpeev #undef __FUNCT__
96816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition_Composite"
96916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition_Composite(DM dm, PetscInt *len,char ***namelist, IS **islist, DM **dmlist)
970e7c4fc90SDmitry Karpeev {
971e7c4fc90SDmitry Karpeev   PetscInt       nDM;
972e7c4fc90SDmitry Karpeev   PetscInt       i;
973e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
974e7c4fc90SDmitry Karpeev 
975e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
976e7c4fc90SDmitry Karpeev   ierr = DMCreateFieldIS_Composite(dm, len, namelist, islist);CHKERRQ(ierr);
977e7c4fc90SDmitry Karpeev   if (dmlist) {
978e7c4fc90SDmitry Karpeev     ierr = DMCompositeGetNumberDM(dm, &nDM);CHKERRQ(ierr);
979785e854fSJed Brown     ierr = PetscMalloc1(nDM, dmlist);CHKERRQ(ierr);
980e7c4fc90SDmitry Karpeev     ierr = DMCompositeGetEntriesArray(dm, *dmlist);CHKERRQ(ierr);
981e7c4fc90SDmitry Karpeev     for (i=0; i<nDM; i++) {
982e7c4fc90SDmitry Karpeev       ierr = PetscObjectReference((PetscObject)((*dmlist)[i]));CHKERRQ(ierr);
983e7c4fc90SDmitry Karpeev     }
984e7c4fc90SDmitry Karpeev   }
985e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
986e7c4fc90SDmitry Karpeev }
987e7c4fc90SDmitry Karpeev 
988e7c4fc90SDmitry Karpeev 
989e7c4fc90SDmitry Karpeev 
99047c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/
99147c6ae99SBarry Smith #undef __FUNCT__
99247c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetLocalVectors"
99347c6ae99SBarry Smith /*@C
9949ae5db72SJed Brown     DMCompositeGetLocalVectors - Gets local vectors for each part of a DMComposite.
99547c6ae99SBarry Smith        Use DMCompositeRestoreLocalVectors() to return them.
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith     Not Collective
99847c6ae99SBarry Smith 
99947c6ae99SBarry Smith     Input Parameter:
100047c6ae99SBarry Smith .    dm - the packer object
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Output Parameter:
10039ae5db72SJed Brown .   Vec ... - the individual sequential Vecs
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith     Level: advanced
100647c6ae99SBarry Smith 
10079ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
10086eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
100947c6ae99SBarry Smith          DMCompositeRestoreLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries()
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith @*/
10127087cfbeSBarry Smith PetscErrorCode  DMCompositeGetLocalVectors(DM dm,...)
101347c6ae99SBarry Smith {
101447c6ae99SBarry Smith   va_list                Argp;
101547c6ae99SBarry Smith   PetscErrorCode         ierr;
101647c6ae99SBarry Smith   struct DMCompositeLink *next;
101747c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith   PetscFunctionBegin;
102047c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
102147c6ae99SBarry Smith   next = com->next;
102247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
102347c6ae99SBarry Smith   va_start(Argp,dm);
102447c6ae99SBarry Smith   while (next) {
102547c6ae99SBarry Smith     Vec *vec;
102647c6ae99SBarry Smith     vec = va_arg(Argp, Vec*);
102706930112SJed Brown     if (vec) {ierr = DMGetLocalVector(next->dm,vec);CHKERRQ(ierr);}
102847c6ae99SBarry Smith     next = next->next;
102947c6ae99SBarry Smith   }
103047c6ae99SBarry Smith   va_end(Argp);
103147c6ae99SBarry Smith   PetscFunctionReturn(0);
103247c6ae99SBarry Smith }
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith #undef __FUNCT__
103547c6ae99SBarry Smith #define __FUNCT__ "DMCompositeRestoreLocalVectors"
103647c6ae99SBarry Smith /*@C
10379ae5db72SJed Brown     DMCompositeRestoreLocalVectors - Restores local vectors for each part of a DMComposite.
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith     Not Collective
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Input Parameter:
104247c6ae99SBarry Smith .    dm - the packer object
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith     Output Parameter:
10459ae5db72SJed Brown .   Vec ... - the individual sequential Vecs
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith     Level: advanced
104847c6ae99SBarry Smith 
10499ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(),
10506eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
105147c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeScatter(), DMCompositeGetEntries()
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith @*/
10547087cfbeSBarry Smith PetscErrorCode  DMCompositeRestoreLocalVectors(DM dm,...)
105547c6ae99SBarry Smith {
105647c6ae99SBarry Smith   va_list                Argp;
105747c6ae99SBarry Smith   PetscErrorCode         ierr;
105847c6ae99SBarry Smith   struct DMCompositeLink *next;
105947c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith   PetscFunctionBegin;
106247c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
106347c6ae99SBarry Smith   next = com->next;
106447c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
106547c6ae99SBarry Smith   va_start(Argp,dm);
106647c6ae99SBarry Smith   while (next) {
106747c6ae99SBarry Smith     Vec *vec;
106847c6ae99SBarry Smith     vec = va_arg(Argp, Vec*);
106906930112SJed Brown     if (vec) {ierr = DMRestoreLocalVector(next->dm,vec);CHKERRQ(ierr);}
107047c6ae99SBarry Smith     next = next->next;
107147c6ae99SBarry Smith   }
107247c6ae99SBarry Smith   va_end(Argp);
107347c6ae99SBarry Smith   PetscFunctionReturn(0);
107447c6ae99SBarry Smith }
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith /* -------------------------------------------------------------------------------------*/
107747c6ae99SBarry Smith #undef __FUNCT__
107847c6ae99SBarry Smith #define __FUNCT__ "DMCompositeGetEntries"
107947c6ae99SBarry Smith /*@C
10809ae5db72SJed Brown     DMCompositeGetEntries - Gets the DM for each entry in a DMComposite.
108147c6ae99SBarry Smith 
108247c6ae99SBarry Smith     Not Collective
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Input Parameter:
108547c6ae99SBarry Smith .    dm - the packer object
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Output Parameter:
10889ae5db72SJed Brown .   DM ... - the individual entries (DMs)
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith     Level: advanced
109147c6ae99SBarry Smith 
10922fa5ba8aSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), DMCompositeGetEntriesArray()
10936eb61c8cSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
109447c6ae99SBarry Smith          DMCompositeRestoreLocalVectors(), DMCompositeGetLocalVectors(),  DMCompositeScatter(),
109547c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors()
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith @*/
10987087cfbeSBarry Smith PetscErrorCode  DMCompositeGetEntries(DM dm,...)
109947c6ae99SBarry Smith {
110047c6ae99SBarry Smith   va_list                Argp;
110147c6ae99SBarry Smith   struct DMCompositeLink *next;
110247c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith   PetscFunctionBegin;
110547c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
110647c6ae99SBarry Smith   next = com->next;
110747c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
110847c6ae99SBarry Smith   va_start(Argp,dm);
110947c6ae99SBarry Smith   while (next) {
111047c6ae99SBarry Smith     DM *dmn;
111147c6ae99SBarry Smith     dmn = va_arg(Argp, DM*);
11129ae5db72SJed Brown     if (dmn) *dmn = next->dm;
111347c6ae99SBarry Smith     next = next->next;
111447c6ae99SBarry Smith   }
111547c6ae99SBarry Smith   va_end(Argp);
111647c6ae99SBarry Smith   PetscFunctionReturn(0);
111747c6ae99SBarry Smith }
111847c6ae99SBarry Smith 
111947c6ae99SBarry Smith #undef __FUNCT__
11202fa5ba8aSJed Brown #define __FUNCT__ "DMCompositeGetEntriesArray"
1121dbab29e1SMark F. Adams /*@C
11222fa5ba8aSJed Brown     DMCompositeGetEntriesArray - Gets the DM for each entry in a DMComposite.
11232fa5ba8aSJed Brown 
11242fa5ba8aSJed Brown     Not Collective
11252fa5ba8aSJed Brown 
11262fa5ba8aSJed Brown     Input Parameter:
11272fa5ba8aSJed Brown +    dm - the packer object
11282fa5ba8aSJed Brown -    dms - array of sufficient length (see DMCompositeGetNumberDM()), holds the DMs on output
11292fa5ba8aSJed Brown 
11302fa5ba8aSJed Brown     Level: advanced
11312fa5ba8aSJed Brown 
11322fa5ba8aSJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCreateGlobalVector(), DMCompositeGetEntries()
11332fa5ba8aSJed Brown          DMCompositeGather(), DMCompositeCreate(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess(),
11342fa5ba8aSJed Brown          DMCompositeRestoreLocalVectors(), DMCompositeGetLocalVectors(),  DMCompositeScatter(),
11352fa5ba8aSJed Brown          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors()
11362fa5ba8aSJed Brown 
11372fa5ba8aSJed Brown @*/
11382fa5ba8aSJed Brown PetscErrorCode DMCompositeGetEntriesArray(DM dm,DM dms[])
11392fa5ba8aSJed Brown {
11402fa5ba8aSJed Brown   struct DMCompositeLink *next;
11412fa5ba8aSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
11422fa5ba8aSJed Brown   PetscInt               i;
11432fa5ba8aSJed Brown 
11442fa5ba8aSJed Brown   PetscFunctionBegin;
11452fa5ba8aSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11462fa5ba8aSJed Brown   /* loop over packed objects, handling one at at time */
11472fa5ba8aSJed Brown   for (next=com->next,i=0; next; next=next->next,i++) dms[i] = next->dm;
11482fa5ba8aSJed Brown   PetscFunctionReturn(0);
11492fa5ba8aSJed Brown }
11502fa5ba8aSJed Brown 
11512fa5ba8aSJed Brown #undef __FUNCT__
11520c010503SBarry Smith #define __FUNCT__ "DMRefine_Composite"
11537087cfbeSBarry Smith PetscErrorCode  DMRefine_Composite(DM dmi,MPI_Comm comm,DM *fine)
115447c6ae99SBarry Smith {
115547c6ae99SBarry Smith   PetscErrorCode         ierr;
115647c6ae99SBarry Smith   struct DMCompositeLink *next;
115747c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dmi->data;
115847c6ae99SBarry Smith   DM                     dm;
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith   PetscFunctionBegin;
116147c6ae99SBarry Smith   PetscValidHeaderSpecific(dmi,DM_CLASSID,1);
1162ce94432eSBarry Smith   if (comm == MPI_COMM_NULL) {
1163ce94432eSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dmi,&comm);CHKERRQ(ierr);
1164ce94432eSBarry Smith   }
11652ce3a92bSJed Brown   ierr = DMSetUp(dmi);CHKERRQ(ierr);
116647c6ae99SBarry Smith   next = com->next;
116747c6ae99SBarry Smith   ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr);
116847c6ae99SBarry Smith 
116947c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
117047c6ae99SBarry Smith   while (next) {
117147c6ae99SBarry Smith     ierr = DMRefine(next->dm,comm,&dm);CHKERRQ(ierr);
117247c6ae99SBarry Smith     ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr);
117347c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
117447c6ae99SBarry Smith     next = next->next;
117547c6ae99SBarry Smith   }
117647c6ae99SBarry Smith   PetscFunctionReturn(0);
117747c6ae99SBarry Smith }
117847c6ae99SBarry Smith 
117914354c39SJed Brown #undef __FUNCT__
118014354c39SJed Brown #define __FUNCT__ "DMCoarsen_Composite"
118114354c39SJed Brown PetscErrorCode  DMCoarsen_Composite(DM dmi,MPI_Comm comm,DM *fine)
118214354c39SJed Brown {
118314354c39SJed Brown   PetscErrorCode         ierr;
118414354c39SJed Brown   struct DMCompositeLink *next;
118514354c39SJed Brown   DM_Composite           *com = (DM_Composite*)dmi->data;
118614354c39SJed Brown   DM                     dm;
118714354c39SJed Brown 
118814354c39SJed Brown   PetscFunctionBegin;
118914354c39SJed Brown   PetscValidHeaderSpecific(dmi,DM_CLASSID,1);
11902ce3a92bSJed Brown   ierr = DMSetUp(dmi);CHKERRQ(ierr);
11912ee06e3bSJed Brown   if (comm == MPI_COMM_NULL) {
119225296bd5SBarry Smith     ierr = PetscObjectGetComm((PetscObject)dmi,&comm);CHKERRQ(ierr);
119325296bd5SBarry Smith   }
119414354c39SJed Brown   next = com->next;
119514354c39SJed Brown   ierr = DMCompositeCreate(comm,fine);CHKERRQ(ierr);
119614354c39SJed Brown 
119714354c39SJed Brown   /* loop over packed objects, handling one at at time */
119814354c39SJed Brown   while (next) {
119914354c39SJed Brown     ierr = DMCoarsen(next->dm,comm,&dm);CHKERRQ(ierr);
120014354c39SJed Brown     ierr = DMCompositeAddDM(*fine,dm);CHKERRQ(ierr);
120114354c39SJed Brown     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
120214354c39SJed Brown     next = next->next;
120314354c39SJed Brown   }
120414354c39SJed Brown   PetscFunctionReturn(0);
120514354c39SJed Brown }
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith #undef __FUNCT__
1208e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation_Composite"
1209e727c939SJed Brown PetscErrorCode  DMCreateInterpolation_Composite(DM coarse,DM fine,Mat *A,Vec *v)
121047c6ae99SBarry Smith {
121147c6ae99SBarry Smith   PetscErrorCode         ierr;
12129ae5db72SJed Brown   PetscInt               m,n,M,N,nDM,i;
121347c6ae99SBarry Smith   struct DMCompositeLink *nextc;
121447c6ae99SBarry Smith   struct DMCompositeLink *nextf;
121525296bd5SBarry Smith   Vec                    gcoarse,gfine,*vecs;
121647c6ae99SBarry Smith   DM_Composite           *comcoarse = (DM_Composite*)coarse->data;
121747c6ae99SBarry Smith   DM_Composite           *comfine   = (DM_Composite*)fine->data;
12189ae5db72SJed Brown   Mat                    *mats;
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith   PetscFunctionBegin;
122147c6ae99SBarry Smith   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
122247c6ae99SBarry Smith   PetscValidHeaderSpecific(fine,DM_CLASSID,2);
1223f692024eSJed Brown   ierr = DMSetUp(coarse);CHKERRQ(ierr);
1224f692024eSJed Brown   ierr = DMSetUp(fine);CHKERRQ(ierr);
122547c6ae99SBarry Smith   /* use global vectors only for determining matrix layout */
12269ae5db72SJed Brown   ierr = DMGetGlobalVector(coarse,&gcoarse);CHKERRQ(ierr);
12279ae5db72SJed Brown   ierr = DMGetGlobalVector(fine,&gfine);CHKERRQ(ierr);
122847c6ae99SBarry Smith   ierr = VecGetLocalSize(gcoarse,&n);CHKERRQ(ierr);
122947c6ae99SBarry Smith   ierr = VecGetLocalSize(gfine,&m);CHKERRQ(ierr);
123047c6ae99SBarry Smith   ierr = VecGetSize(gcoarse,&N);CHKERRQ(ierr);
123147c6ae99SBarry Smith   ierr = VecGetSize(gfine,&M);CHKERRQ(ierr);
12329ae5db72SJed Brown   ierr = DMRestoreGlobalVector(coarse,&gcoarse);CHKERRQ(ierr);
12339ae5db72SJed Brown   ierr = DMRestoreGlobalVector(fine,&gfine);CHKERRQ(ierr);
123447c6ae99SBarry Smith 
12359ae5db72SJed Brown   nDM = comfine->nDM;
1236ce94432eSBarry Smith   if (nDM != comcoarse->nDM) SETERRQ2(PetscObjectComm((PetscObject)fine),PETSC_ERR_ARG_INCOMP,"Fine DMComposite has %D entries, but coarse has %D",nDM,comcoarse->nDM);
12371795a4d1SJed Brown   ierr = PetscCalloc1(nDM*nDM,&mats);CHKERRQ(ierr);
123825296bd5SBarry Smith   if (v) {
12391795a4d1SJed Brown     ierr = PetscCalloc1(nDM,&vecs);CHKERRQ(ierr);
124025296bd5SBarry Smith   }
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
12439ae5db72SJed Brown   for (nextc=comcoarse->next,nextf=comfine->next,i=0; nextc; nextc=nextc->next,nextf=nextf->next,i++) {
124425296bd5SBarry Smith     if (!v) {
12450298fd71SBarry Smith       ierr = DMCreateInterpolation(nextc->dm,nextf->dm,&mats[i*nDM+i],NULL);CHKERRQ(ierr);
124625296bd5SBarry Smith     } else {
124725296bd5SBarry Smith       ierr = DMCreateInterpolation(nextc->dm,nextf->dm,&mats[i*nDM+i],&vecs[i]);CHKERRQ(ierr);
124825296bd5SBarry Smith     }
124947c6ae99SBarry Smith   }
1250ce94432eSBarry Smith   ierr = MatCreateNest(PetscObjectComm((PetscObject)fine),nDM,NULL,nDM,NULL,mats,A);CHKERRQ(ierr);
125125296bd5SBarry Smith   if (v) {
1252ce94432eSBarry Smith     ierr = VecCreateNest(PetscObjectComm((PetscObject)fine),nDM,NULL,vecs,v);CHKERRQ(ierr);
125325296bd5SBarry Smith   }
12549ae5db72SJed Brown   for (i=0; i<nDM*nDM; i++) {ierr = MatDestroy(&mats[i]);CHKERRQ(ierr);}
12559ae5db72SJed Brown   ierr = PetscFree(mats);CHKERRQ(ierr);
125625296bd5SBarry Smith   if (v) {
125725296bd5SBarry Smith     for (i=0; i<nDM; i++) {ierr = VecDestroy(&vecs[i]);CHKERRQ(ierr);}
125825296bd5SBarry Smith     ierr = PetscFree(vecs);CHKERRQ(ierr);
125925296bd5SBarry Smith   }
126047c6ae99SBarry Smith   PetscFunctionReturn(0);
126147c6ae99SBarry Smith }
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith #undef __FUNCT__
1264184d77edSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping_Composite"
1265184d77edSJed Brown static PetscErrorCode DMGetLocalToGlobalMapping_Composite(DM dm)
12661411c6eeSJed Brown {
12671411c6eeSJed Brown   DM_Composite           *com = (DM_Composite*)dm->data;
12681411c6eeSJed Brown   ISLocalToGlobalMapping *ltogs;
1269f7efa3c7SJed Brown   PetscInt               i;
12701411c6eeSJed Brown   PetscErrorCode         ierr;
12711411c6eeSJed Brown 
12721411c6eeSJed Brown   PetscFunctionBegin;
12731411c6eeSJed Brown   /* Set the ISLocalToGlobalMapping on the new matrix */
12741411c6eeSJed Brown   ierr = DMCompositeGetISLocalToGlobalMappings(dm,&ltogs);CHKERRQ(ierr);
1275ce94432eSBarry Smith   ierr = ISLocalToGlobalMappingConcatenate(PetscObjectComm((PetscObject)dm),com->nDM,ltogs,&dm->ltogmap);CHKERRQ(ierr);
12769ae5db72SJed Brown   for (i=0; i<com->nDM; i++) {ierr = ISLocalToGlobalMappingDestroy(&ltogs[i]);CHKERRQ(ierr);}
12771411c6eeSJed Brown   ierr = PetscFree(ltogs);CHKERRQ(ierr);
12781411c6eeSJed Brown   PetscFunctionReturn(0);
12791411c6eeSJed Brown }
12801411c6eeSJed Brown 
12811411c6eeSJed Brown 
12821411c6eeSJed Brown #undef __FUNCT__
1283e727c939SJed Brown #define __FUNCT__ "DMCreateColoring_Composite"
1284b412c318SBarry Smith PetscErrorCode  DMCreateColoring_Composite(DM dm,ISColoringType ctype,ISColoring *coloring)
128547c6ae99SBarry Smith {
128647c6ae99SBarry Smith   PetscErrorCode  ierr;
128747c6ae99SBarry Smith   PetscInt        n,i,cnt;
128847c6ae99SBarry Smith   ISColoringValue *colors;
128947c6ae99SBarry Smith   PetscBool       dense  = PETSC_FALSE;
129047c6ae99SBarry Smith   ISColoringValue maxcol = 0;
129147c6ae99SBarry Smith   DM_Composite    *com   = (DM_Composite*)dm->data;
129247c6ae99SBarry Smith 
129347c6ae99SBarry Smith   PetscFunctionBegin;
129447c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1295ce94432eSBarry Smith   if (ctype == IS_COLORING_GHOSTED) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Only global coloring supported");
1296e3247f34SBarry Smith   else if (ctype == IS_COLORING_GLOBAL) {
129747c6ae99SBarry Smith     n = com->n;
1298ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Unknown ISColoringType");
1299785e854fSJed Brown   ierr = PetscMalloc1(n,&colors);CHKERRQ(ierr); /* freed in ISColoringDestroy() */
130047c6ae99SBarry Smith 
13010298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-dmcomposite_dense_jacobian",&dense,NULL);CHKERRQ(ierr);
130247c6ae99SBarry Smith   if (dense) {
130347c6ae99SBarry Smith     for (i=0; i<n; i++) {
130447c6ae99SBarry Smith       colors[i] = (ISColoringValue)(com->rstart + i);
130547c6ae99SBarry Smith     }
130647c6ae99SBarry Smith     maxcol = com->N;
130747c6ae99SBarry Smith   } else {
130847c6ae99SBarry Smith     struct DMCompositeLink *next = com->next;
130947c6ae99SBarry Smith     PetscMPIInt            rank;
131047c6ae99SBarry Smith 
1311ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
131247c6ae99SBarry Smith     cnt  = 0;
131347c6ae99SBarry Smith     while (next) {
131447c6ae99SBarry Smith       ISColoring lcoloring;
131547c6ae99SBarry Smith 
1316b412c318SBarry Smith       ierr = DMCreateColoring(next->dm,IS_COLORING_GLOBAL,&lcoloring);CHKERRQ(ierr);
131747c6ae99SBarry Smith       for (i=0; i<lcoloring->N; i++) {
131847c6ae99SBarry Smith         colors[cnt++] = maxcol + lcoloring->colors[i];
131947c6ae99SBarry Smith       }
132047c6ae99SBarry Smith       maxcol += lcoloring->n;
1321fcfd50ebSBarry Smith       ierr    = ISColoringDestroy(&lcoloring);CHKERRQ(ierr);
132247c6ae99SBarry Smith       next    = next->next;
132347c6ae99SBarry Smith     }
132447c6ae99SBarry Smith   }
1325ce94432eSBarry Smith   ierr = ISColoringCreate(PetscObjectComm((PetscObject)dm),maxcol,n,colors,coloring);CHKERRQ(ierr);
132647c6ae99SBarry Smith   PetscFunctionReturn(0);
132747c6ae99SBarry Smith }
132847c6ae99SBarry Smith 
132947c6ae99SBarry Smith #undef __FUNCT__
13300c010503SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin_Composite"
13317087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec)
133247c6ae99SBarry Smith {
133347c6ae99SBarry Smith   PetscErrorCode         ierr;
133447c6ae99SBarry Smith   struct DMCompositeLink *next;
133547c6ae99SBarry Smith   PetscInt               cnt = 3;
133647c6ae99SBarry Smith   PetscMPIInt            rank;
133747c6ae99SBarry Smith   PetscScalar            *garray,*larray;
133847c6ae99SBarry Smith   DM_Composite           *com = (DM_Composite*)dm->data;
133947c6ae99SBarry Smith 
134047c6ae99SBarry Smith   PetscFunctionBegin;
134147c6ae99SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134247c6ae99SBarry Smith   PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
134347c6ae99SBarry Smith   next = com->next;
134447c6ae99SBarry Smith   if (!com->setup) {
1345d7bf68aeSBarry Smith     ierr = DMSetUp(dm);CHKERRQ(ierr);
134647c6ae99SBarry Smith   }
1347ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
134847c6ae99SBarry Smith   ierr = VecGetArray(gvec,&garray);CHKERRQ(ierr);
134947c6ae99SBarry Smith   ierr = VecGetArray(lvec,&larray);CHKERRQ(ierr);
135047c6ae99SBarry Smith 
135147c6ae99SBarry Smith   /* loop over packed objects, handling one at at time */
135247c6ae99SBarry Smith   while (next) {
135347c6ae99SBarry Smith     Vec      local,global;
135447c6ae99SBarry Smith     PetscInt N;
135547c6ae99SBarry Smith 
135647c6ae99SBarry Smith     ierr = DMGetGlobalVector(next->dm,&global);CHKERRQ(ierr);
135747c6ae99SBarry Smith     ierr = VecGetLocalSize(global,&N);CHKERRQ(ierr);
135847c6ae99SBarry Smith     ierr = VecPlaceArray(global,garray);CHKERRQ(ierr);
135947c6ae99SBarry Smith     ierr = DMGetLocalVector(next->dm,&local);CHKERRQ(ierr);
136047c6ae99SBarry Smith     ierr = VecPlaceArray(local,larray);CHKERRQ(ierr);
136147c6ae99SBarry Smith     ierr = DMGlobalToLocalBegin(next->dm,global,mode,local);CHKERRQ(ierr);
136247c6ae99SBarry Smith     ierr = DMGlobalToLocalEnd(next->dm,global,mode,local);CHKERRQ(ierr);
136347c6ae99SBarry Smith     ierr = VecResetArray(global);CHKERRQ(ierr);
136447c6ae99SBarry Smith     ierr = VecResetArray(local);CHKERRQ(ierr);
136547c6ae99SBarry Smith     ierr = DMRestoreGlobalVector(next->dm,&global);CHKERRQ(ierr);
136647c6ae99SBarry Smith     ierr = DMRestoreGlobalVector(next->dm,&local);CHKERRQ(ierr);
136747c6ae99SBarry Smith     cnt++;
136806ebdd98SJed Brown     larray += next->nlocal;
136947c6ae99SBarry Smith     next    = next->next;
137047c6ae99SBarry Smith   }
137147c6ae99SBarry Smith 
13720298fd71SBarry Smith   ierr = VecRestoreArray(gvec,NULL);CHKERRQ(ierr);
13730298fd71SBarry Smith   ierr = VecRestoreArray(lvec,NULL);CHKERRQ(ierr);
137447c6ae99SBarry Smith   PetscFunctionReturn(0);
137547c6ae99SBarry Smith }
137647c6ae99SBarry Smith 
137747c6ae99SBarry Smith #undef __FUNCT__
13780c010503SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd_Composite"
13797087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd_Composite(DM dm,Vec gvec,InsertMode mode,Vec lvec)
13800c010503SBarry Smith {
13810c010503SBarry Smith   PetscFunctionBegin;
13820c010503SBarry Smith   PetscFunctionReturn(0);
13830c010503SBarry Smith }
138447c6ae99SBarry Smith 
13856ae3a549SBarry Smith /*MC
13866ae3a549SBarry Smith    DMCOMPOSITE = "composite" - A DM object that is used to manage data for a collection of DMs
13876ae3a549SBarry Smith 
13886ae3a549SBarry Smith   Level: intermediate
13896ae3a549SBarry Smith 
13906ae3a549SBarry Smith .seealso: DMType, DMCOMPOSITE, DMDACreate(), DMCreate(), DMSetType(), DMCompositeCreate()
13916ae3a549SBarry Smith M*/
13926ae3a549SBarry Smith 
13936ae3a549SBarry Smith 
1394a4121054SBarry Smith #undef __FUNCT__
1395a4121054SBarry Smith #define __FUNCT__ "DMCreate_Composite"
13968cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Composite(DM p)
1397a4121054SBarry Smith {
1398a4121054SBarry Smith   PetscErrorCode ierr;
1399a4121054SBarry Smith   DM_Composite   *com;
1400a4121054SBarry Smith 
1401a4121054SBarry Smith   PetscFunctionBegin;
1402b00a9115SJed Brown   ierr      = PetscNewLog(p,&com);CHKERRQ(ierr);
1403a4121054SBarry Smith   p->data   = com;
1404a4121054SBarry Smith   ierr      = PetscObjectChangeTypeName((PetscObject)p,"DMComposite");CHKERRQ(ierr);
1405a4121054SBarry Smith   com->n    = 0;
14060298fd71SBarry Smith   com->next = NULL;
1407a4121054SBarry Smith   com->nDM  = 0;
1408a4121054SBarry Smith 
1409a4121054SBarry Smith   p->ops->createglobalvector              = DMCreateGlobalVector_Composite;
1410a4121054SBarry Smith   p->ops->createlocalvector               = DMCreateLocalVector_Composite;
1411184d77edSJed Brown   p->ops->getlocaltoglobalmapping         = DMGetLocalToGlobalMapping_Composite;
14124d343eeaSMatthew G Knepley   p->ops->createfieldis                   = DMCreateFieldIS_Composite;
141316621825SDmitry Karpeev   p->ops->createfielddecomposition        = DMCreateFieldDecomposition_Composite;
1414a4121054SBarry Smith   p->ops->refine                          = DMRefine_Composite;
141514354c39SJed Brown   p->ops->coarsen                         = DMCoarsen_Composite;
141625296bd5SBarry Smith   p->ops->createinterpolation             = DMCreateInterpolation_Composite;
141725296bd5SBarry Smith   p->ops->creatematrix                    = DMCreateMatrix_Composite;
1418e727c939SJed Brown   p->ops->getcoloring                     = DMCreateColoring_Composite;
1419a4121054SBarry Smith   p->ops->globaltolocalbegin              = DMGlobalToLocalBegin_Composite;
1420a4121054SBarry Smith   p->ops->globaltolocalend                = DMGlobalToLocalEnd_Composite;
1421a4121054SBarry Smith   p->ops->destroy                         = DMDestroy_Composite;
1422a4121054SBarry Smith   p->ops->view                            = DMView_Composite;
1423a4121054SBarry Smith   p->ops->setup                           = DMSetUp_Composite;
1424a4121054SBarry Smith   PetscFunctionReturn(0);
1425a4121054SBarry Smith }
1426a4121054SBarry Smith 
14270c010503SBarry Smith #undef __FUNCT__
14280c010503SBarry Smith #define __FUNCT__ "DMCompositeCreate"
14290c010503SBarry Smith /*@C
14300c010503SBarry Smith     DMCompositeCreate - Creates a vector packer, used to generate "composite"
14310c010503SBarry Smith       vectors made up of several subvectors.
14320c010503SBarry Smith 
14330c010503SBarry Smith     Collective on MPI_Comm
143447c6ae99SBarry Smith 
143547c6ae99SBarry Smith     Input Parameter:
14360c010503SBarry Smith .   comm - the processors that will share the global vector
14370c010503SBarry Smith 
14380c010503SBarry Smith     Output Parameters:
14390c010503SBarry Smith .   packer - the packer object
144047c6ae99SBarry Smith 
144147c6ae99SBarry Smith     Level: advanced
144247c6ae99SBarry Smith 
14439ae5db72SJed Brown .seealso DMDestroy(), DMCompositeAddDM(), DMCompositeScatter(),
14446eb61c8cSJed Brown          DMCompositeGather(), DMCreateGlobalVector(), DMCompositeGetISLocalToGlobalMappings(), DMCompositeGetAccess()
144547c6ae99SBarry Smith          DMCompositeGetLocalVectors(), DMCompositeRestoreLocalVectors(), DMCompositeGetEntries()
144647c6ae99SBarry Smith 
144747c6ae99SBarry Smith @*/
14487087cfbeSBarry Smith PetscErrorCode  DMCompositeCreate(MPI_Comm comm,DM *packer)
144947c6ae99SBarry Smith {
14500c010503SBarry Smith   PetscErrorCode ierr;
14510c010503SBarry Smith 
145247c6ae99SBarry Smith   PetscFunctionBegin;
14530c010503SBarry Smith   PetscValidPointer(packer,2);
1454a4121054SBarry Smith   ierr = DMCreate(comm,packer);CHKERRQ(ierr);
1455a4121054SBarry Smith   ierr = DMSetType(*packer,DMCOMPOSITE);CHKERRQ(ierr);
145647c6ae99SBarry Smith   PetscFunctionReturn(0);
145747c6ae99SBarry Smith }
1458