Actual source code: ex1.c

  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: {
 10:   DM        dmstag;
 11:   PetscInt  dim;
 12:   PetscBool setSizes;

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

 22:   /* Creation (normal) */
 23:   if (!setSizes) {
 24:     switch (dim) {
 25:     case 1:
 26:       PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 3, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, &dmstag));
 27:       break;
 28:     case 2:
 29:       PetscCall(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:       PetscCall(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:       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "No support for dimension %" PetscInt_FMT, 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:     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 45:     switch (dim) {
 46:     case 1:
 47:       PetscCheck(size == ranksx, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Must run on %" PetscInt_FMT " ranks with -dim 1 -setSizes", ranksx);
 48:       PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, mx, 1, 1, DMSTAG_STENCIL_BOX, 1, lx, &dmstag));
 49:       break;
 50:     case 2:
 51:       PetscCheck(size == ranksx * ranksy, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Must run on %" PetscInt_FMT " ranks with -dim 2 -setSizes", ranksx * ranksy);
 52:       PetscCall(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:       PetscCheck(size == ranksx * ranksy * ranksz, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Must run on %" PetscInt_FMT " ranks with -dim 3 -setSizes", ranksx * ranksy * ranksz);
 56:       PetscCall(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:       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "No support for dimension %" PetscInt_FMT, dim);
 60:     }
 61:   }

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

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

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

 76: static PetscErrorCode TestFields(DM dmstag)
 77: {
 78:   Vec       vecLocal, vecGlobal;
 79:   PetscReal norm2;

 81:   PetscFunctionBeginUser;
 82:   PetscCall(DMCreateLocalVector(dmstag, &vecLocal));
 83:   PetscCall(DMCreateGlobalVector(dmstag, &vecGlobal));
 84:   PetscCall(VecSet(vecLocal, 1.0));
 85:   PetscCall(DMLocalToGlobalBegin(dmstag, vecLocal, INSERT_VALUES, vecGlobal));
 86:   PetscCall(DMLocalToGlobalEnd(dmstag, vecLocal, INSERT_VALUES, vecGlobal));
 87:   PetscCall(VecSet(vecGlobal, 2.0));
 88:   PetscCall(DMGlobalToLocalBegin(dmstag, vecGlobal, INSERT_VALUES, vecLocal));
 89:   PetscCall(DMGlobalToLocalEnd(dmstag, vecGlobal, INSERT_VALUES, vecLocal));
 90:   PetscCall(VecNorm(vecGlobal, NORM_2, &norm2));
 91:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "2 Norm of test vector: %g\n", (double)norm2));
 92:   PetscCall(VecDestroy(&vecLocal));
 93:   PetscCall(VecDestroy(&vecGlobal));
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 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:
170:       suffix: ghosted_zerowidth_seq_1
171:       nsize: 1
172:       args:  -dm_view -dim 1 -stag_grid_x 4 -stag_boundary_type_x ghosted -stag_stencil_width 0

174:    test:
175:       suffix: ghosted_zerowidth_par_1
176:       nsize: 3
177:       args:  -dm_view -dim 1 -setsizes -stag_boundary_type_x ghosted -stag_stencil_width 0

179:    test:
180:       suffix: ghosted_zerowidth_seq_2
181:       nsize: 1
182:       args:  -dm_view -dim 2 -stag_grid_x 3 -stag_grid_y 5 -stag_boundary_type_x ghosted -stag_boundary_type_y ghosted -stag_stencil_width 0

184:    test:
185:       suffix: ghosted_zerowidth_par_2
186:       nsize: 6
187:       args:  -dm_view -dim 2 -setsizes -stag_boundary_type_x ghosted -stag_boundary_type_y ghosted -stag_stencil_width 0

189:    test:
190:       suffix: ghosted_zerowidth_seq_3
191:       nsize: 1
192:       args:  -dm_view -dim 3 -stag_grid_x 3 -stag_grid_y 5 -stag_grid_z 4 -stag_boundary_type_x ghosted -stag_boundary_type_y ghosted -stag_boundary_type_z ghosted -stag_stencil_width 0

194:    test:
195:       suffix: ghosted_zerowidth_par_3
196:       nsize: 12
197:       args:  -dm_view -dim 3 -setsizes -stag_boundary_type_x ghosted -stag_boundary_type_y ghosted -stag_boundary_type_z ghosted -stag_stencil_width 0

199:    testset:
200:       suffix: periodic_skinny_1
201:       nsize: 1
202:       args:  -dm_view -dim 1 -stag_grid_x 4 -stag_boundary_type_x periodic -stag_stencil_width {{0 1 2}separate output}

204:    testset:
205:       suffix: periodic_skinny_2
206:       nsize: 1
207:       args:  -dm_view -dim 2 -stag_grid_x 4 -stag_grid_y 5 -stag_boundary_type_x periodic -stag_boundary_type_y periodic -stag_stencil_width {{0 1 2}separate output}

209:    testset:
210:       suffix: periodic_skinny_3
211:       nsize: 1
212:       args:  -dm_view -dim 3 -stag_grid_x 4 -stag_grid_y 5 -stag_grid_z 3 -stag_boundary_type_x periodic -stag_boundary_type_y periodic -stag_boundary_type_z periodic -stag_stencil_width {{0 1 2}separate output}

214: TEST*/