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