xref: /petsc/src/dm/tutorials/ex25.c (revision 1e1ea65d8de51fde77ce8a787efbef25e407badc)
160c22052SBarry Smith 
260c22052SBarry Smith static char help[] = "Takes a patch of a large DMDA vector to one process.\n\n";
360c22052SBarry Smith 
460c22052SBarry Smith #include <petscdm.h>
560c22052SBarry Smith #include <petscdmda.h>
660c22052SBarry Smith #include <petscdmpatch.h>
760c22052SBarry Smith #include <petscsf.h>
860c22052SBarry Smith 
960c22052SBarry Smith typedef struct {
1060c22052SBarry Smith   PetscScalar x,y;
1160c22052SBarry Smith } Field;
1260c22052SBarry Smith 
1360c22052SBarry Smith int main(int argc,char **argv)
1460c22052SBarry Smith {
1560c22052SBarry Smith   Vec            xy,sxy;
1660c22052SBarry Smith   DM             da,sda = NULL;
1760c22052SBarry Smith   PetscSF        sf;
1860c22052SBarry Smith   PetscErrorCode ierr;
1960c22052SBarry Smith   PetscInt       m = 10, n = 10, dof = 2;
2060c22052SBarry Smith   MatStencil     lower = {0,3,2,0}, upper = {0,7,8,0}; /* These are in the order of the z, y, x, logical coordinates, the fourth entry is ignored */
2160c22052SBarry Smith   MPI_Comm       comm;
2260c22052SBarry Smith   PetscMPIInt    rank;
2360c22052SBarry Smith 
2460c22052SBarry Smith   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
2560c22052SBarry Smith 
2660c22052SBarry Smith   /* create the large DMDA and set coordindates (which we will copy down to the small DA). */
2760c22052SBarry Smith   ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,m,n,PETSC_DECIDE,PETSC_DECIDE,dof,1,0,0,&da);CHKERRQ(ierr);
2860c22052SBarry Smith   ierr = DMSetFromOptions(da);CHKERRQ(ierr);
2960c22052SBarry Smith   ierr = DMSetUp(da);CHKERRQ(ierr);
3060c22052SBarry Smith   ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr);
3160c22052SBarry Smith   /* Just as a simple example we use the coordinates as the variables in the vectors we wish to examine. */
3260c22052SBarry Smith   ierr = DMGetCoordinates(da,&xy);CHKERRQ(ierr);
3360c22052SBarry Smith   /* The vector entries are displayed in the "natural" ordering on the two dimensional grid; interlaced x and y with with the x variable increasing more rapidly than the y */
3460c22052SBarry Smith   ierr = VecView(xy,0);CHKERRQ(ierr);
3560c22052SBarry Smith 
3655b25c41SPierre Jolivet   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
3760c22052SBarry Smith   if (!rank) comm = MPI_COMM_SELF;
3860c22052SBarry Smith   else comm = MPI_COMM_NULL;
3960c22052SBarry Smith 
4060c22052SBarry Smith   ierr = DMPatchZoom(da,lower,upper,comm,&sda, NULL,&sf);CHKERRQ(ierr);
4160c22052SBarry Smith   if (!rank) {
4260c22052SBarry Smith     ierr = DMCreateGlobalVector(sda,&sxy);CHKERRQ(ierr);
4360c22052SBarry Smith   } else {
44*1e1ea65dSPierre Jolivet     ierr = VecCreateSeq(PETSC_COMM_SELF,0,&sxy);CHKERRQ(ierr);
4560c22052SBarry Smith   }
4660c22052SBarry Smith   /*  A PetscSF can also be used as a VecScatter context */
4760c22052SBarry Smith   ierr = VecScatterBegin(sf,xy,sxy,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4860c22052SBarry Smith   ierr = VecScatterEnd(sf,xy,sxy,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4960c22052SBarry Smith   /* Only rank == 0 has the entries of the patch, so run code only at that rank */
5060c22052SBarry Smith   if (!rank) {
5160c22052SBarry Smith     Field         **vars;
5260c22052SBarry Smith     DMDALocalInfo info;
5360c22052SBarry Smith     PetscInt      i,j;
5460c22052SBarry Smith     PetscScalar   sum = 0;
5560c22052SBarry Smith 
5660c22052SBarry Smith     /* The vector entries of the patch are displayed in the "natural" ordering on the two grid; interlaced x and y with with the x variable increasing more rapidly */
5760c22052SBarry Smith     ierr = VecView(sxy,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
5860c22052SBarry Smith     /* Compute some trivial statistic of the coordinates */
5960c22052SBarry Smith     ierr = DMDAGetLocalInfo(sda,&info);CHKERRQ(ierr);
6060c22052SBarry Smith     ierr = DMDAVecGetArray(sda,sxy,&vars);CHKERRQ(ierr);
6160c22052SBarry Smith     /* Loop over the patch of the entire domain */
6260c22052SBarry Smith     for (j=info.ys; j<info.ys+info.ym; j++) {
6360c22052SBarry Smith       for (i=info.xs; i<info.xs+info.xm; i++) {
6460c22052SBarry Smith         sum += vars[j][i].x;
6560c22052SBarry Smith       }
6660c22052SBarry Smith     }
6760c22052SBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF,"The sum of the x coordinates is %g\n",(double)PetscRealPart(sum));CHKERRQ(ierr);
6860c22052SBarry Smith     ierr = DMDAVecRestoreArray(sda,sxy,&vars);CHKERRQ(ierr);
6960c22052SBarry Smith   }
7060c22052SBarry Smith 
7160c22052SBarry Smith   ierr = VecDestroy(&sxy);CHKERRQ(ierr);
7260c22052SBarry Smith   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
7360c22052SBarry Smith   ierr = DMDestroy(&sda);CHKERRQ(ierr);
7460c22052SBarry Smith   ierr = DMDestroy(&da);CHKERRQ(ierr);
7560c22052SBarry Smith   ierr = PetscFinalize();
7660c22052SBarry Smith   return ierr;
7760c22052SBarry Smith }
7860c22052SBarry Smith 
7960c22052SBarry Smith /*TEST
8060c22052SBarry Smith 
8160c22052SBarry Smith    test:
8260c22052SBarry Smith 
8360c22052SBarry Smith    test:
8460c22052SBarry Smith      nsize: 4
8560c22052SBarry Smith      suffix: 2
8660c22052SBarry Smith 
8760c22052SBarry Smith TEST*/
88