Actual source code: ex22.c
petsc-3.11.4 2019-09-28
2: static char help[] = "Tests MatSetValuesBlockedStencil() in 3d.\n\n";
4: #include <petscmat.h>
5: #include <petscdm.h>
6: #include <petscdmda.h>
8: int main(int argc,char **argv)
9: {
10: PetscInt M = 3,N = 4,P = 2,s = 1,w = 2,i, m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
11: PetscErrorCode ierr;
12: DM da;
13: Mat mat;
14: DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
15: PetscBool flg = PETSC_FALSE;
16: MatStencil idx[2],idy[2];
17: PetscScalar *values;
19: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
20: PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
21: PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
22: PetscOptionsGetInt(NULL,NULL,"-P",&P,NULL);
23: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
24: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
25: PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL);
26: PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);
27: PetscOptionsGetInt(NULL,NULL,"-w",&w,NULL);
28: PetscOptionsGetBool(NULL,NULL,"-star",&flg,NULL);
29: if (flg) stencil_type = DMDA_STENCIL_STAR;
31: /* Create distributed array and get vectors */
32: DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,stencil_type,M,N,P,m,n,p,w,s,0,0,0,&da);
33: DMSetFromOptions(da);
34: DMSetUp(da);
35: DMSetMatType(da,MATMPIBAIJ);
36: DMCreateMatrix(da,&mat);
38: idx[0].i = 1; idx[0].j = 1; idx[0].k = 0;
39: idx[1].i = 2; idx[1].j = 1; idx[1].k = 0;
40: idy[0].i = 1; idy[0].j = 2; idy[0].k = 0;
41: idy[1].i = 2; idy[1].j = 2; idy[1].k = 0;
42: PetscMalloc1(2*2*w*w,&values);
43: for (i=0; i<2*2*w*w; i++) values[i] = i;
44: MatSetValuesBlockedStencil(mat,2,idx,2,idy,values,INSERT_VALUES);
45: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
46: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
48: /* Free memory */
49: PetscFree(values);
50: MatDestroy(&mat);
51: DMDestroy(&da);
52: PetscFinalize();
53: return ierr;
54: }