Actual source code: ex1.c

petsc-3.11.3 2019-06-26
Report Typos and Errors
  1: static char help[] = "Demonstrate standard DMStag operations.\n\n";

  3:  #include <petscdm.h>
  4:  #include <petscdmstag.h>

  6: static PetscErrorCode TestFields(DM dmstag);

  8: int main(int argc,char **argv)
  9: {
 11:   DM             dmstag;
 12:   PetscInt       dim;
 13:   PetscBool      setSizes;

 15:   /* Initialize PETSc and process command line arguments */
 16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 17:   dim = 2;
 18:   PetscOptionsGetInt(NULL,NULL,"-dim",&dim,NULL);
 19:   setSizes = PETSC_FALSE;
 20:   PetscOptionsGetBool(NULL,NULL,"-setsizes",&setSizes,NULL);

 22:   /* Creation (normal) */
 23:   if (!setSizes) {
 24:     switch (dim) {
 25:       case 1:
 26:         DMStagCreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,3,1,1,DMSTAG_STENCIL_BOX,1,NULL,&dmstag);
 27:         break;
 28:       case 2:
 29:         DMStagCreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,3,2,PETSC_DECIDE,PETSC_DECIDE,1,1,1,DMSTAG_STENCIL_BOX,1,NULL,NULL,&dmstag);
 30:         break;
 31:       case 3:
 32:         DMStagCreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,3,2,4,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,1,1,DMSTAG_STENCIL_BOX,1,NULL,NULL,NULL,&dmstag);
 33:         break;
 34:       default:
 35:         SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No support for dimension %D",dim);
 36:     }
 37:   } else {
 38:     /* Creation (test providing decomp exactly)*/
 39:     PetscMPIInt size;
 40:     PetscInt lx[4] = {1,2,3}, ranksx = 3, mx = 6;
 41:     PetscInt ly[3] = {4,5},   ranksy = 2, my = 9;
 42:     PetscInt lz[2] = {6,7},   ranksz = 2, mz = 13;

 44:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 45:     switch (dim) {
 46:       case 1:
 47:         if (size != ranksx) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Must run on %D ranks with -dim 1 -setSizes",ranksx);
 48:         DMStagCreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,mx,1,1,DMSTAG_STENCIL_BOX,1,lx,&dmstag);
 49:         break;
 50:       case 2:
 51:         if (size != ranksx * ranksy) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Must run on %D ranks with -dim 2 -setSizes",ranksx * ranksy);
 52:         DMStagCreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,mx,my,ranksx,ranksy,1,1,1,DMSTAG_STENCIL_BOX,1,lx,ly,&dmstag);
 53:         break;
 54:       case 3:
 55:         if (size != ranksx * ranksy * ranksz) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Must run on %D ranks with -dim 3 -setSizes", ranksx * ranksy * ranksz);
 56:         DMStagCreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,mx,my,mz,ranksx,ranksy,ranksz,1,1,1,1,DMSTAG_STENCIL_BOX,1,lx,ly,lz,&dmstag);
 57:         break;
 58:       default:
 59:         SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No support for dimension %D",dim);
 60:     }
 61:   }

 63:   /* Setup */
 64:   DMSetFromOptions(dmstag);
 65:   DMSetUp(dmstag);

 67:   /* Field Creation */
 68:   TestFields(dmstag);

 70:   /* Clean up and finalize PETSc */
 71:   DMDestroy(&dmstag);
 72:   PetscFinalize();
 73:   return ierr;
 74: }

 76: static PetscErrorCode TestFields(DM dmstag)
 77: {
 79:   Vec            vecLocal,vecGlobal;
 80:   PetscReal      norm2;
 82:   DMCreateLocalVector(dmstag,&vecLocal);
 83:   DMCreateGlobalVector(dmstag,&vecGlobal);
 84:   VecSet(vecLocal,1.0);
 85:   DMLocalToGlobalBegin(dmstag,vecLocal,INSERT_VALUES,vecGlobal);
 86:   DMLocalToGlobalEnd  (dmstag,vecLocal,INSERT_VALUES,vecGlobal);
 87:   VecSet(vecGlobal,2.0);
 88:   DMGlobalToLocalBegin(dmstag,vecGlobal,INSERT_VALUES,vecLocal);
 89:   DMGlobalToLocalEnd  (dmstag,vecGlobal,INSERT_VALUES,vecLocal);
 90:   VecNorm(vecGlobal,NORM_2,&norm2);
 91:   PetscPrintf(PETSC_COMM_WORLD,"2 Norm of test vector: %g\n",(double)norm2);
 92:   VecDestroy(&vecLocal);
 93:   VecDestroy(&vecGlobal);
 94:   return(0);
 95: }

 97: /*TEST

 99:    test:
100:       suffix: basic_1
101:       nsize: 8
102:       args: -dm_view -dim 1 -stag_grid_x 37 -stag_stencil_type none -stag_stencil_width 2

104:    test:
105:       suffix: basic_2
106:       nsize: 14
107:       args: -dm_view -dim 2 -stag_grid_x 11 -stag_grid_y 7 -stag_stencil_type star

109:    test:
110:       suffix: basic_3
111:       nsize: 27
112:       args: -dm_view -dim 3 -stag_grid_x 4 -stag_grid_y 5 -stag_grid_z 6 -stag_stencil_type star -stag_ranks_x 3 -stag_ranks_y 3 -stag_ranks_z 3

114:    test:
115:       suffix: multidof_1
116:       nsize: 3
117:       args: -dm_view -dim 1 -stag_dof_0 2 -stag_dof_1 7

119:    test:
120:       suffix: multidof_2
121:       nsize: 9
122:       args: -dm_view -dim 2 -stag_grid_x 3 -stag_grid_y 3 -stag_dof_0 3 -stag_dof_1 4 -stag_dof_2 5

124:    test:
125:       suffix: multidof_3
126:       nsize: 27
127:       args: -dm_view -dim 3 -stag_grid_x 6 -stag_grid_y 5 -stag_grid_z 4 -stag_ranks_x 3 -stag_ranks_y 3 -stag_ranks_z 3 -stag_dof_0 3 -stag_dof_1 4 -stag_dof_2 2 -stag_dof_3 5

129:    test:
130:       suffix: zerodof_1
131:       nsize: 3
132:       args: -dm_view -dim 1 -stag_dof_0 0 -stag_dof_1 0

134:    test:
135:       suffix: zerodof_2
136:       nsize: 9
137:       args: -dm_view -dim 2 -stag_grid_x 3 -stag_grid_y 3 -stag_dof_0 0 -stag_dof_1 0 -stag_dof_2 0

139:    test:
140:       suffix: zerodof_3
141:       nsize: 27
142:       args: -dm_view -dim 3 -stag_grid_x 4 -stag_grid_y 5 -stag_grid_z 6 -stag_ranks_x 3 -stag_ranks_y 3 -stag_ranks_z 3 -stag_dof_0 0 -stag_dof_1 4 -stag_dof_2 0 -stag_dof_3 0

144:    test:
145:       suffix: sizes_1
146:       nsize: 3
147:       args: -dm_view -dim 1 -setSizes

149:    test:
150:       suffix: sizes_2
151:       nsize: 6
152:       args: -dm_view -dim 2 -setSizes

154:    test:
155:       suffix: sizes_3
156:       nsize: 12
157:       args: -dm_view -dim 3 -setSizes

159:    test:
160:       suffix: stencil_none_1
161:       nsize: 6
162:       args:  -dm_view -dim 2 -stag_grid_x 4 -stag_grid_y 5 -stag_stencil_type none -stag_stencil_width 0

164:    test:
165:       suffix: stencil_none_2
166:       nsize: 8
167:       args:  -dm_view -dim 3 -stag_grid_x 4 -stag_grid_y 5 -stag_grid_z 3 -stag_stencil_type none -stag_stencil_width 0

169: TEST*/