Actual source code: ex21.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  1: static const char help[] = "Test DMCreateInjection() for mapping coordinates in 3D";

  3: #include <petscvec.h>
  4: #include <petscmat.h>
  5: #include <petscdm.h>
  6: #include <petscdmda.h>

 10: PetscErrorCode test1_DAInjection3d(PetscInt mx, PetscInt my, PetscInt mz)
 11: {
 12:   PetscErrorCode   ierr;
 13:   DM               dac,daf;
 14:   PetscViewer      vv;
 15:   Vec              ac,af;
 16:   PetscInt         periodicity;
 17:   DMBoundaryType   bx,by,bz;

 20:   bx = DM_BOUNDARY_NONE;
 21:   by = DM_BOUNDARY_NONE;
 22:   bz = DM_BOUNDARY_NONE;

 24:   periodicity = 0;

 26:   PetscOptionsGetInt(NULL,NULL,"-periodic", &periodicity, NULL);
 27:   if (periodicity==1) {
 28:     bx = DM_BOUNDARY_PERIODIC;
 29:   } else if (periodicity==2) {
 30:     by = DM_BOUNDARY_PERIODIC;
 31:   } else if (periodicity==3) {
 32:     bz = DM_BOUNDARY_PERIODIC;
 33:   }

 35:   DMDACreate3d(PETSC_COMM_WORLD, bx,by,bz, DMDA_STENCIL_BOX,
 36:                       mx+1, my+1,mz+1,
 37:                       PETSC_DECIDE, PETSC_DECIDE,PETSC_DECIDE,
 38:                       1, /* 1 dof */
 39:                       1, /* stencil = 1 */
 40:                       NULL,NULL,NULL,
 41:                       &daf);

 43:   DMSetFromOptions(daf);

 45:   DMCoarsen(daf,MPI_COMM_NULL,&dac);

 47:   DMDASetUniformCoordinates(dac, -1.0,1.0, -1.0,1.0, -1.0,1.0);
 48:   DMDASetUniformCoordinates(daf, -1.0,1.0, -1.0,1.0, -1.0,1.0);

 50:   {
 51:     DM         cdaf,cdac;
 52:     Vec        coordsc,coordsf,coordsf2;
 53:     Mat        inject;
 54:     VecScatter vscat;
 55:     Mat        interp;
 56:     PetscReal  norm;

 58:     DMGetCoordinateDM(dac,&cdac);
 59:     DMGetCoordinateDM(daf,&cdaf);

 61:     DMGetCoordinates(dac,&coordsc);
 62:     DMGetCoordinates(daf,&coordsf);

 64:     DMCreateInjection(cdac,cdaf,&inject);
 65:     MatScatterGetVecScatter(inject,&vscat);
 66:     VecScatterBegin(vscat,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
 67:     VecScatterEnd(vscat  ,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
 68:     MatDestroy(&inject);

 70:     DMCreateInterpolation(cdac,cdaf,&interp,NULL);
 71:     VecDuplicate(coordsf,&coordsf2);
 72:     MatInterpolate(interp,coordsc,coordsf2);
 73:     VecAXPY(coordsf2,-1.0,coordsf);
 74:     VecNorm(coordsf2,NORM_MAX,&norm);
 75:     /* The fine coordinates are only reproduced in certain cases */
 76:     if (!bx && !by && !bz && norm > PETSC_SQRT_MACHINE_EPSILON) {PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm);}
 77:     VecDestroy(&coordsf2);
 78:     MatDestroy(&interp);
 79:   }

 81:   if (0) {
 82:     DMCreateGlobalVector(dac,&ac);
 83:     VecZeroEntries(ac);

 85:     DMCreateGlobalVector(daf,&af);
 86:     VecZeroEntries(af);

 88:     PetscViewerASCIIOpen(PETSC_COMM_WORLD, "dac_7.vtk", &vv);
 89:     PetscViewerPushFormat(vv, PETSC_VIEWER_ASCII_VTK);
 90:     DMView(dac, vv);
 91:     VecView(ac, vv);
 92:     PetscViewerPopFormat(vv);
 93:     PetscViewerDestroy(&vv);

 95:     PetscViewerASCIIOpen(PETSC_COMM_WORLD, "daf_7.vtk", &vv);
 96:     PetscViewerPushFormat(vv, PETSC_VIEWER_ASCII_VTK);
 97:     DMView(daf, vv);
 98:     VecView(af, vv);
 99:     PetscViewerPopFormat(vv);
100:     PetscViewerDestroy(&vv);
101:     VecDestroy(&ac);
102:     VecDestroy(&af);
103:   }
104:   DMDestroy(&dac);
105:   DMDestroy(&daf);
106:   return(0);
107: }

111: int main(int argc,char **argv)
112: {
114:   PetscInt       mx,my,mz;

116:   PetscInitialize(&argc,&argv,0,help);
117:   mx   = 2;
118:   my   = 2;
119:   mz   = 2;
120:   PetscOptionsGetInt(NULL,NULL,"-mx", &mx, 0);
121:   PetscOptionsGetInt(NULL,NULL,"-my", &my, 0);
122:   PetscOptionsGetInt(NULL,NULL,"-mz", &mz, 0);

124:   test1_DAInjection3d(mx,my,mz);

126:   PetscFinalize();
127:   return 0;
128: }